-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStronghold.cpp
More file actions
2193 lines (1846 loc) · 74.3 KB
/
Copy pathStronghold.cpp
File metadata and controls
2193 lines (1846 loc) · 74.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "Stronghold.h"
#include <windows.h> // For Sleep()
using namespace std;
// Cross-platform sleep function
void crossPlatformSleep(int seconds) {
Sleep(seconds * 1000); // Sleep takes milliseconds
}
// ------------------------
// Resource implementations
// ------------------------
Resource::Resource(const string& name, int amount, double value)
: name(name), amount(amount), value(value) {
}
Resource::~Resource() {}
string Resource::getName() const {
return name;
}
int Resource::getAmount() const {
return amount;
}
double Resource::getValue() const {
return value;
}
void Resource::setAmount(int newAmount) {
amount = newAmount < 0 ? 0 : newAmount;
}
void Resource::changeAmount(int delta) {
amount += delta;
if (amount < 0) amount = 0;
}
void Resource::setValue(double newValue) {
value = newValue > 0 ? newValue : 0; // Ensure value is non-negative
}
double Resource::getTotalValue() const {
return amount * value;
}
void Resource::applyEffects(Kingdom& kingdom) {
// Base implementation does nothing
}
// Food implementation
Food::Food(int amount, double value)
: Resource("Food", amount, value) {
}
void Food::applyEffects(Kingdom& kingdom) {
// Food directly affects population happiness
double happinessModifier = 0;
int totalPopulation = kingdom.getPopulation()->getTotal();
if (totalPopulation > 0) {
double foodPerPerson = static_cast<double>(amount) / totalPopulation;
if (foodPerPerson > 1.5) {
happinessModifier = 0.1; // Plenty of food
}
else if (foodPerPerson > 1.0) {
happinessModifier = 0.05; // Adequate food
}
else if (foodPerPerson < 0.5) {
happinessModifier = -0.2; // Shortage
}
else if (foodPerPerson < 0.25) {
happinessModifier = -0.4; // Severe shortage
}
}
double currentHappiness = kingdom.getPopulation()->getHappiness();
kingdom.getPopulation()->setHappiness(currentHappiness + happinessModifier);
}
// Gold implementation
Gold::Gold(int amount, double value)
: Resource("Gold", amount, value) {
}
void Gold::applyEffects(Kingdom& kingdom) {
// Gold is handled by the Economy class
}
// Wood implementation
Wood::Wood(int amount, double value)
: Resource("Wood", amount, value) {
}
void Wood::applyEffects(Kingdom& kingdom) {
// Wood effects might include building capacity, etc.
}
// Stone implementation
Stone::Stone(int amount, double value)
: Resource("Stone", amount, value) {
}
void Stone::applyEffects(Kingdom& kingdom) {
// Stone effects might include building capacity, etc.
}
// Iron implementation
Iron::Iron(int amount, double value)
: Resource("Iron", amount, value) {
}
void Iron::applyEffects(Kingdom& kingdom) {
// Iron affects army equipment quality
if (amount > 100) {
int currentLevel = kingdom.getArmy()->getTrainingLevel();
kingdom.getArmy()->setTrainingLevel(currentLevel + 1);
}
}
// ---------------------
// Leader implementations
// ---------------------
Leader::Leader(const string& name, int charisma, int intelligence, int strength)
: name(name), charisma(charisma), intelligence(intelligence), strength(strength) {
}
Leader::~Leader() {}
string Leader::getName() const {
return name;
}
int Leader::getCharisma() const {
return charisma;
}
int Leader::getIntelligence() const {
return intelligence;
}
int Leader::getStrength() const {
return strength;
}
void Leader::setName(const string& newName) {
name = newName;
}
void Leader::applyEffects(Kingdom& kingdom) {
// Base implementation does nothing
}
// King implementation
King::King(const string& name, int charisma, int intelligence, int strength, int royalBloodline)
: Leader(name, charisma, intelligence, strength), royalBloodline(royalBloodline), yearsInPower(0) {
}
King::~King() {}
int King::getRoyalBloodline() const {
return royalBloodline;
}
int King::getYearsInPower() const {
return yearsInPower;
}
void King::incrementYearsInPower() {
yearsInPower++;
}
void King::specialAction(Kingdom& kingdom) {
// King's royal decree: temporarily boost economy or population
cout << "\nKing " << name << " issues a Royal Decree!" << endl;
int choice = rand() % 3;
switch (choice) {
case 0: // Economic stimulus
cout << "The decree stimulates the economy, increasing treasury by 10%." << endl;
kingdom.getEconomy()->setTreasuryGold(
static_cast<int>(kingdom.getEconomy()->getTreasuryGold() * 1.1)
);
break;
case 1: // Population happiness
cout << "The decree grants minor tax relief, improving happiness." << endl;
kingdom.getPopulation()->setHappiness(
kingdom.getPopulation()->getHappiness() + 0.1
);
break;
case 2: // Military morale
cout << "The decree honors the military, boosting army morale." << endl;
kingdom.getArmy()->setMorale(
kingdom.getArmy()->getMorale() + 0.15
);
break;
}
}
void King::applyEffects(Kingdom& kingdom) {
// King's passive effects based on stats
// Charisma affects diplomatic relations
double diplomacyBonus = charisma * 0.01;
// Implement diplomacy effects when needed
// Intelligence affects economy
double economyBonus = intelligence * 0.01;
kingdom.getEconomy()->setInflation(
max(0.01, kingdom.getEconomy()->getInflation() - economyBonus)
);
// Strength affects army
double armyBonus = strength * 0.01;
kingdom.getArmy()->setMorale(
min(1.0, kingdom.getArmy()->getMorale() + armyBonus)
);
// Royal bloodline affects population loyalty
double loyaltyBonus = royalBloodline * 0.02;
kingdom.getPopulation()->setHappiness(
min(1.0, kingdom.getPopulation()->getHappiness() + loyaltyBonus)
);
}
// Commander implementation
Commander::Commander(const string& name, int charisma, int intelligence, int strength, int tacticalSkill)
: Leader(name, charisma, intelligence, strength), tacticalSkill(tacticalSkill), loyalty(50 + rand() % 51) {
}
Commander::~Commander() {}
int Commander::getTacticalSkill() const {
return tacticalSkill;
}
int Commander::getLoyalty() const {
return loyalty;
}
void Commander::setLoyalty(int newLoyalty) {
loyalty = max(0, min(100, newLoyalty));
}
void Commander::specialAction(Kingdom& kingdom) {
// Commander's special action: military drill or defense improvement
cout << "\nCommander " << name << " conducts special military operations!" << endl;
// Training takes time
cout << "Training troops... ";
for (int i = 0; i < 3; i++) {
cout << "." << flush;
crossPlatformSleep(1);
}
cout << " Complete!" << endl;
int choice = rand() % 2;
switch (choice) {
case 0: // Military training
cout << "The army's training level increases!" << endl;
kingdom.getArmy()->setTrainingLevel(
kingdom.getArmy()->getTrainingLevel() + 1 + (tacticalSkill / 20)
);
break;
case 1: // Morale boost
cout << "Troop morale is significantly improved!" << endl;
kingdom.getArmy()->setMorale(
min(1.0, kingdom.getArmy()->getMorale() + 0.2 + (charisma * 0.01))
);
break;
}
}
void Commander::applyEffects(Kingdom& kingdom) {
// Commander's passive effects
// Tactical skill affects army strength
double armyStrengthBonus = tacticalSkill * 0.02;
double currentMorale = kingdom.getArmy()->getMorale();
kingdom.getArmy()->setMorale(min(1.0, currentMorale + armyStrengthBonus * 0.1));
// Loyalty affects chance of rebellion
if (loyalty < 30 && rand() % 100 < (30 - loyalty)) {
cout << "\nWARNING: Commander " << name << " is plotting against you!" << endl;
// Potentially trigger rebellion event
}
}
// GuildLeader implementation
GuildLeader::GuildLeader(const string& name, int charisma, int intelligence, int strength,
const string& guildType, int businessAcumen)
: Leader(name, charisma, intelligence, strength), guildType(guildType), businessAcumen(businessAcumen) {
}
GuildLeader::~GuildLeader() {}
string GuildLeader::getGuildType() const {
return guildType;
}
int GuildLeader::getBusinessAcumen() const {
return businessAcumen;
}
void GuildLeader::specialAction(Kingdom& kingdom) {
// Guild leader's special action: economic boost or trade deals
cout << "\nGuild Leader " << name << " of the " << guildType << " Guild initiates a special project!" << endl;
if (guildType == "Merchants") {
cout << "New trade deals bring increased tax revenue!" << endl;
kingdom.getEconomy()->setTreasuryGold(
kingdom.getEconomy()->getTreasuryGold() + 100 + (businessAcumen * 5)
);
}
else if (guildType == "Craftsmen") {
cout << "Improved crafting techniques boost resource production!" << endl;
kingdom.getMarket()->getWood()->changeAmount(50 + (businessAcumen * 2));
kingdom.getMarket()->getIron()->changeAmount(20 + (businessAcumen * 1));
}
else if (guildType == "Farmers") {
cout << "Agricultural innovations increase food stocks!" << endl;
kingdom.getMarket()->getFood()->changeAmount(100 + (businessAcumen * 5));
}
}
void GuildLeader::applyEffects(Kingdom& kingdom) {
// Guild leader's passive effects
// Business acumen affects economy
double economyBonus = businessAcumen * 0.02;
kingdom.getEconomy()->setInflation(
max(0.01, kingdom.getEconomy()->getInflation() - economyBonus * 0.01)
);
// Guild type specific effects
if (guildType == "Merchants") {
// Merchants boost trade income
int merchantCount = kingdom.getPopulation()->getMerchants();
int bonusGold = (merchantCount * businessAcumen) / 100;
kingdom.getEconomy()->setTreasuryGold(
kingdom.getEconomy()->getTreasuryGold() + bonusGold
);
}
else if (guildType == "Craftsmen") {
// Craftsmen improve resource efficiency
// Implementation would adjust resource production rates
}
else if (guildType == "Farmers") {
// Farmers improve food production
kingdom.getMarket()->getFood()->changeAmount((businessAcumen / 10) + 5);
}
}
// ------------------------
// Population implementation
// ------------------------
Population::Population(int initialPeasants, int initialMerchants, int initialNobles)
: peasants(initialPeasants), merchants(initialMerchants), nobles(initialNobles),
growthRate(0.05), happiness(0.5) {
}
Population::~Population() {}
int Population::getPeasants() const {
return peasants;
}
int Population::getMerchants() const {
return merchants;
}
int Population::getNobles() const {
return nobles;
}
int Population::getTotal() const {
return peasants + merchants + nobles;
}
double Population::getGrowthRate() const {
return growthRate;
}
double Population::getHappiness() const {
return happiness;
}
void Population::setPeasants(int count) {
peasants = max(0, count);
}
void Population::setMerchants(int count) {
merchants = max(0, count);
}
void Population::setNobles(int count) {
nobles = max(0, count);
}
void Population::setGrowthRate(double rate) {
growthRate = max(0.0, min(0.2, rate));
}
void Population::setHappiness(double value) {
happiness = max(0.0, min(1.0, value));
}
void Population::updatePopulation(const Economy& economy, const Army& army) {
// Update growth rate based on conditions
double taxBurden = economy.getPeasantTaxRate() + economy.getMerchantTaxRate() + economy.getNobleTaxRate();
double foodSecurity = 1.0; // Placeholder, would be calculated from food resources
// Adjust growth rate based on conditions
growthRate = 0.05 + (happiness * 0.05) - (taxBurden * 0.1) + (foodSecurity * 0.02);
growthRate = max(0.01, min(0.2, growthRate)); // Clamp to reasonable range
// Apply growth to different population groups
peasants += static_cast<int>(peasants * growthRate);
merchants += static_cast<int>(merchants * (growthRate * 0.8)); // Merchants grow more slowly
nobles += static_cast<int>(nobles * (growthRate * 0.5)); // Nobles grow very slowly
// Potential for social mobility
if (rand() % 100 < 5) { // 5% chance each year
int socialMobility = max(1, static_cast<int>(peasants * 0.01));
peasants -= socialMobility;
merchants += socialMobility;
}
if (rand() % 100 < 2) { // 2% chance each year
int socialMobility = max(1, static_cast<int>(merchants * 0.01));
merchants -= socialMobility;
nobles += socialMobility;
}
}
void Population::calculateHappiness(const Economy& economy, const Army& army) {
// Factors affecting happiness
double taxFactor = 1.0 - ((economy.getPeasantTaxRate() * 2) +
(economy.getMerchantTaxRate() * 1.5) +
(economy.getNobleTaxRate() * 0.5));
double armyPresence = min(1.0, static_cast<double>(army.getTotal()) / static_cast<double>(getTotal()) * 0.5);
double inflationFactor = 1.0 - (economy.getInflation() * 2.0);
// Calculate new happiness
double newHappiness = (happiness * 0.7) + (taxFactor * 0.1) +
(armyPresence * 0.1) + (inflationFactor * 0.1);
// Clamp to valid range
happiness = max(0.0, min(1.0, newHappiness));
}
bool Population::checkRebellion() const {
// Check if population is going to rebel
if (happiness < 0.2) {
// Very unhappy population might rebel
return (rand() % 100) < ((0.2 - happiness) * 100 * 2);
}
return false;
}
// -------------------
// Army implementation
// -------------------
Army::Army(int initialInfantry, int initialCavalry, int initialArchers)
: infantry(initialInfantry), cavalry(initialCavalry), archers(initialArchers),
morale(0.7), trainingLevel(1), isAtWar(false) {
}
Army::~Army() {}
int Army::getInfantry() const {
return infantry;
}
int Army::getCavalry() const {
return cavalry;
}
int Army::getArchers() const {
return archers;
}
int Army::getTotal() const {
return infantry + cavalry + archers;
}
double Army::getMorale() const {
return morale;
}
int Army::getTrainingLevel() const {
return trainingLevel;
}
bool Army::getWarStatus() const {
return isAtWar;
}
void Army::setInfantry(int count) {
infantry = max(0, count);
}
void Army::setCavalry(int count) {
cavalry = max(0, count);
}
void Army::setArchers(int count) {
archers = max(0, count);
}
void Army::setMorale(double value) {
morale = max(0.0, min(1.0, value));
}
void Army::setTrainingLevel(int level) {
trainingLevel = max(1, level);
}
void Army::setWarStatus(bool status) {
isAtWar = status;
}
void Army::trainArmy() {
// Training the army takes time and resources but improves effectiveness
cout << "Training army units... ";
for (int i = 0; i < 3; i++) {
cout << "." << flush;
crossPlatformSleep(1);
}
cout << " Complete!" << endl;
// Improve training level
trainingLevel++;
// Boost morale
morale = min(1.0, morale + 0.1);
cout << "Army training level increased to " << trainingLevel << endl;
cout << "Morale improved to " << static_cast<int>(morale * 100) << "%" << endl;
}
int Army::calculateStrength() const {
// Calculate the overall military strength
int baseStrength = infantry + (cavalry * 3) + (archers * 2);
double moraleMultiplier = 0.5 + (morale * 0.5); // 0.5 - 1.0
double trainingMultiplier = 0.8 + (trainingLevel * 0.2); // Starts at 1.0
return static_cast<int>(baseStrength * moraleMultiplier * trainingMultiplier);
}
void Army::updateMorale(const Economy& economy, const Population& population) {
// Factors affecting morale
double payFactor = min(1.0, static_cast<double>(economy.getTreasuryGold()) /
(getTotal() * 5)); // Can the kingdom pay the troops?
double populationSupport = population.getHappiness();
double warEffect = isAtWar ? -0.1 : 0.05; // War decreases morale over time
// Calculate new morale
double newMorale = (morale * 0.7) + (payFactor * 0.1) +
(populationSupport * 0.1) + warEffect;
// Clamp to valid range
morale = max(0.1, min(1.0, newMorale));
}
int Army::calculateDesertion() {
// Calculate how many troops desert based on morale
if (morale < 0.4) {
double desertionRate = (0.4 - morale) * 0.5; // Up to 20% desertion for very low morale
int deserters = static_cast<int>(getTotal() * desertionRate);
// Distribute desertion across unit types
int infantryDeserters = min(infantry, static_cast<int>(deserters * 0.6));
int cavalryDeserters = min(cavalry, static_cast<int>(deserters * 0.2));
int archerDeserters = min(archers, static_cast<int>(deserters * 0.2));
infantry -= infantryDeserters;
cavalry -= cavalryDeserters;
archers -= archerDeserters;
return infantryDeserters + cavalryDeserters + archerDeserters;
}
return 0;
}
bool Army::checkRebellion(const Population& population) const {
// Check if the army will rebel against the ruler
if (morale < 0.2 && population.getHappiness() < 0.3) {
// Both army and population are very unhappy
return (rand() % 100) < ((0.2 - morale) * 100 * 3);
}
return false;
}
// ----------------------
// Economy implementation
// ----------------------
Economy::Economy(double initialPeasantTaxRate, double initialMerchantTaxRate, double initialNobleTaxRate)
: peasantTaxRate(initialPeasantTaxRate), merchantTaxRate(initialMerchantTaxRate),
nobleTaxRate(initialNobleTaxRate), inflation(0.02), treasuryGold(1000), debt(0) {
}
Economy::~Economy() {}
double Economy::getPeasantTaxRate() const {
return peasantTaxRate;
}
double Economy::getMerchantTaxRate() const {
return merchantTaxRate;
}
double Economy::getNobleTaxRate() const {
return nobleTaxRate;
}
double Economy::getInflation() const {
return inflation;
}
int Economy::getTreasuryGold() const {
return treasuryGold;
}
int Economy::getDebt() const {
return debt;
}
void Economy::setPeasantTaxRate(double rate) {
peasantTaxRate = max(0.0, min(0.5, rate));
}
void Economy::setMerchantTaxRate(double rate) {
merchantTaxRate = max(0.0, min(0.5, rate));
}
void Economy::setNobleTaxRate(double rate) {
nobleTaxRate = max(0.0, min(0.5, rate));
}
void Economy::setInflation(double value) {
inflation = max(0.01, min(0.2, value));
}
void Economy::setTreasuryGold(int amount) {
treasuryGold = max(0, amount);
}
void Economy::setDebt(int amount) {
debt = max(0, amount);
}
int Economy::collectTaxes(const Population& population) {
// Calculate tax revenue from different population groups
int peasantTax = static_cast<int>(population.getPeasants() * 2 * peasantTaxRate);
int merchantTax = static_cast<int>(population.getMerchants() * 10 * merchantTaxRate);
int nobleTax = static_cast<int>(population.getNobles() * 50 * nobleTaxRate);
int totalTax = peasantTax + merchantTax + nobleTax;
treasuryGold += totalTax;
return totalTax;
}
void Economy::updateEconomy(const Population& population, const Army& army) {
// Update economic factors
// Army maintenance costs
int armyCost = army.getTotal() * 2;
treasuryGold -= min(treasuryGold, armyCost);
// Bureaucracy costs
int bureaucracyCost = population.getTotal() / 10;
treasuryGold -= min(treasuryGold, bureaucracyCost);
// Update inflation based on economic activity
double economicActivity = static_cast<double>(population.getTotal()) / 1000.0;
double treasuryRatio = min(1.0, static_cast<double>(treasuryGold) / 10000.0);
// Inflation increases with high economic activity and low treasury
inflation = (inflation * 0.8) + (economicActivity * 0.05) - (treasuryRatio * 0.03);
inflation = max(0.01, min(0.2, inflation));
// Apply debt interest
if (debt > 0) {
int interest = static_cast<int>(debt * 0.1); // 10% interest
debt += interest;
}
}
double Economy::calculateUnrest(const Population& population) const {
// Calculate economic unrest level
double taxBurden = (peasantTaxRate + merchantTaxRate + nobleTaxRate) / 3.0;
double inflationImpact = inflation * 5.0;
double happinessOffset = population.getHappiness();
return min(1.0, (taxBurden * 0.5) + (inflationImpact * 0.3) - (happinessOffset * 0.5));
}
bool Economy::checkRiots(const Population& population) const {
// Check if economic conditions will cause riots
double unrest = calculateUnrest(population);
return (unrest > 0.6) && ((rand() % 100) < (unrest * 100));
}
// --------------------
// Market implementation
// --------------------
Market::Market()
: food(make_shared<Food>(1000)), gold(make_shared<Gold>(500)),
wood(make_shared<Wood>(500)), stone(make_shared<Stone>(300)),
iron(make_shared<Iron>(200)), priceFluctuation(0.1) {
}
Market::~Market() {}
shared_ptr<Food> Market::getFood() const {
return food;
}
shared_ptr<Gold> Market::getGold() const {
return gold;
}
shared_ptr<Wood> Market::getWood() const {
return wood;
}
shared_ptr<Stone> Market::getStone() const {
return stone;
}
shared_ptr<Iron> Market::getIron() const {
return iron;
}
void Market::updatePrices(const Economy& economy) {
// Update resource prices based on economy and random fluctuations
double inflationFactor = 1.0 + economy.getInflation();
// Apply inflation to base values
food->setValue(1.0 * inflationFactor * (1.0 + ((rand() % 20) - 10) * 0.01));
wood->setValue(2.0 * inflationFactor * (1.0 + ((rand() % 20) - 10) * 0.01));
stone->setValue(3.0 * inflationFactor * (1.0 + ((rand() % 20) - 10) * 0.01));
iron->setValue(5.0 * inflationFactor * (1.0 + ((rand() % 20) - 10) * 0.01));
}
bool Market::buyResource(const string& resourceType, int amount, Economy& economy) {
// Buy resources from the market
int cost = 0;
shared_ptr<Resource> resource = nullptr;
if (resourceType == "Food") {
cost = static_cast<int>(amount * food->getValue());
resource = food;
}
else if (resourceType == "Wood") {
cost = static_cast<int>(amount * wood->getValue());
resource = wood;
}
else if (resourceType == "Stone") {
cost = static_cast<int>(amount * stone->getValue());
resource = stone;
}
else if (resourceType == "Iron") {
cost = static_cast<int>(amount * iron->getValue());
resource = iron;
}
else {
return false;
}
// Check if the kingdom can afford it
if (economy.getTreasuryGold() >= cost) {
economy.setTreasuryGold(economy.getTreasuryGold() - cost);
resource->changeAmount(amount);
return true;
}
return false;
}
bool Market::sellResource(const string& resourceType, int amount, Economy& economy) {
// Sell resources to the market
int revenue = 0;
shared_ptr<Resource> resource = nullptr;
if (resourceType == "Food") {
if (food->getAmount() < amount) return false;
revenue = static_cast<int>(amount * food->getValue() * 0.9); // 10% market fee
resource = food;
}
else if (resourceType == "Wood") {
if (wood->getAmount() < amount) return false;
revenue = static_cast<int>(amount * wood->getValue() * 0.9);
resource = wood;
}
else if (resourceType == "Stone") {
if (stone->getAmount() < amount) return false;
revenue = static_cast<int>(amount * stone->getValue() * 0.9);
resource = stone;
}
else if (resourceType == "Iron") {
if (iron->getAmount() < amount) return false;
revenue = static_cast<int>(amount * iron->getValue() * 0.9);
resource = iron;
}
else {
return false;
}
// Complete the transaction
resource->changeAmount(-amount);
economy.setTreasuryGold(economy.getTreasuryGold() + revenue);
return true;
}
void Market::produceResources(const Population& population) {
// Calculate resource production based on population
int peasantProduction = population.getPeasants() / 5;
int merchantProduction = population.getMerchants() / 2;
// Food production (mainly from peasants)
food->changeAmount(peasantProduction * 2);
// Wood production
wood->changeAmount(peasantProduction);
// Stone production
stone->changeAmount(peasantProduction / 2);
// Iron production (less common)
iron->changeAmount(peasantProduction / 4);
// Gold from merchant activity
gold->changeAmount(merchantProduction * 2);
}
void Market::consumeResources(const Population& population, const Army& army) {
// Calculate resource consumption
int totalPopulation = population.getTotal();
int totalArmy = army.getTotal();
// Food consumption
int foodConsumption = totalPopulation + (totalArmy * 2); // Army eats more
food->changeAmount(-min(food->getAmount(), foodConsumption));
// Wood consumption (for heating, building, etc.)
int woodConsumption = totalPopulation / 10;
wood->changeAmount(-min(wood->getAmount(), woodConsumption));
// Iron consumption (for tools, weapons)
int ironConsumption = totalPopulation / 50 + totalArmy / 20;
iron->changeAmount(-min(iron->getAmount(), ironConsumption));
}
// ------------------------
// Diplomacy implementation
// ------------------------
Diplomacy::Diplomacy(int maxForeignKingdoms)
: maxKingdoms(maxForeignKingdoms), kingdomCount(0) {
foreignKingdoms = new Kingdom[maxKingdoms];
// Initialize with some default kingdoms
addKingdom("Northlands", 500 + rand() % 500);
addKingdom("Eastern Empire", 600 + rand() % 600);
addKingdom("Southern Realms", 400 + rand() % 400);
}
Diplomacy::~Diplomacy() {
delete[] foreignKingdoms;
}
void Diplomacy::addKingdom(const string& name, int strength) {
if (kingdomCount < maxKingdoms) {
foreignKingdoms[kingdomCount].name = name;
foreignKingdoms[kingdomCount].relationLevel = 0; // Neutral
foreignKingdoms[kingdomCount].isAlly = false;
foreignKingdoms[kingdomCount].atWar = false;
foreignKingdoms[kingdomCount].strength = strength;
kingdomCount++;
}
}
bool Diplomacy::improveRelations(const string& kingdomName, Economy& economy) {
for (int i = 0; i < kingdomCount; i++) {
if (foreignKingdoms[i].name == kingdomName) {
// Lower cost and increase relation impact
int cost = 20 + (foreignKingdoms[i].relationLevel * 5); // Reduced cost
if (economy.getTreasuryGold() >= cost) {
economy.setTreasuryGold(economy.getTreasuryGold() - cost);
foreignKingdoms[i].relationLevel = min(10, foreignKingdoms[i].relationLevel + 2); // +2 instead of +1
cout << "Spent " << cost << " gold to improve relations!" << endl;
return true;
}
else {
cout << "Not enough gold! Need " << cost << " gold." << endl;
return false;
}
}
}
cout << "Kingdom '" << kingdomName << "' not found!" << endl;
return false;
}
bool Diplomacy::declareWar(const string& kingdomName, Army& army) {
for (int i = 0; i < kingdomCount; i++) {
if (foreignKingdoms[i].name == kingdomName) {
if (!foreignKingdoms[i].atWar) {
foreignKingdoms[i].atWar = true;
foreignKingdoms[i].isAlly = false;
foreignKingdoms[i].relationLevel = max(-10, foreignKingdoms[i].relationLevel - 5); // More significant drop
army.setWarStatus(true);
cout << "Your army mobilizes for war!" << endl;
return true;
}
cout << "Already at war with " << kingdomName << "!" << endl;
return false;
}
}
cout << "Kingdom '" << kingdomName << "' not found!" << endl;
return false;
}
bool Diplomacy::signPeace(const string& kingdomName, Economy& economy) {
// Find the kingdom
Army army;
for (int i = 0; i < kingdomCount; i++) {
if (foreignKingdoms[i].name == kingdomName) {
if (foreignKingdoms[i].atWar) {
// Peace treaties often require reparations
int cost = 200 + (foreignKingdoms[i].strength / 10);
if (economy.getTreasuryGold() >= cost) {
economy.setTreasuryGold(economy.getTreasuryGold() - cost);
foreignKingdoms[i].atWar = false;
foreignKingdoms[i].relationLevel = 0; // Reset to neutral
// Check if still at war with any kingdom
bool stillAtWar = false;
for (int j = 0; j < kingdomCount; j++) {
if (foreignKingdoms[j].atWar) {
stillAtWar = true;
break;
}
}
// Update army war status if no longer at war with anyone
if (!stillAtWar) {
army.setWarStatus(false);
}
return true;
}
else {
return false; // Not enough gold
}
}
return false; // Not at war
}
}
return false; // Kingdom not found
}
bool Diplomacy::formAlliance(const string& kingdomName) {
for (int i = 0; i < kingdomCount; i++) {
if (foreignKingdoms[i].name == kingdomName) {
if (!foreignKingdoms[i].atWar && foreignKingdoms[i].relationLevel >= 5) { // Lowered from 7
foreignKingdoms[i].isAlly = true;
foreignKingdoms[i].relationLevel = min(10, foreignKingdoms[i].relationLevel + 1); // Bonus relation
cout << kingdomName << " is now your ally!" << endl;
return true;
}
cout << "Cannot ally! Relations too low (need 5+) or at war." << endl;
return false;
}
}
cout << "Kingdom '" << kingdomName << "' not found!" << endl;
return false;
}
bool Diplomacy::establishTrade(const string& kingdomName, Market& market, Economy& economy) {
for (int i = 0; i < kingdomCount; i++) {
if (foreignKingdoms[i].name == kingdomName) {
if (!foreignKingdoms[i].atWar && foreignKingdoms[i].relationLevel >= 2) { // Lowered from 3
// Increase trade benefits
market.getFood()->changeAmount(100 + (foreignKingdoms[i].relationLevel * 20));
market.getWood()->changeAmount(50 + (foreignKingdoms[i].relationLevel * 10));