2
0

CObjectHandler.cpp 9.5 KB

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