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