home · contact · privacy
Server/AI: Explore map for (long-time) unexplored cells.
[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(t->mem_depth_map);
214     free_things_in_memory(t->t_mem);
215     free(t);
216     if (t == world.things)         /* So add_things()' NULL-delimited thing   */
217     {                              /* iteration loop does not iterate over    */
218         world.things = NULL;       /* freed memory when called the first time */
219     }                              /* after world re-seeding.                 */
220 }
221
222
223
224 extern void free_things_in_memory(struct ThingInMemory * tm)
225 {
226     if (!tm)
227     {
228         return;
229     }
230     free_things_in_memory(tm->next);
231     free(tm);
232 }
233
234
235
236 extern struct ThingAction * get_thing_action(uint8_t id)
237 {
238     struct ThingAction * ta = world.thing_actions;
239     for (; ta && id != ta->id; ta = ta->next);
240     return ta;
241 }
242
243
244
245 extern struct ThingType * get_thing_type(uint8_t id)
246 {
247     struct ThingType * tt = world.thing_types;
248     for (; tt && id != tt->id; tt = tt->next);
249     return tt;
250 }
251
252
253
254 extern uint8_t get_thing_action_id_by_name(char * name)
255 {
256     struct ThingAction * ta = world.thing_actions;
257     while (ta)
258     {
259         if (0 == strcmp(ta->name, name))
260         {
261             break;
262         }
263         ta = ta->next;
264     }
265     if (!ta)
266     {
267         return 0;
268     }
269     return ta->id;
270 }
271
272
273
274 extern struct Thing * get_thing(struct Thing * ptr, uint8_t id, uint8_t deep)
275 {
276     while (1)
277     {
278         if (!ptr || id == ptr->id)
279         {
280             return ptr;
281         }
282         if (deep)
283         {
284             struct Thing * owned_thing = get_thing(ptr->owns, id, 1);
285             if (owned_thing)
286             {
287                 return ptr;
288             }
289         }
290         ptr = ptr->next;
291     }
292 }
293
294
295
296 extern struct Thing * get_player()
297 {
298     return get_thing(world.things, 0, 0);
299 }
300
301
302
303 extern void try_thing_proliferation(struct Thing * t)
304 {
305     struct ThingType * tt = get_thing_type(t->type);
306     if (tt->proliferate)
307     {
308         if (1 == tt->proliferate || 1 == (rrand() % tt->proliferate))
309         {
310             struct yx_uint8 candidates[6];
311             uint8_t n_candidates = 0;
312             char dirs[7] = "dxswed";
313             struct yx_uint8 test = t->pos;
314             uint8_t i;
315             for (i = 0; i < strlen(dirs); i++)
316             {
317                 if (   mv_yx_in_dir_legal(dirs[i], &test)
318                     && cell_is_proliferable(test, t))
319                 {
320                         candidates[n_candidates] = test;
321                         n_candidates++;
322                 }
323             }
324             if (!n_candidates)
325             {
326                 return;
327             }
328             i = rrand() % n_candidates;
329             add_thing(-1, tt->id, candidates[i].y, candidates[i].x);
330         }
331     }
332 }
333
334
335
336 extern void add_things(uint8_t type, uint8_t n)
337 {
338     uint8_t i;
339     for (i = 0; i < n; i++)
340     {
341         struct yx_uint8 pos;
342         while (1)
343         {
344             char * err="Space to put thing on too hard to find. Map too small?";
345             uint16_t i_pos = 0;
346             for (pos.y = pos.x = 0;
347                  '.' != world.map.cells[pos.y * world.map.length + pos.x];
348                  i_pos++)
349             {
350                 exit_err(UINT16_MAX == i_pos, err);
351                 pos.y = rrand() % world.map.length;
352                 pos.x = rrand() % world.map.length;
353             }
354             struct Thing * t;
355             uint8_t clear = 1;
356             for (t = world.things; t; t = t->next)
357             {
358                 if (0 != t->lifepoints && pos.y==t->pos.y && pos.x==t->pos.x)
359                 {
360                     clear = 0;
361                     break;
362                 }
363             }
364             if (1 == clear)
365             {
366                 break;
367             }
368         }
369         add_thing(-1, type, pos.y, pos.x);
370     }
371 }
372
373
374
375 extern void own_thing(struct Thing ** target, struct Thing ** source,
376                       uint8_t id)
377 {
378     struct Thing * t;
379     if (id == (*source)->id)
380     {
381         t = * source;
382         * source = t->next;
383     }
384     else
385     {
386         struct Thing * penult = * source;
387         while (1)
388         {
389             if (id == penult->next->id)
390             {
391                 break;
392             }
393             penult = penult->next;
394         }
395         t = penult->next;
396         penult->next = t->next;
397     }
398     struct Thing ** t_ptr_ptr = target;
399     for (; * t_ptr_ptr; t_ptr_ptr = &(*t_ptr_ptr)->next);
400     * t_ptr_ptr = t;
401     t->next = NULL;
402 }
403
404
405
406 extern void set_thing_position(struct Thing * t, struct yx_uint8 pos)
407 {
408     t->pos = pos;
409     struct Thing * owned = t->owns;
410     for (; owned; set_thing_position(owned, pos), owned = owned->next);
411 }