home · contact · privacy
More consistent code styling whitespace rules.
[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(), find_passable_pos() */
12 #include "main.h" /* for world global */
13 #include "rexit.h" /* for err_exit() */
14 #include "yx_uint16.h" /* for yx_uint16 struct, yx_uint16_cmp() */
15
16
17
18 /* Write representation of "mo" and all of the map objects it owns to "file". */
19 static void write_map_object(FILE * file, struct MapObj * mo);
20
21 /* Return pointer to map object of "id" in chain starting at "ptr". */
22 static struct MapObj * get_map_object(struct MapObj * ptr, uint8_t id);
23
24
25
26 static void write_map_object(FILE * file, struct MapObj * mo)
27 {
28     char * f_name = "write_map_object()";
29     struct MapObj * mo_ptr = mo->owns;
30     uint8_t i = 0;
31     for (; NULL != mo_ptr; mo_ptr = mo_ptr->next, i++);
32     uint8_t size = 3+1 + 3+1 + 3+1 + 5+1 + 5 + ((1+3)*i) + 1 + 1;
33     char line[size];
34     sprintf(line, "%d %d %d %d %d",
35                   mo->id, mo->type, mo->lifepoints, mo->pos.y, mo->pos.x);
36     for (mo_ptr = mo->owns; NULL != mo_ptr; mo_ptr = mo_ptr->next)
37     {
38         sprintf(line + strlen(line), " %d", mo_ptr->id);
39     }
40     line[strlen(line) + 1] = '\0';
41     line[strlen(line)] = '\n';
42     try_fwrite(line, strlen(line), 1, file, f_name);
43     for (mo_ptr = mo->owns; NULL != mo_ptr; mo_ptr = mo_ptr->next)
44     {
45         write_map_object(file, mo_ptr);
46     }
47 }
48
49
50
51 static struct MapObj * get_map_object(struct MapObj * ptr, uint8_t id)
52 {
53     while (1)
54     {
55         if (NULL == ptr || id == ptr->id)
56         {
57             return ptr;
58         }
59         struct MapObj * owned_object = get_map_object(ptr->owns, id);
60         if (NULL != owned_object)
61         {
62             return ptr;
63         }
64         ptr = ptr->next;
65     }
66 }
67
68
69
70 extern void init_map_object_defs(char * filename)
71 {
72     char * f_name = "init_map_object_defs()";
73     FILE * file = try_fopen(filename, "r", f_name);
74     uint16_t linemax = get_linemax(file, f_name);
75     struct MapObjDef ** last_mod_ptr_ptr = &world.map_obj_defs;
76     char * delim = " ";
77     char line[linemax + 1];
78     while (try_fgets(line, linemax + 1, file, f_name))
79     {
80         struct MapObjDef * mod;
81         mod = try_malloc(sizeof(struct MapObjDef), f_name);
82         mod->next = NULL;
83         mod->id = atoi(strtok(line, delim));
84         mod->corpse_id = atoi(strtok(NULL, delim));
85         mod->char_on_map = * strtok(NULL, delim);
86         mod->lifepoints = atoi(strtok(NULL, delim));
87         char * name = strtok(NULL, "\n");
88         mod->name = try_malloc(strlen(name) + 1, f_name);
89         memcpy(mod->name, name, strlen(name) + 1);
90         * last_mod_ptr_ptr = mod;
91         last_mod_ptr_ptr = &mod->next;
92     }
93     try_fclose(file, f_name);
94 }
95
96
97
98 extern void free_map_object_defs(struct MapObjDef * mod_start)
99 {
100     if (NULL == mod_start)
101     {
102         return;
103     }
104     free_map_object_defs(mod_start->next);
105     free(mod_start->name);
106     free(mod_start);
107 }
108
109
110
111 extern void write_map_objects(FILE * file)
112 {
113     struct MapObj * mo = world.map_objs;
114     while (NULL != mo)
115     {
116         write_map_object(file, mo);
117         mo = mo->next;
118     }
119 }
120
121
122
123 extern void read_map_objects(FILE * file, char * line, int linemax)
124 {
125     char * f_name = "read_map_objects()";
126     struct MapObj ** mo_ptr_ptr = &world.map_objs;
127     char * delim = " ";
128     struct MapObj * mo;
129     fpos_t pos;
130     exit_err(-1 == fgetpos(file, &pos), f_name);
131     while (try_fgets(line, linemax + 1, file, f_name))
132     {
133         mo = malloc(sizeof(struct MapObj));
134         mo->next       = NULL;
135         mo->id         = atoi(strtok(line, delim));
136         mo->type       = atoi(strtok(NULL, delim));
137         mo->lifepoints = atoi(strtok(NULL, delim));
138         mo->pos.y      = atoi(strtok(NULL, delim));
139         mo->pos.x      = atoi(strtok(NULL, delim));
140         mo->owns       = NULL;
141         if (mo->id > world.map_obj_count)
142         {
143             world.map_obj_count = mo->id;
144         }
145         * mo_ptr_ptr = mo;
146         mo_ptr_ptr = &mo->next;
147     }
148     exit_err(-1 == fsetpos(file, &pos), f_name);
149     while (try_fgets(line, linemax + 1, file, f_name))
150     {
151         uint8_t id = atoi(strtok(line, delim));
152         strtok(NULL, delim);
153         strtok(NULL, delim);
154         strtok(NULL, delim);
155         strtok(NULL, delim);
156         char * owned = strtok(NULL, "\n");
157         if (NULL != owned)
158         {
159             mo = get_map_object(world.map_objs, id);
160             char * owned_id = "";
161             owned_id = strtok(owned, delim);
162             while (NULL != owned_id)
163             {
164                 own_map_object(&mo->owns, &world.map_objs,
165                                (uint8_t) atoi(owned_id));
166                 owned_id = strtok(NULL, delim);
167             }
168         }
169     }
170 }
171
172
173
174 extern void add_map_object(uint8_t type)
175 {
176     char * f_name = "add_map_object()";
177     struct MapObjDef * mod = get_map_object_def(type);
178     struct MapObj * mo = try_malloc(sizeof(struct MapObj), f_name);
179     mo->id = world.map_obj_count;
180     world.map_obj_count++;
181     mo->type = mod->id;
182     mo->lifepoints = mod->lifepoints;
183     while (1)
184     {
185         struct yx_uint16 pos = find_passable_pos(world.map);
186         struct MapObj * mo_ptr;
187         uint8_t clear = 1;
188         for (mo_ptr = world.map_objs; mo_ptr != NULL; mo_ptr = mo_ptr->next)
189         {
190             if (yx_uint16_cmp(&pos, &mo_ptr->pos) && 0 != mo_ptr->lifepoints)
191             {
192                 clear = 0;
193                 break;
194             }
195         }
196         if (1 == clear)
197         {
198             mo->pos = pos;
199             break;
200         }
201     }
202     mo->owns = NULL;
203     mo->next = NULL;
204     struct MapObj ** last_ptr_ptr = &world.map_objs;
205     struct MapObj * mo_ptr;
206     while (NULL != * last_ptr_ptr)
207     {
208         mo_ptr = * last_ptr_ptr;
209         last_ptr_ptr = & mo_ptr->next;
210     }
211     * last_ptr_ptr = mo;
212 }
213
214
215
216 extern void add_map_objects(uint8_t type, uint8_t n)
217 {
218     uint8_t i;
219     for (i = 0; i < n; i++)
220     {
221         add_map_object(type);
222     }
223 }
224
225
226
227 extern void free_map_objects(struct MapObj * mo_start)
228 {
229     if (NULL == mo_start)
230     {
231         return;
232     }
233     free_map_objects(mo_start->owns);
234     free_map_objects(mo_start->next);
235     free(mo_start);
236 }
237
238
239
240 extern void own_map_object(struct MapObj ** target, struct MapObj ** source,
241                            uint8_t id)
242 {
243     struct MapObj * mo;
244     if (id == (*source)->id)
245     {
246         mo = * source;
247         * source = mo->next;
248     }
249     else
250     {
251         struct MapObj * penult = * source;
252         while (1)
253         {
254             if (id == penult->next->id)
255             {
256                 break;
257             }
258             penult = penult->next;
259         }
260         mo = penult->next;
261         penult->next = mo->next;
262     }
263     struct MapObj ** last_ptr_ptr = target;
264     struct MapObj * mo_ptr;
265     while (NULL != * last_ptr_ptr)
266     {
267         mo_ptr = * last_ptr_ptr;
268         last_ptr_ptr = & mo_ptr->next;
269     }
270     * last_ptr_ptr = mo;
271     mo->next = NULL;
272 }
273
274
275
276 extern struct MapObj * get_player()
277 {
278     return get_map_object(world.map_objs, 0);
279 }
280
281
282
283 extern struct MapObjDef * get_map_object_def(uint8_t id)
284 {
285     struct MapObjDef * mod = world.map_obj_defs;
286     while (id != mod->id)
287     {
288         mod = mod->next;
289     }
290     return mod;
291 }
292
293
294
295 extern void set_object_position(struct MapObj * mo, struct yx_uint16 pos)
296 {
297     mo->pos = pos;
298     struct MapObj * owned = mo->owns;
299     for (; owned != NULL; owned = owned->next)
300     {
301         set_object_position(owned, pos);
302     }
303 }