• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Return-To-The-Roots / s25client / 21599995779

02 Feb 2026 05:15PM UTC coverage: 50.785% (+0.1%) from 50.648%
21599995779

Pull #1680

github

web-flow
Merge 26465ead4 into dc0ce04f6
Pull Request #1680: Add support for one-sided alliances

56 of 74 new or added lines in 15 files covered. (75.68%)

1220 existing lines in 13 files now uncovered.

22836 of 44966 relevant lines covered (50.79%)

44088.72 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

75.02
/libs/s25main/GamePlayer.cpp
1
// Copyright (C) 2005 - 2024 Settlers Freaks (sf-team at siedler25.org)
2
//
3
// SPDX-License-Identifier: GPL-2.0-or-later
4

5
#include "GamePlayer.h"
6
#include "Cheats.h"
7
#include "EventManager.h"
8
#include "FindWhConditions.h"
9
#include "GameInterface.h"
10
#include "GlobalGameSettings.h"
11
#include "RoadSegment.h"
12
#include "SerializedGameData.h"
13
#include "TradePathCache.h"
14
#include "Ware.h"
15
#include "WineLoader.h"
16
#include "addons/const_addons.h"
17
#include "buildings/noBuildingSite.h"
18
#include "buildings/nobHQ.h"
19
#include "buildings/nobHarborBuilding.h"
20
#include "buildings/nobMilitary.h"
21
#include "buildings/nobUsual.h"
22
#include "figures/nofCarrier.h"
23
#include "figures/nofFlagWorker.h"
24
#include "helpers/containerUtils.h"
25
#include "helpers/mathFuncs.h"
26
#include "lua/LuaInterfaceGame.h"
27
#include "notifications/ToolNote.h"
28
#include "pathfinding/RoadPathFinder.h"
29
#include "postSystem/DiplomacyPostQuestion.h"
30
#include "postSystem/PostManager.h"
31
#include "random/Random.h"
32
#include "world/GameWorld.h"
33
#include "world/TradeRoute.h"
34
#include "nodeObjs/noFlag.h"
35
#include "nodeObjs/noShip.h"
36
#include "gameTypes/BuildingCount.h"
37
#include "gameTypes/GoodTypes.h"
38
#include "gameTypes/JobTypes.h"
39
#include "gameTypes/PactTypes.h"
40
#include "gameTypes/VisualSettings.h"
41
#include "gameData/BuildingConsts.h"
42
#include "gameData/BuildingProperties.h"
43
#include "gameData/GoodConsts.h"
44
#include "gameData/SettingTypeConv.h"
45
#include "gameData/ShieldConsts.h"
46
#include "gameData/ToolConsts.h"
47
#include "s25util/Log.h"
48
#include <limits>
49
#include <numeric>
50

51
GamePlayer::GamePlayer(unsigned playerId, const PlayerInfo& playerInfo, GameWorld& world)
399✔
52
    : GamePlayerInfo(playerId, playerInfo), world(world), hqPos(MapPoint::Invalid()), emergency(false)
399✔
53
{
54
    std::fill(building_enabled.begin(), building_enabled.end(), true);
399✔
55

56
    LoadStandardDistribution();
399✔
57
    useCustomBuildOrder_ = false;
399✔
58
    build_order = GetStandardBuildOrder();
399✔
59
    transportPrio = STD_TRANSPORT_PRIO;
399✔
60
    LoadStandardMilitarySettings();
399✔
61
    LoadStandardToolSettings();
399✔
62

63
    // Inventur nullen
64
    global_inventory.clear();
399✔
65

66
    // Statistiken mit 0en füllen
67
    statistic = {};
399✔
68
    statisticCurrentData = {};
399✔
69
    statisticCurrentMerchandiseData = {};
399✔
70

71
    RecalcDistribution();
399✔
72
}
399✔
73

74
void GamePlayer::LoadStandardToolSettings()
399✔
75
{
76
    // metalwork tool request
77

78
    // manually
79
    std::fill(tools_ordered.begin(), tools_ordered.end(), 0u);
399✔
80
    std::fill(tools_ordered_delta.begin(), tools_ordered_delta.end(), 0);
399✔
81

82
    // percentage (tool-settings-window-slider, in 10th percent)
83
    toolsSettings_[Tool::Tongs] = 1;
399✔
84
    toolsSettings_[Tool::Hammer] = 4;
399✔
85
    toolsSettings_[Tool::Axe] = 2;
399✔
86
    toolsSettings_[Tool::Saw] = 5;
399✔
87
    toolsSettings_[Tool::PickAxe] = 7;
399✔
88
    toolsSettings_[Tool::Shovel] = 1;
399✔
89
    toolsSettings_[Tool::Crucible] = 3;
399✔
90
    toolsSettings_[Tool::RodAndLine] = 1;
399✔
91
    toolsSettings_[Tool::Scythe] = 2;
399✔
92
    toolsSettings_[Tool::Cleaver] = 1;
399✔
93
    toolsSettings_[Tool::Rollingpin] = 2;
399✔
94
    toolsSettings_[Tool::Bow] = 1;
399✔
95
}
399✔
96

97
void GamePlayer::LoadStandardMilitarySettings()
399✔
98
{
99
    // military settings (military-window-slider, in 10th percent)
100
    militarySettings_[0] = MILITARY_SETTINGS_SCALE[0]; //-V525
399✔
101
    militarySettings_[1] = 3;
399✔
102
    militarySettings_[2] = MILITARY_SETTINGS_SCALE[2];
399✔
103
    militarySettings_[3] = 3;
399✔
104
    militarySettings_[4] = 0;
399✔
105
    militarySettings_[5] = 1;
399✔
106
    militarySettings_[6] = MILITARY_SETTINGS_SCALE[6];
399✔
107
    militarySettings_[7] = MILITARY_SETTINGS_SCALE[7];
399✔
108
}
399✔
109

110
BuildOrders GamePlayer::GetStandardBuildOrder()
401✔
111
{
112
    BuildOrders ordering;
113

114
    // Baureihenfolge füllen
115
    unsigned curPrio = 0;
401✔
116
    for(const auto bld : helpers::enumRange<BuildingType>())
33,684✔
117
    {
118
        if(bld == BuildingType::Headquarters || !BuildingProperties::IsValid(bld))
16,040✔
119
            continue;
2,005✔
120

121
        RTTR_Assert(curPrio < ordering.size());
14,035✔
122
        ordering[curPrio] = bld;
14,035✔
123
        ++curPrio;
14,035✔
124
    }
125
    RTTR_Assert(curPrio == ordering.size());
401✔
126
    return ordering;
401✔
127
}
128

129
void GamePlayer::LoadStandardDistribution()
399✔
130
{
131
    // Verteilung mit Standardwerten füllen bei Waren mit nur einem Ziel (wie z.B. Mehl, Holz...)
132
    distribution[GoodType::Flour].client_buildings.push_back(BuildingType::Bakery);
399✔
133
    distribution[GoodType::Gold].client_buildings.push_back(BuildingType::Mint);
399✔
134
    distribution[GoodType::IronOre].client_buildings.push_back(BuildingType::Ironsmelter);
399✔
135
    distribution[GoodType::Ham].client_buildings.push_back(BuildingType::Slaughterhouse);
399✔
136
    distribution[GoodType::Stones].client_buildings.push_back(
399✔
137
      BuildingType::Headquarters); // BuildingType::Headquarters = Baustellen!
399✔
138
    distribution[GoodType::Stones].client_buildings.push_back(BuildingType::Catapult);
399✔
139
    distribution[GoodType::Grapes].client_buildings.push_back(BuildingType::Winery);
399✔
140
    distribution[GoodType::Wine].client_buildings.push_back(BuildingType::Temple);
399✔
141

142
    // Waren mit mehreren möglichen Zielen erstmal nullen, kann dann im Fenster eingestellt werden
143
    for(const auto i : helpers::enumRange<GoodType>())
31,122✔
144
    {
145
        std::fill(distribution[i].percent_buildings.begin(), distribution[i].percent_buildings.end(), 0);
14,763✔
146
        distribution[i].selected_goal = 0;
14,763✔
147
    }
148

149
    // Standardverteilung der Waren
150
    for(const DistributionMapping& mapping : distributionMap)
10,773✔
151
    {
152
        distribution[std::get<0>(mapping)].percent_buildings[std::get<1>(mapping)] = std::get<2>(mapping);
10,374✔
153
    }
154
}
399✔
155

156
GamePlayer::~GamePlayer() = default;
806✔
157

158
void GamePlayer::Serialize(SerializedGameData& sgd) const
17✔
159
{
160
    // PlayerStatus speichern, ehemalig
161
    sgd.PushEnum<uint8_t>(ps);
17✔
162

163
    // Nur richtige Spieler serialisieren
164
    if(ps != PlayerState::Occupied && ps != PlayerState::AI)
17✔
165
        return;
3✔
166

167
    sgd.PushBool(isDefeated);
14✔
168

169
    buildings.Serialize(sgd);
14✔
170

171
    sgd.PushObjectContainer(roads, true);
14✔
172

173
    sgd.PushUnsignedInt(jobs_wanted.size());
14✔
174
    for(const JobNeeded& job : jobs_wanted)
18✔
175
    {
176
        sgd.PushEnum<uint8_t>(job.job);
4✔
177
        sgd.PushObject(job.workplace);
4✔
178
    }
179

180
    sgd.PushObjectContainer(ware_list, true);
14✔
181
    sgd.PushObjectContainer(flagworkers);
14✔
182
    sgd.PushObjectContainer(ships, true);
14✔
183

184
    helpers::pushContainer(sgd, shouldSendDefenderList);
14✔
185
    helpers::pushPoint(sgd, hqPos);
14✔
186

187
    for(const Distribution& dist : distribution)
532✔
188
    {
189
        helpers::pushContainer(sgd, dist.percent_buildings);
518✔
190
        helpers::pushContainer(sgd, dist.client_buildings);
518✔
191
        helpers::pushContainer(sgd, dist.goals);
518✔
192
        sgd.PushUnsignedInt(dist.selected_goal);
518✔
193
    }
194

195
    sgd.PushBool(useCustomBuildOrder_);
14✔
196
    helpers::pushContainer(sgd, build_order);
14✔
197
    helpers::pushContainer(sgd, transportPrio);
14✔
198
    helpers::pushContainer(sgd, militarySettings_);
14✔
199
    helpers::pushContainer(sgd, toolsSettings_);
14✔
200
    helpers::pushContainer(sgd, tools_ordered);
14✔
201
    helpers::pushContainer(sgd, global_inventory.goods);
14✔
202
    helpers::pushContainer(sgd, global_inventory.people);
14✔
203

204
    // für Statistik
205
    for(const Statistic& curStatistic : statistic)
70✔
206
    {
207
        // normale Statistik
208
        for(const auto& curData : curStatistic.data)
560✔
209
            helpers::pushContainer(sgd, curData);
504✔
210

211
        // Warenstatistik
212
        for(unsigned j = 0; j < NUM_STAT_MERCHANDISE_TYPES; ++j)
840✔
213
            helpers::pushContainer(sgd, curStatistic.merchandiseData[j]);
784✔
214

215
        sgd.PushUnsignedShort(curStatistic.currentIndex);
56✔
216
        sgd.PushUnsignedShort(curStatistic.counter);
56✔
217
    }
218
    helpers::pushContainer(sgd, statisticCurrentData);
14✔
219
    helpers::pushContainer(sgd, statisticCurrentMerchandiseData);
14✔
220

221
    // Serialize Pacts:
222
    for(const auto& playerPacts : pacts)
126✔
223
    {
224
        for(const Pact& pact : playerPacts)
448✔
225
            pact.Serialize(sgd);
336✔
226
    }
227

228
    sgd.PushBool(emergency);
14✔
229
}
230

