CGameState.cpp 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234
  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/CTownHandler.h"
  10. #include "hch/CSpellHandler.h"
  11. #include "hch/CHeroHandler.h"
  12. #include "hch/CObjectHandler.h"
  13. #include "hch/CCreatureHandler.h"
  14. #include "lib/VCMI_Lib.h"
  15. #include "map.h"
  16. #include "StartInfo.h"
  17. #include "lib/NetPacks.h"
  18. #include <boost/foreach.hpp>
  19. #include <boost/thread.hpp>
  20. #include <boost/thread/shared_mutex.hpp>
  21. boost::rand48 ran;
  22. CGObjectInstance * createObject(int id, int subid, int3 pos, int owner)
  23. {
  24. CGObjectInstance * nobj;
  25. switch(id)
  26. {
  27. case 34: //hero
  28. {
  29. CGHeroInstance * nobj;
  30. nobj = new CGHeroInstance();
  31. nobj->pos = pos;
  32. nobj->tempOwner = owner;
  33. nobj->defInfo = new CGDefInfo();
  34. nobj->defInfo->id = 34;
  35. nobj->defInfo->subid = subid;
  36. nobj->defInfo->printPriority = 0;
  37. nobj->type = VLC->heroh->heroes[subid];
  38. for(int i=0;i<6;i++)
  39. {
  40. nobj->defInfo->blockMap[i]=255;
  41. nobj->defInfo->visitMap[i]=0;
  42. }
  43. nobj->ID = id;
  44. nobj->subID = subid;
  45. nobj->defInfo->handler=NULL;
  46. nobj->defInfo->blockMap[5] = 253;
  47. nobj->defInfo->visitMap[5] = 2;
  48. nobj->artifWorn[16] = 3;
  49. nobj->portrait = subid;
  50. nobj->primSkills.resize(4);
  51. nobj->primSkills[0] = nobj->type->heroClass->initialAttack;
  52. nobj->primSkills[1] = nobj->type->heroClass->initialDefence;
  53. nobj->primSkills[2] = nobj->type->heroClass->initialPower;
  54. nobj->primSkills[3] = nobj->type->heroClass->initialKnowledge;
  55. nobj->mana = 10 * nobj->primSkills[3];
  56. return nobj;
  57. }
  58. case 98: //town
  59. nobj = new CGTownInstance;
  60. break;
  61. default: //rest of objects
  62. nobj = new CGObjectInstance;
  63. nobj->defInfo = VLC->dobjinfo->gobjs[id][subid];
  64. break;
  65. }
  66. nobj->ID = id;
  67. nobj->subID = subid;
  68. if(!nobj->defInfo)
  69. std::cout <<"No def declaration for " <<id <<" "<<subid<<std::endl;
  70. nobj->pos = pos;
  71. //nobj->state = NULL;//new CLuaObjectScript();
  72. nobj->tempOwner = owner;
  73. nobj->info = NULL;
  74. nobj->defInfo->id = id;
  75. nobj->defInfo->subid = subid;
  76. //assigning defhandler
  77. if(nobj->ID==34 || nobj->ID==98)
  78. return nobj;
  79. nobj->defInfo = VLC->dobjinfo->gobjs[id][subid];
  80. //if(!nobj->defInfo->handler)
  81. //{
  82. // nobj->defInfo->handler = CDefHandler::giveDef(nobj->defInfo->name);
  83. // nobj->defInfo->width = nobj->defInfo->handler->ourImages[0].bitmap->w/32;
  84. // nobj->defInfo->height = nobj->defInfo->handler->ourImages[0].bitmap->h/32;
  85. //}
  86. return nobj;
  87. }
  88. CStack * BattleInfo::getStack(int stackID)
  89. {
  90. for(int g=0; g<stacks.size(); ++g)
  91. {
  92. if(stacks[g]->ID == stackID)
  93. return stacks[g];
  94. }
  95. return NULL;
  96. }
  97. CStack * BattleInfo::getStackT(int tileID)
  98. {
  99. for(int g=0; g<stacks.size(); ++g)
  100. {
  101. if(stacks[g]->position == tileID
  102. || (stacks[g]->creature->isDoubleWide() && stacks[g]->attackerOwned && stacks[g]->position-1 == tileID)
  103. || (stacks[g]->creature->isDoubleWide() && !stacks[g]->attackerOwned && stacks[g]->position+1 == tileID))
  104. {
  105. if(stacks[g]->alive)
  106. {
  107. return stacks[g];
  108. }
  109. }
  110. }
  111. return NULL;
  112. }
  113. void BattleInfo::getAccessibilityMap(bool *accessibility, int stackToOmmit)
  114. {
  115. memset(accessibility,1,187); //initialize array with trues
  116. for(int g=0; g<stacks.size(); ++g)
  117. {
  118. if(!stacks[g]->alive || stacks[g]->ID==stackToOmmit) //we don't want to lock position of this stack
  119. continue;
  120. accessibility[stacks[g]->position] = false;
  121. if(stacks[g]->creature->isDoubleWide()) //if it's a double hex creature
  122. {
  123. if(stacks[g]->attackerOwned)
  124. accessibility[stacks[g]->position-1] = false;
  125. else
  126. accessibility[stacks[g]->position+1] = false;
  127. }
  128. }
  129. //TODO: obstacles
  130. }
  131. void BattleInfo::getAccessibilityMapForTwoHex(bool *accessibility, bool atackerSide, int stackToOmmit) //send pointer to at least 187 allocated bytes
  132. {
  133. bool mac[187];
  134. getAccessibilityMap(mac,stackToOmmit);
  135. memcpy(accessibility,mac,187);
  136. for(int b=0; b<187; ++b)
  137. {
  138. if( mac[b] && !(atackerSide ? mac[b-1] : mac[b+1]))
  139. {
  140. accessibility[b] = false;
  141. }
  142. }
  143. //removing accessibility for side hexes
  144. for(int v=0; v<187; ++v)
  145. if(atackerSide ? (v%17)==1 : (v%17)==15)
  146. accessibility[v] = false;
  147. }
  148. void BattleInfo::makeBFS(int start, bool*accessibility, int *predecessor, int *dists) //both pointers must point to the at least 187-elements int arrays
  149. {
  150. //inits
  151. for(int b=0; b<187; ++b)
  152. predecessor[b] = -1;
  153. for(int g=0; g<187; ++g)
  154. dists[g] = 100000000;
  155. std::queue<int> hexq; //bfs queue
  156. hexq.push(start);
  157. dists[hexq.front()] = 0;
  158. int curNext = -1; //for bfs loop only (helper var)
  159. while(!hexq.empty()) //bfs loop
  160. {
  161. int curHex = hexq.front();
  162. std::vector<int> neighbours = neighbouringTiles(curHex);
  163. hexq.pop();
  164. for(int nr=0; nr<neighbours.size(); nr++)
  165. {
  166. curNext = neighbours[nr];
  167. if(!accessibility[curNext] || (dists[curHex]+1)>=dists[curNext])
  168. continue;
  169. hexq.push(curNext);
  170. dists[curNext] = dists[curHex] + 1;
  171. predecessor[curNext] = curHex;
  172. }
  173. }
  174. };
  175. std::vector<int> BattleInfo::getAccessibility(int stackID)
  176. {
  177. std::vector<int> ret;
  178. bool ac[187];
  179. CStack *s = getStack(stackID);
  180. if(s->creature->isDoubleWide())
  181. getAccessibilityMapForTwoHex(ac,s->attackerOwned,stackID);
  182. else
  183. getAccessibilityMap(ac,stackID);
  184. int pr[187], dist[187];
  185. makeBFS(s->position,ac,pr,dist);
  186. for(int i=0;i<187;i++)
  187. if(dist[i] <= s->creature->speed)
  188. ret.push_back(i);
  189. return ret;
  190. }
  191. signed char BattleInfo::mutualPosition(int hex1, int hex2)
  192. {
  193. if(hex2 == hex1 - ( (hex1/17)%2 ? 18 : 17 )) //top left
  194. return 0;
  195. if(hex2 == hex1 - ( (hex1/17)%2 ? 17 : 16 )) //top right
  196. return 1;
  197. if(hex2 == hex1 - 1 && hex1%17 != 0) //left
  198. return 5;
  199. if(hex2 == hex1 + 1 && hex1%17 != 16) //right
  200. return 2;
  201. if(hex2 == hex1 + ( (hex1/17)%2 ? 16 : 17 )) //bottom left
  202. return 4;
  203. if(hex2 == hex1 + ( (hex1/17)%2 ? 17 : 18 )) //bottom right
  204. return 3;
  205. return -1;
  206. }
  207. std::vector<int> BattleInfo::neighbouringTiles(int hex)
  208. {
  209. #define CHECK_AND_PUSH(tile) {int hlp = (tile); if(hlp>=0 && hlp<187 && (hlp%17!=16) && hlp%17) ret.push_back(hlp);}
  210. std::vector<int> ret;
  211. CHECK_AND_PUSH(hex - ( (hex/17)%2 ? 18 : 17 ));
  212. CHECK_AND_PUSH(hex - ( (hex/17)%2 ? 17 : 16 ));
  213. CHECK_AND_PUSH(hex - 1);
  214. CHECK_AND_PUSH(hex + 1);
  215. CHECK_AND_PUSH(hex + ( (hex/17)%2 ? 16 : 17 ));
  216. CHECK_AND_PUSH(hex + ( (hex/17)%2 ? 17 : 18 ));
  217. #undef CHECK_AND_PUSH
  218. return ret;
  219. }
  220. std::vector<int> BattleInfo::getPath(int start, int dest, bool*accessibility)
  221. {
  222. int predecessor[187]; //for getting the Path
  223. int dist[187]; //calculated distances
  224. makeBFS(start,accessibility,predecessor,dist);
  225. //making the Path
  226. std::vector<int> path;
  227. int curElem = dest;
  228. while(curElem != start)
  229. {
  230. path.push_back(curElem);
  231. curElem = predecessor[curElem];
  232. }
  233. return path;
  234. }
  235. CStack::CStack(CCreature * C, int A, int O, int I, bool AO)
  236. :creature(C),amount(A),owner(O), alive(true), position(-1), ID(I), attackerOwned(AO), firstHPleft(C->hitPoints)
  237. {
  238. }
  239. void CGameState::applyNL(IPack * pack)
  240. {
  241. switch(pack->getType())
  242. {
  243. case 101://NewTurn
  244. {
  245. NewTurn * n = static_cast<NewTurn*>(pack);
  246. day = n->day;
  247. BOOST_FOREACH(NewTurn::Hero h, n->heroes) //give mana/movement point
  248. {
  249. static_cast<CGHeroInstance*>(map->objects[h.id])->movement = h.move;
  250. static_cast<CGHeroInstance*>(map->objects[h.id])->mana = h.mana;
  251. }
  252. BOOST_FOREACH(SetResources h, n->res) //give resources
  253. applyNL(&h);
  254. BOOST_FOREACH(SetAvailableCreatures h, n->cres) //set available creatures in towns
  255. applyNL(&h);
  256. if(n->resetBuilded) //reset amount of structures set in this turn in towns
  257. BOOST_FOREACH(CGTownInstance* t, map->towns)
  258. t->builded = 0;
  259. break;
  260. }
  261. case 102: //set resource amount
  262. {
  263. SetResource *sr = static_cast<SetResource*>(pack);
  264. players[sr->player].resources[sr->resid] = sr->val;
  265. break;
  266. }
  267. case 104:
  268. {
  269. SetResources *sr = static_cast<SetResources*>(pack);
  270. for(int i=0;i<sr->res.size();i++)
  271. players[sr->player].resources[i] = sr->res[i];
  272. break;
  273. }
  274. case 105:
  275. {
  276. SetPrimSkill *sr = static_cast<SetPrimSkill*>(pack);
  277. CGHeroInstance *hero = getHero(sr->id);
  278. if(sr->which <4)
  279. {
  280. if(sr->abs)
  281. hero->primSkills[sr->which] = sr->val;
  282. else
  283. hero->primSkills[sr->which] += sr->val;
  284. }
  285. else if(sr->which == 4) //XP
  286. {
  287. if(sr->abs)
  288. hero->exp = sr->val;
  289. else
  290. hero->exp += sr->val;
  291. }
  292. break;
  293. }
  294. case 106:
  295. {
  296. SetSecSkill *sr = static_cast<SetSecSkill*>(pack);
  297. CGHeroInstance *hero = getHero(sr->id);
  298. if(hero->getSecSkillLevel(sr->which) < 0)
  299. {
  300. hero->secSkills.push_back(std::pair<int,int>(sr->which, (sr->abs)? (sr->val) : (sr->val-1)));
  301. }
  302. else
  303. {
  304. for(unsigned i=0;i<hero->secSkills.size();i++)
  305. {
  306. if(hero->secSkills[i].first == sr->which)
  307. {
  308. if(sr->abs)
  309. hero->secSkills[i].second = sr->val;
  310. else
  311. hero->secSkills[i].second += sr->val;
  312. }
  313. }
  314. }
  315. break;
  316. }
  317. case 108:
  318. {
  319. HeroVisitCastle *vc = static_cast<HeroVisitCastle*>(pack);
  320. CGHeroInstance *h = getHero(vc->hid);
  321. CGTownInstance *t = getTown(vc->tid);
  322. if(vc->start())
  323. {
  324. if(vc->garrison())
  325. {
  326. t->garrisonHero = h;
  327. h->visitedTown = t;
  328. h->inTownGarrison = true;
  329. }
  330. else
  331. {
  332. t->visitingHero = h;
  333. h->visitedTown = t;
  334. h->inTownGarrison = false;
  335. }
  336. }
  337. else
  338. {
  339. if(vc->garrison())
  340. {
  341. t->garrisonHero = NULL;
  342. h->visitedTown = NULL;
  343. h->inTownGarrison = false;
  344. }
  345. else
  346. {
  347. t->visitingHero = NULL;
  348. h->visitedTown = NULL;
  349. h->inTownGarrison = false;
  350. }
  351. }
  352. break;
  353. }
  354. case 500:
  355. {
  356. RemoveObject *rh = static_cast<RemoveObject*>(pack);
  357. CGObjectInstance *obj = map->objects[rh->id];
  358. if(obj->ID==34)
  359. {
  360. CGHeroInstance *h = static_cast<CGHeroInstance*>(obj);
  361. std::vector<CGHeroInstance*>::iterator nitr = std::find(map->heroes.begin(), map->heroes.end(),h);
  362. map->heroes.erase(nitr);
  363. int player = h->tempOwner;
  364. nitr = std::find(players[player].heroes.begin(), players[player].heroes.end(), h);
  365. players[player].heroes.erase(nitr);
  366. if(h->visitedTown)
  367. {
  368. if(h->inTownGarrison)
  369. h->visitedTown->garrisonHero = NULL;
  370. else
  371. h->visitedTown->visitingHero = NULL;
  372. h->visitedTown = NULL;
  373. }
  374. }
  375. map->objects[rh->id] = NULL;
  376. //unblock tiles
  377. if(obj->defInfo)
  378. {
  379. map->removeBlockVisTiles(obj);
  380. }
  381. break;
  382. }
  383. case 501://hero try-move
  384. {
  385. TryMoveHero * n = static_cast<TryMoveHero*>(pack);
  386. CGHeroInstance *h = static_cast<CGHeroInstance*>(map->objects[n->id]);
  387. h->movement = n->movePoints;
  388. if(n->start!=n->end && n->result)
  389. {
  390. map->removeBlockVisTiles(h);
  391. h->pos = n->end;
  392. map->addBlockVisTiles(h);
  393. }
  394. BOOST_FOREACH(int3 t, n->fowRevealed)
  395. players[h->getOwner()].fogOfWarMap[t.x][t.y][t.z] = 1;
  396. break;
  397. }
  398. case 502:
  399. {
  400. SetGarrisons * n = static_cast<SetGarrisons*>(pack);
  401. for(std::map<ui32,CCreatureSet>::iterator i = n->garrs.begin(); i!=n->garrs.end(); i++)
  402. {
  403. CArmedInstance *ai = static_cast<CArmedInstance*>(map->objects[i->first]);
  404. ai->army = i->second;
  405. if(ai->ID==98 && (static_cast<CGTownInstance*>(ai))->garrisonHero) //if there is a hero in garrison then we must update also his army
  406. const_cast<CGHeroInstance*>((static_cast<CGTownInstance*>(ai))->garrisonHero)->army = i->second;
  407. else if(ai->ID==34)
  408. {
  409. CGHeroInstance *h = static_cast<CGHeroInstance*>(ai);
  410. if(h->visitedTown && h->inTownGarrison)
  411. h->visitedTown->army = i->second;
  412. }
  413. }
  414. break;
  415. }
  416. case 503:
  417. {
  418. //SetStrInfo *ssi = static_cast<SetStrInfo*>(pack);
  419. //static_cast<CGTownInstance*>(map->objects[ssi->tid])->strInfo.creatures = ssi->cres;
  420. break;
  421. }
  422. case 504:
  423. {
  424. NewStructures *ns = static_cast<NewStructures*>(pack);
  425. CGTownInstance*t = static_cast<CGTownInstance*>(map->objects[ns->tid]);
  426. BOOST_FOREACH(si32 bid,ns->bid)
  427. t->builtBuildings.insert(bid);
  428. t->builded = ns->builded;
  429. break;
  430. }
  431. case 506:
  432. {
  433. SetAvailableCreatures *sac = static_cast<SetAvailableCreatures*>(pack);
  434. static_cast<CGTownInstance*>(map->objects[sac->tid])->strInfo.creatures = sac->creatures;
  435. break;
  436. }
  437. case 508:
  438. {
  439. SetHeroesInTown *sac = static_cast<SetHeroesInTown*>(pack);
  440. CGTownInstance *t = getTown(sac->tid);
  441. CGHeroInstance *v = getHero(sac->visiting), *g = getHero(sac->garrison);
  442. t->visitingHero = v;
  443. t->garrisonHero = g;
  444. if(v)
  445. {
  446. v->visitedTown = t;
  447. v->inTownGarrison = false;
  448. map->addBlockVisTiles(v);
  449. }
  450. if(g)
  451. {
  452. g->visitedTown = t;
  453. g->inTownGarrison = true;
  454. map->removeBlockVisTiles(g);
  455. }
  456. break;
  457. }
  458. case 509:
  459. {
  460. SetHeroArtifacts *sha = static_cast<SetHeroArtifacts*>(pack);
  461. CGHeroInstance *h = getHero(sha->hid);
  462. h->artifacts = sha->artifacts;
  463. h->artifWorn = sha->artifWorn;
  464. break;
  465. }
  466. case 1001://set object property
  467. {
  468. SetObjectProperty *p = static_cast<SetObjectProperty*>(pack);
  469. ui8 CGObjectInstance::*point;
  470. switch(p->what)
  471. {
  472. case 1:
  473. point = &CGObjectInstance::tempOwner;
  474. break;
  475. case 2:
  476. point = &CGObjectInstance::blockVisit;
  477. break;
  478. }
  479. map->objects[p->id]->*point = p->val;
  480. break;
  481. }
  482. case 2000:
  483. {
  484. HeroLevelUp * bs = static_cast<HeroLevelUp*>(pack);
  485. getHero(bs->heroid)->level = bs->level;
  486. break;
  487. }
  488. case 3000:
  489. {
  490. BattleStart * bs = static_cast<BattleStart*>(pack);
  491. curB = bs->info;
  492. break;
  493. }
  494. case 3001:
  495. {
  496. BattleNextRound *ns = static_cast<BattleNextRound*>(pack);
  497. curB->round = ns->round;
  498. break;
  499. }
  500. case 3002:
  501. {
  502. BattleSetActiveStack *ns = static_cast<BattleSetActiveStack*>(pack);
  503. curB->activeStack = ns->stack;
  504. break;
  505. }
  506. case 3003:
  507. {
  508. BattleResult *br = static_cast<BattleResult*>(pack);
  509. //TODO: give exp, artifacts to winner, decrease armies (casualties)
  510. for(unsigned i=0;i<curB->stacks.size();i++)
  511. delete curB->stacks[i];
  512. delete curB;
  513. curB = NULL;
  514. break;
  515. }
  516. case 3004:
  517. {
  518. BattleStackMoved *br = static_cast<BattleStackMoved*>(pack);
  519. curB->getStack(br->stack)->position = br->tile;
  520. break;
  521. }
  522. case 3005:
  523. {
  524. BattleStackAttacked *br = static_cast<BattleStackAttacked*>(pack);
  525. CStack * at = curB->getStack(br->stackAttacked);
  526. at->amount = br->newAmount;
  527. at->firstHPleft = br->newHP;
  528. at->alive = !br->killed();
  529. break;
  530. }
  531. case 3006:
  532. {
  533. BattleAttack *br = static_cast<BattleAttack*>(pack);
  534. applyNL(&br->bsa);
  535. break;
  536. }
  537. }
  538. }
  539. void CGameState::apply(IPack * pack)
  540. {
  541. mx->lock();
  542. applyNL(pack);
  543. mx->unlock();
  544. }
  545. int CGameState::pickHero(int owner)
  546. {
  547. int h=-1;
  548. if(map->getHero(h = scenarioOps->getIthPlayersSettings(owner).hero,0) && h>=0) //we haven't used selected hero
  549. return h;
  550. int f = scenarioOps->getIthPlayersSettings(owner).castle;
  551. int i=0;
  552. do //try to find free hero of our faction
  553. {
  554. i++;
  555. h = scenarioOps->getIthPlayersSettings(owner).castle*HEROES_PER_TYPE*2+(ran()%(HEROES_PER_TYPE*2));//->scenarioOps->playerInfos[pru].hero = VLC->
  556. } while( map->getHero(h) && i<175);
  557. if(i>174) //probably no free heroes - there's no point in further search, we'll take first free
  558. {
  559. std::cout << "Warning: cannot find free hero - trying to get first available..."<<std::endl;
  560. for(int j=0; j<HEROES_PER_TYPE * 2 * F_NUMBER; j++)
  561. if(!map->getHero(j))
  562. h=j;
  563. }
  564. return h;
  565. }
  566. CGHeroInstance *CGameState::getHero(int objid)
  567. {
  568. if(objid<0 || objid>=map->objects.size())
  569. return NULL;
  570. return static_cast<CGHeroInstance *>(map->objects[objid]);
  571. }
  572. CGTownInstance *CGameState::getTown(int objid)
  573. {
  574. if(objid<0 || objid>=map->objects.size())
  575. return NULL;
  576. return static_cast<CGTownInstance *>(map->objects[objid]);
  577. }
  578. std::pair<int,int> CGameState::pickObject(CGObjectInstance *obj)
  579. {
  580. switch(obj->ID)
  581. {
  582. case 65: //random artifact
  583. return std::pair<int,int>(5,(ran()%136)+7); //tylko sensowny zakres - na poczatku sa katapulty itp, na koncu specjalne i blanki
  584. case 66: //random treasure artifact
  585. return std::pair<int,int>(5,VLC->arth->treasures[ran()%VLC->arth->treasures.size()]->id);
  586. case 67: //random minor artifact
  587. return std::pair<int,int>(5,VLC->arth->minors[ran()%VLC->arth->minors.size()]->id);
  588. case 68: //random major artifact
  589. return std::pair<int,int>(5,VLC->arth->majors[ran()%VLC->arth->majors.size()]->id);
  590. case 69: //random relic artifact
  591. return std::pair<int,int>(5,VLC->arth->relics[ran()%VLC->arth->relics.size()]->id);
  592. case 70: //random hero
  593. {
  594. return std::pair<int,int>(34,pickHero(obj->tempOwner));
  595. }
  596. case 71: //random monster
  597. return std::pair<int,int>(54,ran()%(VLC->creh->creatures.size()));
  598. case 72: //random monster lvl1
  599. return std::pair<int,int>(54,VLC->creh->levelCreatures[1][ran()%VLC->creh->levelCreatures[1].size()]->idNumber);
  600. case 73: //random monster lvl2
  601. return std::pair<int,int>(54,VLC->creh->levelCreatures[2][ran()%VLC->creh->levelCreatures[2].size()]->idNumber);
  602. case 74: //random monster lvl3
  603. return std::pair<int,int>(54,VLC->creh->levelCreatures[3][ran()%VLC->creh->levelCreatures[3].size()]->idNumber);
  604. case 75: //random monster lvl4
  605. return std::pair<int,int>(54,VLC->creh->levelCreatures[4][ran()%VLC->creh->levelCreatures[4].size()]->idNumber);
  606. case 76: //random resource
  607. return std::pair<int,int>(79,ran()%7); //now it's OH3 style, use %8 for mithril
  608. case 77: //random town
  609. {
  610. int align = ((CGTownInstance*)obj)->alignment,
  611. f;
  612. if(align>PLAYER_LIMIT-1)//same as owner / random
  613. {
  614. if(obj->tempOwner > PLAYER_LIMIT-1)
  615. f = -1; //random
  616. else
  617. f = scenarioOps->getIthPlayersSettings(obj->tempOwner).castle;
  618. }
  619. else
  620. {
  621. f = scenarioOps->getIthPlayersSettings(align).castle;
  622. }
  623. if(f<0) f = ran()%VLC->townh->towns.size();
  624. return std::pair<int,int>(98,f);
  625. }
  626. case 162: //random monster lvl5
  627. return std::pair<int,int>(54,VLC->creh->levelCreatures[5][ran()%VLC->creh->levelCreatures[5].size()]->idNumber);
  628. case 163: //random monster lvl6
  629. return std::pair<int,int>(54,VLC->creh->levelCreatures[6][ran()%VLC->creh->levelCreatures[6].size()]->idNumber);
  630. case 164: //random monster lvl7
  631. return std::pair<int,int>(54,VLC->creh->levelCreatures[7][ran()%VLC->creh->levelCreatures[7].size()]->idNumber);
  632. case 216: //random dwelling
  633. {
  634. int faction = ran()%F_NUMBER;
  635. CCreGen2ObjInfo* info =(CCreGen2ObjInfo*)obj->info;
  636. if (info->asCastle)
  637. {
  638. for(int i=0;i<map->objects.size();i++)
  639. {
  640. if(map->objects[i]->ID==77 && dynamic_cast<CGTownInstance*>(map->objects[i])->identifier == info->identifier)
  641. {
  642. randomizeObject(map->objects[i]); //we have to randomize the castle first
  643. faction = map->objects[i]->subID;
  644. break;
  645. }
  646. else if(map->objects[i]->ID==98 && dynamic_cast<CGTownInstance*>(map->objects[i])->identifier == info->identifier)
  647. {
  648. faction = map->objects[i]->subID;
  649. break;
  650. }
  651. }
  652. }
  653. else
  654. {
  655. while((!(info->castles[0]&(1<<faction))))
  656. {
  657. if((faction>7) && (info->castles[1]&(1<<(faction-8))))
  658. break;
  659. faction = ran()%F_NUMBER;
  660. }
  661. }
  662. int level = ((info->maxLevel-info->minLevel) ? (ran()%(info->maxLevel-info->minLevel)+info->minLevel) : (info->minLevel));
  663. int cid = VLC->townh->towns[faction].basicCreatures[level];
  664. for(int i=0;i<VLC->objh->cregens.size();i++)
  665. if(VLC->objh->cregens[i]==cid)
  666. return std::pair<int,int>(17,i);
  667. std::cout << "Cannot find a dwelling for creature "<<cid <<std::endl;
  668. return std::pair<int,int>(17,0);
  669. }
  670. case 217:
  671. {
  672. int faction = ran()%F_NUMBER;
  673. CCreGenObjInfo* info =(CCreGenObjInfo*)obj->info;
  674. if (info->asCastle)
  675. {
  676. for(int i=0;i<map->objects.size();i++)
  677. {
  678. if(map->objects[i]->ID==77 && dynamic_cast<CGTownInstance*>(map->objects[i])->identifier == info->identifier)
  679. {
  680. randomizeObject(map->objects[i]); //we have to randomize the castle first
  681. faction = map->objects[i]->subID;
  682. break;
  683. }
  684. else if(map->objects[i]->ID==98 && dynamic_cast<CGTownInstance*>(map->objects[i])->identifier == info->identifier)
  685. {
  686. faction = map->objects[i]->subID;
  687. break;
  688. }
  689. }
  690. }
  691. else
  692. {
  693. while((!(info->castles[0]&(1<<faction))))
  694. {
  695. if((faction>7) && (info->castles[1]&(1<<(faction-8))))
  696. break;
  697. faction = ran()%F_NUMBER;
  698. }
  699. }
  700. int cid = VLC->townh->towns[faction].basicCreatures[obj->subID];
  701. for(int i=0;i<VLC->objh->cregens.size();i++)
  702. if(VLC->objh->cregens[i]==cid)
  703. return std::pair<int,int>(17,i);
  704. std::cout << "Cannot find a dwelling for creature "<<cid <<std::endl;
  705. return std::pair<int,int>(17,0);
  706. }
  707. case 218:
  708. {
  709. CCreGen3ObjInfo* info =(CCreGen3ObjInfo*)obj->info;
  710. int level = ((info->maxLevel-info->minLevel) ? (ran()%(info->maxLevel-info->minLevel)+info->minLevel) : (info->minLevel));
  711. int cid = VLC->townh->towns[obj->subID].basicCreatures[level];
  712. for(int i=0;i<VLC->objh->cregens.size();i++)
  713. if(VLC->objh->cregens[i]==cid)
  714. return std::pair<int,int>(17,i);
  715. std::cout << "Cannot find a dwelling for creature "<<cid <<std::endl;
  716. return std::pair<int,int>(17,0);
  717. }
  718. }
  719. return std::pair<int,int>(-1,-1);
  720. }
  721. void CGameState::randomizeObject(CGObjectInstance *cur)
  722. {
  723. std::pair<int,int> ran = pickObject(cur);
  724. if(ran.first<0 || ran.second<0) //this is not a random object, or we couldn't find anything
  725. {
  726. if(cur->ID==98) //town - set def
  727. {
  728. CGTownInstance *t = dynamic_cast<CGTownInstance*>(cur);
  729. if(t->hasCapitol())
  730. t->defInfo = capitols[t->subID];
  731. else if(t->hasFort())
  732. t->defInfo = forts[t->subID];
  733. else
  734. t->defInfo = villages[t->subID];
  735. }
  736. return;
  737. }
  738. else if(ran.first==34)//special code for hero
  739. {
  740. CGHeroInstance *h = dynamic_cast<CGHeroInstance *>(cur);
  741. if(!h) {std::cout<<"Wrong random hero at "<<cur->pos<<std::endl; return;}
  742. cur->ID = ran.first;
  743. h->portrait = cur->subID = ran.second;
  744. h->type = VLC->heroh->heroes[ran.second];
  745. map->heroes.push_back(h);
  746. return; //TODO: maybe we should do something with definfo?
  747. }
  748. else if(ran.first==98)//special code for town
  749. {
  750. CGTownInstance *t = dynamic_cast<CGTownInstance*>(cur);
  751. if(!t) {std::cout<<"Wrong random town at "<<cur->pos<<std::endl; return;}
  752. cur->ID = ran.first;
  753. cur->subID = ran.second;
  754. t->town = &VLC->townh->towns[ran.second];
  755. if(t->hasCapitol())
  756. t->defInfo = capitols[t->subID];
  757. else if(t->hasFort())
  758. t->defInfo = forts[t->subID];
  759. else
  760. t->defInfo = villages[t->subID];
  761. map->towns.push_back(t);
  762. return;
  763. }
  764. //we have to replace normal random object
  765. cur->ID = ran.first;
  766. cur->subID = ran.second;
  767. map->defs.insert(cur->defInfo = VLC->dobjinfo->gobjs[ran.first][ran.second]);
  768. if(!cur->defInfo)
  769. {
  770. std::cout<<"Missing def declaration for "<<cur->ID<<" "<<cur->subID<<std::endl;
  771. return;
  772. }
  773. }
  774. int CGameState::getDate(int mode) const
  775. {
  776. int temp;
  777. switch (mode)
  778. {
  779. case 0:
  780. return day;
  781. break;
  782. case 1:
  783. temp = (day)%7;
  784. if (temp)
  785. return temp;
  786. else return 7;
  787. break;
  788. case 2:
  789. temp = ((day-1)/7)+1;
  790. if (!(temp%4))
  791. return 4;
  792. else
  793. return (temp%4);
  794. break;
  795. case 3:
  796. return ((day-1)/28)+1;
  797. break;
  798. }
  799. return 0;
  800. }
  801. CGameState::CGameState()
  802. {
  803. mx = new boost::shared_mutex();
  804. }
  805. CGameState::~CGameState()
  806. {
  807. delete mx;
  808. }
  809. void CGameState::init(StartInfo * si, Mapa * map, int Seed)
  810. {
  811. day = 0;
  812. seed = Seed;
  813. ran.seed((boost::int32_t)seed);
  814. scenarioOps = si;
  815. this->map = map;
  816. for(int i=0;i<F_NUMBER;i++)
  817. {
  818. villages[i] = new CGDefInfo(*VLC->dobjinfo->castles[i]);
  819. forts[i] = VLC->dobjinfo->castles[i];
  820. capitols[i] = new CGDefInfo(*VLC->dobjinfo->castles[i]);
  821. }
  822. //picking random factions for players
  823. for(int i=0;i<scenarioOps->playerInfos.size();i++)
  824. {
  825. if(scenarioOps->playerInfos[i].castle==-1)
  826. {
  827. int f;
  828. do
  829. {
  830. f = ran()%F_NUMBER;
  831. }while(!(map->players[scenarioOps->playerInfos[i].color].allowedFactions & 1<<f));
  832. scenarioOps->playerInfos[i].castle = f;
  833. }
  834. }
  835. //randomizing objects
  836. for(int no=0; no<map->objects.size(); ++no)
  837. {
  838. randomizeObject(map->objects[no]);
  839. if(map->objects[no]->ID==26)
  840. map->objects[no]->defInfo->handler=NULL;
  841. map->objects[no]->hoverName = VLC->objh->names[map->objects[no]->ID];
  842. }
  843. //std::cout<<"\tRandomizing objects: "<<th.getDif()<<std::endl;
  844. //giving starting hero
  845. for(int i=0;i<PLAYER_LIMIT;i++)
  846. {
  847. if((map->players[i].generateHeroAtMainTown && map->players[i].hasMainTown) || (map->players[i].hasMainTown && map->version==RoE))
  848. {
  849. int3 hpos = map->players[i].posOfMainTown;
  850. hpos.x+=1;// hpos.y+=1;
  851. int j;
  852. for(j=0; j<scenarioOps->playerInfos.size(); j++)
  853. if(scenarioOps->playerInfos[j].color == i)
  854. break;
  855. if(j == scenarioOps->playerInfos.size())
  856. continue;
  857. int h=pickHero(i);
  858. CGHeroInstance * nnn = static_cast<CGHeroInstance*>(createObject(34,h,hpos,i));
  859. nnn->id = map->objects.size();
  860. hpos = map->players[i].posOfMainTown;hpos.x+=2;
  861. for(int o=0;o<map->towns.size();o++) //find main town
  862. {
  863. if(map->towns[o]->pos == hpos)
  864. {
  865. map->towns[o]->visitingHero = nnn;
  866. nnn->visitedTown = map->towns[o];
  867. nnn->inTownGarrison = false;
  868. break;
  869. }
  870. }
  871. //nnn->defInfo->handler = graphics->flags1[0];
  872. map->heroes.push_back(nnn);
  873. map->objects.push_back(nnn);
  874. }
  875. }
  876. //std::cout<<"\tGiving starting heroes: "<<th.getDif()<<std::endl;
  877. /*********creating players entries in gs****************************************/
  878. for (int i=0; i<scenarioOps->playerInfos.size();i++)
  879. {
  880. std::pair<int,PlayerState> ins(scenarioOps->playerInfos[i].color,PlayerState());
  881. ins.second.color=ins.first;
  882. ins.second.serial=i;
  883. players.insert(ins);
  884. }
  885. /******************RESOURCES****************************************************/
  886. //TODO: zeby komputer dostawal inaczej niz gracz
  887. std::vector<int> startres;
  888. std::ifstream tis("config/startres.txt");
  889. int k;
  890. for (int j=0;j<scenarioOps->difficulty;j++)
  891. {
  892. tis >> k;
  893. for (int z=0;z<RESOURCE_QUANTITY;z++)
  894. tis>>k;
  895. }
  896. tis >> k;
  897. for (int i=0;i<RESOURCE_QUANTITY;i++)
  898. {
  899. tis >> k;
  900. startres.push_back(k);
  901. }
  902. tis.close();
  903. for (std::map<ui8,PlayerState>::iterator i = players.begin(); i!=players.end(); i++)
  904. {
  905. (*i).second.resources.resize(RESOURCE_QUANTITY);
  906. for (int x=0;x<RESOURCE_QUANTITY;x++)
  907. (*i).second.resources[x] = startres[x];
  908. }
  909. /*************************HEROES************************************************/
  910. for (int i=0; i<map->heroes.size();i++) //heroes instances
  911. {
  912. if (map->heroes[i]->getOwner()<0)
  913. continue;
  914. CGHeroInstance * vhi = (map->heroes[i]);
  915. if(!vhi->type)
  916. vhi->type = VLC->heroh->heroes[vhi->subID];
  917. if (vhi->level<1)
  918. {
  919. vhi->exp=40+ran()%50;
  920. vhi->level = 1;
  921. }
  922. if (vhi->level>1) ;//TODO dodac um dr, ale potrzebne los
  923. if ((!vhi->primSkills.size()) || (vhi->primSkills[0]<0))
  924. {
  925. if (vhi->primSkills.size()<PRIMARY_SKILLS)
  926. vhi->primSkills.resize(PRIMARY_SKILLS);
  927. vhi->primSkills[0] = vhi->type->heroClass->initialAttack;
  928. vhi->primSkills[1] = vhi->type->heroClass->initialDefence;
  929. vhi->primSkills[2] = vhi->type->heroClass->initialPower;
  930. vhi->primSkills[3] = vhi->type->heroClass->initialKnowledge;
  931. }
  932. vhi->mana = vhi->primSkills[3]*10;
  933. if (!vhi->name.length())
  934. {
  935. vhi->name = vhi->type->name;
  936. }
  937. if (!vhi->biography.length())
  938. {
  939. vhi->biography = vhi->type->biography;
  940. }
  941. if (vhi->portrait < 0)
  942. vhi->portrait = vhi->type->ID;
  943. vhi->artifWorn[16] = 3;
  944. //initial army
  945. if (!vhi->army.slots.size()) //standard army
  946. {
  947. int pom, pom2=0;
  948. for(int x=0;x<3;x++)
  949. {
  950. pom = (VLC->creh->nameToID[vhi->type->refTypeStack[x]]);
  951. if(pom>=145 && pom<=149) //war machine
  952. {
  953. pom2++;
  954. switch (pom)
  955. {
  956. case 145: //catapult
  957. vhi->artifWorn[16] = 3;
  958. break;
  959. default:
  960. pom-=145;
  961. vhi->artifWorn[13+pom] = 4+pom;
  962. break;
  963. }
  964. continue;
  965. }
  966. vhi->army.slots[x-pom2].first = pom;
  967. if((pom = (vhi->type->highStack[x]-vhi->type->lowStack[x])) > 0)
  968. vhi->army.slots[x-pom2].second = (ran()%pom)+vhi->type->lowStack[x];
  969. else
  970. vhi->army.slots[x-pom2].second = +vhi->type->lowStack[x];
  971. }
  972. }
  973. players[vhi->getOwner()].heroes.push_back(vhi);
  974. }
  975. /*************************FOG**OF**WAR******************************************/
  976. for(std::map<ui8, PlayerState>::iterator k=players.begin(); k!=players.end(); ++k)
  977. {
  978. k->second.fogOfWarMap.resize(map->width);
  979. for(int g=0; g<map->width; ++g)
  980. k->second.fogOfWarMap[g].resize(map->height);
  981. for(int g=-0; g<map->width; ++g)
  982. for(int h=0; h<map->height; ++h)
  983. k->second.fogOfWarMap[g][h].resize(map->twoLevel+1, 0);
  984. for(int g=0; g<map->width; ++g)
  985. for(int h=0; h<map->height; ++h)
  986. for(int v=0; v<map->twoLevel+1; ++v)
  987. k->second.fogOfWarMap[g][h][v] = 0;
  988. for(int xd=0; xd<map->width; ++xd) //revealing part of map around heroes
  989. {
  990. for(int yd=0; yd<map->height; ++yd)
  991. {
  992. for(int ch=0; ch<k->second.heroes.size(); ++ch)
  993. {
  994. int deltaX = (k->second.heroes[ch]->getPosition(false).x-xd)*(k->second.heroes[ch]->getPosition(false).x-xd);
  995. int deltaY = (k->second.heroes[ch]->getPosition(false).y-yd)*(k->second.heroes[ch]->getPosition(false).y-yd);
  996. if(deltaX+deltaY<k->second.heroes[ch]->getSightDistance()*k->second.heroes[ch]->getSightDistance())
  997. k->second.fogOfWarMap[xd][yd][k->second.heroes[ch]->getPosition(false).z] = 1;
  998. }
  999. }
  1000. }
  1001. }
  1002. /****************************TOWNS************************************************/
  1003. for (int i=0;i<map->towns.size();i++)
  1004. {
  1005. CGTownInstance * vti =(map->towns[i]);
  1006. if(!vti->town)
  1007. vti->town = &VLC->townh->towns[vti->subID];
  1008. if (vti->name.length()==0) // if town hasn't name we draw it
  1009. vti->name=vti->town->names[ran()%vti->town->names.size()];
  1010. //init buildings
  1011. if(vti->builtBuildings.find(-50)!=vti->builtBuildings.end()) //give standard set of buildings
  1012. {
  1013. vti->builtBuildings.erase(-50);
  1014. vti->builtBuildings.insert(10);
  1015. vti->builtBuildings.insert(5);
  1016. vti->builtBuildings.insert(30);
  1017. if(ran()%2)
  1018. vti->builtBuildings.insert(31);
  1019. }
  1020. //init spells
  1021. vti->spells.resize(SPELL_LEVELS);
  1022. CSpell *s;
  1023. for(int z=0; z<vti->obligatorySpells.size();z++)
  1024. {
  1025. s = &VLC->spellh->spells[vti->obligatorySpells[z]];
  1026. vti->spells[s->level-1].push_back(s->id);
  1027. vti->possibleSpells -= s->id;
  1028. }
  1029. while(vti->possibleSpells.size())
  1030. {
  1031. ui32 total=0, sel=-1;
  1032. for(int ps=0;ps<vti->possibleSpells.size();ps++)
  1033. total += VLC->spellh->spells[vti->possibleSpells[ps]].probabilities[vti->subID];
  1034. int r = (total)? ran()%total : -1;
  1035. for(int ps=0; ps<vti->possibleSpells.size();ps++)
  1036. {
  1037. r -= VLC->spellh->spells[vti->possibleSpells[ps]].probabilities[vti->subID];
  1038. if(r<0)
  1039. {
  1040. sel = ps;
  1041. break;
  1042. }
  1043. }
  1044. if(sel<0)
  1045. sel=0;
  1046. CSpell *s = &VLC->spellh->spells[vti->possibleSpells[sel]];
  1047. vti->spells[s->level-1].push_back(s->id);
  1048. vti->possibleSpells -= s->id;
  1049. }
  1050. players[vti->getOwner()].towns.push_back(vti);
  1051. }
  1052. for(std::map<ui8, PlayerState>::iterator k=players.begin(); k!=players.end(); ++k)
  1053. {
  1054. if(k->first==-1 || k->first==255)
  1055. continue;
  1056. for(int xd=0; xd<map->width; ++xd) //revealing part of map around towns
  1057. {
  1058. for(int yd=0; yd<map->height; ++yd)
  1059. {
  1060. for(int ch=0; ch<k->second.towns.size(); ++ch)
  1061. {
  1062. int deltaX = (k->second.towns[ch]->pos.x-xd)*(k->second.towns[ch]->pos.x-xd);
  1063. int deltaY = (k->second.towns[ch]->pos.y-yd)*(k->second.towns[ch]->pos.y-yd);
  1064. if(deltaX+deltaY<k->second.towns[ch]->getSightDistance()*k->second.towns[ch]->getSightDistance())
  1065. k->second.fogOfWarMap[xd][yd][k->second.towns[ch]->pos.z] = 1;
  1066. }
  1067. }
  1068. }
  1069. //init visiting and garrisoned heroes
  1070. for(int l=0; l<k->second.heroes.size();l++)
  1071. {
  1072. for(int m=0; m<k->second.towns.size();m++)
  1073. {
  1074. int3 vistile = k->second.towns[m]->pos; vistile.x--; //tile next to the entrance
  1075. if(vistile == k->second.heroes[l]->pos || k->second.heroes[l]->pos==k->second.towns[m]->pos)
  1076. {
  1077. k->second.towns[m]->visitingHero = k->second.heroes[l];
  1078. k->second.heroes[l]->visitedTown = k->second.towns[m];
  1079. k->second.heroes[l]->inTownGarrison = false;
  1080. if(k->second.heroes[l]->pos==k->second.towns[m]->pos)
  1081. k->second.heroes[l]->pos.x -= 1;
  1082. break;
  1083. }
  1084. //else if(k->second.heroes[l]->pos == k->second.towns[m]->pos)
  1085. //{
  1086. // k->second.towns[m]->garrisonHero = k->second.heroes[l];
  1087. // k->second.towns[m]->army = k->second.heroes[l]->army;
  1088. // k->second.heroes[l]->visitedTown = k->second.towns[m];
  1089. // k->second.heroes[l]->inTownGarrison = true;
  1090. // k->second.heroes[l]->pos.x -= 1;
  1091. // goto mainplheloop;
  1092. //}
  1093. }
  1094. }
  1095. }
  1096. }
  1097. bool CGameState::battleShootCreatureStack(int ID, int dest)
  1098. {
  1099. return true;
  1100. }
  1101. int CGameState::battleGetStack(int pos)
  1102. {
  1103. for(int g=0; g<curB->stacks.size(); ++g)
  1104. {
  1105. if(curB->stacks[g]->position == pos ||
  1106. ( curB->stacks[g]->creature->isDoubleWide() &&
  1107. ( (curB->stacks[g]->attackerOwned && curB->stacks[g]->position-1 == pos) ||
  1108. (!curB->stacks[g]->attackerOwned && curB->stacks[g]->position-1 == pos)
  1109. )
  1110. )
  1111. )
  1112. return curB->stacks[g]->ID;
  1113. }
  1114. return -1;
  1115. }
  1116. UpgradeInfo CGameState::getUpgradeInfo(CArmedInstance *obj, int stackPos)
  1117. {
  1118. UpgradeInfo ret;
  1119. CCreature *base = &VLC->creh->creatures[obj->army.slots[stackPos].first];
  1120. if((obj->ID == 98) || ((obj->ID == 34) && static_cast<const CGHeroInstance*>(obj)->visitedTown))
  1121. {
  1122. CGTownInstance * t;
  1123. if(obj->ID == 98)
  1124. t = static_cast<CGTownInstance *>(const_cast<CArmedInstance *>(obj));
  1125. else
  1126. t = static_cast<const CGHeroInstance*>(obj)->visitedTown;
  1127. for(std::set<si32>::iterator i=t->builtBuildings.begin(); i!=t->builtBuildings.end(); i++)
  1128. {
  1129. if( (*i) >= 37 && (*i) < 44 ) //upgraded creature dwelling
  1130. {
  1131. int nid = t->town->upgradedCreatures[(*i)-37]; //upgrade offered by that building
  1132. if(base->upgrades.find(nid) != base->upgrades.end()) //possible upgrade
  1133. {
  1134. ret.newID.push_back(nid);
  1135. ret.cost.push_back(std::set<std::pair<int,int> >());
  1136. for(int j=0;j<RESOURCE_QUANTITY;j++)
  1137. {
  1138. int dif = VLC->creh->creatures[nid].cost[j] - base->cost[j];
  1139. if(dif)
  1140. ret.cost[ret.cost.size()-1].insert(std::make_pair(j,dif));
  1141. }
  1142. }
  1143. }
  1144. }//end for
  1145. }
  1146. //TODO: check if hero ability makes some upgrades possible
  1147. if(ret.newID.size())
  1148. ret.oldID = base->idNumber;
  1149. return ret;
  1150. }
  1151. int BattleInfo::calculateDmg(const CStack* attacker, const CStack* defender)
  1152. {
  1153. int attackDefenseBonus = attacker->creature->attack - defender->creature->defence;
  1154. int damageBase = 0;
  1155. if(attacker->creature->damageMax == attacker->creature->damageMin) //constant damage
  1156. {
  1157. damageBase = attacker->creature->damageMin;
  1158. }
  1159. else
  1160. {
  1161. damageBase = rand()%(attacker->creature->damageMax - attacker->creature->damageMin) + attacker->creature->damageMin + 1;
  1162. }
  1163. float dmgBonusMultiplier = 1.0;
  1164. if(attackDefenseBonus < 0) //decreasing dmg
  1165. {
  1166. if(0.02f * (-attackDefenseBonus) > 0.3f)
  1167. {
  1168. dmgBonusMultiplier += -0.3f;
  1169. }
  1170. else
  1171. {
  1172. dmgBonusMultiplier += 0.02f * attackDefenseBonus;
  1173. }
  1174. }
  1175. else //increasing dmg
  1176. {
  1177. if(0.05f * attackDefenseBonus > 4.0f)
  1178. {
  1179. dmgBonusMultiplier += 4.0f;
  1180. }
  1181. else
  1182. {
  1183. dmgBonusMultiplier += 0.05f * attackDefenseBonus;
  1184. }
  1185. }
  1186. return (float)damageBase * (float)attacker->amount * dmgBonusMultiplier;
  1187. }