123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262 |
- /*
- * CObstacleInstance.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 "CObstacleInstance.h"
- #include "../CHeroHandler.h"
- #include "../ObstacleHandler.h"
- #include "../VCMI_Lib.h"
- #include "../serializer/JsonDeserializer.h"
- #include "../serializer/JsonSerializer.h"
- VCMI_LIB_NAMESPACE_BEGIN
- const ObstacleInfo & CObstacleInstance::getInfo() const
- {
- assert( obstacleType == USUAL || obstacleType == ABSOLUTE_OBSTACLE);
- return *Obstacle(ID).getInfo();
- }
- std::vector<BattleHex> CObstacleInstance::getBlockedTiles() const
- {
- if(blocksTiles())
- return getAffectedTiles();
- return std::vector<BattleHex>();
- }
- std::vector<BattleHex> CObstacleInstance::getStoppingTile() const
- {
- if(stopsMovement())
- return getAffectedTiles();
- return std::vector<BattleHex>();
- }
- std::vector<BattleHex> CObstacleInstance::getAffectedTiles() const
- {
- switch(obstacleType)
- {
- case ABSOLUTE_OBSTACLE:
- case USUAL:
- return getInfo().getBlocked(pos);
- default:
- assert(0);
- return std::vector<BattleHex>();
- }
- }
- bool CObstacleInstance::visibleForSide(BattleSide side, bool hasNativeStack) const
- {
- //by default obstacle is visible for everyone
- return true;
- }
- const AnimationPath & CObstacleInstance::getAnimation() const
- {
- return getInfo().animation;
- }
- const AnimationPath & CObstacleInstance::getAppearAnimation() const
- {
- return getInfo().appearAnimation;
- }
- const AudioPath & CObstacleInstance::getAppearSound() const
- {
- return getInfo().appearSound;
- }
- int CObstacleInstance::getAnimationYOffset(int imageHeight) const
- {
- int offset = imageHeight % 42;
- if(obstacleType == CObstacleInstance::USUAL)
- {
- if(getInfo().blockedTiles.front() < 0 || offset > 37) //second or part is for holy ground ID=62,65,63
- offset -= 42;
- }
- return offset;
- }
- bool CObstacleInstance::stopsMovement() const
- {
- return obstacleType == MOAT;
- }
- bool CObstacleInstance::blocksTiles() const
- {
- return obstacleType == USUAL || obstacleType == ABSOLUTE_OBSTACLE ;
- }
- bool CObstacleInstance::triggersEffects() const
- {
- return getTrigger() != SpellID::NONE;
- }
- SpellID CObstacleInstance::getTrigger() const
- {
- return SpellID::NONE;
- }
- void CObstacleInstance::serializeJson(JsonSerializeFormat & handler)
- {
- auto hidden = false;
- auto needAnimationOffsetFix = obstacleType == CObstacleInstance::USUAL;
- int animationYOffset = 0;
-
- if(getInfo().blockedTiles.front() < 0) //TODO: holy ground ID=62,65,63
- animationYOffset -= 42;
- //We need only a subset of obstacle info for correct render
- handler.serializeInt("position", pos);
- handler.serializeInt("animationYOffset", animationYOffset);
- handler.serializeBool("hidden", hidden);
- handler.serializeBool("needAnimationOffsetFix", needAnimationOffsetFix);
- }
- void CObstacleInstance::toInfo(ObstacleChanges & info, BattleChanges::EOperation operation)
- {
- info.id = uniqueID;
- info.operation = operation;
- info.data.clear();
- JsonSerializer ser(nullptr, info.data);
- ser.serializeStruct("obstacle", *this);
- }
- SpellCreatedObstacle::SpellCreatedObstacle()
- : turnsRemaining(-1),
- casterSpellPower(0),
- spellLevel(0),
- casterSide(BattleSide::NONE),
- hidden(false),
- passable(false),
- trigger(false),
- trap(false),
- removeOnTrigger(false),
- revealed(false),
- animationYOffset(0),
- nativeVisible(true),
- minimalDamage(0)
- {
- obstacleType = SPELL_CREATED;
- }
- bool SpellCreatedObstacle::visibleForSide(BattleSide side, bool hasNativeStack) const
- {
- //we hide mines and not discovered quicksands
- //quicksands are visible to the caster or if owned unit stepped into that particular patch
- //additionally if side has a native unit, mines/quicksands will be visible
- //but it is not a case for a moat, so, hasNativeStack should not work for moats
- auto nativeVis = hasNativeStack && nativeVisible;
- return casterSide == side || !hidden || revealed || nativeVis;
- }
- bool SpellCreatedObstacle::blocksTiles() const
- {
- return !passable;
- }
- bool SpellCreatedObstacle::stopsMovement() const
- {
- return trap;
- }
- SpellID SpellCreatedObstacle::getTrigger() const
- {
- return trigger;
- }
- void SpellCreatedObstacle::fromInfo(const ObstacleChanges & info)
- {
- uniqueID = info.id;
- if(info.operation != ObstacleChanges::EOperation::ADD && info.operation != ObstacleChanges::EOperation::UPDATE)
- logGlobal->error("ADD or UPDATE operation expected");
- JsonDeserializer deser(nullptr, info.data);
- deser.serializeStruct("obstacle", *this);
- }
- void SpellCreatedObstacle::serializeJson(JsonSerializeFormat & handler)
- {
- handler.serializeInt("spell", ID);
- handler.serializeInt("position", pos);
- handler.serializeInt("turnsRemaining", turnsRemaining);
- handler.serializeInt("casterSpellPower", casterSpellPower);
- handler.serializeInt("spellLevel", spellLevel);
- handler.serializeInt("casterSide", casterSide);
- handler.serializeInt("minimalDamage", minimalDamage);
- handler.serializeInt("type", obstacleType);
- handler.serializeBool("hidden", hidden);
- handler.serializeBool("revealed", revealed);
- handler.serializeBool("passable", passable);
- handler.serializeId("trigger", trigger, SpellID::NONE);
- handler.serializeBool("trap", trap);
- handler.serializeBool("removeOnTrigger", removeOnTrigger);
- handler.serializeBool("nativeVisible", nativeVisible);
- handler.serializeStruct("appearSound", appearSound);
- handler.serializeStruct("appearAnimation", appearAnimation);
- handler.serializeStruct("animation", animation);
- handler.serializeInt("animationYOffset", animationYOffset);
- {
- JsonArraySerializer customSizeJson = handler.enterArray("customSize");
- customSizeJson.syncSize(customSize, JsonNode::JsonType::DATA_INTEGER);
- for(size_t index = 0; index < customSizeJson.size(); index++)
- customSizeJson.serializeInt(index, customSize.at(index));
- }
- }
- std::vector<BattleHex> SpellCreatedObstacle::getAffectedTiles() const
- {
- return customSize;
- }
- void SpellCreatedObstacle::battleTurnPassed()
- {
- if(turnsRemaining > 0)
- turnsRemaining--;
- }
- const AnimationPath & SpellCreatedObstacle::getAnimation() const
- {
- return animation;
- }
- const AnimationPath & SpellCreatedObstacle::getAppearAnimation() const
- {
- return appearAnimation;
- }
- const AudioPath & SpellCreatedObstacle::getAppearSound() const
- {
- return appearSound;
- }
- int SpellCreatedObstacle::getAnimationYOffset(int imageHeight) const
- {
- int offset = imageHeight % 42;
- if(obstacleType == CObstacleInstance::SPELL_CREATED || obstacleType == CObstacleInstance::MOAT)
- {
- offset += animationYOffset;
- }
- return offset;
- }
- VCMI_LIB_NAMESPACE_END
|