| 1 |
// ____ _ __ |
| 2 |
// / __ )____ _____ | | / /___ ___________ |
| 3 |
// / __ / __ \/ ___/ | | /| / / __ `/ ___/ ___/ |
| 4 |
// / /_/ / /_/ (__ ) | |/ |/ / /_/ / / (__ ) |
| 5 |
// /_____/\____/____/ |__/|__/\__,_/_/ /____/ |
| 6 |
// |
| 7 |
// A futuristic real-time strategy game. |
| 8 |
// This file is part of Bos Wars. |
| 9 |
// |
| 10 |
/**@name ai_force.cpp - AI force functions. */ |
| 11 |
// |
| 12 |
// (c) Copyright 2001-2009 by Lutz Sammer and Jimmy Salmon |
| 13 |
// |
| 14 |
// This program is free software; you can redistribute it and/or modify |
| 15 |
// it under the terms of the GNU General Public License as published by |
| 16 |
// the Free Software Foundation; only version 2 of the License. |
| 17 |
// |
| 18 |
// This program is distributed in the hope that it will be useful, |
| 19 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 21 |
// GNU General Public License for more details. |
| 22 |
// |
| 23 |
// You should have received a copy of the GNU General Public License |
| 24 |
// along with this program; if not, write to the Free Software |
| 25 |
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
| 26 |
// 02111-1307, USA. |
| 27 |
// |
| 28 |
|
| 29 |
//@{ |
| 30 |
|
| 31 |
/*---------------------------------------------------------------------------- |
| 32 |
-- Includes |
| 33 |
----------------------------------------------------------------------------*/ |
| 34 |
|
| 35 |
#include <stdio.h> |
| 36 |
#include <stdlib.h> |
| 37 |
#include <string.h> |
| 38 |
|
| 39 |
#include "stratagus.h" |
| 40 |
#include "unittype.h" |
| 41 |
#include "unit.h" |
| 42 |
#include "ai_local.h" |
| 43 |
#include "actions.h" |
| 44 |
#include "map.h" |
| 45 |
#include "pathfinder.h" |
| 46 |
#include "player.h" |
| 47 |
|
| 48 |
/*---------------------------------------------------------------------------- |
| 49 |
-- Variables |
| 50 |
----------------------------------------------------------------------------*/ |
| 51 |
|
| 52 |
int UnitTypeEquivs[UnitTypeMax + 1]; /// equivalence between unittypes |
| 53 |
|
| 54 |
/*---------------------------------------------------------------------------- |
| 55 |
-- Functions |
| 56 |
----------------------------------------------------------------------------*/ |
| 57 |
|
| 58 |
|
| 59 |
/** |
| 60 |
** Remove any equivalence between unittypes |
| 61 |
*/ |
| 62 |
void AiResetUnitTypeEquiv() |
| 63 |
{ |
| 64 |
for (int i = 0; i <= UnitTypeMax; ++i) |
| 65 |
{ |
| 66 |
UnitTypeEquivs[i] = i; |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
** Make two unittypes equivalents from the AI's point of vue |
| 72 |
** |
| 73 |
** @param a the first unittype |
| 74 |
** @param b the second unittype |
| 75 |
*/ |
| 76 |
void AiNewUnitTypeEquiv(CUnitType *a, CUnitType *b) |
| 77 |
{ |
| 78 |
int find; |
| 79 |
int replace; |
| 80 |
int i; |
| 81 |
|
| 82 |
find = UnitTypeEquivs[a->Slot]; |
| 83 |
replace = UnitTypeEquivs[b->Slot]; |
| 84 |
|
| 85 |
// Always record equivalences with the lowest unittype. |
| 86 |
if (find < replace) |
| 87 |
{ |
| 88 |
i = find; |
| 89 |
find = replace; |
| 90 |
replace = i; |
| 91 |
} |
| 92 |
|
| 93 |
// Then just find & replace in UnitTypeEquivs... |
| 94 |
for (i = 0; i <= UnitTypeMax; ++i) |
| 95 |
{ |
| 96 |
if (UnitTypeEquivs[i] == find) |
| 97 |
{ |
| 98 |
UnitTypeEquivs[i] = replace; |
| 99 |
} |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
/** |
| 105 |
** Find All unittypes equivalent to a given one |
| 106 |
** |
| 107 |
** @param unittype the unittype to find equivalence for |
| 108 |
** @param result int array which will hold the result. (Size UnitTypeMax+1) |
| 109 |
** |
| 110 |
** @return the number of unittype found |
| 111 |
*/ |
| 112 |
int AiFindUnitTypeEquiv(const CUnitType *unittype, int *result) |
| 113 |
{ |
| 114 |
int i; |
| 115 |
int search; |
| 116 |
int count; |
| 117 |
|
| 118 |
search = UnitTypeEquivs[unittype->Slot]; |
| 119 |
count = 0; |
| 120 |
|
| 121 |
for (i = 0; i <= UnitTypeMax; ++i) |
| 122 |
{ |
| 123 |
if (UnitTypeEquivs[i] == search) |
| 124 |
{ |
| 125 |
// Found one |
| 126 |
result[count] = i; |
| 127 |
count++; |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
return count; |
| 132 |
} |
| 133 |
|
| 134 |
/** |
| 135 |
** Find All unittypes equivalent to a given one, and which are available |
| 136 |
** UnitType are returned in the prefered order ( ie palladin >> knight... ) |
| 137 |
** |
| 138 |
** @param unittype The unittype to find equivalence for |
| 139 |
** @param usableTypes int array which will hold the result. (Size UnitTypeMax+1) |
| 140 |
** |
| 141 |
** @return the number of unittype found |
| 142 |
*/ |
| 143 |
int AiFindAvailableUnitTypeEquiv(const CUnitType *unittype, int *usableTypes) |
| 144 |
{ |
| 145 |
int usableTypesCount; |
| 146 |
int bestlevel; |
| 147 |
int curlevel; |
| 148 |
|
| 149 |
// 1 - Find equivalents |
| 150 |
usableTypesCount = AiFindUnitTypeEquiv(unittype, usableTypes); |
| 151 |
|
| 152 |
// 2 - Sort by level |
| 153 |
// We won't have usableTypesCount>4, so simple sort should do it |
| 154 |
for (int i = 0; i < usableTypesCount - 1; ++i) |
| 155 |
{ |
| 156 |
bestlevel = UnitTypes[usableTypes[i]]->Priority; |
| 157 |
for (int j = i + 1; j < usableTypesCount; ++j) |
| 158 |
{ |
| 159 |
curlevel = UnitTypes[usableTypes[j]]->Priority; |
| 160 |
|
| 161 |
if (curlevel > bestlevel) |
| 162 |
{ |
| 163 |
// Swap |
| 164 |
int tmp = usableTypes[j]; |
| 165 |
usableTypes[j] = usableTypes[i]; |
| 166 |
usableTypes[i] = tmp; |
| 167 |
|
| 168 |
bestlevel = curlevel; |
| 169 |
} |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
return usableTypesCount; |
| 174 |
} |
| 175 |
|
| 176 |
/** |
| 177 |
** Clean units in a force. |
| 178 |
** |
| 179 |
** @param force Force number. |
| 180 |
*/ |
| 181 |
static void AiCleanForce(int force) |
| 182 |
{ |
| 183 |
CUnit *aiunit; |
| 184 |
const AiUnitType *aitype; |
| 185 |
int counter[UnitTypeMax + 1]; |
| 186 |
size_t i; |
| 187 |
|
| 188 |
// |
| 189 |
// Release all killed units. |
| 190 |
// |
| 191 |
i = 0; |
| 192 |
while (i != AiPlayer->Force[force].Units.size()) |
| 193 |
{ |
| 194 |
aiunit = AiPlayer->Force[force].Units[i]; |
| 195 |
if (aiunit->Destroyed || |
| 196 |
aiunit->Orders[0]->Action == UnitActionDie) |
| 197 |
{ |
| 198 |
aiunit->RefsDecrease(); |
| 199 |
AiPlayer->Force[force].Units.erase( |
| 200 |
AiPlayer->Force[force].Units.begin() + i); |
| 201 |
continue; |
| 202 |
} |
| 203 |
++i; |
| 204 |
} |
| 205 |
|
| 206 |
// |
| 207 |
// Count units in force. |
| 208 |
// |
| 209 |
memset(counter, 0, sizeof(counter)); |
| 210 |
for (i = 0; i < AiPlayer->Force[force].Units.size(); ++i) |
| 211 |
{ |
| 212 |
aiunit = AiPlayer->Force[force].Units[i]; |
| 213 |
counter[UnitTypeEquivs[aiunit->Type->Slot]]++; |
| 214 |
} |
| 215 |
|
| 216 |
// |
| 217 |
// Look if the force is complete. |
| 218 |
// |
| 219 |
AiPlayer->Force[force].Completed = true; |
| 220 |
for (i = 0; i < AiPlayer->Force[force].UnitTypes.size(); ++i) |
| 221 |
{ |
| 222 |
aitype = &AiPlayer->Force[force].UnitTypes[i]; |
| 223 |
if (aitype->Want > counter[aitype->Type->Slot]) |
| 224 |
{ |
| 225 |
AiPlayer->Force[force].Completed = false; |
| 226 |
} |
| 227 |
counter[aitype->Type->Slot] -= aitype->Want; |
| 228 |
} |
| 229 |
|
| 230 |
// |
| 231 |
// Release units too much in force. |
| 232 |
// |
| 233 |
if (!AiPlayer->Force[force].Attacking) |
| 234 |
{ |
| 235 |
i = 0; |
| 236 |
while (i != AiPlayer->Force[force].Units.size()) |
| 237 |
{ |
| 238 |
aiunit = AiPlayer->Force[force].Units[i]; |
| 239 |
if (counter[aiunit->Type->Slot] > 0) |
| 240 |
{ |
| 241 |
counter[UnitTypeEquivs[aiunit->Type->Slot]]--; |
| 242 |
aiunit->RefsDecrease(); |
| 243 |
AiPlayer->Force[force].Units.erase( |
| 244 |
AiPlayer->Force[force].Units.begin() + i); |
| 245 |
continue; |
| 246 |
} |
| 247 |
++i; |
| 248 |
} |
| 249 |
} |
| 250 |
} |
| 251 |
|
| 252 |
/** |
| 253 |
** Cleanup units in forces. |
| 254 |
*/ |
| 255 |
void AiCleanForces() |
| 256 |
{ |
| 257 |
// Release all killed units. |
| 258 |
for (int force = 0; force < AI_MAX_ATTACKING_FORCES; ++force) |
| 259 |
{ |
| 260 |
AiCleanForce(force); |
| 261 |
} |
| 262 |
} |
| 263 |
|
| 264 |
/** |
| 265 |
** Check if the units belongs to the force. |
| 266 |
** |
| 267 |
** @param force Force to be checked. |
| 268 |
** @param type Type to check. |
| 269 |
** |
| 270 |
** @return True if it fits, false otherwise. |
| 271 |
*/ |
| 272 |
static int AiCheckBelongsToForce(int force, const CUnitType *type) |
| 273 |
{ |
| 274 |
CUnit *aiunit; |
| 275 |
AiUnitType *aitype; |
| 276 |
int counter[UnitTypeMax + 1]; |
| 277 |
int flag; |
| 278 |
int i; |
| 279 |
|
| 280 |
memset(counter, 0, sizeof(counter)); |
| 281 |
// |
| 282 |
// Count units in force. |
| 283 |
// |
| 284 |
for (i = 0; i < (int)AiPlayer->Force[force].Units.size(); ++i) |
| 285 |
{ |
| 286 |
aiunit = AiPlayer->Force[force].Units[i]; |
| 287 |
counter[UnitTypeEquivs[aiunit->Type->Slot]]++; |
| 288 |
} |
| 289 |
|
| 290 |
// |
| 291 |
// Look what should be in the force. |
| 292 |
// |
| 293 |
flag = 0; |
| 294 |
AiPlayer->Force[force].Completed = true; |
| 295 |
for (i = 0; i < (int)AiPlayer->Force[force].UnitTypes.size(); ++i) |
| 296 |
{ |
| 297 |
aitype = &AiPlayer->Force[force].UnitTypes[i]; |
| 298 |
if (aitype->Want > counter[aitype->Type->Slot]) |
| 299 |
{ |
| 300 |
if (UnitTypeEquivs[type->Slot] == aitype->Type->Slot) |
| 301 |
{ |
| 302 |
if (aitype->Want - 1 > counter[aitype->Type->Slot]) |
| 303 |
{ |
| 304 |
AiPlayer->Force[force].Completed = false; |
| 305 |
} |
| 306 |
flag = 1; |
| 307 |
} |
| 308 |
else |
| 309 |
{ |
| 310 |
AiPlayer->Force[force].Completed = false; |
| 311 |
} |
| 312 |
} |
| 313 |
} |
| 314 |
return flag; |
| 315 |
} |
| 316 |
|
| 317 |
/** |
| 318 |
** Ai assign unit to force. |
| 319 |
** |
| 320 |
** @param unit Unit to assign to force. |
| 321 |
*/ |
| 322 |
void AiAssignToForce(CUnit *unit) |
| 323 |
{ |
| 324 |
// |
| 325 |
// Check to which force it belongs |
| 326 |
// |
| 327 |
for (int force = 0; force < AI_MAX_FORCES; ++force) |
| 328 |
{ |
| 329 |
// No troops for attacking force |
| 330 |
if (!AiPlayer->Force[force].Defending && |
| 331 |
AiPlayer->Force[force].Attacking) |
| 332 |
{ |
| 333 |
continue; |
| 334 |
} |
| 335 |
|
| 336 |
if (AiCheckBelongsToForce(force, unit->Type)) |
| 337 |
{ |
| 338 |
AiPlayer->Force[force].Units.insert( |
| 339 |
AiPlayer->Force[force].Units.begin(), unit); |
| 340 |
unit->RefsIncrease(); |
| 341 |
break; |
| 342 |
} |
| 343 |
} |
| 344 |
} |
| 345 |
|
| 346 |
/** |
| 347 |
** Assign free units to force. |
| 348 |
*/ |
| 349 |
void AiAssignFreeUnitsToForce() |
| 350 |
{ |
| 351 |
const CUnit *aiunit; |
| 352 |
CUnit *table[UnitMax]; |
| 353 |
int n; |
| 354 |
int f; |
| 355 |
int i; |
| 356 |
|
| 357 |
AiCleanForces(); |
| 358 |
|
| 359 |
n = AiPlayer->Player->TotalNumUnits; |
| 360 |
memcpy(table, AiPlayer->Player->Units, sizeof(*AiPlayer->Player->Units) * n); |
| 361 |
|
| 362 |
// |
| 363 |
// Remove all units already in forces. |
| 364 |
// |
| 365 |
for (f = 0; f < AI_MAX_ATTACKING_FORCES; ++f) |
| 366 |
{ |
| 367 |
for (int j = 0; j < (int)AiPlayer->Force[f].Units.size(); ++j) |
| 368 |
{ |
| 369 |
aiunit = AiPlayer->Force[f].Units[j]; |
| 370 |
for (i = 0; i < n; ++i) |
| 371 |
{ |
| 372 |
if (table[i] == aiunit) |
| 373 |
{ |
| 374 |
table[i] = table[--n]; |
| 375 |
} |
| 376 |
} |
| 377 |
} |
| 378 |
} |
| 379 |
|
| 380 |
// |
| 381 |
// Try to assign the remaining units. |
| 382 |
// |
| 383 |
for (i = 0; i < n; ++i) |
| 384 |
{ |
| 385 |
AiAssignToForce(table[i]); |
| 386 |
} |
| 387 |
} |
| 388 |
|
| 389 |
/** |
| 390 |
** Attack at position with force. |
| 391 |
** |
| 392 |
** @param force Force number to attack with. |
| 393 |
** @param x X tile map position to be attacked. |
| 394 |
** @param y Y tile map position to be attacked. |
| 395 |
*/ |
| 396 |
void AiAttackWithForceAt(int force, int x, int y) |
| 397 |
{ |
| 398 |
AiCleanForce(force); |
| 399 |
|
| 400 |
if (!AiPlayer->Force[force].Units.empty()) |
| 401 |
{ |
| 402 |
AiPlayer->Force[force].Attacking = true; |
| 403 |
|
| 404 |
// |
| 405 |
// Send all units in the force to enemy. |
| 406 |
// |
| 407 |
for (int i = 0; i < (int)AiPlayer->Force[force].Units.size(); ++i) |
| 408 |
{ |
| 409 |
CUnit *aiunit = AiPlayer->Force[force].Units[i]; |
| 410 |
aiunit->Wait = i; |
| 411 |
if (aiunit->Type->CanAttack) |
| 412 |
{ |
| 413 |
CommandAttack(aiunit, x, y, NULL, FlushCommands); |
| 414 |
} |
| 415 |
else |
| 416 |
{ |
| 417 |
CommandMove(aiunit, x, y, FlushCommands); |
| 418 |
} |
| 419 |
} |
| 420 |
} |
| 421 |
} |
| 422 |
|
| 423 |
/** |
| 424 |
** Attack opponent with force. |
| 425 |
** |
| 426 |
** @param force Force number to attack with. |
| 427 |
*/ |
| 428 |
void AiAttackWithForce(int force) |
| 429 |
{ |
| 430 |
CUnit *aiunit; |
| 431 |
const CUnit *enemy; |
| 432 |
int x; |
| 433 |
int y; |
| 434 |
int f; |
| 435 |
int i; |
| 436 |
|
| 437 |
// Move the force to a free position so it can be used for a new |
| 438 |
// attacking party |
| 439 |
if (force < AI_MAX_FORCES) |
| 440 |
{ |
| 441 |
f = AI_MAX_FORCES; |
| 442 |
while (AiPlayer->Force[f].Attacking) |
| 443 |
{ |
| 444 |
++f; |
| 445 |
if (f == AI_MAX_ATTACKING_FORCES) |
| 446 |
{ |
| 447 |
DebugPrint("No free attacking forces\n"); |
| 448 |
f = force; |
| 449 |
break; |
| 450 |
} |
| 451 |
} |
| 452 |
if (f != AI_MAX_ATTACKING_FORCES) |
| 453 |
{ |
| 454 |
AiPlayer->Force[f] = AiPlayer->Force[force]; |
| 455 |
AiPlayer->Force[force].Reset(); |
| 456 |
} |
| 457 |
|
| 458 |
force = f; |
| 459 |
} |
| 460 |
|
| 461 |
AiCleanForce(force); |
| 462 |
|
| 463 |
AiPlayer->Force[force].Attacking = false; |
| 464 |
if (!AiPlayer->Force[force].Units.empty()) |
| 465 |
{ |
| 466 |
AiPlayer->Force[force].Attacking = true; |
| 467 |
|
| 468 |
enemy = NULL; |
| 469 |
for (i = 0; !enemy && i < (int)AiPlayer->Force[force].Units.size(); ++i) |
| 470 |
{ |
| 471 |
aiunit = AiPlayer->Force[force].Units[i]; |
| 472 |
if (aiunit->Type->CanAttack) |
| 473 |
{ |
| 474 |
enemy = AttackUnitsInDistance(aiunit, MaxMapWidth); |
| 475 |
} |
| 476 |
} |
| 477 |
|
| 478 |
if (!enemy) |
| 479 |
{ |
| 480 |
DebugPrint("No enemy found\n"); |
| 481 |
return; |
| 482 |
} |
| 483 |
AiPlayer->Force[force].State = 0; |
| 484 |
x = enemy->X; |
| 485 |
y = enemy->Y; |
| 486 |
|
| 487 |
// |
| 488 |
// Send all units in the force to enemy. |
| 489 |
// |
| 490 |
for (i = 0; i < (int)AiPlayer->Force[force].Units.size(); ++i) |
| 491 |
{ |
| 492 |
aiunit = AiPlayer->Force[force].Units[i]; |
| 493 |
aiunit->Wait = i; |
| 494 |
if (aiunit->Type->CanAttack) |
| 495 |
{ |
| 496 |
CommandAttack(aiunit, x, y, NULL, FlushCommands); |
| 497 |
} |
| 498 |
else |
| 499 |
{ |
| 500 |
CommandMove(aiunit, x, y, FlushCommands); |
| 501 |
} |
| 502 |
} |
| 503 |
} |
| 504 |
} |
| 505 |
|
| 506 |
/** |
| 507 |
** Force on attack ride. We attack until there is no unit or enemy left. |
| 508 |
** |
| 509 |
** @param force Force pointer. |
| 510 |
*/ |
| 511 |
static void AiForceAttacks(AiForce *force) |
| 512 |
{ |
| 513 |
CUnit *aiunit; |
| 514 |
int x; |
| 515 |
int y; |
| 516 |
const CUnit *unit; |
| 517 |
int i; |
| 518 |
|
| 519 |
if (force->Units.empty()) |
| 520 |
{ |
| 521 |
force->Attacking = false; |
| 522 |
return; |
| 523 |
} |
| 524 |
|
| 525 |
// Find a unit that isn't idle |
| 526 |
unit = NoUnitP; |
| 527 |
for (i = 0; i < (int)force->Units.size(); ++i) |
| 528 |
{ |
| 529 |
aiunit = force->Units[i]; |
| 530 |
if (!aiunit->IsIdle()) |
| 531 |
{ |
| 532 |
// Found an idle unit, use it if we find nothing better |
| 533 |
if (unit == NoUnitP) |
| 534 |
{ |
| 535 |
unit = aiunit; |
| 536 |
} |
| 537 |
// If the unit has a goal use it |
| 538 |
if (aiunit->Orders[0]->Goal != NoUnitP) |
| 539 |
{ |
| 540 |
unit = aiunit; |
| 541 |
break; |
| 542 |
} |
| 543 |
} |
| 544 |
} |
| 545 |
|
| 546 |
if (unit != NoUnitP) |
| 547 |
{ |
| 548 |
// Give idle units a new goal |
| 549 |
// FIXME: may not be a good goal |
| 550 |
for (i = 0; i < (int)force->Units.size(); ++i) |
| 551 |
{ |
| 552 |
aiunit = force->Units[i]; |
| 553 |
if (aiunit->IsIdle()) |
| 554 |
{ |
| 555 |
if (unit->Orders[0]->Goal) |
| 556 |
{ |
| 557 |
x = unit->Orders[0]->Goal->X; |
| 558 |
y = unit->Orders[0]->Goal->Y; |
| 559 |
} |
| 560 |
else if (unit->Orders[0]->X != -1 && unit->Orders[0]->Y != -1) |
| 561 |
{ |
| 562 |
x = unit->Orders[0]->X; |
| 563 |
y = unit->Orders[0]->Y; |
| 564 |
} |
| 565 |
else |
| 566 |
{ |
| 567 |
x = unit->X; |
| 568 |
y = unit->Y; |
| 569 |
} |
| 570 |
if (aiunit->Type->CanAttack) |
| 571 |
{ |
| 572 |
CommandAttack(aiunit, x, y, NULL, FlushCommands); |
| 573 |
} |
| 574 |
else |
| 575 |
{ |
| 576 |
CommandMove(aiunit, x, y, FlushCommands); |
| 577 |
} |
| 578 |
} |
| 579 |
} |
| 580 |
} |
| 581 |
else |
| 582 |
{ |
| 583 |
// Everyone is idle, find a new target |
| 584 |
unit = NULL; |
| 585 |
for (i = 0; i < (int)force->Units.size(); ++i) |
| 586 |
{ |
| 587 |
aiunit = force->Units[i]; |
| 588 |
if (aiunit->Type->CanAttack) |
| 589 |
{ |
| 590 |
unit = AttackUnitsInDistance(aiunit, MaxMapWidth); |
| 591 |
break; |
| 592 |
} |
| 593 |
} |
| 594 |
if (!unit) |
| 595 |
{ |
| 596 |
// No enemy found, give up |
| 597 |
// FIXME: should the force go home or keep trying to attack? |
| 598 |
DebugPrint("Attack force can't find a target, giving up\n"); |
| 599 |
force->Attacking = false; |
| 600 |
return; |
| 601 |
} |
| 602 |
for (i = 0; i < (int)force->Units.size(); ++i) |
| 603 |
{ |
| 604 |
aiunit = force->Units[i]; |
| 605 |
if (aiunit->Type->CanAttack) |
| 606 |
{ |
| 607 |
CommandAttack(aiunit, unit->X, unit->Y, NULL, FlushCommands); |
| 608 |
} |
| 609 |
else |
| 610 |
{ |
| 611 |
CommandMove(aiunit, unit->X, unit->Y, FlushCommands); |
| 612 |
} |
| 613 |
} |
| 614 |
} |
| 615 |
} |
| 616 |
|
| 617 |
/** |
| 618 |
** Entry point of force manager, called each second. |
| 619 |
*/ |
| 620 |
void AiForceManager() |
| 621 |
{ |
| 622 |
for (int force = 0; force < AI_MAX_ATTACKING_FORCES; ++force) |
| 623 |
{ |
| 624 |
// Defending |
| 625 |
if (AiPlayer->Force[force].Defending) |
| 626 |
{ |
| 627 |
const CUnit *aiunit = NULL; |
| 628 |
|
| 629 |
AiCleanForce(force); |
| 630 |
|
| 631 |
// Check if there are still enemies in attack range. |
| 632 |
for (size_t i = 0; i < AiPlayer->Force[force].Units.size(); ++i) |
| 633 |
{ |
| 634 |
if (AiPlayer->Force[force].Units[i]->Type->CanAttack && |
| 635 |
AttackUnitsInReactRange(AiPlayer->Force[force].Units[i])) |
| 636 |
{ |
| 637 |
aiunit = AiPlayer->Force[force].Units[i]; |
| 638 |
break; |
| 639 |
} |
| 640 |
} |
| 641 |
if (aiunit == NULL) |
| 642 |
{ |
| 643 |
// No enemies go home. |
| 644 |
DebugPrint("FIXME: not written, should send force home\n"); |
| 645 |
AiPlayer->Force[force].Defending = false; |
| 646 |
AiPlayer->Force[force].Attacking = false; |
| 647 |
} |
| 648 |
} |
| 649 |
|
| 650 |
// Attacking |
| 651 |
if (AiPlayer->Force[force].Attacking) |
| 652 |
{ |
| 653 |
AiCleanForce(force); |
| 654 |
AiForceAttacks(&AiPlayer->Force[force]); |
| 655 |
} |
| 656 |
} |
| 657 |
|
| 658 |
AiAssignFreeUnitsToForce(); |
| 659 |
} |
| 660 |
|
| 661 |
//@} |