CGDwelling.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. /*
  2. * CGDwelling.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 "CGDwelling.h"
  12. #include "CObjectClassesHandler.h"
  13. #include "../serializer/JsonSerializeFormat.h"
  14. #include "../CTownHandler.h"
  15. #include "../IGameCallback.h"
  16. #include "../CGameState.h"
  17. #include "../CPlayerState.h"
  18. #include "../NetPacks.h"
  19. #include "../GameSettings.h"
  20. #include "../CConfigHandler.h"
  21. VCMI_LIB_NAMESPACE_BEGIN
  22. CSpecObjInfo::CSpecObjInfo():
  23. owner(nullptr)
  24. {
  25. }
  26. void CCreGenAsCastleInfo::serializeJson(JsonSerializeFormat & handler)
  27. {
  28. handler.serializeString("sameAsTown", instanceId);
  29. if(!handler.saving)
  30. {
  31. asCastle = !instanceId.empty();
  32. allowedFactions.clear();
  33. }
  34. if(!asCastle)
  35. {
  36. std::vector<bool> standard;
  37. standard.resize(VLC->townh->size(), true);
  38. JsonSerializeFormat::LIC allowedLIC(standard, &FactionID::decode, &FactionID::encode);
  39. allowedLIC.any = allowedFactions;
  40. handler.serializeLIC("allowedFactions", allowedLIC);
  41. if(!handler.saving)
  42. {
  43. allowedFactions = allowedLIC.any;
  44. }
  45. }
  46. }
  47. void CCreGenLeveledInfo::serializeJson(JsonSerializeFormat & handler)
  48. {
  49. handler.serializeInt("minLevel", minLevel, static_cast<uint8_t>(1));
  50. handler.serializeInt("maxLevel", maxLevel, static_cast<uint8_t>(7));
  51. if(!handler.saving)
  52. {
  53. //todo: safely allow any level > 7
  54. vstd::abetween<uint8_t>(minLevel, 1, 7);
  55. vstd::abetween<uint8_t>(maxLevel, minLevel, 7);
  56. }
  57. }
  58. void CCreGenLeveledCastleInfo::serializeJson(JsonSerializeFormat & handler)
  59. {
  60. CCreGenAsCastleInfo::serializeJson(handler);
  61. CCreGenLeveledInfo::serializeJson(handler);
  62. }
  63. CGDwelling::CGDwelling()
  64. : info(nullptr)
  65. {
  66. }
  67. CGDwelling::~CGDwelling()
  68. {
  69. vstd::clear_pointer(info);
  70. }
  71. void CGDwelling::initObj(CRandomGenerator & rand)
  72. {
  73. switch(ID)
  74. {
  75. case Obj::CREATURE_GENERATOR1:
  76. case Obj::CREATURE_GENERATOR4:
  77. {
  78. VLC->objtypeh->getHandlerFor(ID, subID)->configureObject(this, rand);
  79. if (getOwner() != PlayerColor::NEUTRAL)
  80. cb->gameState()->players[getOwner()].dwellings.emplace_back(this);
  81. assert(!creatures.empty());
  82. assert(!creatures[0].second.empty());
  83. break;
  84. }
  85. case Obj::REFUGEE_CAMP:
  86. //is handled within newturn func
  87. break;
  88. case Obj::WAR_MACHINE_FACTORY:
  89. creatures.resize(3);
  90. creatures[0].second.emplace_back(CreatureID::BALLISTA);
  91. creatures[1].second.emplace_back(CreatureID::FIRST_AID_TENT);
  92. creatures[2].second.emplace_back(CreatureID::AMMO_CART);
  93. break;
  94. default:
  95. assert(0);
  96. break;
  97. }
  98. }
  99. void CGDwelling::initRandomObjectInfo()
  100. {
  101. vstd::clear_pointer(info);
  102. switch(ID)
  103. {
  104. case Obj::RANDOM_DWELLING: info = new CCreGenLeveledCastleInfo();
  105. break;
  106. case Obj::RANDOM_DWELLING_LVL: info = new CCreGenAsCastleInfo();
  107. break;
  108. case Obj::RANDOM_DWELLING_FACTION: info = new CCreGenLeveledInfo();
  109. break;
  110. }
  111. if(info)
  112. info->owner = this;
  113. }
  114. void CGDwelling::setPropertyDer(ui8 what, ui32 val)
  115. {
  116. switch (what)
  117. {
  118. case ObjProperty::OWNER: //change owner
  119. if (ID == Obj::CREATURE_GENERATOR1 || ID == Obj::CREATURE_GENERATOR2
  120. || ID == Obj::CREATURE_GENERATOR3 || ID == Obj::CREATURE_GENERATOR4)
  121. {
  122. if (tempOwner != PlayerColor::NEUTRAL)
  123. {
  124. std::vector<ConstTransitivePtr<CGDwelling> >* dwellings = &cb->gameState()->players[tempOwner].dwellings;
  125. dwellings->erase (std::find(dwellings->begin(), dwellings->end(), this));
  126. }
  127. if (PlayerColor(val) != PlayerColor::NEUTRAL) //can new owner be neutral?
  128. cb->gameState()->players[PlayerColor(val)].dwellings.emplace_back(this);
  129. }
  130. break;
  131. case ObjProperty::AVAILABLE_CREATURE:
  132. creatures.resize(1);
  133. creatures[0].second.resize(1);
  134. creatures[0].second[0] = CreatureID(val);
  135. break;
  136. }
  137. }
  138. void CGDwelling::onHeroVisit( const CGHeroInstance * h ) const
  139. {
  140. if(ID == Obj::REFUGEE_CAMP && !creatures[0].first) //Refugee Camp, no available cres
  141. {
  142. InfoWindow iw;
  143. iw.type = EInfoWindowMode::AUTO;
  144. iw.player = h->tempOwner;
  145. iw.text.addTxt(MetaString::ADVOB_TXT, 44); //{%s} \n\n The camp is deserted. Perhaps you should try next week.
  146. iw.text.addReplacement(MetaString::OBJ_NAMES, ID);
  147. cb->sendAndApply(&iw);
  148. return;
  149. }
  150. PlayerRelations::PlayerRelations relations = cb->gameState()->getPlayerRelations( h->tempOwner, tempOwner );
  151. if ( relations == PlayerRelations::ALLIES )
  152. return;//do not allow recruiting or capturing
  153. if( !relations && stacksCount() > 0) //object is guarded, owned by enemy
  154. {
  155. BlockingDialog bd(true,false);
  156. bd.player = h->tempOwner;
  157. bd.text.addTxt(MetaString::GENERAL_TXT, 421); //Much to your dismay, the %s is guarded by %s %s. Do you wish to fight the guards?
  158. bd.text.addReplacement(ID == Obj::CREATURE_GENERATOR1 ? MetaString::CREGENS : MetaString::CREGENS4, subID);
  159. if(settings["gameTweaks"]["numericCreaturesQuantities"].Bool())
  160. bd.text.addReplacement(CCreature::getQuantityRangeStringForId(Slots().begin()->second->getQuantityID()));
  161. else
  162. bd.text.addReplacement(MetaString::ARRAY_TXT, 173 + (int)Slots().begin()->second->getQuantityID()*3);
  163. bd.text.addReplacement(*Slots().begin()->second);
  164. cb->showBlockingDialog(&bd);
  165. return;
  166. }
  167. // TODO this shouldn't be hardcoded
  168. if(!relations && ID != Obj::WAR_MACHINE_FACTORY && ID != Obj::REFUGEE_CAMP)
  169. {
  170. cb->setOwner(this, h->tempOwner);
  171. }
  172. BlockingDialog bd (true,false);
  173. bd.player = h->tempOwner;
  174. if(ID == Obj::CREATURE_GENERATOR1 || ID == Obj::CREATURE_GENERATOR4)
  175. {
  176. bd.text.addTxt(MetaString::ADVOB_TXT, ID == Obj::CREATURE_GENERATOR1 ? 35 : 36); //{%s} Would you like to recruit %s? / {%s} Would you like to recruit %s, %s, %s, or %s?
  177. bd.text.addReplacement(ID == Obj::CREATURE_GENERATOR1 ? MetaString::CREGENS : MetaString::CREGENS4, subID);
  178. for(const auto & elem : creatures)
  179. bd.text.addReplacement(MetaString::CRE_PL_NAMES, elem.second[0]);
  180. }
  181. else if(ID == Obj::REFUGEE_CAMP)
  182. {
  183. bd.text.addTxt(MetaString::ADVOB_TXT, 35); //{%s} Would you like to recruit %s?
  184. bd.text.addReplacement(MetaString::OBJ_NAMES, ID);
  185. for(const auto & elem : creatures)
  186. bd.text.addReplacement(MetaString::CRE_PL_NAMES, elem.second[0]);
  187. }
  188. else if(ID == Obj::WAR_MACHINE_FACTORY)
  189. bd.text.addTxt(MetaString::ADVOB_TXT, 157); //{War Machine Factory} Would you like to purchase War Machines?
  190. else
  191. throw std::runtime_error("Illegal dwelling!");
  192. cb->showBlockingDialog(&bd);
  193. }
  194. void CGDwelling::newTurn(CRandomGenerator & rand) const
  195. {
  196. if(cb->getDate(Date::DAY_OF_WEEK) != 1) //not first day of week
  197. return;
  198. //town growths and War Machines Factories are handled separately
  199. if(ID == Obj::TOWN || ID == Obj::WAR_MACHINE_FACTORY)
  200. return;
  201. if(ID == Obj::REFUGEE_CAMP) //if it's a refugee camp, we need to pick an available creature
  202. {
  203. cb->setObjProperty(id, ObjProperty::AVAILABLE_CREATURE, VLC->creh->pickRandomMonster(rand));
  204. }
  205. bool change = false;
  206. SetAvailableCreatures sac;
  207. sac.creatures = creatures;
  208. sac.tid = id;
  209. for (size_t i = 0; i < creatures.size(); i++)
  210. {
  211. if(!creatures[i].second.empty())
  212. {
  213. bool creaturesAccumulate = false;
  214. if (tempOwner.isValidPlayer())
  215. creaturesAccumulate = VLC->settings()->getBoolean(EGameSettings::DWELLINGS_ACCUMULATE_WHEN_OWNED);
  216. else
  217. creaturesAccumulate = VLC->settings()->getBoolean(EGameSettings::DWELLINGS_ACCUMULATE_WHEN_NEUTRAL);
  218. CCreature *cre = VLC->creh->objects[creatures[i].second[0]];
  219. TQuantity amount = cre->getGrowth() * (1 + cre->valOfBonuses(BonusType::CREATURE_GROWTH_PERCENT)/100) + cre->valOfBonuses(BonusType::CREATURE_GROWTH);
  220. if (creaturesAccumulate && ID != Obj::REFUGEE_CAMP) //camp should not try to accumulate different kinds of creatures
  221. sac.creatures[i].first += amount;
  222. else
  223. sac.creatures[i].first = amount;
  224. change = true;
  225. }
  226. }
  227. if(change)
  228. cb->sendAndApply(&sac);
  229. updateGuards();
  230. }
  231. void CGDwelling::updateGuards() const
  232. {
  233. //TODO: store custom guard config and use it
  234. //TODO: store boolean flag for guards
  235. bool guarded = false;
  236. //default condition - creatures are of level 5 or higher
  237. for (auto creatureEntry : creatures)
  238. {
  239. if (VLC->creatures()->getByIndex(creatureEntry.second.at(0))->getLevel() >= 5 && ID != Obj::REFUGEE_CAMP)
  240. {
  241. guarded = true;
  242. break;
  243. }
  244. }
  245. if (guarded)
  246. {
  247. for (auto creatureEntry : creatures)
  248. {
  249. const CCreature * crea = VLC->creh->objects[creatureEntry.second.at(0)];
  250. SlotID slot = getSlotFor(crea->getId());
  251. if (hasStackAtSlot(slot)) //stack already exists, overwrite it
  252. {
  253. ChangeStackCount csc;
  254. csc.army = this->id;
  255. csc.slot = slot;
  256. csc.count = crea->getGrowth() * 3;
  257. csc.absoluteValue = true;
  258. cb->sendAndApply(&csc);
  259. }
  260. else //slot is empty, create whole new stack
  261. {
  262. InsertNewStack ns;
  263. ns.army = this->id;
  264. ns.slot = slot;
  265. ns.type = crea->getId();
  266. ns.count = crea->getGrowth() * 3;
  267. cb->sendAndApply(&ns);
  268. }
  269. }
  270. }
  271. }
  272. void CGDwelling::heroAcceptsCreatures( const CGHeroInstance *h) const
  273. {
  274. CreatureID crid = creatures[0].second[0];
  275. auto *crs = crid.toCreature();
  276. TQuantity count = creatures[0].first;
  277. if(crs->getLevel() == 1 && ID != Obj::REFUGEE_CAMP) //first level - creatures are for free
  278. {
  279. if(count) //there are available creatures
  280. {
  281. SlotID slot = h->getSlotFor(crid);
  282. if(!slot.validSlot()) //no available slot
  283. {
  284. InfoWindow iw;
  285. iw.type = EInfoWindowMode::AUTO;
  286. iw.player = h->tempOwner;
  287. iw.text.addTxt(MetaString::GENERAL_TXT, 425);//The %s would join your hero, but there aren't enough provisions to support them.
  288. iw.text.addReplacement(MetaString::CRE_PL_NAMES, crid);
  289. cb->showInfoDialog(&iw);
  290. }
  291. else //give creatures
  292. {
  293. SetAvailableCreatures sac;
  294. sac.tid = id;
  295. sac.creatures = creatures;
  296. sac.creatures[0].first = 0;
  297. InfoWindow iw;
  298. iw.type = EInfoWindowMode::AUTO;
  299. iw.player = h->tempOwner;
  300. iw.text.addTxt(MetaString::GENERAL_TXT, 423); //%d %s join your army.
  301. iw.text.addReplacement(count);
  302. iw.text.addReplacement(MetaString::CRE_PL_NAMES, crid);
  303. cb->showInfoDialog(&iw);
  304. cb->sendAndApply(&sac);
  305. cb->addToSlot(StackLocation(h, slot), crs, count);
  306. }
  307. }
  308. else //there no creatures
  309. {
  310. InfoWindow iw;
  311. iw.type = EInfoWindowMode::AUTO;
  312. iw.text.addTxt(MetaString::GENERAL_TXT, 422); //There are no %s here to recruit.
  313. iw.text.addReplacement(MetaString::CRE_PL_NAMES, crid);
  314. iw.player = h->tempOwner;
  315. cb->sendAndApply(&iw);
  316. }
  317. }
  318. else
  319. {
  320. if(ID == Obj::WAR_MACHINE_FACTORY) //pick available War Machines
  321. {
  322. //there is 1 war machine available to recruit if hero doesn't have one
  323. SetAvailableCreatures sac;
  324. sac.tid = id;
  325. sac.creatures = creatures;
  326. sac.creatures[0].first = !h->getArt(ArtifactPosition::MACH1); //ballista
  327. sac.creatures[1].first = !h->getArt(ArtifactPosition::MACH3); //first aid tent
  328. sac.creatures[2].first = !h->getArt(ArtifactPosition::MACH2); //ammo cart
  329. cb->sendAndApply(&sac);
  330. }
  331. OpenWindow ow;
  332. ow.id1 = id.getNum();
  333. ow.id2 = h->id.getNum();
  334. ow.window = (ID == Obj::CREATURE_GENERATOR1 || ID == Obj::REFUGEE_CAMP)
  335. ? EOpenWindowMode::RECRUITMENT_FIRST
  336. : EOpenWindowMode::RECRUITMENT_ALL;
  337. cb->sendAndApply(&ow);
  338. }
  339. }
  340. void CGDwelling::battleFinished(const CGHeroInstance *hero, const BattleResult &result) const
  341. {
  342. if (result.winner == 0)
  343. {
  344. onHeroVisit(hero);
  345. }
  346. }
  347. void CGDwelling::blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const
  348. {
  349. auto relations = cb->getPlayerRelations(getOwner(), hero->getOwner());
  350. if(stacksCount() > 0 && relations == PlayerRelations::ENEMIES) //guards present
  351. {
  352. if(answer)
  353. cb->startBattleI(hero, this);
  354. }
  355. else if(answer)
  356. {
  357. heroAcceptsCreatures(hero);
  358. }
  359. }
  360. void CGDwelling::serializeJsonOptions(JsonSerializeFormat & handler)
  361. {
  362. if(!handler.saving)
  363. initRandomObjectInfo();
  364. switch (ID)
  365. {
  366. case Obj::WAR_MACHINE_FACTORY:
  367. case Obj::REFUGEE_CAMP:
  368. //do nothing
  369. break;
  370. case Obj::RANDOM_DWELLING:
  371. case Obj::RANDOM_DWELLING_LVL:
  372. case Obj::RANDOM_DWELLING_FACTION:
  373. info->serializeJson(handler);
  374. //fall through
  375. default:
  376. serializeJsonOwner(handler);
  377. break;
  378. }
  379. }
  380. VCMI_LIB_NAMESPACE_END