home · contact · privacy
b761c6bbbc8039fd2ba75908228f3b006ed9bc8a
[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 "field_of_view.h" /* VISIBLE */
9 #include "hardcoded_strings.h" /* s */
10 #include "thing_actions.h" /* get_thing_action_id_by_name() */
11 #include "things.h" /* struct Thing */
12 #include "world.h" /* global world */
13
14
15
16 #define N_DIRS 6
17
18
19
20 /* Write into "neighbors" scores of the N_DIRS immediate neighbors of the
21  * "score_map" cell at "pos_i" (array index), as found in the directions
22  * north-east, east, south-east etc. (clockwise order). Use "max_score" for
23  * illegal neighborhoods (i.e. if direction would lead beyond the map's border).
24  */
25 static void get_neighbor_scores(uint16_t * score_map, uint16_t pos_i,
26                                 uint16_t max_score, uint16_t * neighbors);
27
28 /* Iterate over scored cells in "score_map" of world.map's geometry. Compare
29  * each cell's score against the score of its immediate neighbors in N_DIRS
30  * directions. If it's neighbors are low enough that the result would be lower
31  * than the current value, re-set it to 1 point higher than its lowest-scored
32  * neighbor. Repeat this whole process until all cells have settled on their
33  * final score. Ignore cells whose position in "score_map" fits cells of
34  * unreachable terrain in world.map.cells or whose score is greater than
35  * "max_score". Expect "max_score" to be the maximum score for cells, marking
36  * them as unreachable.
37  */
38 static void dijkstra_map(uint16_t * score_map, uint16_t max_score);
39
40 /* Return numpad char of direction ("8", "6", "2", "4" etc.) of enemy with the
41  * shortest path visible to "t_origin". If no enemy is around, return 0.
42  */
43 static char get_dir_to_nearest_enemy(struct Thing * thing_origin);
44
45
46
47 static void get_neighbor_scores(uint16_t * score_map, uint16_t pos_i,
48                                 uint16_t max_score, uint16_t * neighbors)
49 {
50     uint32_t map_size = world.map.length * world.map.length;
51     uint8_t i_dir;
52     for (i_dir = 0; i_dir < N_DIRS; neighbors[i_dir] = max_score, i_dir++);
53     uint8_t open_north     = pos_i >= world.map.length;
54     uint8_t open_east      = pos_i + 1 % world.map.length;
55     uint8_t open_south     = pos_i + world.map.length < map_size;
56     uint8_t open_west      = pos_i % world.map.length;
57     uint8_t is_indented    = (pos_i / world.map.length) % 2;
58     uint8_t open_diag_west = is_indented || open_west;
59     uint8_t open_diag_east = !is_indented || open_east;
60     if (open_north && open_diag_east)
61     {
62         neighbors[0] = score_map[pos_i - world.map.length + is_indented];
63     }
64     if (open_east)
65     {
66         neighbors[1] = score_map[pos_i + 1];
67     }
68     if (open_south && open_diag_east)
69     {
70         neighbors[2] = score_map[pos_i + world.map.length + is_indented];
71     }
72     if (open_south && open_diag_west)
73     {
74         neighbors[3] = score_map[pos_i + world.map.length - !is_indented];
75     }
76     if (open_west)
77     {
78         neighbors[4] = score_map[pos_i - 1];
79     }
80     if (open_north && open_diag_west)
81     {
82         neighbors[5] = score_map[pos_i - world.map.length - !is_indented];
83     }
84 }
85
86
87
88 static void dijkstra_map(uint16_t * score_map, uint16_t max_score)
89 {
90     uint32_t map_size = world.map.length * world.map.length;
91     uint32_t pos;
92     uint16_t i_scans, neighbors[N_DIRS], min_neighbor;
93     uint8_t scores_still_changing = 1;
94     uint8_t i_dirs;
95     for (i_scans = 0; scores_still_changing; i_scans++)
96     {
97         scores_still_changing = 0;
98         for (pos = 0; pos < map_size; pos++)
99         {
100             if (score_map[pos] <= max_score)
101             {
102                 get_neighbor_scores(score_map, pos, max_score, neighbors);
103                 min_neighbor = max_score;
104                 for (i_dirs = 0; i_dirs < N_DIRS; i_dirs++)
105                 {
106                     if (min_neighbor > neighbors[i_dirs])
107                     {
108                         min_neighbor = neighbors[i_dirs];
109                     }
110                 }
111                 if (score_map[pos] > min_neighbor + 1)
112                 {
113                     score_map[pos] = min_neighbor + 1;
114                     scores_still_changing = 1;
115                 }
116             }
117         }
118     }
119 }
120
121
122
123 static char get_dir_to_nearest_enemy(struct Thing * t_origin)
124 {
125     /* Calculate for each cell distance to visibly nearest enemy, with movement
126      * possible in the directions or "dir". (Actor's own cells start with 0
127      * distance towards themselves. Cells of actors of own type are invisible.)
128      */
129     uint32_t map_size = world.map.length * world.map.length;
130     uint16_t max_score = UINT16_MAX - 1;
131     uint16_t * score_map = try_malloc(map_size * sizeof(uint16_t), __func__);
132     uint32_t i;
133     for (i = 0; i < map_size; i++)
134     {
135         score_map[i] = UINT16_MAX;
136         if (t_origin->fov_map[i] & VISIBLE && world.map.cells[i] == '.')
137         {
138             score_map[i] = max_score;
139         }
140     }
141     struct Thing * t = world.things;
142     for (; t != NULL; t = t->next)
143     {
144         if (!t->lifepoints || t == t_origin)
145         {
146             continue;
147         }
148         if (t->lifepoints && t->type == t_origin->type)
149         {
150             score_map[t->pos.y * world.map.length + t->pos.x] = UINT16_MAX;
151             continue;
152         }
153         score_map[t->pos.y * world.map.length + t->pos.x] = 0;
154     }
155     dijkstra_map(score_map, max_score);
156
157     /* Return direction of "t_origin"'s lowest-scored neighbor cell. */
158     uint16_t neighbors[N_DIRS];
159     uint16_t pos_i = (t_origin->pos.y * world.map.length) + t_origin->pos.x;
160     get_neighbor_scores(score_map, pos_i, max_score, neighbors);
161     free(score_map);
162     char dir_to_nearest_enemy = 0;
163     uint16_t min_neighbor = max_score;
164     char * dirs = "edcxsw";    /* get_neighbor_scores()'s clockwise dir order.*/
165     for (i = 0; i < N_DIRS; i++)
166     {
167         if (min_neighbor > neighbors[i])
168         {
169             min_neighbor = neighbors[i];
170             dir_to_nearest_enemy = dirs[i];
171         }
172     }
173     return dir_to_nearest_enemy;
174 }
175
176
177
178 extern void ai(struct Thing * t)
179 {
180     t->command = get_thing_action_id_by_name(s[S_CMD_WAIT]);
181     char sel = t->fov_map ? get_dir_to_nearest_enemy(t) : 0;/* t->fov_map may */
182     if (0 != sel)                                           /* be absent due  */
183     {                                                       /* to god command.*/
184         t->command = get_thing_action_id_by_name(s[S_CMD_MOVE]);
185         t->arg = sel;
186     }
187 }