CCreatureSet.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652
  1. #define VCMI_DLL
  2. #include "CCreatureSet.h"
  3. #include "CCreatureHandler.h"
  4. #include "VCMI_Lib.h"
  5. #include <assert.h>
  6. #include "CObjectHandler.h"
  7. #include "IGameCallback.h"
  8. #include "CGameState.h"
  9. #include "CGeneralTextHandler.h"
  10. #include <sstream>
  11. #include "CSpellHandler.h"
  12. #include <boost/lexical_cast.hpp>
  13. #include <boost/algorithm/string/replace.hpp>
  14. const CStackInstance &CCreatureSet::operator[](TSlot slot) const
  15. {
  16. TSlots::const_iterator i = stacks.find(slot);
  17. if (i != stacks.end())
  18. return *i->second;
  19. else
  20. throw std::string("That slot is empty!");
  21. }
  22. const CCreature* CCreatureSet::getCreature(TSlot slot) const
  23. {
  24. TSlots::const_iterator i = stacks.find(slot);
  25. if (i != stacks.end())
  26. return i->second->type;
  27. else
  28. return NULL;
  29. }
  30. bool CCreatureSet::setCreature(TSlot slot, TCreature type, TQuantity quantity) /*slots 0 to 6 */
  31. {
  32. if(slot > 6 || slot < 0)
  33. {
  34. tlog1 << "Cannot set slot " << slot << std::endl;
  35. return false;
  36. }
  37. if(!quantity)
  38. {
  39. tlog2 << "Using set creature to delete stack?\n";
  40. eraseStack(slot);
  41. return true;
  42. }
  43. if(hasStackAtSlot(slot)) //remove old creature
  44. eraseStack(slot);
  45. putStack(slot, new CStackInstance(type, quantity));
  46. return true;
  47. }
  48. TSlot CCreatureSet::getSlotFor(TCreature creature, ui32 slotsAmount/*=7*/) const /*returns -1 if no slot available */
  49. {
  50. return getSlotFor(VLC->creh->creatures[creature], slotsAmount);
  51. }
  52. TSlot CCreatureSet::getSlotFor(const CCreature *c, ui32 slotsAmount/*=ARMY_SIZE*/) const
  53. {
  54. assert(c->valid());
  55. for(TSlots::const_iterator i=stacks.begin(); i!=stacks.end(); ++i)
  56. {
  57. assert(i->second->type->valid());
  58. if(i->second->type == c)
  59. {
  60. return i->first; //if there is already such creature we return its slot id
  61. }
  62. }
  63. for(ui32 i=0; i<slotsAmount; i++)
  64. {
  65. if(stacks.find(i) == stacks.end())
  66. {
  67. return i; //return first free slot
  68. }
  69. }
  70. return -1; //no slot available
  71. }
  72. int CCreatureSet::getStackCount(TSlot slot) const
  73. {
  74. TSlots::const_iterator i = stacks.find(slot);
  75. if (i != stacks.end())
  76. return i->second->count;
  77. else
  78. return 0; //TODO? consider issuing a warning
  79. }
  80. bool CCreatureSet::mergableStacks(std::pair<TSlot, TSlot> &out, TSlot preferable /*= -1*/) const /*looks for two same stacks, returns slot positions */
  81. {
  82. //try to match creature to our preferred stack
  83. if(preferable >= 0 && vstd::contains(stacks, preferable))
  84. {
  85. const CCreature *cr = stacks.find(preferable)->second->type;
  86. for(TSlots::const_iterator j=stacks.begin(); j!=stacks.end(); ++j)
  87. {
  88. if(cr == j->second->type && j->first != preferable)
  89. {
  90. out.first = preferable;
  91. out.second = j->first;
  92. return true;
  93. }
  94. }
  95. }
  96. for(TSlots::const_iterator i=stacks.begin(); i!=stacks.end(); ++i)
  97. {
  98. for(TSlots::const_iterator j=stacks.begin(); j!=stacks.end(); ++j)
  99. {
  100. if(i->second->type == j->second->type && i->first != j->first)
  101. {
  102. out.first = i->first;
  103. out.second = j->first;
  104. return true;
  105. }
  106. }
  107. }
  108. return false;
  109. }
  110. void CCreatureSet::sweep()
  111. {
  112. for(TSlots::iterator i=stacks.begin(); i!=stacks.end(); ++i)
  113. {
  114. if(!i->second->count)
  115. {
  116. stacks.erase(i);
  117. sweep();
  118. break;
  119. }
  120. }
  121. }
  122. void CCreatureSet::addToSlot(TSlot slot, TCreature cre, TQuantity count, bool allowMerging/* = true*/)
  123. {
  124. const CCreature *c = VLC->creh->creatures[cre];
  125. if(!hasStackAtSlot(slot))
  126. {
  127. setCreature(slot, cre, count);
  128. }
  129. else if(getCreature(slot) == c && allowMerging) //that slot was empty or contained same type creature
  130. {
  131. setStackCount(slot, getStackCount(slot) + count);
  132. }
  133. else
  134. {
  135. tlog1 << "Failed adding to slot!\n";
  136. }
  137. }
  138. void CCreatureSet::addToSlot(TSlot slot, CStackInstance *stack, bool allowMerging/* = true*/)
  139. {
  140. assert(stack->valid(true));
  141. if(!hasStackAtSlot(slot))
  142. {
  143. putStack(slot, stack);
  144. }
  145. else if(allowMerging && stack->type == getCreature(slot))
  146. {
  147. joinStack(slot, stack);
  148. }
  149. else
  150. {
  151. tlog1 << "Cannot add to slot " << slot << " stack " << *stack << std::endl;
  152. }
  153. }
  154. bool CCreatureSet::validTypes(bool allowUnrandomized /*= false*/) const
  155. {
  156. for(TSlots::const_iterator i=stacks.begin(); i!=stacks.end(); ++i)
  157. {
  158. if(!i->second->valid(allowUnrandomized))
  159. return false;
  160. }
  161. return true;
  162. }
  163. bool CCreatureSet::slotEmpty(TSlot slot) const
  164. {
  165. return !hasStackAtSlot(slot);
  166. }
  167. bool CCreatureSet::needsLastStack() const
  168. {
  169. return false;
  170. }
  171. int CCreatureSet::getArmyStrength() const
  172. {
  173. int ret = 0;
  174. for(TSlots::const_iterator i = stacks.begin(); i != stacks.end(); i++)
  175. ret += i->second->type->AIValue * i->second->count;
  176. return ret;
  177. }
  178. ui64 CCreatureSet::getPower (TSlot slot) const
  179. {
  180. return getCreature(slot)->AIValue * getStackCount(slot);
  181. }
  182. std::string CCreatureSet::getRoughAmount (TSlot slot) const
  183. {
  184. return VLC->generaltexth->arraytxt[174 + 3*CCreature::getQuantityID(getStackCount(slot))];
  185. }
  186. int CCreatureSet::stacksCount() const
  187. {
  188. return stacks.size();
  189. }
  190. void CCreatureSet::setFormation(bool tight)
  191. {
  192. formation = tight;
  193. }
  194. void CCreatureSet::setStackCount(TSlot slot, TQuantity count)
  195. {
  196. assert(hasStackAtSlot(slot));
  197. assert(count > 0);
  198. stacks[slot]->count = count;
  199. armyChanged();
  200. }
  201. void CCreatureSet::clear()
  202. {
  203. while(!stacks.empty())
  204. {
  205. eraseStack(stacks.begin()->first);
  206. }
  207. }
  208. const CStackInstance& CCreatureSet::getStack(TSlot slot) const
  209. {
  210. assert(hasStackAtSlot(slot));
  211. return *stacks.find(slot)->second;
  212. }
  213. void CCreatureSet::eraseStack(TSlot slot)
  214. {
  215. assert(hasStackAtSlot(slot));
  216. CStackInstance *toErase = detachStack(slot);
  217. delNull(toErase);
  218. }
  219. bool CCreatureSet::contains(const CStackInstance *stack) const
  220. {
  221. if(!stack)
  222. return false;
  223. for(TSlots::const_iterator i = stacks.begin(); i != stacks.end(); ++i)
  224. if(i->second == stack)
  225. return true;
  226. return false;
  227. }
  228. TSlot CCreatureSet::findStack(const CStackInstance *stack) const
  229. {
  230. if(!stack)
  231. return -1;
  232. for(TSlots::const_iterator i = stacks.begin(); i != stacks.end(); ++i)
  233. if(i->second == stack)
  234. return i->first;
  235. return -1;
  236. }
  237. CArmedInstance * CCreatureSet::castToArmyObj()
  238. {
  239. return dynamic_cast<CArmedInstance *>(this);
  240. }
  241. void CCreatureSet::putStack(TSlot slot, CStackInstance *stack)
  242. {
  243. assert(!hasStackAtSlot(slot));
  244. stacks[slot] = stack;
  245. stack->setArmyObj(castToArmyObj());
  246. armyChanged();
  247. }
  248. void CCreatureSet::joinStack(TSlot slot, CStackInstance * stack)
  249. {
  250. const CCreature *c = getCreature(slot);
  251. assert(c == stack->type);
  252. assert(c);
  253. //TODO move stuff
  254. changeStackCount(slot, stack->count);
  255. delNull(stack);
  256. }
  257. void CCreatureSet::changeStackCount(TSlot slot, TQuantity toAdd)
  258. {
  259. setStackCount(slot, getStackCount(slot) + toAdd);
  260. }
  261. CCreatureSet::CCreatureSet()
  262. {
  263. formation = false;
  264. }
  265. CCreatureSet::CCreatureSet(const CCreatureSet&)
  266. {
  267. assert(0);
  268. }
  269. CCreatureSet::~CCreatureSet()
  270. {
  271. clear();
  272. }
  273. void CCreatureSet::setToArmy(CSimpleArmy &src)
  274. {
  275. clear();
  276. while(src)
  277. {
  278. TSimpleSlots::iterator i = src.army.begin();
  279. assert(i->second.type);
  280. assert(i->second.count);
  281. putStack(i->first, new CStackInstance(i->second.type, i->second.count));
  282. src.army.erase(i);
  283. }
  284. }
  285. CStackInstance * CCreatureSet::detachStack(TSlot slot)
  286. {
  287. assert(hasStackAtSlot(slot));
  288. CStackInstance *ret = stacks[slot];
  289. //if(CArmedInstance *armedObj = castToArmyObj())
  290. {
  291. ret->setArmyObj(NULL); //detaches from current armyobj
  292. }
  293. assert(!ret->armyObj); //we failed detaching?
  294. stacks.erase(slot);
  295. armyChanged();
  296. return ret;
  297. }
  298. void CCreatureSet::setStackType(TSlot slot, const CCreature *type)
  299. {
  300. assert(hasStackAtSlot(slot));
  301. CStackInstance *s = stacks[slot];
  302. s->setType(type->idNumber);
  303. armyChanged();
  304. }
  305. bool CCreatureSet::canBeMergedWith(const CCreatureSet &cs, bool allowMergingStacks) const
  306. {
  307. if(!allowMergingStacks)
  308. {
  309. int freeSlots = stacksCount() - ARMY_SIZE;
  310. std::set<const CCreature*> cresToAdd;
  311. for(TSlots::const_iterator i = cs.stacks.begin(); i != cs.stacks.end(); i++)
  312. {
  313. TSlot dest = getSlotFor(i->second->type);
  314. if(dest < 0 || hasStackAtSlot(dest))
  315. cresToAdd.insert(i->second->type);
  316. }
  317. return cresToAdd.size() <= freeSlots;
  318. }
  319. else
  320. {
  321. std::set<const CCreature*> cres;
  322. //get types of creatures that need their own slot
  323. for(TSlots::const_iterator i = cs.stacks.begin(); i != cs.stacks.end(); i++)
  324. cres.insert(i->second->type);
  325. for(TSlots::const_iterator i = stacks.begin(); i != stacks.end(); i++)
  326. cres.insert(i->second->type);
  327. return cres.size() <= ARMY_SIZE;
  328. }
  329. }
  330. bool CCreatureSet::hasStackAtSlot(TSlot slot) const
  331. {
  332. return vstd::contains(stacks, slot);
  333. }
  334. CCreatureSet & CCreatureSet::operator=(const CCreatureSet&cs)
  335. {
  336. assert(0);
  337. return *this;
  338. }
  339. void CCreatureSet::armyChanged()
  340. {
  341. }
  342. CStackInstance::CStackInstance()
  343. : armyObj(_armyObj)
  344. {
  345. init();
  346. }
  347. CStackInstance::CStackInstance(TCreature id, TQuantity Count)
  348. : armyObj(_armyObj)
  349. {
  350. init();
  351. setType(id);
  352. count = Count;
  353. }
  354. CStackInstance::CStackInstance(const CCreature *cre, TQuantity Count)
  355. : armyObj(_armyObj)
  356. {
  357. init();
  358. setType(cre);
  359. count = Count;
  360. }
  361. void CStackInstance::init()
  362. {
  363. experience = 0;
  364. count = 0;
  365. type = NULL;
  366. idRand = -1;
  367. _armyObj = NULL;
  368. nodeType = STACK;
  369. }
  370. int CStackInstance::getQuantityID() const
  371. {
  372. return CCreature::getQuantityID(count);
  373. }
  374. void CStackInstance::setType(int creID)
  375. {
  376. setType(VLC->creh->creatures[creID]);
  377. }
  378. void CStackInstance::setType(const CCreature *c)
  379. {
  380. if(type)
  381. detachFrom(const_cast<CCreature*>(type));
  382. type = c;
  383. attachTo(const_cast<CCreature*>(type));
  384. }
  385. std::string CStackInstance::bonusToString(Bonus *bonus, bool description) const
  386. {
  387. std::map<TBonusType, std::pair<std::string, std::string> >::iterator it = VLC->creh->stackBonuses.find(bonus->type);
  388. if (it != VLC->creh->stackBonuses.end())
  389. {
  390. std::string text;
  391. if (description) //long ability description
  392. {
  393. text = it->second.second;
  394. switch (bonus->type)
  395. {
  396. //no additional modifiers needed
  397. case Bonus::FLYING:
  398. case Bonus::UNLIMITED_RETALIATIONS:
  399. case Bonus::SHOOTER:
  400. case Bonus::FREE_SHOOTING:
  401. case Bonus::NO_SHOTING_PENALTY:
  402. case Bonus::NO_MELEE_PENALTY:
  403. case Bonus::NO_DISTANCE_PENALTY:
  404. case Bonus::NO_OBSTACLES_PENALTY:
  405. case Bonus::JOUSTING: //TODO: percent bonus?
  406. case Bonus::RETURN_AFTER_STRIKE:
  407. case Bonus::BLOCKS_RETALIATION:
  408. case Bonus::TWO_HEX_ATTACK_BREATH:
  409. case Bonus::THREE_HEADED_ATTACK:
  410. case Bonus::ATTACKS_ALL_ADJACENT:
  411. case Bonus::FULL_HP_REGENERATION:
  412. case Bonus::LIFE_DRAIN: //TODO: chance, hp percentage?
  413. case Bonus::SELF_MORALE:
  414. case Bonus::SELF_LUCK:
  415. case Bonus::FEAR:
  416. case Bonus::FEARLESS:
  417. case Bonus::CHARGE_IMMUNITY:
  418. case Bonus::HEALER:
  419. case Bonus::CATAPULT:
  420. case Bonus::DRAGON_NATURE:
  421. case Bonus::NON_LIVING:
  422. case Bonus::UNDEAD:
  423. break;
  424. //One numeric value
  425. //case Bonus::STACKS_SPEED: //Do we need description for creature stats?
  426. //case Bonus::STACK_HEALTH:
  427. case Bonus::MAGIC_RESISTANCE:
  428. case Bonus::SPELL_DAMAGE_REDUCTION:
  429. case Bonus::LEVEL_SPELL_IMMUNITY:
  430. case Bonus::CHANGES_SPELL_COST_FOR_ALLY:
  431. case Bonus::CHANGES_SPELL_COST_FOR_ENEMY:
  432. case Bonus::MANA_CHANNELING:
  433. case Bonus::MANA_DRAIN:
  434. case Bonus::HP_REGENERATION:
  435. case Bonus::ADDITIONAL_RETALIATION:
  436. case Bonus::DOUBLE_DAMAGE_CHANCE:
  437. case Bonus::ENEMY_DEFENCE_REDUCTION:
  438. case Bonus::MAGIC_MIRROR:
  439. case Bonus::DARKNESS: //Darkness Dragons any1?
  440. boost::algorithm::replace_first(text, "%d", boost::lexical_cast<std::string>(bonus->val));
  441. break;
  442. //Complex descriptions
  443. case Bonus::HATE: //TODO: customize damage percent
  444. boost::algorithm::replace_first(text, "%s", VLC->creh->creatures[bonus->subtype]->namePl);
  445. break;
  446. case Bonus::SPELL_IMMUNITY:
  447. boost::algorithm::replace_first(text, "%s", VLC->spellh->spells[bonus->subtype]->name);
  448. break;
  449. case Bonus::SPELL_AFTER_ATTACK:
  450. boost::algorithm::replace_first(text, "%d", boost::lexical_cast<std::string>(bonus->additionalInfo % 100));
  451. boost::algorithm::replace_first(text, "%s", VLC->spellh->spells[bonus->subtype]->name);
  452. break;
  453. default:
  454. {}//TODO: allow custom bonus types... someday, somehow
  455. }
  456. }
  457. else //short name
  458. {
  459. text = it->second.first;
  460. switch (bonus->type)
  461. {
  462. case Bonus::HATE:
  463. boost::algorithm::replace_first(text, "%s", VLC->creh->creatures[bonus->subtype]->namePl);
  464. break;
  465. case Bonus::LEVEL_SPELL_IMMUNITY:
  466. boost::algorithm::replace_first(text, "%d", boost::lexical_cast<std::string>(bonus->val));
  467. break;
  468. case Bonus::SPELL_IMMUNITY:
  469. case Bonus::SPELL_AFTER_ATTACK:
  470. boost::algorithm::replace_first(text, "%s", VLC->spellh->spells[bonus->subtype]->name);
  471. break;
  472. }
  473. }
  474. return text;
  475. }
  476. else
  477. return "";
  478. }
  479. void CStackInstance::setArmyObj(const CArmedInstance *ArmyObj)
  480. {
  481. if(_armyObj)
  482. detachFrom(const_cast<CArmedInstance*>(_armyObj));
  483. _armyObj = ArmyObj;
  484. if(ArmyObj)
  485. {
  486. attachTo(const_cast<CArmedInstance*>(_armyObj));
  487. }
  488. }
  489. // void CStackInstance::getParents(TCNodes &out, const CBonusSystemNode *source /*= NULL*/) const
  490. // {
  491. // out.insert(type);
  492. //
  493. // if(source && source != this) //we should be root, if not - do not inherit anything
  494. // return;
  495. //
  496. // if(armyObj)
  497. // out.insert(armyObj);
  498. // else
  499. // out.insert(&IObjectInterface::cb->gameState()->globalEffects);
  500. // }
  501. std::string CStackInstance::getQuantityTXT(bool capitalized /*= true*/) const
  502. {
  503. return VLC->generaltexth->arraytxt[174 + getQuantityID()*3 + 2 - capitalized];
  504. }
  505. bool CStackInstance::valid(bool allowUnrandomized) const
  506. {
  507. bool isRand = (idRand != -1);
  508. if(!isRand)
  509. {
  510. return (type && type == VLC->creh->creatures[type->idNumber]);
  511. }
  512. else
  513. return allowUnrandomized;
  514. }
  515. CStackInstance::~CStackInstance()
  516. {
  517. }
  518. std::string CStackInstance::nodeName() const
  519. {
  520. std::ostringstream oss;
  521. oss << "Stack of " << count << " creatures of ";
  522. if(type)
  523. oss << type->namePl;
  524. else if(idRand)
  525. oss << "[no type, idRand=" << idRand << "]";
  526. else
  527. oss << "[UNDEFINED TYPE]";
  528. return oss.str();
  529. }
  530. void CStackInstance::deserializationFix()
  531. {
  532. setType(type);
  533. setArmyObj(armyObj);
  534. }
  535. CStackBasicDescriptor::CStackBasicDescriptor()
  536. {
  537. type = NULL;
  538. count = -1;
  539. }
  540. CStackBasicDescriptor::CStackBasicDescriptor(TCreature id, TQuantity Count)
  541. : type (VLC->creh->creatures[id]), count(Count)
  542. {
  543. }
  544. CStackBasicDescriptor::CStackBasicDescriptor(const CCreature *c, TQuantity Count)
  545. : type(c), count(Count)
  546. {
  547. }
  548. DLL_EXPORT std::ostream & operator<<(std::ostream & str, const CStackInstance & sth)
  549. {
  550. if(!sth.valid(true))
  551. str << "an invalid stack!";
  552. str << "stack with " << sth.count << " of ";
  553. if(sth.type)
  554. str << sth.type->namePl;
  555. else
  556. str << sth.idRand;
  557. return str;
  558. }
  559. void CSimpleArmy::clear()
  560. {
  561. army.clear();
  562. }
  563. CSimpleArmy::operator bool() const
  564. {
  565. return army.size();
  566. }
  567. bool CSimpleArmy::setCreature(TSlot slot, TCreature cre, TQuantity count)
  568. {
  569. assert(!vstd::contains(army, slot));
  570. army[slot] = CStackBasicDescriptor(cre, count);
  571. return true;
  572. }