CObjectHandler.cpp 9.3 KB

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