File: MapRendererContext.cpp

package info (click to toggle)
vcmi 1.6.5%2Bdfsg-2
  • links: PTS, VCS
  • area: contrib
  • in suites: forky, sid, trixie
  • size: 32,060 kB
  • sloc: cpp: 238,971; python: 265; sh: 224; xml: 157; ansic: 78; objc: 61; makefile: 49
file content (603 lines) | stat: -rw-r--r-- 14,632 bytes parent folder | download
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
/*
 * MapRendererContextState.cpp, part of VCMI engine
 *
 * Authors: listed in file AUTHORS in main folder
 *
 * License: GNU General Public License v2.0 or later
 * Full text of license available in license.txt file, in main folder
 *
 */

#include "StdInc.h"
#include "MapRendererContext.h"

#include "MapRendererContextState.h"
#include "mapHandler.h"

#include "../../CCallback.h"
#include "../CGameInfo.h"
#include "../CPlayerInterface.h"
#include "../PlayerLocalState.h"

#include "../../lib/Point.h"
#include "../../lib/mapObjects/CGHeroInstance.h"
#include "../../lib/mapObjects/MiscObjects.h"
#include "../../lib/spells/CSpellHandler.h"
#include "../../lib/mapping/CMap.h"
#include "../../lib/pathfinder/CGPathNode.h"

MapRendererBaseContext::MapRendererBaseContext(const MapRendererContextState & viewState)
	: viewState(viewState)
{
}

uint32_t MapRendererBaseContext::getObjectRotation(ObjectInstanceID objectID) const
{
	const CGObjectInstance * obj = getObject(objectID);

	if(obj->ID == Obj::HERO)
	{
		const auto * hero = dynamic_cast<const CGHeroInstance *>(obj);
		return hero->moveDir;
	}

	if(obj->ID == Obj::BOAT)
	{
		const auto * boat = dynamic_cast<const CGBoat *>(obj);

		if(boat->hero)
			return boat->hero->moveDir;
		return boat->direction;
	}
	return 0;
}

int3 MapRendererBaseContext::getMapSize() const
{
	return LOCPLINT->cb->getMapSize();
}

bool MapRendererBaseContext::isInMap(const int3 & coordinates) const
{
	return LOCPLINT->cb->isInTheMap(coordinates);
}

bool MapRendererBaseContext::isVisible(const int3 & coordinates) const
{
	if(settingsSessionSpectate)
		return LOCPLINT->cb->isInTheMap(coordinates);
	else
		return LOCPLINT->cb->isVisible(coordinates);
}

bool MapRendererBaseContext::isActiveHero(const CGObjectInstance * obj) const
{
	if(obj->ID == Obj::HERO)
	{
		assert(dynamic_cast<const CGHeroInstance *>(obj) != nullptr);
		if(LOCPLINT->localState->getCurrentHero() != nullptr)
		{
			if(obj->id == LOCPLINT->localState->getCurrentHero()->id)
				return true;
		}
	}

	return false;
}

bool MapRendererBaseContext::tileAnimated(const int3 & coordinates) const
{
	return false;
}

const TerrainTile & MapRendererBaseContext::getMapTile(const int3 & coordinates) const
{
	return CGI->mh->getMap()->getTile(coordinates);
}

const MapRendererBaseContext::MapObjectsList & MapRendererBaseContext::getObjects(const int3 & coordinates) const
{
	assert(isInMap(coordinates));
	return viewState.objects[coordinates.z][coordinates.x][coordinates.y];
}

const CGObjectInstance * MapRendererBaseContext::getObject(ObjectInstanceID objectID) const
{
	return CGI->mh->getMap()->objects.at(objectID.getNum());
}

const CGPath * MapRendererBaseContext::currentPath() const
{
	return nullptr;
}

size_t MapRendererBaseContext::objectGroupIndex(ObjectInstanceID objectID) const
{
	static const std::array<size_t, 9> idleGroups = {0, 13, 0, 1, 2, 3, 4, 15, 14};
	return idleGroups[getObjectRotation(objectID)];
}

Point MapRendererBaseContext::objectImageOffset(ObjectInstanceID objectID, const int3 & coordinates) const
{
	const CGObjectInstance * object = getObject(objectID);
	int3 offsetTiles(object->anchorPos() - coordinates);
	return Point(offsetTiles) * Point(32, 32);
}

