2010-03-18 22:26:40 +01:00
// ==============================================================
// This file is part of Glest (www.glest.org)
//
2010-04-27 05:36:36 +02:00
// Copyright (C) 2001-2008 Martio Figueroa
2010-03-18 22:26:40 +01:00
//
// You can redistribute this code and/or modify it under
// the terms of the GNU General Public License as published
// by the Free Software Foundation; either version 2 of the
// License, or (at your option) any later version
// ==============================================================
# include "network_types.h"
2010-03-22 05:47:16 +01:00
# include "util.h"
# include "unit.h"
# include "world.h"
# include "unit_type.h"
# include "game.h"
# include "gui.h"
2010-03-18 22:26:40 +01:00
# include "leak_dumper.h"
2010-03-22 05:47:16 +01:00
using namespace Shared : : Util ;
2010-03-18 22:26:40 +01:00
namespace Glest { namespace Game {
// =====================================================
// class NetworkCommand
// =====================================================
2010-08-30 22:45:12 +02:00
NetworkCommand : : NetworkCommand ( World * world , int networkCommandType , int unitId ,
int commandTypeId , const Vec2i & pos , int unitTypeId ,
int targetId , int facing , bool wantQueue ,
CommandStateType commandStateType ,
int commandStateValue )
2010-03-25 13:15:10 +01:00
: networkCommandType ( networkCommandType )
, unitId ( unitId )
, commandTypeId ( commandTypeId )
, positionX ( pos . x )
, positionY ( pos . y )
2010-04-18 09:35:48 +02:00
, unitTypeId ( unitTypeId )
2010-08-30 22:45:12 +02:00
, wantQueue ( wantQueue )
, commandStateType ( commandStateType )
, commandStateValue ( commandStateValue ) {
2010-03-25 13:15:10 +01:00
assert ( targetId = = - 1 | | facing = = - 1 ) ;
this - > targetId = targetId > = 0 ? targetId : facing ;
2010-05-15 20:59:17 +02:00
this - > fromFactionIndex = world - > getThisFactionIndex ( ) ;
2010-03-22 05:47:16 +01:00
if ( this - > networkCommandType = = nctGiveCommand ) {
const Unit * unit = world - > findUnitById ( this - > unitId ) ;
//validate unit
if ( unit ! = NULL ) {
2010-05-28 16:59:09 +02:00
this - > unitFactionIndex = unit - > getFaction ( ) - > getIndex ( ) ;
this - > unitFactionUnitCount = unit - > getFaction ( ) - > getUnitCount ( ) ;
2011-01-02 01:39:13 +01:00
//const UnitType *unitType= world->findUnitTypeById(unit->getFaction()->getType(), this->unitTypeId);
2010-03-22 05:47:16 +01:00
const CommandType * ct = unit - > getType ( ) - > findCommandTypeById ( this - > commandTypeId ) ;
if ( ct ! = NULL & & ct - > getClass ( ) = = ccBuild ) {
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugNetwork ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] %s \n " , __FILE__ , __FUNCTION__ , __LINE__ , toString ( ) . c_str ( ) ) ;
2010-04-27 05:36:36 +02:00
CardinalDir : : assertDirValid ( facing ) ;
assert ( targetId = = - 1 ) ;
2010-03-22 05:47:16 +01:00
}
}
}
2011-01-02 01:39:13 +01:00
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugNetwork ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] Created NetworkCommand as follows: \n %s \n " , __FILE__ , __FUNCTION__ , __LINE__ , toString ( ) . c_str ( ) ) ;
2010-03-22 05:47:16 +01:00
}
void NetworkCommand : : preprocessNetworkCommand ( World * world ) {
if ( networkCommandType = = nctGiveCommand ) {
const Unit * unit = world - > findUnitById ( unitId ) ;
//validate unit
if ( unit ! = NULL ) {
2011-01-02 01:39:13 +01:00
//const UnitType *unitType= world->findUnitTypeById(unit->getFaction()->getType(), unitTypeId);
2010-03-22 05:47:16 +01:00
const CommandType * ct = unit - > getType ( ) - > findCommandTypeById ( commandTypeId ) ;
if ( ct ! = NULL & & ct - > getClass ( ) = = ccBuild & & targetId > = 0 ) {
2010-04-27 05:36:36 +02:00
CardinalDir : : assertDirValid ( targetId ) ;
2010-03-22 05:47:16 +01:00
}
}
else {
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugNetwork ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] (unit == NULL) %s \n " , __FILE__ , __FUNCTION__ , __LINE__ , toString ( ) . c_str ( ) ) ;
2010-03-22 05:47:16 +01:00
}
}
}
2010-08-30 22:45:12 +02:00
2010-03-22 05:47:16 +01:00
string NetworkCommand : : toString ( ) const {
char szBuf [ 1024 ] = " " ;
2010-08-30 22:45:12 +02:00
sprintf ( szBuf , " networkCommandType = %d \n unitId = %d \n commandTypeId = %d \n positionX = %d \n positionY = %d \n unitTypeId = %d \n targetId = %d \n wantQueue= %d \n fromFactionIndex = %d \n unitFactionUnitCount = %d \n unitFactionIndex = %d, commandStateType = %d, commandStateValue = %d " ,
networkCommandType , unitId , commandTypeId , positionX , positionY , unitTypeId , targetId , wantQueue ,
fromFactionIndex , unitFactionUnitCount , unitFactionIndex , commandStateType , commandStateValue ) ;
2010-03-22 05:47:16 +01:00
string result = szBuf ;
return result ;
2010-03-18 22:26:40 +01:00
}
} } //end namespace