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 "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. if (!handler->getTemplates(tile.terType).empty())
  162. appearance = handler->getTemplates(tile.terType)[0];
  163. else
  164. appearance = handler->getTemplates()[0]; // get at least some appearance since alternative is crash
  165. cb->gameState()->map->addBlockVisTiles(this);
  166. }
  167. void CGObjectInstance::initObj()
  168. {
  169. switch(ID)
  170. {
  171. case Obj::TAVERN:
  172. blockVisit = true;
  173. break;
  174. }
  175. }
  176. void CGObjectInstance::setProperty( ui8 what, ui32 val )
  177. {
  178. setPropertyDer(what, val); // call this before any actual changes (needed at least for dwellings)
  179. switch(what)
  180. {
  181. case ObjProperty::OWNER:
  182. tempOwner = PlayerColor(val);
  183. break;
  184. case ObjProperty::BLOCKVIS:
  185. blockVisit = val;
  186. break;
  187. case ObjProperty::ID:
  188. ID = Obj(val);
  189. break;
  190. case ObjProperty::SUBID:
  191. subID = val;
  192. break;
  193. }
  194. }
  195. void CGObjectInstance::setPropertyDer( ui8 what, ui32 val )
  196. {}
  197. int3 CGObjectInstance::getSightCenter() const
  198. {
  199. return visitablePos();
  200. }
  201. int CGObjectInstance::getSightRadious() const
  202. {
  203. return 3;
  204. }
  205. int3 CGObjectInstance::getVisitableOffset() const
  206. {
  207. return appearance.getVisitableOffset();
  208. }
  209. void CGObjectInstance::giveDummyBonus(ObjectInstanceID heroID, ui8 duration) const
  210. {
  211. GiveBonus gbonus;
  212. gbonus.bonus.type = Bonus::NONE;
  213. gbonus.id = heroID.getNum();
  214. gbonus.bonus.duration = duration;
  215. gbonus.bonus.source = Bonus::OBJECT;
  216. gbonus.bonus.sid = ID;
  217. cb->giveHeroBonus(&gbonus);
  218. }
  219. std::string CGObjectInstance::getObjectName() const
  220. {
  221. return VLC->objtypeh->getObjectName(ID, subID);
  222. }
  223. std::string CGObjectInstance::getHoverText(PlayerColor player) const
  224. {
  225. return getObjectName();
  226. }
  227. std::string CGObjectInstance::getHoverText(const CGHeroInstance * hero) const
  228. {
  229. return getHoverText(hero->tempOwner);
  230. }
  231. void CGObjectInstance::onHeroVisit( const CGHeroInstance * h ) const
  232. {
  233. switch(ID)
  234. {
  235. case Obj::HILL_FORT:
  236. {
  237. openWindow(OpenWindow::HILL_FORT_WINDOW,id.getNum(),h->id.getNum());
  238. }
  239. break;
  240. case Obj::SANCTUARY:
  241. {
  242. //You enter the sanctuary and immediately feel as if a great weight has been lifted off your shoulders. You feel safe here.
  243. showInfoDialog(h,114,soundBase::GETPROTECTION);
  244. }
  245. break;
  246. case Obj::TAVERN:
  247. {
  248. openWindow(OpenWindow::TAVERN_WINDOW,h->id.getNum(),id.getNum());
  249. }
  250. break;
  251. }
  252. }
  253. int3 CGObjectInstance::visitablePos() const
  254. {
  255. return pos - getVisitableOffset();
  256. }
  257. bool CGObjectInstance::isVisitable() const
  258. {
  259. return appearance.isVisitable();
  260. }
  261. bool CGObjectInstance::passableFor(PlayerColor color) const
  262. {
  263. return false;
  264. }
  265. CGObjectInstanceBySubIdFinder::CGObjectInstanceBySubIdFinder(CGObjectInstance * obj) : obj(obj)
  266. {
  267. }
  268. bool CGObjectInstanceBySubIdFinder::operator()(CGObjectInstance * obj) const
  269. {
  270. return this->obj->subID == obj->subID;
  271. }
  272. int3 IBoatGenerator::bestLocation() const
  273. {
  274. std::vector<int3> offsets;
  275. getOutOffsets(offsets);
  276. for (auto & offset : offsets)
  277. {
  278. if (const TerrainTile *tile = IObjectInterface::cb->getTile(o->pos + offset, false)) //tile is in the map
  279. {
  280. if (tile->terType == ETerrainType::WATER && (!tile->blocked || tile->blockingObjects.front()->ID == 8)) //and is water and is not blocked or is blocked by boat
  281. return o->pos + offset;
  282. }
  283. }
  284. return int3 (-1,-1,-1);
  285. }
  286. IBoatGenerator::EGeneratorState IBoatGenerator::shipyardStatus() const
  287. {
  288. int3 tile = bestLocation();
  289. const TerrainTile *t = IObjectInterface::cb->getTile(tile);
  290. if(!t)
  291. return TILE_BLOCKED; //no available water
  292. else if(!t->blockingObjects.size())
  293. return GOOD; //OK
  294. else if(t->blockingObjects.front()->ID == Obj::BOAT)
  295. return BOAT_ALREADY_BUILT; //blocked with boat
  296. else
  297. return TILE_BLOCKED; //blocked
  298. }
  299. int IBoatGenerator::getBoatType() const
  300. {
  301. //We make good ships by default
  302. return 1;
  303. }
  304. IBoatGenerator::IBoatGenerator(const CGObjectInstance *O)
  305. : o(O)
  306. {
  307. }
  308. void IBoatGenerator::getProblemText(MetaString &out, const CGHeroInstance *visitor) const
  309. {
  310. switch(shipyardStatus())
  311. {
  312. case BOAT_ALREADY_BUILT:
  313. out.addTxt(MetaString::GENERAL_TXT, 51);
  314. break;
  315. case TILE_BLOCKED:
  316. if(visitor)
  317. {
  318. out.addTxt(MetaString::GENERAL_TXT, 134);
  319. out.addReplacement(visitor->name);
  320. }
  321. else
  322. out.addTxt(MetaString::ADVOB_TXT, 189);
  323. break;
  324. case NO_WATER:
  325. logGlobal->errorStream() << "Shipyard without water!!! " << o->pos << "\t" << o->id;
  326. return;
  327. }
  328. }
  329. void IShipyard::getBoatCost( std::vector<si32> &cost ) const
  330. {
  331. cost.resize(GameConstants::RESOURCE_QUANTITY);
  332. cost[Res::WOOD] = 10;
  333. cost[Res::GOLD] = 1000;
  334. }
  335. IShipyard::IShipyard(const CGObjectInstance *O)
  336. : IBoatGenerator(O)
  337. {
  338. }
  339. IShipyard * IShipyard::castFrom( CGObjectInstance *obj )
  340. {
  341. if(!obj)
  342. return nullptr;
  343. if(obj->ID == Obj::TOWN)
  344. {
  345. return static_cast<CGTownInstance*>(obj);
  346. }
  347. else if(obj->ID == Obj::SHIPYARD)
  348. {
  349. return static_cast<CGShipyard*>(obj);
  350. }
  351. else
  352. {
  353. return nullptr;
  354. }
  355. }
  356. const IShipyard * IShipyard::castFrom( const CGObjectInstance *obj )
  357. {
  358. return castFrom(const_cast<CGObjectInstance*>(obj));
  359. }