2
0

CObjectHandler.cpp 12 KB

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