CObjectHandler.cpp 9.4 KB

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