1 /* src/server/things.c */
3 #define _POSIX_C_SOURCE 200809L /* strdup() */
5 #include <stddef.h> /* NULL, size_t */
6 #include <stdint.h> /* uint8_t, uint16_t, int16_t, UINT8_MAX, UINT16_MAX */
7 #include <stdlib.h> /* free() */
8 #include <string.h> /* memset(), strcmp(), strdup() */
9 #include "../common/rexit.h" /* exit_err() */
10 #include "../common/try_malloc.h" /* try_malloc() */
11 #include "../common/yx_uint8.h" /* yx_uint8 */
12 #include "cleanup.h" /* set_cleanup_flag() */
13 #include "hardcoded_strings.h" /* s */
14 #include "rrand.h" /* rrand() */
15 #include "thing_actions.h" /* actor_wait */
16 #include "world.h" /* world */
17 #include "yx_uint8.h" /* yx_uint8_cmp() */
21 /* Used to treat structs Thing, ThingType and ThingAction the same. */
24 struct NextAndId * next;
30 /* To linked list of NextAndId structs (or rather structs whose start region is
31 * compatible to it) starting at "start", add newly allocated element of
32 * "n_size" and an ID that is either "id" or, if "id" is <= UINT8_MAX and >=
33 * "id_start", get lowest ID >= "start_id" and <= UINT8_MAX for new thing
34 * ("struct_id"=0), thing type ("struct_id"=1) or thing action ("struct_id"=2).
36 static struct NextAndId * add_to_struct_list(size_t n_size, uint8_t start_id,
37 int16_t id, uint8_t struct_id,
38 struct NextAndId ** start);
42 static struct NextAndId * add_to_struct_list(size_t n_size, uint8_t start_id,
43 int16_t id, uint8_t struct_id,
44 struct NextAndId ** start)
46 struct NextAndId * nai = try_malloc(n_size, __func__);
47 memset(nai, 0, n_size);
48 if (start_id <= id && id <= UINT8_MAX)
56 if ( (0 == struct_id && !get_thing(world.things, start_id, 1))
57 || (1 == struct_id && !get_thing_type(start_id))
58 || (2 == struct_id && !get_thing_action(start_id)))
63 char * err = "No unused ID available to add to ID list.";
64 exit_err(start_id == UINT8_MAX, err);
68 struct NextAndId ** nai_ptr_ptr = start;
69 for (; * nai_ptr_ptr; nai_ptr_ptr = &(*nai_ptr_ptr)->next);
76 extern struct ThingAction * add_thing_action(uint8_t id)
78 struct ThingAction * ta;
79 ta = (struct ThingAction *) add_to_struct_list(sizeof(struct ThingAction),
82 &world.thing_actions);
83 set_cleanup_flag(CLEANUP_THING_ACTIONS);
84 ta->name = strdup(s[S_CMD_WAIT]);
86 ta->func = actor_wait;
92 extern struct ThingType * add_thing_type(int16_t id)
94 struct ThingType * tt;
95 tt = (struct ThingType *) add_to_struct_list(sizeof(struct ThingType),
99 set_cleanup_flag(CLEANUP_THING_TYPES);
100 tt->name = strdup("(none)");
101 tt->corpse_id = tt->id;
107 extern struct Thing * add_thing(int16_t id, uint8_t type, uint8_t y, uint8_t x)
110 t = (struct Thing *) add_to_struct_list(sizeof(struct Thing), 0, id, 0,
111 (struct NextAndId **)&world.things);
112 struct ThingType * tt = get_thing_type(type);
113 set_cleanup_flag(CLEANUP_THINGS);
115 t->lifepoints = tt->lifepoints;
123 extern void add_thing_to_memory_map(struct Thing * t, uint8_t type,
124 uint8_t y, uint8_t x)
126 struct ThingInMemory * tm=try_malloc(sizeof(struct ThingInMemory),__func__);
136 extern void free_thing_actions(struct ThingAction * ta)
142 free_thing_actions(ta->next);
149 extern void free_thing_types(struct ThingType * tt)
155 free_thing_types(tt->next);
162 extern void free_things(struct Thing * t)
168 free_things(t->owns);
169 free_things(t->next);
172 free_things_in_memory(t->t_mem);
174 if (t == world.things) /* So add_things()' NULL-delimited thing */
175 { /* iteration loop does not iterate over */
176 world.things = NULL; /* freed memory when called the first time */
177 } /* after world re-seeding. */
182 extern void free_things_in_memory(struct ThingInMemory * tm)
188 free_things_in_memory(tm->next);
194 extern struct ThingAction * get_thing_action(uint8_t id)
196 struct ThingAction * ta = world.thing_actions;
197 for (; ta && id != ta->id; ta = ta->next);
203 extern struct ThingType * get_thing_type(uint8_t id)
205 struct ThingType * tt = world.thing_types;
206 for (; tt && id != tt->id; tt = tt->next);
212 extern uint8_t get_thing_action_id_by_name(char * name)
214 struct ThingAction * ta = world.thing_actions;
217 if (0 == strcmp(ta->name, name))
232 extern struct Thing * get_thing(struct Thing * ptr, uint8_t id, uint8_t deep)
236 if (!ptr || id == ptr->id)
242 struct Thing * owned_thing = get_thing(ptr->owns, id, 1);
254 extern struct Thing * get_player()
256 return get_thing(world.things, 0, 0);
261 extern void add_things(uint8_t type, uint8_t n)
264 for (i = 0; i < n; i++)
269 char * err="Space to put thing on too hard to find. Map too small?";
271 for (pos.y = pos.x = 0;
272 '.' != world.map.cells[pos.y * world.map.length + pos.x];
275 exit_err(UINT16_MAX == i_pos, err);
276 pos.y = rrand() % world.map.length;
277 pos.x = rrand() % world.map.length;
281 for (t = world.things; t; t = t->next)
283 if (yx_uint8_cmp(&pos, &t->pos) && 0 != t->lifepoints)
294 add_thing(-1, type, pos.y, pos.x);
300 extern void own_thing(struct Thing ** target, struct Thing ** source,
304 if (id == (*source)->id)
311 struct Thing * penult = * source;
314 if (id == penult->next->id)
318 penult = penult->next;
321 penult->next = t->next;
323 struct Thing ** t_ptr_ptr = target;
324 for (; * t_ptr_ptr; t_ptr_ptr = &(*t_ptr_ptr)->next);
331 extern void set_thing_position(struct Thing * t, struct yx_uint8 pos)
334 struct Thing * owned = t->owns;
335 for (; owned; set_thing_position(owned, pos), owned = owned->next);