CGameState.cpp 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856
  1. #define VCMI_DLL
  2. #include <algorithm>
  3. #include <queue>
  4. #include <fstream>
  5. #include "CGameState.h"
  6. #include <boost/random/linear_congruential.hpp>
  7. #include "hch/CDefObjInfoHandler.h"
  8. #include "hch/CArtHandler.h"
  9. #include "hch/CGeneralTextHandler.h"
  10. #include "hch/CTownHandler.h"
  11. #include "hch/CSpellHandler.h"
  12. #include "hch/CHeroHandler.h"
  13. #include "hch/CObjectHandler.h"
  14. #include "hch/CCreatureHandler.h"
  15. #include "lib/VCMI_Lib.h"
  16. #include "map.h"
  17. #include "StartInfo.h"
  18. #include "lib/NetPacks.h"
  19. #include <boost/foreach.hpp>
  20. #include <boost/thread.hpp>
  21. #include <boost/thread/shared_mutex.hpp>
  22. boost::rand48 ran;
  23. #ifdef min
  24. #undef min
  25. #endif
  26. #ifdef max
  27. #undef max
  28. #endif
  29. std::string DLL_EXPORT toString(MetaString &ms)
  30. {
  31. std::string ret;
  32. for(size_t i=0;i<ms.message.size();++i)
  33. {
  34. if(ms.message[i]>0)
  35. {
  36. ret += ms.strings[ms.message[i]-1];
  37. }
  38. else
  39. {
  40. std::vector<std::string> *vec;
  41. int type = ms.texts[-ms.message[i]-1].first,
  42. ser = ms.texts[-ms.message[i]-1].second;
  43. if(type == 5)
  44. {
  45. ret += VLC->arth->artifacts[ser].Name();
  46. continue;
  47. }
  48. else if(type == 7)
  49. {
  50. ret += VLC->creh->creatures[ser].namePl;
  51. continue;
  52. }
  53. else if(type == 9)
  54. {
  55. ret += VLC->objh->mines[ser].first;
  56. continue;
  57. }
  58. else if(type == 10)
  59. {
  60. ret += VLC->objh->mines[ser].second;
  61. continue;
  62. }
  63. else
  64. {
  65. switch(type)
  66. {
  67. case 1:
  68. vec = &VLC->generaltexth->allTexts;
  69. break;
  70. case 2:
  71. vec = &VLC->objh->xtrainfo;
  72. break;
  73. case 3:
  74. vec = &VLC->objh->names;
  75. break;
  76. case 4:
  77. vec = &VLC->objh->restypes;
  78. break;
  79. case 6:
  80. vec = &VLC->generaltexth->arraytxt;
  81. break;
  82. case 8:
  83. vec = &VLC->objh->creGens;
  84. break;
  85. case 11:
  86. vec = &VLC->objh->advobtxt;
  87. break;
  88. case 12:
  89. vec = &VLC->generaltexth->artifEvents;
  90. break;
  91. }
  92. ret += (*vec)[ser];
  93. }
  94. }
  95. }
  96. for(size_t i=0; i < ms.replacements.size(); ++i)
  97. {
  98. ret.replace(ret.find("%s"),2,ms.replacements[i]);
  99. }
  100. return ret;
  101. }
  102. CGObjectInstance * createObject(int id, int subid, int3 pos, int owner)
  103. {
  104. CGObjectInstance * nobj;
  105. switch(id)
  106. {
  107. case 34: //hero
  108. {
  109. CGHeroInstance * nobj = new CGHeroInstance();
  110. nobj->pos = pos;
  111. nobj->tempOwner = owner;
  112. nobj->subID = subid;
  113. //nobj->initHero(ran);
  114. return nobj;
  115. }
  116. case 98: //town
  117. nobj = new CGTownInstance;
  118. break;
  119. default: //rest of objects
  120. nobj = new CGObjectInstance;
  121. nobj->defInfo = VLC->dobjinfo->gobjs[id][subid];
  122. break;
  123. }
  124. nobj->ID = id;
  125. nobj->subID = subid;
  126. if(!nobj->defInfo)
  127. tlog3 <<"No def declaration for " <<id <<" "<<subid<<std::endl;
  128. nobj->pos = pos;
  129. //nobj->state = NULL;//new CLuaObjectScript();
  130. nobj->tempOwner = owner;
  131. nobj->info = NULL;
  132. nobj->defInfo->id = id;
  133. nobj->defInfo->subid = subid;
  134. //assigning defhandler
  135. if(nobj->ID==34 || nobj->ID==98)
  136. return nobj;
  137. nobj->defInfo = VLC->dobjinfo->gobjs[id][subid];
  138. return nobj;
  139. }
  140. CStack * BattleInfo::getStack(int stackID)
  141. {
  142. for(int g=0; g<stacks.size(); ++g)
  143. {
  144. if(stacks[g]->ID == stackID)
  145. return stacks[g];
  146. }
  147. return NULL;
  148. }
  149. CStack * BattleInfo::getStackT(int tileID)
  150. {
  151. for(int g=0; g<stacks.size(); ++g)
  152. {
  153. if(stacks[g]->position == tileID
  154. || (stacks[g]->creature->isDoubleWide() && stacks[g]->attackerOwned && stacks[g]->position-1 == tileID)
  155. || (stacks[g]->creature->isDoubleWide() && !stacks[g]->attackerOwned && stacks[g]->position+1 == tileID))
  156. {
  157. if(stacks[g]->alive())
  158. {
  159. return stacks[g];
  160. }
  161. }
  162. }
  163. return NULL;
  164. }
  165. void BattleInfo::getAccessibilityMap(bool *accessibility, int stackToOmmit)
  166. {
  167. memset(accessibility,1,BFIELD_SIZE); //initialize array with trues
  168. for(int g=0; g<stacks.size(); ++g)
  169. {
  170. if(!stacks[g]->alive() || stacks[g]->ID==stackToOmmit) //we don't want to lock position of this stack
  171. continue;
  172. accessibility[stacks[g]->position] = false;
  173. if(stacks[g]->creature->isDoubleWide()) //if it's a double hex creature
  174. {
  175. if(stacks[g]->attackerOwned)
  176. accessibility[stacks[g]->position-1] = false;
  177. else
  178. accessibility[stacks[g]->position+1] = false;
  179. }
  180. }
  181. //TODO: obstacles
  182. }
  183. void BattleInfo::getAccessibilityMapForTwoHex(bool *accessibility, bool atackerSide, int stackToOmmit) //send pointer to at least 187 allocated bytes
  184. {
  185. bool mac[BFIELD_SIZE];
  186. getAccessibilityMap(mac,stackToOmmit);
  187. memcpy(accessibility,mac,BFIELD_SIZE);
  188. for(int b=0; b<BFIELD_SIZE; ++b)
  189. {
  190. if( mac[b] && !(atackerSide ? mac[b-1] : mac[b+1]))
  191. {
  192. accessibility[b] = false;
  193. }
  194. }
  195. //removing accessibility for side hexes
  196. for(int v=0; v<BFIELD_SIZE; ++v)
  197. if(atackerSide ? (v%17)==1 : (v%17)==15)
  198. accessibility[v] = false;
  199. }
  200. void BattleInfo::makeBFS(int start, bool*accessibility, int *predecessor, int *dists) //both pointers must point to the at least 187-elements int arrays
  201. {
  202. //inits
  203. for(int b=0; b<BFIELD_SIZE; ++b)
  204. predecessor[b] = -1;
  205. for(int g=0; g<BFIELD_SIZE; ++g)
  206. dists[g] = 100000000;
  207. std::queue<int> hexq; //bfs queue
  208. hexq.push(start);
  209. dists[hexq.front()] = 0;
  210. int curNext = -1; //for bfs loop only (helper var)
  211. while(!hexq.empty()) //bfs loop
  212. {
  213. int curHex = hexq.front();
  214. std::vector<int> neighbours = neighbouringTiles(curHex);
  215. hexq.pop();
  216. for(int nr=0; nr<neighbours.size(); nr++)
  217. {
  218. curNext = neighbours[nr];
  219. if(!accessibility[curNext] || (dists[curHex]+1)>=dists[curNext])
  220. continue;
  221. hexq.push(curNext);
  222. dists[curNext] = dists[curHex] + 1;
  223. predecessor[curNext] = curHex;
  224. }
  225. }
  226. };
  227. std::vector<int> BattleInfo::getAccessibility(int stackID)
  228. {
  229. std::vector<int> ret;
  230. bool ac[BFIELD_SIZE];
  231. CStack *s = getStack(stackID);
  232. if(s->creature->isDoubleWide())
  233. getAccessibilityMapForTwoHex(ac,s->attackerOwned,stackID);
  234. else
  235. getAccessibilityMap(ac,stackID);
  236. int pr[BFIELD_SIZE], dist[BFIELD_SIZE];
  237. makeBFS(s->position,ac,pr,dist);
  238. for(int i=0;i<BFIELD_SIZE;i++)
  239. if(dist[i] <= s->speed())
  240. ret.push_back(i);
  241. return ret;
  242. }
  243. bool BattleInfo::isStackBlocked(int ID)
  244. {
  245. CStack *our = getStack(ID);
  246. for(int i=0; i<stacks.size();i++)
  247. {
  248. if( !stacks[i]->alive()
  249. || stacks[i]->owner==our->owner
  250. )
  251. continue; //we omit dead and allied stacks
  252. if(stacks[i]->creature->isDoubleWide())
  253. {
  254. if( mutualPosition(stacks[i]->position, our->position) >= 0
  255. || mutualPosition(stacks[i]->position + (stacks[i]->attackerOwned ? -1 : 1), our->position) >= 0)
  256. return true;
  257. }
  258. else
  259. {
  260. if( mutualPosition(stacks[i]->position, our->position) >= 0 )
  261. return true;
  262. }
  263. }
  264. return false;
  265. }
  266. signed char BattleInfo::mutualPosition(int hex1, int hex2)
  267. {
  268. if(hex2 == hex1 - ( (hex1/17)%2 ? 18 : 17 )) //top left
  269. return 0;
  270. if(hex2 == hex1 - ( (hex1/17)%2 ? 17 : 16 )) //top right
  271. return 1;
  272. if(hex2 == hex1 - 1 && hex1%17 != 0) //left
  273. return 5;
  274. if(hex2 == hex1 + 1 && hex1%17 != 16) //right
  275. return 2;
  276. if(hex2 == hex1 + ( (hex1/17)%2 ? 16 : 17 )) //bottom left
  277. return 4;
  278. if(hex2 == hex1 + ( (hex1/17)%2 ? 17 : 18 )) //bottom right
  279. return 3;
  280. return -1;
  281. }
  282. std::vector<int> BattleInfo::neighbouringTiles(int hex)
  283. {
  284. #define CHECK_AND_PUSH(tile) {int hlp = (tile); if(hlp>=0 && hlp<BFIELD_SIZE && (hlp%BFIELD_WIDTH!=16) && hlp%BFIELD_WIDTH) ret.push_back(hlp);}
  285. std::vector<int> ret;
  286. CHECK_AND_PUSH(hex - ( (hex/17)%2 ? 18 : 17 ));
  287. CHECK_AND_PUSH(hex - ( (hex/17)%2 ? 17 : 16 ));
  288. CHECK_AND_PUSH(hex - 1);
  289. CHECK_AND_PUSH(hex + 1);
  290. CHECK_AND_PUSH(hex + ( (hex/17)%2 ? 16 : 17 ));
  291. CHECK_AND_PUSH(hex + ( (hex/17)%2 ? 17 : 18 ));
  292. #undef CHECK_AND_PUSH
  293. return ret;
  294. }
  295. std::vector<int> BattleInfo::getPath(int start, int dest, bool*accessibility)
  296. {
  297. int predecessor[BFIELD_SIZE]; //for getting the Path
  298. int dist[BFIELD_SIZE]; //calculated distances
  299. makeBFS(start,accessibility,predecessor,dist);
  300. //making the Path
  301. std::vector<int> path;
  302. int curElem = dest;
  303. while(curElem != start)
  304. {
  305. path.push_back(curElem);
  306. curElem = predecessor[curElem];
  307. }
  308. return path;
  309. }
  310. CStack::CStack(CCreature * C, int A, int O, int I, bool AO, int S)
  311. :creature(C),amount(A), baseAmount(A), owner(O), position(-1), ID(I), attackerOwned(AO), firstHPleft(C->hitPoints),
  312. shots(C->shots), slot(S), counterAttacks(1), effects(), state()
  313. {
  314. abilities = C->abilities;
  315. state.insert(ALIVE);
  316. }
  317. ui32 CStack::speed() const
  318. {
  319. int premy=0;
  320. const StackEffect *effect = 0;
  321. //haste effect check
  322. effect = getEffect(53);
  323. if(effect)
  324. premy += VLC->spellh->spells[effect->id].powers[effect->level];
  325. //slow effect check
  326. effect = getEffect(54);
  327. if(effect)
  328. premy -= VLC->spellh->spells[effect->id].powers[effect->level];
  329. //prayer effect check
  330. effect = getEffect(48);
  331. if(effect)
  332. premy += VLC->spellh->spells[effect->id].powers[effect->level];
  333. //bind effect check
  334. effect = getEffect(72);
  335. if(effect)
  336. premy = -creature->speed;
  337. return creature->speed + premy;
  338. }
  339. const CStack::StackEffect * CStack::getEffect(ui16 id) const
  340. {
  341. for (int i=0; i< effects.size(); i++)
  342. if(effects[i].id == id)
  343. return &effects[i];
  344. return NULL;
  345. }
  346. CGHeroInstance* CGameState::HeroesPool::pickHeroFor(bool native, int player, const CTown *town, int notThatOne)
  347. {
  348. if(player<0 || player>=PLAYER_LIMIT)
  349. {
  350. tlog1 << "Cannot pick hero for " << town->Name() << ". Wrong owner!\n";
  351. return NULL;
  352. }
  353. std::vector<CGHeroInstance *> pool;
  354. int sum=0, r;
  355. if(native)
  356. {
  357. for(std::map<ui32,CGHeroInstance *>::iterator i=heroesPool.begin(); i!=heroesPool.end(); i++)
  358. {
  359. if(pavailable[i->first] & 1<<player
  360. && i->second->type->heroType/2 == town->typeID
  361. && i->second->subID != notThatOne
  362. )
  363. {
  364. pool.push_back(i->second);
  365. }
  366. }
  367. if(!pool.size())
  368. return pickHeroFor(false,player,town,notThatOne);
  369. else
  370. return pool[rand()%pool.size()];
  371. }
  372. else
  373. {
  374. for(std::map<ui32,CGHeroInstance *>::iterator i=heroesPool.begin(); i!=heroesPool.end(); i++)
  375. {
  376. if(pavailable[i->first] & 1<<player
  377. && i->second->subID != notThatOne
  378. )
  379. {
  380. pool.push_back(i->second);
  381. sum += i->second->type->heroClass->selectionProbability[town->typeID];
  382. }
  383. }
  384. if(!pool.size())
  385. {
  386. tlog1 << "There are no heroes available for player " << player<<"!\n";
  387. return NULL;
  388. }
  389. r = rand()%sum;
  390. for(int i=0; i<pool.size(); i++)
  391. {
  392. r -= pool[i]->type->heroClass->selectionProbability[town->typeID];
  393. if(r<0)
  394. return pool[i];
  395. }
  396. return pool[pool.size()-1];
  397. }
  398. }
  399. void CGameState::applyNL(IPack * pack)
  400. {
  401. switch(pack->getType())
  402. {
  403. case 101://NewTurn
  404. {
  405. NewTurn * n = static_cast<NewTurn*>(pack);
  406. day = n->day;
  407. BOOST_FOREACH(NewTurn::Hero h, n->heroes) //give mana/movement point
  408. {
  409. static_cast<CGHeroInstance*>(map->objects[h.id])->movement = h.move;
  410. static_cast<CGHeroInstance*>(map->objects[h.id])->mana = h.mana;
  411. }
  412. BOOST_FOREACH(SetResources h, n->res) //give resources
  413. applyNL(&h);
  414. BOOST_FOREACH(SetAvailableCreatures h, n->cres) //set available creatures in towns
  415. applyNL(&h);
  416. if(n->resetBuilded) //reset amount of structures set in this turn in towns
  417. BOOST_FOREACH(CGTownInstance* t, map->towns)
  418. t->builded = 0;
  419. break;
  420. }
  421. case 102: //set resource amount
  422. {
  423. SetResource *sr = static_cast<SetResource*>(pack);
  424. players[sr->player].resources[sr->resid] = sr->val;
  425. break;
  426. }
  427. case 104:
  428. {
  429. SetResources *sr = static_cast<SetResources*>(pack);
  430. for(int i=0;i<sr->res.size();i++)
  431. players[sr->player].resources[i] = sr->res[i];
  432. break;
  433. }
  434. case 105:
  435. {
  436. SetPrimSkill *sr = static_cast<SetPrimSkill*>(pack);
  437. CGHeroInstance *hero = getHero(sr->id);
  438. if(sr->which <4)
  439. {
  440. if(sr->abs)
  441. hero->primSkills[sr->which] = sr->val;
  442. else
  443. hero->primSkills[sr->which] += sr->val;
  444. }
  445. else if(sr->which == 4) //XP
  446. {
  447. if(sr->abs)
  448. hero->exp = sr->val;
  449. else
  450. hero->exp += sr->val;
  451. }
  452. break;
  453. }
  454. case 106:
  455. {
  456. SetSecSkill *sr = static_cast<SetSecSkill*>(pack);
  457. CGHeroInstance *hero = getHero(sr->id);
  458. if(hero->getSecSkillLevel(sr->which) == 0)
  459. {
  460. hero->secSkills.push_back(std::pair<int,int>(sr->which, sr->val));
  461. }
  462. else
  463. {
  464. for(unsigned i=0;i<hero->secSkills.size();i++)
  465. {
  466. if(hero->secSkills[i].first == sr->which)
  467. {
  468. if(sr->abs)
  469. hero->secSkills[i].second = sr->val;
  470. else
  471. hero->secSkills[i].second += sr->val;
  472. }
  473. }
  474. }
  475. break;
  476. }
  477. case 108:
  478. {
  479. HeroVisitCastle *vc = static_cast<HeroVisitCastle*>(pack);
  480. CGHeroInstance *h = getHero(vc->hid);
  481. CGTownInstance *t = getTown(vc->tid);
  482. if(vc->start())
  483. {
  484. if(vc->garrison())
  485. {
  486. t->garrisonHero = h;
  487. h->visitedTown = t;
  488. h->inTownGarrison = true;
  489. }
  490. else
  491. {
  492. t->visitingHero = h;
  493. h->visitedTown = t;
  494. h->inTownGarrison = false;
  495. }
  496. }
  497. else
  498. {
  499. if(vc->garrison())
  500. {
  501. t->garrisonHero = NULL;
  502. h->visitedTown = NULL;
  503. h->inTownGarrison = false;
  504. }
  505. else
  506. {
  507. t->visitingHero = NULL;
  508. h->visitedTown = NULL;
  509. h->inTownGarrison = false;
  510. }
  511. }
  512. break;
  513. }
  514. case 109:
  515. {
  516. ChangeSpells *rh = static_cast<ChangeSpells*>(pack);
  517. CGHeroInstance *hero = getHero(rh->hid);
  518. if(rh->learn)
  519. BOOST_FOREACH(ui32 sid, rh->spells)
  520. hero->spells.insert(sid);
  521. else
  522. BOOST_FOREACH(ui32 sid, rh->spells)
  523. hero->spells.erase(sid);
  524. break;
  525. }
  526. case 110:
  527. {
  528. SetMana *rh = static_cast<SetMana*>(pack);
  529. CGHeroInstance *hero = getHero(rh->hid);
  530. hero->mana = rh->val;
  531. break;
  532. }
  533. case 111:
  534. {
  535. SetMovePoints *rh = static_cast<SetMovePoints*>(pack);
  536. CGHeroInstance *hero = getHero(rh->hid);
  537. hero->movement = rh->val;
  538. break;
  539. }
  540. case 112:
  541. {
  542. FoWChange *rh = static_cast<FoWChange*>(pack);
  543. BOOST_FOREACH(int3 t, rh->tiles)
  544. players[rh->player].fogOfWarMap[t.x][t.y][t.z] = rh->mode;
  545. break;
  546. }
  547. case 113:
  548. {
  549. SetAvailableHeroes *rh = static_cast<SetAvailableHeroes*>(pack);
  550. players[rh->player].availableHeroes.clear();
  551. players[rh->player].availableHeroes.push_back(hpool.heroesPool[rh->hid1]);
  552. players[rh->player].availableHeroes.push_back(hpool.heroesPool[rh->hid2]);
  553. if(rh->flags & 1)
  554. {
  555. hpool.heroesPool[rh->hid1]->army.slots.clear();
  556. hpool.heroesPool[rh->hid1]->army.slots[0] = std::pair<ui32,si32>(VLC->creh->nameToID[hpool.heroesPool[rh->hid1]->type->refTypeStack[0]],1);
  557. }
  558. if(rh->flags & 2)
  559. {
  560. hpool.heroesPool[rh->hid2]->army.slots.clear();
  561. hpool.heroesPool[rh->hid2]->army.slots[0] = std::pair<ui32,si32>(VLC->creh->nameToID[hpool.heroesPool[rh->hid2]->type->refTypeStack[0]],1);
  562. }
  563. break;
  564. }
  565. case 500:
  566. {
  567. RemoveObject *rh = static_cast<RemoveObject*>(pack);
  568. CGObjectInstance *obj = map->objects[rh->id];
  569. if(obj->ID==34)
  570. {
  571. CGHeroInstance *h = static_cast<CGHeroInstance*>(obj);
  572. std::vector<CGHeroInstance*>::iterator nitr = std::find(map->heroes.begin(), map->heroes.end(),h);
  573. map->heroes.erase(nitr);
  574. int player = h->tempOwner;
  575. nitr = std::find(players[player].heroes.begin(), players[player].heroes.end(), h);
  576. players[player].heroes.erase(nitr);
  577. if(h->visitedTown)
  578. {
  579. if(h->inTownGarrison)
  580. h->visitedTown->garrisonHero = NULL;
  581. else
  582. h->visitedTown->visitingHero = NULL;
  583. h->visitedTown = NULL;
  584. }
  585. }
  586. map->objects[rh->id] = NULL;
  587. //unblock tiles
  588. if(obj->defInfo)
  589. {
  590. map->removeBlockVisTiles(obj);
  591. }
  592. break;
  593. }
  594. case 501://hero try-move
  595. {
  596. TryMoveHero * n = static_cast<TryMoveHero*>(pack);
  597. CGHeroInstance *h = static_cast<CGHeroInstance*>(map->objects[n->id]);
  598. h->movement = n->movePoints;
  599. if(n->start!=n->end && n->result)
  600. {
  601. map->removeBlockVisTiles(h);
  602. h->pos = n->end;
  603. map->addBlockVisTiles(h);
  604. }
  605. BOOST_FOREACH(int3 t, n->fowRevealed)
  606. players[h->getOwner()].fogOfWarMap[t.x][t.y][t.z] = 1;
  607. break;
  608. }
  609. case 502:
  610. {
  611. SetGarrisons * n = static_cast<SetGarrisons*>(pack);
  612. for(std::map<ui32,CCreatureSet>::iterator i = n->garrs.begin(); i!=n->garrs.end(); i++)
  613. {
  614. CArmedInstance *ai = static_cast<CArmedInstance*>(map->objects[i->first]);
  615. ai->army = i->second;
  616. if(ai->ID==98 && (static_cast<CGTownInstance*>(ai))->garrisonHero) //if there is a hero in garrison then we must update also his army
  617. const_cast<CGHeroInstance*>((static_cast<CGTownInstance*>(ai))->garrisonHero)->army = i->second;
  618. else if(ai->ID==34)
  619. {
  620. CGHeroInstance *h = static_cast<CGHeroInstance*>(ai);
  621. if(h->visitedTown && h->inTownGarrison)
  622. h->visitedTown->army = i->second;
  623. }
  624. }
  625. break;
  626. }
  627. case 503:
  628. {
  629. //SetStrInfo *ssi = static_cast<SetStrInfo*>(pack);
  630. //static_cast<CGTownInstance*>(map->objects[ssi->tid])->strInfo.creatures = ssi->cres;
  631. break;
  632. }
  633. case 504:
  634. {
  635. NewStructures *ns = static_cast<NewStructures*>(pack);
  636. CGTownInstance*t = static_cast<CGTownInstance*>(map->objects[ns->tid]);
  637. BOOST_FOREACH(si32 bid,ns->bid)
  638. t->builtBuildings.insert(bid);
  639. t->builded = ns->builded;
  640. break;
  641. }
  642. case 506:
  643. {
  644. SetAvailableCreatures *sac = static_cast<SetAvailableCreatures*>(pack);
  645. static_cast<CGTownInstance*>(map->objects[sac->tid])->strInfo.creatures = sac->creatures;
  646. break;
  647. }
  648. case 508:
  649. {
  650. SetHeroesInTown *sac = static_cast<SetHeroesInTown*>(pack);
  651. CGTownInstance *t = getTown(sac->tid);
  652. CGHeroInstance *v = getHero(sac->visiting), *g = getHero(sac->garrison);
  653. t->visitingHero = v;
  654. t->garrisonHero = g;
  655. if(v)
  656. {
  657. v->visitedTown = t;
  658. v->inTownGarrison = false;
  659. map->addBlockVisTiles(v);
  660. }
  661. if(g)
  662. {
  663. g->visitedTown = t;
  664. g->inTownGarrison = true;
  665. map->removeBlockVisTiles(g);
  666. }
  667. break;
  668. }
  669. case 509:
  670. {
  671. SetHeroArtifacts *sha = static_cast<SetHeroArtifacts*>(pack);
  672. CGHeroInstance *h = getHero(sha->hid);
  673. h->artifacts = sha->artifacts;
  674. h->artifWorn = sha->artifWorn;
  675. break;
  676. }
  677. case 514:
  678. {
  679. SetSelection *ss = static_cast<SetSelection*>(pack);
  680. players[ss->player].currentSelection = ss->id;
  681. break;
  682. }
  683. case 515:
  684. {
  685. HeroRecruited *sha = static_cast<HeroRecruited*>(pack);
  686. CGHeroInstance *h = hpool.heroesPool[sha->hid];
  687. CGTownInstance *t = getTown(sha->tid);
  688. h->setOwner(sha->player);
  689. h->pos = sha->tile;
  690. h->movement = h->maxMovePoints(true);
  691. hpool.heroesPool.erase(sha->hid);
  692. if(h->id < 0)
  693. {
  694. h->id = map->objects.size();
  695. map->objects.push_back(h);
  696. }
  697. else
  698. map->objects[h->id] = h;
  699. map->heroes.push_back(h);
  700. players[h->tempOwner].heroes.push_back(h);
  701. map->addBlockVisTiles(h);
  702. t->visitingHero = h;
  703. h->visitedTown = t;
  704. h->inTownGarrison = false;
  705. break;
  706. }
  707. case 1001://set object property
  708. {
  709. SetObjectProperty *p = static_cast<SetObjectProperty*>(pack);
  710. setObjProperty(p);
  711. break;
  712. }
  713. case 1002:
  714. {
  715. SetHoverName *shn = static_cast<SetHoverName*>(pack);
  716. map->objects[shn->id]->hoverName = toString(shn->name);
  717. break;
  718. }
  719. case 2000:
  720. {
  721. HeroLevelUp * bs = static_cast<HeroLevelUp*>(pack);
  722. getHero(bs->heroid)->level = bs->level;
  723. break;
  724. }
  725. case 3000:
  726. {
  727. BattleStart * bs = static_cast<BattleStart*>(pack);
  728. curB = bs->info;
  729. break;
  730. }
  731. case 3001:
  732. {
  733. BattleNextRound *ns = static_cast<BattleNextRound*>(pack);
  734. curB->round = ns->round;
  735. for(int i=0; i<curB->stacks.size();i++)
  736. {
  737. curB->stacks[i]->state -= DEFENDING;
  738. curB->stacks[i]->state -= WAITING;
  739. curB->stacks[i]->state -= MOVED;
  740. curB->stacks[i]->state -= HAD_MORALE;
  741. curB->stacks[i]->counterAttacks = 1;
  742. }
  743. break;
  744. }
  745. case 3002:
  746. {
  747. BattleSetActiveStack *ns = static_cast<BattleSetActiveStack*>(pack);
  748. curB->activeStack = ns->stack;
  749. break;
  750. }
  751. case 3003:
  752. {
  753. BattleResult *br = static_cast<BattleResult*>(pack);
  754. //TODO: give exp, artifacts to winner, decrease armies (casualties)
  755. for(unsigned i=0;i<curB->stacks.size();i++)
  756. delete curB->stacks[i];
  757. delete curB;
  758. curB = NULL;
  759. break;
  760. }
  761. case 3004:
  762. {
  763. BattleStackMoved *br = static_cast<BattleStackMoved*>(pack);
  764. curB->getStack(br->stack)->position = br->tile;
  765. break;
  766. }
  767. case 3005:
  768. {
  769. BattleStackAttacked *br = static_cast<BattleStackAttacked*>(pack);
  770. CStack * at = curB->getStack(br->stackAttacked);
  771. at->amount = br->newAmount;
  772. at->firstHPleft = br->newHP;
  773. if(br->killed())
  774. at->state -= ALIVE;
  775. break;
  776. }
  777. case 3006:
  778. {
  779. BattleAttack *br = static_cast<BattleAttack*>(pack);
  780. CStack *attacker = curB->getStack(br->stackAttacking);
  781. if(br->counter())
  782. attacker->counterAttacks--;
  783. if(br->shot())
  784. attacker->shots--;
  785. applyNL(&br->bsa);
  786. break;
  787. }
  788. case 3007:
  789. {
  790. StartAction *br = static_cast<StartAction*>(pack);
  791. CStack *st = curB->getStack(br->ba.stackNumber);
  792. switch(br->ba.actionType)
  793. {
  794. case 3:
  795. st->state.insert(DEFENDING);
  796. break;
  797. case 8:
  798. st->state.insert(WAITING);
  799. break;
  800. case 2: case 6: case 7: case 9: case 10:
  801. st->state.insert(MOVED);
  802. break;
  803. }
  804. break;
  805. }
  806. case 3009:
  807. {
  808. SpellCasted *sc = static_cast<SpellCasted*>(pack);
  809. CGHeroInstance *h = (sc->side) ? getHero(curB->hero2) : getHero(curB->hero1);
  810. if(h)
  811. h->mana -= VLC->spellh->spells[sc->id].costs[sc->skill];
  812. //TODO: counter
  813. break;
  814. }
  815. case 3010:
  816. {
  817. SetStackEffect *sc = static_cast<SetStackEffect*>(pack);
  818. CStack *stack = curB->getStack(sc->stack);
  819. stack->effects.push_back(sc->effect);
  820. break;
  821. }
  822. }
  823. }
  824. void CGameState::apply(IPack * pack)
  825. {
  826. while(!mx->try_lock())
  827. boost::this_thread::sleep(boost::posix_time::milliseconds(50)); //give other threads time to finish
  828. applyNL(pack);
  829. mx->unlock();
  830. }
  831. int CGameState::pickHero(int owner)
  832. {
  833. int h=-1;
  834. if(!map->getHero(h = scenarioOps->getIthPlayersSettings(owner).hero,0) && h>=0) //we haven't used selected hero
  835. return h;
  836. int f = scenarioOps->getIthPlayersSettings(owner).castle;
  837. int i=0;
  838. do //try to find free hero of our faction
  839. {
  840. i++;
  841. h = scenarioOps->getIthPlayersSettings(owner).castle*HEROES_PER_TYPE*2+(ran()%(HEROES_PER_TYPE*2));//->scenarioOps->playerInfos[pru].hero = VLC->
  842. } while( map->getHero(h) && i<175);
  843. if(i>174) //probably no free heroes - there's no point in further search, we'll take first free
  844. {
  845. tlog3 << "Warning: cannot find free hero - trying to get first available..."<<std::endl;
  846. for(int j=0; j<HEROES_PER_TYPE * 2 * F_NUMBER; j++)
  847. if(!map->getHero(j))
  848. h=j;
  849. }
  850. return h;
  851. }
  852. CGHeroInstance *CGameState::getHero(int objid)
  853. {
  854. if(objid<0 || objid>=map->objects.size())
  855. return NULL;
  856. return static_cast<CGHeroInstance *>(map->objects[objid]);
  857. }
  858. CGTownInstance *CGameState::getTown(int objid)
  859. {
  860. if(objid<0 || objid>=map->objects.size())
  861. return NULL;
  862. return static_cast<CGTownInstance *>(map->objects[objid]);
  863. }
  864. std::pair<int,int> CGameState::pickObject(CGObjectInstance *obj)
  865. {
  866. switch(obj->ID)
  867. {
  868. case 65: //random artifact
  869. return std::pair<int,int>(5,(ran()%136)+7); //tylko sensowny zakres - na poczatku sa katapulty itp, na koncu specjalne i blanki
  870. case 66: //random treasure artifact
  871. return std::pair<int,int>(5,VLC->arth->treasures[ran()%VLC->arth->treasures.size()]->id);
  872. case 67: //random minor artifact
  873. return std::pair<int,int>(5,VLC->arth->minors[ran()%VLC->arth->minors.size()]->id);
  874. case 68: //random major artifact
  875. return std::pair<int,int>(5,VLC->arth->majors[ran()%VLC->arth->majors.size()]->id);
  876. case 69: //random relic artifact
  877. return std::pair<int,int>(5,VLC->arth->relics[ran()%VLC->arth->relics.size()]->id);
  878. case 70: //random hero
  879. {
  880. return std::pair<int,int>(34,pickHero(obj->tempOwner));
  881. }
  882. case 71: //random monster
  883. {
  884. int r;
  885. do
  886. {
  887. r = ran()%197;
  888. } while (vstd::contains(VLC->creh->notUsedMonsters,r));
  889. return std::pair<int,int>(54,r);
  890. }
  891. case 72: //random monster lvl1
  892. return std::pair<int,int>(54,VLC->creh->levelCreatures[1][ran()%VLC->creh->levelCreatures[1].size()]->idNumber);
  893. case 73: //random monster lvl2
  894. return std::pair<int,int>(54,VLC->creh->levelCreatures[2][ran()%VLC->creh->levelCreatures[2].size()]->idNumber);
  895. case 74: //random monster lvl3
  896. return std::pair<int,int>(54,VLC->creh->levelCreatures[3][ran()%VLC->creh->levelCreatures[3].size()]->idNumber);
  897. case 75: //random monster lvl4
  898. return std::pair<int,int>(54,VLC->creh->levelCreatures[4][ran()%VLC->creh->levelCreatures[4].size()]->idNumber);
  899. case 76: //random resource
  900. return std::pair<int,int>(79,ran()%7); //now it's OH3 style, use %8 for mithril
  901. case 77: //random town
  902. {
  903. int align = ((CGTownInstance*)obj)->alignment,
  904. f;
  905. if(align>PLAYER_LIMIT-1)//same as owner / random
  906. {
  907. if(obj->tempOwner > PLAYER_LIMIT-1)
  908. f = -1; //random
  909. else
  910. f = scenarioOps->getIthPlayersSettings(obj->tempOwner).castle;
  911. }
  912. else
  913. {
  914. f = scenarioOps->getIthPlayersSettings(align).castle;
  915. }
  916. if(f<0) f = ran()%VLC->townh->towns.size();
  917. return std::pair<int,int>(98,f);
  918. }
  919. case 162: //random monster lvl5
  920. return std::pair<int,int>(54,VLC->creh->levelCreatures[5][ran()%VLC->creh->levelCreatures[5].size()]->idNumber);
  921. case 163: //random monster lvl6
  922. return std::pair<int,int>(54,VLC->creh->levelCreatures[6][ran()%VLC->creh->levelCreatures[6].size()]->idNumber);
  923. case 164: //random monster lvl7
  924. return std::pair<int,int>(54,VLC->creh->levelCreatures[7][ran()%VLC->creh->levelCreatures[7].size()]->idNumber);
  925. case 216: //random dwelling
  926. {
  927. int faction = ran()%F_NUMBER;
  928. CCreGen2ObjInfo* info =(CCreGen2ObjInfo*)obj->info;
  929. if (info->asCastle)
  930. {
  931. for(int i=0;i<map->objects.size();i++)
  932. {
  933. if(map->objects[i]->ID==77 && dynamic_cast<CGTownInstance*>(map->objects[i])->identifier == info->identifier)
  934. {
  935. randomizeObject(map->objects[i]); //we have to randomize the castle first
  936. faction = map->objects[i]->subID;
  937. break;
  938. }
  939. else if(map->objects[i]->ID==98 && dynamic_cast<CGTownInstance*>(map->objects[i])->identifier == info->identifier)
  940. {
  941. faction = map->objects[i]->subID;
  942. break;
  943. }
  944. }
  945. }
  946. else
  947. {
  948. while((!(info->castles[0]&(1<<faction))))
  949. {
  950. if((faction>7) && (info->castles[1]&(1<<(faction-8))))
  951. break;
  952. faction = ran()%F_NUMBER;
  953. }
  954. }
  955. int level = ((info->maxLevel-info->minLevel) ? (ran()%(info->maxLevel-info->minLevel)+info->minLevel) : (info->minLevel));
  956. int cid = VLC->townh->towns[faction].basicCreatures[level];
  957. for(int i=0;i<VLC->objh->cregens.size();i++)
  958. if(VLC->objh->cregens[i]==cid)
  959. return std::pair<int,int>(17,i);
  960. tlog3 << "Cannot find a dwelling for creature "<<cid <<std::endl;
  961. return std::pair<int,int>(17,0);
  962. }
  963. case 217:
  964. {
  965. int faction = ran()%F_NUMBER;
  966. CCreGenObjInfo* info =(CCreGenObjInfo*)obj->info;
  967. if (info->asCastle)
  968. {
  969. for(int i=0;i<map->objects.size();i++)
  970. {
  971. if(map->objects[i]->ID==77 && dynamic_cast<CGTownInstance*>(map->objects[i])->identifier == info->identifier)
  972. {
  973. randomizeObject(map->objects[i]); //we have to randomize the castle first
  974. faction = map->objects[i]->subID;
  975. break;
  976. }
  977. else if(map->objects[i]->ID==98 && dynamic_cast<CGTownInstance*>(map->objects[i])->identifier == info->identifier)
  978. {
  979. faction = map->objects[i]->subID;
  980. break;
  981. }
  982. }
  983. }
  984. else
  985. {
  986. while((!(info->castles[0]&(1<<faction))))
  987. {
  988. if((faction>7) && (info->castles[1]&(1<<(faction-8))))
  989. break;
  990. faction = ran()%F_NUMBER;
  991. }
  992. }
  993. int cid = VLC->townh->towns[faction].basicCreatures[obj->subID];
  994. for(int i=0;i<VLC->objh->cregens.size();i++)
  995. if(VLC->objh->cregens[i]==cid)
  996. return std::pair<int,int>(17,i);
  997. tlog3 << "Cannot find a dwelling for creature "<<cid <<std::endl;
  998. return std::pair<int,int>(17,0);
  999. }
  1000. case 218:
  1001. {
  1002. CCreGen3ObjInfo* info =(CCreGen3ObjInfo*)obj->info;
  1003. int level = ((info->maxLevel-info->minLevel) ? (ran()%(info->maxLevel-info->minLevel)+info->minLevel) : (info->minLevel));
  1004. int cid = VLC->townh->towns[obj->subID].basicCreatures[level];
  1005. for(int i=0;i<VLC->objh->cregens.size();i++)
  1006. if(VLC->objh->cregens[i]==cid)
  1007. return std::pair<int,int>(17,i);
  1008. tlog3 << "Cannot find a dwelling for creature "<<cid <<std::endl;
  1009. return std::pair<int,int>(17,0);
  1010. }
  1011. }
  1012. return std::pair<int,int>(-1,-1);
  1013. }
  1014. void CGameState::randomizeObject(CGObjectInstance *cur)
  1015. {
  1016. std::pair<int,int> ran = pickObject(cur);
  1017. if(ran.first<0 || ran.second<0) //this is not a random object, or we couldn't find anything
  1018. {
  1019. if(cur->ID==98) //town - set def
  1020. {
  1021. CGTownInstance *t = dynamic_cast<CGTownInstance*>(cur);
  1022. if(t->hasCapitol())
  1023. t->defInfo = capitols[t->subID];
  1024. else if(t->hasFort())
  1025. t->defInfo = forts[t->subID];
  1026. else
  1027. t->defInfo = villages[t->subID];
  1028. }
  1029. return;
  1030. }
  1031. else if(ran.first==34)//special code for hero
  1032. {
  1033. CGHeroInstance *h = dynamic_cast<CGHeroInstance *>(cur);
  1034. if(!h) {tlog2<<"Wrong random hero at "<<cur->pos<<std::endl; return;}
  1035. cur->ID = ran.first;
  1036. h->portrait = cur->subID = ran.second;
  1037. h->type = VLC->heroh->heroes[ran.second];
  1038. map->heroes.push_back(h);
  1039. return; //TODO: maybe we should do something with definfo?
  1040. }
  1041. else if(ran.first==98)//special code for town
  1042. {
  1043. CGTownInstance *t = dynamic_cast<CGTownInstance*>(cur);
  1044. if(!t) {tlog2<<"Wrong random town at "<<cur->pos<<std::endl; return;}
  1045. cur->ID = ran.first;
  1046. cur->subID = ran.second;
  1047. t->town = &VLC->townh->towns[ran.second];
  1048. if(t->hasCapitol())
  1049. t->defInfo = capitols[t->subID];
  1050. else if(t->hasFort())
  1051. t->defInfo = forts[t->subID];
  1052. else
  1053. t->defInfo = villages[t->subID];
  1054. map->towns.push_back(t);
  1055. return;
  1056. }
  1057. //we have to replace normal random object
  1058. cur->ID = ran.first;
  1059. cur->subID = ran.second;
  1060. map->defs.insert(cur->defInfo = VLC->dobjinfo->gobjs[ran.first][ran.second]);
  1061. if(!cur->defInfo)
  1062. {
  1063. tlog1<<"*BIG* WARNING: Missing def declaration for "<<cur->ID<<" "<<cur->subID<<std::endl;
  1064. return;
  1065. }
  1066. }
  1067. int CGameState::getDate(int mode) const
  1068. {
  1069. int temp;
  1070. switch (mode)
  1071. {
  1072. case 0:
  1073. return day;
  1074. break;
  1075. case 1:
  1076. temp = (day)%7;
  1077. if (temp)
  1078. return temp;
  1079. else return 7;
  1080. break;
  1081. case 2:
  1082. temp = ((day-1)/7)+1;
  1083. if (!(temp%4))
  1084. return 4;
  1085. else
  1086. return (temp%4);
  1087. break;
  1088. case 3:
  1089. return ((day-1)/28)+1;
  1090. break;
  1091. }
  1092. return 0;
  1093. }
  1094. CGameState::CGameState()
  1095. {
  1096. mx = new boost::shared_mutex();
  1097. }
  1098. CGameState::~CGameState()
  1099. {
  1100. delete mx;
  1101. }
  1102. void CGameState::init(StartInfo * si, Mapa * map, int Seed)
  1103. {
  1104. day = 0;
  1105. seed = Seed;
  1106. ran.seed((boost::int32_t)seed);
  1107. scenarioOps = si;
  1108. this->map = map;
  1109. loadTownDInfos();
  1110. //picking random factions for players
  1111. for(int i=0;i<scenarioOps->playerInfos.size();i++)
  1112. {
  1113. if(scenarioOps->playerInfos[i].castle==-1)
  1114. {
  1115. int f;
  1116. do
  1117. {
  1118. f = ran()%F_NUMBER;
  1119. }while(!(map->players[scenarioOps->playerInfos[i].color].allowedFactions & 1<<f));
  1120. scenarioOps->playerInfos[i].castle = f;
  1121. }
  1122. }
  1123. //randomizing objects
  1124. for(int no=0; no<map->objects.size(); ++no)
  1125. {
  1126. randomizeObject(map->objects[no]);
  1127. if(map->objects[no]->ID==26)
  1128. {
  1129. map->objects[no]->defInfo->handler=NULL;
  1130. map->removeBlockVisTiles(map->objects[no]);
  1131. map->objects[no]->defInfo->blockMap[5] = 255;
  1132. map->addBlockVisTiles(map->objects[no]);
  1133. }
  1134. map->objects[no]->hoverName = VLC->objh->names[map->objects[no]->ID];
  1135. }
  1136. //std::cout<<"\tRandomizing objects: "<<th.getDif()<<std::endl;
  1137. /*********give starting hero****************************************/
  1138. for(int i=0;i<PLAYER_LIMIT;i++)
  1139. {
  1140. if((map->players[i].generateHeroAtMainTown && map->players[i].hasMainTown) || (map->players[i].hasMainTown && map->version==RoE))
  1141. {
  1142. int3 hpos = map->players[i].posOfMainTown;
  1143. hpos.x+=1;// hpos.y+=1;
  1144. int j;
  1145. for(j=0; j<scenarioOps->playerInfos.size(); j++)
  1146. if(scenarioOps->playerInfos[j].color == i)
  1147. break;
  1148. if(j == scenarioOps->playerInfos.size())
  1149. continue;
  1150. int h=pickHero(i);
  1151. CGHeroInstance * nnn = static_cast<CGHeroInstance*>(createObject(34,h,hpos,i));
  1152. nnn->id = map->objects.size();
  1153. hpos = map->players[i].posOfMainTown;hpos.x+=2;
  1154. for(int o=0;o<map->towns.size();o++) //find main town
  1155. {
  1156. if(map->towns[o]->pos == hpos)
  1157. {
  1158. map->towns[o]->visitingHero = nnn;
  1159. nnn->visitedTown = map->towns[o];
  1160. nnn->inTownGarrison = false;
  1161. break;
  1162. }
  1163. }
  1164. nnn->initHero();
  1165. map->heroes.push_back(nnn);
  1166. map->objects.push_back(nnn);
  1167. map->addBlockVisTiles(nnn);
  1168. }
  1169. }
  1170. /*********creating players entries in gs****************************************/
  1171. for (int i=0; i<scenarioOps->playerInfos.size();i++)
  1172. {
  1173. std::pair<int,PlayerState> ins(scenarioOps->playerInfos[i].color,PlayerState());
  1174. ins.second.color=ins.first;
  1175. ins.second.serial=i;
  1176. players.insert(ins);
  1177. }
  1178. /******************RESOURCES****************************************************/
  1179. //TODO: computer player should receive other amount of resource than computer (depending on difficulty)
  1180. std::vector<int> startres;
  1181. std::ifstream tis("config/startres.txt");
  1182. int k;
  1183. for (int j=0;j<scenarioOps->difficulty;j++)
  1184. {
  1185. tis >> k;
  1186. for (int z=0;z<RESOURCE_QUANTITY;z++)
  1187. tis>>k;
  1188. }
  1189. tis >> k;
  1190. for (int i=0;i<RESOURCE_QUANTITY;i++)
  1191. {
  1192. tis >> k;
  1193. startres.push_back(k);
  1194. }
  1195. tis.close();
  1196. tis.clear();
  1197. for (std::map<ui8,PlayerState>::iterator i = players.begin(); i!=players.end(); i++)
  1198. {
  1199. (*i).second.resources.resize(RESOURCE_QUANTITY);
  1200. for (int x=0;x<RESOURCE_QUANTITY;x++)
  1201. (*i).second.resources[x] = startres[x];
  1202. }
  1203. tis.open("config/resources.txt");
  1204. tis >> k;
  1205. int pom;
  1206. for(int i=0;i<k;i++)
  1207. {
  1208. tis >> pom;
  1209. resVals.push_back(pom);
  1210. }
  1211. /*************************HEROES************************************************/
  1212. std::set<int> hids;
  1213. for(int i=0; i<map->allowedHeroes.size(); i++) //add to hids all allowed heroes
  1214. if(map->allowedHeroes[i])
  1215. hids.insert(i);
  1216. for (int i=0; i<map->heroes.size();i++) //heroes instances
  1217. {
  1218. if (map->heroes[i]->getOwner()<0)
  1219. {
  1220. tlog2 << "Warning - hero with uninitialized owner!\n";
  1221. continue;
  1222. }
  1223. CGHeroInstance * vhi = (map->heroes[i]);
  1224. vhi->initHero();
  1225. players.find(vhi->getOwner())->second.heroes.push_back(vhi);
  1226. hids.erase(vhi->subID);
  1227. }
  1228. for(int i=0; i<map->predefinedHeroes.size(); i++)
  1229. {
  1230. if(!vstd::contains(hids,map->predefinedHeroes[i]->subID))
  1231. continue;
  1232. map->predefinedHeroes[i]->initHero();
  1233. hpool.heroesPool[map->predefinedHeroes[i]->subID] = map->predefinedHeroes[i];
  1234. hpool.pavailable[map->predefinedHeroes[i]->subID] = 0xff;
  1235. hids.erase(map->predefinedHeroes[i]->subID);
  1236. }
  1237. BOOST_FOREACH(int hid, hids) //all not used allowed heroes go into the pool
  1238. {
  1239. CGHeroInstance * vhi = new CGHeroInstance();
  1240. vhi->initHero(hid);
  1241. hpool.heroesPool[hid] = vhi;
  1242. hpool.pavailable[hid] = 0xff;
  1243. }
  1244. for(int i=0; i<map->disposedHeroes.size(); i++)
  1245. {
  1246. hpool.pavailable[map->disposedHeroes[i].ID] = map->disposedHeroes[i].players;
  1247. }
  1248. /*************************FOG**OF**WAR******************************************/
  1249. for(std::map<ui8, PlayerState>::iterator k=players.begin(); k!=players.end(); ++k)
  1250. {
  1251. k->second.fogOfWarMap.resize(map->width);
  1252. for(int g=0; g<map->width; ++g)
  1253. k->second.fogOfWarMap[g].resize(map->height);
  1254. for(int g=-0; g<map->width; ++g)
  1255. for(int h=0; h<map->height; ++h)
  1256. k->second.fogOfWarMap[g][h].resize(map->twoLevel+1, 0);
  1257. for(int g=0; g<map->width; ++g)
  1258. for(int h=0; h<map->height; ++h)
  1259. for(int v=0; v<map->twoLevel+1; ++v)
  1260. k->second.fogOfWarMap[g][h][v] = 0;
  1261. for(int xd=0; xd<map->width; ++xd) //revealing part of map around heroes
  1262. {
  1263. for(int yd=0; yd<map->height; ++yd)
  1264. {
  1265. for(int ch=0; ch<k->second.heroes.size(); ++ch)
  1266. {
  1267. int deltaX = (k->second.heroes[ch]->getPosition(false).x-xd)*(k->second.heroes[ch]->getPosition(false).x-xd);
  1268. int deltaY = (k->second.heroes[ch]->getPosition(false).y-yd)*(k->second.heroes[ch]->getPosition(false).y-yd);
  1269. if(deltaX+deltaY<k->second.heroes[ch]->getSightDistance()*k->second.heroes[ch]->getSightDistance())
  1270. k->second.fogOfWarMap[xd][yd][k->second.heroes[ch]->getPosition(false).z] = 1;
  1271. }
  1272. }
  1273. }
  1274. //starting bonus
  1275. if(si->playerInfos[k->second.serial].bonus==brandom)
  1276. si->playerInfos[k->second.serial].bonus = ran()%3;
  1277. switch(si->playerInfos[k->second.serial].bonus)
  1278. {
  1279. case bgold:
  1280. k->second.resources[6] += 500 + (ran()%6)*100;
  1281. break;
  1282. case bresource:
  1283. {
  1284. int res = VLC->townh->towns[si->playerInfos[k->second.serial].castle].primaryRes;
  1285. if(res == 127)
  1286. {
  1287. k->second.resources[0] += 5 + ran()%6;
  1288. k->second.resources[2] += 5 + ran()%6;
  1289. }
  1290. else
  1291. {
  1292. k->second.resources[res] += 3 + ran()%4;
  1293. }
  1294. break;
  1295. }
  1296. case bartifact:
  1297. {
  1298. if(!k->second.heroes.size())
  1299. {
  1300. tlog5 << "Cannot give starting artifact - no heroes!" << std::endl;
  1301. break;
  1302. }
  1303. CArtifact *toGive;
  1304. do
  1305. {
  1306. toGive = VLC->arth->treasures[ran() % VLC->arth->treasures.size()];
  1307. } while (!map->allowedArtifact[toGive->id]);
  1308. CGHeroInstance *hero = k->second.heroes[0];
  1309. std::vector<ui16>::iterator slot = vstd::findFirstNot(hero->artifWorn,toGive->possibleSlots);
  1310. if(slot!=toGive->possibleSlots.end())
  1311. hero->artifWorn[*slot] = toGive->id;
  1312. else
  1313. hero->artifacts.push_back(toGive->id);
  1314. }
  1315. }
  1316. }
  1317. /****************************TOWNS************************************************/
  1318. for (int i=0;i<map->towns.size();i++)
  1319. {
  1320. CGTownInstance * vti =(map->towns[i]);
  1321. if(!vti->town)
  1322. vti->town = &VLC->townh->towns[vti->subID];
  1323. if (vti->name.length()==0) // if town hasn't name we draw it
  1324. vti->name = vti->town->Names()[ran()%vti->town->Names().size()];
  1325. //init buildings
  1326. if(vti->builtBuildings.find(-50)!=vti->builtBuildings.end()) //give standard set of buildings
  1327. {
  1328. vti->builtBuildings.erase(-50);
  1329. vti->builtBuildings.insert(10);
  1330. vti->builtBuildings.insert(5);
  1331. vti->builtBuildings.insert(30);
  1332. if(ran()%2)
  1333. vti->builtBuildings.insert(31);
  1334. }
  1335. //init spells
  1336. vti->spells.resize(SPELL_LEVELS);
  1337. CSpell *s;
  1338. for(int z=0; z<vti->obligatorySpells.size();z++)
  1339. {
  1340. s = &VLC->spellh->spells[vti->obligatorySpells[z]];
  1341. vti->spells[s->level-1].push_back(s->id);
  1342. vti->possibleSpells -= s->id;
  1343. }
  1344. while(vti->possibleSpells.size())
  1345. {
  1346. ui32 total=0, sel=-1;
  1347. for(int ps=0;ps<vti->possibleSpells.size();ps++)
  1348. total += VLC->spellh->spells[vti->possibleSpells[ps]].probabilities[vti->subID];
  1349. int r = (total)? ran()%total : -1;
  1350. for(int ps=0; ps<vti->possibleSpells.size();ps++)
  1351. {
  1352. r -= VLC->spellh->spells[vti->possibleSpells[ps]].probabilities[vti->subID];
  1353. if(r<0)
  1354. {
  1355. sel = ps;
  1356. break;
  1357. }
  1358. }
  1359. if(sel<0)
  1360. sel=0;
  1361. CSpell *s = &VLC->spellh->spells[vti->possibleSpells[sel]];
  1362. vti->spells[s->level-1].push_back(s->id);
  1363. vti->possibleSpells -= s->id;
  1364. }
  1365. //init garrisons
  1366. for (std::map<si32,std::pair<ui32,si32> >::iterator j=vti->army.slots.begin(); j!=vti->army.slots.end();j++)
  1367. {
  1368. if(j->second.first > 196 && j->second.first < 211)
  1369. {
  1370. if(j->second.first%2)
  1371. j->second.first = vti->town->basicCreatures[ (j->second.first-197) / 2 ];
  1372. else
  1373. j->second.first = vti->town->upgradedCreatures[ (j->second.first-197) / 2 ];
  1374. }
  1375. }
  1376. players[vti->getOwner()].towns.push_back(vti);
  1377. }
  1378. for(std::map<ui8, PlayerState>::iterator k=players.begin(); k!=players.end(); ++k)
  1379. {
  1380. if(k->first==-1 || k->first==255)
  1381. continue;
  1382. for(int xd=0; xd<map->width; ++xd) //revealing part of map around towns
  1383. {
  1384. for(int yd=0; yd<map->height; ++yd)
  1385. {
  1386. for(int ch=0; ch<k->second.towns.size(); ++ch)
  1387. {
  1388. int deltaX = (k->second.towns[ch]->pos.x-xd)*(k->second.towns[ch]->pos.x-xd);
  1389. int deltaY = (k->second.towns[ch]->pos.y-yd)*(k->second.towns[ch]->pos.y-yd);
  1390. if(deltaX+deltaY<k->second.towns[ch]->getSightDistance()*k->second.towns[ch]->getSightDistance())
  1391. k->second.fogOfWarMap[xd][yd][k->second.towns[ch]->pos.z] = 1;
  1392. }
  1393. }
  1394. }
  1395. //init visiting and garrisoned heroes
  1396. for(int l=0; l<k->second.heroes.size();l++)
  1397. {
  1398. for(int m=0; m<k->second.towns.size();m++)
  1399. {
  1400. int3 vistile = k->second.towns[m]->pos; vistile.x--; //tile next to the entrance
  1401. if(vistile == k->second.heroes[l]->pos || k->second.heroes[l]->pos==k->second.towns[m]->pos)
  1402. {
  1403. k->second.towns[m]->visitingHero = k->second.heroes[l];
  1404. k->second.heroes[l]->visitedTown = k->second.towns[m];
  1405. k->second.heroes[l]->inTownGarrison = false;
  1406. if(k->second.heroes[l]->pos==k->second.towns[m]->pos)
  1407. k->second.heroes[l]->pos.x -= 1;
  1408. break;
  1409. }
  1410. }
  1411. }
  1412. }
  1413. for(int i=0; i<map->objects.size(); i++)
  1414. map->objects[i]->initObj();
  1415. }
  1416. bool CGameState::battleShootCreatureStack(int ID, int dest)
  1417. {
  1418. return true;
  1419. }
  1420. int CGameState::battleGetStack(int pos)
  1421. {
  1422. if(!curB)
  1423. return -1;
  1424. for(int g=0; g<curB->stacks.size(); ++g)
  1425. {
  1426. if((curB->stacks[g]->position == pos
  1427. || (curB->stacks[g]->creature->isDoubleWide()
  1428. &&( (curB->stacks[g]->attackerOwned && curB->stacks[g]->position-1 == pos)
  1429. || (!curB->stacks[g]->attackerOwned && curB->stacks[g]->position+1 == pos) )
  1430. ))
  1431. && curB->stacks[g]->alive()
  1432. )
  1433. return curB->stacks[g]->ID;
  1434. }
  1435. return -1;
  1436. }
  1437. UpgradeInfo CGameState::getUpgradeInfo(CArmedInstance *obj, int stackPos)
  1438. {
  1439. UpgradeInfo ret;
  1440. CCreature *base = &VLC->creh->creatures[obj->army.slots[stackPos].first];
  1441. if((obj->ID == 98) || ((obj->ID == 34) && static_cast<const CGHeroInstance*>(obj)->visitedTown))
  1442. {
  1443. CGTownInstance * t;
  1444. if(obj->ID == 98)
  1445. t = static_cast<CGTownInstance *>(const_cast<CArmedInstance *>(obj));
  1446. else
  1447. t = static_cast<const CGHeroInstance*>(obj)->visitedTown;
  1448. for(std::set<si32>::iterator i=t->builtBuildings.begin(); i!=t->builtBuildings.end(); i++)
  1449. {
  1450. if( (*i) >= 37 && (*i) < 44 ) //upgraded creature dwelling
  1451. {
  1452. int nid = t->town->upgradedCreatures[(*i)-37]; //upgrade offered by that building
  1453. if(base->upgrades.find(nid) != base->upgrades.end()) //possible upgrade
  1454. {
  1455. ret.newID.push_back(nid);
  1456. ret.cost.push_back(std::set<std::pair<int,int> >());
  1457. for(int j=0;j<RESOURCE_QUANTITY;j++)
  1458. {
  1459. int dif = VLC->creh->creatures[nid].cost[j] - base->cost[j];
  1460. if(dif)
  1461. ret.cost[ret.cost.size()-1].insert(std::make_pair(j,dif));
  1462. }
  1463. }
  1464. }
  1465. }//end for
  1466. }
  1467. //TODO: check if hero ability makes some upgrades possible
  1468. if(ret.newID.size())
  1469. ret.oldID = base->idNumber;
  1470. return ret;
  1471. }
  1472. float CGameState::getMarketEfficiency( int player, int mode/*=0*/ )
  1473. {
  1474. boost::shared_lock<boost::shared_mutex> lock(*mx);
  1475. if(mode) return -1; //todo - support other modes
  1476. int mcount = 0;
  1477. for(int i=0;i<players[player].towns.size();i++)
  1478. if(vstd::contains(players[player].towns[i]->builtBuildings,14))
  1479. mcount++;
  1480. float ret = std::min(((float)mcount+1.0f)/20.0f,0.5f);
  1481. return ret;
  1482. }
  1483. std::set<int3> CGameState::tilesToReveal(int3 pos, int radious, int player) const
  1484. {
  1485. std::set<int3> ret;
  1486. int xbeg = pos.x - radious - 2;
  1487. if(xbeg < 0)
  1488. xbeg = 0;
  1489. int xend = pos.x + radious + 2;
  1490. if(xend >= map->width)
  1491. xend = map->width;
  1492. int ybeg = pos.y - radious - 2;
  1493. if(ybeg < 0)
  1494. ybeg = 0;
  1495. int yend = pos.y + radious + 2;
  1496. if(yend >= map->height)
  1497. yend = map->height;
  1498. for(int xd=xbeg; xd<xend; ++xd) //revealing part of map around heroes
  1499. {
  1500. for(int yd=ybeg; yd<yend; ++yd)
  1501. {
  1502. int deltaX = (pos.x-xd)*(pos.x-xd);
  1503. int deltaY = (pos.y-yd)*(pos.y-yd);
  1504. if(deltaX+deltaY<radious*radious)
  1505. {
  1506. if(player<0 || players.find(player)->second.fogOfWarMap[xd][yd][pos.z]==0)
  1507. {
  1508. ret.insert(int3(xd,yd,pos.z));
  1509. }
  1510. }
  1511. }
  1512. }
  1513. return ret;
  1514. }
  1515. void CGameState::loadTownDInfos()
  1516. {
  1517. for(int i=0;i<F_NUMBER;i++)
  1518. {
  1519. villages[i] = new CGDefInfo(*VLC->dobjinfo->castles[i]);
  1520. forts[i] = VLC->dobjinfo->castles[i];
  1521. capitols[i] = new CGDefInfo(*VLC->dobjinfo->castles[i]);
  1522. }
  1523. }
  1524. void CGameState::setObjProperty( SetObjectProperty * p )
  1525. {
  1526. CGObjectInstance *obj = map->objects[p->id];
  1527. switch(p->what)
  1528. {
  1529. case 1:
  1530. obj->tempOwner = p->val;
  1531. break;
  1532. case 2:
  1533. obj->blockVisit = p->val;
  1534. break;
  1535. case 3:
  1536. static_cast<CArmedInstance*>(obj)->army.slots[0].second = p->val;
  1537. break;
  1538. case 4:
  1539. static_cast<CGVisitableOPH*>(obj)->visitors.insert(p->val);
  1540. break;
  1541. case 5:
  1542. static_cast<CGVisitableOPW*>(obj)->visited = p->val;
  1543. break;
  1544. }
  1545. }
  1546. int BattleInfo::calculateDmg(const CStack* attacker, const CStack* defender, const CGHeroInstance * attackerHero, const CGHeroInstance * defendingHero, bool shooting)
  1547. {
  1548. int attackerAttackBonus = attacker->creature->attack + (attackerHero ? attackerHero->getPrimSkillLevel(0) : 0);
  1549. if(attacker->getEffect(56)) //frenzy for attacker
  1550. {
  1551. attackerAttackBonus += (VLC->spellh->spells[attacker->getEffect(56)->id].powers[attacker->getEffect(56)->level]/100.0) *(attacker->creature->defence + (attackerHero ? attackerHero->getPrimSkillLevel(1) : 0));
  1552. }
  1553. int defenderDefenseBonus = defender->creature->defence + (defendingHero ? defendingHero->getPrimSkillLevel(1) : 0);
  1554. if(defender->getEffect(56)) //frenzy for defender
  1555. {
  1556. defenderDefenseBonus = 0;
  1557. }
  1558. int attackDefenseBonus = attackerAttackBonus - defenderDefenseBonus;
  1559. if(defender->getEffect(48)) //defender's prayer handling
  1560. {
  1561. attackDefenseBonus -= VLC->spellh->spells[defender->getEffect(48)->id].powers[defender->getEffect(48)->level];
  1562. }
  1563. if(attacker->getEffect(48)) //attacker's prayer handling
  1564. {
  1565. attackDefenseBonus += VLC->spellh->spells[attacker->getEffect(48)->id].powers[attacker->getEffect(48)->level];
  1566. }
  1567. if(defender->getEffect(46)) //stone skin handling
  1568. {
  1569. attackDefenseBonus -= VLC->spellh->spells[defender->getEffect(46)->id].powers[defender->getEffect(46)->level];
  1570. }
  1571. if(attacker->getEffect(45)) //weakness handling
  1572. {
  1573. attackDefenseBonus -= VLC->spellh->spells[attacker->getEffect(45)->id].powers[attacker->getEffect(45)->level];
  1574. }
  1575. if(!shooting && attacker->getEffect(43)) //bloodlust handling
  1576. {
  1577. attackDefenseBonus += VLC->spellh->spells[attacker->getEffect(43)->id].powers[attacker->getEffect(43)->level];
  1578. }
  1579. int damageBase = 0;
  1580. if(attacker->getEffect(42)) //curse handling (partial, the rest is below)
  1581. {
  1582. damageBase = attacker->creature->damageMin;
  1583. damageBase -= VLC->spellh->spells[attacker->getEffect(42)->id].powers[attacker->getEffect(42)->level];
  1584. }
  1585. else if(attacker->getEffect(41)) //bless handling
  1586. {
  1587. damageBase = attacker->creature->damageMax;
  1588. damageBase += VLC->spellh->spells[attacker->getEffect(41)->id].powers[attacker->getEffect(41)->level];
  1589. }
  1590. else if(attacker->creature->damageMax == attacker->creature->damageMin) //constant damage
  1591. {
  1592. damageBase = attacker->creature->damageMin;
  1593. }
  1594. else
  1595. {
  1596. damageBase = rand()%(attacker->creature->damageMax - attacker->creature->damageMin) + attacker->creature->damageMin + 1;
  1597. }
  1598. float dmgBonusMultiplier = 1.0f;
  1599. if(attackDefenseBonus < 0) //decreasing dmg
  1600. {
  1601. if(0.02f * (-attackDefenseBonus) > 0.3f)
  1602. {
  1603. dmgBonusMultiplier += -0.3f;
  1604. }
  1605. else
  1606. {
  1607. dmgBonusMultiplier += 0.02f * attackDefenseBonus;
  1608. }
  1609. }
  1610. else //increasing dmg
  1611. {
  1612. if(0.05f * attackDefenseBonus > 4.0f)
  1613. {
  1614. dmgBonusMultiplier += 4.0f;
  1615. }
  1616. else
  1617. {
  1618. dmgBonusMultiplier += 0.05f * attackDefenseBonus;
  1619. }
  1620. }
  1621. //handling secondary abilities
  1622. if(attackerHero)
  1623. {
  1624. if(shooting)
  1625. {
  1626. switch(attackerHero->getSecSkillLevel(1)) //archery
  1627. {
  1628. case 1: //basic
  1629. dmgBonusMultiplier *= 1.1f;
  1630. break;
  1631. case 2: //advanced
  1632. dmgBonusMultiplier *= 1.25f;
  1633. break;
  1634. case 3: //expert
  1635. dmgBonusMultiplier *= 1.5f;
  1636. break;
  1637. }
  1638. }
  1639. else
  1640. {
  1641. switch(attackerHero->getSecSkillLevel(22)) //offence
  1642. {
  1643. case 1: //basic
  1644. dmgBonusMultiplier *= 1.1f;
  1645. break;
  1646. case 2: //advanced
  1647. dmgBonusMultiplier *= 1.2f;
  1648. break;
  1649. case 3: //expert
  1650. dmgBonusMultiplier *= 1.3f;
  1651. break;
  1652. }
  1653. }
  1654. }
  1655. if(defendingHero)
  1656. {
  1657. switch(defendingHero->getSecSkillLevel(23)) //armourer
  1658. {
  1659. case 1: //basic
  1660. dmgBonusMultiplier *= 0.95f;
  1661. break;
  1662. case 2: //advanced
  1663. dmgBonusMultiplier *= 0.9f;
  1664. break;
  1665. case 3: //expert
  1666. dmgBonusMultiplier *= 0.85f;
  1667. break;
  1668. }
  1669. }
  1670. //handling spell effects
  1671. if(!shooting && defender->getEffect(27)) //shield
  1672. {
  1673. if(defender->getEffect(27)->level<=1) //none or basic
  1674. dmgBonusMultiplier *= 0.85f;
  1675. else //adv or expert
  1676. dmgBonusMultiplier *= 0.7f;
  1677. }
  1678. if(shooting && defender->getEffect(28)) //air shield
  1679. {
  1680. if(defender->getEffect(28)->level<=1) //none or basic
  1681. dmgBonusMultiplier *= 0.75f;
  1682. else //adv or expert
  1683. dmgBonusMultiplier *= 0.5f;
  1684. }
  1685. if(attacker->getEffect(42)) //curse, second part of handling
  1686. {
  1687. if(attacker->getEffect(42)->level>=2) //adv or expert
  1688. dmgBonusMultiplier *= 0.8f;
  1689. }
  1690. return (float)damageBase * (float)attacker->amount * dmgBonusMultiplier;
  1691. }
  1692. void BattleInfo::calculateCasualties( std::set<std::pair<ui32,si32> > *casualties )
  1693. {
  1694. for(int i=0; i<stacks.size();i++)//setting casualties
  1695. {
  1696. if(!stacks[i]->alive())
  1697. {
  1698. casualties[!stacks[i]->attackerOwned].insert(std::pair<ui32,si32>(stacks[i]->creature->idNumber,stacks[i]->baseAmount));
  1699. }
  1700. else if(stacks[i]->amount != stacks[i]->baseAmount)
  1701. {
  1702. casualties[!stacks[i]->attackerOwned].insert(std::pair<ui32,si32>(stacks[i]->creature->idNumber,stacks[i]->baseAmount - stacks[i]->amount));
  1703. }
  1704. }
  1705. }
  1706. CStack * BattleInfo::getNextStack()
  1707. {
  1708. CStack *current = getStack(activeStack);
  1709. for (int i = 0; i < stacks.size(); i++) //find fastest not moved/waited stack (stacks vector is sorted by speed)
  1710. {
  1711. if(vstd::contains(stacks[i]->state,DEFENDING)
  1712. ||vstd::contains(stacks[i]->state,WAITING)
  1713. ||vstd::contains(stacks[i]->state,MOVED)
  1714. ||!stacks[i]->alive()
  1715. ||stacks[i] == current
  1716. )
  1717. continue;
  1718. return stacks[i];
  1719. }
  1720. for (int i = stacks.size() - 1; i >= 0 ; i--) //find slowest waiting stack
  1721. {
  1722. if(vstd::contains(stacks[i]->state,DEFENDING)
  1723. ||vstd::contains(stacks[i]->state,MOVED)
  1724. ||!stacks[i]->alive()
  1725. ||stacks[i] == current
  1726. )
  1727. continue;
  1728. return stacks[i];
  1729. }
  1730. return NULL; //all stacks moved or defending!
  1731. }
  1732. std::vector<CStack> BattleInfo::getStackQueue()
  1733. {
  1734. std::vector<CStack> ret;
  1735. std::vector<int> taken; //if non-zero value, corresponding stack has been placed in ret
  1736. taken.resize(stacks.size());
  1737. for(int g=0; g<taken.size(); ++g)
  1738. {
  1739. taken[g] = 0;
  1740. }
  1741. for(int moved=0; moved<2; ++moved) //in first cycle we add stacks that can act in current turn, in second one the rest of them
  1742. {
  1743. for(int gc=0; gc<stacks.size(); ++gc)
  1744. {
  1745. int id = -1, speed = -1;
  1746. for(int i=0; i<stacks.size(); ++i) //find not waited stacks only
  1747. {
  1748. if((moved == 1 ||!vstd::contains(stacks[i]->state,DEFENDING))
  1749. && stacks[i]->alive()
  1750. && (moved == 1 || !vstd::contains(stacks[i]->state,MOVED))
  1751. && !vstd::contains(stacks[i]->state,WAITING)
  1752. && taken[i]==0)
  1753. {
  1754. if(speed == -1 || stacks[i]->speed() > speed)
  1755. {
  1756. id = i;
  1757. speed = stacks[i]->speed();
  1758. }
  1759. }
  1760. }
  1761. if(id != -1)
  1762. {
  1763. ret.push_back(*stacks[id]);
  1764. taken[id] = 1;
  1765. }
  1766. else //choose something from not moved stacks
  1767. {
  1768. int id = -1, speed = 10000; //infinite speed
  1769. for(int i=0; i<stacks.size(); ++i) //find waited stacks only
  1770. {
  1771. if((moved == 1 ||!vstd::contains(stacks[i]->state,DEFENDING))
  1772. && stacks[i]->alive()
  1773. && (moved == 1 || !vstd::contains(stacks[i]->state,MOVED))
  1774. && vstd::contains(stacks[i]->state,WAITING)
  1775. && taken[i]==0)
  1776. {
  1777. if(stacks[i]->speed() < speed) //slowest one
  1778. {
  1779. id = i;
  1780. speed = stacks[i]->speed();
  1781. }
  1782. }
  1783. }
  1784. if(id != -1)
  1785. {
  1786. ret.push_back(*stacks[id]);
  1787. taken[id] = 1;
  1788. }
  1789. else
  1790. {
  1791. break; //no stacks have been found, so none of them will be found in next iterations
  1792. }
  1793. }
  1794. }
  1795. }
  1796. return ret;
  1797. }