2023-02-20 22:16:50 +02:00
|
|
|
/*
|
|
|
|
* MapViewController.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 "MapViewController.h"
|
|
|
|
|
|
|
|
#include "MapRendererContext.h"
|
2023-02-26 18:17:53 +02:00
|
|
|
#include "MapRendererContextState.h"
|
|
|
|
#include "MapViewCache.h"
|
2023-03-01 18:15:42 +02:00
|
|
|
#include "MapViewModel.h"
|
2023-02-20 22:16:50 +02:00
|
|
|
|
2023-02-26 18:17:53 +02:00
|
|
|
#include "../CPlayerInterface.h"
|
2023-05-08 14:18:34 +02:00
|
|
|
#include "../adventureMap/AdventureMapInterface.h"
|
2023-03-31 19:00:54 +02:00
|
|
|
#include "../gui/CGuiHandler.h"
|
2023-05-16 14:10:26 +02:00
|
|
|
#include "../gui/WindowHandler.h"
|
2023-08-27 21:40:56 +02:00
|
|
|
#include "../eventsSDL/InputHandler.h"
|
2023-02-22 22:06:27 +02:00
|
|
|
|
2023-02-20 22:16:50 +02:00
|
|
|
#include "../../lib/CConfigHandler.h"
|
|
|
|
#include "../../lib/mapObjects/CGHeroInstance.h"
|
|
|
|
#include "../../lib/mapObjects/MiscObjects.h"
|
2023-06-21 12:46:09 +02:00
|
|
|
#include "../../lib/pathfinder/CGPathNode.h"
|
2023-02-21 14:38:08 +02:00
|
|
|
#include "../../lib/spells/ViewSpellInt.h"
|
2023-02-20 22:16:50 +02:00
|
|
|
|
|
|
|
void MapViewController::setViewCenter(const int3 & position)
|
|
|
|
{
|
2023-02-23 19:46:41 +02:00
|
|
|
setViewCenter(Point(position) * model->getSingleTileSize() + model->getSingleTileSize() / 2, position.z);
|
2023-02-20 22:16:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void MapViewController::setViewCenter(const Point & position, int level)
|
|
|
|
{
|
2023-03-15 14:47:23 +02:00
|
|
|
Point upperLimit = Point(context->getMapSize()) * model->getSingleTileSize();
|
2023-03-01 18:15:42 +02:00
|
|
|
Point lowerLimit = Point(0, 0);
|
2023-02-23 19:46:41 +02:00
|
|
|
|
2023-03-01 18:15:42 +02:00
|
|
|
if(worldViewContext)
|
2023-02-23 19:46:41 +02:00
|
|
|
{
|
|
|
|
Point area = model->getPixelsVisibleDimensions();
|
|
|
|
Point mapCenter = upperLimit / 2;
|
|
|
|
|
|
|
|
Point desiredLowerLimit = lowerLimit + area / 2;
|
|
|
|
Point desiredUpperLimit = upperLimit - area / 2;
|
|
|
|
|
2023-03-01 18:15:42 +02:00
|
|
|
Point actualLowerLimit{
|
2023-02-23 19:46:41 +02:00
|
|
|
std::min(desiredLowerLimit.x, mapCenter.x),
|
|
|
|
std::min(desiredLowerLimit.y, mapCenter.y)
|
|
|
|
};
|
|
|
|
|
2023-03-01 18:15:42 +02:00
|
|
|
Point actualUpperLimit{
|
2023-02-23 19:46:41 +02:00
|
|
|
std::max(desiredUpperLimit.x, mapCenter.x),
|
|
|
|
std::max(desiredUpperLimit.y, mapCenter.y)
|
|
|
|
};
|
|
|
|
|
|
|
|
upperLimit = actualUpperLimit;
|
|
|
|
lowerLimit = actualLowerLimit;
|
|
|
|
}
|
|
|
|
|
2023-04-11 00:00:46 +02:00
|
|
|
Point betterPosition = {std::clamp(position.x, lowerLimit.x, upperLimit.x), std::clamp(position.y, lowerLimit.y, upperLimit.y)};
|
2023-02-20 22:16:50 +02:00
|
|
|
|
|
|
|
model->setViewCenter(betterPosition);
|
2023-04-11 00:00:46 +02:00
|
|
|
model->setLevel(std::clamp(level, 0, context->getMapSize().z));
|
2023-02-23 19:46:41 +02:00
|
|
|
|
2023-04-24 16:16:55 +02:00
|
|
|
if(adventureInt && !puzzleMapContext) // may be called before adventureInt is initialized
|
2023-02-23 19:46:41 +02:00
|
|
|
adventureInt->onMapViewMoved(model->getTilesTotalRect(), model->getLevel());
|
2023-02-20 22:16:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void MapViewController::setTileSize(const Point & tileSize)
|
|
|
|
{
|
2023-05-17 13:36:12 +02:00
|
|
|
Point oldSize = model->getSingleTileSize();
|
2023-02-20 22:16:50 +02:00
|
|
|
model->setTileSize(tileSize);
|
2023-02-23 19:46:41 +02:00
|
|
|
|
2023-05-17 13:36:12 +02:00
|
|
|
double scaleChangeX = 1.0 * tileSize.x / oldSize.x;
|
|
|
|
double scaleChangeY = 1.0 * tileSize.y / oldSize.y;
|
|
|
|
|
|
|
|
Point newViewCenter {
|
|
|
|
static_cast<int>(std::round(model->getMapViewCenter().x * scaleChangeX)),
|
|
|
|
static_cast<int>(std::round(model->getMapViewCenter().y * scaleChangeY))
|
|
|
|
};
|
|
|
|
|
2023-02-23 19:46:41 +02:00
|
|
|
// force update of view center since changing tile size may invalidated it
|
2023-05-17 13:36:12 +02:00
|
|
|
setViewCenter(newViewCenter, model->getLevel());
|
2023-02-20 22:16:50 +02:00
|
|
|
}
|
|
|
|
|
2023-05-16 23:49:24 +02:00
|
|
|
void MapViewController::modifyTileSize(int stepsChange)
|
2023-05-16 22:42:51 +02:00
|
|
|
{
|
2023-05-16 23:49:24 +02:00
|
|
|
// we want to zoom in/out in fixed 10% steps, to allow player to return back to exactly 100% zoom just by scrolling
|
|
|
|
// so, zooming in for 5 steps will put game at 1.1^5 = 1.61 scale
|
|
|
|
// try to determine current zooming level and change it by requested number of steps
|
2023-08-27 14:00:40 +02:00
|
|
|
double currentZoomFactor = targetTileSize.x / static_cast<double>(defaultTileSize);
|
2023-05-31 15:15:15 +02:00
|
|
|
double currentZoomSteps = std::round(std::log(currentZoomFactor) / std::log(1.01));
|
2023-05-17 13:54:19 +02:00
|
|
|
double newZoomSteps = stepsChange != 0 ? currentZoomSteps + stepsChange : stepsChange;
|
2023-05-31 15:15:15 +02:00
|
|
|
double newZoomFactor = std::pow(1.01, newZoomSteps);
|
2023-05-16 23:49:24 +02:00
|
|
|
|
2023-08-27 14:00:40 +02:00
|
|
|
Point currentZoom = targetTileSize;
|
|
|
|
Point desiredZoom = Point(defaultTileSize,defaultTileSize) * newZoomFactor;
|
2023-05-16 22:42:51 +02:00
|
|
|
|
2023-05-16 23:49:24 +02:00
|
|
|
if (desiredZoom == currentZoom && stepsChange < 0)
|
2023-05-16 22:42:51 +02:00
|
|
|
desiredZoom -= Point(1,1);
|
|
|
|
|
2023-05-16 23:49:24 +02:00
|
|
|
if (desiredZoom == currentZoom && stepsChange > 0)
|
2023-05-16 22:42:51 +02:00
|
|
|
desiredZoom += Point(1,1);
|
|
|
|
|
|
|
|
Point minimal = model->getSingleTileSizeLowerLimit();
|
|
|
|
Point maximal = model->getSingleTileSizeUpperLimit();
|
|
|
|
Point actualZoom = {
|
|
|
|
std::clamp(desiredZoom.x, minimal.x, maximal.x),
|
|
|
|
std::clamp(desiredZoom.y, minimal.y, maximal.y)
|
|
|
|
};
|
|
|
|
|
2023-05-17 13:36:12 +02:00
|
|
|
if (actualZoom != currentZoom)
|
2023-08-27 14:00:40 +02:00
|
|
|
{
|
|
|
|
targetTileSize = actualZoom;
|
|
|
|
if(actualZoom.x >= defaultTileSize - zoomTileDeadArea && actualZoom.x <= defaultTileSize + zoomTileDeadArea)
|
|
|
|
actualZoom.x = defaultTileSize;
|
|
|
|
if(actualZoom.y >= defaultTileSize - zoomTileDeadArea && actualZoom.y <= defaultTileSize + zoomTileDeadArea)
|
|
|
|
actualZoom.y = defaultTileSize;
|
2023-08-27 21:40:56 +02:00
|
|
|
|
2023-08-27 23:17:47 +02:00
|
|
|
bool isInDeadZone = targetTileSize != actualZoom || actualZoom == Point(defaultTileSize, defaultTileSize);
|
2023-08-27 21:40:56 +02:00
|
|
|
|
|
|
|
if(!wasInDeadZone && isInDeadZone)
|
|
|
|
GH.input().hapticFeedback();
|
|
|
|
|
|
|
|
wasInDeadZone = isInDeadZone;
|
|
|
|
|
2023-05-17 13:36:12 +02:00
|
|
|
setTileSize(actualZoom);
|
2023-08-27 14:00:40 +02:00
|
|
|
}
|
2023-02-20 22:16:50 +02:00
|
|
|
}
|
|
|
|
|
2023-02-26 18:17:53 +02:00
|
|
|
MapViewController::MapViewController(std::shared_ptr<MapViewModel> model, std::shared_ptr<MapViewCache> view)
|
|
|
|
: state(new MapRendererContextState())
|
2023-02-20 22:16:50 +02:00
|
|
|
, model(std::move(model))
|
2023-02-26 18:17:53 +02:00
|
|
|
, view(view)
|
2023-02-20 22:16:50 +02:00
|
|
|
{
|
2023-02-26 18:17:53 +02:00
|
|
|
adventureContext = std::make_shared<MapRendererAdventureContext>(*state);
|
|
|
|
context = adventureContext;
|
2023-02-20 22:16:50 +02:00
|
|
|
}
|
|
|
|
|
2023-02-23 19:46:41 +02:00
|
|
|
std::shared_ptr<IMapRendererContext> MapViewController::getContext() const
|
2023-02-21 19:21:50 +02:00
|
|
|
{
|
|
|
|
return context;
|
|
|
|
}
|
|
|
|
|
2023-05-13 16:24:18 +02:00
|
|
|
void MapViewController::tick(uint32_t timeDelta)
|
2023-02-20 22:16:50 +02:00
|
|
|
{
|
|
|
|
// confirmed to match H3 for
|
|
|
|
// - hero embarking on boat (500 ms)
|
|
|
|
// - hero disembarking from boat (500 ms)
|
|
|
|
// - TODO: picking up resources
|
|
|
|
// - TODO: killing mosters
|
|
|
|
// - teleporting ( 250 ms)
|
|
|
|
static const double fadeOutDuration = 500;
|
|
|
|
static const double fadeInDuration = 500;
|
2023-03-01 19:25:51 +02:00
|
|
|
static const double heroTeleportDuration = 250;
|
2023-02-20 22:16:50 +02:00
|
|
|
|
2023-02-26 18:17:53 +02:00
|
|
|
if(movementContext)
|
2023-02-20 22:16:50 +02:00
|
|
|
{
|
2023-02-26 18:17:53 +02:00
|
|
|
const auto * object = context->getObject(movementContext->target);
|
2023-03-01 18:15:42 +02:00
|
|
|
const auto * hero = dynamic_cast<const CGHeroInstance *>(object);
|
|
|
|
const auto * boat = dynamic_cast<const CGBoat *>(object);
|
2023-02-22 22:06:27 +02:00
|
|
|
|
|
|
|
assert(boat || hero);
|
|
|
|
|
2023-03-01 18:15:42 +02:00
|
|
|
if(!hero)
|
2023-02-22 22:06:27 +02:00
|
|
|
hero = boat->hero;
|
|
|
|
|
2023-03-01 18:15:42 +02:00
|
|
|
double heroMoveTime = LOCPLINT->makingTurn ?
|
|
|
|
settings["adventure"]["heroMoveTime"].Float() :
|
2023-02-26 18:17:53 +02:00
|
|
|
settings["adventure"]["enemyMoveTime"].Float();
|
2023-02-20 22:16:50 +02:00
|
|
|
|
2023-02-26 18:17:53 +02:00
|
|
|
movementContext->progress += timeDelta / heroMoveTime;
|
2023-03-28 21:37:53 +02:00
|
|
|
movementContext->progress = std::min( 1.0, movementContext->progress);
|
2023-02-20 22:16:50 +02:00
|
|
|
|
2023-02-26 18:17:53 +02:00
|
|
|
Point positionFrom = Point(hero->convertToVisitablePos(movementContext->tileFrom)) * model->getSingleTileSize() + model->getSingleTileSize() / 2;
|
|
|
|
Point positionDest = Point(hero->convertToVisitablePos(movementContext->tileDest)) * model->getSingleTileSize() + model->getSingleTileSize() / 2;
|
2023-02-20 22:16:50 +02:00
|
|
|
|
2023-02-26 18:17:53 +02:00
|
|
|
Point positionCurr = vstd::lerp(positionFrom, positionDest, movementContext->progress);
|
2023-02-20 22:16:50 +02:00
|
|
|
|
2023-03-28 21:37:53 +02:00
|
|
|
setViewCenter(positionCurr, movementContext->tileDest.z);
|
2023-02-20 22:16:50 +02:00
|
|
|
}
|
|
|
|
|
2023-03-01 19:25:51 +02:00
|
|
|
if(teleportContext)
|
|
|
|
{
|
|
|
|
teleportContext->progress += timeDelta / heroTeleportDuration;
|
2023-03-28 21:37:53 +02:00
|
|
|
teleportContext->progress = std::min( 1.0, teleportContext->progress);
|
2023-03-01 19:25:51 +02:00
|
|
|
}
|
2023-02-26 18:17:53 +02:00
|
|
|
|
|
|
|
if(fadingOutContext)
|
2023-02-20 22:16:50 +02:00
|
|
|
{
|
2023-02-26 18:17:53 +02:00
|
|
|
fadingOutContext->progress -= timeDelta / fadeOutDuration;
|
2023-03-28 21:37:53 +02:00
|
|
|
fadingOutContext->progress = std::max( 0.0, fadingOutContext->progress);
|
2023-02-20 22:16:50 +02:00
|
|
|
}
|
|
|
|
|
2023-02-26 18:17:53 +02:00
|
|
|
if(fadingInContext)
|
2023-02-20 22:16:50 +02:00
|
|
|
{
|
2023-02-26 18:17:53 +02:00
|
|
|
fadingInContext->progress += timeDelta / fadeInDuration;
|
2023-03-28 21:37:53 +02:00
|
|
|
fadingInContext->progress = std::min( 1.0, fadingInContext->progress);
|
2023-02-20 22:16:50 +02:00
|
|
|
}
|
|
|
|
|
2023-07-07 00:27:57 +02:00
|
|
|
if (adventureContext)
|
|
|
|
adventureContext->animationTime += timeDelta;
|
|
|
|
|
|
|
|
updateState();
|
|
|
|
}
|
|
|
|
|
|
|
|
void MapViewController::updateState()
|
|
|
|
{
|
2023-03-01 18:15:42 +02:00
|
|
|
if(adventureContext)
|
2023-02-20 22:16:50 +02:00
|
|
|
{
|
2023-02-26 18:17:53 +02:00
|
|
|
adventureContext->settingsSessionSpectate = settings["session"]["spectate"].Bool();
|
|
|
|
adventureContext->settingsAdventureObjectAnimation = settings["adventure"]["objectAnimation"].Bool();
|
|
|
|
adventureContext->settingsAdventureTerrainAnimation = settings["adventure"]["terrainAnimation"].Bool();
|
|
|
|
adventureContext->settingShowGrid = settings["gameTweaks"]["showGrid"].Bool();
|
|
|
|
adventureContext->settingShowVisitable = settings["session"]["showVisitable"].Bool();
|
2023-03-08 01:19:26 +02:00
|
|
|
adventureContext->settingShowBlocked = settings["session"]["showBlocked"].Bool();
|
2023-04-20 13:15:08 +02:00
|
|
|
adventureContext->settingSpellRange = settings["session"]["showSpellRange"].Bool();
|
2023-02-20 22:16:50 +02:00
|
|
|
}
|
2023-02-26 18:17:53 +02:00
|
|
|
}
|
|
|
|
|
2023-05-13 16:24:18 +02:00
|
|
|
void MapViewController::afterRender()
|
2023-03-28 21:37:53 +02:00
|
|
|
{
|
|
|
|
if(movementContext)
|
|
|
|
{
|
|
|
|
const auto * object = context->getObject(movementContext->target);
|
|
|
|
const auto * hero = dynamic_cast<const CGHeroInstance *>(object);
|
|
|
|
const auto * boat = dynamic_cast<const CGBoat *>(object);
|
|
|
|
|
|
|
|
assert(boat || hero);
|
|
|
|
|
|
|
|
if(!hero)
|
|
|
|
hero = boat->hero;
|
|
|
|
|
|
|
|
if(movementContext->progress >= 1.0)
|
|
|
|
{
|
|
|
|
setViewCenter(hero->getSightCenter());
|
|
|
|
|
|
|
|
removeObject(context->getObject(movementContext->target));
|
|
|
|
addObject(context->getObject(movementContext->target));
|
|
|
|
|
|
|
|
activateAdventureContext(movementContext->animationTime);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(teleportContext && teleportContext->progress >= 1.0)
|
|
|
|
{
|
|
|
|
activateAdventureContext(teleportContext->animationTime);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(fadingOutContext && fadingOutContext->progress <= 0.0)
|
|
|
|
{
|
|
|
|
removeObject(context->getObject(fadingOutContext->target));
|
|
|
|
|
|
|
|
activateAdventureContext(fadingOutContext->animationTime);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(fadingInContext && fadingInContext->progress >= 1.0)
|
|
|
|
{
|
|
|
|
activateAdventureContext(fadingInContext->animationTime);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-10 11:54:26 +02:00
|
|
|
bool MapViewController::isEventInstant(const CGObjectInstance * obj)
|
|
|
|
{
|
|
|
|
if (!isEventVisible(obj))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if(!LOCPLINT->makingTurn && settings["adventure"]["enemyMoveTime"].Float() <= 0)
|
|
|
|
return true; // instant movement speed
|
|
|
|
|
|
|
|
if(LOCPLINT->makingTurn && settings["adventure"]["heroMoveTime"].Float() <= 0)
|
|
|
|
return true; // instant movement speed
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-02-26 18:17:53 +02:00
|
|
|
bool MapViewController::isEventVisible(const CGObjectInstance * obj)
|
|
|
|
{
|
2023-03-01 18:15:42 +02:00
|
|
|
if(adventureContext == nullptr)
|
2023-02-26 18:17:53 +02:00
|
|
|
return false;
|
|
|
|
|
2023-03-01 18:15:42 +02:00
|
|
|
if(!LOCPLINT->makingTurn && settings["adventure"]["enemyMoveTime"].Float() < 0)
|
2023-02-26 18:17:53 +02:00
|
|
|
return false; // enemy move speed set to "hidden/none"
|
|
|
|
|
2023-05-16 17:34:23 +02:00
|
|
|
if(!GH.windows().isTopWindow(adventureInt))
|
2023-03-31 19:00:54 +02:00
|
|
|
return false;
|
|
|
|
|
2023-03-01 18:15:42 +02:00
|
|
|
if(obj->isVisitable())
|
2023-02-26 18:17:53 +02:00
|
|
|
return context->isVisible(obj->visitablePos());
|
|
|
|
else
|
|
|
|
return context->isVisible(obj->pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MapViewController::isEventVisible(const CGHeroInstance * obj, const int3 & from, const int3 & dest)
|
|
|
|
{
|
2023-03-01 18:15:42 +02:00
|
|
|
if(adventureContext == nullptr)
|
2023-02-26 18:17:53 +02:00
|
|
|
return false;
|
2023-02-20 22:16:50 +02:00
|
|
|
|
2023-03-01 18:15:42 +02:00
|
|
|
if(!LOCPLINT->makingTurn && settings["adventure"]["enemyMoveTime"].Float() < 0)
|
2023-02-26 18:17:53 +02:00
|
|
|
return false; // enemy move speed set to "hidden/none"
|
2023-02-22 13:14:11 +02:00
|
|
|
|
2023-05-16 17:34:23 +02:00
|
|
|
if(!GH.windows().isTopWindow(adventureInt))
|
2023-03-31 19:00:54 +02:00
|
|
|
return false;
|
|
|
|
|
2023-03-01 18:15:42 +02:00
|
|
|
if(context->isVisible(obj->convertToVisitablePos(from)))
|
2023-02-26 18:17:53 +02:00
|
|
|
return true;
|
|
|
|
|
2023-03-01 18:15:42 +02:00
|
|
|
if(context->isVisible(obj->convertToVisitablePos(dest)))
|
2023-02-26 18:17:53 +02:00
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MapViewController::fadeOutObject(const CGObjectInstance * obj)
|
|
|
|
{
|
|
|
|
fadingOutContext = std::make_shared<MapRendererAdventureFadingContext>(*state);
|
|
|
|
fadingOutContext->animationTime = adventureContext->animationTime;
|
|
|
|
adventureContext = fadingOutContext;
|
|
|
|
context = fadingOutContext;
|
|
|
|
|
2023-03-15 00:31:51 +02:00
|
|
|
const CGObjectInstance * movingObject = obj;
|
|
|
|
if (obj->ID == Obj::HERO)
|
|
|
|
{
|
|
|
|
auto * hero = dynamic_cast<const CGHeroInstance*>(obj);
|
|
|
|
if (hero->boat)
|
|
|
|
movingObject = hero->boat;
|
|
|
|
}
|
|
|
|
|
|
|
|
fadingOutContext->target = movingObject->id;
|
2023-02-26 18:17:53 +02:00
|
|
|
fadingOutContext->progress = 1.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MapViewController::fadeInObject(const CGObjectInstance * obj)
|
|
|
|
{
|
|
|
|
fadingInContext = std::make_shared<MapRendererAdventureFadingContext>(*state);
|
|
|
|
fadingInContext->animationTime = adventureContext->animationTime;
|
|
|
|
adventureContext = fadingInContext;
|
|
|
|
context = fadingInContext;
|
|
|
|
|
2023-03-15 00:31:51 +02:00
|
|
|
const CGObjectInstance * movingObject = obj;
|
|
|
|
if (obj->ID == Obj::HERO)
|
|
|
|
{
|
|
|
|
auto * hero = dynamic_cast<const CGHeroInstance*>(obj);
|
|
|
|
if (hero->boat)
|
|
|
|
movingObject = hero->boat;
|
|
|
|
}
|
|
|
|
|
|
|
|
fadingInContext->target = movingObject->id;
|
2023-02-26 18:17:53 +02:00
|
|
|
fadingInContext->progress = 0.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MapViewController::removeObject(const CGObjectInstance * obj)
|
|
|
|
{
|
2023-03-22 00:38:55 +02:00
|
|
|
if (obj->ID == Obj::BOAT)
|
|
|
|
{
|
|
|
|
auto * boat = dynamic_cast<const CGBoat*>(obj);
|
|
|
|
if (boat->hero)
|
|
|
|
{
|
|
|
|
view->invalidate(context, boat->hero->id);
|
|
|
|
state->removeObject(boat->hero);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (obj->ID == Obj::HERO)
|
|
|
|
{
|
|
|
|
auto * hero = dynamic_cast<const CGHeroInstance*>(obj);
|
|
|
|
if (hero->boat)
|
|
|
|
{
|
|
|
|
view->invalidate(context, hero->boat->id);
|
|
|
|
state->removeObject(hero->boat);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-26 18:17:53 +02:00
|
|
|
view->invalidate(context, obj->id);
|
|
|
|
state->removeObject(obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MapViewController::addObject(const CGObjectInstance * obj)
|
|
|
|
{
|
|
|
|
state->addObject(obj);
|
|
|
|
view->invalidate(context, obj->id);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MapViewController::onBeforeHeroEmbark(const CGHeroInstance * obj, const int3 & from, const int3 & dest)
|
|
|
|
{
|
2023-03-01 18:15:42 +02:00
|
|
|
if(isEventVisible(obj, from, dest))
|
2023-02-26 18:17:53 +02:00
|
|
|
{
|
2023-08-10 11:54:26 +02:00
|
|
|
if (!isEventInstant(obj))
|
|
|
|
fadeOutObject(obj);
|
2023-02-26 18:17:53 +02:00
|
|
|
setViewCenter(obj->getSightCenter());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
removeObject(obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MapViewController::onAfterHeroEmbark(const CGHeroInstance * obj, const int3 & from, const int3 & dest)
|
|
|
|
{
|
2023-03-01 18:15:42 +02:00
|
|
|
if(isEventVisible(obj, from, dest))
|
2023-02-26 18:17:53 +02:00
|
|
|
setViewCenter(obj->getSightCenter());
|
|
|
|
}
|
|
|
|
|
|
|
|
void MapViewController::onBeforeHeroDisembark(const CGHeroInstance * obj, const int3 & from, const int3 & dest)
|
|
|
|
{
|
2023-03-01 18:15:42 +02:00
|
|
|
if(isEventVisible(obj, from, dest))
|
2023-02-26 18:17:53 +02:00
|
|
|
setViewCenter(obj->getSightCenter());
|
|
|
|
}
|
|
|
|
|
|
|
|
void MapViewController::onAfterHeroDisembark(const CGHeroInstance * obj, const int3 & from, const int3 & dest)
|
|
|
|
{
|
2023-03-01 18:15:42 +02:00
|
|
|
if(isEventVisible(obj, from, dest))
|
2023-02-26 18:17:53 +02:00
|
|
|
{
|
2023-08-10 11:54:26 +02:00
|
|
|
if (!isEventInstant(obj))
|
|
|
|
fadeInObject(obj);
|
2023-02-26 18:17:53 +02:00
|
|
|
setViewCenter(obj->getSightCenter());
|
|
|
|
}
|
|
|
|
addObject(obj);
|
2023-02-20 22:16:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void MapViewController::onObjectFadeIn(const CGObjectInstance * obj)
|
|
|
|
{
|
2023-02-26 18:17:53 +02:00
|
|
|
assert(!hasOngoingAnimations());
|
2023-02-22 22:06:27 +02:00
|
|
|
|
2023-08-10 11:54:26 +02:00
|
|
|
if(isEventVisible(obj) && !isEventInstant(obj) )
|
2023-02-26 18:17:53 +02:00
|
|
|
fadeInObject(obj);
|
2023-02-22 22:06:27 +02:00
|
|
|
|
2023-02-26 18:17:53 +02:00
|
|
|
addObject(obj);
|
2023-02-20 22:16:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void MapViewController::onObjectFadeOut(const CGObjectInstance * obj)
|
|
|
|
{
|
2023-02-26 18:17:53 +02:00
|
|
|
assert(!hasOngoingAnimations());
|
2023-02-22 22:06:27 +02:00
|
|
|
|
2023-08-10 11:54:26 +02:00
|
|
|
if(isEventVisible(obj) && !isEventInstant(obj) )
|
2023-02-26 18:17:53 +02:00
|
|
|
fadeOutObject(obj);
|
2023-02-22 22:06:27 +02:00
|
|
|
else
|
2023-02-26 18:17:53 +02:00
|
|
|
removeObject(obj);
|
2023-02-20 22:16:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void MapViewController::onObjectInstantAdd(const CGObjectInstance * obj)
|
|
|
|
{
|
2023-02-26 18:17:53 +02:00
|
|
|
addObject(obj);
|
2023-02-20 22:16:50 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
void MapViewController::onObjectInstantRemove(const CGObjectInstance * obj)
|
|
|
|
{
|
2023-02-26 18:17:53 +02:00
|
|
|
removeObject(obj);
|
2023-02-20 22:16:50 +02:00
|
|
|
};
|
|
|
|
|
2023-02-26 18:17:53 +02:00
|
|
|
void MapViewController::onBeforeHeroTeleported(const CGHeroInstance * obj, const int3 & from, const int3 & dest)
|
2023-02-20 22:16:50 +02:00
|
|
|
{
|
2023-02-26 18:17:53 +02:00
|
|
|
assert(!hasOngoingAnimations());
|
2023-02-22 22:06:27 +02:00
|
|
|
|
2023-03-01 18:15:42 +02:00
|
|
|
if(isEventVisible(obj, from, dest))
|
2023-02-22 22:06:27 +02:00
|
|
|
{
|
2023-02-26 18:17:53 +02:00
|
|
|
setViewCenter(obj->getSightCenter());
|
2023-03-01 19:25:51 +02:00
|
|
|
view->createTransitionSnapshot(context);
|
2023-02-26 18:17:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MapViewController::onAfterHeroTeleported(const CGHeroInstance * obj, const int3 & from, const int3 & dest)
|
|
|
|
{
|
|
|
|
assert(!hasOngoingAnimations());
|
|
|
|
|
2023-03-15 00:31:51 +02:00
|
|
|
const CGObjectInstance * movingObject = obj;
|
|
|
|
if(obj->boat)
|
|
|
|
movingObject = obj->boat;
|
|
|
|
|
|
|
|
removeObject(movingObject);
|
|
|
|
addObject(movingObject);
|
2023-03-01 19:25:51 +02:00
|
|
|
|
2023-03-01 18:15:42 +02:00
|
|
|
if(isEventVisible(obj, from, dest))
|
2023-02-26 18:17:53 +02:00
|
|
|
{
|
2023-03-01 19:25:51 +02:00
|
|
|
teleportContext = std::make_shared<MapRendererAdventureTransitionContext>(*state);
|
|
|
|
teleportContext->animationTime = adventureContext->animationTime;
|
|
|
|
adventureContext = teleportContext;
|
|
|
|
context = teleportContext;
|
2023-03-15 00:31:51 +02:00
|
|
|
setViewCenter(movingObject->getSightCenter());
|
2023-02-22 22:06:27 +02:00
|
|
|
}
|
2023-02-20 22:16:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void MapViewController::onHeroMoved(const CGHeroInstance * obj, const int3 & from, const int3 & dest)
|
|
|
|
{
|
2023-02-26 18:17:53 +02:00
|
|
|
assert(!hasOngoingAnimations());
|
2023-02-20 22:16:50 +02:00
|
|
|
|
2023-02-28 22:43:19 +02:00
|
|
|
// revisiting via spacebar, no need to animate
|
2023-03-01 18:15:42 +02:00
|
|
|
if(from == dest)
|
2023-02-28 22:43:19 +02:00
|
|
|
return;
|
|
|
|
|
2023-02-20 22:16:50 +02:00
|
|
|
const CGObjectInstance * movingObject = obj;
|
|
|
|
if(obj->boat)
|
|
|
|
movingObject = obj->boat;
|
|
|
|
|
2023-02-26 18:17:53 +02:00
|
|
|
removeObject(movingObject);
|
2023-02-20 22:16:50 +02:00
|
|
|
|
2023-03-01 18:15:42 +02:00
|
|
|
if(!isEventVisible(obj, from, dest))
|
2023-02-20 22:16:50 +02:00
|
|
|
{
|
2023-02-26 18:17:53 +02:00
|
|
|
addObject(movingObject);
|
|
|
|
return;
|
2023-02-20 22:16:50 +02:00
|
|
|
}
|
2023-02-26 18:17:53 +02:00
|
|
|
|
2023-03-01 18:15:42 +02:00
|
|
|
double movementTime = LOCPLINT->playerID == obj->tempOwner ?
|
|
|
|
settings["adventure"]["heroMoveTime"].Float() :
|
2023-02-26 18:17:53 +02:00
|
|
|
settings["adventure"]["enemyMoveTime"].Float();
|
|
|
|
|
|
|
|
if(movementTime > 1)
|
2023-02-20 22:16:50 +02:00
|
|
|
{
|
2023-02-26 18:17:53 +02:00
|
|
|
movementContext = std::make_shared<MapRendererAdventureMovingContext>(*state);
|
|
|
|
movementContext->animationTime = adventureContext->animationTime;
|
|
|
|
adventureContext = movementContext;
|
|
|
|
context = movementContext;
|
2023-02-22 22:06:27 +02:00
|
|
|
|
2023-02-26 18:17:53 +02:00
|
|
|
state->addMovingObject(movingObject, from, dest);
|
2023-02-20 22:16:50 +02:00
|
|
|
|
2023-02-26 18:17:53 +02:00
|
|
|
movementContext->target = movingObject->id;
|
|
|
|
movementContext->tileFrom = from;
|
|
|
|
movementContext->tileDest = dest;
|
|
|
|
movementContext->progress = 0.0;
|
|
|
|
}
|
|
|
|
else // instant movement
|
|
|
|
{
|
|
|
|
addObject(movingObject);
|
|
|
|
setViewCenter(movingObject->visitablePos());
|
|
|
|
}
|
2023-02-20 22:16:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool MapViewController::hasOngoingAnimations()
|
|
|
|
{
|
2023-02-26 18:17:53 +02:00
|
|
|
if(movementContext)
|
2023-02-20 22:16:50 +02:00
|
|
|
return true;
|
|
|
|
|
2023-02-26 18:17:53 +02:00
|
|
|
if(fadingOutContext)
|
2023-02-20 22:16:50 +02:00
|
|
|
return true;
|
|
|
|
|
2023-02-26 18:17:53 +02:00
|
|
|
if(fadingInContext)
|
2023-02-20 22:16:50 +02:00
|
|
|
return true;
|
|
|
|
|
2023-03-02 16:08:02 +02:00
|
|
|
if(teleportContext)
|
2023-03-01 19:25:51 +02:00
|
|
|
return true;
|
|
|
|
|
2023-02-20 22:16:50 +02:00
|
|
|
return false;
|
|
|
|
}
|
2023-02-21 14:38:08 +02:00
|
|
|
|
2023-02-26 18:17:53 +02:00
|
|
|
void MapViewController::activateAdventureContext(uint32_t animationTime)
|
|
|
|
{
|
|
|
|
resetContext();
|
|
|
|
|
|
|
|
adventureContext = std::make_shared<MapRendererAdventureContext>(*state);
|
|
|
|
adventureContext->animationTime = animationTime;
|
|
|
|
context = adventureContext;
|
2023-07-07 00:27:57 +02:00
|
|
|
updateState();
|
2023-02-26 18:17:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void MapViewController::activateAdventureContext()
|
|
|
|
{
|
|
|
|
activateAdventureContext(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MapViewController::activateWorldViewContext()
|
|
|
|
{
|
2023-03-01 18:15:42 +02:00
|
|
|
if(worldViewContext)
|
2023-02-26 18:17:53 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
resetContext();
|
|
|
|
|
|
|
|
worldViewContext = std::make_shared<MapRendererWorldViewContext>(*state);
|
|
|
|
context = worldViewContext;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MapViewController::activateSpellViewContext()
|
|
|
|
{
|
2023-03-01 18:15:42 +02:00
|
|
|
if(spellViewContext)
|
2023-02-26 18:17:53 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
resetContext();
|
|
|
|
|
|
|
|
spellViewContext = std::make_shared<MapRendererSpellViewContext>(*state);
|
|
|
|
worldViewContext = spellViewContext;
|
|
|
|
context = spellViewContext;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MapViewController::activatePuzzleMapContext(const int3 & grailPosition)
|
|
|
|
{
|
|
|
|
resetContext();
|
|
|
|
|
|
|
|
puzzleMapContext = std::make_shared<MapRendererPuzzleMapContext>(*state);
|
|
|
|
context = puzzleMapContext;
|
|
|
|
|
|
|
|
CGPathNode fakeNode;
|
|
|
|
fakeNode.coord = grailPosition;
|
|
|
|
|
|
|
|
puzzleMapContext->grailPos = std::make_unique<CGPath>();
|
|
|
|
|
|
|
|
// create two nodes since 1st one is normally not visible
|
|
|
|
puzzleMapContext->grailPos->nodes.push_back(fakeNode);
|
|
|
|
puzzleMapContext->grailPos->nodes.push_back(fakeNode);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MapViewController::resetContext()
|
|
|
|
{
|
|
|
|
adventureContext.reset();
|
|
|
|
movementContext.reset();
|
|
|
|
fadingOutContext.reset();
|
|
|
|
fadingInContext.reset();
|
2023-03-01 19:25:51 +02:00
|
|
|
teleportContext.reset();
|
2023-02-26 18:17:53 +02:00
|
|
|
worldViewContext.reset();
|
|
|
|
spellViewContext.reset();
|
|
|
|
puzzleMapContext.reset();
|
|
|
|
}
|
|
|
|
|
2023-02-21 14:38:08 +02:00
|
|
|
void MapViewController::setTerrainVisibility(bool showAllTerrain)
|
|
|
|
{
|
2023-02-26 18:17:53 +02:00
|
|
|
assert(spellViewContext);
|
|
|
|
spellViewContext->showAllTerrain = showAllTerrain;
|
2023-02-21 14:38:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void MapViewController::setOverlayVisibility(const std::vector<ObjectPosInfo> & objectPositions)
|
|
|
|
{
|
2023-02-26 18:17:53 +02:00
|
|
|
assert(spellViewContext);
|
|
|
|
spellViewContext->additionalOverlayIcons = objectPositions;
|
2023-02-21 14:38:08 +02:00
|
|
|
}
|