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() */
10 #include "../common/rexit.h" /* exit_err() */
11 #include "../common/try_malloc.h" /* try_malloc() */
12 #include "../common/yx_uint8.h" /* yx_uint8 struct */
13 #include "map.h" /* is_passable() */
14 #include "rrand.h" /* rrand() */
15 #include "world.h" /* global world */
16 #include "yx_uint8.h" /* yx_uint8_cmp() */
18 #include "io.h" /* struct EntrySkeleton */
20 /* Return pointer to map object of "id" in chain starting at "ptr". */
21 static struct MapObj * get_map_object(struct MapObj * ptr, uint8_t id);
23 /* Return random passable (as by is_passable()) position on world.map. */
24 static struct yx_uint8 find_passable_pos();
26 /* Add object of "type" to map on random position. Don't place actor on actor.*/
27 static void add_map_object(uint8_t type);
31 static struct MapObj * get_map_object(struct MapObj * ptr, uint8_t id)
35 if (NULL == ptr || id == ptr->id)
39 struct MapObj * owned_object = get_map_object(ptr->owns, id);
40 if (NULL != owned_object)
50 static struct yx_uint8 find_passable_pos()
53 for (pos.y = pos.x = 0; 0 == is_passable(pos);)
55 pos.y = rrand() % world.map.size.y;
56 pos.x = rrand() % world.map.size.x;
63 static void add_map_object(uint8_t type)
65 char * f_name = "add_map_object()";
66 struct MapObjDef * mod = get_map_object_def(type);
67 struct MapObj * mo = try_malloc(sizeof(struct MapObj), f_name);
68 memset(mo, 0, sizeof(struct MapObj));
69 mo->id = world.map_obj_count++;
71 mo->lifepoints = mod->lifepoints;
74 struct yx_uint8 pos = find_passable_pos(world.map);
75 struct MapObj * mo_ptr;
77 for (mo_ptr = world.map_objs; mo_ptr != NULL; mo_ptr = mo_ptr->next)
79 if (yx_uint8_cmp(&pos, &mo_ptr->pos) && 0 != mo_ptr->lifepoints)
91 struct MapObj ** mo_ptr_ptr = &world.map_objs;
92 for (; NULL != * mo_ptr_ptr; mo_ptr_ptr = &(*mo_ptr_ptr)->next);
98 extern void read_map_object_def(char * line, uint32_t linemax, char * context,
99 struct EntrySkeleton * entry, FILE * file)
101 char * f_name = "init_map_object_defs()";
102 struct MapObjDef * mod = (struct MapObjDef *) entry;
103 err_try_fgets(line, linemax, file, context, "0nfi8");
104 mod->corpse_id = atoi(line);
105 err_try_fgets(line, linemax, file, context, "0nfs");
106 mod->char_on_map = line[0];
107 err_try_fgets(line, linemax, file, context, "0nfi8");
108 mod->lifepoints = atoi(line);
109 err_try_fgets(line, linemax, file, context, "0nf");
110 line[strlen(line) - 1] = '\0';
111 mod->name = try_malloc(strlen(line) + 1, f_name);
112 memcpy(mod->name, line, strlen(line) + 1);
113 err_try_fgets(line, linemax, file, context, "0nfi8");
114 mod->consumable = atoi(line);
119 extern void free_map_object_defs(struct MapObjDef * mod_start)
121 if (NULL == mod_start)
125 free_map_object_defs(mod_start->next);
126 free(mod_start->name);
132 extern void add_map_objects(uint8_t type, uint8_t n)
135 for (i = 0; i < n; i++)
137 add_map_object(type);
143 extern void free_map_objects(struct MapObj * mo_start)
145 if (NULL == mo_start)
149 free_map_objects(mo_start->owns);
150 free_map_objects(mo_start->next);
152 if (mo_start == world.map_objs) /* So add_map_objects()' NULL-delimited */
153 { /* map object iteration loop does not */
154 world.map_objs = NULL; /* iterate over freed memory when called */
155 } /* the 1st time after world re-seeding. */
160 extern void own_map_object(struct MapObj ** target, struct MapObj ** source,
164 if (id == (*source)->id)
171 struct MapObj * penult = * source;
174 if (id == penult->next->id)
178 penult = penult->next;
181 penult->next = mo->next;
183 struct MapObj ** mo_ptr_ptr = target;
184 for (; NULL != * mo_ptr_ptr; mo_ptr_ptr = &(*mo_ptr_ptr)->next);
191 extern struct MapObj * get_player()
193 return get_map_object(world.map_objs, 0);
198 extern struct MapObjDef * get_map_object_def(uint8_t id)
200 struct MapObjDef * mod = world.map_obj_defs;
201 for (; NULL != mod && id != mod->id; mod = mod->next);
202 char * err_intro = "Requested map object definition of unused ID ";
203 char err[strlen(err_intro) + 3 + 1 + 1];
204 sprintf(err, "%s%d.", err_intro, id);
205 exit_err(NULL == mod, err);
211 extern void set_object_position(struct MapObj * mo, struct yx_uint8 pos)
214 struct MapObj * owned = mo->owns;
215 for (; owned != NULL; set_object_position(owned, pos), owned = owned->next);