231
void GamePlayer::Deserialize(SerializedGameData& sgd)
8✔
232
{
233
    std::fill(building_enabled.begin(), building_enabled.end(), true);
8✔
234

235
    // Ehemaligen PS auslesen
236
    auto origin_ps = sgd.Pop<PlayerState>();
8✔
237
    // Nur richtige Spieler serialisieren
238
    if(origin_ps != PlayerState::Occupied && origin_ps != PlayerState::AI)
8✔
239
        return;
1✔
240

241
    isDefeated = sgd.PopBool();
7✔
242
    buildings.Deserialize(sgd);
7✔
243

244
    sgd.PopObjectContainer(roads, GO_Type::Roadsegment);
7✔
245

246
    jobs_wanted.resize(sgd.PopUnsignedInt());
7✔
247
    for(JobNeeded& job : jobs_wanted)
9✔
248
    {
249
        job.job = sgd.Pop<Job>();
2✔
250
        job.workplace = sgd.PopObject<noRoadNode>();
2✔
251
    }
252

253
    if(sgd.GetGameDataVersion() < 2)
7✔
UNCOV
254
        buildings.Deserialize2(sgd);
×
255

256
    sgd.PopObjectContainer(ware_list, GO_Type::Ware);
7✔
257
    sgd.PopObjectContainer(flagworkers);
7✔
258
    sgd.PopObjectContainer(ships, GO_Type::Ship);
7✔
259

260
    sgd.PopContainer(shouldSendDefenderList);
7✔
261

262
    hqPos = sgd.PopMapPoint();
7✔
263

264
    for(const auto i : helpers::enumRange<GoodType>())
546✔
265
    {
266
        if(sgd.GetGameDataVersion() < 11 && wineaddon::isWineAddonGoodType(i))
259✔
UNCOV
267
            continue;
×
268

269
        Distribution& dist = distribution[i];
259✔
270
        helpers::popContainer(sgd, dist.percent_buildings);
259✔
271
        if(sgd.GetGameDataVersion() < 7)
259✔
272
        {
273
            dist.client_buildings.resize(sgd.PopUnsignedInt());
×
274
            helpers::popContainer(sgd, dist.client_buildings, true);
×
275
            dist.goals.resize(sgd.PopUnsignedInt());
×
UNCOV
276
            helpers::popContainer(sgd, dist.goals, true);
×
277
        } else
278
        {
279
            helpers::popContainer(sgd, dist.client_buildings);
259✔
280
            helpers::popContainer(sgd, dist.goals);
259✔
281
        }
282
        dist.selected_goal = sgd.PopUnsignedInt();
259✔
283
    }
284

285
    useCustomBuildOrder_ = sgd.PopBool();
7✔
286

287
    if(sgd.GetGameDataVersion() < 11)
7✔
288
    {
289
        std::vector<BuildingType> build_order_raw(build_order.size() - 3);
×
290
        helpers::popContainer(sgd, build_order_raw, true);
×
291
        build_order_raw.insert(build_order_raw.end(),
×
292
                               {BuildingType::Vineyard, BuildingType::Winery, BuildingType::Temple});
×
UNCOV
293
        std::copy(build_order_raw.begin(), build_order_raw.end(), build_order.begin());
×
294

295
        std::vector<uint8_t> transportPrio_raw(transportPrio.size() - 2);
×
296
        helpers::popContainer(sgd, transportPrio_raw, true);
×
UNCOV
297
        std::copy(transportPrio_raw.begin(), transportPrio_raw.end(), transportPrio.begin());
×
298
    } else
299
    {
300
        helpers::popContainer(sgd, build_order);
7✔
301
        helpers::popContainer(sgd, transportPrio);
7✔
302
    }
303

304
    helpers::popContainer(sgd, militarySettings_);
7✔
305
    helpers::popContainer(sgd, toolsSettings_);
7✔
306

307
    // qx:tools
308
    helpers::popContainer(sgd, tools_ordered);
7✔
309
    tools_ordered_delta = {};
7✔
310

311
    if(sgd.GetGameDataVersion() < 11)
7✔
312
    {
313
        std::vector<unsigned int> global_inventory_good_raw(global_inventory.goods.size() - 2);
×
314
        helpers::popContainer(sgd, global_inventory_good_raw, true);
×
UNCOV
315
        std::copy(global_inventory_good_raw.begin(), global_inventory_good_raw.end(), global_inventory.goods.begin());
×
316

317
        std::vector<unsigned int> global_inventory_people_raw(global_inventory.people.size() - 3);
×
318
        helpers::popContainer(sgd, global_inventory_people_raw, true);
×
UNCOV
319
        std::copy(global_inventory_people_raw.begin(), global_inventory_people_raw.end(),
×
320
                  global_inventory.people.begin());
321
    } else
322
    {
323
        helpers::popContainer(sgd, global_inventory.goods);
7✔
324
        helpers::popContainer(sgd, global_inventory.people);
7✔
325
    }
326

327
    // Visuelle Einstellungen festlegen
328

329
    // für Statistik
330
    for(Statistic& curStatistic : statistic)
35✔
331
    {
332
        // normale Statistik
333
        for(auto& curData : curStatistic.data)
280✔
334
            helpers::popContainer(sgd, curData);
252✔
335

336
        // Warenstatistik
337
        for(unsigned j = 0; j < NUM_STAT_MERCHANDISE_TYPES; ++j)
420✔
338
            helpers::popContainer(sgd, curStatistic.merchandiseData[j]);
392✔
339

340
        curStatistic.currentIndex = sgd.PopUnsignedShort();
28✔
341
        curStatistic.counter = sgd.PopUnsignedShort();
28✔
342
    }
343
    helpers::popContainer(sgd, statisticCurrentData);
7✔
344
    helpers::popContainer(sgd, statisticCurrentMerchandiseData);
7✔
345

346
    // Deserialize Pacts:
347
    for(auto& playerPacts : pacts)
63✔
348
    {
349
        for(Pact& pact : playerPacts)
224✔
350
            pact = GamePlayer::Pact(sgd);
168✔
351
    }
352

353
    emergency = sgd.PopBool();
7✔
354
}
355

356
template<class T_IsWarehouseGood>
357
nobBaseWarehouse* GamePlayer::FindWarehouse(const noRoadNode& start, const T_IsWarehouseGood& isWarehouseGood,
1,525✔
358
                                            bool to_wh, bool use_boat_roads, unsigned* length,
359
                                            const RoadSegment* forbidden) const
360
{
361
    nobBaseWarehouse* best = nullptr;
1,525✔
362

363
    unsigned best_length = std::numeric_limits<unsigned>::max();
1,525✔
364

365
    for(nobBaseWarehouse* wh : buildings.GetStorehouses())
3,508✔
366
    {
367
        // Lagerhaus geeignet?
368
        RTTR_Assert(wh);
1,988✔
369
        if(!isWarehouseGood(*wh))
1,988✔
370
            continue;
1,144✔
371

372
        if(start.GetPos() == wh->GetPos())
853✔
373
        {
374
            // We are already there -> Take it
375
            if(length)
5✔
UNCOV
376
                *length = 0;
×
377
            return wh;
5✔
378
        }
379

380
        // now check if there is at least a chance that the next wh is closer than current best because pathfinding
381
        // takes time
382
        if(world.CalcDistance(start.GetPos(), wh->GetPos()) > best_length)
848✔
383
            continue;
9✔
384
        // Bei der erlaubten Benutzung von Bootsstraßen Waren-Pathfinding benutzen wenns zu nem Lagerhaus gehn soll
385
        // start <-> ziel tauschen bei der wegfindung
386
        unsigned tlength;
387
        if(world.GetRoadPathFinder().FindPath(to_wh ? start : *wh, to_wh ? *wh : start, use_boat_roads, best_length,
839✔
388
                                              forbidden, &tlength))
389
        {
390
            if(tlength < best_length || !best)
365✔
391
            {
392
                best_length = tlength;
361✔
393
                best = wh;
361✔
394
            }
395
        }
396
    }
397

398
    if(length)
1,520✔
399
        *length = best_length;
374✔
400

401
    return best;
1,520✔
402
}
403

404
void GamePlayer::AddBuildingSite(noBuildingSite* bldSite)
28✔
405
{
406
    RTTR_Assert(bldSite->GetPlayer() == GetPlayerId());
28✔
407
    buildings.Add(bldSite);
28✔
408
}
28✔
409

410
void GamePlayer::RemoveBuildingSite(noBuildingSite* bldSite)
15✔
411
{
412
    RTTR_Assert(bldSite->GetPlayer() == GetPlayerId());
15✔
413
    buildings.Remove(bldSite);
15✔
414
}
15✔
415

416
bool GamePlayer::IsHQTent() const
8✔
417
{
418
    if(const nobHQ* hq = GetHQ())
8✔
419
        return hq->IsTent();
8✔
UNCOV
420
    return false;
×
421
}
422

423
void GamePlayer::SetHQIsTent(bool isTent)
6✔
424
{
425
    if(nobHQ* hq = GetHQ())
6✔
426
        hq->SetIsTent(isTent);
5✔
427
}
6✔
428

429
void GamePlayer::AddBuilding(noBuilding* bld, BuildingType bldType)
605✔
430
{
431
    RTTR_Assert(bld->GetPlayer() == GetPlayerId());
605✔
432
    buildings.Add(bld, bldType);
605✔
433
    ChangeStatisticValue(StatisticType::Buildings, 1);
605✔
434

435
    // Order a worker if needed
436
    const auto& description = BLD_WORK_DESC[bldType];
605✔
437
    if(description.job && description.job != Job::Private)
605✔
438
    {
439
        AddJobWanted(*description.job, bld);
102✔
440
    }
441

442
    if(bldType == BuildingType::HarborBuilding)
605✔
443
    {
444
        // Schiff durchgehen und denen Bescheid sagen
445
        for(noShip* ship : ships)
51✔
446
            ship->NewHarborBuilt(static_cast<nobHarborBuilding*>(bld));
11✔
447
    } else if(bldType == BuildingType::Headquarters)
565✔
448
    {
449
        // If there is more than one HQ, keep the original position.
450
        if(!hqPos.isValid())
353✔
451
            hqPos = bld->GetPos();
344✔
452
    } else if(BuildingProperties::IsMilitary(bldType))
212✔
453
    {
454
        auto* milBld = static_cast<nobMilitary*>(bld);
101✔
455
        // New built? -> Calculate frontier distance
456
        if(milBld->IsNewBuilt())
101✔
457
            milBld->LookForEnemyBuildings();
95✔
458
    }
459
}
605✔
460

461
void GamePlayer::RemoveBuilding(noBuilding* bld, BuildingType bldType)
108✔
462
{
463
    RTTR_Assert(bld->GetPlayer() == GetPlayerId());
108✔
464
    buildings.Remove(bld, bldType);
108✔
465
    ChangeStatisticValue(StatisticType::Buildings, -1);
108✔
466
    if(bldType == BuildingType::HarborBuilding)
108✔
467
    { // Schiffen Bescheid sagen
468
        for(noShip* ship : ships)
27✔
469
            ship->HarborDestroyed(static_cast<nobHarborBuilding*>(bld));
12✔
470
    } else if(bldType == BuildingType::Headquarters)
93✔
471
    {
472
        hqPos = MapPoint::Invalid();
31✔
473
        for(const noBaseBuilding* bld : buildings.GetStorehouses())
31✔
474
        {
475
            if(bld->GetBuildingType() == BuildingType::Headquarters)
1✔
476
            {
477
                hqPos = bld->GetPos();
1✔
478
                break;
1✔
479
            }
480
        }
481
    }
482
    if(BuildingProperties::IsWareHouse(bldType) || BuildingProperties::IsMilitary(bldType))
108✔
483
        TestDefeat();
78✔
484
}
108✔
485

486
void GamePlayer::NewRoadConnection(RoadSegment* rs)
156✔
487
{
488
    // Zu den Straßen hinzufgen, da's ja ne neue ist
489
    roads.push_back(rs);
156✔
490

491
    // Alle Straßen müssen nun gucken, ob sie einen Weg zu einem Warehouse finden
492
    FindCarrierForAllRoads();
156✔
493

494
    // Alle Straßen müssen gucken, ob sie einen Esel bekommen können
495
    for(RoadSegment* rs : roads)
339✔
496
        rs->TryGetDonkey();
183✔
497

498
    // Alle Arbeitsplätze müssen nun gucken, ob sie einen Weg zu einem Lagerhaus mit entsprechender Arbeitskraft finden
499
    FindWarehouseForAllJobs();
156✔
500

501
    // Alle Baustellen müssen nun gucken, ob sie ihr benötigtes Baumaterial bekommen (evtl war vorher die Straße zum
502
    // Lagerhaus unterbrochen
503
    FindMaterialForBuildingSites();
156✔
504

505
    // Alle Lost-Wares müssen gucken, ob sie ein Lagerhaus finden
506
    FindClientForLostWares();
156✔
507

508
    // Alle Militärgebäude müssen ihre Truppen überprüfen und können nun ggf. neue bestellen
509
    // und müssen prüfen, ob sie evtl Gold bekommen
510
    for(nobMilitary* mil : buildings.GetMilitaryBuildings())
175✔
511
    {
512
        mil->RegulateTroops();
19✔
513
        mil->SearchCoins();
19✔
514
    }
515
}
156✔
516

517
void GamePlayer::AddRoad(RoadSegment* rs)
18✔
518
{
519
    roads.push_back(rs);
18✔
520
}
18✔
521

522
void GamePlayer::DeleteRoad(RoadSegment* rs)
103✔
523
{
524
    RTTR_Assert(helpers::contains(roads, rs));
103✔
525
    roads.remove(rs);
103✔
526
}
103✔
527

528
void GamePlayer::FindClientForLostWares()
162✔
529
{
530
    // Alle Lost-Wares müssen gucken, ob sie ein Lagerhaus finden
531
    for(Ware* ware : ware_list)
218✔
532
    {
533
        if(ware->IsLostWare())
56✔
534
        {
UNCOV
535
            if(ware->FindRouteToWarehouse() && ware->IsWaitingAtFlag())
×
UNCOV
536
                ware->CallCarrier();
×
537
        }
538
    }
539
}
162✔
540

541
void GamePlayer::RoadDestroyed()
220✔
542
{
543
    // Alle Waren, die an Flagge liegen und in Lagerhäusern, müssen gucken, ob sie ihr Ziel noch erreichen können, jetzt
544
    // wo eine Straße fehlt
545
    for(auto it = ware_list.begin(); it != ware_list.end();)
230✔
546
    {
547
        Ware* ware = *it;
10✔
548
        if(ware->IsWaitingAtFlag()) // Liegt die Flagge an einer Flagge, muss ihr Weg neu berechnet werden
10✔
549
        {
550
            RoadPathDirection last_next_dir = ware->GetNextDir();
2✔
551
            ware->RecalcRoute();
2✔
552
            // special case: ware was lost some time ago and the new goal is at this flag and not a warehouse,hq,harbor
553
            // and the "flip-route" picked so a carrier would pick up the ware carry it away from goal then back and
554
            // drop  it off at the goal was just destroyed?
555
            // -> try to pick another flip route or tell the goal about failure.
556
            noRoadNode& wareLocation = *ware->GetLocation();
2✔
557
            noBaseBuilding* wareGoal = ware->GetGoal();
2✔
558
            if(wareGoal && ware->GetNextDir() == RoadPathDirection::NorthWest
2✔
559
               && wareLocation.GetPos() == wareGoal->GetFlagPos()
2✔
560
               && ((wareGoal->GetBuildingType() != BuildingType::Storehouse
6✔
561
                    && wareGoal->GetBuildingType() != BuildingType::Headquarters
2✔
UNCOV
562
                    && wareGoal->GetBuildingType() != BuildingType::HarborBuilding)
×
563
                   || wareGoal->GetType() == NodalObjectType::Buildingsite))
2✔
564
            {
565
                Direction newWareDir = Direction::NorthWest;
×
UNCOV
566
                for(auto dir : helpers::EnumRange<Direction>{})
×
567
                {
UNCOV
568
                    dir += 2u; // Need to skip Direction::NorthWest and we used to start with an offset of 2. TODO:
×
569
                               // Increase gameDataVersion and just skip NW
UNCOV
570
                    if(wareLocation.GetRoute(dir))
×
571
                    {
UNCOV
572
                        newWareDir = dir;
×
UNCOV
573
                        break;
×
574
                    }
575
                }
576
                if(newWareDir != Direction::NorthWest)
×
577
                {
UNCOV
578
                    ware->SetNextDir(toRoadPathDirection(newWareDir));
×
579
                } else // no route to goal -> notify goal, try to send ware to a warehouse
580
                {
UNCOV
581
                    ware->NotifyGoalAboutLostWare();
×
UNCOV
582
                    ware->FindRouteToWarehouse();
×
583
                }
584
            }
585
            // end of special case
586

587
            // notify carriers/flags about news if there are any
588
            if(ware->GetNextDir() != last_next_dir)
2✔
589
            {
590
                // notify current flag that transport in the old direction might not longer be required
UNCOV
591
                ware->RemoveWareJobForDir(last_next_dir);
×
UNCOV
592
                if(ware->GetNextDir() != RoadPathDirection::None)
×
UNCOV
593
                    ware->CallCarrier();
×
594
            }
595
        } else if(ware->IsWaitingInWarehouse())
8✔
596
        {
UNCOV
597
            if(!ware->IsRouteToGoal())
×
598
            {
599
                // Das Ziel wird nun nich mehr beliefert
UNCOV
600
                ware->NotifyGoalAboutLostWare();
×
601
                // Ware aus der Warteliste des Lagerhauses entfernen
UNCOV
602
                static_cast<nobBaseWarehouse*>(ware->GetLocation())->CancelWare(ware);
×
603
                // Ware aus der Liste raus
UNCOV
604
                it = ware_list.erase(it);
×
UNCOV
605
                continue;
×
606
            }
607
        } else if(ware->IsWaitingForShip())
8✔
608
        {
609
            // Weg neu berechnen
610
            ware->RecalcRoute();
1✔
611
        }
612

613
        ++it;
10✔
614
    }
615

616
    // Alle Häfen müssen ihre Figuren den Weg überprüfen lassen
617
    for(nobHarborBuilding* hb : buildings.GetHarbors())
237✔
618
    {
619
        hb->ExamineShipRouteOfPeople();
17✔
620
    }
621
}
220✔
622

