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, UINT8_MAX */
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_toolarge = "Value is too large.";
109 char * err_uniq = "Declaration of ID already used.";
110 FILE * file = try_fopen(world.path_map_obj_defs, "r", f_name);
111 uint32_t linemax = textfile_width(file);
112 struct MapObjDef ** last_mod_ptr_ptr = &world.map_obj_defs;
113 char line[linemax + 1];
114 reset_err_try_fgets_counter();
117 int test_for_end = try_fgetc(file, f_name);
118 if (EOF == test_for_end || '\n' == test_for_end)
122 exit_trouble(EOF == ungetc(test_for_end, file), f_name, "ungetc()");
123 struct MapObjDef * mod = try_malloc(sizeof(struct MapObjDef), f_name);
125 err_try_fgets(line, linemax, file, context, "nfi");
126 err_line(atoi(line) > UINT8_MAX, line, context, err_toolarge);
127 mod->id = atoi(line);
128 struct MapObjDef * mod_test = world.map_obj_defs;
129 for (; NULL != mod_test; mod_test = mod_test->next)
131 err_line(mod->id == mod_test->id, line, context, err_uniq);
133 err_try_fgets(line, linemax, file, context, "0nfi");
134 err_line(atoi(line) > UINT8_MAX, line, context, err_toolarge);
135 mod->corpse_id = atoi(line);
136 err_try_fgets(line, linemax, file, context, "0nfs");
137 mod->char_on_map = line[0];
138 err_try_fgets(line, linemax, file, context, "0nfi");
139 err_line(atoi(line) > UINT8_MAX, line, context, err_toolarge);
140 mod->lifepoints = atoi(line);
141 err_try_fgets(line, linemax, file, context, "0nf");
142 line[strlen(line) - 1] = '\0';
143 mod->name = try_malloc(strlen(line) + 1, f_name);
144 memcpy(mod->name, line, strlen(line) + 1);
145 * last_mod_ptr_ptr = mod;
146 last_mod_ptr_ptr = &mod->next;
147 err_try_fgets(line, linemax, file, context, "d");
149 try_fclose(file, f_name);
150 set_cleanup_flag(CLEANUP_MAP_OBJECT_DEFS);
155 extern void free_map_object_defs(struct MapObjDef * mod_start)
157 if (NULL == mod_start)
161 free_map_object_defs(mod_start->next);
162 free(mod_start->name);
168 extern void add_map_objects(uint8_t type, uint8_t n)
171 for (i = 0; i < n; i++)
173 add_map_object(type);
179 extern void free_map_objects(struct MapObj * mo_start)
181 if (NULL == mo_start)
185 free_map_objects(mo_start->owns);
186 free_map_objects(mo_start->next);
188 if (mo_start == world.map_objs) /* So add_map_objects()' NULL-delimited */
189 { /* map object iteration loop does not */
190 world.map_objs = NULL; /* iterate over freed memory when called */
191 } /* the 1st time after world re-seeding. */
196 extern void own_map_object(struct MapObj ** target, struct MapObj ** source,
200 if (id == (*source)->id)
207 struct MapObj * penult = * source;
210 if (id == penult->next->id)
214 penult = penult->next;
217 penult->next = mo->next;
219 struct MapObj ** mo_ptr_ptr = target;
220 for (; NULL != * mo_ptr_ptr; mo_ptr_ptr = &(*mo_ptr_ptr)->next);
227 extern struct MapObj * get_player()
229 return get_map_object(world.map_objs, 0);
234 extern struct MapObjDef * get_map_object_def(uint8_t id)
236 struct MapObjDef * mod = world.map_obj_defs;
237 for (; NULL != mod && id != mod->id; mod = mod->next);
238 char * err_intro = "Requested map object definition of unused ID ";
239 char err[strlen(err_intro) + 3 + 1 + 1];
240 sprintf(err, "%s%d.", err_intro, id);
241 exit_err(NULL == mod, err);
247 extern void set_object_position(struct MapObj * mo, struct yx_uint8 pos)
250 struct MapObj * owned = mo->owns;
251 for (; owned != NULL; set_object_position(owned, pos), owned = owned->next);