ScuttleBoatMechanics.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*
  2. * ScuttleBoatMechanics.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "ScuttleBoatMechanics.h"
  12. #include "../CSpellHandler.h"
  13. #include "../../callback/IGameInfoCallback.h"
  14. #include "../../mapObjects/CGHeroInstance.h"
  15. #include "../../mapping/CMap.h"
  16. #include "../../networkPacks/PacksForClient.h"
  17. VCMI_LIB_NAMESPACE_BEGIN
  18. ScuttleBoatMechanics::ScuttleBoatMechanics(const CSpell * s)
  19. : AdventureSpellMechanics(s)
  20. {
  21. }
  22. bool ScuttleBoatMechanics::canBeCastAtImpl(spells::Problem & problem, const IGameInfoCallback * cb, const spells::Caster * caster, const int3 & pos) const
  23. {
  24. if(!cb->isInTheMap(pos))
  25. return false;
  26. if(caster->getHeroCaster())
  27. {
  28. int3 casterPosition = caster->getHeroCaster()->getSightCenter();
  29. if(!isInScreenRange(casterPosition, pos))
  30. return false;
  31. }
  32. if(!cb->isVisibleFor(pos, caster->getCasterOwner()))
  33. return false;
  34. const TerrainTile * t = cb->getTile(pos);
  35. if(!t || t->visitableObjects.empty())
  36. return false;
  37. const CGObjectInstance * topObject = cb->getObj(t->visitableObjects.back());
  38. if(topObject->ID != Obj::BOAT)
  39. return false;
  40. return true;
  41. }
  42. ESpellCastResult ScuttleBoatMechanics::applyAdventureEffects(SpellCastEnvironment * env, const AdventureSpellCastParameters & parameters) const
  43. {
  44. const auto schoolLevel = parameters.caster->getSpellSchoolLevel(owner);
  45. //check if spell works at all
  46. if(env->getRNG()->nextInt(0, 99) >= owner->getLevelPower(schoolLevel)) //power is % chance of success
  47. {
  48. InfoWindow iw;
  49. iw.player = parameters.caster->getCasterOwner();
  50. iw.text.appendLocalString(EMetaText::GENERAL_TXT, 337); //%s tried to scuttle the boat, but failed
  51. parameters.caster->getCasterName(iw.text);
  52. env->apply(iw);
  53. return ESpellCastResult::OK;
  54. }
  55. const TerrainTile & t = env->getMap()->getTile(parameters.pos);
  56. RemoveObject ro;
  57. ro.initiator = parameters.caster->getCasterOwner();
  58. ro.objectID = t.visitableObjects.back();
  59. env->apply(ro);
  60. return ESpellCastResult::OK;
  61. }
  62. VCMI_LIB_NAMESPACE_END