CCreatureSet.cpp 9.4 KB

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