2
0

CCreatureSet.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  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. #include <sstream>
  11. const CStackInstance &CCreatureSet::operator[](TSlot slot) const
  12. {
  13. TSlots::const_iterator i = slots.find(slot);
  14. if (i != slots.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 = slots.find(slot);
  22. if (i != slots.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(vstd::contains(slots, 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=slots.begin(); i!=slots.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(slots.find(i) == slots.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 = slots.find(slot);
  72. if (i != slots.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(slots, preferable))
  81. {
  82. const CCreature *cr = slots.find(preferable)->second->type;
  83. for(TSlots::const_iterator j=slots.begin(); j!=slots.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=slots.begin(); i!=slots.end(); ++i)
  94. {
  95. for(TSlots::const_iterator j=slots.begin(); j!=slots.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=slots.begin(); i!=slots.end(); ++i)
  110. {
  111. if(!i->second->count)
  112. {
  113. slots.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(!vstd::contains(slots, 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(!vstd::contains(slots, 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=slots.begin(); i!=slots.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 !vstd::contains(slots, 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 = slots.begin(); i != slots.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 slots.size();
  186. }
  187. void CCreatureSet::setFormation(bool tight)
  188. {
  189. formation = tight;
  190. }
  191. void CCreatureSet::setStackCount(TSlot slot, TQuantity count)
  192. {
  193. assert(vstd::contains(slots, slot));
  194. assert(count > 0);
  195. slots[slot]->count = count;
  196. }
  197. void CCreatureSet::clear()
  198. {
  199. while(!slots.empty())
  200. {
  201. eraseStack(slots.begin()->first);
  202. }
  203. }
  204. const CStackInstance& CCreatureSet::getStack(TSlot slot) const
  205. {
  206. assert(vstd::contains(slots, slot));
  207. return *slots.find(slot)->second;
  208. }
  209. void CCreatureSet::eraseStack(TSlot slot)
  210. {
  211. assert(vstd::contains(slots, slot));
  212. delNull(slots[slot]);
  213. slots.erase(slot);
  214. }
  215. bool CCreatureSet::contains(const CStackInstance *stack) const
  216. {
  217. if(!stack)
  218. return false;
  219. for(TSlots::const_iterator i = slots.begin(); i != slots.end(); ++i)
  220. if(i->second == stack)
  221. return true;
  222. return false;
  223. }
  224. TSlot CCreatureSet::findStack(const CStackInstance *stack) const
  225. {
  226. if(!stack)
  227. return -1;
  228. for(TSlots::const_iterator i = slots.begin(); i != slots.end(); ++i)
  229. if(i->second == stack)
  230. return i->first;
  231. return -1;
  232. }
  233. CArmedInstance * CCreatureSet::castToArmyObj()
  234. {
  235. return dynamic_cast<CArmedInstance *>(this);
  236. }
  237. void CCreatureSet::putStack(TSlot slot, CStackInstance *stack)
  238. {
  239. assert(!vstd::contains(slots, slot));
  240. slots[slot] = stack;
  241. stack->setArmyObj(castToArmyObj());
  242. }
  243. void CCreatureSet::joinStack(TSlot slot, CStackInstance * stack)
  244. {
  245. const CCreature *c = getCreature(slot);
  246. assert(c == stack->type);
  247. assert(c);
  248. //TODO move stuff
  249. changeStackCount(slot, stack->count);
  250. delNull(stack);
  251. }
  252. void CCreatureSet::changeStackCount(TSlot slot, TQuantity toAdd)
  253. {
  254. setStackCount(slot, getStackCount(slot) + toAdd);
  255. }
  256. CCreatureSet::CCreatureSet()
  257. {
  258. formation = false;
  259. }
  260. CCreatureSet::CCreatureSet(const CCreatureSet&)
  261. {
  262. assert(0);
  263. }
  264. CCreatureSet::~CCreatureSet()
  265. {
  266. clear();
  267. }
  268. void CCreatureSet::setToArmy(CCreatureSet &src)
  269. {
  270. clear();
  271. while(src)
  272. {
  273. TSlots::iterator i = src.slots.begin();
  274. assert(i->second->type);
  275. assert(i->second->valid(false));
  276. assert(i->second->armyObj == NULL);
  277. putStack(i->first, i->second);
  278. src.slots.erase(i);
  279. }
  280. }
  281. CStackInstance * CCreatureSet::detachStack(TSlot slot)
  282. {
  283. assert(vstd::contains(slots, slot));
  284. CStackInstance *ret = slots[slot];
  285. if(CArmedInstance *armedObj = castToArmyObj())
  286. {
  287. ret->setArmyObj(NULL); //detaches from current armyobj
  288. }
  289. assert(!ret->armyObj); //we failed detaching?
  290. slots.erase(slot);
  291. return ret;
  292. }
  293. void CCreatureSet::setStackType(TSlot slot, const CCreature *type)
  294. {
  295. assert(vstd::contains(slots, slot));
  296. CStackInstance *s = slots[slot];
  297. s->setType(type->idNumber);
  298. }
  299. bool CCreatureSet::canBeMergedWith(const CCreatureSet &cs, bool allowMergingStacks) const
  300. {
  301. if(!allowMergingStacks)
  302. {
  303. int freeSlots = stacksCount() - ARMY_SIZE;
  304. std::set<const CCreature*> cresToAdd;
  305. for(TSlots::const_iterator i = cs.slots.begin(); i != cs.slots.end(); i++)
  306. {
  307. TSlot dest = getSlotFor(i->second->type);
  308. if(dest < 0 || hasStackAtSlot(dest))
  309. cresToAdd.insert(i->second->type);
  310. }
  311. return cresToAdd.size() <= freeSlots;
  312. }
  313. else
  314. {
  315. std::set<const CCreature*> cres;
  316. //get types of creatures that need their own slot
  317. for(TSlots::const_iterator i = cs.slots.begin(); i != cs.slots.end(); i++)
  318. cres.insert(i->second->type);
  319. for(TSlots::const_iterator i = slots.begin(); i != slots.end(); i++)
  320. cres.insert(i->second->type);
  321. return cres.size() <= ARMY_SIZE;
  322. }
  323. }
  324. bool CCreatureSet::hasStackAtSlot(TSlot slot) const
  325. {
  326. return vstd::contains(slots, slot);
  327. }
  328. CCreatureSet & CCreatureSet::operator=(const CCreatureSet&cs)
  329. {
  330. assert(0);
  331. return *this;
  332. }
  333. CStackInstance::CStackInstance()
  334. : armyObj(_armyObj)
  335. {
  336. init();
  337. }
  338. CStackInstance::CStackInstance(TCreature id, TQuantity Count)
  339. : armyObj(_armyObj)
  340. {
  341. init();
  342. setType(id);
  343. count = Count;
  344. }
  345. CStackInstance::CStackInstance(const CCreature *cre, TQuantity Count)
  346. : armyObj(_armyObj)
  347. {
  348. init();
  349. type = cre;
  350. count = Count;
  351. }
  352. void CStackInstance::init()
  353. {
  354. experience = 0;
  355. count = 0;
  356. type = NULL;
  357. idRand = -1;
  358. _armyObj = NULL;
  359. nodeType = STACK;
  360. }
  361. int CStackInstance::getQuantityID() const
  362. {
  363. return CCreature::getQuantityID(count);
  364. }
  365. void CStackInstance::setType(int creID)
  366. {
  367. setType(VLC->creh->creatures[creID]);
  368. }
  369. void CStackInstance::setType(const CCreature *c)
  370. {
  371. if(type)
  372. detachFrom(const_cast<CCreature*>(type));
  373. type = c;
  374. attachTo(const_cast<CCreature*>(type));
  375. }
  376. void CStackInstance::setArmyObj(const CArmedInstance *ArmyObj)
  377. {
  378. if(_armyObj)
  379. detachFrom(const_cast<CArmedInstance*>(_armyObj));
  380. _armyObj = ArmyObj;
  381. if(ArmyObj)
  382. {
  383. attachTo(const_cast<CArmedInstance*>(_armyObj));
  384. }
  385. }
  386. // void CStackInstance::getParents(TCNodes &out, const CBonusSystemNode *source /*= NULL*/) const
  387. // {
  388. // out.insert(type);
  389. //
  390. // if(source && source != this) //we should be root, if not - do not inherit anything
  391. // return;
  392. //
  393. // if(armyObj)
  394. // out.insert(armyObj);
  395. // else
  396. // out.insert(&IObjectInterface::cb->gameState()->globalEffects);
  397. // }
  398. std::string CStackInstance::getQuantityTXT(bool capitalized /*= true*/) const
  399. {
  400. return VLC->generaltexth->arraytxt[174 + getQuantityID()*3 + 2 - capitalized];
  401. }
  402. bool CStackInstance::valid(bool allowUnrandomized) const
  403. {
  404. bool isRand = (idRand != -1);
  405. if(!isRand)
  406. {
  407. return (type && type == VLC->creh->creatures[type->idNumber]);
  408. }
  409. else
  410. return allowUnrandomized;
  411. }
  412. CStackInstance::~CStackInstance()
  413. {
  414. }
  415. std::string CStackInstance::nodeName() const
  416. {
  417. std::ostringstream oss;
  418. oss << "Stack of " << count << " creatures of ";
  419. if(type)
  420. oss << type->namePl;
  421. else if(idRand)
  422. oss << "[no type, idRand=" << idRand << "]";
  423. else
  424. oss << "[UNDEFINED TYPE]";
  425. return oss.str();
  426. }
  427. CStackBasicDescriptor::CStackBasicDescriptor()
  428. {
  429. type = NULL;
  430. count = -1;
  431. }
  432. CStackBasicDescriptor::CStackBasicDescriptor(TCreature id, TQuantity Count)
  433. : type (VLC->creh->creatures[id]), count(Count)
  434. {
  435. }
  436. CStackBasicDescriptor::CStackBasicDescriptor(const CCreature *c, TQuantity Count)
  437. : type(c), count(Count)
  438. {
  439. }
  440. DLL_EXPORT std::ostream & operator<<(std::ostream & str, const CStackInstance & sth)
  441. {
  442. if(!sth.valid(true))
  443. str << "an invalid stack!";
  444. str << "stack with " << sth.count << " of ";
  445. if(sth.type)
  446. str << sth.type->namePl;
  447. else
  448. str << sth.idRand;
  449. return str;
  450. }