1 /* src/server/map_objects.c */
3 #include "map_objects.h"
4 #include <stddef.h> /* NULL */
5 #include <stdio.h> /* FILE typedef */
6 #include <stdint.h> /* uint8_t, uint16_t */
7 #include <stdlib.h> /* free(), atoi() */
8 #include <string.h> /* strlen(), memcpy(), memset() */
9 #include "../common/err_try_fgets.h" /* err_try_fgets(), err_line(),
10 * reset_err_try_fgets_counter()
12 #include "../common/readwrite.h" /* try_fopen(), try_fclose(), try_fgetc(),
15 #include "../common/rexit.h" /* exit_err(), exit_trouble() */
16 #include "../common/try_malloc.h" /* try_malloc() */
17 #include "../common/yx_uint8.h" /* yx_uint8 struct */
18 #include "cleanup.h" /* set_cleanup_flag() */
19 #include "map.h" /* is_passable() */
20 #include "rrand.h" /* rrand() */
21 #include "world.h" /* global world */
22 #include "yx_uint8.h" /* yx_uint8_cmp() */
26 /* Return pointer to map object of "id" in chain starting at "ptr". */
27 static struct MapObj * get_map_object(struct MapObj * ptr, uint8_t id);
29 /* Return random passable (as by is_passable()) position on world.map. */
30 static struct yx_uint8 find_passable_pos();
32 /* Add object of "type" to map on random position. Don't place actor on actor.*/
33 static void add_map_object(uint8_t type);
37 static struct MapObj * get_map_object(struct MapObj * ptr, uint8_t id)
41 if (NULL == ptr || id == ptr->id)
45 struct MapObj * owned_object = get_map_object(ptr->owns, id);
46 if (NULL != owned_object)
56 static struct yx_uint8 find_passable_pos()
59 for (pos.y = pos.x = 0; 0 == is_passable(pos);)
61 pos.y = rrand() % world.map.size.y;
62 pos.x = rrand() % world.map.size.x;
69 static void add_map_object(uint8_t type)
71 char * f_name = "add_map_object()";
72 struct MapObjDef * mod = get_map_object_def(type);
73 struct MapObj * mo = try_malloc(sizeof(struct MapObj), f_name);
74 memset(mo, 0, sizeof(struct MapObj));
75 mo->id = world.map_obj_count++;
77 mo->lifepoints = mod->lifepoints;
80 struct yx_uint8 pos = find_passable_pos(world.map);
81 struct MapObj * mo_ptr;
83 for (mo_ptr = world.map_objs; mo_ptr != NULL; mo_ptr = mo_ptr->next)
85 if (yx_uint8_cmp(&pos, &mo_ptr->pos) && 0 != mo_ptr->lifepoints)
97 struct MapObj ** mo_ptr_ptr = &world.map_objs;
98 for (; NULL != * mo_ptr_ptr; mo_ptr_ptr = &(*mo_ptr_ptr)->next);
104 extern void init_map_object_defs()
106 char * f_name = "init_map_object_defs()";
107 char * context = "Failed reading map object definitions file. ";
108 char * err_uniq = "Declaration of ID already used.";
109 FILE * file = try_fopen(world.path_map_obj_defs, "r", f_name);
110 uint32_t linemax = textfile_width(file);
111 struct MapObjDef ** last_mod_ptr_ptr = &world.map_obj_defs;
112 char line[linemax + 1];
113 reset_err_try_fgets_counter();
116 int test_for_end = try_fgetc(file, f_name);
117 if (EOF == test_for_end || '\n' == test_for_end)
121 exit_trouble(EOF == ungetc(test_for_end, file), f_name, "ungetc()");
122 struct MapObjDef * mod = try_malloc(sizeof(struct MapObjDef), f_name);
124 err_try_fgets(line, linemax, file, context, "nfi8");
125 mod->id = atoi(line);
126 struct MapObjDef * mod_test = world.map_obj_defs;
127 for (; NULL != mod_test; mod_test = mod_test->next)
129 err_line(mod->id == mod_test->id, line, context, err_uniq);
131 err_try_fgets(line, linemax, file, context, "0nfi8");
132 mod->corpse_id = atoi(line);
133 err_try_fgets(line, linemax, file, context, "0nfs");
134 mod->char_on_map = line[0];
135 err_try_fgets(line, linemax, file, context, "0nfi8");
136 mod->lifepoints = atoi(line);
137 err_try_fgets(line, linemax, file, context, "0nf");
138 line[strlen(line) - 1] = '\0';
139 mod->name = try_malloc(strlen(line) + 1, f_name);
140 memcpy(mod->name, line, strlen(line) + 1);
141 err_try_fgets(line, linemax, file, context, "0nfi8");
142 mod->consumable = atoi(line);
143 * last_mod_ptr_ptr = mod;
144 last_mod_ptr_ptr = &mod->next;
145 err_try_fgets(line, linemax, file, context, "d");
147 try_fclose(file, f_name);
148 set_cleanup_flag(CLEANUP_MAP_OBJECT_DEFS);
153 extern void free_map_object_defs(struct MapObjDef * mod_start)
155 if (NULL == mod_start)
159 free_map_object_defs(mod_start->next);
160 free(mod_start->name);
166 extern void add_map_objects(uint8_t type, uint8_t n)
169 for (i = 0; i < n; i++)
171 add_map_object(type);
177 extern void free_map_objects(struct MapObj * mo_start)
179 if (NULL == mo_start)
183 free_map_objects(mo_start->owns);
184 free_map_objects(mo_start->next);
186 if (mo_start == world.map_objs) /* So add_map_objects()' NULL-delimited */
187 { /* map object iteration loop does not */
188 world.map_objs = NULL; /* iterate over freed memory when called */
189 } /* the 1st time after world re-seeding. */
194 extern void own_map_object(struct MapObj ** target, struct MapObj ** source,
198 if (id == (*source)->id)
205 struct MapObj * penult = * source;
208 if (id == penult->next->id)
212 penult = penult->next;
215 penult->next = mo->next;
217 struct MapObj ** mo_ptr_ptr = target;
218 for (; NULL != * mo_ptr_ptr; mo_ptr_ptr = &(*mo_ptr_ptr)->next);
225 extern struct MapObj * get_player()
227 return get_map_object(world.map_objs, 0);
232 extern struct MapObjDef * get_map_object_def(uint8_t id)
234 struct MapObjDef * mod = world.map_obj_defs;
235 for (; NULL != mod && id != mod->id; mod = mod->next);
236 char * err_intro = "Requested map object definition of unused ID ";
237 char err[strlen(err_intro) + 3 + 1 + 1];
238 sprintf(err, "%s%d.", err_intro, id);
239 exit_err(NULL == mod, err);
245 extern void set_object_position(struct MapObj * mo, struct yx_uint8 pos)
248 struct MapObj * owned = mo->owns;
249 for (; owned != NULL; set_object_position(owned, pos), owned = owned->next);