diff --git a/source/glest_game/type_instances/unit.cpp b/source/glest_game/type_instances/unit.cpp index d2be5cd3..2eecd9ec 100644 --- a/source/glest_game/type_instances/unit.cpp +++ b/source/glest_game/type_instances/unit.cpp @@ -1,7 +1,7 @@ // ============================================================== // This file is part of Glest (www.glest.org) // -// Copyright (C) 2001-2008 Marti�o Figueroa +// Copyright (C) 2001-2008 Martiño Figueroa // // You can redistribute this code and/or modify it under // the terms of the GNU General Public License as published @@ -500,6 +500,11 @@ unsigned int Unit::getCommandSize() const{ return commands.size(); } +#define deleteSingleCommand(command) {\ + undoCommand(command);\ + delete command;\ +} + //give one command (clear, and push back) CommandResult Unit::giveCommand(Command *command, bool tryQueue){ @@ -515,7 +520,18 @@ CommandResult Unit::giveCommand(Command *command, bool tryQueue){ SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); - if(command->getCommandType()->isQueuable(tryQueue)){ + const int command_priority = command->getCommandType()->getPriority(); + + if(command->getCommandType()->isQueuable(tryQueue) && (commands.empty() || (command_priority >= commands.back()->getCommandType()->getPriority()))){ + //Delete all lower-prioirty commands + for (list::iterator i = commands.begin(); i != commands.end();) { + if ((*i)->getCommandType()->getPriority() < command_priority) { + deleteSingleCommand(*i); + i = commands.erase(i); + } else { + ++i; + } + } //cancel current command if it is not queuable SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); diff --git a/source/glest_game/types/command_type.h b/source/glest_game/types/command_type.h index f40e759d..b0528bbf 100644 --- a/source/glest_game/types/command_type.h +++ b/source/glest_game/types/command_type.h @@ -88,6 +88,8 @@ public: Queueability q = isQueuable(); return (q != qNever) && (q != qOnlyLast); } + //Priority: commands of higher priority will cancel commands of lower priority + virtual int getPriority() const {return 0;} //get CommandClass getClass() const; @@ -110,6 +112,7 @@ public: virtual string getDesc(const TotalUpgrade *totalUpgrade) const; virtual string toString() const; virtual Queueability isQueuable() const {return qNever;} + virtual int getPriority() const {return 100000;} //get const StopSkillType *getStopSkillType() const {return stopSkillType;}; }; @@ -151,6 +154,7 @@ public: virtual string getDesc(const TotalUpgrade *totalUpgrade) const; virtual string toString() const; + //get const MoveSkillType * getMoveSkillType() const {return moveSkillType;} const AttackSkillType * getAttackSkillType() const {return attackSkillType;} @@ -286,6 +290,7 @@ public: virtual string toString() const; virtual const ProducibleType *getProduced() const; virtual Queueability isQueuable() const {return qAlways;} + virtual int getPriority() const {return 5;} //higher priority to cancel attack //get const ProduceSkillType *getProduceSkillType() const {return produceSkillType;} @@ -311,6 +316,7 @@ public: virtual string getReqDesc() const; virtual const ProducibleType *getProduced() const; virtual Queueability isQueuable() const {return qAlways;} + virtual int getPriority() const {return 5;} //higher priority to cancel attack (and same as Build) //get const UpgradeSkillType *getUpgradeSkillType() const {return upgradeSkillType;}