int City::get_resource_consumption(Resource res) { int ret = 0; for (int i = CIT_PEASANT; i <= CIT_BURGHER; i++) { ret += population[i].consumption[res]; if (res == RES_FOOD) { // Special case ret += get_food_consumption( Citizen_type(i) ); } } return ret; }
// type defaults to CIT_NULL int City::get_food_consumption(Citizen_type type) { // TODO: Modify this based on our race. if (type == CIT_NULL) { int ret = 0; for (int i = 1; i < CIT_MAX; i++) { int a = get_food_consumption( Citizen_type(i) ); ret += a; } return ret; } // citizen_food_consumption() is defined in citizen.cpp. It returns the amount // per 100 citizens, so naturally we need to divide by 100. int ret = (population[type].count * citizen_food_consumption( type )) / 100; // Modify based on race - its food_consumption is a percentage. ret = (ret * Race_data[race]->food_consumption) / 100; return ret; }
int City::get_food_cap() { int food_expiration = 25; // How many days does it take our food to go bad? // TODO: Allow modifiers to food_expiration. return food_expiration * get_food_consumption(); }
int AI_city::get_net_food() { return resource_production[RES_FOOD] - get_food_consumption(); }
void AI_city::setup_resource_production(World_map* world) { if (!world) { debugmsg("AI_city::setup_resource_production(NULL) called."); return; } resource_production.clear(); mineral_production.clear(); // Data for our chosen role. City_role_datum* role_dat = City_role_data[role]; // A list of all tiles that are available for us to exploit. std::vector<Map_tile*> tiles; for (int x = 0 - radius; x <= radius; x++) { for (int y = 0 - radius; y <= radius; y++) { int mx = CITY_MAP_SIZE / 2 + x, my = CITY_MAP_SIZE / 2 + y; Map_tile* tmp_tile = map.get_tile(mx, my); if (!tmp_tile) { debugmsg("AI_city's map returned NULL for get_tile(%d, %d)!", mx, my); } else { tiles.push_back( map.get_tile(mx, my) ); } } } // Figure out the food that we need... int food_req = get_food_consumption(); /* We multiply by 10,000 to avoid rounding errors. 10,000 is the product of: * 100 - terrain farmability is a percentage (0 to 100) * 100 - crop food output is per 100 units of the crop. * We also multiply by our chosen role's food_percentage, then divide by 100. * This is because some roles aim to produce more food than we require, while * others produce less and assume we'll import the deficit. */ food_req = (food_req * 10000 * role_dat->food_percentage) / 100; // TODO: If there's a nearby, friendly CITY_ROLE_FARMING city, decrease food_req // Now try to match the required food. // ** FARMING ** int farm_skill = Race_data[race]->skill_level[SKILL_FARMING]; if (farm_skill >= 2) { add_farms(tiles, food_req); } // if (farm_skill > 0) if (food_req > 0) { // Oh no, we still need food! // We can get food from hunting or livestock. Which are we better at? int hunting_skill = Race_data[race]->skill_level[SKILL_HUNTING]; int livestock_skill = Race_data[race]->skill_level[SKILL_LIVESTOCK]; bool added_hunting_camps = false; // Hunting is generally better than keeping livestock, so use it in a tie. if (hunting_skill >= 2 && hunting_skill >= livestock_skill) { added_hunting_camps = true; add_hunting_camps(tiles, food_req); } // If we still need some food, add some livestock! if (food_req > 0 && livestock_skill >= 2) { add_pastures(tiles, food_req); } // Finally, if we didn't add hunting camps on our first try, add them now. if (!added_hunting_camps && food_req > 0 && hunting_skill >= 2) { add_hunting_camps(tiles, food_req); } } // Okay! We are done with setting up food production. // Now set up some production based on our City_role. switch (role) { case CITY_ROLE_NULL: // We can add more buildings! // TODO: Put this in a function and make it more good-er. areas_built[AREA_MARKETPLACE] = 3; break; case CITY_ROLE_FARMING: add_farms(tiles); break; case CITY_ROLE_HUNTING: add_hunting_camps(tiles); break; case CITY_ROLE_LIVESTOCK: add_pastures(tiles); break; case CITY_ROLE_MINING: add_mines(tiles); break; case CITY_ROLE_LOGGING: add_sawmills(tiles); break; } // switch (role) // Finally, add some buildings to create more advanced resources. bool adding_buildings = (get_net_buildings_supported() > 0 && add_random_building()); while (get_net_buildings_supported() > 0 && adding_buildings) { adding_buildings = add_random_building(); } }