double MapRendererBaseContext::objectTransparency(ObjectInstanceID objectID, const int3 & coordinates) const
{
	const CGObjectInstance * object = getObject(objectID);

	if(object->ID == Obj::HERO)
	{
		const auto * hero = dynamic_cast<const CGHeroInstance *>(object);

		if(hero->inTownGarrison)
			return 0;

		if(hero->boat)
			return 0;
	}
	return 1;
}

size_t MapRendererBaseContext::objectImageIndex(ObjectInstanceID objectID, size_t groupSize) const
{
	return 0;
}

size_t MapRendererBaseContext::terrainImageIndex(size_t groupSize) const
{
	return 0;
}

size_t MapRendererBaseContext::overlayImageIndex(const int3 & coordinates) const
{
	return std::numeric_limits<size_t>::max();
}

std::string MapRendererBaseContext::overlayText(const int3 & coordinates) const
{
	return {};
}

ColorRGBA MapRendererBaseContext::overlayTextColor(const int3 & coordinates) const
{
	return {};
}

double MapRendererBaseContext::viewTransitionProgress() const
{
	return 0;
}

bool MapRendererBaseContext::filterGrayscale() const
{
	return false;
}

bool MapRendererBaseContext::showRoads() const
{
	return true;
}

bool MapRendererBaseContext::showRivers() const
{
	return true;
}

bool MapRendererBaseContext::showBorder() const
{
	return false;
}

bool MapRendererBaseContext::showImageOverlay() const
{
	return false;
}

bool MapRendererBaseContext::showTextOverlay() const
{
	return false;
}

bool MapRendererBaseContext::showGrid() const
{
	return false;
}

bool MapRendererBaseContext::showVisitable() const
{
	return false;
}

bool MapRendererBaseContext::showBlocked() const
{
	return false;
}

bool MapRendererBaseContext::showSpellRange(const int3 & position) const
{
	return false;
}

MapRendererAdventureContext::MapRendererAdventureContext(const MapRendererContextState & viewState)
	: MapRendererBaseContext(viewState)
{
}

const CGPath * MapRendererAdventureContext::currentPath() const
{
	const auto * hero = LOCPLINT->localState->getCurrentHero();

	if(!hero)
		return nullptr;

	if(!LOCPLINT->localState->hasPath(hero))
		return nullptr;

	return &LOCPLINT->localState->getPath(hero);
}

size_t MapRendererAdventureContext::objectImageIndex(ObjectInstanceID objectID, size_t groupSize) const
{
	assert(groupSize > 0);

	if(!settingsAdventureObjectAnimation)
		return 0;

	if(groupSize == 0)
		return 0;

	// usign objectID for frameCounter to add pseudo-random element per-object.
	// Without it, animation of multiple visible objects of the same type will always be in sync
	size_t baseFrameTime = 180;
	size_t frameCounter = animationTime / baseFrameTime + objectID.getNum();
	size_t frameIndex = frameCounter % groupSize;
	return frameIndex;
}

size_t MapRendererAdventureContext::terrainImageIndex(size_t groupSize) const
{
	if(!settingsAdventureTerrainAnimation)
		return 0;

	size_t baseFrameTime = 180;
	size_t frameCounter = animationTime / baseFrameTime;
	size_t frameIndex = frameCounter % groupSize;
	return frameIndex;
}

std::string MapRendererAdventureContext::overlayText(const int3 & coordinates) const
{
	if(!isVisible(coordinates))
		return {};

	const auto & tile = getMapTile(coordinates);

	if (!tile.visitable())
		return {};

	return tile.visitableObjects.back()->getObjectName();
}

ColorRGBA MapRendererAdventureContext::overlayTextColor(const int3 & coordinates) const
{
	if(!isVisible(coordinates))
		return {};

	const auto & tile = getMapTile(coordinates);

	if (!tile.visitable())
		return {};

	const auto * object = tile.visitableObjects.back();

	if (object->getOwner() == LOCPLINT->playerID)
		return { 0, 192, 0};

	if (LOCPLINT->cb->getPlayerRelations(object->getOwner(), LOCPLINT->playerID) == PlayerRelations::ALLIES)
		return { 0, 128, 255};

	if (object->getOwner().isValidPlayer())
		return { 255, 0, 0};

	if (object->ID == MapObjectID::MONSTER)
		return { 255, 0, 0};

	auto hero = LOCPLINT->localState->getCurrentHero();

	if (hero)
	{
		if (object->wasVisited(hero))
			return { 160, 160, 160 };
	}
	else
	{
		if (object->wasVisited(LOCPLINT->playerID))
			return { 160, 160, 160 };
	}

	return { 255, 192, 0 };
}

