1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-30 23:18:08 +02:00
vcmi/client/mapHandler.cpp

1489 lines
45 KiB
C++
Raw Normal View History

/*
* mapHandler.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 "mapHandler.h"
#include "CBitmapHandler.h"
#include "gui/CAnimation.h"
#include "gui/SDL_Extensions.h"
2010-04-06 11:59:24 +03:00
#include "CGameInfo.h"
#include "../lib/mapObjects/CGHeroInstance.h"
#include "../lib/mapObjects/CObjectClassesHandler.h"
2010-04-06 11:59:24 +03:00
#include "../lib/CGameState.h"
#include "../lib/CHeroHandler.h"
#include "../lib/CTownHandler.h"
#include "../lib/CModHandler.h"
2010-04-06 11:59:24 +03:00
#include "Graphics.h"
2013-04-07 13:48:07 +03:00
#include "../lib/mapping/CMap.h"
#include "../lib/CConfigHandler.h"
#include "../lib/CGeneralTextHandler.h"
#include "../lib/GameConstants.h"
#include "../lib/CStopWatch.h"
#include "CMT.h"
#include "CMusicHandler.h"
#include "../lib/CRandomGenerator.h"
#include "../lib/Terrain.h"
#include "../lib/filesystem/ResourceID.h"
#include "../lib/JsonDetail.h"
2009-08-31 18:57:15 +03:00
#define ADVOPT (conf.go()->ac)
2015-01-28 21:07:57 +02:00
static bool objectBlitOrderSorter(const TerrainTileObject & a, const TerrainTileObject & b)
{
2015-01-28 21:07:57 +02:00
return CMapHandler::compareObjectBlitOrder(a.obj, b.obj);
}
2014-07-03 19:41:42 +03:00
struct NeighborTilesInfo
{
bool d7, //789
d8, //456
d9, //123
d4,
d5,
d6,
d1,
d2,
d3;
NeighborTilesInfo(const int3 & pos, const int3 & sizes, std::shared_ptr<const boost::multi_array<ui8, 3>> visibilityMap)
2014-07-03 19:41:42 +03:00
{
auto getTile = [&](int dx, int dy)->bool
{
if ( dx + pos.x < 0 || dx + pos.x >= sizes.x
|| dy + pos.y < 0 || dy + pos.y >= sizes.y)
return false;
//FIXME: please do not read settings for every tile...
return settings["session"]["spectate"].Bool() ? true : (*visibilityMap)[pos.z][dx+pos.x][dy+pos.y];
2014-07-03 19:41:42 +03:00
};
d7 = getTile(-1, -1); //789
d8 = getTile( 0, -1); //456
d9 = getTile(+1, -1); //123
d4 = getTile(-1, 0);
d5 = (*visibilityMap)[pos.z][pos.x][pos.y];
2014-07-03 19:41:42 +03:00
d6 = getTile(+1, 0);
d1 = getTile(-1, +1);
d2 = getTile( 0, +1);
d3 = getTile(+1, +1);
}
2015-01-13 21:57:41 +02:00
bool areAllHidden() const
2014-07-03 19:41:42 +03:00
{
return !(d1 || d2 || d3 || d4 || d5 || d6 || d7 || d8 || d9);
2014-07-03 19:41:42 +03:00
}
2015-01-13 21:57:41 +02:00
2014-07-03 19:41:42 +03:00
int getBitmapID() const
{
//NOTE: some images have unused in VCMI pair (same blockmap but a bit different look)
// 0-1, 2-3, 4-5, 11-13, 12-14
static const int visBitmaps[256] = {
-1, 34, 4, 4, 22, 23, 4, 4, 36, 36, 38, 38, 47, 47, 38, 38, //16
3, 25, 12, 12, 3, 25, 12, 12, 9, 9, 6, 6, 9, 9, 6, 6, //32
35, 39, 48, 48, 41, 43, 48, 48, 36, 36, 38, 38, 47, 47, 38, 38, //48
26, 49, 28, 28, 26, 49, 28, 28, 9, 9, 6, 6, 9, 9, 6, 6, //64
0, 45, 29, 29, 24, 33, 29, 29, 37, 37, 7, 7, 50, 50, 7, 7, //80
13, 27, 44, 44, 13, 27, 44, 44, 8, 8, 10, 10, 8, 8, 10, 10, //96
0, 45, 29, 29, 24, 33, 29, 29, 37, 37, 7, 7, 50, 50, 7, 7, //112
13, 27, 44, 44, 13, 27, 44, 44, 8, 8, 10, 10, 8, 8, 10, 10, //128
15, 17, 30, 30, 16, 19, 30, 30, 46, 46, 40, 40, 32, 32, 40, 40, //144
2, 25, 12, 12, 2, 25, 12, 12, 9, 9, 6, 6, 9, 9, 6, 6, //160
18, 42, 31, 31, 20, 21, 31, 31, 46, 46, 40, 40, 32, 32, 40, 40, //176
26, 49, 28, 28, 26, 49, 28, 28, 9, 9, 6, 6, 9, 9, 6, 6, //192
0, 45, 29, 29, 24, 33, 29, 29, 37, 37, 7, 7, 50, 50, 7, 7, //208
13, 27, 44, 44, 13, 27, 44, 44, 8, 8, 10, 10, 8, 8, 10, 10, //224
0, 45, 29, 29, 24, 33, 29, 29, 37, 37, 7, 7, 50, 50, 7, 7, //240
13, 27, 44, 44, 13, 27, 44, 44, 8, 8, 10, 10, 8, 8, 10, 10 //256
2015-01-13 21:57:41 +02:00
};
return visBitmaps[d1 + d2 * 2 + d3 * 4 + d4 * 8 + d6 * 16 + d7 * 32 + d8 * 64 + d9 * 128]; // >=0 -> partial hide, <0 - full hide
2014-07-03 19:41:42 +03:00
}
};
void CMapHandler::prepareFOWDefs()
{
2016-11-07 23:19:53 +02:00
//assume all frames in group 0
size_t size = graphics->fogOfWarFullHide->size(0);
FoWfullHide.resize(size);
for(size_t frame = 0; frame < size; frame++)
FoWfullHide[frame] = graphics->fogOfWarFullHide->getImage(frame);
//initialization of type of full-hide image
hideBitmap.resize(boost::extents[sizes.z][sizes.x][sizes.y]);
for (int i = 0; i < hideBitmap.num_elements(); i++)
hideBitmap.data()[i] = CRandomGenerator::getDefault().nextInt(size - 1);
2016-11-07 23:19:53 +02:00
size = graphics->fogOfWarPartialHide->size(0);
FoWpartialHide.resize(size);
for(size_t frame = 0; frame < size; frame++)
FoWpartialHide[frame] = graphics->fogOfWarPartialHide->getImage(frame);
}
EMapAnimRedrawStatus CMapHandler::drawTerrainRectNew(SDL_Surface * targetSurface, const MapDrawingInfo * info, bool redrawOnlyAnim)
{
assert(info);
bool hasActiveFade = updateObjectsFade();
resolveBlitter(info)->blit(targetSurface, info);
return hasActiveFade ? EMapAnimRedrawStatus::REDRAW_REQUESTED : EMapAnimRedrawStatus::OK;
}
2016-11-07 23:19:53 +02:00
void CMapHandler::initTerrainGraphics()
{
auto loadFlipped = [](TFlippedAnimations & animation, TFlippedCache & cache, const std::map<std::string, std::string> & files)
2016-11-07 23:19:53 +02:00
{
//no rotation and basic setup
for(auto & type : files)
{
animation[type.first][0] = make_unique<CAnimation>(type.second);
animation[type.first][0]->preload();
const size_t views = animation[type.first][0]->size(0);
cache[type.first].resize(views);
2016-11-07 23:19:53 +02:00
for(int j = 0; j < views; j++)
cache[type.first][j][0] = animation[type.first][0]->getImage(j);
}
2016-11-07 23:19:53 +02:00
for(int rotation = 1; rotation < 4; rotation++)
{
for(auto & type : files)
2016-11-07 23:19:53 +02:00
{
animation[type.first][rotation] = make_unique<CAnimation>(type.second);
animation[type.first][rotation]->preload();
const size_t views = animation[type.first][rotation]->size(0);
2016-11-07 23:19:53 +02:00
for(int j = 0; j < views; j++)
{
auto image = animation[type.first][rotation]->getImage(j);
2016-11-07 23:19:53 +02:00
if(rotation == 2 || rotation == 3)
image->horizontalFlip();
if(rotation == 1 || rotation == 3)
image->verticalFlip();
cache[type.first][j][rotation] = image;
2016-11-07 23:19:53 +02:00
}
}
}
2016-11-07 23:19:53 +02:00
};
std::map<std::string, std::string> terrainFiles;
std::map<std::string, std::string> riverFiles;
std::map<std::string, std::string> roadFiles;
for(const auto & terrain : VLC->terrainTypeHandler->terrains())
{
terrainFiles[terrain.name] = terrain.tilesFilename;
}
for(const auto & river : VLC->terrainTypeHandler->rivers())
{
riverFiles[river.fileName] = river.fileName;
}
2022-09-26 10:20:39 +02:00
for(const auto & road : VLC->terrainTypeHandler->roads())
{
2022-09-26 10:20:39 +02:00
roadFiles[road.fileName] = road.fileName;
}
loadFlipped(terrainAnimations, terrainImages, terrainFiles);
loadFlipped(riverAnimations, riverImages, riverFiles);
loadFlipped(roadAnimations, roadImages, roadFiles);
// Create enough room for the whole map and its frame
//ttiles.resize(sizes.x, frameW, frameW);
//FIXME: why do we even handle array with z (surface and undeground) at the same time?
ttiles.resize(boost::extents[sizes.z][sizes.x + 2 * frameW][sizes.y + 2 * frameH]);
ttiles.reindex(std::list<int>{ 0, -frameW, -frameH }); //need to move starting coordinates so that used index is always positive
}
2016-11-07 23:19:53 +02:00
void CMapHandler::initBorderGraphics()
{
2016-11-07 23:19:53 +02:00
egdeImages.resize(egdeAnimation->size(0));
for(size_t i = 0; i < egdeImages.size(); i++)
egdeImages[i] = egdeAnimation->getImage(i);
edgeFrames.resize(sizes.x, frameW, frameW);
for (int i=0-frameW;i<edgeFrames.size()-frameW;i++)
{
edgeFrames[i].resize(sizes.y, frameH, frameH);
}
for (int i=0-frameW;i<edgeFrames.size()-frameW;i++)
{
2016-11-07 23:19:53 +02:00
for (int j=0-frameH;j<(int)sizes.y+frameH;j++)
edgeFrames[i][j].resize(sizes.z, 0, 0);
}
2016-11-07 23:19:53 +02:00
auto & rand = CRandomGenerator::getDefault();
for (int i=0-frameW; i<sizes.x+frameW; i++) //by width
{
for (int j=0-frameH; j<sizes.y+frameH;j++) //by height
{
2016-11-07 23:19:53 +02:00
for(int k=0; k<sizes.z; ++k) //by levels
{
2016-11-07 23:19:53 +02:00
ui8 terBitmapNum = 0;
if(i < 0 || i > (sizes.x-1) || j < 0 || j > (sizes.y-1))
{
if(i==-1 && j==-1)
2009-08-17 16:08:05 +03:00
terBitmapNum = 16;
else if(i==-1 && j==(sizes.y))
2009-08-17 16:08:05 +03:00
terBitmapNum = 19;
else if(i==(sizes.x) && j==-1)
2009-08-17 16:08:05 +03:00
terBitmapNum = 17;
else if(i==(sizes.x) && j==(sizes.y))
2009-08-17 16:08:05 +03:00
terBitmapNum = 18;
else if(j == -1 && i > -1 && i < sizes.x)
terBitmapNum = rand.nextInt(22, 23);
else if(i == -1 && j > -1 && j < sizes.y)
terBitmapNum = rand.nextInt(33, 34);
else if(j == sizes.y && i >-1 && i < sizes.x)
terBitmapNum = rand.nextInt(29, 30);
else if(i == sizes.x && j > -1 && j < sizes.y)
terBitmapNum = rand.nextInt(25, 26);
else
terBitmapNum = rand.nextInt(15);
2009-08-17 16:08:05 +03:00
}
2016-11-07 23:19:53 +02:00
edgeFrames[i][j][k] = terBitmapNum;
}
}
}
}
void CMapHandler::initObjectRects()
{
//initializing objects / rects
for(auto & elem : map->objects)
{
const CGObjectInstance *obj = elem;
if( !obj
|| (obj->ID==Obj::HERO && static_cast<const CGHeroInstance*>(obj)->inTownGarrison) //garrisoned hero
|| (obj->ID==Obj::BOAT && static_cast<const CGBoat*>(obj)->hero)) //boat with hero (hero graphics is used)
{
continue;
}
2016-11-07 23:19:53 +02:00
std::shared_ptr<CAnimation> animation = graphics->getAnimation(obj);
//no animation at all
if(!animation)
continue;
//empty animation
if(animation->size(0) == 0)
continue;
2018-03-30 13:02:04 +02:00
auto image = animation->getImage(0,0);
2016-11-07 23:19:53 +02:00
for(int fx=0; fx < obj->getWidth(); ++fx)
{
for(int fy=0; fy < obj->getHeight(); ++fy)
{
int3 currTile(obj->pos.x - fx, obj->pos.y - fy, obj->pos.z);
SDL_Rect cr;
cr.w = 32;
cr.h = 32;
2016-11-07 23:19:53 +02:00
cr.x = image->width() - fx * 32 - 32;
cr.y = image->height() - fy * 32 - 32;
TerrainTileObject toAdd(obj, cr, obj->visitableAt(currTile.x, currTile.y));
2015-01-13 21:57:41 +02:00
if( map->isInTheMap(currTile) && // within map
cr.x + cr.w > 0 && // image has data on this tile
cr.y + cr.h > 0 &&
obj->coveringAt(currTile.x, currTile.y) // object is visible here
)
{
ttiles[currTile.z][currTile.x][currTile.y].objects.push_back(toAdd);
}
}
}
}
auto shape = ttiles.shape();
for(size_t z = 0; z < shape[0]; z++)
{
for(size_t x = 0; x < shape[1] - frameW; x++)
{
for(size_t y = 0; y < shape[2] - frameH; y++)
{
auto & objects = ttiles[z][x][y].objects;
stable_sort(objects.begin(), objects.end(), objectBlitOrderSorter);
}
}
}
}
void CMapHandler::init()
{
CStopWatch th;
th.getDiff();
2009-06-14 18:14:03 +03:00
// Size of visible terrain.
int mapW = conf.go()->ac.advmapW;
int mapH = conf.go()->ac.advmapH;
//sizes of terrain
sizes.x = map->width;
sizes.y = map->height;
sizes.z = map->levels();
// Total number of visible tiles. Subtract the center tile, then
// compute the number of tiles on each side, and reassemble.
int t1, t2;
t1 = (mapW-32)/2;
t2 = mapW - 32 - t1;
tilesW = 1 + (t1+31)/32 + (t2+31)/32;
t1 = (mapH-32)/2;
t2 = mapH - 32 - t1;
tilesH = 1 + (t1+31)/32 + (t2+31)/32;
// Size of the frame around the map. In extremes positions, the
// frame must not be on the center of the map, but right on the
// edge of the center tile.
frameW = (mapW+31) /32 / 2;
frameH = (mapH+31) /32 / 2;
offsetX = (mapW - (2*frameW+1)*32)/2;
offsetY = (mapH - (2*frameH+1)*32)/2;
prepareFOWDefs();
2016-11-07 23:19:53 +02:00
initTerrainGraphics();
initBorderGraphics();
2017-08-10 20:59:55 +02:00
logGlobal->info("\tPreparing FoW, terrain, roads, rivers, borders: %d ms", th.getDiff());
initObjectRects();
2017-08-10 20:59:55 +02:00
logGlobal->info("\tMaking object rects: %d ms", th.getDiff());
}
CMapHandler::CMapBlitter *CMapHandler::resolveBlitter(const MapDrawingInfo * info) const
2015-01-13 21:57:41 +02:00
{
if (info->scaled)
return worldViewBlitter;
if (info->puzzleMode)
return puzzleViewBlitter;
2015-01-13 21:57:41 +02:00
return normalBlitter;
2015-01-13 21:57:41 +02:00
}
void CMapHandler::CMapNormalBlitter::drawElement(EMapCacheType cacheType, std::shared_ptr<IImage> source, SDL_Rect * sourceRect, SDL_Surface * targetSurf, SDL_Rect * destRect) const
{
2016-11-07 23:19:53 +02:00
source->draw(targetSurf, destRect, sourceRect);
}
void CMapHandler::CMapNormalBlitter::init(const MapDrawingInfo * drawingInfo)
{
info = drawingInfo;
// Width and height of the portion of the map to process. Units in tiles.
tileCount.x = parent->tilesW;
tileCount.y = parent->tilesH;
2015-01-13 21:57:41 +02:00
topTile = info->topTile;
initPos.x = parent->offsetX + info->drawBounds->x;
initPos.y = parent->offsetY + info->drawBounds->y;
2015-01-13 21:57:41 +02:00
realTileRect = Rect(initPos.x, initPos.y, tileSize, tileSize);
// If moving, we need to add an extra column/line
if (info->movement.x != 0)
{
tileCount.x++;
initPos.x += info->movement.x;
if (info->movement.x > 0)
{
// Moving right. We still need to draw the old tile on the
// left, so adjust our referential
topTile.x--;
initPos.x -= tileSize;
}
}
2015-01-13 21:57:41 +02:00
if (info->movement.y != 0)
{
tileCount.y++;
initPos.y += info->movement.y;
if (info->movement.y > 0)
{
// Moving down. We still need to draw the tile on the top,
// so adjust our referential.
topTile.y--;
initPos.y -= tileSize;
}
}
2015-01-13 21:57:41 +02:00
// Reduce sizes if we go out of the full map.
if (topTile.x < -parent->frameW)
topTile.x = -parent->frameW;
if (topTile.y < -parent->frameH)
topTile.y = -parent->frameH;
if (topTile.x + tileCount.x > parent->sizes.x + parent->frameW)
tileCount.x = parent->sizes.x + parent->frameW - topTile.x;
if (topTile.y + tileCount.y > parent->sizes.y + parent->frameH)
tileCount.y = parent->sizes.y + parent->frameH - topTile.y;
}
2015-01-13 21:57:41 +02:00
SDL_Rect CMapHandler::CMapNormalBlitter::clip(SDL_Surface * targetSurf) const
{
SDL_Rect prevClip;
SDL_GetClipRect(targetSurf, &prevClip);
SDL_SetClipRect(targetSurf, info->drawBounds);
return prevClip;
}
2015-01-13 21:57:41 +02:00
CMapHandler::CMapNormalBlitter::CMapNormalBlitter(CMapHandler * parent)
: CMapBlitter(parent)
{
tileSize = 32;
halfTileSizeCeil = 16;
defaultTileRect = Rect(0, 0, tileSize, tileSize);
}
2015-01-13 21:57:41 +02:00
std::shared_ptr<IImage> CMapHandler::CMapWorldViewBlitter::objectToIcon(Obj id, si32 subId, PlayerColor owner) const
{
int ownerIndex = 0;
if(owner < PlayerColor::PLAYER_LIMIT)
{
ownerIndex = owner.getNum() * 19;
}
else if (owner == PlayerColor::NEUTRAL)
{
ownerIndex = PlayerColor::PLAYER_LIMIT.getNum() * 19;
}
2016-08-30 04:46:25 +02:00
switch(id)
{
case Obj::MONOLITH_ONE_WAY_ENTRANCE:
case Obj::MONOLITH_ONE_WAY_EXIT:
case Obj::MONOLITH_TWO_WAY:
2016-10-18 05:32:58 +02:00
return info->icons->getImage((int)EWorldViewIcon::TELEPORT);
case Obj::SUBTERRANEAN_GATE:
2016-10-18 05:32:58 +02:00
return info->icons->getImage((int)EWorldViewIcon::GATE);
case Obj::ARTIFACT:
2016-10-18 05:32:58 +02:00
return info->icons->getImage((int)EWorldViewIcon::ARTIFACT);
case Obj::TOWN:
2016-10-18 05:32:58 +02:00
return info->icons->getImage((int)EWorldViewIcon::TOWN + ownerIndex);
case Obj::HERO:
2016-10-18 05:32:58 +02:00
return info->icons->getImage((int)EWorldViewIcon::HERO + ownerIndex);
case Obj::MINE:
2016-10-18 05:32:58 +02:00
return info->icons->getImage((int)EWorldViewIcon::MINE_WOOD + subId + ownerIndex);
case Obj::RESOURCE:
2016-10-18 05:32:58 +02:00
return info->icons->getImage((int)EWorldViewIcon::RES_WOOD + subId + ownerIndex);
2016-08-30 04:46:25 +02:00
}
return std::shared_ptr<IImage>();
}
void CMapHandler::CMapWorldViewBlitter::calculateWorldViewCameraPos()
{
bool outsideLeft = topTile.x < 0;
bool outsideTop = topTile.y < 0;
bool outsideRight = std::max(0, topTile.x) + tileCount.x > parent->sizes.x;
bool outsideBottom = std::max(0, topTile.y) + tileCount.y > parent->sizes.y;
if (tileCount.x > parent->sizes.x)
topTile.x = parent->sizes.x / 2 - tileCount.x / 2; // center viewport if the whole map can fit into the screen at once
else if (outsideLeft)
{
if (outsideRight)
topTile.x = parent->sizes.x / 2 - tileCount.x / 2;
else
topTile.x = 0;
}
else if (outsideRight)
topTile.x = parent->sizes.x - tileCount.x;
if (tileCount.y > parent->sizes.y)
topTile.y = parent->sizes.y / 2 - tileCount.y / 2;
else if (outsideTop)
{
if (outsideBottom)
topTile.y = parent->sizes.y / 2 - tileCount.y / 2;
else
topTile.y = 0;
}
else if (outsideBottom)
topTile.y = parent->sizes.y - tileCount.y;
}
void CMapHandler::CMapWorldViewBlitter::drawElement(EMapCacheType cacheType, std::shared_ptr<IImage> source, SDL_Rect * sourceRect, SDL_Surface * targetSurf, SDL_Rect * destRect) const
{
2018-03-30 13:02:04 +02:00
auto scaled = parent->cache.requestWorldViewCacheOrCreate(cacheType, source);
2016-11-08 21:14:38 +02:00
if(scaled)
scaled->draw(targetSurf, destRect, sourceRect);
}
void CMapHandler::CMapWorldViewBlitter::drawTileOverlay(SDL_Surface * targetSurf, const TerrainTile2 & tile) const
{
auto drawIcon = [this,targetSurf](Obj id, si32 subId, PlayerColor owner)
{
2018-03-30 13:02:04 +02:00
auto wvIcon = this->objectToIcon(id, subId, owner);
2016-10-18 05:32:58 +02:00
if(nullptr != wvIcon)
{
// centering icon on the object
2016-10-18 05:32:58 +02:00
Point dest(realPos.x + tileSize / 2 - wvIcon->width() / 2, realPos.y + tileSize / 2 - wvIcon->height() / 2);
wvIcon->draw(targetSurf, dest.x, dest.y);
}
};
auto & objects = tile.objects;
for(auto & object : objects)
{
2015-01-28 21:07:57 +02:00
const CGObjectInstance * obj = object.obj;
if(!obj)
continue;
2015-01-13 21:57:41 +02:00
2016-08-30 04:46:25 +02:00
const bool sameLevel = obj->pos.z == pos.z;
//FIXME: Don't read options in a loop :v
const bool isVisible = settings["session"]["spectate"].Bool() ? true : (*info->visibilityMap)[pos.z][pos.x][pos.y];
const bool isVisitable = obj->visitableAt(pos.x, pos.y);
2015-01-13 21:57:41 +02:00
if(sameLevel && isVisible && isVisitable)
drawIcon(obj->ID, obj->subID, obj->tempOwner);
}
}
2015-01-13 21:57:41 +02:00
void CMapHandler::CMapWorldViewBlitter::drawOverlayEx(SDL_Surface * targetSurf)
{
if(nullptr == info->additionalIcons)
return;
2016-08-30 04:46:25 +02:00
const int3 bottomRight = pos + tileCount;
2016-08-30 04:46:25 +02:00
for(const ObjectPosInfo & iconInfo : *(info->additionalIcons))
{
2015-03-30 23:55:37 +02:00
if( iconInfo.pos.z != pos.z)
continue;
2016-08-30 04:46:25 +02:00
if((iconInfo.pos.x < topTile.x) || (iconInfo.pos.y < topTile.y))
continue;
2016-08-30 04:46:25 +02:00
if((iconInfo.pos.x > bottomRight.x) || (iconInfo.pos.y > bottomRight.y))
2016-08-30 04:46:25 +02:00
continue;
realPos.x = initPos.x + (iconInfo.pos.x - topTile.x) * tileSize;
realPos.y = initPos.x + (iconInfo.pos.y - topTile.y) * tileSize;
2016-08-30 04:46:25 +02:00
2018-03-30 13:02:04 +02:00
auto wvIcon = this->objectToIcon(iconInfo.id, iconInfo.subId, iconInfo.owner);
2016-08-30 04:46:25 +02:00
2016-10-18 05:32:58 +02:00
if(nullptr != wvIcon)
{
// centering icon on the object
2016-10-18 05:32:58 +02:00
Point dest(realPos.x + tileSize / 2 - wvIcon->width() / 2, realPos.y + tileSize / 2 - wvIcon->height() / 2);
wvIcon->draw(targetSurf, dest.x, dest.y);
2016-08-30 04:46:25 +02:00
}
}
}
2015-01-13 21:57:41 +02:00
void CMapHandler::CMapWorldViewBlitter::drawHeroFlag(SDL_Surface * targetSurf, std::shared_ptr<IImage> source, SDL_Rect * sourceRect, SDL_Rect * destRect, bool moving) const
{
if (moving)
return;
2015-01-13 21:57:41 +02:00
2016-11-07 23:19:53 +02:00
CMapBlitter::drawHeroFlag(targetSurf, source, sourceRect, destRect, false);
}
2015-01-13 21:57:41 +02:00
void CMapHandler::CMapWorldViewBlitter::drawObject(SDL_Surface * targetSurf, std::shared_ptr<IImage> source, SDL_Rect * sourceRect, bool moving) const
{
if (moving)
return;
2015-01-13 21:57:41 +02:00
Rect scaledSourceRect((int)(sourceRect->x * info->scale), (int)(sourceRect->y * info->scale), sourceRect->w, sourceRect->h);
2016-11-07 23:19:53 +02:00
CMapBlitter::drawObject(targetSurf, source, &scaledSourceRect, false);
}
2015-01-13 21:57:41 +02:00
void CMapHandler::CMapBlitter::drawTileTerrain(SDL_Surface * targetSurf, const TerrainTile & tinfo, const TerrainTile2 & tile) const
{
Rect destRect(realTileRect);
2016-11-07 23:19:53 +02:00
ui8 rotation = tinfo.extTileFlags % 4;
2022-09-21 11:34:23 +02:00
//TODO: use ui8 instead of string key
auto terrainName = tinfo.terType->name;
if(parent->terrainImages[terrainName].size()<=tinfo.terView)
return;
2016-11-07 23:19:53 +02:00
2022-09-21 11:34:23 +02:00
drawElement(EMapCacheType::TERRAIN, parent->terrainImages[terrainName][tinfo.terView][rotation], nullptr, targetSurf, &destRect);
}
2015-01-13 21:57:41 +02:00
void CMapHandler::CMapWorldViewBlitter::init(const MapDrawingInfo * drawingInfo)
{
info = drawingInfo;
parent->cache.updateWorldViewScale(info->scale);
2015-01-13 21:57:41 +02:00
topTile = info->topTile;
tileSize = (int) floorf(32.0f * info->scale);
halfTileSizeCeil = (int)ceilf(tileSize / 2.0f);
2015-01-13 21:57:41 +02:00
tileCount.x = (int) ceilf((float)info->drawBounds->w / tileSize);
tileCount.y = (int) ceilf((float)info->drawBounds->h / tileSize);
2015-01-13 21:57:41 +02:00
initPos.x = info->drawBounds->x;
initPos.y = info->drawBounds->y;
2015-01-13 21:57:41 +02:00
realTileRect = Rect(initPos.x, initPos.y, tileSize, tileSize);
defaultTileRect = Rect(0, 0, tileSize, tileSize);
calculateWorldViewCameraPos();
2015-01-13 21:57:41 +02:00
}
SDL_Rect CMapHandler::CMapWorldViewBlitter::clip(SDL_Surface * targetSurf) const
{
SDL_Rect prevClip;
SDL_FillRect(targetSurf, info->drawBounds, SDL_MapRGB(targetSurf->format, 0, 0, 0));
// makes the clip area smaller if the map is smaller than the screen frame
// (actually, it could be made 1 tile bigger so that overlay icons on edge tiles could be drawn partly outside)
2015-02-01 20:25:42 +02:00
Rect clipRect(std::max<int>(info->drawBounds->x, info->drawBounds->x - topTile.x * tileSize),
std::max<int>(info->drawBounds->y, info->drawBounds->y - topTile.y * tileSize),
std::min<int>(info->drawBounds->w, parent->sizes.x * tileSize),
std::min<int>(info->drawBounds->h, parent->sizes.y * tileSize));
SDL_GetClipRect(targetSurf, &prevClip);
SDL_SetClipRect(targetSurf, &clipRect); //preventing blitting outside of that rect
return prevClip;
}
CMapHandler::CMapWorldViewBlitter::CMapWorldViewBlitter(CMapHandler * parent)
: CMapBlitter(parent)
{
}
void CMapHandler::CMapPuzzleViewBlitter::drawObjects(SDL_Surface * targetSurf, const TerrainTile2 & tile) const
{
CMapBlitter::drawObjects(targetSurf, tile);
// grail X mark
if(pos.x == info->grailPos.x && pos.y == info->grailPos.y)
{
2018-03-30 13:02:04 +02:00
const auto mark = graphics->heroMoveArrows->getImage(0);
2016-10-18 09:31:31 +02:00
mark->draw(targetSurf,realTileRect.x,realTileRect.y);
}
}
void CMapHandler::CMapPuzzleViewBlitter::postProcessing(SDL_Surface * targetSurf) const
{
CSDL_Ext::applyEffect(targetSurf, info->drawBounds, static_cast<int>(!ADVOPT.puzzleSepia));
}
bool CMapHandler::CMapPuzzleViewBlitter::canDrawObject(const CGObjectInstance * obj) const
{
if (!CMapBlitter::canDrawObject(obj))
return false;
//don't print flaggable objects in puzzle mode
if (obj->isVisitable())
return false;
if(std::find(unblittableObjects.begin(), unblittableObjects.end(), obj->ID) != unblittableObjects.end())
return false;
return true;
}
CMapHandler::CMapPuzzleViewBlitter::CMapPuzzleViewBlitter(CMapHandler * parent)
: CMapNormalBlitter(parent)
{
unblittableObjects.push_back(Obj::HOLE);
}
2016-11-26 21:33:08 +02:00
CMapHandler::CMapBlitter::CMapBlitter(CMapHandler * p)
:parent(p), tileSize(0), halfTileSizeCeil(0), info(nullptr)
{
}
CMapHandler::CMapBlitter::~CMapBlitter() = default;
void CMapHandler::CMapBlitter::drawFrame(SDL_Surface * targetSurf) const
{
Rect destRect(realTileRect);
2016-11-07 23:19:53 +02:00
drawElement(EMapCacheType::FRAME, parent->egdeImages[parent->edgeFrames[pos.x][pos.y][topTile.z]], nullptr, targetSurf, &destRect);
}
void CMapHandler::CMapBlitter::drawOverlayEx(SDL_Surface * targetSurf)
{
//nothing to do here
}
void CMapHandler::CMapBlitter::drawHeroFlag(SDL_Surface * targetSurf, std::shared_ptr<IImage> source, SDL_Rect * sourceRect, SDL_Rect * destRect, bool moving) const
{
2016-11-07 23:19:53 +02:00
drawElement(EMapCacheType::HERO_FLAGS, source, sourceRect, targetSurf, destRect);
}
void CMapHandler::CMapBlitter::drawObject(SDL_Surface * targetSurf, std::shared_ptr<IImage> source, SDL_Rect * sourceRect, bool moving) const
{
Rect dstRect(realTileRect);
2016-11-07 23:19:53 +02:00
drawElement(EMapCacheType::OBJECTS, source, sourceRect, targetSurf, &dstRect);
}
void CMapHandler::CMapBlitter::drawObjects(SDL_Surface * targetSurf, const TerrainTile2 & tile) const
{
auto & objects = tile.objects;
for(auto & object : objects)
{
if (object.fadeAnimKey >= 0)
2016-08-30 04:46:25 +02:00
{
auto fadeIter = parent->fadeAnims.find(object.fadeAnimKey);
if (fadeIter != parent->fadeAnims.end())
{
// this object is currently fading, so skip normal drawing
Rect r2(realTileRect);
CFadeAnimation * fade = (*fadeIter).second.second;
fade->draw(targetSurf, nullptr, &r2);
continue;
}
2017-08-11 13:38:10 +02:00
logGlobal->error("Fading map object with missing fade anim : %d", object.fadeAnimKey);
continue;
2015-01-28 21:07:57 +02:00
}
2016-08-30 04:46:25 +02:00
2015-01-28 21:07:57 +02:00
const CGObjectInstance * obj = object.obj;
if (!obj)
{
2017-08-10 18:39:27 +02:00
logGlobal->error("Stray map object that isn't fading");
continue;
}
2016-08-30 04:46:25 +02:00
if (!canDrawObject(obj))
continue;
2016-08-30 04:46:25 +02:00
uint8_t animationFrame;
2019-05-19 19:08:32 +02:00
if(obj->ID == Obj::HERO) //non-generic animation frame pick for hero and boat
animationFrame = info->heroAnim;
else
animationFrame = info->anim;
2019-05-19 19:08:32 +02:00
auto objData = findObjectBitmap(obj, animationFrame);
if (objData.objBitmap)
2016-08-30 04:46:25 +02:00
{
Rect srcRect(object.rect.x, object.rect.y, tileSize, tileSize);
2016-08-30 04:46:25 +02:00
drawObject(targetSurf, objData.objBitmap, &srcRect, objData.isMoving);
if (objData.flagBitmap)
2014-07-03 19:41:42 +03:00
{
if (objData.isMoving)
{
srcRect.y += FRAMES_PER_MOVE_ANIM_GROUP * 2 - tileSize;
Rect dstRect(realPos.x, realPos.y - tileSize / 2, tileSize, tileSize);
drawHeroFlag(targetSurf, objData.flagBitmap, &srcRect, &dstRect, true);
}
else if (obj->pos.x == pos.x && obj->pos.y == pos.y)
{
Rect dstRect(realPos.x - 2 * tileSize, realPos.y - tileSize, 3 * tileSize, 2 * tileSize);
2016-08-30 04:46:25 +02:00
drawHeroFlag(targetSurf, objData.flagBitmap, nullptr, &dstRect, false);
}
}
}
}
}
2009-08-31 18:57:15 +03:00
void CMapHandler::CMapBlitter::drawRoad(SDL_Surface * targetSurf, const TerrainTile & tinfo, const TerrainTile * tinfoUpper) const
{
if (tinfoUpper && tinfoUpper->roadType->id != Road::NO_ROAD)
{
2016-11-07 23:19:53 +02:00
ui8 rotation = (tinfoUpper->extTileFlags >> 4) % 4;
Rect source(0, tileSize / 2, tileSize, tileSize / 2);
Rect dest(realPos.x, realPos.y, tileSize, tileSize / 2);
drawElement(EMapCacheType::ROADS, parent->roadImages[tinfoUpper->roadType->fileName][tinfoUpper->roadDir][rotation],
2016-11-07 23:19:53 +02:00
&source, targetSurf, &dest);
}
if(tinfo.roadType->id != Road::NO_ROAD) //print road from this tile
{
2016-11-07 23:19:53 +02:00
ui8 rotation = (tinfo.extTileFlags >> 4) % 4;
Rect source(0, 0, tileSize, halfTileSizeCeil);
Rect dest(realPos.x, realPos.y + tileSize / 2, tileSize, tileSize / 2);
drawElement(EMapCacheType::ROADS, parent->roadImages[tinfo.roadType->fileName][tinfo.roadDir][rotation],
2016-11-07 23:19:53 +02:00
&source, targetSurf, &dest);
}
}
void CMapHandler::CMapBlitter::drawRiver(SDL_Surface * targetSurf, const TerrainTile & tinfo) const
{
Rect destRect(realTileRect);
2016-11-07 23:19:53 +02:00
ui8 rotation = (tinfo.extTileFlags >> 2) % 4;
drawElement(EMapCacheType::RIVERS, parent->riverImages[tinfo.riverType->fileName][tinfo.riverDir][rotation], nullptr, targetSurf, &destRect);
}
void CMapHandler::CMapBlitter::drawFow(SDL_Surface * targetSurf) const
{
const NeighborTilesInfo neighborInfo(pos, parent->sizes, info->visibilityMap);
2016-11-07 23:19:53 +02:00
int retBitmapID = neighborInfo.getBitmapID();// >=0 -> partial hide, <0 - full hide
if (retBitmapID < 0)
retBitmapID = - parent->hideBitmap[pos.z][pos.x][pos.y] - 1; //fully hidden
2016-11-07 23:19:53 +02:00
std::shared_ptr<IImage> image;
2016-11-07 23:19:53 +02:00
if (retBitmapID >= 0)
image = parent->FoWpartialHide.at(retBitmapID);
else
image = parent->FoWfullHide.at(-retBitmapID - 1);
Rect destRect(realTileRect);
2016-11-07 23:19:53 +02:00
drawElement(EMapCacheType::FOW, image, nullptr, targetSurf, &destRect);
}
void CMapHandler::CMapBlitter::blit(SDL_Surface * targetSurf, const MapDrawingInfo * info)
{
init(info);
auto prevClip = clip(targetSurf);
pos = int3(0, 0, topTile.z);
for (realPos.x = initPos.x, pos.x = topTile.x; pos.x < topTile.x + tileCount.x; pos.x++, realPos.x += tileSize)
{
if (pos.x < 0 || pos.x >= parent->sizes.x)
continue;
for (realPos.y = initPos.y, pos.y = topTile.y; pos.y < topTile.y + tileCount.y; pos.y++, realPos.y += tileSize)
{
if (pos.y < 0 || pos.y >= parent->sizes.y)
continue;
2016-08-30 04:46:25 +02:00
const bool isVisible = canDrawCurrentTile();
realTileRect.x = realPos.x;
realTileRect.y = realPos.y;
const TerrainTile2 & tile = parent->ttiles[pos.z][pos.x][pos.y];
const TerrainTile & tinfo = parent->map->getTile(pos);
const TerrainTile * tinfoUpper = pos.y > 0 ? &parent->map->getTile(int3(pos.x, pos.y - 1, pos.z)) : nullptr;
2016-08-30 04:46:25 +02:00
if(isVisible || info->showAllTerrain)
{
drawTileTerrain(targetSurf, tinfo, tile);
if(tinfo.riverType->id != River::NO_RIVER)
drawRiver(targetSurf, tinfo);
2016-08-30 04:46:25 +02:00
drawRoad(targetSurf, tinfo, tinfoUpper);
}
if(isVisible)
drawObjects(targetSurf, tile);
}
}
for (realPos.x = initPos.x, pos.x = topTile.x; pos.x < topTile.x + tileCount.x; pos.x++, realPos.x += tileSize)
{
for (realPos.y = initPos.y, pos.y = topTile.y; pos.y < topTile.y + tileCount.y; pos.y++, realPos.y += tileSize)
{
realTileRect.x = realPos.x;
realTileRect.y = realPos.y;
if (pos.x < 0 || pos.x >= parent->sizes.x ||
pos.y < 0 || pos.y >= parent->sizes.y)
{
drawFrame(targetSurf);
}
2015-01-13 21:57:41 +02:00
else
{
const TerrainTile2 & tile = parent->ttiles[pos.z][pos.x][pos.y];
2015-01-13 21:57:41 +02:00
if(!settings["session"]["spectate"].Bool() && !(*info->visibilityMap)[topTile.z][pos.x][pos.y] && !info->showAllTerrain)
drawFow(targetSurf);
// overlay needs to be drawn over fow, because of artifacts-aura-like spells
drawTileOverlay(targetSurf, tile);
// drawDebugVisitables()
if (settings["session"]["showBlock"].Bool())
{
if(parent->map->getTile(int3(pos.x, pos.y, pos.z)).blocked) //temporary hiding blocked positions
{
static SDL_Surface * block = nullptr;
if (!block)
block = BitmapHandler::loadBitmap("blocked");
CSDL_Ext::blitSurface(block, nullptr, targetSurf, &realTileRect);
}
}
if (settings["session"]["showVisit"].Bool())
{
if(parent->map->getTile(int3(pos.x, pos.y, pos.z)).visitable) //temporary hiding visitable positions
{
static SDL_Surface * visit = nullptr;
if (!visit)
visit = BitmapHandler::loadBitmap("visitable");
CSDL_Ext::blitSurface(visit, nullptr, targetSurf, &realTileRect);
}
}
}
}
}
2016-08-30 04:46:25 +02:00
drawOverlayEx(targetSurf);
// drawDebugGrid()
2012-05-14 19:29:06 +03:00
if (settings["session"]["showGrid"].Bool())
{
for (realPos.x = initPos.x, pos.x = topTile.x; pos.x < topTile.x + tileCount.x; pos.x++, realPos.x += tileSize)
{
for (realPos.y = initPos.y, pos.y = topTile.y; pos.y < topTile.y + tileCount.y; pos.y++, realPos.y += tileSize)
2012-05-14 19:29:06 +03:00
{
const int3 color(0x555555, 0x555555, 0x555555);
if (realPos.y >= info->drawBounds->y &&
realPos.y < info->drawBounds->y + info->drawBounds->h)
for(int i = 0; i < tileSize; i++)
if (realPos.x + i >= info->drawBounds->x &&
realPos.x + i < info->drawBounds->x + info->drawBounds->w)
CSDL_Ext::SDL_PutPixelWithoutRefresh(targetSurf, realPos.x + i, realPos.y, color.x, color.y, color.z);
if (realPos.x >= info->drawBounds->x &&
realPos.x < info->drawBounds->x + info->drawBounds->w)
for(int i = 0; i < tileSize; i++)
if (realPos.y + i >= info->drawBounds->y &&
realPos.y + i < info->drawBounds->y + info->drawBounds->h)
CSDL_Ext::SDL_PutPixelWithoutRefresh(targetSurf, realPos.x, realPos.y + i, color.x, color.y, color.z);
2012-05-14 19:29:06 +03:00
}
}
}
postProcessing(targetSurf);
SDL_SetClipRect(targetSurf, &prevClip);
}
CMapHandler::AnimBitmapHolder CMapHandler::CMapBlitter::findHeroBitmap(const CGHeroInstance * hero, int anim) const
{
if(hero && hero->moveDir && hero->type) //it's hero or boat
{
if(hero->tempOwner >= PlayerColor::PLAYER_LIMIT) //Neutral hero?
{
logGlobal->error("A neutral hero (%s) at %s. Should not happen!", hero->name, hero->pos.toString());
return CMapHandler::AnimBitmapHolder();
}
//pick graphics of hero (or boat if hero is sailing)
2016-11-07 23:19:53 +02:00
std::shared_ptr<CAnimation> animation;
if (hero->boat)
2016-11-07 23:19:53 +02:00
animation = graphics->boatAnimations[hero->boat->subID];
else
animation = graphics->heroAnimations[hero->appearance->animationFile];
bool moving = !hero->isStanding;
2016-11-07 23:19:53 +02:00
int group = getHeroFrameGroup(hero->moveDir, moving);
if(animation->size(group) > 0)
{
2016-11-07 23:19:53 +02:00
int frame = anim % animation->size(group);
2018-03-30 13:02:04 +02:00
auto heroImage = animation->getImage(frame, group);
2016-11-07 23:19:53 +02:00
//get flag overlay only if we have main image
2018-03-30 13:02:04 +02:00
auto flagImage = findFlagBitmap(hero, anim, &hero->tempOwner, group);
2016-11-07 23:19:53 +02:00
return CMapHandler::AnimBitmapHolder(heroImage, flagImage, moving);
}
}
return CMapHandler::AnimBitmapHolder();
}
CMapHandler::AnimBitmapHolder CMapHandler::CMapBlitter::findBoatBitmap(const CGBoat * boat, int anim) const
{
2016-11-07 23:19:53 +02:00
auto animation = graphics->boatAnimations.at(boat->subID);
int group = getHeroFrameGroup(boat->direction, false);
if(animation->size(group) > 0)
return CMapHandler::AnimBitmapHolder(animation->getImage(anim % animation->size(group), group));
else
return CMapHandler::AnimBitmapHolder();
}
std::shared_ptr<IImage> CMapHandler::CMapBlitter::findFlagBitmap(const CGHeroInstance * hero, int anim, const PlayerColor * color, int group) const
2016-08-30 04:46:25 +02:00
{
if(!hero)
return std::shared_ptr<IImage>();
2016-08-30 04:46:25 +02:00
if(hero->boat)
2016-11-07 23:19:53 +02:00
return findBoatFlagBitmap(hero->boat, anim, color, group, hero->moveDir);
return findHeroFlagBitmap(hero, anim, color, group);
}
std::shared_ptr<IImage> CMapHandler::CMapBlitter::findHeroFlagBitmap(const CGHeroInstance * hero, int anim, const PlayerColor * color, int group) const
{
2016-11-07 23:19:53 +02:00
return findFlagBitmapInternal(graphics->heroFlagAnimations.at(color->getNum()), anim, group, hero->moveDir, !hero->isStanding);
}
std::shared_ptr<IImage> CMapHandler::CMapBlitter::findBoatFlagBitmap(const CGBoat * boat, int anim, const PlayerColor * color, int group, ui8 dir) const
{
2016-11-07 23:19:53 +02:00
int boatType = boat->subID;
if(boatType < 0 || boatType >= graphics->boatFlagAnimations.size())
{
2017-08-11 13:38:10 +02:00
logGlobal->error("Not supported boat subtype: %d", boat->subID);
2016-11-07 23:19:53 +02:00
return nullptr;
}
2016-11-07 23:19:53 +02:00
const auto & subtypeFlags = graphics->boatFlagAnimations.at(boatType);
int colorIndex = color->getNum();
if(colorIndex < 0 || colorIndex >= subtypeFlags.size())
{
2017-08-11 13:38:10 +02:00
logGlobal->error("Invalid player color %d", colorIndex);
2016-11-07 23:19:53 +02:00
return nullptr;
}
return findFlagBitmapInternal(subtypeFlags.at(colorIndex), anim, group, dir, false);
}
std::shared_ptr<IImage> CMapHandler::CMapBlitter::findFlagBitmapInternal(std::shared_ptr<CAnimation> animation, int anim, int group, ui8 dir, bool moving) const
{
2016-11-25 22:02:13 +02:00
size_t groupSize = animation->size(group);
if(groupSize == 0)
return nullptr;
2016-11-07 23:19:53 +02:00
if(moving)
2016-11-25 22:02:13 +02:00
return animation->getImage(anim % groupSize, group);
2016-11-07 23:19:53 +02:00
else
2016-11-25 22:02:13 +02:00
return animation->getImage((anim / 4) % groupSize, group);
}
CMapHandler::AnimBitmapHolder CMapHandler::CMapBlitter::findObjectBitmap(const CGObjectInstance * obj, int anim) const
{
if (!obj)
return CMapHandler::AnimBitmapHolder();
if (obj->ID == Obj::HERO)
return findHeroBitmap(static_cast<const CGHeroInstance*>(obj), anim);
if (obj->ID == Obj::BOAT)
return findBoatBitmap(static_cast<const CGBoat*>(obj), anim);
2016-08-30 04:46:25 +02:00
// normal object
2016-11-07 23:19:53 +02:00
std::shared_ptr<CAnimation> animation = graphics->getAnimation(obj);
2016-11-25 22:02:13 +02:00
size_t groupSize = animation->size();
if(groupSize == 0)
2016-11-25 22:02:13 +02:00
return CMapHandler::AnimBitmapHolder();
2018-03-30 13:02:04 +02:00
auto bitmap = animation->getImage((anim + getPhaseShift(obj)) % groupSize);
2016-11-25 22:02:13 +02:00
if(!bitmap)
return CMapHandler::AnimBitmapHolder();
2016-08-30 04:46:25 +02:00
2016-11-07 23:19:53 +02:00
bitmap->setFlagColor(obj->tempOwner);
return CMapHandler::AnimBitmapHolder(bitmap);
}
ui8 CMapHandler::CMapBlitter::getPhaseShift(const CGObjectInstance *object) const
{
auto i = parent->animationPhase.find(object);
if(i == parent->animationPhase.end())
2009-08-31 18:57:15 +03:00
{
ui8 ret = CRandomGenerator::getDefault().nextInt(254);
parent->animationPhase[object] = ret;
return ret;
2009-08-31 18:57:15 +03:00
}
return i->second;
}
bool CMapHandler::CMapBlitter::canDrawObject(const CGObjectInstance * obj) const
{
//checking if object has non-empty graphic on this tile
return obj->ID == Obj::HERO || obj->coveringAt(pos.x, pos.y);
}
bool CMapHandler::CMapBlitter::canDrawCurrentTile() const
{
if(settings["session"]["spectate"].Bool())
return true;
const NeighborTilesInfo neighbors(pos, parent->sizes, info->visibilityMap);
return !neighbors.areAllHidden();
}
2014-07-03 19:41:42 +03:00
2016-11-07 23:19:53 +02:00
ui8 CMapHandler::CMapBlitter::getHeroFrameGroup(ui8 dir, bool isMoving) const
{
if(isMoving)
{
static const ui8 frame [] = {0xff, 10, 5, 6, 7, 8, 9, 12, 11};
return frame[dir];
}
else //if(isMoving)
{
static const ui8 frame [] = {0xff, 13, 0, 1, 2, 3, 4, 15, 14};
return frame[dir];
}
}
bool CMapHandler::updateObjectsFade()
2016-08-30 04:46:25 +02:00
{
for (auto iter = fadeAnims.begin(); iter != fadeAnims.end(); )
2015-01-28 21:07:57 +02:00
{
int3 pos = (*iter).second.first;
CFadeAnimation * anim = (*iter).second.second;
2016-08-30 04:46:25 +02:00
anim->update();
2016-08-30 04:46:25 +02:00
if (anim->isFading())
++iter;
else // fade finished
2015-01-28 21:07:57 +02:00
{
auto &objs = ttiles[pos.z][pos.x][pos.y].objects;
for (auto objIter = objs.begin(); objIter != objs.end(); ++objIter)
2015-01-28 21:07:57 +02:00
{
if ((*objIter).fadeAnimKey == (*iter).first)
2016-08-30 04:46:25 +02:00
{
logAnim->trace("Fade anim finished for obj at %s; remaining: %d", pos.toString(), fadeAnims.size() - 1);
if (anim->fadingMode == CFadeAnimation::EMode::OUT)
objs.erase(objIter); // if this was fadeout, remove the object from the map
else
(*objIter).fadeAnimKey = -1; // for fadein, just remove its connection to the finished fade
break;
2015-01-28 21:07:57 +02:00
}
}
2016-08-30 04:46:25 +02:00
delete (*iter).second.second;
iter = fadeAnims.erase(iter);
2015-01-28 21:07:57 +02:00
}
}
2016-08-30 04:46:25 +02:00
return !fadeAnims.empty();
2015-01-28 21:07:57 +02:00
}
bool CMapHandler::startObjectFade(TerrainTileObject & obj, bool in, int3 pos)
{
SDL_Surface * fadeBitmap;
assert(obj.obj);
2016-08-30 04:46:25 +02:00
auto objData = normalBlitter->findObjectBitmap(obj.obj, 0);
if (objData.objBitmap)
{
if (objData.isMoving) // ignore fading of moving objects (for now?)
{
2017-08-10 18:39:27 +02:00
logAnim->debug("Ignoring fade of moving object");
return false;
}
2016-08-30 04:46:25 +02:00
fadeBitmap = CSDL_Ext::newSurface(32, 32); // TODO cache these bitmaps instead of creating new ones?
Rect objSrcRect(obj.rect.x, obj.rect.y, 32, 32);
2016-11-07 23:19:53 +02:00
objData.objBitmap->draw(fadeBitmap,0,0,&objSrcRect);
if (objData.flagBitmap)
{
if (obj.obj->pos.x - 1 == pos.x && obj.obj->pos.y - 1 == pos.y) // -1 to draw flag in top-center instead of right-bottom; kind of a hack
{
Rect flagSrcRect(32, 0, 32, 32);
2016-11-07 23:19:53 +02:00
objData.flagBitmap->draw(fadeBitmap,0,0, &flagSrcRect);
}
}
auto anim = new CFadeAnimation();
anim->init(in ? CFadeAnimation::EMode::IN : CFadeAnimation::EMode::OUT, fadeBitmap, true);
fadeAnims[++fadeAnimCounter] = std::pair<int3, CFadeAnimation*>(pos, anim);
obj.fadeAnimKey = fadeAnimCounter;
2016-08-30 04:46:25 +02:00
logAnim->trace("Fade anim started for obj %d at %s; anim count: %d", obj.obj->ID, pos.toString(), fadeAnims.size());
return true;
}
2016-08-30 04:46:25 +02:00
return false;
}
bool CMapHandler::printObject(const CGObjectInstance * obj, bool fadein)
{
2016-11-07 23:19:53 +02:00
auto animation = graphics->getAnimation(obj);
if(!animation)
return false;
2018-03-30 13:02:04 +02:00
auto bitmap = animation->getImage(0);
2016-11-07 23:19:53 +02:00
if(!bitmap)
return false;
const int tilesW = bitmap->width()/32;
const int tilesH = bitmap->height()/32;
auto ttilesWidth = ttiles.shape()[1];
auto ttilesHeight = ttiles.shape()[2];
for(int fx=0; fx<tilesW; ++fx)
{
for(int fy=0; fy<tilesH; ++fy)
{
SDL_Rect cr;
cr.w = 32;
cr.h = 32;
cr.x = fx*32;
cr.y = fy*32;
2016-08-30 04:46:25 +02:00
if((obj->pos.x + fx - tilesW + 1) >= 0 &&
(obj->pos.x + fx - tilesW + 1) < ttilesWidth - frameW &&
(obj->pos.y + fy - tilesH + 1) >= 0 &&
(obj->pos.y + fy - tilesH + 1) < ttilesHeight - frameH)
{
int3 pos(obj->pos.x + fx - tilesW + 1, obj->pos.y + fy - tilesH + 1, obj->pos.z);
TerrainTile2 & curt = ttiles[pos.z][pos.x][pos.y];
2016-08-30 04:46:25 +02:00
TerrainTileObject toAdd(obj, cr, obj->visitableAt(pos.x, pos.y));
if (fadein && ADVOPT.objectFading)
{
startObjectFade(toAdd, true, pos);
}
2016-08-30 04:46:25 +02:00
auto i = curt.objects.begin();
for(; i != curt.objects.end(); i++)
{
if(objectBlitOrderSorter(toAdd, *i))
{
curt.objects.insert(i, toAdd);
i = curt.objects.begin(); //to validate and avoid adding it second time
break;
}
}
if(i == curt.objects.end())
curt.objects.insert(i, toAdd);
}
}
}
return true;
}
bool CMapHandler::hideObject(const CGObjectInstance * obj, bool fadeout)
{
for(size_t z = 0; z < map->levels(); z++)
{
for(size_t x = 0; x < map->width; x++)
{
for(size_t y = 0; y < map->height; y++)
{
auto &objs = ttiles[(int)z][(int)x][(int)y].objects;
for(size_t i = 0; i < objs.size(); i++)
{
if (objs[i].obj && objs[i].obj->id == obj->id)
{
if (fadeout && ADVOPT.objectFading) // object should be faded == erase is delayed until the end of fadeout
{
if (startObjectFade(objs[i], false, int3((si32)x, (si32)y, (si32)z)))
objs[i].obj = nullptr;
else
objs.erase(objs.begin() + i);
}
2015-01-28 21:07:57 +02:00
else
objs.erase(objs.begin() + i);
break;
}
}
}
}
}
return true;
}
bool CMapHandler::canStartHeroMovement()
{
return fadeAnims.empty(); // don't allow movement during fade animation
}
void CMapHandler::updateWater() //shift colors in palettes of water tiles
{
for(auto & elem : terrainImages["lava"])
{
2018-03-30 13:02:04 +02:00
for(auto img : elem)
2016-11-07 23:19:53 +02:00
img->shiftPalette(246, 9);
}
2016-11-07 23:19:53 +02:00
for(auto & elem : terrainImages["water"])
{
2018-03-30 13:02:04 +02:00
for(auto img : elem)
2016-11-07 23:19:53 +02:00
{
img->shiftPalette(229, 12);
img->shiftPalette(242, 14);
}
}
2016-11-07 23:19:53 +02:00
for(auto & elem : riverImages["clrrvr"])
{
2018-03-30 13:02:04 +02:00
for(auto img : elem)
2016-11-07 23:19:53 +02:00
{
img->shiftPalette(183, 12);
img->shiftPalette(195, 6);
}
}
2016-11-07 23:19:53 +02:00
for(auto & elem : riverImages["mudrvr"])
{
2018-03-30 13:02:04 +02:00
for(auto img : elem)
2016-11-07 23:19:53 +02:00
{
img->shiftPalette(228, 12);
img->shiftPalette(183, 6);
img->shiftPalette(240, 6);
}
}
2016-11-07 23:19:53 +02:00
for(auto & elem : riverImages["lavrvr"])
{
2018-03-30 13:02:04 +02:00
for(auto img : elem)
2016-11-07 23:19:53 +02:00
img->shiftPalette(240, 9);
}
}
CMapHandler::~CMapHandler()
{
delete normalBlitter;
delete worldViewBlitter;
delete puzzleViewBlitter;
for (auto & elem : fadeAnims)
{
delete elem.second.second;
}
}
CMapHandler::CMapHandler()
{
frameW = frameH = 0;
normalBlitter = new CMapNormalBlitter(this);
worldViewBlitter = new CMapWorldViewBlitter(this);
puzzleViewBlitter = new CMapPuzzleViewBlitter(this);
fadeAnimCounter = 0;
2016-11-27 16:48:18 +02:00
map = nullptr;
tilesW = tilesH = 0;
offsetX = offsetY = 0;
2016-11-07 23:19:53 +02:00
egdeAnimation = make_unique<CAnimation>("EDG");
egdeAnimation->preload();
}
bool CMapHandler::hasObjectHole(const int3 & pos) const
2010-02-21 17:03:30 +02:00
{
const TerrainTile2 & tt = ttiles[pos.z][pos.x][pos.y];
for(auto & elem : tt.objects)
2010-02-21 17:03:30 +02:00
{
if(elem.obj && elem.obj->ID == Obj::HOLE)
return true;
2010-02-21 17:03:30 +02:00
}
return false;
}
void CMapHandler::getTerrainDescr(const int3 & pos, std::string & out, bool isRMB) const
{
const TerrainTile & t = map->getTile(pos);
2010-02-21 17:03:30 +02:00
if(t.hasFavorableWinds())
{
out = CGI->objtypeh->getObjectName(Obj::FAVORABLE_WINDS);
return;
}
const TerrainTile2 & tt = ttiles[pos.z][pos.x][pos.y];
bool isTile2Terrain = false;
out.clear();
for(auto & elem : tt.objects)
{
if(elem.obj)
{
out = elem.obj->getObjectName();
if(elem.obj->ID == Obj::HOLE)
return;
isTile2Terrain = elem.obj->isTile2Terrain();
break;
}
}
if(!isTile2Terrain || out.empty())
2022-09-21 11:34:23 +02:00
out = CGI->generaltexth->terrainNames[t.terType->id];
if(t.getDiggingStatus(false) == EDiggingStatus::CAN_DIG)
{
out = boost::str(boost::format(isRMB ? "%s\r\n%s" : "%s %s") // New line for the Message Box, space for the Status Bar
% out
% CGI->generaltexth->allTexts[330]); // 'digging ok'
}
2010-02-21 17:03:30 +02:00
}
2015-01-13 21:57:41 +02:00
void CMapHandler::discardWorldViewCache()
{
cache.discardWorldViewCache();
}
2016-11-27 16:48:18 +02:00
CMapHandler::CMapCache::CMapCache()
{
worldViewCachedScale = 0;
}
2015-01-13 21:57:41 +02:00
void CMapHandler::CMapCache::discardWorldViewCache()
{
2016-11-08 21:14:38 +02:00
for(auto & cache : data)
2016-11-07 23:19:53 +02:00
cache.clear();
2016-11-08 21:14:38 +02:00
logAnim->debug("Discarded world view cache");
2015-01-13 21:57:41 +02:00
}
void CMapHandler::CMapCache::updateWorldViewScale(float scale)
{
if (fabs(scale - worldViewCachedScale) > 0.001f)
discardWorldViewCache();
worldViewCachedScale = scale;
}
std::shared_ptr<IImage> CMapHandler::CMapCache::requestWorldViewCacheOrCreate(CMapHandler::EMapCacheType type, std::shared_ptr<IImage> fullSurface)
2016-11-07 23:19:53 +02:00
{
intptr_t key = (intptr_t) (fullSurface.get());
2016-11-08 21:14:38 +02:00
auto & cache = data[(ui8)type];
2016-11-07 23:19:53 +02:00
2016-11-08 21:14:38 +02:00
auto iter = cache.find(key);
if(iter == cache.end())
{
auto scaled = fullSurface->scaleFast(worldViewCachedScale);
cache[key] = scaled;
return scaled;
2016-11-08 21:14:38 +02:00
}
else
{
return (*iter).second;
2016-11-08 21:14:38 +02:00
}
}
bool CMapHandler::compareObjectBlitOrder(const CGObjectInstance * a, const CGObjectInstance * b)
{
if (!a)
return true;
if (!b)
return false;
if (a->appearance->printPriority != b->appearance->printPriority)
return a->appearance->printPriority > b->appearance->printPriority;
if(a->pos.y != b->pos.y)
return a->pos.y < b->pos.y;
if(b->ID==Obj::HERO && a->ID!=Obj::HERO)
return true;
if(b->ID!=Obj::HERO && a->ID==Obj::HERO)
return false;
if(!a->isVisitable() && b->isVisitable())
return true;
if(!b->isVisitable() && a->isVisitable())
return false;
if(a->pos.x < b->pos.x)
return true;
return false;
}
TerrainTileObject::TerrainTileObject(const CGObjectInstance * obj_, SDL_Rect rect_, bool visitablePos)
: obj(obj_),
rect(rect_),
fadeAnimKey(-1)
{
// We store information about ambient sound is here because object might disappear while sound is updating
if(obj->getAmbientSound())
{
// All tiles of static objects are sound sources. E.g Volcanos and special terrains
// For visitable object only their visitable tile is sound source
if(!CCS->soundh->ambientCheckVisitable() || !obj->isVisitable() || visitablePos)
ambientSound = obj->getAmbientSound();
}
}
TerrainTileObject::~TerrainTileObject()
{
}