1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546
|
/*
* 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
*
*/
//code is copied from vcmiclient/mapHandler.cpp with minimal changes
#include "StdInc.h"
#include "maphandler.h"
#include "graphics.h"
#include "../lib/mapping/CMap.h"
#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"
const int tileSize = 32;
static bool objectBlitOrderSorter(const TileObject & a, const TileObject & b)
{
return MapHandler::compareObjectBlitOrder(a.obj, b.obj);
}
int MapHandler::index(int x, int y, int z) const
{
return z * (sizes.x * sizes.y) + y * sizes.x + x;
}
int MapHandler::index(const int3 & p) const
{
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;
//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()
{
auto loadFlipped = [](TFlippedAnimations & animation, TFlippedCache & cache, const std::map<std::string, std::string> & files)
{
for(auto & type : files)
{
animation[type.first] = make_unique<Animation>(type.second);
animation[type.first]->preload();
const size_t views = animation[type.first]->size(0);
cache[type.first].resize(views);
for(int j = 0; j < views; j++)
cache[type.first][j] = animation[type.first]->getImage(j);
}
};
std::map<std::string, std::string> terrainFiles;
std::map<std::string, std::string> roadFiles;
std::map<std::string, std::string> riverFiles;
for(const auto & terrain : VLC->terrainTypeHandler->terrains())
{
terrainFiles[terrain.name] = terrain.tilesFilename;
}
for(const auto & river : VLC->terrainTypeHandler->rivers())
{
riverFiles[river.fileName] = river.fileName;
}
for(const auto & road : VLC->terrainTypeHandler->roads())
{
roadFiles[road.fileName] = road.fileName;
}
loadFlipped(terrainAnimations, terrainImages, terrainFiles);
loadFlipped(riverAnimations, riverImages, riverFiles);
loadFlipped(roadAnimations, roadImages, roadFiles);
}
void MapHandler::drawTerrainTile(QPainter & painter, int x, int y, int z)
{
auto & tinfo = map->getTile(int3(x, y, z));
ui8 rotation = tinfo.extTileFlags % 4;
//TODO: use ui8 instead of string key
auto terrainName = tinfo.terType->name;
if(terrainImages.at(terrainName).size() <= tinfo.terView)
return;
bool hflip = (rotation == 1 || rotation == 3), vflip = (rotation == 2 || rotation == 3);
painter.drawImage(x * tileSize, y * tileSize, terrainImages.at(terrainName)[tinfo.terView]->mirrored(hflip, vflip));
}
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(tinfoUpper && tinfoUpper->roadType->id != Road::NO_ROAD)
{
auto roadName = tinfoUpper->roadType->fileName;
QRect source(0, tileSize / 2, tileSize, tileSize / 2);
ui8 rotation = (tinfoUpper->extTileFlags >> 4) % 4;
bool hflip = (rotation == 1 || rotation == 3), vflip = (rotation == 2 || rotation == 3);
if(roadImages.at(roadName).size() > tinfoUpper->roadDir)
{
painter.drawImage(QPoint(x * tileSize, y * tileSize), roadImages.at(roadName)[tinfoUpper->roadDir]->mirrored(hflip, vflip), source);
}
}
if(tinfo.roadType->id != Road::NO_ROAD) //print road from this tile
{
auto roadName = tinfo.roadType->fileName;
QRect source(0, 0, tileSize, tileSize / 2);
ui8 rotation = (tinfo.extTileFlags >> 4) % 4;
bool hflip = (rotation == 1 || rotation == 3), vflip = (rotation == 2 || rotation == 3);
if(roadImages.at(roadName).size() > tinfo.roadDir)
{
painter.drawImage(QPoint(x * tileSize, y * tileSize + tileSize / 2), roadImages.at(roadName)[tinfo.roadDir]->mirrored(hflip, vflip), source);
}
}
}
void MapHandler::drawRiver(QPainter & painter, int x, int y, int z)
{
auto & tinfo = map->getTile(int3(x, y, z));
if(tinfo.riverType->id == River::NO_RIVER)
return;
//TODO: use ui8 instead of string key
auto riverName = tinfo.riverType->fileName;
if(riverImages.at(riverName).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(riverName)[tinfo.riverDir]->mirrored(hflip, vflip));
}
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 && player < PlayerColor::PLAYER_LIMIT)
color = graphics->playerColors.at(player.getNum());
sur->setColor(5, color);
}
else
logGlobal->warn("Warning, setPlayerColor called on not 8bpp surface!");
}
void MapHandler::initObjectRects()
{
ttiles.resize(sizes.x * sizes.y * sizes.z);
//initializing objects / rects
for(const CGObjectInstance * elem : map->objects)
{
CGObjectInstance *obj = const_cast<CGObjectInstance *>(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;
}
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, obj->ID == Obj::HERO ? 2 : 0);
if(!image)
{
//workaround for prisons
image = animation->getImage(0, 0);
if(!image)
continue;
}
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);
QRect cr(image->width() - fx * tileSize - tileSize,
image->height() - fy * tileSize - tileSize,
image->width(),
image->height());
TileObject toAdd(obj, cr);
if( map->isInTheMap(currTile) && // within map
cr.x() + cr.width() > 0 && // image has data on this tile
cr.y() + cr.height() > 0)
{
ttiles[index(currTile)].push_back(toAdd);
}
}
}
}
for(auto & tt : ttiles)
{
stable_sort(tt.begin(), tt.end(), objectBlitOrderSorter);
}
}
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;
}
TileObject::TileObject(CGObjectInstance * obj_, QRect rect_)
: obj(obj_),
rect(rect_)
{
}
TileObject::~TileObject()
{
}
std::shared_ptr<QImage> MapHandler::findFlagBitmap(const CGHeroInstance * hero, int anim, const PlayerColor color, int group) const
{
if(!hero || hero->boat)
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, int group) const
{
if(!obj)
return MapHandler::AnimBitmapHolder();
// normal object
std::shared_ptr<Animation> animation = graphics->getAnimation(obj);
size_t groupSize = animation->size(group);
if(groupSize == 0)
return MapHandler::AnimBitmapHolder();
animation->playerColored(obj->tempOwner);
auto bitmap = animation->getImage(anim % groupSize, group);
if(!bitmap)
return MapHandler::AnimBitmapHolder();
setPlayerColor(bitmap.get(), obj->tempOwner);
return MapHandler::AnimBitmapHolder(bitmap);
}
std::vector<TileObject> & MapHandler::getObjects(int x, int y, int z)
{
return ttiles[index(x, y, z)];
}
void MapHandler::drawObjects(QPainter & painter, int x, int y, int z)
{
for(auto & object : getObjects(x, y, z))
{
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, obj->ID == Obj::HERO ? 2 : 0);
if(obj->ID == Obj::HERO && obj->tempOwner.isValidPlayer())
objData.flagBitmap = findFlagBitmap(dynamic_cast<const CGHeroInstance*>(obj), 0, obj->tempOwner, 4);
if(objData.objBitmap)
{
auto pos = obj->getPosition();
painter.drawImage(QPoint(x * tileSize, y * tileSize), *objData.objBitmap, object.rect);
if(objData.flagBitmap)
{
if(x == pos.x && y == pos.y)
painter.drawImage(QPoint((x - 2) * tileSize, (y - 1) * tileSize), *objData.flagBitmap);
}
}
}
}
void MapHandler::drawObject(QPainter & painter, const TileObject & object)
{
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, obj->ID == Obj::HERO ? 2 : 0);
if(obj->ID == Obj::HERO && obj->tempOwner.isValidPlayer())
objData.flagBitmap = findFlagBitmap(dynamic_cast<const CGHeroInstance*>(obj), 0, obj->tempOwner, 0);
if (objData.objBitmap)
{
auto pos = obj->getPosition();
painter.drawImage(pos.x * tileSize - object.rect.x(), pos.y * tileSize - object.rect.y(), *objData.objBitmap);
if (objData.flagBitmap)
{
if(object.rect.x() == pos.x && object.rect.y() == pos.y)
painter.drawImage(pos.x * tileSize - object.rect.x(), pos.y * tileSize - object.rect.y(), *objData.flagBitmap);
}
}
}
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, obj->ID == Obj::HERO ? 2 : 0);
if(obj->ID == Obj::HERO && obj->tempOwner.isValidPlayer())
objData.flagBitmap = findFlagBitmap(dynamic_cast<const CGHeroInstance*>(obj), 0, obj->tempOwner, 4);
if (objData.objBitmap)
{
painter.drawImage(QPoint((x + 1) * 32 - objData.objBitmap->width(), (y + 1) * 32 - objData.objBitmap->height()), *objData.objBitmap);
if (objData.flagBitmap)
painter.drawImage(QPoint((x + 1) * 32 - objData.objBitmap->width(), (y + 1) * 32 - objData.objBitmap->height()), *objData.flagBitmap);
}
}
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))
{
if(!object.obj->getBlockedPos().count(int3(x, y, z)))
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 = tile.terType->minimapUnblocked;
if (tile.blocked && (!tile.visitable))
color = 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);
}
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;
}
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, obj->ID == Obj::HERO ? 2 : 0);
if(!image)
return;
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)
{
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::getTilesUnderObject(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
{
result.push_back(currTile);
}
}
}
return result;
}
void MapHandler::invalidateObjects()
{
for(auto obj : map->objects)
{
invalidate(obj);
}
}
void MapHandler::invalidate(const std::vector<int3> & tiles)
{
for(auto & currTile : tiles)
{
invalidate(currTile.x, currTile.y, currTile.z);
}
}
|