Skip to content

Commit

Permalink
-Experimentally removed farming, health, and food consumption meters,…
Browse files Browse the repository at this point in the history
… the food resource, and associated UI elements, AI interface stuff, and content script parsing. Population growth now just depends on the current and target population meters. Various bits of content have been modified accordingly, such as removing health or farming modifiers and replacing with somewhat arbitrarily chosen changes to population or target population.

-Fixed some scripting errors related to [] brackets around one item, and using AnyEmpire as an empire id when it is actually an affiliation type, in GenerateSitRepMessage effects.
-Grooming

svn path=/trunk/FreeOrion/; revision=4777
  • Loading branch information
geoffthemedio committed Apr 1, 2012
1 parent a5e1053 commit 4fea9eb
Show file tree
Hide file tree
Showing 40 changed files with 181 additions and 1,397 deletions.
7 changes: 0 additions & 7 deletions AI/AIInterface.cpp
Expand Up @@ -220,13 +220,6 @@ namespace AIInterface {
EmpireManager& manager = AIClientApp::GetApp()->Empires();
for (EmpireManager::iterator it = manager.begin(); it != manager.end(); ++it)
it->second->UpdateResourcePools();

Universe& universe = AIClientApp::GetApp()->GetUniverse();
// self-allocate resources on unowned planets, so natives don't starve
std::vector<Planet*> planets = universe.Objects().FindObjects<Planet>();
for (std::vector<Planet*>::const_iterator it = planets.begin(); it != planets.end(); ++it)
if ((*it)->Unowned() && (*it)->CurrentMeterValue(METER_POPULATION) > 0.0)
(*it)->SetAllocatedFood(std::min((*it)->CurrentMeterValue(METER_FARMING), (*it)->CurrentMeterValue(METER_POPULATION)));
}

