home · contact · privacy
Make grids hexagonal, remove all diagonal movement penalty hassle.
[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 "map_object_actions.h" /* get_moa_id_by_name() */
9 #include "map_objects.h" /* struct MapObj */
10 #include "world.h" /* global 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 it's neighbors are low enough that the result would be lower
29  * than the current value, 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 position in "score_map" fits cells of
32  * unreachable terrain in world.map.cells. Expect "max_score" to be the maximum
33  * 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 to "mo_origin". If no enemy is around, return 0.
39  */
40 static char get_dir_to_nearest_enemy(struct MapObj * mo_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.size.y * world.map.size.x;
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.size.x;
51     uint8_t open_east      = pos_i + 1 % world.map.size.x;
52     uint8_t open_south     = pos_i + world.map.size.x < map_size;
53     uint8_t open_west      = pos_i % world.map.size.x;
54     uint8_t is_indented    = (pos_i / world.map.size.x) % 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.size.x + 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.size.x + is_indented];
68     }
69     if (open_south && open_diag_west)
70     {
71         neighbors[3] = score_map[pos_i + world.map.size.x - !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.size.x - !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.size.y * world.map.size.x;
88     uint16_t pos, i_scans, neighbors[N_DIRS], min_neighbor;
89     uint8_t scores_still_changing = 1;
90     uint8_t i_dirs;
91     for (i_scans = 0; scores_still_changing; i_scans++)
92     {
93         scores_still_changing = 0;
94         for (pos = 0; pos < map_size; pos++)
95         {
96             if ('.' == world.map.cells[pos])
97             {
98                 get_neighbor_scores(score_map, pos, max_score, neighbors);
99                 min_neighbor = max_score;
100                 for (i_dirs = 0; i_dirs < N_DIRS; i_dirs++)
101                 {
102                     if (min_neighbor > neighbors[i_dirs])
103                     {
104                         min_neighbor = neighbors[i_dirs];
105                     }
106                 }
107                 if (score_map[pos] > min_neighbor + 1)
108                 {
109                     score_map[pos] = min_neighbor + 1;
110                     scores_still_changing = 1;
111                 }
112             }
113         }
114     }
115 }
116
117
118
119 static char get_dir_to_nearest_enemy(struct MapObj * mo_origin)
120 {
121     char * f_name = "get_dir_to_nearest_enemy()";
122
123     /* Calculate for each cell the distance to the nearest map actor that is
124      * not "mo_origin", with movement only possible in the directions of "dir".
125      * (Actors' own cells start with a distance of 0 towards themselves.)
126      */
127     uint32_t map_size = world.map.size.y * world.map.size.x;
128     uint16_t max_score = UINT16_MAX;
129     uint16_t * score_map = try_malloc(map_size * sizeof(uint16_t), f_name);
130     uint32_t i;
131     for (i = 0; i < map_size; i++)
132     {
133         score_map[i] = max_score;
134     }
135     struct MapObj * mo = world.map_objs;
136     for (; mo != NULL; mo = mo->next)
137     {
138         if (!mo->lifepoints || mo == mo_origin)
139         {
140             continue;
141         }
142         score_map[(mo->pos.y * world.map.size.x) + mo->pos.x] = 0;
143     }
144     dijkstra_map(score_map, max_score);
145
146     /* Return direction of "mo_origin"'s lowest-scored neighbor cell. */
147     uint16_t neighbors[N_DIRS];
148     uint16_t pos_i = (mo_origin->pos.y * world.map.size.x) + mo_origin->pos.x;
149     get_neighbor_scores(score_map, pos_i, max_score, neighbors);
150     free(score_map);
151     char dir_to_nearest_enemy = 0;
152     uint16_t min_neighbor = max_score;
153     char * dirs = "edcxsw";    /* get_neighbor_scores()'s clockwise dir order.*/
154     for (i = 0; i < N_DIRS; i++)
155     {
156         if (min_neighbor > neighbors[i])
157         {
158             min_neighbor = neighbors[i];
159             dir_to_nearest_enemy = dirs[i];
160         }
161     }
162     return dir_to_nearest_enemy;
163 }
164
165
166
167 extern void ai(struct MapObj * mo)
168 {
169     mo->command = get_moa_id_by_name("wait");
170     char sel = get_dir_to_nearest_enemy(mo);
171     if (0 != sel)
172     {
173         mo->command = get_moa_id_by_name("move");
174         mo->arg = sel;
175     }
176 }