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 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838
|
/*
Copyright (C) 2001-2004 Stephane Magnenat & Luc-Olivier de Charrière
Copyright (C) 2005 Eli Dupree
for any question or comment contact us at <stephane at magnenat dot net> or <NuageBleu at gmail dot com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "AIWarrush.h"
#include "Building.h"
#include "Unit.h"
#include "Building.h"
#include "Game.h"
#include "GlobalContainer.h"
#include "Order.h"
#include "Player.h"
#include "Brush.h"
#include "Utilities.h"
#include <valarray>
#include <algorithm>
#define BUILDING_DELAY 30
#define AREAS_DELAY 50
using namespace boost;
void AIWarrush::init(Player *player)
{
assert(player);
this->player=player;
this->team=player->team;
this->game=player->game;
this->map=player->map;
buildingDelay = 0;
areaUpdatingDelay = 0;
assert(this->team);
assert(this->game);
assert(this->map);
}
AIWarrush::AIWarrush(Player *player)
{
init(player);
}
AIWarrush::AIWarrush(GAGCore::InputStream *stream, Player *player, Sint32 versionMinor)
{
init(player);
}
AIWarrush::~AIWarrush()
{
}
bool AIWarrush::load(GAGCore::InputStream *stream, Player *player, Sint32 versionMinor)
{
init(player);
return true;
}
void AIWarrush::save(GAGCore::OutputStream *stream)
{
}
int AIWarrush::numberOfUnitsWithSkillGreaterThanValue(const int skill, const int value)const
{
Unit **myUnits=team->myUnits;
int count = 0;
for (int i=0; i<1024; i++)
{
Unit *u=myUnits[i];
if ((u)&&(u->performance[skill]>value))
{
count++;
}
}
return count;
}
int AIWarrush::numberOfUnitsWithSkillEqualToValue(const int skill, const int value)const
{
Unit **myUnits=team->myUnits;
int count = 0;
for (int i=0; i<1024; i++)
{
Unit *u=myUnits[i];
if ((u)&&(u->performance[skill]==value))
{
count++;
}
}
return count;
}
bool AIWarrush::isAnyUnitWithLessThanOneThirdFood()const
{
Unit **myUnits=team->myUnits;
for (int i=0; i<1024; i++)
{
Unit *u=myUnits[i];
if ((u)&&(u->hungry<(Unit::HUNGRY_MAX/2))) //Yeah, it's a half, not a third. Weird huh? :P
{
return true;
}
}
return false;
}
Building *AIWarrush::getSwarmWithoutSettings(const int workerRatio, const int explorerRatio, const int warriorRatio)const
{
Building **myBuildings=team->myBuildings;
for (int i=0; i<1024; i++)
{
Building *b=myBuildings[i];
if ( (b)
&& (b->type->shortTypeNum==IntBuildingType::SWARM_BUILDING)
&& (b->constructionResultState == Building::NO_CONSTRUCTION)
&& ((b->ratio[0] != workerRatio) || (b->ratio[1] != explorerRatio) || (b->ratio[2] != warriorRatio))
)
{
return b;
}
}
return NULL;
}
Building *AIWarrush::getBuildingWithoutWorkersAssigned(Sint32 shortTypeNum, int num_workers)const
{
Building **myBuildings=team->myBuildings;
for (int i=0; i<1024; i++)
{
Building *b=myBuildings[i];
if ( (b)
&& (b->type->shortTypeNum==shortTypeNum)
&& (b->maxUnitWorking != num_workers)
&& (b->constructionResultState != Building::NO_CONSTRUCTION || (b->type->shortTypeNum==shortTypeNum != IntBuildingType::ATTACK_BUILDING && b->type->shortTypeNum==shortTypeNum != IntBuildingType::HEAL_BUILDING && b->type->shortTypeNum==shortTypeNum != IntBuildingType::WALKSPEED_BUILDING && b->type->shortTypeNum==shortTypeNum != IntBuildingType::SWIMSPEED_BUILDING && b->type->shortTypeNum==shortTypeNum != IntBuildingType::SCIENCE_BUILDING))
)
{
return b;
}
}
return NULL;
}
Building *AIWarrush::getSwarmAtRandom()const
{
Building **myBuildings=team->myBuildings;
int swarmsfound = 0;
Building *chosen_swarm = NULL;
for (int i=0; i<1024; i++)
{
Building *b=myBuildings[i];
if ((b) && (b->type->shortTypeNum==IntBuildingType::SWARM_BUILDING))
{
++swarmsfound;
if(syncRand()%swarmsfound == 0)
{
chosen_swarm = b;
}
}
}
return chosen_swarm;
}
bool AIWarrush::allOfBuildingTypeAreCompleted(Sint32 shortTypeNum)const
{
Building **myBuildings=team->myBuildings;
for (int i=0; i<1024; i++)
{
Building *b=myBuildings[i];
if (
(b)
&& (b->type->shortTypeNum==shortTypeNum)
&& (
b->constructionResultState != Building::NO_CONSTRUCTION
|| b->buildingState == Building::DEAD
)
)
{
return false;
}
}
return true;
}
bool AIWarrush::allOfBuildingTypeAreFull(Sint32 shortTypeNum)const
{
Building **myBuildings=team->myBuildings;
for (int i=0; i<1024; i++)
{
Building *b=myBuildings[i];
if (
(b)
&& (b->type->shortTypeNum==shortTypeNum)
&& (
b->unitsInside.size() < (size_t)b->maxUnitInside
)
)
{
return false;
}
}
return true;
}
int AIWarrush::numberOfBuildingsOfType(Sint32 shortTypeNum)const
{
Building **myBuildings=team->myBuildings;
int count = 0;
for (int i=0; i<1024; i++)
{
Building *b=myBuildings[i];
if((b)&&(
b->shortTypeNum == shortTypeNum
))++count;
}
return count;
}
int AIWarrush::numberOfExtraBuildings()const
{
Building **myBuildings=team->myBuildings;
int count = 0;
for (int i=0; i<1024; i++)
{
Building *b=myBuildings[i];
if((b)&&(
b->shortTypeNum == IntBuildingType::HEAL_BUILDING
|| b->shortTypeNum == IntBuildingType::WALKSPEED_BUILDING
|| b->shortTypeNum == IntBuildingType::SWIMSPEED_BUILDING
|| b->shortTypeNum == IntBuildingType::SCIENCE_BUILDING
|| b->shortTypeNum == IntBuildingType::DEFENSE_BUILDING
))++count;
}
return count;
}
bool AIWarrush::allOfBuildingTypeAreFullyWorked(Sint32 shortTypeNum)const
{
Building **myBuildings=team->myBuildings;
for (int i=0; i<1024; i++)
{
Building *b=myBuildings[i];
if((b)&&(b->shortTypeNum == shortTypeNum))
{
if(b->unitsWorking.size() == (size_t)b->maxUnitWorking)
{
}
else
{
return false;
}
}
}
return true;
}
bool AIWarrush::percentageOfBuildingsAreFullyWorked(int percentage)const
{
Building **myBuildings=team->myBuildings;
int num_buildings = 0;
int num_worked_buildings = 0;
for (int i=0; i<1024; i++)
{
Building *b=myBuildings[i];
if((b)&&(b->shortTypeNum != IntBuildingType::WAR_FLAG)&&(b->shortTypeNum != IntBuildingType::EXPLORATION_FLAG)&&(b->shortTypeNum != IntBuildingType::CLEARING_FLAG))
{
++num_buildings;
if(b->unitsWorking.size() == (size_t)b->maxUnitWorking)
{
++num_worked_buildings;
if(verbose)std::cout << "A";
}
else if((b->type->shortTypeNum == IntBuildingType::SWARM_BUILDING
|| b->type->shortTypeNum == IntBuildingType::FOOD_BUILDING)
&&
b->constructionResultState == Building::NO_CONSTRUCTION
&&
(b->ressources[CORN]) > ((b->wishedResources[CORN]) * 2 / 3))
{//heavily worked swarms and inns sometimes are full and have no workers
++num_worked_buildings;
if(verbose)std::cout << "C";
}
}
}
if(verbose)std::cout << ": " << num_worked_buildings << " worked out of " << num_buildings << "\n";
return num_worked_buildings * 100 >= num_buildings * percentage;
}
boost::shared_ptr<Order> AIWarrush::getOrder(void)
{
// reduce delays
if (buildingDelay > 0)
buildingDelay--;
if (areaUpdatingDelay > 0)
areaUpdatingDelay--;
if(game->stepCounter < 64 && game->stepCounter%2 == 0)
{
int teamIndex = game->stepCounter / 2;
Team *enemy_team = game->teams[teamIndex];
if((enemy_team)&&(team->enemies & enemy_team->me))return setupExploreFlagForTeam(enemy_team);
}
//keep those areas up to date
if(areaUpdatingDelay == AREAS_DELAY*2/3)
return pruneGuardAreas();
if(areaUpdatingDelay == AREAS_DELAY/3)
return placeGuardAreas();
if(areaUpdatingDelay <= 0)
{
areaUpdatingDelay = AREAS_DELAY;
return farm();
}
//assuming we didn't have to mess with areas or explore flags, check if we can build stuff
if (buildingDelay <= 0)
{
bool shouldBuildMore = percentageOfBuildingsAreFullyWorked(70);
if(verbose)if(shouldBuildMore)std::cout << "AIWarrush is ready to build more stuff!";
//Build another swarm if all are swarms are working at capacity, and if we have other random stuff we should-have / need
if(verbose)std::cout << "Chance to build swarm: ";
if(shouldBuildMore && allOfBuildingTypeAreCompleted(IntBuildingType::SWARM_BUILDING) && numberOfExtraBuildings() >= numberOfBuildingsOfType(IntBuildingType::SWARM_BUILDING) && numberOfBuildingsOfType(IntBuildingType::FOOD_BUILDING) >= numberOfBuildingsOfType(IntBuildingType::SWARM_BUILDING) * 2)
{
if(verbose)std::cout << "TAKEN!\n";
return buildBuildingOfType(IntBuildingType::SWARM_BUILDING);
}
if(verbose)std::cout << "ignored.\n";
//Silly inns. Don't build them right away and don't build too many at a time and don't build too many.
//More limits than it should have, maybe, but the idea of the AI is that it should either win
//or lose warriors (so it shouldn't need so many inns.)
if(verbose)std::cout << "Chance to build inn: ";
if(isAnyUnitWithLessThanOneThirdFood()
&&
shouldBuildMore
&&
numberOfExtraBuildings() >= numberOfBuildingsOfType(IntBuildingType::FOOD_BUILDING) - 1
&&
(
(allOfBuildingTypeAreCompleted(IntBuildingType::FOOD_BUILDING)
&& allOfBuildingTypeAreFull(IntBuildingType::FOOD_BUILDING))
|| allOfBuildingTypeAreFullyWorked(IntBuildingType::FOOD_BUILDING))
)
{
if(verbose)std::cout << "TAKEN!\n";
return buildBuildingOfType(IntBuildingType::FOOD_BUILDING);
}
if(verbose)std::cout << "ignored.\n";
//if the barracks are all working at capacity,
//build more barracks! (this also builds the first barracks...)
if(verbose)std::cout << "Chance to build barracks: ";
if(
allOfBuildingTypeAreCompleted(IntBuildingType::ATTACK_BUILDING)
&& allOfBuildingTypeAreFull(IntBuildingType::ATTACK_BUILDING)
)
{
if(verbose)std::cout << "TAKEN!\n";
return buildBuildingOfType(IntBuildingType::ATTACK_BUILDING);
}
if(verbose)std::cout << "ignored.\n";
//and if we have excess workers, build random other buildings!
if(verbose)std::cout << "Chance to build etc: ";
if(shouldBuildMore)
{
if(verbose)std::cout << "TAKEN!\n";
Sint32 type;
int random_number = syncRand()%100;
if(random_number < 70 || numberOfBuildingsOfType(IntBuildingType::HEAL_BUILDING) == 0)type = IntBuildingType::HEAL_BUILDING;
else if(random_number < 80 || numberOfBuildingsOfType(IntBuildingType::WALKSPEED_BUILDING) == 0)type = IntBuildingType::WALKSPEED_BUILDING;
else if(random_number < 87)type = IntBuildingType::SWIMSPEED_BUILDING;
else if(random_number < 94)type = IntBuildingType::SCIENCE_BUILDING;
else type = IntBuildingType::DEFENSE_BUILDING;
return buildBuildingOfType(type);
}
if(verbose)std::cout << "ignored.\n";
}
//If we have enough workers, we can switch to dedicated warrushing production.
if(numberOfUnitsWithSkillGreaterThanValue(HARVEST,0) >= 6)
{
//This is basically a way to change all the swarms without bothering to remember
//anything. (It can only issue one order per tick, so it has to do it over several
//ticks and calculate the orders seperately.)
Building *out_of_date_swarm = getSwarmWithoutSettings(4,1,3);
if(out_of_date_swarm)
{
Sint32 settings[3] = {4,1,3};
return shared_ptr<Order>(new OrderModifySwarm(out_of_date_swarm->gid, settings));
}
}
//all swarms should always have 5 workers at them!
Building *weak_swarm = getBuildingWithoutWorkersAssigned(IntBuildingType::SWARM_BUILDING, 5);
if (weak_swarm) return shared_ptr<Order>(new OrderModifyBuilding(weak_swarm->gid, 5));
//all inns should always have 3 workers at them! (best to build fast, make sure they're fed)
Building *weak_inn = getBuildingWithoutWorkersAssigned(IntBuildingType::FOOD_BUILDING, 3);
if (weak_inn) return shared_ptr<Order>(new OrderModifyBuilding(weak_inn->gid, 3));
//work barracks more too.
Building *weak_barracks = getBuildingWithoutWorkersAssigned(IntBuildingType::ATTACK_BUILDING, 3);
if (weak_barracks && weak_barracks->constructionResultState != Building::NO_CONSTRUCTION)
return shared_ptr<Order>(new OrderModifyBuilding(weak_barracks->gid, 3));
//nothing at all to do?!
return shared_ptr<Order>(new NullOrder);
}
boost::shared_ptr<Order> AIWarrush::pruneGuardAreas()
{
//If we have any guard areas that aren't adjacent to an enemy building, we remove them.
BrushAccumulator acc;
for(int x=0;x<map->w;x++)
{
for(int y=0;y<map->h;y++)
{
if(map->isGuardArea(x,y,team->me))
{
bool keep = false;
for(int xmod=-1;xmod<=1;xmod++)
{
for(int ymod=-1;ymod<=1;ymod++)
{
//if there's a building...
if(map->getBuilding(x+xmod,y+ymod)!=NOGBID)
{
//...AND it's an enemy building...
if(team->enemies & game->teams[Building::GIDtoTeam(map->getBuilding(x+xmod,y+ymod))]->me)
{
//...then we still want it guarded.
keep=true;
}
}
}
}
if(!keep)
{
acc.applyBrush(BrushApplication(x,y,0), map);
}
}
}
}
if(acc.getApplicationCount())
{
return shared_ptr<Order>(new OrderAlterateGuardArea(team->teamNumber,BrushTool::MODE_DEL,&acc,map));
}
else return shared_ptr<Order>(new NullOrder);
}
boost::shared_ptr<Order> AIWarrush::placeGuardAreas()
{
BrushAccumulator guard_add_acc;
//Place guard area on an enemy building if there is one...
for(int i=0;i<32;i++)
{
Team *t = game->teams[i];
if((t)&&(team->enemies & t->me))
{
for(int j=0;j<1024;j++)
{
Building *b = t->myBuildings[j];
if ((b)&&(b->buildingState != Building::DEAD)&&(b->hp != 1 || b->constructionResultState == Building::NO_CONSTRUCTION)&&(b->shortTypeNum != IntBuildingType::WAR_FLAG)&&(b->shortTypeNum != IntBuildingType::EXPLORATION_FLAG)&&(b->shortTypeNum != IntBuildingType::CLEARING_FLAG))
{
BuildingType *bt = b->type;
if(
//the area must be discovered to prevent AI cheating.
(
map->isFOWDiscovered(b->posX, b->posY, team->me)
|| map->isFOWDiscovered(b->posX+bt->width - 1, b->posY, team->me)
|| map->isFOWDiscovered(b->posX+bt->width - 1, b->posY+bt->height-1, team->me)
|| map->isFOWDiscovered(b->posX, b->posY+bt->height-1, team->me)
)
&&
//do not order a building attacked if the order is already in place.
(
!map->isGuardArea(b->posX,b->posY,team->me)
)
)
{
if((map->getBuilding(b->posX, b->posY)!=NOGBID)&&(team->enemies & game->teams[Building::GIDtoTeam(map->getBuilding(b->posX, b->posY))]->me)) //paranoia
{
for(int x = 0; x < bt->width; x++)
{
for(int y = 0; y < bt->height; y++)
{
guard_add_acc.applyBrush(BrushApplication((b->posX+x) % map->getW(), (b->posY+y) & map->getH(),6), map);
}
}
}
}
}
}
}
}
if(guard_add_acc.getApplicationCount())
{
return shared_ptr<Order>(new OrderAlterateGuardArea(team->teamNumber,BrushTool::MODE_ADD,&guard_add_acc, map));
}
else return shared_ptr<Order>(new NullOrder);
}
boost::shared_ptr<Order> AIWarrush::farm()
{
// Algorithm initially stolen from Nicowar.
DynamicGradientMapArray water_gradient(map->w,map->h);
for(int x=0;x<map->w;x++)
{
for(int y=0;y<map->h;y++)
{
if (map->isWater(x,y))
{
water_gradient(x, y) = 255;
}
else
{
water_gradient(x, y) = 1;
}
}
}
map->updateGlobalGradientSlow(water_gradient.c_array());
BrushAccumulator del_acc;
BrushAccumulator add_acc;
BrushAccumulator clr_del_acc;
BrushAccumulator clr_add_acc;
for(int x=0;x<map->w;x++)
{
for(int y=0;y<map->h;y++)
{
if((!map->isRessourceTakeable(x, y, WOOD) && !map->isRessourceTakeable(x, y, CORN)))
{
if(map->isForbidden(x, y, team->me))
{
if(
//Make sure we're not deleting buildings' forbidden area!
!map->isForbidden (x + 1,y,team->me)
&& !map->isForbidden (x - 1,y,team->me)
&& !map->isForbidden (x,y + 1,team->me)
&& !map->isForbidden (x,y - 1,team->me)
//Or fruits'!
&& !map->isRessourceTakeable(x, y, CHERRY)
&& !map->isRessourceTakeable(x, y, ORANGE)
&& !map->isRessourceTakeable(x, y, PRUNE)
)
{
del_acc.applyBrush(BrushApplication(x, y, 0), map);
}
}
}
if(map->isForbidden(x, y, team->me) && map->isClearArea(x, y, team->me))
{
del_acc.applyBrush(BrushApplication(x, y, 0), map);
}
//we never clear anything but wood
if(!map->isRessourceTakeable(x, y, WOOD))
{
if(map->isClearArea(x, y, team->me))
{
clr_del_acc.applyBrush(BrushApplication(x, y, 0), map);
}
}
//we clear wood if it's next to nice stuff like wheat or buildings
if(map->isRessourceTakeable(x, y, WOOD))
{
if(!map->isClearArea(x, y, team->me) && map->isMapDiscovered(x, y, team->me))
{
for(int xmod=-1;xmod<=1;xmod++)
{
for(int ymod=-1;ymod<=1;ymod++)
{
if(map->isRessourceTakeable(x+xmod, y+ymod, CORN)
|| (map->getBuilding(x+xmod,y+ymod)!=NOGBID
&& (team->me & game->teams[Building::GIDtoTeam(map->getBuilding(x+xmod,y+ymod))]->me)))
{
clr_add_acc.applyBrush(BrushApplication(x, y, 0), map);
goto doublebreak;
}
}
}
doublebreak:
/*statement for label to point to*/;
}
}
if(x%2==1 && ((y%2==1 && x%4==1) || (y%2==0 && x%4==3)))
{
if(map->isRessourceTakeable(x, y, WOOD))
{
if(!map->isForbidden(x, y, team->me) && !map->isClearArea(x, y, team->me) && map->isMapDiscovered(x, y, team->me) && water_gradient(x, y) > (255 - 15))
{
add_acc.applyBrush(BrushApplication(x, y, 0), map);
}
}
}
if(x%2==y%2)
{
if(map->isRessourceTakeable(x, y, CORN))
{
if(!map->isForbidden(x, y, team->me) && map->isMapDiscovered(x, y, team->me) && water_gradient(x, y) > (255 - 15))
{
add_acc.applyBrush(BrushApplication(x, y, 0), map);
}
}
}
//FORBID FRUITS!!! They're horrible for our warriors and we hate converting.
if(
( map->isRessourceTakeable(x, y, CHERRY)
|| map->isRessourceTakeable(x, y, ORANGE)
|| map->isRessourceTakeable(x, y, PRUNE) )
&& !map->isForbidden(x, y, team->me)
&& map->isMapDiscovered(x, y, team->me)
)
{
add_acc.applyBrush(BrushApplication(x, y, 0), map);
}
}
}
if(del_acc.getApplicationCount()>0)
return shared_ptr<Order>(new OrderAlterateForbidden(team->teamNumber, BrushTool::MODE_DEL, &del_acc, map));
if(add_acc.getApplicationCount()>0)
return shared_ptr<Order>(new OrderAlterateForbidden(team->teamNumber, BrushTool::MODE_ADD, &add_acc, map));
if(clr_del_acc.getApplicationCount()>0)
return shared_ptr<Order>(new OrderAlterateClearArea(team->teamNumber, BrushTool::MODE_DEL, &clr_del_acc, map));
if(clr_add_acc.getApplicationCount()>0)
return shared_ptr<Order>(new OrderAlterateClearArea(team->teamNumber, BrushTool::MODE_ADD, &clr_add_acc, map));
//nothing to do...
return shared_ptr<Order>(new NullOrder());
}
//Simple hack to place explore flags on opponents' starting swarms.
boost::shared_ptr<Order> AIWarrush::setupExploreFlagForTeam(Team *enemy_team)
{
if(verbose)std::cout << "looking for swarms:\n";
for(int j=0;j<1024;j++)
{
Building *b = enemy_team->myBuildings[j];
if((b)&&(b->type->shortTypeNum == IntBuildingType::SWARM_BUILDING)&&(b->constructionResultState == Building::NO_CONSTRUCTION))
{
Sint32 typeNum=globalContainer->buildingsTypes.getTypeNum("explorationflag", 0, false);
return shared_ptr<Order>(new OrderCreate(team->teamNumber, b->posX, b->posY, typeNum, 1, 1));
}
}
if(verbose)std::cout << "No swarms found\n";
//what, they have no swarm? o_O Find any building:
for(int j=0;j<1024;j++)
{
Building *b = enemy_team->myBuildings[j];
if(b)
{
Sint32 typeNum=globalContainer->buildingsTypes.getTypeNum("explorationflag", 0, false);
return shared_ptr<Order>(new OrderCreate(team->teamNumber, b->posX, b->posY, typeNum, 1, 1));
}
}
if(verbose)std::cout << "No buildings found\n";
//what, they have no buildings? o_O Find any unit:
for(int j=0;j<1024;j++)
{
Unit *u = enemy_team->myUnits[j];
if(u)
{
Sint32 typeNum=globalContainer->buildingsTypes.getTypeNum("explorationflag", 0, false);
return shared_ptr<Order>(new OrderCreate(team->teamNumber, u->posX, u->posY, typeNum, 1, 1));
}
}
//what, enemy has no buildings or units at the beginning of the game? o_O O_o o_O
if(verbose)std::cout << "No buildings found, no units found o_O\n";
return shared_ptr<Order>(new NullOrder);
}
bool AIWarrush::locationIsAvailableForBuilding(int x, int y, int width, int height)
{
/*if(map->isHardSpaceForBuilding(x,y,width,height))
{*/
if( map->isMapDiscovered(x, y, team->me)
|| map->isMapDiscovered(x+width-1, y, team->me)
|| map->isMapDiscovered(x+width-1, y+height-1, team->me)
|| map->isMapDiscovered(x, y+height-1, team->me)
)
{
return true;
}
/*}*/
return false;
}
void AIWarrush::initializeGradientWithResource(DynamicGradientMapArray &gradient, Uint8 resource_type)
{
for(int x=0;x<map->w;x++)
{
for(int y=0;y<map->h;y++)
{
Case c=map->getCase(x,y);
if (c.ressource.type==resource_type)
{
gradient(x, y) = 255;
}
else if (c.ressource.type!=NO_RES_TYPE)
{
gradient(x, y) = 0;
}
else if (c.building!=NOGBID)
{
gradient(x, y) = 0;
}
else if (map->isWater(x,y))
{
gradient(x, y) = 0;
}
else
{ //has to be desert or grass with no buildings or resources, at this point.
gradient(x, y) = 1;
}
}
}
map->updateGlobalGradientSlow(gradient.c_array());
for(int x=0;x<map->w;x++)
{
for(int y=0;y<map->h;y++)
{
if (gradient(x, y) == 255)
gradient(x, y) = 0;
else
gradient(x, y)++;
}
}
}
boost::shared_ptr<Order> AIWarrush::buildBuildingOfType(Sint32 shortTypeNum)
{
// set delay
// now doing this first in order to avoid repeated failed builds
// WARNING THIS IS A HACK FIX
// in reality, if it fails to build, it should go on and get another order.
buildingDelay = BUILDING_DELAY;
DynamicGradientMapArray wood_gradient(map->w,map->h);
DynamicGradientMapArray wheat_gradient(map->w,map->h);
initializeGradientWithResource(wood_gradient, WOOD);
initializeGradientWithResource(wheat_gradient, CORN);
BuildingType *bt=globalContainer->buildingsTypes.getByType(IntBuildingType::typeFromShortNumber(shortTypeNum), 0, true);
DynamicGradientMapArray availability_gradient(map->w,map->h);
for(int x=0;x<map->w;x++)
{
for(int y=0;y<map->h;y++)
{
Case c=map->getCase(x,y);
if (c.ressource.type!=NO_RES_TYPE)
{
availability_gradient(x, y) = 0;
}
else if (c.building!=NOGBID)
{
availability_gradient(x, y) = 0;
}
else if (map->isWater(x,y))
{
availability_gradient(x, y) = 0;
}
else if(map->isHardSpaceForBuilding(x-(bt->width / 2),y-(bt->width / 2),bt->width*2,bt->height*2) && locationIsAvailableForBuilding(x,y,bt->width,bt->height)) //the extra numbers at the ends expand the building
{
availability_gradient(x, y) = 255;
}
else availability_gradient(x, y) = 1;
}
}
map->updateGlobalGradientSlow(availability_gradient.c_array());
Building *swarm = getSwarmAtRandom();
if (!swarm)
{
if(verbose)std::cout << "No swarm found!\n";
return shared_ptr<Order>(new NullOrder);
}
Sint32 destination_x,destination_y;
{
Sint32 x,y;
Sint32 x_temp,y_temp;
//map->dumpGradient(gradient.c_array(), "gradient.pgm");
//map->dumpGradient(availability_gradient.c_array(), "availability_gradient.pgm");
x = swarm->posX; y = swarm->posY;
if(shortTypeNum==IntBuildingType::ATTACK_BUILDING || shortTypeNum==IntBuildingType::HEAL_BUILDING || shortTypeNum==IntBuildingType::SCIENCE_BUILDING || shortTypeNum==IntBuildingType::WALKSPEED_BUILDING || shortTypeNum==IntBuildingType::SWIMSPEED_BUILDING || shortTypeNum==IntBuildingType::DEFENSE_BUILDING)
{
map->getGlobalGradientDestination(wood_gradient.c_array(), x, y, &x_temp, &y_temp);
x = x_temp; y = y_temp;
}
if(shortTypeNum==IntBuildingType::SWARM_BUILDING || shortTypeNum==IntBuildingType::FOOD_BUILDING)
{
map->getGlobalGradientDestination(wheat_gradient.c_array(), x, y, &x_temp, &y_temp);
x = x_temp; y = y_temp;
}
bool result = map->getGlobalGradientDestination(availability_gradient.c_array(), x, y, &destination_x, &destination_y);
if(verbose)std::cout << "Trying to build " << shortTypeNum << "(" << bt->width << " x " << bt->height << ")" << " at " << destination_x << "," << destination_y << " from " << x << "," << y << " swarm is " << swarm->posX << "," << swarm->posY << ", found = " << result << std::endl;
if(verbose)if((int)availability_gradient(destination_x, destination_y) != 255)std::cout << "Could not find valid location for building! Best spot: " << destination_x << "," << destination_y << " (" << (int)availability_gradient(destination_x, destination_y) << ")\n";
}
// create and return order
Sint32 typeNum=globalContainer->buildingsTypes.getTypeNum(IntBuildingType::typeFromShortNumber(shortTypeNum), 0, true);
return shared_ptr<Order>(new OrderCreate(team->teamNumber, destination_x, destination_y, typeNum, 1, 1));
}
|