Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature command queue after morph commands #257

Open
wants to merge 18 commits into
base: develop
Choose a base branch
from
40 changes: 29 additions & 11 deletions source/glest_game/game/commander.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,11 @@ std::pair<CommandResult,string> Commander::tryGiveCommand(const Selection *selec
}

if(useCommandtype != NULL) {
auto mct= unit->getCurrMorphCt();
int nextUnitTypeId= mct? mct->getMorphUnit()->getId(): -1;
NetworkCommand networkCommand(this->world,nctGiveCommand, unitId,
useCommandtype->getId(), usePos, unitType->getId(),
(targetUnit != NULL ? targetUnit->getId() : -1),
useCommandtype->getId(), usePos, unitType->getId(), nextUnitTypeId,
(targetUnit != NULL ? targetUnit->getId() : -1),
facing, tryQueue, commandStateType,commandStateValue,
unitCommandGroupId);

Expand Down Expand Up @@ -162,8 +164,10 @@ std::pair<CommandResult,string> Commander::tryGiveCommand(const Unit* unit, cons
std::pair<CommandResult,string> result(crFailUndefined,"");
bool canSubmitCommand=canSubmitCommandType(unit, commandType);
if(canSubmitCommand == true) {
auto mct= unit->getCurrMorphCt();
int nextUnitTypeId= mct? mct->getMorphUnit()->getId(): -1;
NetworkCommand networkCommand(this->world,nctGiveCommand, unit->getId(),
commandType->getId(), pos, unitType->getId(),
commandType->getId(), pos, unitType->getId(), nextUnitTypeId,
(targetUnit != NULL ? targetUnit->getId() : -1),
facing, tryQueue,cst_None,-1,unitGroupCommandId);

Expand Down Expand Up @@ -210,8 +214,10 @@ std::pair<CommandResult,string> Commander::tryGiveCommand(const Selection *selec
int unitId= selection->getUnit(i)->getId();
Vec2i currPos= world->getMap()->computeDestPos(refPos,
selection->getUnit(i)->getPosNotThreadSafe(), pos);
auto mct= unit->getCurrMorphCt();
int nextUnitTypeId= mct? mct->getMorphUnit()->getId(): -1;
NetworkCommand networkCommand(this->world,nctGiveCommand,
unitId, ct->getId(), currPos, -1, targetId, -1,
unitId, ct->getId(), currPos, -1, nextUnitTypeId, targetId, -1,
tryQueue,cst_None,-1,unitCommandGroupId);

//every unit is ordered to a different pos
Expand Down Expand Up @@ -263,8 +269,11 @@ std::pair<CommandResult,string> 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);
auto mct= unit->getCurrMorphCt();
int nextUnitTypeId= mct? mct->getMorphUnit()->getId(): -1;

NetworkCommand networkCommand(this->world,nctGiveCommand, unitId,
commandType->getId(), currPos, -1, targetId, -1, tryQueue,
commandType->getId(), currPos, -1, nextUnitTypeId, targetId, -1, tryQueue,
cst_None, -1, unitCommandGroupId);

//every unit is ordered to a different position
Expand Down Expand Up @@ -310,27 +319,30 @@ std::pair<CommandResult,string> Commander::tryGiveCommand(const Selection *selec
currPos= world->getMap()->computeDestPos(refPos, unit->getPosNotThreadSafe(), pos);

//get command type
const CommandType *commandType= unit->computeCommandType(pos, targetUnit);
auto mct= unit->getCurrMorphCt();
auto nextUnitType= mct? mct->getMorphUnit(): NULL;
const CommandType *commandType= unit->computeCommandType(pos, targetUnit, nextUnitType);

//give commands
if(commandType != NULL) {
int targetId= targetUnit==NULL? Unit::invalidId: targetUnit->getId();
int unitId= unit->getId();
int nextUnitTypeId= nextUnitType? nextUnitType->getId(): -1;

std::pair<CommandResult,string> 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, -1, nextUnitTypeId, targetId,
-1, tryQueue, cst_None, -1, unitCommandGroupId);
resultCur= pushNetworkCommand(&networkCommand);
}
results.push_back(resultCur);
}
else if(unit->isMeetingPointSettable() == true) {
NetworkCommand command(this->world,nctSetMeetingPoint,
unit->getId(), -1, currPos,-1,-1,-1,false,
unit->getId(), -1, currPos,-1,-1,-1,-1,false,
cst_None,-1,unitCommandGroupId);

std::pair<CommandResult,string> resultCur= pushNetworkCommand(&command);
Expand Down Expand Up @@ -358,7 +370,7 @@ CommandResult Commander::tryCancelCommand(const Selection *selection) const {

for(int i = 0; i < selection->getCount(); ++i) {
NetworkCommand command(this->world,nctCancelCommand,
selection->getUnit(i)->getId(),-1,Vec2i(0),-1,-1,-1,false,
selection->getUnit(i)->getId(),-1,Vec2i(0),-1,-1,-1,-1,false,
cst_None,-1,unitCommandGroupId);
pushNetworkCommand(&command);
}
Expand Down Expand Up @@ -950,7 +962,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()) {

Expand Down Expand Up @@ -984,7 +996,13 @@ 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->getNextUnitTypeId() > -1 && networkCommand->getWantQueue()) { //Morph Queue
auto nextUnitType= world->findUnitTypeById(unit->getFaction()->getType(), networkCommand->getNextUnitTypeId());
auto mct = nextUnitType->findCommandTypeById(networkCommand->getCommandTypeId());
if(mct) ct = mct;
}

// debug test!
//throw megaglest_runtime_error("Test missing command type!");
Expand Down
3 changes: 3 additions & 0 deletions source/glest_game/game/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5005,7 +5005,10 @@ void Game::keyUp(SDL_KeyboardEvent key) {
gameCamera.setMoveX(0);
camRightButtonDown= false;
calcCameraMoveX();
} else {
gui.hotKeyReleased(key);
}

}
}
catch(const exception &ex) {
Expand Down
26 changes: 23 additions & 3 deletions source/glest_game/gui/gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,16 @@ void Gui::hotKey(SDL_KeyboardEvent key) {
else if(isKeyPressed(configKeys.getSDLKey("HotKeySelectedUnitsStop"),key) == true) {
clickCommonCommand(ccStop);
}

if(isKeyDown(queueCommandKey)) {
computeDisplay();
}
}

void Gui::hotKeyReleased(SDL_KeyboardEvent key) {
if(!isKeyDown(queueCommandKey)) {
computeDisplay();
}
}

void Gui::switchToNextDisplayColor(){
Expand All @@ -465,6 +475,8 @@ void Gui::giveOneClickOrders(){

std::pair<CommandResult,string> result(crFailUndefined,"");
bool queueKeyDown = isKeyDown(queueCommandKey);
if(activeCommandType && activeCommandType->isQueuable()==qNever && queueKeyDown) return;

if(selection.isUniform()){
result= commander->tryGiveCommand(&selection, activeCommandType, Vec2i(0), (Unit*)NULL, queueKeyDown);
}
Expand Down Expand Up @@ -543,7 +555,8 @@ void Gui::giveTwoClickOrders(int x, int y , bool prepared) {
}

bool queueKeyDown = isKeyDown(queueCommandKey);
//give orders to the units of this faction
if(activeCommandType && activeCommandType->isQueuable()==qNever && queueKeyDown) return;
//give orders to the units of this faction
if(selectingBuilding == false) {
if(selection.isUniform()) {
result= commander->tryGiveCommand(&selection, activeCommandType,
Expand All @@ -552,7 +565,7 @@ void Gui::giveTwoClickOrders(int x, int y , bool prepared) {
else {
result= commander->tryGiveCommand(&selection, activeCommandClass,
targetPos, targetUnit,queueKeyDown);
}
}
}
else {
//selecting building
Expand Down Expand Up @@ -707,6 +720,9 @@ void Gui::mouseDownDisplayUnitSkills(int posDisplay) {
if (activeCommandClass == ccAttack) {
ct = selection.getUnitFromCC(ccAttack)->getType()->getFirstCtOfClass(activeCommandClass);
}
auto mct= unit->getCurrMorphCt();
if(mct && isKeyDown(queueCommandKey)) ct= mct->getMorphUnit()->getFirstCtOfClass(activeCommandClass);

if(activeCommandType!=NULL && activeCommandType->getClass()==ccBuild){
assert(selection.isUniform());
selectingBuilding= true;
Expand Down Expand Up @@ -969,6 +985,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();
}//TODO on queueCommandKey presed disable stop cmd

int morphPos= 8;
for(int i= 0; i < ut->getCommandTypeCount(); ++i){
Expand All @@ -989,7 +1009,7 @@ void Gui::computeDisplay(){
display.setCommandType(displayPos, ct);
display.setCommandClass(displayPos, ct->getClass());
bool reqOk=u->getFaction()->reqsOk(ct);
display.setDownLighted(displayPos,reqOk);
display.setDownLighted(displayPos, reqOk && !(ct->isQueuable()==qNever && isKeyDown(queueCommandKey)));

if (reqOk && produced != NULL) {
if (possibleAmount == 0) {
Expand Down
1 change: 1 addition & 0 deletions source/glest_game/gui/gui.h
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ class Gui {
void mouseDoubleClickLeftGraphics(int x, int y);
void groupKey(int groupIndex);
void hotKey(SDL_KeyboardEvent key);
void hotKeyReleased(SDL_KeyboardEvent key);

//misc
void switchToNextDisplayColor();
Expand Down
70 changes: 36 additions & 34 deletions source/glest_game/network/network_types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,13 @@ namespace Glest{ namespace Game{

NetworkCommand::NetworkCommand(World *world, int networkCommandType, int unitId,
int commandTypeId, const Vec2i &pos, int unitTypeId,
int targetId, int facing, bool wantQueue,
int nextUnitTypeId, int targetId, int facing, bool wantQueue,
CommandStateType commandStateType,
int commandStateValue, int unitCommandGroupId)
: networkCommandType(networkCommandType)
, unitId(unitId)
, unitTypeId(unitTypeId)
, nextUnitTypeId(nextUnitTypeId)
, commandTypeId(commandTypeId)
, positionX(pos.x)
, positionY(pos.y)
Expand All @@ -48,43 +49,44 @@ 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);

//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);
}
}
}

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(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();

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);
}
}
}

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);

//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) {
if(networkCommandType == nctGiveCommand) {
const Unit *unit= world->findUnitById(unitId);

//validate unit
if(unit != NULL) {

const CommandType *ct= unit->getType()->findCommandTypeById(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());
}
}
}


Expand Down
4 changes: 4 additions & 0 deletions source/glest_game/network/network_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ class NetworkCommand {
networkCommandType=0;
unitId=0;
unitTypeId=0;
nextUnitTypeId=0;
commandTypeId=0;
positionX=0;
positionY=0;
Expand All @@ -114,6 +115,7 @@ class NetworkCommand {
int commandTypeId= -1,
const Vec2i &pos= Vec2i(0),
int unitTypeId= -1,
int nextUnitTypeId= -1,
int targetId= -1,
int facing= -1,
bool wantQueue = false,
Expand All @@ -124,6 +126,7 @@ class NetworkCommand {
int16 networkCommandType;
int32 unitId;
int16 unitTypeId;
int16 nextUnitTypeId;
int16 commandTypeId;
int16 positionX;
int16 positionY;
Expand All @@ -141,6 +144,7 @@ class NetworkCommand {
int getCommandTypeId() const {return commandTypeId;}
Vec2i getPosition() const {return Vec2i(positionX, positionY);}
int getUnitTypeId() const {return unitTypeId;}
int getNextUnitTypeId() const {return nextUnitTypeId;}
int getTargetId() const {return targetId;}
int getWantQueue() const {return wantQueue;}
int getFromFactionIndex() const {return fromFactionIndex;}
Expand Down
6 changes: 0 additions & 6 deletions source/glest_game/type_instances/command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,6 @@ Command::Command(const CommandType *ct, const Vec2i &pos, const UnitType *unitTy
//}
}

int Command::getPriority(){
if(this->commandType->commandTypeClass==ccAttack && getUnit()==NULL){
return 5; // attacks to the ground have low priority
}
return this->commandType->getTypePriority();
}
// =============== set ===============

void Command::setCommandType(const CommandType *commandType) {
Expand Down
3 changes: 0 additions & 3 deletions source/glest_game/type_instances/command.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,6 @@ class Command {
inline const UnitType* getUnitType() const {return unitType;}
inline CardinalDir getFacing() const {return facing;}

//Priority: commands of higher priority will cancel commands of lower priority
virtual int getPriority();

//set
void setCommandType(const CommandType *commandType);
void setPos(const Vec2i &pos);
Expand Down
Loading