CObjectHandler.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  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)
  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 () const
  58. {}
  59. IObjectInterface::~IObjectInterface()
  60. {}
  61. IObjectInterface::IObjectInterface()
  62. {}
  63. void IObjectInterface::initObj()
  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->traceStream() << "\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(price.Float());
  94. }
  95. logGlobal->traceStream() << "\t\tDone loading resource prices!";
  96. }
  97. PlayerColor CGObjectInstance::getOwner() const
  98. {
  99. //if (state)
  100. // return state->owner;
  101. //else
  102. return tempOwner; //won't have owner
  103. }
  104. CGObjectInstance::CGObjectInstance():
  105. pos(-1,-1,-1),
  106. ID(Obj::NO_OBJ),
  107. subID(-1),
  108. tempOwner(PlayerColor::UNFLAGGABLE),
  109. blockVisit(false)
  110. {
  111. }
  112. CGObjectInstance::~CGObjectInstance()
  113. {
  114. }
  115. const std::string & CGObjectInstance::getStringId() const
  116. {
  117. //todo: getStringId use real object type
  118. if(stringId == "")
  119. {
  120. boost::format fmt("%s_%d");
  121. fmt % typeName % id.getNum();
  122. stringId = fmt.str();
  123. }
  124. return stringId;
  125. }
  126. void CGObjectInstance::setOwner(PlayerColor ow)
  127. {
  128. tempOwner = ow;
  129. }
  130. int CGObjectInstance::getWidth() const//returns width of object graphic in tiles
  131. {
  132. return appearance.getWidth();
  133. }
  134. int CGObjectInstance::getHeight() const //returns height of object graphic in tiles
  135. {
  136. return appearance.getHeight();
  137. }
  138. 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)
  139. {
  140. return appearance.isVisitableAt(pos.x - x, pos.y - y);
  141. }
  142. bool CGObjectInstance::blockingAt(int x, int y) const
  143. {
  144. return appearance.isBlockedAt(pos.x - x, pos.y - y);
  145. }
  146. bool CGObjectInstance::coveringAt(int x, int y) const
  147. {
  148. return appearance.isVisibleAt(pos.x - x, pos.y - y);
  149. }
  150. std::set<int3> CGObjectInstance::getBlockedPos() const
  151. {
  152. std::set<int3> ret;
  153. for(int w=0; w<getWidth(); ++w)
  154. {
  155. for(int h=0; h<getHeight(); ++h)
  156. {
  157. if(appearance.isBlockedAt(w, h))
  158. ret.insert(int3(pos.x - w, pos.y - h, pos.z));
  159. }
  160. }
  161. return ret;
  162. }
  163. std::set<int3> CGObjectInstance::getBlockedOffsets() const
  164. {
  165. return appearance.getBlockedOffsets();
  166. }
  167. void CGObjectInstance::setType(si32 ID, si32 subID)
  168. {
  169. const TerrainTile &tile = cb->gameState()->map->getTile(visitablePos());
  170. this->ID = Obj(ID);
  171. this->subID = subID;
  172. //recalculate blockvis tiles - new appearance might have different blockmap than before
  173. cb->gameState()->map->removeBlockVisTiles(this, true);
  174. auto handler = VLC->objtypeh->getHandlerFor(ID, subID);
  175. if(!handler)
  176. {
  177. logGlobal->errorStream() << boost::format(
  178. "Unknown object type %d:%d at %s") % ID % subID % visitablePos();
  179. return;
  180. }
  181. if(!handler->getTemplates(tile.terType).empty())
  182. appearance = handler->getTemplates(tile.terType)[0];
  183. else
  184. appearance = handler->getTemplates()[0]; // get at least some appearance since alternative is crash
  185. cb->gameState()->map->addBlockVisTiles(this);
  186. }
  187. void CGObjectInstance::initObj()
  188. {
  189. switch(ID)
  190. {
  191. case Obj::TAVERN:
  192. blockVisit = true;
  193. break;
  194. }
  195. }
  196. void CGObjectInstance::setProperty( ui8 what, ui32 val )
  197. {
  198. setPropertyDer(what, val); // call this before any actual changes (needed at least for dwellings)
  199. switch(what)
  200. {
  201. case ObjProperty::OWNER:
  202. tempOwner = PlayerColor(val);
  203. break;
  204. case ObjProperty::BLOCKVIS:
  205. blockVisit = val;
  206. break;
  207. case ObjProperty::ID:
  208. ID = Obj(val);
  209. break;
  210. case ObjProperty::SUBID:
  211. subID = val;
  212. break;
  213. }
  214. }
  215. void CGObjectInstance::setPropertyDer( ui8 what, ui32 val )
  216. {}
  217. int3 CGObjectInstance::getSightCenter() const
  218. {
  219. return visitablePos();
  220. }
  221. int CGObjectInstance::getSightRadius() const
  222. {
  223. return 3;
  224. }
  225. int3 CGObjectInstance::getVisitableOffset() const
  226. {
  227. return appearance.getVisitableOffset();
  228. }
  229. void CGObjectInstance::giveDummyBonus(ObjectInstanceID heroID, ui8 duration) const
  230. {
  231. GiveBonus gbonus;
  232. gbonus.bonus.type = Bonus::NONE;
  233. gbonus.id = heroID.getNum();
  234. gbonus.bonus.duration = duration;
  235. gbonus.bonus.source = Bonus::OBJECT;
  236. gbonus.bonus.sid = ID;
  237. cb->giveHeroBonus(&gbonus);
  238. }
  239. std::string CGObjectInstance::getObjectName() const
  240. {
  241. return VLC->objtypeh->getObjectName(ID, subID);
  242. }
  243. std::string CGObjectInstance::getHoverText(PlayerColor player) const
  244. {
  245. return getObjectName();
  246. }
  247. std::string CGObjectInstance::getHoverText(const CGHeroInstance * hero) const
  248. {
  249. return getHoverText(hero->tempOwner);
  250. }
  251. void CGObjectInstance::onHeroVisit( const CGHeroInstance * h ) const
  252. {
  253. switch(ID)
  254. {
  255. case Obj::HILL_FORT:
  256. {
  257. openWindow(OpenWindow::HILL_FORT_WINDOW,id.getNum(),h->id.getNum());
  258. }
  259. break;
  260. case Obj::SANCTUARY:
  261. {
  262. //You enter the sanctuary and immediately feel as if a great weight has been lifted off your shoulders. You feel safe here.
  263. showInfoDialog(h,114,soundBase::GETPROTECTION);
  264. }
  265. break;
  266. case Obj::TAVERN:
  267. {
  268. openWindow(OpenWindow::TAVERN_WINDOW,h->id.getNum(),id.getNum());
  269. }
  270. break;
  271. }
  272. }
  273. int3 CGObjectInstance::visitablePos() const
  274. {
  275. return pos - getVisitableOffset();
  276. }
  277. bool CGObjectInstance::isVisitable() const
  278. {
  279. return appearance.isVisitable();
  280. }
  281. bool CGObjectInstance::passableFor(PlayerColor color) const
  282. {
  283. return false;
  284. }
  285. void CGObjectInstance::serializeJson(JsonSerializeFormat & handler)
  286. {
  287. if(handler.saving)
  288. {
  289. handler.serializeString("type", typeName);
  290. handler.serializeString("subType", subTypeName);
  291. }
  292. handler.serializeNumeric("x", pos.x);
  293. handler.serializeNumeric("y", pos.y);
  294. handler.serializeNumeric("l", pos.z);
  295. if(handler.saving)
  296. {
  297. appearance.writeJson(handler.getCurrent()["template"], false);
  298. }
  299. else
  300. {
  301. appearance.readJson(handler.getCurrent()["template"], false);
  302. }
  303. {
  304. auto options = handler.enterStruct("options");
  305. serializeJsonOptions(handler);
  306. }
  307. if(handler.saving && handler.getCurrent()["options"].isNull())
  308. {
  309. handler.getCurrent().Struct().erase("options");
  310. }
  311. }
  312. void CGObjectInstance::serializeJsonOptions(JsonSerializeFormat & handler)
  313. {
  314. }
  315. void CGObjectInstance::serializeJsonOwner(JsonSerializeFormat & handler)
  316. {
  317. std::string temp;
  318. //todo: use enum serialize
  319. if(handler.saving)
  320. {
  321. if(tempOwner.isValidPlayer())
  322. {
  323. temp = GameConstants::PLAYER_COLOR_NAMES[tempOwner.getNum()];
  324. handler.serializeString("owner", temp);
  325. }
  326. }
  327. else
  328. {
  329. tempOwner = PlayerColor::NEUTRAL;//this method assumes that object is ownable
  330. handler.serializeString("owner", temp);
  331. if(temp != "")
  332. {
  333. auto rawOwner = vstd::find_pos(GameConstants::PLAYER_COLOR_NAMES, temp);
  334. if(rawOwner >=0)
  335. tempOwner = PlayerColor(rawOwner);
  336. else
  337. logGlobal->errorStream() << "Invalid owner :" << temp;
  338. }
  339. }
  340. }
  341. CGObjectInstanceBySubIdFinder::CGObjectInstanceBySubIdFinder(CGObjectInstance * obj) : obj(obj)
  342. {
  343. }
  344. bool CGObjectInstanceBySubIdFinder::operator()(CGObjectInstance * obj) const
  345. {
  346. return this->obj->subID == obj->subID;
  347. }
  348. int3 IBoatGenerator::bestLocation() const
  349. {
  350. std::vector<int3> offsets;
  351. getOutOffsets(offsets);
  352. for (auto & offset : offsets)
  353. {
  354. if(const TerrainTile *tile = IObjectInterface::cb->getTile(o->pos + offset, false)) //tile is in the map
  355. {
  356. 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
  357. return o->pos + offset;
  358. }
  359. }
  360. return int3 (-1,-1,-1);
  361. }
  362. IBoatGenerator::EGeneratorState IBoatGenerator::shipyardStatus() const
  363. {
  364. int3 tile = bestLocation();
  365. const TerrainTile *t = IObjectInterface::cb->getTile(tile);
  366. if(!t)
  367. return TILE_BLOCKED; //no available water
  368. else if(!t->blockingObjects.size())
  369. return GOOD; //OK
  370. else if(t->blockingObjects.front()->ID == Obj::BOAT)
  371. return BOAT_ALREADY_BUILT; //blocked with boat
  372. else
  373. return TILE_BLOCKED; //blocked
  374. }
  375. int IBoatGenerator::getBoatType() const
  376. {
  377. //We make good ships by default
  378. return 1;
  379. }
  380. IBoatGenerator::IBoatGenerator(const CGObjectInstance *O)
  381. : o(O)
  382. {
  383. }
  384. void IBoatGenerator::getProblemText(MetaString &out, const CGHeroInstance *visitor) const
  385. {
  386. switch(shipyardStatus())
  387. {
  388. case BOAT_ALREADY_BUILT:
  389. out.addTxt(MetaString::GENERAL_TXT, 51);
  390. break;
  391. case TILE_BLOCKED:
  392. if(visitor)
  393. {
  394. out.addTxt(MetaString::GENERAL_TXT, 134);
  395. out.addReplacement(visitor->name);
  396. }
  397. else
  398. out.addTxt(MetaString::ADVOB_TXT, 189);
  399. break;
  400. case NO_WATER:
  401. logGlobal->errorStream() << "Shipyard without water!!! " << o->pos << "\t" << o->id;
  402. return;
  403. }
  404. }
  405. void IShipyard::getBoatCost( std::vector<si32> &cost ) const
  406. {
  407. cost.resize(GameConstants::RESOURCE_QUANTITY);
  408. cost[Res::WOOD] = 10;
  409. cost[Res::GOLD] = 1000;
  410. }
  411. IShipyard::IShipyard(const CGObjectInstance *O)
  412. : IBoatGenerator(O)
  413. {
  414. }
  415. IShipyard * IShipyard::castFrom( CGObjectInstance *obj )
  416. {
  417. if(!obj)
  418. return nullptr;
  419. if(obj->ID == Obj::TOWN)
  420. {
  421. return static_cast<CGTownInstance*>(obj);
  422. }
  423. else if(obj->ID == Obj::SHIPYARD)
  424. {
  425. return static_cast<CGShipyard*>(obj);
  426. }
  427. else
  428. {
  429. return nullptr;
  430. }
  431. }
  432. const IShipyard * IShipyard::castFrom( const CGObjectInstance *obj )
  433. {
  434. return castFrom(const_cast<CGObjectInstance*>(obj));
  435. }