2010-03-18 22:26:40 +01:00
// ==============================================================
// This file is part of Glest (www.glest.org)
//
2011-12-14 08:40:48 +01:00
// Copyright (C) 2001-2008 Martiño Figueroa
2010-03-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 ,
2011-07-05 06:37:35 +02:00
int commandStateValue , int unitCommandGroupId )
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 )
2011-07-05 06:37:35 +02:00
, commandStateValue ( commandStateValue )
, unitCommandGroupId ( unitCommandGroupId ) {
2010-08-30 22:45:12 +02:00
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 {
2012-10-19 03:31:20 +02:00
char szBuf [ 8096 ] = " " ;
snprintf ( szBuf , 8096 , " 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, unitCommandGroupId = %d " ,
2010-08-30 22:45:12 +02:00
networkCommandType , unitId , commandTypeId , positionX , positionY , unitTypeId , targetId , wantQueue ,
2011-07-05 06:37:35 +02:00
fromFactionIndex , unitFactionUnitCount , unitFactionIndex , commandStateType , commandStateValue ,
unitCommandGroupId ) ;
2010-03-22 05:47:16 +01:00
string result = szBuf ;
return result ;
2010-03-18 22:26:40 +01:00
}
2012-11-01 01:06:23 +01:00
void NetworkCommand : : toEndian ( ) {
networkCommandType = Shared : : PlatformByteOrder : : toCommonEndian ( networkCommandType ) ;
unitId = Shared : : PlatformByteOrder : : toCommonEndian ( unitId ) ;
unitTypeId = Shared : : PlatformByteOrder : : toCommonEndian ( unitTypeId ) ;
commandTypeId = Shared : : PlatformByteOrder : : toCommonEndian ( commandTypeId ) ;
positionX = Shared : : PlatformByteOrder : : toCommonEndian ( positionX ) ;
positionY = Shared : : PlatformByteOrder : : toCommonEndian ( positionY ) ;
targetId = Shared : : PlatformByteOrder : : toCommonEndian ( targetId ) ;
wantQueue = Shared : : PlatformByteOrder : : toCommonEndian ( wantQueue ) ;
fromFactionIndex = Shared : : PlatformByteOrder : : toCommonEndian ( fromFactionIndex ) ;
unitFactionUnitCount = Shared : : PlatformByteOrder : : toCommonEndian ( unitFactionUnitCount ) ;
unitFactionIndex = Shared : : PlatformByteOrder : : toCommonEndian ( unitFactionIndex ) ;
commandStateType = Shared : : PlatformByteOrder : : toCommonEndian ( commandStateType ) ;
commandStateValue = Shared : : PlatformByteOrder : : toCommonEndian ( commandStateValue ) ;
unitCommandGroupId = Shared : : PlatformByteOrder : : toCommonEndian ( unitCommandGroupId ) ;
}
void NetworkCommand : : fromEndian ( ) {
networkCommandType = Shared : : PlatformByteOrder : : fromCommonEndian ( networkCommandType ) ;
unitId = Shared : : PlatformByteOrder : : fromCommonEndian ( unitId ) ;
unitTypeId = Shared : : PlatformByteOrder : : fromCommonEndian ( unitTypeId ) ;
commandTypeId = Shared : : PlatformByteOrder : : fromCommonEndian ( commandTypeId ) ;
positionX = Shared : : PlatformByteOrder : : fromCommonEndian ( positionX ) ;
positionY = Shared : : PlatformByteOrder : : fromCommonEndian ( positionY ) ;
targetId = Shared : : PlatformByteOrder : : fromCommonEndian ( targetId ) ;
wantQueue = Shared : : PlatformByteOrder : : fromCommonEndian ( wantQueue ) ;
fromFactionIndex = Shared : : PlatformByteOrder : : fromCommonEndian ( fromFactionIndex ) ;
unitFactionUnitCount = Shared : : PlatformByteOrder : : fromCommonEndian ( unitFactionUnitCount ) ;
unitFactionIndex = Shared : : PlatformByteOrder : : fromCommonEndian ( unitFactionIndex ) ;
commandStateType = Shared : : PlatformByteOrder : : fromCommonEndian ( commandStateType ) ;
commandStateValue = Shared : : PlatformByteOrder : : fromCommonEndian ( commandStateValue ) ;
unitCommandGroupId = Shared : : PlatformByteOrder : : fromCommonEndian ( unitCommandGroupId ) ;
}
2012-03-20 05:53:26 +01:00
XmlNode * NetworkCommand : : saveGame ( XmlNode * rootNode ) {
2012-03-10 04:27:25 +01:00
std : : map < string , string > mapTagReplacements ;
XmlNode * networkCommandNode = rootNode - > addChild ( " NetworkCommand " ) ;
// int16 networkCommandType;
networkCommandNode - > addAttribute ( " networkCommandType " , intToStr ( networkCommandType ) , mapTagReplacements ) ;
// int32 unitId;
networkCommandNode - > addAttribute ( " unitId " , intToStr ( unitId ) , mapTagReplacements ) ;
// int16 unitTypeId;
networkCommandNode - > addAttribute ( " unitTypeId " , intToStr ( unitTypeId ) , mapTagReplacements ) ;
// int16 commandTypeId;
networkCommandNode - > addAttribute ( " commandTypeId " , intToStr ( commandTypeId ) , mapTagReplacements ) ;
// int16 positionX;
networkCommandNode - > addAttribute ( " positionX " , intToStr ( positionX ) , mapTagReplacements ) ;
// int16 positionY;
networkCommandNode - > addAttribute ( " positionY " , intToStr ( positionY ) , mapTagReplacements ) ;
// int32 targetId;
networkCommandNode - > addAttribute ( " targetId " , intToStr ( targetId ) , mapTagReplacements ) ;
// int8 wantQueue;
networkCommandNode - > addAttribute ( " wantQueue " , intToStr ( wantQueue ) , mapTagReplacements ) ;
// int8 fromFactionIndex;
networkCommandNode - > addAttribute ( " fromFactionIndex " , intToStr ( fromFactionIndex ) , mapTagReplacements ) ;
// uint16 unitFactionUnitCount;
networkCommandNode - > addAttribute ( " unitFactionUnitCount " , intToStr ( unitFactionUnitCount ) , mapTagReplacements ) ;
// int8 unitFactionIndex;
networkCommandNode - > addAttribute ( " unitFactionIndex " , intToStr ( unitFactionIndex ) , mapTagReplacements ) ;
// int8 commandStateType;
networkCommandNode - > addAttribute ( " commandStateType " , intToStr ( commandStateType ) , mapTagReplacements ) ;
// int32 commandStateValue;
networkCommandNode - > addAttribute ( " commandStateValue " , intToStr ( commandStateValue ) , mapTagReplacements ) ;
// int32 unitCommandGroupId;
networkCommandNode - > addAttribute ( " unitCommandGroupId " , intToStr ( unitCommandGroupId ) , mapTagReplacements ) ;
2012-03-20 05:53:26 +01:00
return networkCommandNode ;
2012-03-10 04:27:25 +01:00
}
2012-03-20 05:53:26 +01:00
void NetworkCommand : : loadGame ( const XmlNode * rootNode ) {
const XmlNode * networkCommandNode = rootNode ;
// int16 networkCommandType;
networkCommandType = networkCommandNode - > getAttribute ( " networkCommandType " ) - > getIntValue ( ) ;
// int32 unitId;
unitId = networkCommandNode - > getAttribute ( " unitId " ) - > getIntValue ( ) ;
// int16 unitTypeId;
unitTypeId = networkCommandNode - > getAttribute ( " unitTypeId " ) - > getIntValue ( ) ;
// int16 commandTypeId;
commandTypeId = networkCommandNode - > getAttribute ( " commandTypeId " ) - > getIntValue ( ) ;
// int16 positionX;
positionX = networkCommandNode - > getAttribute ( " positionX " ) - > getIntValue ( ) ;
// int16 positionY;
positionY = networkCommandNode - > getAttribute ( " positionY " ) - > getIntValue ( ) ;
// int32 targetId;
targetId = networkCommandNode - > getAttribute ( " targetId " ) - > getIntValue ( ) ;
// int8 wantQueue;
wantQueue = networkCommandNode - > getAttribute ( " wantQueue " ) - > getIntValue ( ) ;
// int8 fromFactionIndex;
fromFactionIndex = networkCommandNode - > getAttribute ( " fromFactionIndex " ) - > getIntValue ( ) ;
// uint16 unitFactionUnitCount;
unitFactionUnitCount = networkCommandNode - > getAttribute ( " unitFactionUnitCount " ) - > getIntValue ( ) ;
// int8 unitFactionIndex;
unitFactionIndex = networkCommandNode - > getAttribute ( " unitFactionIndex " ) - > getIntValue ( ) ;
// int8 commandStateType;
commandStateType = networkCommandNode - > getAttribute ( " commandStateType " ) - > getIntValue ( ) ;
// int32 commandStateValue;
commandStateValue = networkCommandNode - > getAttribute ( " commandStateValue " ) - > getIntValue ( ) ;
// int32 unitCommandGroupId;
unitCommandGroupId = networkCommandNode - > getAttribute ( " unitCommandGroupId " ) - > getIntValue ( ) ;
}
2010-03-18 22:26:40 +01:00
} } //end namespace