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