3 #include "map_objects.h"
4 #include <stdlib.h> /* for free(), atoi() */
5 #include <stdint.h> /* for uint8_t, uint16_t */
6 #include <stdio.h> /* for FILE typedef */
7 #include <string.h> /* for strlen(), memcpy(), strtok() */
8 #include "readwrite.h" /* for textfile_sizes(), try_fopen(), try_fclose(),
11 #include "misc.h" /* for try_malloc(), find_passable_pos() */
12 #include "main.h" /* for world global */
13 #include "rexit.h" /* for exit_err() */
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 = textfile_sizes(file, NULL);
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 = 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;
129 exit_err(-1 == fgetpos(file, &pos), f_name);
130 while (try_fgets(line, linemax + 1, file, f_name))
132 struct MapObj * mo = try_malloc(sizeof(struct MapObj), f_name);
134 mo->id = atoi(strtok(line, delim));
135 mo->type = atoi(strtok(NULL, delim));
136 mo->lifepoints = atoi(strtok(NULL, delim));
137 mo->pos.y = atoi(strtok(NULL, delim));
138 mo->pos.x = atoi(strtok(NULL, delim));
139 mo->command = atoi(strtok(NULL, delim));;
140 mo->arg = atoi(strtok(NULL, delim));;
141 mo->progress = atoi(strtok(NULL, delim));;
144 mo_ptr_ptr = &mo->next;
146 exit_err(-1 == fsetpos(file, &pos), f_name);
147 while (try_fgets(line, linemax + 1, file, f_name))
149 uint8_t id = atoi(strtok(line, delim));
151 for (i = 0; i < 7; i++, strtok(NULL, delim));
152 char * owned = strtok(NULL, "\n");
155 struct MapObj * mo = get_map_object(world.map_objs, id);
156 char * owned_id = "";
157 owned_id = strtok(owned, delim);
158 while (NULL != owned_id)
160 own_map_object(&mo->owns, &world.map_objs, atoi(owned_id));
161 owned_id = strtok(NULL, delim);
169 extern void add_map_object(uint8_t type)
171 char * f_name = "add_map_object()";
172 struct MapObjDef * mod = get_map_object_def(type);
173 struct MapObj * mo = try_malloc(sizeof(struct MapObj), f_name);
174 mo->id = world.map_obj_count++;
176 mo->lifepoints = mod->lifepoints;
179 struct yx_uint16 pos = find_passable_pos(world.map);
180 struct MapObj * mo_ptr;
182 for (mo_ptr = world.map_objs; mo_ptr != NULL; mo_ptr = mo_ptr->next)
184 if (yx_uint16_cmp(&pos, &mo_ptr->pos) && 0 != mo_ptr->lifepoints)
201 struct MapObj ** mo_ptr_ptr = &world.map_objs;
202 for (; NULL != * mo_ptr_ptr; mo_ptr_ptr = &(*mo_ptr_ptr)->next);
208 extern void add_map_objects(uint8_t type, uint8_t n)
211 for (i = 0; i < n; i++)
213 add_map_object(type);
219 extern void free_map_objects(struct MapObj * mo_start)
221 if (NULL == mo_start)
225 free_map_objects(mo_start->owns);
226 free_map_objects(mo_start->next);
232 extern void own_map_object(struct MapObj ** target, struct MapObj ** source,
236 if (id == (*source)->id)
243 struct MapObj * penult = * source;
246 if (id == penult->next->id)
250 penult = penult->next;
253 penult->next = mo->next;
255 struct MapObj ** mo_ptr_ptr = target;
256 for (; NULL != * mo_ptr_ptr; mo_ptr_ptr = &(*mo_ptr_ptr)->next);
263 extern struct MapObj * get_player()
265 return get_map_object(world.map_objs, 0);
270 extern struct MapObjDef * get_map_object_def(uint8_t id)
272 struct MapObjDef * mod = world.map_obj_defs;
273 for (; id != mod->id; mod = mod->next);
279 extern void set_object_position(struct MapObj * mo, struct yx_uint16 pos)
282 struct MapObj * owned = mo->owns;
283 for (; owned != NULL; set_object_position(owned, pos), owned = owned->next);