From cf5164d5ce418e6926618d7cf39df71f6a59131a Mon Sep 17 00:00:00 2001 From: Mark Vejvoda Date: Fri, 22 Oct 2010 04:16:11 +0000 Subject: [PATCH] - bugfix for consumable resources. Now we get the total usage value BEFORE applying the consequences (thanks for finding this Zoythrus) --- source/glest_game/type_instances/faction.cpp | 85 ++++++++++++++++++++ 1 file changed, 85 insertions(+) diff --git a/source/glest_game/type_instances/faction.cpp b/source/glest_game/type_instances/faction.cpp index 0c1e719a..ba977c41 100644 --- a/source/glest_game/type_instances/faction.cpp +++ b/source/glest_game/type_instances/faction.cpp @@ -366,6 +366,90 @@ void Faction::deApplyStaticConsumption(const ProducibleType *p) //apply resource on interval (cosumable resouces) void Faction::applyCostsOnInterval(){ + // For each Resource type we store in the int a total consumed value, then + // a vector of units that consume the resource type + std::map > > resourceIntervalUsage; + + // count up consumables usage for the interval + for(int j = 0; j < getUnitCount(); ++j) { + Unit *unit = getUnit(j); + if(unit->isOperative() == true) { + for(int k = 0; k < unit->getType()->getCostCount(); ++k) { + const Resource *resource = unit->getType()->getCost(k); + if(resource->getType()->getClass() == rcConsumable && resource->getAmount() != 0) { + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] resource->getType() [%s] store = %d, unit cost = %d\n",__FILE__,__FUNCTION__,__LINE__,resource->getType()->getName().c_str(), getResource(resource->getType())->getAmount(),resource->getAmount()); + + if(resourceIntervalUsage.find(resource->getType()) == resourceIntervalUsage.end()) { + resourceIntervalUsage[resource->getType()] = make_pair >(0,std::vector()); + } + // Negative cost means accumulate the resource type + resourceIntervalUsage[resource->getType()].first += -resource->getAmount(); + + // If the cost > 0 then the unit is a consumer + if(resource->getAmount() > 0) { + resourceIntervalUsage[resource->getType()].second.push_back(unit); + } + + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] resourceIntervalUsage[resource->getType()].first = %d, consumerCount = %d\n",__FILE__,__FUNCTION__,__LINE__,resourceIntervalUsage[resource->getType()].first,resourceIntervalUsage[resource->getType()].second.size()); + } + } + } + } + + // Apply consumable resource usage + if(resourceIntervalUsage.size() > 0) { + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] resourceIntervalUsage.size() = %d\n",__FILE__,__FUNCTION__,__LINE__,resourceIntervalUsage.size()); + + for(std::map > >::iterator iter = resourceIntervalUsage.begin(); + iter != resourceIntervalUsage.end(); + ++iter) { + // Apply resource type usage to faction resource store + const ResourceType *rt = iter->first; + int resourceTypeUsage = iter->second.first; + incResourceAmount(rt, resourceTypeUsage); + + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] rt [%s] resourceTypeUsage = %d store = %d\n",__FILE__,__FUNCTION__,__LINE__,rt->getName().c_str(), resourceTypeUsage,getResource(rt)->getAmount()); + + // Check if we have any unit consumers + if(getResource(rt)->getAmount() < 0) { + resetResourceAmount(rt); + + // Apply consequences to consumer units of this resource type + std::vector &resourceConsumers = iter->second.second; + + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] resourceConsumers.size() = %d\n",__FILE__,__FUNCTION__,__LINE__,resourceConsumers.size()); + + for(int i = 0; i < resourceConsumers.size(); ++i) { + Unit *unit = resourceConsumers[i]; + + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] getResource(resource->getType())->getAmount() = %d\n",__FILE__,__FUNCTION__,__LINE__,getResource(rt)->getAmount()); + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] consume setting for faction index = %d, consume = %d, getResource(resource->getType())->getAmount() = %d, Unit = [%s] - [%d]\n",__FILE__,__FUNCTION__,__LINE__,this->index,scriptManager->getPlayerModifiers(this->index)->getConsumeEnabled(),getResource(rt)->getAmount(),unit->getFullName().c_str(),unit->getId()); + + //decrease unit hp + if(scriptManager->getPlayerModifiers(this->index)->getConsumeEnabled() == true) { + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + + bool decHpResult = unit->decHp(unit->getType()->getMaxHp() / 3); + + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] decHpResult = %d, unit->getType()->getMaxHp() = %d, hp = %d\n",__FILE__,__FUNCTION__,__LINE__,decHpResult,unit->getType()->getMaxHp(),unit->getHp()); + + if(decHpResult) { + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + + world->getStats()->die(unit->getFactionIndex()); + scriptManager->onUnitDied(unit); + } + StaticSound *sound= unit->getType()->getFirstStOfClass(scDie)->getSound(); + if(sound != NULL && thisFaction) { + SoundRenderer::getInstance().playFx(sound); + } + } + } + } + } + } + +/* //increment consumables for(int j=0; j