forked from Return-To-The-Roots/s25client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameWorldView.cpp
More file actions
680 lines (590 loc) · 22.9 KB
/
GameWorldView.cpp
File metadata and controls
680 lines (590 loc) · 22.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
// Copyright (C) 2005 - 2021 Settlers Freaks (sf-team at siedler25.org)
//
// SPDX-License-Identifier: GPL-2.0-or-later
#include "world/GameWorldView.h"
#include "CatapultStone.h"
#include "FOWObjects.h"
#include "GamePlayer.h"
#include "GlobalGameSettings.h"
#include "Loader.h"
#include "MapGeometry.h"
#include "Settings.h"
#include "addons/AddonMaxWaterwayLength.h"
#include "buildings/noBuildingSite.h"
#include "buildings/nobMilitary.h"
#include "buildings/nobUsual.h"
#include "drivers/VideoDriverWrapper.h"
#include "helpers/EnumArray.h"
#include "helpers/containerUtils.h"
#include "helpers/toString.h"
#include "ogl/FontStyle.h"
#include "ogl/glArchivItem_Bitmap.h"
#include "ogl/glFont.h"
#include "ogl/glSmartBitmap.h"
#include "world/GameWorldBase.h"
#include "world/GameWorldViewer.h"
#include "gameTypes/RoadBuildState.h"
#include "gameData/BuildingConsts.h"
#include "gameData/GuiConsts.h"
#include "gameData/MapConsts.h"
#include "s25util/error.h"
#include "s25util/warningSuppression.h"
#include <glad/glad.h>
#include <boost/format.hpp>
#include <cmath>
GameWorldView::GameWorldView(const GameWorldViewer& gwv, const Position& pos, const Extent& size)
: selPt(0, 0), show_bq(SETTINGS.ingame.showBQ), show_names(SETTINGS.ingame.showNames),
show_productivity(SETTINGS.ingame.showProductivity), offset(0, 0), lastOffset(0, 0), gwv(gwv), origin_(pos),
size_(size), zoomFactor_(1.f), targetZoomFactor_(1.f), zoomSpeed_(0.f)
{
MoveBy({0, 0});
}
const GameWorldBase& GameWorldView::GetWorld() const
{
return gwv.GetWorld();
}
SoundManager& GameWorldView::GetSoundMgr()
{
return const_cast<GameWorldViewer&>(gwv).GetSoundMgr();
}
void GameWorldView::SetNextZoomFactor()
{
if(zoomFactor_ == targetZoomFactor_) // == with float is ok here, is explicitly set in last step //-V550
return;
float remainingZoomDiff = targetZoomFactor_ - zoomFactor_;
if(std::abs(remainingZoomDiff) <= 0.5 * zoomSpeed_ * zoomSpeed_ / ZOOM_ACCELERATION)
{
// deceleration towards zero zoom speed
if(zoomSpeed_ > 0)
zoomSpeed_ -= ZOOM_ACCELERATION;
else
zoomSpeed_ += ZOOM_ACCELERATION;
} else
{
// acceleration to unlimited speed
if(remainingZoomDiff > 0)
zoomSpeed_ += ZOOM_ACCELERATION;
else
zoomSpeed_ -= ZOOM_ACCELERATION;
}
if(std::abs(remainingZoomDiff) < std::abs(zoomSpeed_))
{
// last step
zoomFactor_ = targetZoomFactor_;
zoomSpeed_ = 0;
}
zoomFactor_ = zoomFactor_ + zoomSpeed_;
CalcFxLx();
}
void GameWorldView::SetZoomFactor(float zoomFactor, bool smoothTransition /* = true*/)
{
if(zoomFactor < ZOOM_FACTORS.front())
targetZoomFactor_ = ZOOM_FACTORS.front();
else if(zoomFactor > ZOOM_FACTORS.back())
targetZoomFactor_ = ZOOM_FACTORS.back();
else
targetZoomFactor_ = zoomFactor;
if(!smoothTransition)
zoomFactor_ = targetZoomFactor_;
}
float GameWorldView::GetCurrentTargetZoomFactor() const
{
return targetZoomFactor_;
}
struct ObjectBetweenLines
{
noBase& obj;
DrawPoint pos; // Zeichenposition
ObjectBetweenLines(noBase& obj, const DrawPoint& pos) : obj(obj), pos(pos) {}
};
void GameWorldView::Draw(const RoadBuildState& rb, const MapPoint selected, bool drawMouse, unsigned* water)
{
SetNextZoomFactor();
int shortestDistToMouse = 100000;
Position mousePos = VIDEODRIVER.GetMousePos();
mousePos -= Position(origin_);
glScissor(origin_.x, VIDEODRIVER.GetRenderSize().y - origin_.y - size_.y, size_.x, size_.y);
if(zoomFactor_ != 1.f) //-V550
{
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glScalef(zoomFactor_, zoomFactor_, 1);
// Offset to center view
PointF diff(size_.x - size_.x / zoomFactor_, size_.y - size_.y / zoomFactor_);
diff = diff / 2.f;
glTranslatef(-diff.x, -diff.y, 0.f);
// Also adjust mouse
mousePos = Position(PointF(mousePos) / zoomFactor_ + diff);
glMatrixMode(GL_MODELVIEW);
}
glTranslatef(static_cast<GLfloat>(origin_.x) / zoomFactor_, static_cast<GLfloat>(origin_.y) / zoomFactor_, 0.0f);
glTranslatef(static_cast<GLfloat>(-offset.x), static_cast<GLfloat>(-offset.y), 0.0f);
const TerrainRenderer& terrainRenderer = gwv.GetTerrainRenderer();
terrainRenderer.Draw(GetFirstPt(), GetLastPt(), gwv, water);
glTranslatef(static_cast<GLfloat>(offset.x), static_cast<GLfloat>(offset.y), 0.0f);
for(int y = firstPt.y; y <= lastPt.y; ++y)
{
// Figuren speichern, die in dieser Zeile gemalt werden müssen
// und sich zwischen zwei Zeilen befinden, da sie dazwischen laufen
std::vector<ObjectBetweenLines> between_lines;
for(int x = firstPt.x; x <= lastPt.x; ++x)
{
Position curOffset;
const MapPoint curPt = terrainRenderer.ConvertCoords(Position(x, y), &curOffset);
DrawPoint curPos = GetWorld().GetNodePos(curPt) - offset + curOffset;
Position mouseDist = mousePos - curPos;
mouseDist *= mouseDist;
if(std::abs(mouseDist.x) + std::abs(mouseDist.y) < shortestDistToMouse)
{
selPt = curPt;
selPtOffset = curOffset;
shortestDistToMouse = std::abs(mouseDist.x) + std::abs(mouseDist.y);
}
Visibility visibility = gwv.GetVisibility(curPt);
DrawBoundaryStone(curPt, curPos, visibility);
if(visibility == Visibility::Visible)
{
DrawObject(curPt, curPos);
DrawMovingFiguresFromBelow(terrainRenderer, Position(x, y), between_lines);
DrawFigures(curPt, curPos, between_lines);
// Construction aid mode
if(show_bq)
DrawConstructionAid(curPt, curPos);
} else if(visibility == Visibility::FogOfWar)
{
const FOWObject* fowobj = gwv.GetYoungestFOWObject(MapPoint(curPt));
if(fowobj)
fowobj->Draw(curPos);
}
for(IDrawNodeCallback* callback : drawNodeCallbacks)
callback->onDraw(curPt, curPos);
}
// Figuren zwischen den Zeilen zeichnen
for(auto& between_line : between_lines)
between_line.obj.Draw(between_line.pos);
}
if(show_names || show_productivity)
DrawNameProductivityOverlay(terrainRenderer);
DrawGUI(rb, terrainRenderer, selected, drawMouse);
// Umherfliegende Katapultsteine zeichnen
for(auto* catapult_stone : GetWorld().catapult_stones)
{
if(gwv.GetVisibility(catapult_stone->dest_building) == Visibility::Visible
|| gwv.GetVisibility(catapult_stone->dest_map) == Visibility::Visible)
catapult_stone->Draw(offset);
}
if(zoomFactor_ != 1.f) //-V550
{
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
}
glTranslatef(-static_cast<GLfloat>(origin_.x) / zoomFactor_, -static_cast<GLfloat>(origin_.y) / zoomFactor_, 0.0f);
glScissor(0, 0, VIDEODRIVER.GetRenderSize().x, VIDEODRIVER.GetRenderSize().y);
}
void GameWorldView::DrawGUI(const RoadBuildState& rb, const TerrainRenderer& terrainRenderer,
const MapPoint& selectedPt, bool drawMouse)
{
// Falls im Straßenbaumodus: Punkte um den aktuellen Straßenbaupunkt herum ermitteln
helpers::EnumArray<MapPoint, Direction> road_points;
unsigned maxWaterWayLen = 0;
if(rb.mode != RoadBuildMode::Disabled)
{
for(const auto dir : helpers::EnumRange<Direction>{})
road_points[dir] = GetWorld().GetNeighbour(rb.point, dir);
const unsigned index = GetWorld().GetGGS().getSelection(AddonId::MAX_WATERWAY_LENGTH);
RTTR_Assert(index < waterwayLengths.size());
maxWaterWayLen = waterwayLengths[index];
}
for(int x = firstPt.x; x <= lastPt.x; ++x)
{
for(int y = firstPt.y; y <= lastPt.y; ++y)
{
// Coordinates transform
Position curOffset;
MapPoint curPt = terrainRenderer.ConvertCoords(Position(x, y), &curOffset);
Position curPos = GetWorld().GetNodePos(curPt) - offset + curOffset;
/// Current point indicated by Mouse
if(drawMouse && selPt == curPt)
{
// Mauszeiger am boden
unsigned mid = 22;
if(rb.mode == RoadBuildMode::Disabled)
{
switch(gwv.GetBQ(curPt))
{
case BuildingQuality::Flag: mid = 40; break;
case BuildingQuality::Mine: mid = 41; break;
case BuildingQuality::Hut: mid = 42; break;
case BuildingQuality::House: mid = 43; break;
case BuildingQuality::Castle: mid = 44; break;
case BuildingQuality::Harbor: mid = 45; break;
default: break;
}
}
LOADER.GetMapTexture(mid)->DrawFull(curPos);
}
// Currently selected point
if(selectedPt == curPt)
LOADER.GetMapTexture(20)->DrawFull(curPos);
// not building roads, no further action needed
if(rb.mode == RoadBuildMode::Disabled)
continue;
// we dont own curPt, no need for any rendering...
if(!gwv.IsPlayerTerritory(curPt))
continue;
// we are in road build mode
// highlight current route pt
if(rb.point == curPt)
{
LOADER.GetMapTexture(21)->DrawFull(curPos);
continue;
}
// ensure that curPt is a neighbour of rb.point
if(!helpers::contains(road_points, curPt))
{
continue;
}
// test on maximal water way length
if(rb.mode == RoadBuildMode::Boat && maxWaterWayLen != 0 && rb.route.size() >= maxWaterWayLen)
continue;
// render special icon for route revert
if(!rb.route.empty() && road_points[rb.route.back() + 3u] == curPt)
{
LOADER.GetMapTexture(67)->DrawFull(curPos);
continue;
}
// is a flag but not the route start flag, as it would revert the route.
const bool targetFlag = GetWorld().GetNO(curPt)->GetType() == NodalObjectType::Flag && curPt != rb.start;
int altitude = GetWorld().GetNode(rb.point).altitude;
if(targetFlag || gwv.IsRoadAvailable(rb.mode == RoadBuildMode::Boat, curPt))
{
unsigned id;
switch(int(GetWorld().GetNode(curPt).altitude) - altitude)
{
case 1: id = 61; break;
case 2:
case 3: id = 62; break;
case 4:
case 5: id = 63; break;
case -1: id = 64; break;
case -2:
case -3: id = 65; break;
case -4:
case -5: id = 66; break;
default: id = 60; break;
}
if(!targetFlag)
LOADER.GetMapTexture(id)->DrawFull(curPos);
else
{
DrawPoint lastPos = GetWorld().GetNodePos(rb.point) - offset + curOffset;
DrawPoint halfWayPos = (curPos + lastPos) / 2;
LOADER.GetMapTexture(id)->DrawFull(halfWayPos);
LOADER.GetMapTexture(20)->DrawFull(curPos);
}
}
}
}
}
void GameWorldView::DrawNameProductivityOverlay(const TerrainRenderer& terrainRenderer)
{
for(int x = firstPt.x; x <= lastPt.x; ++x)
{
for(int y = firstPt.y; y <= lastPt.y; ++y)
{
// Coordinate transform
Position curOffset;
MapPoint pt = terrainRenderer.ConvertCoords(Position(x, y), &curOffset);
const auto* no = GetWorld().GetSpecObj<noBaseBuilding>(pt);
if(!no)
continue;
Position curPos = GetWorld().GetNodePos(pt) - offset + curOffset;
curPos.y -= 22;
// Is object not belonging to local player?
if(no->GetPlayer() != gwv.GetPlayerId())
{
if(GetWorld().GetGGS().getSelection(AddonId::MILITARY_AID) == 2 && gwv.GetNumSoldiersForAttack(pt) > 0)
{
auto* attackAidImage = LOADER.GetImageN("map_new", 20000);
attackAidImage->DrawFull(curPos - DrawPoint(0, attackAidImage->getHeight()));
}
continue;
}
// Draw object name
if(show_names)
{
unsigned color = (no->GetGOT() == GO_Type::Buildingsite) ? COLOR_GREY : COLOR_YELLOW;
SmallFont->Draw(curPos, _(BUILDING_NAMES[no->GetBuildingType()]),
FontStyle::CENTER | FontStyle::VCENTER, color);
curPos.y += SmallFont->getHeight();
}
// Draw productivity/soldiers
if(show_productivity)
DrawProductivity(*no, curPos);
}
}
}
void GameWorldView::DrawProductivity(const noBaseBuilding& no, const DrawPoint& curPos)
{
const GO_Type got = no.GetGOT();
if(got == GO_Type::Buildingsite)
{
unsigned color = COLOR_GREY;
unsigned short p = static_cast<const noBuildingSite&>(no).GetBuildProgress();
SmallFont->Draw(curPos, (boost::format("(%1% %%)") % p).str(), FontStyle::CENTER | FontStyle::VCENTER, color);
} else if(got == GO_Type::NobUsual || got == GO_Type::NobShipyard)
{
const auto& n = static_cast<const nobUsual&>(no);
std::string text;
unsigned color = COLOR_0_PERCENT;
if(!n.HasWorker())
text = _("(House unoccupied)");
else if(n.IsProductionDisabledVirtual())
text = _("(stopped)");
else
{
// Catapult and Lookout tower doesn't have productivity!
if(n.GetBuildingType() == BuildingType::Catapult || n.GetBuildingType() == BuildingType::LookoutTower)
return;
unsigned short p = n.GetProductivity();
text = helpers::toString(p) + " %";
if(p >= 60)
color = COLOR_60_PERCENT;
else if(p >= 30)
color = COLOR_30_PERCENT;
else if(p >= 20)
color = COLOR_20_PERCENT;
}
SmallFont->Draw(curPos, text, FontStyle::CENTER | FontStyle::VCENTER, color);
} else if(got == GO_Type::NobMilitary)
{
// Display amount of soldiers
unsigned soldiers_count = static_cast<const nobMilitary&>(no).GetNumTroops();
std::string sSoldiers;
if(soldiers_count == 1)
sSoldiers = _("(1 soldier)");
else
sSoldiers = boost::str(boost::format(_("(%d soldiers)")) % soldiers_count);
SmallFont->Draw(curPos, sSoldiers, FontStyle::CENTER | FontStyle::VCENTER,
(soldiers_count > 0) ? COLOR_YELLOW : COLOR_RED);
}
}
void GameWorldView::DrawFigures(const MapPoint& pt, const DrawPoint& curPos,
std::vector<ObjectBetweenLines>& between_lines) const
{
for(noBase& figure : GetWorld().GetFigures(pt))
{
if(figure.IsMoving())
{
// Drawn from above
Direction curMoveDir = static_cast<noMovable&>(figure).GetCurMoveDir();
if(curMoveDir == Direction::NorthEast || curMoveDir == Direction::NorthWest)
continue;
// Draw later
between_lines.push_back(ObjectBetweenLines(figure, curPos));
} else if(figure.GetGOT() == GO_Type::Ship)
between_lines.push_back(ObjectBetweenLines(figure, curPos)); // TODO: Why special handling for ships?
else
// Ansonsten jetzt schon zeichnen
figure.Draw(curPos);
}
}
void GameWorldView::DrawMovingFiguresFromBelow(const TerrainRenderer& terrainRenderer, const DrawPoint& curPos,
std::vector<ObjectBetweenLines>& between_lines)
{
// First draw figures moving towards this point from below
static const std::array<Direction, 2> aboveDirs = {{Direction::NorthEast, Direction::NorthWest}};
for(Direction dir : aboveDirs)
{
// Get figures opposite the current dir and check if they are moving in this dir
// Coordinates transform
Position curOffset;
MapPoint curPt = terrainRenderer.ConvertCoords(GetNeighbour(curPos, dir + 3u), &curOffset);
Position figPos = GetWorld().GetNodePos(curPt) - offset + curOffset;
for(noBase& figure : GetWorld().GetFigures(curPt))
{
if(figure.IsMoving() && static_cast<noMovable&>(figure).GetCurMoveDir() == dir)
between_lines.push_back(ObjectBetweenLines(figure, figPos));
}
}
}
constexpr auto getBqImgs()
{
helpers::EnumArray<unsigned, BuildingQuality> imgs{};
imgs[BuildingQuality::Flag] = 50;
imgs[BuildingQuality::Hut] = 51;
imgs[BuildingQuality::House] = 52;
imgs[BuildingQuality::Castle] = 53;
imgs[BuildingQuality::Mine] = 54;
imgs[BuildingQuality::Harbor] = 55;
return imgs;
}
void GameWorldView::DrawConstructionAid(const MapPoint& pt, const DrawPoint& curPos)
{
BuildingQuality bq = gwv.GetBQ(pt);
if(bq != BuildingQuality::Nothing)
{
constexpr auto bqImgs = getBqImgs();
auto* bm = LOADER.GetMapTexture(bqImgs[bq]);
// Draw building quality icon
bm->DrawFull(curPos);
// Show ability to construct military buildings
if(GetWorld().GetGGS().isEnabled(AddonId::MILITARY_AID))
{
if(!GetWorld().IsMilitaryBuildingNearNode(pt, gwv.GetPlayerId())
&& (bq == BuildingQuality::Hut || bq == BuildingQuality::House || bq == BuildingQuality::Castle
|| bq == BuildingQuality::Harbor))
LOADER.GetImageN("map_new", 20000)->DrawFull(curPos - DrawPoint(-1, bm->GetSize().y + 5));
}
}
}
void GameWorldView::DrawObject(const MapPoint& pt, const DrawPoint& curPos) const
{
noBase* obj = GetWorld().GetNode(pt).obj;
if(!obj)
return;
obj->Draw(curPos);
}
void GameWorldView::DrawBoundaryStone(const MapPoint& pt, const DrawPoint pos, Visibility vis)
{
if(vis == Visibility::Invisible)
return;
const bool isFoW = vis == Visibility::FogOfWar;
const BoundaryStones& boundary_stones =
isFoW ? gwv.GetYoungestFOWNode(pt).boundary_stones : GetWorld().GetNode(pt).boundary_stones;
const unsigned char owner = boundary_stones[BorderStonePos::OnPoint];
if(!owner)
return;
const Nation nation = GetWorld().GetPlayer(owner - 1).nation;
unsigned player_color = GetWorld().GetPlayer(owner - 1).color;
if(isFoW)
player_color = CalcPlayerFOWDrawColor(player_color);
const auto curVertexPos = gwv.GetTerrainRenderer().GetVertexPos(pt);
for(const auto bPos : helpers::EnumRange<BorderStonePos>{})
{
DrawPoint curPos;
if(bPos == BorderStonePos::OnPoint)
curPos = pos;
else if(boundary_stones[bPos])
curPos = pos
- DrawPoint((curVertexPos - gwv.GetTerrainRenderer().GetNeighbourVertexPos(pt, toDirection(bPos)))
/ 2.0f);
else
continue;
LOADER.boundary_stone_cache[nation].draw(curPos, isFoW ? FOW_DRAW_COLOR : COLOR_WHITE, player_color);
}
}
void GameWorldView::ToggleShowBQ()
{
show_bq = !show_bq;
SaveIngameSettingsValues();
onHudSettingsChanged();
}
void GameWorldView::ToggleShowNames()
{
show_names = !show_names;
SaveIngameSettingsValues();
onHudSettingsChanged();
}
void GameWorldView::ToggleShowProductivity()
{
show_productivity = !show_productivity;
SaveIngameSettingsValues();
onHudSettingsChanged();
}
void GameWorldView::ToggleShowNamesAndProductivity()
{
if(show_productivity && show_names)
show_productivity = show_names = false;
else
show_productivity = show_names = true;
SaveIngameSettingsValues();
onHudSettingsChanged();
}
void GameWorldView::CopyHudSettingsTo(GameWorldView& other, bool copyBQ) const
{
other.show_bq = (copyBQ ? show_bq : false);
other.show_names = show_names;
other.show_productivity = show_productivity;
}
void GameWorldView::MoveBy(const DrawPoint& numPixels)
{
MoveTo(offset + numPixels);
}
void GameWorldView::MoveTo(const DrawPoint& newPos)
{
offset = newPos;
DrawPoint size(GetWorld().GetWidth() * TR_W, GetWorld().GetHeight() * TR_H);
if(size.x && size.y)
{
offset.x %= size.x;
offset.y %= size.y;
if(offset.x < 0)
offset.x += size.x;
if(offset.y < 0)
offset.y += size.y;
}
CalcFxLx();
}
void GameWorldView::MoveToMapPt(const MapPoint pt)
{
if(!pt.isValid())
return;
lastOffset = offset;
Position nodePos = GetWorld().GetNodePos(pt);
MoveTo(nodePos - GetSize() / 2u);
}
void GameWorldView::MoveToLastPosition()
{
Position newLastOffset = offset;
MoveTo(lastOffset);
lastOffset = newLastOffset;
}
void GameWorldView::AddDrawNodeCallback(IDrawNodeCallback* newCallback)
{
RTTR_Assert(newCallback);
drawNodeCallbacks.push_back(newCallback);
}
void GameWorldView::RemoveDrawNodeCallback(IDrawNodeCallback* callbackToRemove)
{
auto itPos = helpers::find(drawNodeCallbacks, callbackToRemove);
RTTR_Assert(itPos != drawNodeCallbacks.end());
drawNodeCallbacks.erase(itPos);
}
void GameWorldView::CalcFxLx()
{
// Calc first and last point in map units (with 1 extra for incomplete triangles)
firstPt.x = offset.x / TR_W - 1;
firstPt.y = offset.y / TR_H - 1;
lastPt.x = (offset.x + size_.x) / TR_W + 1;
const auto maxAltitude = gwv.getMaxNodeAltitude();
lastPt.y = (offset.y + size_.y + maxAltitude * HEIGHT_FACTOR) / TR_H + 1;
if(zoomFactor_ != 1.f) //-V550
{
// Calc pixels we can remove from sides, as they are not drawn due to zoom
PointF diff(size_.x - size_.x / zoomFactor_, size_.y - size_.y / zoomFactor_);
// Stay centered by removing half the pixels from opposite sites
diff = diff / 2.f;
// Convert to map points
diff.x /= TR_W;
diff.y /= TR_H;
// Don't remove to much
diff.x = std::floor(diff.x);
diff.y = std::floor(diff.y);
firstPt = Position(PointF(firstPt) + diff);
lastPt = Position(PointF(lastPt) - diff);
}
}
void GameWorldView::Resize(const Extent& newSize)
{
size_ = newSize;
CalcFxLx();
}
void GameWorldView::SaveIngameSettingsValues() const
{
auto& ingameSettings = SETTINGS.ingame;
ingameSettings.showBQ = show_bq;
ingameSettings.showNames = show_names;
ingameSettings.showProductivity = show_productivity;
}