623
bool GamePlayer::FindCarrierForRoad(RoadSegment* rs) const
184✔
624
{
625
    RTTR_Assert(rs->GetF1() != nullptr && rs->GetF2() != nullptr);
184✔
626
    std::array<unsigned, 2> length;
627
    std::array<nobBaseWarehouse*, 2> best;
628

629
    // Braucht der ein Boot?
630
    if(rs->GetRoadType() == RoadType::Water)
184✔
631
    {
632
        // dann braucht man Träger UND Boot
UNCOV
633
        best[0] = FindWarehouse(*rs->GetF1(), FW::HasWareAndFigure(GoodType::Boat, Job::Helper, false), false, false,
×
634
                                length.data(), rs);
635
        // 2. Flagge des Weges
UNCOV
636
        best[1] = FindWarehouse(*rs->GetF2(), FW::HasWareAndFigure(GoodType::Boat, Job::Helper, false), false, false,
×
UNCOV
637
                                &length[1], rs);
×
638
    } else
639
    {
640
        // 1. Flagge des Weges
641
        best[0] = FindWarehouse(*rs->GetF1(), FW::HasFigure(Job::Helper, false), false, false, length.data(), rs);
184✔
642
        // 2. Flagge des Weges
643
        best[1] = FindWarehouse(*rs->GetF2(), FW::HasFigure(Job::Helper, false), false, false, &length[1], rs);
184✔
644
    }
645

646
    // überhaupt nen Weg gefunden?
647
    // Welche Flagge benutzen?
648
    if(best[0] && (!best[1] || length[0] < length[1]))
184✔
649
        best[0]->OrderCarrier(*rs->GetF1(), *rs);
64✔
650
    else if(best[1])
120✔
651
        best[1]->OrderCarrier(*rs->GetF2(), *rs);
42✔
652
    else
653
        return false;
78✔
654
    return true;
106✔
655
}
656

UNCOV
657
bool GamePlayer::IsWarehouseValid(nobBaseWarehouse* wh) const
×
658
{
UNCOV
659
    return helpers::contains(buildings.GetStorehouses(), wh);
×
660
}
661

662
void GamePlayer::RecalcDistribution()
405✔
663
{
664
    GoodType lastWare = GoodType::Nothing;
405✔
665
    for(const DistributionMapping& mapping : distributionMap)
10,935✔
666
    {
667
        if(lastWare == std::get<0>(mapping))
10,530✔
668
            continue;
7,695✔
669
        lastWare = std::get<0>(mapping);
2,835✔
670
        RecalcDistributionOfWare(std::get<0>(mapping));
2,835✔
671
    }
672
}
405✔
673

674
void GamePlayer::RecalcDistributionOfWare(const GoodType ware)
2,835✔
675
{
676
    // Punktesystem zur Verteilung, in der Liste alle Gebäude sammeln, die die Ware wollen
677
    distribution[ware].client_buildings.clear();
2,835✔
678

679
    // 1. Anteile der einzelnen Waren ausrechnen
680

681
    /// Mapping of buildings that want the current ware to its percentage
682
    using BldEntry = std::pair<BuildingType, uint8_t>;
683
    std::vector<BldEntry> bldPercentageMap;
2,835✔
684

685
    unsigned goal_count = 0;
2,835✔
686

687
    for(const auto bld : helpers::enumRange<BuildingType>())
238,140✔
688
    {
689
        uint8_t percentForCurBld = distribution[ware].percent_buildings[bld];
113,400✔
690
        if(percentForCurBld)
113,400✔
691
        {
692
            distribution[ware].client_buildings.push_back(bld);
10,529✔
693
            goal_count += percentForCurBld;
10,529✔
694
            bldPercentageMap.emplace_back(bld, percentForCurBld);
10,529✔
695
        }
696
    }
697

698
    // TODO: evtl noch die counts miteinander kürzen (ggt berechnen)
699

700
    // Array für die Gebäudtypen erstellen
701

702
    std::vector<BuildingType>& wareGoals = distribution[ware].goals;
2,835✔
703
    wareGoals.clear();
2,835✔
704
    wareGoals.reserve(goal_count);
2,835✔
705

706
    // just drop them in the list, the distribution will be handled by going through this list using a prime as step
707
    // (see GameClientPlayer::FindClientForWare)
708
    for(const BldEntry& bldEntry : bldPercentageMap)
13,364✔
709
    {
710
        for(unsigned char i = 0; i < bldEntry.second; ++i)
69,207✔
711
            wareGoals.push_back(bldEntry.first);
58,678✔
712
    }
713

714
    distribution[ware].selected_goal = 0;
2,835✔
715
}
2,835✔
716

717
void GamePlayer::FindCarrierForAllRoads()
176✔
718
{
719
    for(RoadSegment* rs : roads)
388✔
720
    {
721
        if(!rs->hasCarrier(0))
212✔
722
            FindCarrierForRoad(rs);
162✔
723
    }
724
}
176✔
725

726
void GamePlayer::FindMaterialForBuildingSites()
171✔
727
{
728
    for(noBuildingSite* bldSite : buildings.GetBuildingSites())
194✔
729
        bldSite->OrderConstructionMaterial();
23✔
730
}
171✔
731

732
void GamePlayer::AddJobWanted(const Job job, noRoadNode* workplace)
138✔
733
{
734
    // Und gleich suchen
735
    if(!FindWarehouseForJob(job, workplace))
138✔
736
    {
737
        JobNeeded jn = {job, workplace};
125✔
738
        jobs_wanted.push_back(jn);
125✔
739
    }
740
}
138✔
741

742
void GamePlayer::JobNotWanted(noRoadNode* workplace, bool all)
53✔
743
{
744
    for(auto it = jobs_wanted.begin(); it != jobs_wanted.end();)
57✔
745
    {
746
        if(it->workplace == workplace)
42✔
747
        {
748
            it = jobs_wanted.erase(it);
38✔
749
            if(!all)
38✔
750
                return;
38✔
751
        } else
752
        {
753
            ++it;
4✔
754
        }
755
    }
756
}
757

758
void GamePlayer::OneJobNotWanted(const Job job, noRoadNode* workplace)
2✔
759
{
760
    const auto it = helpers::find_if(
761
      jobs_wanted, [workplace, job](const auto& it) { return it.workplace == workplace && it.job == job; });
2✔
762
    if(it != jobs_wanted.end())
2✔
UNCOV
763
        jobs_wanted.erase(it);
×
764
}
2✔
765

766
void GamePlayer::SendPostMessage(std::unique_ptr<PostMsg> msg)
35✔
767
{
768
    world.GetPostMgr().SendMsg(GetPlayerId(), std::move(msg));
35✔
769
}
35✔
770

771
unsigned GamePlayer::GetToolsOrderedVisual(Tool tool) const
×
772
{
UNCOV
773
    return std::max(0, int(tools_ordered[tool] + tools_ordered_delta[tool]));
×
774
}
775

776
unsigned GamePlayer::GetToolsOrdered(Tool tool) const
369✔
777
{
778
    return tools_ordered[tool];
369✔
779
}
780

UNCOV
781
bool GamePlayer::ChangeToolOrderVisual(Tool tool, int changeAmount) const
×
782
{
UNCOV
783
    if(std::abs(changeAmount) > 100)
×
UNCOV
784
        return false;
×
UNCOV
785
    int newOrderAmount = int(GetToolsOrderedVisual(tool)) + changeAmount;
×
UNCOV
786
    if(newOrderAmount < 0 || newOrderAmount > 100)
×
UNCOV
787
        return false;
×
UNCOV
788
    tools_ordered_delta[tool] += changeAmount;
×
UNCOV
789
    return true;
×
790
}
791

792
unsigned GamePlayer::GetToolPriority(Tool tool) const
271✔
793
{
794
    return toolsSettings_[tool];
271✔
795
}
796

797
void GamePlayer::ToolOrderProcessed(Tool tool)
4✔
798
{
799
    if(tools_ordered[tool])
4✔
800
    {
801
        --tools_ordered[tool];
4✔
802
        world.GetNotifications().publish(ToolNote(ToolNote::OrderCompleted, GetPlayerId()));
4✔
803
    }
804
}
4✔
805

806
bool GamePlayer::FindWarehouseForJob(const Job job, noRoadNode* goal) const
157✔
807
{
808
    nobBaseWarehouse* wh = FindWarehouse(*goal, FW::HasFigure(job, true), false, false);
157✔
809

810
    if(wh)
157✔
811
    {
812
        // Es wurde ein Lagerhaus gefunden, wo es den geforderten Beruf gibt, also den Typen zur Arbeit rufen
813
        wh->OrderJob(job, goal, true);
32✔
814
        return true;
32✔
815
    }
816

817
    return false;
125✔
818
}
819

820
void GamePlayer::FindWarehouseForAllJobs()
175✔
821
{
822
    for(auto it = jobs_wanted.begin(); it != jobs_wanted.end();)
194✔
823
    {
824
        if(FindWarehouseForJob(it->job, it->workplace))
19✔
825
            it = jobs_wanted.erase(it);
19✔
826
        else
UNCOV
827
            ++it;
×
828
    }
829
}
175✔
830

831
void GamePlayer::FindWarehouseForAllJobs(const Job job)
209✔
832
{
833
    for(auto it = jobs_wanted.begin(); it != jobs_wanted.end();)
209✔
834
    {
UNCOV
835
        if(it->job == job)
×
836
        {
UNCOV
837
            if(FindWarehouseForJob(it->job, it->workplace))
×
UNCOV
838
                it = jobs_wanted.erase(it);
×
839
            else
UNCOV
840
                ++it;
×
841
        } else
842
            ++it;
×
843
    }
844
}
209✔
845

846
Ware* GamePlayer::OrderWare(const GoodType ware, noBaseBuilding* goal)
149✔
847
{
848
    /// Gibt es ein Lagerhaus mit dieser Ware?
849
    nobBaseWarehouse* wh = FindWarehouse(*goal, FW::HasMinWares(ware, 1), false, true);
149✔
850

851
    if(wh)
149✔
852
    {
853
        // Prüfe ob Notfallprogramm aktiv
854
        if(!emergency)
108✔
855
            return wh->OrderWare(ware, goal);
108✔
856
        else
857
        {
858
            // Wenn Notfallprogramm aktiv nur an Holzfäller und Sägewerke Bretter/Steine liefern
859
            if((ware != GoodType::Boards && ware != GoodType::Stones)
×
UNCOV
860
               || goal->GetBuildingType() == BuildingType::Woodcutter
×
861
               || goal->GetBuildingType() == BuildingType::Sawmill)
×
862
                return wh->OrderWare(ware, goal);
×
863
            else
UNCOV
864
                return nullptr;
×
865
        }
866
    } else // no warehouse can deliver the ware -> check all our wares for lost wares that might match the order
867
    {
868
        unsigned bestLength = std::numeric_limits<unsigned>::max();
41✔
869
        Ware* bestWare = nullptr;
41✔
870
        for(Ware* curWare : ware_list)
85✔
871
        {
872
            if(curWare->IsLostWare() && curWare->type == ware)
44✔
873
            {
874
                // got a lost ware with a road to goal -> find best
UNCOV
875
                unsigned curLength = curWare->CheckNewGoalForLostWare(*goal);
×
UNCOV
876
                if(curLength < bestLength)
×
877
                {
UNCOV
878
                    bestLength = curLength;
×
UNCOV
879
                    bestWare = curWare;
×
880
                }
881
            }
882
        }
883
        if(bestWare)
41✔
884
        {
UNCOV
885
            bestWare->SetNewGoalForLostWare(goal);
×
UNCOV
886
            return bestWare;
×
887
        }
888
    }
889
    return nullptr;
41✔
890
}
891

892
nofCarrier* GamePlayer::OrderDonkey(RoadSegment* road) const
3✔
893
{
894
    std::array<unsigned, 2> length;
895
    std::array<nobBaseWarehouse*, 2> best;
896

897
    // 1. Flagge des Weges
898
    best[0] = FindWarehouse(*road->GetF1(), FW::HasFigure(Job::PackDonkey, false), false, false, length.data(), road);
3✔
899
    // 2. Flagge des Weges
900
    best[1] = FindWarehouse(*road->GetF2(), FW::HasFigure(Job::PackDonkey, false), false, false, &length[1], road);
3✔
901

902
    // überhaupt nen Weg gefunden?
903
    // Welche Flagge benutzen?
904
    if(best[0] && (!best[1] || length[0] < length[1]))
3✔
905
        return best[0]->OrderDonkey(road, road->GetF1());
2✔
906
    else if(best[1])
1✔
UNCOV
907
        return best[1]->OrderDonkey(road, road->GetF2());
×
908
    else
909
        return nullptr;
1✔
910
}
911

