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