home · contact · privacy
30563220a26d4830fded78b757a278aab0e5dc8a
[plomrogue] / src / server / things.c
1 /* src/server/things.c
2  *
3  * This file is part of PlomRogue. PlomRogue is licensed under the GPL version 3
4  * or any later version. For details on its copyright, license, and warranties,
5  * see the file NOTICE in the root directory of the PlomRogue source package.
6  */
7
8 #define _POSIX_C_SOURCE 200809L /* strdup() */
9 #include "things.h"
10 #include <stddef.h> /* NULL, size_t */
11 #include <stdint.h> /* uint8_t, uint16_t, int16_t, UINT8_MAX, UINT16_MAX */
12 #include <stdlib.h> /* free() */
13 #include <string.h> /* memset(), strcmp(), strdup(), strlen() */
14 #include "../common/rexit.h" /* exit_err() */
15 #include "../common/try_malloc.h" /* try_malloc() */
16 #include "../common/yx_uint8.h" /* yx_uint8 */
17 #include "cleanup.h" /* set_cleanup_flag() */
18 #include "hardcoded_strings.h" /* s */
19 #include "field_of_view.h" /* build_fov_map() */
20 #include "map.h" /* mv_yx_in_dir_legal() */
21 #include "rrand.h" /* rrand() */
22 #include "thing_actions.h" /* actor_wait */
23 #include "world.h" /* world */
24
25
26
27 /* Used to treat structs Thing, ThingType and ThingAction the same. */
28 struct NextAndId
29 {
30     struct NextAndId * next;
31     uint8_t id;
32 };
33
34
35
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 lowest ID >= "start_id" and <= UINT8_MAX for new thing
40  * ("struct_id"=0), thing type ("struct_id"=1) or thing action ("struct_id"=2).
41  */
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);
45
46
47
48 static struct NextAndId * add_to_struct_list(size_t n_size, uint8_t start_id,
49                                              int16_t id, uint8_t struct_id,
50                                              struct NextAndId ** start)
51 {
52     struct NextAndId * nai  = try_malloc(n_size, __func__);
53     memset(nai, 0, n_size);
54     if (start_id <= id && id <= UINT8_MAX)
55     {
56         nai->id = id;
57     }
58     else
59     {
60         while (1)
61         {
62             if (   (0 == struct_id && !get_thing(world.things, start_id, 1))
63                 || (1 == struct_id && !get_thing_type(start_id))
64                 || (2 == struct_id && !get_thing_action(start_id)))
65             {
66                 nai->id = start_id;
67                 break;
68             }
69             char * err =  "No unused ID available to add to ID list.";
70             exit_err(start_id == UINT8_MAX, err);
71             start_id++;
72         }
73     }
74     struct NextAndId ** nai_ptr_ptr = start;
75     for (; * nai_ptr_ptr; nai_ptr_ptr = &(*nai_ptr_ptr)->next);
76     *nai_ptr_ptr = nai;
77     return nai;
78 }
79
80
81
82 extern struct ThingAction * add_thing_action(uint8_t id)
83 {
84     struct ThingAction * ta;
85     ta = (struct ThingAction *) add_to_struct_list(sizeof(struct ThingAction),
86                                                    1, (int16_t) id, 2,
87                                                    (struct NextAndId **)
88                                                    &world.thing_actions);
89     set_cleanup_flag(CLEANUP_THING_ACTIONS);
90     ta->name = strdup(s[S_CMD_WAIT]);
91     ta->effort = 1;
92     ta->func = actor_wait;
93     return ta;
94 }
95
96
97
98 extern struct ThingType * add_thing_type(int16_t id)
99 {
100     struct ThingType * tt;
101     tt = (struct ThingType *) add_to_struct_list(sizeof(struct ThingType),
102                                                  0, id, 1,
103                                                  (struct NextAndId **)
104                                                  &world.thing_types);
105     set_cleanup_flag(CLEANUP_THING_TYPES);
106     tt->name = strdup("(none)");
107     tt->corpse_id = tt->id;
108     return tt;
109 }
110
111
112
113 extern struct Thing * add_thing(int16_t id, uint8_t type, uint8_t y, uint8_t x)
114 {
115     struct Thing * t;
116     t = (struct Thing *) add_to_struct_list(sizeof(struct Thing), 0, id, 0,
117                                             (struct NextAndId **)&world.things);
118     struct ThingType * tt = get_thing_type(type);
119     set_cleanup_flag(CLEANUP_THINGS);
120     t->type       = tt->id;
121     t->lifepoints = tt->lifepoints;
122     t->pos.y      = y;
123     t->pos.x      = x;
124     if (t->lifepoints && world.exists)
125     {
126         build_fov_map(t);
127     }
128     return t;
129 }
130
131
132
133 extern void add_thing_to_memory_map(struct Thing * t, uint8_t type,
134                                     uint8_t y, uint8_t x)
135 {
136     struct ThingInMemory * tm=try_malloc(sizeof(struct ThingInMemory),__func__);
137     tm->type = type;
138     tm->pos.y = y;
139     tm->pos.x = x;
140     tm->next = t->t_mem;
141     t->t_mem = tm;
142 }
143
144
145
146 extern void free_thing_actions(struct ThingAction * ta)
147 {
148     if (!ta)
149     {
150         return;
151     }
152     free_thing_actions(ta->next);
153     free(ta->name);
154     free(ta);
155 }
156
157
158
159 extern void free_thing_types(struct ThingType * tt)
160 {
161     if (!tt)
162     {
163         return;
164     }
165     free_thing_types(tt->next);
166     free(tt->name);
167     free(tt);
168 }
169
170
171
172 extern void free_things(struct Thing * t)
173 {
174     if (!t)
175     {
176         return;
177     }
178     free_things(t->owns);
179     free_things(t->next);
180     free(t->fov_map);
181     free(t->mem_map);
182     free_things_in_memory(t->t_mem);
183     free(t);
184     if (t == world.things)         /* So add_things()' NULL-delimited thing   */
185     {                              /* iteration loop does not iterate over    */
186         world.things = NULL;       /* freed memory when called the first time */
187     }                              /* after world re-seeding.                 */
188 }
189
190
191
192 extern void free_things_in_memory(struct ThingInMemory * tm)
193 {
194     if (!tm)
195     {
196         return;
197     }
198     free_things_in_memory(tm->next);
199     free(tm);
200 }
201
202
203
204 extern struct ThingAction * get_thing_action(uint8_t id)
205 {
206     struct ThingAction * ta = world.thing_actions;
207     for (; ta && id != ta->id; ta = ta->next);
208     return ta;
209 }
210
211
212
213 extern struct ThingType * get_thing_type(uint8_t id)
214 {
215     struct ThingType * tt = world.thing_types;
216     for (; tt && id != tt->id; tt = tt->next);
217     return tt;
218 }
219
220
221
222 extern uint8_t get_thing_action_id_by_name(char * name)
223 {
224     struct ThingAction * ta = world.thing_actions;
225     while (ta)
226     {
227         if (0 == strcmp(ta->name, name))
228         {
229             break;
230         }
231         ta = ta->next;
232     }
233     if (!ta)
234     {
235         return 0;
236     }
237     return ta->id;
238 }
239
240
241
242 extern struct Thing * get_thing(struct Thing * ptr, uint8_t id, uint8_t deep)
243 {
244     while (1)
245     {
246         if (!ptr || id == ptr->id)
247         {
248             return ptr;
249         }
250         if (deep)
251         {
252             struct Thing * owned_thing = get_thing(ptr->owns, id, 1);
253             if (owned_thing)
254             {
255                 return ptr;
256             }
257         }
258         ptr = ptr->next;
259     }
260 }
261
262
263
264 extern struct Thing * get_player()
265 {
266     return get_thing(world.things, 0, 0);
267 }
268
269
270
271 extern void try_thing_proliferation(struct Thing * t)
272 {
273     struct ThingType * tt = get_thing_type(t->type);
274     if (tt->proliferate)
275     {
276         if (1 == tt->proliferate || 1 == (rrand() % tt->proliferate))
277         {
278             struct yx_uint8 candidates[6];
279             uint8_t n_candidates = 0;
280             char dirs[7] = "dxswed";
281             struct yx_uint8 start = t->pos;
282             uint8_t i;
283             for (i = 0; i < strlen(dirs); i++)
284             {
285                 if (   mv_yx_in_dir_legal(dirs[i], &start)
286                     && '.' == world.map.cells[start.y*world.map.length+start.x])
287                 {
288                     uint8_t drop = 0;
289                     if (tt->lifepoints)
290                     {
291                         for (t = world.things; t; t = t->next)
292                         {
293                             if (   t->lifepoints
294                                 && start.y == t->pos.y && start.x == t->pos.x)
295                             {
296                                 drop = 1;
297                                 break;
298                             }
299                         }
300                     }
301                     if (!drop)
302                     {
303                         candidates[n_candidates] = start;
304                         n_candidates++;
305                     }
306                 }
307             }
308             if (!n_candidates)
309             {
310                 return;
311             }
312             i = rrand() % n_candidates;
313             add_thing(-1, tt->id, candidates[i].y, candidates[i].x);
314         }
315     }
316 }
317
318
319
320 extern void add_things(uint8_t type, uint8_t n)
321 {
322     uint8_t i;
323     for (i = 0; i < n; i++)
324     {
325         struct yx_uint8 pos;
326         while (1)
327         {
328             char * err="Space to put thing on too hard to find. Map too small?";
329             uint16_t i_pos = 0;
330             for (pos.y = pos.x = 0;
331                  '.' != world.map.cells[pos.y * world.map.length + pos.x];
332                  i_pos++)
333             {
334                 exit_err(UINT16_MAX == i_pos, err);
335                 pos.y = rrand() % world.map.length;
336                 pos.x = rrand() % world.map.length;
337             }
338             struct Thing * t;
339             uint8_t clear = 1;
340             for (t = world.things; t; t = t->next)
341             {
342                 if (0 != t->lifepoints && pos.y==t->pos.y && pos.x==t->pos.x)
343                 {
344                     clear = 0;
345                     break;
346                 }
347             }
348             if (1 == clear)
349             {
350                 break;
351             }
352         }
353         add_thing(-1, type, pos.y, pos.x);
354     }
355 }
356
357
358
359 extern void own_thing(struct Thing ** target, struct Thing ** source,
360                       uint8_t id)
361 {
362     struct Thing * t;
363     if (id == (*source)->id)
364     {
365         t = * source;
366         * source = t->next;
367     }
368     else
369     {
370         struct Thing * penult = * source;
371         while (1)
372         {
373             if (id == penult->next->id)
374             {
375                 break;
376             }
377             penult = penult->next;
378         }
379         t = penult->next;
380         penult->next = t->next;
381     }
382     struct Thing ** t_ptr_ptr = target;
383     for (; * t_ptr_ptr; t_ptr_ptr = &(*t_ptr_ptr)->next);
384     * t_ptr_ptr = t;
385     t->next = NULL;
386 }
387
388
389
390 extern void set_thing_position(struct Thing * t, struct yx_uint8 pos)
391 {
392     t->pos = pos;
393     struct Thing * owned = t->owns;
394     for (; owned; set_thing_position(owned, pos), owned = owned->next);
395 }