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 %d %d %d",
35 mo->id, mo->type, mo->lifepoints, mo->pos.y, mo->pos.x,
36 mo->command, mo->arg, mo->progress);
37 for (mo_ptr = mo->owns; NULL != mo_ptr; mo_ptr = mo_ptr->next)
39 sprintf(line + strlen(line), " %d", mo_ptr->id);
41 line[strlen(line) + 1] = '\0';
42 line[strlen(line)] = '\n';
43 try_fwrite(line, strlen(line), 1, file, f_name);
44 for (mo_ptr = mo->owns; NULL != mo_ptr; mo_ptr = mo_ptr->next)
46 write_map_object(file, mo_ptr);
52 static struct MapObj * get_map_object(struct MapObj * ptr, uint8_t id)
56 if (NULL == ptr || id == ptr->id)
60 struct MapObj * owned_object = get_map_object(ptr->owns, id);
61 if (NULL != owned_object)
71 extern void init_map_object_defs(char * filename)
73 char * f_name = "init_map_object_defs()";
74 FILE * file = try_fopen(filename, "r", f_name);
75 uint16_t linemax = get_linemax(file, f_name);
76 struct MapObjDef ** last_mod_ptr_ptr = &world.map_obj_defs;
78 char line[linemax + 1];
79 while (try_fgets(line, linemax + 1, file, f_name))
81 struct MapObjDef * mod;
82 mod = try_malloc(sizeof(struct MapObjDef), f_name);
84 mod->id = atoi(strtok(line, delim));
85 mod->corpse_id = atoi(strtok(NULL, delim));
86 mod->char_on_map = * strtok(NULL, delim);
87 mod->lifepoints = atoi(strtok(NULL, delim));
88 char * name = strtok(NULL, "\n");
89 mod->name = try_malloc(strlen(name) + 1, f_name);
90 memcpy(mod->name, name, strlen(name) + 1);
91 * last_mod_ptr_ptr = mod;
92 last_mod_ptr_ptr = &mod->next;
94 try_fclose(file, f_name);
99 extern void free_map_object_defs(struct MapObjDef * mod_start)
101 if (NULL == mod_start)
105 free_map_object_defs(mod_start->next);
106 free(mod_start->name);
112 extern void write_map_objects(FILE * file)
114 struct MapObj * mo = world.map_objs;
117 write_map_object(file, mo);
124 extern void read_map_objects(FILE * file, char * line, int linemax)
126 char * f_name = "read_map_objects()";
127 struct MapObj ** mo_ptr_ptr = &world.map_objs;
131 exit_err(-1 == fgetpos(file, &pos), f_name);
132 while (try_fgets(line, linemax + 1, file, f_name))
134 mo = try_malloc(sizeof(struct MapObj), f_name);
136 mo->id = atoi(strtok(line, delim));
137 mo->type = atoi(strtok(NULL, delim));
138 mo->lifepoints = atoi(strtok(NULL, delim));
139 mo->pos.y = atoi(strtok(NULL, delim));
140 mo->pos.x = atoi(strtok(NULL, delim));
141 mo->command = atoi(strtok(NULL, delim));;
142 mo->arg = atoi(strtok(NULL, delim));;
143 mo->progress = atoi(strtok(NULL, delim));;
145 if (mo->id > world.map_obj_count)
147 world.map_obj_count = mo->id;
150 mo_ptr_ptr = &mo->next;
152 exit_err(-1 == fsetpos(file, &pos), f_name);
153 while (try_fgets(line, linemax + 1, file, f_name))
155 uint8_t id = atoi(strtok(line, delim));
157 for (i = 0; i < 7; i++)
161 char * owned = strtok(NULL, "\n");
164 mo = get_map_object(world.map_objs, id);
165 char * owned_id = "";
166 owned_id = strtok(owned, delim);
167 while (NULL != owned_id)
169 own_map_object(&mo->owns, &world.map_objs,
170 (uint8_t) atoi(owned_id));
171 owned_id = strtok(NULL, delim);
179 extern void add_map_object(uint8_t type)
181 char * f_name = "add_map_object()";
182 struct MapObjDef * mod = get_map_object_def(type);
183 struct MapObj * mo = try_malloc(sizeof(struct MapObj), f_name);
184 mo->id = world.map_obj_count;
185 world.map_obj_count++;
187 mo->lifepoints = mod->lifepoints;
190 struct yx_uint16 pos = find_passable_pos(world.map);
191 struct MapObj * mo_ptr;
193 for (mo_ptr = world.map_objs; mo_ptr != NULL; mo_ptr = mo_ptr->next)
195 if (yx_uint16_cmp(&pos, &mo_ptr->pos) && 0 != mo_ptr->lifepoints)
212 struct MapObj ** last_ptr_ptr = &world.map_objs;
213 struct MapObj * mo_ptr;
214 while (NULL != * last_ptr_ptr)
216 mo_ptr = * last_ptr_ptr;
217 last_ptr_ptr = & mo_ptr->next;
224 extern void add_map_objects(uint8_t type, uint8_t n)
227 for (i = 0; i < n; i++)
229 add_map_object(type);
235 extern void free_map_objects(struct MapObj * mo_start)
237 if (NULL == mo_start)
241 free_map_objects(mo_start->owns);
242 free_map_objects(mo_start->next);
248 extern void own_map_object(struct MapObj ** target, struct MapObj ** source,
252 if (id == (*source)->id)
259 struct MapObj * penult = * source;
262 if (id == penult->next->id)
266 penult = penult->next;
269 penult->next = mo->next;
271 struct MapObj ** last_ptr_ptr = target;
272 struct MapObj * mo_ptr;
273 while (NULL != * last_ptr_ptr)
275 mo_ptr = * last_ptr_ptr;
276 last_ptr_ptr = & mo_ptr->next;
284 extern struct MapObj * get_player()
286 return get_map_object(world.map_objs, 0);
291 extern struct MapObjDef * get_map_object_def(uint8_t id)
293 struct MapObjDef * mod = world.map_obj_defs;
294 while (id != mod->id)
303 extern void set_object_position(struct MapObj * mo, struct yx_uint16 pos)
306 struct MapObj * owned = mo->owns;
307 for (; owned != NULL; owned = owned->next)
309 set_object_position(owned, pos);