1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607
|
// -------------------------------------------------------------------------
// AAI
//
// A skirmish AI for the Spring engine.
// Copyright Alexander Seizinger
//
// Released under GPL license: see LICENSE.html for more information.
// -------------------------------------------------------------------------
#include <set>
#include "AAI.h"
#include "AAIConstructor.h"
#include "AAIBuildTask.h"
#include "AAIExecute.h"
#include "AAIBuildTable.h"
#include "AAIUnitTable.h"
#include "AAIConfig.h"
#include "AAIMap.h"
#include "AAISector.h"
#include "LegacyCpp/UnitDef.h"
#include "LegacyCpp/CommandQueue.h"
using namespace springLegacyAI;
AAIConstructor::AAIConstructor(AAI *ai, int unit_id, int def_id, bool factory, bool builder, bool assistant)
{
this->ai = ai;
this->unit_id = unit_id;
this->def_id = def_id;
buildspeed = ai->Getbt()->GetUnitDef(def_id).buildSpeed;
construction_unit_id = -1;
construction_def_id = -1;
construction_category = UNKNOWN;
assistance = -1;
build_task = 0;
order_tick = 0;
task = UNIT_IDLE;
build_pos = ZeroVector;
this->factory = factory;
this->builder = builder;
this->assistant = assistant;
buildque = ai->Getexecute()->GetBuildqueueOfFactory(def_id);
}
AAIConstructor::~AAIConstructor(void)
{
}
// returns true if factory is busy
bool AAIConstructor::IsBusy()
{
const CCommandQueue *commands = ai->Getcb()->GetCurrentUnitCommands(unit_id);
if(commands->empty())
return false;
else
return true;
}
void AAIConstructor::Idle()
{
//ai->LogConsole("%s is idle", ai->Getbt()->GetUnitDef(def_id-1).humanName.c_str());
if(builder)
{
if(task == BUILDING)
{
if(!ai->Getbt()->IsValidUnitDefID(construction_unit_id))
{
//ai->Getbt()->units_dynamic[construction_def_id].active -= 1;
//assert(ai->Getbt()->units_dynamic[construction_def_id].active >= 0);
ai->Getut()->UnitRequestFailed(construction_category);
// clear up buildmap etc. (make sure conctructor wanted to build a building and not a unit)
if(ai->Getbt()->units_static[construction_def_id].category <= METAL_MAKER)
ai->Getexecute()->ConstructionFailed(build_pos, construction_def_id);
// free builder
ConstructionFinished();
}
}
else if(task != UNIT_KILLED)
{
task = UNIT_IDLE;
assistance = -1;
ReleaseAllAssistants();
}
}
if(factory)
{
ConstructionFinished();
Update();
}
}
void AAIConstructor::Update()
{
if(factory && buildque != nullptr)
{
if(!ai->Getbt()->IsValidUnitDefID(construction_def_id) && !buildque->empty())
{
int def_id = (*buildque->begin());
UnitCategory cat = ai->Getbt()->units_static[def_id].category;
if(ai->Getbt()->IsBuilder(def_id) || cat == SCOUT || ai->Getcb()->GetMetal() > 50
|| ai->Getbt()->units_static[def_id].cost < ai->Getbt()->avg_cost[ai->Getbt()->units_static[def_id].category][ai->Getside()-1])
{
// check if mobile or stationary builder
if(ai->Getbt()->IsStatic(this->def_id))
{
// give build order
Command c;
c.id = -def_id;
ai->Getcb()->GiveOrder(unit_id, &c);
construction_def_id = def_id;
assert(ai->Getbt()->IsValidUnitDefID(def_id));
task = BUILDING;
//if(ai->Getbt()->IsFactory(def_id))
// ++ai->futureFactories;
buildque->pop_front();
}
else
{
// find buildpos for the unit
float3 pos = ai->Getexecute()->GetUnitBuildsite(unit_id, def_id);
if(pos.x > 0)
{
// give build order
Command c;
c.id = -def_id;
c.params.resize(3);
c.params[0] = pos.x;
c.params[1] = pos.y;
c.params[2] = pos.z;
ai->Getcb()->GiveOrder(unit_id, &c);
construction_def_id = def_id;
assert(ai->Getbt()->IsValidUnitDefID(def_id));
task = BUILDING;
++ai->Getut()->futureUnits[cat];
buildque->pop_front();
}
}
return;
}
}
CheckAssistance();
}
if(builder)
{
if(task == BUILDING)
{
// if building has begun, check for possible assisters
if(construction_unit_id >= 0)
CheckAssistance();
// if building has not yet begun, check if something unexpected happened (buildsite blocked)
else
{
if(!IsBusy() && !ai->Getbt()->IsValidUnitDefID(construction_unit_id))
ConstructionFailed();
}
}
/*else if(task == UNIT_IDLE)
{
float3 pos = ai->Getcb()->GetUnitPos(unit_id);
if(pos.x > 0)
{
int x = pos.x/ai->Getmap()->xSectorSize;
int y = pos.z/ai->Getmap()->ySectorSize;
if(x >= 0 && y >= 0 && x < ai->Getmap()->xSectors && y < ai->Getmap()->ySectors)
{
if(ai->Getmap()->sector[x][y].distance_to_base < 2)
{
pos = ai->Getmap()->sector[x][y].GetCenter();
Command c;
const UnitDef *def;
def = ai->Getcb()->GetUnitDef(unit_id);
// can this thing resurrect? If so, maybe we should raise the corpses instead of consuming them?
if(def->canResurrect)
{
if(rand()%2 == 1)
c.id = CMD_RESURRECT;
else
c.id = CMD_RECLAIM;
}
else
c.id = CMD_RECLAIM;
c.params.resize(4);
c.params[0] = pos.x;
c.params[1] = ai->Getcb()->GetElevation(pos.x, pos.z);
c.params[2] = pos.z;
c.params[3] = 500.0;
//ai->Getcb()->GiveOrder(unit_id, &c);
task = RECLAIMING;
ai->Getexecute()->GiveOrder(&c, unit_id, "Builder::Reclaming");
}
}
}
}
*/
}
}
void AAIConstructor::CheckAssistance()
{
if(factory && (buildque != nullptr))
{
// check if another factory of that type needed
if(buildque->size() >= cfg->MAX_BUILDQUE_SIZE - 2 && assistants.size() >= cfg->MAX_ASSISTANTS-2)
{
if(ai->Getbt()->units_dynamic[def_id].active + ai->Getbt()->units_dynamic[def_id].requested + ai->Getbt()->units_dynamic[def_id].under_construction < cfg->MAX_FACTORIES_PER_TYPE)
{
ai->Getbt()->units_dynamic[def_id].requested += 1;
if(ai->Getexecute()->urgency[STATIONARY_CONSTRUCTOR] < 1.5f)
ai->Getexecute()->urgency[STATIONARY_CONSTRUCTOR] = 1.5f;
for(list<int>::iterator j = ai->Getbt()->units_static[def_id].canBuildList.begin(); j != ai->Getbt()->units_static[def_id].canBuildList.end(); ++j)
ai->Getbt()->units_dynamic[*j].constructorsRequested += 1;
}
}
// check if support needed
if(assistants.size() < cfg->MAX_ASSISTANTS)
{
bool assist = false;
if(buildque->size() > 2)
assist = true;
else if(ai->Getbt()->IsValidUnitDefID(construction_def_id)) {
float buildtime = 1e6;
if (buildspeed > 0) {
//FIXME why use *1/30 here? below there is exactly the same code w/o it, so what's the correct one?
buildtime = ai->Getbt()->GetUnitDef(construction_def_id).buildTime / (30.0f * buildspeed);
}
if (buildtime > cfg->MIN_ASSISTANCE_BUILDTIME)
assist = true;
}
if(assist)
{
AAIConstructor* assistant = ai->Getut()->FindClosestAssistant(ai->Getcb()->GetUnitPos(unit_id), 5, true);
if(assistant)
{
assistants.insert(assistant->unit_id);
assistant->AssistConstruction(unit_id);
}
}
}
// check if assistants are needed anymore
else if(!assistants.empty() && buildque->empty() && !ai->Getbt()->IsValidUnitDefID(construction_def_id))
{
//ai->LogConsole("factory releasing assistants");
ReleaseAllAssistants();
}
}
if(builder && build_task)
{
// prevent assisting when low on ressources
if(ai->Getexecute()->averageMetalSurplus < 0.1)
{
if(construction_category == METAL_MAKER)
{
if(ai->Getexecute()->averageEnergySurplus < 0.5 * ai->Getbt()->GetUnitDef(construction_def_id).energyUpkeep)
return;
}
else if(construction_category != EXTRACTOR && construction_category != POWER_PLANT)
return;
}
float buildtime = 1e6;
if (buildspeed > 0) {
buildtime = ai->Getbt()->GetUnitDef(construction_def_id).buildTime / buildspeed;
}
if((buildtime > cfg->MIN_ASSISTANCE_BUILDTIME) && (assistants.size() < cfg->MAX_ASSISTANTS))
{
// com only allowed if buildpos is inside the base
bool commander = false;
int x = build_pos.x / ai->Getmap()->xSectorSize;
int y = build_pos.z / ai->Getmap()->ySectorSize;
if(x >= 0 && y >= 0 && x < ai->Getmap()->xSectors && y < ai->Getmap()->ySectors)
{
if(ai->Getmap()->sector[x][y].distance_to_base == 0)
commander = true;
}
AAIConstructor* assistant = ai->Getut()->FindClosestAssistant(build_pos, 5, commander);
if(assistant)
{
assistants.insert(assistant->unit_id);
assistant->AssistConstruction(unit_id, construction_unit_id);
}
}
}
}
double AAIConstructor::GetMyQueBuildtime()
{
if (buildque == nullptr)
return 0;
double buildtime = 0;
for(list<int>::iterator unit = buildque->begin(); unit != buildque->end(); ++unit)
buildtime += ai->Getbt()->GetUnitDef((*unit)).buildTime;
return buildtime;
}
void AAIConstructor::GiveReclaimOrder(int unit_id)
{
if(assistance >= 0)
{
ai->Getut()->units[assistance].cons->RemoveAssitant(unit_id);
assistance = -1;
}
task = RECLAIMING;
Command c;
c.id = CMD_RECLAIM;
c.params.push_back(unit_id);
//ai->Getcb()->GiveOrder(this->unit_id, &c);
ai->Getexecute()->GiveOrder(&c, this->unit_id, "Builder::GiveRelaimOrder");
}
void AAIConstructor::GiveConstructionOrder(int id_building, float3 pos, bool water)
{
// get def and final position
const UnitDef *def = &ai->Getbt()->GetUnitDef(id_building);
// give order if building can be placed at the desired position (position lies within a valid sector)
if(ai->Getexecute()->InitBuildingAt(def, &pos, water))
{
order_tick = ai->Getcb()->GetCurrentFrame();
// check if builder was previously assisting other builders/factories
if(assistance >= 0)
{
ai->Getut()->units[assistance].cons->RemoveAssitant(unit_id);
assistance = -1;
}
// set building as current task and order construction
build_pos = pos;
construction_def_id = id_building;
assert(ai->Getbt()->IsValidUnitDefID(id_building));
task = BUILDING;
construction_category = ai->Getbt()->units_static[id_building].category;
// order builder to construct building
Command c;
c.id = - id_building;
c.params.resize(3);
c.params[0] = build_pos.x;
c.params[1] = build_pos.y;
c.params[2] = build_pos.z;
ai->Getcb()->GiveOrder(unit_id, &c);
// increase number of active units of that type/category
ai->Getbt()->units_dynamic[def->id].requested += 1;
ai->Getut()->UnitRequested(construction_category);
if(ai->Getbt()->IsFactory(id_building))
ai->Getut()->futureFactories += 1;
}
}
void AAIConstructor::AssistConstruction(int constructor, int target_unit)
{
if(target_unit == -1)
{
Command c;
// Check if the target can be assisted at all. If not, try to repair it instead
const UnitDef *def;
def = ai->Getcb()->GetUnitDef(constructor);
if(def->canBeAssisted)
{
c.id = CMD_GUARD;
} else
{
c.id = CMD_REPAIR;
}
c.params.push_back(constructor);
//ai->Getcb()->GiveOrder(unit_id, &c);
ai->Getexecute()->GiveOrder(&c, unit_id, "Builder::Assist");
task = ASSISTING;
assistance = constructor;
}
else
{
Command c;
c.id = CMD_REPAIR;
c.params.push_back(target_unit);
//ai->Getcb()->GiveOrder(unit_id, &c);
ai->Getexecute()->GiveOrder(&c, unit_id, "Builder::Assist");
task = ASSISTING;
assistance = constructor;
}
}
void AAIConstructor::TakeOverConstruction(AAIBuildTask *build_task)
{
if(assistance >= 0)
{
ai->Getut()->units[assistance].cons->RemoveAssitant(unit_id);
assistance = -1;
}
order_tick = build_task->order_tick;
construction_unit_id = build_task->unit_id;
construction_def_id = build_task->def_id;
assert(ai->Getbt()->IsValidUnitDefID(construction_def_id));
construction_category = ai->Getbt()->units_static[construction_def_id].category;
build_pos = build_task->build_pos;
Command c;
c.id = CMD_REPAIR;
c.params.push_back(build_task->unit_id);
task = BUILDING;
ai->Getcb()->GiveOrder(unit_id, &c);
}
void AAIConstructor::ConstructionFailed()
{
--ai->Getbt()->units_dynamic[construction_def_id].requested;
ai->Getut()->UnitRequestFailed(construction_category);
// clear up buildmap etc.
if(ai->Getbt()->units_static[construction_def_id].category <= METAL_MAKER)
ai->Getexecute()->ConstructionFailed(build_pos, construction_def_id);
// tells the builder construction has finished
ConstructionFinished();
}
void AAIConstructor::ConstructionFinished()
{
task = UNIT_IDLE;
build_pos = ZeroVector;
construction_def_id = -1;
construction_unit_id = -1;
construction_category = UNKNOWN;
build_task = 0;
// release assisters
ReleaseAllAssistants();
}
void AAIConstructor::ReleaseAllAssistants()
{
// release assisters
for(set<int>::iterator i = assistants.begin(); i != assistants.end(); ++i)
{
if(ai->Getut()->units[*i].cons)
ai->Getut()->units[*i].cons->StopAssisting();
}
assistants.clear();
}
void AAIConstructor::StopAssisting()
{
task = UNIT_IDLE;
assistance = -1;
Command c;
c.id = CMD_STOP;
//ai->Getcb()->GiveOrder(unit_id, &c);
ai->Getexecute()->GiveOrder(&c, unit_id, "Builder::StopAssisting");
}
void AAIConstructor::RemoveAssitant(int unit_id)
{
assistants.erase(unit_id);
}
void AAIConstructor::Killed()
{
if(builder)
{
// when builder was killed on the way to the buildsite, inform ai that construction
// of building hasnt been started
if(task == BUILDING)
{
//if buildling has not begun yet, decrease some values
if(construction_unit_id == -1)
{
// killed on the way to the buildsite
ai->Getmap()->UnitKilledAt(&build_pos, MOBILE_CONSTRUCTOR);
// clear up buildmap etc.
ConstructionFailed();
}
// building has begun
else
{
if(build_task)
build_task->BuilderDestroyed();
}
}
else if(task == ASSISTING)
{
ai->Getut()->units[assistance].cons->RemoveAssitant(unit_id);
}
}
ReleaseAllAssistants();
task = UNIT_KILLED;
}
void AAIConstructor::Retreat(UnitCategory attacked_by)
{
if(task != UNIT_KILLED)
{
float3 pos = ai->Getcb()->GetUnitPos(unit_id);
int x = pos.x / ai->Getmap()->xSectorSize;
int y = pos.z / ai->Getmap()->ySectorSize;
// attacked by scout
if(attacked_by == SCOUT)
{
// dont flee from scouts in your own base
if(x >= 0 && y >= 0 && x < ai->Getmap()->xSectors && y < ai->Getmap()->ySectors)
{
// builder is within base
if(ai->Getmap()->sector[x][y].distance_to_base == 0)
return;
// dont flee outside of the base if health is > 50%
else
{
if(ai->Getcb()->GetUnitHealth(unit_id) > ai->Getbt()->GetUnitDef(def_id).health / 2.0)
return;
}
}
}
else
{
if(x >= 0 && y >= 0 && x < ai->Getmap()->xSectors && y < ai->Getmap()->ySectors)
{
// builder is within base
if(ai->Getmap()->sector[x][y].distance_to_base == 0)
return;
}
}
// get safe position
pos = ai->Getexecute()->GetSafePos(def_id, pos);
if(pos.x > 0)
{
Command c;
c.id = CMD_MOVE;
c.params.push_back(pos.x);
c.params.push_back(ai->Getcb()->GetElevation(pos.x, pos.z));
c.params.push_back(pos.z);
ai->Getexecute()->GiveOrder(&c, unit_id, "BuilderRetreat");
//ai->Getcb()->GiveOrder(unit_id, &c);
}
}
}
|