bool MapRendererAdventureContext::showBorder() const
{
	return true;
}

bool MapRendererAdventureContext::showGrid() const
{
	return settingShowGrid;
}

bool MapRendererAdventureContext::showVisitable() const
{
	return settingShowVisitable;
}

bool MapRendererAdventureContext::showBlocked() const
{
	return settingShowBlocked;
}

bool MapRendererAdventureContext::showTextOverlay() const
{
	return settingTextOverlay;
}

bool MapRendererAdventureContext::showSpellRange(const int3 & position) const
{
	if (!settingSpellRange)
		return false;

	auto hero = LOCPLINT->localState->getCurrentHero();

	if (!hero)
		return false;

	return !isInScreenRange(hero->getSightCenter(), position);
}

MapRendererAdventureTransitionContext::MapRendererAdventureTransitionContext(const MapRendererContextState & viewState)
	: MapRendererAdventureContext(viewState)
{
}

double MapRendererAdventureTransitionContext::viewTransitionProgress() const
{
	return progress;
}

MapRendererAdventureFadingContext::MapRendererAdventureFadingContext(const MapRendererContextState & viewState)
	: MapRendererAdventureContext(viewState)
{
}

bool MapRendererAdventureFadingContext::tileAnimated(const int3 & coordinates) const
{
	if(!isInMap(coordinates))
		return false;

	auto objects = getObjects(coordinates);
	if(vstd::contains(objects, target))
		return true;

	return false;
}

double MapRendererAdventureFadingContext::objectTransparency(ObjectInstanceID objectID, const int3 & coordinates) const
{
	if(objectID == target)
		return progress;

	return MapRendererAdventureContext::objectTransparency(objectID, coordinates);
}

MapRendererAdventureMovingContext::MapRendererAdventureMovingContext(const MapRendererContextState & viewState)
	: MapRendererAdventureContext(viewState)
{
}

size_t MapRendererAdventureMovingContext::objectGroupIndex(ObjectInstanceID objectID) const
{
	if(target == objectID)
	{
		static const std::array<size_t, 9> moveGroups = {0, 10, 5, 6, 7, 8, 9, 12, 11};
		return moveGroups[getObjectRotation(objectID)];
	}
	return MapRendererAdventureContext::objectGroupIndex(objectID);
}

bool MapRendererAdventureMovingContext::tileAnimated(const int3 & coordinates) const
{
	if(!isInMap(coordinates))
		return false;

	auto objects = getObjects(coordinates);
	if(vstd::contains(objects, target))
		return true;

	return false;
}

Point MapRendererAdventureMovingContext::objectImageOffset(ObjectInstanceID objectID, const int3 & coordinates) const
{
	if(target == objectID)
	{
		int3 offsetTilesFrom = tileFrom - coordinates;
		int3 offsetTilesDest = tileDest - coordinates;

		Point offsetPixelsFrom = Point(offsetTilesFrom) * Point(32, 32);
		Point offsetPixelsDest = Point(offsetTilesDest) * Point(32, 32);

		Point result = vstd::lerp(offsetPixelsFrom, offsetPixelsDest, progress);

		return result;
	}

	return MapRendererAdventureContext::objectImageOffset(objectID, coordinates);
}

size_t MapRendererAdventureMovingContext::objectImageIndex(ObjectInstanceID objectID, size_t groupSize) const
{
	if(target != objectID)
		return MapRendererAdventureContext::objectImageIndex(objectID, groupSize);

	int32_t baseFrameTime = 50;
	size_t frameCounter = animationTime / baseFrameTime;
	size_t frameIndex = frameCounter % groupSize;
	return frameIndex;
}

