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