1 /* Definitions relating to units in Xconq. 2 Copyright (C) 1987-1989, 1991-2000 Stanley T. Shebs. 3 Copyright (C) 2004-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 kernel/unit.h 11 * \brief Definitions relating to units in Xconq. 12 * 13 * Prototypes, constants, and macros relating to units. 14 */ 15 16 #include "parambox.h" 17 18 /* Advanced unit support. */ 19 20 /*! \brief Advanced \Unit: default size. */ 21 extern short default_size; 22 /*! \brief Advanced \Unit: default used cells. */ 23 extern short default_usedcells; 24 /*! \brief Advanced \Unit: default max cells. */ 25 extern short default_maxcells; 26 /*! \brief Advanced \Unit: default population. */ 27 extern long default_population; 28 29 /* Forward declarations of various structs. */ 30 struct a_unit; 31 struct a_unit_extras; 32 struct a_unit_view; 33 34 /*! \brief Unit. 35 * 36 * The Unit structure should be small, because there may be many of them. 37 * Unit semantics go in this structure, while unit brains go into the 38 * act/plan. Test: a unit that is like a rock and can't do anything at all 39 * just needs basic slots, plan needn't be allocated. Another test: 40 * unit should still function correctly after its current plan has been 41 * destroyed and replae`d with another. 42 * \note unit->side and unit->origside must always point to 43 * valid side objects; dereferences are not checked! 44 */ 45 typedef struct a_unit { 46 short type; /*!< type */ 47 int id; /*!< truly unique id number */ 48 char *name; /*!< the name, if given */ 49 int number; /*!< semi-unique number */ 50 struct a_unit_view *uview; /*!< master view of unit */ 51 struct a_image_family *imf; /*!< the image family */ 52 char *image_name; /*!< the name of the image, if given */ 53 short x; /*!< x position of unit in world */ 54 short y; /*!< y position of unit in world */ 55 short z; /*!< z position of unit in world */ 56 struct a_side *side; /*!< whose side this unit is on */ 57 struct a_side *origside; /*!< the first side this unit was on */ 58 short hp; /*!< how much more damage each part can take */ 59 short hp2; /*!< buffer for next value of hp */ 60 short cp; /*!< state of construction */ 61 short cxp; /*!< combat experience */ 62 short morale; /*!< morale */ 63 struct a_unit *transport; /*!< pointer to transporting unit if any */ 64 SideMask tracking; /*!< which sides always see us (bit vector) */ 65 long *supply; /*!< how much supply we're carrying */ 66 short s_flow; /*!< how much supply we received this turn */ 67 short s_conn; /*!< how well connected we are to supply zones */ 68 short *tooling; /*!< level of preparation for construction */ 69 short *opinions; /*!< opinion of each side, both own and others */ 70 struct a_actorstate *act; /*!< the unit's current actor state */ 71 struct a_plan *plan; /*!< the unit's current plan */ 72 struct a_unit_extras *extras; /*!< pointer to optional stuff */ 73 char *aihook; /*!< used by AI to keep info about this unit */ 74 /* Following slots are never saved. */ 75 struct a_unit *occupant; /*!< pointer to first unit being carried */ 76 struct a_unit *nexthere; /*!< pointer to fellow occupant */ 77 struct a_unit *prev; /*!< previous unit in list of side's units */ 78 struct a_unit *next; /*!< next unit in list of side's units */ 79 struct a_unit *unext; /*!< next unit in list of all units */ 80 short prevx; /*!< x co-odinate of where were we last */ 81 short prevy; /*!< y co-ordinate of where were we last */ 82 Obj *transport_id; /*!< read-in id of transport */ 83 /* (transport_id should be tmp array instead) */ 84 short flags; /*!< assorted flags */ 85 /* (should make optional in UnitExtras?) */ 86 short size; /*!< Abstract size of the unit */ 87 short reach; /*!< Size of the unit's zone of influence */ 88 short usedcells; /*!< Number of cells actually used by unit */ 89 short maxcells; /*!< Max number of cells that unit can use */ 90 short curadvance; /*!< Advance currently being researched. */ 91 long population; /*!< Size of units population */ 92 short *production; /*!< Cache of units last production. */ 93 short cp_stash; /*!< CP's recovered from incomplete build tasks. */ 94 short researchdone; /*!< We are done with research for this turn. */ 95 short busy; /*!< Broadcasted action that has not yet executed. 96 * \note this is NOT the same as has_pending_action(unit), 97 * which tells us that an action has been broadcasted and 98 * then confirmed by the host. The busy flag is set locally 99 * client-side long before that, when the action is first 100 * broadcasted. Its purpose is to filter out double-clicks 101 * which may cause commands to be issued to a dead unit. */ 102 int creation_id; /*!< Id of unit's unbuilt creation. */ 103 } Unit; 104 105 /*! \brief Unit exta options. 106 * 107 * The unit extras structure stores properties that are (usually) 108 * uncommon or special-purpose. It is allocated as needed, access is 109 * through macros, and this does not appear as a separate structure 110 * when saving/restoring. If a value is likely to be filled in for 111 * most or all units in several different game designs, then it should 112 * be in the main structure (even at the cost of extra space), rather 113 * than here. */ 114 typedef struct a_unit_extras { 115 short point_value; /*!< individual point value for this unit */ 116 short appear; /*!< turn of appearance in game */ 117 short appear_var_x; /*!< variation around appearance location */ 118 short appear_var_y; /*!< variation around appearance location */ 119 short disappear; /*!< turn of disappearance from game */ 120 short priority; /*!< individual priority */ 121 Obj *sym; /*!< symbol for xrefs */ 122 Obj *sides; /*!< list of possible sides */ 123 } UnitExtras; 124 125 /*! \brief Unit View. 126 * 127 * The \UnitView is a partial mirror of an actual \Unit, including only 128 * the data that might be known to another side seeing the \Unit from a 129 * distance. */ 130 typedef struct a_unit_view { 131 short observer; /*!< number of observing side */ 132 short type; /*!< type */ 133 short siden; /*!< side number of unit */ 134 char *name; /*!< the name, if given */ 135 struct a_image_family *imf; /*!< the image family */ 136 char *image_name; /*!< the name of the image, if given */ 137 short size; /*!< abstract size */ 138 short x; /*!< x location of view */ 139 short y; /*!< y location of view */ 140 short complete; /*!< true if the underlying \Unit was completed */ 141 short date; /*!< turn on which last updated */ 142 int id; /*!< id of actual \Unit if known */ 143 struct a_unit *unit; /*!< pointer to actual \Unit, if id valid */ 144 struct a_unit_view *transport; /*!< pointer to transport's \UnitView */ 145 struct a_unit_view *occupant; /*!< pointer to 1st occupant's \UnitView */ 146 struct a_unit_view *nexthere; /*!< pointer to next \UnitView 147 at this level if any */ 148 struct a_unit_view *nextinhash; /*!< pointer to next \UnitView in the hash 149 table */ 150 struct a_unit_view *vnext; /*!< pointer to next \UnitView in the 151 global list */ 152 } UnitView; 153 154 /* Unit parameter box. */ 155 struct ParamBoxUnit : public ParamBox { 156 Unit *unit; ParamBoxUnitParamBoxUnit157 ParamBoxUnit() { pboxtype = PBOX_TYPE_UNIT; unit = NULL; } 158 }; 159 160 /* "Unit at" parameter box. */ 161 struct ParamBoxUnitAt : public ParamBoxUnit { 162 int x, y; ParamBoxUnitAtParamBoxUnitAt163 ParamBoxUnitAt() : ParamBoxUnit() { 164 pboxtype = PBOX_TYPE_UNIT_AT; 165 x = -1; y = -1; 166 } 167 }; 168 169 /* Unit-unit parameter box. */ 170 struct ParamBoxUnitUnit : public ParamBox { 171 Unit *unit1, *unit2; ParamBoxUnitUnitParamBoxUnitUnit172 ParamBoxUnitUnit() { 173 pboxtype = PBOX_TYPE_UNIT_UNIT; 174 unit1 = NULL; 175 unit2 = NULL; 176 } 177 }; 178 179 /* Unit-side parameter box. */ 180 struct ParamBoxUnitSide : public ParamBoxUnit { 181 Side *side; ParamBoxUnitSideParamBoxUnitSide182 ParamBoxUnitSide() : ParamBoxUnit() { 183 pboxtype = PBOX_TYPE_UNIT_SIDE; 184 side = NULL; 185 } 186 }; 187 188 /* SeerNode is an useful struct for tracking who sees clearly, 189 and who doesn't. */ 190 191 typedef struct a_seer_node { 192 Unit *seer; 193 int mistakes; 194 } SeerNode; 195 196 /* Parameter boxen related to seers. */ 197 198 struct ParamBoxUnitSideSeers : public ParamBoxUnitSide { 199 SeerNode *seers; ParamBoxUnitSideSeersParamBoxUnitSideSeers200 ParamBoxUnitSideSeers() : ParamBoxUnitSide () { 201 pboxtype = PBOX_TYPE_UNIT_SIDE_SEERS; 202 seers = NULL; 203 } ~ParamBoxUnitSideSeersParamBoxUnitSideSeers204 virtual ~ParamBoxUnitSideSeers() { seers = NULL; } 205 }; 206 207 struct ParamBoxUnitUnitSeers : public ParamBoxUnitUnit { 208 SeerNode *seers; ParamBoxUnitUnitSeersParamBoxUnitUnitSeers209 ParamBoxUnitUnitSeers() : ParamBoxUnitUnit () { 210 pboxtype = PBOX_TYPE_UNIT_UNIT_SEERS; 211 seers = NULL; 212 } ~ParamBoxUnitUnitSeersParamBoxUnitUnitSeers213 virtual ~ParamBoxUnitUnitSeers() { seers = NULL; } 214 }; 215 216 /* The global linked list of all unit views. */ 217 218 extern UnitView *viewlist; 219 220 /* Some convenient macros. */ 221 222 223 /*! \brief Iteration over all units. 224 * 225 * For header to iterat through all \Units. 226 * \note Since it is possible for a unit to change sides and therefore 227 * prev/next pointers while iterating using the macros below, one 228 * must be very careful either that unit sides can't change during 229 * the loop, or else to maintain a temp var that can be used to 230 * repair the iteration. This also applies to units dying. 231 *\param v is the Unit vector list iteration variable. 232 */ 233 #define for_all_units(v) \ 234 for (v = unitlist; v != NULL; v = v->unext) 235 236 /*! \brief Iterate over all units. 237 * 238 * Iteration over all \Units on a given side. 239 * \see for_all_units 240 * \param s is a pointer to a \Side. 241 * \param v is the view iteration parameter. 242 */ 243 #define for_all_side_units(s,v) \ 244 for (v = (s)->unithead->next; v != (s)->unithead; v = v->next) 245 246 /*! \brief Iterate over direct components. 247 * 248 * Iteration over all occupants of a \Unit (but not sub-occupants). 249 * \see for_all_units 250 * \param u1 is pointer to unit list. 251 * \param v is the iteration parameter. 252 */ 253 #define for_all_occupants(u1,v) \ 254 for (v = (u1)->occupant; v != NULL; v = v->nexthere) 255 256 /*! \brief Iterate over ALL components. 257 * 258 * Iterate through all occupants including occupents within occupents within 259 * occupents within \Unit. 260 * This nifty little macro will climb the occupant : nexthere tree and 261 * follow all branches three levels below (u) to find all the occs 262 * within occs within occs within (u). The test for (var) != 263 * (u)->nexthere is to stop the macro from climbing up above (u). 264 * \see for_all_units 265 * \param u is a pointer to the \Unit. 266 * \param var is the iteration varable. 267 */ 268 #define for_all_occs_with_occs(u,var) \ 269 for ((var) = (u)->occupant; \ 270 (var) != NULL && (var) != (u)->nexthere; \ 271 (var) = ((var)->occupant != NULL ? \ 272 (var)->occupant : \ 273 ((var)->nexthere != NULL ? \ 274 (var)->nexthere : \ 275 ((var)->transport != NULL && \ 276 (var)->transport->nexthere != NULL ? \ 277 (var)->transport->nexthere : \ 278 ((var)->transport != NULL && \ 279 (var)->transport->transport != NULL && \ 280 (var)->transport->transport->nexthere != NULL ? \ 281 (var)->transport->transport->nexthere : NULL))))) 282 283 /*! \brief Is valid Unit type? 284 * 285 * Check to see if \Unit is a valid Unit type. 286 * \param unit is a pointer to a \Unit. 287 * \return TRUE if the type of a \Unit is a valid type. 288 */ 289 #define is_unit(unit) ((unit) != NULL && is_unit_type((unit)->type)) 290 291 /*! \brief Is alive? 292 * 293 * Is this unit alive? 294 * \param unit is a pointer to a \Unit. 295 * \return TRUE if the Unit has at least 1 HP. 296 */ 297 #define alive(unit) ((unit)->hp > 0) 298 299 /*! \brief Is independant? 300 * 301 * Is this \Unit independant? 302 * \param unit is a pointer to a \Unit. 303 * \return TRUE if the Unit belongs to an independant. 304 */ 305 #define indep(unit) ((unit)->side == indepside) 306 307 /*! \brief Is complete? 308 * 309 * Has this \Unit been completed? 310 * \param unit is a pointer to a \Unit. 311 * \return TRUE if the Unit has been completed. 312 */ 313 #define completed(unit) \ 314 ((unit)->cp >= completenesses[(unit)->type]) 315 /* ((unit)->cp >= (u_cp((unit)->type) / u_parts((unit)->type))) */ 316 317 /*! \brief Is full sized? 318 * 319 * Is \Unit full sized? ??? 320 * \param unit is a pointer to a \Unit. 321 * \return TRUE if the Unit is full sized. 322 */ 323 #define fullsized(unit) \ 324 ((unit)->cp >= u_cp((unit)->type)) 325 326 /*! \brief Unit altitude. 327 * 328 * Extractor for the actual altitude of an airborne unit. 329 * \param unit is a pointer to a \Unit. 330 * \return altitude of unit or zero. 331 */ 332 #define unit_alt(unit) (((unit)->z & 1) == 0 ? ((unit)->z >> 1) : 0) 333 334 /*! \brief Unit connection. 335 * 336 * Extractor for the connection a unit is on. */ 337 #define unit_conn(unit) (((unit)->z & 1) == 1 ? ((unit)->z >> 1) : NONTTYPE) 338 339 /*! Unit in play. 340 * This is true if the unit is on the board somewhere. 341 * \see is_unit, alive, inside_area. 342 * \param unit is the pointer to the \Unit. 343 * \return TRUE if it is a unit AND it's alive AND it's in a cell on the board. 344 */ 345 #define in_play(unit) \ 346 (is_unit(unit) && alive(unit) && inside_area((unit)->x, (unit)->y)) 347 348 /*! \brief Is Unit Active? 349 * 350 * Is the unit currently available? 351 * \see in_play, completed. 352 * \param unit is the pointer to the \Unit. 353 * \return TRUE if it is in play and completed. 354 */ 355 #define is_active(unit) (in_play(unit) && completed(unit)) 356 357 /*! \brief Is unit located at (x,y)? 358 * 359 * Is unit in play and located at (x,y)? 360 * \see in_play, wrapx. 361 * \param unit is the pointer to the \Unit. 362 * \param x is a possibly unwrapped x co-ordinate. 363 * \param y is a y co-ordinate. 364 * \return TRUE if unit is in play and located at (wrap(x), y). 365 */ 366 #define is_located_at(unit,x,y) (in_play(unit) && (unit)->x == wrapx(x) && (unit)->y == (y)) 367 368 /*! \brief Was unit located at (x,y)? 369 * 370 * Was unit (maybe now dead) located at (x,y)? 371 * \see is_unit, wrapx. 372 * \param unit is the pointer to the \Unit. 373 * \param x is a possibly unwrapped x co-ordinate. 374 * \param y is a y co-ordinate. 375 * \return TRUE if unit previously was located at (wrap(x), y). 376 * \since unit may be dead now, we just test for is_unit instead of in_play. 377 */ 378 #define was_located_at(unit,x,y) (is_unit(unit) && (unit)->prevx == wrapx(x) && (unit)->prevy == (y)) 379 380 /*! \brief Is unit being tracked? 381 * 382 * Is a side watching a \Unit? 383 * \see side_in_set. 384 * \param side is a pointer to a \Side. 385 * \param unit is the pointer to the \Unit. 386 * \return TRUE if the side bit for the side is set in the tracking mask. 387 */ 388 #define side_tracking_unit(side,unit) (side_in_set((side), (unit)->tracking)) 389 390 /*! \brief Unit opinion. 391 * 392 * \Unit opinion, if defined, is an array of 2-byte values, one per side, 393 * indexed by side number. Each value consists of a low byte, which is the 394 * for/against strength (positive is "for", negative is "against", neutral is 0), 395 * and a high byte, which is the level of courage/fear (positive is courage, 396 * negative is fear). 397 * \see unit_courage, x_opinion, side_number. 398 * \param unit is a pointer to the \Unit. 399 * \param side is a pointer to the \Side. 400 * \return 401 * - < 0 if against, 402 * - = 0 if neutral or not defined, or 403 * - > 0 if for. 404 * */ 405 #define unit_opinion(unit,side) \ 406 ((unit)->opinions != NULL ? x_opinion((unit)->opinions[side_number(side)]) : 0) 407 408 /*! \brief Unit courage. 409 * 410 * \Unit courage is the upper byte of the unit opinon array. 411 * \see unit_opinion, x_courage, side_number. 412 * \param unit is a pointer to the \Unit. 413 * \param side is a pointer to the \Side. 414 * \return 415 * - < 0 if fearful, 416 * - = 0 if neutral or not defined, or 417 * - > 0 if couragous. 418 * */ 419 #define unit_courage(unit,side) \ 420 ((unit)->opinions != NULL ? x_courage((unit)->opinions[side_number(side)]) : 0) 421 422 /*! \brief Extract opinion. 423 * 424 * Extract the low order byte, and convert to +- range. 425 * \param val is the value to extract 426 * \return low byte value in range -128/127. 427 */ 428 #define x_opinion(val) (((val) & 0xff) - 128) 429 430 /*! \brief Extract courage. 431 * 432 * Extract the high order byte, and convert to +- range. 433 * \param val is the value to extract 434 * \return high byte value in range -128/127. 435 */ 436 #define x_courage(val) (((val) >> 8) & 0xff) 437 438 /*! \brief Set opinion value. 439 * 440 * Set the opinion value. Mask old value and 'or' in new 441 * value. 442 * \param val is the opinion word. 443 * \param val2 is the new opinion part of the opinion word. 444 * \return new 'opion' word. 445 */ 446 #define s_opinion(val,val2) (((val) & ~0xff) | (val2 + 128)) 447 448 /*! \brief Set courage value. 449 * 450 * Set the courage value. Mask old value and 'or' in new 451 * value. 452 * \param val is the opinion word. 453 * \param val2 is the new courage part of the opinion word. 454 * \return new 'opinion' word. 455 */ 456 #define s_courage(val,val2) (((val) & ~0xff00) | (val2 << 8)) 457 458 /* These return a percentage value. */ 459 460 #define supply_inflow(unit) ((unit)->s_flow / 163) 461 462 #define supply_connectedness(unit) ((unit)->s_conn / 254) 463 464 /* (could use bit fields in struct I suppose...) */ 465 466 #define DETONATE_FLAG_BIT 0 467 468 #define was_detonated(unit) ((unit)->flags & (1 << DETONATE_FLAG_BIT)) 469 470 #define set_was_detonated(unit, v) \ 471 ((v) ? ((unit)->flags |= 1 << DETONATE_FLAG_BIT) \ 472 : ((unit)->flags &= ~(1 << DETONATE_FLAG_BIT))) 473 474 #define unit_point_value(unit) ((unit)->extras ? (unit)->extras->point_value : -1) 475 476 #define unit_appear_turn(unit) ((unit)->extras ? (unit)->extras->appear : -1) 477 478 #define unit_appear_var_x(unit) ((unit)->extras ? (unit)->extras->appear_var_x : -1) 479 480 #define unit_appear_var_y(unit) ((unit)->extras ? (unit)->extras->appear_var_y : -1) 481 482 #define unit_disappear_turn(unit) ((unit)->extras ? (unit)->extras->disappear : -1) 483 484 #define unit_extra_priority(unit) ((unit)->extras ? (unit)->extras->priority : -1) 485 486 #define unit_symbol(unit) ((unit)->extras ? (unit)->extras->sym : lispnil) 487 488 #define unit_sides(unit) ((unit)->extras ? (unit)->extras->sides : lispnil) 489 490 #define unit_doctrine(unit) \ 491 ((unit)->side->udoctrine[(unit)->type]) 492 493 #define construction_run_doctrine(unit,u2) \ 494 (unit_doctrine(unit)->construction_run[u2]) 495 496 #define unit_side_treasury(unit) \ 497 ((unit)->side->treasury) 498 499 /* A sortable vector of units, generally useful. */ 500 501 /*! \brief Sort types. 502 * 503 * The kinds of sort keys available for list windows. */ 504 enum sortkeys { 505 bynothing, /*!< No sort. */ 506 bytype, /*!< Sort by type. */ 507 byname, /*!< Sort by name. */ 508 byactorder, /*!< Sort by actions. */ 509 bylocation, /*!< Sort by location. */ 510 byside, /*!< Sord by side. */ 511 numsortkeytypes /*!< Number sort key types. */ 512 }; 513 514 /*! Can sort on as many as five keys. */ 515 #define MAXSORTKEYS 5 516 517 /*! Unit vector entry. */ 518 typedef struct a_unitvectorentry { 519 Unit *unit; /*!< Pointer to Unit. */ 520 int flag; /*!< Flag */ 521 } UnitVectorEntry; 522 523 /*! Sortable unit vector */ 524 typedef struct a_unitvector { 525 int size; /*!< Size */ 526 int numunits; /*!< Number of entries. */ 527 enum sortkeys sortkeys[MAXSORTKEYS]; /*!< Sort by keys. */ 528 UnitVectorEntry units[1]; /*!< Head of vector. */ 529 } UnitVector; 530 531 /*! \brief Unit in vector? 532 * 533 * Given an index into a unit vector, return the unit. */ 534 /* \note (should check args?) */ 535 #define unit_in_vector(uvec,ix) ((uvec)->units[ix].unit) 536 537 #undef DEF_ACTION 538 #define DEF_ACTION(name,CODE,args,prepfn,netprepfn,dofn,checkfn,argdecl,doc) CODE, 539 540 /*! \brief Action types. 541 * 542 * Types of primitive unit actions. 543 */ 544 typedef enum actiontype { 545 546 #include "action.def" 547 548 NUMACTIONTYPES 549 550 } ActionType; 551 552 /*! \brief Action definition. */ 553 typedef struct a_actiondefn { 554 ActionType typecode; /*!< Action type. */ 555 char *name; /*!< Name. */ 556 char *argtypes; /*!< argument type string. */ 557 } ActionDefn; 558 559 /*! \brief Maximum number of arguments to action. */ 560 #define MAXACTIONARGS 4 561 562 /*! \brief Action. */ 563 typedef struct a_action { 564 ActionType type; /*!< the type of the action */ 565 int args[MAXACTIONARGS]; /*!< assorted parameters */ 566 int actee; /*!< the unit being affected by action */ 567 struct a_action *next; /*!< chain to next action */ 568 } Action; 569 570 /*! Actor state. */ 571 typedef struct a_actorstate { 572 short initacp; /*!< how much we can still do */ 573 short acp; /*!< how much we can still do */ 574 short actualmoves; /*!< cells actually covered this turn */ 575 Action nextaction; /*!< Next action. */ 576 } ActorState; 577 578 #define valid(x) ((x) == A_ANY_OK) 579 580 #define has_pending_action(unit) \ 581 ((unit)->act && (unit)->act->nextaction.type != ACTION_NONE) 582 583 #define acp_indep(unit) u_acp_independent((unit)->type) 584 585 /*! \brief Does unit have ACP left? 586 * 587 * This is true if the unit still has acp left to act with. This used 588 * to test acp > acp-min, but that allows units to move around with 589 * negative acp, and doesn't work as well in game designs. 590 * Now returns TRUE if an acp-independent unit that can build is 591 * either waiting for tasks or not yet done with construction for 592 * this turn. 593 * \param unit is pointer to unit. 594 */ 595 596 #define has_acp_left(unit) \ 597 ((unit)->act && ((0 < (unit)->act->acp) || acp_indep(unit))) 598 599 /* What is the maximum amount of ACP an unit type can have? */ 600 #define type_acp_max(u) ((u_acp_max(u) >= 0) ? u_acp_max(u) : u_acp(u)) 601 602 /* All the definitions that govern planning. */ 603 604 #undef DEF_GOAL 605 #define DEF_GOAL(name,dname,GOALTYPE,args) GOALTYPE, 606 607 /*! 608 * \brief Goal Type. 609 * 610 * The different types of goals. 611 * A goal is a predicate object that can be tested to see whether it has 612 * been achieved. As such, it is a relatively static object and may be 613 * shared. 614 */ 615 enum goaltype { 616 617 #include "goal.def" 618 619 g_t_dummy 620 }; 621 typedef enum goaltype GoalType; 622 623 /*! \brief Goal definition. */ 624 typedef struct a_goaldefn { 625 char *name; /*!< Name. */ 626 char *display_name; /*!< Display name. */ 627 char *argtypes; /*!< Type string of arguments. */ 628 } GoalDefn; 629 630 /* The goal structure proper. */ 631 632 /*! \brief Maximum number of goal arguments. */ 633 #define MAXGOALARGS 5 634 635 /*! \brief Goal. */ 636 typedef struct a_goal { 637 GoalType type; /*!< Goal type. */ 638 short tf; /*!< Terrain flag. ??? */ 639 Side *side; /*!< Pointer to side. */ 640 short args[MAXGOALARGS]; /*!< Argument array. */ 641 } Goal; 642 643 #undef DEF_TASK 644 #define DEF_TASK(name,dname,CODE,argtypes,dofn,createfn,setfn,netsetfn,pushfn,netpushfn,argdecl) CODE, 645 646 /*! \brief Task type. 647 * 648 * A task is a single executable element of a unit's plan. Each task 649 * type is something that has been found useful or convenient to 650 * encapsulate as a step in a plan. Tasks are also useful for human 651 * interfaces to use instead of actions, since they have retry 652 * capabilities that are not part of action execution. 653 * Note that a task's arguments are not the same as the arguments 654 * passed to the functions that create and push tasks. For instance, 655 * the 'c' task argument is actually a state variable recording a 656 * direction choice; this is important for coherent routefinding, but 657 * is not needed in order to create a movement task. 658 */ 659 typedef enum a_tasktype { 660 661 #include "task.def" 662 663 NUMTASKTYPES 664 } TaskType; 665 666 /*! \brief Task outcome. */ 667 typedef enum a_taskoutcome { 668 TASK_UNKNOWN, /*!< Unknown task. */ 669 TASK_FAILED, /*!< Task failed. */ 670 TASK_IS_INCOMPLETE, /*!< Task incomplete. */ 671 TASK_PREPPED_ACTION, /*!< Task prepped action. */ 672 TASK_IS_COMPLETE /*!< Task complete. */ 673 } TaskOutcome; 674 675 /*! \brief Maximum arguments for a task. */ 676 #define MAXTASKARGS 6 677 678 /*! \brief Task. */ 679 typedef struct a_task { 680 TaskType type; /*!< the kind of task we want to do */ 681 int args[MAXTASKARGS]; /*!< arguments */ 682 short execnum; /*!< how many times this has been done */ 683 short retrynum; /*!< number of immed failures so far */ 684 struct a_task *next; /*!< the next task to undertake */ 685 } Task; 686 687 /*! \brief Task definition. */ 688 typedef struct a_taskdefn { 689 char *name; /*!< name of the task type */ 690 char *display_name; /*!< name to display in interfaces */ 691 char *argtypes; /*!< string giving types of task's args */ 692 TaskOutcome (*exec)(Unit *unit, Task *task); /*!< Pointer to task function. */ 693 } TaskDefn; 694 695 #define is_task_type(x) (between(0, (x), NUMTASKTYPES - 1)) 696 697 #undef DEF_PLAN 698 #define DEF_PLAN(name,CODE) CODE, 699 700 /*! \brief Plan type. 701 * 702 * A plan is what a single unit uses to make decisions, both for itself and 703 * for any other units it commands. Any unit that can act at all has a 704 * plan object. A plan collects lots of unit behavior, but its most 705 * important structure is the task queue, which contains a list of what 706 * to do next, in order. 707 * Plan types distinguish several kinds of usages. 708 */ 709 typedef enum plantype { 710 711 #include "plan.def" 712 713 NUMPLANTYPES 714 } PlanType; 715 716 /*! \brief Plan. */ 717 typedef struct a_plan { 718 PlanType type; /*!< general type of plan that we've got here */ 719 short creation_turn; /*!< turn at which this plan was created */ 720 short initial_turn; /*!< turn at which this plan is to be done */ 721 short final_turn; /*!< turn to deactivate this plan */ 722 short asleep; /*!< true if the unit is doing nothing */ 723 short reserve; /*!< true if unit waiting until next turn */ 724 short delayed; /*!< true if plan delayed. ??? */ 725 short waitingfortasks; /*!< true if waiting to be given a task */ 726 short aicontrol; /*!< true if an AI can operate on the unit */ 727 short supply_alarm; /*! true if supply alarm is needed. */ 728 short supply_is_low; /*! true is supply is low. */ 729 short waitingfortransport; /*!< true if waiting for transport. */ 730 struct a_goal *maingoal; /*! the main goal of this plan */ 731 struct a_goal *formation; /*! goal to keep in a formation */ 732 struct a_task *tasks; /*! pointer to chain of sequential tasks */ 733 /* Not saved/restored. (little value, some trouble to do) */ 734 struct a_unit *funit; /*! pointer to unit keeping formation */ 735 Action lastaction; /*! a copy of the last action attempted */ 736 short lastresult; /*! that action's outcome */ 737 Task last_task; /*! a copy of the last task executed */ 738 TaskOutcome last_task_outcome; /*! that task's outcome */ 739 short execs_this_turn; /*!< Excutes this turn if true. */ 740 } Plan; 741 742 #define for_all_tasks(plan,task) \ 743 for (task = (plan)->tasks; task != NULL; task = task->next) 744 745 #define ai_controlled(unit) \ 746 ((unit)->plan && (unit)->plan->aicontrol && side_has_ai(unit->side)) 747 748 /* Global unit variables. */ 749 750 /*! \brief Unit list. */ 751 extern Unit *unitlist; 752 /*! \brief Unit temporary. */ 753 extern Unit *tmpunit; 754 755 /*! \brief Number of Units. */ 756 extern int numunits; 757 /*! \brief Number of live Units. */ 758 extern int numliveunits; 759 760 /*! \brief Completeness. ??? Progress indicator? */ 761 extern short *completenesses; 762 763 /*! \brief Temporary sort keys. */ 764 extern enum sortkeys tmpsortkeys[]; 765 766 767 /* \brief Action definition array. */ 768 extern ActionDefn actiondefns[]; 769 770 /*! \brief Goal definition array. */ 771 extern GoalDefn goaldefns[]; 772 773 /*! \brief Task definition array. */ 774 extern TaskDefn taskdefns[]; 775 776 /*! \brief Plan type name array. */ 777 extern char *plantypenames[]; 778 779 /* Declarations of unit-related functions that do not change the kernel state. 780 Those that actually do something have been moved to kernel.h. */ 781 782 /* Testable conditions etc. */ 783 784 extern int side_owns_occupant_of_unit(Side *side, Unit *unit); 785 extern int side_owns_viewer_in_unit(Side *side, Unit *unit); 786 extern int type_can_occupy_cell(int u, int x, int y); 787 extern int side_thinks_type_can_occupy_cell(Side *side, int u, int x, int y); 788 extern int type_can_occupy_empty_cell(int u, int x, int y); 789 extern int type_can_occupy_terrain(int u, int t); 790 extern int can_occupy_cell_without(Unit *unit, int x, int y, Unit *unit3); 791 extern int type_can_occupy_cell_without(int u, int x, int y, Unit *unit3); 792 extern int side_can_put_type_at(Side *side, int u, int x, int y); 793 extern int side_can_put_type_at_without(Side *side, int u, int x, int y, Unit *unit); 794 extern int side_thinks_it_can_put_type_at(Side *side, int u, int x, int y); 795 extern int side_thinks_it_can_put_type_at_without(Side *side, int u, int x, int y, Unit *unit); 796 extern int can_occupy(Unit *unit, Unit *transport); 797 extern int type_can_occupy(int u, Unit *transport); 798 extern int side_thinks_type_can_occupy(Side *side, int u, UnitView *transport); 799 extern int type_can_occupy_without(int u, Unit *transport, Unit *unit); 800 extern int side_thinks_type_can_occupy_without(int u, UnitView *transport, Unit *unit); 801 extern int type_can_occupy_empty_type(int u, int u2); 802 extern int type_can_have_occs(int u); 803 extern int new_unit_allowed_on_side(int u, Side *side); 804 extern int unit_allowed_on_side(Unit *unit, Side *side); 805 extern int num_sides_allowed(int u); 806 extern int type_allowed_on_side(int u, Side *side); 807 extern int type_ever_available(int u, Side *side); 808 extern int unit_trusts_unit(Unit *unit1, Unit *unit2); 809 extern int type_survives_in_cell(int u, int nx, int ny); 810 extern int type_survives_in_terrain(int u, int t); 811 extern int can_build(Unit *unit); 812 extern int type_can_build(int u, Side *side); 813 extern int can_move(Unit *unit); 814 extern int can_extract_at(Unit *unit, int x, int y, int *mp); 815 extern int can_load_at(Unit *unit, int x, int y, int *mp); 816 extern int can_develop(Unit *unit); 817 extern int type_can_develop(int u); 818 extern int can_change_type(Unit *unit); 819 extern int can_change_type_to(Unit *unit, int u2); 820 extern int could_change_type(int u); 821 extern int could_change_type_to(int u, int u2); 822 #define could_auto_upgrade_to(u,u2) ((u2) == u_auto_upgrade_to(u)) 823 extern int can_disband(Unit *unit); 824 extern int type_can_disband(int u); 825 extern int side_can_disband(Side *side, Unit *unit); 826 extern int can_add_terrain(Unit *unit); 827 extern int type_can_add_terrain(int u); 828 extern int can_remove_terrain(Unit *unit); 829 extern int type_can_remove_terrain(int u); 830 extern int can_build_attackers(Side *side, int u); 831 extern int can_build_defenders(Side *side, int u); 832 extern int can_build_explorers(Side *side, int u); 833 extern int can_build_colonizers(Side *side, int u); 834 extern int can_build_facilities(Side *side, int u); 835 extern int can_build_or_help(Unit *unit); 836 extern int can_research(Unit *unit); 837 extern int can_produce(Unit *unit); 838 extern int total_production(Unit *unit, int m); 839 extern int base_production(Unit *unit, int m); 840 extern int total_consumption(Unit *unit, int m); 841 extern int base_consumption(Unit *unit, int m); 842 extern int survival_time(Unit *unit); 843 extern int will_not_move(Unit *unit); 844 extern int needs_material_to_move(Unit *unit, int m); 845 extern int needs_material_to_survive(Unit *unit, int m); 846 extern int operating_range_best(int u); 847 extern int operating_range_worst(int u); 848 extern int real_operating_range_best(Unit *unit); 849 extern int real_operating_range_worst(Unit *unit); 850 extern int can_build_type(Unit *unit, int u2); 851 extern int can_develop_or_build_type(Unit *unit, int u2); 852 extern int unit_can_build_type_at(Unit *unit, int u2, int x, int y); 853 extern int unit_can_build_type(Unit *unit, int u2); 854 extern int type_can_build_type(int u, Side *side, int u2); 855 856 extern Unit *incomplete_build_target(Unit *unit); 857 858 #if 0 859 extern int moves_till_low_supplies(Unit *unit); 860 extern int num_occupants(Unit *unit); 861 extern int num_units_at(int x, int y); 862 #endif 863 864 extern int see_chance(int u, int u2, int dist); 865 extern int see_chance(Unit *seer, Unit *tosee); 866 867 /* Unit names and designations. */ 868 869 extern char *unit_desig(Unit *unit); 870 extern char *unit_desig_no_loc(Unit *unit); 871 extern char *utype_name_n(int u, int n); 872 extern char *shortest_unique_name(int u); 873 extern char *shortest_generic_name(int u); 874 extern char *actorstate_desig(struct a_actorstate *as); 875 876 /* Unit locators. */ 877 878 extern Unit *find_unit(int n); 879 extern Unit *find_unit_dead_or_alive(int n); 880 extern Unit *find_unit_by_name(char *nm); 881 extern Unit *find_unit_by_number(int nb); 882 extern Unit *find_unit_by_symbol(Obj *sym); 883 884 /* Unit vector manipulating functions. Declared here rather than in kernel.h 885 since they also are used in the interfaces. */ 886 887 extern UnitVector *make_unit_vector(int initsize); 888 extern void clear_unit_vector(UnitVector *vec); 889 extern UnitVector *add_unit_to_vector(UnitVector *vec, Unit *unit, int flag); 890 extern void remove_unit_from_vector(UnitVector *vec, Unit *unit, int pos); 891 extern void sort_unit_vector(UnitVector *vec); 892 893 /* Used in mknames.c. */ 894 895 extern void sort_units(int byidonly); 896 897 /* Turn init code - does not really belong here. */ 898 899 extern void check_all_units(void); 900 901 #undef DEF_ACTION 902 #define DEF_ACTION(name,code,args,prepfn,netprepfn,dofn,CHECKFN,ARGDECL,doc) \ 903 extern int CHECKFN ARGDECL; 904 905 #include "action.def" 906 907 #undef DEF_TASK 908 #define DEF_TASK(name,dname,code,argtypes,dofn,CREATEFN,setfn,netsetfn,pushfn,netpushfn,ARGDECL) \ 909 extern Task *CREATEFN ARGDECL; 910 911 #include "task.def" 912