home · contact · privacy
Server/AI: Pick up consumable if standing on it and no enemies in FOV.
[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 /* Return numpad char of direction ("8", "6", "2", "4" etc.) of enemy with the
38  * shortest path visible to "t_origin". If no enemy is around, return 0.
39  */
40 static char get_dir_to_nearest_enemy(struct Thing * thing_origin);
41
42 /* Return 1 if any animate thing not "t_origin" is in its FOV, else 0. */
43 static uint8_t seeing_enemies(struct Thing * t_origin);
44
45 /* Return slot ID of strongest consumable in "t_owner"'s inventory, else -1. */
46 static int16_t get_inventory_slot_to_consume(struct Thing * t_owner);
47
48 /* Return 1 if "t_standing" is standing on a consumable, else 0. */
49 static uint8_t standing_on_consumable(struct Thing * t_standing);
50
51
52
53 static void get_neighbor_scores(uint16_t * score_map, uint16_t pos_i,
54                                 uint16_t max_score, uint16_t * neighbors)
55 {
56     uint32_t map_size = world.map.length * world.map.length;
57     uint8_t i_dir;
58     for (i_dir = 0; i_dir < N_DIRS; neighbors[i_dir] = max_score, i_dir++);
59     uint8_t open_north     = pos_i >= world.map.length;
60     uint8_t open_east      = pos_i + 1 % world.map.length;
61     uint8_t open_south     = pos_i + world.map.length < map_size;
62     uint8_t open_west      = pos_i % world.map.length;
63     uint8_t is_indented    = (pos_i / world.map.length) % 2;
64     uint8_t open_diag_west = is_indented || open_west;
65     uint8_t open_diag_east = !is_indented || open_east;
66     if (open_north && open_diag_east)
67     {
68         neighbors[0] = score_map[pos_i - world.map.length + is_indented];
69     }
70     if (open_east)
71     {
72         neighbors[1] = score_map[pos_i + 1];
73     }
74     if (open_south && open_diag_east)
75     {
76         neighbors[2] = score_map[pos_i + world.map.length + is_indented];
77     }
78     if (open_south && open_diag_west)
79     {
80         neighbors[3] = score_map[pos_i + world.map.length - !is_indented];
81     }
82     if (open_west)
83     {
84         neighbors[4] = score_map[pos_i - 1];
85     }
86     if (open_north && open_diag_west)
87     {
88         neighbors[5] = score_map[pos_i - world.map.length - !is_indented];
89     }
90 }
91
92
93
94 static void dijkstra_map(uint16_t * score_map, uint16_t max_score)
95 {
96     uint32_t map_size = world.map.length * world.map.length;
97     uint32_t pos;
98     uint16_t i_scans, neighbors[N_DIRS], min_neighbor;
99     uint8_t scores_still_changing = 1;
100     uint8_t i_dirs;
101     for (i_scans = 0; scores_still_changing; i_scans++)
102     {
103         scores_still_changing = 0;
104         for (pos = 0; pos < map_size; pos++)
105         {
106             if (score_map[pos] <= max_score)
107             {
108                 get_neighbor_scores(score_map, pos, max_score, neighbors);
109                 min_neighbor = max_score;
110                 for (i_dirs = 0; i_dirs < N_DIRS; i_dirs++)
111                 {
112                     if (min_neighbor > neighbors[i_dirs])
113                     {
114                         min_neighbor = neighbors[i_dirs];
115                     }
116                 }
117                 if (score_map[pos] > min_neighbor + 1)
118                 {
119                     score_map[pos] = min_neighbor + 1;
120                     scores_still_changing = 1;
121                 }
122             }
123         }
124     }
125 }
126
127
128
129 static char get_dir_to_nearest_enemy(struct Thing * t_origin)
130 {
131     /* Calculate for each cell distance to visibly nearest enemy, with movement
132      * possible in the directions or "dir". (Actor's own cells start with 0
133      * distance towards themselves. Cells of actors of own type are invisible.)
134      */
135     uint32_t map_size = world.map.length * world.map.length;
136     uint16_t max_score = UINT16_MAX - 1;
137     uint16_t * score_map = try_malloc(map_size * sizeof(uint16_t), __func__);
138     uint32_t i;
139     for (i = 0; i < map_size; i++)
140     {
141         score_map[i] = UINT16_MAX;
142         if ('.' == t_origin->mem_map[i])
143         {
144             score_map[i] = max_score;
145         }
146     }
147     struct Thing * t = world.things;
148     for (; t != NULL; t = t->next)
149     {
150         if (   !t->lifepoints || t == t_origin
151             || 'H' == t_origin->fov_map[t->pos.y * world.map.length + t->pos.x])
152         {
153             continue;
154         }
155         if (t->lifepoints && t->type == t_origin->type)
156         {
157             score_map[t->pos.y * world.map.length + t->pos.x] = UINT16_MAX;
158             continue;
159         }
160         score_map[t->pos.y * world.map.length + t->pos.x] = 0;
161     }
162     dijkstra_map(score_map, max_score);
163
164     /* Return direction of "t_origin"'s lowest-scored neighbor cell. */
165     uint16_t neighbors[N_DIRS];
166     uint16_t pos_i = (t_origin->pos.y * world.map.length) + t_origin->pos.x;
167     get_neighbor_scores(score_map, pos_i, max_score, neighbors);
168     free(score_map);
169     char dir_to_nearest_enemy = 0;
170     uint16_t min_neighbor = max_score;
171     char * dirs = "edcxsw";    /* get_neighbor_scores()'s clockwise dir order.*/
172     for (i = 0; i < N_DIRS; i++)
173     {
174         if (min_neighbor > neighbors[i])
175         {
176             min_neighbor = neighbors[i];
177             dir_to_nearest_enemy = dirs[i];
178         }
179     }
180     return dir_to_nearest_enemy;
181 }
182
183
184
185 static uint8_t seeing_enemies(struct Thing * t_origin)
186 {
187     struct Thing * t = world.things;
188     for (; t != NULL; t = t->next)
189     {
190         if (   t->lifepoints
191             && t != t_origin
192             && 'v' == t_origin->fov_map[t->pos.y * world.map.length + t->pos.x])
193         {
194             return 1;
195         }
196     }
197     return 0;
198 }
199
200
201
202 static int16_t get_inventory_slot_to_consume(struct Thing * t_owner)
203 {
204     uint8_t compare_consumability = 0;
205     int16_t selection = -1;
206     struct Thing * t = t_owner->owns;;
207     uint8_t i;
208     for (i = 0; t != NULL; t = t->next, i++)
209     {
210         struct ThingType * tt = get_thing_type(t->type);
211         if (tt->consumable > compare_consumability)
212         {
213             compare_consumability = tt->consumable;
214             selection = i;
215         }
216     }
217     return selection;
218 }
219
220
221
222 static uint8_t standing_on_consumable(struct Thing * t_standing)
223 {
224     struct Thing * t = world.things;
225     for (; t != NULL; t = t->next)
226     {
227         if (t->pos.y == t_standing->pos.y && t->pos.x && t_standing->pos.x)
228         {
229             struct ThingType * tt = get_thing_type(t->type);
230             if (tt->consumable)
231             {
232                 return 1;
233             }
234         }
235     }
236     return 0;
237 }
238
239
240
241 extern void ai(struct Thing * t)
242 {
243     t->command = get_thing_action_id_by_name(s[S_CMD_WAIT]);
244     if (seeing_enemies(t))
245     {
246         char sel = t->fov_map ? get_dir_to_nearest_enemy(t) : 0;
247         if (0 != sel)
248         {
249             t->command = get_thing_action_id_by_name(s[S_CMD_MOVE]);
250             t->arg = sel;
251         }
252     }
253     else
254     {
255         int16_t sel = get_inventory_slot_to_consume(t);
256         if (-1 != sel)
257         {
258             t->command = get_thing_action_id_by_name(s[S_CMD_USE]);
259             t->arg = (uint8_t) sel;
260         }
261         else if (standing_on_consumable(t))
262         {
263             t->command = get_thing_action_id_by_name(s[S_CMD_PICKUP]);
264         }
265     }
266 }