CObjectHandler.cpp 11 KB

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