2010-03-24 22:26:17 +01:00
// ==============================================================
// This file is part of Glest (www.glest.org)
//
2010-09-14 21:10:37 +02:00
// Copyright (C) 2001-2008 Martio Figueroa
2010-03-24 22:26:17 +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
// License, or (at your option) any later version
// ==============================================================
# include "ai_interface.h"
# include "ai.h"
# include "command_type.h"
# include "faction.h"
# include "unit.h"
# include "unit_type.h"
# include "object.h"
# include "game.h"
# include "config.h"
2010-06-03 00:06:10 +02:00
# include "network_manager.h"
2010-03-24 22:26:17 +01:00
# include "leak_dumper.h"
using namespace Shared : : Util ;
using namespace Shared : : Graphics ;
// =====================================================
// class AiInterface
// =====================================================
namespace Glest { namespace Game {
2010-10-19 01:09:43 +02:00
AiInterface : : AiInterface ( Game & game , int factionIndex , int teamIndex , int useStartLocation ) {
2010-06-01 18:54:44 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-03-24 22:26:17 +01:00
this - > world = game . getWorld ( ) ;
this - > commander = game . getCommander ( ) ;
this - > console = game . getConsole ( ) ;
2010-06-01 18:54:44 +02:00
this - > gameSettings = game . getGameSettings ( ) ;
2010-03-24 22:26:17 +01:00
this - > factionIndex = factionIndex ;
this - > teamIndex = teamIndex ;
timer = 0 ;
//init ai
2010-10-19 01:09:43 +02:00
ai . init ( this , useStartLocation ) ;
2010-03-24 22:26:17 +01:00
//config
logLevel = Config : : getInstance ( ) . getInt ( " AiLog " ) ;
redir = Config : : getInstance ( ) . getBool ( " AiRedir " ) ;
//clear log file
if ( logLevel > 0 ) {
FILE * f = fopen ( getLogFilename ( ) . c_str ( ) , " wt " ) ;
if ( f = = NULL ) {
throw runtime_error ( " Can't open file: " + getLogFilename ( ) ) ;
}
fprintf ( f , " %s " , " Glest AI log file \n \n " ) ;
fclose ( f ) ;
}
2010-06-01 18:54:44 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-03-24 22:26:17 +01:00
}
// ==================== main ====================
void AiInterface : : update ( ) {
timer + + ;
ai . update ( ) ;
}
// ==================== misc ====================
void AiInterface : : printLog ( int logLevel , const string & s ) {
if ( this - > logLevel > = logLevel ) {
string logString = " ( " + intToStr ( factionIndex ) + " ) " + s ;
//print log to file
FILE * f = fopen ( getLogFilename ( ) . c_str ( ) , " at " ) ;
if ( f = = NULL ) {
throw runtime_error ( " Can't open file: " + getLogFilename ( ) ) ;
}
fprintf ( f , " %s \n " , logString . c_str ( ) ) ;
fclose ( f ) ;
//redirect to console
if ( redir ) {
console - > addLine ( logString ) ;
}
}
}
// ==================== interaction ====================
2010-09-14 21:10:37 +02:00
bool AiInterface : : executeCommandOverNetwork ( ) {
bool enableServerControlledAI = gameSettings - > getEnableServerControlledAI ( ) ;
bool isNetworkGame = gameSettings - > isNetworkGame ( ) ;
NetworkRole role = NetworkManager : : getInstance ( ) . getNetworkRole ( ) ;
Faction * faction = world - > getFaction ( factionIndex ) ;
return faction - > getCpuControl ( enableServerControlledAI , isNetworkGame , role ) ;
}
2010-03-24 22:26:17 +01:00
CommandResult AiInterface : : giveCommand ( int unitIndex , CommandClass commandClass , const Vec2i & pos ) {
2010-06-01 18:54:44 +02:00
assert ( this - > gameSettings ! = NULL ) ;
2010-05-01 11:27:08 +02:00
2010-09-14 21:10:37 +02:00
if ( executeCommandOverNetwork ( ) = = true ) {
2010-06-08 09:40:32 +02:00
const Unit * unit = getMyUnit ( unitIndex ) ;
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] unitIndex = %d \n unit = [%s] \n commandClass = [%d] \n " , __FILE__ , __FUNCTION__ , __LINE__ , unitIndex , unit - > toString ( ) . c_str ( ) , commandClass ) ;
CommandResult result = commander - > tryGiveCommand ( unit , unit - > getType ( ) - > getFirstCtOfClass ( commandClass ) , pos , unit - > getType ( ) , CardinalDir : : NORTH ) ;
2010-05-01 11:27:08 +02:00
2010-06-01 18:54:44 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
return result ;
}
else {
Command * c = new Command ( world - > getFaction ( factionIndex ) - > getUnit ( unitIndex ) - > getType ( ) - > getFirstCtOfClass ( commandClass ) , pos ) ;
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
CommandResult result = world - > getFaction ( factionIndex ) - > getUnit ( unitIndex ) - > giveCommand ( c ) ;
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
return result ;
}
2010-03-24 22:26:17 +01:00
}
CommandResult AiInterface : : giveCommand ( int unitIndex , const CommandType * commandType , const Vec2i & pos ) {
2010-06-01 18:54:44 +02:00
assert ( this - > gameSettings ! = NULL ) ;
2010-06-09 02:38:15 +02:00
const Unit * unit = getMyUnit ( unitIndex ) ;
if ( unit = = NULL ) {
char szBuf [ 1024 ] = " " ;
sprintf ( szBuf , " In [%s::%s Line: %d] Can not find AI unit with index: %d, AI factionIndex = %d. Game out of synch. " , __FILE__ , __FUNCTION__ , __LINE__ , unitIndex , factionIndex ) ;
throw runtime_error ( szBuf ) ;
}
const UnitType * unitType = unit - > getType ( ) ;
if ( unitType = = NULL ) {
char szBuf [ 1024 ] = " " ;
sprintf ( szBuf , " In [%s::%s Line: %d] Can not find AI unittype with unit index: %d, AI factionIndex = %d. Game out of synch. " , __FILE__ , __FUNCTION__ , __LINE__ , unitIndex , factionIndex ) ;
throw runtime_error ( szBuf ) ;
}
const CommandType * ct = unit - > getType ( ) - > findCommandTypeById ( commandType - > getId ( ) ) ;
if ( ct = = NULL ) {
char szBuf [ 4096 ] = " " ;
sprintf ( szBuf , " In [%s::%s Line: %d] \n Can not find AI command type for: \n unit = %d \n [%s] \n [%s] \n actual local factionIndex = %d. \n Game out of synch. " ,
__FILE__ , __FUNCTION__ , __LINE__ ,
unit - > getId ( ) , unit - > getFullName ( ) . c_str ( ) , unit - > getDesc ( ) . c_str ( ) ,
unit - > getFaction ( ) - > getIndex ( ) ) ;
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " %s \n " , szBuf ) ;
std : : string worldLog = world - > DumpWorldToLog ( ) ;
std : : string sError = " worldLog = " + worldLog + " " + string ( szBuf ) ;
throw runtime_error ( sError ) ;
}
2010-09-14 21:10:37 +02:00
if ( executeCommandOverNetwork ( ) = = true ) {
2010-06-08 09:40:32 +02:00
const Unit * unit = getMyUnit ( unitIndex ) ;
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] unitIndex = %d \n unit = [%s] \n commandType = %d - [%s] \n Command Type List: \n %s \n " , __FILE__ , __FUNCTION__ , __LINE__ , unitIndex , unit - > toString ( ) . c_str ( ) , commandType - > getId ( ) , commandType - > toString ( ) . c_str ( ) , unit - > getType ( ) - > getCommandTypeListDesc ( ) . c_str ( ) ) ;
CommandResult result = commander - > tryGiveCommand ( unit , commandType , pos , unit - > getType ( ) , CardinalDir : : NORTH ) ;
2010-06-01 18:54:44 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
return result ;
}
else {
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
CommandResult result = world - > getFaction ( factionIndex ) - > getUnit ( unitIndex ) - > giveCommand ( new Command ( commandType , pos ) ) ;
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
return result ;
}
2010-03-24 22:26:17 +01:00
}
CommandResult AiInterface : : giveCommand ( int unitIndex , const CommandType * commandType , const Vec2i & pos , const UnitType * ut ) {
2010-06-01 18:54:44 +02:00
assert ( this - > gameSettings ! = NULL ) ;
2010-06-09 02:38:15 +02:00
const Unit * unit = getMyUnit ( unitIndex ) ;
if ( unit = = NULL ) {
char szBuf [ 1024 ] = " " ;
sprintf ( szBuf , " In [%s::%s Line: %d] Can not find AI unit with index: %d, AI factionIndex = %d. Game out of synch. " , __FILE__ , __FUNCTION__ , __LINE__ , unitIndex , factionIndex ) ;
throw runtime_error ( szBuf ) ;
}
const UnitType * unitType = unit - > getType ( ) ;
if ( unitType = = NULL ) {
char szBuf [ 1024 ] = " " ;
sprintf ( szBuf , " In [%s::%s Line: %d] Can not find AI unittype with unit index: %d, AI factionIndex = %d. Game out of synch. " , __FILE__ , __FUNCTION__ , __LINE__ , unitIndex , factionIndex ) ;
throw runtime_error ( szBuf ) ;
}
const CommandType * ct = unit - > getType ( ) - > findCommandTypeById ( commandType - > getId ( ) ) ;
if ( ct = = NULL ) {
char szBuf [ 4096 ] = " " ;
sprintf ( szBuf , " In [%s::%s Line: %d] \n Can not find AI command type for: \n unit = %d \n [%s] \n [%s] \n actual local factionIndex = %d. \n Game out of synch. " ,
__FILE__ , __FUNCTION__ , __LINE__ ,
unit - > getId ( ) , unit - > getFullName ( ) . c_str ( ) , unit - > getDesc ( ) . c_str ( ) ,
unit - > getFaction ( ) - > getIndex ( ) ) ;
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " %s \n " , szBuf ) ;
std : : string worldLog = world - > DumpWorldToLog ( ) ;
std : : string sError = " worldLog = " + worldLog + " " + string ( szBuf ) ;
throw runtime_error ( sError ) ;
}
2010-09-14 21:10:37 +02:00
if ( executeCommandOverNetwork ( ) = = true ) {
2010-06-08 09:40:32 +02:00
const Unit * unit = getMyUnit ( unitIndex ) ;
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] unitIndex = %d \n unit = [%s] \n commandType = %d - [%s] \n ut = %p \n " , __FILE__ , __FUNCTION__ , __LINE__ , unitIndex , unit - > toString ( ) . c_str ( ) , commandType - > getId ( ) , commandType - > toString ( ) . c_str ( ) , ut ) ;
CommandResult result = commander - > tryGiveCommand ( unit , commandType , pos , ut , CardinalDir : : NORTH ) ;
2010-06-01 18:54:44 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
return result ;
}
else {
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
CommandResult result = world - > getFaction ( factionIndex ) - > getUnit ( unitIndex ) - > giveCommand ( new Command ( commandType , pos , ut , CardinalDir : : NORTH ) ) ;
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-05-01 11:27:08 +02:00
2010-06-01 18:54:44 +02:00
return result ;
}
2010-03-24 22:26:17 +01:00
}
CommandResult AiInterface : : giveCommand ( int unitIndex , const CommandType * commandType , Unit * u ) {
2010-06-01 18:54:44 +02:00
assert ( this - > gameSettings ! = NULL ) ;
assert ( this - > commander ! = NULL ) ;
2010-05-01 11:27:08 +02:00
2010-06-09 02:38:15 +02:00
const Unit * unit = getMyUnit ( unitIndex ) ;
if ( unit = = NULL ) {
char szBuf [ 1024 ] = " " ;
sprintf ( szBuf , " In [%s::%s Line: %d] Can not find AI unit with index: %d, AI factionIndex = %d. Game out of synch. " , __FILE__ , __FUNCTION__ , __LINE__ , unitIndex , factionIndex ) ;
throw runtime_error ( szBuf ) ;
}
const UnitType * unitType = unit - > getType ( ) ;
if ( unitType = = NULL ) {
char szBuf [ 1024 ] = " " ;
sprintf ( szBuf , " In [%s::%s Line: %d] Can not find AI unittype with unit index: %d, AI factionIndex = %d. Game out of synch. " , __FILE__ , __FUNCTION__ , __LINE__ , unitIndex , factionIndex ) ;
throw runtime_error ( szBuf ) ;
}
2010-06-10 16:58:06 +02:00
const CommandType * ct = ( commandType ! = NULL ? unit - > getType ( ) - > findCommandTypeById ( commandType - > getId ( ) ) : NULL ) ;
2010-06-09 02:38:15 +02:00
if ( ct = = NULL ) {
char szBuf [ 4096 ] = " " ;
sprintf ( szBuf , " In [%s::%s Line: %d] \n Can not find AI command type for: \n unit = %d \n [%s] \n [%s] \n actual local factionIndex = %d. \n Game out of synch. " ,
__FILE__ , __FUNCTION__ , __LINE__ ,
unit - > getId ( ) , unit - > getFullName ( ) . c_str ( ) , unit - > getDesc ( ) . c_str ( ) ,
unit - > getFaction ( ) - > getIndex ( ) ) ;
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " %s \n " , szBuf ) ;
std : : string worldLog = world - > DumpWorldToLog ( ) ;
std : : string sError = " worldLog = " + worldLog + " " + string ( szBuf ) ;
throw runtime_error ( sError ) ;
}
2010-09-14 21:10:37 +02:00
if ( executeCommandOverNetwork ( ) = = true ) {
2010-06-08 09:40:32 +02:00
Unit * targetUnit = u ;
const Unit * unit = getMyUnit ( unitIndex ) ;
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] unitIndex = %d \n unit = [%s] \n commandType = %d - [%s] \n Target Unit Id= %d \n Unit Commands: \n %s \n " , __FILE__ , __FUNCTION__ , __LINE__ , unitIndex , unit - > toString ( ) . c_str ( ) , ( commandType ! = NULL ? commandType - > getId ( ) : - 1 ) , ( commandType ! = NULL ? commandType - > toString ( ) . c_str ( ) : " null " ) , ( targetUnit ! = NULL ? targetUnit - > getId ( ) : - 1 ) , unit - > getType ( ) - > getCommandTypeListDesc ( ) . c_str ( ) ) ;
2010-06-01 18:54:44 +02:00
2010-06-08 09:40:32 +02:00
CommandResult result = commander - > tryGiveCommand ( unit , commandType , Vec2i ( 0 ) , unit - > getType ( ) , CardinalDir : : NORTH , false , targetUnit ) ;
2010-06-01 18:54:44 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
return result ;
}
else {
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
CommandResult result = world - > getFaction ( factionIndex ) - > getUnit ( unitIndex ) - > giveCommand ( new Command ( commandType , u ) ) ;
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
return result ;
}
2010-03-24 22:26:17 +01:00
}
// ==================== get data ====================
int AiInterface : : getMapMaxPlayers ( ) {
return world - > getMaxPlayers ( ) ;
}
Vec2i AiInterface : : getHomeLocation ( ) {
return world - > getMap ( ) - > getStartLocation ( world - > getFaction ( factionIndex ) - > getStartLocationIndex ( ) ) ;
}
Vec2i AiInterface : : getStartLocation ( int loactionIndex ) {
return world - > getMap ( ) - > getStartLocation ( loactionIndex ) ;
}
int AiInterface : : getFactionCount ( ) {
return world - > getFactionCount ( ) ;
}
int AiInterface : : getMyUnitCount ( ) const {
return world - > getFaction ( factionIndex ) - > getUnitCount ( ) ;
}
int AiInterface : : getMyUpgradeCount ( ) const {
return world - > getFaction ( factionIndex ) - > getUpgradeManager ( ) - > getUpgradeCount ( ) ;
}
int AiInterface : : onSightUnitCount ( ) {
int count = 0 ;
Map * map = world - > getMap ( ) ;
for ( int i = 0 ; i < world - > getFactionCount ( ) ; + + i ) {
for ( int j = 0 ; j < world - > getFaction ( i ) - > getUnitCount ( ) ; + + j ) {
SurfaceCell * sc = map - > getSurfaceCell ( Map : : toSurfCoords ( world - > getFaction ( i ) - > getUnit ( j ) - > getPos ( ) ) ) ;
if ( sc - > isVisible ( teamIndex ) ) {
count + + ;
}
}
}
return count ;
}
const Resource * AiInterface : : getResource ( const ResourceType * rt ) {
return world - > getFaction ( factionIndex ) - > getResource ( rt ) ;
}
const Unit * AiInterface : : getMyUnit ( int unitIndex ) {
2010-06-08 09:40:32 +02:00
if ( unitIndex > = world - > getFaction ( factionIndex ) - > getUnitCount ( ) ) {
char szBuf [ 1024 ] = " " ;
sprintf ( szBuf , " In [%s::%s Line: %d] unitIndex >= world->getFaction(factionIndex)->getUnitCount(), unitIndex = %d, world->getFaction(factionIndex)->getUnitCount() = %d " , __FILE__ , __FUNCTION__ , __LINE__ , unitIndex , world - > getFaction ( factionIndex ) - > getUnitCount ( ) ) ;
throw runtime_error ( szBuf ) ;
}
return world - > getFaction ( factionIndex ) - > getUnit ( unitIndex ) ;
2010-03-24 22:26:17 +01:00
}
const Unit * AiInterface : : getOnSightUnit ( int unitIndex ) {
int count = 0 ;
Map * map = world - > getMap ( ) ;
for ( int i = 0 ; i < world - > getFactionCount ( ) ; + + i ) {
for ( int j = 0 ; j < world - > getFaction ( i ) - > getUnitCount ( ) ; + + j ) {
Unit * u = world - > getFaction ( i ) - > getUnit ( j ) ;
if ( map - > getSurfaceCell ( Map : : toSurfCoords ( u - > getPos ( ) ) ) - > isVisible ( teamIndex ) ) {
if ( count = = unitIndex ) {
return u ;
}
else {
count + + ;
}
}
}
}
return NULL ;
}
const FactionType * AiInterface : : getMyFactionType ( ) {
return world - > getFaction ( factionIndex ) - > getType ( ) ;
}
const ControlType AiInterface : : getControlType ( ) {
return world - > getFaction ( factionIndex ) - > getControlType ( ) ;
}
const TechTree * AiInterface : : getTechTree ( ) {
return world - > getTechTree ( ) ;
}
2010-11-07 04:27:06 +01:00
//returns if there is a resource next to a unit, in "resourcePos" is stored the relative position of the resource
bool AiInterface : : isResourceNear ( const Vec2i & pos , const ResourceType * rt , Vec2i & resourcePos , Faction * faction , bool fallbackToPeersHarvestingSameResource ) const {
const Map * map = world - > getMap ( ) ;
int size = 1 ;
for ( int i = - 1 ; i < = size ; + + i ) {
for ( int j = - 1 ; j < = size ; + + j ) {
if ( map - > isInside ( pos . x + i , pos . y + j ) ) {
Resource * r = map - > getSurfaceCell ( map - > toSurfCoords ( Vec2i ( pos . x + i , pos . y + j ) ) ) - > getResource ( ) ;
if ( r ! = NULL ) {
if ( r - > getType ( ) = = rt ) {
resourcePos = pos + Vec2i ( i , j ) ;
return true ;
}
}
}
}
}
if ( fallbackToPeersHarvestingSameResource = = true & & faction ! = NULL ) {
// Look for another unit that is currently harvesting the same resource
// type right now
// Check the faction cache for a known position where we can harvest
// this resource type
Vec2i result = faction - > getClosestResourceTypeTargetFromCache ( pos , rt ) ;
if ( result . x > = 0 ) {
resourcePos = result ;
if ( pos . dist ( resourcePos ) < = size ) {
return true ;
}
}
}
return false ;
}
2010-10-02 03:11:59 +02:00
bool AiInterface : : getNearestSightedResource ( const ResourceType * rt , const Vec2i & pos ,
Vec2i & resultPos , bool usableResourceTypeOnly ) {
2010-11-09 10:06:52 +01:00
Faction * faction = world - > getFaction ( factionIndex ) ;
2010-10-02 03:11:59 +02:00
float tmpDist = 0 ;
2010-03-24 22:26:17 +01:00
float nearestDist = infinity ;
bool anyResource = false ;
2010-11-09 10:06:52 +01:00
resultPos . x = - 1 ;
resultPos . y = - 1 ;
2010-03-24 22:26:17 +01:00
2010-10-02 03:11:59 +02:00
bool canUseResourceType = ( usableResourceTypeOnly = = false ) ;
if ( usableResourceTypeOnly = = true ) {
// can any unit harvest this resource yet?
2010-11-09 10:06:52 +01:00
std : : map < const ResourceType * , int > : : iterator iterFind = cacheUnitHarvestResourceLookup . find ( rt ) ;
if ( iterFind ! = cacheUnitHarvestResourceLookup . end ( ) & &
faction - > findUnit ( iterFind - > second ) ! = NULL ) {
canUseResourceType = true ;
}
else {
int unitCount = getMyUnitCount ( ) ;
for ( int i = 0 ; i < unitCount ; + + i ) {
const Unit * unit = getMyUnit ( i ) ;
const HarvestCommandType * hct = unit - > getType ( ) - > getFirstHarvestCommand ( rt , unit - > getFaction ( ) ) ;
if ( hct ! = NULL ) {
canUseResourceType = true ;
cacheUnitHarvestResourceLookup [ rt ] = unit - > getId ( ) ;
break ;
}
2010-10-02 03:11:59 +02:00
}
}
}
if ( canUseResourceType = = true ) {
2010-11-09 10:06:52 +01:00
bool isResourceClose = isResourceNear ( pos , rt , resultPos , faction , true ) ;
//bool isResourceClose = false;
// Found a resource
if ( isResourceClose = = true | | resultPos . x > = 0 ) {
2010-11-07 04:27:06 +01:00
anyResource = true ;
}
else {
2010-11-09 10:06:52 +01:00
const Map * map = world - > getMap ( ) ;
Faction * faction = world - > getFaction ( factionIndex ) ;
2010-11-07 04:27:06 +01:00
for ( int i = 0 ; i < map - > getW ( ) ; + + i ) {
for ( int j = 0 ; j < map - > getH ( ) ; + + j ) {
2010-11-09 10:06:52 +01:00
Vec2i resPos = Vec2i ( i , j ) ;
Vec2i surfPos = Map : : toSurfCoords ( resPos ) ;
SurfaceCell * sc = map - > getSurfaceCell ( surfPos ) ;
2010-11-07 04:27:06 +01:00
//if explored cell
2010-11-09 10:06:52 +01:00
if ( sc ! = NULL & & sc - > isExplored ( teamIndex ) ) {
Resource * r = sc - > getResource ( ) ;
2010-11-07 04:27:06 +01:00
//if resource cell
2010-11-09 10:06:52 +01:00
if ( r ! = NULL ) {
if ( r - > getType ( ) = = rt ) {
tmpDist = pos . dist ( resPos ) ;
if ( tmpDist < nearestDist ) {
anyResource = true ;
nearestDist = tmpDist ;
resultPos = resPos ;
}
2010-11-07 04:27:06 +01:00
}
2010-11-09 10:06:52 +01:00
faction - > addResourceTargetToCache ( resPos , false ) ;
2010-10-02 03:11:59 +02:00
}
2010-03-24 22:26:17 +01:00
}
}
}
}
}
return anyResource ;
}
bool AiInterface : : isAlly ( const Unit * unit ) const {
return world - > getFaction ( factionIndex ) - > isAlly ( unit - > getFaction ( ) ) ;
}
bool AiInterface : : reqsOk ( const RequirableType * rt ) {
return world - > getFaction ( factionIndex ) - > reqsOk ( rt ) ;
}
bool AiInterface : : reqsOk ( const CommandType * ct ) {
return world - > getFaction ( factionIndex ) - > reqsOk ( ct ) ;
}
bool AiInterface : : checkCosts ( const ProducibleType * pt ) {
return world - > getFaction ( factionIndex ) - > checkCosts ( pt ) ;
}
bool AiInterface : : isFreeCells ( const Vec2i & pos , int size , Field field ) {
return world - > getMap ( ) - > isFreeCells ( pos , size , field ) ;
}
} } //end namespace