// ============================================================== // This file is part of Glest (www.glest.org) // // Copyright (C) 2001-2005 Martiņo Figueroa // // You can redistribute this code and/or modify it under // the terms of the GNU General Public License as published // by the Free Software Foundation; either version 2 of the // License, or (at your option) any later version // ============================================================== #include "commander.h" #include "world.h" #include "unit.h" #include "upgrade.h" #include "command.h" #include "command_type.h" #include "leak_dumper.h" using namespace Shared::Graphics; using namespace Shared::Util; namespace Glest{ namespace Game{ // ===================================================== // class Commander // ===================================================== const float Commander::maxFormationDist=3.f; // ===================== PUBLIC ======================== void Commander::init(World *world){ this->world= world; } CommandResult Commander::tryGiveCommand(const Selection *selection, CommandClass commandClass, const Vec2i &pos, Unit *targetUnit) const{ if(!selection->isEmpty()){ Vec2i refPos, currPos; CommandResultContainer results; refPos= computeRefPos(selection); //give orders to all selected units for(int i=0; igetCount(); ++i){ Unit *unit= selection->getUnit(i); const CommandType *ct= unit->getType()->getFirstCtOfClass(commandClass); if(ct!=NULL){ //every unit is ordered to a different pos currPos= computeDestPos(refPos, unit->getPos(), pos); CommandResult result= isueCommand(selection->getUnit(i), ct, currPos, targetUnit); results.push_back(result); } else{ results.push_back(crFailUndefined); } } return computeResult(results); } else{ return crFailUndefined; } } CommandResult Commander::tryGiveCommand(const Selection *selection, const CommandType *commandType, const Vec2i &pos, Unit *targetUnit) const{ if(!selection->isEmpty()){ Vec2i refPos, currPos; CommandResultContainer results; refPos= computeRefPos(selection); //give orders to all selected units for(int i=0; igetCount(); ++i){ //every unit is ordered to a different position currPos= computeDestPos(refPos, selection->getUnit(i)->getPos(), pos); CommandResult result= isueCommand(selection->getUnit(i), commandType, currPos, targetUnit); results.push_back(result); } return computeResult(results); } else{ return crFailUndefined; } } //auto command CommandResult Commander::tryGiveCommand(const Selection *selection, const Vec2i &pos, Unit *targetUnit) const{ if(!selection->isEmpty()){ Vec2i refPos, currPos; CommandResultContainer results; //give orders to all selected units refPos= computeRefPos(selection); for(int i=0; igetCount(); ++i){ //every unit is ordered to a different pos currPos= computeDestPos(refPos, selection->getUnit(i)->getPos(), pos); //get command type const CommandType *commandType= selection->getUnit(i)->computeCommandType(pos, targetUnit); //give commands if(commandType!=NULL){ CommandResult result= isueCommand(selection->getUnit(i), commandType, currPos, targetUnit); results.push_back(result); } else{ results.push_back(crFailUndefined); } } return computeResult(results); } else{ return crFailUndefined; } } CommandResult Commander::tryCancelCommand(const Selection *selection) const{ for(int i=0; igetCount(); ++i){ selection->getUnit(i)->cancelCommand(); } return crSuccess; } // ==================== PRIVATE ==================== Vec2i Commander::computeRefPos(const Selection *selection) const{ Vec2i total= Vec2i(0); for(int i=0; igetCount(); ++i){ total= total+selection->getUnit(i)->getPos(); } return Vec2i(total.x/ selection->getCount(), total.y/ selection->getCount()); } Vec2i Commander::computeDestPos(const Vec2i &refUnitPos, const Vec2i &unitPos, const Vec2i &commandPos) const{ Vec2i pos; if(refUnitPos.dist(unitPos)getMap()->clampPos(pos); return pos; } CommandResult Commander::computeResult(const CommandResultContainer &results) const{ switch(results.size()){ case 0: return crFailUndefined; case 1: return results.front(); default: for(int i=0; igiveCommand(command); } }}//end namespace