912
RoadSegment* GamePlayer::FindRoadForDonkey(noRoadNode* start, noRoadNode** goal)
8✔
913
{
914
    // Bisher höchste Trägerproduktivität und die entsprechende Straße dazu
915
    unsigned best_productivity = 0;
8✔
916
    RoadSegment* best_road = nullptr;
8✔
917
    // Beste Flagge dieser Straße
918
    *goal = nullptr;
8✔
919

920
    for(RoadSegment* roadSeg : roads)
8✔
921
    {
922
        // Braucht die Straße einen Esel?
UNCOV
923
        if(roadSeg->NeedDonkey())
×
924
        {
925
            // Beste Flagge von diesem Weg, und beste Wegstrecke
UNCOV
926
            noRoadNode* current_best_goal = nullptr;
×
927
            // Weg zu beiden Flaggen berechnen
928
            unsigned length1, length2;
929
            bool isF1Reachable = world.FindHumanPathOnRoads(*start, *roadSeg->GetF1(), &length1, nullptr, roadSeg)
×
930
                                 != RoadPathDirection::None;
×
UNCOV
931
            bool isF2Reachable = world.FindHumanPathOnRoads(*start, *roadSeg->GetF2(), &length2, nullptr, roadSeg)
×
UNCOV
932
                                 != RoadPathDirection::None;
×
933

934
            // Wenn man zu einer Flagge nich kommt, die jeweils andere nehmen
UNCOV
935
            if(!isF1Reachable)
×
UNCOV
936
                current_best_goal = (isF2Reachable) ? roadSeg->GetF2() : nullptr;
×
937
            else if(!isF2Reachable)
×
UNCOV
938
                current_best_goal = roadSeg->GetF1();
×
939
            else
940
            {
941
                // ansonsten die kürzeste von beiden
UNCOV
942
                current_best_goal = (length1 < length2) ? roadSeg->GetF1() : roadSeg->GetF2();
×
943
            }
944

945
            // Kein Weg führt hin, nächste Straße bitte
UNCOV
946
            if(!current_best_goal)
×
UNCOV
947
                continue;
×
948

949
            // Jeweiligen Weg bestimmen
UNCOV
950
            unsigned current_best_way = (roadSeg->GetF1() == current_best_goal) ? length1 : length2;
×
951

952
            // Produktivität ausrechnen, *10 die Produktivität + die Wegstrecke, damit die
953
            // auch noch mit einberechnet wird
UNCOV
954
            unsigned current_productivity = 10 * roadSeg->getCarrier(0)->GetProductivity() + current_best_way;
×
955

956
            // Besser als der bisher beste?
UNCOV
957
            if(current_productivity > best_productivity)
×
958
            {
959
                // Dann wird der vom Thron gestoßen
960
                best_productivity = current_productivity;
×
961
                best_road = roadSeg;
×
UNCOV
962
                *goal = current_best_goal;
×
963
            }
964
        }
965
    }
966

967
    return best_road;
8✔
968
}
969

970
struct ClientForWare
971
{
972
    noBaseBuilding* bld;
973
    unsigned estimate; // points minus half the optimal distance
974
    unsigned points;
975

UNCOV
976
    ClientForWare(noBaseBuilding* bld, unsigned estimate, unsigned points)
×
UNCOV
977
        : bld(bld), estimate(estimate), points(points)
×
UNCOV
978
    {}
×
979

UNCOV
980
    bool operator<(const ClientForWare& b) const
×
981
    {
982
        // use estimate, points and object id (as tie breaker) for sorting
UNCOV
983
        if(estimate != b.estimate)
×
UNCOV
984
            return estimate > b.estimate;
×
UNCOV
985
        else if(points != b.points)
×
UNCOV
986
            return points > b.points;
×
987
        else
UNCOV
988
            return bld->GetObjId() > b.bld->GetObjId();
×
989
    }
990
};
991

992
noBaseBuilding* GamePlayer::FindClientForWare(const Ware& ware)
12✔
993
{
994
    // Wenn es eine Goldmünze ist, wird das Ziel auf eine andere Art und Weise berechnet
995
    if(ware.type == GoodType::Coins)
12✔
996
        return FindClientForCoin(ware);
×
997

998
    // Warentyp herausfinden
999
    GoodType gt = ware.type;
12✔
1000
    // All food is considered fish in the distribution table
1001
    Distribution& wareDistribution =
1002
      (gt == GoodType::Bread || gt == GoodType::Meat) ? distribution[GoodType::Fish] : distribution[gt];
12✔
1003

1004
    std::vector<ClientForWare> possibleClients;
12✔
1005

1006
    const noRoadNode* start = ware.GetLocation();
12✔
1007

1008
    // Bretter und Steine können evtl. auch Häfen für Expeditionen gebrauchen
1009
    if(gt == GoodType::Stones || gt == GoodType::Boards)
12✔
1010
    {
1011
        for(nobHarborBuilding* harbor : buildings.GetHarbors())
8✔
1012
        {
UNCOV
1013
            unsigned points = harbor->CalcDistributionPoints(gt);
×
1014
            if(!points)
×
1015
                continue;
×
1016

UNCOV
1017
            points += 10 * 30; // Verteilung existiert nicht, Expeditionen haben allerdings hohe Priorität
×
1018
            unsigned distance = world.CalcDistance(start->GetPos(), harbor->GetPos()) / 2;
×
1019
            possibleClients.push_back(ClientForWare(harbor, points > distance ? points - distance : 0, points));
×
1020
        }
1021
    }
1022

1023
    for(const auto bldType : wareDistribution.client_buildings)
41✔
1024
    {
1025
        // BuildingType::Headquarters sind Baustellen!!, da HQs ja sowieso nicht gebaut werden können
1026
        if(bldType == BuildingType::Headquarters)
29✔
1027
        {
1028
            // Bei Baustellen die Extraliste abfragen
1029
            for(noBuildingSite* bldSite : buildings.GetBuildingSites())
8✔
1030
            {
1031
                unsigned points = bldSite->CalcDistributionPoints(gt);
×
UNCOV
1032
                if(!points)
×
1033
                    continue;
×
1034

1035
                points += wareDistribution.percent_buildings[BuildingType::Headquarters] * 30;
×
1036
                unsigned distance = world.CalcDistance(start->GetPos(), bldSite->GetPos()) / 2;
×
1037
                possibleClients.push_back(ClientForWare(bldSite, points > distance ? points - distance : 0, points));
×
1038
            }
1039
        } else
1040
        {
1041
            // Für übrige Gebäude
1042
            for(nobUsual* bld : buildings.GetBuildings(bldType))
21✔
1043
            {
UNCOV
1044
                unsigned points = bld->CalcDistributionPoints(gt);
×
UNCOV
1045
                if(!points)
×
UNCOV
1046
                    continue; // Ware not needed
×
1047

UNCOV
1048
                if(!wareDistribution.goals.empty())
×
1049
                {
UNCOV
1050
                    if(bld->GetBuildingType()
×
UNCOV
1051
                       == static_cast<BuildingType>(wareDistribution.goals[wareDistribution.selected_goal]))
×
UNCOV
1052
                        points += 300;
×
UNCOV
1053
                    else if(points >= 300) // avoid overflows (async!)
×
UNCOV
1054
                        points -= 300;
×
1055
                    else
UNCOV
1056
                        points = 0;
×
1057
                }
1058

UNCOV
1059
                unsigned distance = world.CalcDistance(start->GetPos(), bld->GetPos()) / 2;
×
1060
                possibleClients.push_back(ClientForWare(bld, points > distance ? points - distance : 0, points));
×
1061
            }
1062
        }
1063
    }
1064

1065
    // sort our clients, highest score first
1066
    std::sort(possibleClients.begin(), possibleClients.end());
12✔
1067

1068
    noBaseBuilding* lastBld = nullptr;
12✔
1069
    noBaseBuilding* bestBld = nullptr;
12✔
1070
    unsigned best_points = 0;
12✔
1071
    for(auto& possibleClient : possibleClients)
12✔
1072
    {
1073
        unsigned path_length;
1074

1075
        // If our estimate is worse (or equal) best_points, the real value cannot be better.
1076
        // As our list is sorted, further entries cannot be better either, so stop searching.
1077
        if(possibleClient.estimate <= best_points)
×
1078
            break;
×
1079

1080
        // get rid of double building entries. TODO: why are there double entries!?
UNCOV
1081
        if(possibleClient.bld == lastBld)
×
UNCOV
1082
            continue;
×
1083

UNCOV
1084
        lastBld = possibleClient.bld;
×
1085

1086
        // Just to be sure no underflow happens...
1087
        if(possibleClient.points < best_points + 1)
×
1088
            continue;
×
1089

1090
        // Find path ONLY if it may be better. Pathfinding is limited to the worst path score that would lead to a
1091
        // better score. This eliminates the worst case scenario where all nodes in a split road network would be hit by
1092
        // the pathfinding only to conclude that there is no possible path.
1093
        if(world.FindPathForWareOnRoads(*start, *possibleClient.bld, &path_length, nullptr,
×
1094
                                        (possibleClient.points - best_points) * 2 - 1)
×
UNCOV
1095
           != RoadPathDirection::None)
×
1096
        {
UNCOV
1097
            unsigned score = possibleClient.points - (path_length / 2);
×
1098

1099
            // As we have limited our pathfinding to take a maximum of (points - best_points) * 2 - 1 steps,
1100
            // path_length / 2 can at most be points - best_points - 1, so the score will be greater than best_points.
1101
            // :)
UNCOV
1102
            RTTR_Assert(score > best_points);
×
1103

UNCOV
1104
            best_points = score;
×
UNCOV
1105
            bestBld = possibleClient.bld;
×
1106
        }
1107
    }
1108

1109
    if(bestBld && !wareDistribution.goals.empty())
12✔
UNCOV
1110
        wareDistribution.selected_goal =
×
UNCOV
1111
          (wareDistribution.selected_goal + 907) % unsigned(wareDistribution.goals.size());
×
1112

1113
    // Wenn kein Abnehmer gefunden wurde, muss es halt in ein Lagerhaus
1114
    if(!bestBld)
12✔
1115
        bestBld = FindWarehouseForWare(ware);
12✔
1116

1117
    return bestBld;
12✔
1118
}
1119

1120
nobBaseWarehouse* GamePlayer::FindWarehouseForWare(const Ware& ware) const
16✔
1121
{
1122
    // Check whs that collect this ware
1123
    nobBaseWarehouse* wh = FindWarehouse(*ware.GetLocation(), FW::CollectsWare(ware.type), true, true);
16✔
1124
    // If there is none, check those that accept it
1125
    if(!wh)
16✔
1126
    {
1127
        // First find the ones, that do not send it right away (IMPORTANT: This avoids sending a ware to the wh that is
1128
        // sending the ware out)
1129
        wh = FindWarehouse(*ware.GetLocation(), FW::AcceptsWareButNoSend(ware.type), true, true);
16✔
1130
        // The others only if this fails
1131
        if(!wh)
16✔
1132
            wh = FindWarehouse(*ware.GetLocation(), FW::AcceptsWare(ware.type), true, true);
5✔
1133
    }
1134
    return wh;
16✔
1135
}
1136

UNCOV
1137
nobBaseMilitary* GamePlayer::FindClientForCoin(const Ware& ware) const
×
1138
{
UNCOV
1139
    nobBaseMilitary* bb = nullptr;
×
1140
    unsigned best_points = 0, points;
×
1141

1142
    // Militärgebäude durchgehen
1143
    for(nobMilitary* milBld : buildings.GetMilitaryBuildings())
×
1144
    {
1145
        unsigned way_points;
1146

UNCOV
1147
        points = milBld->CalcCoinsPoints();
×
1148
        // Wenn 0, will er gar keine Münzen (Goldzufuhr gestoppt)
UNCOV
1149
        if(points)
×
1150
        {
1151
            // Weg dorthin berechnen
UNCOV
1152
            if(world.FindPathForWareOnRoads(*ware.GetLocation(), *milBld, &way_points) != RoadPathDirection::None)
×
1153
            {
1154
                // Die Wegpunkte noch davon abziehen
UNCOV
1155
                points -= way_points;
×
1156
                // Besser als der bisher Beste?
UNCOV
1157
                if(points > best_points)
×
1158
                {
UNCOV
1159
                    best_points = points;
×
UNCOV
1160
                    bb = milBld;
×
1161
                }
1162
            }
1163
        }
1164
    }
1165

1166
    // Wenn kein Abnehmer gefunden wurde, muss es halt in ein Lagerhaus
UNCOV
1167
    if(!bb)
×
UNCOV
1168
        bb = FindWarehouseForWare(ware);
×
1169

UNCOV
1170
    return bb;
×
1171
}
1172

UNCOV
1173
unsigned GamePlayer::GetBuidingSitePriority(const noBuildingSite* building_site)
×
1174
{
1175
    if(useCustomBuildOrder_)
×
1176
    {
1177
        // Spezielle Reihenfolge
1178

1179
        // Typ in der Reihenfolge suchen und Position als Priorität zurückgeben
1180
        for(unsigned i = 0; i < build_order.size(); ++i)
×
1181
        {
UNCOV
1182
            if(building_site->GetBuildingType() == build_order[i])
×
UNCOV
1183
                return i;
×
1184
        }
1185
    } else
1186
    {
1187
        // Reihenfolge der Bauaufträge, also was zuerst in Auftrag gegeben wurde, wird zuerst gebaut
UNCOV
1188
        unsigned i = 0;
×
UNCOV
1189
        for(noBuildingSite* bldSite : buildings.GetBuildingSites())
×
1190
        {
UNCOV
1191
            if(building_site == bldSite)
×
UNCOV
1192
                return i;
×
UNCOV
1193
            i++;
×
1194
        }
1195
    }
1196

UNCOV
1197
    LOG.write("GameClientPlayer::GetBuidingSitePriority: ERROR: Buildingsite or type of it not found in the list!\n");
×
NEW
1198
    RTTR_Assert(false);
×
1199
    // We may want to multiply this value so don't return the absolute max value
1200
    return std::numeric_limits<unsigned>::max() / 1000;
1201
}
1202

1203
void GamePlayer::ConvertTransportData(const TransportOrders& transport_data)
2✔
1204
{
1205
    for(const auto ware : helpers::EnumRange<GoodType>{})
156✔
1206
        transportPrio[ware] = GetTransportPrioFromOrdering(transport_data, ware);
74✔
1207
}
2✔
1208

1209
bool GamePlayer::IsAlly(const unsigned char playerId) const
10,446✔
1210
{
1211
    // Der Spieler ist ja auch zu sich selber verbündet
1212
    if(GetPlayerId() == playerId)
10,446✔
1213
        return true;
2,639✔
1214
    else
1215
        return GetPactState(PactType::TreatyOfAlliance, playerId) == PactState::Accepted
7,807✔
1216
               || GetPactState(PactType::OneSidedAlliance, playerId) == PactState::Accepted;
7,807✔
1217
}
1218

1219
bool GamePlayer::CanAttack(const unsigned char otherPlayerId) const
854✔
1220
{
1221
    if(GetPactState(PactType::OneSidedAlliance, otherPlayerId) == PactState::Accepted)
854✔
1222
        return isHuman();
28✔
1223

1224
    // Verbündete dürfen nicht angegriffen werden
1225
    if(IsAlly(otherPlayerId))
826✔
1226
        return false;
40✔
1227
    else
1228
        // Ansonsten darf bei bestehendem Nichtangriffspakt ebenfalls nicht angegriffen werden
1229
        return GetPactState(PactType::NonAgressionPact, otherPlayerId) != PactState::Accepted;
786✔
1230
}
1231

