2010-03-24 22:26:17 +01:00
// ==============================================================
// This file is part of Glest (www.glest.org)
//
2011-12-14 08:40:48 +01:00
// Copyright (C) 2001-2008 Martiño Figueroa
2010-03-24 22:26:17 +01:00
//
2011-01-02 01:39:13 +01:00
// You can redistribute this code and/or modify it under
// the terms of the GNU General Public License as published
// by the Free Software Foundation; either version 2 of the
2010-03-24 22:26:17 +01:00
// License, or (at your option) any later version
// ==============================================================
# include "ai_rule.h"
# include <algorithm>
# include <limits.h>
# include "ai.h"
# include "ai_interface.h"
# include "unit.h"
# include "leak_dumper.h"
using Shared : : Graphics : : Vec2i ;
2011-06-24 18:14:39 +02:00
namespace Glest { namespace Game {
2010-03-24 22:26:17 +01:00
// =====================================================
// class AiRule
// =====================================================
2011-01-02 01:39:13 +01:00
AiRule : : AiRule ( Ai * ai ) {
2010-03-24 22:26:17 +01:00
this - > ai = ai ;
}
// =====================================================
// class AiRuleWorkerHarvest
// =====================================================
2011-06-24 18:14:39 +02:00
AiRuleWorkerHarvest : : AiRuleWorkerHarvest ( Ai * ai ) :
AiRule ( ai )
{
2010-03-24 22:26:17 +01:00
stoppedWorkerIndex = - 1 ;
}
bool AiRuleWorkerHarvest : : test ( ) {
return ai - > findAbleUnit ( & stoppedWorkerIndex , ccHarvest , true ) ;
}
void AiRuleWorkerHarvest : : execute ( ) {
ai - > harvest ( stoppedWorkerIndex ) ;
}
// =====================================================
// class AiRuleRefreshHarvester
// =====================================================
2011-06-24 18:14:39 +02:00
AiRuleRefreshHarvester : : AiRuleRefreshHarvester ( Ai * ai ) :
AiRule ( ai )
{
2010-03-24 22:26:17 +01:00
workerIndex = - 1 ;
}
bool AiRuleRefreshHarvester : : test ( ) {
return ai - > findAbleUnit ( & workerIndex , ccHarvest , ccHarvest ) ;
}
void AiRuleRefreshHarvester : : execute ( ) {
ai - > harvest ( workerIndex ) ;
}
// =====================================================
// class AiRuleScoutPatrol
// =====================================================
2011-06-24 18:14:39 +02:00
AiRuleScoutPatrol : : AiRuleScoutPatrol ( Ai * ai ) :
AiRule ( ai )
{
2010-03-24 22:26:17 +01:00
}
bool AiRuleScoutPatrol : : test ( ) {
return ai - > isStableBase ( ) ;
}
void AiRuleScoutPatrol : : execute ( ) {
ai - > sendScoutPatrol ( ) ;
}
// =====================================================
// class AiRuleRepair
// =====================================================
2011-06-24 18:14:39 +02:00
AiRuleRepair : : AiRuleRepair ( Ai * ai ) :
AiRule ( ai )
{
2011-09-01 01:10:43 +02:00
damagedUnitIndex = 0 ;
damagedUnitIsCastle = false ;
2010-03-24 22:26:17 +01:00
}
2011-09-01 01:10:43 +02:00
double AiRuleRepair : : getMinCastleHpRatio ( ) const {
2011-05-01 09:24:37 +02:00
return 0.6 ;
2011-05-01 07:36:04 +02:00
}
2011-06-24 18:14:39 +02:00
int AiRuleRepair : : getMinUnitsToRepairCastle ( ) {
int minUnitsRepairingCastle = 7 ;
2011-06-24 18:18:10 +02:00
if ( ai - > getCountOfClass ( ucWorker ) < = 6 ) {
minUnitsRepairingCastle = 1 ;
2011-05-01 07:36:04 +02:00
}
2011-06-24 18:18:10 +02:00
else if ( ai - > getCountOfClass ( ucWorker ) < = 8 ) {
minUnitsRepairingCastle = 2 ;
2011-05-01 07:36:04 +02:00
}
2011-06-24 18:18:10 +02:00
else if ( ai - > getCountOfClass ( ucWorker ) < = 10 ) {
minUnitsRepairingCastle = 3 ;
}
else if ( ai - > getCountOfClass ( ucWorker ) < = 12 ) {
minUnitsRepairingCastle = 5 ;
2011-05-01 07:36:04 +02:00
}
return minUnitsRepairingCastle ;
}
2010-03-24 22:26:17 +01:00
bool AiRuleRepair : : test ( ) {
AiInterface * aiInterface = ai - > getAiInterface ( ) ;
2011-06-24 18:14:39 +02:00
int minUnitsRepairingCastle = getMinUnitsToRepairCastle ( ) ;
const double minCastleHpRatio = getMinCastleHpRatio ( ) ;
2011-05-01 07:36:04 +02:00
2011-04-30 01:42:16 +02:00
// look for a damaged unit and give priority to the factions bases
// (units that produce workers and store resources)
2011-06-24 18:14:39 +02:00
for ( int i = 0 ; i < aiInterface - > getMyUnitCount ( ) ; + + i ) {
2011-04-30 01:42:16 +02:00
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()));
2011-06-24 18:14:39 +02:00
if ( u - > getHpRatio ( ) < 1.f ) {
2011-05-01 07:36:04 +02:00
//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()));
2011-04-30 01:42:16 +02:00
2011-06-24 18:14:39 +02:00
bool unitCanProduceWorker = false ;
for ( int j = 0 ; unitCanProduceWorker = = false & &
j < u - > getType ( ) - > getCommandTypeCount ( ) ; + + j ) {
2011-04-30 01:42:16 +02:00
const CommandType * ct = u - > getType ( ) - > getCommandType ( j ) ;
//if the command is produce
2011-06-24 18:14:39 +02:00
if ( ct - > getClass ( ) = = ccProduce | | ct - > getClass ( ) = = ccMorph ) {
const ProducibleType * pt = ct - > getProduced ( ) ;
if ( pt ! = NULL ) {
const UnitType * ut = dynamic_cast < const UnitType * > ( pt ) ;
if ( ut ! = NULL & & ut - > hasCommandClass ( ccHarvest ) = = true & &
u - > getType ( ) - > getStoredResourceCount ( ) > 0 ) {
2011-05-01 07:36:04 +02:00
//printf("\n\n\n\n!!!!!! found candidate castle unit to repair [%d - %s]\n",u->getId(),u->getType()->getName().c_str());
2011-06-24 18:14:39 +02:00
unitCanProduceWorker = true ;
2011-04-30 01:42:16 +02:00
}
}
}
}
2011-06-24 18:14:39 +02:00
if ( unitCanProduceWorker = = true ) {
2011-09-01 01:10:43 +02:00
int candidatedamagedUnitIndex = - 1 ;
2011-06-24 18:14:39 +02:00
int unitCountAlreadyRepairingDamagedUnit = 0 ;
2011-04-30 01:42:16 +02:00
// Now check if any other unit is able to repair this unit
2011-06-24 18:14:39 +02:00
for ( int i1 = 0 ; i1 < aiInterface - > getMyUnitCount ( ) ; + + i1 ) {
2011-04-30 01:42:16 +02:00
const Unit * u1 = aiInterface - > getMyUnit ( i1 ) ;
2011-06-24 18:14:39 +02:00
const RepairCommandType * rct = static_cast < const RepairCommandType * > ( u1 - > getType ( ) - > getFirstCtOfClass ( ccRepair ) ) ;
2011-04-30 01:42:16 +02:00
//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()));
2011-06-24 18:14:39 +02:00
if ( rct ! = NULL ) {
2011-05-01 07:36:04 +02:00
//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());
2011-06-24 18:14:39 +02:00
if ( u1 - > getCurrSkill ( ) - > getClass ( ) = = scStop | | u1 - > getCurrSkill ( ) - > getClass ( ) = = scMove | |
u - > getHpRatio ( ) < = minCastleHpRatio ) {
if ( rct - > isRepairableUnitType ( u - > getType ( ) ) ) {
2011-04-30 01:42:16 +02:00
candidatedamagedUnitIndex = i ;
//return true;
}
}
2011-06-24 18:14:39 +02:00
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 ( ) ) {
2011-05-01 07:36:04 +02:00
//printf("\n\n\n\n^^^^^^^^^^ unit is ALREADY repairer unit [%d - %s]\n",u1->getId(),u1->getType()->getName().c_str());
2011-04-30 01:42:16 +02:00
unitCountAlreadyRepairingDamagedUnit + + ;
}
}
}
}
}
2011-06-24 18:14:39 +02:00
if ( candidatedamagedUnitIndex > = 0 & & unitCountAlreadyRepairingDamagedUnit < minUnitsRepairingCastle ) {
2011-05-01 07:36:04 +02:00
//printf("\n\n\n\n^^^^^^^^^^ AI test will repair damaged unit [%d - %s]\n",u->getId(),u->getType()->getName().c_str());
2011-06-24 18:14:39 +02:00
damagedUnitIndex = candidatedamagedUnitIndex ;
2011-06-24 18:18:10 +02:00
damagedUnitIsCastle = true ;
2011-04-30 01:42:16 +02:00
return true ;
}
}
}
}
2011-06-24 18:18:10 +02:00
damagedUnitIsCastle = false ;
damagedUnitIndex = - 1 ;
2011-04-30 01:42:16 +02:00
// Normal Repair checking
2011-06-24 18:14:39 +02:00
for ( int i = 0 ; i < aiInterface - > getMyUnitCount ( ) ; + + i ) {
2010-03-24 22:26:17 +01:00
const Unit * u = aiInterface - > getMyUnit ( i ) ;
2011-04-29 07:37:35 +02:00
//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()));
2011-06-24 18:14:39 +02:00
if ( u - > getHpRatio ( ) < 1.f ) {
2011-04-29 07:37:35 +02:00
// Now check if any other unit is able to repair this unit
2011-06-24 18:14:39 +02:00
for ( int i1 = 0 ; i1 < aiInterface - > getMyUnitCount ( ) ; + + i1 ) {
2011-04-29 07:37:35 +02:00
const Unit * u1 = aiInterface - > getMyUnit ( i1 ) ;
2011-06-24 18:14:39 +02:00
const RepairCommandType * rct = static_cast < const RepairCommandType * > ( u1 - > getType ( ) - > getFirstCtOfClass ( ccRepair ) ) ;
2011-04-29 07:37:35 +02:00
//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()));
2011-06-24 18:14:39 +02:00
if ( rct ! = NULL & & ( u1 - > getCurrSkill ( ) - > getClass ( ) = = scStop | | u1 - > getCurrSkill ( ) - > getClass ( ) = = scMove ) ) {
if ( rct - > isRepairableUnitType ( u - > getType ( ) ) ) {
2011-04-29 07:37:35 +02:00
damagedUnitIndex = i ;
2011-06-24 18:18:10 +02:00
//random if return or not so we get different targets from time to time
if ( ai - > getRandom ( ) - > randRange ( 0 , 1 ) = = 0 )
2011-04-29 07:37:35 +02:00
return true ;
}
}
}
2010-03-24 22:26:17 +01:00
}
}
2011-06-24 18:18:10 +02:00
if ( damagedUnitIndex ! = - 1 )
{
return true ;
}
2010-03-24 22:26:17 +01:00
return false ;
}
2011-06-24 18:14:39 +02:00
void AiRuleRepair : : execute ( ) {
2010-03-24 22:26:17 +01:00
AiInterface * aiInterface = ai - > getAiInterface ( ) ;
const Unit * damagedUnit = aiInterface - > getMyUnit ( damagedUnitIndex ) ;
2011-04-29 07:37:35 +02:00
//printf("\n\n\n\n###^^^^^^^^^^ Looking for repairer for damaged unit [%d - %s]\n",damagedUnit->getId(),damagedUnit->getType()->getName().c_str());
2011-01-02 01:39:13 +01:00
2011-06-24 18:14:39 +02:00
int minUnitsRepairingCastle = getMinUnitsToRepairCastle ( ) ;
const double minCastleHpRatio = getMinCastleHpRatio ( ) ;
2011-05-01 07:36:04 +02:00
2011-06-24 18:18:10 +02:00
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 ;
}
}
2011-06-24 18:14:39 +02:00
int unitCountAlreadyRepairingDamagedUnit = 0 ;
2011-06-24 18:18:10 +02:00
//printf("team %d has damaged unit\n", damagedUnit->getTeam());
2011-05-01 08:02:49 +02:00
// Now check if any other unit is able to repair this unit
2011-06-24 18:14:39 +02:00
for ( int i1 = 0 ; i1 < aiInterface - > getMyUnitCount ( ) ; + + i1 ) {
2011-05-01 08:02:49 +02:00
const Unit * u1 = aiInterface - > getMyUnit ( i1 ) ;
2011-06-24 18:14:39 +02:00
const RepairCommandType * rct = static_cast < const RepairCommandType * > ( u1 - > getType ( ) - > getFirstCtOfClass ( ccRepair ) ) ;
2011-06-24 18:18:10 +02:00
//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()));
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 ( 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 ( ( ( 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 + + ;
2011-05-01 08:02:49 +02:00
}
}
}
}
2011-06-24 18:18:10 +02:00
if ( unitCountAlreadyRepairingDamagedUnit > = minUnitsRepairingCastle ) {
return ;
}
2011-07-05 06:37:35 +02:00
int unitGroupCommandId = - 1 ;
2010-03-24 22:26:17 +01:00
//find a repairer and issue command
2011-06-24 18:14:39 +02:00
for ( int i = 0 ; i < aiInterface - > getMyUnitCount ( ) ; + + i ) {
2010-03-24 22:26:17 +01:00
const Unit * u = aiInterface - > getMyUnit ( i ) ;
2011-06-24 18:14:39 +02:00
const RepairCommandType * rct = static_cast < const RepairCommandType * > ( u - > getType ( ) - > getFirstCtOfClass ( ccRepair ) ) ;
2011-04-29 07:37:35 +02:00
//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()));
2010-06-08 09:40:32 +02:00
2011-06-24 18:14:39 +02:00
if ( rct ! = NULL ) {
2011-05-01 07:36:04 +02:00
//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());
2011-06-24 18:14:39 +02:00
if ( ( u - > getCurrSkill ( ) - > getClass ( ) = = scStop | | u - > getCurrSkill ( ) - > getClass ( ) = = scMove | | damagedUnit - > getHpRatio ( ) < = minCastleHpRatio ) ) {
2011-06-24 18:18:10 +02:00
if ( ( u - > getCurrCommand ( ) = = NULL | | ( u - > getCurrCommand ( ) - > getCommandType ( ) - > getClass ( ) ! = ccBuild
& & u - > getCurrCommand ( ) - > getCommandType ( ) - > getClass ( ) ! = ccProduce ) )
& & rct - > isRepairableUnitType ( damagedUnit - > getType ( ) ) ) {
2011-05-01 07:36:04 +02:00
//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());
/*
2011-06-24 18:14:39 +02:00
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 ( ) ) ;
}
}
*/
2011-05-01 07:36:04 +02:00
//aiInterface->giveCommand(i, rct, damagedUnit->getPos());
2011-06-24 18:14:39 +02:00
if ( unitCountAlreadyRepairingDamagedUnit < minUnitsRepairingCastle ) {
2011-07-05 06:37:35 +02:00
if ( unitGroupCommandId = = - 1 ) {
unitGroupCommandId = aiInterface - > getWorld ( ) - > getNextCommandGroupId ( ) ;
}
aiInterface - > giveCommand ( i , rct , damagedUnit - > getPosWithCellMapSet ( ) , unitGroupCommandId ) ;
2013-01-12 02:11:53 +01:00
if ( aiInterface - > isLogLevelEnabled ( 3 ) = = true ) aiInterface - > printLog ( 3 , " Repairing order issued " ) ;
2011-05-01 09:24:37 +02:00
unitCountAlreadyRepairingDamagedUnit + + ;
2011-06-24 18:18:10 +02:00
// printf(
// "^^^^^^^^^^adding one unit to repair ... unitCountAlreadyRepairingDamagedUnit/minUnitsRepairingCastle=%d/%d\n",
// unitCountAlreadyRepairingDamagedUnit, minUnitsRepairingCastle);
2011-05-01 09:24:37 +02:00
}
2011-05-01 08:02:49 +02:00
2011-06-24 18:18:10 +02:00
if ( ! damagedUnitIsCastle | | unitCountAlreadyRepairingDamagedUnit > = minUnitsRepairingCastle ) {
2011-05-01 08:02:49 +02:00
return ;
}
2011-05-01 07:36:04 +02:00
}
2010-03-24 22:26:17 +01:00
}
}
}
}
// =====================================================
// class AiRuleReturnBase
// =====================================================
2011-06-24 18:14:39 +02:00
AiRuleReturnBase : : AiRuleReturnBase ( Ai * ai ) :
AiRule ( ai )
{
2010-03-24 22:26:17 +01:00
stoppedUnitIndex = - 1 ;
}
bool AiRuleReturnBase : : test ( ) {
return ai - > findAbleUnit ( & stoppedUnitIndex , ccMove , true ) ;
}
void AiRuleReturnBase : : execute ( ) {
ai - > returnBase ( stoppedUnitIndex ) ;
}
// =====================================================
// class AiRuleMassiveAttack
// =====================================================
2011-06-24 18:14:39 +02:00
AiRuleMassiveAttack : : AiRuleMassiveAttack ( Ai * ai ) :
AiRule ( ai )
{
2011-09-01 01:10:43 +02:00
ultraAttack = false ;
2012-10-06 05:42:31 +02:00
field = fLand ;
2010-03-24 22:26:17 +01:00
}
bool AiRuleMassiveAttack : : test ( ) {
2011-01-02 01:39:13 +01:00
2010-03-24 22:26:17 +01:00
if ( ai - > isStableBase ( ) ) {
ultraAttack = false ;
return ai - > beingAttacked ( attackPos , field , INT_MAX ) ;
}
else {
ultraAttack = true ;
return ai - > beingAttacked ( attackPos , field , baseRadius ) ;
2011-01-02 01:39:13 +01:00
}
2010-03-24 22:26:17 +01:00
}
void AiRuleMassiveAttack : : execute ( ) {
ai - > massiveAttack ( attackPos , field , ultraAttack ) ;
}
// =====================================================
// class AiRuleAddTasks
// =====================================================
2011-06-24 18:14:39 +02:00
AiRuleAddTasks : : AiRuleAddTasks ( Ai * ai ) :
AiRule ( ai )
{
2010-03-24 22:26:17 +01:00
}
bool AiRuleAddTasks : : test ( ) {
2011-04-30 01:42:16 +02:00
return ! ai - > anyTask ( ) | | ai - > getCountOfClass ( ucWorker ) < 4 ;
2010-03-24 22:26:17 +01:00
}
void AiRuleAddTasks : : execute ( ) {
int buildingCount = ai - > getCountOfClass ( ucBuilding ) ;
2011-06-24 18:14:39 +02:00
UnitClass ucWorkerType = ucWorker ;
int warriorCount = ai - > getCountOfClass ( ucWarrior , & ucWorkerType ) ;
2010-03-24 22:26:17 +01:00
int workerCount = ai - > getCountOfClass ( ucWorker ) ;
int upgradeCount = ai - > getAiInterface ( ) - > getMyUpgradeCount ( ) ;
float buildingRatio = ai - > getRatioOfClass ( ucBuilding ) ;
float warriorRatio = ai - > getRatioOfClass ( ucWarrior ) ;
float workerRatio = ai - > getRatioOfClass ( ucWorker ) ;
//standard tasks
2011-06-24 18:14:39 +02:00
if ( ai - > outputAIBehaviourToConsole ( ) ) printf ( " Add a TASK - AiRuleAddTasks adding ProduceTask(ucWorker) workerCount = %d, RULE Name[%s] \n " , workerCount , this - > getName ( ) . c_str ( ) ) ;
2010-03-24 22:26:17 +01:00
//emergency workers
2011-04-06 17:44:33 +02:00
if ( workerCount < 4 ) {
2011-06-24 18:14:39 +02:00
if ( ai - > outputAIBehaviourToConsole ( ) ) printf ( " AAA AiRuleAddTasks adding ProduceTask(ucWorker) workerCount = %d, RULE Name[%s] \n " , workerCount , this - > getName ( ) . c_str ( ) ) ;
2012-03-17 20:22:03 +01:00
2010-03-24 22:26:17 +01:00
ai - > addPriorityTask ( new ProduceTask ( ucWorker ) ) ;
}
else {
2011-06-24 18:14:39 +02:00
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 ( ) ) ;
2011-04-06 17:44:33 +02:00
2010-03-24 22:26:17 +01:00
//workers
2011-06-24 18:14:39 +02:00
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 ) ) ;
2011-01-02 01:39:13 +01:00
2010-03-24 22:26:17 +01:00
//warriors
2011-06-24 18:14:39 +02:00
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 ) ) ;
2012-03-31 07:54:24 +02:00
if ( warriorCount < ai - > getMinWarriors ( ) + 2 )
2011-06-24 18:14:39 +02:00
{
2010-03-24 22:26:17 +01:00
ai - > addTask ( new ProduceTask ( ucWarrior ) ) ;
2011-06-24 18:14:39 +02:00
if ( buildingCount > 9 )
{
2010-03-24 22:26:17 +01:00
ai - > addTask ( new ProduceTask ( ucWarrior ) ) ;
ai - > addTask ( new ProduceTask ( ucWarrior ) ) ;
}
2011-06-24 18:14:39 +02:00
if ( buildingCount > 12 )
{
2010-03-24 22:26:17 +01:00
ai - > addTask ( new ProduceTask ( ucWarrior ) ) ;
ai - > addTask ( new ProduceTask ( ucWarrior ) ) ;
}
}
2011-01-02 01:39:13 +01:00
2010-03-24 22:26:17 +01:00
//buildings
2012-03-13 16:21:25 +01:00
if ( buildingCount < 6 | | buildingRatio < 0.20 ) ai - > addTask ( new BuildTask ( ( UnitType * ) NULL ) ) ;
if ( buildingCount < 10 & & workerCount > 12 ) ai - > addTask ( new BuildTask ( ( UnitType * ) NULL ) ) ;
2010-03-24 22:26:17 +01:00
//upgrades
2012-03-13 16:21:25 +01:00
if ( upgradeCount = = 0 & & workerCount > 5 ) ai - > addTask ( new UpgradeTask ( ( const UpgradeType * ) NULL ) ) ;
if ( upgradeCount = = 1 & & workerCount > 10 ) ai - > addTask ( new UpgradeTask ( ( const UpgradeType * ) NULL ) ) ;
if ( upgradeCount = = 2 & & workerCount > 15 ) ai - > addTask ( new UpgradeTask ( ( const UpgradeType * ) NULL ) ) ;
if ( ai - > isStableBase ( ) ) ai - > addTask ( new UpgradeTask ( ( const UpgradeType * ) NULL ) ) ;
2010-03-24 22:26:17 +01:00
}
2011-06-24 18:14:39 +02:00
else if ( ai - > getAiInterface ( ) - > getControlType ( ) = = ctCpuEasy | |
ai - > getAiInterface ( ) - > getControlType ( ) = = ctNetworkCpuEasy )
{ // Easy CPU
2011-04-06 17:44:33 +02:00
2011-06-24 18:14:39 +02:00
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 ( ) ) ;
2011-04-06 17:44:33 +02:00
2010-03-24 22:26:17 +01:00
//workers
2011-06-24 18:14:39 +02:00
if ( workerCount < buildingCount + 2 ) ai - > addTask ( new ProduceTask ( ucWorker ) ) ;
if ( workerCount > 5 & & workerRatio < 0.20 ) ai - > addTask ( new ProduceTask ( ucWorker ) ) ;
2011-01-02 01:39:13 +01:00
2010-03-24 22:26:17 +01:00
//warriors
2011-06-24 18:14:39 +02:00
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 ) ) ;
2011-01-02 01:39:13 +01:00
2010-03-24 22:26:17 +01:00
//buildings
2012-03-13 16:21:25 +01:00
if ( buildingCount < 6 | | buildingRatio < 0.20 ) ai - > addTask ( new BuildTask ( ( UnitType * ) NULL ) ) ;
if ( buildingCount < 10 & & ai - > isStableBase ( ) ) ai - > addTask ( new BuildTask ( ( UnitType * ) NULL ) ) ;
2011-01-02 01:39:13 +01:00
2010-03-24 22:26:17 +01:00
//upgrades
2012-03-13 16:21:25 +01:00
if ( upgradeCount = = 0 & & workerCount > 6 ) ai - > addTask ( new UpgradeTask ( ( const UpgradeType * ) NULL ) ) ;
if ( upgradeCount = = 1 & & workerCount > 7 ) ai - > addTask ( new UpgradeTask ( ( const UpgradeType * ) NULL ) ) ;
if ( upgradeCount = = 2 & & workerCount > 9 ) ai - > addTask ( new UpgradeTask ( ( const UpgradeType * ) NULL ) ) ;
2010-03-24 22:26:17 +01:00
//if(ai->isStableBase()) ai->addTask(new UpgradeTask());
}
2011-06-24 18:14:39 +02:00
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 ( ) ) ;
2011-04-06 17:44:33 +02:00
2010-03-24 22:26:17 +01:00
//workers
2011-06-24 18:14:39 +02:00
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 ) ) ;
2011-01-02 01:39:13 +01:00
2010-03-24 22:26:17 +01:00
//warriors
2011-06-24 18:14:39 +02:00
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 ) ) ;
2011-01-02 01:39:13 +01:00
2010-03-24 22:26:17 +01:00
//buildings
2012-03-13 16:21:25 +01:00
if ( buildingCount < 6 | | buildingRatio < 0.20 ) ai - > addTask ( new BuildTask ( ( UnitType * ) NULL ) ) ;
if ( buildingCount < 10 & & workerCount > 12 ) ai - > addTask ( new BuildTask ( ( UnitType * ) NULL ) ) ;
2011-01-02 01:39:13 +01:00
2010-03-24 22:26:17 +01:00
//upgrades
2012-03-13 16:21:25 +01:00
if ( upgradeCount = = 0 & & workerCount > 5 ) ai - > addTask ( new UpgradeTask ( ( const UpgradeType * ) NULL ) ) ;
if ( upgradeCount = = 1 & & workerCount > 10 ) ai - > addTask ( new UpgradeTask ( ( const UpgradeType * ) NULL ) ) ;
if ( upgradeCount = = 2 & & workerCount > 15 ) ai - > addTask ( new UpgradeTask ( ( const UpgradeType * ) NULL ) ) ;
if ( ai - > isStableBase ( ) ) ai - > addTask ( new UpgradeTask ( ( const UpgradeType * ) NULL ) ) ;
2010-03-24 22:26:17 +01:00
}
}
}
// =====================================================
// class AiRuleBuildOneFarm
// =====================================================
2011-06-24 18:14:39 +02:00
AiRuleBuildOneFarm : : AiRuleBuildOneFarm ( Ai * ai ) :
AiRule ( ai )
{
2011-09-01 01:10:43 +02:00
farm = NULL ;
2010-03-24 22:26:17 +01:00
}
bool AiRuleBuildOneFarm : : test ( ) {
AiInterface * aiInterface = ai - > getAiInterface ( ) ;
//for all units
2011-06-24 18:14:39 +02:00
for ( int i = 0 ; i < aiInterface - > getMyFactionType ( ) - > getUnitTypeCount ( ) ; + + i ) {
2010-03-24 22:26:17 +01:00
const UnitType * ut = aiInterface - > getMyFactionType ( ) - > getUnitType ( i ) ;
2011-01-02 01:39:13 +01:00
2010-03-24 22:26:17 +01:00
//for all production commands
2011-06-24 18:14:39 +02:00
for ( int j = 0 ; j < ut - > getCommandTypeCount ( ) ; + + j ) {
2010-03-24 22:26:17 +01:00
const CommandType * ct = ut - > getCommandType ( j ) ;
2011-06-24 18:14:39 +02:00
if ( ct - > getClass ( ) = = ccProduce ) {
const UnitType * producedType = static_cast < const ProduceCommandType * > ( ct ) - > getProducedUnit ( ) ;
2011-01-02 01:39:13 +01:00
2010-03-24 22:26:17 +01:00
//for all resources
2011-06-24 18:14:39 +02:00
for ( int k = 0 ; k < producedType - > getCostCount ( ) ; + + k ) {
2010-03-24 22:26:17 +01:00
const Resource * r = producedType - > getCost ( k ) ;
//find a food producer in the farm produced units
2011-10-04 08:49:44 +02:00
if ( r - > getAmount ( ) < 0 & & r - > getType ( ) - > getClass ( ) = = rcConsumable & & ai - > getCountOfType ( ut ) = = 0 ) {
if ( aiInterface - > reqsOk ( ct ) & & aiInterface - > getMyFaction ( ) - > canCreateUnit ( ut , true , true , true ) = = true ) {
farm = ut ;
//printf("AiRuleBuildOneFarm returning true, RULE Name[%s] ut [%s] producedType [%s]\n",this->getName().c_str(),ut->getName().c_str(),producedType->getName().c_str());
2011-04-06 17:44:33 +02:00
2011-10-04 08:49:44 +02:00
if ( ai - > outputAIBehaviourToConsole ( ) ) printf ( " AiRuleBuildOneFarm returning true, RULE Name[%s] \n " , this - > getName ( ) . c_str ( ) ) ;
2012-03-17 20:22:03 +01:00
2011-10-04 08:49:44 +02:00
return true ;
}
2010-03-24 22:26:17 +01:00
}
}
}
}
}
return false ;
}
void AiRuleBuildOneFarm : : execute ( ) {
ai - > addPriorityTask ( new BuildTask ( farm ) ) ;
}
// =====================================================
// class AiRuleProduceResourceProducer
// =====================================================
2011-06-24 18:14:39 +02:00
AiRuleProduceResourceProducer : : AiRuleProduceResourceProducer ( Ai * ai ) :
AiRule ( ai )
{
2010-03-24 22:26:17 +01:00
interval = shortInterval ;
2011-09-01 01:10:43 +02:00
rt = NULL ;
2013-09-19 00:44:04 +02:00
newResourceBehaviour = Config : : getInstance ( ) . getBool ( " NewResourceBehaviour " , " false " ) ; ;
2010-03-24 22:26:17 +01:00
}
bool AiRuleProduceResourceProducer : : test ( ) {
//emergency tasks: resource buildings
AiInterface * aiInterface = ai - > getAiInterface ( ) ;
//consumables first
2011-06-24 18:14:39 +02:00
for ( int i = 0 ; i < aiInterface - > getTechTree ( ) - > getResourceTypeCount ( ) ; + + i ) {
rt = aiInterface - > getTechTree ( ) - > getResourceType ( i ) ;
2011-01-02 01:39:13 +01:00
const Resource * r = aiInterface - > getResource ( rt ) ;
2011-04-06 17:44:33 +02:00
2011-06-24 18:14:39 +02:00
if ( ai - > outputAIBehaviourToConsole ( ) ) printf ( " CONSUMABLE [%s][%d] Testing AI RULE Name[%s] \n " , rt - > getName ( ) . c_str ( ) , r - > getBalance ( ) , this - > getName ( ) . c_str ( ) ) ;
2011-04-06 17:44:33 +02:00
2013-01-12 02:11:53 +01:00
if ( aiInterface - > isLogLevelEnabled ( 4 ) = = true ) {
char szBuf [ 8096 ] = " " ;
snprintf ( szBuf , 8096 , " CONSUMABLE [%s][%d] Testing AI RULE Name[%s] " , rt - > getName ( ) . c_str ( ) , r - > getBalance ( ) , this - > getName ( ) . c_str ( ) ) ;
aiInterface - > printLog ( 4 , szBuf ) ;
}
2011-01-02 01:39:13 +01:00
2013-01-12 02:11:53 +01:00
bool factionUsesResourceType = aiInterface - > factionUsesResourceType ( aiInterface - > getMyFactionType ( ) , rt ) ;
if ( factionUsesResourceType = = true & & rt - > getClass ( ) = = rcConsumable ) {
// The consumable balance is negative
if ( r - > getBalance ( ) < 0 ) {
2013-12-14 08:04:12 +01:00
if ( newResourceBehaviour = = true ) {
2013-08-30 00:30:09 +02:00
interval = shortInterval ;
2013-12-14 08:04:12 +01:00
}
else {
2013-08-30 00:30:09 +02:00
interval = longInterval ;
2013-12-14 08:04:12 +01:00
}
2013-01-12 02:11:53 +01:00
return true ;
}
// If the consumable balance is down to 1/3 of what we need
else {
if ( r - > getBalance ( ) * 3 + r - > getAmount ( ) < 0 ) {
2013-12-14 08:04:12 +01:00
if ( newResourceBehaviour = = true ) {
interval = shortInterval ;
}
else {
interval = longInterval ;
}
2013-01-12 02:11:53 +01:00
return true ;
}
}
2013-12-14 08:04:12 +01:00
}
}
2010-03-24 22:26:17 +01:00
2012-03-17 20:22:03 +01:00
int targetStaticResourceCount = minStaticResources ;
2012-03-31 07:54:24 +02:00
if ( aiInterface - > getMyFactionType ( ) - > getAIBehaviorStaticOverideValue ( aibsvcMinStaticResourceCount ) ! = INT_MAX ) {
targetStaticResourceCount = aiInterface - > getMyFactionType ( ) - > getAIBehaviorStaticOverideValue ( aibsvcMinStaticResourceCount ) ;
2012-03-17 20:22:03 +01:00
}
2010-03-24 22:26:17 +01:00
//statics second
2011-06-24 18:14:39 +02:00
for ( int i = 0 ; i < aiInterface - > getTechTree ( ) - > getResourceTypeCount ( ) ; + + i ) {
rt = aiInterface - > getTechTree ( ) - > getResourceType ( i ) ;
2011-01-02 01:39:13 +01:00
const Resource * r = aiInterface - > getResource ( rt ) ;
2012-03-17 20:22:03 +01:00
if ( ai - > outputAIBehaviourToConsole ( ) ) printf ( " STATIC [%s][%d] [min %d] Testing AI RULE Name[%s] \n " , rt - > getName ( ) . c_str ( ) , r - > getAmount ( ) , targetStaticResourceCount , this - > getName ( ) . c_str ( ) ) ;
2013-01-12 02:11:53 +01:00
if ( aiInterface - > isLogLevelEnabled ( 4 ) = = true ) {
char szBuf [ 8096 ] = " " ;
snprintf ( szBuf , 8096 , " STATIC resource check [%s][%d] [min %d] Testing AI RULE Name[%s] " , rt - > getName ( ) . c_str ( ) , r - > getAmount ( ) , targetStaticResourceCount , this - > getName ( ) . c_str ( ) ) ;
aiInterface - > printLog ( 4 , szBuf ) ;
}
2011-04-06 17:44:33 +02:00
2012-03-17 20:22:03 +01:00
if ( rt - > getClass ( ) = = rcStatic & & r - > getAmount ( ) < targetStaticResourceCount ) {
2011-06-24 18:14:39 +02:00
bool factionUsesResourceType = aiInterface - > factionUsesResourceType ( aiInterface - > getMyFactionType ( ) , rt ) ;
if ( factionUsesResourceType = = true ) {
2013-08-30 00:30:09 +02:00
if ( newResourceBehaviour = = true )
interval = shortInterval ;
else
interval = longInterval ;
2011-04-06 17:44:33 +02:00
return true ;
}
2011-06-24 18:14:39 +02:00
}
}
2010-03-24 22:26:17 +01:00
2011-06-24 18:14:39 +02:00
if ( ai - > outputAIBehaviourToConsole ( ) ) printf ( " STATIC returning FALSE \n " ) ;
2013-01-12 02:11:53 +01:00
if ( aiInterface - > isLogLevelEnabled ( 4 ) = = true ) aiInterface - > printLog ( 4 , " Static Resource check returning FALSE " ) ;
2011-04-06 17:44:33 +02:00
2013-08-30 00:30:09 +02:00
if ( newResourceBehaviour = = true )
interval = longInterval ;
else
interval = shortInterval ;
2010-03-24 22:26:17 +01:00
return false ;
}
void AiRuleProduceResourceProducer : : execute ( ) {
ai - > addPriorityTask ( new ProduceTask ( rt ) ) ;
ai - > addTask ( new BuildTask ( rt ) ) ;
}
// =====================================================
// class AiRuleProduce
// =====================================================
2011-06-24 18:14:39 +02:00
AiRuleProduce : : AiRuleProduce ( Ai * ai ) :
AiRule ( ai )
{
2010-03-24 22:26:17 +01:00
produceTask = NULL ;
2013-08-30 00:30:09 +02:00
newResourceBehaviour = Config : : getInstance ( ) . getBool ( " NewResourceBehaviour " , " false " ) ;
2010-03-24 22:26:17 +01:00
}
bool AiRuleProduce : : test ( ) {
const Task * task = ai - > getTask ( ) ;
2011-06-24 18:14:39 +02:00
if ( task = = NULL | | task - > getClass ( ) ! = tcProduce ) {
2010-03-24 22:26:17 +01:00
return false ;
}
2011-06-24 18:14:39 +02:00
produceTask = static_cast < const ProduceTask * > ( task ) ;
2010-03-24 22:26:17 +01:00
return true ;
}
2012-03-17 20:22:03 +01:00
void AiRuleProduce : : execute ( ) {
AiInterface * aiInterface = ai - > getAiInterface ( ) ;
if ( produceTask ! = NULL ) {
2011-01-02 01:39:13 +01:00
2013-06-13 10:55:48 +02:00
if ( ai - > outputAIBehaviourToConsole ( ) ) printf ( " AiRuleProduce producing [%s] \n " , ( produceTask - > getUnitType ( ) ! = NULL ? produceTask - > getUnitType ( ) - > getName ( false ) . c_str ( ) : " null " ) ) ;
2013-01-12 02:11:53 +01:00
if ( aiInterface - > isLogLevelEnabled ( 4 ) = = true ) {
char szBuf [ 8096 ] = " " ;
2013-06-13 10:55:48 +02:00
snprintf ( szBuf , 8096 , " AiRuleProduce producing [%s] " , ( produceTask - > getUnitType ( ) ! = NULL ? produceTask - > getUnitType ( ) - > getName ( false ) . c_str ( ) : " null " ) ) ;
2013-01-12 02:11:53 +01:00
aiInterface - > printLog ( 4 , szBuf ) ;
}
2011-04-06 17:44:33 +02:00
2010-03-24 22:26:17 +01:00
//generic produce task, produce random unit that has the skill or produces the resource
2012-03-17 20:22:03 +01:00
if ( produceTask - > getUnitType ( ) = = NULL ) {
2013-08-30 00:30:09 +02:00
if ( newResourceBehaviour ) {
produceGenericNew ( produceTask ) ;
}
else
produceGeneric ( produceTask ) ;
2010-03-24 22:26:17 +01:00
}
//specific produce task, produce if possible, retry if not enough resources
2012-03-17 20:22:03 +01:00
else {
2010-03-24 22:26:17 +01:00
produceSpecific ( produceTask ) ;
}
//remove the task
ai - > removeTask ( produceTask ) ;
}
}
2013-08-02 02:08:16 +02:00
bool AiRuleProduce : : canUnitTypeOfferResourceType ( const UnitType * ut , const ResourceType * rt ) {
bool unitTypeOffersResourceType = false ;
AiInterface * aiInterface = ai - > getAiInterface ( ) ;
2013-08-02 23:46:55 +02:00
2013-08-02 02:08:16 +02:00
if ( ut ! = NULL & & rt ! = NULL & & aiInterface ! = NULL & & aiInterface - > reqsOk ( ut ) ) {
// Check of the unit 'gives' the resource
// if the unit produces the resource
const Resource * r = ut - > getCost ( rt ) ;
if ( r ! = NULL ) {
if ( ai - > outputAIBehaviourToConsole ( ) ) printf ( " #2 produceGeneric r = [%s][%d] Testing AI RULE Name[%s] \n " , r - > getDescription ( false ) . c_str ( ) , r - > getAmount ( ) , this - > getName ( ) . c_str ( ) ) ;
}
if ( r ! = NULL & & r - > getAmount ( ) < 0 ) {
unitTypeOffersResourceType = true ;
}
else {
// for each command check if we produce a unit that handles the resource
for ( int commandIndex = 0 ; commandIndex < ut - > getCommandTypeCount ( ) ; + + commandIndex ) {
const CommandType * ct = ut - > getCommandType ( commandIndex ) ;
//if the command is produce
if ( ct - > getClass ( ) = = ccProduce | | ct - > getClass ( ) = = ccMorph ) {
const UnitType * producedUnit = static_cast < const UnitType * > ( ct - > getProduced ( ) ) ;
if ( ai - > outputAIBehaviourToConsole ( ) ) printf ( " produceGeneric [%p] Testing AI RULE Name[%s] \n " , rt , this - > getName ( ) . c_str ( ) ) ;
//if the unit produces the resource
const Resource * r = producedUnit - > getCost ( rt ) ;
if ( r ! = NULL ) {
if ( ai - > outputAIBehaviourToConsole ( ) ) printf ( " produceGeneric r = [%s][%d] Testing AI RULE Name[%s] \n " , r - > getDescription ( false ) . c_str ( ) , r - > getAmount ( ) , this - > getName ( ) . c_str ( ) ) ;
}
if ( r ! = NULL & & r - > getAmount ( ) < 0 ) {
unitTypeOffersResourceType = true ;
break ;
}
}
}
}
}
2013-12-14 08:04:12 +01:00
if ( aiInterface ! = NULL & & aiInterface - > isLogLevelEnabled ( 4 ) = = true ) {
2013-08-02 23:46:55 +02:00
char szBuf [ 8096 ] = " " ;
2013-12-14 08:04:12 +01:00
snprintf ( szBuf , 8096 , " canUnitTypeOfferResourceType for unit type [%s] for resource type [%s] returned: %d " , ( ut ! = NULL ? ut - > getName ( false ) . c_str ( ) : " n/a " ) , ( rt ! = NULL ? rt - > getName ( false ) . c_str ( ) : " n/a " ) , unitTypeOffersResourceType ) ;
2013-08-02 23:46:55 +02:00
aiInterface - > printLog ( 4 , szBuf ) ;
}
2013-08-02 02:08:16 +02:00
return unitTypeOffersResourceType ;
}
bool AiRuleProduce : : setAIProduceTaskForResourceType ( const ProduceTask * pt ,
2013-08-02 23:46:55 +02:00
AiInterface * aiInterface ) {
bool taskAdded = false ;
2013-08-02 02:08:16 +02:00
if ( aiInterface - > getMyFactionType ( ) - > getAIBehaviorUnits (
aibcResourceProducerUnits ) . size ( ) > 0 ) {
const std : : vector < FactionType : : PairPUnitTypeInt > & unitList =
aiInterface - > getMyFactionType ( ) - > getAIBehaviorUnits (
aibcResourceProducerUnits ) ;
for ( unsigned int i = 0 ; i < unitList . size ( ) ; + + i ) {
const FactionType : : PairPUnitTypeInt & priorityUnit = unitList [ i ] ;
const UnitType * ut = priorityUnit . first ;
if ( ai - > getCountOfType ( ut ) < priorityUnit . second & &
canUnitTypeOfferResourceType ( ut , pt - > getResourceType ( ) ) = = true & &
aiInterface - > getMyFaction ( ) - > canCreateUnit ( priorityUnit . first , false , true , true ) = = true ) {
ai - > addTask ( new ProduceTask ( priorityUnit . first ) ) ;
2013-08-02 23:46:55 +02:00
taskAdded = true ;
break ;
2013-08-02 02:08:16 +02:00
}
}
}
2013-08-02 23:46:55 +02:00
if ( aiInterface - > isLogLevelEnabled ( 4 ) = = true ) {
char szBuf [ 8096 ] = " " ;
snprintf ( szBuf , 8096 , " setAIProduceTaskForResourceType for resource type [%s] returned: %d " , pt - > getResourceType ( ) - > getName ( false ) . c_str ( ) , taskAdded ) ;
aiInterface - > printLog ( 4 , szBuf ) ;
}
return taskAdded ;
2013-08-02 02:08:16 +02:00
}
void AiRuleProduce : : addUnitTypeToCandidates ( const UnitType * producedUnit ,
UnitTypes & ableUnits , UnitTypesGiveBack & ableUnitsGiveBack ,
bool unitCanGiveBackResource ) {
2013-08-02 23:46:55 +02:00
// if the unit is not already on the list
2013-08-02 02:08:16 +02:00
if ( find ( ableUnits . begin ( ) , ableUnits . end ( ) , producedUnit ) = = ableUnits . end ( ) ) {
ableUnits . push_back ( producedUnit ) ;
ableUnitsGiveBack . push_back ( unitCanGiveBackResource ) ;
2013-08-02 23:46:55 +02:00
AiInterface * aiInterface = ai - > getAiInterface ( ) ;
if ( aiInterface - > isLogLevelEnabled ( 4 ) = = true ) {
char szBuf [ 8096 ] = " " ;
2013-08-10 08:53:16 +02:00
snprintf ( szBuf , 8096 , " addUnitTypeToCandidates for unit type [%s] unitCanGiveBackResource = %d " , producedUnit - > getName ( false ) . c_str ( ) , unitCanGiveBackResource ) ;
2013-08-02 23:46:55 +02:00
aiInterface - > printLog ( 4 , szBuf ) ;
}
2013-08-02 02:08:16 +02:00
}
}
2013-08-30 00:30:09 +02:00
void AiRuleProduce : : produceGenericNew ( const ProduceTask * pt ) {
2010-03-24 22:26:17 +01:00
UnitTypes ableUnits ;
2013-06-20 22:04:19 +02:00
UnitTypesGiveBack ableUnitsGiveBack ;
2010-03-24 22:26:17 +01:00
AiInterface * aiInterface = ai - > getAiInterface ( ) ;
2011-10-04 08:49:44 +02:00
if ( pt - > getResourceType ( ) ! = NULL ) {
2013-08-10 08:53:16 +02:00
if ( aiInterface - > isLogLevelEnabled ( 4 ) = = true ) {
char szBuf [ 8096 ] = " " ;
snprintf ( szBuf , 8096 , " ****START: produceGeneric for resource type [%s] " , pt - > getResourceType ( ) - > getName ( false ) . c_str ( ) ) ;
aiInterface - > printLog ( 4 , szBuf ) ;
}
2013-08-02 02:08:16 +02:00
if ( setAIProduceTaskForResourceType ( pt , aiInterface ) = = true ) {
return ;
2011-10-04 08:49:44 +02:00
}
}
else if ( pt - > getUnitClass ( ) = = ucWorker ) {
if ( aiInterface - > getMyFactionType ( ) - > getAIBehaviorUnits ( aibcWorkerUnits ) . size ( ) > 0 ) {
const std : : vector < FactionType : : PairPUnitTypeInt > & unitList = aiInterface - > getMyFactionType ( ) - > getAIBehaviorUnits ( aibcWorkerUnits ) ;
for ( unsigned int i = 0 ; i < unitList . size ( ) ; + + i ) {
const FactionType : : PairPUnitTypeInt & priorityUnit = unitList [ i ] ;
if ( ai - > getCountOfType ( priorityUnit . first ) < priorityUnit . second & &
aiInterface - > getMyFaction ( ) - > canCreateUnit ( priorityUnit . first , false , true , true ) = = true ) {
ai - > addTask ( new ProduceTask ( priorityUnit . first ) ) ;
return ;
}
}
}
}
else if ( pt - > getUnitClass ( ) = = ucWarrior ) {
if ( aiInterface - > getMyFactionType ( ) - > getAIBehaviorUnits ( aibcWarriorUnits ) . size ( ) > 0 ) {
const std : : vector < FactionType : : PairPUnitTypeInt > & unitList = aiInterface - > getMyFactionType ( ) - > getAIBehaviorUnits ( aibcWarriorUnits ) ;
for ( unsigned int i = 0 ; i < unitList . size ( ) ; + + i ) {
const FactionType : : PairPUnitTypeInt & priorityUnit = unitList [ i ] ;
if ( ai - > getCountOfType ( priorityUnit . first ) < priorityUnit . second & &
aiInterface - > getMyFaction ( ) - > canCreateUnit ( priorityUnit . first , false , true , true ) = = true ) {
ai - > addTask ( new ProduceTask ( priorityUnit . first ) ) ;
return ;
}
}
}
}
2010-03-24 22:26:17 +01:00
//for each unit, produce it if possible
2011-10-04 08:49:44 +02:00
for ( int i = 0 ; i < aiInterface - > getMyUnitCount ( ) ; + + i ) {
2011-01-02 01:39:13 +01:00
2010-03-24 22:26:17 +01:00
//for each command
const UnitType * ut = aiInterface - > getMyUnit ( i ) - > getType ( ) ;
2013-08-02 02:08:16 +02:00
2013-09-19 08:32:47 +02:00
//bool produceIt= false;
2011-10-04 08:49:44 +02:00
for ( int j = 0 ; j < ut - > getCommandTypeCount ( ) ; + + j ) {
2010-03-24 22:26:17 +01:00
const CommandType * ct = ut - > getCommandType ( j ) ;
2011-01-02 01:39:13 +01:00
2010-03-24 22:26:17 +01:00
//if the command is produce
2013-08-10 08:53:16 +02:00
//bool produceIt= false;
2011-10-04 08:49:44 +02:00
if ( ct - > getClass ( ) = = ccProduce | | ct - > getClass ( ) = = ccMorph ) {
2011-06-24 18:14:39 +02:00
const UnitType * producedUnit = static_cast < const UnitType * > ( ct - > getProduced ( ) ) ;
2010-03-24 22:26:17 +01:00
2011-06-24 18:14:39 +02:00
if ( ai - > outputAIBehaviourToConsole ( ) ) printf ( " produceGeneric [%p] Testing AI RULE Name[%s] \n " , pt - > getResourceType ( ) , this - > getName ( ) . c_str ( ) ) ;
2011-04-06 17:44:33 +02:00
2010-03-24 22:26:17 +01:00
//if the unit produces the resource
2011-10-04 08:49:44 +02:00
if ( pt - > getResourceType ( ) ! = NULL ) {
2010-03-24 22:26:17 +01:00
const Resource * r = producedUnit - > getCost ( pt - > getResourceType ( ) ) ;
2011-06-24 18:14:39 +02:00
if ( r ! = NULL ) {
2013-06-13 10:55:48 +02:00
if ( ai - > outputAIBehaviourToConsole ( ) ) printf ( " produceGeneric r = [%s][%d] Testing AI RULE Name[%s] \n " , r - > getDescription ( false ) . c_str ( ) , r - > getAmount ( ) , this - > getName ( ) . c_str ( ) ) ;
2011-04-06 17:44:33 +02:00
}
2011-10-04 08:49:44 +02:00
if ( r ! = NULL & & r - > getAmount ( ) < 0 ) {
2013-08-10 08:53:16 +02:00
if ( aiInterface - > reqsOk ( ct ) & & aiInterface - > reqsOk ( producedUnit ) ) {
2013-09-19 03:19:36 +02:00
//produceIt= true;
2013-08-10 08:53:16 +02:00
addUnitTypeToCandidates ( producedUnit , ableUnits , ableUnitsGiveBack , false ) ;
}
2010-03-24 22:26:17 +01:00
}
}
2011-10-04 08:49:44 +02:00
else {
2010-03-24 22:26:17 +01:00
//if the unit is from the right class
2011-06-24 18:14:39 +02:00
if ( ai - > outputAIBehaviourToConsole ( ) ) printf ( " produceGeneric right class = [%d] Testing AI RULE Name[%s] \n " , producedUnit - > isOfClass ( pt - > getUnitClass ( ) ) , this - > getName ( ) . c_str ( ) ) ;
2011-04-06 17:44:33 +02:00
2010-03-24 22:26:17 +01:00
if ( producedUnit - > isOfClass ( pt - > getUnitClass ( ) ) ) {
if ( aiInterface - > reqsOk ( ct ) & & aiInterface - > reqsOk ( producedUnit ) ) {
2013-09-19 00:44:04 +02:00
//produceIt= true;
2013-08-02 02:08:16 +02:00
addUnitTypeToCandidates ( producedUnit , ableUnits , ableUnitsGiveBack , false ) ;
2010-03-24 22:26:17 +01:00
}
}
}
2013-06-20 22:04:19 +02:00
}
2010-03-24 22:26:17 +01:00
}
2013-08-10 08:53:16 +02:00
// Now check of the unit 'gives' the resource
// This is likely a unit that it BUILT by another and that is handled by a different AI task type: Build
// if(produceIt == false && pt->getResourceType() != NULL) {
// const Resource *r= ut->getCost(pt->getResourceType());
// if(r != NULL) {
// if(ai->outputAIBehaviourToConsole()) printf("#2 produceGeneric r = [%s][%d] Testing AI RULE Name[%s]\n",r->getDescription(false).c_str(),r->getAmount(), this->getName().c_str());
// }
//
// if(r != NULL && r->getAmount() < 0) {
// if(aiInterface->reqsOk(ut)){
// produceIt= true;
// addUnitTypeToCandidates(ut, ableUnits,ableUnitsGiveBack, true);
// }
// }
// }
2010-03-24 22:26:17 +01:00
}
//add specific produce task
2011-10-04 08:49:44 +02:00
if ( ableUnits . empty ( ) = = false ) {
2010-03-24 22:26:17 +01:00
2011-06-24 18:14:39 +02:00
if ( ai - > outputAIBehaviourToConsole ( ) ) printf ( " produceGeneric !ableUnits.empty(), ableUnits.size() = [%d] Testing AI RULE Name[%s] \n " , ( int ) ableUnits . size ( ) , this - > getName ( ) . c_str ( ) ) ;
2011-04-06 17:44:33 +02:00
2013-06-20 22:04:19 +02:00
// Now check if we already have at least 2 produce or morph
// resource based units, if so prefer units that give back the resource
2013-08-10 08:53:16 +02:00
if ( pt - > getResourceType ( ) ! = NULL & & ableUnits . size ( ) > 1 ) {
2013-06-20 22:04:19 +02:00
//priority for non produced units
UnitTypes newAbleUnits ;
bool haveEnoughProducers = true ;
bool haveNonProducers = false ;
for ( unsigned int i = 0 ; i < ableUnits . size ( ) ; + + i ) {
const UnitType * ut = ableUnits [ i ] ;
bool givesBack = ableUnitsGiveBack [ i ] ;
if ( givesBack = = false & & ai - > getCountOfType ( ut ) < 2 ) {
haveEnoughProducers = false ;
}
else if ( givesBack = = true ) {
haveNonProducers = true ;
newAbleUnits . push_back ( ut ) ;
}
2013-08-10 08:53:16 +02:00
if ( aiInterface - > isLogLevelEnabled ( 4 ) = = true ) {
char szBuf [ 8096 ] = " " ;
snprintf ( szBuf , 8096 , " In produceGeneric for unit type [%s] givesBack: %d count of unit type: %d " , ut - > getName ( false ) . c_str ( ) , givesBack , ai - > getCountOfType ( ut ) ) ;
aiInterface - > printLog ( 4 , szBuf ) ;
}
2013-06-20 22:04:19 +02:00
}
2013-08-02 23:46:55 +02:00
if ( aiInterface - > isLogLevelEnabled ( 4 ) = = true ) {
char szBuf [ 8096 ] = " " ;
snprintf ( szBuf , 8096 , " haveEnoughProducers [%d] haveNonProducers [%d] " , haveEnoughProducers , haveNonProducers ) ;
aiInterface - > printLog ( 4 , szBuf ) ;
for ( unsigned int i = 0 ; i < ableUnits . size ( ) ; + + i ) {
const UnitType * ut = ableUnits [ i ] ;
2013-09-19 00:44:04 +02:00
snprintf ( szBuf , 8096 , " i: %u unit type [%s] " , i , ut - > getName ( false ) . c_str ( ) ) ;
2013-08-02 23:46:55 +02:00
aiInterface - > printLog ( 4 , szBuf ) ;
}
for ( unsigned int i = 0 ; i < newAbleUnits . size ( ) ; + + i ) {
const UnitType * ut = newAbleUnits [ i ] ;
2013-09-19 00:44:04 +02:00
snprintf ( szBuf , 8096 , " i: %u new unit type [%s] " , i , ut - > getName ( false ) . c_str ( ) ) ;
2013-08-02 23:46:55 +02:00
aiInterface - > printLog ( 4 , szBuf ) ;
}
}
2013-06-20 22:04:19 +02:00
if ( haveEnoughProducers = = true & & haveNonProducers = = true ) {
ableUnits = newAbleUnits ;
}
}
2010-03-24 22:26:17 +01:00
//priority for non produced units
2011-10-04 08:49:44 +02:00
for ( unsigned int i = 0 ; i < ableUnits . size ( ) ; + + i ) {
if ( ai - > getCountOfType ( ableUnits [ i ] ) = = 0 ) {
2013-08-02 23:46:55 +02:00
if ( ai - > getRandom ( ) - > randRange ( 0 , 1 ) = = 0 ) {
if ( aiInterface - > isLogLevelEnabled ( 4 ) = = true ) {
char szBuf [ 8096 ] = " " ;
2013-08-10 08:53:16 +02:00
snprintf ( szBuf , 8096 , " In produceGeneric priority adding produce task: %d of " MG_SIZE_T_SPECIFIER " for unit type [%s] " , i , ableUnits . size ( ) , ableUnits [ i ] - > getName ( false ) . c_str ( ) ) ;
2013-08-02 23:46:55 +02:00
aiInterface - > printLog ( 4 , szBuf ) ;
}
2010-03-24 22:26:17 +01:00
ai - > addTask ( new ProduceTask ( ableUnits [ i ] ) ) ;
return ;
}
}
}
//normal case
2013-11-02 12:04:52 +01:00
int randomUnitTypeIndex = ai - > getRandom ( ) - > randRange ( 0 , ( int ) ableUnits . size ( ) - 1 ) ;
2013-08-10 08:53:16 +02:00
if ( aiInterface - > isLogLevelEnabled ( 4 ) = = true ) {
char szBuf [ 8096 ] = " " ;
snprintf ( szBuf , 8096 , " In produceGeneric randomUnitTypeIndex = %d of " MG_SIZE_T_SPECIFIER " equals unit type [%s] " , randomUnitTypeIndex , ableUnits . size ( ) - 1 , ableUnits [ randomUnitTypeIndex ] - > getName ( false ) . c_str ( ) ) ;
aiInterface - > printLog ( 4 , szBuf ) ;
}
const UnitType * ut = ableUnits [ randomUnitTypeIndex ] ;
2013-08-02 23:46:55 +02:00
if ( aiInterface - > isLogLevelEnabled ( 4 ) = = true ) {
char szBuf [ 8096 ] = " " ;
2013-08-10 08:53:16 +02:00
snprintf ( szBuf , 8096 , " == END In produceGeneric normal adding produce task for unit type [%s] " , ut - > getName ( false ) . c_str ( ) ) ;
2013-08-02 23:46:55 +02:00
aiInterface - > printLog ( 4 , szBuf ) ;
}
ai - > addTask ( new ProduceTask ( ut ) ) ;
2010-03-24 22:26:17 +01:00
}
}
2013-08-30 00:30:09 +02:00
void AiRuleProduce : : produceGeneric ( const ProduceTask * pt ) {
typedef vector < const UnitType * > UnitTypes ;
UnitTypes ableUnits ;
AiInterface * aiInterface = ai - > getAiInterface ( ) ;
if ( pt - > getResourceType ( ) ! = NULL ) {
if ( aiInterface - > getMyFactionType ( ) - > getAIBehaviorUnits ( aibcResourceProducerUnits ) . size ( ) > 0 ) {
const std : : vector < FactionType : : PairPUnitTypeInt > & unitList = aiInterface - > getMyFactionType ( ) - > getAIBehaviorUnits ( aibcResourceProducerUnits ) ;
for ( unsigned int i = 0 ; i < unitList . size ( ) ; + + i ) {
const FactionType : : PairPUnitTypeInt & priorityUnit = unitList [ i ] ;
if ( ai - > getCountOfType ( priorityUnit . first ) < priorityUnit . second & &
aiInterface - > getMyFaction ( ) - > canCreateUnit ( priorityUnit . first , false , true , true ) = = true ) {
//if(ai->getRandom()->randRange(0, 1)==0) {
ai - > addTask ( new ProduceTask ( priorityUnit . first ) ) ;
return ;
//}
}
}
}
}
else if ( pt - > getUnitClass ( ) = = ucWorker ) {
if ( aiInterface - > getMyFactionType ( ) - > getAIBehaviorUnits ( aibcWorkerUnits ) . size ( ) > 0 ) {
const std : : vector < FactionType : : PairPUnitTypeInt > & unitList = aiInterface - > getMyFactionType ( ) - > getAIBehaviorUnits ( aibcWorkerUnits ) ;
for ( unsigned int i = 0 ; i < unitList . size ( ) ; + + i ) {
const FactionType : : PairPUnitTypeInt & priorityUnit = unitList [ i ] ;
if ( ai - > getCountOfType ( priorityUnit . first ) < priorityUnit . second & &
aiInterface - > getMyFaction ( ) - > canCreateUnit ( priorityUnit . first , false , true , true ) = = true ) {
//if(ai->getRandom()->randRange(0, 1)==0) {
ai - > addTask ( new ProduceTask ( priorityUnit . first ) ) ;
return ;
//}
}
}
}
}
else if ( pt - > getUnitClass ( ) = = ucWarrior ) {
if ( aiInterface - > getMyFactionType ( ) - > getAIBehaviorUnits ( aibcWarriorUnits ) . size ( ) > 0 ) {
const std : : vector < FactionType : : PairPUnitTypeInt > & unitList = aiInterface - > getMyFactionType ( ) - > getAIBehaviorUnits ( aibcWarriorUnits ) ;
for ( unsigned int i = 0 ; i < unitList . size ( ) ; + + i ) {
const FactionType : : PairPUnitTypeInt & priorityUnit = unitList [ i ] ;
if ( ai - > getCountOfType ( priorityUnit . first ) < priorityUnit . second & &
aiInterface - > getMyFaction ( ) - > canCreateUnit ( priorityUnit . first , false , true , true ) = = true ) {
//if(ai->getRandom()->randRange(0, 1)==0) {
ai - > addTask ( new ProduceTask ( priorityUnit . first ) ) ;
return ;
//}
}
}
}
}
//for each unit, produce it if possible
for ( int i = 0 ; i < aiInterface - > getMyUnitCount ( ) ; + + i ) {
//for each command
const UnitType * ut = aiInterface - > getMyUnit ( i ) - > getType ( ) ;
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 < const UnitType * > ( ct - > getProduced ( ) ) ;
bool produceIt = false ;
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 ) {
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 ( false ) . c_str ( ) , r - > getAmount ( ) , this - > getName ( ) . c_str ( ) ) ;
}
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 ( producedUnit - > isOfClass ( pt - > getUnitClass ( ) ) ) {
if ( aiInterface - > reqsOk ( ct ) & & aiInterface - > reqsOk ( producedUnit ) ) {
produceIt = true ;
}
}
}
if ( produceIt ) {
//if the unit is not already on the list
if ( find ( ableUnits . begin ( ) , ableUnits . end ( ) , producedUnit ) = = ableUnits . end ( ) ) {
ableUnits . push_back ( producedUnit ) ;
}
}
}
}
}
//add specific produce task
if ( ableUnits . empty ( ) = = false ) {
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 ; i < ableUnits . size ( ) ; + + i ) {
if ( ai - > getCountOfType ( ableUnits [ i ] ) = = 0 ) {
if ( ai - > getRandom ( ) - > randRange ( 0 , 1 ) = = 0 ) {
ai - > addTask ( new ProduceTask ( ableUnits [ i ] ) ) ;
return ;
}
}
}
//normal case
2013-11-02 12:04:52 +01:00
ai - > addTask ( new ProduceTask ( ableUnits [ ai - > getRandom ( ) - > randRange ( 0 , ( int ) ableUnits . size ( ) - 1 ) ] ) ) ;
2013-08-30 00:30:09 +02:00
}
}
2010-03-24 22:26:17 +01:00
void AiRuleProduce : : produceSpecific ( const ProduceTask * pt ) {
2011-01-02 01:39:13 +01:00
2010-03-24 22:26:17 +01:00
AiInterface * aiInterface = ai - > getAiInterface ( ) ;
2011-01-02 01:39:13 +01:00
2012-03-17 20:22:03 +01:00
if ( ai - > outputAIBehaviourToConsole ( ) ) printf ( " produceSpecific aiInterface->reqsOk(pt->getUnitType()) = [%s][%d] Testing AI RULE Name[%s] \n " , pt - > getUnitType ( ) - > getName ( ) . c_str ( ) , aiInterface - > reqsOk ( pt - > getUnitType ( ) ) , this - > getName ( ) . c_str ( ) ) ;
2013-01-12 02:11:53 +01:00
if ( aiInterface - > isLogLevelEnabled ( 4 ) = = true ) {
char szBuf [ 8096 ] = " " ;
2013-08-10 08:53:16 +02:00
snprintf ( szBuf , 8096 , " == START produceSpecific aiInterface->reqsOk(pt->getUnitType()) = [%s][%d] Testing AI RULE Name[%s] " , pt - > getUnitType ( ) - > getName ( ) . c_str ( ) , aiInterface - > reqsOk ( pt - > getUnitType ( ) ) , this - > getName ( ) . c_str ( ) ) ;
2013-01-12 02:11:53 +01:00
aiInterface - > printLog ( 4 , szBuf ) ;
}
2011-04-06 17:44:33 +02:00
2010-03-24 22:26:17 +01:00
//if unit meets requirements
2012-03-17 20:22:03 +01:00
if ( aiInterface - > reqsOk ( pt - > getUnitType ( ) ) ) {
2011-01-02 01:39:13 +01:00
2012-03-27 05:23:03 +02:00
const CommandType * ctypeForCostCheck = NULL ;
//for each unit
for ( int i = 0 ; i < aiInterface - > getMyUnitCount ( ) ; + + i ) {
//for each command
const UnitType * ut = aiInterface - > getMyUnit ( i ) - > getType ( ) ;
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 < const UnitType * > ( 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 ( aiInterface - > reqsOk ( ct ) ) {
if ( ctypeForCostCheck = = NULL | | ct - > getClass ( ) = = ccMorph ) {
if ( ctypeForCostCheck ! = NULL & & ct - > getClass ( ) = = ccMorph ) {
const MorphCommandType * mct = dynamic_cast < const MorphCommandType * > ( ct ) ;
2013-12-14 08:04:12 +01:00
if ( mct = = NULL ) {
throw megaglest_runtime_error ( " mct == NULL " ) ;
}
2012-03-27 05:23:03 +02:00
if ( mct - > getIgnoreResourceRequirements ( ) = = true ) {
ctypeForCostCheck = ct ;
}
}
else {
ctypeForCostCheck = ct ;
}
}
}
}
}
}
}
if ( ai - > outputAIBehaviourToConsole ( ) ) printf ( " produceSpecific aiInterface->checkCosts(pt->getUnitType()) = [%d] Testing AI RULE Name[%s] \n " , aiInterface - > checkCosts ( pt - > getUnitType ( ) , ctypeForCostCheck ) , this - > getName ( ) . c_str ( ) ) ;
2013-01-12 02:11:53 +01:00
if ( aiInterface - > isLogLevelEnabled ( 4 ) = = true ) {
char szBuf [ 8096 ] = " " ;
snprintf ( szBuf , 8096 , " produceSpecific aiInterface->checkCosts(pt->getUnitType()) = [%d] Testing AI RULE Name[%s] " , aiInterface - > checkCosts ( pt - > getUnitType ( ) , ctypeForCostCheck ) , this - > getName ( ) . c_str ( ) ) ;
aiInterface - > printLog ( 4 , szBuf ) ;
}
2011-04-06 17:44:33 +02:00
2010-03-24 22:26:17 +01:00
//if unit doesnt meet resources retry
2012-03-27 05:23:03 +02:00
if ( aiInterface - > checkCosts ( pt - > getUnitType ( ) , ctypeForCostCheck ) = = false ) {
2013-01-12 02:11:53 +01:00
if ( aiInterface - > isLogLevelEnabled ( 4 ) = = true ) {
char szBuf [ 8096 ] = " " ;
snprintf ( szBuf , 8096 , " Check costs FAILED. " ) ;
aiInterface - > printLog ( 4 , szBuf ) ;
}
2012-03-17 20:22:03 +01:00
2010-03-24 22:26:17 +01:00
ai - > retryTask ( pt ) ;
return ;
}
//produce specific unit
vector < int > producers ;
2010-07-14 00:51:27 +02:00
// Hold a list of units which can produce or morph
// then a list of commandtypes for each unit
2011-06-24 18:14:39 +02:00
map < int , vector < const CommandType * > > producersDefaultCommandType ;
2010-03-24 22:26:17 +01:00
const CommandType * defCt = NULL ;
//for each unit
2011-06-24 18:14:39 +02:00
for ( int i = 0 ; i < aiInterface - > getMyUnitCount ( ) ; + + i ) {
2011-01-02 01:39:13 +01:00
2010-03-24 22:26:17 +01:00
//for each command
const UnitType * ut = aiInterface - > getMyUnit ( i ) - > getType ( ) ;
2011-06-24 18:14:39 +02:00
for ( int j = 0 ; j < ut - > getCommandTypeCount ( ) ; + + j ) {
2010-03-24 22:26:17 +01:00
const CommandType * ct = ut - > getCommandType ( j ) ;
2011-01-02 01:39:13 +01:00
2010-03-24 22:26:17 +01:00
//if the command is produce
2011-06-24 18:14:39 +02:00
if ( ct - > getClass ( ) = = ccProduce | | ct - > getClass ( ) = = ccMorph ) {
const UnitType * producedUnit = static_cast < const UnitType * > ( ct - > getProduced ( ) ) ;
2011-01-02 01:39:13 +01:00
2010-03-24 22:26:17 +01:00
//if units match
if ( producedUnit = = pt - > getUnitType ( ) ) {
2011-04-06 17:44:33 +02:00
2011-06-24 18:14:39 +02:00
if ( ai - > outputAIBehaviourToConsole ( ) ) printf ( " produceSpecific aiInterface->reqsOk(ct) = [%d] Testing AI RULE Name[%s] \n " , aiInterface - > reqsOk ( ct ) , this - > getName ( ) . c_str ( ) ) ;
2011-04-06 17:44:33 +02:00
2010-03-24 22:26:17 +01:00
if ( aiInterface - > reqsOk ( ct ) ) {
defCt = ct ;
producers . push_back ( i ) ;
2010-07-14 00:51:27 +02:00
producersDefaultCommandType [ i ] . push_back ( ct ) ;
2010-03-24 22:26:17 +01:00
}
2011-01-02 01:39:13 +01:00
}
2010-03-24 22:26:17 +01:00
}
}
}
//produce from random producer
2012-03-17 20:22:03 +01:00
if ( producers . empty ( ) = = false ) {
2011-04-06 17:44:33 +02:00
2011-06-24 18:14:39 +02:00
if ( ai - > outputAIBehaviourToConsole ( ) ) printf ( " produceSpecific producers.empty() = [%d] Testing AI RULE Name[%s] \n " , producers . empty ( ) , this - > getName ( ) . c_str ( ) ) ;
2013-01-12 02:11:53 +01:00
if ( aiInterface - > isLogLevelEnabled ( 4 ) = = true ) {
char szBuf [ 8096 ] = " " ;
snprintf ( szBuf , 8096 , " produceSpecific producers.empty() = [%d] Testing AI RULE Name[%s] " , producers . empty ( ) , this - > getName ( ) . c_str ( ) ) ;
aiInterface - > printLog ( 4 , szBuf ) ;
}
2012-03-17 20:22:03 +01:00
// Narrow down producers list to those who are not busy if possible
vector < int > idle_producers ;
for ( unsigned int i = 0 ; i < producers . size ( ) ; + + i ) {
int currentProducerIndex = producers [ i ] ;
if ( currentProducerIndex > = aiInterface - > getMyUnitCount ( ) ) {
2012-10-19 03:31:20 +02:00
char szBuf [ 8096 ] = " " ;
2012-11-10 07:37:23 +01:00
printf ( " In [%s::%s Line: %d] currentProducerIndex >= aiInterface->getMyUnitCount(), currentProducerIndex = %d, aiInterface->getMyUnitCount() = %d, i = %u,producers.size() = " MG_SIZE_T_SPECIFIER " \n " , __FILE__ , __FUNCTION__ , __LINE__ , currentProducerIndex , aiInterface - > getMyUnitCount ( ) , i , producers . size ( ) ) ;
snprintf ( szBuf , 8096 , " In [%s::%s Line: %d] currentProducerIndex >= aiInterface->getMyUnitCount(), currentProducerIndex = %d, aiInterface->getMyUnitCount() = %u, i = %u,producers.size() = " MG_SIZE_T_SPECIFIER " " , __FILE__ , __FUNCTION__ , __LINE__ , currentProducerIndex , aiInterface - > getMyUnitCount ( ) , i , producers . size ( ) ) ;
2012-04-14 23:21:09 +02:00
throw megaglest_runtime_error ( szBuf ) ;
2012-03-17 20:22:03 +01:00
}
const Unit * unit = aiInterface - > getMyUnit ( currentProducerIndex ) ;
if ( unit - > anyCommand ( ) = = false ) {
idle_producers . push_back ( currentProducerIndex ) ;
}
}
2012-03-28 08:25:57 +02:00
if ( idle_producers . empty ( ) = = false ) {
2012-03-17 20:22:03 +01:00
producers = idle_producers ;
}
2011-06-24 18:14:39 +02:00
if ( aiInterface - > getControlType ( ) = = ctCpuMega | |
aiInterface - > getControlType ( ) = = ctNetworkCpuMega )
{ // mega cpu trys to balance the commands to the producers
2013-11-02 12:04:52 +01:00
int randomstart = ai - > getRandom ( ) - > randRange ( 0 , ( int ) producers . size ( ) - 1 ) ;
2011-06-24 18:14:39 +02:00
int lowestCommandCount = 1000000 ;
int currentProducerIndex = producers [ randomstart ] ;
int bestIndex = - 1 ;
2011-09-01 01:10:43 +02:00
//int besti=0;
2011-06-24 18:14:39 +02:00
int currentCommandCount = 0 ;
for ( unsigned int i = randomstart ; i < producers . size ( ) + randomstart ; i + + ) {
int prIndex = i ;
if ( i > = producers . size ( ) ) {
2013-11-02 12:04:52 +01:00
prIndex = ( i - ( int ) producers . size ( ) ) ;
2010-06-10 23:33:50 +02:00
}
2011-06-24 18:14:39 +02:00
currentProducerIndex = producers [ prIndex ] ;
if ( currentProducerIndex > = aiInterface - > getMyUnitCount ( ) ) {
2012-10-19 03:31:20 +02:00
char szBuf [ 8096 ] = " " ;
2012-11-10 07:37:23 +01:00
printf ( " In [%s::%s Line: %d] currentProducerIndex >= aiInterface->getMyUnitCount(), currentProducerIndex = %d, aiInterface->getMyUnitCount() = %d, i = %u,producers.size() = " MG_SIZE_T_SPECIFIER " \n " , __FILE__ , __FUNCTION__ , __LINE__ , currentProducerIndex , aiInterface - > getMyUnitCount ( ) , i , producers . size ( ) ) ;
snprintf ( szBuf , 8096 , " In [%s::%s Line: %d] currentProducerIndex >= aiInterface->getMyUnitCount(), currentProducerIndex = %d, aiInterface->getMyUnitCount() = %u, i = %u,producers.size() = " MG_SIZE_T_SPECIFIER " " , __FILE__ , __FUNCTION__ , __LINE__ , currentProducerIndex , aiInterface - > getMyUnitCount ( ) , i , producers . size ( ) ) ;
2012-04-14 23:21:09 +02:00
throw megaglest_runtime_error ( szBuf ) ;
2010-06-08 09:40:32 +02:00
}
2013-11-19 07:14:06 +01:00
if ( prIndex > = ( int ) producers . size ( ) ) {
2012-10-19 03:31:20 +02:00
char szBuf [ 8096 ] = " " ;
2012-11-10 07:37:23 +01:00
printf ( " In [%s::%s Line: %d] prIndex >= producers.size(), currentProducerIndex = %d, i = %u,producers.size() = " MG_SIZE_T_SPECIFIER " \n " , __FILE__ , __FUNCTION__ , __LINE__ , prIndex , i , producers . size ( ) ) ;
snprintf ( szBuf , 8096 , " In [%s::%s Line: %d] currentProducerIndex >= producers.size(), currentProducerIndex = %d, i = %u,producers.size() = " MG_SIZE_T_SPECIFIER " " , __FILE__ , __FUNCTION__ , __LINE__ , currentProducerIndex , i , producers . size ( ) ) ;
2012-04-14 23:21:09 +02:00
throw megaglest_runtime_error ( szBuf ) ;
2010-06-10 22:37:38 +02:00
}
2011-06-24 18:14:39 +02:00
currentCommandCount = aiInterface - > getMyUnit ( currentProducerIndex ) - > getCommandSize ( ) ;
if ( currentCommandCount = = 1 & &
aiInterface - > getMyUnit ( currentProducerIndex ) - > getCurrCommand ( ) - > getCommandType ( ) - > getClass ( ) = = ccStop )
{ // special for non buildings
currentCommandCount = 0 ;
2010-03-24 22:26:17 +01:00
}
2011-06-24 18:14:39 +02:00
if ( lowestCommandCount > currentCommandCount )
{
lowestCommandCount = aiInterface - > getMyUnit ( currentProducerIndex ) - > getCommandSize ( ) ;
bestIndex = currentProducerIndex ;
2011-09-01 01:10:43 +02:00
//besti=i%(producers.size());
2010-03-24 22:26:17 +01:00
}
}
2011-06-24 18:14:39 +02:00
if ( aiInterface - > getMyUnit ( bestIndex ) - > getCommandSize ( ) > 2 ) {
2010-03-24 22:26:17 +01:00
// maybe we need another producer of this kind if possible!
2011-06-24 18:14:39 +02:00
if ( aiInterface - > reqsOk ( aiInterface - > getMyUnit ( bestIndex ) - > getType ( ) ) ) {
2012-03-17 20:22:03 +01:00
if ( ai - > getCountOfClass ( ucBuilding ) > 5 ) {
2010-03-24 22:26:17 +01:00
ai - > addTask ( new BuildTask ( aiInterface - > getMyUnit ( bestIndex ) - > getType ( ) ) ) ;
2012-03-17 20:22:03 +01:00
}
2010-03-24 22:26:17 +01:00
}
2010-06-08 09:40:32 +02:00
// need to calculate another producer, maybe its better to produce another warrior with another producer
2010-03-24 22:26:17 +01:00
vector < int > backupProducers ;
// find another producer unit which is free and produce any kind of warrior.
//for each unit
2011-06-24 18:14:39 +02:00
for ( int i = 0 ; i < aiInterface - > getMyUnitCount ( ) ; + + i ) {
2010-03-24 22:26:17 +01:00
const UnitType * ut = aiInterface - > getMyUnit ( i ) - > getType ( ) ;
//for each command
2011-06-24 18:14:39 +02:00
for ( int j = 0 ; j < ut - > getCommandTypeCount ( ) ; + + j ) {
2010-03-24 22:26:17 +01:00
const CommandType * ct = ut - > getCommandType ( j ) ;
//if the command is produce
2011-06-24 18:14:39 +02:00
if ( ct - > getClass ( ) = = ccProduce ) {
const UnitType * unitType = static_cast < const UnitType * > ( ct - > getProduced ( ) ) ;
if ( unitType - > hasSkillClass ( scAttack ) & & ! unitType - > hasCommandClass ( ccHarvest ) & & aiInterface - > reqsOk ( ct ) )
{ //this can produce a warrior
2010-06-08 09:40:32 +02:00
backupProducers . push_back ( i ) ;
2011-01-02 01:39:13 +01:00
}
2010-03-24 22:26:17 +01:00
}
2011-01-02 01:39:13 +01:00
}
2010-03-24 22:26:17 +01:00
}
2011-06-24 18:14:39 +02:00
if ( ! backupProducers . empty ( ) ) {
2013-11-02 12:04:52 +01:00
int randomstart = ai - > getRandom ( ) - > randRange ( 0 , ( int ) backupProducers . size ( ) - 1 ) ;
2011-06-24 18:14:39 +02:00
int lowestCommandCount = 1000000 ;
int currentProducerIndex = backupProducers [ randomstart ] ;
int bestIndex = - 1 ;
for ( unsigned int i = randomstart ; i < backupProducers . size ( ) + randomstart ; i + + ) {
int prIndex = i ;
if ( i > = backupProducers . size ( ) ) {
2013-11-02 12:04:52 +01:00
prIndex = ( i - ( int ) backupProducers . size ( ) ) ;
2010-06-10 23:33:50 +02:00
}
2010-06-10 23:53:50 +02:00
2011-06-24 18:14:39 +02:00
currentProducerIndex = backupProducers [ prIndex ] ;
if ( currentProducerIndex > = aiInterface - > getMyUnitCount ( ) ) {
2012-10-19 03:31:20 +02:00
char szBuf [ 8096 ] = " " ;
2012-11-10 07:37:23 +01:00
printf ( " In [%s::%s Line: %d] currentProducerIndex >= aiInterface->getMyUnitCount(), currentProducerIndex = %d, aiInterface->getMyUnitCount() = %d, i = %u,backupProducers.size() = " MG_SIZE_T_SPECIFIER " \n " , __FILE__ , __FUNCTION__ , __LINE__ , currentProducerIndex , aiInterface - > getMyUnitCount ( ) , i , backupProducers . size ( ) ) ;
snprintf ( szBuf , 8096 , " In [%s::%s Line: %d] currentProducerIndex >= aiInterface->getMyUnitCount(), currentProducerIndex = %d, aiInterface->getMyUnitCount() = %d, i = %u,backupProducers.size() = " MG_SIZE_T_SPECIFIER " " , __FILE__ , __FUNCTION__ , __LINE__ , currentProducerIndex , aiInterface - > getMyUnitCount ( ) , i , backupProducers . size ( ) ) ;
2012-04-14 23:21:09 +02:00
throw megaglest_runtime_error ( szBuf ) ;
2010-06-10 23:33:50 +02:00
}
2013-11-19 07:14:06 +01:00
if ( prIndex > = ( int ) backupProducers . size ( ) ) {
2012-10-19 03:31:20 +02:00
char szBuf [ 8096 ] = " " ;
2012-11-10 07:37:23 +01:00
printf ( " In [%s::%s Line: %d] prIndex >= backupProducers.size(), currentProducerIndex = %d, i = %u,backupProducers.size() = " MG_SIZE_T_SPECIFIER " \n " , __FILE__ , __FUNCTION__ , __LINE__ , prIndex , i , backupProducers . size ( ) ) ;
snprintf ( szBuf , 8096 , " In [%s::%s Line: %d] currentProducerIndex >= backupProducers.size(), currentProducerIndex = %d, i = %u,backupProducers.size() = " MG_SIZE_T_SPECIFIER " " , __FILE__ , __FUNCTION__ , __LINE__ , currentProducerIndex , i , backupProducers . size ( ) ) ;
2012-04-14 23:21:09 +02:00
throw megaglest_runtime_error ( szBuf ) ;
2010-06-10 23:53:50 +02:00
}
2013-10-25 07:45:54 +02:00
int currentCommandCount = aiInterface - > getMyUnit ( currentProducerIndex ) - > getCommandSize ( ) ;
2011-06-24 18:14:39 +02:00
if ( currentCommandCount = = 1 & &
aiInterface - > getMyUnit ( currentProducerIndex ) - > getCurrCommand ( ) - > getCommandType ( ) - > getClass ( ) = = ccStop )
{ // special for non buildings
currentCommandCount = 0 ;
2010-03-24 22:26:17 +01:00
}
2011-06-24 18:14:39 +02:00
if ( lowestCommandCount > currentCommandCount ) {
lowestCommandCount = currentCommandCount ;
bestIndex = currentProducerIndex ;
if ( lowestCommandCount = = 0 ) break ;
2010-03-24 22:26:17 +01:00
}
}
// a good producer is found, lets choose a warrior production
2010-06-09 02:38:15 +02:00
vector < int > productionCommandIndexes ;
2011-06-24 18:14:39 +02:00
const UnitType * ut = aiInterface - > getMyUnit ( bestIndex ) - > getType ( ) ;
for ( int j = 0 ; j < ut - > getCommandTypeCount ( ) ; + + j ) {
2010-03-24 22:26:17 +01:00
const CommandType * ct = ut - > getCommandType ( j ) ;
2011-01-02 01:39:13 +01:00
2010-03-24 22:26:17 +01:00
//if the command is produce
2011-06-24 18:14:39 +02:00
if ( ct - > getClass ( ) = = ccProduce ) {
const UnitType * unitType = static_cast < const UnitType * > ( ct - > getProduced ( ) ) ;
if ( unitType - > hasSkillClass ( scAttack ) & & ! unitType - > hasCommandClass ( ccHarvest ) & & aiInterface - > reqsOk ( ct ) )
{ //this can produce a warrior
2011-04-06 17:44:33 +02:00
productionCommandIndexes . push_back ( j ) ;
2011-01-02 01:39:13 +01:00
}
2010-03-24 22:26:17 +01:00
}
}
2013-11-02 12:04:52 +01:00
int commandIndex = productionCommandIndexes [ ai - > getRandom ( ) - > randRange ( 0 , ( int ) productionCommandIndexes . size ( ) - 1 ) ] ;
2011-06-24 18:14:39 +02:00
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 ( ) ) ;
2013-01-12 02:11:53 +01:00
if ( aiInterface - > isLogLevelEnabled ( 4 ) = = true ) {
char szBuf [ 8096 ] = " " ;
snprintf ( szBuf , 8096 , " mega #1 produceSpecific giveCommand to unit [%s] commandType [%s] " , aiInterface - > getMyUnit ( bestIndex ) - > getType ( ) - > getName ( ) . c_str ( ) , ut - > getCommandType ( commandIndex ) - > getName ( ) . c_str ( ) ) ;
aiInterface - > printLog ( 4 , szBuf ) ;
}
2012-03-17 20:22:03 +01:00
2010-03-24 22:26:17 +01:00
aiInterface - > giveCommand ( bestIndex , ut - > getCommandType ( commandIndex ) ) ;
}
2011-06-24 18:14:39 +02:00
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 ( ) ) {
2013-11-02 12:04:52 +01:00
int bestCommandTypeCount = ( int ) producersDefaultCommandType [ bestIndex ] . size ( ) ;
2011-06-24 18:14:39 +02:00
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 ) ;
defCt = producersDefaultCommandType [ bestIndex ] [ bestCommandTypeIndex ] ;
2010-06-10 23:53:50 +02:00
}
2011-04-06 17:44:33 +02:00
2013-06-08 05:40:04 +02:00
if ( ai - > outputAIBehaviourToConsole ( ) ) printf ( " mega #2 produceSpecific giveCommand to unit [%s] commandType [%s] \n " , aiInterface - > getMyUnit ( bestIndex ) - > getType ( ) - > getName ( ) . c_str ( ) , ( defCt ! = NULL ? defCt - > getName ( ) . c_str ( ) : " n/a " ) ) ;
2013-01-12 02:11:53 +01:00
if ( aiInterface - > isLogLevelEnabled ( 4 ) = = true ) {
char szBuf [ 8096 ] = " " ;
2013-06-08 05:40:04 +02:00
snprintf ( szBuf , 8096 , " mega #2 produceSpecific giveCommand to unit [%s] commandType [%s] " , aiInterface - > getMyUnit ( bestIndex ) - > getType ( ) - > getName ( ) . c_str ( ) , ( defCt ! = NULL ? defCt - > getName ( ) . c_str ( ) : " n/a " ) ) ;
2013-01-12 02:11:53 +01:00
aiInterface - > printLog ( 4 , szBuf ) ;
}
2010-03-24 22:26:17 +01:00
aiInterface - > giveCommand ( bestIndex , defCt ) ;
}
}
2011-06-24 18:14:39 +02:00
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 ( ) ) {
2010-07-14 00:51:27 +02:00
//defCt = producersDefaultCommandType[bestIndex];
2013-11-02 12:04:52 +01:00
int bestCommandTypeCount = ( int ) producersDefaultCommandType [ bestIndex ] . size ( ) ;
2011-06-24 18:14:39 +02:00
int bestCommandTypeIndex = ai - > getRandom ( ) - > randRange ( 0 , bestCommandTypeCount - 1 ) ;
2010-07-14 00:51:27 +02:00
2011-06-24 18:14:39 +02:00
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 ) ;
2010-07-14 00:51:27 +02:00
2011-06-24 18:14:39 +02:00
defCt = producersDefaultCommandType [ bestIndex ] [ bestCommandTypeIndex ] ;
2010-06-10 23:53:50 +02:00
}
2011-04-06 17:44:33 +02:00
2013-06-08 05:40:04 +02:00
if ( ai - > outputAIBehaviourToConsole ( ) ) printf ( " mega #3 produceSpecific giveCommand to unit [%s] commandType [%s] \n " , aiInterface - > getMyUnit ( bestIndex ) - > getType ( ) - > getName ( ) . c_str ( ) , ( defCt ! = NULL ? defCt - > getName ( ) . c_str ( ) : " n/a " ) ) ;
2013-01-12 02:11:53 +01:00
if ( aiInterface - > isLogLevelEnabled ( 4 ) = = true ) {
char szBuf [ 8096 ] = " " ;
2013-06-08 05:40:04 +02:00
snprintf ( szBuf , 8096 , " mega #3 produceSpecific giveCommand to unit [%s] commandType [%s] " , aiInterface - > getMyUnit ( bestIndex ) - > getType ( ) - > getName ( ) . c_str ( ) , ( defCt ! = NULL ? defCt - > getName ( ) . c_str ( ) : " n/a " ) ) ;
2013-01-12 02:11:53 +01:00
aiInterface - > printLog ( 4 , szBuf ) ;
}
2012-03-17 20:22:03 +01:00
2010-03-24 22:26:17 +01:00
aiInterface - > giveCommand ( bestIndex , defCt ) ;
}
2011-06-24 18:14:39 +02:00
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 ( ) ) {
2010-07-14 00:51:27 +02:00
//defCt = producersDefaultCommandType[bestIndex];
2013-11-02 12:04:52 +01:00
int bestCommandTypeCount = ( int ) producersDefaultCommandType [ bestIndex ] . size ( ) ;
2011-06-24 18:14:39 +02:00
int bestCommandTypeIndex = ai - > getRandom ( ) - > randRange ( 0 , bestCommandTypeCount - 1 ) ;
2010-07-14 00:51:27 +02:00
2011-06-24 18:14:39 +02:00
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 ) ;
2010-07-14 00:51:27 +02:00
2011-06-24 18:14:39 +02:00
defCt = producersDefaultCommandType [ bestIndex ] [ bestCommandTypeIndex ] ;
2010-06-10 23:53:50 +02:00
}
2013-06-08 05:40:04 +02:00
if ( ai - > outputAIBehaviourToConsole ( ) ) printf ( " mega #4 produceSpecific giveCommand to unit [%s] commandType [%s] \n " , aiInterface - > getMyUnit ( bestIndex ) - > getType ( ) - > getName ( ) . c_str ( ) , ( defCt ! = NULL ? defCt - > getName ( ) . c_str ( ) : " n/a " ) ) ;
2013-01-12 02:11:53 +01:00
if ( aiInterface - > isLogLevelEnabled ( 4 ) = = true ) {
char szBuf [ 8096 ] = " " ;
2013-06-08 05:40:04 +02:00
snprintf ( szBuf , 8096 , " mega #4 produceSpecific giveCommand to unit [%s] commandType [%s] " , aiInterface - > getMyUnit ( bestIndex ) - > getType ( ) - > getName ( ) . c_str ( ) , ( defCt ! = NULL ? defCt - > getName ( ) . c_str ( ) : " n/a " ) ) ;
2013-01-12 02:11:53 +01:00
aiInterface - > printLog ( 4 , szBuf ) ;
}
2012-03-17 20:22:03 +01:00
2010-03-24 22:26:17 +01:00
aiInterface - > giveCommand ( bestIndex , defCt ) ;
}
}
2011-06-24 18:14:39 +02:00
else {
2013-11-02 12:04:52 +01:00
int pIndex = ai - > getRandom ( ) - > randRange ( 0 , ( int ) producers . size ( ) - 1 ) ;
2010-06-08 09:40:32 +02:00
int producerIndex = producers [ pIndex ] ;
2011-06-24 18:14:39 +02:00
defCt = NULL ;
if ( producersDefaultCommandType . find ( producerIndex ) ! = producersDefaultCommandType . end ( ) ) {
2010-07-14 00:51:27 +02:00
//defCt = producersDefaultCommandType[producerIndex];
2013-11-02 12:04:52 +01:00
int bestCommandTypeCount = ( int ) producersDefaultCommandType [ producerIndex ] . size ( ) ;
2011-06-24 18:14:39 +02:00
int bestCommandTypeIndex = ai - > getRandom ( ) - > randRange ( 0 , bestCommandTypeCount - 1 ) ;
2010-07-14 00:51:27 +02:00
2011-06-24 18:14:39 +02:00
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 ) ;
2010-07-14 00:51:27 +02:00
2011-06-24 18:14:39 +02:00
defCt = producersDefaultCommandType [ producerIndex ] [ bestCommandTypeIndex ] ;
2010-06-10 23:53:50 +02:00
}
2011-06-24 18:14:39 +02:00
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 ( ) ) ;
2011-04-06 17:44:33 +02:00
2013-06-08 01:17:27 +02:00
if ( ai - > outputAIBehaviourToConsole ( ) ) printf ( " produceSpecific giveCommand to unit [%s] commandType [%s] \n " , aiInterface - > getMyUnit ( producerIndex ) - > getType ( ) - > getName ( ) . c_str ( ) , ( defCt ! = NULL ? defCt - > getName ( ) . c_str ( ) : " n/a " ) ) ;
2013-01-12 02:11:53 +01:00
if ( aiInterface - > isLogLevelEnabled ( 4 ) = = true ) {
char szBuf [ 8096 ] = " " ;
2013-02-04 09:30:43 +01:00
snprintf ( szBuf , 8096 , " produceSpecific giveCommand to unit [%s] commandType [%s] " , aiInterface - > getMyUnit ( producerIndex ) - > getType ( ) - > getName ( ) . c_str ( ) , ( defCt ! = NULL ? defCt - > getName ( ) . c_str ( ) : " (null) " ) ) ;
2013-01-12 02:11:53 +01:00
aiInterface - > printLog ( 4 , szBuf ) ;
}
2010-03-24 22:26:17 +01:00
aiInterface - > giveCommand ( producerIndex , defCt ) ;
}
}
}
}
// ========================================
// class AiRuleBuild
// ========================================
2011-06-24 18:14:39 +02:00
AiRuleBuild : : AiRuleBuild ( Ai * ai ) :
AiRule ( ai )
{
2010-03-24 22:26:17 +01:00
buildTask = NULL ;
}
bool AiRuleBuild : : test ( ) {
const Task * task = ai - > getTask ( ) ;
2011-06-24 18:14:39 +02:00
if ( task = = NULL | | task - > getClass ( ) ! = tcBuild ) {
2010-03-24 22:26:17 +01:00
return false ;
}
2011-06-24 18:14:39 +02:00
buildTask = static_cast < const BuildTask * > ( task ) ;
2010-03-24 22:26:17 +01:00
return true ;
}
2011-06-24 18:14:39 +02:00
void AiRuleBuild : : execute ( ) {
if ( buildTask ! = NULL ) {
2013-06-13 10:55:48 +02:00
if ( ai - > outputAIBehaviourToConsole ( ) ) printf ( " BUILD AiRuleBuild Unit Name[%s] \n " , ( buildTask - > getUnitType ( ) ! = NULL ? buildTask - > getUnitType ( ) - > getName ( false ) . c_str ( ) : " null " ) ) ;
2010-03-24 22:26:17 +01:00
//generic build task, build random building that can be built
2011-06-24 18:14:39 +02:00
if ( buildTask - > getUnitType ( ) = = NULL ) {
2010-03-24 22:26:17 +01:00
buildGeneric ( buildTask ) ;
}
//specific building task, build if possible, retry if not enough resources or not position
2011-06-24 18:14:39 +02:00
else {
2010-03-24 22:26:17 +01:00
buildSpecific ( buildTask ) ;
}
//remove the task
ai - > removeTask ( buildTask ) ;
}
}
2011-10-04 08:49:44 +02:00
void AiRuleBuild : : buildGeneric ( const BuildTask * bt ) {
2011-01-02 01:39:13 +01:00
2010-03-24 22:26:17 +01:00
//find buildings that can be built
AiInterface * aiInterface = ai - > getAiInterface ( ) ;
2013-08-10 08:53:16 +02:00
if ( aiInterface - > isLogLevelEnabled ( 4 ) = = true ) {
char szBuf [ 8096 ] = " " ;
snprintf ( szBuf , 8096 , " == START: buildGeneric for resource type [%s] " , ( bt - > getResourceType ( ) ! = NULL ? bt - > getResourceType ( ) - > getName ( ) . c_str ( ) : " null " ) ) ;
aiInterface - > printLog ( 4 , szBuf ) ;
}
2011-01-02 01:39:13 +01:00
typedef vector < const UnitType * > UnitTypes ;
2010-03-24 22:26:17 +01:00
UnitTypes buildings ;
2011-01-02 01:39:13 +01:00
2011-10-04 08:49:44 +02:00
if ( bt - > getResourceType ( ) ! = NULL ) {
if ( aiInterface - > getMyFactionType ( ) - > getAIBehaviorUnits ( aibcResourceProducerUnits ) . size ( ) > 0 ) {
const std : : vector < FactionType : : PairPUnitTypeInt > & unitList = aiInterface - > getMyFactionType ( ) - > getAIBehaviorUnits ( aibcResourceProducerUnits ) ;
for ( unsigned int i = 0 ; i < unitList . size ( ) ; + + i ) {
const FactionType : : PairPUnitTypeInt & priorityUnit = unitList [ i ] ;
if ( ai - > getCountOfType ( priorityUnit . first ) < priorityUnit . second & &
aiInterface - > getMyFaction ( ) - > canCreateUnit ( priorityUnit . first , true , false , false ) = = true ) {
//if(ai->getRandom()->randRange(0, 1)==0) {
2013-08-10 08:53:16 +02:00
if ( aiInterface - > isLogLevelEnabled ( 4 ) = = true ) {
char szBuf [ 8096 ] = " " ;
snprintf ( szBuf , 8096 , " In buildGeneric for resource type [%s] aibcResourceProducerUnits = " MG_SIZE_T_SPECIFIER " priorityUnit.first: [%s] \n " , bt - > getResourceType ( ) - > getName ( ) . c_str ( ) , unitList . size ( ) , priorityUnit . first - > getName ( ) . c_str ( ) ) ;
aiInterface - > printLog ( 4 , szBuf ) ;
}
2011-10-04 08:49:44 +02:00
ai - > addTask ( new BuildTask ( priorityUnit . first ) ) ;
return ;
//}
}
}
}
}
else {
if ( aiInterface - > getMyFactionType ( ) - > getAIBehaviorUnits ( aibcBuildingUnits ) . size ( ) > 0 ) {
const std : : vector < FactionType : : PairPUnitTypeInt > & unitList = aiInterface - > getMyFactionType ( ) - > getAIBehaviorUnits ( aibcBuildingUnits ) ;
for ( unsigned int i = 0 ; i < unitList . size ( ) ; + + i ) {
const FactionType : : PairPUnitTypeInt & priorityUnit = unitList [ i ] ;
if ( ai - > getCountOfType ( priorityUnit . first ) < priorityUnit . second & &
aiInterface - > getMyFaction ( ) - > canCreateUnit ( priorityUnit . first , true , false , false ) = = true ) {
//if(ai->getRandom()->randRange(0, 1)==0) {
2013-08-10 08:53:16 +02:00
if ( aiInterface - > isLogLevelEnabled ( 4 ) = = true ) {
char szBuf [ 8096 ] = " " ;
snprintf ( szBuf , 8096 , " In buildGeneric for resource type [%s] aibcBuildingUnits = " MG_SIZE_T_SPECIFIER " priorityUnit.first: [%s] \n " , bt - > getResourceType ( ) - > getName ( ) . c_str ( ) , unitList . size ( ) , priorityUnit . first - > getName ( ) . c_str ( ) ) ;
aiInterface - > printLog ( 4 , szBuf ) ;
}
2011-10-04 08:49:44 +02:00
ai - > addTask ( new BuildTask ( priorityUnit . first ) ) ;
return ;
//}
}
}
}
}
2010-03-24 22:26:17 +01:00
//for each unit
2011-06-24 18:14:39 +02:00
for ( int i = 0 ; i < aiInterface - > getMyUnitCount ( ) ; + + i ) {
2011-01-02 01:39:13 +01:00
2010-03-24 22:26:17 +01:00
//for each command
const UnitType * ut = aiInterface - > getMyUnit ( i ) - > getType ( ) ;
2011-06-24 18:14:39 +02:00
for ( int j = 0 ; j < ut - > getCommandTypeCount ( ) ; + + j ) {
2010-03-24 22:26:17 +01:00
const CommandType * ct = ut - > getCommandType ( j ) ;
2011-01-02 01:39:13 +01:00
2010-03-24 22:26:17 +01:00
//if the command is build
2011-06-24 18:14:39 +02:00
if ( ct - > getClass ( ) = = ccBuild ) {
const BuildCommandType * bct = static_cast < const BuildCommandType * > ( ct ) ;
2011-01-02 01:39:13 +01:00
2010-03-24 22:26:17 +01:00
//for each building
2011-06-24 18:14:39 +02:00
for ( int k = 0 ; k < bct - > getBuildingCount ( ) ; + + k ) {
2011-01-02 01:39:13 +01:00
const UnitType * building = bct - > getBuilding ( k ) ;
2010-03-24 22:26:17 +01:00
if ( aiInterface - > reqsOk ( bct ) & & aiInterface - > reqsOk ( building ) ) {
2011-01-02 01:39:13 +01:00
2010-03-24 22:26:17 +01:00
//if any building, or produces resource
const ResourceType * rt = bt - > getResourceType ( ) ;
const Resource * cost = building - > getCost ( rt ) ;
2011-06-24 18:14:39 +02:00
if ( rt = = NULL | | ( cost ! = NULL & & cost - > getAmount ( ) < 0 ) ) {
2013-08-10 08:53:16 +02:00
if ( find ( buildings . begin ( ) , buildings . end ( ) , building ) = = buildings . end ( ) ) {
buildings . push_back ( building ) ;
}
2010-03-24 22:26:17 +01:00
}
}
}
}
}
}
2013-08-10 08:53:16 +02:00
if ( aiInterface - > isLogLevelEnabled ( 4 ) = = true ) {
2013-11-19 07:14:06 +01:00
for ( int i = 0 ; i < ( int ) buildings . size ( ) ; + + i ) {
2013-08-10 08:53:16 +02:00
char szBuf [ 8096 ] = " " ;
snprintf ( szBuf , 8096 , " In buildGeneric i = %d unit type: [%s] \n " , i , buildings [ i ] - > getName ( ) . c_str ( ) ) ;
aiInterface - > printLog ( 4 , szBuf ) ;
}
}
2010-03-24 22:26:17 +01:00
//add specific build task
buildBestBuilding ( buildings ) ;
}
void AiRuleBuild : : buildBestBuilding ( const vector < const UnitType * > & buildings ) {
2011-01-02 01:39:13 +01:00
2013-08-10 08:53:16 +02:00
AiInterface * aiInterface = ai - > getAiInterface ( ) ;
if ( aiInterface - > isLogLevelEnabled ( 4 ) = = true ) {
char szBuf [ 8096 ] = " " ;
snprintf ( szBuf , 8096 , " ==> START buildBestBuilding buildings.size = " MG_SIZE_T_SPECIFIER " \n " , buildings . size ( ) ) ;
aiInterface - > printLog ( 4 , szBuf ) ;
}
2010-03-24 22:26:17 +01:00
if ( ! buildings . empty ( ) ) {
//build the least built building
bool buildingFound = false ;
2011-06-24 18:14:39 +02:00
for ( int i = 0 ; i < 10 & & ! buildingFound ; + + i ) {
2011-01-02 01:39:13 +01:00
2011-06-24 18:14:39 +02:00
if ( i > 0 ) {
2010-03-24 22:26:17 +01:00
//Defensive buildings have priority
2013-11-19 07:14:06 +01:00
for ( int j = 0 ; j < ( int ) buildings . size ( ) & & buildingFound = = false ; + + j ) {
2010-03-24 22:26:17 +01:00
const UnitType * building = buildings [ j ] ;
2013-08-10 08:53:16 +02:00
if ( ai - > getCountOfType ( building ) < = i + 1 & & isDefensive ( building ) ) {
if ( aiInterface - > isLogLevelEnabled ( 4 ) = = true ) {
char szBuf [ 8096 ] = " " ;
snprintf ( szBuf , 8096 , " In buildBestBuilding defensive building unit type: [%s] i = %d j = %d \n " , building - > getName ( ) . c_str ( ) , i , j ) ;
aiInterface - > printLog ( 4 , szBuf ) ;
}
2010-03-24 22:26:17 +01:00
ai - > addTask ( new BuildTask ( building ) ) ;
buildingFound = true ;
}
}
//Warrior producers next
2011-06-24 18:14:39 +02:00
for ( unsigned int j = 0 ; j < buildings . size ( ) & & ! buildingFound ; + + j ) {
2010-03-24 22:26:17 +01:00
const UnitType * building = buildings [ j ] ;
2013-08-10 08:53:16 +02:00
if ( ai - > getCountOfType ( building ) < = i + 1 & & isWarriorProducer ( building ) ) {
if ( aiInterface - > isLogLevelEnabled ( 4 ) = = true ) {
char szBuf [ 8096 ] = " " ;
2013-09-19 00:44:04 +02:00
snprintf ( szBuf , 8096 , " In buildBestBuilding warriorproducer building unit type: [%s] i = %d j = %u \n " , building - > getName ( ) . c_str ( ) , i , j ) ;
2013-08-10 08:53:16 +02:00
aiInterface - > printLog ( 4 , szBuf ) ;
}
2010-03-24 22:26:17 +01:00
ai - > addTask ( new BuildTask ( building ) ) ;
buildingFound = true ;
}
}
//Resource producers next
2011-06-24 18:14:39 +02:00
for ( unsigned int j = 0 ; j < buildings . size ( ) & & ! buildingFound ; + + j ) {
2010-03-24 22:26:17 +01:00
const UnitType * building = buildings [ j ] ;
2013-08-10 08:53:16 +02:00
if ( ai - > getCountOfType ( building ) < = i + 1 & & isResourceProducer ( building ) ) {
if ( aiInterface - > isLogLevelEnabled ( 4 ) = = true ) {
char szBuf [ 8096 ] = " " ;
2013-09-19 00:44:04 +02:00
snprintf ( szBuf , 8096 , " In buildBestBuilding resourceproducer building unit type: [%s] i = %d j = %u \n " , building - > getName ( ) . c_str ( ) , i , j ) ;
2013-08-10 08:53:16 +02:00
aiInterface - > printLog ( 4 , szBuf ) ;
}
2010-03-24 22:26:17 +01:00
ai - > addTask ( new BuildTask ( building ) ) ;
buildingFound = true ;
}
}
}
//Any building
2011-06-24 18:14:39 +02:00
for ( unsigned int j = 0 ; j < buildings . size ( ) & & ! buildingFound ; + + j ) {
2010-03-24 22:26:17 +01:00
const UnitType * building = buildings [ j ] ;
2013-08-10 08:53:16 +02:00
if ( ai - > getCountOfType ( building ) < = i ) {
if ( aiInterface - > isLogLevelEnabled ( 4 ) = = true ) {
char szBuf [ 8096 ] = " " ;
2013-09-19 00:44:04 +02:00
snprintf ( szBuf , 8096 , " In buildBestBuilding ANY building unit type: [%s] i = %d j = %u \n " , building - > getName ( ) . c_str ( ) , i , j ) ;
2013-08-10 08:53:16 +02:00
aiInterface - > printLog ( 4 , szBuf ) ;
}
2010-03-24 22:26:17 +01:00
ai - > addTask ( new BuildTask ( building ) ) ;
buildingFound = true ;
}
}
}
}
2013-08-10 08:53:16 +02:00
if ( aiInterface - > isLogLevelEnabled ( 4 ) = = true ) {
char szBuf [ 8096 ] = " " ;
snprintf ( szBuf , 8096 , " ==> END buildBestBuilding buildings.size = " MG_SIZE_T_SPECIFIER " \n " , buildings . size ( ) ) ;
aiInterface - > printLog ( 4 , szBuf ) ;
}
2010-03-24 22:26:17 +01:00
}
2011-06-24 18:14:39 +02:00
void AiRuleBuild : : buildSpecific ( const BuildTask * bt ) {
2010-03-24 22:26:17 +01:00
AiInterface * aiInterface = ai - > getAiInterface ( ) ;
2013-08-10 08:53:16 +02:00
if ( aiInterface - > isLogLevelEnabled ( 4 ) = = true ) {
char szBuf [ 8096 ] = " " ;
snprintf ( szBuf , 8096 , " == START: buildSpecific for resource type [%s] bt->getUnitType() [%s] " , ( bt - > getResourceType ( ) ! = NULL ? bt - > getResourceType ( ) - > getName ( ) . c_str ( ) : " null " ) , ( bt - > getUnitType ( ) ! = NULL ? bt - > getUnitType ( ) - > getName ( false ) . c_str ( ) : " null " ) ) ;
aiInterface - > printLog ( 4 , szBuf ) ;
}
2010-03-24 22:26:17 +01:00
//if reqs ok
2011-06-24 18:14:39 +02:00
if ( aiInterface - > reqsOk ( bt - > getUnitType ( ) ) ) {
2010-03-24 22:26:17 +01:00
//retry if not enough resources
2012-03-27 05:23:03 +02:00
if ( aiInterface - > checkCosts ( bt - > getUnitType ( ) , NULL ) = = false ) {
2013-08-10 08:53:16 +02:00
if ( aiInterface - > isLogLevelEnabled ( 4 ) = = true ) {
char szBuf [ 8096 ] = " " ;
snprintf ( szBuf , 8096 , " In buildSpecific for resource type [%s] checkcosts == false RETRYING " , ( bt - > getResourceType ( ) ! = NULL ? bt - > getResourceType ( ) - > getName ( ) . c_str ( ) : " null " ) ) ;
aiInterface - > printLog ( 4 , szBuf ) ;
}
2010-03-24 22:26:17 +01:00
ai - > retryTask ( bt ) ;
return ;
}
vector < int > builders ;
2010-07-14 00:51:27 +02:00
// Hold a list of units which can build
// then a list of build commandtypes for each unit
2011-06-24 18:14:39 +02:00
map < int , vector < const BuildCommandType * > > buildersDefaultCommandType ;
2010-03-24 22:26:17 +01:00
const BuildCommandType * defBct = NULL ;
2011-01-02 01:39:13 +01:00
2010-03-24 22:26:17 +01:00
//for each unit
2011-06-24 18:14:39 +02:00
for ( int i = 0 ; i < aiInterface - > getMyUnitCount ( ) ; + + i ) {
2011-01-02 01:39:13 +01:00
2010-03-24 22:26:17 +01:00
//if the unit is not going to build
2011-06-24 18:14:39 +02:00
const Unit * u = aiInterface - > getMyUnit ( i ) ;
if ( u - > anyCommand ( ) = = false | | u - > getCurrCommand ( ) - > getCommandType ( ) - > getClass ( ) ! = ccBuild ) {
2010-03-24 22:26:17 +01:00
//for each command
const UnitType * ut = aiInterface - > getMyUnit ( i ) - > getType ( ) ;
2011-06-24 18:14:39 +02:00
for ( int j = 0 ; j < ut - > getCommandTypeCount ( ) ; + + j ) {
2010-03-24 22:26:17 +01:00
const CommandType * ct = ut - > getCommandType ( j ) ;
2011-01-02 01:39:13 +01:00
2010-03-24 22:26:17 +01:00
//if the command is build
2011-06-24 18:14:39 +02:00
if ( ct - > getClass ( ) = = ccBuild ) {
const BuildCommandType * bct = static_cast < const BuildCommandType * > ( ct ) ;
2011-01-02 01:39:13 +01:00
2010-03-24 22:26:17 +01:00
//for each building
2011-06-24 18:14:39 +02:00
for ( int k = 0 ; k < bct - > getBuildingCount ( ) ; + + k ) {
2011-01-02 01:39:13 +01:00
const UnitType * building = bct - > getBuilding ( k ) ;
2010-03-24 22:26:17 +01:00
//if building match
2011-06-24 18:14:39 +02:00
if ( bt - > getUnitType ( ) = = building ) {
if ( aiInterface - > reqsOk ( bct ) ) {
2010-03-24 22:26:17 +01:00
builders . push_back ( i ) ;
2010-07-14 00:51:27 +02:00
buildersDefaultCommandType [ i ] . push_back ( bct ) ;
2010-03-24 22:26:17 +01:00
defBct = bct ;
}
}
}
}
}
}
}
//use random builder to build
2011-06-24 18:14:39 +02:00
if ( builders . empty ( ) = = false ) {
2013-11-02 12:04:52 +01:00
int bIndex = ai - > getRandom ( ) - > randRange ( 0 , ( int ) builders . size ( ) - 1 ) ;
2010-06-08 09:40:32 +02:00
int builderIndex = builders [ bIndex ] ;
2011-01-02 01:39:13 +01:00
Vec2i pos ;
2011-06-24 18:14:39 +02:00
Vec2i searchPos = bt - > getForcePos ( ) ? bt - > getPos ( ) : ai - > getRandomHomePosition ( ) ;
if ( bt - > getForcePos ( ) = = false ) {
const int enemySightDistanceToAvoid = 18 ;
2011-05-01 09:24:37 +02:00
vector < Unit * > enemies ;
2011-06-24 18:14:39 +02:00
ai - > getAiInterface ( ) - > getWorld ( ) - > getUnitUpdater ( ) - > findEnemiesForCell ( searchPos , bt - > getUnitType ( ) - > getSize ( ) , enemySightDistanceToAvoid , ai - > getAiInterface ( ) - > getMyFaction ( ) , enemies , true ) ;
2011-09-01 01:10:43 +02:00
if ( enemies . empty ( ) = = false ) {
for ( int i1 = 0 ; i1 < 25 & & enemies . empty ( ) = = false ; + + i1 ) {
for ( int j1 = 0 ; j1 < 25 & & enemies . empty ( ) = = false ; + + j1 ) {
2011-06-24 18:14:39 +02:00
Vec2i tryPos = searchPos + Vec2i ( i1 , j1 ) ;
const int spacing = 1 ;
if ( ai - > getAiInterface ( ) - > isFreeCells ( tryPos - Vec2i ( spacing ) , bt - > getUnitType ( ) - > getSize ( ) + spacing * 2 , fLand ) ) {
2011-05-01 09:24:37 +02:00
enemies . clear ( ) ;
2011-06-24 18:14:39 +02:00
ai - > getAiInterface ( ) - > getWorld ( ) - > getUnitUpdater ( ) - > findEnemiesForCell ( tryPos , bt - > getUnitType ( ) - > getSize ( ) , enemySightDistanceToAvoid , ai - > getAiInterface ( ) - > getMyFaction ( ) , enemies , true ) ;
2012-10-06 09:06:40 +02:00
if ( enemies . empty ( ) = = true ) {
2011-06-24 18:14:39 +02:00
searchPos = tryPos ;
2011-05-01 09:24:37 +02:00
}
}
}
}
}
2011-09-01 01:10:43 +02:00
if ( enemies . empty ( ) = = false ) {
for ( int i1 = - 1 ; i1 > = - 25 & & enemies . empty ( ) = = false ; - - i1 ) {
for ( int j1 = - 1 ; j1 > = - 25 & & enemies . empty ( ) = = false ; - - j1 ) {
2011-06-24 18:14:39 +02:00
Vec2i tryPos = searchPos + Vec2i ( i1 , j1 ) ;
const int spacing = 1 ;
if ( ai - > getAiInterface ( ) - > isFreeCells ( tryPos - Vec2i ( spacing ) , bt - > getUnitType ( ) - > getSize ( ) + spacing * 2 , fLand ) ) {
2011-05-01 09:24:37 +02:00
enemies . clear ( ) ;
2011-06-24 18:14:39 +02:00
ai - > getAiInterface ( ) - > getWorld ( ) - > getUnitUpdater ( ) - > findEnemiesForCell ( tryPos , bt - > getUnitType ( ) - > getSize ( ) , enemySightDistanceToAvoid , ai - > getAiInterface ( ) - > getMyFaction ( ) , enemies , true ) ;
2012-10-06 09:06:40 +02:00
if ( enemies . empty ( ) = = true ) {
2011-06-24 18:14:39 +02:00
searchPos = tryPos ;
2011-05-01 09:24:37 +02:00
}
}
}
}
}
}
2010-03-24 22:26:17 +01:00
//if free pos give command, else retry
2011-06-24 18:14:39 +02:00
if ( ai - > findPosForBuilding ( bt - > getUnitType ( ) , searchPos , pos ) ) {
defBct = NULL ;
if ( buildersDefaultCommandType . find ( builderIndex ) ! = buildersDefaultCommandType . end ( ) ) {
2010-07-14 00:51:27 +02:00
//defBct = buildersDefaultCommandType[builderIndex];
2013-11-02 12:04:52 +01:00
int bestCommandTypeCount = ( int ) buildersDefaultCommandType [ builderIndex ] . size ( ) ;
2011-06-24 18:14:39 +02:00
int bestCommandTypeIndex = ai - > getRandom ( ) - > randRange ( 0 , bestCommandTypeCount - 1 ) ;
2010-07-14 00:51:27 +02:00
2011-06-24 18:14:39 +02:00
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 ) ;
2010-07-14 00:51:27 +02:00
2011-06-24 18:14:39 +02:00
defBct = buildersDefaultCommandType [ builderIndex ] [ bestCommandTypeIndex ] ;
2010-06-10 23:53:50 +02:00
}
2011-06-24 18:14:39 +02:00
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 ) ;
2010-06-08 09:40:32 +02:00
2010-03-24 22:26:17 +01:00
aiInterface - > giveCommand ( builderIndex , defBct , pos , bt - > getUnitType ( ) ) ;
}
2011-06-24 18:14:39 +02:00
else {
2010-03-24 22:26:17 +01:00
ai - > retryTask ( bt ) ;
return ;
}
}
}
2013-08-10 08:53:16 +02:00
else {
if ( aiInterface - > isLogLevelEnabled ( 4 ) = = true ) {
char szBuf [ 8096 ] = " " ;
snprintf ( szBuf , 8096 , " In buildSpecific for resource type [%s] reqsok == false " , ( bt - > getResourceType ( ) ! = NULL ? bt - > getResourceType ( ) - > getName ( ) . c_str ( ) : " null " ) ) ;
aiInterface - > printLog ( 4 , szBuf ) ;
}
}
2010-03-24 22:26:17 +01:00
}
bool AiRuleBuild : : isDefensive ( const UnitType * building ) {
2013-06-13 10:55:48 +02:00
if ( ai - > outputAIBehaviourToConsole ( ) ) printf ( " BUILD isDefensive check for Unit Name[%s] result = %d \n " , building - > getName ( false ) . c_str ( ) , building - > hasSkillClass ( scAttack ) ) ;
2011-04-06 17:44:33 +02:00
2010-03-24 22:26:17 +01:00
return building - > hasSkillClass ( scAttack ) ;
}
bool AiRuleBuild : : isResourceProducer ( const UnitType * building ) {
2011-06-24 18:14:39 +02:00
for ( int i = 0 ; i < building - > getCostCount ( ) ; i + + ) {
if ( building - > getCost ( i ) - > getAmount ( ) < 0 ) {
2013-06-13 10:55:48 +02:00
if ( ai - > outputAIBehaviourToConsole ( ) ) printf ( " BUILD isResourceProducer check for Unit Name[%s] result = true \n " , building - > getName ( false ) . c_str ( ) ) ;
2013-01-12 02:11:53 +01:00
2010-03-24 22:26:17 +01:00
return true ;
}
}
2013-06-13 10:55:48 +02:00
if ( ai - > outputAIBehaviourToConsole ( ) ) printf ( " BUILD isResourceProducer check for Unit Name[%s] result = false \n " , building - > getName ( false ) . c_str ( ) ) ;
2012-03-17 20:22:03 +01:00
2010-03-24 22:26:17 +01:00
return false ;
}
bool AiRuleBuild : : isWarriorProducer ( const UnitType * building ) {
for ( int i = 0 ; i < building - > getCommandTypeCount ( ) ; i + + ) {
const CommandType * ct = building - > getCommandType ( i ) ;
if ( ct - > getClass ( ) = = ccProduce ) {
2011-06-24 18:14:39 +02:00
const UnitType * ut = static_cast < const ProduceCommandType * > ( ct ) - > getProducedUnit ( ) ;
2010-03-24 22:26:17 +01:00
if ( ut - > isOfClass ( ucWarrior ) ) {
2013-06-13 10:55:48 +02:00
if ( ai - > outputAIBehaviourToConsole ( ) ) printf ( " BUILD isWarriorProducer check for Unit Name[%s] result = true \n " , building - > getName ( false ) . c_str ( ) ) ;
2013-01-12 02:11:53 +01:00
2010-03-24 22:26:17 +01:00
return true ;
}
}
}
2013-06-13 10:55:48 +02:00
if ( ai - > outputAIBehaviourToConsole ( ) ) printf ( " BUILD isWarriorProducer check for Unit Name[%s] result = false \n " , building - > getName ( false ) . c_str ( ) ) ;
2012-03-17 20:22:03 +01:00
2010-03-24 22:26:17 +01:00
return false ;
}
// ========================================
// class AiRuleUpgrade
// ========================================
2011-06-24 18:14:39 +02:00
AiRuleUpgrade : : AiRuleUpgrade ( Ai * ai ) :
AiRule ( ai )
{
2010-03-24 22:26:17 +01:00
upgradeTask = NULL ;
}
bool AiRuleUpgrade : : test ( ) {
const Task * task = ai - > getTask ( ) ;
2011-06-24 18:14:39 +02:00
if ( task = = NULL | | task - > getClass ( ) ! = tcUpgrade ) {
2010-03-24 22:26:17 +01:00
return false ;
}
2011-06-24 18:14:39 +02:00
upgradeTask = static_cast < const UpgradeTask * > ( task ) ;
2010-03-24 22:26:17 +01:00
return true ;
}
void AiRuleUpgrade : : execute ( ) {
//upgrade any upgrade
2011-06-24 18:14:39 +02:00
if ( upgradeTask - > getUpgradeType ( ) = = NULL ) {
2010-03-24 22:26:17 +01:00
upgradeGeneric ( upgradeTask ) ;
}
//upgrade specific upgrade
else {
upgradeSpecific ( upgradeTask ) ;
}
//remove the task
ai - > removeTask ( upgradeTask ) ;
}
void AiRuleUpgrade : : upgradeGeneric ( const UpgradeTask * upgt ) {
2011-01-02 01:39:13 +01:00
typedef vector < const UpgradeType * > UpgradeTypes ;
2010-03-24 22:26:17 +01:00
AiInterface * aiInterface = ai - > getAiInterface ( ) ;
2011-01-02 01:39:13 +01:00
2010-03-24 22:26:17 +01:00
//find upgrades that can be upgraded
UpgradeTypes upgrades ;
2011-01-02 01:39:13 +01:00
2011-10-04 19:15:08 +02:00
if ( aiInterface - > getMyFactionType ( ) - > getAIBehaviorUpgrades ( ) . size ( ) > 0 ) {
const std : : vector < const UpgradeType * > & upgradeList = aiInterface - > getMyFactionType ( ) - > getAIBehaviorUpgrades ( ) ;
for ( unsigned int i = 0 ; i < upgradeList . size ( ) ; + + i ) {
const UpgradeType * priorityUpgrade = upgradeList [ i ] ;
//for each upgrade, upgrade it if possible
for ( int k = 0 ; k < aiInterface - > getMyUnitCount ( ) ; + + k ) {
//for each command
const UnitType * ut = aiInterface - > getMyUnit ( k ) - > getType ( ) ;
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 = dynamic_cast < const UpgradeCommandType * > ( ct ) ;
if ( upgct ! = NULL ) {
const UpgradeType * upgrade = upgct - > getProducedUpgrade ( ) ;
if ( upgrade = = priorityUpgrade ) {
if ( aiInterface - > reqsOk ( upgct ) = = true & &
aiInterface - > getMyFaction ( ) - > getUpgradeManager ( ) - > isUpgradingOrUpgraded ( priorityUpgrade ) = = false ) {
//if(ai->getRandom()->randRange(0, 1)==0) {
ai - > addTask ( new UpgradeTask ( priorityUpgrade ) ) ;
return ;
//}
}
}
}
}
}
}
}
}
2010-03-24 22:26:17 +01:00
//for each upgrade, upgrade it if possible
2011-06-24 18:14:39 +02:00
for ( int i = 0 ; i < aiInterface - > getMyUnitCount ( ) ; + + i ) {
2011-01-02 01:39:13 +01:00
2010-03-24 22:26:17 +01:00
//for each command
const UnitType * ut = aiInterface - > getMyUnit ( i ) - > getType ( ) ;
2011-06-24 18:14:39 +02:00
for ( int j = 0 ; j < ut - > getCommandTypeCount ( ) ; + + j ) {
2010-03-24 22:26:17 +01:00
const CommandType * ct = ut - > getCommandType ( j ) ;
2011-01-02 01:39:13 +01:00
2010-03-24 22:26:17 +01:00
//if the command is upgrade
2011-06-24 18:14:39 +02:00
if ( ct - > getClass ( ) = = ccUpgrade ) {
const UpgradeCommandType * upgct = static_cast < const UpgradeCommandType * > ( ct ) ;
2010-03-24 22:26:17 +01:00
const UpgradeType * upgrade = upgct - > getProducedUpgrade ( ) ;
if ( aiInterface - > reqsOk ( upgct ) ) {
upgrades . push_back ( upgrade ) ;
}
}
}
}
//add specific upgrade task
if ( ! upgrades . empty ( ) ) {
2013-11-02 12:04:52 +01:00
ai - > addTask ( new UpgradeTask ( upgrades [ ai - > getRandom ( ) - > randRange ( 0 , ( int ) upgrades . size ( ) - 1 ) ] ) ) ;
2010-03-24 22:26:17 +01:00
}
}
void AiRuleUpgrade : : upgradeSpecific ( const UpgradeTask * upgt ) {
2011-01-02 01:39:13 +01:00
2010-03-24 22:26:17 +01:00
AiInterface * aiInterface = ai - > getAiInterface ( ) ;
//if reqs ok
if ( aiInterface - > reqsOk ( upgt - > getUpgradeType ( ) ) ) {
//if resources dont meet retry
2012-03-27 05:23:03 +02:00
if ( ! aiInterface - > checkCosts ( upgt - > getUpgradeType ( ) , NULL ) ) {
2010-03-24 22:26:17 +01:00
ai - > retryTask ( upgt ) ;
return ;
}
//for each unit
2011-06-24 18:14:39 +02:00
for ( int i = 0 ; i < aiInterface - > getMyUnitCount ( ) ; + + i ) {
2011-01-02 01:39:13 +01:00
2010-03-24 22:26:17 +01:00
//for each command
const UnitType * ut = aiInterface - > getMyUnit ( i ) - > getType ( ) ;
2011-06-24 18:14:39 +02:00
for ( int j = 0 ; j < ut - > getCommandTypeCount ( ) ; + + j ) {
2010-03-24 22:26:17 +01:00
const CommandType * ct = ut - > getCommandType ( j ) ;
2011-01-02 01:39:13 +01:00
2010-03-24 22:26:17 +01:00
//if the command is upgrade
2011-06-24 18:14:39 +02:00
if ( ct - > getClass ( ) = = ccUpgrade ) {
const UpgradeCommandType * uct = static_cast < const UpgradeCommandType * > ( ct ) ;
2010-03-24 22:26:17 +01:00
const UpgradeType * producedUpgrade = uct - > getProducedUpgrade ( ) ;
2011-01-02 01:39:13 +01:00
2010-03-24 22:26:17 +01:00
//if upgrades match
if ( producedUpgrade = = upgt - > getUpgradeType ( ) ) {
if ( aiInterface - > reqsOk ( uct ) ) {
2011-06-24 18:14:39 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-03-24 22:26:17 +01:00
aiInterface - > giveCommand ( i , uct ) ;
}
2011-01-02 01:39:13 +01:00
}
2010-03-24 22:26:17 +01:00
}
}
}
}
}
// ========================================
// class AiRuleExpand
// ========================================
2011-06-24 18:14:39 +02:00
AiRuleExpand : : AiRuleExpand ( Ai * ai ) :
AiRule ( ai )
{
2010-03-24 22:26:17 +01:00
storeType = NULL ;
}
2011-06-24 18:14:39 +02:00
bool AiRuleExpand : : test ( ) {
AiInterface * aiInterface = ai - > getAiInterface ( ) ;
2010-03-24 22:26:17 +01:00
2011-06-24 18:14:39 +02:00
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 ) {
2011-04-06 17:44:33 +02:00
// If any resource sighted
2011-06-24 18:14:39 +02:00
if ( aiInterface - > getNearestSightedResource ( rt , aiInterface - > getHomeLocation ( ) , expandPos , true ) ) {
2011-04-06 17:44:33 +02:00
int minDistance = INT_MAX ;
storeType = NULL ;
//If there is no close store
2011-06-24 18:14:39 +02:00
for ( int j = 0 ; j < unitCount ; + + j ) {
2011-04-06 17:44:33 +02:00
const Unit * u = aiInterface - > getMyUnit ( j ) ;
const UnitType * ut = u - > getType ( ) ;
// If this building is a store
2011-06-24 18:14:39 +02:00
if ( ut - > getStore ( rt ) > 0 ) {
storeType = ut ;
2013-02-04 09:30:43 +01:00
int distance = static_cast < int > ( u - > getPosNotThreadSafe ( ) . dist ( expandPos ) ) ;
2011-06-24 18:14:39 +02:00
if ( distance < minDistance ) {
minDistance = distance ;
2011-04-06 17:44:33 +02:00
}
2010-03-24 22:26:17 +01:00
}
}
2011-06-24 18:14:39 +02:00
if ( minDistance > expandDistance ) {
2011-04-06 17:44:33 +02:00
return true ;
}
}
2011-06-24 18:14:39 +02:00
else {
2011-04-06 17:44:33 +02:00
// send patrol to look for resource
ai - > sendScoutPatrol ( ) ;
2010-03-24 22:26:17 +01:00
}
}
}
}
2011-01-02 01:39:13 +01:00
2010-03-24 22:26:17 +01:00
return false ;
}
void AiRuleExpand : : execute ( ) {
ai - > addExpansion ( expandPos ) ;
ai - > addPriorityTask ( new BuildTask ( storeType , expandPos ) ) ;
}
2011-06-24 18:14:39 +02:00
2011-02-25 17:32:27 +01:00
// ========================================
// class AiRuleUnBlock
// ========================================
2011-06-24 18:14:39 +02:00
AiRuleUnBlock : : AiRuleUnBlock ( Ai * ai ) :
AiRule ( ai )
{
2011-02-25 17:32:27 +01:00
}
2011-06-24 18:14:39 +02:00
bool AiRuleUnBlock : : test ( ) {
2011-02-25 17:32:27 +01:00
return ai - > haveBlockedUnits ( ) ;
}
void AiRuleUnBlock : : execute ( ) {
ai - > unblockUnits ( ) ;
}
2011-06-24 18:14:39 +02:00
} } //end namespace