| 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_resource.cpp - AI resource manager. */ |
| 11 |
// |
| 12 |
// (c) Copyright 2000-2009 by Lutz Sammer and Antonis Chaniotis. |
| 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 <string.h> |
| 36 |
#include <stdio.h> |
| 37 |
#include <stdlib.h> |
| 38 |
|
| 39 |
#include "stratagus.h" |
| 40 |
#include "unit.h" |
| 41 |
#include "unit_cache.h" |
| 42 |
#include "unittype.h" |
| 43 |
#include "upgrade.h" |
| 44 |
#include "map.h" |
| 45 |
#include "pathfinder.h" |
| 46 |
#include "ai_local.h" |
| 47 |
#include "actions.h" |
| 48 |
#include "player.h" |
| 49 |
|
| 50 |
/*---------------------------------------------------------------------------- |
| 51 |
-- Defines |
| 52 |
----------------------------------------------------------------------------*/ |
| 53 |
|
| 54 |
#define COLLECT_RESOURCES_INTERVAL 4 |
| 55 |
|
| 56 |
/*---------------------------------------------------------------------------- |
| 57 |
-- Functions |
| 58 |
----------------------------------------------------------------------------*/ |
| 59 |
|
| 60 |
/** |
| 61 |
** Check if the costs are available for the AI. |
| 62 |
** |
| 63 |
** Take reserve and already used resources into account. |
| 64 |
** |
| 65 |
** @param costs Costs for something. |
| 66 |
** |
| 67 |
** @return A bit field of the missing costs. |
| 68 |
*/ |
| 69 |
static int AiCheckCosts(const int *costs) |
| 70 |
{ |
| 71 |
int err = 0; |
| 72 |
|
| 73 |
for (int i = 0; i < MaxCosts; ++i) |
| 74 |
{ |
| 75 |
if (costs[i] && |
| 76 |
AiPlayer->Player->ProductionRate[i] == 0 && |
| 77 |
AiPlayer->Player->StoredResources[i] == 0) |
| 78 |
{ |
| 79 |
err |= (1 << i); |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
return err; |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
** Check if the costs for a unit-type are available for the AI. |
| 88 |
** |
| 89 |
** Take reserve and already used resources into account. |
| 90 |
** |
| 91 |
** @param type Unit-type to check the costs for. |
| 92 |
** |
| 93 |
** @return A bit field of the missing costs. |
| 94 |
*/ |
| 95 |
static int AiCheckUnitTypeCosts(const CUnitType *type) |
| 96 |
{ |
| 97 |
return AiCheckCosts(type->ProductionCosts); |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
** Enemy units in distance. |
| 102 |
** |
| 103 |
** @param player Find enemies of this player |
| 104 |
** @param type Optional unit type to check if enemy can target this |
| 105 |
** @param x X location |
| 106 |
** @param y Y location |
| 107 |
** @param range Distance range to look. |
| 108 |
** |
| 109 |
** @return Number of enemy units. |
| 110 |
*/ |
| 111 |
int AiEnemyUnitsInDistance(const CPlayer *player, const CUnitType *type, int x, int y, unsigned range) |
| 112 |
{ |
| 113 |
const CUnit *dest; |
| 114 |
CUnit *table[UnitMax]; |
| 115 |
unsigned n; |
| 116 |
unsigned i; |
| 117 |
int e; |
| 118 |
|
| 119 |
// |
| 120 |
// Select all units in range. |
| 121 |
// |
| 122 |
n = UnitCache.Select(x - range, y - range, x + range, y + range, table, UnitMax); |
| 123 |
|
| 124 |
// |
| 125 |
// Find the enemy units which can attack |
| 126 |
// |
| 127 |
for (e = i = 0; i < n; ++i) |
| 128 |
{ |
| 129 |
dest = table[i]; |
| 130 |
// |
| 131 |
// Those can't attack anyway. |
| 132 |
// |
| 133 |
if (dest->Removed || dest->Orders[0]->Action == UnitActionDie) |
| 134 |
{ |
| 135 |
continue; |
| 136 |
} |
| 137 |
|
| 138 |
if (!player->IsEnemy(dest)) |
| 139 |
{ |
| 140 |
// a friend or neutral |
| 141 |
continue; |
| 142 |
} |
| 143 |
// |
| 144 |
// Unit can attack back? |
| 145 |
// |
| 146 |
if (!type || CanTarget(dest->Type, type)) |
| 147 |
{ |
| 148 |
++e; |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
return e; |
| 153 |
} |
| 154 |
|
| 155 |
/** |
| 156 |
** Enemy units in distance. |
| 157 |
** |
| 158 |
** @param unit Find in distance for this unit. |
| 159 |
** @param range Distance range to look. |
| 160 |
** |
| 161 |
** @return Number of enemy units. |
| 162 |
*/ |
| 163 |
int AiEnemyUnitsInDistance(const CUnit *unit, unsigned range) |
| 164 |
{ |
| 165 |
range += std::max(unit->Type->TileWidth, unit->Type->TileHeight); |
| 166 |
return AiEnemyUnitsInDistance(unit->Player, unit->Type, unit->X, unit->Y, range); |
| 167 |
} |
| 168 |
|
| 169 |
/** |
| 170 |
** Check if we can build the building. |
| 171 |
** |
| 172 |
** @param type Unit that can build the building. |
| 173 |
** @param building Building to be build. |
| 174 |
** |
| 175 |
** @return True if made, false if can't be made. |
| 176 |
** |
| 177 |
** @note We must check if the dependencies are fulfilled. |
| 178 |
*/ |
| 179 |
static int AiBuildBuilding(const CUnitType *type, CUnitType *building) |
| 180 |
{ |
| 181 |
CUnit *table[UnitMax]; |
| 182 |
CUnit *unit; |
| 183 |
int nunits; |
| 184 |
int i; |
| 185 |
int num; |
| 186 |
int x; |
| 187 |
int y; |
| 188 |
|
| 189 |
// |
| 190 |
// Remove all workers on the way building something |
| 191 |
// |
| 192 |
nunits = FindPlayerUnitsByType(AiPlayer->Player, type, table, UnitMax); |
| 193 |
for (num = i = 0; i < nunits; ++i) |
| 194 |
{ |
| 195 |
unit = table[i]; |
| 196 |
for (x = 0; x < unit->OrderCount; ++x) |
| 197 |
{ |
| 198 |
if (unit->Orders[x]->Action == UnitActionBuild || |
| 199 |
unit->Orders[x]->Action == UnitActionRepair) |
| 200 |
{ |
| 201 |
break; |
| 202 |
} |
| 203 |
} |
| 204 |
if (x == unit->OrderCount) |
| 205 |
{ |
| 206 |
table[num++] = unit; |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
if (num == 0) |
| 211 |
{ |
| 212 |
// No workers available to build |
| 213 |
return 0; |
| 214 |
} |
| 215 |
|
| 216 |
// Try one worker at random to save cpu |
| 217 |
unit = table[SyncRand() % num]; |
| 218 |
|
| 219 |
// Find a place to build. |
| 220 |
if (AiFindBuildingPlace(unit, building, &x, &y)) |
| 221 |
{ |
| 222 |
CommandBuildBuilding(unit, x, y, building, FlushCommands); |
| 223 |
return 1; |
| 224 |
} |
| 225 |
return 0; |
| 226 |
} |
| 227 |
|
| 228 |
/** |
| 229 |
** Check if we can train the unit. |
| 230 |
** |
| 231 |
** @param type Unit that can train the unit. |
| 232 |
** @param what What to be trained. |
| 233 |
** |
| 234 |
** @return True if made, false if can't be made. |
| 235 |
** |
| 236 |
** @note We must check if the dependencies are fulfilled. |
| 237 |
*/ |
| 238 |
static int AiTrainUnit(const CUnitType *type, CUnitType *what) |
| 239 |
{ |
| 240 |
CUnit *table[UnitMax]; |
| 241 |
CUnit *unit; |
| 242 |
int nunits; |
| 243 |
int i; |
| 244 |
int num; |
| 245 |
|
| 246 |
// |
| 247 |
// Remove all units already doing something |
| 248 |
// or located in an unsuitable terrain. |
| 249 |
// |
| 250 |
nunits = FindPlayerUnitsByType(AiPlayer->Player, type, table, UnitMax); |
| 251 |
for (num = i = 0; i < nunits; ++i) |
| 252 |
{ |
| 253 |
unit = table[i]; |
| 254 |
if (unit->IsIdle() && TerrainAllowsTraining(unit, what)) |
| 255 |
{ |
| 256 |
table[num++] = unit; |
| 257 |
} |
| 258 |
} |
| 259 |
|
| 260 |
for (i = 0; i < num; ++i) |
| 261 |
{ |
| 262 |
unit = table[i]; |
| 263 |
CommandTrainUnit(unit, what, FlushCommands); |
| 264 |
|
| 265 |
return 1; |
| 266 |
} |
| 267 |
|
| 268 |
return 0; |
| 269 |
} |
| 270 |
|
| 271 |
/** |
| 272 |
** Return the number of unit which can produce the given unittype. |
| 273 |
** |
| 274 |
** @param type The unittype we wan't to build |
| 275 |
*/ |
| 276 |
int AiCountUnitBuilders(CUnitType *type) |
| 277 |
{ |
| 278 |
int result; |
| 279 |
int n; |
| 280 |
const int *unit_count; |
| 281 |
std::vector<std::vector<CUnitType *> > *tablep; |
| 282 |
const std::vector<CUnitType *> *table; |
| 283 |
|
| 284 |
if (UnitIdAllowed(AiPlayer->Player, type->Slot) == 0) |
| 285 |
{ |
| 286 |
DebugPrint("Can't build `%s' now\n" _C_ type->Ident.c_str()); |
| 287 |
return 0; |
| 288 |
} |
| 289 |
// |
| 290 |
// Check if we have a place for building or a unit to build. |
| 291 |
// |
| 292 |
if (type->Building) |
| 293 |
{ |
| 294 |
n = AiHelpers.Build.size(); |
| 295 |
tablep = &AiHelpers.Build; |
| 296 |
} |
| 297 |
else |
| 298 |
{ |
| 299 |
n = AiHelpers.Train.size(); |
| 300 |
tablep = &AiHelpers.Train; |
| 301 |
} |
| 302 |
if (type->Slot > n) |
| 303 |
{ |
| 304 |
// Oops not known. |
| 305 |
DebugPrint("Nothing known about `%s'\n" _C_ type->Ident.c_str()); |
| 306 |
return 0; |
| 307 |
} |
| 308 |
table = &(*tablep)[type->Slot]; |
| 309 |
if (!table->size()) |
| 310 |
{ |
| 311 |
// Oops not known. |
| 312 |
DebugPrint("Nothing known about `%s'\n" _C_ type->Ident.c_str()); |
| 313 |
return 0; |
| 314 |
} |
| 315 |
|
| 316 |
unit_count = AiPlayer->Player->UnitTypesCount; |
| 317 |
result = 0; |
| 318 |
for (size_t i = 0; i < table->size(); ++i) |
| 319 |
{ |
| 320 |
// |
| 321 |
// The type for builder/trainer is available |
| 322 |
// |
| 323 |
result += unit_count[(*table)[i]->Slot]; |
| 324 |
} |
| 325 |
return result; |
| 326 |
} |
| 327 |
|
| 328 |
/** |
| 329 |
** Check if we can make a unit-type. |
| 330 |
** |
| 331 |
** @param type Unit-type that must be made. |
| 332 |
** |
| 333 |
** @return True if made, false if can't be made. |
| 334 |
** |
| 335 |
** @note We must check if the dependencies are fulfilled. |
| 336 |
*/ |
| 337 |
static int AiMakeUnit(CUnitType *type) |
| 338 |
{ |
| 339 |
int i; |
| 340 |
int n; |
| 341 |
const int *unit_count; |
| 342 |
std::vector<std::vector<CUnitType *> > *tablep; |
| 343 |
std::vector<CUnitType *> *table; |
| 344 |
|
| 345 |
int usableTypes[UnitTypeMax + 1]; |
| 346 |
int usableTypesCount; |
| 347 |
int currentType; |
| 348 |
|
| 349 |
// Find equivalents unittypes. |
| 350 |
usableTypesCount = AiFindAvailableUnitTypeEquiv(type, usableTypes); |
| 351 |
|
| 352 |
// Iterate them |
| 353 |
for (currentType = 0; currentType < usableTypesCount; ++currentType) |
| 354 |
{ |
| 355 |
type = UnitTypes[usableTypes[currentType]]; |
| 356 |
|
| 357 |
// |
| 358 |
// Check if we have a place for building or a unit to build. |
| 359 |
// |
| 360 |
if (type->Building) |
| 361 |
{ |
| 362 |
n = AiHelpers.Build.size(); |
| 363 |
tablep = &AiHelpers.Build; |
| 364 |
} |
| 365 |
else |
| 366 |
{ |
| 367 |
n = AiHelpers.Train.size(); |
| 368 |
tablep = &AiHelpers.Train; |
| 369 |
} |
| 370 |
if (type->Slot > n) |
| 371 |
{ |
| 372 |
// Oops not known. |
| 373 |
DebugPrint("Nothing known about `%s'\n" _C_ type->Ident.c_str()); |
| 374 |
continue; |
| 375 |
} |
| 376 |
table = &(*tablep)[type->Slot]; |
| 377 |
if (!table->size()) |
| 378 |
{ |
| 379 |
// Oops not known. |
| 380 |
DebugPrint("Nothing known about `%s'\n" _C_ type->Ident.c_str()); |
| 381 |
continue; |
| 382 |
} |
| 383 |
|
| 384 |
unit_count = AiPlayer->Player->UnitTypesCount; |
| 385 |
for (i = 0; i < (int)table->size(); ++i) |
| 386 |
{ |
| 387 |
// |
| 388 |
// The type for builder/trainer is available |
| 389 |
// |
| 390 |
if (unit_count[(*table)[i]->Slot]) |
| 391 |
{ |
| 392 |
if (type->Building) |
| 393 |
{ |
| 394 |
if (AiBuildBuilding((*table)[i], type)) |
| 395 |
{ |
| 396 |
return 1; |
| 397 |
} |
| 398 |
} |
| 399 |
else |
| 400 |
{ |
| 401 |
if (AiTrainUnit((*table)[i], type)) |
| 402 |
{ |
| 403 |
return 1; |
| 404 |
} |
| 405 |
} |
| 406 |
} |
| 407 |
} |
| 408 |
} |
| 409 |
return 0; |
| 410 |
} |
| 411 |
|
| 412 |
/** |
| 413 |
** Check what must be built / trained. |
| 414 |
*/ |
| 415 |
static void AiCheckingWork() |
| 416 |
{ |
| 417 |
int c; |
| 418 |
CUnitType *type; |
| 419 |
AiBuildQueue *queue; |
| 420 |
|
| 421 |
// |
| 422 |
// Look to the build requests, what can be done. |
| 423 |
// |
| 424 |
int sz = AiPlayer->UnitTypeBuilt.size(); |
| 425 |
for (int i = 0; i < sz; ++i) |
| 426 |
{ |
| 427 |
queue = &AiPlayer->UnitTypeBuilt[AiPlayer->UnitTypeBuilt.size() - sz + i]; |
| 428 |
type = queue->Type; |
| 429 |
|
| 430 |
// |
| 431 |
// FIXME: must check if requirements are fulfilled. |
| 432 |
// Buildings can be destroyed. |
| 433 |
|
| 434 |
// |
| 435 |
// Check limits, AI should be broken if reached. |
| 436 |
// |
| 437 |
if (queue->Want > queue->Made && AiPlayer->Player->CheckLimits(type) < 0) |
| 438 |
{ |
| 439 |
continue; |
| 440 |
} |
| 441 |
// |
| 442 |
// Check if resources available. |
| 443 |
// |
| 444 |
if ((c = AiCheckUnitTypeCosts(type))) |
| 445 |
{ |
| 446 |
AiPlayer->NeededMask |= c; |
| 447 |
// |
| 448 |
// NOTE: we can continue and build things with lesser |
| 449 |
// resource or other resource need! |
| 450 |
continue; |
| 451 |
} |
| 452 |
else if (queue->Want > queue->Made && queue->Wait <= GameCycle) |
| 453 |
{ |
| 454 |
if (AiMakeUnit(type)) |
| 455 |
{ |
| 456 |
// AiRequestSupply can change UnitTypeBuilt so recalculate queue |
| 457 |
queue = &AiPlayer->UnitTypeBuilt[AiPlayer->UnitTypeBuilt.size() - sz + i]; |
| 458 |
++queue->Made; |
| 459 |
queue->Wait = 0; |
| 460 |
} |
| 461 |
else if (queue->Type->Building) |
| 462 |
{ |
| 463 |
// Finding a building place is costly, don't try again for a while |
| 464 |
if (queue->Wait == 0) |
| 465 |
{ |
| 466 |
queue->Wait = GameCycle + 150; |
| 467 |
} |
| 468 |
else |
| 469 |
{ |
| 470 |
queue->Wait = GameCycle + 450; |
| 471 |
} |
| 472 |
} |
| 473 |
} |
| 474 |
} |
| 475 |
} |
| 476 |
|
| 477 |
/*---------------------------------------------------------------------------- |
| 478 |
-- WORKERS/RESOURCES |
| 479 |
----------------------------------------------------------------------------*/ |
| 480 |
|
| 481 |
/** |
| 482 |
** Assign worker to gather a resource. |
| 483 |
** |
| 484 |
** @param unit Unit to be assigned. |
| 485 |
** @param resource Resource to find, -1 for any |
| 486 |
** |
| 487 |
** @return 1 if the worker was assigned, 0 otherwise. |
| 488 |
*/ |
| 489 |
static int AiAssignHarvester(CUnit *unit, int resource) |
| 490 |
{ |
| 491 |
CUnit *dest; |
| 492 |
int res = resource; |
| 493 |
|
| 494 |
// It can't. |
| 495 |
if (unit->Removed) |
| 496 |
{ |
| 497 |
return 0; |
| 498 |
} |
| 499 |
|
| 500 |
if (res == -1) |
| 501 |
{ |
| 502 |
// No resource specified, see which resource we need most |
| 503 |
for (int i = 0; i < MaxCosts; ++i) |
| 504 |
{ |
| 505 |
if (unit->Player->ProductionRate[i] == 0) |
| 506 |
{ |
| 507 |
res = i; |
| 508 |
if (unit->Player->StoredResources[i] == 0) |
| 509 |
{ |
| 510 |
break; |
| 511 |
} |
| 512 |
} |
| 513 |
} |
| 514 |
} |
| 515 |
|
| 516 |
// |
| 517 |
// Find a resource to harvest from. |
| 518 |
// |
| 519 |
dest = UnitFindResource(unit, unit->X, unit->Y, 1000, res); |
| 520 |
if (!dest && res != -1) |
| 521 |
{ |
| 522 |
dest = UnitFindResource(unit, unit->X, unit->Y, 1000, -1); |
| 523 |
} |
| 524 |
if (dest) |
| 525 |
{ |
| 526 |
CommandResource(unit, dest, FlushCommands); |
| 527 |
return 1; |
| 528 |
} |
| 529 |
|
| 530 |
// Failed. |
| 531 |
return 0; |
| 532 |
} |
| 533 |
|
| 534 |
/** |
| 535 |
** Assign workers to collect resources. |
| 536 |
*/ |
| 537 |
static void AiCollectResources() |
| 538 |
{ |
| 539 |
int i; |
| 540 |
int needed = -1; |
| 541 |
std::vector<CUnit *> harvesters; |
| 542 |
|
| 543 |
// Check if we need a resource |
| 544 |
if (AiPlayer->NeededMask) |
| 545 |
{ |
| 546 |
int n = AiPlayer->NeededMask; |
| 547 |
while (n != 0) |
| 548 |
{ |
| 549 |
n >>= 1; |
| 550 |
++needed; |
| 551 |
} |
| 552 |
} |
| 553 |
|
| 554 |
// Build a list of all harvesters |
| 555 |
for (i = 0; i < AiPlayer->Player->TotalNumUnits; ++i) |
| 556 |
{ |
| 557 |
CUnit *unit = AiPlayer->Player->Units[i]; |
| 558 |
|
| 559 |
// Ignore non-harvesters |
| 560 |
if (!unit->Type->Harvester) |
| 561 |
{ |
| 562 |
continue; |
| 563 |
} |
| 564 |
|
| 565 |
harvesters.push_back(unit); |
| 566 |
} |
| 567 |
|
| 568 |
// If we need a resource, pick one unit to harvest it |
| 569 |
if (needed != -1 && harvesters.size() != 0) |
| 570 |
{ |
| 571 |
int best = -1; |
| 572 |
for (i = 0; i < (int)harvesters.size(); ++i) |
| 573 |
{ |
| 574 |
// Idle units are always best |
| 575 |
if (harvesters[i]->IsIdle()) |
| 576 |
{ |
| 577 |
best = i; |
| 578 |
break; |
| 579 |
} |
| 580 |
// Unit harvesting the wrong resource |
| 581 |
if (harvesters[i]->Orders[0]->Action == UnitActionResource && |
| 582 |
harvesters[i]->Data.Harvest.CurrentProduction[needed] == 0) |
| 583 |
{ |
| 584 |
best = i; |
| 585 |
} |
| 586 |
} |
| 587 |
|
| 588 |
if (best == -1) |
| 589 |
{ |
| 590 |
// No best unit found, pick one at random |
| 591 |
best = SyncRand() % harvesters.size(); |
| 592 |
} |
| 593 |
|
| 594 |
if (AiAssignHarvester(harvesters[best], needed)) |
| 595 |
{ |
| 596 |
harvesters.erase(harvesters.begin() + best); |
| 597 |
} |
| 598 |
} |
| 599 |
|
| 600 |
// Assign one random idle harvester at a time |
| 601 |
std::vector<CUnit *> idleHarvesters; |
| 602 |
for (i = 0; i < (int)harvesters.size(); ++i) |
| 603 |
{ |
| 604 |
if (harvesters[i]->IsIdle()) |
| 605 |
{ |
| 606 |
idleHarvesters.push_back(harvesters[i]); |
| 607 |
} |
| 608 |
} |
| 609 |
if (idleHarvesters.size() > 0) |
| 610 |
{ |
| 611 |
AiAssignHarvester(idleHarvesters[SyncRand() % idleHarvesters.size()], -1); |
| 612 |
} |
| 613 |
} |
| 614 |
|
| 615 |
/*---------------------------------------------------------------------------- |
| 616 |
-- WORKERS/REPAIR |
| 617 |
----------------------------------------------------------------------------*/ |
| 618 |
|
| 619 |
/** |
| 620 |
** Check if we can repair the building. |
| 621 |
** |
| 622 |
** @param type Unit that can repair the building. |
| 623 |
** @param building Building to be repaired. |
| 624 |
** |
| 625 |
** @return True if can repair, false if can't repair.. |
| 626 |
*/ |
| 627 |
static int AiRepairBuilding(const CUnitType *type, CUnit *building) |
| 628 |
{ |
| 629 |
CUnit *table[UnitMax]; |
| 630 |
CUnit *unit; |
| 631 |
CUnit *unit_temp; |
| 632 |
int distance[UnitMax]; |
| 633 |
int rX; |
| 634 |
int rY; |
| 635 |
int r_temp; |
| 636 |
int index_temp; |
| 637 |
int nunits; |
| 638 |
int i; |
| 639 |
int j; |
| 640 |
int k; |
| 641 |
int num; |
| 642 |
|
| 643 |
unit = NoUnitP; |
| 644 |
|
| 645 |
// |
| 646 |
// Remove all workers not mining. on the way building something |
| 647 |
// FIXME: It is not clever to use workers with gold |
| 648 |
// Idea: Antonis: Put the rest of the workers in a table in case |
| 649 |
// miners can't reach but others can. This will be useful if AI becomes |
| 650 |
// more flexible (e.g.: transports workers to an island) |
| 651 |
// FIXME: too hardcoded, not nice, needs improvement. |
| 652 |
// FIXME: too many workers repair the same building! |
| 653 |
|
| 654 |
// Selection of mining workers. |
| 655 |
nunits = FindPlayerUnitsByType(AiPlayer->Player, type, table, UnitMax); |
| 656 |
for (num = i = 0; i < nunits; ++i) |
| 657 |
{ |
| 658 |
unit = table[i]; |
| 659 |
if (unit->Type->RepairRange && |
| 660 |
(unit->Orders[0]->Action == UnitActionResource || |
| 661 |
unit->Orders[0]->Action == UnitActionStill) && |
| 662 |
unit->OrderCount == 1) |
| 663 |
{ |
| 664 |
table[num++] = unit; |
| 665 |
} |
| 666 |
} |
| 667 |
|
| 668 |
// Sort by distance loops -Antonis- |
| 669 |
for (i = 0; i < num; ++i) |
| 670 |
{ |
| 671 |
unit = table[i]; |
| 672 |
// FIXME: Probably calculated from top left corner of building |
| 673 |
if ((rX = unit->X - building->X) < 0) |
| 674 |
{ |
| 675 |
rX = -rX; |
| 676 |
} |
| 677 |
if ((rY = unit->Y - building->Y) < 0) |
| 678 |
{ |
| 679 |
rY = -rY; |
| 680 |
} |
| 681 |
if (rX < rY) |
| 682 |
{ |
| 683 |
distance[i] = rX; |
| 684 |
} |
| 685 |
else |
| 686 |
{ |
| 687 |
distance[i] = rY; |
| 688 |
} |
| 689 |
} |
| 690 |
for (i = 0; i < num; ++i) |
| 691 |
{ |
| 692 |
r_temp = distance[i]; |
| 693 |
index_temp = i; |
| 694 |
for (j = i; j < num; ++j) |
| 695 |
{ |
| 696 |
if (distance[j] < r_temp) |
| 697 |
{ |
| 698 |
r_temp = distance[j]; |
| 699 |
index_temp = j; |
| 700 |
} |
| 701 |
} |
| 702 |
if (index_temp > i) |
| 703 |
{ |
| 704 |
unit_temp = table[index_temp]; |
| 705 |
table[index_temp] = table[i]; |
| 706 |
distance[index_temp] = distance[i]; |
| 707 |
table[i] = unit_temp; |
| 708 |
distance[i] = r_temp; // May be omitted, here for completence |
| 709 |
} |
| 710 |
} |
| 711 |
|
| 712 |
// Check if building is reachable and try next trio of workers |
| 713 |
|
| 714 |
if ((j = AiPlayer->TriedRepairWorkers[UnitNumber(building)]) > num) |
| 715 |
{ |
| 716 |
j = AiPlayer->TriedRepairWorkers[UnitNumber(building)] = 0; |
| 717 |
} |
| 718 |
|
| 719 |
for (k = i = j; i < num && i < j + 3; ++i) |
| 720 |
{ |
| 721 |
unit = table[i]; |
| 722 |
|
| 723 |
if (UnitReachable(unit, building, unit->Type->RepairRange)) |
| 724 |
{ |
| 725 |
CommandRepair(unit, 0, 0, building, FlushCommands); |
| 726 |
return 1; |
| 727 |
} |
| 728 |
k = i; |
| 729 |
} |
| 730 |
AiPlayer->TriedRepairWorkers[UnitNumber(building)] = k + 1; |
| 731 |
return 0; |
| 732 |
} |
| 733 |
|
| 734 |
/** |
| 735 |
** Check if we can repair this unit. |
| 736 |
** |
| 737 |
** @param unit Unit that must be repaired. |
| 738 |
** |
| 739 |
** @return True if made, false if can't be made. |
| 740 |
*/ |
| 741 |
static int AiRepairUnit(CUnit *unit) |
| 742 |
{ |
| 743 |
int n; |
| 744 |
const CUnitType *type; |
| 745 |
const int *unit_count; |
| 746 |
std::vector<std::vector<CUnitType *> > *tablep; |
| 747 |
std::vector<CUnitType *> *table; |
| 748 |
|
| 749 |
n = AiHelpers.Repair.size(); |
| 750 |
tablep = &AiHelpers.Repair; |
| 751 |
type = unit->Type; |
| 752 |
if (type->Slot > n) |
| 753 |
{ |
| 754 |
// Oops not known. |
| 755 |
DebugPrint("Nothing known about `%s'\n" _C_ type->Ident.c_str()); |
| 756 |
return 0; |
| 757 |
} |
| 758 |
table = &(*tablep)[type->Slot]; |
| 759 |
if (!table->size()) |
| 760 |
{ |
| 761 |
// Oops not known. |
| 762 |
DebugPrint("Nothing known about `%s'\n" _C_ type->Ident.c_str()); |
| 763 |
return 0; |
| 764 |
} |
| 765 |
|
| 766 |
unit_count = AiPlayer->Player->UnitTypesCount; |
| 767 |
for (size_t i = 0; i < table->size(); ++i) |
| 768 |
{ |
| 769 |
// The type is available |
| 770 |
if (unit_count[(*table)[i]->Slot]) |
| 771 |
{ |
| 772 |
if (AiRepairBuilding((*table)[i], unit)) |
| 773 |
{ |
| 774 |
return 1; |
| 775 |
} |
| 776 |
} |
| 777 |
} |
| 778 |
|
| 779 |
return 0; |
| 780 |
} |
| 781 |
|
| 782 |
/** |
| 783 |
** Check if there's a unit that should be repaired. |
| 784 |
*/ |
| 785 |
static void AiCheckRepair() |
| 786 |
{ |
| 787 |
int i; |
| 788 |
int k; |
| 789 |
int n; |
| 790 |
bool repair_flag; |
| 791 |
CUnit *unit; |
| 792 |
|
| 793 |
n = AiPlayer->Player->TotalNumUnits; |
| 794 |
k = 0; |
| 795 |
// Selector for next unit |
| 796 |
for (i = n - 1; i >= 0; --i) |
| 797 |
{ |
| 798 |
unit = AiPlayer->Player->Units[i]; |
| 799 |
if (UnitNumber(unit) == AiPlayer->LastRepairBuilding) |
| 800 |
{ |
| 801 |
k = i + 1; |
| 802 |
} |
| 803 |
} |
| 804 |
|
| 805 |
|
| 806 |
for (i = k; i < n; ++i) |
| 807 |
{ |
| 808 |
unit = AiPlayer->Player->Units[i]; |
| 809 |
repair_flag = true; |
| 810 |
// Unit damaged? |
| 811 |
// Don't repair attacked unit (wait 5 sec before repairing) |
| 812 |
if (unit->Type->RepairHP && |
| 813 |
unit->Orders[0]->Action != UnitActionBuilt && |
| 814 |
unit->Variable[HP_INDEX].Value < unit->Variable[HP_INDEX].Max && |
| 815 |
unit->Attacked + 5 * CYCLES_PER_SECOND < GameCycle) |
| 816 |
{ |
| 817 |
|
| 818 |
// FIXME: Repair only units under control |
| 819 |
if (AiEnemyUnitsInDistance(unit, unit->Stats->Variables[SIGHTRANGE_INDEX].Max)) |
| 820 |
{ |
| 821 |
continue; |
| 822 |
} |
| 823 |
// |
| 824 |
// Must check if there are enough resources |
| 825 |
// |
| 826 |
for (int j = 0; j < MaxCosts; ++j) |
| 827 |
{ |
| 828 |
if (unit->Type->ProductionCosts[j] && |
| 829 |
AiPlayer->Player->ProductionRate[j] == 0 && |
| 830 |
AiPlayer->Player->StoredResources[j] == 0) |
| 831 |
{ |
| 832 |
repair_flag = false; |
| 833 |
break; |
| 834 |
} |
| 835 |
} |
| 836 |
|
| 837 |
// |
| 838 |
// Find a free worker, who can build this building can repair it? |
| 839 |
// |
| 840 |
if (repair_flag) |
| 841 |
{ |
| 842 |
AiRepairUnit(unit); |
| 843 |
AiPlayer->LastRepairBuilding = UnitNumber(unit); |
| 844 |
return; |
| 845 |
} |
| 846 |
} |
| 847 |
|
| 848 |
// Building under construction but no worker |
| 849 |
if (unit->Orders[0]->Action == UnitActionBuilt) |
| 850 |
{ |
| 851 |
int j; |
| 852 |
for (j = 0; j < AiPlayer->Player->TotalNumUnits; ++j) |
| 853 |
{ |
| 854 |
COrder *order = AiPlayer->Player->Units[j]->Orders[0]; |
| 855 |
if (order->Action == UnitActionRepair && order->Goal == unit) |
| 856 |
{ |
| 857 |
break; |
| 858 |
} |
| 859 |
} |
| 860 |
if (j == AiPlayer->Player->TotalNumUnits) |
| 861 |
{ |
| 862 |
// Make sure we have enough resources first |
| 863 |
for (j = 0; j < MaxCosts; ++j) |
| 864 |
{ |
| 865 |
// FIXME: the resources don't necessarily have to be in storage |
| 866 |
if (AiPlayer->Player->StoredResources[j] < unit->Type->ProductionCosts[j]) |
| 867 |
{ |
| 868 |
break; |
| 869 |
} |
| 870 |
} |
| 871 |
if (j == MaxCosts) |
| 872 |
{ |
| 873 |
AiRepairUnit(unit); |
| 874 |
AiPlayer->LastRepairBuilding = UnitNumber(unit); |
| 875 |
return; |
| 876 |
} |
| 877 |
} |
| 878 |
} |
| 879 |
} |
| 880 |
AiPlayer->LastRepairBuilding = 0; |
| 881 |
} |
| 882 |
|
| 883 |
/** |
| 884 |
** Add unit-type request to resource manager. |
| 885 |
** |
| 886 |
** @param type Unit type requested. |
| 887 |
** @param count How many units. |
| 888 |
** |
| 889 |
** @todo FIXME: should store the end of list and not search it. |
| 890 |
*/ |
| 891 |
void AiAddUnitTypeRequest(CUnitType *type, int count) |
| 892 |
{ |
| 893 |
AiBuildQueue queue; |
| 894 |
|
| 895 |
queue.Type = type; |
| 896 |
queue.Want = count; |
| 897 |
queue.Made = 0; |
| 898 |
AiPlayer->UnitTypeBuilt.push_back(queue); |
| 899 |
} |
| 900 |
|
| 901 |
/** |
| 902 |
** Entry point of resource manager, periodically called. |
| 903 |
*/ |
| 904 |
void AiResourceManager() |
| 905 |
{ |
| 906 |
// |
| 907 |
// Check if something needs to be build / trained. |
| 908 |
// |
| 909 |
AiCheckingWork(); |
| 910 |
|
| 911 |
// |
| 912 |
// Collect resources. |
| 913 |
// |
| 914 |
if ((GameCycle / CYCLES_PER_SECOND) % COLLECT_RESOURCES_INTERVAL == |
| 915 |
(unsigned long)AiPlayer->Player->Index % COLLECT_RESOURCES_INTERVAL) |
| 916 |
{ |
| 917 |
AiCollectResources(); |
| 918 |
} |
| 919 |
|
| 920 |
// |
| 921 |
// Check repair. |
| 922 |
// |
| 923 |
AiCheckRepair(); |
| 924 |
|
| 925 |
AiPlayer->NeededMask = 0; |
| 926 |
} |
| 927 |
|
| 928 |
//@} |