1232
void GamePlayer::OnAttackedBy(unsigned char attackerId)
33✔
1233
{
1234
    BreakOneSidedAllianceTo(attackerId);
33✔
1235

1236
    // also break alliances for our allies
1237
    const auto thisPlayerId = GetPlayerId();
33✔
1238
    for(auto i = 0u; i < world.GetNumPlayers(); ++i)
123✔
1239
    {
1240
        // skip ourselves
1241
        if(i == thisPlayerId)
90✔
1242
            continue;
33✔
1243

1244
        // skip if we are not allied to them
1245
        if(!IsAlly(i))
57✔
1246
            continue;
54✔
1247

1248
        auto& player = world.GetPlayer(i);
3✔
1249

1250
        // skip if they are not allied to us
1251
        if(!player.IsAlly(thisPlayerId))
3✔
1252
            continue;
1✔
1253

1254
        player.BreakOneSidedAllianceTo(attackerId);
2✔
1255
    }
1256
}
33✔
1257

1258
void GamePlayer::OrderTroops(nobMilitary* goal, std::array<unsigned, NUM_SOLDIER_RANKS> counts,
33✔
1259
                             unsigned total_max) const
1260
{
1261
    // Solange Lagerhäuser nach Soldaten absuchen, bis entweder keins mehr übrig ist oder alle Soldaten bestellt sind
1262
    nobBaseWarehouse* wh;
1263
    unsigned sum = 0;
33✔
1264
    do
3✔
1265
    {
1266
        std::array<bool, NUM_SOLDIER_RANKS> desiredRanks;
1267
        for(unsigned i = 0; i < NUM_SOLDIER_RANKS; i++)
216✔
1268
            desiredRanks[i] = counts[i] > 0;
180✔
1269

1270
        wh = FindWarehouse(*goal, FW::HasAnyMatchingSoldier(desiredRanks), false, false);
36✔
1271
        if(wh)
36✔
1272
        {
1273
            wh->OrderTroops(goal, counts, total_max);
19✔
1274
            sum = std::accumulate(counts.begin(), counts.end(), 0u);
19✔
1275
        }
1276
    } while(total_max && sum && wh);
36✔
1277
}
33✔
1278

1279
void GamePlayer::RegulateAllTroops()
66✔
1280
{
1281
    for(nobMilitary* milBld : buildings.GetMilitaryBuildings())
69✔
1282
        milBld->RegulateTroops();
3✔
1283
}
66✔
1284

1285
/// Prüft von allen Militärgebäuden die Fahnen neu
1286
void GamePlayer::RecalcMilitaryFlags()
48✔
1287
{
1288
    for(nobMilitary* milBld : buildings.GetMilitaryBuildings())
72✔
1289
        milBld->LookForEnemyBuildings(nullptr);
24✔
1290
}
48✔
1291

1292
/// Sucht für Soldaten ein neues Militärgebäude, als Argument wird Referenz auf die
1293
/// entsprechende Soldatenanzahl im Lagerhaus verlangt
1294
void GamePlayer::NewSoldiersAvailable(const unsigned& soldier_count)
80✔
1295
{
1296
    RTTR_Assert(soldier_count > 0);
80✔
1297
    // solange laufen lassen, bis soldier_count = 0, d.h. der Soldat irgendwohin geschickt wurde
1298
    // Zuerst nach unbesetzten Militärgebäude schauen
1299
    for(nobMilitary* milBld : buildings.GetMilitaryBuildings())
97✔
1300
    {
1301
        if(milBld->IsNewBuilt())
17✔
1302
        {
UNCOV
1303
            milBld->RegulateTroops();
×
1304
            // Used that soldier? Go out
UNCOV
1305
            if(!soldier_count)
×
UNCOV
1306
                return;
×
1307
        }
1308
    }
1309

1310
    // Als nächstes Gebäude in Grenznähe
1311
    for(nobMilitary* milBld : buildings.GetMilitaryBuildings())
97✔
1312
    {
1313
        if(milBld->GetFrontierDistance() == FrontierDistance::Near)
17✔
1314
        {
1315
            milBld->RegulateTroops();
17✔
1316
            // Used that soldier? Go out
1317
            if(!soldier_count)
17✔
UNCOV
1318
                return;
×
1319
        }
1320
    }
1321

1322
    // Und den Rest ggf.
1323
    for(nobMilitary* milBld : buildings.GetMilitaryBuildings())
97✔
1324
    {
1325
        // already checked? -> skip
1326
        if(milBld->GetFrontierDistance() == FrontierDistance::Near || milBld->IsNewBuilt())
17✔
1327
            continue;
17✔
UNCOV
1328
        milBld->RegulateTroops();
×
UNCOV
1329
        if(!soldier_count) // used the soldier?
×
1330
            return;
×
1331
    }
1332
}
1333

1334
void GamePlayer::CallFlagWorker(const MapPoint pt, const Job job)
32✔
1335
{
1336
    auto* flag = world.GetSpecObj<noFlag>(pt);
32✔
1337
    if(!flag)
32✔
1338
        return;
2✔
1339
    /// Find wh with given job type (e.g. geologist, scout, ...)
1340
    nobBaseWarehouse* wh = FindWarehouse(*flag, FW::HasFigure(job, true), false, false);
30✔
1341

1342
    /// Wenns eins gibt, dann rufen
1343
    if(wh)
30✔
1344
        wh->OrderJob(job, flag, true);
26✔
1345
}
1346

UNCOV
1347
bool GamePlayer::IsFlagWorker(const nofFlagWorker* flagworker)
×
1348
{
UNCOV
1349
    return helpers::contains(flagworkers, flagworker);
×
1350
}
1351

1352
void GamePlayer::FlagDestroyed(noFlag* flag)
771✔
1353
{
1354
    // Alle durchgehen und ggf. sagen, dass sie keine Flagge mehr haben, wenn das ihre Flagge war, die zerstört wurde
1355
    for(auto it = flagworkers.begin(); it != flagworkers.end();)
771✔
1356
    {
UNCOV
1357
        if((*it)->GetFlag() == flag)
×
1358
        {
UNCOV
1359
            (*it)->LostWork();
×
UNCOV
1360
            it = flagworkers.erase(it);
×
1361
        } else
UNCOV
1362
            ++it;
×
1363
    }
1364
}
771✔
1365

1366
void GamePlayer::RefreshDefenderList()
54✔
1367
{
1368
    shouldSendDefenderList.clear();
54✔
1369
    // Add as many true values as set in the settings, the rest will be false
1370
    for(unsigned i = 0; i < MILITARY_SETTINGS_SCALE[2]; ++i)
324✔
1371
        shouldSendDefenderList.push_back(i < militarySettings_[2]);
270✔
1372
    // und ordentlich schütteln
1373
    RANDOM_SHUFFLE2(shouldSendDefenderList, 0);
54✔
1374
}
54✔
1375

1376
void GamePlayer::ChangeMilitarySettings(const MilitarySettings& military_settings)
54✔
1377
{
1378
    for(unsigned i = 0; i < military_settings.size(); ++i)
486✔
1379
    {
1380
        // Sicherstellen, dass im validen Bereich
1381
        RTTR_Assert(military_settings[i] <= MILITARY_SETTINGS_SCALE[i]);
432✔
1382
        this->militarySettings_[i] = military_settings[i];
432✔
1383
    }
1384
    /// Truppen müssen neu kalkuliert werden
1385
    RegulateAllTroops();
54✔
1386
    /// Die Verteidigungsliste muss erneuert werden
1387
    RefreshDefenderList();
54✔
1388
}
54✔
1389

1390
/// Setzt neue Werkzeugeinstellungen
1391
void GamePlayer::ChangeToolsSettings(const ToolSettings& tools_settings,
6✔
1392
                                     const helpers::EnumArray<int8_t, Tool>& orderChanges)
1393
{
1394
    const bool settingsChanged = toolsSettings_ != tools_settings;
6✔
1395
    toolsSettings_ = tools_settings;
6✔
1396
    if(settingsChanged)
6✔
1397
        world.GetNotifications().publish(ToolNote(ToolNote::SettingsChanged, GetPlayerId()));
4✔
1398

1399
    for(const auto tool : helpers::enumRange<Tool>())
168✔
1400
    {
1401
        tools_ordered[tool] = helpers::clamp(tools_ordered[tool] + orderChanges[tool], 0, 100);
72✔
1402
        tools_ordered_delta[tool] -= orderChanges[tool];
72✔
1403

1404
        if(orderChanges[tool] != 0)
72✔
1405
        {
1406
            LOG.write(">> Committing an order of %1% for tool #%2%(%3%)\n", LogTarget::File) % (int)orderChanges[tool]
8✔
1407
              % static_cast<unsigned>(tool) % _(WARE_NAMES[TOOL_TO_GOOD[tool]]);
8✔
1408
            world.GetNotifications().publish(ToolNote(ToolNote::OrderPlaced, GetPlayerId()));
4✔
1409
        }
1410
    }
1411
}
6✔
1412

1413
/// Setzt neue Verteilungseinstellungen
1414
void GamePlayer::ChangeDistribution(const Distributions& distribution_settings)
6✔
1415
{
1416
    unsigned idx = 0;
6✔
1417
    for(const DistributionMapping& mapping : distributionMap)
162✔
1418
    {
1419
        distribution[std::get<0>(mapping)].percent_buildings[std::get<1>(mapping)] = distribution_settings[idx++];
156✔
1420
    }
1421

1422
    RecalcDistribution();
6✔
1423
}
6✔
1424

1425
/// Setzt neue Baureihenfolge-Einstellungen
1426
void GamePlayer::ChangeBuildOrder(bool useCustomBuildOrder, const BuildOrders& order_data)
2✔
1427
{
1428
    this->useCustomBuildOrder_ = useCustomBuildOrder;
2✔
1429
    this->build_order = order_data;
2✔
1430
}
2✔
1431

1432
bool GamePlayer::ShouldSendDefender()
18✔
1433
{
1434
    // Wenn wir schon am Ende sind, muss die Verteidgungsliste erneuert werden
1435
    if(shouldSendDefenderList.empty())
18✔
UNCOV
1436
        RefreshDefenderList();
×
1437

1438
    bool result = shouldSendDefenderList.back();
18✔
1439
    shouldSendDefenderList.pop_back();
18✔
1440
    return result;
18✔
1441
}
1442

1443
void GamePlayer::TestDefeat()
78✔
1444
{
1445
    // Nicht schon besiegt?
1446
    // Keine Militärgebäude, keine Lagerhäuser (HQ,Häfen) -> kein Land --> verloren
1447
    if(!isDefeated && buildings.GetMilitaryBuildings().empty() && buildings.GetStorehouses().empty())
78✔
1448
        Surrender();
29✔
1449
}
78✔
1450

1451
nobHQ* GamePlayer::GetHQ() const
14✔
1452
{
1453
    const MapPoint& hqPos = GetHQPos();
14✔
1454
    return const_cast<nobHQ*>(hqPos.isValid() ? GetGameWorld().GetSpecObj<nobHQ>(hqPos) : nullptr);
14✔
1455
}
1456

1457
void GamePlayer::Surrender()
32✔
1458
{
1459
    if(isDefeated)
32✔
1460
        return;
1✔
1461

1462
    isDefeated = true;
31✔
1463

1464
    // GUI Bescheid sagen
1465
    if(world.GetGameInterface())
31✔
UNCOV
1466
        world.GetGameInterface()->GI_PlayerDefeated(GetPlayerId());
×
1467
}
1468

1469
void GamePlayer::SetStatisticValue(StatisticType type, unsigned value)
×
1470
{
1471
    statisticCurrentData[type] = value;
×
1472
}
×
1473

1474
void GamePlayer::ChangeStatisticValue(StatisticType type, int change)
2,002✔
1475
{
1476
    RTTR_Assert(change >= 0 || statisticCurrentData[type] >= static_cast<unsigned>(-change));
2,002✔
1477
    statisticCurrentData[type] += change;
2,002✔
1478
}
2,002✔
1479

1480
void GamePlayer::IncreaseMerchandiseStatistic(GoodType type)
4✔
1481
{
1482
    // Einsortieren...
1483
    switch(type)
4✔
1484
    {
UNCOV
1485
        case GoodType::Wood: statisticCurrentMerchandiseData[0]++; break;
×
UNCOV
1486
        case GoodType::Boards: statisticCurrentMerchandiseData[1]++; break;
×
1487
        case GoodType::Stones: statisticCurrentMerchandiseData[2]++; break;
×
UNCOV
1488
        case GoodType::Fish:
×
1489
        case GoodType::Bread:
UNCOV
1490
        case GoodType::Meat: statisticCurrentMerchandiseData[3]++; break;
×
1491
        case GoodType::Water: statisticCurrentMerchandiseData[4]++; break;
×
1492
        case GoodType::Beer: statisticCurrentMerchandiseData[5]++; break;
×
UNCOV
1493
        case GoodType::Coal: statisticCurrentMerchandiseData[6]++; break;
×
UNCOV
1494
        case GoodType::IronOre: statisticCurrentMerchandiseData[7]++; break;
×
UNCOV
1495
        case GoodType::Gold: statisticCurrentMerchandiseData[8]++; break;
×
UNCOV
1496
        case GoodType::Iron: statisticCurrentMerchandiseData[9]++; break;
×
UNCOV
1497
        case GoodType::Coins: statisticCurrentMerchandiseData[10]++; break;
×
1498
        case GoodType::Tongs:
3✔
1499
        case GoodType::Axe:
1500
        case GoodType::Saw:
1501
        case GoodType::PickAxe:
1502
        case GoodType::Hammer:
1503
        case GoodType::Shovel:
1504
        case GoodType::Crucible:
1505
        case GoodType::RodAndLine:
1506
        case GoodType::Scythe:
1507
        case GoodType::Cleaver:
1508
        case GoodType::Rollingpin:
1509
        case GoodType::Bow: statisticCurrentMerchandiseData[11]++; break;
3✔
UNCOV
1510
        case GoodType::ShieldVikings:
×
1511
        case GoodType::ShieldAfricans:
1512
        case GoodType::ShieldRomans:
1513
        case GoodType::ShieldJapanese:
UNCOV
1514
        case GoodType::Sword: statisticCurrentMerchandiseData[12]++; break;
×
UNCOV
1515
        case GoodType::Boat: statisticCurrentMerchandiseData[13]++; break;
×
1516
        default: break;
1✔
1517
    }
1518
}
4✔
1519

