home · contact · privacy
Server: Free memorized things on map upon actor death.
[plomrogue] / src / server / things.c
1 /* src/server/things.c */
2
3 #define _POSIX_C_SOURCE 200809L /* strdup() */
4 #include "things.h"
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() */
19
20
21
22 /* Used to treat structs Thing, ThingType and ThingAction the same. */
23 struct NextAndId
24 {
25     struct NextAndId * next;
26     uint8_t id;
27 };
28
29
30
31 /* To linked list of NextAndId structs (or rather structs whose start region is
32  * compatible to it) starting at "start", add newly allocated element of
33  * "n_size" and an ID that is either "id" or, if "id" is <= UINT8_MAX and >=
34  * "id_start", get lowest ID >= "start_id" and <= UINT8_MAX for new thing
35  * ("struct_id"=0), thing type ("struct_id"=1) or thing action ("struct_id"=2).
36  */
37 static struct NextAndId * add_to_struct_list(size_t n_size, uint8_t start_id,
38                                              int16_t id, uint8_t struct_id,
39                                              struct NextAndId ** start);
40
41
42
43 static struct NextAndId * add_to_struct_list(size_t n_size, uint8_t start_id,
44                                              int16_t id, uint8_t struct_id,
45                                              struct NextAndId ** start)
46 {
47     struct NextAndId * nai  = try_malloc(n_size, __func__);
48     memset(nai, 0, n_size);
49     if (start_id <= id && id <= UINT8_MAX)
50     {
51         nai->id = id;
52     }
53     else
54     {
55         while (1)
56         {
57             if (   (0 == struct_id && !get_thing(world.things, start_id, 1))
58                 || (1 == struct_id && !get_thing_type(start_id))
59                 || (2 == struct_id && !get_thing_action(start_id)))
60             {
61                 nai->id = start_id;
62                 break;
63             }
64             char * err =  "No unused ID available to add to ID list.";
65             exit_err(start_id == UINT8_MAX, err);
66             start_id++;
67         }
68     }
69     struct NextAndId ** nai_ptr_ptr = start;
70     for (; * nai_ptr_ptr; nai_ptr_ptr = &(*nai_ptr_ptr)->next);
71     *nai_ptr_ptr = nai;
72     return nai;
73 }
74
75
76
77 extern struct ThingAction * add_thing_action(uint8_t id)
78 {
79     struct ThingAction * ta;
80     ta = (struct ThingAction *) add_to_struct_list(sizeof(struct ThingAction),
81                                                    1, (int16_t) id, 2,
82                                                    (struct NextAndId **)
83                                                    &world.thing_actions);
84     set_cleanup_flag(CLEANUP_THING_ACTIONS);
85     ta->name = strdup(s[S_CMD_WAIT]);
86     ta->effort = 1;
87     ta->func = actor_wait;
88     return ta;
89 }
90
91
92
93 extern struct ThingType * add_thing_type(int16_t id)
94 {
95     struct ThingType * tt;
96     tt = (struct ThingType *) add_to_struct_list(sizeof(struct ThingType),
97                                                  0, id, 1,
98                                                  (struct NextAndId **)
99                                                  &world.thing_types);
100     set_cleanup_flag(CLEANUP_THING_TYPES);
101     tt->name = strdup("(none)");
102     tt->corpse_id = tt->id;
103     return tt;
104 }
105
106
107
108 extern struct Thing * add_thing(int16_t id, uint8_t type, uint8_t y, uint8_t x)
109 {
110     struct Thing * t;
111     t = (struct Thing *) add_to_struct_list(sizeof(struct Thing), 0, id, 0,
112                                             (struct NextAndId **)&world.things);
113     struct ThingType * tt = get_thing_type(type);
114     set_cleanup_flag(CLEANUP_THINGS);
115     t->type       = tt->id;
116     t->lifepoints = tt->lifepoints;
117     t->pos.y      = y;
118     t->pos.x      = x;
119     return t;
120 }
121
122
123
124 extern void add_thing_to_memory_map(struct Thing * t, uint8_t type,
125                                     uint8_t y, uint8_t x)
126 {
127     struct ThingInMemory * tm=try_malloc(sizeof(struct ThingInMemory),__func__);
128     tm->type = type;
129     tm->pos.y = y;
130     tm->pos.x = x;
131     tm->next = t->t_mem;
132     t->t_mem = tm;
133 }
134
135
136
137 extern void free_thing_actions(struct ThingAction * ta)
138 {
139     if (!ta)
140     {
141         return;
142     }
143     free_thing_actions(ta->next);
144     free(ta->name);
145     free(ta);
146 }
147
148
149
150 extern void free_thing_types(struct ThingType * tt)
151 {
152     if (!tt)
153     {
154         return;
155     }
156     free_thing_types(tt->next);
157     free(tt->name);
158     free(tt);
159 }
160
161
162
163 extern void free_things(struct Thing * t)
164 {
165     if (!t)
166     {
167         return;
168     }
169     free_things(t->owns);
170     free_things(t->next);
171     free(t->fov_map);
172     free(t->mem_map);
173     free_things_in_memory(t->t_mem);
174     free(t);
175     if (t == world.things)         /* So add_things()' NULL-delimited thing   */
176     {                              /* iteration loop does not iterate over    */
177         world.things = NULL;       /* freed memory when called the first time */
178     }                              /* after world re-seeding.                 */
179 }
180
181
182
183 extern void free_things_in_memory(struct ThingInMemory * tm)
184 {
185     if (!tm)
186     {
187         return;
188     }
189     free_things_in_memory(tm->next);
190     free(tm);
191 }
192
193
194
195 extern struct ThingAction * get_thing_action(uint8_t id)
196 {
197     struct ThingAction * ta = world.thing_actions;
198     for (; ta && id != ta->id; ta = ta->next);
199     return ta;
200 }
201
202
203
204 extern struct ThingType * get_thing_type(uint8_t id)
205 {
206     struct ThingType * tt = world.thing_types;
207     for (; tt && id != tt->id; tt = tt->next);
208     return tt;
209 }
210
211
212
213 extern uint8_t get_thing_action_id_by_name(char * name)
214 {
215     struct ThingAction * ta = world.thing_actions;
216     while (ta)
217     {
218         if (0 == strcmp(ta->name, name))
219         {
220             break;
221         }
222         ta = ta->next;
223     }
224     if (!ta)
225     {
226         return 0;
227     }
228     return ta->id;
229 }
230
231
232
233 extern struct Thing * get_thing(struct Thing * ptr, uint8_t id, uint8_t deep)
234 {
235     while (1)
236     {
237         if (!ptr || id == ptr->id)
238         {
239             return ptr;
240         }
241         if (deep)
242         {
243             struct Thing * owned_thing = get_thing(ptr->owns, id, 1);
244             if (owned_thing)
245             {
246                 return ptr;
247             }
248         }
249         ptr = ptr->next;
250     }
251 }
252
253
254
255 extern struct Thing * get_player()
256 {
257     return get_thing(world.things, 0, 0);
258 }
259
260
261
262 extern void add_things(uint8_t type, uint8_t n)
263 {
264     uint8_t i;
265     for (i = 0; i < n; i++)
266     {
267         struct yx_uint8 pos;
268         while (1)
269         {
270             char * err = "Space to put thing on too hard to find."
271                          "Map too small?";
272             uint16_t i_pos = 0;
273             for (pos.y = pos.x = 0; 0 == is_passable(pos); i_pos++)
274             {
275                 exit_err(UINT16_MAX == i_pos, err);
276                 pos.y = rrand() % world.map.length;
277                 pos.x = rrand() % world.map.length;
278             }
279             struct Thing * t;
280             uint8_t clear = 1;
281             for (t = world.things; t; t = t->next)
282             {
283                 if (yx_uint8_cmp(&pos, &t->pos) && 0 != t->lifepoints)
284                 {
285                     clear = 0;
286                     break;
287                 }
288             }
289             if (1 == clear)
290             {
291                 break;
292             }
293         }
294         add_thing(-1, type, pos.y, pos.x);
295     }
296 }
297
298
299
300 extern void own_thing(struct Thing ** target, struct Thing ** source,
301                       uint8_t id)
302 {
303     struct Thing * t;
304     if (id == (*source)->id)
305     {
306         t = * source;
307         * source = t->next;
308     }
309     else
310     {
311         struct Thing * penult = * source;
312         while (1)
313         {
314             if (id == penult->next->id)
315             {
316                 break;
317             }
318             penult = penult->next;
319         }
320         t = penult->next;
321         penult->next = t->next;
322     }
323     struct Thing ** t_ptr_ptr = target;
324     for (; * t_ptr_ptr; t_ptr_ptr = &(*t_ptr_ptr)->next);
325     * t_ptr_ptr = t;
326     t->next = NULL;
327 }
328
329
330
331 extern void set_thing_position(struct Thing * t, struct yx_uint8 pos)
332 {
333     t->pos = pos;
334     struct Thing * owned = t->owns;
335     for (; owned; set_thing_position(owned, pos), owned = owned->next);
336 }