CObjectHandler.cpp 9.3 KB

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