From 0b1dcc70f5591bfbb715aea251dd9cd84ab35f15 Mon Sep 17 00:00:00 2001 From: pavanvo Date: Sat, 27 Aug 2022 15:16:25 +0400 Subject: [PATCH] feat: allow queue after Morph commands --- source/glest_game/game/commander.cpp | 19 ++++-- source/glest_game/gui/gui.cpp | 6 +- source/glest_game/network/network_types.cpp | 67 ++++++++++++--------- source/glest_game/type_instances/unit.cpp | 12 ++++ source/glest_game/type_instances/unit.h | 1 + source/glest_game/types/command_type.h | 2 +- 6 files changed, 72 insertions(+), 35 deletions(-) diff --git a/source/glest_game/game/commander.cpp b/source/glest_game/game/commander.cpp index a9f10b75..a6881800 100644 --- a/source/glest_game/game/commander.cpp +++ b/source/glest_game/game/commander.cpp @@ -263,8 +263,12 @@ std::pair Commander::tryGiveCommand(const Selection *selec int targetId= targetUnit==NULL? Unit::invalidId: targetUnit->getId(); int unitId= unit->getId(); Vec2i currPos= world->getMap()->computeDestPos(refPos, unit->getPosNotThreadSafe(), pos); + int unitTypeId= -1; + auto mct= unit->getCurrMorphCt(); + if(mct) unitTypeId= mct->getMorphUnit()->getId(); + NetworkCommand networkCommand(this->world,nctGiveCommand, unitId, - commandType->getId(), currPos, -1, targetId, -1, tryQueue, + commandType->getId(), currPos, unitTypeId, targetId, -1, tryQueue, cst_None, -1, unitCommandGroupId); //every unit is ordered to a different position @@ -316,13 +320,16 @@ std::pair Commander::tryGiveCommand(const Selection *selec if(commandType != NULL) { int targetId= targetUnit==NULL? Unit::invalidId: targetUnit->getId(); int unitId= unit->getId(); + int unitTypeId= -1; + auto mct= unit->getCurrMorphCt(); + if(mct) unitTypeId= mct->getMorphUnit()->getId(); std::pair resultCur(crFailUndefined,""); bool canSubmitCommand=canSubmitCommandType(unit, commandType); if(canSubmitCommand == true) { NetworkCommand networkCommand(this->world,nctGiveCommand, - unitId, commandType->getId(), currPos, -1, targetId, + unitId, commandType->getId(), currPos, unitTypeId, targetId, -1, tryQueue, cst_None, -1, unitCommandGroupId); resultCur= pushNetworkCommand(&networkCommand); } @@ -950,7 +957,7 @@ Command* Commander::buildCommand(const NetworkCommand* networkCommand) const { throw megaglest_runtime_error(szBuf); } - ct = unit->getType()->findCommandTypeById(networkCommand->getCommandTypeId()); + ct = unit->getType()->findCommandTypeById(networkCommand->getCommandTypeId()); if(unit->getFaction()->getIndex() != networkCommand->getUnitFactionIndex()) { @@ -984,7 +991,11 @@ Command* Commander::buildCommand(const NetworkCommand* networkCommand) const { throw megaglest_runtime_error(sError); } - const UnitType* unitType= world->findUnitTypeById(unit->getFaction()->getType(), networkCommand->getUnitTypeId()); + const UnitType* unitType= world->findUnitTypeById(unit->getFaction()->getType(), networkCommand->getUnitTypeId()); + + if( networkCommand->getUnitTypeId() > -1 && networkCommand->getWantQueue()) { //Morph Queue + ct = unitType->findCommandTypeById(networkCommand->getCommandTypeId()); + } // debug test! //throw megaglest_runtime_error("Test missing command type!"); diff --git a/source/glest_game/gui/gui.cpp b/source/glest_game/gui/gui.cpp index ce6f79f6..a0ebeba9 100644 --- a/source/glest_game/gui/gui.cpp +++ b/source/glest_game/gui/gui.cpp @@ -552,7 +552,7 @@ void Gui::giveTwoClickOrders(int x, int y , bool prepared) { else { result= commander->tryGiveCommand(&selection, activeCommandClass, targetPos, targetUnit,queueKeyDown); - } + } } else { //selecting building @@ -969,6 +969,10 @@ void Gui::computeDisplay(){ //uniform selection if(u->isBuilt()){ //printf("u->isBuilt()\n"); + auto mct = u->getCurrMorphCt(); + if(mct && isKeyDown(queueCommandKey)) {//Morph Queue + ut=mct->getMorphUnit(); + } int morphPos= 8; for(int i= 0; i < ut->getCommandTypeCount(); ++i){ diff --git a/source/glest_game/network/network_types.cpp b/source/glest_game/network/network_types.cpp index 62b73425..db2c4ac4 100644 --- a/source/glest_game/network/network_types.cpp +++ b/source/glest_game/network/network_types.cpp @@ -48,43 +48,52 @@ NetworkCommand::NetworkCommand(World *world, int networkCommandType, int unitId, this->targetId = targetId >= 0 ? targetId : facing; this->fromFactionIndex = world->getThisFactionIndex(); - if(this->networkCommandType == nctGiveCommand) { - const Unit *unit= world->findUnitById(this->unitId); + if(this->networkCommandType == nctGiveCommand) { + const Unit *unit= world->findUnitById(this->unitId); - //validate unit - if(unit != NULL) { - this->unitFactionIndex = unit->getFaction()->getIndex(); - this->unitFactionUnitCount = unit->getFaction()->getUnitCount(); + //validate unit + if(unit != NULL) { + this->unitFactionIndex = unit->getFaction()->getIndex(); + this->unitFactionUnitCount = unit->getFaction()->getUnitCount(); - //const UnitType *unitType= world->findUnitTypeById(unit->getFaction()->getType(), this->unitTypeId); - const CommandType *ct = unit->getType()->findCommandTypeById(this->commandTypeId); - if(ct != NULL && ct->getClass() == ccBuild) { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] %s\n",__FILE__,__FUNCTION__,__LINE__,toString().c_str()); - CardinalDir::assertDirValid(facing); - assert(targetId == -1); - } - } - } + const CommandType *ct= unit->getType()->findCommandTypeById(this->commandTypeId); + if(unitTypeId > -1) { + const UnitType *unitType= world->findUnitTypeById(unit->getFaction()->getType(), this->unitTypeId); + ct= unitType->findCommandTypeById(this->commandTypeId); + } + + if(ct != NULL && ct->getClass() == ccBuild) { + if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] %s\n",__FILE__,__FUNCTION__,__LINE__,toString().c_str()); + CardinalDir::assertDirValid(facing); + assert(targetId == -1); + } + } + } - 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()); + 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()); } void NetworkCommand::preprocessNetworkCommand(World *world) { - if(networkCommandType == nctGiveCommand) { - const Unit *unit= world->findUnitById(unitId); + 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) { + //validate unit + if(unit != NULL) { + + const CommandType *ct= unit->getType()->findCommandTypeById(commandTypeId); + if(unitTypeId > -1) { + const UnitType *unitType= world->findUnitTypeById(unit->getFaction()->getType(), unitTypeId); + ct= unitType->findCommandTypeById(this->commandTypeId); + } + + if(ct != NULL && ct->getClass() == ccBuild && targetId >= 0) { CardinalDir::assertDirValid(targetId); - } - } - else { - 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()); - } - } + } + } + else { + 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()); + } + } } diff --git a/source/glest_game/type_instances/unit.cpp b/source/glest_game/type_instances/unit.cpp index 429a2334..6f6788f6 100644 --- a/source/glest_game/type_instances/unit.cpp +++ b/source/glest_game/type_instances/unit.cpp @@ -745,6 +745,15 @@ void Unit::cleanupAllParticlesystems() { } +const MorphCommandType* Unit::getCurrMorphCt() const { + auto result = std::find_if(commands.rbegin(), commands.rend(),[](Command *i) + { return i->getCommandType()->getClass() == ccMorph? true: false; }); + if(result != commands.rend()) { + return static_cast((*result)->getCommandType()); + } + else return NULL; +} + ParticleSystem * Unit::getFire() const { if(this->fire != NULL && Renderer::getInstance().validateParticleSystemStillExists(this->fire,rsGame) == false) { @@ -3826,6 +3835,9 @@ std::pair Unit::checkCommand(Command *command) const { this->getType()->getFirstRepairCommand(this->getType()) != NULL) { } + else if(getCurrMorphCt()->getMorphUnit()->hasCommandType(command->getCommandType())) { + // Allow Current Morph Commands + } else { //printf("In [%s::%s Line: %d] command = %p\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,command); diff --git a/source/glest_game/type_instances/unit.h b/source/glest_game/type_instances/unit.h index 1760f17e..80ac488f 100644 --- a/source/glest_game/type_instances/unit.h +++ b/source/glest_game/type_instances/unit.h @@ -653,6 +653,7 @@ public: } return NULL; } + const MorphCommandType* getCurrMorphCt() const; void replaceCurrCommand(Command *cmd); int getCountOfProducedUnitsPreExistence(const UnitType *ut) const; unsigned int getCommandSize() const; diff --git a/source/glest_game/types/command_type.h b/source/glest_game/types/command_type.h index 8808d27d..42dd6f01 100644 --- a/source/glest_game/types/command_type.h +++ b/source/glest_game/types/command_type.h @@ -425,7 +425,7 @@ public: virtual string toString(bool translatedValue) const; virtual string getReqDesc(bool translatedValue) const; virtual const ProducibleType *getProduced() const; - Queueability isQueuable() const {return qOnlyLast;} //After morph anything can happen + Queueability isQueuable() const {return qAlways;} //After morph anything can happen //get const MorphSkillType *getMorphSkillType() const {return morphSkillType;}