1520
/// Calculates current statistics
1521
void GamePlayer::CalcStatistics()
6✔
1522
{
1523
    // Waren aus der Inventur zählen
1524
    statisticCurrentData[StatisticType::Merchandise] = 0;
6✔
1525
    for(const auto i : helpers::enumRange<GoodType>())
468✔
1526
        statisticCurrentData[StatisticType::Merchandise] += global_inventory[i];
222✔
1527

1528
    // Bevölkerung aus der Inventur zählen
1529
    statisticCurrentData[StatisticType::Inhabitants] = 0;
6✔
1530
    for(const auto i : helpers::enumRange<Job>())
444✔
1531
        statisticCurrentData[StatisticType::Inhabitants] += global_inventory[i];
210✔
1532

1533
    // Militär aus der Inventur zählen
1534
    statisticCurrentData[StatisticType::Military] =
12✔
1535
      global_inventory.people[Job::Private] + global_inventory.people[Job::PrivateFirstClass] * 2
6✔
1536
      + global_inventory.people[Job::Sergeant] * 3 + global_inventory.people[Job::Officer] * 4
6✔
1537
      + global_inventory.people[Job::General] * 5;
6✔
1538

1539
    // Produktivität berechnen
1540
    statisticCurrentData[StatisticType::Productivity] = buildings.CalcAverageProductivity();
6✔
1541

1542
    // Total points for tournament games
1543
    statisticCurrentData[StatisticType::Tournament] =
12✔
1544
      statisticCurrentData[StatisticType::Military] + 3 * statisticCurrentData[StatisticType::Vanquished];
6✔
1545
}
6✔
1546

1547
void GamePlayer::StatisticStep()
6✔
1548
{
1549
    CalcStatistics();
6✔
1550

1551
    // 15-min-Statistik ein Feld weiterschieben
1552
    for(const auto i : helpers::enumRange<StatisticType>())
132✔
1553
    {
1554
        statistic[StatisticTime::T15Minutes].data[i][incrStatIndex(statistic[StatisticTime::T15Minutes].currentIndex)] =
54✔
1555
          statisticCurrentData[i];
54✔
1556
    }
1557
    for(unsigned i = 0; i < NUM_STAT_MERCHANDISE_TYPES; ++i)
90✔
1558
    {
1559
        statistic[StatisticTime::T15Minutes]
84✔
1560
          .merchandiseData[i][incrStatIndex(statistic[StatisticTime::T15Minutes].currentIndex)] =
168✔
1561
          statisticCurrentMerchandiseData[i];
84✔
1562
    }
1563
    statistic[StatisticTime::T15Minutes].currentIndex =
6✔
1564
      incrStatIndex(statistic[StatisticTime::T15Minutes].currentIndex);
6✔
1565

1566
    statistic[StatisticTime::T15Minutes].counter++;
6✔
1567

1568
    // Prüfen ob 4mal 15-min-Statistik weitergeschoben wurde, wenn ja: 1-h-Statistik weiterschieben
1569
    // und aktuellen Wert der 15min-Statistik benutzen
1570
    // gleiches für die 4h und 16h Statistik
1571
    for(const auto t : helpers::enumRange<StatisticTime>())
60✔
1572
    {
1573
        if(t == StatisticTime(helpers::MaxEnumValue_v<StatisticTime>))
24✔
1574
            break;
6✔
1575
        const auto nextT = StatisticTime(rttr::enum_cast(t) + 1);
18✔
1576
        if(statistic[t].counter == 4)
18✔
1577
        {
UNCOV
1578
            statistic[t].counter = 0;
×
UNCOV
1579
            for(const auto i : helpers::enumRange<StatisticType>())
×
1580
            {
UNCOV
1581
                statistic[nextT].data[i][incrStatIndex(statistic[nextT].currentIndex)] = statisticCurrentData[i];
×
1582
            }
1583

1584
            // Summe für den Zeitraum berechnen (immer 4 Zeitschritte der jeweils kleineren Statistik)
UNCOV
1585
            for(unsigned i = 0; i < NUM_STAT_MERCHANDISE_TYPES; ++i)
×
1586
            {
UNCOV
1587
                statistic[nextT].merchandiseData[i][incrStatIndex(statistic[nextT].currentIndex)] =
×
UNCOV
1588
                  statisticCurrentMerchandiseData[i]
×
UNCOV
1589
                  + statistic[t].merchandiseData[i][decrStatIndex(statistic[t].currentIndex, 1)]
×
UNCOV
1590
                  + statistic[t].merchandiseData[i][decrStatIndex(statistic[t].currentIndex, 2)]
×
UNCOV
1591
                  + statistic[t].merchandiseData[i][decrStatIndex(statistic[t].currentIndex, 3)];
×
1592
            }
1593

UNCOV
1594
            statistic[nextT].currentIndex = incrStatIndex(statistic[nextT].currentIndex);
×
UNCOV
1595
            statistic[nextT].counter++;
×
1596
        }
1597
    }
1598

1599
    // Warenstatistikzähler nullen
1600
    statisticCurrentMerchandiseData.fill(0);
6✔
1601
}
6✔
1602

1603
GamePlayer::Pact::Pact(SerializedGameData& sgd)
168✔
1604
    : duration(sgd.PopUnsignedInt()), start(sgd.PopUnsignedInt()), accepted(sgd.PopBool()), want_cancel(sgd.PopBool())
168✔
1605
{}
168✔
1606

1607
void GamePlayer::Pact::Serialize(SerializedGameData& sgd) const
336✔
1608
{
1609
    sgd.PushUnsignedInt(duration);
336✔
1610
    sgd.PushUnsignedInt(start);
336✔
1611
    sgd.PushBool(accepted);
336✔
1612
    sgd.PushBool(want_cancel);
336✔
1613
}
336✔
1614

1615
void GamePlayer::PactChanged(const PactType pt)
24✔
1616
{
1617
    // Recheck military flags as the border (to an enemy) might have changed
1618
    RecalcMilitaryFlags();
24✔
1619

1620
    // Ggf. den GUI Bescheid sagen, um Sichtbarkeiten etc. neu zu berechnen
1621
    if(pt == PactType::TreatyOfAlliance)
24✔
1622
    {
1623
        if(world.GetGameInterface())
6✔
UNCOV
1624
            world.GetGameInterface()->GI_TreatyOfAllianceChanged(GetPlayerId());
×
1625
    }
1626
}
24✔
1627

1628
void GamePlayer::SuggestPact(const unsigned char targetPlayerId, const PactType pt, const unsigned duration)
10✔
1629
{
1630
    // Don't try to make pact with self
1631
    if(targetPlayerId == GetPlayerId())
10✔
1632
        return;
1✔
1633

1634
    if(!pacts[targetPlayerId][pt].accepted && duration > 0)
9✔
1635
    {
1636
        pacts[targetPlayerId][pt].duration = duration;
8✔
1637
        pacts[targetPlayerId][pt].start = world.GetEvMgr().GetCurrentGF();
8✔
1638
        GamePlayer targetPlayer = world.GetPlayer(targetPlayerId);
16✔
1639
        if(targetPlayer.isHuman())
8✔
1640
            targetPlayer.SendPostMessage(std::make_unique<DiplomacyPostQuestion>(
6✔
1641
              world.GetEvMgr().GetCurrentGF(), pt, pacts[targetPlayerId][pt].start, *this, duration));
12✔
1642
        else if(world.HasLua())
2✔
1643
            world.GetLua().EventSuggestPact(pt, GetPlayerId(), targetPlayerId, duration);
2✔
1644
    }
1645
}
1646

1647
void GamePlayer::AcceptPact(const unsigned id, const PactType pt, const unsigned char targetPlayer)
21✔
1648
{
1649
    if(!pacts[targetPlayer][pt].accepted && pacts[targetPlayer][pt].duration > 0 && pacts[targetPlayer][pt].start == id)
21✔
1650
    {
1651
        MakePact(pt, targetPlayer, pacts[targetPlayer][pt].duration);
8✔
1652
        world.GetPlayer(targetPlayer).MakePact(pt, GetPlayerId(), pacts[targetPlayer][pt].duration);
8✔
1653
        PactChanged(pt);
8✔
1654
        world.GetPlayer(targetPlayer).PactChanged(pt);
8✔
1655
        if(world.HasLua())
8✔
1656
            world.GetLua().EventPactCreated(pt, GetPlayerId(), targetPlayer, pacts[targetPlayer][pt].duration);
3✔
1657
    }
1658
}
21✔
1659

1660
/// Bündnis (real, d.h. spielentscheidend) abschließen
1661
void GamePlayer::MakePact(const PactType pt, const unsigned char other_player, const unsigned duration)
16✔
1662
{
1663
    pacts[other_player][pt].accepted = true;
16✔
1664
    pacts[other_player][pt].start = world.GetEvMgr().GetCurrentGF();
16✔
1665
    pacts[other_player][pt].duration = duration;
16✔
1666
    pacts[other_player][pt].want_cancel = false;
16✔
1667

1668
    SendPostMessage(
16✔
1669
      std::make_unique<PostMsg>(world.GetEvMgr().GetCurrentGF(), pt, world.GetPlayer(other_player), true));
32✔
1670
}
16✔
1671

1672
/// Zeigt an, ob ein Pakt besteht
1673
PactState GamePlayer::GetPactState(const PactType pt, const unsigned char other_player) const
12,874✔
1674
{
1675
    // Prüfen, ob Bündnis in Kraft ist
1676
    if(pacts[other_player][pt].duration)
12,874✔
1677
    {
1678
        if(!pacts[other_player][pt].accepted)
4,724✔
1679
            return PactState::InProgress;
16✔
1680

1681
        if(pacts[other_player][pt].duration == DURATION_INFINITE
4,708✔
1682
           || world.GetEvMgr().GetCurrentGF() < pacts[other_player][pt].start + pacts[other_player][pt].duration)
4,708✔
1683
            return PactState::Accepted;
4,706✔
1684
    }
1685

1686
    return PactState::None;
8,152✔
1687
}
1688

1689
/// all allied players get a letter with the location
1690
void GamePlayer::NotifyAlliesOfLocation(const MapPoint pt)
6✔
1691
{
1692
    for(unsigned i = 0; i < world.GetNumPlayers(); ++i)
24✔
1693
    {
1694
        if(i != GetPlayerId() && IsAlly(i))
18✔
1695
            world.GetPlayer(i).SendPostMessage(std::make_unique<PostMsg>(
10✔
1696
              world.GetEvMgr().GetCurrentGF(), _("Your ally wishes to notify you of this location"),
5✔
1697
              PostCategory::Diplomacy, pt));
10✔
1698
    }
1699
}
6✔
1700

1701
/// Gibt die verbleibende Dauer zurück, die ein Bündnis noch laufen wird (DURATION_INFINITE = für immer)
1702
unsigned GamePlayer::GetRemainingPactTime(const PactType pt, const unsigned char other_player) const
134✔
1703
{
1704
    if(pacts[other_player][pt].duration)
134✔
1705
    {
1706
        if(pacts[other_player][pt].accepted)
88✔
1707
        {
1708
            if(pacts[other_player][pt].duration == DURATION_INFINITE)
76✔
UNCOV
1709
                return DURATION_INFINITE;
×
1710
            else if(world.GetEvMgr().GetCurrentGF() <= pacts[other_player][pt].start + pacts[other_player][pt].duration)
76✔
1711
                return ((pacts[other_player][pt].start + pacts[other_player][pt].duration)
76✔
1712
                        - world.GetEvMgr().GetCurrentGF());
76✔
1713
        }
1714
    }
1715

1716
    return 0;
58✔
1717
}
1718

1719
/// Gibt Einverständnis, dass dieser Spieler den Pakt auflösen will
1720
/// Falls dieser Spieler einen Bündnisvorschlag gemacht hat, wird dieser dagegen zurückgenommen
1721
void GamePlayer::CancelPact(const PactType pt, const unsigned char otherPlayerIdx)
8✔
1722
{
1723
    // Don't try to cancel pact with self
1724
    if(otherPlayerIdx == GetPlayerId())
8✔
1725
        return;
2✔
1726

1727
    // Besteht bereits ein Bündnis?
1728
    if(pacts[otherPlayerIdx][pt].accepted)
6✔
1729
    {
1730
        // Vermerken, dass der Spieler das Bündnis auflösen will
1731
        pacts[otherPlayerIdx][pt].want_cancel = true;
6✔
1732

1733
        // Will der andere Spieler das Bündnis auch auflösen?
1734
        GamePlayer& otherPlayer = world.GetPlayer(otherPlayerIdx);
6✔
1735
        if(otherPlayer.pacts[GetPlayerId()][pt].want_cancel)
6✔
1736
        {
1737
            // Dann wird das Bündnis aufgelöst
1738
            pacts[otherPlayerIdx][pt].accepted = false;
2✔
1739
            pacts[otherPlayerIdx][pt].duration = 0;
2✔
1740
            pacts[otherPlayerIdx][pt].want_cancel = false;
2✔
1741

1742
            otherPlayer.pacts[GetPlayerId()][pt].accepted = false;
2✔
1743
            otherPlayer.pacts[GetPlayerId()][pt].duration = 0;
2✔
1744
            otherPlayer.pacts[GetPlayerId()][pt].want_cancel = false;
2✔
1745

1746
            // Den Spielern eine Informationsnachricht schicken
1747
            world.GetPlayer(otherPlayerIdx)
2✔
1748
              .SendPostMessage(std::make_unique<PostMsg>(world.GetEvMgr().GetCurrentGF(), pt, *this, false));
2✔
1749
            SendPostMessage(
2✔
1750
              std::make_unique<PostMsg>(world.GetEvMgr().GetCurrentGF(), pt, world.GetPlayer(otherPlayerIdx), false));
4✔
1751
            PactChanged(pt);
2✔
1752
            otherPlayer.PactChanged(pt);
2✔
1753
            if(world.HasLua())
2✔
1754
                world.GetLua().EventPactCanceled(pt, GetPlayerId(), otherPlayerIdx);
1✔
1755
        } else
1756
        {
1757
            // Ansonsten den anderen Spieler fragen, ob der das auch so sieht
1758
            if(otherPlayer.isHuman())
4✔
1759
                otherPlayer.SendPostMessage(std::make_unique<DiplomacyPostQuestion>(
3✔
1760
                  world.GetEvMgr().GetCurrentGF(), pt, pacts[otherPlayerIdx][pt].start, *this));
6✔
1761
            else if(!world.HasLua() || world.GetLua().EventCancelPactRequest(pt, GetPlayerId(), otherPlayerIdx))
1✔
1762
            {
1763
                // AI accepts cancels, if there is no lua-interace
1764
                pacts[otherPlayerIdx][pt].accepted = false;
1✔
1765
                pacts[otherPlayerIdx][pt].duration = 0;
1✔
1766
                pacts[otherPlayerIdx][pt].want_cancel = false;
1✔
1767

1768
                otherPlayer.pacts[GetPlayerId()][pt].accepted = false;
1✔
1769
                otherPlayer.pacts[GetPlayerId()][pt].duration = 0;
1✔
1770
                otherPlayer.pacts[GetPlayerId()][pt].want_cancel = false;
1✔
1771

1772
                if(world.HasLua())
1✔
1773
                    world.GetLua().EventPactCanceled(pt, GetPlayerId(), otherPlayerIdx);
1✔
1774
            }
1775
        }
1776
    } else
1777
    {
1778
        // Es besteht kein Bündnis, also unseren Bündnisvorschlag wieder zurücknehmen
UNCOV
1779
        pacts[otherPlayerIdx][pt].duration = 0;
×
1780
    }
1781
}
1782

