From e32eb9c162d4fca12980ef42bdc93710ec30a44d Mon Sep 17 00:00:00 2001 From: Mark Vejvoda Date: Mon, 30 Aug 2010 20:45:12 +0000 Subject: [PATCH] - modified command object to support special states which flow through network play as well. This allows for better handling of multi-unit build and allows for better future expanding of command processing. --- source/glest_game/game/commander.cpp | 26 ++++++++++++++----- source/glest_game/network/network_message.cpp | 4 +-- source/glest_game/network/network_types.cpp | 17 +++++++++--- source/glest_game/network/network_types.h | 12 ++++++--- source/glest_game/type_instances/command.cpp | 10 ++++++- source/glest_game/type_instances/command.h | 17 +++++++++++- source/glest_game/types/command_type.h | 7 ++++- source/glest_game/world/unit_updater.cpp | 7 ++--- 8 files changed, 79 insertions(+), 21 deletions(-) diff --git a/source/glest_game/game/commander.cpp b/source/glest_game/game/commander.cpp index 99f7e071..c090de8a 100644 --- a/source/glest_game/game/commander.cpp +++ b/source/glest_game/game/commander.cpp @@ -52,6 +52,10 @@ CommandResult Commander::tryGiveCommand(const Selection *selection, const Comman refPos= computeRefPos(selection); + int builderUnitId = -1; + CommandStateType commandStateType = cst_None; + int commandStateValue = -1; + bool unitSignalledToBuild = false; //give orders to all selected units for(int i=0; igetCount(); ++i) { @@ -64,20 +68,23 @@ CommandResult Commander::tryGiveCommand(const Selection *selection, const Comman if(dynamic_cast(commandType) != NULL) { usePos = pos; if(unitSignalledToBuild == false) { + builderUnitId = unitId; unitSignalledToBuild = true; } else { - useCommandtype = unit->getType()->getFirstRepairCommand(unitType); - tryQueue = true; + useCommandtype = unit->getType()->getFirstRepairCommand(unitType); + commandStateType = cst_linkedUnit; + commandStateValue = builderUnitId; + //tryQueue = true; } } NetworkCommand networkCommand(this->world,nctGiveCommand, unitId, useCommandtype->getId(), usePos, unitType->getId(), (targetUnit != NULL ? targetUnit->getId() : -1), - facing, tryQueue); + facing, tryQueue, commandStateType,commandStateValue); - //every unit is ordered to a different position + //every unit is ordered to a the position CommandResult result= pushNetworkCommand(&networkCommand); results.push_back(result); } @@ -290,7 +297,7 @@ CommandResult Commander::computeResult(const CommandResultContainer &results) co } } -CommandResult Commander::pushNetworkCommand(const NetworkCommand* networkCommand) const{ +CommandResult Commander::pushNetworkCommand(const NetworkCommand* networkCommand) const { GameNetworkInterface *gameNetworkInterface= NetworkManager::getInstance().getGameNetworkInterface(); const Unit* unit= world->findUnitById(networkCommand->getUnitId()); CommandResult cr= crSuccess; @@ -432,7 +439,7 @@ void Commander::giveNetworkCommand(NetworkCommand* networkCommand) const { } } -Command* Commander::buildCommand(const NetworkCommand* networkCommand) const{ +Command* Commander::buildCommand(const NetworkCommand* networkCommand) const { assert(networkCommand->getNetworkCommandType()==nctGiveCommand); SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] networkCommand [%s]\n",__FILE__,__FUNCTION__,__LINE__,networkCommand->toString().c_str()); @@ -523,6 +530,13 @@ Command* Commander::buildCommand(const NetworkCommand* networkCommand) const{ command= new Command(ct, target); } + // Add in any special state + CommandStateType commandStateType = networkCommand->getCommandStateType(); + int commandStateValue = networkCommand->getCommandStateValue(); + + command->setStateType(commandStateType); + command->setStateValue(commandStateValue); + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); //issue command diff --git a/source/glest_game/network/network_message.cpp b/source/glest_game/network/network_message.cpp index 9b7542d4..3da8a699 100644 --- a/source/glest_game/network/network_message.cpp +++ b/source/glest_game/network/network_message.cpp @@ -314,7 +314,7 @@ bool NetworkMessageCommandList::addCommand(const NetworkCommand* networkCommand) bool NetworkMessageCommandList::receive(Socket* socket) { // _peek_ type, commandCount & frame num first. - for(int peekAttempt = 1; peekAttempt < 15; peekAttempt++) { + for(int peekAttempt = 1; peekAttempt < 1000; peekAttempt++) { SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] peekAttempt = %d\n",__FILE__,__FUNCTION__,__LINE__,peekAttempt); if (NetworkMessage::peek(socket, &data, commandListHeaderSize) == true) { @@ -337,7 +337,7 @@ bool NetworkMessageCommandList::receive(Socket* socket) { int totalMsgSize = commandListHeaderSize + (sizeof(NetworkCommand) * data.header.commandCount); // _peek_ type, commandCount & frame num first. - for(int peekAttempt = 1; peekAttempt < 15; peekAttempt++) { + for(int peekAttempt = 1; peekAttempt < 1000; peekAttempt++) { SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] peekAttempt = %d\n",__FILE__,__FUNCTION__,__LINE__,peekAttempt); if (NetworkMessage::peek(socket, &data, totalMsgSize) == true) { diff --git a/source/glest_game/network/network_types.cpp b/source/glest_game/network/network_types.cpp index 57874aaa..745da2f1 100644 --- a/source/glest_game/network/network_types.cpp +++ b/source/glest_game/network/network_types.cpp @@ -27,14 +27,21 @@ namespace Glest{ namespace Game{ // class NetworkCommand // ===================================================== -NetworkCommand::NetworkCommand(World *world, int networkCommandType, int unitId, int commandTypeId, const Vec2i &pos, int unitTypeId, int targetId, int facing, bool wantQueue) +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) : networkCommandType(networkCommandType) , unitId(unitId) , commandTypeId(commandTypeId) , positionX(pos.x) , positionY(pos.y) , unitTypeId(unitTypeId) - , wantQueue(wantQueue) { + , wantQueue(wantQueue) + , commandStateType(commandStateType) + , commandStateValue(commandStateValue) { + assert(targetId == -1 || facing == -1); this->targetId = targetId >= 0 ? targetId : facing; this->fromFactionIndex = world->getThisFactionIndex(); @@ -76,10 +83,12 @@ void NetworkCommand::preprocessNetworkCommand(World *world) { } } + string NetworkCommand::toString() const { char szBuf[1024]=""; - sprintf(szBuf,"networkCommandType = %d\nunitId = %d\ncommandTypeId = %d\npositionX = %d\npositionY = %d\nunitTypeId = %d\ntargetId = %d\nwantQueue= %d\nfromFactionIndex = %d\nunitFactionUnitCount = %d\nunitFactionIndex = %d", - networkCommandType,unitId,commandTypeId,positionX,this->positionY,unitTypeId,targetId,wantQueue,fromFactionIndex,unitFactionUnitCount,unitFactionIndex); + sprintf(szBuf,"networkCommandType = %d\nunitId = %d\ncommandTypeId = %d\npositionX = %d\npositionY = %d\nunitTypeId = %d\ntargetId = %d\nwantQueue= %d\nfromFactionIndex = %d\nunitFactionUnitCount = %d\nunitFactionIndex = %d, commandStateType = %d, commandStateValue = %d", + networkCommandType,unitId,commandTypeId,positionX,positionY,unitTypeId,targetId,wantQueue, + fromFactionIndex,unitFactionUnitCount,unitFactionIndex,commandStateType,commandStateValue); string result = szBuf; return result; diff --git a/source/glest_game/network/network_types.h b/source/glest_game/network/network_types.h index b6982c53..59bb9b59 100644 --- a/source/glest_game/network/network_types.h +++ b/source/glest_game/network/network_types.h @@ -16,6 +16,7 @@ #include "types.h" #include "vec.h" +#include "command.h" using std::string; using std::min; @@ -83,6 +84,8 @@ private: int8 fromFactionIndex; uint16 unitFactionUnitCount; int8 unitFactionIndex; + int8 commandStateType; + int32 commandStateValue; public: NetworkCommand(){}; @@ -95,9 +98,9 @@ public: int unitTypeId= -1, int targetId= -1, int facing= -1, - bool wantQueue = false); - - //NetworkCommand(int networkCommandType, NetworkCommandSubType ncstType, int unitId, int value1, int value2=-1); + bool wantQueue = false, + CommandStateType commandStateType = cst_None, + int commandTypeStateValue = -1); NetworkCommandType getNetworkCommandType() const {return static_cast(networkCommandType);} int getUnitId() const {return unitId;} @@ -110,6 +113,9 @@ public: int getUnitFactionUnitCount() const {return unitFactionUnitCount;} int getUnitFactionIndex() const {return unitFactionIndex;} + CommandStateType getCommandStateType() const {return static_cast(commandStateType);} + int getCommandStateValue() const {return commandStateValue;} + void preprocessNetworkCommand(World *world); string toString() const; }; diff --git a/source/glest_game/type_instances/command.cpp b/source/glest_game/type_instances/command.cpp index fa2e534c..51953dd1 100644 --- a/source/glest_game/type_instances/command.cpp +++ b/source/glest_game/type_instances/command.cpp @@ -31,6 +31,8 @@ Command::Command(const CommandType *ct, const Vec2i &pos){ this->commandType= ct; this->pos= pos; unitType= NULL; + stateType = cst_None; + stateValue = -1; } Command::Command(const CommandType *ct, Unit* unit){ @@ -38,10 +40,12 @@ Command::Command(const CommandType *ct, Unit* unit){ this->pos= Vec2i(0); this->unitRef= unit; unitType= NULL; - if(unit!=NULL){ + if(unit!=NULL) { unit->resetHighlight(); pos= unit->getCellPos(); } + stateType = cst_None; + stateValue = -1; } Command::Command(const CommandType *ct, const Vec2i &pos, const UnitType *unitType, CardinalDir facing){ @@ -49,6 +53,8 @@ Command::Command(const CommandType *ct, const Vec2i &pos, const UnitType *unitTy this->pos= pos; this->unitType= unitType; this->facing = facing; + stateType = cst_None; + stateValue = -1; if(this->unitType != NULL) { SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] unitType = [%s]\n",__FILE__,__FUNCTION__,__LINE__,this->unitType->toString().c_str()); @@ -97,6 +103,8 @@ std::string Command::toString() const { //SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__, __LINE__); + result = ", stateType = " + intToStr(stateType) + ", stateValue = " + intToStr(stateValue); + return result; } diff --git a/source/glest_game/type_instances/command.h b/source/glest_game/type_instances/command.h index 268de2e0..4746245c 100644 --- a/source/glest_game/type_instances/command.h +++ b/source/glest_game/type_instances/command.h @@ -24,13 +24,18 @@ using Shared::Graphics::Vec2i; class CommandType; +enum CommandStateType { + cst_None, + cst_linkedUnit +}; + // ===================================================== // class Command // /// A unit command // ===================================================== -class Command{ +class Command { private: const CommandType *commandType; Vec2i pos; @@ -38,6 +43,9 @@ private: CardinalDir facing; // facing, for build command const UnitType *unitType; //used for build + CommandStateType stateType; + int stateValue; + public: //constructor Command(const CommandType *ct, const Vec2i &pos=Vec2i(0)); @@ -56,6 +64,13 @@ public: void setPos(const Vec2i &pos); void setUnit(Unit *unit); + void setStateType(CommandStateType value) { stateType = value; } + CommandStateType getStateType() const { return stateType; } + + void setStateValue(int value) { stateValue = value; } + int getStateValue() const { return stateValue; } + + std::string toString() const; }; diff --git a/source/glest_game/types/command_type.h b/source/glest_game/types/command_type.h index c314528e..1d3c7041 100644 --- a/source/glest_game/types/command_type.h +++ b/source/glest_game/types/command_type.h @@ -64,7 +64,7 @@ enum Queueability { /// A complex action performed by a unit, composed by skills // ===================================================== -class CommandType: public RequirableType{ +class CommandType: public RequirableType { protected: CommandClass commandTypeClass; Clicks clicks; @@ -74,6 +74,11 @@ public: static const int invalidId= -1; public: + CommandType() { + commandTypeClass = ccNull; + clicks = cOne; + id = -1; + } virtual void update(UnitUpdater *unitUpdater, Unit *unit) const= 0; virtual void load(int id, const XmlNode *n, const string &dir, const TechTree *tt, const FactionType *ft, const UnitType &ut); virtual string getDesc(const TotalUpgrade *totalUpgrade) const= 0; diff --git a/source/glest_game/world/unit_updater.cpp b/source/glest_game/world/unit_updater.cpp index 02a8b40c..5c88d880 100644 --- a/source/glest_game/world/unit_updater.cpp +++ b/source/glest_game/world/unit_updater.cpp @@ -753,10 +753,10 @@ Unit * UnitUpdater::findPeerUnitBuilder(Unit *unit) { Unit *foundUnitBuilder = NULL; if(unit->getCommandSize() > 0 ) { - Command *command= unit->getCurrCommand(); + Command *command = unit->getCurrCommand(); if(command != NULL) { const RepairCommandType *rct= dynamic_cast(command->getCommandType()); - if(rct) { + if(rct != NULL && command->getStateType() == cst_linkedUnit) { SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); for(int i = 0; i < unit->getFaction()->getUnitCount(); ++i) { @@ -770,7 +770,8 @@ Unit * UnitUpdater::findPeerUnitBuilder(Unit *unit) { if(bct != NULL) { SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); - if(command->getPos() == peerCommand->getPos()) { + //if(command->getPos() == peerCommand->getPos()) { + if(command->getStateValue() == peerUnit->getId()) { SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); foundUnitBuilder = peerUnit;