123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278 |
- /*
- * MapRendererContext.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 "MapRendererContext.h"
- #include "mapHandler.h"
- #include "../CGameInfo.h"
- #include "../CPlayerInterface.h"
- #include "../adventureMap/CAdvMapInt.h"
- //#include "../gui/CGuiHandler.h"
- //#include "../render/CAnimation.h"
- //#include "../render/Canvas.h"
- //#include "../render/IImage.h"
- //#include "../renderSDL/SDL_Extensions.h"
- //
- #include "../../CCallback.h"
- #include "../../lib/CConfigHandler.h"
- #include "../../lib/mapObjects/CGHeroInstance.h"
- #include "../../lib/mapping/CMap.h"
- MapObjectsSorter::MapObjectsSorter(const IMapRendererContext & context)
- : context(context)
- {
- }
- bool MapObjectsSorter::operator()(const ObjectInstanceID & left, const ObjectInstanceID & right) const
- {
- return (*this)(context.getObject(left), context.getObject(right));
- }
- bool MapObjectsSorter::operator()(const CGObjectInstance * left, const CGObjectInstance * right) const
- {
- //FIXME: remove mh access
- return CGI->mh->compareObjectBlitOrder(left, right);
- }
- int3 MapRendererContext::getMapSize() const
- {
- return LOCPLINT->cb->getMapSize();
- }
- bool MapRendererContext::isInMap(const int3 & coordinates) const
- {
- return LOCPLINT->cb->isInTheMap(coordinates);
- }
- const TerrainTile & MapRendererContext::getMapTile(const int3 & coordinates) const
- {
- return CGI->mh->getMap()->getTile(coordinates);
- }
- const CGObjectInstance * MapRendererContext::getObject(ObjectInstanceID objectID) const
- {
- return CGI->mh->getMap()->objects.at(objectID.getNum());
- }
- bool MapRendererContext::isVisible(const int3 & coordinates) const
- {
- return LOCPLINT->cb->isVisible(coordinates) || settings["session"]["spectate"].Bool();
- }
- const CGPath * MapRendererContext::currentPath() const
- {
- const auto * hero = adventureInt->curHero();
- if(!hero)
- return nullptr;
- if(!LOCPLINT->paths.hasPath(hero))
- return nullptr;
- return &LOCPLINT->paths.getPath(hero);
- }
- size_t MapRendererContext::objectImageIndex(ObjectInstanceID objectID, size_t groupSize) const
- {
- assert(groupSize > 0);
- if(groupSize == 0)
- return 0;
- // H3 timing for adventure map objects animation is 180 ms
- // Terrain animations also use identical interval, however those are only present in HotA and/or HD Mod
- size_t baseFrameTime = 180;
- // hero movement animation always plays at ~50ms / frame
- // in-game setting only affect movement across screen
- if(movementAnimation && movementAnimation->target == objectID)
- baseFrameTime = 50;
- size_t frameCounter = animationTime / baseFrameTime;
- size_t frameIndex = frameCounter % groupSize;
- return frameIndex;
- }
- size_t MapRendererContext::terrainImageIndex(size_t groupSize) const
- {
- size_t baseFrameTime = 180;
- size_t frameCounter = animationTime / baseFrameTime;
- size_t frameIndex = frameCounter % groupSize;
- return frameIndex;
- }
- //Point MapRendererContext::getTileSize() const
- //{
- // return Point(32, 32);
- //}
- bool MapRendererContext::showOverlay() const
- {
- return worldViewModeActive;
- }
- bool MapRendererContext::showGrid() const
- {
- return settings["gameTweaks"]["showGrid"].Bool();
- }
- bool MapRendererContext::showVisitable() const
- {
- return settings["session"]["showVisitable"].Bool();
- }
- bool MapRendererContext::showBlockable() const
- {
- return settings["session"]["showBlockable"].Bool();
- }
- MapRendererContext::MapRendererContext()
- {
- auto mapSize = getMapSize();
- objects.resize(boost::extents[mapSize.z][mapSize.x][mapSize.y]);
- for(const auto & obj : CGI->mh->getMap()->objects)
- addObject(obj);
- }
- void MapRendererContext::addObject(const CGObjectInstance * obj)
- {
- if(!obj)
- return;
- 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);
- if(isInMap(currTile) && obj->coveringAt(currTile.x, currTile.y))
- {
- auto & container = objects[currTile.z][currTile.x][currTile.y];
- container.push_back(obj->id);
- boost::range::sort(container, MapObjectsSorter(*this));
- }
- }
- }
- }
- void MapRendererContext::addMovingObject(const CGObjectInstance * object, const int3 & tileFrom, const int3 & tileDest)
- {
- int xFrom = std::min(tileFrom.x, tileDest.x) - object->getWidth();
- int xDest = std::max(tileFrom.x, tileDest.x);
- int yFrom = std::min(tileFrom.y, tileDest.y) - object->getHeight();
- int yDest = std::max(tileFrom.y, tileDest.y);
- for(int x = xFrom; x <= xDest; ++x)
- {
- for(int y = yFrom; y <= yDest; ++y)
- {
- int3 currTile(x, y, object->pos.z);
- if(isInMap(currTile))
- {
- auto & container = objects[currTile.z][currTile.x][currTile.y];
- container.push_back(object->id);
- boost::range::sort(container, MapObjectsSorter(*this));
- }
- }
- }
- }
- void MapRendererContext::removeObject(const CGObjectInstance * object)
- {
- for(int z = 0; z < getMapSize().z; z++)
- for(int x = 0; x < getMapSize().x; x++)
- for(int y = 0; y < getMapSize().y; y++)
- vstd::erase(objects[z][x][y], object->id);
- }
- const MapRendererContext::MapObjectsList & MapRendererContext::getObjects(const int3 & coordinates) const
- {
- assert(isInMap(coordinates));
- return objects[coordinates.z][coordinates.x][coordinates.y];
- }
- size_t MapRendererContext::objectGroupIndex(ObjectInstanceID objectID) const
- {
- const CGObjectInstance * obj = getObject(objectID);
- // TODO
- static const std::vector<size_t> moveGroups = {99, 10, 5, 6, 7, 8, 9, 12, 11};
- static const std::vector<size_t> idleGroups = {99, 13, 0, 1, 2, 3, 4, 15, 14};
- if(obj->ID == Obj::HERO)
- {
- const auto * hero = dynamic_cast<const CGHeroInstance *>(obj);
- if(movementAnimation && movementAnimation->target == objectID)
- return moveGroups[hero->moveDir];
- return idleGroups[hero->moveDir];
- }
- if(obj->ID == Obj::BOAT)
- {
- const auto * boat = dynamic_cast<const CGBoat *>(obj);
- uint8_t direction = boat->hero ? boat->hero->moveDir : boat->direction;
- if(movementAnimation && movementAnimation->target == objectID)
- return moveGroups[direction];
- return idleGroups[direction];
- }
- return 0;
- }
- Point MapRendererContext::objectImageOffset(ObjectInstanceID objectID, const int3 & coordinates) const
- {
- if(movementAnimation && movementAnimation->target == objectID)
- {
- int3 offsetTilesFrom = movementAnimation->tileFrom - coordinates;
- int3 offsetTilesDest = movementAnimation->tileDest - coordinates;
- Point offsetPixelsFrom = Point(offsetTilesFrom) * Point(32, 32);
- Point offsetPixelsDest = Point(offsetTilesDest) * Point(32, 32);
- Point result = vstd::lerp(offsetPixelsFrom, offsetPixelsDest, movementAnimation->progress);
- return result;
- }
- const CGObjectInstance * object = getObject(objectID);
- int3 offsetTiles(object->getPosition() - coordinates);
- return Point(offsetTiles) * Point(32, 32);
- }
- double MapRendererContext::objectTransparency(ObjectInstanceID objectID) const
- {
- const CGObjectInstance * object = getObject(objectID);
- if(object && object->ID == Obj::HERO)
- {
- const auto * hero = dynamic_cast<const CGHeroInstance *>(object);
- if(hero->inTownGarrison)
- return 0;
- if(hero->boat)
- return 0;
- }
- if(fadeOutAnimation && objectID == fadeOutAnimation->target)
- return 1.0 - fadeOutAnimation->progress;
- if(fadeInAnimation && objectID == fadeInAnimation->target)
- return fadeInAnimation->progress;
- return 1.0;
- }
|