home · contact · privacy
e825755b86a991b1b823756038d1df0a4181ec89
[plomrogue] / src / map_objects.c
1 /* map_objects.c */
2
3 #include "map_objects.h"
4 #include <stdlib.h> /* for free(), atoi() */
5 #include <stdint.h> /* for uint8_t */
6 #include <stdio.h> /* for FILE typedef */
7 #include <string.h> /* for strchr(), strlen(), memcpy(), strtok() */
8 #include "readwrite.h" /* for get_linemax(), try_fopen(), try_fclose()
9                         * [read/write]_uint[8/16/23][_bigendian]()
10                         */
11 #include "misc.h" /* for try_malloc(), try_calloc(), find_passable_pos() */
12 #include "main.h" /* for World struct */
13 #include "rexit.h" /* for err_exit() */
14
15
16
17 extern void init_map_object_defs(struct World * world, char * filename)
18 {
19     char * f_name = "init_map_object_defs()";
20     FILE * file = try_fopen(filename, "r", world, f_name);
21     uint16_t linemax = get_linemax(file, world, f_name);
22     struct MapObjDef ** last_mod_ptr_ptr = &world->map_obj_defs;
23     char * delim = " ";
24     char line[linemax + 1];
25     while (try_fgets(line, linemax + 1, file, world, f_name))
26     {
27         struct MapObjDef * mod;
28         mod = try_malloc(sizeof(struct MapObjDef), world, f_name);
29         mod->next = NULL;
30         mod->id = atoi(strtok(line, delim));
31         mod->corpse_id = atoi(strtok(NULL, delim));
32         mod->char_on_map = * strtok(NULL, delim);
33         mod->lifepoints = atoi(strtok(NULL, delim));
34         char * name = strtok(NULL, "\n");
35         mod->name = try_malloc(strlen(name) + 1, world, f_name);
36         memcpy(mod->name, name, strlen(name) + 1);
37         * last_mod_ptr_ptr = mod;
38         last_mod_ptr_ptr = &mod->next;
39     }
40     try_fclose(file, world, f_name);
41 }
42
43
44
45 extern void free_map_object_defs(struct MapObjDef * mod_start)
46 {
47     if (NULL == mod_start)
48     {
49         return;
50     }
51     free_map_object_defs(mod_start->next);
52     free(mod_start->name);
53     free(mod_start);
54 }
55
56
57
58 extern void write_map_objects(struct World * world, FILE * file)
59 {
60     char * f_name = "write_map_objects()";
61     struct MapObj * mo = world->map_objs;
62     uint8_t size = 3 + 1 + 3 + 1 + 3 + 1 + 5 + 1 + 5 + 1;
63     char line[size];
64     while (NULL != mo)
65     {
66         sprintf(line, "%d %d %d %d %d\n",
67                 mo->id, mo->type, mo->lifepoints, mo->pos.y, mo->pos.x);
68         try_fwrite(line, strlen(line), 1, file, world, f_name);
69         mo = mo->next;
70     }
71 }
72
73
74
75 extern void read_map_objects(struct World * world, FILE * file, char * line,
76                               int linemax)
77 {
78     char * f_name = "read_map_objects()";
79     struct MapObj ** mo_ptr_ptr = &world->map_objs;
80     char * delim = " ";
81     struct MapObj * mo;
82     while (try_fgets(line, linemax + 1, file, world, f_name))
83     {
84         mo = malloc(sizeof(struct MapObj));
85         mo->next = NULL;
86         mo->id = atoi(strtok(line, delim));
87         if (mo->id > world->map_obj_count)
88         {
89             world->map_obj_count = mo->id;
90         }
91         mo->type = atoi(strtok(NULL, delim));
92         mo->lifepoints = atoi(strtok(NULL, delim));
93         mo->pos.y = atoi(strtok(NULL, delim));
94         mo->pos.x = atoi(strtok(NULL, delim));
95         * mo_ptr_ptr = mo;
96         mo_ptr_ptr = &mo->next;
97     }
98     world->last_map_obj = mo;
99 }
100
101
102
103 extern void add_map_object(struct World * world, uint8_t type)
104 {
105     char * f_name = "add_map_object";
106     struct MapObjDef * mod = get_map_object_def(world, type);
107     struct MapObj * mo = try_malloc(sizeof(struct MapObj), world, f_name);
108     mo->id = world->map_obj_count;
109     world->map_obj_count++;
110     mo->type = mod->id;
111     mo->lifepoints = mod->lifepoints;
112     mo->pos = find_passable_pos(world->map);
113     mo->next = NULL;
114     if (NULL == world->last_map_obj)
115     {
116         world->map_objs = mo;
117     }
118     else
119     {
120         world->last_map_obj->next = mo;
121     }
122     world->last_map_obj = mo;
123 }
124
125
126
127 extern void add_map_objects(struct World * world, uint8_t type, uint8_t n)
128 {
129     uint8_t i;
130     for (i = 0; i < n; i++)
131     {
132         add_map_object(world, type);
133     }
134 }
135
136
137
138 extern void free_map_objects(struct MapObj * mo_start)
139 {
140     if (NULL == mo_start)
141     {
142         return;
143     }
144     free_map_objects(mo_start->next);
145     free(mo_start);
146 }
147
148
149
150 extern struct MapObj * get_player(struct World * world)
151 {
152     struct MapObj * ptr = world->map_objs;
153     while (1)
154     {
155         if (NULL == ptr)
156         {
157             return ptr;
158         }
159         if (0 == ptr->id)
160         {
161             return ptr;
162         }
163         ptr = ptr->next;
164     }
165 }
166
167
168
169 extern struct MapObjDef * get_map_object_def(struct World * w, uint8_t id)
170 {
171     struct MapObjDef * mod = w->map_obj_defs;
172     while (id != mod->id)
173     {
174         mod = mod->next;
175     }
176     return mod;
177 }