1 //       _________ __                 __
2 //      /   _____//  |_____________ _/  |______     ____  __ __  ______
3 //      \_____  \\   __\_  __ \__  \\   __\__  \   / ___\|  |  \/  ___/
4 //      /        \|  |  |  | \// __ \|  |  / __ \_/ /_/  >  |  /\___ |
5 //     /_______  /|__|  |__|  (____  /__| (____  /\___  /|____//____  >
6 //             \/                  \/          \//_____/            \/
7 //  ______________________                           ______________________
8 //                        T H E   W A R   B E G I N S
9 //         Stratagus - A free fantasy real time strategy game engine
10 //
11 /**@name grand_strategy.h - The grand strategy header file. */
12 //
13 //      (c) Copyright 2015-2019 by Andrettin
14 //
15 //      This program is free software; you can redistribute it and/or modify
16 //      it under the terms of the GNU General Public License as published by
17 //      the Free Software Foundation; only version 2 of the License.
18 //
19 //      This program is distributed in the hope that it will be useful,
20 //      but WITHOUT ANY WARRANTY; without even the implied warranty of
21 //      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22 //      GNU General Public License for more details.
23 //
24 //      You should have received a copy of the GNU General Public License
25 //      along with this program; if not, write to the Free Software
26 //      Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
27 //      02111-1307, USA.
28 //
29 
30 #ifndef __GRAND_STRATEGY_H__
31 #define __GRAND_STRATEGY_H__
32 
33 /*----------------------------------------------------------------------------
34 --  Includes
35 ----------------------------------------------------------------------------*/
36 
37 #include <vector>
38 
39 #include "map/map.h"
40 #include "province.h"
41 #include "character.h"
42 #include "vec2i.h"
43 #include "video.h"
44 #include "player.h"
45 #include "upgrade/upgrade_structs.h"
46 
47 /*----------------------------------------------------------------------------
48 --  Declarations
49 ----------------------------------------------------------------------------*/
50 
51 #define BasePopulationGrowthPermyriad 12					/// Base population growth per 10,000
52 #define FoodConsumptionPerWorker 100
53 
54 class CGrandStrategyProvince;
55 class CGrandStrategyFaction;
56 class CGrandStrategyHero;
57 class LuaCallback;
58 
59 /**
60 **  Indexes into diplomacy state array.
61 */
62 enum DiplomacyStates {
63 	DiplomacyStateAlliance,
64 	DiplomacyStatePeace,
65 	DiplomacyStateWar,
66 	DiplomacyStateOverlord,
67 	DiplomacyStateVassal,
68 
69 	MaxDiplomacyStates
70 };
71 
72 class GrandStrategyWorldMapTile : public WorldMapTile
73 {
74 public:
GrandStrategyWorldMapTile()75 	GrandStrategyWorldMapTile() : WorldMapTile(),
76 		Port(false),
77 		Province(nullptr), BaseTile(nullptr), GraphicTile(nullptr), ResourceBuildingGraphics(nullptr), ResourceBuildingGraphicsPlayerColor(nullptr)
78 	{
79 		memset(Borders, 0, sizeof(Borders));
80 	}
81 
82 	bool Port;								/// Whether the tile has a port
83 	std::string Name;						/// Name of the tile (used for instance to name particular mountains)
84 	CGrandStrategyProvince *Province;		/// Province to which the tile belongs
85 	CGraphic *BaseTile;
86 	CGraphic *GraphicTile;					/// The tile image used by this tile
87 	CGraphic *ResourceBuildingGraphics;
88 	CPlayerColorGraphic *ResourceBuildingGraphicsPlayerColor;
89 	bool Borders[MaxDirections];			/// Whether this tile borders a tile of another province to a particular direction
90 };
91 
92 class CGrandStrategyProvince : public CProvince
93 {
94 public:
CGrandStrategyProvince()95 	CGrandStrategyProvince() : CProvince(),
96 		Civilization(-1),
97 		TotalUnits(0), TotalWorkers(0), PopulationGrowthProgress(0), FoodConsumption(0), Labor(0),
98 		MilitaryScore(0), OffensiveMilitaryScore(0), AttackingMilitaryScore(0),
99 		Movement(false),
100 		Owner(nullptr),
101 		Governor(nullptr)
102 	{
103 		memset(SettlementBuildings, 0, sizeof(SettlementBuildings));
104 		memset(Units, 0, sizeof(Units));
105 		memset(Income, 0, sizeof(Income));
106 		memset(ProductionCapacity, 0, sizeof(ProductionCapacity));
107 		memset(ProductionCapacityFulfilled, 0, sizeof(ProductionCapacityFulfilled));
108 		memset(ProductionEfficiencyModifier, 0, sizeof(ProductionEfficiencyModifier));
109 	}
110 
111 	void SetOwner(int civilization_id, int faction_id);					/// Set a new owner for the province
112 	void SetSettlementBuilding(int building_id, bool has_settlement_building);
113 	void SetModifier(CUpgrade *modifier, bool has_modifier);
114 	void SetUnitQuantity(int unit_type_id, int quantity);
115 	void ChangeUnitQuantity(int unit_type_id, int quantity);
116 	void SetPopulation(int quantity);
117 	void SetHero(std::string hero_full_name, int value);
118 	void AllocateLabor();
119 	void AllocateLaborToResource(int resource);
120 	void DeallocateLabor();
121 	void ReallocateLabor();
122 	void AddFactionClaim(int civilization_id, int faction_id);
123 	void RemoveFactionClaim(int civilization_id, int faction_id);
124 	bool HasBuildingClass(std::string building_class_name);
125 	bool HasModifier(CUpgrade *modifier);
126 	bool BordersModifier(CUpgrade *modifier);
127 	bool HasFactionClaim(int civilization_id, int faction_id);
128 	bool BordersProvince(CGrandStrategyProvince *province);
129 	bool HasSecondaryBorderThroughWaterWith(CGrandStrategyProvince *province);
130 	bool BordersFaction(int faction_civilization, int faction, bool check_through_water = false);
131 	int GetPopulation();
132 	int GetClassUnitType(int class_id);
133 	int GetDesirabilityRating();
134 	std::string GenerateWorkName();
135 	CGrandStrategyHero *GetRandomAuthor();
136 
137 	int Civilization;													/// Civilization of the province (-1 = no one).
138 	int TotalUnits;														/// Total quantity of units in the province
139 	int TotalWorkers;													/// Total quantity of workers in the province
140 	int PopulationGrowthProgress;										/// Progress of current population growth; when reaching the population growth threshold a new worker unit will be created
141 	int FoodConsumption;												/// How much food the people in the province consume
142 	int Labor;															/// How much labor available this province has
143 	int MilitaryScore;													/// Military score of the forces in the province (including fortifications and militia)
144 	int OffensiveMilitaryScore;											/// Military score of the forces in the province which can attack other provinces
145 	int AttackingMilitaryScore;											/// Military score of the forces attacking the province
146 	bool Movement;														/// Whether a unit or hero is currently moving to the province
147 	CGrandStrategyFaction *Owner;										/// Owner of the province
148 	CGrandStrategyHero *Governor;										/// Governor of this province
149 	bool SettlementBuildings[UnitTypeMax];								/// Buildings in the province; 0 = not constructed, 1 = under construction, 2 = constructed
150 	int Units[UnitTypeMax];												/// Quantity of units of a particular unit type in the province
151 	std::vector<CGrandStrategyHero *> Heroes;							/// Heroes in the province
152 	std::vector<CGrandStrategyHero *> ActiveHeroes;						/// Active (can move, attack and defend) heroes in the province
153 	std::vector<CGrandStrategyProvince *> BorderProvinces;				/// Which provinces this province borders
154 	int Income[MaxCosts];												/// Income for each resource.
155 	int ProductionCapacity[MaxCosts];									/// The province's capacity to produce each resource (1 for each unit of base output)
156 	int ProductionCapacityFulfilled[MaxCosts];							/// How much of the province's production capacity for each resource is actually fulfilled
157 	int ProductionEfficiencyModifier[MaxCosts];							/// Efficiency modifier for each resource.
158 	std::vector<CGrandStrategyFaction *> Claims;						/// Factions which have a claim to this province
159 	std::vector<Vec2i> ResourceTiles[MaxCosts];							/// Resources tiles in the province
160 	std::vector<CUpgrade *> Modifiers;									/// Modifiers affecting the province
161 };
162 
163 class CGrandStrategyFaction
164 {
165 public:
CGrandStrategyFaction()166 	CGrandStrategyFaction() :
167 		Faction(-1), Civilization(-1), FactionTier(FactionTierBarony), GovernmentType(GovernmentTypeMonarchy), Capital(nullptr)
168 	{
169 		memset(Technologies, 0, sizeof(Technologies));
170 		memset(Resources, 0, sizeof(Resources));
171 		memset(Income, 0, sizeof(Income));
172 		memset(ProductionEfficiencyModifier, 0, sizeof(ProductionEfficiencyModifier));
173 		memset(Trade, 0, sizeof(Trade));
174 		memset(MilitaryScoreBonus, 0, sizeof(MilitaryScoreBonus));
175 		memset(Ministers, 0, sizeof(Ministers));
176 	}
177 
178 	void SetTechnology(int upgrade_id, bool has_technology, bool secondary_setting = false);
179 	void SetCapital(CGrandStrategyProvince *province);
180 	void SetDiplomacyState(CGrandStrategyFaction *faction, int diplomacy_state_id);
181 	void SetMinister(int title, std::string hero_full_name);
182 	void MinisterSuccession(int title);
183 	bool IsAlive();
184 	bool HasTechnologyClass(std::string technology_class_name);
185 	bool CanHaveSuccession(int title, bool family_inheritance);
186 	bool IsConquestDesirable(CGrandStrategyProvince *province);
187 	int GetTroopCostModifier();
188 	int GetDiplomacyState(CGrandStrategyFaction *faction);
189 	int GetDiplomacyStateProposal(CGrandStrategyFaction *faction);
190 	std::string GetFullName();
191 	CGrandStrategyProvince *GetRandomProvinceWeightedByPopulation();
192 
193 	int Faction;														/// The faction's ID (-1 = none).
194 	int Civilization;													/// Civilization of the faction (-1 = none).
195 	int GovernmentType;													/// Government type of the faction (-1 = none).
196 	int FactionTier;													/// What is the tier of this faction (barony, etc.).
197 	CGrandStrategyProvince *Capital;									/// Capital province of this faction
198 	bool Technologies[UpgradeMax];										/// Whether a faction has a particular technology or not
199 	std::vector<int> OwnedProvinces;									/// Provinces owned by this faction
200 	int Resources[MaxCosts];											/// Amount of each resource stored by the faction.
201 	int Income[MaxCosts];												/// Income of each resource for the faction.
202 	int ProductionEfficiencyModifier[MaxCosts];							/// Efficiency modifier for each resource.
203 	int Trade[MaxCosts];												/// How much of each resource the faction wants to trade; negative values are imports and positive ones exports
204 	int MilitaryScoreBonus[UnitTypeMax];
205 	std::map<CGrandStrategyFaction *, int> DiplomacyStates;				/// Diplomacy states between this faction and other factions
206 	std::map<CGrandStrategyFaction *, int> DiplomacyStateProposals;		/// Diplomacy state being offered by this faction to other factions
207 	CGrandStrategyHero *Ministers[MaxCharacterTitles];					/// Ministers of the faction
208 	std::vector<CGrandStrategyProvince *> Claims;						/// Provinces which this faction claims
209 	std::vector<CGrandStrategyHero *> HistoricalMinisters[MaxCharacterTitles];	/// All characters who had a ministerial (or head of state or government) title in this faction
210 	std::map<CUpgrade *, int> HistoricalTechnologies;					/// historical technologies of the faction, with the year of discovery
211 };
212 
213 class CGrandStrategyHero : public CCharacter
214 {
215 public:
CGrandStrategyHero()216 	CGrandStrategyHero() : CCharacter(),
217 		State(0), Existed(false),
218 		Province(nullptr), ProvinceOfOrigin(nullptr),
219 		Father(nullptr), Mother(nullptr)
220 	{
221 	}
222 
223 	void Die();
224 	void SetType(int unit_type_id);
225 	bool IsAlive();
226 	bool IsVisible();
227 	bool IsGenerated();
228 	bool IsEligibleForTitle(int title);
229 	int GetTroopCostModifier();
230 	int GetTitleScore(int title, CGrandStrategyProvince *province = nullptr);
231 	std::string GetMinisterEffectsString(int title);
232 	std::string GetBestDisplayTitle();
233 	CGrandStrategyFaction *GetFaction();
234 
235 	int State;			/// 0 = hero isn't in the province, 1 = hero is moving to the province, 2 = hero is in the province, 3 = hero is attacking the province, 4 = hero is in the province but not defending it
236 	bool Existed;								/// whether the character has existed in this playthrough
237 	CGrandStrategyProvince *Province;
238 	CGrandStrategyProvince *ProvinceOfOrigin;	/// Province from which the hero originates
239 	CGrandStrategyHero *Father;					/// Character's father
240 	CGrandStrategyHero *Mother;					/// Character's mother
241 	std::vector<CGrandStrategyHero *> Children;	/// Children of the character
242 	std::vector<CGrandStrategyHero *> Siblings;	/// Siblings of the character
243 	std::vector<std::pair<int, CGrandStrategyFaction *>> Titles;	/// Titles of the character (first value is the title type, and the second one is the faction
244 	std::vector<std::pair<int, CGrandStrategyProvince *>> ProvinceTitles;	/// Provincial titles of the character (first value is the title type, and the second one is the province
245 };
246 
247 class CGrandStrategyEvent
248 {
249 public:
CGrandStrategyEvent()250 	CGrandStrategyEvent() :
251 		Persistent(false),
252 		ID(-1), MinYear(0), MaxYear(0), HistoricalYear(0),
253 		World(nullptr),
254 		Conditions(nullptr)
255 	{
256 	}
257 	~CGrandStrategyEvent();
258 
259 	void Trigger(CGrandStrategyFaction *faction);
260 	bool CanTrigger(CGrandStrategyFaction *faction);
261 
262 	std::string Name;
263 	std::string Description;
264 	bool Persistent;
265 	int ID;
266 	int MinYear;
267 	int MaxYear;
268 	int HistoricalYear;
269 	CWorld *World;
270 	LuaCallback *Conditions;
271 	std::vector<std::string> Options;
272 	std::vector<LuaCallback *> OptionConditions;
273 	std::vector<LuaCallback *> OptionEffects;
274 	std::vector<std::string> OptionTooltips;
275 };
276 
277 /**
278 **  Grand Strategy game instance
279 **  Mapped with #GrandStrategy to a symbolic name.
280 */
281 class CGrandStrategyGame
282 {
283 public:
CGrandStrategyGame()284 	CGrandStrategyGame() :
285 		WorldMapWidth(0), WorldMapHeight(0),
286 		PlayerFaction(nullptr)
287 	{
288 		memset(CommodityPrices, 0, sizeof(CommodityPrices));
289 	}
290 
291 	void DrawInterface();					/// Draw the interface
292 	void DoTurn();							/// Process the grand strategy turn
293 	void PerformTrade(CGrandStrategyFaction &importer_faction, CGrandStrategyFaction &exporter_faction, int resource);
294 	void CreateWork(CUpgrade *work, CGrandStrategyHero *author, CGrandStrategyProvince *province);
295 	bool TradePriority(CGrandStrategyFaction &faction_a, CGrandStrategyFaction &faction_b);
296 	CGrandStrategyHero *GetHero(std::string hero_full_name);
297 
298 public:
299 	int WorldMapWidth;
300 	int WorldMapHeight;
301 	std::vector<CGrandStrategyProvince *> Provinces;
302 	std::map<int, std::vector<CGrandStrategyProvince *>> CultureProvinces;	/// provinces belonging to each culture
303 	std::vector<CGrandStrategyFaction *> Factions[MAX_RACES];
304 	std::vector<CGrandStrategyHero *> Heroes;
305 	std::vector<CUpgrade *> UnpublishedWorks;
306 	std::vector<CGrandStrategyEvent *> AvailableEvents;
307 	CGrandStrategyFaction *PlayerFaction;
308 	int CommodityPrices[MaxCosts];								/// price for every 100 of each commodity
309 };
310 
311 /*----------------------------------------------------------------------------
312 -- Variables
313 ----------------------------------------------------------------------------*/
314 
315 extern bool GrandStrategy;								/// if the game is in grand strategy mode
316 extern int GrandStrategyYear;
317 extern std::string GrandStrategyWorld;
318 extern int PopulationGrowthThreshold;					/// How much population growth progress must be accumulated before a new worker unit is created in the province
319 extern CGrandStrategyGame GrandStrategyGame;			/// Grand strategy game
320 extern std::map<std::string, int> GrandStrategyHeroStringToIndex;
321 extern std::vector<CGrandStrategyEvent *> GrandStrategyEvents;
322 extern std::map<std::string, CGrandStrategyEvent *> GrandStrategyEventStringToPointer;
323 
324 /*----------------------------------------------------------------------------
325 -- Functions
326 ----------------------------------------------------------------------------*/
327 
328 extern std::string GetDiplomacyStateNameById(int diplomacy_state);
329 extern int GetDiplomacyStateIdByName(std::string diplomacy_state);
330 extern std::string GetFactionTierNameById(int faction_tier);
331 extern int GetFactionTierIdByName(std::string faction_tier);
332 extern int GetProvinceId(std::string province_name);
333 extern void SetProvinceOwner(std::string province_name, std::string civilization_name, std::string faction_name);
334 extern void SetProvinceSettlementBuilding(std::string province_name, std::string settlement_building_ident, bool has_settlement_building);
335 extern void SetProvinceUnitQuantity(std::string province_name, std::string unit_type_ident, int quantity);
336 extern void ChangeProvinceUnitQuantity(std::string province_name, std::string unit_type_ident, int quantity);
337 extern void SetProvinceHero(std::string province_name, std::string hero_full_name, int value);
338 extern void SetProvinceFood(std::string province_name, int quantity);
339 extern void ChangeProvinceFood(std::string province_name, int quantity);
340 extern void AddProvinceClaim(std::string province_name, std::string civilization_name, std::string faction_name);
341 extern void RemoveProvinceClaim(std::string province_name, std::string civilization_name, std::string faction_name);
342 extern void InitializeGrandStrategyGame(bool show_loading = true);
343 extern void FinalizeGrandStrategyInitialization();
344 extern void SetGrandStrategyWorld(std::string world);
345 extern void DoGrandStrategyTurn();
346 extern bool ProvinceBordersProvince(std::string province_name, std::string second_province_name);
347 extern bool ProvinceBordersFaction(std::string province_name, std::string faction_civilization_name, std::string faction_name);
348 extern bool ProvinceHasBuildingClass(std::string province_name, std::string building_class);
349 extern std::string GetProvinceCivilization(std::string province_name);
350 extern bool GetProvinceSettlementBuilding(std::string province_name, std::string building_ident);
351 extern int GetProvinceUnitQuantity(std::string province_name, std::string unit_type_ident);
352 extern int GetProvinceHero(std::string province_name, std::string hero_full_name);
353 extern int GetProvinceMilitaryScore(std::string province_name, bool attacker, bool count_defenders);
354 extern std::string GetProvinceOwner(std::string province_name);
355 extern void SetFactionGovernmentType(std::string civilization_name, std::string faction_name, std::string government_type_name);
356 extern void SetFactionDiplomacyStateProposal(std::string civilization_name, std::string faction_name, std::string second_civilization_name, std::string second_faction_name, std::string diplomacy_state_name);
357 extern std::string GetFactionDiplomacyStateProposal(std::string civilization_name, std::string faction_name, std::string second_civilization_name, std::string second_faction_name);
358 extern void SetFactionTier(std::string civilization_name, std::string faction_name, std::string faction_tier_name);
359 extern std::string GetFactionTier(std::string civilization_name, std::string faction_name);
360 extern bool IsGrandStrategyUnit(const CUnitType &type);
361 extern bool IsMilitaryUnit(const CUnitType &type);
362 extern void SetFactionMinister(std::string civilization_name, std::string faction_name, std::string title_name, std::string hero_full_name);
363 extern std::string GetFactionMinister(std::string civilization_name, std::string faction_name, std::string title_name);
364 extern void KillGrandStrategyHero(std::string hero_full_name);
365 extern void GrandStrategyHeroExisted(std::string hero_full_name);
366 extern bool GrandStrategyHeroIsAlive(std::string hero_full_name);
367 extern void GrandStrategyWorkCreated(std::string work_ident);
368 extern void MakeGrandStrategyEventAvailable(std::string event_name);
369 extern bool GetGrandStrategyEventTriggered(std::string event_name);
370 extern void CleanGrandStrategyEvents();
371 extern CGrandStrategyEvent *GetGrandStrategyEvent(std::string event_name);
372 extern void GrandStrategyCclRegister();
373 
374 #endif
375