1 /* src/server/map_objects.c */
3 #include "map_objects.h"
4 #include <stddef.h> /* NULL */
5 #include <stdint.h> /* uint8_t, uint16_t, UINT16_MAX */
6 #include <stdlib.h> /* free() */
7 #include <string.h> /* memset(), strlen() */
8 #include "../common/rexit.h" /* exit_err() */
9 #include "../common/try_malloc.h" /* try_malloc() */
10 #include "../common/yx_uint8.h" /* yx_uint8 struct */
11 #include "map.h" /* is_passable() */
12 #include "rrand.h" /* rrand() */
13 #include "world.h" /* global world */
14 #include "yx_uint8.h" /* yx_uint8_cmp() */
18 /* Return pointer to map object of "id" in chain starting at "ptr". */
19 static struct MapObj * get_map_object(struct MapObj * ptr, uint8_t id);
21 /* Add object of "type" to map on passable position. Don't put actor on actor.*/
22 static void add_map_object(uint8_t type);
26 static struct MapObj * get_map_object(struct MapObj * ptr, uint8_t id)
30 if (NULL == ptr || id == ptr->id)
34 struct MapObj * owned_object = get_map_object(ptr->owns, id);
35 if (NULL != owned_object)
45 static void add_map_object(uint8_t type)
47 char * f_name = "add_map_object()";
48 struct MapObjDef * mod = get_map_object_def(type);
49 struct MapObj * mo = try_malloc(sizeof(struct MapObj), f_name);
50 memset(mo, 0, sizeof(struct MapObj));
51 mo->id = world.map_obj_count++;
53 mo->lifepoints = mod->lifepoints;
54 char * err = "Space to put map object on too hard to find. Map too small?";
59 for (pos.y = pos.x = 0; 0 == is_passable(pos); i++)
61 exit_err(UINT16_MAX == i, err);
62 pos.y = rrand() % world.map.size.y;
63 pos.x = rrand() % world.map.size.x;
65 struct MapObj * mo_ptr;
67 for (mo_ptr = world.map_objs; mo_ptr != NULL; mo_ptr = mo_ptr->next)
69 if (yx_uint8_cmp(&pos, &mo_ptr->pos) && 0 != mo_ptr->lifepoints)
81 struct MapObj ** mo_ptr_ptr = &world.map_objs;
82 for (; NULL != * mo_ptr_ptr; mo_ptr_ptr = &(*mo_ptr_ptr)->next);
88 extern void free_map_object_defs(struct MapObjDef * mod_start)
90 if (NULL == mod_start)
94 free_map_object_defs(mod_start->next);
95 free(mod_start->name);
101 extern void add_map_objects(uint8_t type, uint8_t n)
104 for (i = 0; i < n; i++)
106 add_map_object(type);
112 extern void free_map_objects(struct MapObj * mo_start)
114 if (NULL == mo_start)
118 free_map_objects(mo_start->owns);
119 free_map_objects(mo_start->next);
120 free(mo_start->fov_map);
122 if (mo_start == world.map_objs) /* So add_map_objects()' NULL-delimited */
123 { /* map object iteration loop does not */
124 world.map_objs = NULL; /* iterate over freed memory when called */
125 } /* the 1st time after world re-seeding. */
130 extern void own_map_object(struct MapObj ** target, struct MapObj ** source,
134 if (id == (*source)->id)
141 struct MapObj * penult = * source;
144 if (id == penult->next->id)
148 penult = penult->next;
151 penult->next = mo->next;
153 struct MapObj ** mo_ptr_ptr = target;
154 for (; NULL != * mo_ptr_ptr; mo_ptr_ptr = &(*mo_ptr_ptr)->next);
161 extern struct MapObj * get_player()
163 return get_map_object(world.map_objs, 0);
168 extern struct MapObjDef * get_map_object_def(uint8_t id)
170 struct MapObjDef * mod = world.map_obj_defs;
171 for (; NULL != mod && id != mod->id; mod = mod->next);
172 char * err_intro = "Requested map object definition of unused ID ";
173 char err[strlen(err_intro) + 3 + 1 + 1];
174 sprintf(err, "%s%d.", err_intro, id);
175 exit_err(NULL == mod, err);
181 extern void set_object_position(struct MapObj * mo, struct yx_uint8 pos)
184 struct MapObj * owned = mo->owns;
185 for (; owned != NULL; set_object_position(owned, pos), owned = owned->next);