CObjectHandler.cpp 11 KB

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