CCreatureSet.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  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. const CStackInstance &CCreatureSet::operator[](TSlot slot) const
  12. {
  13. TSlots::const_iterator i = stacks.find(slot);
  14. if (i != stacks.end())
  15. return *i->second;
  16. else
  17. throw std::string("That slot is empty!");
  18. }
  19. const CCreature* CCreatureSet::getCreature(TSlot slot) const
  20. {
  21. TSlots::const_iterator i = stacks.find(slot);
  22. if (i != stacks.end())
  23. return i->second->type;
  24. else
  25. return NULL;
  26. }
  27. bool CCreatureSet::setCreature(TSlot slot, TCreature type, TQuantity quantity) /*slots 0 to 6 */
  28. {
  29. if(slot > 6 || slot < 0)
  30. {
  31. tlog1 << "Cannot set slot " << slot << std::endl;
  32. return false;
  33. }
  34. if(!quantity)
  35. {
  36. tlog2 << "Using set creature to delete stack?\n";
  37. eraseStack(slot);
  38. return true;
  39. }
  40. if(hasStackAtSlot(slot)) //remove old creature
  41. eraseStack(slot);
  42. putStack(slot, new CStackInstance(type, quantity));
  43. return true;
  44. }
  45. TSlot CCreatureSet::getSlotFor(TCreature creature, ui32 slotsAmount/*=7*/) const /*returns -1 if no slot available */
  46. {
  47. return getSlotFor(VLC->creh->creatures[creature], slotsAmount);
  48. }
  49. TSlot CCreatureSet::getSlotFor(const CCreature *c, ui32 slotsAmount/*=ARMY_SIZE*/) const
  50. {
  51. assert(c->valid());
  52. for(TSlots::const_iterator i=stacks.begin(); i!=stacks.end(); ++i)
  53. {
  54. assert(i->second->type->valid());
  55. if(i->second->type == c)
  56. {
  57. return i->first; //if there is already such creature we return its slot id
  58. }
  59. }
  60. for(ui32 i=0; i<slotsAmount; i++)
  61. {
  62. if(stacks.find(i) == stacks.end())
  63. {
  64. return i; //return first free slot
  65. }
  66. }
  67. return -1; //no slot available
  68. }
  69. int CCreatureSet::getStackCount(TSlot slot) const
  70. {
  71. TSlots::const_iterator i = stacks.find(slot);
  72. if (i != stacks.end())
  73. return i->second->count;
  74. else
  75. return 0; //TODO? consider issuing a warning
  76. }
  77. bool CCreatureSet::mergableStacks(std::pair<TSlot, TSlot> &out, TSlot preferable /*= -1*/) const /*looks for two same stacks, returns slot positions */
  78. {
  79. //try to match creature to our preferred stack
  80. if(preferable >= 0 && vstd::contains(stacks, preferable))
  81. {
  82. const CCreature *cr = stacks.find(preferable)->second->type;
  83. for(TSlots::const_iterator j=stacks.begin(); j!=stacks.end(); ++j)
  84. {
  85. if(cr == j->second->type && j->first != preferable)
  86. {
  87. out.first = preferable;
  88. out.second = j->first;
  89. return true;
  90. }
  91. }
  92. }
  93. for(TSlots::const_iterator i=stacks.begin(); i!=stacks.end(); ++i)
  94. {
  95. for(TSlots::const_iterator j=stacks.begin(); j!=stacks.end(); ++j)
  96. {
  97. if(i->second->type == j->second->type && i->first != j->first)
  98. {
  99. out.first = i->first;
  100. out.second = j->first;
  101. return true;
  102. }
  103. }
  104. }
  105. return false;
  106. }
  107. void CCreatureSet::sweep()
  108. {
  109. for(TSlots::iterator i=stacks.begin(); i!=stacks.end(); ++i)
  110. {
  111. if(!i->second->count)
  112. {
  113. stacks.erase(i);
  114. sweep();
  115. break;
  116. }
  117. }
  118. }
  119. void CCreatureSet::addToSlot(TSlot slot, TCreature cre, TQuantity count, bool allowMerging/* = true*/)
  120. {
  121. const CCreature *c = VLC->creh->creatures[cre];
  122. if(!hasStackAtSlot(slot))
  123. {
  124. setCreature(slot, cre, count);
  125. }
  126. else if(getCreature(slot) == c && allowMerging) //that slot was empty or contained same type creature
  127. {
  128. setStackCount(slot, getStackCount(slot) + count);
  129. }
  130. else
  131. {
  132. tlog1 << "Failed adding to slot!\n";
  133. }
  134. }
  135. void CCreatureSet::addToSlot(TSlot slot, CStackInstance *stack, bool allowMerging/* = true*/)
  136. {
  137. assert(stack->valid(true));
  138. if(!hasStackAtSlot(slot))
  139. {
  140. putStack(slot, stack);
  141. }
  142. else if(allowMerging && stack->type == getCreature(slot))
  143. {
  144. joinStack(slot, stack);
  145. }
  146. else
  147. {
  148. tlog1 << "Cannot add to slot " << slot << " stack " << *stack << std::endl;
  149. }
  150. }
  151. bool CCreatureSet::validTypes(bool allowUnrandomized /*= false*/) const
  152. {
  153. for(TSlots::const_iterator i=stacks.begin(); i!=stacks.end(); ++i)
  154. {
  155. if(!i->second->valid(allowUnrandomized))
  156. return false;
  157. }
  158. return true;
  159. }
  160. bool CCreatureSet::slotEmpty(TSlot slot) const
  161. {
  162. return !hasStackAtSlot(slot);
  163. }
  164. bool CCreatureSet::needsLastStack() const
  165. {
  166. return false;
  167. }
  168. int CCreatureSet::getArmyStrength() const
  169. {
  170. int ret = 0;
  171. for(TSlots::const_iterator i = stacks.begin(); i != stacks.end(); i++)
  172. ret += i->second->type->AIValue * i->second->count;
  173. return ret;
  174. }
  175. ui64 CCreatureSet::getPower (TSlot slot) const
  176. {
  177. return getCreature(slot)->AIValue * getStackCount(slot);
  178. }
  179. std::string CCreatureSet::getRoughAmount (TSlot slot) const
  180. {
  181. return VLC->generaltexth->arraytxt[174 + 3*CCreature::getQuantityID(getStackCount(slot))];
  182. }
  183. int CCreatureSet::stacksCount() const
  184. {
  185. return stacks.size();
  186. }
  187. void CCreatureSet::setFormation(bool tight)
  188. {
  189. formation = tight;
  190. }
  191. void CCreatureSet::setStackCount(TSlot slot, TQuantity count)
  192. {
  193. assert(hasStackAtSlot(slot));
  194. assert(count > 0);
  195. stacks[slot]->count = count;
  196. armyChanged();
  197. }
  198. void CCreatureSet::clear()
  199. {
  200. while(!stacks.empty())
  201. {
  202. eraseStack(stacks.begin()->first);
  203. }
  204. }
  205. const CStackInstance& CCreatureSet::getStack(TSlot slot) const
  206. {
  207. assert(hasStackAtSlot(slot));
  208. return *stacks.find(slot)->second;
  209. }
  210. void CCreatureSet::eraseStack(TSlot slot)
  211. {
  212. assert(hasStackAtSlot(slot));
  213. CStackInstance *toErase = detachStack(slot);
  214. delNull(toErase);
  215. }
  216. bool CCreatureSet::contains(const CStackInstance *stack) const
  217. {
  218. if(!stack)
  219. return false;
  220. for(TSlots::const_iterator i = stacks.begin(); i != stacks.end(); ++i)
  221. if(i->second == stack)
  222. return true;
  223. return false;
  224. }
  225. TSlot CCreatureSet::findStack(const CStackInstance *stack) const
  226. {
  227. if(!stack)
  228. return -1;
  229. for(TSlots::const_iterator i = stacks.begin(); i != stacks.end(); ++i)
  230. if(i->second == stack)
  231. return i->first;
  232. return -1;
  233. }
  234. CArmedInstance * CCreatureSet::castToArmyObj()
  235. {
  236. return dynamic_cast<CArmedInstance *>(this);
  237. }
  238. void CCreatureSet::putStack(TSlot slot, CStackInstance *stack)
  239. {
  240. assert(!hasStackAtSlot(slot));
  241. stacks[slot] = stack;
  242. stack->setArmyObj(castToArmyObj());
  243. armyChanged();
  244. }
  245. void CCreatureSet::joinStack(TSlot slot, CStackInstance * stack)
  246. {
  247. const CCreature *c = getCreature(slot);
  248. assert(c == stack->type);
  249. assert(c);
  250. //TODO move stuff
  251. changeStackCount(slot, stack->count);
  252. delNull(stack);
  253. }
  254. void CCreatureSet::changeStackCount(TSlot slot, TQuantity toAdd)
  255. {
  256. setStackCount(slot, getStackCount(slot) + toAdd);
  257. }
  258. CCreatureSet::CCreatureSet()
  259. {
  260. formation = false;
  261. }
  262. CCreatureSet::CCreatureSet(const CCreatureSet&)
  263. {
  264. assert(0);
  265. }
  266. CCreatureSet::~CCreatureSet()
  267. {
  268. clear();
  269. }
  270. void CCreatureSet::setToArmy(CSimpleArmy &src)
  271. {
  272. clear();
  273. while(src)
  274. {
  275. TSimpleSlots::iterator i = src.army.begin();
  276. assert(i->second.type);
  277. assert(i->second.count);
  278. putStack(i->first, new CStackInstance(i->second.type, i->second.count));
  279. src.army.erase(i);
  280. }
  281. }
  282. CStackInstance * CCreatureSet::detachStack(TSlot slot)
  283. {
  284. assert(hasStackAtSlot(slot));
  285. CStackInstance *ret = stacks[slot];
  286. if(CArmedInstance *armedObj = castToArmyObj())
  287. {
  288. ret->setArmyObj(NULL); //detaches from current armyobj
  289. }
  290. assert(!ret->armyObj); //we failed detaching?
  291. stacks.erase(slot);
  292. armyChanged();
  293. return ret;
  294. }
  295. void CCreatureSet::setStackType(TSlot slot, const CCreature *type)
  296. {
  297. assert(hasStackAtSlot(slot));
  298. CStackInstance *s = stacks[slot];
  299. s->setType(type->idNumber);
  300. armyChanged();
  301. }
  302. bool CCreatureSet::canBeMergedWith(const CCreatureSet &cs, bool allowMergingStacks) const
  303. {
  304. if(!allowMergingStacks)
  305. {
  306. int freeSlots = stacksCount() - ARMY_SIZE;
  307. std::set<const CCreature*> cresToAdd;
  308. for(TSlots::const_iterator i = cs.stacks.begin(); i != cs.stacks.end(); i++)
  309. {
  310. TSlot dest = getSlotFor(i->second->type);
  311. if(dest < 0 || hasStackAtSlot(dest))
  312. cresToAdd.insert(i->second->type);
  313. }
  314. return cresToAdd.size() <= freeSlots;
  315. }
  316. else
  317. {
  318. std::set<const CCreature*> cres;
  319. //get types of creatures that need their own slot
  320. for(TSlots::const_iterator i = cs.stacks.begin(); i != cs.stacks.end(); i++)
  321. cres.insert(i->second->type);
  322. for(TSlots::const_iterator i = stacks.begin(); i != stacks.end(); i++)
  323. cres.insert(i->second->type);
  324. return cres.size() <= ARMY_SIZE;
  325. }
  326. }
  327. bool CCreatureSet::hasStackAtSlot(TSlot slot) const
  328. {
  329. return vstd::contains(stacks, slot);
  330. }
  331. CCreatureSet & CCreatureSet::operator=(const CCreatureSet&cs)
  332. {
  333. assert(0);
  334. return *this;
  335. }
  336. void CCreatureSet::armyChanged()
  337. {
  338. }
  339. CStackInstance::CStackInstance()
  340. : armyObj(_armyObj)
  341. {
  342. init();
  343. }
  344. CStackInstance::CStackInstance(TCreature id, TQuantity Count)
  345. : armyObj(_armyObj)
  346. {
  347. init();
  348. setType(id);
  349. count = Count;
  350. }
  351. CStackInstance::CStackInstance(const CCreature *cre, TQuantity Count)
  352. : armyObj(_armyObj)
  353. {
  354. init();
  355. setType(cre);
  356. count = Count;
  357. }
  358. void CStackInstance::init()
  359. {
  360. experience = 0;
  361. count = 0;
  362. type = NULL;
  363. idRand = -1;
  364. _armyObj = NULL;
  365. nodeType = STACK;
  366. }
  367. int CStackInstance::getQuantityID() const
  368. {
  369. return CCreature::getQuantityID(count);
  370. }
  371. void CStackInstance::setType(int creID)
  372. {
  373. setType(VLC->creh->creatures[creID]);
  374. }
  375. void CStackInstance::setType(const CCreature *c)
  376. {
  377. if(type)
  378. detachFrom(const_cast<CCreature*>(type));
  379. type = c;
  380. attachTo(const_cast<CCreature*>(type));
  381. }
  382. void CStackInstance::setArmyObj(const CArmedInstance *ArmyObj)
  383. {
  384. if(_armyObj)
  385. detachFrom(const_cast<CArmedInstance*>(_armyObj));
  386. _armyObj = ArmyObj;
  387. if(ArmyObj)
  388. {
  389. attachTo(const_cast<CArmedInstance*>(_armyObj));
  390. }
  391. }
  392. // void CStackInstance::getParents(TCNodes &out, const CBonusSystemNode *source /*= NULL*/) const
  393. // {
  394. // out.insert(type);
  395. //
  396. // if(source && source != this) //we should be root, if not - do not inherit anything
  397. // return;
  398. //
  399. // if(armyObj)
  400. // out.insert(armyObj);
  401. // else
  402. // out.insert(&IObjectInterface::cb->gameState()->globalEffects);
  403. // }
  404. std::string CStackInstance::getQuantityTXT(bool capitalized /*= true*/) const
  405. {
  406. return VLC->generaltexth->arraytxt[174 + getQuantityID()*3 + 2 - capitalized];
  407. }
  408. bool CStackInstance::valid(bool allowUnrandomized) const
  409. {
  410. bool isRand = (idRand != -1);
  411. if(!isRand)
  412. {
  413. return (type && type == VLC->creh->creatures[type->idNumber]);
  414. }
  415. else
  416. return allowUnrandomized;
  417. }
  418. CStackInstance::~CStackInstance()
  419. {
  420. }
  421. std::string CStackInstance::nodeName() const
  422. {
  423. std::ostringstream oss;
  424. oss << "Stack of " << count << " creatures of ";
  425. if(type)
  426. oss << type->namePl;
  427. else if(idRand)
  428. oss << "[no type, idRand=" << idRand << "]";
  429. else
  430. oss << "[UNDEFINED TYPE]";
  431. return oss.str();
  432. }
  433. void CStackInstance::deserializationFix()
  434. {
  435. setType(type);
  436. setArmyObj(armyObj);
  437. }
  438. CStackBasicDescriptor::CStackBasicDescriptor()
  439. {
  440. type = NULL;
  441. count = -1;
  442. }
  443. CStackBasicDescriptor::CStackBasicDescriptor(TCreature id, TQuantity Count)
  444. : type (VLC->creh->creatures[id]), count(Count)
  445. {
  446. }
  447. CStackBasicDescriptor::CStackBasicDescriptor(const CCreature *c, TQuantity Count)
  448. : type(c), count(Count)
  449. {
  450. }
  451. DLL_EXPORT std::ostream & operator<<(std::ostream & str, const CStackInstance & sth)
  452. {
  453. if(!sth.valid(true))
  454. str << "an invalid stack!";
  455. str << "stack with " << sth.count << " of ";
  456. if(sth.type)
  457. str << sth.type->namePl;
  458. else
  459. str << sth.idRand;
  460. return str;
  461. }
  462. void CSimpleArmy::clear()
  463. {
  464. army.clear();
  465. }
  466. CSimpleArmy::operator bool() const
  467. {
  468. return army.size();
  469. }
  470. bool CSimpleArmy::setCreature(TSlot slot, TCreature cre, TQuantity count)
  471. {
  472. assert(!vstd::contains(army, slot));
  473. army[slot] = CStackBasicDescriptor(cre, count);
  474. return true;
  475. }