Skip to content

Commit

Permalink
Improved reference province code, and fixed a GSM save game issue
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrettin committed May 27, 2016
1 parent 82e851b commit 637aeb5
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 49 deletions.
4 changes: 1 addition & 3 deletions src/include/grand_strategy.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ class CGrandStrategyProvince : public CProvince
{
public:
CGrandStrategyProvince() : CProvince(),
Civilization(-1), ReferenceProvince(-1), CurrentConstruction(-1),
Civilization(-1), CurrentConstruction(-1),
TotalUnits(0), TotalWorkers(0), PopulationGrowthProgress(0), FoodConsumption(0), Labor(0),
MilitaryScore(0), OffensiveMilitaryScore(0), AttackingMilitaryScore(0),
Movement(false),
Expand Down Expand Up @@ -195,7 +195,6 @@ class CGrandStrategyProvince : public CProvince
CGrandStrategyHero *GetRandomAuthor();

int Civilization; /// Civilization of the province (-1 = no one).
int ReferenceProvince; /// Reference province, if a water province (used for name changing) (-1 = none).
int CurrentConstruction; /// Building currently under construction (unit type index).
int TotalUnits; /// Total quantity of units in the province
int TotalWorkers; /// Total quantity of workers in the province
Expand Down Expand Up @@ -577,7 +576,6 @@ extern void SetProvinceCivilization(std::string province_name, std::string civil
extern void SetProvinceSettlementLocation(std::string province_name, int x, int y);
extern void SetProvinceCulturalName(std::string province_name, std::string civilization_name, std::string province_cultural_name);
extern void SetProvinceFactionCulturalName(std::string province_name, std::string civilization_name, std::string faction_name, std::string province_cultural_name);
extern void SetProvinceReferenceProvince(std::string province_name, std::string reference_province_name);
extern void SetProvinceSettlementBuilding(std::string province_name, std::string settlement_building_ident, bool has_settlement_building);
extern void SetProvinceCurrentConstruction(std::string province_name, std::string settlement_building_ident);
extern void SetProvincePopulation(std::string province_name, int quantity);
Expand Down
3 changes: 2 additions & 1 deletion src/include/province.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ class CProvince
{
public:
CProvince() :
ID(-1),
ID(-1), ReferenceProvince(-1),
Water(false), Coastal(false), SettlementLocation(-1, -1),
World(NULL)
{
Expand All @@ -126,6 +126,7 @@ class CProvince
std::string Map;
std::string SettlementTerrain;
int ID; /// ID of this province
int ReferenceProvince; /// Used by water provinces to see based on which province should they use which cultural name
bool Water; /// Whether the province is a water province or not
bool Coastal; /// Whether the province is a coastal province or not
Vec2i SettlementLocation; /// In which tile the province's settlement is located
Expand Down
81 changes: 40 additions & 41 deletions src/stratagus/grand_strategy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2867,6 +2867,8 @@ void CGrandStrategyProvince::SetUnitQuantity(int unit_type_id, int quantity)
return;
}

// fprintf(stderr, "Setting the quantity of unit type \"%s\" to %d in the %s province.\n", UnitTypes[unit_type_id]->Ident.c_str(), quantity, this->Name.c_str());

quantity = std::max(0, quantity);

int change = quantity - this->Units[unit_type_id];
Expand Down Expand Up @@ -6434,16 +6436,6 @@ void SetProvinceFactionCulturalName(std::string province_name, std::string civil
}
}

void SetProvinceReferenceProvince(std::string province_name, std::string reference_province_name)
{
int province_id = GetProvinceId(province_name);
int reference_province_id = GetProvinceId(reference_province_name);

if (province_id != -1 && GrandStrategyGame.Provinces[province_id] && reference_province_id != -1) {
GrandStrategyGame.Provinces[province_id]->ReferenceProvince = reference_province_id;
}
}

void SetProvinceSettlementBuilding(std::string province_name, std::string settlement_building_ident, bool has_settlement_building)
{
int province_id = GetProvinceId(province_name);
Expand Down Expand Up @@ -7263,6 +7255,9 @@ void InitializeGrandStrategyProvinces()
GrandStrategyGame.Provinces.push_back(province);
province->Name = Provinces[i]->Name;
province->World = Provinces[i]->World;
if (Provinces[i]->ReferenceProvince != -1) {
province->ReferenceProvince = GetProvinceId(Provinces[Provinces[i]->ReferenceProvince]->Name);
}
province->Water = Provinces[i]->Water;
province->Coastal = Provinces[i]->Coastal;
province->SettlementLocation = Provinces[i]->SettlementLocation;
Expand Down Expand Up @@ -7378,42 +7373,44 @@ void FinalizeGrandStrategyInitialization()
CGrandStrategyProvince *province = GrandStrategyGame.Provinces[i];
CProvince *base_province = GetProvince(province->Name);

// add historical population from regions to provinces here, for a lack of a better place (all province's region belongings need to be defined before this takes place, so this can't happen during the province definitions)
for (size_t j = 0; j < base_province->Regions.size(); ++j) {
for (std::map<int, int>::iterator iterator = base_province->Regions[j]->HistoricalPopulation.begin(); iterator != base_province->Regions[j]->HistoricalPopulation.end(); ++iterator) {
if (base_province->HistoricalPopulation.find(iterator->first) == base_province->HistoricalPopulation.end()) { // if the province doesn't have historical population information for a given year but the region does, then use the region's population quantity divided by the number of provinces the region has
base_province->HistoricalPopulation[iterator->first] = iterator->second / base_province->Regions[j]->Provinces.size();
if (GrandStrategyGameLoading == false) {
// add historical population from regions to provinces here, for a lack of a better place (all province's region belongings need to be defined before this takes place, so this can't happen during the province definitions)
for (size_t j = 0; j < base_province->Regions.size(); ++j) {
for (std::map<int, int>::iterator iterator = base_province->Regions[j]->HistoricalPopulation.begin(); iterator != base_province->Regions[j]->HistoricalPopulation.end(); ++iterator) {
if (base_province->HistoricalPopulation.find(iterator->first) == base_province->HistoricalPopulation.end()) { // if the province doesn't have historical population information for a given year but the region does, then use the region's population quantity divided by the number of provinces the region has
base_province->HistoricalPopulation[iterator->first] = iterator->second / base_province->Regions[j]->Provinces.size();
}
}
}
}

for (std::map<int, int>::reverse_iterator iterator = base_province->HistoricalPopulation.rbegin(); iterator != base_province->HistoricalPopulation.rend(); ++iterator) {
if (GrandStrategyYear >= iterator->first) {
province->SetPopulation(iterator->second);
break;
}
}

for (std::map<int, std::map<int, bool>>::iterator iterator = base_province->HistoricalSettlementBuildings.begin(); iterator != base_province->HistoricalSettlementBuildings.end(); ++iterator) {
for (std::map<int, bool>::reverse_iterator second_iterator = iterator->second.rbegin(); second_iterator != iterator->second.rend(); ++second_iterator) {
if (GrandStrategyYear >= second_iterator->first) {
province->SetSettlementBuilding(iterator->first, second_iterator->second);

for (std::map<int, int>::reverse_iterator iterator = base_province->HistoricalPopulation.rbegin(); iterator != base_province->HistoricalPopulation.rend(); ++iterator) {
if (GrandStrategyYear >= iterator->first) {
province->SetPopulation(iterator->second);
break;
}
}
}

for (std::map<CUpgrade *, std::map<int, bool>>::iterator iterator = base_province->HistoricalModifiers.begin(); iterator != base_province->HistoricalModifiers.end(); ++iterator) {
for (std::map<int, bool>::reverse_iterator second_iterator = iterator->second.rbegin(); second_iterator != iterator->second.rend(); ++second_iterator) {
if (GrandStrategyYear >= second_iterator->first) {
province->SetModifier(iterator->first, second_iterator->second);
break;
for (std::map<int, std::map<int, bool>>::iterator iterator = base_province->HistoricalSettlementBuildings.begin(); iterator != base_province->HistoricalSettlementBuildings.end(); ++iterator) {
for (std::map<int, bool>::reverse_iterator second_iterator = iterator->second.rbegin(); second_iterator != iterator->second.rend(); ++second_iterator) {
if (GrandStrategyYear >= second_iterator->first) {
province->SetSettlementBuilding(iterator->first, second_iterator->second);
break;
}
}
}
}

if (province->Coastal && province->Tiles.size() == 1) { //if the province is a 1-tile island, it has to start with a port in its capital to feed itself
GrandStrategyGame.WorldMapTiles[province->SettlementLocation.x][province->SettlementLocation.y]->SetPort(true);

for (std::map<CUpgrade *, std::map<int, bool>>::iterator iterator = base_province->HistoricalModifiers.begin(); iterator != base_province->HistoricalModifiers.end(); ++iterator) {
for (std::map<int, bool>::reverse_iterator second_iterator = iterator->second.rbegin(); second_iterator != iterator->second.rend(); ++second_iterator) {
if (GrandStrategyYear >= second_iterator->first) {
province->SetModifier(iterator->first, second_iterator->second);
break;
}
}
}

if (province->Coastal && province->Tiles.size() == 1) { //if the province is a 1-tile island, it has to start with a port in its capital to feed itself
GrandStrategyGame.WorldMapTiles[province->SettlementLocation.x][province->SettlementLocation.y]->SetPort(true);
}
}

if (province->Civilization != -1 && province->Owner != NULL) {
Expand Down Expand Up @@ -7453,8 +7450,10 @@ void FinalizeGrandStrategyInitialization()


if (province->Civilization != -1 && province->FoodConsumption > province->GetFoodCapacity()) { // remove workers if there are so many the province will starve
province->ChangeUnitQuantity(province->GetClassUnitType(GetUnitTypeClassIndexByName("worker")), ((province->GetFoodCapacity() - province->FoodConsumption) / FoodConsumptionPerWorker));
province->ReallocateLabor();
if (GrandStrategyGameLoading == false) {
province->ChangeUnitQuantity(province->GetClassUnitType(GetUnitTypeClassIndexByName("worker")), ((province->GetFoodCapacity() - province->FoodConsumption) / FoodConsumptionPerWorker));
province->ReallocateLabor();
}
}

for (size_t j = 0; j < province->Tiles.size(); ++j) { // generate cultural names for tiles (since this isn't done throughout the history to increase performance)
Expand Down
3 changes: 0 additions & 3 deletions src/stratagus/script.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2924,9 +2924,6 @@ void SaveGrandStrategyGame(const std::string &filename)
if (GrandStrategyGame.Provinces[i]->Owner != NULL) {
fprintf(fd, "SetProvinceOwner(\"%s\", \"%s\", \"%s\")\n", GrandStrategyGame.Provinces[i]->Name.c_str(), PlayerRaces.Name[GrandStrategyGame.Provinces[i]->Owner->Civilization].c_str(), PlayerRaces.Factions[GrandStrategyGame.Provinces[i]->Owner->Civilization][GrandStrategyGame.Provinces[i]->Owner->Faction]->Name.c_str()); //save province owner
}
if (GrandStrategyGame.Provinces[i]->ReferenceProvince != -1) {
fprintf(fd, "SetProvinceReferenceProvince(\"%s\", \"%s\")\n", GrandStrategyGame.Provinces[i]->Name.c_str(), GrandStrategyGame.Provinces[GrandStrategyGame.Provinces[i]->ReferenceProvince]->Name.c_str()); //save the province's reference province (for water provinces' cultural name)
}
if (GrandStrategyGame.Provinces[i]->AttackedBy != NULL) {
fprintf(fd, "SetProvinceAttackedBy(\"%s\", \"%s\", \"%s\")\n", GrandStrategyGame.Provinces[i]->Name.c_str(), PlayerRaces.Name[GrandStrategyGame.Provinces[i]->AttackedBy->Civilization].c_str(), PlayerRaces.Factions[GrandStrategyGame.Provinces[i]->AttackedBy->Civilization][GrandStrategyGame.Provinces[i]->AttackedBy->Faction]->Name.c_str()); //save attacked by
}
Expand Down
7 changes: 7 additions & 0 deletions src/stratagus/script_province.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,13 @@ static int CclDefineProvince(lua_State *l)
province->Map = LuaToString(l, -1);
} else if (!strcmp(value, "SettlementTerrain")) {
province->SettlementTerrain = LuaToString(l, -1);
} else if (!strcmp(value, "ReferenceProvince")) {
CProvince *reference_province = GetProvince(LuaToString(l, -1));
if (reference_province != NULL) {
reference_province->ReferenceProvince = reference_province->ID;
} else {
LuaError(l, "Reference province doesn't exist.");
}
} else if (!strcmp(value, "CulturalNames")) {
if (!lua_istable(l, -1)) {
LuaError(l, "incorrect argument (expected table)");
Expand Down
1 change: 0 additions & 1 deletion src/tolua/grand_strategy.pkg
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ extern void SetProvinceCivilization(std::string province_name, std::string civil
extern void SetProvinceSettlementLocation(std::string province_name, int x, int y);
extern void SetProvinceCulturalName(std::string province_name, std::string civilization_name, std::string province_cultural_name);
extern void SetProvinceFactionCulturalName(std::string province_name, std::string civilization_name, std::string faction_name, std::string province_cultural_name);
extern void SetProvinceReferenceProvince(std::string province_name, std::string reference_province_name);
extern void SetProvinceSettlementBuilding(std::string province_name, std::string settlement_building_ident, bool has_settlement_building);
extern void SetProvinceCurrentConstruction(std::string province_name, std::string settlement_building_ident);
extern void SetProvincePopulation(std::string province_name, int quantity);
Expand Down

0 comments on commit 637aeb5

Please sign in to comment.