2010-03-18 21:26:40 +00:00
// ==============================================================
// This file is part of Glest (www.glest.org)
//
2011-12-14 07:40:48 +00:00
// Copyright (C) 2001-2008 Martiño 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 ) {
2011-03-28 03:54:23 +00:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugNetwork ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s] Line: %d value = %d \n " , __FILE__ , __FUNCTION__ , __LINE__ , value ) ;
2011-01-11 08:45:58 +00:00
BaseThread : : setQuitStatus ( value ) ;
if ( value = = true ) {
signalUpdate ( - 1 ) ;
}
2011-03-28 03:54:23 +00:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugNetwork ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s] Line: %d \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2011-01-11 08:45:58 +00:00
}
void CommanderNetworkThread : : signalUpdate ( int id ) {
2011-01-31 23:01:39 +00:00
MutexSafeWrapper safeMutex ( & idMutex , string ( __FILE__ ) + " _ " + intToStr ( __LINE__ ) ) ;
2011-01-11 08:45:58 +00:00
this - > idStatus . first = id ;
this - > idStatus . second = false ;
safeMutex . ReleaseLock ( ) ;
semTaskSignalled . signal ( ) ;
}
void CommanderNetworkThread : : setTaskCompleted ( int id ) {
2011-01-31 23:01:39 +00:00
MutexSafeWrapper safeMutex ( & idMutex , string ( __FILE__ ) + " _ " + intToStr ( __LINE__ ) ) ;
2011-01-11 08:45:58 +00:00
this - > idStatus . second = true ;
safeMutex . ReleaseLock ( ) ;
}
bool CommanderNetworkThread : : isSignalCompleted ( int id ) {
2011-01-31 23:01:39 +00:00
MutexSafeWrapper safeMutex ( & idMutex , string ( __FILE__ ) + " _ " + intToStr ( __LINE__ ) ) ;
2011-01-11 08:45:58 +00:00
bool result = this - > idStatus . second ;
safeMutex . ReleaseLock ( ) ;
return result ;
}
void CommanderNetworkThread : : execute ( ) {
RunningStatusSafeWrapper runningStatus ( this ) ;
try {
2011-03-28 03:54:23 +00:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2011-01-11 08:45:58 +00:00
2011-08-31 23:10:43 +00:00
//unsigned int idx = 0;
2011-01-11 08:45:58 +00:00
for ( ; this - > commanderInterface ! = NULL ; ) {
if ( getQuitStatus ( ) = = true ) {
2011-03-28 03:54:23 +00:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2011-01-11 08:45:58 +00:00
break ;
}
semTaskSignalled . waitTillSignalled ( ) ;
if ( getQuitStatus ( ) = = true ) {
2011-03-28 03:54:23 +00:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2011-01-11 08:45:58 +00:00
break ;
}
2011-01-31 23:01:39 +00:00
MutexSafeWrapper safeMutex ( & idMutex , string ( __FILE__ ) + " _ " + intToStr ( __LINE__ ) ) ;
2011-01-11 08:45:58 +00:00
if ( idStatus . first > 0 ) {
int updateId = this - > idStatus . first ;
safeMutex . ReleaseLock ( ) ;
this - > commanderInterface - > commanderNetworkUpdateTask ( updateId ) ;
setTaskCompleted ( updateId ) ;
}
else {
safeMutex . ReleaseLock ( ) ;
}
if ( getQuitStatus ( ) = = true ) {
2011-03-28 03:54:23 +00:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2011-01-11 08:45:58 +00:00
break ;
}
}
2011-03-28 03:54:23 +00:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2011-01-11 08:45:58 +00:00
}
catch ( const exception & ex ) {
SystemFlags : : OutputDebug ( SystemFlags : : debugError , " In [%s::%s Line: %d] Error [%s] \n " , __FILE__ , __FUNCTION__ , __LINE__ , ex . what ( ) ) ;
2011-03-28 03:54:23 +00:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2011-01-11 08:45:58 +00:00
2012-04-14 21:21:09 +00:00
throw megaglest_runtime_error ( ex . what ( ) ) ;
2011-01-11 08:45:58 +00:00
}
2011-03-28 03:54:23 +00:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugNetwork ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s] Line: %d \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2011-01-11 08:45:58 +00:00
}
// =====================================================
// class
// =====================================================
Commander : : Commander ( ) {
2011-01-11 22:09:46 +00:00
//this->networkThread = new CommanderNetworkThread(this);
//this->networkThread->setUniqueID(__FILE__);
//this->networkThread->start();
2011-08-31 23:10:43 +00:00
world = NULL ;
2011-01-11 08:45:58 +00:00
}
Commander : : ~ Commander ( ) {
2011-01-11 22:09:46 +00:00
//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;
2011-01-11 08:45:58 +00:00
}
2010-03-18 21:26:40 +00:00
void Commander : : init ( World * world ) {
this - > world = world ;
}
2011-02-10 21:02:07 +00:00
bool Commander : : canSubmitCommandType ( const Unit * unit , const CommandType * commandType ) const {
bool canSubmitCommand = true ;
const MorphCommandType * mct = dynamic_cast < const MorphCommandType * > ( commandType ) ;
if ( mct & & unit - > getCommandSize ( ) > 0 ) {
Command * cur_command = unit - > getCurrCommand ( ) ;
2011-02-11 04:48:17 +00:00
if ( cur_command ! = NULL ) {
const MorphCommandType * cur_mct = dynamic_cast < const MorphCommandType * > ( cur_command - > getCommandType ( ) ) ;
if ( cur_mct & & unit - > getCurrSkill ( ) & & unit - > getCurrSkill ( ) - > getClass ( ) = = scMorph ) {
const UnitType * morphUnitType = mct - > getMorphUnit ( ) ;
const UnitType * cur_morphUnitType = cur_mct - > getMorphUnit ( ) ;
if ( morphUnitType ! = NULL & & cur_morphUnitType ! = NULL & & morphUnitType - > getId ( ) = = cur_morphUnitType - > getId ( ) ) {
canSubmitCommand = false ;
}
2011-02-10 21:02:07 +00:00
}
}
}
return canSubmitCommand ;
}
2012-11-10 19:39:55 +00:00
std : : pair < CommandResult , string > Commander : : tryGiveCommand ( const Selection * selection , const CommandType * commandType ,
2010-08-28 01:46:26 +00:00
const Vec2i & pos , const UnitType * unitType ,
CardinalDir facing , bool tryQueue , Unit * targetUnit ) const {
2011-03-28 03:54:23 +00:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-08-28 01:46:26 +00:00
2012-11-10 19:39:55 +00:00
std : : pair < CommandResult , string > result ( crFailUndefined , " " ) ;
2010-08-28 01:46:26 +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-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 ;
2011-07-05 04:37:35 +00:00
int unitCommandGroupId = - 1 ;
if ( selection - > getCount ( ) > 1 ) {
unitCommandGroupId = world - > getNextCommandGroupId ( ) ;
}
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 ) ;
2011-02-10 21:02:07 +00:00
2012-11-10 19:39:55 +00:00
std : : pair < CommandResult , string > resultCur ( crFailUndefined , " " ) ;
2011-02-10 21:02:07 +00:00
bool canSubmitCommand = canSubmitCommandType ( unit , commandType ) ;
if ( canSubmitCommand = = true ) {
int unitId = unit - > getId ( ) ;
2013-02-04 08:30:43 +00:00
Vec2i currPos = world - > getMap ( ) - > computeDestPos ( refPos , unit - > getPosNotThreadSafe ( ) , pos ) ;
2011-02-10 21:02:07 +00:00
Vec2i usePos = currPos ;
const CommandType * useCommandtype = commandType ;
if ( dynamic_cast < const BuildCommandType * > ( commandType ) ! = NULL ) {
usePos = pos ;
//if(unitSignalledToBuild == false) {
//if(builderUnit->getId() == unitId)
// builderUnitId = unitId;
//unitSignalledToBuild = true;
//}
//else {
if ( builderUnit - > getId ( ) ! = unitId ) {
useCommandtype = unit - > getType ( ) - > getFirstRepairCommand ( unitType ) ;
commandStateType = cst_linkedUnit ;
commandStateValue = builderUnitId ;
//tryQueue = true;
}
else {
commandStateType = cst_None ;
commandStateValue = - 1 ;
}
2010-08-31 23:14:15 +00:00
}
2010-08-28 01:46:26 +00:00
2011-02-10 21:02:07 +00:00
if ( useCommandtype ! = NULL ) {
NetworkCommand networkCommand ( this - > world , nctGiveCommand , unitId ,
useCommandtype - > getId ( ) , usePos , unitType - > getId ( ) ,
( targetUnit ! = NULL ? targetUnit - > getId ( ) : - 1 ) ,
2011-07-05 04:37:35 +00:00
facing , tryQueue , commandStateType , commandStateValue ,
unitCommandGroupId ) ;
2010-09-01 04:19:20 +00:00
2011-02-10 21:02:07 +00:00
//every unit is ordered to a the position
2012-11-10 19:39:55 +00:00
resultCur = pushNetworkCommand ( & networkCommand ) ;
2011-02-10 21:02:07 +00:00
}
2010-09-01 04:19:20 +00:00
}
2011-02-10 21:02:07 +00:00
2012-11-10 19:39:55 +00:00
results . push_back ( resultCur ) ;
2010-08-28 01:46:26 +00:00
}
return computeResult ( results ) ;
}
2012-11-10 19:39:55 +00:00
return std : : pair < CommandResult , string > ( crFailUndefined , " " ) ;
2010-08-28 01:46:26 +00:00
}
2012-11-10 19:39:55 +00:00
std : : pair < CommandResult , string > Commander : : tryGiveCommand ( const Unit * unit , const CommandType * commandType ,
2010-08-28 01:46:26 +00:00
const Vec2i & pos , const UnitType * unitType ,
2011-07-05 04:37:35 +00:00
CardinalDir facing , bool tryQueue , Unit * targetUnit ,
int unitGroupCommandId ) const {
2011-03-28 03:54:23 +00:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-06-01 16:54:44 +00:00
2010-11-11 08:02:50 +00:00
Chrono chrono ;
2011-03-28 03:54:23 +00:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugPerformance ) . enabled ) chrono . start ( ) ;
2010-11-11 08:02:50 +00:00
2010-05-29 10:34:56 +00:00
assert ( this - > world ! = NULL ) ;
assert ( unit ! = NULL ) ;
assert ( commandType ! = NULL ) ;
assert ( unitType ! = NULL ) ;
2011-03-28 03:54:23 +00: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 08:02:50 +00:00
2012-11-10 19:39:55 +00:00
std : : pair < CommandResult , string > result ( crFailUndefined , " " ) ;
2011-02-10 21:02:07 +00:00
bool canSubmitCommand = canSubmitCommandType ( unit , commandType ) ;
if ( canSubmitCommand = = true ) {
NetworkCommand networkCommand ( this - > world , nctGiveCommand , unit - > getId ( ) ,
commandType - > getId ( ) , pos , unitType - > getId ( ) ,
( targetUnit ! = NULL ? targetUnit - > getId ( ) : - 1 ) ,
2011-07-05 04:37:35 +00:00
facing , tryQueue , cst_None , - 1 , unitGroupCommandId ) ;
2010-06-01 16:54:44 +00:00
2011-03-28 03:54:23 +00: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-06-01 16:54:44 +00:00
2011-02-10 21:02:07 +00:00
result = pushNetworkCommand ( & networkCommand ) ;
}
2010-11-11 08:02:50 +00:00
2011-03-28 03:54:23 +00: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 08:02:50 +00:00
return result ;
2010-03-18 21:26:40 +00:00
}
2012-11-10 19:39:55 +00:00
std : : pair < CommandResult , string > Commander : : tryGiveCommand ( const Selection * selection , CommandClass commandClass ,
2011-07-05 04:37:35 +00:00
const Vec2i & pos , const Unit * targetUnit , bool tryQueue ) const {
2010-12-28 02:57:36 +00:00
2012-11-10 19:39:55 +00:00
std : : pair < CommandResult , string > result ( crFailUndefined , " " ) ;
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
2011-07-05 04:37:35 +00:00
int unitCommandGroupId = - 1 ;
if ( selection - > getCount ( ) > 1 ) {
unitCommandGroupId = world - > getNextCommandGroupId ( ) ;
}
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 ) {
2012-11-10 19:39:55 +00:00
std : : pair < CommandResult , string > resultCur ( crFailUndefined , " " ) ;
2011-02-10 21:02:07 +00:00
bool canSubmitCommand = canSubmitCommandType ( unit , ct ) ;
if ( canSubmitCommand = = true ) {
2010-03-18 21:26:40 +00:00
2011-02-10 21:02:07 +00:00
int targetId = targetUnit = = NULL ? Unit : : invalidId : targetUnit - > getId ( ) ;
int unitId = selection - > getUnit ( i ) - > getId ( ) ;
2011-07-05 04:37:35 +00:00
Vec2i currPos = world - > getMap ( ) - > computeDestPos ( refPos ,
2013-02-04 08:30:43 +00:00
selection - > getUnit ( i ) - > getPosNotThreadSafe ( ) , pos ) ;
2011-07-05 04:37:35 +00:00
NetworkCommand networkCommand ( this - > world , nctGiveCommand ,
unitId , ct - > getId ( ) , currPos , - 1 , targetId , - 1 ,
tryQueue , cst_None , - 1 , unitCommandGroupId ) ;
2011-02-10 21:02:07 +00:00
//every unit is ordered to a different pos
2012-11-10 19:39:55 +00:00
resultCur = pushNetworkCommand ( & networkCommand ) ;
2011-02-10 21:02:07 +00:00
}
2012-11-10 19:39:55 +00:00
results . push_back ( resultCur ) ;
2010-03-18 21:26:40 +00:00
}
else {
2012-11-10 19:39:55 +00:00
results . push_back ( std : : pair < CommandResult , string > ( crFailUndefined , " " ) ) ;
2010-03-18 21:26:40 +00:00
}
}
return computeResult ( results ) ;
}
else {
2012-11-10 19:39:55 +00:00
return std : : pair < CommandResult , string > ( crFailUndefined , " " ) ;
2010-03-18 21:26:40 +00:00
}
}
2012-11-10 19:39:55 +00:00
std : : pair < CommandResult , string > Commander : : tryGiveCommand ( const Selection * selection ,
2010-08-28 01:46:26 +00:00
const CommandType * commandType , const Vec2i & pos ,
2011-07-05 04:37:35 +00:00
const Unit * targetUnit , bool tryQueue ) const {
2012-11-10 19:39:55 +00:00
std : : pair < CommandResult , string > result ( crFailUndefined , " " ) ;
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
2011-07-05 04:37:35 +00:00
int unitCommandGroupId = - 1 ;
if ( selection - > getCount ( ) > 1 ) {
unitCommandGroupId = world - > getNextCommandGroupId ( ) ;
}
2010-03-18 21:26:40 +00:00
//give orders to all selected units
2011-07-05 04:37:35 +00:00
for ( int i = 0 ; i < selection - > getCount ( ) ; + + i ) {
2011-02-10 21:02:07 +00:00
const Unit * unit = selection - > getUnit ( i ) ;
assert ( unit ! = NULL ) ;
2010-03-18 21:26:40 +00:00
2012-11-10 19:39:55 +00:00
std : : pair < CommandResult , string > resultCur ( crFailUndefined , " " ) ;
2011-02-10 21:02:07 +00:00
bool canSubmitCommand = canSubmitCommandType ( unit , commandType ) ;
if ( canSubmitCommand = = true ) {
int targetId = targetUnit = = NULL ? Unit : : invalidId : targetUnit - > getId ( ) ;
int unitId = unit - > getId ( ) ;
2013-02-04 08:30:43 +00:00
Vec2i currPos = world - > getMap ( ) - > computeDestPos ( refPos , unit - > getPosNotThreadSafe ( ) , pos ) ;
2011-07-05 04:37:35 +00:00
NetworkCommand networkCommand ( this - > world , nctGiveCommand , unitId ,
commandType - > getId ( ) , currPos , - 1 , targetId , - 1 , tryQueue ,
cst_None , - 1 , unitCommandGroupId ) ;
2011-02-10 21:02:07 +00:00
//every unit is ordered to a different position
2012-11-10 19:39:55 +00:00
resultCur = pushNetworkCommand ( & networkCommand ) ;
2011-02-10 21:02:07 +00:00
}
2012-11-10 19:39:55 +00:00
results . push_back ( resultCur ) ;
2010-03-18 21:26:40 +00:00
}
return computeResult ( results ) ;
}
else {
2012-11-10 19:39:55 +00:00
return std : : pair < CommandResult , string > ( crFailUndefined , " " ) ;
2010-03-18 21:26:40 +00:00
}
}
//auto command
2012-11-10 19:39:55 +00:00
std : : pair < CommandResult , string > Commander : : tryGiveCommand ( const Selection * selection , const Vec2i & pos ,
2011-07-05 04:37:35 +00:00
const Unit * targetUnit , bool tryQueue , int unitCommandGroupId ) const {
2011-03-28 03:54:23 +00:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-05-10 23:56:55 +00:00
2012-11-10 19:39:55 +00:00
std : : pair < CommandResult , string > result ( crFailUndefined , " " ) ;
2010-05-10 23:56:55 +00:00
if ( selection - > isEmpty ( ) = = false ) {
2010-03-18 21:26:40 +00:00
Vec2i refPos , currPos ;
CommandResultContainer results ;
2011-07-05 04:37:35 +00:00
if ( unitCommandGroupId = = - 1 & & selection - > getCount ( ) > 1 ) {
unitCommandGroupId = world - > getNextCommandGroupId ( ) ;
}
2010-03-18 21:26:40 +00:00
//give orders to all selected units
2010-08-31 23:14:15 +00:00
refPos = world - > getMap ( ) - > computeRefPos ( selection ) ;
2011-07-05 04:37:35 +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 ) ;
2013-02-04 08:30:43 +00:00
currPos = world - > getMap ( ) - > computeDestPos ( refPos , unit - > getPosNotThreadSafe ( ) , 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 ) ;
2010-03-18 21:26:40 +00:00
//give commands
2011-02-10 21:02:07 +00:00
if ( commandType ! = NULL ) {
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-03-18 21:26:40 +00:00
2012-11-10 19:39:55 +00:00
std : : pair < CommandResult , string > resultCur ( crFailUndefined , " " ) ;
2011-02-10 21:02:07 +00:00
bool canSubmitCommand = canSubmitCommandType ( unit , commandType ) ;
if ( canSubmitCommand = = true ) {
2011-07-05 04:37:35 +00:00
NetworkCommand networkCommand ( this - > world , nctGiveCommand ,
unitId , commandType - > getId ( ) , currPos , - 1 , targetId ,
- 1 , tryQueue , cst_None , - 1 , unitCommandGroupId ) ;
2012-11-10 19:39:55 +00:00
resultCur = pushNetworkCommand ( & networkCommand ) ;
2011-02-10 21:02:07 +00:00
}
2012-11-10 19:39:55 +00:00
results . push_back ( resultCur ) ;
2010-03-18 21:26:40 +00:00
}
2010-05-10 23:56:55 +00:00
else if ( unit - > isMeetingPointSettable ( ) = = true ) {
2011-07-05 04:37:35 +00:00
NetworkCommand command ( this - > world , nctSetMeetingPoint ,
unit - > getId ( ) , - 1 , currPos , - 1 , - 1 , - 1 , false ,
cst_None , - 1 , unitCommandGroupId ) ;
2010-05-10 23:56:55 +00:00
2012-11-10 19:39:55 +00:00
std : : pair < CommandResult , string > resultCur = pushNetworkCommand ( & command ) ;
results . push_back ( resultCur ) ;
2010-05-10 23:56:55 +00:00
}
else {
2012-11-10 19:39:55 +00:00
results . push_back ( std : : pair < CommandResult , string > ( crFailUndefined , " " ) ) ;
2010-03-18 21:26:40 +00:00
}
}
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
2011-04-11 00:11:52 +00:00
//if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] result = %d\n",__FILE__,__FUNCTION__,__LINE__,result);
2010-05-10 23:56:55 +00:00
return result ;
2010-03-18 21:26:40 +00:00
}
2011-07-05 04:37:35 +00:00
CommandResult Commander : : tryCancelCommand ( const Selection * selection ) const {
int unitCommandGroupId = - 1 ;
if ( selection - > getCount ( ) > 1 ) {
unitCommandGroupId = world - > getNextCommandGroupId ( ) ;
}
2010-03-18 21:26:40 +00:00
2011-07-05 04:37:35 +00:00
for ( int i = 0 ; i < selection - > getCount ( ) ; + + i ) {
NetworkCommand command ( this - > world , nctCancelCommand ,
selection - > getUnit ( i ) - > getId ( ) , - 1 , Vec2i ( 0 ) , - 1 , - 1 , - 1 , false ,
cst_None , - 1 , unitCommandGroupId ) ;
2010-03-18 21:26:40 +00:00
pushNetworkCommand ( & command ) ;
}
return crSuccess ;
}
2011-07-05 04:37:35 +00:00
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 ) ;
}
2011-09-21 06:51:28 +00:00
void Commander : : trySwitchTeam ( const Faction * faction , int teamIndex ) const {
NetworkCommand command ( this - > world , nctSwitchTeam , faction - > getIndex ( ) , teamIndex ) ;
pushNetworkCommand ( & command ) ;
}
void Commander : : trySwitchTeamVote ( const Faction * faction , SwitchTeamVote * vote ) const {
NetworkCommand command ( this - > world , nctSwitchTeamVote , faction - > getIndex ( ) , vote - > factionIndex , Vec2i ( 0 ) , vote - > allowSwitchTeam ) ;
pushNetworkCommand ( & command ) ;
}
2010-03-18 21:26:40 +00:00
2012-09-21 05:45:09 +00:00
void Commander : : tryDisconnectNetworkPlayer ( const Faction * faction , int playerIndex ) const {
NetworkCommand command ( this - > world , nctDisconnectNetworkPlayer , faction - > getIndex ( ) , playerIndex ) ;
pushNetworkCommand ( & command ) ;
}
2011-10-03 20:48:09 +00:00
void Commander : : tryPauseGame ( ) const {
2012-05-22 06:17:56 +00:00
NetworkCommand command ( this - > world , nctPauseResume , 1 ) ;
2011-10-03 20:48:09 +00:00
pushNetworkCommand ( & command ) ;
}
void Commander : : tryResumeGame ( ) const {
2012-05-22 06:17:56 +00:00
NetworkCommand command ( this - > world , nctPauseResume , 0 ) ;
2011-10-03 20:48:09 +00:00
pushNetworkCommand ( & command ) ;
}
2012-06-09 15:21:18 +00:00
void Commander : : tryNetworkPlayerDisconnected ( int factionIndex ) const {
2012-09-21 15:03:13 +00:00
NetworkCommand command ( this - > world , nctPlayerStatusChange , factionIndex , npst_Disconnected ) ;
2012-06-09 15:21:18 +00:00
pushNetworkCommand ( & command ) ;
}
2010-03-18 21:26:40 +00:00
// ==================== PRIVATE ====================
2012-11-10 19:39:55 +00:00
std : : pair < CommandResult , string > Commander : : computeResult ( const CommandResultContainer & results ) const {
std : : pair < CommandResult , string > result ( crFailUndefined , " " ) ;
2011-01-08 21:53:05 +00:00
switch ( results . size ( ) ) {
case 0 :
2012-11-10 19:39:55 +00:00
return std : : pair < CommandResult , string > ( crFailUndefined , " " ) ;
2011-01-08 21:53:05 +00:00
case 1 :
return results . front ( ) ;
default :
for ( int i = 0 ; i < results . size ( ) ; + + i ) {
2012-11-10 19:39:55 +00:00
if ( results [ i ] . first ! = crSuccess ) {
return std : : pair < CommandResult , string > ( crSomeFailed , results [ i ] . second ) ;
2011-01-08 21:53:05 +00:00
}
}
2012-11-10 19:39:55 +00:00
break ;
2010-03-18 21:26:40 +00:00
}
2012-11-10 19:39:55 +00:00
return std : : pair < CommandResult , string > ( crSuccess , " " ) ;
2010-03-18 21:26:40 +00:00
}
2012-11-10 19:39:55 +00:00
std : : pair < CommandResult , string > Commander : : pushNetworkCommand ( const NetworkCommand * networkCommand ) const {
2010-03-18 21:26:40 +00:00
GameNetworkInterface * gameNetworkInterface = NetworkManager : : getInstance ( ) . getGameNetworkInterface ( ) ;
2012-11-10 19:39:55 +00:00
std : : pair < CommandResult , string > result ( crSuccess , " " ) ;
2010-03-18 21:26:40 +00:00
//validate unit
2011-09-22 05:14:20 +00:00
const Unit * unit = NULL ;
if ( networkCommand - > getNetworkCommandType ( ) ! = nctSwitchTeam & &
2011-10-03 20:48:09 +00:00
networkCommand - > getNetworkCommandType ( ) ! = nctSwitchTeamVote & &
2012-07-10 02:12:07 +00:00
networkCommand - > getNetworkCommandType ( ) ! = nctPauseResume & &
2012-09-21 05:45:09 +00:00
networkCommand - > getNetworkCommandType ( ) ! = nctPlayerStatusChange & &
networkCommand - > getNetworkCommandType ( ) ! = nctDisconnectNetworkPlayer ) {
2011-09-22 05:14:20 +00:00
unit = world - > findUnitById ( networkCommand - > getUnitId ( ) ) ;
if ( unit = = NULL ) {
2012-10-19 01:31:20 +00:00
char szBuf [ 8096 ] = " " ;
snprintf ( szBuf , 8096 , " In [%s::%s - %d] Command refers to non existent unit id = %d. Game out of synch. " , __FILE__ , __FUNCTION__ , __LINE__ , networkCommand - > getUnitId ( ) ) ;
2011-09-22 05:14:20 +00:00
GameNetworkInterface * gameNetworkInterface = NetworkManager : : getInstance ( ) . getGameNetworkInterface ( ) ;
if ( gameNetworkInterface ! = NULL ) {
2012-10-19 01:31:20 +00:00
char szMsg [ 8096 ] = " " ;
snprintf ( szMsg , 8096 , " Player detected an error: Command refers to non existent unit id = %d. Game out of synch. " , networkCommand - > getUnitId ( ) ) ;
2011-09-22 05:14:20 +00:00
gameNetworkInterface - > sendTextMessage ( szMsg , - 1 , true , " " ) ;
}
2012-04-14 21:21:09 +00:00
throw megaglest_runtime_error ( szBuf ) ;
2011-09-22 05:14:20 +00:00
}
2010-03-18 21:26:40 +00:00
}
//add the command to the interface
gameNetworkInterface - > requestCommand ( networkCommand ) ;
//calculate the result of the command
2013-02-04 08:30:43 +00:00
if ( unit ! = NULL & & networkCommand - > getNetworkCommandType ( ) = = nctGiveCommand ) {
2010-03-18 21:26:40 +00:00
Command * command = buildCommand ( networkCommand ) ;
2012-11-10 19:39:55 +00:00
result = unit - > checkCommand ( command ) ;
2010-03-18 21:26:40 +00:00
delete command ;
}
2012-11-10 19:39:55 +00:00
return result ;
2010-03-18 21:26:40 +00:00
}
2011-01-11 08:45:58 +00:00
void Commander : : signalNetworkUpdate ( Game * game ) {
2011-01-11 22:09:46 +00:00
updateNetwork ( game ) ;
2011-01-11 08:45:58 +00:00
}
void Commander : : commanderNetworkUpdateTask ( int id ) {
2011-01-11 22:09:46 +00:00
//updateNetwork(game);
2011-01-11 08:45:58 +00:00
}
2012-03-20 04:53:26 +00:00
bool Commander : : getReplayCommandListForFrame ( int worldFrameCount ) {
bool haveReplyCommands = false ;
if ( replayCommandList . empty ( ) = = false ) {
//int worldFrameCount = world->getFrameCount();
2012-11-10 06:37:23 +00:00
if ( SystemFlags : : VERBOSE_MODE_ENABLED ) printf ( " worldFrameCount = %d replayCommandList.size() = " MG_SIZE_T_SPECIFIER " \n " , worldFrameCount , replayCommandList . size ( ) ) ;
2012-03-20 04:53:26 +00:00
std : : vector < NetworkCommand > replayList ;
for ( unsigned int i = 0 ; i < replayCommandList . size ( ) ; + + i ) {
std : : pair < int , NetworkCommand > & cmd = replayCommandList [ i ] ;
if ( cmd . first < = worldFrameCount ) {
replayList . push_back ( cmd . second ) ;
haveReplyCommands = true ;
}
}
if ( haveReplyCommands = = true ) {
replayCommandList . erase ( replayCommandList . begin ( ) , replayCommandList . begin ( ) + replayList . size ( ) ) ;
2012-11-10 06:37:23 +00:00
if ( SystemFlags : : VERBOSE_MODE_ENABLED ) printf ( " worldFrameCount = %d GIVING COMMANDS replayList.size() = " MG_SIZE_T_SPECIFIER " \n " , worldFrameCount , replayList . size ( ) ) ;
2012-03-20 04:53:26 +00:00
for ( int i = 0 ; i < replayList . size ( ) ; + + i ) {
giveNetworkCommand ( & replayList [ i ] ) ;
//pushNetworkCommand(&replayList[i]);
}
GameNetworkInterface * gameNetworkInterface = NetworkManager : : getInstance ( ) . getGameNetworkInterface ( ) ;
gameNetworkInterface - > setKeyframe ( worldFrameCount ) ;
}
}
return haveReplyCommands ;
}
bool Commander : : hasReplayCommandListForFrame ( ) const {
return ( replayCommandList . empty ( ) = = false ) ;
}
2012-03-20 05:58:24 +00:00
int Commander : : getReplayCommandListForFrameCount ( ) const {
return replayCommandList . size ( ) ;
}
2011-01-11 08:45:58 +00:00
void Commander : : updateNetwork ( Game * game ) {
2012-04-02 01:45:13 +00:00
if ( world = = NULL ) {
return ;
}
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 22:09:46 +00:00
if ( game ! = NULL ) {
GameSettings * gameSettings = game - > getGameSettings ( ) ;
if ( networkManager . isNetworkGame ( ) = = false | |
( world - > getFrameCount ( ) % gameSettings - > getNetworkFramePeriod ( ) ) = = 0 ) {
2010-06-08 07:40:32 +00:00
2011-03-28 03:54:23 +00:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) 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
2012-03-20 04:53:26 +00:00
//std::vector<NetworkCommand> replayList = getReplayCommandListForFrame();
if ( getReplayCommandListForFrame ( world - > getFrameCount ( ) ) = = false ) {
GameNetworkInterface * gameNetworkInterface = NetworkManager : : getInstance ( ) . getGameNetworkInterface ( ) ;
2010-03-18 21:26:40 +00:00
2012-03-20 04:53:26 +00:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugPerformance ) . enabled ) perfTimer . start ( ) ;
//update the keyframe
gameNetworkInterface - > updateKeyframe ( world - > getFrameCount ( ) ) ;
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugPerformance ) . enabled & & 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
2012-03-20 04:53:26 +00:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugPerformance ) . enabled ) perfTimer . start ( ) ;
//give pending commands
for ( int i = 0 ; i < gameNetworkInterface - > getPendingCommandCount ( ) ; + + i ) {
giveNetworkCommand ( gameNetworkInterface - > getPendingCommand ( i ) ) ;
}
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugPerformance ) . enabled & & 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 ( ) ) ;
gameNetworkInterface - > clearPendingCommands ( ) ;
}
2011-01-11 22:09:46 +00:00
}
2010-03-18 21:26:40 +00:00
}
}
2012-03-20 04:53:26 +00:00
void Commander : : addToReplayCommandList ( NetworkCommand & command , int worldFrameCount ) {
replayCommandList . push_back ( make_pair ( worldFrameCount , command ) ) ;
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 ;
2011-03-28 03:54:23 +00:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugPerformance ) . enabled ) chrono . start ( ) ;
2010-11-11 08:02:50 +00:00
2011-03-28 03:54:23 +00:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugPerformance ) . enabled & & chrono . getMillis ( ) > 0 ) SystemFlags : : OutputDebug ( SystemFlags : : debugPerformance , " In [%s::%s Line: %d] took msecs: %lld [START] \n " , __FILE__ , __FUNCTION__ , __LINE__ , chrono . getMillis ( ) ) ;
2010-11-11 08:02:50 +00:00
2012-03-20 04:53:26 +00:00
world - > getGame ( ) - > addNetworkCommandToReplayList ( networkCommand , world - > getFrameCount ( ) ) ;
2010-03-22 04:47:16 +00:00
networkCommand - > preprocessNetworkCommand ( this - > world ) ;
2010-10-26 06:43:42 +00:00
2011-03-28 03:54:23 +00:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugPerformance ) . enabled & & 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-11-11 08:02:50 +00:00
2011-10-05 15:56:08 +00:00
bool commandWasHandled = false ;
// Handle special commands first (that just use network command members as placeholders)
switch ( networkCommand - > getNetworkCommandType ( ) ) {
case nctSwitchTeam : {
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] found nctSwitchTeam \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
commandWasHandled = true ;
int factionIndex = networkCommand - > getUnitId ( ) ;
int newTeam = networkCommand - > getCommandTypeId ( ) ;
// Auto join empty team or ask players to join
bool autoJoinTeam = true ;
for ( int i = 0 ; i < world - > getFactionCount ( ) ; + + i ) {
if ( newTeam = = world - > getFaction ( i ) - > getTeam ( ) ) {
autoJoinTeam = false ;
break ;
}
}
if ( autoJoinTeam = = true ) {
Faction * faction = world - > getFaction ( factionIndex ) ;
int oldTeam = faction - > getTeam ( ) ;
faction - > setTeam ( newTeam ) ;
GameSettings * settings = world - > getGameSettingsPtr ( ) ;
settings - > setTeam ( factionIndex , newTeam ) ;
2012-07-19 23:21:55 +00:00
world - > getStats ( ) - > setTeam ( factionIndex , newTeam ) ;
2011-10-05 15:56:08 +00:00
if ( factionIndex = = world - > getThisFactionIndex ( ) ) {
world - > setThisTeamIndex ( newTeam ) ;
GameNetworkInterface * gameNetworkInterface = NetworkManager : : getInstance ( ) . getGameNetworkInterface ( ) ;
if ( gameNetworkInterface ! = NULL ) {
Lang & lang = Lang : : getInstance ( ) ;
const vector < string > languageList = settings - > getUniqueNetworkPlayerLanguages ( ) ;
for ( unsigned int i = 0 ; i < languageList . size ( ) ; + + i ) {
2012-10-19 01:31:20 +00:00
char szMsg [ 8096 ] = " " ;
2011-10-05 15:56:08 +00:00
if ( lang . hasString ( " PlayerSwitchedTeam " , languageList [ i ] ) = = true ) {
2012-10-19 01:31:20 +00:00
snprintf ( szMsg , 8096 , lang . get ( " PlayerSwitchedTeam " , languageList [ i ] ) . c_str ( ) , settings - > getNetworkPlayerName ( factionIndex ) . c_str ( ) , oldTeam , newTeam ) ;
2011-10-05 15:56:08 +00:00
}
else {
2012-10-19 01:31:20 +00:00
snprintf ( szMsg , 8096 , " Player %s switched from team# %d to team# %d. " , settings - > getNetworkPlayerName ( factionIndex ) . c_str ( ) , oldTeam , newTeam ) ;
2011-10-05 15:56:08 +00:00
}
bool localEcho = lang . isLanguageLocal ( languageList [ i ] ) ;
gameNetworkInterface - > sendTextMessage ( szMsg , - 1 , localEcho , languageList [ i ] ) ;
}
}
}
}
else {
for ( int i = 0 ; i < world - > getFactionCount ( ) ; + + i ) {
if ( newTeam = = world - > getFaction ( i ) - > getTeam ( ) ) {
Faction * faction = world - > getFaction ( factionIndex ) ;
SwitchTeamVote vote ;
vote . factionIndex = factionIndex ;
vote . allowSwitchTeam = false ;
vote . oldTeam = faction - > getTeam ( ) ;
vote . newTeam = newTeam ;
vote . voted = false ;
world - > getFaction ( i ) - > setSwitchTeamVote ( vote ) ;
}
}
}
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugPerformance ) . enabled & & chrono . getMillis ( ) > 0 ) SystemFlags : : OutputDebug ( SystemFlags : : debugPerformance , " In [%s::%s Line: %d] took msecs: %lld [after unit->setMeetingPos] \n " , __FILE__ , __FUNCTION__ , __LINE__ , chrono . getMillis ( ) ) ;
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] found nctSetMeetingPoint \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
}
break ;
case nctSwitchTeamVote : {
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] found nctSwitchTeamVote \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
commandWasHandled = true ;
int votingFactionIndex = networkCommand - > getUnitId ( ) ;
int factionIndex = networkCommand - > getCommandTypeId ( ) ;
2012-04-16 20:15:57 +00:00
bool allowSwitchTeam = networkCommand - > getUnitTypeId ( ) ! = 0 ;
2011-10-05 15:56:08 +00:00
Faction * faction = world - > getFaction ( votingFactionIndex ) ;
SwitchTeamVote * vote = faction - > getSwitchTeamVote ( factionIndex ) ;
if ( vote = = NULL ) {
2012-04-14 21:21:09 +00:00
throw megaglest_runtime_error ( " vote == NULL " ) ;
2011-10-05 15:56:08 +00:00
}
vote - > voted = true ;
vote - > allowSwitchTeam = allowSwitchTeam ;
// Join the new team if > 50 % said yes
int newTeamTotalMemberCount = 0 ;
int newTeamVotedYes = 0 ;
int newTeamVotedNo = 0 ;
for ( int i = 0 ; i < world - > getFactionCount ( ) ; + + i ) {
if ( vote - > newTeam = = world - > getFaction ( i ) - > getTeam ( ) ) {
newTeamTotalMemberCount + + ;
SwitchTeamVote * teamVote = world - > getFaction ( i ) - > getSwitchTeamVote ( factionIndex ) ;
if ( teamVote ! = NULL & & teamVote - > voted = = true ) {
if ( teamVote - > allowSwitchTeam = = true ) {
newTeamVotedYes + + ;
}
else {
newTeamVotedNo + + ;
}
}
}
}
// If > 50% of team vote yes, switch th eplayers team
if ( newTeamTotalMemberCount > 0 & & newTeamVotedYes > 0 & &
newTeamVotedYes / newTeamTotalMemberCount > 0.5 ) {
Faction * faction = world - > getFaction ( factionIndex ) ;
int oldTeam = faction - > getTeam ( ) ;
faction - > setTeam ( vote - > newTeam ) ;
GameSettings * settings = world - > getGameSettingsPtr ( ) ;
settings - > setTeam ( factionIndex , vote - > newTeam ) ;
2012-07-19 23:21:55 +00:00
world - > getStats ( ) - > setTeam ( factionIndex , vote - > newTeam ) ;
2011-10-05 15:56:08 +00:00
if ( factionIndex = = world - > getThisFactionIndex ( ) ) {
world - > setThisTeamIndex ( vote - > newTeam ) ;
GameNetworkInterface * gameNetworkInterface = NetworkManager : : getInstance ( ) . getGameNetworkInterface ( ) ;
if ( gameNetworkInterface ! = NULL ) {
Lang & lang = Lang : : getInstance ( ) ;
const vector < string > languageList = settings - > getUniqueNetworkPlayerLanguages ( ) ;
for ( unsigned int i = 0 ; i < languageList . size ( ) ; + + i ) {
2012-10-19 01:31:20 +00:00
char szMsg [ 8096 ] = " " ;
2011-10-05 15:56:08 +00:00
if ( lang . hasString ( " PlayerSwitchedTeam " , languageList [ i ] ) = = true ) {
2012-10-19 01:31:20 +00:00
snprintf ( szMsg , 8096 , lang . get ( " PlayerSwitchedTeam " , languageList [ i ] ) . c_str ( ) , settings - > getNetworkPlayerName ( factionIndex ) . c_str ( ) , oldTeam , vote - > newTeam ) ;
2011-10-05 15:56:08 +00:00
}
else {
2012-10-19 01:31:20 +00:00
snprintf ( szMsg , 8096 , " Player %s switched from team# %d to team# %d. " , settings - > getNetworkPlayerName ( factionIndex ) . c_str ( ) , oldTeam , vote - > newTeam ) ;
2011-10-05 15:56:08 +00:00
}
bool localEcho = lang . isLanguageLocal ( languageList [ i ] ) ;
gameNetworkInterface - > sendTextMessage ( szMsg , - 1 , localEcho , languageList [ i ] ) ;
}
}
}
}
else if ( newTeamTotalMemberCount = = ( newTeamVotedYes + newTeamVotedNo ) ) {
if ( factionIndex = = world - > getThisFactionIndex ( ) ) {
GameSettings * settings = world - > getGameSettingsPtr ( ) ;
Faction * faction = world - > getFaction ( factionIndex ) ;
int oldTeam = faction - > getTeam ( ) ;
GameNetworkInterface * gameNetworkInterface = NetworkManager : : getInstance ( ) . getGameNetworkInterface ( ) ;
if ( gameNetworkInterface ! = NULL ) {
Lang & lang = Lang : : getInstance ( ) ;
const vector < string > languageList = settings - > getUniqueNetworkPlayerLanguages ( ) ;
for ( unsigned int i = 0 ; i < languageList . size ( ) ; + + i ) {
2012-10-19 01:31:20 +00:00
char szMsg [ 8096 ] = " " ;
2011-10-05 15:56:08 +00:00
if ( lang . hasString ( " PlayerSwitchedTeamDenied " , languageList [ i ] ) = = true ) {
2012-10-19 01:31:20 +00:00
snprintf ( szMsg , 8096 , lang . get ( " PlayerSwitchedTeamDenied " , languageList [ i ] ) . c_str ( ) , settings - > getNetworkPlayerName ( factionIndex ) . c_str ( ) , oldTeam , vote - > newTeam ) ;
2011-10-05 15:56:08 +00:00
}
else {
2012-10-19 01:31:20 +00:00
snprintf ( szMsg , 8096 , " Player %s was denied the request to switch from team# %d to team# %d. " , settings - > getNetworkPlayerName ( factionIndex ) . c_str ( ) , oldTeam , vote - > newTeam ) ;
2011-10-05 15:56:08 +00:00
}
bool localEcho = lang . isLanguageLocal ( languageList [ i ] ) ;
gameNetworkInterface - > sendTextMessage ( szMsg , - 1 , localEcho , languageList [ i ] ) ;
}
}
}
}
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugPerformance ) . enabled & & chrono . getMillis ( ) > 0 ) SystemFlags : : OutputDebug ( SystemFlags : : debugPerformance , " In [%s::%s Line: %d] took msecs: %lld [after unit->setMeetingPos] \n " , __FILE__ , __FUNCTION__ , __LINE__ , chrono . getMillis ( ) ) ;
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] found nctSetMeetingPoint \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
}
break ;
2012-09-21 05:45:09 +00:00
case nctDisconnectNetworkPlayer : {
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] found nctDisconnectNetworkPlayer \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
commandWasHandled = true ;
NetworkManager & networkManager = NetworkManager : : getInstance ( ) ;
NetworkRole role = networkManager . getNetworkRole ( ) ;
2012-09-22 20:37:42 +00:00
//GameSettings *settings = world->getGameSettingsPtr();
2012-09-21 05:45:09 +00:00
if ( role = = nrServer ) {
2012-09-22 20:37:42 +00:00
//int factionIndex = networkCommand->getUnitId();
2012-09-21 05:45:09 +00:00
int playerIndex = networkCommand - > getCommandTypeId ( ) ;
GameNetworkInterface * gameNetworkInterface = NetworkManager : : getInstance ( ) . getGameNetworkInterface ( ) ;
if ( gameNetworkInterface ! = NULL ) {
ServerInterface * server = networkManager . getServerInterface ( ) ;
if ( server - > isClientConnected ( playerIndex ) = = true ) {
ConnectionSlot * slot = server - > getSlot ( playerIndex ) ;
2012-09-22 05:26:58 +00:00
if ( slot ! = NULL ) {
NetworkMessageQuit networkMessageQuit ;
slot - > sendMessage ( & networkMessageQuit ) ;
sleep ( 5 ) ;
slot - > close ( ) ;
}
2012-09-21 05:45:09 +00:00
}
}
}
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] found nctDisconnectNetworkPlayer \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
}
break ;
2012-06-09 15:21:18 +00:00
case nctPauseResume :
{
2011-10-05 15:56:08 +00:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] found nctPauseResume \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
commandWasHandled = true ;
2012-04-16 20:15:57 +00:00
bool pauseGame = networkCommand - > getUnitId ( ) ! = 0 ;
2011-10-05 15:56:08 +00:00
Game * game = this - > world - > getGame ( ) ;
//printf("nctPauseResume pauseGame = %d\n",pauseGame);
game - > setPaused ( pauseGame , true ) ;
2012-06-09 15:21:18 +00:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] found nctPauseResume \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
}
2011-10-05 15:56:08 +00:00
break ;
2012-06-09 15:21:18 +00:00
case nctPlayerStatusChange :
{
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] found nctPlayerStatusChange \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
commandWasHandled = true ;
int factionIndex = networkCommand - > getUnitId ( ) ;
int playerStatus = networkCommand - > getCommandTypeId ( ) ;
2012-09-21 15:03:13 +00:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " nctPlayerStatusChange factionIndex = %d playerStatus = %d \n " , factionIndex , playerStatus ) ;
2012-10-10 00:27:34 +00:00
//printf("#1 nctPlayerStatusChange factionIndex = %d playerStatus = %d\n",factionIndex,playerStatus);
2012-06-11 23:50:30 +00:00
GameSettings * settings = world - > getGameSettingsPtr ( ) ;
2012-09-21 15:03:13 +00:00
if ( playerStatus = = npst_Disconnected ) {
settings - > setNetworkPlayerStatuses ( factionIndex , npst_Disconnected ) ;
//printf("nctPlayerStatusChange -> faction->getPersonalityType() = %d index [%d] control [%d] networkstatus [%d]\n",
// world->getFaction(factionIndex)->getPersonalityType(),world->getFaction(factionIndex)->getIndex(),world->getFaction(factionIndex)->getControlType(),settings->getNetworkPlayerStatuses(factionIndex));
2012-10-10 00:27:34 +00:00
//printf("#2 nctPlayerStatusChange factionIndex = %d playerStatus = %d\n",factionIndex,playerStatus);
2012-10-09 23:18:07 +00:00
settings - > setFactionControl ( factionIndex , ctCpuUltra ) ;
settings - > setResourceMultiplierIndex ( factionIndex , settings - > getFallbackCpuMultiplier ( ) ) ;
2012-10-10 00:27:34 +00:00
//Game *game = this->world->getGame();
//game->get
Faction * faction = this - > world - > getFaction ( factionIndex ) ;
faction - > setControlType ( ctCpuUltra ) ;
2012-10-09 23:18:07 +00:00
2012-07-18 23:52:44 +00:00
if ( ! world - > getGame ( ) - > getGameOver ( ) & & ! this - > world - > getGame ( ) - > factionLostGame ( factionIndex ) ) {
2012-09-18 23:36:51 +00:00
// use the fallback multiplier here
2012-10-09 23:18:07 +00:00
2012-09-18 23:36:51 +00:00
// mark player as "leaver"
2012-07-18 23:52:44 +00:00
this - > world - > getStats ( ) - > setPlayerLeftBeforeEnd ( factionIndex , true ) ;
2012-09-18 23:36:51 +00:00
// set disconnect time for endgame stats
2012-07-20 22:24:27 +00:00
this - > world - > getStats ( ) - > setTimePlayerLeft ( factionIndex , this - > world - > getStats ( ) - > getFramesToCalculatePlaytime ( ) ) ;
2012-07-18 23:52:44 +00:00
}
2012-06-09 15:21:18 +00:00
}
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] found nctPlayerStatusChange \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
}
break ;
2011-10-05 15:56:08 +00:00
}
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
*/
2011-10-05 15:56:08 +00:00
if ( commandWasHandled = = false ) {
2010-03-18 21:26:40 +00:00
Unit * unit = world - > findUnitById ( networkCommand - > getUnitId ( ) ) ;
2011-03-28 03:54:23 +00:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugPerformance ) . enabled & & 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-11-11 08:02:50 +00:00
2012-03-20 04:53:26 +00:00
if ( SystemFlags : : VERBOSE_MODE_ENABLED ) printf ( " Running command NetworkCommandType = %d, unitid = %d [%p] factionindex = %d \n " , networkCommand - > getNetworkCommandType ( ) , networkCommand - > getUnitId ( ) , unit , ( unit ! = NULL ? unit - > getFactionIndex ( ) : - 1 ) ) ;
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
2011-03-28 03:54:23 +00:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) 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
2011-03-28 03:54:23 +00:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugPerformance ) . enabled & & chrono . getMillis ( ) > 0 ) SystemFlags : : OutputDebug ( SystemFlags : : debugPerformance , " In [%s::%s Line: %d] took msecs: %lld [after buildCommand] \n " , __FILE__ , __FUNCTION__ , __LINE__ , chrono . getMillis ( ) ) ;
2010-11-11 08:02:50 +00:00
2011-03-28 03:54:23 +00:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) 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
2011-03-28 03:54:23 +00:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugPerformance ) . enabled & & 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-11-11 08:02:50 +00:00
2011-03-28 03:54:23 +00:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) 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 : {
2011-03-28 03:54:23 +00:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] found nctCancelCommand \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-10-26 06:43:42 +00:00
unit - > cancelCommand ( ) ;
2011-03-28 03:54:23 +00:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugPerformance ) . enabled & & 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-11-11 08:02:50 +00:00
2011-03-28 03:54:23 +00:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) 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 : {
2011-03-28 03:54:23 +00:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) 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
2011-03-28 03:54:23 +00:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugPerformance ) . enabled & & 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-11-11 08:02:50 +00:00
2011-09-21 06:51:28 +00:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] found nctSetMeetingPoint \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
}
break ;
2010-03-18 21:26:40 +00:00
default :
assert ( false ) ;
2012-09-22 20:13:57 +00:00
break ;
2010-03-18 21:26:40 +00:00
}
}
else {
2011-03-28 03:54:23 +00:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) 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
2011-03-28 03:54:23 +00:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugPerformance ) . enabled & & 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 ) ;
2011-03-28 03:54:23 +00:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) 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 ) {
2012-10-19 01:31:20 +00:00
char szBuf [ 8096 ] = " " ;
snprintf ( szBuf , 8096 , " In [%s::%s Line: %d] world == NULL for unit with id: %d " , __FILE__ , __FUNCTION__ , __LINE__ , networkCommand - > getUnitId ( ) ) ;
2012-04-14 21:21:09 +00:00
throw megaglest_runtime_error ( szBuf ) ;
2010-04-20 02:19:37 +00:00
}
2010-03-18 21:26:40 +00:00
Unit * target = NULL ;
const CommandType * ct = NULL ;
const Unit * unit = world - > findUnitById ( networkCommand - > getUnitId ( ) ) ;
//validate unit
if ( unit = = NULL ) {
2012-10-19 01:31:20 +00:00
char szBuf [ 8096 ] = " " ;
snprintf ( szBuf , 8096 , " In [%s::%s Line: %d] Can not find unit with id: %d. Game out of synch. " , __FILE__ , __FUNCTION__ , __LINE__ , networkCommand - > getUnitId ( ) ) ;
2012-03-20 04:53:26 +00:00
SystemFlags : : OutputDebug ( SystemFlags : : debugError , " %s \n " , szBuf ) ;
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " %s \n " , szBuf ) ;
2011-01-02 01:48:56 +00:00
GameNetworkInterface * gameNetworkInterface = NetworkManager : : getInstance ( ) . getGameNetworkInterface ( ) ;
if ( gameNetworkInterface ! = NULL ) {
2012-10-19 01:31:20 +00:00
char szMsg [ 8096 ] = " " ;
snprintf ( szMsg , 8096 , " Player detected an error: Can not find unit with id: %d. Game out of synch. " , networkCommand - > getUnitId ( ) ) ;
2011-04-05 18:39:47 +00:00
gameNetworkInterface - > sendTextMessage ( szMsg , - 1 , true , " " ) ;
2011-01-02 01:48:56 +00:00
}
2012-04-14 21:21:09 +00:00
throw megaglest_runtime_error ( szBuf ) ;
2010-03-18 21:26:40 +00:00
}
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 ( ) ) {
2012-03-20 04:53:26 +00:00
2012-10-19 01:31:20 +00:00
char szBuf [ 8096 ] = " " ;
snprintf ( szBuf , 8096 , " 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. " ,
2010-05-29 08:23:52 +00:00
__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 ) ;
2011-03-28 03:54:23 +00:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " %s \n " , szBuf ) ;
2011-08-31 23:10:43 +00:00
//std::string worldLog = world->DumpWorldToLog();
world - > DumpWorldToLog ( ) ;
2011-01-02 01:48:56 +00:00
GameNetworkInterface * gameNetworkInterface = NetworkManager : : getInstance ( ) . getGameNetworkInterface ( ) ;
2011-01-25 00:54:37 +00:00
if ( gameNetworkInterface ! = NULL & & gameNetworkInterface - > isConnected ( ) = = true ) {
2012-10-19 01:31:20 +00:00
char szMsg [ 8096 ] = " " ;
snprintf ( szMsg , 8096 , " Player detected an error: Unit / Faction mismatch for unitId: %d " , networkCommand - > getUnitId ( ) ) ;
2011-04-05 18:39:47 +00:00
gameNetworkInterface - > sendTextMessage ( szMsg , - 1 , true , " " ) ;
2012-10-19 01:31:20 +00:00
snprintf ( szMsg , 8096 , " Local faction index = %d, remote index = %d. Game out of synch. " , unit - > getFaction ( ) - > getIndex ( ) , networkCommand - > getUnitFactionIndex ( ) ) ;
2011-04-05 18:39:47 +00:00
gameNetworkInterface - > sendTextMessage ( szMsg , - 1 , true , " " ) ;
2011-01-31 23:36:13 +00:00
2011-01-25 00:54:37 +00:00
}
2011-08-31 19:44:19 +00:00
else if ( gameNetworkInterface ! = NULL ) {
2012-10-19 01:31:20 +00:00
char szMsg [ 8096 ] = " " ;
snprintf ( szMsg , 8096 , " Player detected an error: Connection lost, possible Unit / Faction mismatch for unitId: %d " , networkCommand - > getUnitId ( ) ) ;
2011-04-05 18:39:47 +00:00
gameNetworkInterface - > sendTextMessage ( szMsg , - 1 , true , " " ) ;
2012-10-19 01:31:20 +00:00
snprintf ( szMsg , 8096 , " Local faction index = %d, remote index = %d. Game out of synch. " , unit - > getFaction ( ) - > getIndex ( ) , networkCommand - > getUnitFactionIndex ( ) ) ;
2011-04-05 18:39:47 +00:00
gameNetworkInterface - > sendTextMessage ( szMsg , - 1 , true , " " ) ;
2011-01-02 01:48:56 +00:00
}
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. " ;
2012-04-14 21:21:09 +00:00
throw megaglest_runtime_error ( sError ) ;
2011-01-02 00:39:13 +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!
2012-04-14 21:21:09 +00:00
//throw megaglest_runtime_error("Test missing command type!");
2010-05-29 07:58:58 +00:00
2010-03-18 21:26:40 +00:00
//validate command type
2012-10-02 06:20:39 +00:00
// !!!Test out of synch behaviour
//ct = NULL;
2010-03-18 21:26:40 +00:00
if ( ct = = NULL ) {
2012-10-19 01:31:20 +00:00
char szBuf [ 8096 ] = " " ;
snprintf ( szBuf , 8096 , " 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 Unit Type Info: \n [%s] \n Network unit type: \n [%s] \n Game out of synch. " ,
2012-03-20 04:53:26 +00:00
__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 ( ) - > toString ( ) . c_str ( ) ,
( unitType ! = NULL ? unitType - > getName ( ) . c_str ( ) : " null " ) ) ;
2010-03-18 21:26:40 +00:00
2010-05-28 00:26:29 +00:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " %s \n " , szBuf ) ;
2012-03-20 04:53:26 +00:00
SystemFlags : : OutputDebug ( SystemFlags : : debugError , " %s \n " , szBuf ) ;
2011-08-31 23:10:43 +00:00
//std::string worldLog = world->DumpWorldToLog();
world - > DumpWorldToLog ( ) ;
2011-01-02 01:48:56 +00:00
GameNetworkInterface * gameNetworkInterface = NetworkManager : : getInstance ( ) . getGameNetworkInterface ( ) ;
if ( gameNetworkInterface ! = NULL ) {
2012-10-19 01:31:20 +00:00
char szMsg [ 8096 ] = " " ;
snprintf ( szMsg , 8096 , " Player detected an error: Can not find command type: %d for unitId: %d [%s]. Game out of synch. " , networkCommand - > getCommandTypeId ( ) , networkCommand - > getUnitId ( ) , ( unitType ! = NULL ? unitType - > getName ( ) . c_str ( ) : " null " ) ) ;
2011-04-05 18:39:47 +00:00
gameNetworkInterface - > sendTextMessage ( szMsg , - 1 , true , " " ) ;
2011-01-02 01:48:56 +00:00
}
std : : string sError = " Error [#3]: Game is out of sync, please check log files for details. " ;
2012-03-20 07:14:50 +00:00
//abort();
2012-04-14 21:21:09 +00:00
throw megaglest_runtime_error ( sError ) ;
2010-03-18 21:26:40 +00:00
}
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 ) {
2011-04-18 04:28:25 +00:00
//assert(networkCommand->getTargetId() >= 0 && networkCommand->getTargetId() < 4);
if ( networkCommand - > getTargetId ( ) < 0 | | networkCommand - > getTargetId ( ) > = 4 ) {
2012-10-19 01:31:20 +00:00
char szBuf [ 8096 ] = " " ;
snprintf ( szBuf , 8096 , " networkCommand->getTargetId() >= 0 && networkCommand->getTargetId() < 4, [%s] " , networkCommand - > toString ( ) . c_str ( ) ) ;
2012-04-14 21:21:09 +00:00
throw megaglest_runtime_error ( szBuf ) ;
2011-04-18 04:28:25 +00:00
}
2010-03-25 12:15:10 +00:00
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 ( ) ) ;
}
//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 ) ;
2011-07-05 04:37:35 +00:00
command - > setUnitCommandGroupId ( networkCommand - > getUnitCommandGroupId ( ) ) ;
2010-08-30 20:45:12 +00:00
2011-03-28 03:54:23 +00:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-05-28 00:57:24 +00:00
2010-03-18 21:26:40 +00:00
//issue command
return command ;
}
} } //end namespace