2
0

CObjectHandler.cpp 9.5 KB

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