From e58ddd35b41ae2d689dc87311fb0dcf40c378753 Mon Sep 17 00:00:00 2001 From: Titus Tscharntke Date: Mon, 20 Jun 2011 23:51:13 +0000 Subject: [PATCH] fixes build bug reported in forum/ adds some better mega-cpu behaviour / fixed some bugs ( I hopefully did not added new ones :-D ) --- source/glest_game/ai/ai.cpp | 76 +- source/glest_game/ai/ai_interface.h | 2 +- source/glest_game/ai/ai_rule.cpp | 1109 +++++++++++++--------- source/glest_game/ai/ai_rule.h | 1 + source/glest_game/world/unit_updater.cpp | 4 +- 5 files changed, 686 insertions(+), 506 deletions(-) diff --git a/source/glest_game/ai/ai.cpp b/source/glest_game/ai/ai.cpp index 096522b9..52c79cf1 100644 --- a/source/glest_game/ai/ai.cpp +++ b/source/glest_game/ai/ai.cpp @@ -451,13 +451,10 @@ void Ai::massiveAttack(const Vec2i &pos, Field field, bool ultraAttack){ int attackerWorkersHarvestingCount = 0; for(int i = 0; i < unitCount; ++i) { bool isWarrior=false; + bool productionInProgress=false; const Unit *unit= aiInterface->getMyUnit(i); const AttackCommandType *act= unit->getType()->getFirstAttackCommand(field); - if(act != NULL && unit->getType()->hasCommandClass(ccProduce)) { - producerWarriorCount++; - } - if( aiInterface->getControlType() == ctCpuMega || aiInterface->getControlType() == ctNetworkCpuMega) { if(producerWarriorCount > maxProducerWarriors) { @@ -469,7 +466,9 @@ void Ai::massiveAttack(const Vec2i &pos, Field field, bool ultraAttack){ unit->getCurrCommand()->getCommandType()->getClass()==ccProduce ) ) { + productionInProgress=true; isWarrior=false; + producerWarriorCount++; } else { isWarrior =! unit->getType()->hasCommandClass(ccHarvest); @@ -487,49 +486,48 @@ void Ai::massiveAttack(const Vec2i &pos, Field field, bool ultraAttack){ bool alreadyAttacking= (unit->getCurrSkill()->getClass() == scAttack); bool unitSignalledToAttack = false; - if( alreadyAttacking == false && - unit->getType()->hasSkillClass(scAttack) && - (aiInterface->getControlType() == ctCpuUltra || - aiInterface->getControlType() == ctCpuMega || - aiInterface->getControlType() == ctNetworkCpuUltra || - aiInterface->getControlType() == ctNetworkCpuMega)) { + if(alreadyAttacking == false && unit->getType()->hasSkillClass(scAttack) && (aiInterface->getControlType() + == ctCpuUltra || aiInterface->getControlType() == ctCpuMega || aiInterface->getControlType() + == ctNetworkCpuUltra || aiInterface->getControlType() == ctNetworkCpuMega)){ //printf("~~~~~~~~ Unit [%s - %d] checking if unit is being attacked\n",unit->getFullName().c_str(),unit->getId()); - std::pair beingAttacked = aiInterface->getWorld()->getUnitUpdater()->unitBeingAttacked(unit); - if(beingAttacked.first == true) { - Unit *enemy = beingAttacked.second; - const AttackCommandType *act_forenemy = unit->getType()->getFirstAttackCommand(enemy->getCurrField()); - //printf("~~~~~~~~ Unit [%s - %d] found enemy [%s - %d] act_forenemy [%p] enemy->getCurrField() = %d\n",unit->getFullName().c_str(),unit->getId(),enemy->getFullName().c_str(),enemy->getId(),act_forenemy,enemy->getCurrField()); + std::pair beingAttacked= aiInterface->getWorld()->getUnitUpdater()->unitBeingAttacked(unit); + if(beingAttacked.first == true){ + Unit *enemy= beingAttacked.second; + const AttackCommandType *act_forenemy= unit->getType()->getFirstAttackCommand(enemy->getCurrField()); - if(act_forenemy != NULL) { - bool shouldAttack = true; - if(unit->getType()->hasSkillClass(scHarvest)) { - shouldAttack = (attackerWorkersHarvestingCount > minWorkerAttackersHarvesting); - if(shouldAttack == false) { + //printf("~~~~~~~~ Unit [%s - %d] attacked by enemy [%s - %d] act_forenemy [%p] enemy->getCurrField() = %d\n",unit->getFullName().c_str(),unit->getId(),enemy->getFullName().c_str(),enemy->getId(),act_forenemy,enemy->getCurrField()); + + if(act_forenemy != NULL){ + bool shouldAttack= true; + if(unit->getType()->hasSkillClass(scHarvest)){ + shouldAttack= (attackerWorkersHarvestingCount > minWorkerAttackersHarvesting); + if(shouldAttack == false){ attackerWorkersHarvestingCount++; } } - if(shouldAttack) { - //printf("~~~~~~~~ Unit [%s - %d] WILL ATTACK [%s - %d]\n",unit->getFullName().c_str(),unit->getId(),enemy->getFullName().c_str(),enemy->getId()); + if(shouldAttack){ aiInterface->giveCommand(i, act_forenemy, beingAttacked.second->getPos()); - unitSignalledToAttack = true; + unitSignalledToAttack= true; } } - else { - const AttackStoppedCommandType *asct_forenemy = unit->getType()->getFirstAttackStoppedCommand(enemy->getCurrField()); + else{ + const AttackStoppedCommandType *asct_forenemy= unit->getType()->getFirstAttackStoppedCommand( + enemy->getCurrField()); //printf("~~~~~~~~ Unit [%s - %d] found enemy [%s - %d] asct_forenemy [%p] enemy->getCurrField() = %d\n",unit->getFullName().c_str(),unit->getId(),enemy->getFullName().c_str(),enemy->getId(),asct_forenemy,enemy->getCurrField()); - if(asct_forenemy != NULL) { - bool shouldAttack = true; - if(unit->getType()->hasSkillClass(scHarvest)) { - shouldAttack = (attackerWorkersHarvestingCount > minWorkerAttackersHarvesting); - if(shouldAttack == false) { + if(asct_forenemy != NULL){ + bool shouldAttack= true; + if(unit->getType()->hasSkillClass(scHarvest)){ + shouldAttack= (attackerWorkersHarvestingCount > minWorkerAttackersHarvesting); + if(shouldAttack == false){ attackerWorkersHarvestingCount++; } } - if(shouldAttack) { - //printf("~~~~~~~~ Unit [%s - %d] WILL ATTACK [%s - %d]\n",unit->getFullName().c_str(),unit->getId(),enemy->getFullName().c_str(),enemy->getId()); + if(shouldAttack){ +// printf("~~~~~~~~ Unit [%s - %d] WILL AttackStoppedCommand [%s - %d]\n", unit->getFullName().c_str(), +// unit->getId(), enemy->getFullName().c_str(), enemy->getId()); aiInterface->giveCommand(i, asct_forenemy, beingAttacked.second->getCenteredPos()); - unitSignalledToAttack = true; + unitSignalledToAttack= true; } } } @@ -544,7 +542,17 @@ void Ai::massiveAttack(const Vec2i &pos, Field field, bool ultraAttack){ attackerWorkersHarvestingCount++; } } - if(shouldAttack) { + + // Mega CPU does not send ( far away ) units which are currently producing something + if(aiInterface->getControlType() == ctCpuMega || aiInterface->getControlType() == ctNetworkCpuMega){ + if(!isWarrior ){ + if(!productionInProgress){ + shouldAttack= false; + //printf("no attack \n "); + } + } + } + if(shouldAttack){ aiInterface->giveCommand(i, act, pos); } } diff --git a/source/glest_game/ai/ai_interface.h b/source/glest_game/ai/ai_interface.h index 556e3213..b92b1240 100644 --- a/source/glest_game/ai/ai_interface.h +++ b/source/glest_game/ai/ai_interface.h @@ -67,7 +67,7 @@ public: CommandResult giveCommand(int unitIndex, CommandClass commandClass, const Vec2i &pos=Vec2i(0)); CommandResult giveCommand(int unitIndex, const CommandType *commandType, const Vec2i &pos, const UnitType* unitType); CommandResult giveCommand(int unitIndex, const CommandType *commandType, const Vec2i &pos); - CommandResult giveCommand(int unitIndex, const CommandType *commandType, Unit *u= NULL); + CommandResult giveCommand(int unitIndex, const CommandType *commandType, Unit *u= NULL); CommandResult giveCommand(const Unit *unit, const CommandType *commandType, const Vec2i &pos); //get data diff --git a/source/glest_game/ai/ai_rule.cpp b/source/glest_game/ai/ai_rule.cpp index 2e228003..92ede7cd 100644 --- a/source/glest_game/ai/ai_rule.cpp +++ b/source/glest_game/ai/ai_rule.cpp @@ -20,7 +20,8 @@ using Shared::Graphics::Vec2i; -namespace Glest{ namespace Game{ +namespace Glest { +namespace Game { // ===================================================== // class AiRule @@ -34,9 +35,8 @@ AiRule::AiRule(Ai *ai){ // class AiRuleWorkerHarvest // ===================================================== -AiRuleWorkerHarvest::AiRuleWorkerHarvest(Ai *ai): - AiRule(ai) -{ +AiRuleWorkerHarvest::AiRuleWorkerHarvest(Ai *ai) : + AiRule(ai){ stoppedWorkerIndex= -1; } @@ -52,9 +52,8 @@ void AiRuleWorkerHarvest::execute(){ // class AiRuleRefreshHarvester // ===================================================== -AiRuleRefreshHarvester::AiRuleRefreshHarvester(Ai *ai): - AiRule(ai) -{ +AiRuleRefreshHarvester::AiRuleRefreshHarvester(Ai *ai) : + AiRule(ai){ workerIndex= -1; } @@ -70,9 +69,8 @@ void AiRuleRefreshHarvester::execute(){ // class AiRuleScoutPatrol // ===================================================== -AiRuleScoutPatrol::AiRuleScoutPatrol(Ai *ai): - AiRule(ai) -{ +AiRuleScoutPatrol::AiRuleScoutPatrol(Ai *ai) : + AiRule(ai){ } bool AiRuleScoutPatrol::test(){ @@ -86,25 +84,27 @@ void AiRuleScoutPatrol::execute(){ // class AiRuleRepair // ===================================================== -AiRuleRepair::AiRuleRepair(Ai *ai): - AiRule(ai) -{ +AiRuleRepair::AiRuleRepair(Ai *ai) : + AiRule(ai){ } -double AiRuleRepair::getMinCastleHpRatio() { +double AiRuleRepair::getMinCastleHpRatio(){ return 0.6; } -int AiRuleRepair::getMinUnitsToRepairCastle() { - int minUnitsRepairingCastle = 7; - if(ai->getCountOfClass(ucWorker) <= 4) { - minUnitsRepairingCastle = 1; +int AiRuleRepair::getMinUnitsToRepairCastle(){ + int minUnitsRepairingCastle= 7; + if(ai->getCountOfClass(ucWorker) <= 6){ + minUnitsRepairingCastle= 1; } - else if(ai->getCountOfClass(ucWorker) <= 7) { - minUnitsRepairingCastle = 2; + else if(ai->getCountOfClass(ucWorker) <= 8){ + minUnitsRepairingCastle= 2; } - else if(ai->getCountOfClass(ucWorker) <= 10) { - minUnitsRepairingCastle = 5; + else if(ai->getCountOfClass(ucWorker) <= 10){ + minUnitsRepairingCastle= 3; + } + else if(ai->getCountOfClass(ucWorker) <= 12){ + minUnitsRepairingCastle= 5; } return minUnitsRepairingCastle; } @@ -112,59 +112,59 @@ int AiRuleRepair::getMinUnitsToRepairCastle() { bool AiRuleRepair::test(){ AiInterface *aiInterface= ai->getAiInterface(); - int minUnitsRepairingCastle = getMinUnitsToRepairCastle(); - const double minCastleHpRatio = getMinCastleHpRatio(); + int minUnitsRepairingCastle= getMinUnitsToRepairCastle(); + const double minCastleHpRatio= getMinCastleHpRatio(); // look for a damaged unit and give priority to the factions bases // (units that produce workers and store resources) - for(int i = 0; i < aiInterface->getMyUnitCount(); ++i) { + for(int i= 0; i < aiInterface->getMyUnitCount(); ++i){ const Unit *u= aiInterface->getMyUnit(i); //printf("\n\n\n\n!!!!!! Is damaged unit [%d - %s] u->getHpRatio() = %f, hp = %d, mapHp = %d\n",u->getId(),u->getType()->getName().c_str(),u->getHpRatio(),u->getHp(),u->getType()->getTotalMaxHp(u->getTotalUpgrade())); - if(u->getHpRatio() < 1.f) { + if(u->getHpRatio() < 1.f){ //printf("\n\n\n\n!!!!!! Is damaged unit [%d - %s] u->getHpRatio() = %f, hp = %d, mapHp = %d\n",u->getId(),u->getType()->getName().c_str(),u->getHpRatio(),u->getHp(),u->getType()->getTotalMaxHp(u->getTotalUpgrade())); - bool unitCanProduceWorker = false; - for(int j = 0; unitCanProduceWorker == false && - j < u->getType()->getCommandTypeCount(); ++j) { + bool unitCanProduceWorker= false; + for(int j= 0; unitCanProduceWorker == false && j < u->getType()->getCommandTypeCount(); ++j){ const CommandType *ct= u->getType()->getCommandType(j); //if the command is produce - if(ct->getClass() == ccProduce || ct->getClass() == ccMorph) { - const ProducibleType *pt = ct->getProduced(); - if(pt != NULL) { - const UnitType *ut = dynamic_cast(pt); - if( ut != NULL && ut->hasCommandClass(ccHarvest) == true && - u->getType()->getStoredResourceCount() > 0) { + if(ct->getClass() == ccProduce || ct->getClass() == ccMorph){ + const ProducibleType *pt= ct->getProduced(); + if(pt != NULL){ + const UnitType *ut= dynamic_cast (pt); + if(ut != NULL && ut->hasCommandClass(ccHarvest) == true + && u->getType()->getStoredResourceCount() > 0){ //printf("\n\n\n\n!!!!!! found candidate castle unit to repair [%d - %s]\n",u->getId(),u->getType()->getName().c_str()); - unitCanProduceWorker = true; + unitCanProduceWorker= true; } } } } - int candidatedamagedUnitIndex=-1; - if(unitCanProduceWorker == true) { - int unitCountAlreadyRepairingDamagedUnit = 0; + int candidatedamagedUnitIndex= -1; + if(unitCanProduceWorker == true){ + int unitCountAlreadyRepairingDamagedUnit= 0; // Now check if any other unit is able to repair this unit - for(int i1 = 0; i1 < aiInterface->getMyUnitCount(); ++i1) { + for(int i1= 0; i1 < aiInterface->getMyUnitCount(); ++i1){ const Unit *u1= aiInterface->getMyUnit(i1); - const RepairCommandType *rct= static_cast(u1->getType()->getFirstCtOfClass(ccRepair)); + const RepairCommandType *rct= + static_cast (u1->getType()->getFirstCtOfClass(ccRepair)); //if(rct) printf("\n\n\n\n^^^^^^^^^^ possible repairer unit [%d - %s] current skill [%d] can reapir damaged unit [%d]\n",u1->getId(),u1->getType()->getName().c_str(),u->getCurrSkill()->getClass(),rct->isRepairableUnitType(u->getType())); - if(rct != NULL) { + if(rct != NULL){ //printf("\n\n\n\n^^^^^^^^^^ possible repairer unit [%d - %s] current skill [%d] can repair damaged unit [%d] Castles hp-ratio = %f\n",u1->getId(),u1->getType()->getName().c_str(),u1->getCurrSkill()->getClass(),rct->isRepairableUnitType(u->getType()),u->getHpRatio()); - if(u1->getCurrSkill()->getClass() == scStop || u1->getCurrSkill()->getClass() == scMove || - u->getHpRatio() <= minCastleHpRatio) { - if(rct->isRepairableUnitType(u->getType())) { + if(u1->getCurrSkill()->getClass() == scStop || u1->getCurrSkill()->getClass() == scMove + || u->getHpRatio() <= minCastleHpRatio){ + if(rct->isRepairableUnitType(u->getType())){ candidatedamagedUnitIndex= i; //return true; } } - else if(u1->getCurrSkill()->getClass() == scRepair) { - Command *cmd = u1->getCurrCommand(); - if(cmd != NULL && cmd->getCommandType()->getClass() == ccRepair) { - if(cmd->getUnit() != NULL && cmd->getUnit()->getId() == u->getId()) { + else if(u1->getCurrSkill()->getClass() == scRepair){ + Command *cmd= u1->getCurrCommand(); + if(cmd != NULL && cmd->getCommandType()->getClass() == ccRepair){ + if(cmd->getUnit() != NULL && cmd->getUnit()->getId() == u->getId()){ //printf("\n\n\n\n^^^^^^^^^^ unit is ALREADY repairer unit [%d - %s]\n",u1->getId(),u1->getType()->getName().c_str()); unitCountAlreadyRepairingDamagedUnit++; } @@ -173,103 +173,149 @@ bool AiRuleRepair::test(){ } } - if(candidatedamagedUnitIndex >= 0 && unitCountAlreadyRepairingDamagedUnit < minUnitsRepairingCastle) { + if(candidatedamagedUnitIndex >= 0 && unitCountAlreadyRepairingDamagedUnit < minUnitsRepairingCastle){ //printf("\n\n\n\n^^^^^^^^^^ AI test will repair damaged unit [%d - %s]\n",u->getId(),u->getType()->getName().c_str()); - damagedUnitIndex = candidatedamagedUnitIndex; + damagedUnitIndex= candidatedamagedUnitIndex; + damagedUnitIsCastle=true; return true; } } } } - + damagedUnitIsCastle=false; + damagedUnitIndex=-1; // Normal Repair checking - for(int i = 0; i < aiInterface->getMyUnitCount(); ++i) { + for(int i= 0; i < aiInterface->getMyUnitCount(); ++i){ const Unit *u= aiInterface->getMyUnit(i); //printf("\n\n\n\n!!!!!! Is damaged unit [%d - %s] u->getHpRatio() = %f, hp = %d, mapHp = %d\n",u->getId(),u->getType()->getName().c_str(),u->getHpRatio(),u->getHp(),u->getType()->getTotalMaxHp(u->getTotalUpgrade())); - if(u->getHpRatio() < 1.f) { + if(u->getHpRatio() < 1.f){ // Now check if any other unit is able to repair this unit - for(int i1 = 0; i1 < aiInterface->getMyUnitCount(); ++i1) { + for(int i1= 0; i1 < aiInterface->getMyUnitCount(); ++i1){ const Unit *u1= aiInterface->getMyUnit(i1); - const RepairCommandType *rct= static_cast(u1->getType()->getFirstCtOfClass(ccRepair)); + const RepairCommandType *rct= static_cast (u1->getType()->getFirstCtOfClass( + ccRepair)); //if(rct) printf("\n\n\n\n^^^^^^^^^^ possible repairer unit [%d - %s] current skill [%d] can reapir damaged unit [%d]\n",u1->getId(),u1->getType()->getName().c_str(),u->getCurrSkill()->getClass(),rct->isRepairableUnitType(u->getType())); - if(rct != NULL && (u1->getCurrSkill()->getClass() == scStop || u1->getCurrSkill()->getClass() == scMove)) { - if(rct->isRepairableUnitType(u->getType())) { + if(rct != NULL + && (u1->getCurrSkill()->getClass() == scStop || u1->getCurrSkill()->getClass() == scMove)){ + if(rct->isRepairableUnitType(u->getType())){ damagedUnitIndex= i; + + //random if return or not so we get different targets from time to time + if(ai->getRandom()->randRange(0, 1) == 0) return true; } } } } } + if( damagedUnitIndex!=-1) + { + return true; + } return false; } -void AiRuleRepair::execute() { +void AiRuleRepair::execute(){ AiInterface *aiInterface= ai->getAiInterface(); const Unit *damagedUnit= aiInterface->getMyUnit(damagedUnitIndex); //printf("\n\n\n\n###^^^^^^^^^^ Looking for repairer for damaged unit [%d - %s]\n",damagedUnit->getId(),damagedUnit->getType()->getName().c_str()); - int minUnitsRepairingCastle = getMinUnitsToRepairCastle(); - const double minCastleHpRatio = getMinCastleHpRatio(); + int minUnitsRepairingCastle= getMinUnitsToRepairCastle(); + const double minCastleHpRatio= getMinCastleHpRatio(); - int unitCountAlreadyRepairingDamagedUnit = 0; + if(minUnitsRepairingCastle > 2){ + if(damagedUnit->getCurrSkill()->getClass() == scBeBuilt){// if build is still be build 2 helpers are enough + minUnitsRepairingCastle= 2; + } + + if(!damagedUnitIsCastle){ + minUnitsRepairingCastle= 2; + } + } + if( aiInterface->getControlType() == ctCpuEasy || + aiInterface->getControlType() == ctNetworkCpuEasy) { + if(!damagedUnitIsCastle){ + // cpu easy does not repair! + minUnitsRepairingCastle=0; + } + } + if( aiInterface->getControlType() == ctCpu || + aiInterface->getControlType() == ctNetworkCpu) { + if(!damagedUnitIsCastle){ + // cpu does only repair with one unit! + minUnitsRepairingCastle=1; + } + } + int unitCountAlreadyRepairingDamagedUnit= 0; + //printf("team %d has damaged unit\n", damagedUnit->getTeam()); // Now check if any other unit is able to repair this unit - for(int i1 = 0; i1 < aiInterface->getMyUnitCount(); ++i1) { + for(int i1= 0; i1 < aiInterface->getMyUnitCount(); ++i1){ const Unit *u1= aiInterface->getMyUnit(i1); - const RepairCommandType *rct= static_cast(u1->getType()->getFirstCtOfClass(ccRepair)); - //if(rct) printf("\n\n\n\n^^^^^^^^^^ possible repairer unit [%d - %s] current skill [%d] can reapir damaged unit [%d]\n",u1->getId(),u1->getType()->getName().c_str(),u->getCurrSkill()->getClass(),rct->isRepairableUnitType(u->getType())); + const RepairCommandType *rct= + static_cast (u1->getType()->getFirstCtOfClass(ccRepair)); + //if(rct) printf("\n\n\n\n^^^^^^^^^^ possible repairer unit [%d - %s] current skill [%d] can reapir damaged unit [%d]\n",u1->getId(),u1->getType()->getName().c_str(),u1->getCurrSkill()->getClass(),rct->isRepairableUnitType(u1->getType())); - if(rct != NULL) { + if(rct != NULL){ //printf("\n\n\n\n^^^^^^^^^^ possible repairer unit [%d - %s] current skill [%d] can repair damaged unit [%d] Castles hp-ratio = %f\n",u1->getId(),u1->getType()->getName().c_str(),u1->getCurrSkill()->getClass(),rct->isRepairableUnitType(u->getType()),u->getHpRatio()); + Command *cmd= u1->getCurrCommand(); + if(cmd != NULL && cmd->getCommandType()->getClass() == ccRepair){ + //if(cmd->getUnit() != NULL && cmd->getUnit()->getId() == damagedUnit->getId()) + //if(cmd->getUnit() != NULL && cmd->getPos() == damagedUnit->getPosWithCellMapSet()) - if(u1->getCurrSkill()->getClass() == scRepair) { - Command *cmd = u1->getCurrCommand(); - if(cmd != NULL && cmd->getCommandType()->getClass() == ccRepair) { - if(cmd->getUnit() != NULL && cmd->getUnit()->getId() == damagedUnit->getId()) { - //printf("\n\n\n\n^^^^^^^^^^ unit is ALREADY repairer unit [%d - %s]\n",u1->getId(),u1->getType()->getName().c_str()); - unitCountAlreadyRepairingDamagedUnit++; - } + if(((RepairCommandType*)(cmd->getCommandType()))->isRepairableUnitType(damagedUnit->getType())){ + //printf("^^^^test^^^^^^ unit is ALREADY repairer unit [%d - %s]\nminUnitsRepairingCastle=%d\n",u1->getId(), u1->getType()->getName().c_str(), minUnitsRepairingCastle); + unitCountAlreadyRepairingDamagedUnit++; } } } } + if(unitCountAlreadyRepairingDamagedUnit >= minUnitsRepairingCastle){ + return; + } //find a repairer and issue command - for(int i = 0; i < aiInterface->getMyUnitCount(); ++i) { + for(int i= 0; i < aiInterface->getMyUnitCount(); ++i){ const Unit *u= aiInterface->getMyUnit(i); - const RepairCommandType *rct= static_cast(u->getType()->getFirstCtOfClass(ccRepair)); + const RepairCommandType *rct= + static_cast (u->getType()->getFirstCtOfClass(ccRepair)); //if(rct) printf("\n\n\n\n^^^^^^^^^^ possible repairer unit [%d - %s] current skill [%d] can reapir damaged unit [%d]\n",u->getId(),u->getType()->getName().c_str(),u->getCurrSkill()->getClass(),rct->isRepairableUnitType(damagedUnit->getType())); - if(rct != NULL) { + if(rct != NULL){ //printf("\n\n\n\n^^^^^^^^^^ possible excute repairer unit [%d - %s] current skill [%d] can repair damaged unit [%d] Castles hp-ratio = %f\n",u->getId(),u->getType()->getName().c_str(),u->getCurrSkill()->getClass(),rct->isRepairableUnitType(damagedUnit->getType()),damagedUnit->getHpRatio()); - if((u->getCurrSkill()->getClass() == scStop || u->getCurrSkill()->getClass() == scMove || damagedUnit->getHpRatio() <= minCastleHpRatio)) { - if(rct->isRepairableUnitType(damagedUnit->getType())) { + if((u->getCurrSkill()->getClass() == scStop || u->getCurrSkill()->getClass() == scMove + || damagedUnit->getHpRatio() <= minCastleHpRatio)){ + if((u->getCurrCommand() == NULL || (u->getCurrCommand()->getCommandType()->getClass() != ccBuild + && u->getCurrCommand()->getCommandType()->getClass() != ccProduce)) + && rct->isRepairableUnitType(damagedUnit->getType())){ //if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); //printf("\n\n\n\n^^^^^^^^^^ AI execute will repair damaged unit [%d - %s] at pos [%s] cellmapPos [%s] using unit [%d -%s]\n",damagedUnit->getId(),damagedUnit->getType()->getName().c_str(),damagedUnit->getPos().getString().c_str(),damagedUnit->getPosWithCellMapSet().getString().c_str(),u->getId(),u->getType()->getName().c_str()); /* - Map *map= aiInterface->getWorld()->getMap(); - Cell *cell = map->getCell(damagedUnit->getPosWithCellMapSet()); - if(cell != NULL) { - printf("\n\n\n\n^^^^^^^^^^ cell is ok\n"); + Map *map= aiInterface->getWorld()->getMap(); + Cell *cell = map->getCell(damagedUnit->getPosWithCellMapSet()); + if(cell != NULL) { + printf("\n\n\n\n^^^^^^^^^^ cell is ok\n"); - Unit *cellUnit = cell->getUnit(damagedUnit->getCurrField()); - if(cellUnit != NULL) { - printf("\n\n\n\n^^^^^^^^^^ cell unit [%d - %s] at pos [%s]\n",cellUnit->getId(),cellUnit->getType()->getName().c_str(),cellUnit->getPos().getString().c_str()); - } - } - */ + Unit *cellUnit = cell->getUnit(damagedUnit->getCurrField()); + if(cellUnit != NULL) { + printf("\n\n\n\n^^^^^^^^^^ cell unit [%d - %s] at pos [%s]\n",cellUnit->getId(),cellUnit->getType()->getName().c_str(),cellUnit->getPos().getString().c_str()); + } + } + */ //aiInterface->giveCommand(i, rct, damagedUnit->getPos()); - if(unitCountAlreadyRepairingDamagedUnit < minUnitsRepairingCastle) { + if(unitCountAlreadyRepairingDamagedUnit < minUnitsRepairingCastle){ aiInterface->giveCommand(i, rct, damagedUnit->getPosWithCellMapSet()); aiInterface->printLog(3, "Repairing order issued"); unitCountAlreadyRepairingDamagedUnit++; +// printf( +// "^^^^^^^^^^adding one unit to repair ... unitCountAlreadyRepairingDamagedUnit/minUnitsRepairingCastle=%d/%d\n", +// unitCountAlreadyRepairingDamagedUnit, minUnitsRepairingCastle); } - if(unitCountAlreadyRepairingDamagedUnit >= minUnitsRepairingCastle) { + if( !damagedUnitIsCastle || unitCountAlreadyRepairingDamagedUnit >= minUnitsRepairingCastle){ return; } } @@ -282,9 +328,8 @@ void AiRuleRepair::execute() { // class AiRuleReturnBase // ===================================================== -AiRuleReturnBase::AiRuleReturnBase(Ai *ai): - AiRule(ai) -{ +AiRuleReturnBase::AiRuleReturnBase(Ai *ai) : + AiRule(ai){ stoppedUnitIndex= -1; } @@ -300,9 +345,8 @@ void AiRuleReturnBase::execute(){ // class AiRuleMassiveAttack // ===================================================== -AiRuleMassiveAttack::AiRuleMassiveAttack(Ai *ai): - AiRule(ai) -{ +AiRuleMassiveAttack::AiRuleMassiveAttack(Ai *ai) : + AiRule(ai){ } bool AiRuleMassiveAttack::test(){ @@ -324,9 +368,8 @@ void AiRuleMassiveAttack::execute(){ // class AiRuleAddTasks // ===================================================== -AiRuleAddTasks::AiRuleAddTasks(Ai *ai): - AiRule(ai) -{ +AiRuleAddTasks::AiRuleAddTasks(Ai *ai) : + AiRule(ai){ } bool AiRuleAddTasks::test(){ @@ -335,8 +378,8 @@ bool AiRuleAddTasks::test(){ void AiRuleAddTasks::execute(){ int buildingCount= ai->getCountOfClass(ucBuilding); - UnitClass ucWorkerType = ucWorker; - int warriorCount= ai->getCountOfClass(ucWarrior,&ucWorkerType); + UnitClass ucWorkerType= ucWorker; + int warriorCount= ai->getCountOfClass(ucWarrior, &ucWorkerType); int workerCount= ai->getCountOfClass(ucWorker); int upgradeCount= ai->getAiInterface()->getMyUpgradeCount(); @@ -345,111 +388,160 @@ void AiRuleAddTasks::execute(){ float workerRatio= ai->getRatioOfClass(ucWorker); //standard tasks - if(ai->outputAIBehaviourToConsole()) printf("Add a TASK - AiRuleAddTasks adding ProduceTask(ucWorker) workerCount = %d, RULE Name[%s]\n",workerCount,this->getName().c_str()); + if(ai->outputAIBehaviourToConsole()) + printf("Add a TASK - AiRuleAddTasks adding ProduceTask(ucWorker) workerCount = %d, RULE Name[%s]\n", + workerCount, this->getName().c_str()); //emergency workers if(workerCount < 4){ - if(ai->outputAIBehaviourToConsole()) printf("AAA AiRuleAddTasks adding ProduceTask(ucWorker) workerCount = %d, RULE Name[%s]\n",workerCount,this->getName().c_str()); + if(ai->outputAIBehaviourToConsole()) + printf("AAA AiRuleAddTasks adding ProduceTask(ucWorker) workerCount = %d, RULE Name[%s]\n", workerCount, + this->getName().c_str()); ai->addPriorityTask(new ProduceTask(ucWorker)); } else{ - if( ai->getAiInterface()->getControlType() == ctCpuMega || - ai->getAiInterface()->getControlType() == ctNetworkCpuMega) - { - if(ai->outputAIBehaviourToConsole()) printf("AAA AiRuleAddTasks adding #1 workerCount = %d[%.2f], buildingCount = %d[%.2f] warriorCount = %d[%.2f] upgradeCount = %d RULE Name[%s]\n", - workerCount,workerRatio,buildingCount,buildingRatio,warriorCount,warriorRatio,upgradeCount,this->getName().c_str()); + if(ai->getAiInterface()->getControlType() == ctCpuMega || ai->getAiInterface()->getControlType() + == ctNetworkCpuMega){ + if(ai->outputAIBehaviourToConsole()) + printf( + "AAA AiRuleAddTasks adding #1 workerCount = %d[%.2f], buildingCount = %d[%.2f] warriorCount = %d[%.2f] upgradeCount = %d RULE Name[%s]\n", + workerCount, workerRatio, buildingCount, buildingRatio, warriorCount, warriorRatio, + upgradeCount, this->getName().c_str()); //workers - if(workerCount<5) ai->addTask(new ProduceTask(ucWorker)); - if(workerCount<10) ai->addTask(new ProduceTask(ucWorker)); - if(workerRatio<0.20) ai->addTask(new ProduceTask(ucWorker)); - if(workerRatio<0.30) ai->addTask(new ProduceTask(ucWorker)); + if(workerCount < 5) + ai->addTask(new ProduceTask(ucWorker)); + if(workerCount < 10) + ai->addTask(new ProduceTask(ucWorker)); + if(workerRatio < 0.20) + ai->addTask(new ProduceTask(ucWorker)); + if(workerRatio < 0.30) + ai->addTask(new ProduceTask(ucWorker)); //warriors - if(warriorCount<10) ai->addTask(new ProduceTask(ucWarrior)); - if(warriorRatio<0.20) ai->addTask(new ProduceTask(ucWarrior)); - if(warriorRatio<0.30) ai->addTask(new ProduceTask(ucWarrior)); - if(workerCount>=10) ai->addTask(new ProduceTask(ucWarrior)); - if(workerCount>=15) ai->addTask(new ProduceTask(ucWarrior)); - if(warriorCountminWarriors+2) - { + if(warriorCount < 10) ai->addTask(new ProduceTask(ucWarrior)); - if( buildingCount>9 ) - { + if(warriorRatio < 0.20) + ai->addTask(new ProduceTask(ucWarrior)); + if(warriorRatio < 0.30) + ai->addTask(new ProduceTask(ucWarrior)); + if(workerCount >= 10) + ai->addTask(new ProduceTask(ucWarrior)); + if(workerCount >= 15) + ai->addTask(new ProduceTask(ucWarrior)); + if(warriorCount < ai->minWarriors + 2){ + ai->addTask(new ProduceTask(ucWarrior)); + if(buildingCount > 9){ ai->addTask(new ProduceTask(ucWarrior)); ai->addTask(new ProduceTask(ucWarrior)); } - if( buildingCount>12 ) - { + if(buildingCount > 12){ ai->addTask(new ProduceTask(ucWarrior)); ai->addTask(new ProduceTask(ucWarrior)); } } //buildings - if(buildingCount<6 || buildingRatio<0.20) ai->addTask(new BuildTask()); - if(buildingCount<10 && workerCount>12) ai->addTask(new BuildTask()); + if(buildingCount < 6 || buildingRatio < 0.20) + ai->addTask(new BuildTask()); + if(buildingCount < 10 && workerCount > 12) + ai->addTask(new BuildTask()); //upgrades - if(upgradeCount==0 && workerCount>5) ai->addTask(new UpgradeTask()); - if(upgradeCount==1 && workerCount>10) ai->addTask(new UpgradeTask()); - if(upgradeCount==2 && workerCount>15) ai->addTask(new UpgradeTask()); - if(ai->isStableBase()) ai->addTask(new UpgradeTask()); + if(upgradeCount == 0 && workerCount > 5) + ai->addTask(new UpgradeTask()); + if(upgradeCount == 1 && workerCount > 10) + ai->addTask(new UpgradeTask()); + if(upgradeCount == 2 && workerCount > 15) + ai->addTask(new UpgradeTask()); + if(ai->isStableBase()) + ai->addTask(new UpgradeTask()); } - else if(ai->getAiInterface()->getControlType() == ctCpuEasy || - ai->getAiInterface()->getControlType() == ctNetworkCpuEasy) - {// Easy CPU + else if(ai->getAiInterface()->getControlType() == ctCpuEasy || ai->getAiInterface()->getControlType() + == ctNetworkCpuEasy){// Easy CPU - if(ai->outputAIBehaviourToConsole()) printf("AAA AiRuleAddTasks adding #2 workerCount = %d[%.2f], buildingCount = %d[%.2f] warriorCount = %d[%.2f] upgradeCount = %d RULE Name[%s]\n", - workerCount,workerRatio,buildingCount,buildingRatio,warriorCount,warriorRatio,upgradeCount,this->getName().c_str()); + if(ai->outputAIBehaviourToConsole()) + printf( + "AAA AiRuleAddTasks adding #2 workerCount = %d[%.2f], buildingCount = %d[%.2f] warriorCount = %d[%.2f] upgradeCount = %d RULE Name[%s]\n", + workerCount, workerRatio, buildingCount, buildingRatio, warriorCount, warriorRatio, + upgradeCount, this->getName().c_str()); //workers - if(workerCountaddTask(new ProduceTask(ucWorker)); - if(workerCount>5 && workerRatio<0.20) ai->addTask(new ProduceTask(ucWorker)); + if(workerCount < buildingCount + 2) + ai->addTask(new ProduceTask(ucWorker)); + if(workerCount > 5 && workerRatio < 0.20) + ai->addTask(new ProduceTask(ucWorker)); //warriors - if(warriorCount<10) ai->addTask(new ProduceTask(ucWarrior)); - if(warriorRatio<0.20) ai->addTask(new ProduceTask(ucWarrior)); - if(warriorRatio<0.30) ai->addTask(new ProduceTask(ucWarrior)); - if(workerCount>=10) ai->addTask(new ProduceTask(ucWarrior)); - if(workerCount>=15) ai->addTask(new ProduceTask(ucWarrior)); + if(warriorCount < 10) + ai->addTask(new ProduceTask(ucWarrior)); + if(warriorRatio < 0.20) + ai->addTask(new ProduceTask(ucWarrior)); + if(warriorRatio < 0.30) + ai->addTask(new ProduceTask(ucWarrior)); + if(workerCount >= 10) + ai->addTask(new ProduceTask(ucWarrior)); + if(workerCount >= 15) + ai->addTask(new ProduceTask(ucWarrior)); //buildings - if(buildingCount<6 || buildingRatio<0.20) ai->addTask(new BuildTask()); - if(buildingCount<10 && ai->isStableBase()) ai->addTask(new BuildTask()); + if(buildingCount < 6 || buildingRatio < 0.20) + ai->addTask(new BuildTask()); + if(buildingCount < 10 && ai->isStableBase()) + ai->addTask(new BuildTask()); //upgrades - if(upgradeCount==0 && workerCount>6) ai->addTask(new UpgradeTask()); - if(upgradeCount==1 && workerCount>7) ai->addTask(new UpgradeTask()); - if(upgradeCount==2 && workerCount>9) ai->addTask(new UpgradeTask()); + if(upgradeCount == 0 && workerCount > 6) + ai->addTask(new UpgradeTask()); + if(upgradeCount == 1 && workerCount > 7) + ai->addTask(new UpgradeTask()); + if(upgradeCount == 2 && workerCount > 9) + ai->addTask(new UpgradeTask()); //if(ai->isStableBase()) ai->addTask(new UpgradeTask()); } - else - {// normal CPU / UltraCPU ... - if(ai->outputAIBehaviourToConsole()) printf("AAA AiRuleAddTasks adding #3 workerCount = %d[%.2f], buildingCount = %d[%.2f] warriorCount = %d[%.2f] upgradeCount = %d RULE Name[%s]\n", - workerCount,workerRatio,buildingCount,buildingRatio,warriorCount,warriorRatio,upgradeCount,this->getName().c_str()); + else{// normal CPU / UltraCPU ... + if(ai->outputAIBehaviourToConsole()) + printf( + "AAA AiRuleAddTasks adding #3 workerCount = %d[%.2f], buildingCount = %d[%.2f] warriorCount = %d[%.2f] upgradeCount = %d RULE Name[%s]\n", + workerCount, workerRatio, buildingCount, buildingRatio, warriorCount, warriorRatio, + upgradeCount, this->getName().c_str()); //workers - if(workerCount<5) ai->addTask(new ProduceTask(ucWorker)); - if(workerCount<10) ai->addTask(new ProduceTask(ucWorker)); - if(workerRatio<0.20) ai->addTask(new ProduceTask(ucWorker)); - if(workerRatio<0.30) ai->addTask(new ProduceTask(ucWorker)); + if(workerCount < 5) + ai->addTask(new ProduceTask(ucWorker)); + if(workerCount < 10) + ai->addTask(new ProduceTask(ucWorker)); + if(workerRatio < 0.20) + ai->addTask(new ProduceTask(ucWorker)); + if(workerRatio < 0.30) + ai->addTask(new ProduceTask(ucWorker)); //warriors - if(warriorCount<10) ai->addTask(new ProduceTask(ucWarrior)); - if(warriorRatio<0.20) ai->addTask(new ProduceTask(ucWarrior)); - if(warriorRatio<0.30) ai->addTask(new ProduceTask(ucWarrior)); - if(workerCount>=10) ai->addTask(new ProduceTask(ucWarrior)); - if(workerCount>=15) ai->addTask(new ProduceTask(ucWarrior)); + if(warriorCount < 10) + ai->addTask(new ProduceTask(ucWarrior)); + if(warriorRatio < 0.20) + ai->addTask(new ProduceTask(ucWarrior)); + if(warriorRatio < 0.30) + ai->addTask(new ProduceTask(ucWarrior)); + if(workerCount >= 10) + ai->addTask(new ProduceTask(ucWarrior)); + if(workerCount >= 15) + ai->addTask(new ProduceTask(ucWarrior)); //buildings - if(buildingCount<6 || buildingRatio<0.20) ai->addTask(new BuildTask()); - if(buildingCount<10 && workerCount>12) ai->addTask(new BuildTask()); + if(buildingCount < 6 || buildingRatio < 0.20) + ai->addTask(new BuildTask()); + if(buildingCount < 10 && workerCount > 12) + ai->addTask(new BuildTask()); //upgrades - if(upgradeCount==0 && workerCount>5) ai->addTask(new UpgradeTask()); - if(upgradeCount==1 && workerCount>10) ai->addTask(new UpgradeTask()); - if(upgradeCount==2 && workerCount>15) ai->addTask(new UpgradeTask()); - if(ai->isStableBase()) ai->addTask(new UpgradeTask()); + if(upgradeCount == 0 && workerCount > 5) + ai->addTask(new UpgradeTask()); + if(upgradeCount == 1 && workerCount > 10) + ai->addTask(new UpgradeTask()); + if(upgradeCount == 2 && workerCount > 15) + ai->addTask(new UpgradeTask()); + if(ai->isStableBase()) + ai->addTask(new UpgradeTask()); } } } @@ -458,33 +550,33 @@ void AiRuleAddTasks::execute(){ // class AiRuleBuildOneFarm // ===================================================== -AiRuleBuildOneFarm::AiRuleBuildOneFarm(Ai *ai): - AiRule(ai) -{ +AiRuleBuildOneFarm::AiRuleBuildOneFarm(Ai *ai) : + AiRule(ai){ } bool AiRuleBuildOneFarm::test(){ AiInterface *aiInterface= ai->getAiInterface(); //for all units - for(int i=0; igetMyFactionType()->getUnitTypeCount(); ++i){ + for(int i= 0; i < aiInterface->getMyFactionType()->getUnitTypeCount(); ++i){ const UnitType *ut= aiInterface->getMyFactionType()->getUnitType(i); //for all production commands - for(int j=0; jgetCommandTypeCount(); ++j){ + for(int j= 0; j < ut->getCommandTypeCount(); ++j){ const CommandType *ct= ut->getCommandType(j); - if(ct->getClass()==ccProduce){ - const UnitType *producedType= static_cast(ct)->getProducedUnit(); + if(ct->getClass() == ccProduce){ + const UnitType *producedType= static_cast (ct)->getProducedUnit(); //for all resources - for(int k=0; kgetCostCount(); ++k){ + for(int k= 0; k < producedType->getCostCount(); ++k){ const Resource *r= producedType->getCost(k); //find a food producer in the farm produced units - if(r->getAmount()<0 && r->getType()->getClass()==rcConsumable && ai->getCountOfType(ut)==0){ + if(r->getAmount() < 0 && r->getType()->getClass() == rcConsumable && ai->getCountOfType(ut) == 0){ farm= ut; - if(ai->outputAIBehaviourToConsole()) printf("AiRuleBuildOneFarm returning true, RULE Name[%s]\n",this->getName().c_str()); + if(ai->outputAIBehaviourToConsole()) + printf("AiRuleBuildOneFarm returning true, RULE Name[%s]\n", this->getName().c_str()); return true; } } @@ -502,9 +594,8 @@ void AiRuleBuildOneFarm::execute(){ // class AiRuleProduceResourceProducer // ===================================================== -AiRuleProduceResourceProducer::AiRuleProduceResourceProducer(Ai *ai): - AiRule(ai) -{ +AiRuleProduceResourceProducer::AiRuleProduceResourceProducer(Ai *ai) : + AiRule(ai){ interval= shortInterval; } @@ -513,38 +604,43 @@ bool AiRuleProduceResourceProducer::test(){ AiInterface *aiInterface= ai->getAiInterface(); //consumables first - for(int i=0; igetTechTree()->getResourceTypeCount(); ++i){ - rt= aiInterface->getTechTree()->getResourceType(i); + for(int i= 0; i < aiInterface->getTechTree()->getResourceTypeCount(); ++i){ + rt= aiInterface->getTechTree()->getResourceType(i); const Resource *r= aiInterface->getResource(rt); - if(ai->outputAIBehaviourToConsole()) printf("CONSUMABLE [%s][%d] Testing AI RULE Name[%s]\n",rt->getName().c_str(), r->getBalance(), this->getName().c_str()); + if(ai->outputAIBehaviourToConsole()) + printf("CONSUMABLE [%s][%d] Testing AI RULE Name[%s]\n", rt->getName().c_str(), r->getBalance(), + this->getName().c_str()); - bool factionUsesResourceType = aiInterface->factionUsesResourceType(aiInterface->getMyFactionType(), rt); + bool factionUsesResourceType= aiInterface->factionUsesResourceType(aiInterface->getMyFactionType(), rt); //if(rt->getClass()==rcConsumable && r->getBalance()<0){ - if(factionUsesResourceType == true && rt->getClass() == rcConsumable && r->getBalance() < 0) { + if(factionUsesResourceType == true && rt->getClass() == rcConsumable && r->getBalance() < 0){ interval= longInterval; return true; - } - } + } + } //statics second - for(int i=0; i < aiInterface->getTechTree()->getResourceTypeCount(); ++i) { - rt= aiInterface->getTechTree()->getResourceType(i); + for(int i= 0; i < aiInterface->getTechTree()->getResourceTypeCount(); ++i){ + rt= aiInterface->getTechTree()->getResourceType(i); const Resource *r= aiInterface->getResource(rt); - if(ai->outputAIBehaviourToConsole()) printf("STATIC [%s][%d] [min %d] Testing AI RULE Name[%s]\n",rt->getName().c_str(), r->getAmount(), minStaticResources, this->getName().c_str()); + if(ai->outputAIBehaviourToConsole()) + printf("STATIC [%s][%d] [min %d] Testing AI RULE Name[%s]\n", rt->getName().c_str(), r->getAmount(), + minStaticResources, this->getName().c_str()); - if(rt->getClass() == rcStatic && r->getAmount() < minStaticResources) { - bool factionUsesResourceType = aiInterface->factionUsesResourceType(aiInterface->getMyFactionType(), rt); - if(factionUsesResourceType == true) { + if(rt->getClass() == rcStatic && r->getAmount() < minStaticResources){ + bool factionUsesResourceType= aiInterface->factionUsesResourceType(aiInterface->getMyFactionType(), rt); + if(factionUsesResourceType == true){ interval= longInterval; return true; } - } - } + } + } - if(ai->outputAIBehaviourToConsole()) printf("STATIC returning FALSE\n"); + if(ai->outputAIBehaviourToConsole()) + printf("STATIC returning FALSE\n"); interval= shortInterval; return false; @@ -559,30 +655,31 @@ void AiRuleProduceResourceProducer::execute(){ // class AiRuleProduce // ===================================================== -AiRuleProduce::AiRuleProduce(Ai *ai): - AiRule(ai) -{ +AiRuleProduce::AiRuleProduce(Ai *ai) : + AiRule(ai){ produceTask= NULL; } bool AiRuleProduce::test(){ const Task *task= ai->getTask(); - if(task==NULL || task->getClass()!=tcProduce){ + if(task == NULL || task->getClass() != tcProduce){ return false; } - produceTask= static_cast(task); + produceTask= static_cast (task); return true; } void AiRuleProduce::execute(){ - if(produceTask!=NULL){ + if(produceTask != NULL){ - if(ai->outputAIBehaviourToConsole()) printf("AiRuleProduce producing [%s]\n",(produceTask->getUnitType() != NULL ? produceTask->getUnitType()->getName().c_str() : "null")); + if(ai->outputAIBehaviourToConsole()) + printf("AiRuleProduce producing [%s]\n", + (produceTask->getUnitType() != NULL ? produceTask->getUnitType()->getName().c_str() : "null")); //generic produce task, produce random unit that has the skill or produces the resource - if(produceTask->getUnitType()==NULL){ + if(produceTask->getUnitType() == NULL){ produceGeneric(produceTask); } @@ -602,37 +699,43 @@ void AiRuleProduce::produceGeneric(const ProduceTask *pt){ AiInterface *aiInterface= ai->getAiInterface(); //for each unit, produce it if possible - for(int i=0; igetMyUnitCount(); ++i){ + for(int i= 0; i < aiInterface->getMyUnitCount(); ++i){ //for each command const UnitType *ut= aiInterface->getMyUnit(i)->getType(); - for(int j=0; jgetCommandTypeCount(); ++j){ + for(int j= 0; j < ut->getCommandTypeCount(); ++j){ const CommandType *ct= ut->getCommandType(j); //if the command is produce - if(ct->getClass()==ccProduce || ct->getClass()==ccMorph){ + if(ct->getClass() == ccProduce || ct->getClass() == ccMorph){ - const UnitType *producedUnit= static_cast(ct->getProduced()); + const UnitType *producedUnit= static_cast (ct->getProduced()); bool produceIt= false; - if(ai->outputAIBehaviourToConsole()) printf("produceGeneric [%p] Testing AI RULE Name[%s]\n",pt->getResourceType(), this->getName().c_str()); + if(ai->outputAIBehaviourToConsole()) + printf("produceGeneric [%p] Testing AI RULE Name[%s]\n", pt->getResourceType(), + this->getName().c_str()); //if the unit produces the resource - if(pt->getResourceType()!=NULL){ + if(pt->getResourceType() != NULL){ const Resource *r= producedUnit->getCost(pt->getResourceType()); - if(r != NULL) { - if(ai->outputAIBehaviourToConsole()) printf("produceGeneric r = [%s][%d] Testing AI RULE Name[%s]\n",r->getDescription().c_str(),r->getAmount(), this->getName().c_str()); + if(r != NULL){ + if(ai->outputAIBehaviourToConsole()) + printf("produceGeneric r = [%s][%d] Testing AI RULE Name[%s]\n", + r->getDescription().c_str(), r->getAmount(), this->getName().c_str()); } - if(r!=NULL && r->getAmount()<0){ + if(r != NULL && r->getAmount() < 0){ produceIt= true; } } else{ //if the unit is from the right class - if(ai->outputAIBehaviourToConsole()) printf("produceGeneric right class = [%d] Testing AI RULE Name[%s]\n",producedUnit->isOfClass(pt->getUnitClass()), this->getName().c_str()); + if(ai->outputAIBehaviourToConsole()) + printf("produceGeneric right class = [%d] Testing AI RULE Name[%s]\n", producedUnit->isOfClass( + pt->getUnitClass()), this->getName().c_str()); if(producedUnit->isOfClass(pt->getUnitClass())){ if(aiInterface->reqsOk(ct) && aiInterface->reqsOk(producedUnit)){ @@ -643,7 +746,7 @@ void AiRuleProduce::produceGeneric(const ProduceTask *pt){ if(produceIt){ //if the unit is not already on the list - if(find(ableUnits.begin(), ableUnits.end(), producedUnit)==ableUnits.end()){ + if(find(ableUnits.begin(), ableUnits.end(), producedUnit) == ableUnits.end()){ ableUnits.push_back(producedUnit); } } @@ -654,12 +757,14 @@ void AiRuleProduce::produceGeneric(const ProduceTask *pt){ //add specific produce task if(!ableUnits.empty()){ - if(ai->outputAIBehaviourToConsole()) printf("produceGeneric !ableUnits.empty(), ableUnits.size() = [%d] Testing AI RULE Name[%s]\n",(int)ableUnits.size(), this->getName().c_str()); + if(ai->outputAIBehaviourToConsole()) + printf("produceGeneric !ableUnits.empty(), ableUnits.size() = [%d] Testing AI RULE Name[%s]\n", + (int) ableUnits.size(), this->getName().c_str()); //priority for non produced units - for(unsigned int i=0; igetCountOfType(ableUnits[i])==0){ - if(ai->getRandom()->randRange(0, 1)==0){ + for(unsigned int i= 0; i < ableUnits.size(); ++i){ + if(ai->getCountOfType(ableUnits[i]) == 0){ + if(ai->getRandom()->randRange(0, 1) == 0){ ai->addTask(new ProduceTask(ableUnits[i])); return; } @@ -667,7 +772,7 @@ void AiRuleProduce::produceGeneric(const ProduceTask *pt){ } //normal case - ai->addTask(new ProduceTask(ableUnits[ai->getRandom()->randRange(0, ableUnits.size()-1)])); + ai->addTask(new ProduceTask(ableUnits[ai->getRandom()->randRange(0, ableUnits.size() - 1)])); } } @@ -675,12 +780,16 @@ void AiRuleProduce::produceSpecific(const ProduceTask *pt){ AiInterface *aiInterface= ai->getAiInterface(); - if(ai->outputAIBehaviourToConsole()) printf("produceSpecific aiInterface->reqsOk(pt->getUnitType()) = [%d] Testing AI RULE Name[%s]\n",aiInterface->reqsOk(pt->getUnitType()), this->getName().c_str()); + if(ai->outputAIBehaviourToConsole()) + printf("produceSpecific aiInterface->reqsOk(pt->getUnitType()) = [%d] Testing AI RULE Name[%s]\n", + aiInterface->reqsOk(pt->getUnitType()), this->getName().c_str()); //if unit meets requirements if(aiInterface->reqsOk(pt->getUnitType())){ - if(ai->outputAIBehaviourToConsole()) printf("produceSpecific aiInterface->checkCosts(pt->getUnitType()) = [%d] Testing AI RULE Name[%s]\n",aiInterface->checkCosts(pt->getUnitType()), this->getName().c_str()); + if(ai->outputAIBehaviourToConsole()) + printf("produceSpecific aiInterface->checkCosts(pt->getUnitType()) = [%d] Testing AI RULE Name[%s]\n", + aiInterface->checkCosts(pt->getUnitType()), this->getName().c_str()); //if unit doesnt meet resources retry if(!aiInterface->checkCosts(pt->getUnitType())){ @@ -692,25 +801,27 @@ void AiRuleProduce::produceSpecific(const ProduceTask *pt){ vector producers; // Hold a list of units which can produce or morph // then a list of commandtypes for each unit - map > producersDefaultCommandType; + map > producersDefaultCommandType; const CommandType *defCt= NULL; //for each unit - for(int i=0; igetMyUnitCount(); ++i){ + for(int i= 0; i < aiInterface->getMyUnitCount(); ++i){ //for each command const UnitType *ut= aiInterface->getMyUnit(i)->getType(); - for(int j=0; jgetCommandTypeCount(); ++j){ + for(int j= 0; j < ut->getCommandTypeCount(); ++j){ const CommandType *ct= ut->getCommandType(j); //if the command is produce - if(ct->getClass()==ccProduce || ct->getClass()==ccMorph){ - const UnitType *producedUnit= static_cast(ct->getProduced()); + if(ct->getClass() == ccProduce || ct->getClass() == ccMorph){ + const UnitType *producedUnit= static_cast (ct->getProduced()); //if units match if(producedUnit == pt->getUnitType()){ - if(ai->outputAIBehaviourToConsole()) printf("produceSpecific aiInterface->reqsOk(ct) = [%d] Testing AI RULE Name[%s]\n",aiInterface->reqsOk(ct), this->getName().c_str()); + if(ai->outputAIBehaviourToConsole()) + printf("produceSpecific aiInterface->reqsOk(ct) = [%d] Testing AI RULE Name[%s]\n", + aiInterface->reqsOk(ct), this->getName().c_str()); if(aiInterface->reqsOk(ct)){ defCt= ct; @@ -725,53 +836,65 @@ void AiRuleProduce::produceSpecific(const ProduceTask *pt){ //produce from random producer if(!producers.empty()){ - if(ai->outputAIBehaviourToConsole()) printf("produceSpecific producers.empty() = [%d] Testing AI RULE Name[%s]\n",producers.empty(), this->getName().c_str()); + if(ai->outputAIBehaviourToConsole()) + printf("produceSpecific producers.empty() = [%d] Testing AI RULE Name[%s]\n", producers.empty(), + this->getName().c_str()); - if( aiInterface->getControlType() == ctCpuMega || - aiInterface->getControlType() == ctNetworkCpuMega) - {// mega cpu trys to balance the commands to the producers - int randomstart=ai->getRandom()->randRange(0, producers.size()-1); - int lowestCommandCount=1000000; - int currentProducerIndex=producers[randomstart]; - int bestIndex=-1; - int besti=0; - int currentCommandCount=0; - for(unsigned int i=randomstart; i= producers.size()) { - prIndex = (i - producers.size()); + if(aiInterface->getControlType() == ctCpuMega || aiInterface->getControlType() == ctNetworkCpuMega){// mega cpu trys to balance the commands to the producers + int randomstart= ai->getRandom()->randRange(0, producers.size() - 1); + int lowestCommandCount= 1000000; + int currentProducerIndex= producers[randomstart]; + int bestIndex= -1; + int besti= 0; + int currentCommandCount= 0; + for(unsigned int i= randomstart; i < producers.size() + randomstart; i++){ + int prIndex= i; + if(i >= producers.size()){ + prIndex= (i - producers.size()); } - currentProducerIndex=producers[prIndex]; + currentProducerIndex= producers[prIndex]; - if(currentProducerIndex >= aiInterface->getMyUnitCount()) { - char szBuf[1024]=""; - printf("In [%s::%s Line: %d] currentProducerIndex >= aiInterface->getMyUnitCount(), currentProducerIndex = %d, aiInterface->getMyUnitCount() = %d, i = %d,producers.size() = %lu\n",__FILE__,__FUNCTION__,__LINE__,currentProducerIndex,aiInterface->getMyUnitCount(),i,(unsigned long)producers.size()); - sprintf(szBuf,"In [%s::%s Line: %d] currentProducerIndex >= aiInterface->getMyUnitCount(), currentProducerIndex = %d, aiInterface->getMyUnitCount() = %d, i = %d,producers.size() = %lu",__FILE__,__FUNCTION__,__LINE__,currentProducerIndex,aiInterface->getMyUnitCount(),i,(unsigned long)producers.size()); + if(currentProducerIndex >= aiInterface->getMyUnitCount()){ + char szBuf[1024]= ""; + printf( + "In [%s::%s Line: %d] currentProducerIndex >= aiInterface->getMyUnitCount(), currentProducerIndex = %d, aiInterface->getMyUnitCount() = %d, i = %d,producers.size() = %lu\n", + __FILE__, __FUNCTION__, __LINE__, currentProducerIndex, aiInterface->getMyUnitCount(), + i, (unsigned long) producers.size()); + sprintf( + szBuf, + "In [%s::%s Line: %d] currentProducerIndex >= aiInterface->getMyUnitCount(), currentProducerIndex = %d, aiInterface->getMyUnitCount() = %d, i = %d,producers.size() = %lu", + __FILE__, __FUNCTION__, __LINE__, currentProducerIndex, aiInterface->getMyUnitCount(), + i, (unsigned long) producers.size()); throw runtime_error(szBuf); } - if(prIndex >= producers.size()) { - char szBuf[1024]=""; - printf("In [%s::%s Line: %d] prIndex >= producers.size(), currentProducerIndex = %d, i = %d,producers.size() = %lu \n",__FILE__,__FUNCTION__,__LINE__,prIndex,i,(unsigned long)producers.size()); - sprintf(szBuf,"In [%s::%s Line: %d] currentProducerIndex >= producers.size(), currentProducerIndex = %d, i = %d,producers.size() = %lu",__FILE__,__FUNCTION__,__LINE__,currentProducerIndex,i,(unsigned long)producers.size()); + if(prIndex >= producers.size()){ + char szBuf[1024]= ""; + printf( + "In [%s::%s Line: %d] prIndex >= producers.size(), currentProducerIndex = %d, i = %d,producers.size() = %lu \n", + __FILE__, __FUNCTION__, __LINE__, prIndex, i, (unsigned long) producers.size()); + sprintf( + szBuf, + "In [%s::%s Line: %d] currentProducerIndex >= producers.size(), currentProducerIndex = %d, i = %d,producers.size() = %lu", + __FILE__, __FUNCTION__, __LINE__, currentProducerIndex, i, + (unsigned long) producers.size()); throw runtime_error(szBuf); } - currentCommandCount=aiInterface->getMyUnit(currentProducerIndex)->getCommandSize(); - if( currentCommandCount==1 && - aiInterface->getMyUnit(currentProducerIndex)->getCurrCommand()->getCommandType()->getClass()==ccStop) - {// special for non buildings - currentCommandCount=0; + currentCommandCount= aiInterface->getMyUnit(currentProducerIndex)->getCommandSize(); + if(currentCommandCount == 1 + && aiInterface->getMyUnit(currentProducerIndex)->getCurrCommand()->getCommandType()->getClass() + == ccStop){// special for non buildings + currentCommandCount= 0; } - if(lowestCommandCount>currentCommandCount) - { - lowestCommandCount=aiInterface->getMyUnit(currentProducerIndex)->getCommandSize(); - bestIndex=currentProducerIndex; - besti=i%(producers.size()); + if(lowestCommandCount > currentCommandCount){ + lowestCommandCount= aiInterface->getMyUnit(currentProducerIndex)->getCommandSize(); + bestIndex= currentProducerIndex; + besti= i % (producers.size()); } } - if( aiInterface->getMyUnit(bestIndex)->getCommandSize() > 2) { + if(aiInterface->getMyUnit(bestIndex)->getCommandSize() > 2){ // maybe we need another producer of this kind if possible! - if(aiInterface->reqsOk(aiInterface->getMyUnit(bestIndex)->getType())) { + if(aiInterface->reqsOk(aiInterface->getMyUnit(bestIndex)->getType())){ if(ai->getCountOfClass(ucBuilding) > 5) ai->addTask(new BuildTask(aiInterface->getMyUnit(bestIndex)->getType())); } @@ -779,148 +902,185 @@ void AiRuleProduce::produceSpecific(const ProduceTask *pt){ vector backupProducers; // find another producer unit which is free and produce any kind of warrior. //for each unit - for(int i=0; igetMyUnitCount(); ++i){ + for(int i= 0; i < aiInterface->getMyUnitCount(); ++i){ const UnitType *ut= aiInterface->getMyUnit(i)->getType(); //for each command - for(int j=0; jgetCommandTypeCount(); ++j){ + for(int j= 0; j < ut->getCommandTypeCount(); ++j){ const CommandType *ct= ut->getCommandType(j); //if the command is produce - if(ct->getClass() == ccProduce) { - const UnitType *unitType= static_cast(ct->getProduced()); - if(unitType->hasSkillClass(scAttack) && !unitType->hasCommandClass(ccHarvest) && aiInterface->reqsOk(ct)) - {//this can produce a warrior + if(ct->getClass() == ccProduce){ + const UnitType *unitType= static_cast (ct->getProduced()); + if(unitType->hasSkillClass(scAttack) && !unitType->hasCommandClass(ccHarvest) + && aiInterface->reqsOk(ct)){//this can produce a warrior backupProducers.push_back(i); } } } } - if(!backupProducers.empty()) { - int randomstart=ai->getRandom()->randRange(0, backupProducers.size()-1); - int lowestCommandCount=1000000; - int currentProducerIndex=backupProducers[randomstart]; - int bestIndex=-1; - int currentCommandCount=0; - for(unsigned int i=randomstart; i= backupProducers.size()) { - prIndex = (i - backupProducers.size()); + if(!backupProducers.empty()){ + int randomstart= ai->getRandom()->randRange(0, backupProducers.size() - 1); + int lowestCommandCount= 1000000; + int currentProducerIndex= backupProducers[randomstart]; + int bestIndex= -1; + int currentCommandCount= 0; + for(unsigned int i= randomstart; i < backupProducers.size() + randomstart; i++){ + int prIndex= i; + if(i >= backupProducers.size()){ + prIndex= (i - backupProducers.size()); } - currentProducerIndex=backupProducers[prIndex]; + currentProducerIndex= backupProducers[prIndex]; - if(currentProducerIndex >= aiInterface->getMyUnitCount()) { - char szBuf[1024]=""; - printf("In [%s::%s Line: %d] currentProducerIndex >= aiInterface->getMyUnitCount(), currentProducerIndex = %d, aiInterface->getMyUnitCount() = %d, i = %d,backupProducers.size() = %lu\n",__FILE__,__FUNCTION__,__LINE__,currentProducerIndex,aiInterface->getMyUnitCount(),i,(unsigned long)backupProducers.size()); - sprintf(szBuf,"In [%s::%s Line: %d] currentProducerIndex >= aiInterface->getMyUnitCount(), currentProducerIndex = %d, aiInterface->getMyUnitCount() = %d, i = %d,backupProducers.size() = %lu",__FILE__,__FUNCTION__,__LINE__,currentProducerIndex,aiInterface->getMyUnitCount(),i,(unsigned long)backupProducers.size()); + if(currentProducerIndex >= aiInterface->getMyUnitCount()){ + char szBuf[1024]= ""; + printf( + "In [%s::%s Line: %d] currentProducerIndex >= aiInterface->getMyUnitCount(), currentProducerIndex = %d, aiInterface->getMyUnitCount() = %d, i = %d,backupProducers.size() = %lu\n", + __FILE__, __FUNCTION__, __LINE__, currentProducerIndex, + aiInterface->getMyUnitCount(), i, (unsigned long) backupProducers.size()); + sprintf( + szBuf, + "In [%s::%s Line: %d] currentProducerIndex >= aiInterface->getMyUnitCount(), currentProducerIndex = %d, aiInterface->getMyUnitCount() = %d, i = %d,backupProducers.size() = %lu", + __FILE__, __FUNCTION__, __LINE__, currentProducerIndex, + aiInterface->getMyUnitCount(), i, (unsigned long) backupProducers.size()); throw runtime_error(szBuf); } - if(prIndex >= backupProducers.size()) { - char szBuf[1024]=""; - printf("In [%s::%s Line: %d] prIndex >= backupProducers.size(), currentProducerIndex = %d, i = %d,backupProducers.size() = %lu \n",__FILE__,__FUNCTION__,__LINE__,prIndex,i,(unsigned long)backupProducers.size()); - sprintf(szBuf,"In [%s::%s Line: %d] currentProducerIndex >= backupProducers.size(), currentProducerIndex = %d, i = %d,backupProducers.size() = %lu",__FILE__,__FUNCTION__,__LINE__,currentProducerIndex,i,(unsigned long)backupProducers.size()); + if(prIndex >= backupProducers.size()){ + char szBuf[1024]= ""; + printf( + "In [%s::%s Line: %d] prIndex >= backupProducers.size(), currentProducerIndex = %d, i = %d,backupProducers.size() = %lu \n", + __FILE__, __FUNCTION__, __LINE__, prIndex, i, + (unsigned long) backupProducers.size()); + sprintf( + szBuf, + "In [%s::%s Line: %d] currentProducerIndex >= backupProducers.size(), currentProducerIndex = %d, i = %d,backupProducers.size() = %lu", + __FILE__, __FUNCTION__, __LINE__, currentProducerIndex, i, + (unsigned long) backupProducers.size()); throw runtime_error(szBuf); } - currentCommandCount=aiInterface->getMyUnit(currentProducerIndex)->getCommandSize(); - if( currentCommandCount==1 && - aiInterface->getMyUnit(currentProducerIndex)->getCurrCommand()->getCommandType()->getClass()==ccStop) - {// special for non buildings - currentCommandCount=0; + currentCommandCount= aiInterface->getMyUnit(currentProducerIndex)->getCommandSize(); + if(currentCommandCount == 1 + && aiInterface->getMyUnit(currentProducerIndex)->getCurrCommand()->getCommandType()->getClass() + == ccStop){// special for non buildings + currentCommandCount= 0; } - if(lowestCommandCount>currentCommandCount) { - lowestCommandCount=currentCommandCount; - bestIndex=currentProducerIndex; - if(lowestCommandCount==0) break; + if(lowestCommandCount > currentCommandCount){ + lowestCommandCount= currentCommandCount; + bestIndex= currentProducerIndex; + if(lowestCommandCount == 0) + break; } } // a good producer is found, lets choose a warrior production vector productionCommandIndexes; - const UnitType *ut=aiInterface->getMyUnit(bestIndex)->getType(); - for(int j=0; jgetCommandTypeCount(); ++j){ + const UnitType *ut= aiInterface->getMyUnit(bestIndex)->getType(); + for(int j= 0; j < ut->getCommandTypeCount(); ++j){ const CommandType *ct= ut->getCommandType(j); //if the command is produce - if(ct->getClass()==ccProduce) { - const UnitType *unitType= static_cast(ct->getProduced()); - if(unitType->hasSkillClass(scAttack) && !unitType->hasCommandClass(ccHarvest) && aiInterface->reqsOk(ct)) - {//this can produce a warrior + if(ct->getClass() == ccProduce){ + const UnitType *unitType= static_cast (ct->getProduced()); + if(unitType->hasSkillClass(scAttack) && !unitType->hasCommandClass(ccHarvest) + && aiInterface->reqsOk(ct)){//this can produce a warrior productionCommandIndexes.push_back(j); } } } - int commandIndex=productionCommandIndexes[ai->getRandom()->randRange(0, productionCommandIndexes.size()-1)]; - if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + int commandIndex= productionCommandIndexes[ai->getRandom()->randRange(0, + productionCommandIndexes.size() - 1)]; + if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); - if(ai->outputAIBehaviourToConsole()) printf("mega #1 produceSpecific giveCommand to unit [%s] commandType [%s]\n",aiInterface->getMyUnit(bestIndex)->getType()->getName().c_str(),ut->getCommandType(commandIndex)->getName().c_str()); + if(ai->outputAIBehaviourToConsole()) + printf("mega #1 produceSpecific giveCommand to unit [%s] commandType [%s]\n", + aiInterface->getMyUnit(bestIndex)->getType()->getName().c_str(), + ut->getCommandType(commandIndex)->getName().c_str()); aiInterface->giveCommand(bestIndex, ut->getCommandType(commandIndex)); } - else - {// do it like normal CPU - if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); - defCt = NULL; - if(producersDefaultCommandType.find(bestIndex) != producersDefaultCommandType.end()) { - int bestCommandTypeCount = producersDefaultCommandType[bestIndex].size(); - int bestCommandTypeIndex = ai->getRandom()->randRange(0, bestCommandTypeCount-1); + else{// do it like normal CPU + if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + defCt= NULL; + if(producersDefaultCommandType.find(bestIndex) != producersDefaultCommandType.end()){ + int bestCommandTypeCount= producersDefaultCommandType[bestIndex].size(); + int bestCommandTypeIndex= ai->getRandom()->randRange(0, bestCommandTypeCount - 1); - if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] bestCommandTypeIndex = %d, bestCommandTypeCount = %d\n",__FILE__,__FUNCTION__,__LINE__,bestCommandTypeIndex,bestCommandTypeCount); + if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] bestCommandTypeIndex = %d, bestCommandTypeCount = %d\n",__FILE__,__FUNCTION__,__LINE__,bestCommandTypeIndex,bestCommandTypeCount); - defCt = producersDefaultCommandType[bestIndex][bestCommandTypeIndex]; + defCt= producersDefaultCommandType[bestIndex][bestCommandTypeIndex]; } - if(ai->outputAIBehaviourToConsole()) printf("mega #2 produceSpecific giveCommand to unit [%s] commandType [%s]\n",aiInterface->getMyUnit(bestIndex)->getType()->getName().c_str(),defCt->getName().c_str()); + if(ai->outputAIBehaviourToConsole()) + printf("mega #2 produceSpecific giveCommand to unit [%s] commandType [%s]\n", + aiInterface->getMyUnit(bestIndex)->getType()->getName().c_str(), + defCt->getName().c_str()); aiInterface->giveCommand(bestIndex, defCt); } } - else - { - if(currentCommandCount==0) { - if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); - defCt = NULL; - if(producersDefaultCommandType.find(bestIndex) != producersDefaultCommandType.end()) { + else{ + if(currentCommandCount == 0){ + if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + defCt= NULL; + if(producersDefaultCommandType.find(bestIndex) != producersDefaultCommandType.end()){ //defCt = producersDefaultCommandType[bestIndex]; - int bestCommandTypeCount = producersDefaultCommandType[bestIndex].size(); - int bestCommandTypeIndex = ai->getRandom()->randRange(0, bestCommandTypeCount-1); + int bestCommandTypeCount= producersDefaultCommandType[bestIndex].size(); + int bestCommandTypeIndex= ai->getRandom()->randRange(0, bestCommandTypeCount - 1); - if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] bestCommandTypeIndex = %d, bestCommandTypeCount = %d\n",__FILE__,__FUNCTION__,__LINE__,bestCommandTypeIndex,bestCommandTypeCount); + if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] bestCommandTypeIndex = %d, bestCommandTypeCount = %d\n",__FILE__,__FUNCTION__,__LINE__,bestCommandTypeIndex,bestCommandTypeCount); - defCt = producersDefaultCommandType[bestIndex][bestCommandTypeIndex]; + defCt= producersDefaultCommandType[bestIndex][bestCommandTypeIndex]; } - if(ai->outputAIBehaviourToConsole()) printf("mega #3 produceSpecific giveCommand to unit [%s] commandType [%s]\n",aiInterface->getMyUnit(bestIndex)->getType()->getName().c_str(),defCt->getName().c_str()); + if(ai->outputAIBehaviourToConsole()) + printf("mega #3 produceSpecific giveCommand to unit [%s] commandType [%s]\n", + aiInterface->getMyUnit(bestIndex)->getType()->getName().c_str(), + defCt->getName().c_str()); aiInterface->giveCommand(bestIndex, defCt); } - if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); - defCt = NULL; - if(producersDefaultCommandType.find(bestIndex) != producersDefaultCommandType.end()) { + if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + defCt= NULL; + if(producersDefaultCommandType.find(bestIndex) != producersDefaultCommandType.end()){ //defCt = producersDefaultCommandType[bestIndex]; - int bestCommandTypeCount = producersDefaultCommandType[bestIndex].size(); - int bestCommandTypeIndex = ai->getRandom()->randRange(0, bestCommandTypeCount-1); + int bestCommandTypeCount= producersDefaultCommandType[bestIndex].size(); + int bestCommandTypeIndex= ai->getRandom()->randRange(0, bestCommandTypeCount - 1); - if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] bestCommandTypeIndex = %d, bestCommandTypeCount = %d\n",__FILE__,__FUNCTION__,__LINE__,bestCommandTypeIndex,bestCommandTypeCount); + if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] bestCommandTypeIndex = %d, bestCommandTypeCount = %d\n",__FILE__,__FUNCTION__,__LINE__,bestCommandTypeIndex,bestCommandTypeCount); - defCt = producersDefaultCommandType[bestIndex][bestCommandTypeIndex]; + defCt= producersDefaultCommandType[bestIndex][bestCommandTypeIndex]; } - if(ai->outputAIBehaviourToConsole()) printf("mega #4 produceSpecific giveCommand to unit [%s] commandType [%s]\n",aiInterface->getMyUnit(bestIndex)->getType()->getName().c_str(),defCt->getName().c_str()); + if(ai->outputAIBehaviourToConsole()) + printf("mega #4 produceSpecific giveCommand to unit [%s] commandType [%s]\n", + aiInterface->getMyUnit(bestIndex)->getType()->getName().c_str(), + defCt->getName().c_str()); aiInterface->giveCommand(bestIndex, defCt); } } - else { - int pIndex = ai->getRandom()->randRange(0, producers.size()-1); + else{ + int pIndex= ai->getRandom()->randRange(0, producers.size() - 1); int producerIndex= producers[pIndex]; - defCt = NULL; - if(producersDefaultCommandType.find(producerIndex) != producersDefaultCommandType.end()) { + defCt= NULL; + if(producersDefaultCommandType.find(producerIndex) != producersDefaultCommandType.end()){ //defCt = producersDefaultCommandType[producerIndex]; - int bestCommandTypeCount = producersDefaultCommandType[producerIndex].size(); - int bestCommandTypeIndex = ai->getRandom()->randRange(0, bestCommandTypeCount-1); + int bestCommandTypeCount= producersDefaultCommandType[producerIndex].size(); + int bestCommandTypeIndex= ai->getRandom()->randRange(0, bestCommandTypeCount - 1); - if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] bestCommandTypeIndex = %d, bestCommandTypeCount = %d\n",__FILE__,__FUNCTION__,__LINE__,bestCommandTypeIndex,bestCommandTypeCount); + if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] bestCommandTypeIndex = %d, bestCommandTypeCount = %d\n",__FILE__,__FUNCTION__,__LINE__,bestCommandTypeIndex,bestCommandTypeCount); - defCt = producersDefaultCommandType[producerIndex][bestCommandTypeIndex]; + defCt= producersDefaultCommandType[producerIndex][bestCommandTypeIndex]; } - if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] producers.size() = %d, producerIndex = %d, pIndex = %d, producersDefaultCommandType.size() = %d\n",__FILE__,__FUNCTION__,__LINE__,producers.size(),producerIndex,pIndex,producersDefaultCommandType.size()); + if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] producers.size() = %d, producerIndex = %d, pIndex = %d, producersDefaultCommandType.size() = %d\n",__FILE__,__FUNCTION__,__LINE__,producers.size(),producerIndex,pIndex,producersDefaultCommandType.size()); - if(ai->outputAIBehaviourToConsole()) printf("produceSpecific giveCommand to unit [%s] commandType [%s]\n",aiInterface->getMyUnit(producerIndex)->getType()->getName().c_str(),defCt->getName().c_str()); + if(ai->outputAIBehaviourToConsole()) + printf("produceSpecific giveCommand to unit [%s] commandType [%s]\n", aiInterface->getMyUnit( + producerIndex)->getType()->getName().c_str(), defCt->getName().c_str()); aiInterface->giveCommand(producerIndex, defCt); } } @@ -931,34 +1091,34 @@ void AiRuleProduce::produceSpecific(const ProduceTask *pt){ // class AiRuleBuild // ======================================== -AiRuleBuild::AiRuleBuild(Ai *ai): - AiRule(ai) -{ +AiRuleBuild::AiRuleBuild(Ai *ai) : + AiRule(ai){ buildTask= NULL; } bool AiRuleBuild::test(){ const Task *task= ai->getTask(); - if(task==NULL || task->getClass()!=tcBuild){ + if(task == NULL || task->getClass() != tcBuild){ return false; } - buildTask= static_cast(task); + buildTask= static_cast (task); return true; } - -void AiRuleBuild::execute() { - if(buildTask!=NULL) { - if(ai->outputAIBehaviourToConsole()) printf("BUILD AiRuleBuild Unit Name[%s]\n",(buildTask->getUnitType() != NULL ? buildTask->getUnitType()->getName().c_str() : "null")); +void AiRuleBuild::execute(){ + if(buildTask != NULL){ + if(ai->outputAIBehaviourToConsole()) + printf("BUILD AiRuleBuild Unit Name[%s]\n", + (buildTask->getUnitType() != NULL ? buildTask->getUnitType()->getName().c_str() : "null")); //generic build task, build random building that can be built - if(buildTask->getUnitType() == NULL) { + if(buildTask->getUnitType() == NULL){ buildGeneric(buildTask); } //specific building task, build if possible, retry if not enough resources or not position - else { + else{ buildSpecific(buildTask); } @@ -975,26 +1135,26 @@ void AiRuleBuild::buildGeneric(const BuildTask *bt){ UnitTypes buildings; //for each unit - for(int i=0; igetMyUnitCount(); ++i){ + for(int i= 0; i < aiInterface->getMyUnitCount(); ++i){ //for each command const UnitType *ut= aiInterface->getMyUnit(i)->getType(); - for(int j=0; jgetCommandTypeCount(); ++j){ + for(int j= 0; j < ut->getCommandTypeCount(); ++j){ const CommandType *ct= ut->getCommandType(j); //if the command is build - if(ct->getClass()==ccBuild){ - const BuildCommandType *bct= static_cast(ct); + if(ct->getClass() == ccBuild){ + const BuildCommandType *bct= static_cast (ct); //for each building - for(int k=0; kgetBuildingCount(); ++k){ + for(int k= 0; k < bct->getBuildingCount(); ++k){ const UnitType *building= bct->getBuilding(k); if(aiInterface->reqsOk(bct) && aiInterface->reqsOk(building)){ //if any building, or produces resource const ResourceType *rt= bt->getResourceType(); const Resource *cost= building->getCost(rt); - if(rt==NULL || (cost!=NULL && cost->getAmount()<0)){ + if(rt == NULL || (cost != NULL && cost->getAmount() < 0)){ buildings.push_back(building); } } @@ -1013,35 +1173,32 @@ void AiRuleBuild::buildBestBuilding(const vector &buildings){ //build the least built building bool buildingFound= false; - for(int i=0; i<10 && !buildingFound; ++i){ + for(int i= 0; i < 10 && !buildingFound; ++i){ - if(i>0){ + if(i > 0){ //Defensive buildings have priority - for(int j=0; jgetCountOfType(building)<=i+1 && isDefensive(building)) - { + if(ai->getCountOfType(building) <= i + 1 && isDefensive(building)){ ai->addTask(new BuildTask(building)); buildingFound= true; } } //Warrior producers next - for(unsigned int j=0; jgetCountOfType(building)<=i+1 && isWarriorProducer(building)) - { + if(ai->getCountOfType(building) <= i + 1 && isWarriorProducer(building)){ ai->addTask(new BuildTask(building)); buildingFound= true; } } //Resource producers next - for(unsigned int j=0; jgetCountOfType(building)<=i+1 && isResourceProducer(building)) - { + if(ai->getCountOfType(building) <= i + 1 && isResourceProducer(building)){ ai->addTask(new BuildTask(building)); buildingFound= true; } @@ -1049,10 +1206,9 @@ void AiRuleBuild::buildBestBuilding(const vector &buildings){ } //Any building - for(unsigned int j=0; jgetCountOfType(building)<=i) - { + if(ai->getCountOfType(building) <= i){ ai->addTask(new BuildTask(building)); buildingFound= true; } @@ -1061,13 +1217,13 @@ void AiRuleBuild::buildBestBuilding(const vector &buildings){ } } -void AiRuleBuild::buildSpecific(const BuildTask *bt) { +void AiRuleBuild::buildSpecific(const BuildTask *bt){ AiInterface *aiInterface= ai->getAiInterface(); //if reqs ok - if(aiInterface->reqsOk(bt->getUnitType())) { + if(aiInterface->reqsOk(bt->getUnitType())){ //retry if not enough resources - if(aiInterface->checkCosts(bt->getUnitType()) == false) { + if(aiInterface->checkCosts(bt->getUnitType()) == false){ ai->retryTask(bt); return; } @@ -1075,32 +1231,32 @@ void AiRuleBuild::buildSpecific(const BuildTask *bt) { vector builders; // Hold a list of units which can build // then a list of build commandtypes for each unit - map > buildersDefaultCommandType; + map > buildersDefaultCommandType; const BuildCommandType *defBct= NULL; //for each unit - for(int i = 0; i < aiInterface->getMyUnitCount(); ++i) { + for(int i= 0; i < aiInterface->getMyUnitCount(); ++i){ //if the unit is not going to build - const Unit *u = aiInterface->getMyUnit(i); - if(u->anyCommand() == false || u->getCurrCommand()->getCommandType()->getClass() != ccBuild) { + const Unit *u= aiInterface->getMyUnit(i); + if(u->anyCommand() == false || u->getCurrCommand()->getCommandType()->getClass() != ccBuild){ //for each command const UnitType *ut= aiInterface->getMyUnit(i)->getType(); - for(int j = 0; j < ut->getCommandTypeCount(); ++j) { + for(int j= 0; j < ut->getCommandTypeCount(); ++j){ const CommandType *ct= ut->getCommandType(j); //if the command is build - if(ct->getClass() == ccBuild) { - const BuildCommandType *bct= static_cast(ct); + if(ct->getClass() == ccBuild){ + const BuildCommandType *bct= static_cast (ct); //for each building - for(int k = 0; k < bct->getBuildingCount(); ++k) { + for(int k= 0; k < bct->getBuildingCount(); ++k){ const UnitType *building= bct->getBuilding(k); //if building match - if(bt->getUnitType() == building) { - if(aiInterface->reqsOk(bct)) { + if(bt->getUnitType() == building){ + if(aiInterface->reqsOk(bct)){ builders.push_back(i); buildersDefaultCommandType[i].push_back(bct); defBct= bct; @@ -1113,42 +1269,50 @@ void AiRuleBuild::buildSpecific(const BuildTask *bt) { } //use random builder to build - if(builders.empty() == false) { - int bIndex = ai->getRandom()->randRange(0, builders.size()-1); + if(builders.empty() == false){ + int bIndex= ai->getRandom()->randRange(0, builders.size() - 1); int builderIndex= builders[bIndex]; Vec2i pos; - Vec2i searchPos = bt->getForcePos()? bt->getPos(): ai->getRandomHomePosition(); - if(bt->getForcePos() == false) { - const int enemySightDistanceToAvoid = 18; + Vec2i searchPos= bt->getForcePos() ? bt->getPos() : ai->getRandomHomePosition(); + if(bt->getForcePos() == false){ + const int enemySightDistanceToAvoid= 18; vector enemies; - ai->getAiInterface()->getWorld()->getUnitUpdater()->findEnemiesForCell(searchPos,bt->getUnitType()->getSize(),enemySightDistanceToAvoid,ai->getAiInterface()->getMyFaction(),enemies,true); - if(enemies.size() > 0) { - for(int i1 = 0; i1 < 25 && enemies.size() > 0; ++i1) { - for(int j1 = 0; j1 < 25 && enemies.size() > 0; ++j1) { - Vec2i tryPos = searchPos + Vec2i(i1,j1); + ai->getAiInterface()->getWorld()->getUnitUpdater()->findEnemiesForCell(searchPos, + bt->getUnitType()->getSize(), enemySightDistanceToAvoid, ai->getAiInterface()->getMyFaction(), + enemies, true); + if(enemies.size() > 0){ + for(int i1= 0; i1 < 25 && enemies.size() > 0; ++i1){ + for(int j1= 0; j1 < 25 && enemies.size() > 0; ++j1){ + Vec2i tryPos= searchPos + Vec2i(i1, j1); - const int spacing = 1; - if(ai->getAiInterface()->isFreeCells(tryPos - Vec2i(spacing), bt->getUnitType()->getSize() + spacing * 2, fLand)) { + const int spacing= 1; + if(ai->getAiInterface()->isFreeCells(tryPos - Vec2i(spacing), bt->getUnitType()->getSize() + + spacing * 2, fLand)){ enemies.clear(); - ai->getAiInterface()->getWorld()->getUnitUpdater()->findEnemiesForCell(tryPos,bt->getUnitType()->getSize(),enemySightDistanceToAvoid,ai->getAiInterface()->getMyFaction(),enemies,true); - if(enemies.size() <= 0) { - searchPos = tryPos; + ai->getAiInterface()->getWorld()->getUnitUpdater()->findEnemiesForCell(tryPos, + bt->getUnitType()->getSize(), enemySightDistanceToAvoid, + ai->getAiInterface()->getMyFaction(), enemies, true); + if(enemies.size() <= 0){ + searchPos= tryPos; } } } } } - if(enemies.size() > 0) { - for(int i1 = -1; i1 >= -25 && enemies.size() > 0; --i1) { - for(int j1 = -1; j1 >= -25 && enemies.size() > 0; --j1) { - Vec2i tryPos = searchPos + Vec2i(i1,j1); + if(enemies.size() > 0){ + for(int i1= -1; i1 >= -25 && enemies.size() > 0; --i1){ + for(int j1= -1; j1 >= -25 && enemies.size() > 0; --j1){ + Vec2i tryPos= searchPos + Vec2i(i1, j1); - const int spacing = 1; - if(ai->getAiInterface()->isFreeCells(tryPos - Vec2i(spacing), bt->getUnitType()->getSize() + spacing * 2, fLand)) { + const int spacing= 1; + if(ai->getAiInterface()->isFreeCells(tryPos - Vec2i(spacing), bt->getUnitType()->getSize() + + spacing * 2, fLand)){ enemies.clear(); - ai->getAiInterface()->getWorld()->getUnitUpdater()->findEnemiesForCell(tryPos,bt->getUnitType()->getSize(),enemySightDistanceToAvoid,ai->getAiInterface()->getMyFaction(),enemies,true); - if(enemies.size() <= 0) { - searchPos = tryPos; + ai->getAiInterface()->getWorld()->getUnitUpdater()->findEnemiesForCell(tryPos, + bt->getUnitType()->getSize(), enemySightDistanceToAvoid, + ai->getAiInterface()->getMyFaction(), enemies, true); + if(enemies.size() <= 0){ + searchPos= tryPos; } } } @@ -1157,22 +1321,24 @@ void AiRuleBuild::buildSpecific(const BuildTask *bt) { } //if free pos give command, else retry - if(ai->findPosForBuilding(bt->getUnitType(), searchPos, pos)) { - defBct = NULL; - if(buildersDefaultCommandType.find(builderIndex) != buildersDefaultCommandType.end()) { + if(ai->findPosForBuilding(bt->getUnitType(), searchPos, pos)){ + defBct= NULL; + if(buildersDefaultCommandType.find(builderIndex) != buildersDefaultCommandType.end()){ //defBct = buildersDefaultCommandType[builderIndex]; - int bestCommandTypeCount = buildersDefaultCommandType[builderIndex].size(); - int bestCommandTypeIndex = ai->getRandom()->randRange(0, bestCommandTypeCount-1); + int bestCommandTypeCount= buildersDefaultCommandType[builderIndex].size(); + int bestCommandTypeIndex= ai->getRandom()->randRange(0, bestCommandTypeCount - 1); - if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] bestCommandTypeIndex = %d, bestCommandTypeCount = %d\n",__FILE__,__FUNCTION__,__LINE__,bestCommandTypeIndex,bestCommandTypeCount); + if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] bestCommandTypeIndex = %d, bestCommandTypeCount = %d\n",__FILE__,__FUNCTION__,__LINE__,bestCommandTypeIndex,bestCommandTypeCount); - defBct = buildersDefaultCommandType[builderIndex][bestCommandTypeIndex]; + defBct= buildersDefaultCommandType[builderIndex][bestCommandTypeIndex]; } - if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] builderIndex = %d, bIndex = %d, defBct = %p\n",__FILE__,__FUNCTION__,__LINE__,builderIndex,bIndex,defBct); + if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] builderIndex = %d, bIndex = %d, defBct = %p\n",__FILE__,__FUNCTION__,__LINE__,builderIndex,bIndex,defBct); aiInterface->giveCommand(builderIndex, defBct, pos, bt->getUnitType()); } - else { + else{ ai->retryTask(bt); return; } @@ -1181,19 +1347,23 @@ void AiRuleBuild::buildSpecific(const BuildTask *bt) { } bool AiRuleBuild::isDefensive(const UnitType *building){ - if(ai->outputAIBehaviourToConsole()) printf("BUILD isDefensive check for Unit Name[%s] result = %d\n",building->getName().c_str(),building->hasSkillClass(scAttack)); + if(ai->outputAIBehaviourToConsole()) + printf("BUILD isDefensive check for Unit Name[%s] result = %d\n", building->getName().c_str(), + building->hasSkillClass(scAttack)); return building->hasSkillClass(scAttack); } bool AiRuleBuild::isResourceProducer(const UnitType *building){ - for(int i= 0; igetCostCount(); i++){ - if(building->getCost(i)->getAmount()<0){ - if(ai->outputAIBehaviourToConsole()) printf("BUILD isResourceProducer check for Unit Name[%s] result = true\n",building->getName().c_str()); + for(int i= 0; i < building->getCostCount(); i++){ + if(building->getCost(i)->getAmount() < 0){ + if(ai->outputAIBehaviourToConsole()) + printf("BUILD isResourceProducer check for Unit Name[%s] result = true\n", building->getName().c_str()); return true; } } - if(ai->outputAIBehaviourToConsole()) printf("BUILD isResourceProducer check for Unit Name[%s] result = false\n",building->getName().c_str()); + if(ai->outputAIBehaviourToConsole()) + printf("BUILD isResourceProducer check for Unit Name[%s] result = false\n", building->getName().c_str()); return false; } @@ -1201,15 +1371,18 @@ bool AiRuleBuild::isWarriorProducer(const UnitType *building){ for(int i= 0; i < building->getCommandTypeCount(); i++){ const CommandType *ct= building->getCommandType(i); if(ct->getClass() == ccProduce){ - const UnitType *ut= static_cast(ct)->getProducedUnit(); + const UnitType *ut= static_cast (ct)->getProducedUnit(); if(ut->isOfClass(ucWarrior)){ - if(ai->outputAIBehaviourToConsole()) printf("BUILD isWarriorProducer check for Unit Name[%s] result = true\n",building->getName().c_str()); + if(ai->outputAIBehaviourToConsole()) + printf("BUILD isWarriorProducer check for Unit Name[%s] result = true\n", + building->getName().c_str()); return true; } } } - if(ai->outputAIBehaviourToConsole()) printf("BUILD isWarriorProducer check for Unit Name[%s] result = false\n",building->getName().c_str()); + if(ai->outputAIBehaviourToConsole()) + printf("BUILD isWarriorProducer check for Unit Name[%s] result = false\n", building->getName().c_str()); return false; } @@ -1217,27 +1390,26 @@ bool AiRuleBuild::isWarriorProducer(const UnitType *building){ // class AiRuleUpgrade // ======================================== -AiRuleUpgrade::AiRuleUpgrade(Ai *ai): - AiRule(ai) -{ +AiRuleUpgrade::AiRuleUpgrade(Ai *ai) : + AiRule(ai){ upgradeTask= NULL; } bool AiRuleUpgrade::test(){ const Task *task= ai->getTask(); - if(task==NULL || task->getClass()!=tcUpgrade){ + if(task == NULL || task->getClass() != tcUpgrade){ return false; } - upgradeTask= static_cast(task); + upgradeTask= static_cast (task); return true; } void AiRuleUpgrade::execute(){ //upgrade any upgrade - if(upgradeTask->getUpgradeType()==NULL){ + if(upgradeTask->getUpgradeType() == NULL){ upgradeGeneric(upgradeTask); } //upgrade specific upgrade @@ -1258,16 +1430,16 @@ void AiRuleUpgrade::upgradeGeneric(const UpgradeTask *upgt){ UpgradeTypes upgrades; //for each upgrade, upgrade it if possible - for(int i=0; igetMyUnitCount(); ++i){ + for(int i= 0; i < aiInterface->getMyUnitCount(); ++i){ //for each command const UnitType *ut= aiInterface->getMyUnit(i)->getType(); - for(int j=0; jgetCommandTypeCount(); ++j){ + for(int j= 0; j < ut->getCommandTypeCount(); ++j){ const CommandType *ct= ut->getCommandType(j); //if the command is upgrade - if(ct->getClass()==ccUpgrade){ - const UpgradeCommandType *upgct= static_cast(ct); + if(ct->getClass() == ccUpgrade){ + const UpgradeCommandType *upgct= static_cast (ct); const UpgradeType *upgrade= upgct->getProducedUpgrade(); if(aiInterface->reqsOk(upgct)){ upgrades.push_back(upgrade); @@ -1278,7 +1450,7 @@ void AiRuleUpgrade::upgradeGeneric(const UpgradeTask *upgt){ //add specific upgrade task if(!upgrades.empty()){ - ai->addTask(new UpgradeTask(upgrades[ai->getRandom()->randRange(0, upgrades.size()-1)])); + ai->addTask(new UpgradeTask(upgrades[ai->getRandom()->randRange(0, upgrades.size() - 1)])); } } @@ -1296,22 +1468,23 @@ void AiRuleUpgrade::upgradeSpecific(const UpgradeTask *upgt){ } //for each unit - for(int i=0; igetMyUnitCount(); ++i){ + for(int i= 0; i < aiInterface->getMyUnitCount(); ++i){ //for each command const UnitType *ut= aiInterface->getMyUnit(i)->getType(); - for(int j=0; jgetCommandTypeCount(); ++j){ + for(int j= 0; j < ut->getCommandTypeCount(); ++j){ const CommandType *ct= ut->getCommandType(j); //if the command is upgrade - if(ct->getClass()==ccUpgrade){ - const UpgradeCommandType *uct= static_cast(ct); + if(ct->getClass() == ccUpgrade){ + const UpgradeCommandType *uct= static_cast (ct); const UpgradeType *producedUpgrade= uct->getProducedUpgrade(); //if upgrades match if(producedUpgrade == upgt->getUpgradeType()){ if(aiInterface->reqsOk(uct)){ - if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); aiInterface->giveCommand(i, uct); } } @@ -1326,46 +1499,45 @@ void AiRuleUpgrade::upgradeSpecific(const UpgradeTask *upgt){ // class AiRuleExpand // ======================================== -AiRuleExpand::AiRuleExpand(Ai *ai): - AiRule(ai) -{ +AiRuleExpand::AiRuleExpand(Ai *ai) : + AiRule(ai){ storeType= NULL; } -bool AiRuleExpand::test() { - AiInterface *aiInterface = ai->getAiInterface(); +bool AiRuleExpand::test(){ + AiInterface *aiInterface= ai->getAiInterface(); - int unitCount = aiInterface->getMyUnitCount(); - for(int i = 0; i < aiInterface->getTechTree()->getResourceTypeCount(); ++i) { - const ResourceType *rt = aiInterface->getTechTree()->getResourceType(i); - if(rt->getClass() == rcTech) { - bool factionUsesResourceType = aiInterface->factionUsesResourceType(aiInterface->getMyFactionType(), rt); - if(factionUsesResourceType == true) { + int unitCount= aiInterface->getMyUnitCount(); + for(int i= 0; i < aiInterface->getTechTree()->getResourceTypeCount(); ++i){ + const ResourceType *rt= aiInterface->getTechTree()->getResourceType(i); + if(rt->getClass() == rcTech){ + bool factionUsesResourceType= aiInterface->factionUsesResourceType(aiInterface->getMyFactionType(), rt); + if(factionUsesResourceType == true){ // If any resource sighted - if(aiInterface->getNearestSightedResource(rt, aiInterface->getHomeLocation(), expandPos, true)) { + if(aiInterface->getNearestSightedResource(rt, aiInterface->getHomeLocation(), expandPos, true)){ int minDistance= INT_MAX; storeType= NULL; //If there is no close store - for(int j=0; j < unitCount; ++j) { + for(int j= 0; j < unitCount; ++j){ const Unit *u= aiInterface->getMyUnit(j); const UnitType *ut= u->getType(); // If this building is a store - if(ut->getStore(rt) > 0) { - storeType = ut; + if(ut->getStore(rt) > 0){ + storeType= ut; int distance= static_cast (u->getPos().dist(expandPos)); - if(distance < minDistance) { - minDistance = distance; + if(distance < minDistance){ + minDistance= distance; } } } - if(minDistance > expandDistance) { + if(minDistance > expandDistance){ return true; } } - else { + else{ // send patrol to look for resource ai->sendScoutPatrol(); } @@ -1381,18 +1553,16 @@ void AiRuleExpand::execute(){ ai->addPriorityTask(new BuildTask(storeType, expandPos)); } - // ======================================== // class AiRuleUnBlock // ======================================== -AiRuleUnBlock::AiRuleUnBlock(Ai *ai): - AiRule(ai) -{ +AiRuleUnBlock::AiRuleUnBlock(Ai *ai) : + AiRule(ai){ } -bool AiRuleUnBlock::test() { +bool AiRuleUnBlock::test(){ return ai->haveBlockedUnits(); } @@ -1400,4 +1570,5 @@ void AiRuleUnBlock::execute(){ ai->unblockUnits(); } -}}//end namespace +} +}//end namespace diff --git a/source/glest_game/ai/ai_rule.h b/source/glest_game/ai/ai_rule.h index 7ff782e8..217cc27f 100644 --- a/source/glest_game/ai/ai_rule.h +++ b/source/glest_game/ai/ai_rule.h @@ -112,6 +112,7 @@ public: class AiRuleRepair: public AiRule{ private: int damagedUnitIndex; + bool damagedUnitIsCastle; int getMinUnitsToRepairCastle(); double getMinCastleHpRatio(); diff --git a/source/glest_game/world/unit_updater.cpp b/source/glest_game/world/unit_updater.cpp index 5989c880..3b1ef916 100644 --- a/source/glest_game/world/unit_updater.cpp +++ b/source/glest_game/world/unit_updater.cpp @@ -599,10 +599,10 @@ void UnitUpdater::unitBeingAttacked(std::pair &result, const Unit * if(distToUnit < 0 || unit->getCenteredPos().dist(enemy->getCenteredPos()) < distToUnit) { distToUnit = unit->getCenteredPos().dist(enemy->getCenteredPos()); - result.first = true; - result.second = enemy; if( ast->getAttackRange()>=distToUnit) { + result.first = true; + result.second = enemy; break; } }