CObjectHandler.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  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 "../StringConstants.h"
  20. #include "../mapping/CMap.h"
  21. #include "CObjectClassesHandler.h"
  22. #include "CGTownInstance.h"
  23. #include "../serializer/JsonSerializeFormat.h"
  24. VCMI_LIB_NAMESPACE_BEGIN
  25. IGameCallback * IObjectInterface::cb = nullptr;
  26. ///helpers
  27. static void openWindow(const OpenWindow::EWindow type, const int id1, const int id2 = -1)
  28. {
  29. OpenWindow ow;
  30. ow.window = type;
  31. ow.id1 = id1;
  32. ow.id2 = id2;
  33. IObjectInterface::cb->sendAndApply(&ow);
  34. }
  35. static void showInfoDialog(const PlayerColor playerID, const ui32 txtID, const ui16 soundID)
  36. {
  37. InfoWindow iw;
  38. iw.soundID = soundID;
  39. iw.player = playerID;
  40. iw.text.addTxt(MetaString::ADVOB_TXT,txtID);
  41. IObjectInterface::cb->sendAndApply(&iw);
  42. }
  43. /*static void showInfoDialog(const ObjectInstanceID heroID, const ui32 txtID, const ui16 soundID)
  44. {
  45. const PlayerColor playerID = IObjectInterface::cb->getOwner(heroID);
  46. showInfoDialog(playerID,txtID,soundID);
  47. }*/
  48. static void showInfoDialog(const CGHeroInstance* h, const ui32 txtID, const ui16 soundID = 0)
  49. {
  50. const PlayerColor playerID = h->getOwner();
  51. showInfoDialog(playerID,txtID,soundID);
  52. }
  53. ///IObjectInterface
  54. void IObjectInterface::onHeroVisit(const CGHeroInstance * h) const
  55. {}
  56. void IObjectInterface::onHeroLeave(const CGHeroInstance * h) const
  57. {}
  58. void IObjectInterface::newTurn(CRandomGenerator & rand) const
  59. {}
  60. IObjectInterface::~IObjectInterface()
  61. {}
  62. IObjectInterface::IObjectInterface()
  63. {}
  64. void IObjectInterface::initObj(CRandomGenerator & rand)
  65. {}
  66. void IObjectInterface::setProperty( ui8 what, ui32 val )
  67. {}
  68. bool IObjectInterface::wasVisited (PlayerColor player) const
  69. {
  70. return false;
  71. }
  72. bool IObjectInterface::wasVisited (const CGHeroInstance * h) const
  73. {
  74. return false;
  75. }
  76. void IObjectInterface::postInit()
  77. {}
  78. void IObjectInterface::preInit()
  79. {}
  80. void IObjectInterface::battleFinished(const CGHeroInstance *hero, const BattleResult &result) const
  81. {}
  82. void IObjectInterface::blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const
  83. {}
  84. void IObjectInterface::garrisonDialogClosed(const CGHeroInstance *hero) const
  85. {}
  86. void IObjectInterface::heroLevelUpDone(const CGHeroInstance *hero) const
  87. {}
  88. CObjectHandler::CObjectHandler()
  89. {
  90. logGlobal->trace("\t\tReading resources prices ");
  91. const JsonNode config2(ResourceID("config/resources.json"));
  92. for(const JsonNode &price : config2["resources_prices"].Vector())
  93. {
  94. resVals.push_back(static_cast<ui32>(price.Float()));
  95. }
  96. logGlobal->trace("\t\tDone loading resource prices!");
  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. int32_t CGObjectInstance::getObjGroupIndex() const
  110. {
  111. return ID.num;
  112. }
  113. int32_t CGObjectInstance::getObjTypeIndex() const
  114. {
  115. return subID;
  116. }
  117. int3 CGObjectInstance::getPosition() const
  118. {
  119. return pos;
  120. }
  121. void CGObjectInstance::setOwner(PlayerColor ow)
  122. {
  123. tempOwner = ow;
  124. }
  125. int CGObjectInstance::getWidth() const//returns width of object graphic in tiles
  126. {
  127. return appearance->getWidth();
  128. }
  129. int CGObjectInstance::getHeight() const //returns height of object graphic in tiles
  130. {
  131. return appearance->getHeight();
  132. }
  133. 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)
  134. {
  135. return appearance->isVisitableAt(pos.x - x, pos.y - y);
  136. }
  137. bool CGObjectInstance::blockingAt(int x, int y) const
  138. {
  139. return appearance->isBlockedAt(pos.x - x, pos.y - y);
  140. }
  141. bool CGObjectInstance::coveringAt(int x, int y) const
  142. {
  143. return appearance->isVisibleAt(pos.x - x, pos.y - y);
  144. }
  145. std::set<int3> CGObjectInstance::getBlockedPos() const
  146. {
  147. std::set<int3> ret;
  148. for(int w=0; w<getWidth(); ++w)
  149. {
  150. for(int h=0; h<getHeight(); ++h)
  151. {
  152. if(appearance->isBlockedAt(w, h))
  153. ret.insert(int3(pos.x - w, pos.y - h, pos.z));
  154. }
  155. }
  156. return ret;
  157. }
  158. std::set<int3> CGObjectInstance::getBlockedOffsets() const
  159. {
  160. return appearance->getBlockedOffsets();
  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. if(!handler)
  171. {
  172. logGlobal->error("Unknown object type %d:%d at %s", ID, subID, visitablePos().toString());
  173. return;
  174. }
  175. if(!handler->getTemplates(tile.terType->id).empty())
  176. appearance = handler->getTemplates(tile.terType->id)[0];
  177. else
  178. appearance = handler->getTemplates()[0]; // get at least some appearance since alternative is crash
  179. if (ID == Obj::HERO)
  180. {
  181. //adjust for the prison offset
  182. pos = visitablePos();
  183. }
  184. cb->gameState()->map->addBlockVisTiles(this);
  185. }
  186. void CGObjectInstance::initObj(CRandomGenerator & rand)
  187. {
  188. switch(ID)
  189. {
  190. case Obj::TAVERN:
  191. blockVisit = true;
  192. break;
  193. }
  194. }
  195. void CGObjectInstance::setProperty( ui8 what, ui32 val )
  196. {
  197. setPropertyDer(what, val); // call this before any actual changes (needed at least for dwellings)
  198. switch(what)
  199. {
  200. case ObjProperty::OWNER:
  201. tempOwner = PlayerColor(val);
  202. break;
  203. case ObjProperty::BLOCKVIS:
  204. blockVisit = val;
  205. break;
  206. case ObjProperty::ID:
  207. ID = Obj(val);
  208. break;
  209. case ObjProperty::SUBID:
  210. subID = val;
  211. break;
  212. }
  213. }
  214. void CGObjectInstance::setPropertyDer( ui8 what, ui32 val )
  215. {}
  216. int3 CGObjectInstance::getSightCenter() const
  217. {
  218. return visitablePos();
  219. }
  220. int CGObjectInstance::getSightRadius() const
  221. {
  222. return 3;
  223. }
  224. int3 CGObjectInstance::getVisitableOffset() const
  225. {
  226. return appearance->getVisitableOffset();
  227. }
  228. void CGObjectInstance::giveDummyBonus(ObjectInstanceID heroID, ui8 duration) const
  229. {
  230. GiveBonus gbonus;
  231. gbonus.bonus.type = Bonus::NONE;
  232. gbonus.id = heroID.getNum();
  233. gbonus.bonus.duration = (Bonus::BonusDuration)duration;
  234. gbonus.bonus.source = Bonus::OBJECT;
  235. gbonus.bonus.sid = ID;
  236. cb->giveHeroBonus(&gbonus);
  237. }
  238. std::string CGObjectInstance::getObjectName() const
  239. {
  240. return VLC->objtypeh->getObjectName(ID, subID);
  241. }
  242. boost::optional<std::string> CGObjectInstance::getAmbientSound() const
  243. {
  244. const auto & sounds = VLC->objtypeh->getObjectSounds(ID, subID).ambient;
  245. if(sounds.size())
  246. return sounds.front(); // TODO: Support randomization of ambient sounds
  247. return boost::none;
  248. }
  249. boost::optional<std::string> CGObjectInstance::getVisitSound() const
  250. {
  251. const auto & sounds = VLC->objtypeh->getObjectSounds(ID, subID).visit;
  252. if(sounds.size())
  253. return *RandomGeneratorUtil::nextItem(sounds, CRandomGenerator::getDefault());
  254. return boost::none;
  255. }
  256. boost::optional<std::string> CGObjectInstance::getRemovalSound() const
  257. {
  258. const auto & sounds = VLC->objtypeh->getObjectSounds(ID, subID).removal;
  259. if(sounds.size())
  260. return *RandomGeneratorUtil::nextItem(sounds, CRandomGenerator::getDefault());
  261. return boost::none;
  262. }
  263. std::string CGObjectInstance::getHoverText(PlayerColor player) const
  264. {
  265. return getObjectName();
  266. }
  267. std::string CGObjectInstance::getHoverText(const CGHeroInstance * hero) const
  268. {
  269. return getHoverText(hero->tempOwner);
  270. }
  271. void CGObjectInstance::onHeroVisit( const CGHeroInstance * h ) const
  272. {
  273. switch(ID)
  274. {
  275. case Obj::HILL_FORT:
  276. {
  277. openWindow(OpenWindow::HILL_FORT_WINDOW,id.getNum(),h->id.getNum());
  278. }
  279. break;
  280. case Obj::SANCTUARY:
  281. {
  282. //You enter the sanctuary and immediately feel as if a great weight has been lifted off your shoulders. You feel safe here.
  283. showInfoDialog(h, 114);
  284. }
  285. break;
  286. case Obj::TAVERN:
  287. {
  288. openWindow(OpenWindow::TAVERN_WINDOW,h->id.getNum(),id.getNum());
  289. }
  290. break;
  291. }
  292. }
  293. int3 CGObjectInstance::visitablePos() const
  294. {
  295. return pos - getVisitableOffset();
  296. }
  297. bool CGObjectInstance::isVisitable() const
  298. {
  299. return appearance->isVisitable();
  300. }
  301. bool CGObjectInstance::passableFor(PlayerColor color) const
  302. {
  303. return false;
  304. }
  305. void CGObjectInstance::updateFrom(const JsonNode & data)
  306. {
  307. }
  308. void CGObjectInstance::serializeJson(JsonSerializeFormat & handler)
  309. {
  310. //only save here, loading is handled by map loader
  311. if(handler.saving)
  312. {
  313. handler.serializeString("type", typeName);
  314. handler.serializeString("subtype", subTypeName);
  315. handler.serializeInt("x", pos.x);
  316. handler.serializeInt("y", pos.y);
  317. handler.serializeInt("l", pos.z);
  318. JsonNode app;
  319. appearance->writeJson(app, false);
  320. handler.serializeRaw("template",app, boost::none);
  321. }
  322. {
  323. auto options = handler.enterStruct("options");
  324. serializeJsonOptions(handler);
  325. }
  326. }
  327. void CGObjectInstance::afterAddToMap(CMap * map)
  328. {
  329. //nothing here
  330. }
  331. void CGObjectInstance::afterRemoveFromMap(CMap * map)
  332. {
  333. //nothing here
  334. }
  335. void CGObjectInstance::serializeJsonOptions(JsonSerializeFormat & handler)
  336. {
  337. //nothing here
  338. }
  339. void CGObjectInstance::serializeJsonOwner(JsonSerializeFormat & handler)
  340. {
  341. ui8 temp = tempOwner.getNum();
  342. handler.serializeEnum("owner", temp, PlayerColor::NEUTRAL.getNum(), GameConstants::PLAYER_COLOR_NAMES);
  343. if(!handler.saving)
  344. tempOwner = PlayerColor(temp);
  345. }
  346. BattleField CGObjectInstance::getBattlefield() const
  347. {
  348. return VLC->objtypeh->getHandlerFor(ID, subID)->getBattlefield();
  349. }
  350. CGObjectInstanceBySubIdFinder::CGObjectInstanceBySubIdFinder(CGObjectInstance * obj) : obj(obj)
  351. {
  352. }
  353. bool CGObjectInstanceBySubIdFinder::operator()(CGObjectInstance * obj) const
  354. {
  355. return this->obj->subID == obj->subID;
  356. }
  357. int3 IBoatGenerator::bestLocation() const
  358. {
  359. std::vector<int3> offsets;
  360. getOutOffsets(offsets);
  361. for (auto & offset : offsets)
  362. {
  363. if(const TerrainTile *tile = IObjectInterface::cb->getTile(o->pos + offset, false)) //tile is in the map
  364. {
  365. if(tile->terType->isWater() && (!tile->blocked || tile->blockingObjects.front()->ID == Obj::BOAT)) //and is water and is not blocked or is blocked by boat
  366. return o->pos + offset;
  367. }
  368. }
  369. return int3 (-1,-1,-1);
  370. }
  371. IBoatGenerator::EGeneratorState IBoatGenerator::shipyardStatus() const
  372. {
  373. int3 tile = bestLocation();
  374. const TerrainTile *t = IObjectInterface::cb->getTile(tile);
  375. if(!t)
  376. return TILE_BLOCKED; //no available water
  377. else if(!t->blockingObjects.size())
  378. return GOOD; //OK
  379. else if(t->blockingObjects.front()->ID == Obj::BOAT)
  380. return BOAT_ALREADY_BUILT; //blocked with boat
  381. else
  382. return TILE_BLOCKED; //blocked
  383. }
  384. int IBoatGenerator::getBoatType() const
  385. {
  386. //We make good ships by default
  387. return 1;
  388. }
  389. IBoatGenerator::IBoatGenerator(const CGObjectInstance *O)
  390. : o(O)
  391. {
  392. }
  393. void IBoatGenerator::getProblemText(MetaString &out, const CGHeroInstance *visitor) const
  394. {
  395. switch(shipyardStatus())
  396. {
  397. case BOAT_ALREADY_BUILT:
  398. out.addTxt(MetaString::GENERAL_TXT, 51);
  399. break;
  400. case TILE_BLOCKED:
  401. if(visitor)
  402. {
  403. out.addTxt(MetaString::GENERAL_TXT, 134);
  404. out.addReplacement(visitor->name);
  405. }
  406. else
  407. out.addTxt(MetaString::ADVOB_TXT, 189);
  408. break;
  409. case NO_WATER:
  410. logGlobal->error("Shipyard without water! %s \t %d", o->pos.toString(), o->id.getNum());
  411. return;
  412. }
  413. }
  414. void IShipyard::getBoatCost( std::vector<si32> &cost ) const
  415. {
  416. cost.resize(GameConstants::RESOURCE_QUANTITY);
  417. cost[Res::WOOD] = 10;
  418. cost[Res::GOLD] = 1000;
  419. }
  420. IShipyard::IShipyard(const CGObjectInstance *O)
  421. : IBoatGenerator(O)
  422. {
  423. }
  424. IShipyard * IShipyard::castFrom( CGObjectInstance *obj )
  425. {
  426. if(!obj)
  427. return nullptr;
  428. if(obj->ID == Obj::TOWN)
  429. {
  430. return static_cast<CGTownInstance*>(obj);
  431. }
  432. else if(obj->ID == Obj::SHIPYARD)
  433. {
  434. return static_cast<CGShipyard*>(obj);
  435. }
  436. else
  437. {
  438. return nullptr;
  439. }
  440. }
  441. const IShipyard * IShipyard::castFrom( const CGObjectInstance *obj )
  442. {
  443. return castFrom(const_cast<CGObjectInstance*>(obj));
  444. }
  445. VCMI_LIB_NAMESPACE_END