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