diff --git a/source/glest_game/game/commander.cpp b/source/glest_game/game/commander.cpp index 7569aae5f..62f005c35 100644 --- a/source/glest_game/game/commander.cpp +++ b/source/glest_game/game/commander.cpp @@ -41,7 +41,7 @@ void Commander::init(World *world){ } CommandResult Commander::tryGiveCommand(const Unit* unit, const CommandType *commandType, const Vec2i &pos, const UnitType* unitType) const{ - NetworkCommand networkCommand(nctGiveCommand, unit->getId(), commandType->getId(), pos, unitType->getId()); + NetworkCommand networkCommand(this->world,nctGiveCommand, unit->getId(), commandType->getId(), pos, unitType->getId()); return pushNetworkCommand(&networkCommand); } @@ -61,7 +61,7 @@ CommandResult Commander::tryGiveCommand(const Selection *selection, CommandClass int targetId= targetUnit==NULL? Unit::invalidId: targetUnit->getId(); int unitId= selection->getUnit(i)->getId(); Vec2i currPos= computeDestPos(refPos, selection->getUnit(i)->getPos(), pos); - NetworkCommand networkCommand(nctGiveCommand, unitId, ct->getId(), currPos, -1, targetId); + NetworkCommand networkCommand(this->world,nctGiveCommand, unitId, ct->getId(), currPos, -1, targetId); //every unit is ordered to a different pos CommandResult result= pushNetworkCommand(&networkCommand); @@ -90,7 +90,7 @@ CommandResult Commander::tryGiveCommand(const Selection *selection, const Comman int targetId= targetUnit==NULL? Unit::invalidId: targetUnit->getId(); int unitId= selection->getUnit(i)->getId(); Vec2i currPos= computeDestPos(refPos, selection->getUnit(i)->getPos(), pos); - NetworkCommand networkCommand(nctGiveCommand, unitId, commandType->getId(), currPos, -1, targetId); + NetworkCommand networkCommand(this->world,nctGiveCommand, unitId, commandType->getId(), currPos, -1, targetId); //every unit is ordered to a different position CommandResult result= pushNetworkCommand(&networkCommand); @@ -125,7 +125,7 @@ CommandResult Commander::tryGiveCommand(const Selection *selection, const Vec2i if(commandType!=NULL){ int targetId= targetUnit==NULL? Unit::invalidId: targetUnit->getId(); int unitId= selection->getUnit(i)->getId(); - NetworkCommand networkCommand(nctGiveCommand, unitId, commandType->getId(), currPos, -1, targetId); + NetworkCommand networkCommand(this->world,nctGiveCommand, unitId, commandType->getId(), currPos, -1, targetId); CommandResult result= pushNetworkCommand(&networkCommand); results.push_back(result); @@ -144,7 +144,7 @@ CommandResult Commander::tryGiveCommand(const Selection *selection, const Vec2i CommandResult Commander::tryCancelCommand(const Selection *selection) const{ for(int i=0; igetCount(); ++i){ - NetworkCommand command(nctCancelCommand, selection->getUnit(i)->getId()); + NetworkCommand command(this->world,nctCancelCommand, selection->getUnit(i)->getId()); pushNetworkCommand(&command); } @@ -152,7 +152,7 @@ CommandResult Commander::tryCancelCommand(const Selection *selection) const{ } void Commander::trySetMeetingPoint(const Unit* unit, const Vec2i &pos)const{ - NetworkCommand command(nctSetMeetingPoint, unit->getId(), -1, pos); + NetworkCommand command(this->world,nctSetMeetingPoint, unit->getId(), -1, pos); pushNetworkCommand(&command); } @@ -245,6 +245,7 @@ void Commander::updateNetwork(){ } } +/* void Commander::giveNetworkCommandSpecial(const NetworkCommand* networkCommand) const { switch(networkCommand->getNetworkCommandType()) { case nctNetworkCommand: { @@ -282,13 +283,18 @@ void Commander::giveNetworkCommandSpecial(const NetworkCommand* networkCommand) assert(false); } } +*/ -void Commander::giveNetworkCommand(const NetworkCommand* networkCommand) const { +void Commander::giveNetworkCommand(NetworkCommand* networkCommand) const { + networkCommand->preprocessNetworkCommand(this->world); + /* if(networkCommand->getNetworkCommandType() == nctNetworkCommand) { giveNetworkCommandSpecial(networkCommand); } - else { + else + */ + { Unit* unit= world->findUnitById(networkCommand->getUnitId()); //exec ute command, if unit is still alive diff --git a/source/glest_game/game/commander.h b/source/glest_game/game/commander.h index b719f7948..07029fda5 100644 --- a/source/glest_game/game/commander.h +++ b/source/glest_game/game/commander.h @@ -54,13 +54,13 @@ public: CommandResult tryCancelCommand(const Selection *selection) const; void trySetMeetingPoint(const Unit* unit, const Vec2i &pos) const; CommandResult pushNetworkCommand(const NetworkCommand* networkCommand) const; - void giveNetworkCommandSpecial(const NetworkCommand* networkCommand) const; + //void giveNetworkCommandSpecial(const NetworkCommand* networkCommand) const; private: Vec2i computeRefPos(const Selection *selection) const; Vec2i computeDestPos(const Vec2i &refUnitPos, const Vec2i &unitPos, const Vec2i &commandPos) const; CommandResult computeResult(const CommandResultContainer &results) const; - void giveNetworkCommand(const NetworkCommand* networkCommand) const; + void giveNetworkCommand(NetworkCommand* networkCommand) const; Command* buildCommand(const NetworkCommand* networkCommand) const; }; diff --git a/source/glest_game/gui/gui.cpp b/source/glest_game/gui/gui.cpp index 1b070b53a..fb399179c 100644 --- a/source/glest_game/gui/gui.cpp +++ b/source/glest_game/gui/gui.cpp @@ -363,6 +363,7 @@ void Gui::hotKey(char key){ unitTypeBuildRotation[unitKey] = unitTypeRotation; //!!! + /* //if(allowRotateUnits == true && unitRotation > 0) { SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s] before sending nctNetworkCommand RotateUnit unitTypeid = %d, factionIndex = %d, unitTypeRotation = %f\n",__FILE__,__FUNCTION__,unitType->getId(),factionIndex,unitTypeRotation); @@ -375,6 +376,7 @@ void Gui::hotKey(char key){ //SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s] after sending nctNetworkCommand RotateUnit [%d] result = %d\n",__FILE__,__FUNCTION__,builtUnit->getId(),result); SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s] after sending nctNetworkCommand RotateUnit unitTypeid = %d, factionIndex = %d, unitTypeRotation = %f\n",__FILE__,__FUNCTION__,unitType->getId(),factionIndex,unitTypeRotation); //} + */ SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s] unitType->getId() = %d NEW unitTypeRotation = %f\n",__FILE__,__FUNCTION__,unitType->getId(),unitTypeRotation); } diff --git a/source/glest_game/network/network_interface.h b/source/glest_game/network/network_interface.h index f51ea3609..d8d56240f 100644 --- a/source/glest_game/network/network_interface.h +++ b/source/glest_game/network/network_interface.h @@ -137,7 +137,7 @@ public: //access functions void requestCommand(const NetworkCommand *networkCommand, bool insertAtStart=false); int getPendingCommandCount() const {return pendingCommands.size();} - const NetworkCommand* getPendingCommand(int i) const {return &pendingCommands[i];} + NetworkCommand* getPendingCommand(int i) {return &pendingCommands[i];} void clearPendingCommands() {pendingCommands.clear();} bool getQuit() const {return quit;} }; diff --git a/source/glest_game/network/network_types.cpp b/source/glest_game/network/network_types.cpp index 645c1f185..902213cd2 100644 --- a/source/glest_game/network/network_types.cpp +++ b/source/glest_game/network/network_types.cpp @@ -10,16 +10,24 @@ // ============================================================== #include "network_types.h" +#include "util.h" +#include "unit.h" +#include "world.h" +#include "unit_type.h" +#include "game.h" +#include "gui.h" #include "leak_dumper.h" +using namespace Shared::Util; + namespace Glest{ namespace Game{ // ===================================================== // class NetworkCommand // ===================================================== -NetworkCommand::NetworkCommand(int networkCommandType, int unitId, int commandTypeId, const Vec2i &pos, int unitTypeId, int targetId){ +NetworkCommand::NetworkCommand(World *world, int networkCommandType, int unitId, int commandTypeId, const Vec2i &pos, int unitTypeId, int targetId){ this->networkCommandType= networkCommandType; this->unitId= unitId; this->commandTypeId= commandTypeId; @@ -27,8 +35,72 @@ NetworkCommand::NetworkCommand(int networkCommandType, int unitId, int commandTy this->positionY= pos.y; this->unitTypeId= unitTypeId; this->targetId= targetId; + + if(this->networkCommandType == nctGiveCommand) { + const Unit *unit= world->findUnitById(this->unitId); + + //validate unit + if(unit != NULL) { + const UnitType *unitType= world->findUnitTypeById(unit->getFaction()->getType(), this->unitTypeId); + const CommandType *ct = unit->getType()->findCommandTypeById(this->commandTypeId); + if(ct != NULL && ct->getClass() == ccBuild) { + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] %s\n",__FILE__,__FUNCTION__,__LINE__,toString().c_str()); + + Game *game = world->getGame(); + Gui *gui = game->getGui(); + + int factionIndex = world->getThisFactionIndex(); + char unitKey[50]=""; + sprintf(unitKey,"%d_%d",this->unitTypeId,factionIndex); + float unitTypeRotation = gui->getUnitTypeBuildRotation(unitKey); + + if(unitTypeRotation >= 0) { + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s] attaching RotateUnit to ccBuild command for unitTypeid = %d, factionIndex = %d, unitTypeRotation = %f\n",__FILE__,__FUNCTION__,this->unitTypeId,factionIndex,unitTypeRotation); + this->targetId = unitTypeRotation; + } + } + } + } } +void NetworkCommand::preprocessNetworkCommand(World *world) { + if(networkCommandType == nctGiveCommand) { + const Unit *unit= world->findUnitById(unitId); + + //validate unit + if(unit != NULL) { + const UnitType *unitType= world->findUnitTypeById(unit->getFaction()->getType(), unitTypeId); + const CommandType *ct = unit->getType()->findCommandTypeById(commandTypeId); + if(ct != NULL && ct->getClass() == ccBuild && targetId >= 0) { + Game *game = world->getGame(); + Gui *gui = game->getGui(); + + int factionIndex = unit->getFactionIndex(); + char unitKey[50]=""; + sprintf(unitKey,"%d_%d",unitTypeId,factionIndex); + gui->setUnitTypeBuildRotation(unitKey,targetId); + + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] %s, unitKey = [%s] targetId = %d\n",__FILE__,__FUNCTION__,__LINE__,toString().c_str(),unitKey,targetId); + + targetId = Unit::invalidId; + } + } + else { + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] (unit == NULL) %s\n",__FILE__,__FUNCTION__,__LINE__,toString().c_str()); + } + } +} + +string NetworkCommand::toString() const { + char szBuf[1024]=""; + sprintf(szBuf,"networkCommandType = %d\nunitId = %d\ncommandTypeId = %d\npositionX = %d\nthis->positionY = %d\nunitTypeId = %d\ntargetId = %d", + networkCommandType,unitId,commandTypeId,positionX,this->positionY,unitTypeId,targetId); + + string result = szBuf; + return result; +} + +/* NetworkCommand::NetworkCommand(int networkCommandType, NetworkCommandSubType ncstType, int unitId, int value1, int value2) { this->networkCommandType= networkCommandType; this->unitId= unitId; @@ -38,5 +110,6 @@ NetworkCommand::NetworkCommand(int networkCommandType, NetworkCommandSubType ncs this->unitTypeId= value1; this->targetId= value2; } +*/ }}//end namespace diff --git a/source/glest_game/network/network_types.h b/source/glest_game/network/network_types.h index 7c2d76e46..803b0e78b 100644 --- a/source/glest_game/network/network_types.h +++ b/source/glest_game/network/network_types.h @@ -25,6 +25,8 @@ using Shared::Graphics::Vec2i; namespace Glest{ namespace Game{ +class World; + // ===================================================== // class NetworkString // ===================================================== @@ -47,13 +49,13 @@ public: enum NetworkCommandType { nctGiveCommand, nctCancelCommand, - nctSetMeetingPoint, - nctNetworkCommand + nctSetMeetingPoint + //nctNetworkCommand }; -enum NetworkCommandSubType { - ncstRotateUnit -}; +//enum NetworkCommandSubType { +// ncstRotateUnit +//}; #pragma pack(push, 2) class NetworkCommand{ @@ -68,8 +70,8 @@ private: public: NetworkCommand(){}; - NetworkCommand(int networkCommandType, int unitId, int commandTypeId= -1, const Vec2i &pos= Vec2i(0), int unitTypeId= -1, int targetId= -1); - NetworkCommand(int networkCommandType, NetworkCommandSubType ncstType, int unitId, int value1, int value2=-1); + NetworkCommand(World *world, int networkCommandType, int unitId, int commandTypeId= -1, const Vec2i &pos= Vec2i(0), int unitTypeId= -1, int targetId= -1); + //NetworkCommand(int networkCommandType, NetworkCommandSubType ncstType, int unitId, int value1, int value2=-1); NetworkCommandType getNetworkCommandType() const {return static_cast(networkCommandType);} int getUnitId() const {return unitId;} @@ -77,6 +79,9 @@ public: Vec2i getPosition() const {return Vec2i(positionX, positionY);} int getUnitTypeId() const {return unitTypeId;} int getTargetId() const {return targetId;} + + void preprocessNetworkCommand(World *world); + string toString() const; }; #pragma pack(pop) diff --git a/source/glest_game/world/unit_updater.cpp b/source/glest_game/world/unit_updater.cpp index 3b0e0280e..2cbec433e 100644 --- a/source/glest_game/world/unit_updater.cpp +++ b/source/glest_game/world/unit_updater.cpp @@ -301,7 +301,7 @@ void UnitUpdater::updateBuild(Unit *unit){ sprintf(unitKey,"%d_%d",builtUnitType->getId(),unit->getFaction()->getIndex()); unitRotation = gui->getUnitTypeBuildRotation(unitKey); - SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] builtUnitType->getId() = %d unitRotation = %f\n",__FILE__,__FUNCTION__,__LINE__,builtUnitType->getId(),unitRotation); + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] unitKey = [%s] unitRotation = %f\n",__FILE__,__FUNCTION__,__LINE__,unitKey,unitRotation); } Unit *builtUnit= new Unit(world->getNextUnitId(), command->getPos(), builtUnitType, unit->getFaction(), world->getMap(),unitRotation); builtUnit->create();