1 /* Definitions common to all AIs. 2 Copyright (C) 1992-1997, 1999-2000 Stanley T. Shebs. 3 Copyright (C) 2005 Eric A. McDonald. 4 5 Xconq is free software; you can redistribute it and/or modify 6 it under the terms of the GNU General Public License as published by 7 the Free Software Foundation; either version 2, or (at your option) 8 any later version. See the file COPYING. */ 9 10 /*! \file ai.h 11 * Declaration of AI structs, functions, and enums. 12 */ 13 14 /*! Game class */ 15 /*! This enum defines the type of game for the AI 16 */ 17 typedef enum a_game_class { 18 gc_none, /*!< No game class defined */ 19 gc_standard, /*!< Standard game class. */ 20 gc_time, /*!< Time-like game class. */ 21 gc_advanced /*!< Advanced (game with advances?) game class */ 22 } GameClass; 23 24 /*! AI operation? */ 25 typedef struct a_ai_op { 26 char *name; 27 char *help; 28 int (*to_test_compat)(void); 29 void (*to_init)(Side *side); 30 void (*to_init_turn)(Side *side); 31 void (*to_decide_plan)(Side *side, Unit *unit); 32 void (*to_react_to_task_result)(Side *side, Unit *unit, 33 Task *task, TaskOutcome rslt); 34 void (*to_react_to_new_side)(Side *side, Side *side2); 35 int (*to_adjust_plan)(Side *side, Unit *unit); 36 void (*to_finish_movement)(Side *side); 37 Obj *(*to_save_state)(Side *side); 38 int (*region_at)(Side *side, int x, int y); 39 char *(*at_desig)(Side *side, int x, int y); 40 } AI_ops; 41 42 /* Definition common to all ai types. (?) */ 43 44 typedef struct a_ai { 45 int dummy; 46 } AI; 47 48 #define side_ai_type(s) ((s)->aitype) 49 50 /* Limit on the number of goals that a side may have. */ 51 52 #define MAXGOALS 30 53 54 /* Limit on the number of theaters a single side may have. */ 55 56 #define MAXTHEATERS 98 57 58 /* Strategy is what a side uses to make decisions. */ 59 60 typedef struct a_strategy { 61 int trytowin; 62 int report_not_understood; 63 int creationdate; 64 short *strengths[MAXSIDES + 1]; /* estimated numbers of units */ 65 short points[MAXSIDES + 1]; /* estimated point value */ 66 short *alstrengths[MAXSIDES + 1]; /* numbers in alliances */ 67 short alpoints[MAXSIDES + 1]; /* points in alliances */ 68 short initial_strengths_computed; 69 short *strengths0[MAXSIDES + 1]; /* initial estimated numbers of units */ 70 short points0[MAXSIDES + 1]; /* initial estimated point value */ 71 short *alstrengths0[MAXSIDES + 1]; /* initial numbers in alliances */ 72 short alpoints0[MAXSIDES + 1]; /* initial points in alliances */ 73 short contacted[MAXSIDES+1]; 74 short homefound[MAXSIDES+1]; 75 int analyzegame; 76 struct a_theater *theaters; 77 struct a_theater **theatertable; 78 short numtheaters; 79 char *areatheaters; 80 struct a_theater *homefront; 81 struct a_theater *perimeters[NUMDIRS]; 82 struct a_theater *midranges[NUMDIRS]; 83 struct a_theater *remotes[NUMDIRS]; 84 int numgoals; 85 struct a_goal *goals[MAXGOALS]; 86 /* Exploration and search slots. */ 87 int zonewidth, zoneheight; 88 int numzonex, numzoney; /* dimensions of search zone array */ 89 int numzones; 90 struct a_searchzone *searchzones; 91 short *explorertypes; 92 short explorersneeded; 93 short *terrainguess; 94 short cx, cy; /* "centroid" of all our units */ 95 short *demand; /* worth of each utype w.r.t. strategy */ 96 int explore_priority; 97 int defend_priority; 98 int attack_priority; 99 struct a_unit **unitlist; /* lists to help mplay efficiency */ 100 short *unitlistcount; /* counts of above lists */ 101 short *actualmix; 102 short *expectedmix; 103 short *idealmix; 104 short *develop_status; /* specific to the "time" game */ 105 short *develop_on; /* specific to the "time" game */ 106 Obj *writable_state; 107 } Strategy; 108 109 /* A Theater is a sub-area that can be planned for all at once. */ 110 111 /* To save space in theater layer, no more than 127 theaters may exist 112 at once. This should be sufficient, even a Napoleon would have 113 trouble keeping track of that much activity. */ 114 115 typedef struct a_theater { 116 short id; 117 char *name; /* an informative name for this theater */ 118 short x, y; /* center of the theater */ 119 short xmin, ymin; 120 short xmax, ymax; 121 int size; /* number of cells in the theater */ 122 short importance; /* 0 = shrug, 100 = critical */ 123 Goal *maingoal; 124 short allied_units; /* How many units on our side here. */ 125 short makers; /* Total number of makers */ 126 short unexplored; /* number of unseen cells in theater */ 127 short allied_bases; /* total number of our bases, includes towns */ 128 short border; /* true if this is a border theater. */ 129 short reinforce; /* priority on request for units. */ 130 short *numassigned; /* num of each type assigned to theater */ 131 short *numneeded; /* units we should move to theater. */ 132 short *numtotransport; /* types needing transportation. */ 133 short *numenemies; 134 short *numsuspected; 135 short *numsuspectedmax; 136 int *people; /* number of populated cells seen */ 137 int enemystrengthmin; /* estimate of enemy unit strength */ 138 int enemystrengthmax; /* estimate of enemy unit strength */ 139 short units_lost; /* How many units have we lost here. */ 140 struct a_theater *next; /* pointer to the next theater */ 141 } Theater; 142 143 #define ai(s) ((Strategy *) (s)->ai) 144 145 #define strength_est(side,side2,u) ((ai(side)->strengths[side2->id])[u]) 146 #define allied_strength_est(side,side2,u) ((ai(side)->alstrengths[side2->id])[u]) 147 #define strength0_est(side,side2,u) ((ai(side)->strengths0[side2->id])[u]) 148 #define allied_strength0_est(side,side2,u) ((ai(side)->alstrengths0[side2->id])[u]) 149 150 #define for_all_theaters(s,th) \ 151 for ((th) = ai(s)->theaters; (th) != NULL; (th) = (th)->next) \ 152 153 #define theater_at(s,x,y) \ 154 (ai(s)->theaters ? ai(s)->theatertable[((int) ai(s)->areatheaters[(x)+area.width*(y)])] : NULL) 155 156 #define set_theater_at(s,x,y,th) \ 157 ((ai(s)->areatheaters[(x)+area.width*(y)]) = (th)->id) 158 159 #define for_all_cells_in_theater(s,x,y,th) { \ 160 for ((x) = theater->xmin; (x) < theater->xmax; ++(x)) \ 161 for ((y) = theater->ymin; (y) < theater->ymax; ++(y)) \ 162 if (theater_at((s), (x), (y)) == (th)) } 163 164 #define unit_theater(unit) ((Theater *) (unit)->aihook) 165 166 #define set_unit_theater(unit,theater) ((unit)->aihook = (char *) (theater)) 167 168 /* utype-specific develop status codes for the "time" game */ 169 170 #define RS_DEVELOP_NEEDED 4 171 #define RS_DEVELOP_ASSIGNED 3 172 #define RS_UPGRADE_NEEDED 1 173 174 struct weightelt { 175 int weight; 176 long data; 177 }; 178 179 extern GameClass game_class; 180 extern int bhw_max; 181 182 /* Common functions shared between ai.c and specific AIs. */ 183 184 extern void try_to_draw(Side *side, int flag, char *ainame); 185 extern void give_up(Side *side, char *ainame); 186 extern int goal_truth(Side *side, Goal *goal); 187 extern int accelerable(int u); 188 extern int accelerator(int u1, int u2); 189 extern GameClass find_game_class(void); 190 extern void update_unit_plans(Side *side); 191 extern void update_unit_plans_randomly(Side *side); 192 extern int need_this_type_to_collect(Side *side, int u, int m); 193 extern void assign_to_collection(Side *side, Unit *unit, int m); 194 extern void assign_to_improve(Side *side, Unit *unit); 195 extern int assign_to_colonize(Side *side, Unit *unit); 196 extern void assign_to_defend_unit(Unit *unit, Unit *unit2); 197 extern void assign_to_exploration(Side *side, Unit *unit); 198 extern void assign_explorer_to_theater( 199 Side *side, Unit *unit, Theater *theater); 200 extern void assign_to_explorer_construction(Side *side, Unit *unit); 201 extern void assign_to_offense(Side *side, Unit *unit); 202 extern void assign_to_offense_support(Side *side, Unit *unit); 203 extern void assign_to_colonization_support(Side *side, Unit *unit); 204 extern void assign_to_defense(Side *side, Unit *unit); 205 extern void assign_to_defense_support(Side *side, Unit *unit); 206 extern int preferred_build_type(Side *side, Unit *unit, int plantype); 207 extern int can_develop_on(int u, int u2); 208 extern int needs_develop(Side *side, int u); 209 extern int assign_to_develop_on(Side *side, Unit *unit, int u2); 210 extern int build_depot_for_self(Unit *unit); 211 extern int desired_direction_impassable(Unit *unit, int x, int y); 212 extern int could_be_ferried(Unit *unit, int x, int y); 213 extern int blocked_by_enemy(Unit *unit, int x, int y, int shortest); 214 extern int attack_blockage(Side *side, Unit *unit, int x, int y, int shortest); 215 extern int enemy_close_by(Side *side, Unit *unit, int dist, int *xp, int *yp); 216 extern int suitable_port(Unit *unit); 217 extern int need_more_transportation(Side *side); 218 extern int need_explorers(Side *side); 219 extern void add_goal(Side *side, Goal *goal); 220 extern Goal *has_goal(Side *side, GoalType goaltype); 221 extern Goal *has_unsatisfied_goal(Side *side, GoalType goaltype); 222 223 #if 0 /* Unused functions. */ 224 225 extern void assign_to_defend_cell(Unit *unit, int x, int y); 226 extern void assign_to_defend_vicinity(Unit *unit, int x, int y, int w, int h); 227 extern void assign_to_defend_theater(Unit *unit, Theater *theater); 228 extern int build_base_for_self(Side *side, Unit *unit); 229 extern int build_base_for_others(Side *side, Unit *unit); 230 231 #endif 232