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);
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);
28 static void write_map_object(FILE * file, struct MapObj * mo)
30 char * f_name = "write_map_object()";
31 struct MapObj * mo_ptr = mo->owns;
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;
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)
40 sprintf(line + strlen(line), " %d", mo_ptr->id);
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)
47 write_map_object(file, mo_ptr);
53 static struct MapObj * get_map_object(struct MapObj * ptr, uint8_t id)
57 if (NULL == ptr || id == ptr->id)
61 struct MapObj * owned_object = get_map_object(ptr->owns, id);
62 if (NULL != owned_object)
72 extern void init_map_object_defs(char * filename)
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;
79 char line[linemax + 1];
80 while (try_fgets(line, linemax + 1, file, f_name))
82 struct MapObjDef * mod;
83 mod = try_malloc(sizeof(struct MapObjDef), f_name);
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;
95 try_fclose(file, f_name);
100 extern void free_map_object_defs(struct MapObjDef * mod_start)
102 if (NULL == mod_start)
106 free_map_object_defs(mod_start->next);
107 free(mod_start->name);
113 extern void write_map_objects(FILE * file)
115 struct MapObj * mo = world.map_objs;
118 write_map_object(file, mo);
125 extern void read_map_objects(FILE * file, char * line, int linemax)
127 char * f_name = "read_map_objects()";
128 struct MapObj ** mo_ptr_ptr = &world.map_objs;
132 exit_err(-1 == fgetpos(file, &pos), f_name);
133 while (try_fgets(line, linemax + 1, file, f_name))
135 mo = malloc(sizeof(struct MapObj));
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));
143 if (mo->id > world.map_obj_count)
145 world.map_obj_count = mo->id;
148 mo_ptr_ptr = &mo->next;
150 exit_err(-1 == fsetpos(file, &pos), f_name);
151 while (try_fgets(line, linemax + 1, file, f_name))
153 uint8_t id = atoi(strtok(line, delim));
158 char * owned = strtok(NULL, "\n");
161 mo = get_map_object(world.map_objs, id);
162 char * owned_id = "";
163 owned_id = strtok(owned, delim);
164 while (NULL != owned_id)
166 own_map_object(&mo->owns, &world.map_objs,
167 (uint8_t) atoi(owned_id));
168 owned_id = strtok(NULL, delim);
176 extern void add_map_object(uint8_t type)
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++;
184 mo->lifepoints = mod->lifepoints;
187 struct yx_uint16 pos = find_passable_pos(world.map);
188 struct MapObj * mo_ptr;
190 for (mo_ptr = world.map_objs; mo_ptr != NULL; mo_ptr = mo_ptr->next)
192 if (yx_uint16_cmp(&pos, &mo_ptr->pos) && 0 != mo_ptr->lifepoints)
206 struct MapObj ** last_ptr_ptr = &world.map_objs;
207 struct MapObj * mo_ptr;
208 while (NULL != * last_ptr_ptr)
210 mo_ptr = * last_ptr_ptr;
211 last_ptr_ptr = & mo_ptr->next;
218 extern void add_map_objects(uint8_t type, uint8_t n)
221 for (i = 0; i < n; i++)
223 add_map_object(type);
229 extern void free_map_objects(struct MapObj * mo_start)
231 if (NULL == mo_start)
235 free_map_objects(mo_start->owns);
236 free_map_objects(mo_start->next);
242 extern void own_map_object(struct MapObj ** target, struct MapObj ** source,
246 if (id == (*source)->id)
253 struct MapObj * penult = * source;
256 if (id == penult->next->id)
260 penult = penult->next;
263 penult->next = mo->next;
265 struct MapObj ** last_ptr_ptr = target;
266 struct MapObj * mo_ptr;
267 while (NULL != * last_ptr_ptr)
269 mo_ptr = * last_ptr_ptr;
270 last_ptr_ptr = & mo_ptr->next;
278 extern struct MapObj * get_player()
280 return get_map_object(world.map_objs, 0);
285 extern struct MapObjDef * get_map_object_def(uint8_t id)
287 struct MapObjDef * mod = world.map_obj_defs;
288 while (id != mod->id)
297 extern void set_object_position(struct MapObj * mo, struct yx_uint16 pos)
300 struct MapObj * owned = mo->owns;
301 for (; owned != NULL; owned = owned->next)
303 set_object_position(owned, pos);