2010-03-13 22:10:45 +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-13 22:10:45 +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 "unit_updater.h"
# include <algorithm>
# include <cassert>
2010-07-22 00:05:50 +02:00
# include "cartographer.h"
2010-03-13 22:10:45 +01:00
# include "core_data.h"
# include "config.h"
# include "game.h"
# include "faction.h"
# include "network_manager.h"
2010-07-22 00:05:50 +02:00
# include "object.h"
# include "particle_type.h"
# include "path_finder.h"
# include "renderer.h"
2010-07-13 07:33:43 +02:00
# include "route_planner.h"
2010-07-22 00:05:50 +02:00
# include "sound.h"
# include "sound_renderer.h"
# include "upgrade.h"
# include "unit.h"
2010-03-13 22:10:45 +01:00
# include "leak_dumper.h"
using namespace Shared : : Graphics ;
using namespace Shared : : Util ;
namespace Glest { namespace Game {
// =====================================================
// class UnitUpdater
// =====================================================
// ===================== PUBLIC ========================
2010-07-30 03:19:31 +02:00
UnitUpdater : : UnitUpdater ( ) {
this - > game = NULL ;
this - > gui = NULL ;
this - > gameCamera = NULL ;
this - > world = NULL ;
this - > map = NULL ;
this - > console = NULL ;
this - > scriptManager = NULL ;
this - > routePlanner = NULL ;
this - > pathFinder = NULL ;
2010-11-28 04:35:44 +01:00
//UnitRangeCellsLookupItemCacheTimerCount = 0;
2011-09-01 03:11:23 +02:00
attackWarnRange = 0 ;
2010-07-30 03:19:31 +02:00
}
2010-03-13 22:10:45 +01:00
void UnitUpdater : : init ( Game * game ) {
2010-03-18 22:26:40 +01:00
this - > game = game ;
2010-03-13 22:10:45 +01:00
this - > gui = game - > getGui ( ) ;
this - > gameCamera = game - > getGameCamera ( ) ;
this - > world = game - > getWorld ( ) ;
this - > map = world - > getMap ( ) ;
this - > console = game - > getConsole ( ) ;
this - > scriptManager = game - > getScriptManager ( ) ;
2010-07-21 22:40:11 +02:00
this - > routePlanner = NULL ;
2010-07-22 00:05:50 +02:00
this - > pathFinder = NULL ;
2010-12-21 16:55:37 +01:00
attackWarnRange = Config : : getInstance ( ) . getFloat ( " AttackWarnRange " , " 50.0 " ) ;
2010-11-28 04:35:44 +01:00
//UnitRangeCellsLookupItemCacheTimerCount = 0;
2010-07-21 20:21:40 +02:00
switch ( this - > game - > getGameSettings ( ) - > getPathFinderType ( ) ) {
case pfBasic :
2010-07-22 00:05:50 +02:00
pathFinder = new PathFinder ( ) ;
pathFinder - > init ( map ) ;
2010-07-21 20:21:40 +02:00
break ;
case pfRoutePlanner :
routePlanner = world - > getRoutePlanner ( ) ;
break ;
2010-07-21 22:40:11 +02:00
default :
throw runtime_error ( " detected unsupported pathfinder type! " ) ;
2010-07-21 20:21:40 +02:00
}
2010-03-13 22:10:45 +01:00
}
2011-03-18 04:53:06 +01:00
void UnitUpdater : : clearUnitPrecache ( Unit * unit ) {
if ( pathFinder ! = NULL ) {
pathFinder - > clearUnitPrecache ( unit ) ;
}
}
2011-03-29 12:01:01 +02:00
void UnitUpdater : : removeUnitPrecache ( Unit * unit ) {
if ( pathFinder ! = NULL ) {
pathFinder - > removeUnitPrecache ( unit ) ;
}
}
2010-07-22 00:05:50 +02:00
UnitUpdater : : ~ UnitUpdater ( ) {
2010-11-28 04:35:44 +01:00
//UnitRangeCellsLookupItemCache.clear();
2010-08-25 18:01:24 +02:00
2010-07-22 00:05:50 +02:00
delete pathFinder ;
pathFinder = NULL ;
2010-12-26 05:38:03 +01:00
2010-12-21 16:55:37 +01:00
while ( attackWarnings . empty ( ) = = false ) {
AttackWarningData * awd = attackWarnings . back ( ) ;
attackWarnings . pop_back ( ) ;
delete awd ;
}
2010-07-22 00:05:50 +02:00
}
2010-03-13 22:10:45 +01:00
// ==================== progress skills ====================
//skill dependent actions
2010-08-25 09:29:35 +02:00
void UnitUpdater : : updateUnit ( Unit * unit ) {
2010-11-11 09:02:50 +01:00
Chrono chrono ;
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugPerformance ) . enabled ) chrono . start ( ) ;
2010-11-11 09:02:50 +01:00
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugPerformance ) . enabled & & chrono . getMillis ( ) > 0 ) SystemFlags : : OutputDebug ( SystemFlags : : debugPerformance , " In [%s::%s] Line: %d took msecs: %lld --------------------------- [START OF METHOD] --------------------------- \n " , __FILE__ , __FUNCTION__ , __LINE__ , chrono . getMillis ( ) ) ;
2010-11-11 09:02:50 +01:00
2010-03-13 22:10:45 +01:00
SoundRenderer & soundRenderer = SoundRenderer : : getInstance ( ) ;
//play skill sound
const SkillType * currSkill = unit - > getCurrSkill ( ) ;
2011-03-18 22:23:34 +01:00
if ( currSkill - > getSound ( ) ! = NULL ) {
2010-03-13 22:10:45 +01:00
float soundStartTime = currSkill - > getSoundStartTime ( ) ;
2011-03-18 22:23:34 +01:00
if ( soundStartTime > = unit - > getLastAnimProgress ( ) & & soundStartTime < unit - > getAnimProgress ( ) ) {
if ( map - > getSurfaceCell ( Map : : toSurfCoords ( unit - > getPos ( ) ) ) - > isVisible ( world - > getThisTeamIndex ( ) ) | |
2011-10-12 18:03:55 +02:00
( game - > getWorld ( ) - > showWorldForPlayer ( game - > getWorld ( ) - > getThisTeamIndex ( ) ) = = true ) ) {
2010-03-13 22:10:45 +01:00
soundRenderer . playFx ( currSkill - > getSound ( ) , unit - > getCurrVector ( ) , gameCamera - > getPos ( ) ) ;
}
}
}
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugPerformance ) . enabled & & chrono . getMillis ( ) > 0 ) SystemFlags : : OutputDebug ( SystemFlags : : debugPerformance , " In [%s::%s] Line: %d took msecs: %lld [after playsound] \n " , __FILE__ , __FUNCTION__ , __LINE__ , chrono . getMillis ( ) ) ;
2010-11-11 09:02:50 +01:00
2011-08-27 08:52:17 +02:00
unit - > updateTimedParticles ( ) ;
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugPerformance ) . enabled & & chrono . getMillis ( ) > 0 ) SystemFlags : : OutputDebug ( SystemFlags : : debugPerformance , " In [%s::%s] Line: %d took msecs: %lld [after playsound] \n " , __FILE__ , __FUNCTION__ , __LINE__ , chrono . getMillis ( ) ) ;
2010-03-13 22:10:45 +01:00
//start attack particle system
2010-09-09 07:42:19 +02:00
if ( unit - > getCurrSkill ( ) - > getClass ( ) = = scAttack ) {
2010-03-13 22:10:45 +01:00
const AttackSkillType * ast = static_cast < const AttackSkillType * > ( unit - > getCurrSkill ( ) ) ;
float attackStartTime = ast - > getAttackStartTime ( ) ;
if ( attackStartTime > = unit - > getLastAnimProgress ( ) & & attackStartTime < unit - > getAnimProgress ( ) ) {
startAttackParticleSystem ( unit ) ;
}
}
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugPerformance ) . enabled & & chrono . getMillis ( ) > 0 ) SystemFlags : : OutputDebug ( SystemFlags : : debugPerformance , " In [%s::%s] Line: %d took msecs: %lld [after attack particle system] \n " , __FILE__ , __FUNCTION__ , __LINE__ , chrono . getMillis ( ) ) ;
2010-11-11 09:02:50 +01:00
bool update = unit - > update ( ) ;
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugPerformance ) . enabled & & chrono . getMillis ( ) > 0 ) SystemFlags : : OutputDebug ( SystemFlags : : debugPerformance , " In [%s::%s] Line: %d took msecs: %lld [after unit->update()] \n " , __FILE__ , __FUNCTION__ , __LINE__ , chrono . getMillis ( ) ) ;
2010-11-11 09:02:50 +01:00
2010-03-13 22:10:45 +01:00
//update unit
2010-11-11 09:02:50 +01:00
if ( update = = true ) {
2010-03-20 00:26:00 +01:00
//SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
2010-03-13 22:10:45 +01:00
2011-03-18 04:53:06 +01:00
updateUnitCommand ( unit , - 1 ) ;
2010-03-13 22:10:45 +01:00
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugPerformance ) . enabled & & chrono . getMillis ( ) > 0 ) SystemFlags : : OutputDebug ( SystemFlags : : debugPerformance , " In [%s::%s] Line: %d took msecs: %lld [after updateUnitCommand()] \n " , __FILE__ , __FUNCTION__ , __LINE__ , chrono . getMillis ( ) ) ;
2010-11-11 09:02:50 +01:00
2010-03-13 22:10:45 +01:00
//if unit is out of EP, it stops
2011-10-12 06:40:48 +02:00
if ( unit - > computeEp ( ) = = true ) {
bool reQueueHoldPosition = false ;
string holdPositionName = " " ;
if ( unit - > getCurrCommand ( ) ! = NULL & &
unit - > getCurrCommand ( ) - > getCommandType ( ) - > getClass ( ) = = ccAttackStopped ) {
reQueueHoldPosition = true ;
holdPositionName = unit - > getCurrCommand ( ) - > getCommandType ( ) - > getName ( ) ;
}
2010-03-13 22:10:45 +01:00
unit - > setCurrSkill ( scStop ) ;
unit - > cancelCommand ( ) ;
2011-10-12 06:40:48 +02:00
if ( reQueueHoldPosition = = true ) {
//Search for a command that can produce the unit
const UnitType * ut = unit - > getType ( ) ;
for ( int i = 0 ; i < ut - > getCommandTypeCount ( ) ; + + i ) {
const CommandType * ct = ut - > getCommandType ( i ) ;
if ( ct ! = NULL & & ct - > getClass ( ) = = ccAttackStopped ) {
const AttackStoppedCommandType * act = static_cast < const AttackStoppedCommandType * > ( ct ) ;
if ( act ! = NULL & & act - > getName ( ) = = holdPositionName ) {
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugUnitCommands ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugUnitCommands , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
//printf("Re-Queing hold pos = %d, ep = %d skillep = %d skillname [%s]\n ",unit->getFaction()->reqsOk(act),unit->getEp(),act->getAttackSkillType()->getEpCost(),act->getName().c_str());
if ( unit - > getFaction ( ) - > reqsOk ( act ) = = true & &
unit - > getEp ( ) > = act - > getStopSkillType ( ) - > getEpCost ( ) ) {
unit - > giveCommand ( new Command ( act ) , true ) ;
}
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugUnitCommands ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugUnitCommands , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
break ;
}
}
}
}
2010-03-13 22:10:45 +01:00
}
2011-02-11 08:39:58 +01:00
if ( unit - > getCurrSkill ( ) ! = NULL & & unit - > getCurrSkill ( ) - > getClass ( ) ! = scAttack ) {
2011-02-21 02:34:31 +01:00
// !!! Is this causing choppy network play somehow?
//unit->computeHp();
2011-02-06 19:33:49 +01:00
}
2011-02-11 08:39:58 +01:00
else if ( unit - > getCommandSize ( ) > 0 ) {
Command * command = unit - > getCurrCommand ( ) ;
if ( command ! = NULL ) {
2011-02-12 22:47:31 +01:00
const AttackCommandType * act = dynamic_cast < const AttackCommandType * > ( command - > getCommandType ( ) ) ;
2011-02-11 08:39:58 +01:00
if ( act ! = NULL & & act - > getAttackSkillType ( ) ! = NULL & &
act - > getAttackSkillType ( ) - > getSpawnUnit ( ) ! = " " & & act - > getAttackSkillType ( ) - > getSpawnUnitCount ( ) > 0 ) {
const FactionType * ft = unit - > getFaction ( ) - > getType ( ) ;
const UnitType * spawnUnitType = ft - > getUnitType ( act - > getAttackSkillType ( ) - > getSpawnUnit ( ) ) ;
int spawnCount = act - > getAttackSkillType ( ) - > getSpawnUnitCount ( ) ;
for ( int y = 0 ; y < spawnCount ; + + y ) {
2011-07-07 00:35:35 +02:00
if ( spawnUnitType - > getMaxUnitCount ( ) > 0 ) {
if ( spawnUnitType - > getMaxUnitCount ( ) < = unit - > getFaction ( ) - > getCountForMaxUnitCount ( spawnUnitType ) ) {
break ;
}
}
2011-02-11 08:39:58 +01:00
UnitPathInterface * newpath = NULL ;
switch ( this - > game - > getGameSettings ( ) - > getPathFinderType ( ) ) {
case pfBasic :
newpath = new UnitPathBasic ( ) ;
break ;
case pfRoutePlanner :
newpath = new UnitPath ( ) ;
break ;
default :
throw runtime_error ( " detected unsupported pathfinder type! " ) ;
}
2011-02-06 19:33:49 +01:00
2011-02-11 08:39:58 +01:00
Unit * spawned = new Unit ( world - > getNextUnitId ( unit - > getFaction ( ) ) , newpath ,
Vec2i ( 0 ) , spawnUnitType , unit - > getFaction ( ) ,
world - > getMap ( ) , CardinalDir : : NORTH ) ;
2011-03-28 05:54:23 +02:00
//SystemFlags::OutputDebug(SystemFlags::debugUnitCommands,"In [%s::%s Line: %d] about to place unit for unit [%s]\n",__FILE__,__FUNCTION__,__LINE__,spawned->toString().c_str());
2011-02-11 08:39:58 +01:00
if ( ! world - > placeUnit ( unit - > getCenteredPos ( ) , 10 , spawned ) ) {
2011-03-28 05:54:23 +02:00
//SystemFlags::OutputDebug(SystemFlags::debugUnitCommands,"In [%s::%s Line: %d] COULD NOT PLACE UNIT for unitID [%d]\n",__FILE__,__FUNCTION__,__LINE__,spawned->getId());
2011-02-06 19:33:49 +01:00
2011-02-11 08:39:58 +01:00
// This will also cleanup newPath
delete spawned ;
spawned = NULL ;
}
else {
spawned - > create ( ) ;
spawned - > born ( ) ;
world - > getStats ( ) - > produce ( unit - > getFactionIndex ( ) ) ;
const CommandType * ct = spawned - > computeCommandType ( command - > getPos ( ) , command - > getUnit ( ) ) ;
if ( ct ! = NULL ) {
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugUnitCommands ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugUnitCommands , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2011-02-11 08:39:58 +01:00
spawned - > giveCommand ( new Command ( ct , unit - > getMeetingPos ( ) ) ) ;
}
scriptManager - > onUnitCreated ( spawned ) ;
2011-02-06 19:33:49 +01:00
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugPerformance ) . enabled & & chrono . getMillis ( ) > 0 ) SystemFlags : : OutputDebug ( SystemFlags : : debugPerformance , " In [%s::%s] Line: %d took msecs: %lld \n " , __FILE__ , __FUNCTION__ , __LINE__ , chrono . getMillis ( ) ) ;
2011-02-11 08:39:58 +01:00
}
}
}
}
2011-02-06 19:33:49 +01:00
}
2010-03-13 22:10:45 +01:00
//move unit in cells
2010-09-09 07:42:19 +02:00
if ( unit - > getCurrSkill ( ) - > getClass ( ) = = scMove ) {
2010-03-13 22:10:45 +01:00
world - > moveUnitCells ( unit ) ;
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugPerformance ) . enabled & & chrono . getMillis ( ) > 0 ) SystemFlags : : OutputDebug ( SystemFlags : : debugPerformance , " In [%s::%s] Line: %d took msecs: %lld [after world->moveUnitCells()] \n " , __FILE__ , __FUNCTION__ , __LINE__ , chrono . getMillis ( ) ) ;
2010-11-11 09:02:50 +01:00
2010-03-13 22:10:45 +01:00
//play water sound
2010-11-11 09:02:50 +01:00
if ( map - > getCell ( unit - > getPos ( ) ) - > getHeight ( ) < map - > getWaterLevel ( ) & & unit - > getCurrField ( ) = = fLand ) {
2011-03-23 05:41:38 +01:00
if ( Config : : getInstance ( ) . getBool ( " DisableWaterSounds " , " false " ) = = false ) {
soundRenderer . playFx (
CoreData : : getInstance ( ) . getWaterSound ( ) ,
unit - > getCurrVector ( ) ,
gameCamera - > getPos ( )
) ;
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugPerformance ) . enabled & & chrono . getMillis ( ) > 0 ) SystemFlags : : OutputDebug ( SystemFlags : : debugPerformance , " In [%s::%s] Line: %d took msecs: %lld [after soundFx()] \n " , __FILE__ , __FUNCTION__ , __LINE__ , chrono . getMillis ( ) ) ;
2011-03-23 05:41:38 +01:00
}
2010-03-13 22:10:45 +01:00
}
}
}
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugPerformance ) . enabled & & chrono . getMillis ( ) > 0 ) SystemFlags : : OutputDebug ( SystemFlags : : debugPerformance , " In [%s::%s] Line: %d took msecs: %lld \n " , __FILE__ , __FUNCTION__ , __LINE__ , chrono . getMillis ( ) ) ;
2010-11-11 09:02:50 +01:00
2010-03-13 22:10:45 +01:00
//unit death
2010-09-09 07:42:19 +02:00
if ( unit - > isDead ( ) & & unit - > getCurrSkill ( ) - > getClass ( ) ! = scDie ) {
2010-03-13 22:10:45 +01:00
unit - > kill ( ) ;
}
2010-11-11 09:02:50 +01:00
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugPerformance ) . enabled & & chrono . getMillis ( ) > 0 ) SystemFlags : : OutputDebug ( SystemFlags : : debugPerformance , " In [%s::%s] Line: %d took msecs: %lld --------------------------- [END OF METHOD] --------------------------- \n " , __FILE__ , __FUNCTION__ , __LINE__ , chrono . getMillis ( ) ) ;
2010-03-13 22:10:45 +01:00
}
// ==================== progress commands ====================
//VERY IMPORTANT: compute next state depending on the first order of the list
2011-03-18 04:53:06 +01:00
void UnitUpdater : : updateUnitCommand ( Unit * unit , int frameIndex ) {
// Clear previous cached unit data
if ( frameIndex > = 0 ) {
clearUnitPrecache ( unit ) ;
}
2010-12-05 00:19:43 +01:00
Chrono chrono ;
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugPerformance ) . enabled ) chrono . start ( ) ;
2010-12-05 00:19:43 +01:00
2010-07-14 08:59:55 +02:00
//if unit has command process it
2010-03-13 22:10:45 +01:00
if ( unit - > anyCommand ( ) ) {
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugUnitCommands ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugUnitCommands , " In [%s::%s Line: %d] unit [%s] has command [%s] \n " , __FILE__ , __FUNCTION__ , __LINE__ , unit - > toString ( ) . c_str ( ) , unit - > getCurrCommand ( ) - > toString ( ) . c_str ( ) ) ;
2011-01-08 22:53:05 +01:00
CommandClass cc = unit - > getCurrCommand ( ) - > getCommandType ( ) - > commandTypeClass ;
2011-03-18 04:53:06 +01:00
unit - > getCurrCommand ( ) - > getCommandType ( ) - > update ( this , unit , frameIndex ) ;
2010-03-13 22:10:45 +01:00
}
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugPerformance ) . enabled & & chrono . getMillis ( ) > 0 ) SystemFlags : : OutputDebug ( SystemFlags : : debugPerformance , " In [%s::%s Line: %d] took msecs: %lld \n " , __FILE__ , __FUNCTION__ , __LINE__ , chrono . getMillis ( ) ) ;
2010-12-05 00:19:43 +01:00
2011-03-18 04:53:06 +01:00
if ( frameIndex < 0 ) {
//if no commands stop and add stop command
if ( unit - > anyCommand ( ) = = false & & unit - > isOperative ( ) ) {
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugUnitCommands ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugUnitCommands , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-11-09 10:06:52 +01:00
2011-03-29 01:47:22 +02:00
if ( unit - > getType ( ) - > hasSkillClass ( scStop ) ) {
unit - > setCurrSkill ( scStop ) ;
}
2011-03-23 01:49:21 +01:00
2011-03-29 01:47:22 +02:00
if ( unit - > getType ( ) - > hasCommandClass ( ccStop ) ) {
unit - > giveCommand ( new Command ( unit - > getType ( ) - > getFirstCtOfClass ( ccStop ) ) ) ;
2011-03-24 22:55:39 +01:00
}
2010-03-13 22:10:45 +01:00
}
2011-03-18 04:53:06 +01:00
}
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugPerformance ) . enabled & & chrono . getMillis ( ) > 0 ) SystemFlags : : OutputDebug ( SystemFlags : : debugPerformance , " In [%s::%s] Line: %d took msecs: %lld --------------------------- [END OF METHOD] --------------------------- \n " , __FILE__ , __FUNCTION__ , __LINE__ , chrono . getMillis ( ) ) ;
2010-03-13 22:10:45 +01:00
}
// ==================== updateStop ====================
2011-03-18 04:53:06 +01:00
void UnitUpdater : : updateStop ( Unit * unit , int frameIndex ) {
// Nothing to do
if ( frameIndex > = 0 ) {
clearUnitPrecache ( unit ) ;
return ;
}
2011-03-28 05:54:23 +02:00
Chrono chrono ;
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugPerformance ) . enabled ) chrono . start ( ) ;
2010-12-05 00:19:43 +01:00
Command * command = unit - > getCurrCommand ( ) ;
2010-03-13 22:10:45 +01:00
const StopCommandType * sct = static_cast < const StopCommandType * > ( command - > getCommandType ( ) ) ;
2011-03-23 03:45:42 +01:00
Unit * sighted = NULL ;
2010-03-13 22:10:45 +01:00
unit - > setCurrSkill ( sct - > getStopSkillType ( ) ) ;
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugPerformance ) . enabled & & chrono . getMillis ( ) > 0 ) SystemFlags : : OutputDebug ( SystemFlags : : debugPerformance , " In [%s::%s Line: %d] took msecs: %lld \n " , __FILE__ , __FUNCTION__ , __LINE__ , chrono . getMillis ( ) ) ;
2010-12-05 00:19:43 +01:00
2010-03-13 22:10:45 +01:00
//we can attack any unit => attack it
2010-11-09 10:06:52 +01:00
if ( unit - > getType ( ) - > hasSkillClass ( scAttack ) ) {
int cmdTypeCount = unit - > getType ( ) - > getCommandTypeCount ( ) ;
for ( int i = 0 ; i < cmdTypeCount ; + + i ) {
2010-03-13 22:10:45 +01:00
const CommandType * ct = unit - > getType ( ) - > getCommandType ( i ) ;
//look for an attack skill
const AttackSkillType * ast = NULL ;
2011-03-23 03:45:42 +01:00
if ( ct - > getClass ( ) = = ccAttack ) {
2010-03-13 22:10:45 +01:00
ast = static_cast < const AttackCommandType * > ( ct ) - > getAttackSkillType ( ) ;
}
2011-03-23 03:45:42 +01:00
else if ( ct - > getClass ( ) = = ccAttackStopped ) {
2010-03-13 22:10:45 +01:00
ast = static_cast < const AttackStoppedCommandType * > ( ct ) - > getAttackSkillType ( ) ;
}
//use it to attack
2010-11-09 10:06:52 +01:00
if ( ast ! = NULL ) {
2010-09-09 07:42:19 +02:00
if ( attackableOnSight ( unit , & sighted , ast ) ) {
2011-01-17 17:31:25 +01:00
//SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
2010-03-13 22:10:45 +01:00
unit - > giveCommand ( new Command ( ct , sighted - > getPos ( ) ) ) ;
break ;
}
}
}
2010-12-05 00:19:43 +01:00
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugPerformance ) . enabled & & chrono . getMillis ( ) > 0 ) SystemFlags : : OutputDebug ( SystemFlags : : debugPerformance , " In [%s::%s Line: %d] took msecs: %lld \n " , __FILE__ , __FUNCTION__ , __LINE__ , chrono . getMillis ( ) ) ;
2010-03-13 22:10:45 +01:00
}
//see any unit and cant attack it => run
2010-09-09 07:42:19 +02:00
else if ( unit - > getType ( ) - > hasCommandClass ( ccMove ) ) {
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugPerformance ) . enabled & & chrono . getMillis ( ) > 0 ) SystemFlags : : OutputDebug ( SystemFlags : : debugPerformance , " In [%s::%s Line: %d] took msecs: %lld \n " , __FILE__ , __FUNCTION__ , __LINE__ , chrono . getMillis ( ) ) ;
2010-12-05 00:19:43 +01:00
2010-09-09 07:42:19 +02:00
if ( attackerOnSight ( unit , & sighted ) ) {
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugPerformance ) . enabled & & chrono . getMillis ( ) > 0 ) SystemFlags : : OutputDebug ( SystemFlags : : debugPerformance , " In [%s::%s Line: %d] took msecs: %lld \n " , __FILE__ , __FUNCTION__ , __LINE__ , chrono . getMillis ( ) ) ;
2011-03-23 03:45:42 +01:00
Vec2i escapePos = unit - > getPos ( ) * 2 - sighted - > getPos ( ) ;
//SystemFlags::OutputDebug(SystemFlags::debugUnitCommands,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
2010-03-13 22:10:45 +01:00
unit - > giveCommand ( new Command ( unit - > getType ( ) - > getFirstCtOfClass ( ccMove ) , escapePos ) ) ;
}
2010-12-05 00:19:43 +01:00
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugPerformance ) . enabled & & chrono . getMillis ( ) > 0 ) SystemFlags : : OutputDebug ( SystemFlags : : debugPerformance , " In [%s::%s Line: %d] took msecs: %lld \n " , __FILE__ , __FUNCTION__ , __LINE__ , chrono . getMillis ( ) ) ;
2010-03-13 22:10:45 +01:00
}
2010-12-05 00:19:43 +01:00
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugPerformance ) . enabled & & chrono . getMillis ( ) > 0 ) SystemFlags : : OutputDebug ( SystemFlags : : debugPerformance , " In [%s::%s] Line: %d took msecs: %lld --------------------------- [END OF METHOD] --------------------------- \n " , __FILE__ , __FUNCTION__ , __LINE__ , chrono . getMillis ( ) ) ;
2010-03-13 22:10:45 +01:00
}
// ==================== updateMove ====================
2011-03-18 04:53:06 +01:00
void UnitUpdater : : updateMove ( Unit * unit , int frameIndex ) {
2010-12-05 00:19:43 +01:00
Chrono chrono ;
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugPerformance ) . enabled ) chrono . start ( ) ;
2010-12-05 00:19:43 +01:00
2010-03-13 22:10:45 +01:00
Command * command = unit - > getCurrCommand ( ) ;
const MoveCommandType * mct = static_cast < const MoveCommandType * > ( command - > getCommandType ( ) ) ;
Vec2i pos = command - > getUnit ( ) ! = NULL ? command - > getUnit ( ) - > getCenteredPos ( ) : command - > getPos ( ) ;
2011-04-03 04:42:45 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugWorldSynch ) . enabled = = true & & frameIndex < 0 ) {
2010-12-01 00:32:39 +01:00
char szBuf [ 4096 ] = " " ;
2011-02-16 05:43:27 +01:00
sprintf ( szBuf , " [updateMove] pos [%s] unit [%d - %s] cmd [%s] " , pos . getString ( ) . c_str ( ) , unit - > getId ( ) , unit - > getFullName ( ) . c_str ( ) , command - > toString ( ) . c_str ( ) ) ;
2010-12-02 00:38:03 +01:00
unit - > logSynchData ( __FILE__ , __LINE__ , szBuf ) ;
2010-12-01 00:32:39 +01:00
}
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugPerformance ) . enabled & & chrono . getMillis ( ) > 0 ) SystemFlags : : OutputDebug ( SystemFlags : : debugPerformance , " In [%s::%s Line: %d] took msecs: %lld \n " , __FILE__ , __FUNCTION__ , __LINE__ , chrono . getMillis ( ) ) ;
2010-12-05 00:19:43 +01:00
2010-07-21 20:21:40 +02:00
TravelState tsValue = tsImpossible ;
switch ( this - > game - > getGameSettings ( ) - > getPathFinderType ( ) ) {
case pfBasic :
2011-03-18 04:53:06 +01:00
tsValue = pathFinder - > findPath ( unit , pos , NULL , frameIndex ) ;
2010-07-21 20:21:40 +02:00
break ;
case pfRoutePlanner :
tsValue = routePlanner - > findPath ( unit , pos ) ;
break ;
2010-07-21 22:40:11 +02:00
default :
throw runtime_error ( " detected unsupported pathfinder type! " ) ;
2010-07-21 20:21:40 +02:00
}
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugPerformance ) . enabled & & chrono . getMillis ( ) > 0 ) SystemFlags : : OutputDebug ( SystemFlags : : debugPerformance , " In [%s::%s Line: %d] took msecs: %lld \n " , __FILE__ , __FUNCTION__ , __LINE__ , chrono . getMillis ( ) ) ;
2010-12-05 00:19:43 +01:00
2011-03-18 04:53:06 +01:00
if ( frameIndex < 0 ) {
switch ( tsValue ) {
case tsMoving :
unit - > setCurrSkill ( mct - > getMoveSkillType ( ) ) ;
break ;
case tsBlocked :
unit - > setCurrSkill ( scStop ) ;
if ( unit - > getPath ( ) - > isBlocked ( ) ) {
unit - > finishCommand ( ) ;
}
break ;
2010-03-13 22:10:45 +01:00
2011-03-18 04:53:06 +01:00
default :
2010-03-13 22:10:45 +01:00
unit - > finishCommand ( ) ;
}
2011-03-18 04:53:06 +01:00
}
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugPerformance ) . enabled & & chrono . getMillis ( ) > 0 ) SystemFlags : : OutputDebug ( SystemFlags : : debugPerformance , " In [%s::%s] Line: %d took msecs: %lld --------------------------- [END OF METHOD] --------------------------- \n " , __FILE__ , __FUNCTION__ , __LINE__ , chrono . getMillis ( ) ) ;
2010-03-13 22:10:45 +01:00
}
// ==================== updateAttack ====================
2011-03-18 04:53:06 +01:00
void UnitUpdater : : updateAttack ( Unit * unit , int frameIndex ) {
2010-12-05 00:19:43 +01:00
Chrono chrono ;
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugPerformance ) . enabled ) chrono . start ( ) ;
2010-12-05 00:19:43 +01:00
2010-03-13 22:10:45 +01:00
Command * command = unit - > getCurrCommand ( ) ;
const AttackCommandType * act = static_cast < const AttackCommandType * > ( command - > getCommandType ( ) ) ;
Unit * target = NULL ;
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugPerformance ) . enabled & & chrono . getMillis ( ) > 0 ) SystemFlags : : OutputDebug ( SystemFlags : : debugPerformance , " In [%s::%s Line: %d] took msecs: %lld \n " , __FILE__ , __FUNCTION__ , __LINE__ , chrono . getMillis ( ) ) ;
2010-12-05 00:19:43 +01:00
2011-03-18 04:53:06 +01:00
if ( ( command - > getUnit ( ) = = NULL | | ! ( command - > getUnit ( ) - > isAlive ( ) ) ) & & unit - > getCommandSize ( ) > 1 ) {
if ( frameIndex < 0 ) {
unit - > finishCommand ( ) ; // all queued "ground attacks" are skipped if somthing else is queued after them.
}
2010-12-23 11:44:11 +01:00
return ;
}
2010-03-13 22:10:45 +01:00
//if found
2010-09-09 07:42:19 +02:00
if ( attackableOnRange ( unit , & target , act - > getAttackSkillType ( ) ) ) {
2011-03-18 04:53:06 +01:00
if ( frameIndex < 0 ) {
if ( unit - > getEp ( ) > = act - > getAttackSkillType ( ) - > getEpCost ( ) ) {
unit - > setCurrSkill ( act - > getAttackSkillType ( ) ) ;
unit - > setTarget ( target ) ;
}
else {
unit - > setCurrSkill ( scStop ) ;
}
}
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugPerformance ) . enabled & & chrono . getMillis ( ) > 0 ) SystemFlags : : OutputDebug ( SystemFlags : : debugPerformance , " In [%s::%s Line: %d] took msecs: %lld \n " , __FILE__ , __FUNCTION__ , __LINE__ , chrono . getMillis ( ) ) ;
2010-03-13 22:10:45 +01:00
}
2010-09-09 07:42:19 +02:00
else {
2010-03-13 22:10:45 +01:00
//compute target pos
Vec2i pos ;
2011-03-18 04:53:06 +01:00
if ( command - > getUnit ( ) ! = NULL ) {
2010-03-13 22:10:45 +01:00
pos = command - > getUnit ( ) - > getCenteredPos ( ) ;
}
2010-09-09 07:42:19 +02:00
else if ( attackableOnSight ( unit , & target , act - > getAttackSkillType ( ) ) ) {
2010-03-13 22:10:45 +01:00
pos = target - > getPos ( ) ;
}
2010-09-09 07:42:19 +02:00
else {
2010-03-13 22:10:45 +01:00
pos = command - > getPos ( ) ;
}
2011-04-03 04:42:45 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugWorldSynch ) . enabled = = true & & frameIndex < 0 ) {
2010-12-01 00:32:39 +01:00
char szBuf [ 4096 ] = " " ;
2010-12-02 00:38:03 +01:00
sprintf ( szBuf , " [updateAttack] pos [%s] unit->getPos() [%s] " ,
2010-12-01 00:32:39 +01:00
pos . getString ( ) . c_str ( ) , unit - > getPos ( ) . getString ( ) . c_str ( ) ) ;
2010-12-02 00:38:03 +01:00
unit - > logSynchData ( __FILE__ , __LINE__ , szBuf ) ;
2010-12-01 00:32:39 +01:00
}
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugPerformance ) . enabled & & chrono . getMillis ( ) > 0 ) SystemFlags : : OutputDebug ( SystemFlags : : debugPerformance , " In [%s::%s Line: %d] took msecs: %lld \n " , __FILE__ , __FUNCTION__ , __LINE__ , chrono . getMillis ( ) ) ;
2010-12-05 00:19:43 +01:00
2010-07-21 20:21:40 +02:00
TravelState tsValue = tsImpossible ;
switch ( this - > game - > getGameSettings ( ) - > getPathFinderType ( ) ) {
case pfBasic :
2011-03-18 04:53:06 +01:00
tsValue = pathFinder - > findPath ( unit , pos , NULL , frameIndex ) ;
2010-07-21 20:21:40 +02:00
break ;
case pfRoutePlanner :
tsValue = routePlanner - > findPath ( unit , pos ) ;
break ;
2010-07-21 22:40:11 +02:00
default :
throw runtime_error ( " detected unsupported pathfinder type! " ) ;
2010-07-21 20:21:40 +02:00
}
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugPerformance ) . enabled & & chrono . getMillis ( ) > 0 ) SystemFlags : : OutputDebug ( SystemFlags : : debugPerformance , " In [%s::%s Line: %d] took msecs: %lld \n " , __FILE__ , __FUNCTION__ , __LINE__ , chrono . getMillis ( ) ) ;
2010-12-05 00:19:43 +01:00
2011-03-18 04:53:06 +01:00
if ( frameIndex < 0 ) {
if ( command - > getUnit ( ) ! = NULL & & ! command - > getUnit ( ) - > isAlive ( ) & & unit - > getCommandSize ( ) > 1 ) {
// don't run over to dead body if there is still something to do in the queue
unit - > finishCommand ( ) ;
}
else {
//if unit arrives destPos order has ended
2011-06-09 23:38:04 +02:00
switch ( tsValue ) {
2011-06-09 23:50:54 +02:00
case tsMoving :
unit - > setCurrSkill ( act - > getMoveSkillType ( ) ) ;
break ;
case tsBlocked :
if ( unit - > getPath ( ) - > isBlocked ( ) ) {
unit - > finishCommand ( ) ;
}
break ;
default :
unit - > finishCommand ( ) ;
}
2011-06-09 23:38:04 +02:00
/*
2011-06-02 09:09:19 +02:00
case tsMoving :
unit - > setCurrSkill ( act - > getMoveSkillType ( ) ) ;
{
std : : pair < bool , Unit * > beingAttacked = unitBeingAttacked ( unit ) ;
if ( beingAttacked . first = = true ) {
Unit * enemy = beingAttacked . second ;
const AttackCommandType * act_forenemy = unit - > getType ( ) - > getFirstAttackCommand ( enemy - > getCurrField ( ) ) ;
if ( act_forenemy ! = NULL ) {
if ( unit - > getEp ( ) > = act_forenemy - > getAttackSkillType ( ) - > getEpCost ( ) ) {
unit - > setCurrSkill ( act_forenemy - > getAttackSkillType ( ) ) ;
unit - > setTarget ( enemy ) ;
}
//aiInterface->giveCommand(i, act_forenemy, beingAttacked.second->getPos());
}
else {
const AttackStoppedCommandType * asct_forenemy = unit - > getType ( ) - > getFirstAttackStoppedCommand ( enemy - > getCurrField ( ) ) ;
if ( asct_forenemy ! = NULL ) {
//aiInterface->giveCommand(i, asct_forenemy, beingAttacked.second->getCenteredPos());
if ( unit - > getEp ( ) > = asct_forenemy - > getAttackSkillType ( ) - > getEpCost ( ) ) {
unit - > setCurrSkill ( asct_forenemy - > getAttackSkillType ( ) ) ;
unit - > setTarget ( enemy ) ;
}
}
}
}
}
break ;
2011-06-09 23:50:54 +02:00
2011-06-02 09:09:19 +02:00
case tsBlocked :
if ( unit - > getPath ( ) - > isBlocked ( ) ) {
unit - > finishCommand ( ) ;
}
break ;
default :
2011-03-18 04:53:06 +01:00
unit - > finishCommand ( ) ;
2010-12-23 11:44:11 +01:00
}
2011-06-09 23:50:54 +02:00
*/
2010-03-13 22:10:45 +01:00
}
}
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugPerformance ) . enabled & & chrono . getMillis ( ) > 0 ) SystemFlags : : OutputDebug ( SystemFlags : : debugPerformance , " In [%s::%s Line: %d] took msecs: %lld \n " , __FILE__ , __FUNCTION__ , __LINE__ , chrono . getMillis ( ) ) ;
2010-03-13 22:10:45 +01:00
}
2010-12-05 00:19:43 +01:00
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugPerformance ) . enabled & & chrono . getMillis ( ) > 0 ) SystemFlags : : OutputDebug ( SystemFlags : : debugPerformance , " In [%s::%s] Line: %d took msecs: %lld --------------------------- [END OF METHOD] --------------------------- \n " , __FILE__ , __FUNCTION__ , __LINE__ , chrono . getMillis ( ) ) ;
2010-03-13 22:10:45 +01:00
}
// ==================== updateAttackStopped ====================
2011-03-18 04:53:06 +01:00
void UnitUpdater : : updateAttackStopped ( Unit * unit , int frameIndex ) {
// Nothing to do
if ( frameIndex > = 0 ) {
clearUnitPrecache ( unit ) ;
return ;
}
2010-12-05 00:19:43 +01:00
Chrono chrono ;
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugPerformance ) . enabled ) chrono . start ( ) ;
2010-12-05 00:19:43 +01:00
Command * command = unit - > getCurrCommand ( ) ;
2010-03-13 22:10:45 +01:00
const AttackStoppedCommandType * asct = static_cast < const AttackStoppedCommandType * > ( command - > getCommandType ( ) ) ;
2011-03-28 23:04:47 +02:00
Unit * enemy = NULL ;
2010-03-13 22:10:45 +01:00
2011-03-18 04:53:06 +01:00
if ( unit - > getCommandSize ( ) > 1 )
2011-01-31 00:01:55 +01:00
{
unit - > finishCommand ( ) ; // attackStopped is skipped if somthing else is queued after this.
return ;
}
2011-03-28 23:04:47 +02:00
float distToUnit = - 1 ;
std : : pair < bool , Unit * > result = make_pair ( false , ( Unit * ) NULL ) ;
unitBeingAttacked ( result , unit , asct - > getAttackSkillType ( ) , & distToUnit ) ;
if ( result . first = = true ) {
unit - > setCurrSkill ( asct - > getAttackSkillType ( ) ) ;
unit - > setTarget ( result . second ) ;
}
else if ( attackableOnRange ( unit , & enemy , asct - > getAttackSkillType ( ) ) ) {
2010-03-13 22:10:45 +01:00
unit - > setCurrSkill ( asct - > getAttackSkillType ( ) ) ;
unit - > setTarget ( enemy ) ;
}
2010-12-05 00:19:43 +01:00
else {
2010-03-13 22:10:45 +01:00
unit - > setCurrSkill ( asct - > getStopSkillType ( ) ) ;
}
2010-12-05 00:19:43 +01:00
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugPerformance ) . enabled & & chrono . getMillis ( ) > 0 ) SystemFlags : : OutputDebug ( SystemFlags : : debugPerformance , " In [%s::%s] Line: %d took msecs: %lld --------------------------- [END OF METHOD] --------------------------- \n " , __FILE__ , __FUNCTION__ , __LINE__ , chrono . getMillis ( ) ) ;
2010-03-13 22:10:45 +01:00
}
2011-03-28 23:04:47 +02:00
void UnitUpdater : : unitBeingAttacked ( std : : pair < bool , Unit * > & result , const Unit * unit , const AttackSkillType * ast , float * currentDistToUnit ) {
//std::pair<bool,Unit *> result = make_pair(false,(Unit *)NULL);
float distToUnit = - 1 ;
if ( currentDistToUnit ! = NULL ) {
distToUnit = * currentDistToUnit ;
}
if ( ast ! = NULL ) {
vector < Unit * > enemies = enemyUnitsOnRange ( unit , ast ) ;
for ( unsigned j = 0 ; j < enemies . size ( ) ; + + j ) {
Unit * enemy = enemies [ j ] ;
//printf("~~~~~~~~ Unit [%s - %d] enemy # %d found enemy [%s - %d] distToUnit = %f\n",unit->getFullName().c_str(),unit->getId(),j,enemy->getFullName().c_str(),enemy->getId(),unit->getCenteredPos().dist(enemy->getCenteredPos()));
if ( distToUnit < 0 | | unit - > getCenteredPos ( ) . dist ( enemy - > getCenteredPos ( ) ) < distToUnit ) {
distToUnit = unit - > getCenteredPos ( ) . dist ( enemy - > getCenteredPos ( ) ) ;
2011-06-24 01:30:49 +02:00
if ( ast - > getAttackRange ( ) > = distToUnit ) {
result . first = true ;
result . second = enemy ;
2011-03-30 00:12:55 +02:00
break ;
}
2011-03-28 23:04:47 +02:00
}
}
}
if ( currentDistToUnit ! = NULL ) {
* currentDistToUnit = distToUnit ;
}
// if(result.first == true) {
// printf("~~~~~~~~ Unit [%s - %d] found enemy [%s - %d] distToUnit = %f\n",unit->getFullName().c_str(),unit->getId(),result.second->getFullName().c_str(),result.second->getId(),distToUnit);
// }
//return result;
}
std : : pair < bool , Unit * > UnitUpdater : : unitBeingAttacked ( const Unit * unit ) {
std : : pair < bool , Unit * > result = make_pair ( false , ( Unit * ) NULL ) ;
float distToUnit = - 1 ;
for ( unsigned int i = 0 ; i < unit - > getType ( ) - > getSkillTypeCount ( ) ; + + i ) {
const SkillType * st = unit - > getType ( ) - > getSkillType ( i ) ;
const AttackSkillType * ast = dynamic_cast < const AttackSkillType * > ( st ) ;
unitBeingAttacked ( result , unit , ast , & distToUnit ) ;
}
// if(result.first == true) {
// printf("~~~~~~~~ Unit [%s - %d] found enemy [%s - %d] distToUnit = %f\n",unit->getFullName().c_str(),unit->getId(),result.second->getFullName().c_str(),result.second->getId(),distToUnit);
// }
return result ;
}
2010-03-13 22:10:45 +01:00
// ==================== updateBuild ====================
2011-03-18 04:53:06 +01:00
void UnitUpdater : : updateBuild ( Unit * unit , int frameIndex ) {
2010-12-05 00:19:43 +01:00
Chrono chrono ;
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugPerformance ) . enabled ) chrono . start ( ) ;
2010-12-05 00:19:43 +01:00
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugUnitCommands ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugUnitCommands , " In [%s::%s Line: %d] unit [%s] will build using command [%s] \n " , __FILE__ , __FUNCTION__ , __LINE__ , unit - > toString ( ) . c_str ( ) , unit - > getCurrCommand ( ) - > toString ( ) . c_str ( ) ) ;
2010-08-28 03:46:26 +02:00
2010-03-13 22:10:45 +01:00
Command * command = unit - > getCurrCommand ( ) ;
const BuildCommandType * bct = static_cast < const BuildCommandType * > ( command - > getCommandType ( ) ) ;
2010-10-24 04:16:44 +02:00
//std::pair<float,Vec2i> distance = map->getUnitDistanceToPos(unit,command->getPos(),command->getUnitType());
//unit->setCurrentUnitTitle("Distance: " + floatToStr(distance.first) + " build pos: " + distance.second.getString() + " current pos: " + unit->getPos().getString());
2010-09-01 01:14:15 +02:00
2010-03-13 22:10:45 +01:00
if ( unit - > getCurrSkill ( ) - > getClass ( ) ! = scBuild ) {
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugUnitCommands ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugUnitCommands , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-08-28 03:46:26 +02:00
2010-03-13 22:10:45 +01:00
//if not building
const UnitType * ut = command - > getUnitType ( ) ;
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugPerformance ) . enabled & & chrono . getMillis ( ) > 0 ) SystemFlags : : OutputDebug ( SystemFlags : : debugPerformance , " In [%s::%s Line: %d] took msecs: %lld \n " , __FILE__ , __FUNCTION__ , __LINE__ , chrono . getMillis ( ) ) ;
2010-12-05 00:19:43 +01:00
2010-07-21 20:21:40 +02:00
TravelState tsValue = tsImpossible ;
switch ( this - > game - > getGameSettings ( ) - > getPathFinderType ( ) ) {
case pfBasic :
2010-08-28 05:43:14 +02:00
{
//Vec2i buildPos = (command->getPos()-Vec2i(1));
2010-12-26 05:38:03 +01:00
Vec2i buildPos = map - > findBestBuildApproach ( unit , command - > getPos ( ) , ut ) ;
2010-08-28 05:43:14 +02:00
//Vec2i buildPos = (command->getPos() + Vec2i(ut->getSize() / 2));
2010-12-01 00:32:39 +01:00
2011-04-03 04:42:45 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugWorldSynch ) . enabled = = true & & frameIndex < 0 ) {
2010-12-01 00:32:39 +01:00
char szBuf [ 4096 ] = " " ;
2010-12-02 00:38:03 +01:00
sprintf ( szBuf , " [updateBuild] unit->getPos() [%s] command->getPos() [%s] buildPos [%s] " ,
unit - > getPos ( ) . getString ( ) . c_str ( ) , command - > getPos ( ) . getString ( ) . c_str ( ) , buildPos . getString ( ) . c_str ( ) ) ;
unit - > logSynchData ( __FILE__ , __LINE__ , szBuf ) ;
2010-12-01 00:32:39 +01:00
}
2011-03-18 04:53:06 +01:00
tsValue = pathFinder - > findPath ( unit , buildPos , NULL , frameIndex ) ;
2010-08-28 05:43:14 +02:00
}
2010-07-21 20:21:40 +02:00
break ;
case pfRoutePlanner :
tsValue = routePlanner - > findPathToBuildSite ( unit , ut , command - > getPos ( ) , command - > getFacing ( ) ) ;
break ;
2010-07-21 22:40:11 +02:00
default :
throw runtime_error ( " detected unsupported pathfinder type! " ) ;
2010-07-21 20:21:40 +02:00
}
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugUnitCommands ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugUnitCommands , " In [%s::%s Line: %d] tsValue = %d \n " , __FILE__ , __FUNCTION__ , __LINE__ , tsValue ) ;
2010-08-28 03:46:26 +02:00
2011-03-18 04:53:06 +01:00
if ( frameIndex < 0 ) {
switch ( tsValue ) {
case tsMoving :
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugUnitCommands ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugUnitCommands , " In [%s::%s Line: %d] tsMoving \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-07-21 20:21:40 +02:00
2011-03-18 04:53:06 +01:00
unit - > setCurrSkill ( bct - > getMoveSkillType ( ) ) ;
break ;
2010-07-21 20:21:40 +02:00
2011-03-18 04:53:06 +01:00
case tsArrived :
{
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugUnitCommands ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugUnitCommands , " In [%s::%s Line: %d] tsArrived: \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2011-01-08 22:53:05 +01:00
2011-03-18 04:53:06 +01:00
//if arrived destination
assert ( ut ) ;
if ( ut = = NULL ) {
throw runtime_error ( " ut == NULL " ) ;
}
2010-07-21 20:21:40 +02:00
2011-03-18 04:53:06 +01:00
bool canOccupyCell = false ;
2010-07-21 20:21:40 +02:00
switch ( this - > game - > getGameSettings ( ) - > getPathFinderType ( ) ) {
case pfBasic :
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugUnitCommands ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugUnitCommands , " In [%s::%s Line: %d] tsArrived about to call map->isFreeCells() for command->getPos() = %s, ut->getSize() = %d \n " , __FILE__ , __FUNCTION__ , __LINE__ , command - > getPos ( ) . getString ( ) . c_str ( ) , ut - > getSize ( ) ) ;
2011-03-18 04:53:06 +01:00
canOccupyCell = map - > isFreeCells ( command - > getPos ( ) , ut - > getSize ( ) , fLand ) ;
2010-07-21 20:21:40 +02:00
break ;
case pfRoutePlanner :
2011-03-18 04:53:06 +01:00
canOccupyCell = map - > canOccupy ( command - > getPos ( ) , ut - > getField ( ) , ut , command - > getFacing ( ) ) ;
2010-07-21 20:21:40 +02:00
break ;
2010-07-21 22:40:11 +02:00
default :
throw runtime_error ( " detected unsupported pathfinder type! " ) ;
2011-03-18 04:53:06 +01:00
}
2010-07-21 20:21:40 +02:00
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugUnitCommands ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugUnitCommands , " In [%s::%s Line: %d] canOccupyCell = %d \n " , __FILE__ , __FUNCTION__ , __LINE__ , canOccupyCell ) ;
2011-03-18 04:53:06 +01:00
if ( canOccupyCell = = true ) {
const UnitType * builtUnitType = command - > getUnitType ( ) ;
CardinalDir facing = command - > getFacing ( ) ;
UnitPathInterface * newpath = NULL ;
switch ( this - > game - > getGameSettings ( ) - > getPathFinderType ( ) ) {
case pfBasic :
newpath = new UnitPathBasic ( ) ;
break ;
case pfRoutePlanner :
newpath = new UnitPath ( ) ;
break ;
default :
throw runtime_error ( " detected unsupported pathfinder type! " ) ;
}
2010-10-30 10:54:00 +02:00
2011-03-18 04:53:06 +01:00
Vec2i buildPos = command - > getPos ( ) ;
Unit * builtUnit = new Unit ( world - > getNextUnitId ( unit - > getFaction ( ) ) , newpath , buildPos , builtUnitType , unit - > getFaction ( ) , world - > getMap ( ) , facing ) ;
2010-10-30 10:54:00 +02:00
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugUnitCommands ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugUnitCommands , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-03-13 22:10:45 +01:00
2011-03-18 04:53:06 +01:00
builtUnit - > create ( ) ;
2010-03-13 22:10:45 +01:00
2011-03-18 04:53:06 +01:00
if ( builtUnitType - > hasSkillClass ( scBeBuilt ) = = false ) {
2011-05-03 20:25:35 +02:00
throw runtime_error ( " Unit [ " + builtUnitType - > getName ( ) + " ] has no be_built skill, producer was [ " + intToStr ( unit - > getId ( ) ) + " - " + unit - > getType ( ) - > getName ( ) + " ]. " ) ;
2011-03-18 04:53:06 +01:00
}
2010-10-30 10:54:00 +02:00
2011-03-18 04:53:06 +01:00
builtUnit - > setCurrSkill ( scBeBuilt ) ;
2010-07-21 20:21:40 +02:00
2011-03-18 04:53:06 +01:00
unit - > setCurrSkill ( bct - > getBuildSkillType ( ) ) ;
unit - > setTarget ( builtUnit ) ;
map - > prepareTerrain ( builtUnit ) ;
2010-10-30 10:54:00 +02:00
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugUnitCommands ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugUnitCommands , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-07-21 20:21:40 +02:00
2011-03-18 04:53:06 +01:00
switch ( this - > game - > getGameSettings ( ) - > getPathFinderType ( ) ) {
case pfBasic :
break ;
case pfRoutePlanner :
world - > getCartographer ( ) - > updateMapMetrics ( builtUnit - > getPos ( ) , builtUnit - > getType ( ) - > getSight ( ) ) ;
break ;
default :
throw runtime_error ( " detected unsupported pathfinder type! " ) ;
}
2010-03-13 22:10:45 +01:00
2011-03-18 04:53:06 +01:00
command - > setUnit ( builtUnit ) ;
2010-05-29 11:38:00 +02:00
2011-03-18 04:53:06 +01:00
//play start sound
2011-03-18 22:23:34 +01:00
if ( unit - > getFactionIndex ( ) = = world - > getThisFactionIndex ( ) | |
2011-10-12 18:03:55 +02:00
( game - > getWorld ( ) - > showWorldForPlayer ( game - > getWorld ( ) - > getThisTeamIndex ( ) ) = = true ) ) {
2011-03-18 04:53:06 +01:00
SoundRenderer : : getInstance ( ) . playFx (
bct - > getStartSound ( ) ,
unit - > getCurrVector ( ) ,
gameCamera - > getPos ( ) ) ;
}
2010-09-01 01:14:15 +02:00
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugUnitCommands ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugUnitCommands , " In [%s::%s Line: %d] unit created for unit [%s] \n " , __FILE__ , __FUNCTION__ , __LINE__ , builtUnit - > toString ( ) . c_str ( ) ) ;
2010-03-13 22:10:45 +01:00
}
2011-03-18 04:53:06 +01:00
else {
//if there are no free cells
unit - > cancelCommand ( ) ;
unit - > setCurrSkill ( scStop ) ;
2010-09-02 07:38:49 +02:00
2011-03-18 04:53:06 +01:00
if ( unit - > getFactionIndex ( ) = = world - > getThisFactionIndex ( ) ) {
console - > addStdMessage ( " BuildingNoPlace " ) ;
}
2010-03-13 22:10:45 +01:00
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugUnitCommands ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugUnitCommands , " In [%s::%s Line: %d] got BuildingNoPlace \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2011-03-18 04:53:06 +01:00
}
}
break ;
2010-09-01 01:14:15 +02:00
2011-03-18 04:53:06 +01:00
case tsBlocked :
if ( unit - > getPath ( ) - > isBlocked ( ) ) {
unit - > cancelCommand ( ) ;
2010-12-05 00:19:43 +01:00
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugUnitCommands ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugUnitCommands , " In [%s::%s Line: %d] got tsBlocked \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2011-03-18 04:53:06 +01:00
}
break ;
}
}
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugPerformance ) . enabled & & chrono . getMillis ( ) > 0 ) SystemFlags : : OutputDebug ( SystemFlags : : debugPerformance , " In [%s::%s Line: %d] took msecs: %lld \n " , __FILE__ , __FUNCTION__ , __LINE__ , chrono . getMillis ( ) ) ;
2010-03-13 22:10:45 +01:00
}
2010-08-28 03:46:26 +02:00
else {
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugUnitCommands ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugUnitCommands , " In [%s::%s Line: %d] tsArrived unit = %s \n " , __FILE__ , __FUNCTION__ , __LINE__ , unit - > toString ( ) . c_str ( ) ) ;
2010-08-28 03:46:26 +02:00
2011-03-18 04:53:06 +01:00
if ( frameIndex < 0 ) {
//if building
Unit * builtUnit = map - > getCell ( unit - > getTargetPos ( ) ) - > getUnit ( fLand ) ;
if ( builtUnit = = NULL ) {
builtUnit = map - > getCell ( unit - > getTargetPos ( ) ) - > getUnitWithEmptyCellMap ( fLand ) ;
}
2010-10-30 10:54:00 +02:00
2011-03-18 04:53:06 +01:00
if ( builtUnit ! = NULL ) {
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugUnitCommands ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugUnitCommands , " In [%s::%s Line: %d] builtUnit = %s \n " , __FILE__ , __FUNCTION__ , __LINE__ , builtUnit - > toString ( ) . c_str ( ) ) ;
2011-03-18 04:53:06 +01:00
}
2010-03-13 22:10:45 +01:00
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugUnitCommands ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugUnitCommands , " In [%s::%s Line: %d] builtUnit = [%p] \n " , __FILE__ , __FUNCTION__ , __LINE__ , builtUnit ) ;
2011-01-08 22:53:05 +01:00
2011-03-18 04:53:06 +01:00
//if unit is killed while building then u==NULL;
if ( builtUnit ! = NULL & & builtUnit ! = command - > getUnit ( ) ) {
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugUnitCommands ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugUnitCommands , " In [%s::%s Line: %d] builtUnit is not the command's unit! \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2011-03-18 04:53:06 +01:00
unit - > setCurrSkill ( scStop ) ;
}
else if ( builtUnit = = NULL | | builtUnit - > isBuilt ( ) ) {
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugUnitCommands ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugUnitCommands , " In [%s::%s Line: %d] builtUnit is NULL or ALREADY built \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-09-02 07:38:49 +02:00
2011-03-18 04:53:06 +01:00
unit - > finishCommand ( ) ;
unit - > setCurrSkill ( scStop ) ;
2010-09-01 01:14:15 +02:00
2011-03-18 04:53:06 +01:00
}
else if ( builtUnit = = NULL | | builtUnit - > repair ( ) ) {
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugUnitCommands ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugUnitCommands , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-09-02 07:38:49 +02:00
2011-03-18 04:53:06 +01:00
//building finished
unit - > finishCommand ( ) ;
unit - > setCurrSkill ( scStop ) ;
2010-09-01 01:14:15 +02:00
2011-03-18 04:53:06 +01:00
builtUnit - > born ( ) ;
scriptManager - > onUnitCreated ( builtUnit ) ;
2011-03-18 22:23:34 +01:00
if ( unit - > getFactionIndex ( ) = = world - > getThisFactionIndex ( ) | |
2011-10-12 18:03:55 +02:00
( game - > getWorld ( ) - > showWorldForPlayer ( game - > getWorld ( ) - > getThisTeamIndex ( ) ) = = true ) ) {
2011-03-18 04:53:06 +01:00
SoundRenderer : : getInstance ( ) . playFx (
bct - > getBuiltSound ( ) ,
unit - > getCurrVector ( ) ,
gameCamera - > getPos ( ) ) ;
}
2010-03-13 22:10:45 +01:00
}
2011-03-18 04:53:06 +01:00
}
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugPerformance ) . enabled & & chrono . getMillis ( ) > 0 ) SystemFlags : : OutputDebug ( SystemFlags : : debugPerformance , " In [%s::%s Line: %d] took msecs: %lld \n " , __FILE__ , __FUNCTION__ , __LINE__ , chrono . getMillis ( ) ) ;
2010-03-13 22:10:45 +01:00
}
2010-12-05 00:19:43 +01:00
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugPerformance ) . enabled & & chrono . getMillis ( ) > 0 ) SystemFlags : : OutputDebug ( SystemFlags : : debugPerformance , " In [%s::%s] Line: %d took msecs: %lld --------------------------- [END OF METHOD] --------------------------- \n " , __FILE__ , __FUNCTION__ , __LINE__ , chrono . getMillis ( ) ) ;
2010-03-13 22:10:45 +01:00
}
// ==================== updateHarvest ====================
2011-03-18 04:53:06 +01:00
void UnitUpdater : : updateHarvest ( Unit * unit , int frameIndex ) {
2010-12-05 00:19:43 +01:00
Chrono chrono ;
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugPerformance ) . enabled ) chrono . start ( ) ;
2010-12-05 00:19:43 +01:00
2010-03-13 22:10:45 +01:00
Command * command = unit - > getCurrCommand ( ) ;
const HarvestCommandType * hct = static_cast < const HarvestCommandType * > ( command - > getCommandType ( ) ) ;
2010-10-20 22:40:29 +02:00
Vec2i targetPos ( - 1 ) ;
2010-03-13 22:10:45 +01:00
2010-07-21 20:21:40 +02:00
TravelState tsValue = tsImpossible ;
2011-09-01 03:11:23 +02:00
//UnitPathInterface *path= unit->getPath();
2010-07-21 20:21:40 +02:00
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugPerformance ) . enabled & & chrono . getMillis ( ) > 0 ) SystemFlags : : OutputDebug ( SystemFlags : : debugPerformance , " In [%s::%s Line: %d] took msecs: %lld \n " , __FILE__ , __FUNCTION__ , __LINE__ , chrono . getMillis ( ) ) ;
2010-12-05 00:19:43 +01:00
2010-03-13 22:10:45 +01:00
if ( unit - > getCurrSkill ( ) - > getClass ( ) ! = scHarvest ) {
//if not working
2010-10-17 10:50:27 +02:00
if ( unit - > getLoadCount ( ) = = 0 ) {
2010-03-13 22:10:45 +01:00
//if not loaded go for resources
2010-10-20 22:40:29 +02:00
Resource * r = map - > getSurfaceCell ( Map : : toSurfCoords ( command - > getPos ( ) ) ) - > getResource ( ) ;
2010-10-17 10:50:27 +02:00
if ( r ! = NULL & & hct - > canHarvest ( r - > getType ( ) ) ) {
2010-03-13 22:10:45 +01:00
//if can harvest dest. pos
2010-07-21 20:21:40 +02:00
bool canHarvestDestPos = false ;
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugPerformance ) . enabled & & chrono . getMillis ( ) > 0 ) SystemFlags : : OutputDebug ( SystemFlags : : debugPerformance , " In [%s::%s Line: %d] took msecs: %lld \n " , __FILE__ , __FUNCTION__ , __LINE__ , chrono . getMillis ( ) ) ;
2010-12-05 00:19:43 +01:00
2010-07-21 20:21:40 +02:00
switch ( this - > game - > getGameSettings ( ) - > getPathFinderType ( ) ) {
case pfBasic :
2010-10-20 22:40:29 +02:00
{
2011-03-29 21:01:15 +02:00
const bool newHarvestPath = false ;
2011-03-29 06:57:36 +02:00
bool isNearResource = false ;
2011-03-29 03:45:10 +02:00
Vec2i clickPos = command - > getOriginalPos ( ) ;
2011-03-29 06:57:36 +02:00
if ( newHarvestPath = = true ) {
isNearResource = map - > isResourceNear ( unit - > getPos ( ) , r - > getType ( ) , targetPos , unit - > getType ( ) - > getSize ( ) , unit , false , & clickPos ) ;
}
else {
isNearResource = map - > isResourceNear ( unit - > getPos ( ) , r - > getType ( ) , targetPos , unit - > getType ( ) - > getSize ( ) , unit ) ;
}
2010-10-20 22:40:29 +02:00
if ( isNearResource = = true ) {
if ( ( unit - > getPos ( ) . dist ( command - > getPos ( ) ) < harvestDistance | | unit - > getPos ( ) . dist ( targetPos ) < harvestDistance ) & & isNearResource = = true ) {
canHarvestDestPos = true ;
}
}
2011-03-29 06:57:36 +02:00
else if ( newHarvestPath = = true ) {
2011-03-29 03:45:10 +02:00
if ( clickPos ! = command - > getOriginalPos ( ) ) {
//printf("%%----------- unit [%s - %d] CHANGING RESOURCE POS from [%s] to [%s]\n",unit->getFullName().c_str(),unit->getId(),command->getOriginalPos().getString().c_str(),clickPos.getString().c_str());
command - > setPos ( clickPos ) ;
}
}
2010-10-20 22:40:29 +02:00
}
2010-07-21 20:21:40 +02:00
break ;
case pfRoutePlanner :
canHarvestDestPos = map - > isResourceNear ( unit - > getPos ( ) , unit - > getType ( ) - > getSize ( ) , r - > getType ( ) , targetPos ) ;
break ;
2010-07-21 22:40:11 +02:00
default :
throw runtime_error ( " detected unsupported pathfinder type! " ) ;
2010-07-21 20:21:40 +02:00
}
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugPerformance ) . enabled & & chrono . getMillis ( ) > 0 ) SystemFlags : : OutputDebug ( SystemFlags : : debugPerformance , " In [%s::%s Line: %d] took msecs: %lld \n " , __FILE__ , __FUNCTION__ , __LINE__ , chrono . getMillis ( ) ) ;
2010-12-05 00:19:43 +01:00
2011-03-18 04:53:06 +01:00
if ( canHarvestDestPos = = true ) {
if ( frameIndex < 0 ) {
unit - > setLastHarvestResourceTarget ( NULL ) ;
}
2010-10-17 10:50:27 +02:00
2010-10-21 03:07:11 +02:00
canHarvestDestPos = ( map - > getSurfaceCell ( Map : : toSurfCoords ( targetPos ) ) ! = NULL & & map - > getSurfaceCell ( Map : : toSurfCoords ( targetPos ) ) - > getResource ( ) ! = NULL & & map - > getSurfaceCell ( Map : : toSurfCoords ( targetPos ) ) - > getResource ( ) - > getType ( ) ! = NULL ) ;
2011-03-18 04:53:06 +01:00
2010-10-20 00:26:49 +02:00
if ( canHarvestDestPos = = true ) {
2011-03-18 04:53:06 +01:00
if ( frameIndex < 0 ) {
//if it finds resources it starts harvesting
unit - > setCurrSkill ( hct - > getHarvestSkillType ( ) ) ;
unit - > setTargetPos ( targetPos ) ;
command - > setPos ( targetPos ) ;
unit - > setLoadCount ( 0 ) ;
unit - > getFaction ( ) - > addResourceTargetToCache ( targetPos ) ;
2010-10-20 00:26:49 +02:00
2011-03-18 04:53:06 +01:00
switch ( this - > game - > getGameSettings ( ) - > getPathFinderType ( ) ) {
case pfBasic :
unit - > setLoadType ( r - > getType ( ) ) ;
break ;
case pfRoutePlanner :
unit - > setLoadType ( r - > getType ( ) ) ;
break ;
default :
throw runtime_error ( " detected unsupported pathfinder type! " ) ;
}
2010-10-20 00:26:49 +02:00
}
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugPerformance ) . enabled & & chrono . getMillis ( ) > 0 ) SystemFlags : : OutputDebug ( SystemFlags : : debugPerformance , " In [%s::%s Line: %d] took msecs: %lld \n " , __FILE__ , __FUNCTION__ , __LINE__ , chrono . getMillis ( ) ) ;
2010-10-17 10:50:27 +02:00
}
2010-03-13 22:10:45 +01:00
}
2010-10-20 00:26:49 +02:00
if ( canHarvestDestPos = = false ) {
2011-03-18 04:53:06 +01:00
if ( frameIndex < 0 ) {
unit - > setLastHarvestResourceTarget ( & targetPos ) ;
}
2010-07-21 20:21:40 +02:00
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugPerformance ) . enabled & & chrono . getMillis ( ) > 0 ) SystemFlags : : OutputDebug ( SystemFlags : : debugPerformance , " In [%s::%s Line: %d] took msecs: %lld \n " , __FILE__ , __FUNCTION__ , __LINE__ , chrono . getMillis ( ) ) ;
2010-12-05 00:19:43 +01:00
2011-04-03 04:42:45 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugWorldSynch ) . enabled = = true & & frameIndex < 0 ) {
2010-12-01 00:32:39 +01:00
char szBuf [ 4096 ] = " " ;
2010-12-02 00:38:03 +01:00
sprintf ( szBuf , " [updateHarvest] unit->getPos() [%s] command->getPos() [%s] " ,
unit - > getPos ( ) . getString ( ) . c_str ( ) , command - > getPos ( ) . getString ( ) . c_str ( ) ) ;
unit - > logSynchData ( __FILE__ , __LINE__ , szBuf ) ;
2010-12-01 00:32:39 +01:00
}
2010-03-13 22:10:45 +01:00
//if not continue walking
2010-10-19 18:54:48 +02:00
bool wasStuck = false ;
2010-07-21 20:21:40 +02:00
TravelState tsValue = tsImpossible ;
switch ( this - > game - > getGameSettings ( ) - > getPathFinderType ( ) ) {
case pfBasic :
2011-03-18 04:53:06 +01:00
tsValue = pathFinder - > findPath ( unit , command - > getPos ( ) , & wasStuck , frameIndex ) ;
if ( tsValue = = tsMoving & & frameIndex < 0 ) {
2010-07-21 20:21:40 +02:00
unit - > setCurrSkill ( hct - > getMoveSkillType ( ) ) ;
}
break ;
case pfRoutePlanner :
tsValue = routePlanner - > findPathToResource ( unit , command - > getPos ( ) , r - > getType ( ) ) ;
2011-03-18 04:53:06 +01:00
if ( tsValue = = tsMoving & & frameIndex < 0 ) {
2010-07-21 20:21:40 +02:00
unit - > setCurrSkill ( hct - > getMoveSkillType ( ) ) ;
}
break ;
2010-07-21 22:40:11 +02:00
default :
throw runtime_error ( " detected unsupported pathfinder type! " ) ;
2010-07-21 20:21:40 +02:00
}
2010-10-19 18:54:48 +02:00
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugPerformance ) . enabled & & chrono . getMillis ( ) > 0 ) SystemFlags : : OutputDebug ( SystemFlags : : debugPerformance , " In [%s::%s Line: %d] took msecs: %lld \n " , __FILE__ , __FUNCTION__ , __LINE__ , chrono . getMillis ( ) ) ;
2010-12-05 00:19:43 +01:00
2010-10-22 06:48:24 +02:00
// If the unit is blocked or Even worse 'stuck' then try to
// find the same resource type elsewhere, but close by
if ( ( wasStuck = = true | | tsValue = = tsBlocked ) & & unit - > isAlive ( ) = = true ) {
2010-10-20 00:26:49 +02:00
switch ( this - > game - > getGameSettings ( ) - > getPathFinderType ( ) ) {
case pfBasic :
2010-10-20 22:40:29 +02:00
{
2010-10-22 06:48:24 +02:00
bool isNearResource = map - > isResourceNear ( unit - > getPos ( ) , r - > getType ( ) , targetPos , unit - > getType ( ) - > getSize ( ) , unit , true ) ;
2010-10-20 22:40:29 +02:00
if ( isNearResource = = true ) {
if ( ( unit - > getPos ( ) . dist ( command - > getPos ( ) ) < harvestDistance | | unit - > getPos ( ) . dist ( targetPos ) < harvestDistance ) & & isNearResource = = true ) {
canHarvestDestPos = true ;
}
}
}
2010-10-20 00:26:49 +02:00
break ;
case pfRoutePlanner :
canHarvestDestPos = map - > isResourceNear ( unit - > getPos ( ) , unit - > getType ( ) - > getSize ( ) , r - > getType ( ) , targetPos ) ;
break ;
default :
throw runtime_error ( " detected unsupported pathfinder type! " ) ;
}
if ( canHarvestDestPos = = true ) {
2011-03-18 04:53:06 +01:00
if ( frameIndex < 0 ) {
unit - > setLastHarvestResourceTarget ( NULL ) ;
}
2010-10-20 00:26:49 +02:00
2010-10-21 03:07:11 +02:00
canHarvestDestPos = ( map - > getSurfaceCell ( Map : : toSurfCoords ( targetPos ) ) ! = NULL & & map - > getSurfaceCell ( Map : : toSurfCoords ( targetPos ) ) - > getResource ( ) ! = NULL & & map - > getSurfaceCell ( Map : : toSurfCoords ( targetPos ) ) - > getResource ( ) - > getType ( ) ! = NULL ) ;
2010-10-20 00:26:49 +02:00
if ( canHarvestDestPos = = true ) {
2011-03-18 04:53:06 +01:00
if ( frameIndex < 0 ) {
//if it finds resources it starts harvesting
unit - > setCurrSkill ( hct - > getHarvestSkillType ( ) ) ;
unit - > setTargetPos ( targetPos ) ;
command - > setPos ( targetPos ) ;
unit - > setLoadCount ( 0 ) ;
unit - > getFaction ( ) - > addResourceTargetToCache ( targetPos ) ;
switch ( this - > game - > getGameSettings ( ) - > getPathFinderType ( ) ) {
case pfBasic :
unit - > setLoadType ( r - > getType ( ) ) ;
break ;
case pfRoutePlanner :
unit - > setLoadType ( r - > getType ( ) ) ;
break ;
default :
throw runtime_error ( " detected unsupported pathfinder type! " ) ;
}
2010-10-20 00:26:49 +02:00
}
}
2010-12-05 00:19:43 +01:00
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugPerformance ) . enabled & & chrono . getMillis ( ) > 0 ) SystemFlags : : OutputDebug ( SystemFlags : : debugPerformance , " In [%s::%s Line: %d] took msecs: %lld \n " , __FILE__ , __FUNCTION__ , __LINE__ , chrono . getMillis ( ) ) ;
2010-10-20 00:26:49 +02:00
}
2010-10-26 08:43:42 +02:00
2010-10-20 00:26:49 +02:00
if ( canHarvestDestPos = = false ) {
2011-03-18 04:53:06 +01:00
if ( frameIndex < 0 ) {
unit - > setLastHarvestResourceTarget ( & targetPos ) ;
}
2010-10-20 00:26:49 +02:00
if ( targetPos . x > = 0 ) {
//if not continue walking
2010-12-01 00:32:39 +01:00
2011-04-03 04:42:45 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugWorldSynch ) . enabled = = true & & frameIndex < 0 ) {
2010-12-01 00:32:39 +01:00
char szBuf [ 4096 ] = " " ;
2010-12-02 00:38:03 +01:00
sprintf ( szBuf , " [updateHarvest #2] unit->getPos() [%s] command->getPos() [%s] targetPos [%s] " ,
unit - > getPos ( ) . getString ( ) . c_str ( ) , command - > getPos ( ) . getString ( ) . c_str ( ) , targetPos . getString ( ) . c_str ( ) ) ;
unit - > logSynchData ( __FILE__ , __LINE__ , szBuf ) ;
2010-12-01 00:32:39 +01:00
}
2010-10-20 00:26:49 +02:00
wasStuck = false ;
TravelState tsValue = tsImpossible ;
switch ( this - > game - > getGameSettings ( ) - > getPathFinderType ( ) ) {
case pfBasic :
2011-03-18 04:53:06 +01:00
tsValue = pathFinder - > findPath ( unit , targetPos , & wasStuck , frameIndex ) ;
if ( tsValue = = tsMoving & & frameIndex < 0 ) {
2010-10-20 00:26:49 +02:00
unit - > setCurrSkill ( hct - > getMoveSkillType ( ) ) ;
command - > setPos ( targetPos ) ;
}
break ;
case pfRoutePlanner :
tsValue = routePlanner - > findPathToResource ( unit , targetPos , r - > getType ( ) ) ;
2011-03-18 04:53:06 +01:00
if ( tsValue = = tsMoving & & frameIndex < 0 ) {
2010-10-20 00:26:49 +02:00
unit - > setCurrSkill ( hct - > getMoveSkillType ( ) ) ;
command - > setPos ( targetPos ) ;
}
break ;
default :
throw runtime_error ( " detected unsupported pathfinder type! " ) ;
}
}
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugPerformance ) . enabled & & chrono . getMillis ( ) > 0 ) SystemFlags : : OutputDebug ( SystemFlags : : debugPerformance , " In [%s::%s Line: %d] took msecs: %lld \n " , __FILE__ , __FUNCTION__ , __LINE__ , chrono . getMillis ( ) ) ;
2010-12-05 00:19:43 +01:00
2011-03-18 04:53:06 +01:00
if ( wasStuck = = true & & frameIndex < 0 ) {
2010-10-20 00:26:49 +02:00
//if can't harvest, search for another resource
unit - > setCurrSkill ( scStop ) ;
if ( searchForResource ( unit , hct ) = = false ) {
unit - > finishCommand ( ) ;
}
}
2010-10-19 18:54:48 +02:00
}
}
2010-03-13 22:10:45 +01:00
}
}
2010-10-19 18:54:48 +02:00
else {
2011-03-18 04:53:06 +01:00
if ( frameIndex < 0 ) {
//if can't harvest, search for another resource
unit - > setCurrSkill ( scStop ) ;
if ( searchForResource ( unit , hct ) = = false ) {
unit - > finishCommand ( ) ;
}
2010-03-13 22:10:45 +01:00
}
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugPerformance ) . enabled & & chrono . getMillis ( ) > 0 ) SystemFlags : : OutputDebug ( SystemFlags : : debugPerformance , " In [%s::%s Line: %d] took msecs: %lld \n " , __FILE__ , __FUNCTION__ , __LINE__ , chrono . getMillis ( ) ) ;
2010-03-13 22:10:45 +01:00
}
2010-12-05 00:19:43 +01:00
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugPerformance ) . enabled & & chrono . getMillis ( ) > 0 ) SystemFlags : : OutputDebug ( SystemFlags : : debugPerformance , " In [%s::%s Line: %d] took msecs: %lld \n " , __FILE__ , __FUNCTION__ , __LINE__ , chrono . getMillis ( ) ) ;
2010-03-13 22:10:45 +01:00
}
2010-10-17 10:50:27 +02:00
else {
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugPerformance ) . enabled & & chrono . getMillis ( ) > 0 ) SystemFlags : : OutputDebug ( SystemFlags : : debugPerformance , " In [%s::%s Line: %d] took msecs: %lld \n " , __FILE__ , __FUNCTION__ , __LINE__ , chrono . getMillis ( ) ) ;
2010-12-05 00:19:43 +01:00
2010-03-13 22:10:45 +01:00
//if loaded, return to store
Unit * store = world - > nearestStore ( unit - > getPos ( ) , unit - > getFaction ( ) - > getIndex ( ) , unit - > getLoadType ( ) ) ;
2011-03-18 04:53:06 +01:00
if ( store ! = NULL ) {
2011-04-03 04:42:45 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugWorldSynch ) . enabled = = true & & frameIndex < 0 ) {
2010-12-01 00:32:39 +01:00
char szBuf [ 4096 ] = " " ;
2010-12-02 00:38:03 +01:00
sprintf ( szBuf , " [updateHarvest #3] unit->getPos() [%s] store->getCenteredPos() [%s] " ,
unit - > getPos ( ) . getString ( ) . c_str ( ) , store - > getCenteredPos ( ) . getString ( ) . c_str ( ) ) ;
unit - > logSynchData ( __FILE__ , __LINE__ , szBuf ) ;
2010-12-01 00:32:39 +01:00
}
2010-07-21 20:21:40 +02:00
TravelState tsValue = tsImpossible ;
switch ( this - > game - > getGameSettings ( ) - > getPathFinderType ( ) ) {
case pfBasic :
2011-03-18 04:53:06 +01:00
tsValue = pathFinder - > findPath ( unit , store - > getCenteredPos ( ) , NULL , frameIndex ) ;
2010-07-21 20:21:40 +02:00
break ;
case pfRoutePlanner :
tsValue = routePlanner - > findPathToStore ( unit , store ) ;
break ;
2010-07-21 22:40:11 +02:00
default :
throw runtime_error ( " detected unsupported pathfinder type! " ) ;
2010-07-21 20:21:40 +02:00
}
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugPerformance ) . enabled & & chrono . getMillis ( ) > 0 ) SystemFlags : : OutputDebug ( SystemFlags : : debugPerformance , " In [%s::%s Line: %d] took msecs: %lld \n " , __FILE__ , __FUNCTION__ , __LINE__ , chrono . getMillis ( ) ) ;
2010-12-05 00:19:43 +01:00
2011-03-18 04:53:06 +01:00
if ( frameIndex < 0 ) {
switch ( tsValue ) {
case tsMoving :
2011-04-17 07:29:25 +02:00
{
if ( hct - > canHarvest ( unit - > getLoadType ( ) ) = = false ) {
// hct has changed to a different harvest command.
const HarvestCommandType * previousHarvestCmd = unit - > getType ( ) - > getFirstHarvestCommand ( unit - > getLoadType ( ) , unit - > getFaction ( ) ) ;
if ( previousHarvestCmd ! = NULL ) {
//printf("\n\n#1\n\n");
unit - > setCurrSkill ( previousHarvestCmd - > getMoveLoadedSkillType ( ) ) ; // make sure we use the right harvest animation
}
else {
//printf("\n\n#2\n\n");
unit - > setCurrSkill ( hct - > getMoveLoadedSkillType ( ) ) ;
}
}
else {
unit - > setCurrSkill ( hct - > getMoveLoadedSkillType ( ) ) ;
}
}
2011-03-18 04:53:06 +01:00
break ;
default :
break ;
2010-11-11 02:24:48 +01:00
}
2010-03-13 22:10:45 +01:00
2011-03-18 04:53:06 +01:00
//world->changePosCells(unit,unit->getPos()+unit->getDest());
2011-10-28 07:22:41 +02:00
if ( map - > isNextTo ( unit , store ) ) {
2011-03-18 04:53:06 +01:00
//update resources
int resourceAmount = unit - > getLoadCount ( ) ;
if ( unit - > getFaction ( ) - > getCpuControl ( ) )
{
int resourceMultiplierIndex = game - > getGameSettings ( ) - > getResourceMultiplierIndex ( unit - > getFaction ( ) - > getIndex ( ) ) ;
resourceAmount = ( resourceAmount * ( resourceMultiplierIndex + 5 ) ) / 10 ;
}
unit - > getFaction ( ) - > incResourceAmount ( unit - > getLoadType ( ) , resourceAmount ) ;
world - > getStats ( ) - > harvest ( unit - > getFactionIndex ( ) , resourceAmount ) ;
scriptManager - > onResourceHarvested ( ) ;
2010-12-05 00:19:43 +01:00
2011-03-18 04:53:06 +01:00
//if next to a store unload resources
unit - > getPath ( ) - > clear ( ) ;
unit - > setCurrSkill ( scStop ) ;
unit - > setLoadCount ( 0 ) ;
2011-03-25 21:51:14 +01:00
command - > setPosToOriginalPos ( ) ;
2011-03-18 04:53:06 +01:00
}
}
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugPerformance ) . enabled & & chrono . getMillis ( ) > 0 ) SystemFlags : : OutputDebug ( SystemFlags : : debugPerformance , " In [%s::%s Line: %d] took msecs: %lld \n " , __FILE__ , __FUNCTION__ , __LINE__ , chrono . getMillis ( ) ) ;
2010-03-13 22:10:45 +01:00
}
2010-10-19 18:54:48 +02:00
else {
2011-03-18 04:53:06 +01:00
if ( frameIndex < 0 ) {
unit - > finishCommand ( ) ;
}
2010-03-13 22:10:45 +01:00
}
}
}
2010-10-17 10:50:27 +02:00
else {
2011-03-18 04:53:06 +01:00
if ( frameIndex < 0 ) {
//if working
//unit->setLastHarvestResourceTarget(NULL);
2010-10-17 10:50:27 +02:00
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugPerformance ) . enabled & & chrono . getMillis ( ) > 0 ) SystemFlags : : OutputDebug ( SystemFlags : : debugPerformance , " In [%s::%s Line: %d] took msecs: %lld \n " , __FILE__ , __FUNCTION__ , __LINE__ , chrono . getMillis ( ) ) ;
2010-12-05 00:19:43 +01:00
2011-03-18 04:53:06 +01:00
const Vec2i unitTargetPos = unit - > getTargetPos ( ) ;
SurfaceCell * sc = map - > getSurfaceCell ( Map : : toSurfCoords ( unitTargetPos ) ) ;
Resource * r = sc - > getResource ( ) ;
2010-04-06 05:28:20 +02:00
2011-03-18 04:53:06 +01:00
if ( r ! = NULL ) {
2011-04-17 07:29:25 +02:00
if ( hct - > canHarvest ( r - > getType ( ) ) = = false | |
r - > getType ( ) - > getName ( ) ! = unit - > getLoadType ( ) - > getName ( ) ) {
2011-03-18 04:53:06 +01:00
// hct has changed to a different harvest command.
2011-04-17 07:29:25 +02:00
if ( r - > getType ( ) - > getName ( ) ! = unit - > getLoadType ( ) - > getName ( ) ) {
const HarvestCommandType * previousHarvestCmd = unit - > getType ( ) - > getFirstHarvestCommand ( unit - > getLoadType ( ) , unit - > getFaction ( ) ) ;
if ( previousHarvestCmd ! = NULL ) {
//printf("\n\n#1\n\n");
unit - > setCurrSkill ( previousHarvestCmd - > getStopLoadedSkillType ( ) ) ; // make sure we use the right harvest animation
}
else {
//printf("\n\n#2\n\n");
unit - > setCurrSkill ( hct - > getStopLoadedSkillType ( ) ) ;
}
}
else if ( hct - > canHarvest ( r - > getType ( ) ) = = false ) {
const HarvestCommandType * previousHarvestCmd = unit - > getType ( ) - > getFirstHarvestCommand ( unit - > getLoadType ( ) , unit - > getFaction ( ) ) ;
if ( previousHarvestCmd ! = NULL ) {
//printf("\n\n#3\n\n");
unit - > setCurrSkill ( previousHarvestCmd - > getStopLoadedSkillType ( ) ) ; // make sure we use the right harvest animation
}
else {
//printf("\n\n#4\n\n");
unit - > setCurrSkill ( hct - > getStopLoadedSkillType ( ) ) ;
}
}
else {
//printf("\n\n#5 [%s] [%s]\n\n",r->getType()->getName().c_str(),unit->getLoadType()->getName().c_str());
unit - > setCurrSkill ( hct - > getStopLoadedSkillType ( ) ) ; // this is actually the wrong animation
}
2011-03-18 04:53:06 +01:00
unit - > getPath ( ) - > clear ( ) ;
2010-12-05 00:19:43 +01:00
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugPerformance ) . enabled & & chrono . getMillis ( ) > 0 ) SystemFlags : : OutputDebug ( SystemFlags : : debugPerformance , " In [%s::%s Line: %d] took msecs: %lld \n " , __FILE__ , __FUNCTION__ , __LINE__ , chrono . getMillis ( ) ) ;
2011-03-18 04:53:06 +01:00
}
else {
2010-11-09 10:06:52 +01:00
2011-03-18 04:53:06 +01:00
// if there is a resource, continue working, until loaded
unit - > update2 ( ) ;
2010-11-09 10:06:52 +01:00
2011-03-18 04:53:06 +01:00
if ( unit - > getProgress2 ( ) > = hct - > getHitsPerUnit ( ) ) {
if ( unit - > getLoadCount ( ) < hct - > getMaxLoad ( ) ) {
unit - > setProgress2 ( 0 ) ;
unit - > setLoadCount ( unit - > getLoadCount ( ) + 1 ) ;
2010-08-25 09:29:35 +02:00
2011-03-18 04:53:06 +01:00
//if resource exausted, then delete it and stop
if ( r - > decAmount ( 1 ) ) {
const ResourceType * rt = r - > getType ( ) ;
sc - > deleteResource ( ) ;
2011-12-02 17:07:59 +01:00
world - > removeResourceTargetFromCache ( unitTargetPos ) ;
2010-08-25 09:29:35 +02:00
2011-03-18 04:53:06 +01:00
switch ( this - > game - > getGameSettings ( ) - > getPathFinderType ( ) ) {
case pfBasic :
break ;
case pfRoutePlanner :
world - > getCartographer ( ) - > onResourceDepleted ( Map : : toSurfCoords ( unit - > getTargetPos ( ) ) , rt ) ;
break ;
default :
throw runtime_error ( " detected unsupported pathfinder type! " ) ;
}
2011-04-17 07:29:25 +02:00
//printf("\n\n#6\n\n");
2011-03-18 04:53:06 +01:00
unit - > setCurrSkill ( hct - > getStopLoadedSkillType ( ) ) ;
2010-08-25 09:29:35 +02:00
}
2011-03-18 04:53:06 +01:00
}
2010-04-06 05:28:20 +02:00
2011-03-18 04:53:06 +01:00
if ( unit - > getLoadCount ( ) > = hct - > getMaxLoad ( ) ) {
2011-04-17 07:29:25 +02:00
//printf("\n\n#7\n\n");
2010-08-25 09:29:35 +02:00
unit - > setCurrSkill ( hct - > getStopLoadedSkillType ( ) ) ;
2011-03-18 04:53:06 +01:00
unit - > getPath ( ) - > clear ( ) ;
2010-08-25 09:29:35 +02:00
}
}
2010-11-09 10:06:52 +01:00
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugPerformance ) . enabled & & chrono . getMillis ( ) > 0 ) SystemFlags : : OutputDebug ( SystemFlags : : debugPerformance , " In [%s::%s Line: %d] took msecs: %lld \n " , __FILE__ , __FUNCTION__ , __LINE__ , chrono . getMillis ( ) ) ;
2010-04-06 05:28:20 +02:00
}
}
2011-03-18 04:53:06 +01:00
else {
//if there is no resource, just stop
2011-04-17 07:29:25 +02:00
//printf("\n\n#8\n\n");
2011-03-18 04:53:06 +01:00
unit - > setCurrSkill ( hct - > getStopLoadedSkillType ( ) ) ;
}
2010-04-06 05:28:20 +02:00
}
2010-03-13 22:10:45 +01:00
}
2010-12-05 00:19:43 +01:00
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugPerformance ) . enabled & & chrono . getMillis ( ) > 0 ) SystemFlags : : OutputDebug ( SystemFlags : : debugPerformance , " In [%s::%s] Line: %d took msecs: %lld --------------------------- [END OF METHOD] --------------------------- \n " , __FILE__ , __FUNCTION__ , __LINE__ , chrono . getMillis ( ) ) ;
2010-03-13 22:10:45 +01:00
}
2010-08-28 03:46:26 +02:00
void UnitUpdater : : SwapActiveCommand ( Unit * unitSrc , Unit * unitDest ) {
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugUnitCommands ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugUnitCommands , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-08-28 03:46:26 +02:00
if ( unitSrc - > getCommandSize ( ) > 0 & & unitDest - > getCommandSize ( ) > 0 ) {
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugUnitCommands ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugUnitCommands , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-08-28 03:46:26 +02:00
Command * cmd1 = unitSrc - > getCurrCommand ( ) ;
Command * cmd2 = unitDest - > getCurrCommand ( ) ;
unitSrc - > replaceCurrCommand ( cmd2 ) ;
unitDest - > replaceCurrCommand ( cmd1 ) ;
}
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugUnitCommands ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugUnitCommands , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-08-28 03:46:26 +02:00
}
2010-09-02 07:38:49 +02:00
void UnitUpdater : : SwapActiveCommandState ( Unit * unit , CommandStateType commandStateType ,
const CommandType * commandType ,
int originalValue , int newValue ) {
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugUnitCommands ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugUnitCommands , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-09-02 07:38:49 +02:00
if ( commandStateType = = cst_linkedUnit ) {
if ( dynamic_cast < const BuildCommandType * > ( commandType ) ! = NULL ) {
for ( int i = 0 ; i < unit - > getFaction ( ) - > getUnitCount ( ) ; + + i ) {
Unit * peerUnit = unit - > getFaction ( ) - > getUnit ( i ) ;
if ( peerUnit ! = NULL ) {
if ( peerUnit - > getCommandSize ( ) > 0 ) {
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugUnitCommands ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugUnitCommands , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-09-02 07:38:49 +02:00
Command * peerCommand = peerUnit - > getCurrCommand ( ) ;
//const BuildCommandType *bct = dynamic_cast<const BuildCommandType*>(peerCommand->getCommandType());
//if(bct != NULL) {
if ( peerCommand ! = NULL ) {
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugUnitCommands ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugUnitCommands , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-09-02 07:38:49 +02:00
//if(command->getPos() == peerCommand->getPos()) {
if ( peerCommand - > getStateType ( ) = = commandStateType & &
peerCommand - > getStateValue ( ) = = originalValue ) {
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugUnitCommands ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugUnitCommands , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-09-02 07:38:49 +02:00
peerCommand - > setStateValue ( newValue ) ;
}
}
}
}
}
}
}
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugUnitCommands ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugUnitCommands , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-09-02 07:38:49 +02:00
}
2010-08-28 03:46:26 +02:00
Unit * UnitUpdater : : findPeerUnitBuilder ( Unit * unit ) {
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugUnitCommands ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugUnitCommands , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-08-28 03:46:26 +02:00
Unit * foundUnitBuilder = NULL ;
if ( unit - > getCommandSize ( ) > 0 ) {
2010-08-30 22:45:12 +02:00
Command * command = unit - > getCurrCommand ( ) ;
2010-08-28 03:46:26 +02:00
if ( command ! = NULL ) {
const RepairCommandType * rct = dynamic_cast < const RepairCommandType * > ( command - > getCommandType ( ) ) ;
2010-08-30 22:45:12 +02:00
if ( rct ! = NULL & & command - > getStateType ( ) = = cst_linkedUnit ) {
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugUnitCommands ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugUnitCommands , " In [%s::%s Line: %d] looking for command->getStateValue() = %d \n " , __FILE__ , __FUNCTION__ , __LINE__ , command - > getStateValue ( ) ) ;
2011-01-08 22:53:05 +01:00
Unit * firstLinkedPeerRepairer = NULL ;
2010-08-28 03:46:26 +02:00
for ( int i = 0 ; i < unit - > getFaction ( ) - > getUnitCount ( ) ; + + i ) {
Unit * peerUnit = unit - > getFaction ( ) - > getUnit ( i ) ;
if ( peerUnit ! = NULL ) {
if ( peerUnit - > getCommandSize ( ) > 0 ) {
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugUnitCommands ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugUnitCommands , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-08-28 03:46:26 +02:00
Command * peerCommand = peerUnit - > getCurrCommand ( ) ;
const BuildCommandType * bct = dynamic_cast < const BuildCommandType * > ( peerCommand - > getCommandType ( ) ) ;
if ( bct ! = NULL ) {
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugUnitCommands ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugUnitCommands , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-08-28 03:46:26 +02:00
2010-08-30 22:45:12 +02:00
if ( command - > getStateValue ( ) = = peerUnit - > getId ( ) ) {
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugUnitCommands ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugUnitCommands , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-08-28 03:46:26 +02:00
foundUnitBuilder = peerUnit ;
break ;
}
}
2011-01-08 22:53:05 +01:00
else {
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugUnitCommands ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugUnitCommands , " In [%s::%s Line: %d] **peer NOT building**, peerUnit = [%s] \n " , __FILE__ , __FUNCTION__ , __LINE__ , peerUnit - > toString ( ) . c_str ( ) ) ;
2011-01-08 22:53:05 +01:00
if ( firstLinkedPeerRepairer = = NULL ) {
const RepairCommandType * prct = dynamic_cast < const RepairCommandType * > ( peerCommand - > getCommandType ( ) ) ;
if ( prct ! = NULL ) {
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugUnitCommands ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugUnitCommands , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2011-01-08 22:53:05 +01:00
2011-01-11 06:18:53 +01:00
if ( unit - > getId ( ) ! = peerUnit - > getId ( ) & & command - > getStateValue ( ) = = peerUnit - > getId ( ) ) {
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugUnitCommands ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugUnitCommands , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2011-01-08 22:53:05 +01:00
firstLinkedPeerRepairer = peerUnit ;
}
}
}
}
2010-08-28 03:46:26 +02:00
}
}
}
2011-01-08 22:53:05 +01:00
2011-01-11 06:18:53 +01:00
if ( foundUnitBuilder = = NULL & & firstLinkedPeerRepairer ! = NULL ) {
2011-01-08 22:53:05 +01:00
foundUnitBuilder = firstLinkedPeerRepairer ;
}
2010-08-28 03:46:26 +02:00
}
}
}
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugUnitCommands ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugUnitCommands , " In [%s::%s Line: %d] returning foundUnitBuilder = [%s] \n " , __FILE__ , __FUNCTION__ , __LINE__ , ( foundUnitBuilder ! = NULL ? foundUnitBuilder - > toString ( ) . c_str ( ) : " null " ) ) ;
2011-01-08 22:53:05 +01:00
2010-08-28 03:46:26 +02:00
return foundUnitBuilder ;
}
2010-03-13 22:10:45 +01:00
// ==================== updateRepair ====================
2011-03-18 04:53:06 +01:00
void UnitUpdater : : updateRepair ( Unit * unit , int frameIndex ) {
// Nothing to do
if ( frameIndex > = 0 ) {
clearUnitPrecache ( unit ) ;
return ;
}
2010-12-05 00:19:43 +01:00
Chrono chrono ;
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugPerformance ) . enabled ) chrono . start ( ) ;
2010-08-28 03:46:26 +02:00
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugUnitCommands ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugUnitCommands , " In [%s::%s Line: %d] unit = %p \n " , __FILE__ , __FUNCTION__ , __LINE__ , unit ) ;
2010-08-28 03:46:26 +02:00
2011-08-31 21:44:19 +02:00
//if(unit != NULL) {
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugUnitCommands ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugUnitCommands , " In [%s::%s Line: %d] unit doing the repair [%s] - %d \n " , __FILE__ , __FUNCTION__ , __LINE__ , unit - > getFullName ( ) . c_str ( ) , unit - > getId ( ) ) ;
//}
2010-09-28 21:52:31 +02:00
2010-03-13 22:10:45 +01:00
Command * command = unit - > getCurrCommand ( ) ;
const RepairCommandType * rct = static_cast < const RepairCommandType * > ( command - > getCommandType ( ) ) ;
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugUnitCommands ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugUnitCommands , " In [%s::%s Line: %d] rct = %p \n " , __FILE__ , __FUNCTION__ , __LINE__ , rct ) ;
2010-08-28 03:46:26 +02:00
2010-10-30 10:54:00 +02:00
Unit * repaired = map - > getCell ( command - > getPos ( ) ) - > getUnitWithEmptyCellMap ( fLand ) ;
if ( repaired = = NULL ) {
repaired = map - > getCell ( command - > getPos ( ) ) - > getUnit ( fLand ) ;
}
2010-09-28 17:22:04 +02:00
2010-09-28 21:52:31 +02:00
if ( repaired ! = NULL ) {
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugUnitCommands ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugUnitCommands , " In [%s::%s Line: %d] unit to repair [%s] - %d \n " , __FILE__ , __FUNCTION__ , __LINE__ , repaired - > getFullName ( ) . c_str ( ) , repaired - > getId ( ) ) ;
2010-09-28 21:52:31 +02:00
}
2010-12-05 00:19:43 +01:00
if ( chrono . getMillis ( ) > 0 ) SystemFlags : : OutputDebug ( SystemFlags : : debugPerformance , " In [%s::%s] Line: %d took msecs: %lld \n " , __FILE__ , __FUNCTION__ , __LINE__ , chrono . getMillis ( ) ) ;
2010-09-28 17:22:04 +02:00
// Check if the 'repaired' unit is actually the peer unit in a multi-build?
Unit * peerUnitBuilder = findPeerUnitBuilder ( unit ) ;
2010-09-28 21:52:31 +02:00
if ( peerUnitBuilder ! = NULL ) {
2011-01-17 17:31:25 +01:00
SystemFlags : : OutputDebug ( SystemFlags : : debugUnitCommands , " In [%s::%s Line: %d] unit peer [%s] - %d \n " , __FILE__ , __FUNCTION__ , __LINE__ , peerUnitBuilder - > getFullName ( ) . c_str ( ) , peerUnitBuilder - > getId ( ) ) ;
2010-09-28 21:52:31 +02:00
}
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugPerformance ) . enabled & & chrono . getMillis ( ) > 0 ) SystemFlags : : OutputDebug ( SystemFlags : : debugPerformance , " In [%s::%s] Line: %d took msecs: %lld \n " , __FILE__ , __FUNCTION__ , __LINE__ , chrono . getMillis ( ) ) ;
2010-12-05 00:19:43 +01:00
2011-01-20 08:56:02 +01:00
// Ensure we have the right unit to repair
2010-09-28 21:52:31 +02:00
if ( peerUnitBuilder ! = NULL ) {
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugUnitCommands ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugUnitCommands , " In [%s::%s Line: %d] peerUnitBuilder = %p \n " , __FILE__ , __FUNCTION__ , __LINE__ , peerUnitBuilder ) ;
2010-09-28 17:22:04 +02:00
if ( peerUnitBuilder - > getCurrCommand ( ) - > getUnit ( ) ! = NULL ) {
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugUnitCommands ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugUnitCommands , " In [%s::%s Line: %d] peerbuilder's unitid = %d \n " , __FILE__ , __FUNCTION__ , __LINE__ , peerUnitBuilder - > getCurrCommand ( ) - > getUnit ( ) - > getId ( ) ) ;
2010-09-28 17:22:04 +02:00
repaired = peerUnitBuilder - > getCurrCommand ( ) - > getUnit ( ) ;
}
}
2011-10-28 07:22:41 +02:00
bool nextToRepaired = repaired ! = NULL & & map - > isNextTo ( unit , repaired ) ;
2010-03-13 22:10:45 +01:00
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugPerformance ) . enabled & & chrono . getMillis ( ) > 0 ) SystemFlags : : OutputDebug ( SystemFlags : : debugPerformance , " In [%s::%s] Line: %d took msecs: %lld \n " , __FILE__ , __FUNCTION__ , __LINE__ , chrono . getMillis ( ) ) ;
2010-12-05 00:19:43 +01:00
2010-09-28 17:22:04 +02:00
peerUnitBuilder = NULL ;
2010-08-28 03:46:26 +02:00
if ( repaired = = NULL ) {
peerUnitBuilder = findPeerUnitBuilder ( unit ) ;
if ( peerUnitBuilder ! = NULL ) {
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugUnitCommands ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugUnitCommands , " In [%s::%s Line: %d] peerUnitBuilder = %p \n " , __FILE__ , __FUNCTION__ , __LINE__ , peerUnitBuilder ) ;
2010-08-28 03:46:26 +02:00
2010-09-02 07:38:49 +02:00
if ( peerUnitBuilder - > getCurrCommand ( ) - > getUnit ( ) ! = NULL ) {
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugUnitCommands ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugUnitCommands , " In [%s::%s Line: %d] peerbuilder's unitid = %d \n " , __FILE__ , __FUNCTION__ , __LINE__ , peerUnitBuilder - > getCurrCommand ( ) - > getUnit ( ) - > getId ( ) ) ;
2010-09-01 06:19:20 +02:00
2010-09-02 07:38:49 +02:00
repaired = peerUnitBuilder - > getCurrCommand ( ) - > getUnit ( ) ;
2011-10-28 07:22:41 +02:00
nextToRepaired = repaired ! = NULL & & map - > isNextTo ( unit , repaired ) ;
2010-09-02 07:38:49 +02:00
}
else {
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugUnitCommands ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugUnitCommands , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-08-28 03:46:26 +02:00
2010-12-26 05:38:03 +01:00
Vec2i buildPos = map - > findBestBuildApproach ( unit , command - > getPos ( ) , peerUnitBuilder - > getCurrCommand ( ) - > getUnitType ( ) ) ;
2010-08-28 03:46:26 +02:00
2010-09-02 07:38:49 +02:00
//nextToRepaired= (unit->getPos() == (command->getPos()-Vec2i(1)));
nextToRepaired = ( unit - > getPos ( ) = = buildPos ) ;
2010-08-28 03:46:26 +02:00
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugUnitCommands ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugUnitCommands , " In [%s::%s Line: %d] peerUnitBuilder = %p, nextToRepaired = %d \n " , __FILE__ , __FUNCTION__ , __LINE__ , peerUnitBuilder , nextToRepaired ) ;
2010-09-02 07:38:49 +02:00
if ( nextToRepaired = = true ) {
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugUnitCommands ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugUnitCommands , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-09-02 07:38:49 +02:00
2011-01-20 08:56:02 +01:00
Command * peerCommand = peerUnitBuilder - > getCurrCommand ( ) ;
const RepairCommandType * rct = dynamic_cast < const RepairCommandType * > ( peerCommand - > getCommandType ( ) ) ;
// If the peer is also scheduled to do a repair we CANNOT swap their commands or
// it will result in a stack overflow as each swaps the others repair command.
// We must convert this unit's repair into a build right now!
if ( rct ! = NULL ) {
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugUnitCommands ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugUnitCommands , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2011-01-20 08:56:02 +01:00
const CommandType * ctbuild = unit - > getType ( ) - > getFirstCtOfClass ( ccBuild ) ;
NetworkCommand networkCommand ( this - > world , nctGiveCommand , unit - > getId ( ) , ctbuild - > getId ( ) , command - > getPos ( ) ,
command - > getUnitType ( ) - > getId ( ) , - 1 , CardinalDir : : NORTH , true , command - > getStateType ( ) ,
command - > getStateValue ( ) ) ;
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugUnitCommands ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugUnitCommands , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2011-01-20 08:56:02 +01:00
Command * command = this - > game - > getCommander ( ) - > buildCommand ( & networkCommand ) ;
CommandResult cr = unit - > checkCommand ( command ) ;
if ( cr = = crSuccess ) {
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugUnitCommands ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugUnitCommands , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2011-01-20 08:56:02 +01:00
unit - > replaceCurrCommand ( command ) ;
}
else {
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugUnitCommands ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugUnitCommands , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2011-01-20 08:56:02 +01:00
delete command ;
2011-03-18 04:53:06 +01:00
unit - > setCurrSkill ( scStop ) ;
unit - > finishCommand ( ) ;
2011-01-20 08:56:02 +01:00
}
}
else {
CommandStateType commandStateType = unit - > getCurrCommand ( ) - > getStateType ( ) ;
SwapActiveCommand ( unit , peerUnitBuilder ) ;
int oldPeerUnitId = peerUnitBuilder - > getId ( ) ;
int newPeerUnitId = unit - > getId ( ) ;
SwapActiveCommandState ( unit , commandStateType , unit - > getCurrCommand ( ) - > getCommandType ( ) , oldPeerUnitId , newPeerUnitId ) ;
// Give the swapped unit a fresh chance to help build in case they
// were or are about to be blocked
peerUnitBuilder - > getPath ( ) - > clear ( ) ;
peerUnitBuilder - > setRetryCurrCommandCount ( 1 ) ;
2011-03-18 04:53:06 +01:00
updateUnitCommand ( unit , - 1 ) ;
2011-01-20 08:56:02 +01:00
}
2010-09-02 07:38:49 +02:00
return ;
}
2010-08-28 03:46:26 +02:00
}
2010-12-05 00:19:43 +01:00
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugPerformance ) . enabled & & chrono . getMillis ( ) > 0 ) SystemFlags : : OutputDebug ( SystemFlags : : debugPerformance , " In [%s::%s] Line: %d took msecs: %lld \n " , __FILE__ , __FUNCTION__ , __LINE__ , chrono . getMillis ( ) ) ;
2010-08-28 03:46:26 +02:00
}
}
else {
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugUnitCommands ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugUnitCommands , " In [%s::%s Line: %d] unit to repair[%s] \n " , __FILE__ , __FUNCTION__ , __LINE__ , repaired - > getFullName ( ) . c_str ( ) ) ;
2010-08-28 03:46:26 +02:00
}
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugUnitCommands ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugUnitCommands , " In [%s::%s Line: %d] repaired = %p, nextToRepaired = %d, unit->getCurrSkill()->getClass() = %d \n " , __FILE__ , __FUNCTION__ , __LINE__ , repaired , nextToRepaired , unit - > getCurrSkill ( ) - > getClass ( ) ) ;
2010-08-28 03:46:26 +02:00
2011-09-01 03:11:23 +02:00
//UnitPathInterface *path= unit->getPath();
2010-07-21 20:21:40 +02:00
2010-08-28 03:46:26 +02:00
if ( unit - > getCurrSkill ( ) - > getClass ( ) ! = scRepair | |
( nextToRepaired = = false & & peerUnitBuilder = = NULL ) ) {
Vec2i repairPos = command - > getPos ( ) ;
bool startRepairing = ( repaired ! = NULL & & rct - > isRepairableUnitType ( repaired - > getType ( ) ) & & repaired - > isDamaged ( ) ) ;
2010-10-30 10:54:00 +02:00
2011-06-30 06:45:17 +02:00
if ( startRepairing = = true ) {
//printf("STARTING REPAIR, unit [%s - %d] for unit [%s - %d]\n",unit->getType()->getName().c_str(),unit->getId(),repaired->getType()->getName().c_str(),repaired->getId());
// for(unsigned int i = 0; i < rct->getRepairCount(); ++i) {
// const UnitType *rUnit = rct->getRepair(i);
// printf("Can repair unittype [%s]\n",rUnit->getName().c_str());
// }
}
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugUnitCommands ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugUnitCommands , " In [%s::%s Line: %d] repairPos = %s, startRepairing = %d \n " , __FILE__ , __FUNCTION__ , __LINE__ , repairPos . getString ( ) . c_str ( ) , startRepairing ) ;
2010-10-30 10:54:00 +02:00
2010-08-28 03:46:26 +02:00
if ( startRepairing = = false & & peerUnitBuilder ! = NULL ) {
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugUnitCommands ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugUnitCommands , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-08-28 03:46:26 +02:00
startRepairing = true ;
// Since the unit to be built is not yet existing we need to tell the
// other units to move to the build position or else they get in the way
2010-09-06 07:34:55 +02:00
// No need to adjust repair pos since we already did this above via: Vec2i buildPos = map->findBestBuildApproach(unit->getPos(), command->getPos(), peerUnitBuilder->getCurrCommand()->getUnitType());
//repairPos = command->getPos()-Vec2i(1);
2010-08-28 03:46:26 +02:00
}
2010-03-13 22:10:45 +01:00
//if not repairing
2010-08-28 03:46:26 +02:00
if ( startRepairing = = true ) {
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugUnitCommands ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugUnitCommands , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-08-28 03:46:26 +02:00
if ( nextToRepaired = = true ) {
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugUnitCommands ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugUnitCommands , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-03-13 22:10:45 +01:00
unit - > setTarget ( repaired ) ;
2011-03-18 04:53:06 +01:00
unit - > setCurrSkill ( rct - > getRepairSkillType ( ) ) ;
2010-03-13 22:10:45 +01:00
}
2010-07-21 20:21:40 +02:00
else {
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugUnitCommands ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugUnitCommands , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-08-28 03:46:26 +02:00
2011-04-03 04:42:45 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugWorldSynch ) . enabled = = true & & frameIndex < 0 ) {
2010-12-01 00:32:39 +01:00
char szBuf [ 4096 ] = " " ;
2010-12-02 00:38:03 +01:00
sprintf ( szBuf , " [updateRepair] unit->getPos() [%s] command->getPos()() [%s] repairPos [%s] " , unit - > getPos ( ) . getString ( ) . c_str ( ) , command - > getPos ( ) . getString ( ) . c_str ( ) , repairPos . getString ( ) . c_str ( ) ) ;
unit - > logSynchData ( __FILE__ , __LINE__ , szBuf ) ;
2010-12-01 00:32:39 +01:00
}
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugPerformance ) . enabled & & chrono . getMillis ( ) > 0 ) SystemFlags : : OutputDebug ( SystemFlags : : debugPerformance , " In [%s::%s] Line: %d took msecs: %lld \n " , __FILE__ , __FUNCTION__ , __LINE__ , chrono . getMillis ( ) ) ;
2010-12-05 00:19:43 +01:00
2011-10-28 07:22:41 +02:00
// If the repair command has no move skill and we are not next to
// the unit we cannot repair it
if ( rct - > getMoveSkillType ( ) = = NULL ) {
//printf("CANCEL REPAIR NOT NEXT TO REPAIR UNIT\n");
2010-08-28 03:46:26 +02:00
2011-10-28 07:22:41 +02:00
//Vec2i repairPos = command->getPos();
//bool startRepairing = (repaired != NULL && rct->isRepairableUnitType(repaired->getType()) && repaired->isDamaged());
//bool nextToRepaired = repaired != NULL && map->isNextTo(unit, repaired);
2010-08-28 03:46:26 +02:00
2011-10-28 07:22:41 +02:00
//printf("repairPos [%s] startRepairing = %d nextToRepaired = %d unit->getPos() [%s] repaired->getPos() [%s]\n",repairPos.getString().c_str(),startRepairing,nextToRepaired,unit->getPos().getString().c_str(),repaired->getPos().getString().c_str());
2010-07-21 20:21:40 +02:00
2011-10-28 07:22:41 +02:00
console - > addStdMessage ( " InvalidPosition " ) ;
unit - > setCurrSkill ( scStop ) ;
unit - > finishCommand ( ) ;
}
else {
TravelState ts ;
switch ( this - > game - > getGameSettings ( ) - > getPathFinderType ( ) ) {
case pfBasic :
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugUnitCommands ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugUnitCommands , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-10-30 10:54:00 +02:00
2011-10-28 07:22:41 +02:00
ts = pathFinder - > findPath ( unit , repairPos , NULL , frameIndex ) ;
break ;
case pfRoutePlanner :
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugUnitCommands ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugUnitCommands , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-12-05 00:19:43 +01:00
2011-10-28 07:22:41 +02:00
if ( repaired & & ! repaired - > getType ( ) - > isMobile ( ) ) {
ts = routePlanner - > findPathToBuildSite ( unit , repaired - > getType ( ) , repaired - > getPos ( ) , repaired - > getModelFacing ( ) ) ;
}
else {
ts = routePlanner - > findPath ( unit , repairPos ) ;
}
break ;
default :
throw runtime_error ( " detected unsupported pathfinder type! " ) ;
}
2010-08-28 03:46:26 +02:00
2011-10-28 07:22:41 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugUnitCommands ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugUnitCommands , " In [%s::%s Line: %d] ts = %d \n " , __FILE__ , __FUNCTION__ , __LINE__ , ts ) ;
2010-08-28 03:46:26 +02:00
2011-10-28 07:22:41 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugPerformance ) . enabled & & chrono . getMillis ( ) > 0 ) SystemFlags : : OutputDebug ( SystemFlags : : debugPerformance , " In [%s::%s] Line: %d took msecs: %lld \n " , __FILE__ , __FUNCTION__ , __LINE__ , chrono . getMillis ( ) ) ;
2010-08-28 03:46:26 +02:00
2011-10-28 07:22:41 +02:00
switch ( ts ) {
case tsMoving :
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugUnitCommands ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugUnitCommands , " In [%s::%s Line: %d] tsMoving \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
unit - > setCurrSkill ( rct - > getMoveSkillType ( ) ) ;
break ;
case tsBlocked :
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugUnitCommands ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugUnitCommands , " In [%s::%s Line: %d] tsBlocked \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
if ( unit - > getPath ( ) - > isBlocked ( ) ) {
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugUnitCommands ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugUnitCommands , " In [%s::%s Line: %d] about to call [scStop] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
if ( unit - > getRetryCurrCommandCount ( ) > 0 ) {
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugUnitCommands ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugUnitCommands , " In [%s::%s Line: %d] will retry command, unit->getRetryCurrCommandCount() = %d \n " , __FILE__ , __FUNCTION__ , __LINE__ , unit - > getRetryCurrCommandCount ( ) ) ;
unit - > setRetryCurrCommandCount ( 0 ) ;
unit - > getPath ( ) - > clear ( ) ;
updateUnitCommand ( unit , - 1 ) ;
}
else {
unit - > finishCommand ( ) ;
}
2010-08-28 03:46:26 +02:00
}
2011-10-28 07:22:41 +02:00
break ;
default :
break ;
2010-03-13 22:10:45 +01:00
}
}
}
}
2010-08-28 03:46:26 +02:00
else {
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugUnitCommands ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugUnitCommands , " In [%s::%s Line: %d] about to call [scStop] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-08-28 03:46:26 +02:00
2011-10-29 00:28:24 +02:00
//console->addStdMessage("InvalidPosition");
2011-03-18 04:53:06 +01:00
unit - > setCurrSkill ( scStop ) ;
unit - > finishCommand ( ) ;
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugPerformance ) . enabled & & chrono . getMillis ( ) > 0 ) SystemFlags : : OutputDebug ( SystemFlags : : debugPerformance , " In [%s::%s] Line: %d took msecs: %lld \n " , __FILE__ , __FUNCTION__ , __LINE__ , chrono . getMillis ( ) ) ;
2010-03-13 22:10:45 +01:00
}
}
2010-08-28 03:46:26 +02:00
else {
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugUnitCommands ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugUnitCommands , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-08-28 03:46:26 +02:00
2011-06-30 06:45:17 +02:00
bool cancelRepair = false ;
//if repairing
2010-08-28 03:46:26 +02:00
if ( repaired ! = NULL ) {
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugUnitCommands ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugUnitCommands , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-09-06 07:34:55 +02:00
2011-06-30 06:45:17 +02:00
// Check if we can still repair the unit (may have morphed, etc)
bool canStillRepair = rct - > isRepairableUnitType ( repaired - > getType ( ) ) ;
if ( canStillRepair = = true ) {
unit - > setTarget ( repaired ) ;
}
else {
//printf("CANCELLING CURRENT REPAIR, unit [%s - %d] for unit [%s - %d]\n",unit->getType()->getName().c_str(),unit->getId(),repaired->getType()->getName().c_str(),repaired->getId());
// for(unsigned int i = 0; i < rct->getRepairCount(); ++i) {
// const UnitType *rUnit = rct->getRepair(i);
// printf("Can repair unittype [%s]\n",rUnit->getName().c_str());
// }
unit - > setCurrSkill ( scStop ) ;
unit - > finishCommand ( ) ;
cancelRepair = true ;
}
2010-03-13 22:10:45 +01:00
}
2010-08-28 03:46:26 +02:00
else if ( peerUnitBuilder ! = NULL ) {
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugUnitCommands ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugUnitCommands , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-09-06 07:34:55 +02:00
2010-08-28 03:46:26 +02:00
unit - > setTargetPos ( command - > getPos ( ) ) ;
}
2011-06-30 06:45:17 +02:00
if ( cancelRepair = = false & & ( repaired = = NULL | | repaired - > repair ( ) ) & &
2010-08-28 03:46:26 +02:00
peerUnitBuilder = = NULL ) {
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugUnitCommands ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugUnitCommands , " In [%s::%s Line: %d] about to call [scStop] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-08-28 03:46:26 +02:00
2011-03-18 04:53:06 +01:00
unit - > setCurrSkill ( scStop ) ;
unit - > finishCommand ( ) ;
2010-08-28 03:46:26 +02:00
if ( repaired ! = NULL & & repaired - > isBuilt ( ) = = false ) {
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugUnitCommands ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugUnitCommands , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-08-28 03:46:26 +02:00
2010-03-13 22:10:45 +01:00
repaired - > born ( ) ;
scriptManager - > onUnitCreated ( repaired ) ;
}
2010-12-05 00:19:43 +01:00
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugPerformance ) . enabled & & chrono . getMillis ( ) > 0 ) SystemFlags : : OutputDebug ( SystemFlags : : debugPerformance , " In [%s::%s] Line: %d took msecs: %lld \n " , __FILE__ , __FUNCTION__ , __LINE__ , chrono . getMillis ( ) ) ;
2011-03-18 04:53:06 +01:00
}
2010-03-13 22:10:45 +01:00
}
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugPerformance ) . enabled & & chrono . getMillis ( ) > 0 ) SystemFlags : : OutputDebug ( SystemFlags : : debugPerformance , " In [%s::%s] Line: %d took msecs: %lld --------------------------- [END OF METHOD] --------------------------- \n " , __FILE__ , __FUNCTION__ , __LINE__ , chrono . getMillis ( ) ) ;
2010-03-13 22:10:45 +01:00
}
// ==================== updateProduce ====================
2011-03-18 04:53:06 +01:00
void UnitUpdater : : updateProduce ( Unit * unit , int frameIndex ) {
// Nothing to do
if ( frameIndex > = 0 ) {
clearUnitPrecache ( unit ) ;
return ;
}
2010-12-05 00:19:43 +01:00
Chrono chrono ;
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugPerformance ) . enabled ) chrono . start ( ) ;
2010-12-05 00:19:43 +01:00
2010-03-13 22:10:45 +01:00
Command * command = unit - > getCurrCommand ( ) ;
const ProduceCommandType * pct = static_cast < const ProduceCommandType * > ( command - > getCommandType ( ) ) ;
Unit * produced ;
2011-03-18 04:53:06 +01:00
if ( unit - > getCurrSkill ( ) - > getClass ( ) ! = scProduce ) {
2010-03-13 22:10:45 +01:00
//if not producing
unit - > setCurrSkill ( pct - > getProduceSkillType ( ) ) ;
}
2010-12-05 00:19:43 +01:00
else {
2010-03-13 22:10:45 +01:00
unit - > update2 ( ) ;
2010-12-05 00:19:43 +01:00
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugPerformance ) . enabled & & chrono . getMillis ( ) > 0 ) SystemFlags : : OutputDebug ( SystemFlags : : debugPerformance , " In [%s::%s] Line: %d took msecs: %lld \n " , __FILE__ , __FUNCTION__ , __LINE__ , chrono . getMillis ( ) ) ;
2010-12-05 00:19:43 +01:00
2010-03-13 22:10:45 +01:00
if ( unit - > getProgress2 ( ) > pct - > getProduced ( ) - > getProductionTime ( ) ) {
unit - > finishCommand ( ) ;
unit - > setCurrSkill ( scStop ) ;
2010-07-21 20:21:40 +02:00
UnitPathInterface * newpath = NULL ;
switch ( this - > game - > getGameSettings ( ) - > getPathFinderType ( ) ) {
case pfBasic :
newpath = new UnitPathBasic ( ) ;
break ;
case pfRoutePlanner :
newpath = new UnitPath ( ) ;
break ;
2010-07-21 22:40:11 +02:00
default :
throw runtime_error ( " detected unsupported pathfinder type! " ) ;
2010-07-21 20:21:40 +02:00
}
produced = new Unit ( world - > getNextUnitId ( unit - > getFaction ( ) ) , newpath , Vec2i ( 0 ) , pct - > getProducedUnit ( ) , unit - > getFaction ( ) , world - > getMap ( ) , CardinalDir : : NORTH ) ;
2010-03-13 22:10:45 +01:00
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugUnitCommands ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugUnitCommands , " In [%s::%s Line: %d] about to place unit for unit [%s] \n " , __FILE__ , __FUNCTION__ , __LINE__ , produced - > toString ( ) . c_str ( ) ) ;
2010-05-29 11:04:22 +02:00
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugPerformance ) . enabled & & chrono . getMillis ( ) > 0 ) SystemFlags : : OutputDebug ( SystemFlags : : debugPerformance , " In [%s::%s] Line: %d took msecs: %lld \n " , __FILE__ , __FUNCTION__ , __LINE__ , chrono . getMillis ( ) ) ;
2010-12-05 00:19:43 +01:00
2010-03-13 22:10:45 +01:00
//place unit creates the unit
2010-05-29 11:04:22 +02:00
if ( ! world - > placeUnit ( unit - > getCenteredPos ( ) , 10 , produced ) ) {
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugUnitCommands ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugUnitCommands , " In [%s::%s Line: %d] COULD NOT PLACE UNIT for unitID [%d] \n " , __FILE__ , __FUNCTION__ , __LINE__ , produced - > getId ( ) ) ;
2010-05-29 11:04:22 +02:00
2010-03-13 22:10:45 +01:00
delete produced ;
}
else {
produced - > create ( ) ;
produced - > born ( ) ;
world - > getStats ( ) - > produce ( unit - > getFactionIndex ( ) ) ;
const CommandType * ct = produced - > computeCommandType ( unit - > getMeetingPos ( ) ) ;
if ( ct ! = NULL ) {
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugUnitCommands ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugUnitCommands , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-03-13 22:10:45 +01:00
produced - > giveCommand ( new Command ( ct , unit - > getMeetingPos ( ) ) ) ;
}
scriptManager - > onUnitCreated ( produced ) ;
2010-12-05 00:19:43 +01:00
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugPerformance ) . enabled & & chrono . getMillis ( ) > 0 ) SystemFlags : : OutputDebug ( SystemFlags : : debugPerformance , " In [%s::%s] Line: %d took msecs: %lld \n " , __FILE__ , __FUNCTION__ , __LINE__ , chrono . getMillis ( ) ) ;
2010-03-13 22:10:45 +01:00
}
}
}
2010-12-05 00:19:43 +01:00
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugPerformance ) . enabled & & chrono . getMillis ( ) > 0 ) SystemFlags : : OutputDebug ( SystemFlags : : debugPerformance , " In [%s::%s] Line: %d took msecs: %lld --------------------------- [END OF METHOD] --------------------------- \n " , __FILE__ , __FUNCTION__ , __LINE__ , chrono . getMillis ( ) ) ;
2010-03-13 22:10:45 +01:00
}
// ==================== updateUpgrade ====================
2011-03-18 04:53:06 +01:00
void UnitUpdater : : updateUpgrade ( Unit * unit , int frameIndex ) {
// Nothing to do
if ( frameIndex > = 0 ) {
clearUnitPrecache ( unit ) ;
return ;
}
2010-12-05 00:19:43 +01:00
Chrono chrono ;
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugPerformance ) . enabled ) chrono . start ( ) ;
2010-12-05 00:19:43 +01:00
2010-03-13 22:10:45 +01:00
Command * command = unit - > getCurrCommand ( ) ;
const UpgradeCommandType * uct = static_cast < const UpgradeCommandType * > ( command - > getCommandType ( ) ) ;
if ( unit - > getCurrSkill ( ) - > getClass ( ) ! = scUpgrade ) {
//if not producing
unit - > setCurrSkill ( uct - > getUpgradeSkillType ( ) ) ;
}
else {
//if producing
unit - > update2 ( ) ;
if ( unit - > getProgress2 ( ) > uct - > getProduced ( ) - > getProductionTime ( ) ) {
unit - > finishCommand ( ) ;
unit - > setCurrSkill ( scStop ) ;
unit - > getFaction ( ) - > finishUpgrade ( uct - > getProducedUpgrade ( ) ) ;
}
}
2010-12-05 00:19:43 +01:00
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugPerformance ) . enabled & & chrono . getMillis ( ) > 0 ) SystemFlags : : OutputDebug ( SystemFlags : : debugPerformance , " In [%s::%s] Line: %d took msecs: %lld --------------------------- [END OF METHOD] --------------------------- \n " , __FILE__ , __FUNCTION__ , __LINE__ , chrono . getMillis ( ) ) ;
2010-03-13 22:10:45 +01:00
}
// ==================== updateMorph ====================
2011-03-18 04:53:06 +01:00
void UnitUpdater : : updateMorph ( Unit * unit , int frameIndex ) {
// Nothing to do
if ( frameIndex > = 0 ) {
clearUnitPrecache ( unit ) ;
return ;
}
2010-12-05 00:19:43 +01:00
Chrono chrono ;
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugPerformance ) . enabled ) chrono . start ( ) ;
2010-12-05 00:19:43 +01:00
Command * command = unit - > getCurrCommand ( ) ;
2010-03-13 22:10:45 +01:00
const MorphCommandType * mct = static_cast < const MorphCommandType * > ( command - > getCommandType ( ) ) ;
if ( unit - > getCurrSkill ( ) - > getClass ( ) ! = scMorph ) {
//if not morphing, check space
2011-09-07 01:12:05 +02:00
if ( map - > isFreeCellsOrHasUnit ( unit - > getPos ( ) , mct - > getMorphUnit ( ) - > getSize ( ) , mct - > getMorphUnit ( ) - > getField ( ) , unit , mct - > getMorphUnit ( ) ) ) {
2010-03-13 22:10:45 +01:00
unit - > setCurrSkill ( mct - > getMorphSkillType ( ) ) ;
}
else {
if ( unit - > getFactionIndex ( ) = = world - > getThisFactionIndex ( ) ) {
console - > addStdMessage ( " InvalidPosition " ) ;
}
unit - > cancelCommand ( ) ;
}
}
else {
unit - > update2 ( ) ;
if ( unit - > getProgress2 ( ) > mct - > getProduced ( ) - > getProductionTime ( ) ) {
2010-07-21 20:21:40 +02:00
int oldSize = 0 ;
bool needMapUpdate = false ;
switch ( this - > game - > getGameSettings ( ) - > getPathFinderType ( ) ) {
case pfBasic :
break ;
case pfRoutePlanner :
oldSize = unit - > getType ( ) - > getSize ( ) ;
needMapUpdate = unit - > getType ( ) - > isMobile ( ) ! = mct - > getMorphUnit ( ) - > isMobile ( ) ;
break ;
2010-07-21 22:40:11 +02:00
default :
throw runtime_error ( " detected unsupported pathfinder type! " ) ;
2010-07-21 20:21:40 +02:00
}
2010-07-13 07:33:43 +02:00
2010-03-13 22:10:45 +01:00
//finish the command
if ( unit - > morph ( mct ) ) {
unit - > finishCommand ( ) ;
if ( gui - > isSelected ( unit ) ) {
gui - > onSelectionChanged ( ) ;
}
2010-07-21 20:21:40 +02:00
switch ( this - > game - > getGameSettings ( ) - > getPathFinderType ( ) ) {
case pfBasic :
break ;
case pfRoutePlanner :
if ( needMapUpdate ) {
2011-01-15 01:29:25 +01:00
int size = max ( oldSize , unit - > getType ( ) - > getSize ( ) ) ;
2010-07-21 20:21:40 +02:00
world - > getCartographer ( ) - > updateMapMetrics ( unit - > getPos ( ) , size ) ;
}
break ;
2010-07-21 22:40:11 +02:00
default :
throw runtime_error ( " detected unsupported pathfinder type! " ) ;
2010-07-21 20:21:40 +02:00
}
2010-03-13 22:10:45 +01:00
scriptManager - > onUnitCreated ( unit ) ;
}
else {
unit - > cancelCommand ( ) ;
if ( unit - > getFactionIndex ( ) = = world - > getThisFactionIndex ( ) ) {
console - > addStdMessage ( " InvalidPosition " ) ;
}
}
unit - > setCurrSkill ( scStop ) ;
}
}
2010-12-05 00:19:43 +01:00
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugPerformance ) . enabled & & chrono . getMillis ( ) > 0 ) SystemFlags : : OutputDebug ( SystemFlags : : debugPerformance , " In [%s::%s] Line: %d took msecs: %lld --------------------------- [END OF METHOD] --------------------------- \n " , __FILE__ , __FUNCTION__ , __LINE__ , chrono . getMillis ( ) ) ;
2010-03-13 22:10:45 +01:00
}
2011-09-21 08:51:28 +02:00
// ==================== updateMove ====================
void UnitUpdater : : updateSwitchTeam ( Unit * unit , int frameIndex ) {
// Chrono chrono;
// if(SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled) chrono.start();
//
// Command *command= unit->getCurrCommand();
// const MoveCommandType *mct= static_cast<const MoveCommandType*>(command->getCommandType());
//
// Vec2i pos= command->getUnit()!=NULL? command->getUnit()->getCenteredPos(): command->getPos();
//
// if(SystemFlags::getSystemSettingType(SystemFlags::debugWorldSynch).enabled == true && frameIndex < 0) {
// char szBuf[4096]="";
// sprintf(szBuf,"[updateMove] pos [%s] unit [%d - %s] cmd [%s]",pos.getString().c_str(),unit->getId(),unit->getFullName().c_str(),command->toString().c_str());
// unit->logSynchData(__FILE__,__LINE__,szBuf);
// }
//
// if(SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s Line: %d] took msecs: %lld\n",__FILE__,__FUNCTION__,__LINE__,chrono.getMillis());
//
// TravelState tsValue = tsImpossible;
// switch(this->game->getGameSettings()->getPathFinderType()) {
// case pfBasic:
// tsValue = pathFinder->findPath(unit, pos, NULL, frameIndex);
// break;
// case pfRoutePlanner:
// tsValue = routePlanner->findPath(unit, pos);
// break;
// default:
// throw runtime_error("detected unsupported pathfinder type!");
// }
//
// if(SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s Line: %d] took msecs: %lld\n",__FILE__,__FUNCTION__,__LINE__,chrono.getMillis());
//
// if(frameIndex < 0) {
// switch (tsValue) {
// case tsMoving:
// unit->setCurrSkill(mct->getMoveSkillType());
// break;
//
// case tsBlocked:
// unit->setCurrSkill(scStop);
// if(unit->getPath()->isBlocked()){
// unit->finishCommand();
// }
// break;
//
// default:
// unit->finishCommand();
// }
// }
// if(SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s] Line: %d took msecs: %lld --------------------------- [END OF METHOD] ---------------------------\n",__FILE__,__FUNCTION__,__LINE__,chrono.getMillis());
}
// ==================== updateAttack ====================
2010-03-13 22:10:45 +01:00
// ==================== PRIVATE ====================
// ==================== attack ====================
void UnitUpdater : : hit ( Unit * attacker ) {
hit ( attacker , static_cast < const AttackSkillType * > ( attacker - > getCurrSkill ( ) ) , attacker - > getTargetPos ( ) , attacker - > getTargetField ( ) ) ;
}
void UnitUpdater : : hit ( Unit * attacker , const AttackSkillType * ast , const Vec2i & targetPos , Field targetField ) {
//hit attack positions
if ( ast - > getSplash ( ) ) {
PosCircularIterator pci ( map , targetPos , ast - > getSplashRadius ( ) ) ;
2011-11-16 22:38:12 +01:00
while ( pci . next ( ) ) {
2010-03-13 22:10:45 +01:00
Unit * attacked = map - > getCell ( pci . getPos ( ) ) - > getUnit ( targetField ) ;
2011-11-16 22:38:12 +01:00
if ( attacked ! = NULL ) {
2010-03-13 22:10:45 +01:00
if ( ast - > getSplashDamageAll ( )
2011-11-16 22:38:12 +01:00
| | attacker - > isAlly ( attacked ) = = false
| | ( targetPos . x = = pci . getPos ( ) . x & & targetPos . y = = pci . getPos ( ) . y ) ) {
attacker - > setLastAttackedUnitId ( attacked - > getId ( ) ) ;
scriptManager - > onUnitAttacking ( attacker ) ;
2010-03-13 22:10:45 +01:00
damage ( attacker , ast , attacked , pci . getPos ( ) . dist ( attacker - > getTargetPos ( ) ) ) ;
}
}
}
}
else {
Unit * attacked = map - > getCell ( targetPos ) - > getUnit ( targetField ) ;
if ( attacked ! = NULL ) {
damage ( attacker , ast , attacked , 0.f ) ;
}
}
}
2011-11-16 22:38:12 +01:00
void UnitUpdater : : damage ( Unit * attacker , const AttackSkillType * ast , Unit * attacked , float distance ) {
2011-02-11 08:39:58 +01:00
if ( attacker = = NULL ) {
throw runtime_error ( " attacker == NULL " ) ;
}
if ( ast = = NULL ) {
throw runtime_error ( " ast == NULL " ) ;
}
2010-03-13 22:10:45 +01:00
//get vars
2011-11-16 22:38:12 +01:00
float damage = ast - > getTotalAttackStrength ( attacker - > getTotalUpgrade ( ) ) ;
int var = ast - > getAttackVar ( ) ;
int armor = attacked - > getType ( ) - > getTotalArmor ( attacked - > getTotalUpgrade ( ) ) ;
float damageMultiplier = world - > getTechTree ( ) - > getDamageMultiplier ( ast - > getAttackType ( ) , attacked - > getType ( ) - > getArmorType ( ) ) ;
2010-03-13 22:10:45 +01:00
//compute damage
2011-11-16 22:38:12 +01:00
damage + = random . randRange ( - var , var ) ;
damage / = distance + 1 ;
damage - = armor ;
damage * = damageMultiplier ;
if ( damage < 1 ) {
2010-03-13 22:10:45 +01:00
damage = 1 ;
}
2011-11-16 22:38:12 +01:00
attacked - > setLastAttackerUnitId ( attacker - > getId ( ) ) ;
2010-03-13 22:10:45 +01:00
//damage the unit
2011-11-16 22:38:12 +01:00
if ( attacked - > decHp ( static_cast < int > ( damage ) ) ) {
2011-04-05 08:32:23 +02:00
world - > getStats ( ) - > kill ( attacker - > getFactionIndex ( ) , attacked - > getFactionIndex ( ) , attacker - > getTeam ( ) ! = attacked - > getTeam ( ) ) ;
2011-04-20 18:46:47 +02:00
attacker - > incKills ( attacked - > getTeam ( ) ) ;
2010-07-21 20:21:40 +02:00
switch ( this - > game - > getGameSettings ( ) - > getPathFinderType ( ) ) {
case pfBasic :
break ;
case pfRoutePlanner :
if ( ! attacked - > getType ( ) - > isMobile ( ) ) {
world - > getCartographer ( ) - > updateMapMetrics ( attacked - > getPos ( ) , attacked - > getType ( ) - > getSize ( ) ) ;
}
break ;
2010-07-21 22:40:11 +02:00
default :
throw runtime_error ( " detected unsupported pathfinder type! " ) ;
2010-07-21 20:21:40 +02:00
}
2011-11-18 16:43:05 +01:00
attacked - > setCauseOfDeath ( ucodAttacked ) ;
2010-03-13 22:10:45 +01:00
scriptManager - > onUnitDied ( attacked ) ;
}
2011-11-16 22:38:12 +01:00
if ( attacked - > isAlive ( ) = = true ) {
scriptManager - > onUnitAttacked ( attacked ) ;
}
2011-02-21 02:34:31 +01:00
// !!! Is this causing choppy network play somehow?
//attacker->computeHp();
2010-03-13 22:10:45 +01:00
}
void UnitUpdater : : startAttackParticleSystem ( Unit * unit ) {
Renderer & renderer = Renderer : : getInstance ( ) ;
ProjectileParticleSystem * psProj = 0 ;
SplashParticleSystem * psSplash ;
const AttackSkillType * ast = static_cast < const AttackSkillType * > ( unit - > getCurrSkill ( ) ) ;
ParticleSystemTypeProjectile * pstProj = ast - > getProjParticleType ( ) ;
ParticleSystemTypeSplash * pstSplash = ast - > getSplashParticleType ( ) ;
Vec3f startPos = unit - > getCurrVector ( ) ;
Vec3f endPos = unit - > getTargetVec ( ) ;
//make particle system
const SurfaceCell * sc = map - > getSurfaceCell ( Map : : toSurfCoords ( unit - > getPos ( ) ) ) ;
const SurfaceCell * tsc = map - > getSurfaceCell ( Map : : toSurfCoords ( unit - > getTargetPos ( ) ) ) ;
bool visible = sc - > isVisible ( world - > getThisTeamIndex ( ) ) | | tsc - > isVisible ( world - > getThisTeamIndex ( ) ) ;
2011-01-27 23:10:44 +01:00
if ( visible = = false & & world - > showWorldForPlayer ( world - > getThisFactionIndex ( ) ) = = true ) {
visible = true ;
}
2010-03-13 22:10:45 +01:00
//projectile
if ( pstProj ! = NULL ) {
psProj = pstProj - > create ( ) ;
psProj - > setPath ( startPos , endPos ) ;
psProj - > setObserver ( new ParticleDamager ( unit , this , gameCamera ) ) ;
psProj - > setVisible ( visible ) ;
2011-09-27 09:01:08 +02:00
if ( unit - > getFaction ( ) - > getTexture ( ) ) {
psProj - > setFactionColor ( unit - > getFaction ( ) - > getTexture ( ) - > getPixmapConst ( ) - > getPixel3f ( 0 , 0 ) ) ;
}
2010-03-13 22:10:45 +01:00
renderer . manageParticleSystem ( psProj , rsGame ) ;
}
else {
hit ( unit ) ;
}
//splash
if ( pstSplash ! = NULL ) {
psSplash = pstSplash - > create ( ) ;
psSplash - > setPos ( endPos ) ;
psSplash - > setVisible ( visible ) ;
2011-09-27 09:01:08 +02:00
if ( unit - > getFaction ( ) - > getTexture ( ) ) {
psSplash - > setFactionColor ( unit - > getFaction ( ) - > getTexture ( ) - > getPixmapConst ( ) - > getPixel3f ( 0 , 0 ) ) ;
}
2010-03-13 22:10:45 +01:00
renderer . manageParticleSystem ( psSplash , rsGame ) ;
if ( pstProj ! = NULL ) {
psProj - > link ( psSplash ) ;
}
}
}
// ==================== misc ====================
//looks for a resource of type rt, if rt==NULL looks for any
//resource the unit can harvest
2010-10-19 18:54:48 +02:00
bool UnitUpdater : : searchForResource ( Unit * unit , const HarvestCommandType * hct ) {
2010-03-13 22:10:45 +01:00
Vec2i pos = unit - > getCurrCommand ( ) - > getPos ( ) ;
2010-10-19 18:54:48 +02:00
for ( int radius = 0 ; radius < maxResSearchRadius ; radius + + ) {
for ( int i = pos . x - radius ; i < = pos . x + radius ; + + i ) {
for ( int j = pos . y - radius ; j < = pos . y + radius ; + + j ) {
if ( map - > isInside ( i , j ) ) {
2010-03-13 22:10:45 +01:00
Resource * r = map - > getSurfaceCell ( Map : : toSurfCoords ( Vec2i ( i , j ) ) ) - > getResource ( ) ;
2010-10-19 18:54:48 +02:00
if ( r ! = NULL ) {
if ( hct - > canHarvest ( r - > getType ( ) ) ) {
const Vec2i newPos = Vec2i ( i , j ) ;
if ( unit - > isBadHarvestPos ( newPos ) = = false ) {
unit - > getCurrCommand ( ) - > setPos ( newPos ) ;
2010-08-25 09:29:35 +02:00
2010-10-19 18:54:48 +02:00
return true ;
}
2010-03-13 22:10:45 +01:00
}
}
}
}
}
}
2010-08-25 09:29:35 +02:00
2010-03-13 22:10:45 +01:00
return false ;
}
bool UnitUpdater : : attackerOnSight ( const Unit * unit , Unit * * rangedPtr ) {
int range = unit - > getType ( ) - > getSight ( ) ;
return unitOnRange ( unit , range , rangedPtr , NULL ) ;
}
bool UnitUpdater : : attackableOnSight ( const Unit * unit , Unit * * rangedPtr , const AttackSkillType * ast ) {
int range = unit - > getType ( ) - > getSight ( ) ;
return unitOnRange ( unit , range , rangedPtr , ast ) ;
}
bool UnitUpdater : : attackableOnRange ( const Unit * unit , Unit * * rangedPtr , const AttackSkillType * ast ) {
int range = ast - > getTotalAttackRange ( unit - > getTotalUpgrade ( ) ) ;
return unitOnRange ( unit , range , rangedPtr , ast ) ;
}
2010-08-25 17:55:17 +02:00
bool UnitUpdater : : findCachedCellsEnemies ( Vec2i center , int range , int size , vector < Unit * > & enemies ,
2010-08-25 09:29:35 +02:00
const AttackSkillType * ast , const Unit * unit ,
const Unit * commandTarget ) {
bool result = false ;
2010-08-25 17:55:17 +02:00
//return result;
2010-08-25 09:29:35 +02:00
2011-12-03 02:19:54 +01:00
//if(game->isMasterserverMode() == false) {
{
2011-12-02 17:07:59 +01:00
MutexSafeWrapper safeMutex ( & mutexUnitRangeCellsLookupItemCache , string ( __FILE__ ) + " _ " + intToStr ( __LINE__ ) ) ;
std : : map < Vec2i , std : : map < int , std : : map < int , UnitRangeCellsLookupItem > > > : : iterator iterFind = UnitRangeCellsLookupItemCache . find ( center ) ;
2011-09-25 00:17:48 +02:00
2011-12-02 17:07:59 +01:00
if ( iterFind ! = UnitRangeCellsLookupItemCache . end ( ) ) {
std : : map < int , std : : map < int , UnitRangeCellsLookupItem > > : : iterator iterFind3 = iterFind - > second . find ( size ) ;
if ( iterFind3 ! = iterFind - > second . end ( ) ) {
std : : map < int , UnitRangeCellsLookupItem > : : iterator iterFind4 = iterFind3 - > second . find ( range ) ;
if ( iterFind4 ! = iterFind3 - > second . end ( ) ) {
result = true ;
2010-08-25 17:55:17 +02:00
2011-12-02 17:07:59 +01:00
std : : vector < Cell * > & cellList = iterFind4 - > second . rangeCellList ;
for ( int idx = 0 ; idx < cellList . size ( ) ; + + idx ) {
Cell * cell = cellList [ idx ] ;
2010-08-25 17:55:17 +02:00
2011-12-02 17:07:59 +01:00
findEnemiesForCell ( ast , cell , unit , commandTarget , enemies ) ;
}
2010-08-25 09:29:35 +02:00
}
}
}
2011-12-02 17:07:59 +01:00
safeMutex . ReleaseLock ( ) ;
2010-08-25 09:29:35 +02:00
}
return result ;
}
void UnitUpdater : : findEnemiesForCell ( const AttackSkillType * ast , Cell * cell , const Unit * unit ,
const Unit * commandTarget , vector < Unit * > & enemies ) {
//all fields
2010-11-09 10:06:52 +01:00
for ( int k = 0 ; k < fieldCount ; k + + ) {
2010-08-25 09:29:35 +02:00
Field f = static_cast < Field > ( k ) ;
//check field
if ( ( ast = = NULL | | ast - > getAttackField ( f ) ) ) {
Unit * possibleEnemy = cell - > getUnit ( f ) ;
//check enemy
if ( possibleEnemy ! = NULL & & possibleEnemy - > isAlive ( ) ) {
2010-11-09 10:06:52 +01:00
if ( ( unit - > isAlly ( possibleEnemy ) = = false & & commandTarget = = NULL ) | |
2010-08-25 09:29:35 +02:00
commandTarget = = possibleEnemy ) {
2010-11-09 10:06:52 +01:00
2010-08-25 09:29:35 +02:00
enemies . push_back ( possibleEnemy ) ;
}
}
2011-05-01 09:24:37 +02:00
}
}
}
void UnitUpdater : : findEnemiesForCell ( const Vec2i pos , int size , int sightRange , const Faction * faction , vector < Unit * > & enemies , bool attackersOnly ) const {
//all fields
for ( int k = 0 ; k < fieldCount ; k + + ) {
Field f = static_cast < Field > ( k ) ;
for ( int i = pos . x - sightRange ; i < pos . x + size + sightRange ; + + i ) {
for ( int j = pos . y - sightRange ; j < pos . y + size + sightRange ; + + j ) {
Vec2i testPos ( i , j ) ;
2011-05-01 17:22:58 +02:00
if ( map - > isInside ( testPos ) & &
map - > isInsideSurface ( map - > toSurfCoords ( testPos ) ) ) {
Cell * cell = map - > getCell ( testPos ) ;
//check field
Unit * possibleEnemy = cell - > getUnit ( f ) ;
//check enemy
if ( possibleEnemy ! = NULL & & possibleEnemy - > isAlive ( ) ) {
if ( faction - > getTeam ( ) ! = possibleEnemy - > getTeam ( ) ) {
if ( attackersOnly = = true ) {
if ( possibleEnemy - > getType ( ) - > hasCommandClass ( ccAttack ) | | possibleEnemy - > getType ( ) - > hasCommandClass ( ccAttackStopped ) ) {
enemies . push_back ( possibleEnemy ) ;
}
}
else {
2011-05-01 09:24:37 +02:00
enemies . push_back ( possibleEnemy ) ;
}
}
}
}
}
2010-08-25 09:29:35 +02:00
}
}
}
2010-03-13 22:10:45 +01:00
//if the unit has any enemy on range
2010-08-25 09:29:35 +02:00
bool UnitUpdater : : unitOnRange ( const Unit * unit , int range , Unit * * rangedPtr ,
2011-03-23 03:45:42 +01:00
const AttackSkillType * ast ) {
2010-03-13 22:10:45 +01:00
vector < Unit * > enemies ;
2010-12-18 21:17:41 +01:00
bool result = false ;
2010-03-13 22:10:45 +01:00
//we check command target
2010-08-25 09:29:35 +02:00
const Unit * commandTarget = NULL ;
if ( unit - > anyCommand ( ) ) {
commandTarget = static_cast < const Unit * > ( unit - > getCurrCommand ( ) - > getUnit ( ) ) ;
2010-03-13 22:10:45 +01:00
}
2010-08-25 09:29:35 +02:00
if ( commandTarget ! = NULL & & commandTarget - > isDead ( ) ) {
commandTarget = NULL ;
2010-03-13 22:10:45 +01:00
}
//aux vars
2010-08-25 09:29:35 +02:00
int size = unit - > getType ( ) - > getSize ( ) ;
Vec2i center = unit - > getPos ( ) ;
Vec2f floatCenter = unit - > getFloatCenteredPos ( ) ;
2010-03-13 22:10:45 +01:00
2011-09-01 03:11:23 +02:00
//bool foundInCache = true;
2010-12-01 00:32:39 +01:00
if ( findCachedCellsEnemies ( center , range , size , enemies , ast ,
unit , commandTarget ) = = false ) {
2011-09-01 03:11:23 +02:00
//foundInCache = false;
2010-08-25 09:29:35 +02:00
//nearby cells
2010-12-01 00:32:39 +01:00
UnitRangeCellsLookupItem cacheItem ;
2011-03-23 03:45:42 +01:00
for ( int i = center . x - range ; i < center . x + range + size ; + + i ) {
for ( int j = center . y - range ; j < center . y + range + size ; + + j ) {
2010-08-25 09:29:35 +02:00
//cells inside map and in range
2010-05-01 22:14:25 +02:00
# ifdef USE_STREFLOP
2010-08-25 09:29:35 +02:00
if ( map - > isInside ( i , j ) & & streflop : : floor ( floatCenter . dist ( Vec2f ( ( float ) i , ( float ) j ) ) ) < = ( range + 1 ) ) {
2010-05-01 22:14:25 +02:00
# else
2010-08-25 09:29:35 +02:00
if ( map - > isInside ( i , j ) & & floor ( floatCenter . dist ( Vec2f ( ( float ) i , ( float ) j ) ) ) < = ( range + 1 ) ) {
2010-05-01 22:14:25 +02:00
# endif
2010-08-25 09:29:35 +02:00
Cell * cell = map - > getCell ( i , j ) ;
findEnemiesForCell ( ast , cell , unit , commandTarget , enemies ) ;
2010-12-01 00:32:39 +01:00
cacheItem . rangeCellList . push_back ( cell ) ;
2010-03-13 22:10:45 +01:00
}
}
}
2010-08-25 09:29:35 +02:00
// Ok update our caches with the latest info
2011-12-02 17:07:59 +01:00
if ( cacheItem . rangeCellList . empty ( ) = = false ) {
2011-09-25 00:17:48 +02:00
MutexSafeWrapper safeMutex ( & mutexUnitRangeCellsLookupItemCache , string ( __FILE__ ) + " _ " + intToStr ( __LINE__ ) ) ;
2011-03-23 03:45:42 +01:00
//cacheItem.UnitRangeCellsLookupItemCacheTimerCountIndex = UnitRangeCellsLookupItemCacheTimerCount++;
2010-12-01 00:32:39 +01:00
UnitRangeCellsLookupItemCache [ center ] [ size ] [ range ] = cacheItem ;
}
}
2010-03-13 22:10:45 +01:00
//attack enemies that can attack first
2011-03-28 23:04:47 +02:00
float distToUnit = - 1 ;
2011-03-23 03:45:42 +01:00
Unit * enemySeen = NULL ;
2010-09-09 07:42:19 +02:00
for ( int i = 0 ; i < enemies . size ( ) ; + + i ) {
2011-03-28 23:04:47 +02:00
Unit * enemy = enemies [ i ] ;
if ( enemy - > isAlive ( ) = = true ) {
2011-03-23 03:45:42 +01:00
// Here we default to first enemy if no attackers found
if ( enemySeen = = NULL ) {
2011-03-28 23:04:47 +02:00
* rangedPtr = enemy ;
enemySeen = enemy ;
2011-03-23 03:45:42 +01:00
result = true ;
}
// Attackers get first priority
2011-03-28 23:04:47 +02:00
if ( enemy - > getType ( ) - > hasSkillClass ( scAttack ) = = true ) {
// Select closest attacking unit
if ( distToUnit < 0 | | unit - > getCenteredPos ( ) . dist ( enemy - > getCenteredPos ( ) ) < distToUnit ) {
distToUnit = unit - > getCenteredPos ( ) . dist ( enemy - > getCenteredPos ( ) ) ;
* rangedPtr = enemies [ i ] ;
enemySeen = enemies [ i ] ;
result = true ;
}
//break;
2011-03-23 03:45:42 +01:00
}
}
}
/*
if ( enemies [ i ] - > getType ( ) - > hasSkillClass ( scAttack ) & &
enemies [ i ] - > isAlive ( ) = = true ) {
2010-03-13 22:10:45 +01:00
* rangedPtr = enemies [ i ] ;
2010-12-19 23:33:08 +01:00
enemySeen = enemies [ i ] ;
2010-12-18 21:17:41 +01:00
result = true ;
break ;
2010-03-13 22:10:45 +01:00
}
}
2011-03-23 03:45:42 +01:00
/*
if ( result = = false ) {
2011-02-13 17:46:32 +01:00
//any enemy
2011-03-23 03:45:42 +01:00
for ( int i = 0 ; i < enemies . size ( ) ; + + i ) {
if ( enemies [ i ] - > isAlive ( ) = = true ) {
2011-02-13 17:46:32 +01:00
* rangedPtr = enemies [ i ] ;
enemySeen = enemies [ i ] ;
result = true ;
break ;
}
}
}
2011-03-23 03:45:42 +01:00
*/
2010-03-13 22:10:45 +01:00
2011-03-23 03:45:42 +01:00
if ( result = = true ) {
2011-09-01 03:11:23 +02:00
//const Unit* teamUnit = NULL;
2011-03-23 03:45:42 +01:00
const Unit * enemyUnit = NULL ;
bool onlyEnemyUnits = true ;
2010-12-26 05:38:03 +01:00
2011-03-23 03:45:42 +01:00
if ( unit - > getTeam ( ) = = world - > getThisTeamIndex ( ) ) {
2011-09-01 03:11:23 +02:00
//teamUnit = unit;
2011-03-23 03:45:42 +01:00
enemyUnit = enemySeen ;
onlyEnemyUnits = false ;
2010-12-29 19:23:54 +01:00
}
2011-03-23 03:45:42 +01:00
else if ( enemySeen - > getTeam ( ) = = world - > getThisTeamIndex ( ) ) {
2011-09-01 03:11:23 +02:00
//teamUnit = enemySeen;
2011-03-23 03:45:42 +01:00
enemyUnit = unit ;
onlyEnemyUnits = false ;
2010-12-29 19:23:54 +01:00
}
2011-03-23 03:45:42 +01:00
if ( onlyEnemyUnits = = false & &
enemyUnit - > getTeam ( ) ! = world - > getThisTeamIndex ( ) ) {
2010-12-29 19:23:54 +01:00
Vec2f enemyFloatCenter = enemyUnit - > getFloatCenteredPos ( ) ;
// find nearest Attack and cleanup old dates
2011-03-23 03:45:42 +01:00
AttackWarningData * nearest = NULL ;
float currentDistance = 0.f ;
float nearestDistance = 0.f ;
for ( int i = attackWarnings . size ( ) - 1 ; i > = 0 ; - - i ) {
if ( world - > getFrameCount ( ) - attackWarnings [ i ] - > lastFrameCount > 200 ) { //after 200 frames attack break we warn again
AttackWarningData * toDelete = attackWarnings [ i ] ;
2010-12-29 19:23:54 +01:00
attackWarnings . erase ( attackWarnings . begin ( ) + i ) ;
delete toDelete ; // old one
2010-12-21 16:55:37 +01:00
}
else {
2011-02-16 00:53:48 +01:00
# ifdef USE_STREFLOP
currentDistance = streflop : : floor ( enemyFloatCenter . dist ( attackWarnings [ i ] - > attackPosition ) ) ; // no need for streflops here!
# else
currentDistance = floor ( enemyFloatCenter . dist ( attackWarnings [ i ] - > attackPosition ) ) ; // no need for streflops here!
# endif
2011-03-23 03:45:42 +01:00
if ( nearest = = NULL ) {
nearest = attackWarnings [ i ] ;
nearestDistance = currentDistance ;
2010-12-29 19:23:54 +01:00
}
else {
2011-03-23 03:45:42 +01:00
if ( currentDistance < nearestDistance ) {
nearest = attackWarnings [ i ] ;
nearestDistance = currentDistance ;
2010-12-21 16:55:37 +01:00
}
2011-03-23 03:45:42 +01:00
}
2010-12-29 19:23:54 +01:00
}
}
2011-03-23 03:45:42 +01:00
if ( nearest ! = NULL ) {
// does it fit?
if ( nearestDistance < attackWarnRange ) {
// update entry with current values
nearest - > lastFrameCount = world - > getFrameCount ( ) ;
nearest - > attackPosition . x = enemyFloatCenter . x ;
nearest - > attackPosition . y = enemyFloatCenter . y ;
2010-12-29 19:23:54 +01:00
}
2011-03-23 03:45:42 +01:00
else {
//Must be a different Attack!
2010-12-29 19:23:54 +01:00
nearest = NULL ; //set to null to force a new entry in next step
}
}
// add new attack
2011-03-23 03:45:42 +01:00
if ( nearest = = NULL ) {
// no else!
2010-12-29 19:23:54 +01:00
AttackWarningData * awd = new AttackWarningData ( ) ;
awd - > lastFrameCount = world - > getFrameCount ( ) ;
awd - > attackPosition . x = enemyFloatCenter . x ;
awd - > attackPosition . y = enemyFloatCenter . y ;
attackWarnings . push_back ( awd ) ;
SoundRenderer : : getInstance ( ) . playFx ( CoreData : : getInstance ( ) . getAttentionSound ( ) ) ;
world - > addAttackEffects ( enemyUnit ) ;
}
}
2010-12-18 21:17:41 +01:00
}
return result ;
2010-03-13 22:10:45 +01:00
}
2011-03-28 23:04:47 +02:00
//if the unit has any enemy on range
vector < Unit * > UnitUpdater : : enemyUnitsOnRange ( const Unit * unit , const AttackSkillType * ast ) {
int range = unit - > getType ( ) - > getSight ( ) ;
if ( ast ! = NULL ) {
range = ast - > getTotalAttackRange ( unit - > getTotalUpgrade ( ) ) ;
}
vector < Unit * > enemies ;
//we check command target
const Unit * commandTarget = NULL ;
// if(unit->anyCommand()) {
// commandTarget = static_cast<const Unit*>(unit->getCurrCommand()->getUnit());
// }
// if(commandTarget != NULL && commandTarget->isDead()) {
// commandTarget = NULL;
// }
//aux vars
int size = unit - > getType ( ) - > getSize ( ) ;
Vec2i center = unit - > getPos ( ) ;
Vec2f floatCenter = unit - > getFloatCenteredPos ( ) ;
2011-09-01 03:11:23 +02:00
//bool foundInCache = true;
2011-03-28 23:04:47 +02:00
if ( findCachedCellsEnemies ( center , range , size , enemies , ast ,
unit , commandTarget ) = = false ) {
2011-09-01 03:11:23 +02:00
//foundInCache = false;
2011-03-28 23:04:47 +02:00
//nearby cells
UnitRangeCellsLookupItem cacheItem ;
for ( int i = center . x - range ; i < center . x + range + size ; + + i ) {
for ( int j = center . y - range ; j < center . y + range + size ; + + j ) {
//cells inside map and in range
# ifdef USE_STREFLOP
if ( map - > isInside ( i , j ) & & streflop : : floor ( floatCenter . dist ( Vec2f ( ( float ) i , ( float ) j ) ) ) < = ( range + 1 ) ) {
# else
if ( map - > isInside ( i , j ) & & floor ( floatCenter . dist ( Vec2f ( ( float ) i , ( float ) j ) ) ) < = ( range + 1 ) ) {
# endif
Cell * cell = map - > getCell ( i , j ) ;
findEnemiesForCell ( ast , cell , unit , commandTarget , enemies ) ;
cacheItem . rangeCellList . push_back ( cell ) ;
}
}
}
// Ok update our caches with the latest info
2011-12-02 17:07:59 +01:00
if ( cacheItem . rangeCellList . empty ( ) = = false ) {
2011-09-25 00:17:48 +02:00
MutexSafeWrapper safeMutex ( & mutexUnitRangeCellsLookupItemCache , string ( __FILE__ ) + " _ " + intToStr ( __LINE__ ) ) ;
2011-03-28 23:04:47 +02:00
//cacheItem.UnitRangeCellsLookupItemCacheTimerCountIndex = UnitRangeCellsLookupItemCacheTimerCount++;
UnitRangeCellsLookupItemCache [ center ] [ size ] [ range ] = cacheItem ;
}
}
return enemies ;
}
2011-06-25 10:53:53 +02:00
void UnitUpdater : : findUnitsForCell ( Cell * cell , const Unit * unit , vector < Unit * > & units ) {
//all fields
for ( int k = 0 ; k < fieldCount ; k + + ) {
Field f = static_cast < Field > ( k ) ;
//check field
Unit * cellUnit = cell - > getUnit ( f ) ;
if ( cellUnit ! = NULL & & cellUnit - > isAlive ( ) ) {
units . push_back ( cellUnit ) ;
}
}
}
vector < Unit * > UnitUpdater : : findUnitsInRange ( const Unit * unit , int radius ) {
int range = radius ;
vector < Unit * > units ;
//aux vars
int size = unit - > getType ( ) - > getSize ( ) ;
Vec2i center = unit - > getPos ( ) ;
Vec2f floatCenter = unit - > getFloatCenteredPos ( ) ;
//nearby cells
UnitRangeCellsLookupItem cacheItem ;
for ( int i = center . x - range ; i < center . x + range + size ; + + i ) {
for ( int j = center . y - range ; j < center . y + range + size ; + + j ) {
//cells inside map and in range
# ifdef USE_STREFLOP
if ( map - > isInside ( i , j ) & & streflop : : floor ( floatCenter . dist ( Vec2f ( ( float ) i , ( float ) j ) ) ) < = ( range + 1 ) ) {
# else
if ( map - > isInside ( i , j ) & & floor ( floatCenter . dist ( Vec2f ( ( float ) i , ( float ) j ) ) ) < = ( range + 1 ) ) {
# endif
Cell * cell = map - > getCell ( i , j ) ;
findUnitsForCell ( cell , unit , units ) ;
}
}
}
return units ;
}
2011-12-02 17:07:59 +01:00
string UnitUpdater : : getUnitRangeCellsLookupItemCacheStats ( ) {
string result = " " ;
int posCount = 0 ;
int sizeCount = 0 ;
int rangeCount = 0 ;
int rangeCountCellCount = 0 ;
Mutex mutexUnitRangeCellsLookupItemCache ;
//std::map<Vec2i, std::map<int, std::map<int, UnitRangeCellsLookupItem > > > UnitRangeCellsLookupItemCache;
for ( std : : map < Vec2i , std : : map < int , std : : map < int , UnitRangeCellsLookupItem > > > : : iterator iterMap1 = UnitRangeCellsLookupItemCache . begin ( ) ;
iterMap1 ! = UnitRangeCellsLookupItemCache . end ( ) ; + + iterMap1 ) {
posCount + + ;
for ( std : : map < int , std : : map < int , UnitRangeCellsLookupItem > > : : iterator iterMap2 = iterMap1 - > second . begin ( ) ;
iterMap2 ! = iterMap1 - > second . end ( ) ; + + iterMap2 ) {
sizeCount + + ;
for ( std : : map < int , UnitRangeCellsLookupItem > : : iterator iterMap3 = iterMap2 - > second . begin ( ) ;
iterMap3 ! = iterMap2 - > second . end ( ) ; + + iterMap3 ) {
rangeCount + + ;
rangeCountCellCount + = iterMap3 - > second . rangeCellList . size ( ) ;
}
}
}
uint64 totalBytes = rangeCountCellCount * sizeof ( Cell * ) ;
totalBytes / = 1000 ;
char szBuf [ 1024 ] = " " ;
sprintf ( szBuf , " pos [%d] size [%d] range [%d][%d] total KB: %s " , posCount , sizeCount , rangeCount , rangeCountCellCount , formatNumber ( totalBytes ) . c_str ( ) ) ;
result = szBuf ;
return result ;
}
2010-03-13 22:10:45 +01:00
// =====================================================
// class ParticleDamager
// =====================================================
ParticleDamager : : ParticleDamager ( Unit * attacker , UnitUpdater * unitUpdater , const GameCamera * gameCamera ) {
this - > gameCamera = gameCamera ;
this - > attackerRef = attacker ;
this - > ast = static_cast < const AttackSkillType * > ( attacker - > getCurrSkill ( ) ) ;
this - > targetPos = attacker - > getTargetPos ( ) ;
this - > targetField = attacker - > getTargetField ( ) ;
this - > unitUpdater = unitUpdater ;
}
2011-03-18 04:53:06 +01:00
void ParticleDamager : : update ( ParticleSystem * particleSystem ) {
2010-03-13 22:10:45 +01:00
Unit * attacker = attackerRef . getUnit ( ) ;
2011-03-18 04:53:06 +01:00
if ( attacker ! = NULL ) {
2010-03-13 22:10:45 +01:00
unitUpdater - > hit ( attacker , ast , targetPos , targetField ) ;
//play sound
StaticSound * projSound = ast - > getProjSound ( ) ;
2011-03-18 22:23:34 +01:00
if ( particleSystem - > getVisible ( ) & & projSound ! = NULL ) {
2010-03-13 22:10:45 +01:00
SoundRenderer : : getInstance ( ) . playFx ( projSound , attacker - > getCurrVector ( ) , gameCamera - > getPos ( ) ) ;
}
}
particleSystem - > setObserver ( NULL ) ;
delete this ;
}
} } //end namespace