diff --git a/source/glest_game/gui/selection.cpp b/source/glest_game/gui/selection.cpp new file mode 100644 index 00000000..c282cb46 --- /dev/null +++ b/source/glest_game/gui/selection.cpp @@ -0,0 +1,207 @@ +// ============================================================== +// This file is part of Glest (www.glest.org) +// +// 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 +// by the Free Software Foundation; either version 2 of the +// License, or (at your option) any later version +// ============================================================== + +#include "selection.h" + +#include + +#include "unit_type.h" +#include "gui.h" +#include "leak_dumper.h" + +using namespace std; + +namespace Glest{ namespace Game{ + +// ===================================================== +// class Selection +// ===================================================== + +void Selection::init(Gui *gui, int factionIndex){ + this->factionIndex= factionIndex; + this->gui= gui; +} + +Selection::~Selection(){ + clear(); +} + +void Selection::select(Unit *unit){ + + //check size + if(selectedUnits.size()>=maxUnits){ + return; + } + + //check if already selected + for(int i=0; iisDead()){ + return; + } + + //check if multisel + if(!unit->getType()->getMultiSelect() && !isEmpty()){ + return; + } + + //check if enemy + if(unit->getFactionIndex()!=factionIndex && !isEmpty()){ + return; + } + + //check existing enemy + if(selectedUnits.size()==1 && selectedUnits.front()->getFactionIndex()!=factionIndex){ + clear(); + } + + //check existing multisel + if(selectedUnits.size()==1 && !selectedUnits.front()->getType()->getMultiSelect()){ + clear(); + } + + unit->addObserver(this); + selectedUnits.push_back(unit); + gui->onSelectionChanged(); +} + +void Selection::select(const UnitContainer &units){ + + //add units to gui + for(UnitIterator it= units.begin(); it!=units.end(); ++it){ + select(*it); + } +} + +void Selection::unSelect(const UnitContainer &units){ + + //add units to gui + for(UnitIterator it= units.begin(); it!=units.end(); ++it){ + for(int i=0; ionSelectionChanged(); +} + +void Selection::clear(){ + //clear list + selectedUnits.clear(); +} + +bool Selection::isUniform() const{ + if(selectedUnits.empty()){ + return true; + } + + const UnitType *ut= selectedUnits.front()->getType(); + + for(int i=0; igetType()!=ut){ + return false; + } + } + return true; +} + +bool Selection::isEnemy() const{ + return selectedUnits.size()==1 && selectedUnits.front()->getFactionIndex()!=factionIndex; +} + +bool Selection::isComandable() const{ + return + !isEmpty() && + !isEnemy() && + !(selectedUnits.size()==1 && !selectedUnits.front()->isOperative()); +} + +bool Selection::isCancelable() const{ + return + selectedUnits.size()>1 || + (selectedUnits.size()==1 && selectedUnits[0]->anyCommand()); +} + +bool Selection::isMeetable() const{ + return + isUniform() && + isComandable() && + selectedUnits.front()->getType()->getMeetingPoint(); +} + +Vec3f Selection::getRefPos() const{ + return getFrontUnit()->getCurrVector(); +} + +bool Selection::hasUnit(const Unit* unit) const{ + return find(selectedUnits.begin(), selectedUnits.end(), unit)!=selectedUnits.end(); +} + +void Selection::assignGroup(int groupIndex){ + //clear group + groups[groupIndex].clear(); + + //assign new group + for(int i=0; ionSelectionChanged(); + } + + } +} + +}}//end namespace diff --git a/source/glest_game/type_instances/faction.cpp b/source/glest_game/type_instances/faction.cpp index 72986b4e..3d08879b 100644 --- a/source/glest_game/type_instances/faction.cpp +++ b/source/glest_game/type_instances/faction.cpp @@ -294,27 +294,31 @@ void Faction::applyCostsOnInterval(){ } } } - + //decrement consumables - for(int j=0; jisOperative()){ - for(int k=0; kgetType()->getCostCount(); ++k){ - const Resource *resource= unit->getType()->getCost(k); - if(resource->getType()->getClass()==rcConsumable && resource->getAmount()>0){ - incResourceAmount(resource->getType(), -resource->getAmount()); - - //decrease unit hp - if(getResource(resource->getType())->getAmount()<0){ - resetResourceAmount(resource->getType()); - bool decHpResult=unit->decHp(unit->getType()->getMaxHp()/3); - if(decHpResult){ - world->getStats()->die(unit->getFactionIndex()); - scriptManager->onUnitDied(unit); - } - StaticSound *sound= unit->getType()->getFirstStOfClass(scDie)->getSound(); - if(sound!=NULL && thisFaction){ - SoundRenderer::getInstance().playFx(sound); + if(!getCpuControl() || + getCpuControl() && !scriptManager->getPlayerModifiers(this->thisFaction)->getAiEnabled()) + { + for(int j=0; jisOperative()){ + for(int k=0; kgetType()->getCostCount(); ++k){ + const Resource *resource= unit->getType()->getCost(k); + if(resource->getType()->getClass()==rcConsumable && resource->getAmount()>0){ + incResourceAmount(resource->getType(), -resource->getAmount()); + + //decrease unit hp + if(getResource(resource->getType())->getAmount()<0){ + resetResourceAmount(resource->getType()); + bool decHpResult=unit->decHp(unit->getType()->getMaxHp()/3); + if(decHpResult){ + world->getStats()->die(unit->getFactionIndex()); + scriptManager->onUnitDied(unit); + } + StaticSound *sound= unit->getType()->getFirstStOfClass(scDie)->getSound(); + if(sound!=NULL && thisFaction){ + SoundRenderer::getInstance().playFx(sound); + } } } }