CGDwelling.cpp 13 KB

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