2010-03-18 21:26:40 +00:00
// ==============================================================
// This file is part of Glest (www.glest.org)
//
2010-04-18 06:01:20 +00:00
// Copyright (C) 2001-2008 Martio Figueroa
2010-03-18 21:26:40 +00: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 "commander.h"
# include "world.h"
# include "unit.h"
# include "conversion.h"
# include "upgrade.h"
# include "command.h"
# include "command_type.h"
# include "network_manager.h"
# include "console.h"
# include "config.h"
# include "platform_util.h"
2010-06-05 07:52:14 +00:00
# include "game.h"
# include "game_settings.h"
2010-03-18 21:26:40 +00:00
# include "game.h"
using namespace Shared : : Graphics ;
using namespace Shared : : Util ;
using namespace Shared : : Platform ;
namespace Glest { namespace Game {
// =====================================================
// class Commander
// =====================================================
// ===================== PUBLIC ========================
2011-01-11 08:45:58 +00:00
CommanderNetworkThread : : CommanderNetworkThread ( ) : BaseThread ( ) {
this - > idStatus = make_pair < int , bool > ( - 1 , false ) ;
this - > commanderInterface = NULL ;
}
CommanderNetworkThread : : CommanderNetworkThread ( CommanderNetworkCallbackInterface * commanderInterface ) : BaseThread ( ) {
this - > idStatus = make_pair < int , bool > ( - 1 , false ) ;
this - > commanderInterface = commanderInterface ;
}
void CommanderNetworkThread : : setQuitStatus ( bool value ) {
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s] Line: %d value = %d \n " , __FILE__ , __FUNCTION__ , __LINE__ , value ) ;
BaseThread : : setQuitStatus ( value ) ;
if ( value = = true ) {
signalUpdate ( - 1 ) ;
}
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s] Line: %d \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
}
void CommanderNetworkThread : : signalUpdate ( int id ) {
//SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] event = %p\n",__FILE__,__FUNCTION__,__LINE__,event);
MutexSafeWrapper safeMutex ( & idMutex ) ;
this - > idStatus . first = id ;
this - > idStatus . second = false ;
safeMutex . ReleaseLock ( ) ;
//SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] Line: %d\n",__FILE__,__FUNCTION__,__LINE__);
semTaskSignalled . signal ( ) ;
}
void CommanderNetworkThread : : setTaskCompleted ( int id ) {
//SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] Line: %d\n",__FILE__,__FUNCTION__,__LINE__);
MutexSafeWrapper safeMutex ( & idMutex ) ;
this - > idStatus . second = true ;
safeMutex . ReleaseLock ( ) ;
//SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] Line: %d\n",__FILE__,__FUNCTION__,__LINE__);
}
bool CommanderNetworkThread : : isSignalCompleted ( int id ) {
//SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] slotIndex = %d\n",__FILE__,__FUNCTION__,__LINE__,slotIndex);
MutexSafeWrapper safeMutex ( & idMutex ) ;
bool result = this - > idStatus . second ;
safeMutex . ReleaseLock ( ) ;
//if(result == false) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] slotIndex = %d, result = %d\n",__FILE__,__FUNCTION__,__LINE__,slotIndex,result);
return result ;
}
void CommanderNetworkThread : : execute ( ) {
RunningStatusSafeWrapper runningStatus ( this ) ;
try {
//setRunningStatus(true);
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
unsigned int idx = 0 ;
for ( ; this - > commanderInterface ! = NULL ; ) {
if ( getQuitStatus ( ) = = true ) {
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
break ;
}
//SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
semTaskSignalled . waitTillSignalled ( ) ;
//SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] Line: %d\n",__FILE__,__FUNCTION__,__LINE__);
if ( getQuitStatus ( ) = = true ) {
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
break ;
}
//SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
MutexSafeWrapper safeMutex ( & idMutex ) ;
if ( idStatus . first > 0 ) {
int updateId = this - > idStatus . first ;
safeMutex . ReleaseLock ( ) ;
this - > commanderInterface - > commanderNetworkUpdateTask ( updateId ) ;
setTaskCompleted ( updateId ) ;
}
else {
safeMutex . ReleaseLock ( ) ;
}
//SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] Line: %d\n",__FILE__,__FUNCTION__,__LINE__);
if ( getQuitStatus ( ) = = true ) {
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
break ;
}
//SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] Line: %d\n",__FILE__,__FUNCTION__,__LINE__);
}
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
}
catch ( const exception & ex ) {
//setRunningStatus(false);
SystemFlags : : OutputDebug ( SystemFlags : : debugError , " In [%s::%s Line: %d] Error [%s] \n " , __FILE__ , __FUNCTION__ , __LINE__ , ex . what ( ) ) ;
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
throw runtime_error ( ex . what ( ) ) ;
}
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s] Line: %d \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
//setRunningStatus(false);
}
// =====================================================
// class
// =====================================================
Commander : : Commander ( ) {
this - > networkThread = new CommanderNetworkThread ( this ) ;
this - > networkThread - > setUniqueID ( __FILE__ ) ;
this - > networkThread - > start ( ) ;
}
Commander : : ~ Commander ( ) {
if ( BaseThread : : shutdownAndWait ( networkThread ) = = true ) {
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
delete networkThread ;
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
}
networkThread = NULL ;
}
2010-03-18 21:26:40 +00:00
void Commander : : init ( World * world ) {
this - > world = world ;
}
2010-08-28 01:46:26 +00:00
CommandResult Commander : : tryGiveCommand ( const Selection * selection , const CommandType * commandType ,
const Vec2i & pos , const UnitType * unitType ,
CardinalDir facing , bool tryQueue , Unit * targetUnit ) const {
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
if ( ! selection - > isEmpty ( ) & & commandType ! = NULL ) {
Vec2i refPos ;
CommandResultContainer results ;
2010-08-31 23:14:15 +00:00
refPos = world - > getMap ( ) - > computeRefPos ( selection ) ;
2010-08-28 01:46:26 +00:00
2010-08-31 23:14:15 +00:00
const Unit * builderUnit = world - > getMap ( ) - > findClosestUnitToPos ( selection , pos , unitType ) ;
//Vec2i = world->getMap()->computeDestPos(refPos, builderUnit->getPos(), pos);
//std::pair<float,Vec2i> distance = world->getMap()->getUnitDistanceToPos(builderUnit,pos,unitType);
//builderUnit->setCurrentUnitTitle("Distance: " + floatToStr(distance.first) + " pos: " + distance.second.getString());
int builderUnitId = builderUnit - > getId ( ) ;
2010-08-30 20:45:12 +00:00
CommandStateType commandStateType = cst_None ;
int commandStateValue = - 1 ;
2010-08-31 23:14:15 +00:00
//bool unitSignalledToBuild = false;
2010-08-28 01:46:26 +00:00
//give orders to all selected units
2010-08-31 23:14:15 +00:00
for ( int i = 0 ; i < selection - > getCount ( ) ; + + i ) {
2010-08-28 01:46:26 +00:00
const Unit * unit = selection - > getUnit ( i ) ;
int unitId = unit - > getId ( ) ;
2010-08-31 23:14:15 +00:00
Vec2i currPos = world - > getMap ( ) - > computeDestPos ( refPos , unit - > getPos ( ) , pos ) ;
2010-08-28 01:46:26 +00:00
Vec2i usePos = currPos ;
const CommandType * useCommandtype = commandType ;
if ( dynamic_cast < const BuildCommandType * > ( commandType ) ! = NULL ) {
usePos = pos ;
2010-08-31 23:14:15 +00:00
//if(unitSignalledToBuild == false) {
//if(builderUnit->getId() == unitId)
// builderUnitId = unitId;
//unitSignalledToBuild = true;
//}
//else {
if ( builderUnit - > getId ( ) ! = unitId ) {
2010-08-30 20:45:12 +00:00
useCommandtype = unit - > getType ( ) - > getFirstRepairCommand ( unitType ) ;
commandStateType = cst_linkedUnit ;
commandStateValue = builderUnitId ;
//tryQueue = true;
2010-08-28 01:46:26 +00:00
}
2010-08-31 23:14:15 +00:00
else {
commandStateType = cst_None ;
commandStateValue = - 1 ;
}
2010-08-28 01:46:26 +00:00
}
2010-09-01 04:19:20 +00:00
if ( useCommandtype ! = NULL ) {
NetworkCommand networkCommand ( this - > world , nctGiveCommand , unitId ,
useCommandtype - > getId ( ) , usePos , unitType - > getId ( ) ,
( targetUnit ! = NULL ? targetUnit - > getId ( ) : - 1 ) ,
facing , tryQueue , commandStateType , commandStateValue ) ;
//every unit is ordered to a the position
CommandResult result = pushNetworkCommand ( & networkCommand ) ;
results . push_back ( result ) ;
}
2010-08-28 01:46:26 +00:00
}
return computeResult ( results ) ;
}
else {
return crFailUndefined ;
}
}
CommandResult Commander : : tryGiveCommand ( const Unit * unit , const CommandType * commandType ,
const Vec2i & pos , const UnitType * unitType ,
CardinalDir facing , bool tryQueue , Unit * targetUnit ) const {
2010-06-01 16:54:44 +00:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-11-11 08:02:50 +00:00
Chrono chrono ;
chrono . start ( ) ;
2010-05-29 10:34:56 +00:00
assert ( this - > world ! = NULL ) ;
assert ( unit ! = NULL ) ;
assert ( commandType ! = NULL ) ;
assert ( unitType ! = NULL ) ;
2010-06-01 16:54:44 +00:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-11-11 08:02:50 +00:00
if ( chrono . getMillis ( ) > 0 ) SystemFlags : : OutputDebug ( SystemFlags : : debugPerformance , " In [%s::%s] Line: %d took msecs: %lld \n " , __FILE__ , __FUNCTION__ , __LINE__ , chrono . getMillis ( ) ) ;
2010-08-28 01:46:26 +00:00
NetworkCommand networkCommand ( this - > world , nctGiveCommand , unit - > getId ( ) ,
commandType - > getId ( ) , pos , unitType - > getId ( ) ,
( targetUnit ! = NULL ? targetUnit - > getId ( ) : - 1 ) ,
facing , tryQueue ) ;
2010-06-01 16:54:44 +00:00
2010-11-11 08:02:50 +00:00
if ( chrono . getMillis ( ) > 0 ) SystemFlags : : OutputDebug ( SystemFlags : : debugPerformance , " In [%s::%s] Line: %d took msecs: %lld \n " , __FILE__ , __FUNCTION__ , __LINE__ , chrono . getMillis ( ) ) ;
2010-06-01 16:54:44 +00:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-11-11 08:02:50 +00:00
CommandResult result = pushNetworkCommand ( & networkCommand ) ;
if ( chrono . getMillis ( ) > 0 ) SystemFlags : : OutputDebug ( SystemFlags : : debugPerformance , " In [%s::%s] Line: %d took msecs: %lld \n " , __FILE__ , __FUNCTION__ , __LINE__ , chrono . getMillis ( ) ) ;
return result ;
2010-03-18 21:26:40 +00:00
}
2010-04-18 07:35:48 +00:00
CommandResult Commander : : tryGiveCommand ( const Selection * selection , CommandClass commandClass , const Vec2i & pos , const Unit * targetUnit , bool tryQueue ) const {
2010-12-28 02:57:36 +00:00
2011-01-08 21:53:05 +00:00
if ( selection - > isEmpty ( ) = = false ) {
2010-03-18 21:26:40 +00:00
Vec2i refPos , currPos ;
CommandResultContainer results ;
2010-08-31 23:14:15 +00:00
refPos = world - > getMap ( ) - > computeRefPos ( selection ) ;
2010-03-18 21:26:40 +00:00
//give orders to all selected units
2011-01-08 21:53:05 +00:00
for ( int i = 0 ; i < selection - > getCount ( ) ; + + i ) {
2010-03-18 21:26:40 +00:00
const Unit * unit = selection - > getUnit ( i ) ;
const CommandType * ct = unit - > getType ( ) - > getFirstCtOfClass ( commandClass ) ;
2011-01-08 21:53:05 +00:00
if ( ct ! = NULL ) {
2010-03-18 21:26:40 +00:00
int targetId = targetUnit = = NULL ? Unit : : invalidId : targetUnit - > getId ( ) ;
int unitId = selection - > getUnit ( i ) - > getId ( ) ;
2010-08-31 23:14:15 +00:00
Vec2i currPos = world - > getMap ( ) - > computeDestPos ( refPos , selection - > getUnit ( i ) - > getPos ( ) , pos ) ;
2010-04-18 07:35:48 +00:00
NetworkCommand networkCommand ( this - > world , nctGiveCommand , unitId , ct - > getId ( ) , currPos , - 1 , targetId , - 1 , tryQueue ) ;
2010-03-18 21:26:40 +00:00
//every unit is ordered to a different pos
CommandResult result = pushNetworkCommand ( & networkCommand ) ;
results . push_back ( result ) ;
}
else {
results . push_back ( crFailUndefined ) ;
}
}
return computeResult ( results ) ;
}
else {
return crFailUndefined ;
}
}
2010-08-28 01:46:26 +00:00
CommandResult Commander : : tryGiveCommand ( const Selection * selection ,
const CommandType * commandType , const Vec2i & pos ,
const Unit * targetUnit , bool tryQueue ) const {
2010-03-18 21:26:40 +00:00
if ( ! selection - > isEmpty ( ) & & commandType ! = NULL ) {
Vec2i refPos ;
CommandResultContainer results ;
2010-08-31 23:14:15 +00:00
refPos = world - > getMap ( ) - > computeRefPos ( selection ) ;
2010-03-18 21:26:40 +00:00
//give orders to all selected units
for ( int i = 0 ; i < selection - > getCount ( ) ; + + i ) {
int targetId = targetUnit = = NULL ? Unit : : invalidId : targetUnit - > getId ( ) ;
int unitId = selection - > getUnit ( i ) - > getId ( ) ;
2010-08-31 23:14:15 +00:00
Vec2i currPos = world - > getMap ( ) - > computeDestPos ( refPos , selection - > getUnit ( i ) - > getPos ( ) , pos ) ;
2010-04-18 07:35:48 +00:00
NetworkCommand networkCommand ( this - > world , nctGiveCommand , unitId , commandType - > getId ( ) , currPos , - 1 , targetId , - 1 , tryQueue ) ;
2010-03-18 21:26:40 +00:00
//every unit is ordered to a different position
CommandResult result = pushNetworkCommand ( & networkCommand ) ;
results . push_back ( result ) ;
}
return computeResult ( results ) ;
}
else {
return crFailUndefined ;
}
}
//auto command
2011-01-08 21:53:05 +00:00
CommandResult Commander : : tryGiveCommand ( const Selection * selection , const Vec2i & pos , const Unit * targetUnit , bool tryQueue ) const {
2010-05-10 23:56:55 +00:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
CommandResult result = crFailUndefined ;
if ( selection - > isEmpty ( ) = = false ) {
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-03-18 21:26:40 +00:00
Vec2i refPos , currPos ;
CommandResultContainer results ;
//give orders to all selected units
2010-08-31 23:14:15 +00:00
refPos = world - > getMap ( ) - > computeRefPos ( selection ) ;
2010-05-10 23:56:55 +00:00
for ( int i = 0 ; i < selection - > getCount ( ) ; + + i ) {
2010-03-18 21:26:40 +00:00
//every unit is ordered to a different pos
2010-05-10 23:56:55 +00:00
const Unit * unit = selection - > getUnit ( i ) ;
assert ( unit ! = NULL ) ;
2010-08-31 23:14:15 +00:00
currPos = world - > getMap ( ) - > computeDestPos ( refPos , unit - > getPos ( ) , pos ) ;
2010-03-18 21:26:40 +00:00
//get command type
2010-05-10 23:56:55 +00:00
const CommandType * commandType = unit - > computeCommandType ( pos , targetUnit ) ;
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] unit = [%s] commandType = %p \n " , __FILE__ , __FUNCTION__ , __LINE__ , unit - > getFullName ( ) . c_str ( ) , commandType ) ;
2010-03-18 21:26:40 +00:00
//give commands
2010-05-10 23:56:55 +00:00
if ( commandType ! = NULL ) {
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] commandType->toString() [%s] \n " , __FILE__ , __FUNCTION__ , __LINE__ , commandType - > toString ( ) . c_str ( ) ) ;
2010-03-18 21:26:40 +00:00
int targetId = targetUnit = = NULL ? Unit : : invalidId : targetUnit - > getId ( ) ;
2010-05-10 23:56:55 +00:00
int unitId = unit - > getId ( ) ;
2010-04-18 07:35:48 +00:00
NetworkCommand networkCommand ( this - > world , nctGiveCommand , unitId , commandType - > getId ( ) , currPos , - 1 , targetId , - 1 , tryQueue ) ;
2010-03-18 21:26:40 +00:00
CommandResult result = pushNetworkCommand ( & networkCommand ) ;
results . push_back ( result ) ;
}
2010-05-10 23:56:55 +00:00
else if ( unit - > isMeetingPointSettable ( ) = = true ) {
NetworkCommand command ( this - > world , nctSetMeetingPoint , unit - > getId ( ) , - 1 , currPos ) ;
CommandResult result = pushNetworkCommand ( & command ) ;
results . push_back ( result ) ;
}
else {
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-03-18 21:26:40 +00:00
results . push_back ( crFailUndefined ) ;
}
}
2010-05-10 23:56:55 +00:00
result = computeResult ( results ) ;
2010-03-18 21:26:40 +00:00
}
2010-05-10 23:56:55 +00:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] result = %d \n " , __FILE__ , __FUNCTION__ , __LINE__ , result ) ;
return result ;
2010-03-18 21:26:40 +00:00
}
CommandResult Commander : : tryCancelCommand ( const Selection * selection ) const {
for ( int i = 0 ; i < selection - > getCount ( ) ; + + i ) {
2010-03-22 04:47:16 +00:00
NetworkCommand command ( this - > world , nctCancelCommand , selection - > getUnit ( i ) - > getId ( ) ) ;
2010-03-18 21:26:40 +00:00
pushNetworkCommand ( & command ) ;
}
return crSuccess ;
}
void Commander : : trySetMeetingPoint ( const Unit * unit , const Vec2i & pos ) const {
2010-03-22 04:47:16 +00:00
NetworkCommand command ( this - > world , nctSetMeetingPoint , unit - > getId ( ) , - 1 , pos ) ;
2010-03-18 21:26:40 +00:00
pushNetworkCommand ( & command ) ;
}
// ==================== PRIVATE ====================
2011-01-08 21:53:05 +00:00
CommandResult Commander : : computeResult ( const CommandResultContainer & results ) const {
switch ( results . size ( ) ) {
case 0 :
return crFailUndefined ;
case 1 :
return results . front ( ) ;
default :
for ( int i = 0 ; i < results . size ( ) ; + + i ) {
if ( results [ i ] ! = crSuccess ) {
return crSomeFailed ;
}
}
return crSuccess ;
2010-03-18 21:26:40 +00:00
}
}
2010-08-30 20:45:12 +00:00
CommandResult Commander : : pushNetworkCommand ( const NetworkCommand * networkCommand ) const {
2010-03-18 21:26:40 +00:00
GameNetworkInterface * gameNetworkInterface = NetworkManager : : getInstance ( ) . getGameNetworkInterface ( ) ;
const Unit * unit = world - > findUnitById ( networkCommand - > getUnitId ( ) ) ;
CommandResult cr = crSuccess ;
//validate unit
2010-05-31 06:11:31 +00:00
if ( unit = = NULL ) {
2010-03-18 21:26:40 +00:00
char szBuf [ 1024 ] = " " ;
2011-01-02 01:48:56 +00:00
sprintf ( szBuf , " In [%s::%s - %d] Command refers to non existent unit id = %d. Game out of synch. " , __FILE__ , __FUNCTION__ , __LINE__ , networkCommand - > getUnitId ( ) ) ;
GameNetworkInterface * gameNetworkInterface = NetworkManager : : getInstance ( ) . getGameNetworkInterface ( ) ;
if ( gameNetworkInterface ! = NULL ) {
char szMsg [ 1024 ] = " " ;
sprintf ( szMsg , " Player detected an error: Command refers to non existent unit id = %d. Game out of synch. " , networkCommand - > getUnitId ( ) ) ;
gameNetworkInterface - > sendTextMessage ( szMsg , - 1 , true ) ;
}
2010-03-18 21:26:40 +00:00
throw runtime_error ( szBuf ) ;
}
//add the command to the interface
gameNetworkInterface - > requestCommand ( networkCommand ) ;
//calculate the result of the command
2010-05-31 06:11:31 +00:00
if ( networkCommand - > getNetworkCommandType ( ) = = nctGiveCommand ) {
2010-03-18 21:26:40 +00:00
Command * command = buildCommand ( networkCommand ) ;
cr = unit - > checkCommand ( command ) ;
delete command ;
}
return cr ;
}
2011-01-11 08:45:58 +00:00
void Commander : : signalNetworkUpdate ( Game * game ) {
if ( this - > networkThread ! = NULL ) {
this - > game = game ;
this - > networkThread - > signalUpdate ( 1 ) ;
time_t elapsedWait = time ( NULL ) ;
for ( ; difftime ( time ( NULL ) , elapsedWait ) < = 4 & &
this - > networkThread - > isSignalCompleted ( 1 ) = = false ; ) {
game - > render ( ) ;
}
}
}
void Commander : : commanderNetworkUpdateTask ( int id ) {
updateNetwork ( game ) ;
}
void Commander : : updateNetwork ( Game * game ) {
2010-03-18 21:26:40 +00:00
NetworkManager & networkManager = NetworkManager : : getInstance ( ) ;
2010-06-05 07:52:14 +00:00
//check that this is a keyframe
2011-01-11 08:45:58 +00:00
//GameSettings *gameSettings = this->world->getGame()->getGameSettings();
GameSettings * gameSettings = game - > getGameSettings ( ) ;
2010-06-08 07:40:32 +00:00
if ( networkManager . isNetworkGame ( ) = = false | |
( world - > getFrameCount ( ) % gameSettings - > getNetworkFramePeriod ( ) ) = = 0 ) {
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] networkManager.isNetworkGame() = %d,world->getFrameCount() = %d, gameSettings->getNetworkFramePeriod() = %d \n " , __FILE__ , __FUNCTION__ , __LINE__ , networkManager . isNetworkGame ( ) , world - > getFrameCount ( ) , gameSettings - > getNetworkFramePeriod ( ) ) ;
2010-03-18 21:26:40 +00:00
GameNetworkInterface * gameNetworkInterface = NetworkManager : : getInstance ( ) . getGameNetworkInterface ( ) ;
2010-04-15 01:19:00 +00:00
perfTimer . start ( ) ;
2010-03-18 21:26:40 +00:00
//update the keyframe
gameNetworkInterface - > updateKeyframe ( world - > getFrameCount ( ) ) ;
2010-07-06 05:30:34 +00:00
if ( perfTimer . getMillis ( ) > 0 ) SystemFlags : : OutputDebug ( SystemFlags : : debugPerformance , " In [%s::%s Line: %d] gameNetworkInterface->updateKeyframe for %d took %lld msecs \n " , __FILE__ , __FUNCTION__ , __LINE__ , world - > getFrameCount ( ) , perfTimer . getMillis ( ) ) ;
2010-03-18 21:26:40 +00:00
2010-04-15 01:19:00 +00:00
perfTimer . start ( ) ;
2010-03-18 21:26:40 +00:00
//give pending commands
for ( int i = 0 ; i < gameNetworkInterface - > getPendingCommandCount ( ) ; + + i ) {
giveNetworkCommand ( gameNetworkInterface - > getPendingCommand ( i ) ) ;
}
2010-10-24 05:52:21 +00:00
if ( perfTimer . getMillis ( ) > 0 ) SystemFlags : : OutputDebug ( SystemFlags : : debugPerformance , " In [%s::%s Line: %d] giveNetworkCommand took %lld msecs, PendingCommandCount = %d \n " , __FILE__ , __FUNCTION__ , __LINE__ , perfTimer . getMillis ( ) , gameNetworkInterface - > getPendingCommandCount ( ) ) ;
2010-03-18 21:26:40 +00:00
gameNetworkInterface - > clearPendingCommands ( ) ;
}
}
2010-03-22 04:47:16 +00:00
/*
2010-03-18 21:26:40 +00:00
void Commander : : giveNetworkCommandSpecial ( const NetworkCommand * networkCommand ) const {
switch ( networkCommand - > getNetworkCommandType ( ) ) {
case nctNetworkCommand : {
2010-03-19 23:26:00 +00:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] found nctNetworkCommand \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-03-18 21:26:40 +00:00
switch ( networkCommand - > getCommandTypeId ( ) ) {
case ncstRotateUnit : {
2010-03-19 23:26:00 +00:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] found ncstRotateUnit [%d] \n " , __FILE__ , __FUNCTION__ , __LINE__ , networkCommand - > getTargetId ( ) ) ;
2010-03-18 21:26:40 +00:00
int unitTypeId = networkCommand - > getUnitId ( ) ;
int factionIndex = networkCommand - > getUnitTypeId ( ) ;
int rotateAmount = networkCommand - > getTargetId ( ) ;
//const Faction *faction = world->getFaction(factionIndex);
//const UnitType* unitType= world->findUnitTypeById(faction->getType(), factionIndex);
char unitKey [ 50 ] = " " ;
sprintf ( unitKey , " %d_%d " , unitTypeId , factionIndex ) ;
2010-03-19 23:26:00 +00:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] unitKey = [%s] \n " , __FILE__ , __FUNCTION__ , __LINE__ , unitKey ) ;
2010-03-18 21:26:40 +00:00
Game * game = this - > world - > getGame ( ) ;
Gui * gui = game - > getGui ( ) ;
gui - > setUnitTypeBuildRotation ( unitKey , rotateAmount ) ;
//unit->setRotateAmount(networkCommand->getTargetId());
2010-03-19 23:26:00 +00:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] found ncstRotateUnit [%d] \n " , __FILE__ , __FUNCTION__ , __LINE__ , networkCommand - > getTargetId ( ) ) ;
2010-03-18 21:26:40 +00:00
}
break ;
}
2010-03-19 23:26:00 +00:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] found nctNetworkCommand \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-03-18 21:26:40 +00:00
}
break ;
default :
assert ( false ) ;
}
}
2010-03-22 04:47:16 +00:00
*/
2010-03-18 21:26:40 +00:00
2010-03-22 04:47:16 +00:00
void Commander : : giveNetworkCommand ( NetworkCommand * networkCommand ) const {
2010-11-11 08:02:50 +00:00
Chrono chrono ;
chrono . start ( ) ;
if ( chrono . getMillis ( ) > 0 ) SystemFlags : : OutputDebug ( SystemFlags : : debugPerformance , " In [%s::%s Line: %d] took msecs: %lld [START] \n " , __FILE__ , __FUNCTION__ , __LINE__ , chrono . getMillis ( ) ) ;
2010-03-22 04:47:16 +00:00
networkCommand - > preprocessNetworkCommand ( this - > world ) ;
2010-10-26 06:43:42 +00:00
2010-11-11 08:02:50 +00:00
if ( chrono . getMillis ( ) > 0 ) SystemFlags : : OutputDebug ( SystemFlags : : debugPerformance , " In [%s::%s Line: %d] took msecs: %lld [after networkCommand->preprocessNetworkCommand] \n " , __FILE__ , __FUNCTION__ , __LINE__ , chrono . getMillis ( ) ) ;
2010-03-22 04:47:16 +00:00
/*
2010-03-18 21:26:40 +00:00
if ( networkCommand - > getNetworkCommandType ( ) = = nctNetworkCommand ) {
giveNetworkCommandSpecial ( networkCommand ) ;
}
2010-03-22 04:47:16 +00:00
else
*/
{
2010-03-18 21:26:40 +00:00
Unit * unit = world - > findUnitById ( networkCommand - > getUnitId ( ) ) ;
2010-11-11 08:02:50 +00:00
if ( chrono . getMillis ( ) > 0 ) SystemFlags : : OutputDebug ( SystemFlags : : debugPerformance , " In [%s::%s Line: %d] took msecs: %lld [after world->findUnitById] \n " , __FILE__ , __FUNCTION__ , __LINE__ , chrono . getMillis ( ) ) ;
2010-10-24 05:52:21 +00:00
//execute command, if unit is still alive
if ( unit ! = NULL ) {
switch ( networkCommand - > getNetworkCommandType ( ) ) {
2010-03-18 21:26:40 +00:00
case nctGiveCommand : {
2010-10-24 05:52:21 +00:00
assert ( networkCommand - > getCommandTypeId ( ) ! = CommandType : : invalidId ) ;
2010-03-18 21:26:40 +00:00
2010-03-19 23:26:00 +00:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] found nctGiveCommand networkCommand->getUnitId() = %d \n " , __FILE__ , __FUNCTION__ , __LINE__ , networkCommand - > getUnitId ( ) ) ;
2010-03-18 21:26:40 +00:00
Command * command = buildCommand ( networkCommand ) ;
2010-05-01 09:27:08 +00:00
2010-11-11 08:02:50 +00:00
if ( chrono . getMillis ( ) > 0 ) SystemFlags : : OutputDebug ( SystemFlags : : debugPerformance , " In [%s::%s Line: %d] took msecs: %lld [after buildCommand] \n " , __FILE__ , __FUNCTION__ , __LINE__ , chrono . getMillis ( ) ) ;
2010-05-31 09:24:44 +00:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] command = %p \n " , __FILE__ , __FUNCTION__ , __LINE__ , command ) ;
2010-05-28 00:57:24 +00:00
2010-10-21 17:42:45 +00:00
unit - > giveCommand ( command , ( networkCommand - > getWantQueue ( ) ! = 0 ) ) ;
2010-03-18 21:26:40 +00:00
2010-11-11 08:02:50 +00:00
if ( chrono . getMillis ( ) > 0 ) SystemFlags : : OutputDebug ( SystemFlags : : debugPerformance , " In [%s::%s Line: %d] took msecs: %lld [after unit->giveCommand] \n " , __FILE__ , __FUNCTION__ , __LINE__ , chrono . getMillis ( ) ) ;
2010-03-19 23:26:00 +00:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] found nctGiveCommand networkCommand->getUnitId() = %d \n " , __FILE__ , __FUNCTION__ , __LINE__ , networkCommand - > getUnitId ( ) ) ;
2010-03-18 21:26:40 +00:00
}
break ;
case nctCancelCommand : {
2010-10-26 06:43:42 +00:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] found nctCancelCommand \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
unit - > cancelCommand ( ) ;
2010-11-11 08:02:50 +00:00
if ( chrono . getMillis ( ) > 0 ) SystemFlags : : OutputDebug ( SystemFlags : : debugPerformance , " In [%s::%s Line: %d] took msecs: %lld [after unit->cancelCommand] \n " , __FILE__ , __FUNCTION__ , __LINE__ , chrono . getMillis ( ) ) ;
2010-03-19 23:26:00 +00:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] found nctCancelCommand \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-03-18 21:26:40 +00:00
}
break ;
case nctSetMeetingPoint : {
2010-03-19 23:26:00 +00:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] found nctSetMeetingPoint \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-10-26 06:43:42 +00:00
2010-03-18 21:26:40 +00:00
unit - > setMeetingPos ( networkCommand - > getPosition ( ) ) ;
2010-10-26 06:43:42 +00:00
2010-11-11 08:02:50 +00:00
if ( chrono . getMillis ( ) > 0 ) SystemFlags : : OutputDebug ( SystemFlags : : debugPerformance , " In [%s::%s Line: %d] took msecs: %lld [after unit->setMeetingPos] \n " , __FILE__ , __FUNCTION__ , __LINE__ , chrono . getMillis ( ) ) ;
2010-03-19 23:26:00 +00:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] found nctSetMeetingPoint \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-03-18 21:26:40 +00:00
}
break ;
default :
assert ( false ) ;
}
}
else {
2010-03-19 23:26:00 +00:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] NULL Unit for id = %d, networkCommand->getNetworkCommandType() = %d \n " , __FILE__ , __FUNCTION__ , __LINE__ , networkCommand - > getUnitId ( ) , networkCommand - > getNetworkCommandType ( ) ) ;
2010-03-18 21:26:40 +00:00
}
}
2010-11-11 08:02:50 +00:00
if ( chrono . getMillis ( ) > 0 ) SystemFlags : : OutputDebug ( SystemFlags : : debugPerformance , " In [%s::%s Line: %d] took msecs: %lld [END] \n " , __FILE__ , __FUNCTION__ , __LINE__ , chrono . getMillis ( ) ) ;
2010-03-18 21:26:40 +00:00
}
2010-08-30 20:45:12 +00:00
Command * Commander : : buildCommand ( const NetworkCommand * networkCommand ) const {
2010-03-18 21:26:40 +00:00
assert ( networkCommand - > getNetworkCommandType ( ) = = nctGiveCommand ) ;
2010-05-29 10:34:56 +00:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] networkCommand [%s] \n " , __FILE__ , __FUNCTION__ , __LINE__ , networkCommand - > toString ( ) . c_str ( ) ) ;
2010-05-28 00:57:24 +00:00
2010-04-20 02:19:37 +00:00
if ( world = = NULL ) {
char szBuf [ 1024 ] = " " ;
sprintf ( szBuf , " In [%s::%s Line: %d] world == NULL for unit with id: %d " , __FILE__ , __FUNCTION__ , __LINE__ , networkCommand - > getUnitId ( ) ) ;
throw runtime_error ( szBuf ) ;
}
2010-03-18 21:26:40 +00:00
Unit * target = NULL ;
const CommandType * ct = NULL ;
const Unit * unit = world - > findUnitById ( networkCommand - > getUnitId ( ) ) ;
2010-05-28 00:57:24 +00:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-03-18 21:26:40 +00:00
//validate unit
if ( unit = = NULL ) {
char szBuf [ 1024 ] = " " ;
2010-05-15 18:59:17 +00:00
sprintf ( szBuf , " In [%s::%s Line: %d] Can not find unit with id: %d. Game out of synch. " , __FILE__ , __FUNCTION__ , __LINE__ , networkCommand - > getUnitId ( ) ) ;
2011-01-02 01:48:56 +00:00
GameNetworkInterface * gameNetworkInterface = NetworkManager : : getInstance ( ) . getGameNetworkInterface ( ) ;
if ( gameNetworkInterface ! = NULL ) {
char szMsg [ 1024 ] = " " ;
sprintf ( szMsg , " Player detected an error: Can not find unit with id: %d. Game out of synch. " , networkCommand - > getUnitId ( ) ) ;
gameNetworkInterface - > sendTextMessage ( szMsg , - 1 , true ) ;
}
2010-03-18 21:26:40 +00:00
throw runtime_error ( szBuf ) ;
}
2010-05-29 08:23:52 +00:00
2011-01-02 00:39:13 +00:00
ct = unit - > getType ( ) - > findCommandTypeById ( networkCommand - > getCommandTypeId ( ) ) ;
2010-05-29 08:23:52 +00:00
if ( unit - > getFaction ( ) - > getIndex ( ) ! = networkCommand - > getUnitFactionIndex ( ) ) {
2010-05-29 10:34:56 +00:00
char szBuf [ 4096 ] = " " ;
2010-05-29 08:23:52 +00:00
sprintf ( szBuf , " In [%s::%s Line: %d] \n Unit / Faction mismatch for network command = [%s] \n %s \n for unit = %d \n [%s] \n [%s] \n actual local factionIndex = %d. \n Game out of synch. " ,
__FILE__ , __FUNCTION__ , __LINE__ , networkCommand - > toString ( ) . c_str ( ) , unit - > getType ( ) - > getCommandTypeListDesc ( ) . c_str ( ) , unit - > getId ( ) , unit - > getFullName ( ) . c_str ( ) , unit - > getDesc ( ) . c_str ( ) , unit - > getFaction ( ) - > getIndex ( ) ) ;
2011-01-02 00:39:13 +00:00
SystemFlags : : OutputDebug ( SystemFlags : : debugError , " %s \n " , szBuf ) ;
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " %s \n " , szBuf ) ;
std : : string worldLog = world - > DumpWorldToLog ( ) ;
2011-01-02 01:48:56 +00:00
GameNetworkInterface * gameNetworkInterface = NetworkManager : : getInstance ( ) . getGameNetworkInterface ( ) ;
if ( gameNetworkInterface ! = NULL ) {
char szMsg [ 1024 ] = " " ;
sprintf ( szMsg , " Player detected an error: Unit / Faction mismatch for unitId: %d, Local faction index = %d, remote idnex = %d. Game out of synch. " , networkCommand - > getUnitId ( ) , unit - > getFaction ( ) - > getIndex ( ) , networkCommand - > getUnitFactionIndex ( ) ) ;
gameNetworkInterface - > sendTextMessage ( szMsg , - 1 , true ) ;
}
2011-01-02 00:39:13 +00:00
std : : string sError = " Error [#1]: Game is out of sync (Unit / Faction mismatch) \n please check log files for details. " ;
throw runtime_error ( sError ) ;
}
2011-01-02 01:48:56 +00:00
/*
I don ' t think we can validate in unit type since it can be different for certain commands ( like attack and build etc )
2011-01-02 00:39:13 +00:00
else if ( networkCommand - > getUnitTypeId ( ) > = 0 & &
unit - > getType ( ) - > getId ( ) ! = networkCommand - > getUnitTypeId ( ) & &
ct - > getClass ( ) ! = ccBuild ) {
char szBuf [ 4096 ] = " " ;
sprintf ( szBuf , " In [%s::%s Line: %d] \n Unit / Type mismatch for network command = [%s] \n %s \n for unit = %d \n [%s] \n [%s] \n actual local factionIndex = %d. \n actual local unitTypeId = %d. \n Game out of synch. " ,
__FILE__ , __FUNCTION__ , __LINE__ , networkCommand - > toString ( ) . c_str ( ) , unit - > getType ( ) - > getCommandTypeListDesc ( ) . c_str ( ) , unit - > getId ( ) , unit - > getFullName ( ) . c_str ( ) , unit - > getDesc ( ) . c_str ( ) , unit - > getFaction ( ) - > getIndex ( ) , unit - > getType ( ) - > getId ( ) ) ;
SystemFlags : : OutputDebug ( SystemFlags : : debugError , " %s \n " , szBuf ) ;
2010-05-29 08:23:52 +00:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " %s \n " , szBuf ) ;
std : : string worldLog = world - > DumpWorldToLog ( ) ;
2011-01-02 01:48:56 +00:00
GameNetworkInterface * gameNetworkInterface = NetworkManager : : getInstance ( ) . getGameNetworkInterface ( ) ;
if ( gameNetworkInterface ! = NULL ) {
char szMsg [ 1024 ] = " " ;
sprintf ( szMsg , " Player detected an error: Unit / Faction mismatch for unitId: %d, Local faction index = %d, remote idnex = %d. Game out of synch. " , networkCommand - > getUnitId ( ) , unit - > getFaction ( ) - > getIndex ( ) , networkCommand - > getUnitFactionIndex ( ) ) ;
gameNetworkInterface - > sendTextMessage ( szMsg , - 1 , true ) ;
}
2011-01-02 00:39:13 +00:00
std : : string sError = " Error [#2]: Game is out of sync (unit type mismatch) \n please check log files for details. " ;
2010-05-29 08:23:52 +00:00
throw runtime_error ( sError ) ;
}
2011-01-02 01:48:56 +00:00
*/
2010-03-18 21:26:40 +00:00
const UnitType * unitType = world - > findUnitTypeById ( unit - > getFaction ( ) - > getType ( ) , networkCommand - > getUnitTypeId ( ) ) ;
2010-05-29 07:58:58 +00:00
// debug test!
//throw runtime_error("Test missing command type!");
2010-03-18 21:26:40 +00:00
//validate command type
if ( ct = = NULL ) {
2010-05-29 10:34:56 +00:00
char szBuf [ 4096 ] = " " ;
2010-05-29 07:58:58 +00:00
sprintf ( szBuf , " In [%s::%s Line: %d] \n Can not find command type for network command = [%s] \n %s \n for unit = %d \n [%s] \n [%s] \n actual local factionIndex = %d. \n Game out of synch. " ,
__FILE__ , __FUNCTION__ , __LINE__ , networkCommand - > toString ( ) . c_str ( ) , unit - > getType ( ) - > getCommandTypeListDesc ( ) . c_str ( ) , unit - > getId ( ) , unit - > getFullName ( ) . c_str ( ) , unit - > getDesc ( ) . c_str ( ) , unit - > getFaction ( ) - > getIndex ( ) ) ;
2010-03-18 21:26:40 +00:00
2010-05-28 00:26:29 +00:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " %s \n " , szBuf ) ;
2010-05-18 03:53:57 +00:00
std : : string worldLog = world - > DumpWorldToLog ( ) ;
2011-01-02 01:48:56 +00:00
GameNetworkInterface * gameNetworkInterface = NetworkManager : : getInstance ( ) . getGameNetworkInterface ( ) ;
if ( gameNetworkInterface ! = NULL ) {
char szMsg [ 1024 ] = " " ;
sprintf ( szMsg , " Player detected an error: Can not find command type for unitId: %d. Game out of synch. " , networkCommand - > getUnitId ( ) ) ;
gameNetworkInterface - > sendTextMessage ( szMsg , - 1 , true ) ;
}
std : : string sError = " Error [#3]: Game is out of sync, please check log files for details. " ;
2010-05-18 03:53:57 +00:00
throw runtime_error ( sError ) ;
2010-03-18 21:26:40 +00:00
}
2010-05-28 00:57:24 +00:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-03-25 12:15:10 +00:00
CardinalDir facing ;
// get facing/target ... the target might be dead due to lag, cope with it
if ( ct - > getClass ( ) = = ccBuild ) {
assert ( networkCommand - > getTargetId ( ) > = 0 & & networkCommand - > getTargetId ( ) < 4 ) ;
facing = CardinalDir ( networkCommand - > getTargetId ( ) ) ;
2010-04-20 02:19:37 +00:00
}
else if ( networkCommand - > getTargetId ( ) ! = Unit : : invalidId ) {
2010-03-18 21:26:40 +00:00
target = world - > findUnitById ( networkCommand - > getTargetId ( ) ) ;
}
2010-05-28 00:57:24 +00:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-03-18 21:26:40 +00:00
//create command
Command * command = NULL ;
2011-01-02 00:39:13 +00:00
if ( unitType ! = NULL ) {
2010-03-25 12:15:10 +00:00
command = new Command ( ct , networkCommand - > getPosition ( ) , unitType , facing ) ;
2010-03-18 21:26:40 +00:00
}
2011-01-02 00:39:13 +00:00
else if ( target = = NULL ) {
2010-03-18 21:26:40 +00:00
command = new Command ( ct , networkCommand - > getPosition ( ) ) ;
}
2011-01-02 00:39:13 +00:00
else {
2010-03-18 21:26:40 +00:00
command = new Command ( ct , target ) ;
}
2010-08-30 20:45:12 +00:00
// Add in any special state
CommandStateType commandStateType = networkCommand - > getCommandStateType ( ) ;
int commandStateValue = networkCommand - > getCommandStateValue ( ) ;
command - > setStateType ( commandStateType ) ;
command - > setStateValue ( commandStateValue ) ;
2010-05-28 00:57:24 +00:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-03-18 21:26:40 +00:00
//issue command
return command ;
}
} } //end namespace