2017-07-13 10:26:03 +02:00
|
|
|
/*
|
2023-02-01 16:42:03 +02:00
|
|
|
* CMinimap.cpp, part of VCMI engine
|
2017-07-13 10:26:03 +02:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*
|
|
|
|
*/
|
2012-06-13 16:04:06 +03:00
|
|
|
|
2023-02-01 20:42:06 +02:00
|
|
|
#include "StdInc.h"
|
|
|
|
#include "CMinimap.h"
|
2014-07-15 10:14:49 +03:00
|
|
|
|
2023-02-01 20:42:06 +02:00
|
|
|
#include "CAdvMapInt.h"
|
2023-02-10 16:26:32 +02:00
|
|
|
#include "CTerrainRect.h"
|
2014-07-13 20:53:37 +03:00
|
|
|
|
2023-02-01 20:42:06 +02:00
|
|
|
#include "../widgets/Images.h"
|
2014-07-13 20:53:37 +03:00
|
|
|
#include "../CGameInfo.h"
|
|
|
|
#include "../CPlayerInterface.h"
|
|
|
|
#include "../gui/CGuiHandler.h"
|
2023-02-02 21:15:13 +02:00
|
|
|
#include "../render/Colors.h"
|
2023-02-01 20:42:06 +02:00
|
|
|
#include "../renderSDL/SDL_PixelAccess.h"
|
2023-02-10 15:29:30 +02:00
|
|
|
#include "../windows/InfoWindows.h"
|
2014-07-13 20:53:37 +03:00
|
|
|
|
|
|
|
#include "../../CCallback.h"
|
|
|
|
#include "../../lib/CGeneralTextHandler.h"
|
2023-01-09 01:17:37 +02:00
|
|
|
#include "../../lib/TerrainHandler.h"
|
2014-07-13 20:53:37 +03:00
|
|
|
#include "../../lib/mapObjects/CGHeroInstance.h"
|
2023-02-01 20:42:06 +02:00
|
|
|
#include "../../lib/mapping/CMapDefines.h"
|
2012-06-13 16:04:06 +03:00
|
|
|
|
2023-02-10 15:29:30 +02:00
|
|
|
ColorRGBA CMinimapInstance::getTileColor(const int3 & pos) const
|
2012-06-13 16:04:06 +03:00
|
|
|
{
|
|
|
|
const TerrainTile * tile = LOCPLINT->cb->getTile(pos, false);
|
|
|
|
|
|
|
|
// if tile is not visible it will be black on minimap
|
|
|
|
if(!tile)
|
2023-02-10 15:29:30 +02:00
|
|
|
return CSDL_Ext::fromSDL(Colors::BLACK);
|
2012-06-13 16:04:06 +03:00
|
|
|
|
|
|
|
// if object at tile is owned - it will be colored as its owner
|
2014-10-01 15:19:03 +03:00
|
|
|
for (const CGObjectInstance *obj : tile->blockingObjects)
|
2012-06-13 16:04:06 +03:00
|
|
|
{
|
2013-03-03 20:06:03 +03:00
|
|
|
PlayerColor player = obj->getOwner();
|
|
|
|
if(player == PlayerColor::NEUTRAL)
|
2023-02-10 15:29:30 +02:00
|
|
|
return CSDL_Ext::fromSDL(*graphics->neutralColor);
|
|
|
|
|
2013-03-03 20:06:03 +03:00
|
|
|
if (player < PlayerColor::PLAYER_LIMIT)
|
2023-02-10 15:29:30 +02:00
|
|
|
return CSDL_Ext::fromSDL(graphics->playerColors[player.getNum()]);
|
2012-06-13 16:04:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (tile->blocked && (!tile->visitable))
|
2023-02-10 15:29:30 +02:00
|
|
|
return tile->terType->minimapBlocked;
|
2012-06-13 16:04:06 +03:00
|
|
|
else
|
2023-02-10 15:29:30 +02:00
|
|
|
return tile->terType->minimapUnblocked;
|
2012-06-13 16:04:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void CMinimapInstance::refreshTile(const int3 &tile)
|
|
|
|
{
|
2023-02-10 15:29:30 +02:00
|
|
|
if (level == tile.z)
|
|
|
|
minimap->drawPoint(Point(tile.x, tile.y), getTileColor(tile));
|
2012-06-13 16:04:06 +03:00
|
|
|
}
|
|
|
|
|
2023-02-10 15:29:30 +02:00
|
|
|
void CMinimapInstance::redrawMinimap()
|
2012-06-13 16:04:06 +03:00
|
|
|
{
|
|
|
|
int3 mapSizes = LOCPLINT->cb->getMapSize();
|
|
|
|
|
2023-02-10 15:29:30 +02:00
|
|
|
for (int y = 0; y < mapSizes.y; ++y)
|
|
|
|
for (int x = 0; x < mapSizes.x; ++x)
|
|
|
|
minimap->drawPoint(Point(x, y), getTileColor(int3(x, y, level)));
|
2012-06-13 16:04:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
CMinimapInstance::CMinimapInstance(CMinimap *Parent, int Level):
|
2015-01-13 21:57:41 +02:00
|
|
|
parent(Parent),
|
2023-02-10 15:29:30 +02:00
|
|
|
minimap(new Canvas(Point(LOCPLINT->cb->getMapSize().x, LOCPLINT->cb->getMapSize().y))),
|
2015-01-13 21:57:41 +02:00
|
|
|
level(Level)
|
2012-06-13 16:04:06 +03:00
|
|
|
{
|
|
|
|
pos.w = parent->pos.w;
|
|
|
|
pos.h = parent->pos.h;
|
2023-02-10 15:29:30 +02:00
|
|
|
redrawMinimap();
|
2012-06-13 16:04:06 +03:00
|
|
|
}
|
|
|
|
|
2018-04-07 13:34:11 +02:00
|
|
|
void CMinimapInstance::showAll(SDL_Surface * to)
|
2012-06-13 16:04:06 +03:00
|
|
|
{
|
2023-02-10 15:29:30 +02:00
|
|
|
Canvas target(to);
|
|
|
|
target.draw(*minimap, pos.topLeft(), pos.dimensions());
|
2012-06-13 16:04:06 +03:00
|
|
|
}
|
|
|
|
|
2018-04-07 13:34:11 +02:00
|
|
|
CMinimap::CMinimap(const Rect & position)
|
|
|
|
: CIntObject(LCLICK | RCLICK | HOVER | MOVE, position.topLeft()),
|
2023-02-10 15:29:30 +02:00
|
|
|
level(0)
|
2012-06-13 16:04:06 +03:00
|
|
|
{
|
2018-04-07 13:34:11 +02:00
|
|
|
OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
|
2012-06-13 16:04:06 +03:00
|
|
|
pos.w = position.w;
|
|
|
|
pos.h = position.h;
|
2018-04-07 13:34:11 +02:00
|
|
|
|
|
|
|
aiShield = std::make_shared<CPicture>("AIShield");
|
|
|
|
aiShield->disable();
|
2012-06-13 16:04:06 +03:00
|
|
|
}
|
|
|
|
|
2023-02-10 15:29:30 +02:00
|
|
|
int3 CMinimap::pixelToTile(const Point & cursorPos) const
|
2012-06-13 16:04:06 +03:00
|
|
|
{
|
|
|
|
// 0 = top-left corner, 1 = bottom-right corner
|
2023-02-10 15:29:30 +02:00
|
|
|
double dx = static_cast<double>(cursorPos.x) / pos.w;
|
|
|
|
double dy = static_cast<double>(cursorPos.y) / pos.h;
|
|
|
|
|
|
|
|
int3 mapSizes = LOCPLINT->cb->getMapSize();
|
|
|
|
|
|
|
|
int tileX(std::round(mapSizes.x * dx));
|
|
|
|
int tileY(std::round(mapSizes.y * dy));
|
|
|
|
|
|
|
|
return int3(tileX, tileY, level);
|
|
|
|
}
|
2012-06-13 16:04:06 +03:00
|
|
|
|
2023-02-10 15:29:30 +02:00
|
|
|
Point CMinimap::tileToPixels(const int3 &tile) const
|
|
|
|
{
|
2012-06-13 16:04:06 +03:00
|
|
|
int3 mapSizes = LOCPLINT->cb->getMapSize();
|
|
|
|
|
2023-02-10 15:29:30 +02:00
|
|
|
double stepX = static_cast<double>(pos.w) / mapSizes.x;
|
|
|
|
double stepY = static_cast<double>(pos.h) / mapSizes.y;
|
|
|
|
|
|
|
|
int x = static_cast<int>(stepX * tile.x);
|
|
|
|
int y = static_cast<int>(stepY * tile.y);
|
|
|
|
|
|
|
|
return Point(x,y);
|
2012-07-08 19:36:20 +03:00
|
|
|
}
|
2012-06-13 16:04:06 +03:00
|
|
|
|
2012-07-08 19:36:20 +03:00
|
|
|
void CMinimap::moveAdvMapSelection()
|
|
|
|
{
|
2023-02-10 15:29:30 +02:00
|
|
|
int3 newLocation = pixelToTile(GH.getCursorPosition() - pos.topLeft());
|
2012-06-13 16:04:06 +03:00
|
|
|
adventureInt->centerOn(newLocation);
|
|
|
|
|
2012-08-07 14:28:52 +03:00
|
|
|
if (!(adventureInt->active & GENERAL))
|
|
|
|
GH.totalRedraw(); //redraw this as well as inactive adventure map
|
|
|
|
else
|
|
|
|
redraw();//redraw only this
|
2012-06-13 16:04:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void CMinimap::clickLeft(tribool down, bool previousState)
|
|
|
|
{
|
2018-04-07 13:34:11 +02:00
|
|
|
if(down)
|
2012-06-13 16:04:06 +03:00
|
|
|
moveAdvMapSelection();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CMinimap::clickRight(tribool down, bool previousState)
|
|
|
|
{
|
2023-02-10 15:29:30 +02:00
|
|
|
if (down)
|
|
|
|
CRClickPopup::createAndPush(CGI->generaltexth->zelp[291].second);
|
2012-06-13 16:04:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void CMinimap::hover(bool on)
|
|
|
|
{
|
2018-04-07 13:34:11 +02:00
|
|
|
if(on)
|
2022-11-18 17:54:10 +02:00
|
|
|
GH.statusbar->write(CGI->generaltexth->zelp[291].first);
|
2012-06-13 16:04:06 +03:00
|
|
|
else
|
|
|
|
GH.statusbar->clear();
|
|
|
|
}
|
|
|
|
|
2023-02-02 15:49:23 +02:00
|
|
|
void CMinimap::mouseMoved(const Point & cursorPosition)
|
2012-06-13 16:04:06 +03:00
|
|
|
{
|
2023-02-02 16:22:19 +02:00
|
|
|
if(mouseState(MouseButton::LEFT))
|
2012-06-13 16:04:06 +03:00
|
|
|
moveAdvMapSelection();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CMinimap::showAll(SDL_Surface * to)
|
|
|
|
{
|
|
|
|
CIntObject::showAll(to);
|
2018-04-07 13:34:11 +02:00
|
|
|
if(minimap)
|
2012-06-13 16:04:06 +03:00
|
|
|
{
|
2023-02-10 15:29:30 +02:00
|
|
|
Canvas target(to);
|
|
|
|
|
2012-06-13 16:04:06 +03:00
|
|
|
int3 mapSizes = LOCPLINT->cb->getMapSize();
|
2023-02-10 23:29:13 +02:00
|
|
|
Rect screenArea = adventureInt->terrainAreaTiles();
|
2012-06-13 16:04:06 +03:00
|
|
|
|
|
|
|
//draw radar
|
2023-01-17 22:01:35 +02:00
|
|
|
Rect radar =
|
2012-06-13 16:04:06 +03:00
|
|
|
{
|
2023-02-10 23:29:13 +02:00
|
|
|
screenArea.x * pos.w / mapSizes.x,
|
|
|
|
screenArea.y * pos.h / mapSizes.y,
|
|
|
|
screenArea.w * pos.w / mapSizes.x - 1,
|
|
|
|
screenArea.h * pos.h / mapSizes.y - 1
|
2012-06-13 16:04:06 +03:00
|
|
|
};
|
|
|
|
|
2023-02-10 15:29:30 +02:00
|
|
|
Canvas clippedTarget(target, pos);
|
|
|
|
clippedTarget.drawBorderDashed(radar, CSDL_Ext::fromSDL(Colors::PURPLE));
|
2012-06-13 16:04:06 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CMinimap::update()
|
|
|
|
{
|
2018-04-07 13:34:11 +02:00
|
|
|
if(aiShield->recActions & UPDATE) //AI turn is going on. There is no need to update minimap
|
2012-06-13 16:04:06 +03:00
|
|
|
return;
|
|
|
|
|
2018-04-07 13:34:11 +02:00
|
|
|
OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255-DISPOSE);
|
|
|
|
minimap = std::make_shared<CMinimapInstance>(this, level);
|
2012-06-15 20:08:19 +03:00
|
|
|
redraw();
|
2012-06-13 16:04:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void CMinimap::setLevel(int newLevel)
|
|
|
|
{
|
2023-02-10 15:29:30 +02:00
|
|
|
if (level == newLevel)
|
|
|
|
return;
|
|
|
|
|
2012-06-13 16:04:06 +03:00
|
|
|
level = newLevel;
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CMinimap::setAIRadar(bool on)
|
|
|
|
{
|
2018-04-07 13:34:11 +02:00
|
|
|
if(on)
|
2012-06-13 16:04:06 +03:00
|
|
|
{
|
2018-04-07 13:34:11 +02:00
|
|
|
aiShield->enable();
|
|
|
|
minimap.reset();
|
2012-06-13 16:04:06 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-04-07 13:34:11 +02:00
|
|
|
aiShield->disable();
|
2012-06-13 16:04:06 +03:00
|
|
|
update();
|
|
|
|
}
|
2023-02-10 15:29:30 +02:00
|
|
|
|
|
|
|
// this may happen during AI turn when this interface is inactive
|
2013-12-19 20:29:35 +03:00
|
|
|
// force redraw in order to properly update interface
|
|
|
|
GH.totalRedraw();
|
2012-06-13 16:04:06 +03:00
|
|
|
}
|
|
|
|
|
2023-02-10 15:29:30 +02:00
|
|
|
void CMinimap::updateTile(const int3 &pos)
|
2012-06-13 16:04:06 +03:00
|
|
|
{
|
2018-04-07 13:34:11 +02:00
|
|
|
if(minimap)
|
2012-06-13 16:04:06 +03:00
|
|
|
minimap->refreshTile(pos);
|
|
|
|
}
|
|
|
|
|