forked from Return-To-The-Roots/s25client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnobHarborBuilding.cpp
More file actions
1374 lines (1229 loc) · 49.2 KB
/
nobHarborBuilding.cpp
File metadata and controls
1374 lines (1229 loc) · 49.2 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
// Copyright (C) 2005 - 2024 Settlers Freaks (sf-team at siedler25.org)
//
// SPDX-License-Identifier: GPL-2.0-or-later
#include "nobHarborBuilding.h"
#include "EventManager.h"
#include "GamePlayer.h"
#include "GlobalGameSettings.h"
#include "Loader.h"
#include "SerializedGameData.h"
#include "Ware.h"
#include "figures/noFigure.h"
#include "figures/nofAttacker.h"
#include "figures/nofDefender.h"
#include "helpers/containerUtils.h"
#include "helpers/pointerContainerUtils.h"
#include "network/GameClient.h"
#include "nobMilitary.h"
#include "ogl/glArchivItem_Bitmap.h"
#include "ogl/glArchivItem_Bitmap_Player.h"
#include "ogl/glSmartBitmap.h"
#include "pathfinding/RoadPathFinder.h"
#include "postSystem/PostMsgWithBuilding.h"
#include "random/Random.h"
#include "world/GameWorld.h"
#include "nodeObjs/noShip.h"
#include "gameData/BuildingConsts.h"
#include "gameData/GameConsts.h"
#include "gameData/MilitaryConsts.h"
#include "gameData/ShieldConsts.h"
nobHarborBuilding::ExpeditionInfo::ExpeditionInfo(SerializedGameData& sgd)
: boards(sgd.PopUnsignedInt()), stones(sgd.PopUnsignedInt()), active(sgd.PopBool()), builder(sgd.PopBool())
{}
void nobHarborBuilding::ExpeditionInfo::Serialize(SerializedGameData& sgd) const
{
sgd.PushUnsignedInt(boards);
sgd.PushUnsignedInt(stones);
sgd.PushBool(active);
sgd.PushBool(builder);
}
nobHarborBuilding::ExplorationExpeditionInfo::ExplorationExpeditionInfo(SerializedGameData& sgd)
: active(sgd.PopBool()), scouts(sgd.PopUnsignedInt())
{}
void nobHarborBuilding::ExplorationExpeditionInfo::Serialize(SerializedGameData& sgd) const
{
sgd.PushBool(active);
sgd.PushUnsignedInt(scouts);
}
nobHarborBuilding::nobHarborBuilding(const MapPoint pos, const unsigned char player, const Nation nation)
: nobBaseWarehouse(BuildingType::HarborBuilding, pos, player, nation), orderware_ev(nullptr)
{
// ins Militärquadrat einfügen
world->GetMilitarySquares().Add(this);
world->RecalcTerritory(*this, TerritoryChangeReason::Build);
// Alle Waren 0
inventory.clear();
// Aktuellen Warenbestand zur aktuellen Inventur dazu addieren
AddToInventory();
// Take 1 as the reserve per rank
for(unsigned i = 0; i <= world->GetGGS().GetMaxMilitaryRank(); ++i)
{
reserve_soldiers_claimed_visual[i] = reserve_soldiers_claimed_real[i] = 1;
RefreshReserve(i);
}
/// Die Meere herausfinden, an die dieser Hafen grenzt
for(const auto dir : helpers::EnumRange<Direction>{})
seaIds[dir] = world->GetSeaFromCoastalPoint(world->GetNeighbour(pos, dir));
// Post versenden
SendPostMessage(player,
std::make_unique<PostMsgWithBuilding>(GetEvMgr().GetCurrentGF(), _("New harbor building finished"),
PostCategory::Economy, *this));
}
void nobHarborBuilding::DestroyBuilding()
{
GetEvMgr().RemoveEvent(orderware_ev);
// Der Wirtschaftsverwaltung Bescheid sagen
GamePlayer& owner = world->GetPlayer(player);
// Baumaterialien in der Inventur verbuchen
if(expedition.active)
{
owner.DecreaseInventoryWare(GoodType::Boards, expedition.boards);
owner.DecreaseInventoryWare(GoodType::Stones, expedition.stones);
// Und Bauarbeiter (später) rausschicken
if(expedition.builder)
inventory.Add(Job::Builder);
else
owner.OneJobNotWanted(Job::Builder, this);
}
// cancel order for scouts
if(exploration_expedition.active)
{
inventory.real.Add(Job::Scout, exploration_expedition.scouts);
for(unsigned i = exploration_expedition.scouts; i < world->GetGGS().GetNumScoutsExpedition(); i++)
owner.OneJobNotWanted(Job::Scout, this);
}
// cancel all jobs wanted for this building
owner.JobNotWanted(this, true);
// Waiting Wares löschen
for(auto& wares_for_ship : wares_for_ships)
{
wares_for_ship->WareLost(player);
wares_for_ship->Destroy();
}
wares_for_ships.clear();
// Leute, die noch aufs Schiff warten, rausschicken
for(auto& figures_for_ship : figures_for_ships)
{
noFigure& figure = world->AddFigure(pos, std::move(figures_for_ship.fig));
figure.Abrogate();
figure.StartWandering();
figure.StartWalking(RANDOM_ENUM(Direction));
}
figures_for_ships.clear();
for(auto& soldiers_for_ship : soldiers_for_ships)
{
nofAttacker& soldier = world->AddFigure(pos, std::move(soldiers_for_ship.attacker));
soldier.CancelSeaAttack();
RTTR_Assert(!soldier.GetAttackedGoal());
RTTR_Assert(soldier.HasNoHome());
RTTR_Assert(soldier.HasNoGoal());
soldier.StartWandering();
soldier.StartWalking(RANDOM_ENUM(Direction));
}
soldiers_for_ships.clear();
nobBaseWarehouse::DestroyBuilding();
world->GetMilitarySquares().Remove(this);
// Recalc territory. AFTER calling base destroy as otherwise figures might get stuck here
world->RecalcTerritory(*this, TerritoryChangeReason::Destroyed);
}
void nobHarborBuilding::Serialize(SerializedGameData& sgd) const
{
nobBaseWarehouse::Serialize(sgd);
expedition.Serialize(sgd);
exploration_expedition.Serialize(sgd);
sgd.PushEvent(orderware_ev);
helpers::pushContainer(sgd, seaIds);
sgd.PushObjectContainer(wares_for_ships, true);
sgd.PushUnsignedInt(figures_for_ships.size());
for(const auto& figures_for_ship : figures_for_ships)
{
helpers::pushPoint(sgd, figures_for_ship.dest);
sgd.PushObject(figures_for_ship.fig);
}
sgd.PushUnsignedInt(soldiers_for_ships.size());
for(const auto& soldiers_for_ship : soldiers_for_ships)
{
helpers::pushPoint(sgd, soldiers_for_ship.dest);
sgd.PushObject(soldiers_for_ship.attacker, true);
}
}
nobHarborBuilding::nobHarborBuilding(SerializedGameData& sgd, const unsigned obj_id)
: nobBaseWarehouse(sgd, obj_id), expedition(sgd), exploration_expedition(sgd), orderware_ev(sgd.PopEvent())
{
// ins Militärquadrat einfügen
world->GetMilitarySquares().Add(this);
helpers::popContainer(sgd, seaIds);
sgd.PopObjectContainer(wares_for_ships, GO_Type::Ware);
unsigned count = sgd.PopUnsignedInt();
for(unsigned i = 0; i < count; ++i)
{
const MapPoint dest = sgd.PopMapPoint();
auto* fig = sgd.PopObject<noFigure>();
figures_for_ships.emplace_back(FigureForShip{std::unique_ptr<noFigure>(fig), dest});
}
count = sgd.PopUnsignedInt();
for(unsigned i = 0; i < count; ++i)
{
const MapPoint dest = sgd.PopMapPoint();
auto* fig = sgd.PopObject<nofAttacker>(GO_Type::NofAttacker);
soldiers_for_ships.emplace_back(SoldierForShip{std::unique_ptr<nofAttacker>(fig), dest});
}
}
// Relative Position des Bauarbeiters
constexpr helpers::EnumArray<Position, Nation> BUILDER_POS = {
{Position(-20, 18), Position(-28, 17), Position(-20, 15), Position(-38, 17), Position(-38, 17)}};
/// Relative Position der Brettertürme
constexpr helpers::EnumArray<Position, Nation> BOARDS_POS = {
{Position(-75, -5), Position(-60, -5), Position(-55, -5), Position(-65, -5), Position(-65, -5)}};
/// Relative Position der Steintürme
constexpr helpers::EnumArray<Position, Nation> STONES_POS = {
{Position(-65, 10), Position(-52, 10), Position(-42, 10), Position(-52, 10), Position(-52, 10)}};
/// Relative Postion der inneren Hafenfeuer
constexpr helpers::EnumArray<Position, Nation> FIRE_POS = {
{Position(36, -51), Position(0, 0), Position(0, 0), Position(5, -80), Position(0, 0)}};
/// Relative Postion der äußeren Hafenfeuer
constexpr helpers::EnumArray<Position, Nation> EXTRAFIRE_POS = {
{Position(0, 0), Position(0, 0), Position(8, -115), Position(0, 0), Position(0, 0)}};
void nobHarborBuilding::Draw(DrawPoint drawPt)
{
// Gebäude an sich zeichnen
DrawBaseBuilding(drawPt);
// Hafenfeuer zeichnen // TODO auch für nicht-römer machen
if(nation == Nation::Romans || nation == Nation::Japanese || nation == Nation::Babylonians)
{
LOADER.GetNationImage(nation, 500 + 5 * GAMECLIENT.GetGlobalAnimation(8, 2, 1, GetObjId() + GetX() + GetY()))
->DrawFull(drawPt + FIRE_POS[nation]);
} else if(nation == Nation::Africans || nation == Nation::Vikings)
{
LOADER.GetMapTexture(740 + GAMECLIENT.GetGlobalAnimation(8, 5, 2, GetObjId() + GetX() + GetY()))
->DrawFull(drawPt + FIRE_POS[nation]);
}
if(nation == Nation::Romans)
{
// Zusätzliches Feuer
LOADER.GetMapTexture(740 + GAMECLIENT.GetGlobalAnimation(8, 5, 2, GetObjId() + GetX() + GetY()))
->DrawFull(drawPt + EXTRAFIRE_POS[nation]);
}
// Läuft gerade eine Expedition?
if(expedition.active)
{
// Waren für die Expedition zeichnen
// Bretter
DrawPoint boardsPos = drawPt + BOARDS_POS[nation];
for(unsigned char i = 0; i < expedition.boards; ++i)
LOADER.GetWareStackTex(GoodType::Boards)->DrawFull(boardsPos - DrawPoint(0, i * 4));
DrawPoint stonesPos = drawPt + STONES_POS[nation];
// Steine
for(unsigned char i = 0; i < expedition.stones; ++i)
LOADER.GetWareStackTex(GoodType::Stones)->DrawFull(stonesPos - DrawPoint(0, i * 4));
// Und den Bauarbeiter, falls er schon da ist
if(expedition.builder)
{
unsigned id = GAMECLIENT.GetGlobalAnimation(1000, 7, 1, GetX() + GetY());
const int WALKING_DISTANCE = 30;
// Wegstrecke, die er von einem Punkt vom anderen schon gelaufen ist
int walking_distance = (id % 500) * WALKING_DISTANCE / 500;
// Id vom laufen
unsigned walking_id = (id / 32) % 8;
DrawPoint builderPos = drawPt + BUILDER_POS[nation];
if(id < 500)
LOADER.getBobSprite(nation, Job::Builder, Direction::West, walking_id)
.draw(builderPos - DrawPoint(walking_distance, 0), COLOR_WHITE, world->GetPlayer(player).color);
else
LOADER.getBobSprite(nation, Job::Builder, Direction::East, walking_id)
.draw(builderPos + DrawPoint(walking_distance - WALKING_DISTANCE, 0), COLOR_WHITE,
world->GetPlayer(player).color);
}
}
}
void nobHarborBuilding::HandleEvent(const unsigned id)
{
switch(id)
{
case 10:
// Waren-Bestell-Event
this->orderware_ev = nullptr;
// Mal wieder schauen, ob es Waren für unsere Expedition gibt
OrderExpeditionWares();
break;
default: HandleBaseEvent(id); break;
}
}
/// Startet eine Expedition
void nobHarborBuilding::StartExpedition()
{
// Schon eine Expedition gestartet?
if(expedition.active)
return;
// Initialisierung
expedition.active = true;
// In unseren Warenbestand gucken und die erforderlichen Bretter und Steine sowie den
// Bauarbeiter holen, falls vorhanden
expedition.boards =
std::min(unsigned(BUILDING_COSTS[BuildingType::HarborBuilding].boards), inventory[GoodType::Boards]);
expedition.stones =
std::min(unsigned(BUILDING_COSTS[BuildingType::HarborBuilding].stones), inventory[GoodType::Stones]);
inventory.Remove(GoodType::Boards, expedition.boards);
inventory.Remove(GoodType::Stones, expedition.stones);
if(inventory[Job::Builder])
{
expedition.builder = true;
inventory.Remove(Job::Builder);
} else
{
bool convert = true;
expedition.builder = false;
// got a builder in ANY storehouse?
GamePlayer& owner = world->GetPlayer(player);
for(const nobBaseWarehouse* wh : owner.GetBuildingRegister().GetStorehouses()) //-V807
{
if(wh->GetNumRealFigures(Job::Builder))
{
convert = false;
break;
}
}
if(convert && inventory[GoodType::Hammer]
&& inventory[Job::Helper] > 1) // maybe have a hammer & helper to create our own builder?
{
inventory.Remove(GoodType::Hammer);
owner.DecreaseInventoryWare(GoodType::Hammer, 1);
inventory.Remove(Job::Helper);
owner.DecreaseInventoryJob(Job::Helper, 1);
owner.IncreaseInventoryJob(Job::Builder, 1);
expedition.builder = true;
}
// not in harbor, and didnt have to or couldnt convert so order a builder
if(!expedition.builder)
owner.AddJobWanted(Job::Builder, this);
}
// Ggf. Waren bestellen, die noch fehlen
OrderExpeditionWares();
// Ggf. ist jetzt alles benötigte schon da
// Dann Schiff rufen
CheckExpeditionReady();
}
void nobHarborBuilding::StopExpedition()
{
if(!expedition.active)
return;
// Dann diese stoppen
expedition.active = false;
// Waren zurücktransferieren
inventory.Add(GoodType::Boards, expedition.boards);
inventory.Add(GoodType::Stones, expedition.stones);
if(expedition.builder)
{
inventory.Add(Job::Builder);
// Evtl. Abnehmer für die Figur wieder finden
world->GetPlayer(player).FindWarehouseForAllJobs(Job::Builder);
} else // todo falls noch nicht da - unterscheiden ob unterwegs oder nur bestellt - falls bestellt stornieren sonst
// informieren damit kein ersatz geschickt wird falls was nicht klappt aufm weg
{
world->GetPlayer(player).OneJobNotWanted(Job::Builder, this);
}
}
/// Startet eine Erkundungs-Expedition oder stoppt sie, wenn bereits eine stattfindet
void nobHarborBuilding::StartExplorationExpedition()
{
// Schon eine Expedition gestartet?
if(exploration_expedition.active)
return;
// Initialisierung
exploration_expedition.active = true;
exploration_expedition.scouts = 0;
// Look for missing scouts
const unsigned numScoutsRequired = world->GetGGS().GetNumScoutsExpedition();
if(inventory[Job::Scout] < numScoutsRequired)
{
unsigned missing = numScoutsRequired - inventory[Job::Scout];
// got scouts in ANY storehouse?
GamePlayer& owner = world->GetPlayer(player);
for(const nobBaseWarehouse* wh : owner.GetBuildingRegister().GetStorehouses()) //-V807
{
const unsigned numScouts = wh->GetNumRealFigures(Job::Scout);
if(numScouts >= missing)
{
missing = 0;
break;
} else if(numScouts > 0)
missing -= numScouts;
}
// Recruit missing ones if possible
while(missing > 0 && TryRecruitJob(Job::Scout))
missing--;
// Order scouts, we still requires
for(unsigned i = inventory[Job::Scout]; i < numScoutsRequired; ++i)
owner.AddJobWanted(Job::Scout, this);
}
if(inventory[Job::Scout])
{
exploration_expedition.scouts = std::min(inventory[Job::Scout], numScoutsRequired);
inventory.real.Remove(Job::Scout, exploration_expedition.scouts);
}
CheckExplorationExpeditionReady();
}
void nobHarborBuilding::StopExplorationExpedition()
{
if(!exploration_expedition.active)
return;
// Dann diese stoppen
exploration_expedition.active = false;
// cancel order for scouts
for(unsigned i = exploration_expedition.scouts; i < world->GetGGS().GetNumScoutsExpedition(); i++)
{
world->GetPlayer(player).OneJobNotWanted(Job::Scout, this);
}
// Erkunder zurücktransferieren
if(exploration_expedition.scouts)
{
inventory.real.Add(Job::Scout, exploration_expedition.scouts);
exploration_expedition.scouts = 0;
// Evtl. Abnehmer für die Figur wieder finden
world->GetPlayer(player).FindWarehouseForAllJobs(Job::Scout);
}
}
/// Bestellt die zusätzlichen erforderlichen Waren für eine Expedition
void nobHarborBuilding::OrderExpeditionWares()
{
RTTR_Assert(!IsBeingDestroyedNow()); // Wares should already be canceled!
if(this->IsBeingDestroyedNow()) // don't order new stuff if we are about to be destroyed
return;
if(!expedition.active) // expedition no longer active?
return;
// Waren in der Bestellungsliste mit beachten
unsigned boards = 0, stones = 0;
for(const auto* ware : dependent_wares)
{
RTTR_Assert(ware);
if(ware->type == GoodType::Boards)
++boards;
else if(ware->type == GoodType::Stones)
++stones;
}
// Prüfen, ob jeweils noch weitere Waren bestellt werden müssen
unsigned todo_boards = 0;
if(boards + expedition.boards < BUILDING_COSTS[BuildingType::HarborBuilding].boards)
{
todo_boards = BUILDING_COSTS[BuildingType::HarborBuilding].boards - (boards + expedition.boards);
Ware* ware;
do
{
ware = world->GetPlayer(player).OrderWare(GoodType::Boards, this);
if(ware)
{
RTTR_Assert(IsWareDependent(*ware));
--todo_boards;
}
} while(ware && todo_boards);
}
unsigned todo_stones = 0;
if(stones + expedition.stones < BUILDING_COSTS[BuildingType::HarborBuilding].stones)
{
todo_stones = BUILDING_COSTS[BuildingType::HarborBuilding].stones - (stones + expedition.stones);
Ware* ware;
do
{
ware = world->GetPlayer(player).OrderWare(GoodType::Stones, this);
if(ware)
{
RTTR_Assert(IsWareDependent(*ware));
--todo_stones;
}
} while(ware && todo_stones);
}
// Wenn immer noch nicht alles da ist, später noch einmal bestellen
if(!orderware_ev)
orderware_ev = GetEvMgr().AddEvent(this, 210, 10);
}
/// Eine bestellte Ware konnte doch nicht kommen
void nobHarborBuilding::WareLost(Ware& ware)
{
RTTR_Assert(!IsBeingDestroyedNow());
// ggf. neue Waren für Expedition bestellen
if(expedition.active && (ware.type == GoodType::Boards || ware.type == GoodType::Stones))
OrderExpeditionWares();
nobBaseWarehouse::WareLost(ware);
}
/// Schiff ist angekommen
void nobHarborBuilding::ShipArrived(noShip& ship)
{
// get a new job - priority is given according to this list: attack,expedition,exploration,transport
// any attackers ready?
if(!soldiers_for_ships.empty())
{
// load all soldiers that share the same target as the first soldier in the list
std::vector<std::unique_ptr<nofAttacker>> attackers;
MapPoint ship_dest = soldiers_for_ships.begin()->dest;
for(auto it = soldiers_for_ships.begin(); it != soldiers_for_ships.end();)
{
if(it->dest == ship_dest)
{
inventory.visual.Remove(it->attacker->GetJobType());
attackers.push_back(std::move(it->attacker));
it = soldiers_for_ships.erase(it);
} else
++it;
}
ship.PrepareSeaAttack(GetHarborPosID(), ship_dest, std::move(attackers));
return;
}
// Expedition ready?
if(expedition.active && expedition.builder
&& expedition.boards == BUILDING_COSTS[BuildingType::HarborBuilding].boards
&& expedition.stones == BUILDING_COSTS[BuildingType::HarborBuilding].stones)
{
// Aufräumen am Hafen
expedition.active = false;
// Expedition starten
ship.StartExpedition(GetHarborPosID());
return;
}
// Exploration-Expedition ready?
if(IsExplorationExpeditionReady())
{
// Aufräumen am Hafen
exploration_expedition.active = false;
// Expedition starten
ship.StartExplorationExpedition(GetHarborPosID());
inventory.visual.Remove(Job::Scout, exploration_expedition.scouts);
return;
}
// Gibt es Waren oder Figuren, die ein Schiff von hier aus nutzen wollen?
if(!wares_for_ships.empty() || !figures_for_ships.empty())
{
// Das Ziel wird nach der ersten Figur bzw. ersten Ware gewählt
// actually since the wares might not yet have informed the harbor that their target harbor was destroyed we
// pick the first figure/ware with a valid target instead
MapPoint dest;
bool gotdest = false;
for(const auto& figureForShip : figures_for_ships)
{
noBase* nb = world->GetNO(figureForShip.dest);
if(nb->GetGOT() == GO_Type::NobHarborbuilding
&& world->GetNode(figureForShip.dest).owner
== player + 1) // target is a harbor and owned by the same player
{
dest = figureForShip.dest;
gotdest = true;
break;
}
}
for(const auto& wareForShip : wares_for_ships)
{
noBase* nb = world->GetNO(wareForShip->GetNextHarbor());
if(nb->GetGOT() == GO_Type::NobHarborbuilding
&& world->GetNode(wareForShip->GetNextHarbor()).owner == player + 1)
{
dest = wareForShip->GetNextHarbor();
gotdest = true;
break;
}
}
if(gotdest)
{
std::list<std::unique_ptr<noFigure>> figures;
// Figuren auswählen, die zu diesem Ziel wollen
for(auto it = figures_for_ships.begin(); it != figures_for_ships.end() && figures.size() < SHIP_CAPACITY;)
{
if(it->dest == dest)
{
it->fig->StartShipJourney();
if(it->fig->GetJobType() != Job::BoatCarrier)
inventory.visual.Remove(it->fig->GetJobType());
else
{
inventory.visual.Remove(Job::Helper);
inventory.visual.Remove(GoodType::Boat);
}
figures.push_back(std::move(it->fig));
it = figures_for_ships.erase(it);
} else
++it;
}
// Und noch die Waren auswählen
std::list<std::unique_ptr<Ware>> wares;
for(auto it = wares_for_ships.begin();
it != wares_for_ships.end() && figures.size() + wares.size() < SHIP_CAPACITY;)
{
if((*it)->GetNextHarbor() == dest)
{
(*it)->StartShipJourney();
inventory.visual.Remove(ConvertShields((*it)->type));
wares.push_back(std::move(*it));
it = wares_for_ships.erase(it);
} else
++it;
}
// Und das Schiff starten lassen
ship.PrepareTransport(GetHarborPosID(), dest, std::move(figures), std::move(wares));
}
}
}
void nobHarborBuilding::AddWare(std::unique_ptr<Ware> ware)
{
if(ware->GetGoal() && ware->GetGoal() != this)
{
// This is not the goal but we have one -> Get new route
ware->RecalcRoute();
// Go by ship next?
if(ware->GetNextDir() == RoadPathDirection::Ship)
{
AddWareForShip(std::move(ware));
return;
}
if(ware->GetNextDir() != RoadPathDirection::None)
{
// Travel on roads -> Carry out
RTTR_Assert(ware->GetGoal() != this);
AddWaitingWare(std::move(ware));
return;
}
// No next dir means the ware reached its goal, i.e. us,
// or there is no valid, reachable goal
// In both cases we take it as we initially would have.
if(ware->GetGoal() && ware->GetGoal() != this)
{
// We have a goal but it isn't this and there is no next dir
// This can only happen when the ware was redirected to a warehouse while being carried,
// see Ware::FindRouteToWarehouse called by Ware::RecalcRoute
// TODO(Replay) When Ware::FindRouteToWarehouse recalculates the route when called from RecalcRoute
// and next_dir is None then goal will be NULL or us.
// So the condition of this branch can never be true and the branch can be replaced by:
// RTTR_Assert(!ware->GetGoal() || ware->GetGoal() == this)
RTTR_Assert(ware->IsCarried());
// Explicitly calculate the route which now should set it.
ware->RecalcRoute();
RTTR_Assert(ware->GetGoal());
RTTR_Assert(ware->GetNextDir() != RoadPathDirection::None);
if(ware->GetNextDir() == RoadPathDirection::Ship)
AddWareForShip(std::move(ware));
else
AddWaitingWare(std::move(ware));
return;
}
}
// When ware should be transported to any other goal we returned above, so now we need to take it
// Do we need the ware for an expedition?
if(expedition.active)
{
if((ware->type == GoodType::Boards && expedition.boards < BUILDING_COSTS[BuildingType::HarborBuilding].boards)
|| (ware->type == GoodType::Stones
&& expedition.stones < BUILDING_COSTS[BuildingType::HarborBuilding].stones))
{
// Don't wait for it any longer if it had a goal, i.e. us.
// Without a goal it was a "lost" ware.
if(ware->GetGoal())
RemoveDependentWare(*ware);
// Add to expedition
world->GetPlayer(player).RemoveWare(*ware);
if(ware->type == GoodType::Boards)
++expedition.boards;
else
++expedition.stones;
// Could be ready now
CheckExpeditionReady();
return;
}
}
nobBaseWarehouse::AddWare(std::move(ware));
}
/// Eine Figur geht ins Lagerhaus
void nobHarborBuilding::AddFigure(std::unique_ptr<noFigure> figure, const bool increase_visual_counts)
{
// Brauchen wir einen Bauarbeiter für die Expedition?
if(figure->GetJobType() == Job::Builder && expedition.active && !expedition.builder)
{
// Make sure the figure came from outside and was not already here waiting for a ship
if(IsDependentFigure(*figure))
nobBaseWarehouse::RemoveDependentFigure(*figure);
GetEvMgr().AddToKillList(std::move(figure));
expedition.builder = true;
// Ggf. ist jetzt alles benötigte da
CheckExpeditionReady();
}
// Brauchen wir einen Spähter für die Expedition?
else if(figure->GetJobType() == Job::Scout && exploration_expedition.active && !IsExplorationExpeditionReady())
{
// Make sure the figure came from outside and was not already here waiting for a ship
if(IsDependentFigure(*figure))
nobBaseWarehouse::RemoveDependentFigure(*figure);
GetEvMgr().AddToKillList(std::move(figure));
++exploration_expedition.scouts;
inventory.visual.Add(Job::Scout);
// Ggf. ist jetzt alles benötigte da
CheckExplorationExpeditionReady();
} else
// ansonsten weiterdelegieren
nobBaseWarehouse::AddFigure(std::move(figure), increase_visual_counts);
}
/// Gibt zurück, ob Expedition vollständig ist
bool nobHarborBuilding::IsExpeditionReady() const
{
if(!expedition.active)
return false;
// Alles da?
if(expedition.boards < BUILDING_COSTS[BuildingType::HarborBuilding].boards)
return false;
if(expedition.stones < BUILDING_COSTS[BuildingType::HarborBuilding].stones)
return false;
if(!expedition.builder)
return false;
return true;
}
/// Gibt zurück, ob Expedition vollständig ist
bool nobHarborBuilding::IsExplorationExpeditionReady() const
{
if(!exploration_expedition.active)
return false;
// Alles da?
if(exploration_expedition.scouts < world->GetGGS().GetNumScoutsExpedition())
return false;
return true;
}
/// Prüft, ob eine Expedition von den Waren her vollständig ist und ruft ggf. das Schiff
void nobHarborBuilding::CheckExpeditionReady()
{
// Alles da?
// Dann bestellen wir mal das Schiff
if(IsExpeditionReady())
OrderShip();
}
/// Prüft, ob eine Expedition von den Spähern her vollständig ist und ruft ggf. das Schiff
void nobHarborBuilding::CheckExplorationExpeditionReady()
{
// Alles da?
// Dann bestellen wir mal das Schiff
if(IsExplorationExpeditionReady())
OrderShip();
}
/// Schiff konnte nicht mehr kommen
void nobHarborBuilding::ShipLost(noShip* /*ship*/)
{
// Neues Schiff bestellen
OrderShip();
}
/// Gibt die Hafenplatz-ID zurück, auf der der Hafen steht
unsigned nobHarborBuilding::GetHarborPosID() const
{
return world->GetHarborPointID(pos);
}
/// Abfangen, wenn ein Mann nicht mehr kommen kann --> könnte ein Bauarbeiter sein und
/// wenn wir einen benötigen, müssen wir einen neuen bestellen
void nobHarborBuilding::RemoveDependentFigure(noFigure& figure)
{
nobBaseWarehouse::RemoveDependentFigure(figure);
// Ist das ein Bauarbeiter und brauchen wir noch einen
if(figure.GetJobType() == Job::Builder && expedition.active && !expedition.builder)
{
// Alle Figuren durchkommen, die noch hierher kommen wollen und gucken, ob ein
// Bauarbeiter dabei ist
for(noFigure* dependent_figure : dependent_figures)
{
if(dependent_figure->GetJobType() == Job::Builder)
// Brauchen keinen bestellen, also raus
return;
}
// Keinen gefunden, also müssen wir noch einen bestellen
world->GetPlayer(player).AddJobWanted(Job::Builder, this);
}
// Ist das ein Erkunder und brauchen wir noch welche?
else if(figure.GetJobType() == Job::Scout && exploration_expedition.active)
{
unsigned scouts_coming = 0;
for(noFigure* dependent_figure : dependent_figures)
{
if(dependent_figure->GetJobType() == Job::Scout)
++scouts_coming;
}
// Wenn nicht genug Erkunder mehr kommen, müssen wir einen neuen bestellen
if(exploration_expedition.scouts + scouts_coming < world->GetGGS().GetNumScoutsExpedition())
world->GetPlayer(player).AddJobWanted(Job::Scout, this);
}
}
/// Gibt eine Liste mit möglichen Verbindungen zurück
std::vector<nobHarborBuilding::ShipConnection> nobHarborBuilding::GetShipConnections() const
{
std::vector<ShipConnection> connections;
// Is the harbor being destroyed right now? Could happen due to pathfinding for wares that get notified about this
// buildings destruction
if(IsBeingDestroyedNow())
return connections;
// Should already be handled by the above check, but keep the runtime check for now (TODO: remove runtime check)
RTTR_Assert(world->GetGOT(pos) == GO_Type::NobHarborbuilding);
// Is there any harbor building at all? (could be destroyed)?
if(world->GetGOT(pos) != GO_Type::NobHarborbuilding)
return connections;
std::vector<nobHarborBuilding*> harbor_buildings;
for(unsigned short seaId : seaIds)
{
if(seaId != 0)
world->GetPlayer(player).GetHarborsAtSea(harbor_buildings, seaId);
}
for(auto* harbor_building : harbor_buildings)
{
ShipConnection sc;
sc.dest = harbor_building;
// Use twice the distance as cost (ship might need to arrive first) and a fixed value to represent
// loading&unloading
sc.way_costs = 2 * world->CalcHarborDistance(GetHarborPosID(), harbor_building->GetHarborPosID()) + 10;
connections.push_back(sc);
}
return connections;
}
/// Fügt einen Mensch hinzu, der mit dem Schiff irgendwo hin fahren will
void nobHarborBuilding::AddFigureForShip(std::unique_ptr<noFigure> fig, MapPoint dest)
{
RTTR_Assert(!world->HasFigureAt(fig->GetPos(), *fig)); // Figure is in the harbor, so it cannot be outside
// Anzahl visuell erhöhen
if(fig->GetJobType() != Job::BoatCarrier)
inventory.visual.Add(fig->GetJobType());
else
{
inventory.visual.Add(Job::Helper);
inventory.visual.Add(GoodType::Boat);
}
figures_for_ships.emplace_back(FigureForShip{std::move(fig), dest});
OrderShip();
}
/// Fügt eine Ware hinzu, die mit dem Schiff verschickt werden soll
void nobHarborBuilding::AddWareForShip(std::unique_ptr<Ware> ware)
{
// Anzahl visuell erhöhen
inventory.visual.Add(ConvertShields(ware->type));
ware->WaitForShip(this);
wares_for_ships.emplace_back(std::move(ware));
OrderShip();
// Take ownership
ware = nullptr;
}
/// Gibt Anzahl der Schiffe zurück, die noch für ausstehende Aufgaben benötigt werden
unsigned nobHarborBuilding::GetNumNeededShips() const
{
unsigned count = 0;
// Expedition -> 1 Schiff
if(IsExpeditionReady())
++count;
// Erkundungs-Expedition -> noch ein Schiff
if(IsExplorationExpeditionReady())
++count;
// Evtl. Waren und Figuren -> noch ein Schiff pro Ziel
if(!figures_for_ships.empty() || !wares_for_ships.empty())
{
// Die verschiedenen Zielhäfen -> Für jeden Hafen ein Schiff ordern
std::vector<MapPoint> destinations;
for(const auto& figures_for_ship : figures_for_ships)
{
if(!helpers::contains(destinations, figures_for_ship.dest))
{
destinations.push_back(figures_for_ship.dest);
++count;
}
}
for(const auto& wares_for_ship : wares_for_ships)
{
if(!helpers::contains(destinations, wares_for_ship->GetNextHarbor()))
{
destinations.push_back(wares_for_ship->GetNextHarbor());
++count;
}
}
}
// Evtl. Angreifer, die noch verschifft werden müssen
if(!soldiers_for_ships.empty())
{
// Die verschiedenen Zielhäfen -> Für jeden Hafen ein Schiff ordern
std::vector<MapPoint> different_dests;
for(const auto& soldiers_for_ship : soldiers_for_ships)
{
if(!helpers::contains(different_dests, soldiers_for_ship.dest))
{
different_dests.push_back(soldiers_for_ship.dest);
++count;
}
}
}
return count;
}
/// Gibt die Wichtigkeit an, dass ein Schiff kommen muss (0 -> keine Bedürftigkeit)
int nobHarborBuilding::GetNeedForShip(unsigned ships_coming) const
{
int points = 0;
// Expedition -> 1 Schiff
if(IsExpeditionReady())
{
if(ships_coming == 0)
points += 100;
else
--ships_coming;
}
if(IsExplorationExpeditionReady())
{
if(ships_coming == 0)
points += 100;
else
--ships_coming;
}
if(!figures_for_ships.empty() || !wares_for_ships.empty())
{
if(ships_coming)
--ships_coming;
else
points += (figures_for_ships.size() + wares_for_ships.size()) * 5;
}
if(!soldiers_for_ships.empty() && ships_coming == 0)
points += (soldiers_for_ships.size() * 10);
return points;
}
// try to order any ship that might be needed and is not ordered yet
void nobHarborBuilding::OrderShip()
{
unsigned needed = GetNumNeededShips();
GamePlayer& owner = world->GetPlayer(player);
// Order (possibly) remaining ships
for(unsigned ordered = owner.GetShipsToHarbor(*this); ordered < needed; ++ordered)
owner.OrderShip(*this);
}
/// Abgeleitete kann eine gerade erzeugte Ware ggf. sofort verwenden
/// (muss in dem Fall true zurückgeben)
bool nobHarborBuilding::UseWareAtOnce(std::unique_ptr<Ware>& ware, noBaseBuilding& goal)
{
// If the ware is coming to us, there is nothing to do
if(&goal == this)