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 "map.h" /* is_passable() */
15 #include "rrand.h" /* rrand() */
16 #include "thing_actions.h" /* actor_wait */
17 #include "world.h" /* world */
18 #include "yx_uint8.h" /* yx_uint8_cmp() */
22 /* Used to treat structs Thing, ThingType and ThingAction the same. */
25 struct NextAndId * next;
31 /* Return lowest unused id for new thing ("sel"==0), thing type ("sel"==1) or
32 * thing action ("sel"==2).
34 static uint8_t get_unused_id(uint8_t sel);
36 /* To linked list of NextAndId structs (or rather structs whose start region is
37 * compatible to it) starting at "start", add newly allocated element of
38 * "n_size" and an ID that is either "id" or, if "id" is <= UINT8_MAX and >=
39 * "id_start", get ID from get_unused_id("struct_id").
41 static struct NextAndId * add_to_struct_list(size_t n_size, uint8_t start_id,
42 int16_t id, uint8_t struct_id,
43 struct NextAndId ** start);
47 static uint8_t get_unused_id(uint8_t sel)
52 if ( (0 == sel && !get_thing(world.things, i, 1))
53 || (1 == sel && !get_thing_type(i))
54 || (2 == sel && !get_thing_action(i)))
58 exit_err(i == UINT8_MAX, "No unused ID available to add to ID list.");
65 static struct NextAndId * add_to_struct_list(size_t n_size, uint8_t start_id,
66 int16_t id, uint8_t struct_id,
67 struct NextAndId ** start)
69 char * f_name = "add_to_struct_list()";
70 struct NextAndId * nai = try_malloc(n_size, f_name);
71 memset(nai, 0, n_size);
72 nai->id = (start_id<=id && id<=UINT8_MAX) ? id : get_unused_id(struct_id);
73 struct NextAndId ** nai_ptr_ptr = start;
74 for (; NULL != * nai_ptr_ptr; nai_ptr_ptr = &(*nai_ptr_ptr)->next);
81 extern struct ThingAction * add_thing_action(int16_t id)
83 struct ThingAction * ta;
84 ta = (struct ThingAction *) add_to_struct_list(sizeof(struct ThingAction),
87 &world.thing_actions);
88 set_cleanup_flag(CLEANUP_THING_ACTIONS);
89 ta->name = strdup(s[S_CMD_WAIT]);
91 ta->func = actor_wait;
97 extern struct ThingType * add_thing_type(int16_t id)
99 struct ThingType * tt;
100 tt = (struct ThingType *) add_to_struct_list(sizeof(struct ThingType),
102 (struct NextAndId **)
104 set_cleanup_flag(CLEANUP_THING_TYPES);
105 tt->name = strdup("(none)");
111 extern struct Thing * add_thing(int16_t id, uint8_t type, uint8_t y, uint8_t x)
114 t = (struct Thing *) add_to_struct_list(sizeof(struct Thing), 0, id, 0,
115 (struct NextAndId **)&world.things);
116 struct ThingType * tt = get_thing_type(type);
117 set_cleanup_flag(CLEANUP_THINGS);
119 t->lifepoints = tt->lifepoints;
127 extern void free_thing_actions(struct ThingAction * ta)
133 free_thing_actions(ta->next);
140 extern void free_thing_types(struct ThingType * tt)
146 free_thing_types(tt->next);
153 extern void free_things(struct Thing * t)
159 free_things(t->owns);
160 free_things(t->next);
163 if (t == world.things) /* So add_things()' NULL-delimited thing */
164 { /* iteration loop does not iterate over */
165 world.things = NULL; /* freed memory when called the first time */
166 } /* after world re-seeding. */
171 extern struct ThingAction * get_thing_action(uint8_t id)
173 struct ThingAction * ta = world.thing_actions;
174 for (; NULL != ta && id != ta->id; ta = ta->next);
180 extern struct ThingType * get_thing_type(uint8_t id)
182 struct ThingType * tt = world.thing_types;
183 for (; NULL != tt && id != tt->id; tt = tt->next);
189 extern uint8_t get_thing_action_id_by_name(char * name)
191 struct ThingAction * ta = world.thing_actions;
194 if (0 == strcmp(ta->name, name))
209 extern struct Thing * get_thing(struct Thing * ptr, uint8_t id, uint8_t deep)
213 if (NULL == ptr || id == ptr->id)
219 struct Thing * owned_thing = get_thing(ptr->owns, id, 1);
220 if (NULL != owned_thing)
231 extern struct Thing * get_player()
233 return get_thing(world.things, 0, 0);
238 extern void add_things(uint8_t type, uint8_t n)
241 for (i = 0; i < n; i++)
246 char * err = "Space to put thing on too hard to find."
249 for (pos.y = pos.x = 0; 0 == is_passable(pos); i_pos++)
251 exit_err(UINT16_MAX == i_pos, err);
252 pos.y = rrand() % world.map.length;
253 pos.x = rrand() % world.map.length;
257 for (t = world.things; t; t = t->next)
259 if (yx_uint8_cmp(&pos, &t->pos) && 0 != t->lifepoints)
270 add_thing(-1, type, pos.y, pos.x);
276 extern void own_thing(struct Thing ** target, struct Thing ** source,
280 if (id == (*source)->id)
287 struct Thing * penult = * source;
290 if (id == penult->next->id)
294 penult = penult->next;
297 penult->next = t->next;
299 struct Thing ** t_ptr_ptr = target;
300 for (; NULL != * t_ptr_ptr; t_ptr_ptr = &(*t_ptr_ptr)->next);
307 extern void set_thing_position(struct Thing * t, struct yx_uint8 pos)
310 struct Thing * owned = t->owns;
311 for (; owned != NULL; set_thing_position(owned, pos), owned = owned->next);