home · contact · privacy
dd11f38deab002a4694d84f649ba4fe348dbf8be
[plomrogue] / src / server / ai.c
1 /* src/server/ai.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 #include "ai.h"
9 #include <stdint.h> /* uint8_t, uint16_t, uint32_t, int16_t, UINT16_MAX */
10 #include <stdlib.h> /* free() */
11 #include "../common/try_malloc.h" /* try_malloc() */
12 #include "hardcoded_strings.h" /* s */
13 #include "thing_actions.h" /* get_thing_action_id_by_name() */
14 #include "things.h" /* Thing, ThingType, ThingInMemory */
15 #include "world.h" /* world */
16
17
18
19 #define N_DIRS 6
20
21
22
23 /* Write into "neighbors" scores of the N_DIRS immediate neighbors of the
24  * "score_map" cell at "pos_i" (array index), as found in the directions
25  * north-east, east, south-east etc. (clockwise order). Use "max_score" for
26  * illegal neighborhoods (i.e. if direction would lead beyond the map's border).
27  */
28 static void get_neighbor_scores(uint16_t * score_map, uint16_t pos_i,
29                                 uint16_t max_score, uint16_t * neighbors);
30
31 /* Iterate over scored cells in "score_map" of world.map's geometry. Compare
32  * each cell's score against the score of its immediate neighbors in N_DIRS
33  * directions. If any neighbor's score is at least two points lower than the
34  * current cell's score, re-set it to 1 point higher than its lowest-scored
35  * neighbor. Repeat this whole process until all cells have settled on their
36  * final score. Ignore cells whose score is greater than "max_score". Expect
37  * "max_score" to be the maximum score for cells, marking them as unreachable.
38  */
39 static void dijkstra_map(uint16_t * score_map, uint16_t max_score);
40
41 /* get_dir_to_nearest_thing() helper: Prepare "score_map" for dijkstra_map(). */
42 static void init_score_map(char filter, uint16_t * score_map, uint32_t map_size,
43                            struct Thing * t_eye);
44
45 /* Set (if possible) as "t_eye"'s command a move to the path to the path-wise
46  * nearest thing that is not "t_eye" and fits criteria set by "filter". On
47  * success, return 1, else 0. Values for "filter":
48  * "e": thing in FOV is animate, but not of "t_eye"'s thing type; build path as
49  *      avoiding things of "t_eye"'s type
50  * "c": thing in memorized map is consumable.
51  */
52 static uint8_t get_dir_to_nearest_thing(struct Thing * t_eye, char filter);
53
54 /* Return 1 if any thing not "t_eye" is known and fulfills some criteria defined
55  * by "filter", else 0. Values for "filter":
56  * "e": thing in FOV is animate, but not of "t_eye"'s thing type
57  * "c": thing in memorized map is consumable
58  */
59 static uint8_t seeing_thing(struct Thing * t_eye, char filter);
60
61 /* Return slot ID of strongest consumable in "t_owner"'s inventory, else -1. */
62 static int16_t get_inventory_slot_to_consume(struct Thing * t_owner);
63
64 /* Return 1 if "t_standing" is standing on a consumable, else 0. */
65 static uint8_t standing_on_consumable(struct Thing * t_standing);
66
67
68
69 static void get_neighbor_scores(uint16_t * score_map, uint16_t pos_i,
70                                 uint16_t max_score, uint16_t * neighbors)
71 {
72     uint32_t map_size = world.map.length * world.map.length;
73     uint8_t i_dir;
74     for (i_dir = 0; i_dir < N_DIRS; neighbors[i_dir] = max_score, i_dir++);
75     uint8_t open_north     = pos_i >= world.map.length;
76     uint8_t open_east      = pos_i + 1 % world.map.length;
77     uint8_t open_south     = pos_i + world.map.length < map_size;
78     uint8_t open_west      = pos_i % world.map.length;
79     uint8_t is_indented    = (pos_i / world.map.length) % 2;
80     uint8_t open_diag_west = is_indented || open_west;
81     uint8_t open_diag_east = !is_indented || open_east;
82     if (open_north && open_diag_east)
83     {
84         neighbors[0] = score_map[pos_i - world.map.length + is_indented];
85     }
86     if (open_east)
87     {
88         neighbors[1] = score_map[pos_i + 1];
89     }
90     if (open_south && open_diag_east)
91     {
92         neighbors[2] = score_map[pos_i + world.map.length + is_indented];
93     }
94     if (open_south && open_diag_west)
95     {
96         neighbors[3] = score_map[pos_i + world.map.length - !is_indented];
97     }
98     if (open_west)
99     {
100         neighbors[4] = score_map[pos_i - 1];
101     }
102     if (open_north && open_diag_west)
103     {
104         neighbors[5] = score_map[pos_i - world.map.length - !is_indented];
105     }
106 }
107
108
109
110 static void dijkstra_map(uint16_t * score_map, uint16_t max_score)
111 {
112     uint32_t map_size = world.map.length * world.map.length;
113     uint32_t pos;
114     uint16_t i_scans, neighbors[N_DIRS], min_neighbor;
115     uint8_t scores_still_changing = 1;
116     uint8_t i_dirs;
117     for (i_scans = 0; scores_still_changing; i_scans++)
118     {
119         scores_still_changing = 0;
120         for (pos = 0; pos < map_size; pos++)
121         {
122             if (score_map[pos] <= max_score)
123             {
124                 get_neighbor_scores(score_map, pos, max_score, neighbors);
125                 min_neighbor = max_score;
126                 for (i_dirs = 0; i_dirs < N_DIRS; i_dirs++)
127                 {
128                     if (min_neighbor > neighbors[i_dirs])
129                     {
130                         min_neighbor = neighbors[i_dirs];
131                     }
132                 }
133                 if (score_map[pos] > min_neighbor + 1)
134                 {
135                     score_map[pos] = min_neighbor + 1;
136                     scores_still_changing = 1;
137                 }
138             }
139         }
140     }
141 }
142
143
144
145 static void init_score_map(char filter, uint16_t * score_map, uint32_t map_size,
146                            struct Thing * t_eye)
147 {
148     uint32_t i;
149     for (i = 0; i < map_size; i++)
150     {
151         score_map[i] = UINT16_MAX;
152         if ('.' == t_eye->mem_map[i])
153         {
154             score_map[i] = UINT16_MAX-1;
155         }
156     }
157     if      ('e' == filter)
158     {
159         struct Thing * t = world.things;
160         for (; t; t = t->next)
161         {
162             if (   t==t_eye || !t->lifepoints
163                 || 'H' == t_eye->fov_map[t->pos.y*world.map.length + t->pos.x])
164             {
165                 continue;
166             }
167             else if (t->lifepoints && t->type == t_eye->type)
168             {
169                 score_map[t->pos.y * world.map.length + t->pos.x] = UINT16_MAX;
170                 continue;
171             }
172             score_map[t->pos.y * world.map.length + t->pos.x] = 0;
173         }
174     }
175     else if ('c' == filter)
176     {
177         struct ThingInMemory * tm = t_eye->t_mem;
178         for (; tm; tm = tm->next)
179         {
180             if (' ' == t_eye->mem_map[tm->pos.y * world.map.length + tm->pos.x])
181             {
182                 continue;
183             }
184             struct ThingType * tt = get_thing_type(tm->type);
185             if (!tt->consumable)
186             {
187                 continue;
188             }
189             score_map[tm->pos.y * world.map.length + tm->pos.x] = 0;
190         }
191     }
192 }
193
194
195
196 static uint8_t get_dir_to_nearest_thing(struct Thing * t_eye, char filter)
197 {
198     char dir_to_nearest_thing = 0;
199     if (seeing_thing(t_eye, filter))
200     {
201         uint32_t map_size = world.map.length * world.map.length;
202         uint16_t * score_map = try_malloc(map_size * sizeof(uint16_t),__func__);
203         init_score_map(filter, score_map, map_size, t_eye);
204         dijkstra_map(score_map, UINT16_MAX-1);
205         uint16_t neighbors[N_DIRS];
206         uint16_t pos_i = (t_eye->pos.y * world.map.length) + t_eye->pos.x;
207         get_neighbor_scores(score_map, pos_i, UINT16_MAX-1, neighbors);
208         free(score_map);
209         uint16_t min_neighbor = UINT16_MAX-1;
210         char * dirs = "edcxsw";/* get_neighbor_scores()'s clockwise dir order.*/
211         uint8_t i;
212         for (i = 0; i < N_DIRS; i++)
213         {
214             if (min_neighbor > neighbors[i])
215             {
216                 min_neighbor = neighbors[i];
217                 dir_to_nearest_thing = dirs[i];
218             }
219         }
220     }
221     if (dir_to_nearest_thing)
222     {
223         t_eye->command = get_thing_action_id_by_name(s[S_CMD_MOVE]);
224         t_eye->arg = dir_to_nearest_thing;
225         return 1;
226     }
227     return 0;
228 }
229
230
231
232 static uint8_t seeing_thing(struct Thing * t_eye, char filter)
233 {
234     if (t_eye->fov_map && 'e' == filter)
235     {
236         struct Thing * t = world.things;
237         for (; t; t = t->next)
238         {
239             if (   t != t_eye
240                 && 'v' == t_eye->fov_map[t->pos.y*world.map.length + t->pos.x])
241             {
242                 if (t->lifepoints && t->type != t_eye->type)
243                 {
244                     return 1;
245                 }
246             }
247         }
248     }
249     else if (t_eye->mem_map && 'c' == filter)
250     {
251         struct ThingInMemory * tm = t_eye->t_mem;
252         for (; tm; tm = tm->next)
253         {
254             if (' ' != t_eye->mem_map[tm->pos.y * world.map.length + tm->pos.x])
255             {
256                 struct ThingType * tt = get_thing_type(tm->type);
257                 if (tt->consumable)
258                 {
259                     return 1;
260                 }
261             }
262         }
263     }
264     return 0;
265 }
266
267
268
269 static int16_t get_inventory_slot_to_consume(struct Thing * t_owner)
270 {
271     uint8_t compare_consumability = 0;
272     int16_t selection = -1;
273     struct Thing * t = t_owner->owns;;
274     uint8_t i;
275     for (i = 0; t; t = t->next, i++)
276     {
277         struct ThingType * tt = get_thing_type(t->type);
278         if (tt->consumable > compare_consumability)
279         {
280             compare_consumability = tt->consumable;
281             selection = i;
282         }
283     }
284     return selection;
285 }
286
287
288
289 static uint8_t standing_on_consumable(struct Thing * t_standing)
290 {
291     struct Thing * t = world.things;
292     for (; t; t = t->next)
293     {
294         if (   t != t_standing
295             && t->pos.y == t_standing->pos.y && t->pos.x == t_standing->pos.x)
296         {
297             struct ThingType * tt = get_thing_type(t->type);
298             if (tt->consumable)
299             {
300                 return 1;
301             }
302         }
303     }
304     return 0;
305 }
306
307
308
309 extern void ai(struct Thing * t)
310 {
311     t->command = get_thing_action_id_by_name(s[S_CMD_WAIT]);
312     if (!get_dir_to_nearest_thing(t, 'e'))
313     {
314         int16_t sel = get_inventory_slot_to_consume(t);
315         if (-1 != sel)
316         {
317             t->command = get_thing_action_id_by_name(s[S_CMD_USE]);
318             t->arg = (uint8_t) sel;
319         }
320         else if (standing_on_consumable(t))
321         {
322             t->command = get_thing_action_id_by_name(s[S_CMD_PICKUP]);
323         }
324         else
325         {
326             get_dir_to_nearest_thing(t, 'c');
327         }
328     }
329 }