4 #include <stdlib.h> /* for free() */
5 #include <string.h> /* for strlen(), memset() */
6 #include <stdint.h> /* for uint8_t */
7 #include "main.h" /* for world global */
8 #include "misc.h" /* for try_malloc() */
9 #include "map.h" /* for Map struct */
10 #include "yx_uint16.h" /* for yx_uint16 struct,yx_uint16_cmp(),mv_yx_in_dir() */
11 #include "map_objects.h" /* for MapObj struct */
12 #include "map_object_actions.h" /* for get_moa_id_by_name() */
16 /* Change cardinal direction string ("NNE" etc.) of any length >1 pointed to by
17 * "path_ptr" one step clockwise ("NNE" -> "NEE", "NEE" -> "EEE" etc.).
19 static void clockwise_path(char ** path_ptr);
21 /* Return dir char (north = "N" etc.) to enemy nearest to "origin" (beeline). */
22 static char nearest_enemy_dir(struct yx_uint16 origin);
26 static void clockwise_path(char ** path_ptr)
28 char * path = * path_ptr;
29 char old_char = path[0];
35 else if ('E' == old_char)
39 else if ('S' == old_char)
43 uint8_t len = strlen(path);
47 uint8_t next_i = i + 1;
48 if (next_i == len || old_char != path[next_i])
58 static char nearest_enemy_dir(struct yx_uint16 origin)
60 char * f_name = "nearest_enemy_dir()";
63 uint16_t dist_max = world.map->size.y;
64 if (world.map->size.x > world.map->size.y)
66 dist_max = world.map->size.x;
70 for (dist = 1; !escape && dist <= dist_max; dist++)
72 char * path = try_malloc(dist + 1, f_name);
73 memset(path, 'N', dist);
75 for (i = 0; !escape && i < (dist * 4); i++)
77 clockwise_path(&path);
78 struct yx_uint16 testpos = origin;
79 for (j = 0; j < dist; j++)
81 testpos = mv_yx_in_dir(path[j], testpos);
83 if (yx_uint16_cmp(&testpos, &origin) ||
84 testpos.y > world.map->size.y || testpos.x > world.map->size.x)
88 for (mo = world.map_objs; mo != 0; mo = mo->next)
90 if (mo->lifepoints && 1 == yx_uint16_cmp(&testpos, &mo->pos))
105 extern void pretty_dumb_ai(struct MapObj * mo)
107 mo->command = get_moa_id_by_name("wait");
108 char sel = nearest_enemy_dir(mo->pos);
111 mo->command = get_moa_id_by_name("move");