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]()
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() */
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);
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);
26 static void write_map_object(FILE * file, struct MapObj * mo)
28 char * f_name = "write_map_object()";
29 struct MapObj * mo_ptr = mo->owns;
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;
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)
38 sprintf(line + strlen(line), " %d", mo_ptr->id);
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)
45 write_map_object(file, mo_ptr);
51 static struct MapObj * get_map_object(struct MapObj * ptr, uint8_t id)
55 if (NULL == ptr || id == ptr->id)
59 struct MapObj * owned_object = get_map_object(ptr->owns, id);
60 if (NULL != owned_object)
70 extern void init_map_object_defs(char * filename)
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;
77 char line[linemax + 1];
78 while (try_fgets(line, linemax + 1, file, f_name))
80 struct MapObjDef * mod;
81 mod = try_malloc(sizeof(struct MapObjDef), f_name);
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;
93 try_fclose(file, f_name);
98 extern void free_map_object_defs(struct MapObjDef * mod_start)
100 if (NULL == mod_start)
104 free_map_object_defs(mod_start->next);
105 free(mod_start->name);
111 extern void write_map_objects(FILE * file)
113 struct MapObj * mo = world.map_objs;
116 write_map_object(file, mo);
123 extern void read_map_objects(FILE * file, char * line, int linemax)
125 char * f_name = "read_map_objects()";
126 struct MapObj ** mo_ptr_ptr = &world.map_objs;
130 exit_err(-1 == fgetpos(file, &pos), f_name);
131 while (try_fgets(line, linemax + 1, file, f_name))
133 mo = malloc(sizeof(struct MapObj));
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));
141 if (mo->id > world.map_obj_count)
143 world.map_obj_count = mo->id;
146 mo_ptr_ptr = &mo->next;
148 exit_err(-1 == fsetpos(file, &pos), f_name);
149 while (try_fgets(line, linemax + 1, file, f_name))
151 uint8_t id = atoi(strtok(line, delim));
156 char * owned = strtok(NULL, "\n");
159 mo = get_map_object(world.map_objs, id);
160 char * owned_id = "";
161 owned_id = strtok(owned, delim);
162 while (NULL != owned_id)
164 own_map_object(&mo->owns, &world.map_objs,
165 (uint8_t) atoi(owned_id));
166 owned_id = strtok(NULL, delim);
174 extern void add_map_object(uint8_t type)
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++;
182 mo->lifepoints = mod->lifepoints;
185 struct yx_uint16 pos = find_passable_pos(world.map);
186 struct MapObj * mo_ptr;
188 for (mo_ptr = world.map_objs; mo_ptr != NULL; mo_ptr = mo_ptr->next)
190 if (yx_uint16_cmp(&pos, &mo_ptr->pos) && 0 != mo_ptr->lifepoints)
204 struct MapObj ** last_ptr_ptr = &world.map_objs;
205 struct MapObj * mo_ptr;
206 while (NULL != * last_ptr_ptr)
208 mo_ptr = * last_ptr_ptr;
209 last_ptr_ptr = & mo_ptr->next;
216 extern void add_map_objects(uint8_t type, uint8_t n)
219 for (i = 0; i < n; i++)
221 add_map_object(type);
227 extern void free_map_objects(struct MapObj * mo_start)
229 if (NULL == mo_start)
233 free_map_objects(mo_start->owns);
234 free_map_objects(mo_start->next);
240 extern void own_map_object(struct MapObj ** target, struct MapObj ** source,
244 if (id == (*source)->id)
251 struct MapObj * penult = * source;
254 if (id == penult->next->id)
258 penult = penult->next;
261 penult->next = mo->next;
263 struct MapObj ** last_ptr_ptr = target;
264 struct MapObj * mo_ptr;
265 while (NULL != * last_ptr_ptr)
267 mo_ptr = * last_ptr_ptr;
268 last_ptr_ptr = & mo_ptr->next;
276 extern struct MapObj * get_player()
278 return get_map_object(world.map_objs, 0);
283 extern struct MapObjDef * get_map_object_def(uint8_t id)
285 struct MapObjDef * mod = world.map_obj_defs;
286 while (id != mod->id)
295 extern void set_object_position(struct MapObj * mo, struct yx_uint16 pos)
298 struct MapObj * owned = mo->owns;
299 for (; owned != NULL; owned = owned->next)
301 set_object_position(owned, pos);