1783
void GamePlayer::MakeStartPacts()
57✔
1784
{
1785
    // Reset pacts
1786
    for(unsigned i = 0; i < world.GetNumPlayers(); ++i)
226✔
1787
    {
1788
        for(const auto z : helpers::enumRange<PactType>())
1,690✔
1789
            pacts[i][z] = Pact();
507✔
1790
    }
1791

1792
    // No team -> No pacts
1793
    if(team == Team::None)
57✔
1794
        return;
4✔
1795
    RTTR_Assert(isTeam(team));
53✔
1796

1797
    // Create ally- and non-aggression-pact for all players of same team
1798
    for(unsigned i = 0; i < world.GetNumPlayers(); ++i)
209✔
1799
    {
1800
        if(team != world.GetPlayer(i).team)
156✔
1801
            continue;
63✔
1802
        for(const auto z : helpers::enumRange<PactType>())
930✔
1803
        {
1804
            if(z == PactType::OneSidedAlliance)
279✔
1805
                continue;
93✔
1806

1807
            pacts[i][z].duration = DURATION_INFINITE;
186✔
1808
            pacts[i][z].start = 0;
186✔
1809
            pacts[i][z].accepted = true;
186✔
1810
            pacts[i][z].want_cancel = false;
186✔
1811
        }
1812
    }
1813
}
1814

1815
void GamePlayer::MakeOneSidedAllianceTo(unsigned char otherPlayerId)
21✔
1816
{
1817
    auto& pact = pacts[otherPlayerId][PactType::OneSidedAlliance];
21✔
1818
    pact.duration = DURATION_INFINITE;
21✔
1819
    pact.accepted = true;
21✔
1820
}
21✔
1821

1822
void GamePlayer::BreakOneSidedAllianceTo(unsigned char otherPlayerId)
35✔
1823
{
1824
    pacts[otherPlayerId][PactType::OneSidedAlliance].duration = 0;
35✔
1825
    // you can make a one-sided alliance but breaking one side of it breaks the other as well
1826
    world.GetPlayer(otherPlayerId).pacts[GetPlayerId()][PactType::OneSidedAlliance].duration = 0;
35✔
1827
}
35✔
1828

1829
bool GamePlayer::IsWareRegistred(const Ware& ware)
128✔
1830
{
1831
    return helpers::contains(ware_list, &ware);
128✔
1832
}
1833

1834
bool GamePlayer::IsWareDependent(const Ware& ware)
14✔
1835
{
1836
    for(nobBaseWarehouse* wh : buildings.GetStorehouses())
41✔
1837
    {
1838
        if(wh->IsWareDependent(ware))
27✔
UNCOV
1839
            return true;
×
1840
    }
1841

1842
    return false;
14✔
1843
}
1844

1845
void GamePlayer::IncreaseInventoryWare(const GoodType ware, const unsigned count)
14,984✔
1846
{
1847
    global_inventory.Add(ConvertShields(ware), count);
14,984✔
1848
}
14,984✔
1849

1850
void GamePlayer::DecreaseInventoryWare(const GoodType ware, const unsigned count)
2,279✔
1851
{
1852
    global_inventory.Remove(ConvertShields(ware), count);
2,279✔
1853
}
2,279✔
1854

1855
/// Registriert ein Schiff beim Einwohnermeldeamt
1856
void GamePlayer::RegisterShip(noShip& ship)
21✔
1857
{
1858
    ships.push_back(&ship);
21✔
1859
    // Evtl bekommt das Schiffchen gleich was zu tun?
1860
    GetJobForShip(ship);
21✔
1861
}
21✔
1862

1863
struct ShipForHarbor
1864
{
1865
    noShip* ship;
1866
    uint32_t estimate;
1867

1868
    ShipForHarbor(noShip* ship, uint32_t estimate) : ship(ship), estimate(estimate) {}
18✔
1869

1870
    bool operator<(const ShipForHarbor& b) const
×
1871
    {
UNCOV
1872
        return (estimate < b.estimate) || (estimate == b.estimate && ship->GetObjId() < b.ship->GetObjId());
×
1873
    }
1874
};
1875

1876
/// Schiff für Hafen bestellen
1877
bool GamePlayer::OrderShip(nobHarborBuilding& hb)
32✔
1878
{
1879
    std::vector<ShipForHarbor> sfh;
64✔
1880

1881
    // we need more ships than those that are already on their way? limit search to idle ships
1882
    if(GetShipsToHarbor(hb) < hb.GetNumNeededShips())
32✔
1883
    {
1884
        for(noShip* ship : ships)
64✔
1885
        {
1886
            if(ship->IsIdling() && world.IsHarborAtSea(world.GetHarborPointID(hb.GetPos()), ship->GetSeaID()))
32✔
1887
                sfh.push_back(ShipForHarbor(ship, world.CalcDistance(hb.GetPos(), ship->GetPos())));
18✔
1888
        }
1889
    } else
1890
    {
UNCOV
1891
        for(noShip* ship : ships)
×
1892
        {
UNCOV
1893
            if((ship->IsIdling() && world.IsHarborAtSea(world.GetHarborPointID(hb.GetPos()), ship->GetSeaID()))
×
UNCOV
1894
               || ship->IsGoingToHarbor(hb))
×
1895
            {
UNCOV
1896
                sfh.push_back(ShipForHarbor(ship, world.CalcDistance(hb.GetPos(), ship->GetPos())));
×
1897
            }
1898
        }
1899
    }
1900

1901
    std::sort(sfh.begin(), sfh.end());
32✔
1902

1903
    noShip* best_ship = nullptr;
32✔
1904
    uint32_t best_distance = std::numeric_limits<uint32_t>::max();
32✔
1905
    std::vector<Direction> best_route;
64✔
1906

1907
    for(auto& it : sfh)
41✔
1908
    {
1909
        uint32_t distance;
1910
        std::vector<Direction> route;
18✔
1911

1912
        // the estimate (air-line distance) for this and all other ships in the list is already worse than what we
1913
        // found? disregard the rest
1914
        if(it.estimate >= best_distance)
18✔
UNCOV
1915
            break;
×
1916

1917
        noShip& ship = *it.ship;
18✔
1918

1919
        MapPoint dest = world.GetCoastalPoint(hb.GetHarborPosID(), ship.GetSeaID());
18✔
1920

1921
        // ship already there?
1922
        if(ship.GetPos() == dest)
18✔
1923
        {
1924
            hb.ShipArrived(ship);
9✔
1925
            return (true);
9✔
1926
        }
1927

1928
        if(world.FindShipPathToHarbor(ship.GetPos(), hb.GetHarborPosID(), ship.GetSeaID(), &route, &distance))
9✔
1929
        {
1930
            if(distance < best_distance)
9✔
1931
            {
1932
                best_ship = &ship;
9✔
1933
                best_distance = distance;
9✔
1934
                best_route = route;
9✔
1935
            }
1936
        }
1937
    }
1938

1939
    // only order ships not already on their way
1940
    if(best_ship && best_ship->IsIdling())
23✔
1941
    {
1942
        best_ship->GoToHarbor(hb, best_route);
9✔
1943

1944
        return (true);
9✔
1945
    }
1946

1947
    return (false);
14✔
1948
}
1949

1950
/// Meldet das Schiff wieder ab
UNCOV
1951
void GamePlayer::RemoveShip(noShip* ship)
×
1952
{
UNCOV
1953
    for(unsigned i = 0; i < ships.size(); ++i)
×
1954
    {
UNCOV
1955
        if(ships[i] == ship)
×
1956
        {
UNCOV
1957
            ships.erase(ships.begin() + i);
×
UNCOV
1958
            return;
×
1959
        }
1960
    }
1961
}
1962

1963
/// Versucht, für ein untätiges Schiff eine Arbeit zu suchen
1964
void GamePlayer::GetJobForShip(noShip& ship)
31✔
1965
{
1966
    // Evtl. steht irgendwo eine Expedition an und das Schiff kann diese übernehmen
1967
    nobHarborBuilding* best = nullptr;
31✔
1968
    int best_points = 0;
31✔
1969
    std::vector<Direction> best_route;
31✔
1970

1971
    // Beste Weglänge, die ein Schiff zurücklegen muss, welches gerade nichts zu tun hat
1972
    for(nobHarborBuilding* harbor : buildings.GetHarbors())
64✔
1973
    {
1974
        // Braucht der Hafen noch Schiffe?
1975
        if(harbor->GetNumNeededShips() == 0)
33✔
1976
            continue;
33✔
1977

1978
        // Anzahl der Schiffe ermitteln, die diesen Hafen bereits anfahren
UNCOV
1979
        unsigned ships_coming = GetShipsToHarbor(*harbor);
×
1980

1981
        // Evtl. kommen schon genug?
UNCOV
1982
        if(harbor->GetNumNeededShips() <= ships_coming)
×
1983
            continue;
×
1984

1985
        // liegen wir am gleichen Meer?
UNCOV
1986
        if(world.IsHarborAtSea(harbor->GetHarborPosID(), ship.GetSeaID()))
×
1987
        {
UNCOV
1988
            const MapPoint coastPt = world.GetCoastalPoint(harbor->GetHarborPosID(), ship.GetSeaID());
×
1989

1990
            // Evtl. sind wir schon da?
UNCOV
1991
            if(ship.GetPos() == coastPt)
×
1992
            {
UNCOV
1993
                harbor->ShipArrived(ship);
×
1994
                return;
×
1995
            }
1996

1997
            unsigned length;
UNCOV
1998
            std::vector<Direction> route;
×
1999

UNCOV
2000
            if(world.FindShipPathToHarbor(ship.GetPos(), harbor->GetHarborPosID(), ship.GetSeaID(), &route, &length))
×
2001
            {
2002
                // Punkte ausrechnen
UNCOV
2003
                int points = harbor->GetNeedForShip(ships_coming) - length;
×
2004
                if(points > best_points || !best)
×
2005
                {
UNCOV
2006
                    best = harbor;
×
UNCOV
2007
                    best_points = points;
×
UNCOV
2008
                    best_route = route;
×
2009
                }
2010
            }
2011
        }
2012
    }
2013

2014
    // Einen Hafen gefunden?
2015
    if(best)
31✔
2016
        // Dann bekommt das gleich der Hafen
UNCOV
2017
        ship.GoToHarbor(*best, best_route);
×
2018
}
2019

2020
/// Gibt die ID eines Schiffes zurück
2021
unsigned GamePlayer::GetShipID(const noShip* const ship) const
13✔
2022
{
2023
    for(unsigned i = 0; i < ships.size(); ++i)
13✔
2024
        if(ships[i] == ship)
13✔
2025
            return i;
13✔
2026

UNCOV
2027
    return 0xFFFFFFFF;
×
2028
}
2029

2030
/// Gibt ein Schiff anhand der ID zurück bzw. nullptr, wenn keines mit der ID existiert
2031
noShip* GamePlayer::GetShipByID(const unsigned ship_id) const
20✔
2032
{
2033
    if(ship_id >= ships.size())
20✔
UNCOV
2034
        return nullptr;
×
2035
    else
2036
        return ships[ship_id];
20✔
2037
}
2038

2039
/// Gibt eine Liste mit allen Häfen dieses Spieler zurück, die an ein bestimmtes Meer angrenzen
2040
void GamePlayer::GetHarborsAtSea(std::vector<nobHarborBuilding*>& harbor_buildings, const unsigned short seaId) const
198✔
2041
{
2042
    for(nobHarborBuilding* harbor : buildings.GetHarbors())
642✔
2043
    {
2044
        if(helpers::contains(harbor_buildings, harbor))
444✔
2045
            continue;
216✔
2046

2047
        if(world.IsHarborAtSea(harbor->GetHarborPosID(), seaId))
228✔
2048
            harbor_buildings.push_back(harbor);
228✔
2049
    }
2050
}
198✔
2051

2052
/// Gibt die Anzahl der Schiffe, die einen bestimmten Hafen ansteuern, zurück
2053
unsigned GamePlayer::GetShipsToHarbor(const nobHarborBuilding& hb) const
71✔
2054
{
2055
    unsigned count = 0;
71✔
2056
    for(const auto* ship : ships)
142✔
2057
    {
2058
        if(ship->IsGoingToHarbor(hb))
71✔
2059
            ++count;
5✔
2060
    }
2061

2062
    return count;
71✔
2063
}
2064

2065
/// Sucht einen Hafen in der Nähe, wo dieses Schiff seine Waren abladen kann
2066
/// gibt true zurück, falls erfolgreich
2067
bool GamePlayer::FindHarborForUnloading(noShip* ship, const MapPoint start, unsigned* goal_harborId,
5✔
2068
                                        std::vector<Direction>* route, nobHarborBuilding* exception)