int IssueFleetMoveOrder(int fleet_id, int destination_id) {
Expand Down
193 changes: 1 addition & 192 deletions Empire/Empire.cpp
Expand Up @@ -952,7 +952,6 @@ Empire::Empire(const std::string& name, const std::string& player_name, int empi

void Empire::Init() {
m_resource_pools[RE_MINERALS] = boost::shared_ptr<ResourcePool>(new ResourcePool(RE_MINERALS));
m_resource_pools[RE_FOOD] = boost::shared_ptr<ResourcePool>(new ResourcePool(RE_FOOD));
m_resource_pools[RE_RESEARCH] = boost::shared_ptr<ResourcePool>(new ResourcePool(RE_RESEARCH));
m_resource_pools[RE_INDUSTRY] = boost::shared_ptr<ResourcePool>(new ResourcePool(RE_INDUSTRY));
m_resource_pools[RE_TRADE] = boost::shared_ptr<ResourcePool>(new ResourcePool(RE_TRADE));
Expand Down Expand Up @@ -988,7 +987,6 @@ int Empire::CapitalID() const
int Empire::StockpileID(ResourceType res) const {
switch (res) {
case RE_MINERALS:
case RE_FOOD:
case RE_TRADE:
return m_capital_id;
break;
Expand Down Expand Up @@ -2509,7 +2507,7 @@ void Empire::CheckProductionProgress() {


if (stockpile_object_id == INVALID_OBJECT_ID) {
// empire has nowhere to stockpile food, so has no stockpile.
// empire has nowhere to stockpile production, so has no stockpile.
pool->SetStockpile(0.0);
//Logger().debugStream() << "no mineral stockpile location. stockpile is set to 0.0";

Expand Down Expand Up @@ -2555,82 +2553,6 @@ void Empire::CheckProductionProgress() {
void Empire::CheckTradeSocialProgress()
{ m_resource_pools[RE_TRADE]->SetStockpile(m_resource_pools[RE_TRADE]->TotalAvailable() - m_maintenance_total_cost); }

void Empire::CheckGrowthFoodProgress() {
Logger().debugStream() << "========Empire::CheckGrowthFoodProgress=======";

boost::shared_ptr<ResourcePool> pool = m_resource_pools[RE_FOOD];
const PopulationPool& pop_pool = m_population_pool; // adding a reference to a member variable of this object for consistency with other implementation of this code in MapWnd

int stockpile_object_id = pool->StockpileObjectID();

//Logger().debugStream() << "food stockpile object id: " << stockpile_object_id;

if (stockpile_object_id == INVALID_OBJECT_ID) {
// empire has nowhere to stockpile food, so has no stockpile.
pool->SetStockpile(0.0);

} else {
// find total food allocated to group that has access to stockpile...

// first find the set of objects that contains the stockpile object
std::map<std::set<int>, double> food_sharing_groups = pool->Available(); // don't actually need the available PP; just using the map as a set of sets of systems
std::set<int> stockpile_group_object_ids;
//Logger().debugStream() << "trying to find stockpile object group... stockpile object has id: " << stockpile_object_id;
for (std::map<std::set<int>, double>::const_iterator it = food_sharing_groups.begin();
it != food_sharing_groups.end(); ++it)
{
const std::set<int>& group = it->first; // get group
//Logger().debugStream() << "potential group:";
//for (std::set<int>::const_iterator qit = group.begin(); qit != group.end(); ++qit)
//Logger().debugStream() << "...." << *qit;

if (group.find(stockpile_object_id) != group.end()) { // check for stockpile object
stockpile_group_object_ids = group;
//Logger().debugStream() << "Empire::CheckGrowthFoodProgress found group of objects for stockpile object. size: " << stockpile_group_object_ids.size();
break;
}

//Logger().debugStream() << "didn't find in group... trying next.";
}

const std::vector<int>& pop_centers = pop_pool.PopCenterIDs();

double stockpile_group_food_allocation = 0.0;

// go through population pool, adding up food allocation of popcenters
// that are in the group of objects that can access the stockpile
for (std::vector<int>::const_iterator it = pop_centers.begin(); it != pop_centers.end(); ++it) {
int object_id = *it;
if (stockpile_group_object_ids.find(object_id) == stockpile_group_object_ids.end())
continue;

const UniverseObject* obj = GetUniverseObject(*it);
if (!obj) {
Logger().debugStream() << "Empire::CheckGrowthFoodProgress couldn't get an object with id " << object_id;
continue;
}
const PopCenter* pop = dynamic_cast<const PopCenter*>(obj);
if (!pop) {
Logger().debugStream() << "Empire::CheckGrowthFoodProgress couldn't cast a UniverseObject* to an PopCenter*";
continue;
}

stockpile_group_food_allocation += pop->AllocatedFood(); // finally add allocation for this PopCenter
//Logger().debugStream() << "object " << obj->Name() << " is in stockpile object group that has " << pop->AllocatedFood() << " food allocated to it";
}

double stockpile_object_group_available = pool->GroupAvailable(stockpile_object_id);
//Logger().debugStream() << "food available in stockpile group is: " << stockpile_object_group_available;
//Logger().debugStream() << "food allocation in stockpile group is: " << stockpile_group_food_allocation;

//Logger().debugStream() << "Old stockpile was " << pool->Stockpile();

double new_stockpile = stockpile_object_group_available - stockpile_group_food_allocation;
pool->SetStockpile(new_stockpile);
//Logger().debugStream() << "New stockpile is: " << new_stockpile;
}
}

void Empire::SetColor(const GG::Clr& color)
{ m_color = color; }

Expand All @@ -2655,15 +2577,13 @@ void Empire::InitResourcePools() {
}
m_population_pool.SetPopCenters(popcenter_ids_vec);
m_resource_pools[RE_MINERALS]->SetObjects(object_ids_vec);
m_resource_pools[RE_FOOD]->SetObjects(object_ids_vec);
m_resource_pools[RE_RESEARCH]->SetObjects(object_ids_vec);
m_resource_pools[RE_INDUSTRY]->SetObjects(object_ids_vec);
m_resource_pools[RE_TRADE]->SetObjects(object_ids_vec);


// inform the blockadeable resource pools about systems that can share
m_resource_pools[RE_MINERALS]->SetConnectedSupplyGroups(m_resource_supply_groups);
m_resource_pools[RE_FOOD]->SetConnectedSupplyGroups(m_resource_supply_groups);
m_resource_pools[RE_INDUSTRY]->SetConnectedSupplyGroups(m_resource_supply_groups);


Expand All @@ -2680,7 +2600,6 @@ void Empire::InitResourcePools() {

// set stockpile object locations for each resource, ensuring those systems exist
std::vector<ResourceType> res_type_vec;
res_type_vec.push_back(RE_FOOD);
res_type_vec.push_back(RE_MINERALS);
res_type_vec.push_back(RE_INDUSTRY);
res_type_vec.push_back(RE_TRADE);
Expand All @@ -2702,7 +2621,6 @@ void Empire::UpdateResourcePools() {
UpdateResearchQueue();
UpdateProductionQueue();
UpdateTradeSpending();
UpdateFoodDistribution();
UpdatePopulationGrowth();
}

Expand Down Expand Up @@ -2744,114 +2662,5 @@ void Empire::UpdateTradeSpending() {
m_resource_pools[RE_TRADE]->ChangedSignal();
}

void Empire::UpdateFoodDistribution() {
Logger().debugStream() << "======= Food distribution for empire: " << EmpireID() << " =======";

m_resource_pools[RE_FOOD]->Update(); // recalculate total food production

// get sets of resource-sharing objects and amount of resource available in
// each, and distribute food within each group independently
std::map<std::set<int>, double> groups_food_available = m_resource_pools[RE_FOOD]->Available();
for (std::map<std::set<int>, double>::iterator groups_it = groups_food_available.begin();
groups_it != groups_food_available.end(); ++groups_it)
{
const std::set<int>& group_object_ids = groups_it->first;

// get subset of group objects that are PopCenters
std::vector<std::pair<UniverseObject*, PopCenter*> > pop_in_group;
for (std::set<int>::const_iterator obj_it = group_object_ids.begin(); obj_it != group_object_ids.end(); ++obj_it)
if (UniverseObject* obj = GetUniverseObject(*obj_it))
if (PopCenter* pop = dynamic_cast<PopCenter*>(obj))
pop_in_group.push_back(std::make_pair(obj, pop));


//Logger().debugStream() << " !! Zeroth Pass Food Distribution";
// clear food allocations to all PopCenters to start, so that if no
// further allocations occur due to insufficient food being
// available, previous turns or iterations' allocations won't be left
for (std::vector<std::pair<UniverseObject*, PopCenter*> >::const_iterator pop_it = pop_in_group.begin();
pop_it != pop_in_group.end(); ++pop_it)
{
pop_it->second->SetAllocatedFood(0.0);
//Logger().debugStream() << "allocating 0.0 food to " << pop_center_objects[*pop_it]->Name() << " to initialize";
}


double food_available = groups_it->second;
//Logger().debugStream() << "group has " << food_available << " food available for allocation";


//Logger().debugStream() << " !! First Pass Food Distribution";

// first pass: give food to PopCenters that produce food, limited by their food need and their food production
for (std::vector<std::pair<UniverseObject*, PopCenter*> >::const_iterator pop_it = pop_in_group.begin();
pop_it != pop_in_group.end(); ++pop_it)
{
if (food_available <= 0.0)
break;

double need = pop_it->second->CurrentMeterValue(METER_FOOD_CONSUMPTION);
double prod = 0.0;
if (pop_it->first->GetMeter(METER_FARMING))
prod = pop_it->first->CurrentMeterValue(METER_FARMING); // preferential allocation for food producers

// allocate food to this PopCenter, deduct from pool, add to total food distribution tally
double allocation = std::min(std::min(need, prod), food_available);

//Logger().debugStream() << "allocating " << allocation << " food to " << pop_center_objects[pc]->Name() << " limited by need and by production";

pop_it->second->SetAllocatedFood(allocation);
food_available -= allocation;
}

//Logger().debugStream() << " !! Second Pass Food Distribution";

// second pass: give as much food as needed to PopCenters to maintain current population
for (std::vector<std::pair<UniverseObject*, PopCenter*> >::const_iterator pop_it = pop_in_group.begin();
pop_it != pop_in_group.end(); ++pop_it)
{
if (food_available <= 0.0)
break;

double need = pop_it->second->CurrentMeterValue(METER_FOOD_CONSUMPTION);
double has = pop_it->second->AllocatedFood();
double addition = std::min(std::max(need - has, 0.0), food_available);
double new_allocation = has + addition;

//Logger().debugStream() << "allocating " << new_allocation << " food to " << pop_center_objects[pc]->Name() << " limited by need (to maintain population)";

pop_it->second->SetAllocatedFood(new_allocation);
food_available -= addition;
}

//Logger().debugStream() << " !! Third Pass Food Distribution";

// third pass: give as much food as needed to PopCenters to allow max possible growth
for (std::vector<std::pair<UniverseObject*, PopCenter*> >::const_iterator pop_it = pop_in_group.begin();
pop_it != pop_in_group.end(); ++pop_it)
{
if (food_available <= 0.0)
break;

double most_needed_to_grow = pop_it->second->FoodAllocationForMaxGrowth();
double has = pop_it->second->AllocatedFood();
double extra_needed_for_max_growth = most_needed_to_grow - has;
double addition = std::min(std::max(extra_needed_for_max_growth, 0.0), food_available);
double new_allocation = has + addition;

//Logger().debugStream() << "allocating " << new_allocation << " food to " << pop_center_objects[pc]->Name() << " to allow max possible growth";

pop_it->second->SetAllocatedFood(new_allocation);
food_available -= addition;
}
}


// after changing food distribution, population growth predictions may need to be redone
// by calling UpdatePopulationGrowth()

m_resource_pools[RE_FOOD]->ChangedSignal();
}

void Empire::UpdatePopulationGrowth()
{ m_population_pool.Update(); }
18 changes: 5 additions & 13 deletions Empire/Empire.h
Expand Up @@ -341,8 +341,8 @@ class Empire
const std::set<std::pair<int, int> >& FleetSupplyStarlaneTraversals() const; ///< returns set of directed starlane traversals along which supply can flow. results are pairs of system ids of start and end system of traversal
const std::map<int, int>& FleetSupplyRanges() const; ///< returns map from system id to number of starlane jumps away the system can deliver fleet supply

const std::set<std::set<int> >& ResourceSupplyGroups() const; ///< returns set of sets of systems that can share food, industry and minerals (systems in separate groups are blockaded or otherwise separated)
const std::set<std::pair<int, int> >& ResourceSupplyStarlaneTraversals() const; ///< returns set of directed starlane traversals along which system resource exchange (food, industry, minerals) can flow. results are pairs of system ids of start and end of traversal
const std::set<std::set<int> >& ResourceSupplyGroups() const; ///< returns set of sets of systems that can share industry and minerals (systems in separate groups are blockaded or otherwise separated)
const std::set<std::pair<int, int> >& ResourceSupplyStarlaneTraversals() const; ///< returns set of directed starlane traversals along which system resource exchange (industry, minerals) can flow. results are pairs of system ids of start and end of traversal
const std::set<std::pair<int, int> >& ResourceSupplyOstructedStarlaneTraversals() const; ///< returns set of directed starlane traversals along which system resources could flow for this empire, but which can't due to some obstruction in the destination system
const std::map<int, int>& ResourceSupplyRanges() const; ///< returns map from system id to number of starlane jumps away the system can exchange resources

Expand Down Expand Up @@ -493,8 +493,6 @@ class Empire
* Currently: Deducts cost of maintenance of buildings from empire's trade stockpile */
void CheckTradeSocialProgress();

/** Updates food stockpile. Growth actually occurs in PopGrowthProductionResearchPhase() of objects */
void CheckGrowthFoodProgress();

void SetColor(const GG::Clr& color); ///< Mutator for empire color
void SetName(const std::string& name); ///< Mutator for empire name
Expand All @@ -510,7 +508,7 @@ class Empire

/** Resets production of resources and calculates allocated resources (on each item in
* queues and overall) for each resource by calling UpdateResearchQueue, UpdateProductionQueue,
* UpdateTradeSpending, and UpdateFoodDistribution. Does not actually "spend" resources,
* UpdateTradeSpending. Does not actually "spend" resources,
* but just determines how much and on what to spend. Actual consumption of resources, removal
* of items from queue, processing of finished items and population growth happens in various
* Check(Whatever)Progress functions. */
Expand All @@ -530,14 +528,8 @@ class Empire
* Currently: Sums maintenance costs of all buildings owned by empire, sets m_maintenance_total_cost */
void UpdateTradeSpending();

/** Allocates available food to PopCenters. Doesn't actually distribute food; just calculates how
* how much food each PopCenter gets. Does not automatically update population growth estimates,
* so UpdatePopulationGrowth() may need to be called after calling this function. */
void UpdateFoodDistribution();

/** Has m_population_pool recalculate all PopCenters' and empire's total expected population growth
* Assumes UpdateFoodDistribution() has been called to determine food allocations to each planet (which
* are a factor in the growth prediction calculation). */
/** Has m_population_pool recalculate all PopCenters' and empire's total
* expected population growth */
void UpdatePopulationGrowth();
//@}

Expand Down
2 changes: 1 addition & 1 deletion Empire/ResourcePool.cpp
Expand Up @@ -185,7 +185,7 @@ void ResourcePool::Update() {
// if object's system is not in a system group, add it as its
// own entry in m_connected_object_groups_resource_production
// this will allow the object to use its own locally produced
// resource when, for instance, distributing food
// resource when, for instance, distributing pp
if (object_system_group.empty()) {
object_system_group.insert(object_id); // just use this already-available set to store the object id, even though it is not likely actually a system
double obj_output = obj->GetMeter(meter_type) ? obj->CurrentMeterValue(meter_type) : 0.0;
Expand Down
5 changes: 2 additions & 3 deletions Empire/ResourcePool.h
Expand Up @@ -16,9 +16,8 @@ class UniverseObject;
class Empire;

/** The ResourcePool class keeps track of an empire's stockpile and production
* of a particular resource (food, minerals, trade, research or industry). */
class ResourcePool
{
* of a particular resource (minerals, trade, research or industry). */
class ResourcePool {
public:
/** \name Structors */ //@{
ResourcePool(ResourceType type);
Expand Down
2 changes: 1 addition & 1 deletion UI/CUIControls.h
Expand Up @@ -338,7 +338,7 @@ struct CUISimpleDropDownListRow : public GG::ListBox::Row {
};

/** Encapsulates an icon and text that goes with it in a single control. For
* example, "[food icon] +1" or "[population icon] 66 (+5)", where [... icon]
* example, "[trade icon] +1" or "[population icon] 66 (+5)", where [... icon]
* is an icon image, not text.
* The icon may have one or two numerical values. If one, just that number is
* displayed. If two, the first number is displayed followed by the second in
Expand Down

0 comments on commit 4fea9eb

Please sign in to comment.