2022-08-30 00:44:02 +02:00
|
|
|
#include "StdInc.h"
|
|
|
|
#include "maphandler.h"
|
2022-08-30 04:38:24 +02:00
|
|
|
#include "graphics.h"
|
2022-08-30 00:44:02 +02:00
|
|
|
#include "../lib/mapping/CMap.h"
|
2022-08-30 04:38:24 +02:00
|
|
|
#include "../lib/mapObjects/CGHeroInstance.h"
|
|
|
|
#include "../lib/mapObjects/CObjectClassesHandler.h"
|
|
|
|
#include "../lib/CHeroHandler.h"
|
|
|
|
#include "../lib/CTownHandler.h"
|
|
|
|
#include "../lib/CModHandler.h"
|
|
|
|
#include "../lib/mapping/CMap.h"
|
|
|
|
#include "../lib/GameConstants.h"
|
|
|
|
#include "../lib/JsonDetail.h"
|
2022-08-30 00:44:02 +02:00
|
|
|
|
2022-08-30 02:15:54 +02:00
|
|
|
const int tileSize = 32;
|
2022-08-30 00:44:02 +02:00
|
|
|
|
2022-09-06 19:15:13 +02:00
|
|
|
static bool objectBlitOrderSorter(const TileObject & a, const TileObject & b)
|
2022-08-30 04:38:24 +02:00
|
|
|
{
|
|
|
|
return MapHandler::compareObjectBlitOrder(a.obj, b.obj);
|
|
|
|
}
|
|
|
|
|
2022-09-06 19:15:13 +02:00
|
|
|
int MapHandler::index(int x, int y, int z) const
|
2022-08-30 02:15:54 +02:00
|
|
|
{
|
2022-09-06 19:15:13 +02:00
|
|
|
return z * (sizes.x * sizes.y) + y * sizes.x + x;
|
2022-08-30 00:44:02 +02:00
|
|
|
}
|
|
|
|
|
2022-09-06 19:15:13 +02:00
|
|
|
int MapHandler::index(const int3 & p) const
|
2022-08-30 00:44:02 +02:00
|
|
|
{
|
2022-09-06 19:15:13 +02:00
|
|
|
return index(p.x, p.y, p.z);
|
|
|
|
}
|
|
|
|
|
|
|
|
MapHandler::MapHandler()
|
|
|
|
{
|
|
|
|
initTerrainGraphics();
|
|
|
|
logGlobal->info("\tPreparing terrain, roads, rivers, borders");
|
|
|
|
}
|
|
|
|
|
|
|
|
void MapHandler::reset(const CMap * Map)
|
|
|
|
{
|
|
|
|
ttiles.clear();
|
|
|
|
map = Map;
|
|
|
|
|
2022-08-30 00:44:02 +02:00
|
|
|
//sizes of terrain
|
|
|
|
sizes.x = map->width;
|
|
|
|
sizes.y = map->height;
|
|
|
|
sizes.z = map->twoLevel ? 2 : 1;
|
|
|
|
|
|
|
|
initObjectRects();
|
|
|
|
logGlobal->info("\tMaking object rects");
|
|
|
|
}
|
|
|
|
|
|
|
|
void MapHandler::initTerrainGraphics()
|
|
|
|
{
|
|
|
|
static const std::map<std::string, std::string> ROAD_FILES =
|
|
|
|
{
|
|
|
|
{ROAD_NAMES[1], "dirtrd"},
|
|
|
|
{ROAD_NAMES[2], "gravrd"},
|
|
|
|
{ROAD_NAMES[3], "cobbrd"}
|
|
|
|
};
|
|
|
|
|
|
|
|
static const std::map<std::string, std::string> RIVER_FILES =
|
|
|
|
{
|
|
|
|
{RIVER_NAMES[1], "clrrvr"},
|
|
|
|
{RIVER_NAMES[2], "icyrvr"},
|
|
|
|
{RIVER_NAMES[3], "mudrvr"},
|
|
|
|
{RIVER_NAMES[4], "lavrvr"}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
auto loadFlipped = [](TFlippedAnimations & animation, TFlippedCache & cache, const std::map<std::string, std::string> & files)
|
|
|
|
{
|
|
|
|
for(auto & type : files)
|
|
|
|
{
|
2022-08-30 02:48:44 +02:00
|
|
|
animation[type.first] = make_unique<Animation>(type.second);
|
|
|
|
animation[type.first]->preload();
|
|
|
|
const size_t views = animation[type.first]->size(0);
|
2022-08-30 00:44:02 +02:00
|
|
|
cache[type.first].resize(views);
|
|
|
|
|
|
|
|
for(int j = 0; j < views; j++)
|
2022-08-30 02:48:44 +02:00
|
|
|
cache[type.first][j] = animation[type.first]->getImage(j);
|
2022-08-30 00:44:02 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
std::map<std::string, std::string> terrainFiles;
|
|
|
|
for(auto & terrain : Terrain::Manager::terrains())
|
|
|
|
{
|
|
|
|
terrainFiles[terrain] = Terrain::Manager::getInfo(terrain).tilesFilename;
|
|
|
|
}
|
|
|
|
|
|
|
|
loadFlipped(terrainAnimations, terrainImages, terrainFiles);
|
|
|
|
loadFlipped(roadAnimations, roadImages, ROAD_FILES);
|
|
|
|
loadFlipped(riverAnimations, riverImages, RIVER_FILES);
|
|
|
|
}
|
|
|
|
|
2022-09-01 03:51:29 +02:00
|
|
|
void MapHandler::drawTerrainTile(QPainter & painter, int x, int y, int z)
|
2022-08-30 00:44:02 +02:00
|
|
|
{
|
2022-08-30 04:38:24 +02:00
|
|
|
auto & tinfo = map->getTile(int3(x, y, z));
|
2022-08-30 00:44:02 +02:00
|
|
|
ui8 rotation = tinfo.extTileFlags % 4;
|
|
|
|
|
|
|
|
if(terrainImages.at(tinfo.terType).size() <= tinfo.terView)
|
2022-08-30 02:15:54 +02:00
|
|
|
return;
|
2022-08-30 00:44:02 +02:00
|
|
|
|
2022-08-30 02:48:44 +02:00
|
|
|
bool hflip = (rotation == 1 || rotation == 3), vflip = (rotation == 2 || rotation == 3);
|
|
|
|
painter.drawImage(x * tileSize, y * tileSize, terrainImages.at(tinfo.terType)[tinfo.terView]->mirrored(hflip, vflip));
|
2022-08-30 00:44:02 +02:00
|
|
|
}
|
|
|
|
|
2022-09-03 06:12:07 +02:00
|
|
|
void MapHandler::drawRoad(QPainter & painter, int x, int y, int z)
|
|
|
|
{
|
|
|
|
auto & tinfo = map->getTile(int3(x, y, z));
|
|
|
|
auto * tinfoUpper = map->isInTheMap(int3(x, y - 1, z)) ? &map->getTile(int3(x, y - 1, z)) : nullptr;
|
|
|
|
|
|
|
|
if(tinfo.roadType != ROAD_NAMES[0]) //print road from this tile
|
|
|
|
{
|
|
|
|
ui8 rotation = (tinfo.extTileFlags >> 4) % 4;
|
|
|
|
|
|
|
|
bool hflip = (rotation == 1 || rotation == 3), vflip = (rotation == 2 || rotation == 3);
|
|
|
|
if(roadImages.at(tinfo.roadType).size() > tinfo.roadDir)
|
|
|
|
{
|
|
|
|
painter.drawImage(x * tileSize, y * tileSize, roadImages.at(tinfo.roadType)[tinfo.roadDir]->mirrored(hflip, vflip));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MapHandler::drawRiver(QPainter & painter, int x, int y, int z)
|
|
|
|
{
|
|
|
|
auto & tinfo = map->getTile(int3(x, y, z));
|
|
|
|
|
|
|
|
if(tinfo.riverType == RIVER_NAMES[0])
|
|
|
|
return;
|
|
|
|
|
|
|
|
if(riverImages.at(tinfo.riverType).size() <= tinfo.riverDir)
|
|
|
|
return;
|
|
|
|
|
|
|
|
ui8 rotation = (tinfo.extTileFlags >> 2) % 4;
|
|
|
|
bool hflip = (rotation == 1 || rotation == 3), vflip = (rotation == 2 || rotation == 3);
|
|
|
|
|
|
|
|
painter.drawImage(x * tileSize, y * tileSize, riverImages.at(tinfo.riverType)[tinfo.riverDir]->mirrored(hflip, vflip));
|
|
|
|
}
|
|
|
|
|
2022-09-04 20:29:56 +02:00
|
|
|
void setPlayerColor(QImage * sur, PlayerColor player)
|
|
|
|
{
|
|
|
|
if(player == PlayerColor::UNFLAGGABLE)
|
|
|
|
return;
|
|
|
|
if(sur->format() == QImage::Format_Indexed8)
|
|
|
|
{
|
|
|
|
QRgb color = graphics->neutralColor;
|
|
|
|
if(player != PlayerColor::NEUTRAL)
|
|
|
|
color = graphics->playerColors[player.getNum()];
|
|
|
|
|
|
|
|
sur->setColor(5, color);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
logGlobal->warn("Warning, setPlayerColor called on not 8bpp surface!");
|
|
|
|
}
|
|
|
|
|
2022-08-30 00:44:02 +02:00
|
|
|
void MapHandler::initObjectRects()
|
|
|
|
{
|
2022-09-06 19:15:13 +02:00
|
|
|
ttiles.resize(sizes.x * sizes.y * sizes.z);
|
|
|
|
|
2022-08-30 00:44:02 +02:00
|
|
|
//initializing objects / rects
|
2022-09-01 15:08:03 +02:00
|
|
|
for(const CGObjectInstance * elem : map->objects)
|
2022-08-30 00:44:02 +02:00
|
|
|
{
|
2022-09-01 15:08:03 +02:00
|
|
|
CGObjectInstance *obj = const_cast<CGObjectInstance *>(elem);
|
2022-08-30 00:44:02 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<Animation> animation = graphics->getAnimation(obj);
|
|
|
|
|
|
|
|
//no animation at all
|
|
|
|
if(!animation)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
//empty animation
|
|
|
|
if(animation->size(0) == 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
auto image = animation->getImage(0,0);
|
|
|
|
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);
|
2022-09-06 19:15:13 +02:00
|
|
|
QRect cr(image->width() - fx * tileSize - tileSize,
|
|
|
|
image->height() - fy * tileSize - tileSize,
|
|
|
|
image->width(),
|
|
|
|
image->height());
|
2022-08-30 00:44:02 +02:00
|
|
|
|
2022-09-06 19:15:13 +02:00
|
|
|
TileObject toAdd(obj, cr);
|
2022-08-30 00:44:02 +02:00
|
|
|
|
|
|
|
if( map->isInTheMap(currTile) && // within map
|
2022-08-30 04:38:24 +02:00
|
|
|
cr.x() + cr.width() > 0 && // image has data on this tile
|
|
|
|
cr.y() + cr.height() > 0 &&
|
2022-08-30 00:44:02 +02:00
|
|
|
obj->coveringAt(currTile.x, currTile.y) // object is visible here
|
|
|
|
)
|
|
|
|
{
|
2022-09-06 19:15:13 +02:00
|
|
|
ttiles[index(currTile)].push_back(toAdd);
|
2022-08-30 00:44:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-30 04:38:24 +02:00
|
|
|
for(auto & tt : ttiles)
|
|
|
|
{
|
2022-09-06 19:15:13 +02:00
|
|
|
stable_sort(tt.begin(), tt.end(), objectBlitOrderSorter);
|
2022-08-30 04:38:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MapHandler::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;
|
|
|
|
}
|
|
|
|
|
2022-09-06 19:15:13 +02:00
|
|
|
TileObject::TileObject(CGObjectInstance * obj_, QRect rect_)
|
2022-08-30 04:38:24 +02:00
|
|
|
: obj(obj_),
|
2022-09-06 19:15:13 +02:00
|
|
|
rect(rect_)
|
2022-08-30 04:38:24 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-09-06 19:15:13 +02:00
|
|
|
TileObject::~TileObject()
|
2022-08-30 04:38:24 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<QImage> MapHandler::findFlagBitmap(const CGHeroInstance * hero, int anim, const PlayerColor * color, int group) const
|
|
|
|
{
|
2022-09-06 19:15:13 +02:00
|
|
|
if(!hero || hero->boat)
|
2022-08-30 04:38:24 +02:00
|
|
|
return std::shared_ptr<QImage>();
|
|
|
|
|
|
|
|
return findFlagBitmapInternal(graphics->heroFlagAnimations.at(color->getNum()), anim, group, hero->moveDir, !hero->isStanding);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<QImage> MapHandler::findFlagBitmapInternal(std::shared_ptr<Animation> animation, int anim, int group, ui8 dir, bool moving) const
|
|
|
|
{
|
|
|
|
size_t groupSize = animation->size(group);
|
|
|
|
if(groupSize == 0)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
if(moving)
|
|
|
|
return animation->getImage(anim % groupSize, group);
|
|
|
|
else
|
|
|
|
return animation->getImage((anim / 4) % groupSize, group);
|
|
|
|
}
|
|
|
|
|
|
|
|
MapHandler::AnimBitmapHolder MapHandler::findObjectBitmap(const CGObjectInstance * obj, int anim) const
|
|
|
|
{
|
2022-09-06 19:15:13 +02:00
|
|
|
if(!obj || obj->ID == Obj::HERO || obj->ID == Obj::BOAT)
|
2022-08-30 04:38:24 +02:00
|
|
|
return MapHandler::AnimBitmapHolder();
|
2022-09-06 19:15:13 +02:00
|
|
|
|
2022-08-30 04:38:24 +02:00
|
|
|
// normal object
|
|
|
|
std::shared_ptr<Animation> animation = graphics->getAnimation(obj);
|
|
|
|
size_t groupSize = animation->size();
|
|
|
|
if(groupSize == 0)
|
|
|
|
return MapHandler::AnimBitmapHolder();
|
|
|
|
|
|
|
|
animation->playerColored(obj->tempOwner);
|
2022-09-06 19:15:13 +02:00
|
|
|
auto bitmap = animation->getImage(anim % groupSize);
|
2022-08-30 04:38:24 +02:00
|
|
|
if(!bitmap)
|
|
|
|
return MapHandler::AnimBitmapHolder();
|
2022-09-04 20:29:56 +02:00
|
|
|
|
|
|
|
setPlayerColor(bitmap.get(), obj->tempOwner);
|
2022-08-30 04:38:24 +02:00
|
|
|
|
|
|
|
return MapHandler::AnimBitmapHolder(bitmap);
|
|
|
|
}
|
|
|
|
|
2022-09-06 19:15:13 +02:00
|
|
|
std::vector<TileObject> & MapHandler::getObjects(int x, int y, int z)
|
2022-09-01 13:54:39 +02:00
|
|
|
{
|
2022-09-06 19:15:13 +02:00
|
|
|
return ttiles[index(x, y, z)];
|
2022-09-01 13:54:39 +02:00
|
|
|
}
|
|
|
|
|
2022-09-01 03:51:29 +02:00
|
|
|
void MapHandler::drawObjects(QPainter & painter, int x, int y, int z)
|
2022-08-30 04:38:24 +02:00
|
|
|
{
|
2022-09-01 13:54:39 +02:00
|
|
|
for(auto & object : getObjects(x, y, z))
|
2022-08-30 04:38:24 +02:00
|
|
|
{
|
|
|
|
const CGObjectInstance * obj = object.obj;
|
|
|
|
if (!obj)
|
|
|
|
{
|
|
|
|
logGlobal->error("Stray map object that isn't fading");
|
2022-09-01 13:54:39 +02:00
|
|
|
return;
|
2022-08-30 04:38:24 +02:00
|
|
|
}
|
2022-09-01 13:54:39 +02:00
|
|
|
|
2022-08-30 04:38:24 +02:00
|
|
|
uint8_t animationFrame = 0;
|
2022-09-01 13:54:39 +02:00
|
|
|
|
2022-08-30 04:38:24 +02:00
|
|
|
auto objData = findObjectBitmap(obj, animationFrame);
|
|
|
|
if (objData.objBitmap)
|
2022-08-30 00:44:02 +02:00
|
|
|
{
|
2022-09-01 13:54:39 +02:00
|
|
|
auto pos = obj->getPosition();
|
|
|
|
|
2022-09-06 19:15:13 +02:00
|
|
|
painter.drawImage(QPoint(x * tileSize, y * tileSize), *objData.objBitmap, object.rect);
|
2022-09-04 20:29:56 +02:00
|
|
|
if(objData.flagBitmap)
|
2022-08-30 00:44:02 +02:00
|
|
|
{
|
2022-09-04 20:29:56 +02:00
|
|
|
if (obj->pos.x == pos.x && obj->pos.y == pos.y)
|
2022-09-06 19:15:13 +02:00
|
|
|
painter.drawImage(QPoint(x * tileSize, y * tileSize), *objData.flagBitmap, object.rect);
|
2022-08-30 00:44:02 +02:00
|
|
|
}
|
|
|
|
}
|
2022-08-30 04:38:24 +02:00
|
|
|
}
|
2022-08-30 00:44:02 +02:00
|
|
|
}
|
2022-09-01 13:54:39 +02:00
|
|
|
|
2022-09-06 19:15:13 +02:00
|
|
|
void MapHandler::drawObject(QPainter & painter, const TileObject & object)
|
2022-09-01 13:54:39 +02:00
|
|
|
{
|
|
|
|
const CGObjectInstance * obj = object.obj;
|
|
|
|
if (!obj)
|
|
|
|
{
|
|
|
|
logGlobal->error("Stray map object that isn't fading");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t animationFrame = 0;
|
|
|
|
|
|
|
|
auto objData = findObjectBitmap(obj, animationFrame);
|
|
|
|
if (objData.objBitmap)
|
|
|
|
{
|
|
|
|
auto pos = obj->getPosition();
|
|
|
|
|
2022-09-06 19:15:13 +02:00
|
|
|
painter.drawImage(pos.x * tileSize - object.rect.x(), pos.y * tileSize - object.rect.y(), *objData.objBitmap);
|
2022-09-01 13:54:39 +02:00
|
|
|
if (objData.flagBitmap)
|
|
|
|
{
|
2022-09-04 20:29:56 +02:00
|
|
|
if (obj->pos.x == pos.x && obj->pos.y == pos.y)
|
2022-09-01 13:54:39 +02:00
|
|
|
{
|
2022-09-06 19:15:13 +02:00
|
|
|
painter.drawImage(pos.x * tileSize - object.rect.x(), pos.y * tileSize - object.rect.y(), *objData.flagBitmap);
|
2022-09-04 20:29:56 +02:00
|
|
|
}
|
2022-09-01 13:54:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-09-01 16:07:36 +02:00
|
|
|
|
|
|
|
|
|
|
|
void MapHandler::drawObjectAt(QPainter & painter, const CGObjectInstance * obj, int x, int y)
|
|
|
|
{
|
|
|
|
if (!obj)
|
|
|
|
{
|
|
|
|
logGlobal->error("Stray map object that isn't fading");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t animationFrame = 0;
|
|
|
|
|
|
|
|
auto objData = findObjectBitmap(obj, animationFrame);
|
|
|
|
if (objData.objBitmap)
|
|
|
|
{
|
2022-09-02 23:04:28 +02:00
|
|
|
painter.drawImage(QPoint((x + 1) * 32 - objData.objBitmap->width(), (y + 1) * 32 - objData.objBitmap->height()), *objData.objBitmap);
|
2022-09-01 16:07:36 +02:00
|
|
|
if (objData.flagBitmap)
|
|
|
|
{
|
2022-09-04 20:29:56 +02:00
|
|
|
painter.drawImage(QPoint((x + 1) * 32 - objData.objBitmap->width(), (y + 1) * 32 - objData.objBitmap->height()), *objData.flagBitmap);
|
2022-09-01 16:07:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-09-05 12:10:39 +02:00
|
|
|
|
|
|
|
QRgb MapHandler::getTileColor(int x, int y, int z)
|
|
|
|
{
|
|
|
|
// if object at tile is owned - it will be colored as its owner
|
|
|
|
for(auto & object : getObjects(x, y, z))
|
|
|
|
{
|
|
|
|
//heroes will be blitted later
|
|
|
|
switch (object.obj->ID)
|
|
|
|
{
|
|
|
|
case Obj::HERO:
|
|
|
|
case Obj::PRISON:
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
PlayerColor player = object.obj->getOwner();
|
|
|
|
if(player == PlayerColor::NEUTRAL)
|
|
|
|
return graphics->neutralColor;
|
|
|
|
else
|
|
|
|
if (player < PlayerColor::PLAYER_LIMIT)
|
|
|
|
return graphics->playerColors[player.getNum()];
|
|
|
|
}
|
|
|
|
|
|
|
|
// else - use terrain color (blocked version or normal)
|
|
|
|
auto & tile = map->getTile(int3(x, y, z));
|
|
|
|
auto color = Terrain::Manager::getInfo(tile.terType).minimapUnblocked;
|
|
|
|
if (tile.blocked && (!tile.visitable))
|
|
|
|
color = Terrain::Manager::getInfo(tile.terType).minimapBlocked;
|
|
|
|
|
|
|
|
return qRgb(color[0], color[1], color[2]);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MapHandler::drawMinimapTile(QPainter & painter, int x, int y, int z)
|
|
|
|
{
|
|
|
|
painter.setPen(getTileColor(x, y, z));
|
|
|
|
painter.drawPoint(x, y);
|
|
|
|
}
|
2022-09-06 19:15:13 +02:00
|
|
|
|
|
|
|
void MapHandler::invalidate(int x, int y, int z)
|
|
|
|
{
|
|
|
|
auto & objects = getObjects(x, y, z);
|
|
|
|
|
|
|
|
for(auto obj = objects.begin(); obj != objects.end();)
|
|
|
|
{
|
|
|
|
//object was removed
|
|
|
|
if(std::find(map->objects.begin(), map->objects.end(), obj->obj) == map->objects.end())
|
|
|
|
{
|
|
|
|
obj = objects.erase(obj);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
//object was moved
|
|
|
|
auto & pos = obj->obj->pos;
|
|
|
|
if(pos.z != z || pos.x < x || pos.y < y || pos.x - obj->obj->getWidth() >= x || pos.y - obj->obj->getHeight() >= y)
|
|
|
|
{
|
|
|
|
obj = objects.erase(obj);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
++obj;
|
|
|
|
//invalidate(obj->obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
stable_sort(objects.begin(), objects.end(), objectBlitOrderSorter);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MapHandler::invalidate(CGObjectInstance * obj)
|
|
|
|
{
|
|
|
|
std::shared_ptr<Animation> animation = graphics->getAnimation(obj);
|
|
|
|
|
|
|
|
//no animation at all or empty animation
|
|
|
|
if(!animation || animation->size(0) == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
auto image = animation->getImage(0,0);
|
|
|
|
for(int fx=0; fx < obj->getWidth(); ++fx)
|
|
|
|
{
|
|
|
|
for(int fy=0; fy < obj->getHeight(); ++fy)
|
|
|
|
{
|
|
|
|
//object presented on the tile
|
|
|
|
int3 currTile(obj->pos.x - fx, obj->pos.y - fy, obj->pos.z);
|
|
|
|
QRect cr(image->width() - fx * tileSize - tileSize, image->height() - fy * tileSize - tileSize, image->width(), image->height());
|
|
|
|
|
|
|
|
if( map->isInTheMap(currTile) && // within map
|
|
|
|
cr.x() + cr.width() > 0 && // image has data on this tile
|
|
|
|
cr.y() + cr.height() > 0 &&
|
|
|
|
obj->coveringAt(currTile.x, currTile.y) // object is visible here
|
|
|
|
)
|
|
|
|
{
|
|
|
|
auto & objects = ttiles[index(currTile)];
|
|
|
|
bool found = false;
|
|
|
|
for(auto & o : objects)
|
|
|
|
{
|
|
|
|
if(o.obj == obj)
|
|
|
|
{
|
|
|
|
o.rect = cr;
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(!found)
|
|
|
|
objects.emplace_back(obj, cr);
|
|
|
|
|
|
|
|
stable_sort(objects.begin(), objects.end(), objectBlitOrderSorter);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<int3> MapHandler::geTilesUnderObject(CGObjectInstance * obj) const
|
|
|
|
{
|
|
|
|
std::vector<int3> result;
|
|
|
|
for(int fx=0; fx < obj->getWidth(); ++fx)
|
|
|
|
{
|
|
|
|
for(int fy=0; fy < obj->getHeight(); ++fy)
|
|
|
|
{
|
|
|
|
//object presented on the tile
|
|
|
|
int3 currTile(obj->pos.x - fx, obj->pos.y - fy, obj->pos.z);
|
|
|
|
if(map->isInTheMap(currTile) && // within map
|
|
|
|
obj->coveringAt(currTile.x, currTile.y) // object is visible here
|
|
|
|
)
|
|
|
|
{
|
|
|
|
result.push_back(currTile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MapHandler::invalidate(const std::vector<int3> & tiles)
|
|
|
|
{
|
|
|
|
for(auto & currTile : tiles)
|
|
|
|
{
|
|
|
|
invalidate(currTile.x, currTile.y, currTile.z);
|
|
|
|
}
|
|
|
|
}
|