2069
{
2070
    nobHarborBuilding* best = nullptr;
5✔
2071
    unsigned best_distance = 0xffffffff;
5✔
2072

2073
    for(nobHarborBuilding* hb : buildings.GetHarbors())
8✔
2074
    {
2075
        // Bestimmten Hafen ausschließen
2076
        if(hb == exception)
3✔
UNCOV
2077
            continue;
×
2078

2079
        // Prüfen, ob Hafen an das Meer, wo sich das Schiff gerade befindet, angrenzt
2080
        if(!world.IsHarborAtSea(hb->GetHarborPosID(), ship->GetSeaID()))
3✔
UNCOV
2081
            continue;
×
2082

2083
        // Distanz ermitteln zwischen Schiff und Hafen, Schiff kann natürlich auch über Kartenränder fahren
2084
        unsigned distance = world.CalcDistance(ship->GetPos(), hb->GetPos());
3✔
2085

2086
        // Kürzerer Weg als bisher bestes Ziel?
2087
        if(distance < best_distance)
3✔
2088
        {
2089
            best_distance = distance;
3✔
2090
            best = hb;
3✔
2091
        }
2092
    }
2093

2094
    // Hafen gefunden?
2095
    if(best)
5✔
2096
    {
2097
        // Weg dorthin suchen
2098
        route->clear();
3✔
2099
        *goal_harborId = best->GetHarborPosID();
3✔
2100
        const MapPoint coastPt = world.GetCoastalPoint(best->GetHarborPosID(), ship->GetSeaID());
3✔
2101
        if(start == coastPt
3✔
2102
           || world.FindShipPathToHarbor(start, best->GetHarborPosID(), ship->GetSeaID(), route, nullptr))
3✔
2103
            return true;
3✔
2104
    }
2105

2106
    return false;
2✔
2107
}
2108

UNCOV
2109
void GamePlayer::TestForEmergencyProgramm()
×
2110
{
2111
    // we are already defeated, do not even think about an emergency program - it's too late :-(
2112
    if(isDefeated)
×
2113
        return;
×
2114

2115
    // In Lagern vorhandene Bretter und Steine zählen
UNCOV
2116
    unsigned boards = 0;
×
UNCOV
2117
    unsigned stones = 0;
×
UNCOV
2118
    for(nobBaseWarehouse* wh : buildings.GetStorehouses())
×
2119
    {
UNCOV
2120
        boards += wh->GetInventory().goods[GoodType::Boards];
×
2121
        stones += wh->GetInventory().goods[GoodType::Stones];
×
2122
    }
2123

2124
    // Emergency happens, if we have less than 10 boards or stones...
2125
    bool isNewEmergency = boards <= 10 || stones <= 10;
×
2126
    // ...and no woddcutter or sawmill
UNCOV
2127
    isNewEmergency &=
×
UNCOV
2128
      buildings.GetBuildings(BuildingType::Woodcutter).empty() || buildings.GetBuildings(BuildingType::Sawmill).empty();
×
2129

2130
    // Wenn nötig, Notfallprogramm auslösen
UNCOV
2131
    if(isNewEmergency)
×
2132
    {
UNCOV
2133
        if(!emergency)
×
2134
        {
UNCOV
2135
            emergency = true;
×
UNCOV
2136
            SendPostMessage(std::make_unique<PostMsg>(
×
UNCOV
2137
              world.GetEvMgr().GetCurrentGF(), _("The emergency program has been activated."), PostCategory::Economy));
×
2138
        }
2139
    } else
2140
    {
2141
        // Sobald Notfall vorbei, Notfallprogramm beenden, evtl. Baustellen wieder mit Kram versorgen
UNCOV
2142
        if(emergency)
×
2143
        {
UNCOV
2144
            emergency = false;
×
UNCOV
2145
            SendPostMessage(std::make_unique<PostMsg>(world.GetEvMgr().GetCurrentGF(),
×
UNCOV
2146
                                                      _("The emergency program has been deactivated."),
×
UNCOV
2147
                                                      PostCategory::Economy));
×
UNCOV
2148
            FindMaterialForBuildingSites();
×
2149
        }
2150
    }
2151
}
2152

2153
/// Testet die Bündnisse, ob sie nicht schon abgelaufen sind
2154
void GamePlayer::TestPacts()
20✔
2155
{
2156
    for(unsigned i = 0; i < world.GetNumPlayers(); ++i)
80✔
2157
    {
2158
        if(i == GetPlayerId())
60✔
2159
            continue;
20✔
2160

2161
        for(const auto pact : helpers::enumRange<PactType>())
400✔
2162
        {
2163
            // Pact not running
2164
            if(pacts[i][pact].duration == 0)
120✔
2165
                continue;
100✔
2166
            if(GetPactState(pact, i) == PactState::None)
20✔
2167
            {
2168
                // Pact was running but is expired -> Cancel for both players
2169
                pacts[i][pact].duration = 0;
2✔
2170
                pacts[i][pact].accepted = false;
2✔
2171
                GamePlayer& otherPlayer = world.GetPlayer(i);
2✔
2172
                RTTR_Assert(otherPlayer.pacts[GetPlayerId()][pact].duration);
2✔
2173
                RTTR_Assert(otherPlayer.pacts[GetPlayerId()][pact].accepted);
2✔
2174
                otherPlayer.pacts[GetPlayerId()][pact].duration = 0;
2✔
2175
                otherPlayer.pacts[GetPlayerId()][pact].accepted = false;
2✔
2176
                // And notify
2177
                PactChanged(pact);
2✔
2178
                otherPlayer.PactChanged(pact);
2✔
2179
            }
2180
        }
2181
    }
2182
}
20✔
2183

2184
bool GamePlayer::CanBuildCatapult() const
1✔
2185
{
2186
    // Wenn AddonId::LIMIT_CATAPULTS nicht aktiv ist, bauen immer erlaubt
2187
    if(!world.GetGGS().isEnabled(AddonId::LIMIT_CATAPULTS)) //-V807
1✔
2188
        return true;
1✔
2189

UNCOV
2190
    BuildingCount bc = buildings.GetBuildingNums();
×
2191

UNCOV
2192
    unsigned max = 0;
×
2193
    // proportional?
UNCOV
2194
    if(world.GetGGS().getSelection(AddonId::LIMIT_CATAPULTS) == 1)
×
2195
    {
UNCOV
2196
        max = int(bc.buildings[BuildingType::Barracks] * 0.125 + bc.buildings[BuildingType::Guardhouse] * 0.25
×
UNCOV
2197
                  + bc.buildings[BuildingType::Watchtower] * 0.5 + bc.buildings[BuildingType::Fortress]
×
UNCOV
2198
                  + 0.111); // to avoid rounding errors
×
UNCOV
2199
    } else if(world.GetGGS().getSelection(AddonId::LIMIT_CATAPULTS) < 8)
×
2200
    {
UNCOV
2201
        const std::array<unsigned, 6> limits = {{0, 3, 5, 10, 20, 30}};
×
UNCOV
2202
        max = limits[world.GetGGS().getSelection(AddonId::LIMIT_CATAPULTS) - 2];
×
2203
    }
2204

UNCOV
2205
    return bc.buildings[BuildingType::Catapult] + bc.buildingSites[BuildingType::Catapult] < max;
×
2206
}
2207

2208
/// A ship has discovered new hostile territory --> determines if this is new
2209
/// i.e. there is a sufficient distance to older locations
2210
/// Returns true if yes and false if not
2211
bool GamePlayer::ShipDiscoveredHostileTerritory(const MapPoint location)
25✔
2212
{
2213
    // Prüfen, ob Abstand zu bisherigen Punkten nicht zu klein
2214
    for(const auto& enemies_discovered_by_ship : enemies_discovered_by_ships)
25✔
2215
    {
2216
        if(world.CalcDistance(enemies_discovered_by_ship, location) < 30)
24✔
2217
            return false;
24✔
2218
    }
2219

2220
    // Nein? Dann haben wir ein neues Territorium gefunden
2221
    enemies_discovered_by_ships.push_back(location);
1✔
2222

2223
    return true;
1✔
2224
}
2225

2226
/// For debug only
2227
bool GamePlayer::IsDependentFigure(const noFigure& fig)
157✔
2228
{
2229
    for(const nobBaseWarehouse* wh : buildings.GetStorehouses())
350✔
2230
    {
2231
        if(wh->IsDependentFigure(fig))
193✔
UNCOV
2232
            return true;
×
2233
    }
2234
    return false;
157✔
2235
}
2236

2237
std::vector<nobBaseWarehouse*> GamePlayer::GetWarehousesForTrading(const nobBaseWarehouse& goalWh) const
18✔
2238
{
2239
    std::vector<nobBaseWarehouse*> result;
18✔
2240

2241
    // Don't try to trade with us!
2242
    if(goalWh.GetPlayer() == GetPlayerId())
18✔
2243
        return result;
6✔
2244

2245
    const MapPoint goalFlagPos = goalWh.GetFlagPos();
12✔
2246

2247
    TradePathCache& tradePathCache = world.GetTradePathCache();
12✔
2248
    for(nobBaseWarehouse* wh : buildings.GetStorehouses())
24✔
2249
    {
2250
        // Is there a trade path from this warehouse to wh? (flag to flag)
2251
        if(tradePathCache.pathExists(wh->GetFlagPos(), goalFlagPos, GetPlayerId()))
12✔
2252
            result.push_back(wh);
4✔
2253
    }
2254

2255
    return result;
12✔
2256
}
2257

2258
struct WarehouseDistanceComparator
2259
{
2260
    // Reference warehouse position, to which we want to calc the distance
2261
    const MapPoint refWareHousePos_;
2262
    /// GameWorld
2263
    const GameWorld& gwg_;
2264

2265
    WarehouseDistanceComparator(const nobBaseWarehouse& refWareHouse, const GameWorld& world)
12✔
2266
        : refWareHousePos_(refWareHouse.GetPos()), gwg_(world)
12✔
2267
    {}
12✔
2268

UNCOV
2269
    bool operator()(nobBaseWarehouse* const wh1, nobBaseWarehouse* const wh2) const
×
2270
    {
UNCOV
2271
        unsigned dist1 = gwg_.CalcDistance(wh1->GetPos(), refWareHousePos_);
×
UNCOV
2272
        unsigned dist2 = gwg_.CalcDistance(wh2->GetPos(), refWareHousePos_);
×
UNCOV
2273
        return (dist1 < dist2) || (dist1 == dist2 && wh1->GetObjId() < wh2->GetObjId());
×
2274
    }
2275
};
2276

2277
/// Send wares to warehouse wh
2278
void GamePlayer::Trade(nobBaseWarehouse* goalWh, const boost_variant2<GoodType, Job>& what, unsigned count) const
18✔
2279
{
2280
    if(!world.GetGGS().isEnabled(AddonId::TRADE))
18✔
2281
        return;
6✔
2282

2283
    if(count == 0)
16✔
2284
        return;
2✔
2285

2286
    // Don't try to trade with us!
2287
    if(goalWh->GetPlayer() == GetPlayerId())
14✔
2288
        return;
1✔
2289

2290
    // No trades with enemies
2291
    if(!IsAlly(goalWh->GetPlayer()))
13✔
2292
        return;
1✔
2293

2294
    const MapPoint goalFlagPos = goalWh->GetFlagPos();
12✔
2295

2296
    std::vector<nobBaseWarehouse*> whs(buildings.GetStorehouses().begin(), buildings.GetStorehouses().end());
12✔
2297
    std::sort(whs.begin(), whs.end(), WarehouseDistanceComparator(*goalWh, world));
12✔
2298
    TradePathCache& tradePathCache = world.GetTradePathCache();
12✔
2299
    for(nobBaseWarehouse* wh : whs)
24✔
2300
    {
2301
        // Get available wares
2302
        const unsigned available =
2303
          boost::variant2::visit(composeVisitor([wh](GoodType gt) { return wh->GetAvailableWaresForTrading(gt); },
19✔
2304
                                                [wh](Job job) { return wh->GetAvailableFiguresForTrading(job); }),
17✔
2305
                                 what);
12✔
2306
        if(available == 0)
12✔
UNCOV
2307
            continue;
×
2308

2309
        const unsigned actualCount = std::min(available, count);
12✔
2310

2311
        // Find a trade path from flag to flag
2312
        TradeRoute tr(world, GetPlayerId(), wh->GetFlagPos(), goalFlagPos);
12✔
2313

2314
        // Found a path?
2315
        if(tr.IsValid())
12✔
2316
        {
2317
            // Add to cache for future searches
2318
            tradePathCache.addEntry(tr.GetTradePath(), GetPlayerId());
11✔
2319

2320
            wh->StartTradeCaravane(what, actualCount, tr, goalWh);
11✔
2321
            count -= available;
11✔
2322
            if(count == 0)
11✔
UNCOV
2323
                return;
×
2324
        }
2325
    }
2326
}
2327

2328
bool GamePlayer::IsBuildingEnabled(BuildingType type) const
951✔
2329
{
2330
    return building_enabled[type] || (isHuman() && world.GetGameInterface()->GI_GetCheats().areAllBuildingsEnabled());
951✔
2331
}
2332

2333
void GamePlayer::FillVisualSettings(VisualSettings& visualSettings) const
2✔
2334
{
2335
    Distributions& visDistribution = visualSettings.distribution;
2✔
2336
    unsigned visIdx = 0;
2✔
2337
    for(const DistributionMapping& mapping : distributionMap)
54✔
2338
    {
2339
        visDistribution[visIdx++] = distribution[std::get<0>(mapping)].percent_buildings[std::get<1>(mapping)];
52✔
2340
    }
2341

2342
    visualSettings.useCustomBuildOrder = useCustomBuildOrder_;
2✔
2343
    visualSettings.build_order = build_order;
2✔
2344

2345
    visualSettings.transport_order = GetOrderingFromTransportPrio(transportPrio);
2✔
2346

2347
    visualSettings.military_settings = militarySettings_;
2✔
2348
    visualSettings.tools_settings = toolsSettings_;
2✔
2349
}
2✔
2350

2351
#define INSTANTIATE_FINDWH(Cond)                                                                                \
2352
    template nobBaseWarehouse* GamePlayer::FindWarehouse(const noRoadNode&, const Cond&, bool, bool, unsigned*, \
2353
                                                         const RoadSegment*) const
2354

2355
INSTANTIATE_FINDWH(FW::HasMinWares);
2356
INSTANTIATE_FINDWH(FW::HasFigure);
2357
INSTANTIATE_FINDWH(FW::HasWareAndFigure);
2358
INSTANTIATE_FINDWH(FW::HasAnyMatchingSoldier);
2359
INSTANTIATE_FINDWH(FW::AcceptsWare);
2360
INSTANTIATE_FINDWH(FW::AcceptsFigure);
2361
INSTANTIATE_FINDWH(FW::CollectsWare);
2362
INSTANTIATE_FINDWH(FW::CollectsFigure);
2363
INSTANTIATE_FINDWH(FW::HasWareButNoCollect);
2364
INSTANTIATE_FINDWH(FW::HasFigureButNoCollect);
2365
INSTANTIATE_FINDWH(FW::AcceptsFigureButNoSend);
2366
INSTANTIATE_FINDWH(FW::NoCondition);
2367

2368
#undef INSTANTIATE_FINDWH
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc