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 */
20 /* Used to treat structs Thing, ThingType and ThingAction the same. */
23 struct NextAndId * next;
29 /* To linked list of NextAndId structs (or rather structs whose start region is
30 * compatible to it) starting at "start", add newly allocated element of
31 * "n_size" and an ID that is either "id" or, if "id" is <= UINT8_MAX and >=
32 * "id_start", get lowest ID >= "start_id" and <= UINT8_MAX for new thing
33 * ("struct_id"=0), thing type ("struct_id"=1) or thing action ("struct_id"=2).
35 static struct NextAndId * add_to_struct_list(size_t n_size, uint8_t start_id,
36 int16_t id, uint8_t struct_id,
37 struct NextAndId ** start);
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)
45 struct NextAndId * nai = try_malloc(n_size, __func__);
46 memset(nai, 0, n_size);
47 if (start_id <= id && id <= UINT8_MAX)
55 if ( (0 == struct_id && !get_thing(world.things, start_id, 1))
56 || (1 == struct_id && !get_thing_type(start_id))
57 || (2 == struct_id && !get_thing_action(start_id)))
62 char * err = "No unused ID available to add to ID list.";
63 exit_err(start_id == UINT8_MAX, err);
67 struct NextAndId ** nai_ptr_ptr = start;
68 for (; * nai_ptr_ptr; nai_ptr_ptr = &(*nai_ptr_ptr)->next);
75 extern struct ThingAction * add_thing_action(uint8_t id)
77 struct ThingAction * ta;
78 ta = (struct ThingAction *) add_to_struct_list(sizeof(struct ThingAction),
81 &world.thing_actions);
82 set_cleanup_flag(CLEANUP_THING_ACTIONS);
83 ta->name = strdup(s[S_CMD_WAIT]);
85 ta->func = actor_wait;
91 extern struct ThingType * add_thing_type(int16_t id)
93 struct ThingType * tt;
94 tt = (struct ThingType *) add_to_struct_list(sizeof(struct ThingType),
98 set_cleanup_flag(CLEANUP_THING_TYPES);
99 tt->name = strdup("(none)");
100 tt->corpse_id = tt->id;
106 extern struct Thing * add_thing(int16_t id, uint8_t type, uint8_t y, uint8_t x)
109 t = (struct Thing *) add_to_struct_list(sizeof(struct Thing), 0, id, 0,
110 (struct NextAndId **)&world.things);
111 struct ThingType * tt = get_thing_type(type);
112 set_cleanup_flag(CLEANUP_THINGS);
114 t->lifepoints = tt->lifepoints;
122 extern void add_thing_to_memory_map(struct Thing * t, uint8_t type,
123 uint8_t y, uint8_t x)
125 struct ThingInMemory * tm=try_malloc(sizeof(struct ThingInMemory),__func__);
135 extern void free_thing_actions(struct ThingAction * ta)
141 free_thing_actions(ta->next);
148 extern void free_thing_types(struct ThingType * tt)
154 free_thing_types(tt->next);
161 extern void free_things(struct Thing * t)
167 free_things(t->owns);
168 free_things(t->next);
171 free_things_in_memory(t->t_mem);
173 if (t == world.things) /* So add_things()' NULL-delimited thing */
174 { /* iteration loop does not iterate over */
175 world.things = NULL; /* freed memory when called the first time */
176 } /* after world re-seeding. */
181 extern void free_things_in_memory(struct ThingInMemory * tm)
187 free_things_in_memory(tm->next);
193 extern struct ThingAction * get_thing_action(uint8_t id)
195 struct ThingAction * ta = world.thing_actions;
196 for (; ta && id != ta->id; ta = ta->next);
202 extern struct ThingType * get_thing_type(uint8_t id)
204 struct ThingType * tt = world.thing_types;
205 for (; tt && id != tt->id; tt = tt->next);
211 extern uint8_t get_thing_action_id_by_name(char * name)
213 struct ThingAction * ta = world.thing_actions;
216 if (0 == strcmp(ta->name, name))
231 extern struct Thing * get_thing(struct Thing * ptr, uint8_t id, uint8_t deep)
235 if (!ptr || id == ptr->id)
241 struct Thing * owned_thing = get_thing(ptr->owns, id, 1);
253 extern struct Thing * get_player()
255 return get_thing(world.things, 0, 0);
260 extern void add_things(uint8_t type, uint8_t n)
263 for (i = 0; i < n; i++)
268 char * err="Space to put thing on too hard to find. Map too small?";
270 for (pos.y = pos.x = 0;
271 '.' != world.map.cells[pos.y * world.map.length + pos.x];
274 exit_err(UINT16_MAX == i_pos, err);
275 pos.y = rrand() % world.map.length;
276 pos.x = rrand() % world.map.length;
280 for (t = world.things; t; t = t->next)
282 if (0 != t->lifepoints && pos.y==t->pos.y && pos.x==t->pos.x)
293 add_thing(-1, type, pos.y, pos.x);
299 extern void own_thing(struct Thing ** target, struct Thing ** source,
303 if (id == (*source)->id)
310 struct Thing * penult = * source;
313 if (id == penult->next->id)
317 penult = penult->next;
320 penult->next = t->next;
322 struct Thing ** t_ptr_ptr = target;
323 for (; * t_ptr_ptr; t_ptr_ptr = &(*t_ptr_ptr)->next);
330 extern void set_thing_position(struct Thing * t, struct yx_uint8 pos)
333 struct Thing * owned = t->owns;
334 for (; owned; set_thing_position(owned, pos), owned = owned->next);