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"
|
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-01 20:42:06 +02:00
|
|
|
#include "../renderSDL/SDL_PixelAccess.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-01-30 19:55:32 +02:00
|
|
|
#include <SDL_surface.h>
|
|
|
|
|
2012-06-13 16:04:06 +03:00
|
|
|
const SDL_Color & CMinimapInstance::getTileColor(const int3 & pos)
|
|
|
|
{
|
|
|
|
const TerrainTile * tile = LOCPLINT->cb->getTile(pos, false);
|
|
|
|
|
|
|
|
// if tile is not visible it will be black on minimap
|
|
|
|
if(!tile)
|
2023-01-30 00:12:43 +02:00
|
|
|
return 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
|
|
|
{
|
|
|
|
//heroes will be blitted later
|
2014-10-01 15:19:03 +03:00
|
|
|
switch (obj->ID)
|
|
|
|
{
|
|
|
|
case Obj::HERO:
|
|
|
|
case Obj::PRISON:
|
|
|
|
continue;
|
|
|
|
}
|
2012-06-13 16:04:06 +03:00
|
|
|
|
2013-03-03 20:06:03 +03:00
|
|
|
PlayerColor player = obj->getOwner();
|
|
|
|
if(player == PlayerColor::NEUTRAL)
|
2012-06-13 16:04:06 +03:00
|
|
|
return *graphics->neutralColor;
|
|
|
|
else
|
2013-03-03 20:06:03 +03:00
|
|
|
if (player < PlayerColor::PLAYER_LIMIT)
|
|
|
|
return graphics->playerColors[player.getNum()];
|
2012-06-13 16:04:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// else - use terrain color (blocked version or normal)
|
2023-01-01 17:10:47 +02:00
|
|
|
const auto & colorPair = parent->colors.find(tile->terType->getId())->second;
|
2012-06-13 16:04:06 +03:00
|
|
|
if (tile->blocked && (!tile->visitable))
|
2022-09-21 11:34:23 +02:00
|
|
|
return colorPair.second;
|
2012-06-13 16:04:06 +03:00
|
|
|
else
|
2022-09-21 11:34:23 +02:00
|
|
|
return colorPair.first;
|
2012-06-13 16:04:06 +03:00
|
|
|
}
|
2012-07-08 19:36:20 +03:00
|
|
|
void CMinimapInstance::tileToPixels (const int3 &tile, int &x, int &y, int toX, int toY)
|
2012-06-13 16:04:06 +03:00
|
|
|
{
|
|
|
|
int3 mapSizes = LOCPLINT->cb->getMapSize();
|
|
|
|
|
|
|
|
double stepX = double(pos.w) / mapSizes.x;
|
|
|
|
double stepY = double(pos.h) / mapSizes.y;
|
|
|
|
|
2020-10-01 10:38:06 +02:00
|
|
|
x = static_cast<int>(toX + stepX * tile.x);
|
|
|
|
y = static_cast<int>(toY + stepY * tile.y);
|
2012-07-08 19:36:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void CMinimapInstance::blitTileWithColor(const SDL_Color &color, const int3 &tile, SDL_Surface *to, int toX, int toY)
|
|
|
|
{
|
2012-06-13 16:04:06 +03:00
|
|
|
//coordinates of rectangle on minimap representing this tile
|
|
|
|
// begin - first to blit, end - first NOT to blit
|
2012-07-08 19:36:20 +03:00
|
|
|
int xBegin, yBegin, xEnd, yEnd;
|
|
|
|
tileToPixels (tile, xBegin, yBegin, toX, toY);
|
|
|
|
tileToPixels (int3 (tile.x + 1, tile.y + 1, tile.z), xEnd, yEnd, toX, toY);
|
2012-06-13 16:04:06 +03:00
|
|
|
|
|
|
|
for (int y=yBegin; y<yEnd; y++)
|
|
|
|
{
|
2023-01-30 19:55:32 +02:00
|
|
|
uint8_t *ptr = (uint8_t*)to->pixels + y * to->pitch + xBegin * minimap->format->BytesPerPixel;
|
2012-06-13 16:04:06 +03:00
|
|
|
|
|
|
|
for (int x=xBegin; x<xEnd; x++)
|
|
|
|
ColorPutter<4, 1>::PutColor(ptr, color);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CMinimapInstance::refreshTile(const int3 &tile)
|
|
|
|
{
|
|
|
|
blitTileWithColor(getTileColor(int3(tile.x, tile.y, level)), tile, minimap, 0, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CMinimapInstance::drawScaled(int level)
|
|
|
|
{
|
|
|
|
int3 mapSizes = LOCPLINT->cb->getMapSize();
|
|
|
|
|
|
|
|
//size of one map tile on our minimap
|
|
|
|
double stepX = double(pos.w) / mapSizes.x;
|
|
|
|
double stepY = double(pos.h) / mapSizes.y;
|
|
|
|
|
|
|
|
double currY = 0;
|
|
|
|
for (int y=0; y<mapSizes.y; y++, currY += stepY)
|
|
|
|
{
|
|
|
|
double currX = 0;
|
|
|
|
for (int x=0; x<mapSizes.x; x++, currX += stepX)
|
|
|
|
{
|
|
|
|
const SDL_Color &color = getTileColor(int3(x,y,level));
|
|
|
|
|
|
|
|
//coordinates of rectangle on minimap representing this tile
|
|
|
|
// begin - first to blit, end - first NOT to blit
|
2020-10-01 10:38:06 +02:00
|
|
|
int xBegin = static_cast<int>(currX);
|
|
|
|
int yBegin = static_cast<int>(currY);
|
|
|
|
int xEnd = static_cast<int>(currX + stepX);
|
|
|
|
int yEnd = static_cast<int>(currY + stepY);
|
2012-06-13 16:04:06 +03:00
|
|
|
|
|
|
|
for (int y=yBegin; y<yEnd; y++)
|
|
|
|
{
|
2023-01-30 19:55:32 +02:00
|
|
|
uint8_t *ptr = (uint8_t*)minimap->pixels + y * minimap->pitch + xBegin * minimap->format->BytesPerPixel;
|
2012-06-13 16:04:06 +03:00
|
|
|
|
|
|
|
for (int x=xBegin; x<xEnd; x++)
|
|
|
|
ColorPutter<4, 1>::PutColor(ptr, color);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CMinimapInstance::CMinimapInstance(CMinimap *Parent, int Level):
|
2015-01-13 21:57:41 +02:00
|
|
|
parent(Parent),
|
|
|
|
minimap(CSDL_Ext::createSurfaceWithBpp<4>(parent->pos.w, parent->pos.h)),
|
|
|
|
level(Level)
|
2012-06-13 16:04:06 +03:00
|
|
|
{
|
|
|
|
pos.w = parent->pos.w;
|
|
|
|
pos.h = parent->pos.h;
|
|
|
|
drawScaled(level);
|
|
|
|
}
|
|
|
|
|
|
|
|
CMinimapInstance::~CMinimapInstance()
|
|
|
|
{
|
|
|
|
SDL_FreeSurface(minimap);
|
|
|
|
}
|
|
|
|
|
2018-04-07 13:34:11 +02:00
|
|
|
void CMinimapInstance::showAll(SDL_Surface * to)
|
2012-06-13 16:04:06 +03:00
|
|
|
{
|
|
|
|
blitAtLoc(minimap, 0, 0, to);
|
|
|
|
|
|
|
|
//draw heroes
|
2015-01-13 21:57:41 +02:00
|
|
|
std::vector <const CGHeroInstance *> heroes = LOCPLINT->cb->getHeroesInfo(false); //TODO: do we really need separate function for drawing heroes?
|
2013-06-29 16:05:48 +03:00
|
|
|
for(auto & hero : heroes)
|
2012-06-13 16:04:06 +03:00
|
|
|
{
|
2022-12-07 22:10:08 +02:00
|
|
|
int3 position = hero->visitablePos();
|
2018-04-07 13:34:11 +02:00
|
|
|
if(position.z == level)
|
2012-06-13 16:04:06 +03:00
|
|
|
{
|
2013-03-03 20:06:03 +03:00
|
|
|
const SDL_Color & color = graphics->playerColors[hero->getOwner().getNum()];
|
2012-06-13 16:04:06 +03:00
|
|
|
blitTileWithColor(color, position, to, pos.x, pos.y);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-29 11:44:46 +02:00
|
|
|
std::map<TerrainId, std::pair<SDL_Color, SDL_Color> > CMinimap::loadColors()
|
2012-06-13 16:04:06 +03:00
|
|
|
{
|
2022-09-29 11:44:46 +02:00
|
|
|
std::map<TerrainId, std::pair<SDL_Color, SDL_Color> > ret;
|
2012-06-13 16:04:06 +03:00
|
|
|
|
2022-12-20 16:14:06 +02:00
|
|
|
for(const auto & terrain : CGI->terrainTypeHandler->objects)
|
2012-06-13 16:04:06 +03:00
|
|
|
{
|
2023-01-30 00:12:43 +02:00
|
|
|
SDL_Color normal = CSDL_Ext::toSDL(terrain->minimapUnblocked);
|
|
|
|
SDL_Color blocked = CSDL_Ext::toSDL(terrain->minimapBlocked);
|
2012-06-13 16:04:06 +03:00
|
|
|
|
2023-01-01 17:10:47 +02:00
|
|
|
ret[terrain->getId()] = std::make_pair(normal, blocked);
|
2012-06-13 16:04:06 +03:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-04-07 13:34:11 +02:00
|
|
|
CMinimap::CMinimap(const Rect & position)
|
|
|
|
: CIntObject(LCLICK | RCLICK | HOVER | MOVE, position.topLeft()),
|
2015-01-13 21:57:41 +02:00
|
|
|
level(0),
|
2022-06-20 16:39:50 +02:00
|
|
|
colors(loadColors())
|
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
|
|
|
}
|
|
|
|
|
2012-07-08 19:36:20 +03:00
|
|
|
int3 CMinimap::translateMousePosition()
|
2012-06-13 16:04:06 +03:00
|
|
|
{
|
|
|
|
// 0 = top-left corner, 1 = bottom-right corner
|
2023-01-27 00:27:06 +02:00
|
|
|
double dx = double(GH.getCursorPosition().x - pos.x) / pos.w;
|
|
|
|
double dy = double(GH.getCursorPosition().y - pos.y) / pos.h;
|
2012-06-13 16:04:06 +03:00
|
|
|
|
|
|
|
int3 mapSizes = LOCPLINT->cb->getMapSize();
|
|
|
|
|
2020-10-01 10:38:06 +02:00
|
|
|
int3 tile ((si32)(mapSizes.x * dx), (si32)(mapSizes.y * dy), level);
|
2012-07-08 19:36:20 +03:00
|
|
|
return tile;
|
|
|
|
}
|
2012-06-13 16:04:06 +03:00
|
|
|
|
2012-07-08 19:36:20 +03:00
|
|
|
void CMinimap::moveAdvMapSelection()
|
|
|
|
{
|
|
|
|
int3 newLocation = translateMousePosition();
|
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)
|
|
|
|
{
|
|
|
|
adventureInt->handleRightClick(CGI->generaltexth->zelp[291].second, down);
|
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CMinimap::mouseMoved(const SDL_MouseMotionEvent & sEvent)
|
|
|
|
{
|
2017-06-08 21:07:09 +02:00
|
|
|
if(mouseState(EIntObjMouseBtnType::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
|
|
|
{
|
|
|
|
int3 mapSizes = LOCPLINT->cb->getMapSize();
|
2015-01-13 21:57:41 +02:00
|
|
|
int3 tileCountOnScreen = adventureInt->terrain.tileCountOnScreen();
|
2012-06-13 16:04:06 +03:00
|
|
|
|
|
|
|
//draw radar
|
2023-01-17 22:01:35 +02:00
|
|
|
Rect oldClip;
|
|
|
|
Rect radar =
|
2012-06-13 16:04:06 +03:00
|
|
|
{
|
|
|
|
si16(adventureInt->position.x * pos.w / mapSizes.x + pos.x),
|
|
|
|
si16(adventureInt->position.y * pos.h / mapSizes.y + pos.y),
|
2015-01-13 21:57:41 +02:00
|
|
|
ui16(tileCountOnScreen.x * pos.w / mapSizes.x),
|
|
|
|
ui16(tileCountOnScreen.y * pos.h / mapSizes.y)
|
2012-06-13 16:04:06 +03:00
|
|
|
};
|
|
|
|
|
2018-04-07 13:34:11 +02:00
|
|
|
if(adventureInt->mode == EAdvMapMode::WORLD_VIEW)
|
2015-01-17 14:41:59 +02:00
|
|
|
{
|
|
|
|
// adjusts radar so that it doesn't go out of map in world view mode (since there's no frame)
|
2015-02-01 20:25:42 +02:00
|
|
|
radar.x = std::min<int>(std::max(pos.x, radar.x), pos.x + pos.w - radar.w);
|
|
|
|
radar.y = std::min<int>(std::max(pos.y, radar.y), pos.y + pos.h - radar.h);
|
2015-01-17 14:41:59 +02:00
|
|
|
|
2018-04-07 13:34:11 +02:00
|
|
|
if(radar.x < pos.x && radar.y < pos.y)
|
2015-01-17 14:41:59 +02:00
|
|
|
return; // whole map is visible at once, no point in redrawing border
|
|
|
|
}
|
|
|
|
|
2023-01-17 22:01:35 +02:00
|
|
|
CSDL_Ext::getClipRect(to, oldClip);
|
|
|
|
CSDL_Ext::setClipRect(to, pos);
|
|
|
|
CSDL_Ext::drawDashedBorder(to, radar, Colors::PURPLE);
|
|
|
|
CSDL_Ext::setClipRect(to, oldClip);
|
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)
|
|
|
|
{
|
|
|
|
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();
|
|
|
|
}
|
2013-12-19 20:29:35 +03:00
|
|
|
// this my happen during AI turn when this interface is inactive
|
|
|
|
// force redraw in order to properly update interface
|
|
|
|
GH.totalRedraw();
|
2012-06-13 16:04:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void CMinimap::hideTile(const int3 &pos)
|
|
|
|
{
|
2018-04-07 13:34:11 +02:00
|
|
|
if(minimap)
|
2012-06-13 16:04:06 +03:00
|
|
|
minimap->refreshTile(pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CMinimap::showTile(const int3 &pos)
|
|
|
|
{
|
2018-04-07 13:34:11 +02:00
|
|
|
if(minimap)
|
2012-06-13 16:04:06 +03:00
|
|
|
minimap->refreshTile(pos);
|
|
|
|
}
|
|
|
|
|