home · contact · privacy
Server: Minor field of view code simplifications.
[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, 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" /* struct Thing */
11 #include "world.h" /* global 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
43
44 static void get_neighbor_scores(uint16_t * score_map, uint16_t pos_i,
45                                 uint16_t max_score, uint16_t * neighbors)
46 {
47     uint32_t map_size = world.map.length * world.map.length;
48     uint8_t i_dir;
49     for (i_dir = 0; i_dir < N_DIRS; neighbors[i_dir] = max_score, i_dir++);
50     uint8_t open_north     = pos_i >= world.map.length;
51     uint8_t open_east      = pos_i + 1 % world.map.length;
52     uint8_t open_south     = pos_i + world.map.length < map_size;
53     uint8_t open_west      = pos_i % world.map.length;
54     uint8_t is_indented    = (pos_i / world.map.length) % 2;
55     uint8_t open_diag_west = is_indented || open_west;
56     uint8_t open_diag_east = !is_indented || open_east;
57     if (open_north && open_diag_east)
58     {
59         neighbors[0] = score_map[pos_i - world.map.length + is_indented];
60     }
61     if (open_east)
62     {
63         neighbors[1] = score_map[pos_i + 1];
64     }
65     if (open_south && open_diag_east)
66     {
67         neighbors[2] = score_map[pos_i + world.map.length + is_indented];
68     }
69     if (open_south && open_diag_west)
70     {
71         neighbors[3] = score_map[pos_i + world.map.length - !is_indented];
72     }
73     if (open_west)
74     {
75         neighbors[4] = score_map[pos_i - 1];
76     }
77     if (open_north && open_diag_west)
78     {
79         neighbors[5] = score_map[pos_i - world.map.length - !is_indented];
80     }
81 }
82
83
84
85 static void dijkstra_map(uint16_t * score_map, uint16_t max_score)
86 {
87     uint32_t map_size = world.map.length * world.map.length;
88     uint32_t pos;
89     uint16_t i_scans, neighbors[N_DIRS], min_neighbor;
90     uint8_t scores_still_changing = 1;
91     uint8_t i_dirs;
92     for (i_scans = 0; scores_still_changing; i_scans++)
93     {
94         scores_still_changing = 0;
95         for (pos = 0; pos < map_size; pos++)
96         {
97             if (score_map[pos] <= max_score)
98             {
99                 get_neighbor_scores(score_map, pos, max_score, neighbors);
100                 min_neighbor = max_score;
101                 for (i_dirs = 0; i_dirs < N_DIRS; i_dirs++)
102                 {
103                     if (min_neighbor > neighbors[i_dirs])
104                     {
105                         min_neighbor = neighbors[i_dirs];
106                     }
107                 }
108                 if (score_map[pos] > min_neighbor + 1)
109                 {
110                     score_map[pos] = min_neighbor + 1;
111                     scores_still_changing = 1;
112                 }
113             }
114         }
115     }
116 }
117
118
119
120 static char get_dir_to_nearest_enemy(struct Thing * t_origin)
121 {
122     /* Calculate for each cell distance to visibly nearest enemy, with movement
123      * possible in the directions or "dir". (Actor's own cells start with 0
124      * distance towards themselves. Cells of actors of own type are invisible.)
125      */
126     uint32_t map_size = world.map.length * world.map.length;
127     uint16_t max_score = UINT16_MAX - 1;
128     uint16_t * score_map = try_malloc(map_size * sizeof(uint16_t), __func__);
129     uint32_t i;
130     for (i = 0; i < map_size; i++)
131     {
132         score_map[i] = UINT16_MAX;
133         if ('.' == t_origin->mem_map[i])
134         {
135             score_map[i] = max_score;
136         }
137     }
138     struct Thing * t = world.things;
139     for (; t != NULL; t = t->next)
140     {
141         if (   !t->lifepoints || t == t_origin
142             || 'H' == t_origin->fov_map[t->pos.y * world.map.length + t->pos.x])
143         {
144             continue;
145         }
146         if (t->lifepoints && t->type == t_origin->type)
147         {
148             score_map[t->pos.y * world.map.length + t->pos.x] = UINT16_MAX;
149             continue;
150         }
151         score_map[t->pos.y * world.map.length + t->pos.x] = 0;
152     }
153     dijkstra_map(score_map, max_score);
154
155     /* Return direction of "t_origin"'s lowest-scored neighbor cell. */
156     uint16_t neighbors[N_DIRS];
157     uint16_t pos_i = (t_origin->pos.y * world.map.length) + t_origin->pos.x;
158     get_neighbor_scores(score_map, pos_i, max_score, neighbors);
159     free(score_map);
160     char dir_to_nearest_enemy = 0;
161     uint16_t min_neighbor = max_score;
162     char * dirs = "edcxsw";    /* get_neighbor_scores()'s clockwise dir order.*/
163     for (i = 0; i < N_DIRS; i++)
164     {
165         if (min_neighbor > neighbors[i])
166         {
167             min_neighbor = neighbors[i];
168             dir_to_nearest_enemy = dirs[i];
169         }
170     }
171     return dir_to_nearest_enemy;
172 }
173
174
175
176 extern void ai(struct Thing * t)
177 {
178     t->command = get_thing_action_id_by_name(s[S_CMD_WAIT]);
179     char sel = t->fov_map ? get_dir_to_nearest_enemy(t) : 0;/* t->fov_map may */
180     if (0 != sel)                                           /* be absent due  */
181     {                                                       /* to god command.*/
182         t->command = get_thing_action_id_by_name(s[S_CMD_MOVE]);
183         t->arg = sel;
184     }
185 }