CObjectHandler.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. /*
  2. * CObjectHandler.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 "CObjectHandler.h"
  12. #include "../NetPacks.h"
  13. #include "../CGeneralTextHandler.h"
  14. #include "../CHeroHandler.h"
  15. #include "../CSoundBase.h"
  16. #include "../filesystem/ResourceID.h"
  17. #include "../IGameCallback.h"
  18. #include "../CGameState.h"
  19. #include "CObjectClassesHandler.h"
  20. using namespace boost::assign;
  21. IGameCallback * IObjectInterface::cb = nullptr;
  22. ///helpers
  23. static void openWindow(const OpenWindow::EWindow type, const int id1, const int id2 = -1)
  24. {
  25. OpenWindow ow;
  26. ow.window = type;
  27. ow.id1 = id1;
  28. ow.id2 = id2;
  29. IObjectInterface::cb->sendAndApply(&ow);
  30. }
  31. static void showInfoDialog(const PlayerColor playerID, const ui32 txtID, const ui16 soundID)
  32. {
  33. InfoWindow iw;
  34. iw.soundID = soundID;
  35. iw.player = playerID;
  36. iw.text.addTxt(MetaString::ADVOB_TXT,txtID);
  37. IObjectInterface::cb->sendAndApply(&iw);
  38. }
  39. /*static void showInfoDialog(const ObjectInstanceID heroID, const ui32 txtID, const ui16 soundID)
  40. {
  41. const PlayerColor playerID = IObjectInterface::cb->getOwner(heroID);
  42. showInfoDialog(playerID,txtID,soundID);
  43. }*/
  44. static void showInfoDialog(const CGHeroInstance* h, const ui32 txtID, const ui16 soundID)
  45. {
  46. const PlayerColor playerID = h->getOwner();
  47. showInfoDialog(playerID,txtID,soundID);
  48. }
  49. ///IObjectInterface
  50. void IObjectInterface::onHeroVisit(const CGHeroInstance * h) const
  51. {}
  52. void IObjectInterface::onHeroLeave(const CGHeroInstance * h) const
  53. {}
  54. void IObjectInterface::newTurn () const
  55. {}
  56. IObjectInterface::~IObjectInterface()
  57. {}
  58. IObjectInterface::IObjectInterface()
  59. {}
  60. void IObjectInterface::initObj()
  61. {}
  62. void IObjectInterface::setProperty( ui8 what, ui32 val )
  63. {}
  64. bool IObjectInterface::wasVisited (PlayerColor player) const
  65. {
  66. return false;
  67. }
  68. bool IObjectInterface::wasVisited (const CGHeroInstance * h) const
  69. {
  70. return false;
  71. }
  72. void IObjectInterface::postInit()
  73. {}
  74. void IObjectInterface::preInit()
  75. {}
  76. void IObjectInterface::battleFinished(const CGHeroInstance *hero, const BattleResult &result) const
  77. {}
  78. void IObjectInterface::blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const
  79. {}
  80. void IObjectInterface::garrisonDialogClosed(const CGHeroInstance *hero) const
  81. {}
  82. void IObjectInterface::heroLevelUpDone(const CGHeroInstance *hero) const
  83. {}
  84. CObjectHandler::CObjectHandler()
  85. {
  86. logGlobal->traceStream() << "\t\tReading resources prices ";
  87. const JsonNode config2(ResourceID("config/resources.json"));
  88. for(const JsonNode &price : config2["resources_prices"].Vector())
  89. {
  90. resVals.push_back(price.Float());
  91. }
  92. logGlobal->traceStream() << "\t\tDone loading resource prices!";
  93. }
  94. PlayerColor CGObjectInstance::getOwner() const
  95. {
  96. //if (state)
  97. // return state->owner;
  98. //else
  99. return tempOwner; //won't have owner
  100. }
  101. CGObjectInstance::CGObjectInstance():
  102. pos(-1,-1,-1),
  103. ID(Obj::NO_OBJ),
  104. subID(-1),
  105. tempOwner(PlayerColor::UNFLAGGABLE),
  106. blockVisit(false)
  107. {
  108. }
  109. CGObjectInstance::~CGObjectInstance()
  110. {
  111. }
  112. void CGObjectInstance::setOwner(PlayerColor ow)
  113. {
  114. tempOwner = ow;
  115. }
  116. int CGObjectInstance::getWidth() const//returns width of object graphic in tiles
  117. {
  118. return appearance.getWidth();
  119. }
  120. int CGObjectInstance::getHeight() const //returns height of object graphic in tiles
  121. {
  122. return appearance.getHeight();
  123. }
  124. bool CGObjectInstance::visitableAt(int x, int y) const //returns true if object is visitable at location (x, y) form left top tile of image (x, y in tiles)
  125. {
  126. return appearance.isVisitableAt(pos.x - x, pos.y - y);
  127. }
  128. bool CGObjectInstance::blockingAt(int x, int y) const
  129. {
  130. return appearance.isBlockedAt(pos.x - x, pos.y - y);
  131. }
  132. bool CGObjectInstance::coveringAt(int x, int y) const
  133. {
  134. return appearance.isVisibleAt(pos.x - x, pos.y - y);
  135. }
  136. std::set<int3> CGObjectInstance::getBlockedPos() const
  137. {
  138. std::set<int3> ret;
  139. for(int w=0; w<getWidth(); ++w)
  140. {
  141. for(int h=0; h<getHeight(); ++h)
  142. {
  143. if (appearance.isBlockedAt(w, h))
  144. ret.insert(int3(pos.x - w, pos.y - h, pos.z));
  145. }
  146. }
  147. return ret;
  148. }
  149. std::set<int3> CGObjectInstance::getBlockedOffsets() const
  150. {
  151. return appearance.getBlockedOffsets();
  152. }
  153. void CGObjectInstance::setType(si32 ID, si32 subID)
  154. {
  155. const TerrainTile &tile = cb->gameState()->map->getTile(visitablePos());
  156. this->ID = Obj(ID);
  157. this->subID = subID;
  158. //recalculate blockvis tiles - new appearance might have different blockmap than before
  159. cb->gameState()->map->removeBlockVisTiles(this, true);
  160. auto handler = VLC->objtypeh->getHandlerFor(ID, subID);
  161. appearance = handler->getTemplates(tile.terType).at(0);
  162. cb->gameState()->map->addBlockVisTiles(this);
  163. }
  164. void CGObjectInstance::initObj()
  165. {
  166. switch(ID)
  167. {
  168. case Obj::TAVERN:
  169. blockVisit = true;
  170. break;
  171. }
  172. }
  173. void CGObjectInstance::setProperty( ui8 what, ui32 val )
  174. {
  175. setPropertyDer(what, val); // call this before any actual changes (needed at least for dwellings)
  176. switch(what)
  177. {
  178. case ObjProperty::OWNER:
  179. tempOwner = PlayerColor(val);
  180. break;
  181. case ObjProperty::BLOCKVIS:
  182. blockVisit = val;
  183. break;
  184. case ObjProperty::ID:
  185. ID = Obj(val);
  186. break;
  187. case ObjProperty::SUBID:
  188. subID = val;
  189. break;
  190. }
  191. }
  192. void CGObjectInstance::setPropertyDer( ui8 what, ui32 val )
  193. {}
  194. int3 CGObjectInstance::getSightCenter() const
  195. {
  196. return visitablePos();
  197. }
  198. int CGObjectInstance::getSightRadious() const
  199. {
  200. return 3;
  201. }
  202. int3 CGObjectInstance::getVisitableOffset() const
  203. {
  204. return appearance.getVisitableOffset();
  205. }
  206. void CGObjectInstance::giveDummyBonus(ObjectInstanceID heroID, ui8 duration) const
  207. {
  208. GiveBonus gbonus;
  209. gbonus.bonus.type = Bonus::NONE;
  210. gbonus.id = heroID.getNum();
  211. gbonus.bonus.duration = duration;
  212. gbonus.bonus.source = Bonus::OBJECT;
  213. gbonus.bonus.sid = ID;
  214. cb->giveHeroBonus(&gbonus);
  215. }
  216. std::string CGObjectInstance::getObjectName() const
  217. {
  218. return VLC->objtypeh->getObjectName(ID, subID);
  219. }
  220. std::string CGObjectInstance::getHoverText(PlayerColor player) const
  221. {
  222. return getObjectName();
  223. }
  224. std::string CGObjectInstance::getHoverText(const CGHeroInstance * hero) const
  225. {
  226. return getHoverText(hero->tempOwner);
  227. }
  228. void CGObjectInstance::onHeroVisit( const CGHeroInstance * h ) const
  229. {
  230. switch(ID)
  231. {
  232. case Obj::HILL_FORT:
  233. {
  234. openWindow(OpenWindow::HILL_FORT_WINDOW,id.getNum(),h->id.getNum());
  235. }
  236. break;
  237. case Obj::SANCTUARY:
  238. {
  239. //You enter the sanctuary and immediately feel as if a great weight has been lifted off your shoulders. You feel safe here.
  240. showInfoDialog(h,114,soundBase::GETPROTECTION);
  241. }
  242. break;
  243. case Obj::TAVERN:
  244. {
  245. openWindow(OpenWindow::TAVERN_WINDOW,h->id.getNum(),id.getNum());
  246. }
  247. break;
  248. }
  249. }
  250. int3 CGObjectInstance::visitablePos() const
  251. {
  252. return pos - getVisitableOffset();
  253. }
  254. bool CGObjectInstance::isVisitable() const
  255. {
  256. return appearance.isVisitable();
  257. }
  258. bool CGObjectInstance::passableFor(PlayerColor color) const
  259. {
  260. return false;
  261. }
  262. CGObjectInstanceBySubIdFinder::CGObjectInstanceBySubIdFinder(CGObjectInstance * obj) : obj(obj)
  263. {
  264. }
  265. bool CGObjectInstanceBySubIdFinder::operator()(CGObjectInstance * obj) const
  266. {
  267. return this->obj->subID == obj->subID;
  268. }
  269. int3 IBoatGenerator::bestLocation() const
  270. {
  271. std::vector<int3> offsets;
  272. getOutOffsets(offsets);
  273. for (auto & offset : offsets)
  274. {
  275. if (const TerrainTile *tile = IObjectInterface::cb->getTile(o->pos + offset, false)) //tile is in the map
  276. {
  277. if (tile->terType == ETerrainType::WATER && (!tile->blocked || tile->blockingObjects.front()->ID == 8)) //and is water and is not blocked or is blocked by boat
  278. return o->pos + offset;
  279. }
  280. }
  281. return int3 (-1,-1,-1);
  282. }
  283. IBoatGenerator::EGeneratorState IBoatGenerator::shipyardStatus() const
  284. {
  285. int3 tile = bestLocation();
  286. const TerrainTile *t = IObjectInterface::cb->getTile(tile);
  287. if(!t)
  288. return TILE_BLOCKED; //no available water
  289. else if(!t->blockingObjects.size())
  290. return GOOD; //OK
  291. else if(t->blockingObjects.front()->ID == Obj::BOAT)
  292. return BOAT_ALREADY_BUILT; //blocked with boat
  293. else
  294. return TILE_BLOCKED; //blocked
  295. }
  296. int IBoatGenerator::getBoatType() const
  297. {
  298. //We make good ships by default
  299. return 1;
  300. }
  301. IBoatGenerator::IBoatGenerator(const CGObjectInstance *O)
  302. : o(O)
  303. {
  304. }
  305. void IBoatGenerator::getProblemText(MetaString &out, const CGHeroInstance *visitor) const
  306. {
  307. switch(shipyardStatus())
  308. {
  309. case BOAT_ALREADY_BUILT:
  310. out.addTxt(MetaString::GENERAL_TXT, 51);
  311. break;
  312. case TILE_BLOCKED:
  313. if(visitor)
  314. {
  315. out.addTxt(MetaString::GENERAL_TXT, 134);
  316. out.addReplacement(visitor->name);
  317. }
  318. else
  319. out.addTxt(MetaString::ADVOB_TXT, 189);
  320. break;
  321. case NO_WATER:
  322. logGlobal->errorStream() << "Shipyard without water!!! " << o->pos << "\t" << o->id;
  323. return;
  324. }
  325. }
  326. void IShipyard::getBoatCost( std::vector<si32> &cost ) const
  327. {
  328. cost.resize(GameConstants::RESOURCE_QUANTITY);
  329. cost[Res::WOOD] = 10;
  330. cost[Res::GOLD] = 1000;
  331. }
  332. IShipyard::IShipyard(const CGObjectInstance *O)
  333. : IBoatGenerator(O)
  334. {
  335. }
  336. IShipyard * IShipyard::castFrom( CGObjectInstance *obj )
  337. {
  338. if(!obj)
  339. return nullptr;
  340. if(obj->ID == Obj::TOWN)
  341. {
  342. return static_cast<CGTownInstance*>(obj);
  343. }
  344. else if(obj->ID == Obj::SHIPYARD)
  345. {
  346. return static_cast<CGShipyard*>(obj);
  347. }
  348. else
  349. {
  350. return nullptr;
  351. }
  352. }
  353. const IShipyard * IShipyard::castFrom( const CGObjectInstance *obj )
  354. {
  355. return castFrom(const_cast<CGObjectInstance*>(obj));
  356. }