size_t MapRendererWorldViewContext::selectOverlayImageForObject(const ObjectPosInfo & object) const
{
	size_t ownerIndex = PlayerColor::PLAYER_LIMIT.getNum() * static_cast<size_t>(EWorldViewIcon::ICONS_PER_PLAYER);

	if(object.owner.isValidPlayer())
		ownerIndex = object.owner.getNum() * static_cast<size_t>(EWorldViewIcon::ICONS_PER_PLAYER);

	switch(object.id)
	{
		case Obj::MONOLITH_ONE_WAY_ENTRANCE:
		case Obj::MONOLITH_ONE_WAY_EXIT:
		case Obj::MONOLITH_TWO_WAY:
			return ownerIndex + static_cast<size_t>(EWorldViewIcon::TELEPORT);
		case Obj::SUBTERRANEAN_GATE:
			return ownerIndex + static_cast<size_t>(EWorldViewIcon::GATE);
		case Obj::ARTIFACT:
			return ownerIndex + static_cast<size_t>(EWorldViewIcon::ARTIFACT);
		case Obj::TOWN:
			return ownerIndex + static_cast<size_t>(EWorldViewIcon::TOWN);
		case Obj::HERO:
			return ownerIndex + static_cast<size_t>(EWorldViewIcon::HERO);
		case Obj::MINE:
			return ownerIndex + static_cast<size_t>(EWorldViewIcon::MINE_WOOD) + object.subId;
		case Obj::RESOURCE:
			return ownerIndex + static_cast<size_t>(EWorldViewIcon::RES_WOOD) + object.subId;
	}
	return std::numeric_limits<size_t>::max();
}

MapRendererWorldViewContext::MapRendererWorldViewContext(const MapRendererContextState & viewState)
	: MapRendererBaseContext(viewState)
{
}

bool MapRendererWorldViewContext::showImageOverlay() const
{
	return true;
}

size_t MapRendererWorldViewContext::overlayImageIndex(const int3 & coordinates) const
{
	if(!isVisible(coordinates))
		return std::numeric_limits<size_t>::max();

	for(const auto & objectID : getObjects(coordinates))
	{
		const auto * object = getObject(objectID);

		if(!object->visitableAt(coordinates))
			continue;

		ObjectPosInfo info(object);

		size_t iconIndex = selectOverlayImageForObject(info);

		if(iconIndex != std::numeric_limits<size_t>::max())
			return iconIndex;
	}

	return std::numeric_limits<size_t>::max();
}

MapRendererSpellViewContext::MapRendererSpellViewContext(const MapRendererContextState & viewState)
	: MapRendererWorldViewContext(viewState)
{
}

double MapRendererSpellViewContext::objectTransparency(ObjectInstanceID objectID, const int3 & coordinates) const
{
	if(showAllTerrain)
	{
		if(getObject(objectID)->isVisitable() && !MapRendererWorldViewContext::isVisible(coordinates))
			return 0;
	}

	return MapRendererWorldViewContext::objectTransparency(objectID, coordinates);
}

bool MapRendererSpellViewContext::isVisible(const int3 & coordinates) const
{
	if(showAllTerrain)
		return isInMap(coordinates);
	return MapRendererBaseContext::isVisible(coordinates);
}

size_t MapRendererSpellViewContext::overlayImageIndex(const int3 & coordinates) const
{
	for(const auto & entry : additionalOverlayIcons)
	{
		if(entry.pos != coordinates)
			continue;

		size_t iconIndex = selectOverlayImageForObject(entry);

		if(iconIndex != std::numeric_limits<size_t>::max())
			return iconIndex;
	}

	if (MapRendererBaseContext::isVisible(coordinates))
		return MapRendererWorldViewContext::overlayImageIndex(coordinates);
	else
		return std::numeric_limits<size_t>::max();
}

MapRendererPuzzleMapContext::MapRendererPuzzleMapContext(const MapRendererContextState & viewState)
	: MapRendererBaseContext(viewState)
{
}

MapRendererPuzzleMapContext::~MapRendererPuzzleMapContext() = default;

const CGPath * MapRendererPuzzleMapContext::currentPath() const
{
	return grailPos.get();
}

double MapRendererPuzzleMapContext::objectTransparency(ObjectInstanceID objectID, const int3 & coordinates) const
{
	const auto * object = getObject(objectID);

	if(!object)
		return 0;

	if(object->isVisitable())
		return 0;

	if(object->ID == Obj::HOLE)
		return 0;

	return MapRendererBaseContext::objectTransparency(objectID, coordinates);
}

bool MapRendererPuzzleMapContext::isVisible(const int3 & coordinates) const
{
	return LOCPLINT->cb->isInTheMap(coordinates);
}

bool MapRendererPuzzleMapContext::filterGrayscale() const
{
	return true;
}

bool MapRendererPuzzleMapContext::showRoads() const
{
	return false;
}

bool MapRendererPuzzleMapContext::showRivers() const
{
	return false;
}