CGameState.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999
  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/CHeroHandler.h"
  11. #include "hch/CObjectHandler.h"
  12. #include "hch/CCreatureHandler.h"
  13. #include "lib/VCMI_Lib.h"
  14. #include "map.h"
  15. #include "StartInfo.h"
  16. #include "lib/NetPacks.h"
  17. #include <boost/foreach.hpp>
  18. #include <boost/thread.hpp>
  19. #include <boost/thread/shared_mutex.hpp>
  20. boost::rand48 ran;
  21. CGObjectInstance * createObject(int id, int subid, int3 pos, int owner)
  22. {
  23. CGObjectInstance * nobj;
  24. switch(id)
  25. {
  26. case 34: //hero
  27. {
  28. CGHeroInstance * nobj;
  29. nobj = new CGHeroInstance();
  30. nobj->pos = pos;
  31. nobj->tempOwner = owner;
  32. nobj->defInfo = new CGDefInfo();
  33. nobj->defInfo->id = 34;
  34. nobj->defInfo->subid = subid;
  35. nobj->defInfo->printPriority = 0;
  36. nobj->type = VLC->heroh->heroes[subid];
  37. for(int i=0;i<6;i++)
  38. {
  39. nobj->defInfo->blockMap[i]=255;
  40. nobj->defInfo->visitMap[i]=0;
  41. }
  42. nobj->ID = id;
  43. nobj->subID = subid;
  44. nobj->defInfo->handler=NULL;
  45. nobj->defInfo->blockMap[5] = 253;
  46. nobj->defInfo->visitMap[5] = 2;
  47. nobj->artifacts.resize(20);
  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::apply(IPack * pack)
  240. {
  241. mx->lock();
  242. switch(pack->getType())
  243. {
  244. case 101://NewTurn
  245. {
  246. NewTurn * n = static_cast<NewTurn*>(pack);
  247. day = n->day;
  248. BOOST_FOREACH(NewTurn::Hero h, n->heroes) //give mana/movement point
  249. {
  250. static_cast<CGHeroInstance*>(map->objects[h.id])->movement = h.move;
  251. static_cast<CGHeroInstance*>(map->objects[h.id])->mana = h.mana;
  252. }
  253. BOOST_FOREACH(NewTurn::Resources h, n->res) //give resources
  254. {
  255. for(int i=0;i<RESOURCE_QUANTITY;i++)
  256. players[h.player].resources[i] = h.resources[i];
  257. }
  258. if(n->resetBuilded) //reset amount of structures set in this turn in towns
  259. BOOST_FOREACH(CGTownInstance* t, map->towns)
  260. t->builded = 0;
  261. break;
  262. }
  263. case 102: //set resource amount
  264. {
  265. SetResource *sr = static_cast<SetResource*>(pack);
  266. players[sr->player].resources[sr->resid] = sr->val;
  267. break;
  268. }
  269. case 104:
  270. {
  271. SetResources *sr = static_cast<SetResources*>(pack);
  272. for(int i=0;i<sr->res.size();i++)
  273. players[sr->player].resources[i] = sr->res[i];
  274. break;
  275. }
  276. case 105:
  277. {
  278. SetPrimSkill *sr = static_cast<SetPrimSkill*>(pack);
  279. CGHeroInstance *hero = getHero(sr->id);
  280. if(sr->which <4)
  281. {
  282. if(sr->abs)
  283. hero->primSkills[sr->which] = sr->val;
  284. else
  285. hero->primSkills[sr->which] += sr->val;
  286. }
  287. else if(sr->which == 4) //XP
  288. {
  289. if(sr->abs)
  290. hero->exp = sr->val;
  291. else
  292. hero->exp += sr->val;
  293. }
  294. break;
  295. }
  296. case 500:
  297. {
  298. RemoveHero *rh = static_cast<RemoveHero*>(pack);
  299. CGHeroInstance *h = static_cast<CGHeroInstance*>(map->objects[rh->id]);
  300. std::vector<CGHeroInstance*>::iterator nitr = std::find(map->heroes.begin(), map->heroes.end(),h);
  301. map->heroes.erase(nitr);
  302. int player = h->tempOwner;
  303. nitr = std::find(players[player].heroes.begin(), players[player].heroes.end(), h);
  304. players[player].heroes.erase(nitr);
  305. map->objects[h->id] = NULL;
  306. break;
  307. }
  308. case 501://hero try-move
  309. {
  310. TryMoveHero * n = static_cast<TryMoveHero*>(pack);
  311. CGHeroInstance *h = static_cast<CGHeroInstance*>(map->objects[n->id]);
  312. h->movement = n->movePoints;
  313. if(n->result)
  314. h->pos = n->end;
  315. else
  316. h->pos = n->start;
  317. BOOST_FOREACH(int3 t, n->fowRevealed)
  318. players[h->getOwner()].fogOfWarMap[t.x][t.y][t.z] = 1;
  319. break;
  320. }
  321. case 502:
  322. {
  323. SetGarrisons * n = static_cast<SetGarrisons*>(pack);
  324. for(std::map<ui32,CCreatureSet>::iterator i = n->garrs.begin(); i!=n->garrs.end(); i++)
  325. static_cast<CArmedInstance*>(map->objects[i->first])->army = i->second;
  326. break;
  327. }
  328. case 503:
  329. {
  330. SetStrInfo *ssi = static_cast<SetStrInfo*>(pack);
  331. static_cast<CGTownInstance*>(map->objects[ssi->tid])->strInfo.creatures = ssi->cres;
  332. break;
  333. }
  334. case 504:
  335. {
  336. NewStructures *ns = static_cast<NewStructures*>(pack);
  337. CGTownInstance*t = static_cast<CGTownInstance*>(map->objects[ns->tid]);
  338. BOOST_FOREACH(si32 bid,ns->bid)
  339. t->builtBuildings.insert(bid);
  340. t->builded = ns->builded;
  341. break;
  342. }
  343. case 1001://set object property
  344. {
  345. SetObjectProperty *p = static_cast<SetObjectProperty*>(pack);
  346. int CGObjectInstance::*point;
  347. switch(p->what)
  348. {
  349. case 1:
  350. point = &CGObjectInstance::tempOwner;
  351. break;
  352. case 2:
  353. point = &CGObjectInstance::blockVisit;
  354. break;
  355. }
  356. map->objects[p->id]->*point = p->val;
  357. break;
  358. }
  359. case 3000:
  360. {
  361. BattleStart * bs = static_cast<BattleStart*>(pack);
  362. curB = bs->info;
  363. break;
  364. }
  365. case 3001:
  366. {
  367. BattleNextRound *ns = static_cast<BattleNextRound*>(pack);
  368. curB->round = ns->round;
  369. break;
  370. }
  371. case 3002:
  372. {
  373. BattleSetActiveStack *ns = static_cast<BattleSetActiveStack*>(pack);
  374. curB->activeStack = ns->stack;
  375. break;
  376. }
  377. case 3003:
  378. {
  379. BattleResult *br = static_cast<BattleResult*>(pack);
  380. //TODO: give exp, artifacts to winner, decrease armies (casualties)
  381. for(unsigned i=0;i<curB->stacks.size();i++)
  382. delete curB->stacks[i];
  383. delete curB;
  384. curB = NULL;
  385. break;
  386. }
  387. case 3004:
  388. {
  389. BattleStackMoved *br = static_cast<BattleStackMoved*>(pack);
  390. curB->getStack(br->stack)->position = br->tile;
  391. break;
  392. }
  393. case 3005:
  394. {
  395. BattleStackAttacked *br = static_cast<BattleStackAttacked*>(pack);
  396. CStack * at = curB->getStack(br->stackAttacked);
  397. at->amount = br->newAmount;
  398. at->firstHPleft = br->newHP;
  399. at->alive = !br->killed();
  400. break;
  401. }
  402. case 3006:
  403. {
  404. BattleAttack *br = static_cast<BattleAttack*>(pack);
  405. mx->unlock();
  406. apply(&br->bsa);
  407. mx->lock();
  408. break;
  409. }
  410. //case 1002://set hover name
  411. // {
  412. // SetHoverName * shn = static_cast<SetHoverName*>(pack);
  413. // map->objects[shn->id]->hoverName = toString(shn->name);
  414. // break;
  415. // }
  416. }
  417. mx->unlock();
  418. }
  419. int CGameState::pickHero(int owner)
  420. {
  421. int h=-1;
  422. if(map->getHero(h = scenarioOps->getIthPlayersSettings(owner).hero,0) && h>=0) //we haven't used selected hero
  423. return h;
  424. int f = scenarioOps->getIthPlayersSettings(owner).castle;
  425. int i=0;
  426. do //try to find free hero of our faction
  427. {
  428. i++;
  429. h = scenarioOps->getIthPlayersSettings(owner).castle*HEROES_PER_TYPE*2+(ran()%(HEROES_PER_TYPE*2));//->scenarioOps->playerInfos[pru].hero = VLC->
  430. } while( map->getHero(h) && i<175);
  431. if(i>174) //probably no free heroes - there's no point in further search, we'll take first free
  432. {
  433. std::cout << "Warning: cannot find free hero - trying to get first available..."<<std::endl;
  434. for(int j=0; j<HEROES_PER_TYPE * 2 * F_NUMBER; j++)
  435. if(!map->getHero(j))
  436. h=j;
  437. }
  438. return h;
  439. }
  440. CGHeroInstance *CGameState::getHero(int objid)
  441. {
  442. if(objid<0 || objid>=map->objects.size())
  443. return NULL;
  444. return static_cast<CGHeroInstance *>(map->objects[objid]);
  445. }
  446. std::pair<int,int> CGameState::pickObject(CGObjectInstance *obj)
  447. {
  448. switch(obj->ID)
  449. {
  450. case 65: //random artifact
  451. return std::pair<int,int>(5,(ran()%136)+7); //tylko sensowny zakres - na poczatku sa katapulty itp, na koncu specjalne i blanki
  452. case 66: //random treasure artifact
  453. return std::pair<int,int>(5,VLC->arth->treasures[ran()%VLC->arth->treasures.size()]->id);
  454. case 67: //random minor artifact
  455. return std::pair<int,int>(5,VLC->arth->minors[ran()%VLC->arth->minors.size()]->id);
  456. case 68: //random major artifact
  457. return std::pair<int,int>(5,VLC->arth->majors[ran()%VLC->arth->majors.size()]->id);
  458. case 69: //random relic artifact
  459. return std::pair<int,int>(5,VLC->arth->relics[ran()%VLC->arth->relics.size()]->id);
  460. case 70: //random hero
  461. {
  462. return std::pair<int,int>(34,pickHero(obj->tempOwner));
  463. }
  464. case 71: //random monster
  465. return std::pair<int,int>(54,ran()%(VLC->creh->creatures.size()));
  466. case 72: //random monster lvl1
  467. return std::pair<int,int>(54,VLC->creh->levelCreatures[1][ran()%VLC->creh->levelCreatures[1].size()]->idNumber);
  468. case 73: //random monster lvl2
  469. return std::pair<int,int>(54,VLC->creh->levelCreatures[2][ran()%VLC->creh->levelCreatures[2].size()]->idNumber);
  470. case 74: //random monster lvl3
  471. return std::pair<int,int>(54,VLC->creh->levelCreatures[3][ran()%VLC->creh->levelCreatures[3].size()]->idNumber);
  472. case 75: //random monster lvl4
  473. return std::pair<int,int>(54,VLC->creh->levelCreatures[4][ran()%VLC->creh->levelCreatures[4].size()]->idNumber);
  474. case 76: //random resource
  475. return std::pair<int,int>(79,ran()%7); //now it's OH3 style, use %8 for mithril
  476. case 77: //random town
  477. {
  478. int align = ((CGTownInstance*)obj)->alignment,
  479. f;
  480. if(align>PLAYER_LIMIT-1)//same as owner / random
  481. {
  482. if(obj->tempOwner > PLAYER_LIMIT-1)
  483. f = -1; //random
  484. else
  485. f = scenarioOps->getIthPlayersSettings(obj->tempOwner).castle;
  486. }
  487. else
  488. {
  489. f = scenarioOps->getIthPlayersSettings(align).castle;
  490. }
  491. if(f<0) f = ran()%VLC->townh->towns.size();
  492. return std::pair<int,int>(98,f);
  493. }
  494. case 162: //random monster lvl5
  495. return std::pair<int,int>(54,VLC->creh->levelCreatures[5][ran()%VLC->creh->levelCreatures[5].size()]->idNumber);
  496. case 163: //random monster lvl6
  497. return std::pair<int,int>(54,VLC->creh->levelCreatures[6][ran()%VLC->creh->levelCreatures[6].size()]->idNumber);
  498. case 164: //random monster lvl7
  499. return std::pair<int,int>(54,VLC->creh->levelCreatures[7][ran()%VLC->creh->levelCreatures[7].size()]->idNumber);
  500. case 216: //random dwelling
  501. {
  502. int faction = ran()%F_NUMBER;
  503. CCreGen2ObjInfo* info =(CCreGen2ObjInfo*)obj->info;
  504. if (info->asCastle)
  505. {
  506. for(int i=0;i<map->objects.size();i++)
  507. {
  508. if(map->objects[i]->ID==77 && dynamic_cast<CGTownInstance*>(map->objects[i])->identifier == info->identifier)
  509. {
  510. randomizeObject(map->objects[i]); //we have to randomize the castle first
  511. faction = map->objects[i]->subID;
  512. break;
  513. }
  514. else if(map->objects[i]->ID==98 && dynamic_cast<CGTownInstance*>(map->objects[i])->identifier == info->identifier)
  515. {
  516. faction = map->objects[i]->subID;
  517. break;
  518. }
  519. }
  520. }
  521. else
  522. {
  523. while((!(info->castles[0]&(1<<faction))))
  524. {
  525. if((faction>7) && (info->castles[1]&(1<<(faction-8))))
  526. break;
  527. faction = ran()%F_NUMBER;
  528. }
  529. }
  530. int level = ((info->maxLevel-info->minLevel) ? (ran()%(info->maxLevel-info->minLevel)+info->minLevel) : (info->minLevel));
  531. int cid = VLC->townh->towns[faction].basicCreatures[level];
  532. for(int i=0;i<VLC->objh->cregens.size();i++)
  533. if(VLC->objh->cregens[i]==cid)
  534. return std::pair<int,int>(17,i);
  535. std::cout << "Cannot find a dwelling for creature "<<cid <<std::endl;
  536. return std::pair<int,int>(17,0);
  537. }
  538. case 217:
  539. {
  540. int faction = ran()%F_NUMBER;
  541. CCreGenObjInfo* info =(CCreGenObjInfo*)obj->info;
  542. if (info->asCastle)
  543. {
  544. for(int i=0;i<map->objects.size();i++)
  545. {
  546. if(map->objects[i]->ID==77 && dynamic_cast<CGTownInstance*>(map->objects[i])->identifier == info->identifier)
  547. {
  548. randomizeObject(map->objects[i]); //we have to randomize the castle first
  549. faction = map->objects[i]->subID;
  550. break;
  551. }
  552. else if(map->objects[i]->ID==98 && dynamic_cast<CGTownInstance*>(map->objects[i])->identifier == info->identifier)
  553. {
  554. faction = map->objects[i]->subID;
  555. break;
  556. }
  557. }
  558. }
  559. else
  560. {
  561. while((!(info->castles[0]&(1<<faction))))
  562. {
  563. if((faction>7) && (info->castles[1]&(1<<(faction-8))))
  564. break;
  565. faction = ran()%F_NUMBER;
  566. }
  567. }
  568. int cid = VLC->townh->towns[faction].basicCreatures[obj->subID];
  569. for(int i=0;i<VLC->objh->cregens.size();i++)
  570. if(VLC->objh->cregens[i]==cid)
  571. return std::pair<int,int>(17,i);
  572. std::cout << "Cannot find a dwelling for creature "<<cid <<std::endl;
  573. return std::pair<int,int>(17,0);
  574. }
  575. case 218:
  576. {
  577. CCreGen3ObjInfo* info =(CCreGen3ObjInfo*)obj->info;
  578. int level = ((info->maxLevel-info->minLevel) ? (ran()%(info->maxLevel-info->minLevel)+info->minLevel) : (info->minLevel));
  579. int cid = VLC->townh->towns[obj->subID].basicCreatures[level];
  580. for(int i=0;i<VLC->objh->cregens.size();i++)
  581. if(VLC->objh->cregens[i]==cid)
  582. return std::pair<int,int>(17,i);
  583. std::cout << "Cannot find a dwelling for creature "<<cid <<std::endl;
  584. return std::pair<int,int>(17,0);
  585. }
  586. }
  587. return std::pair<int,int>(-1,-1);
  588. }
  589. void CGameState::randomizeObject(CGObjectInstance *cur)
  590. {
  591. std::pair<int,int> ran = pickObject(cur);
  592. if(ran.first<0 || ran.second<0) //this is not a random object, or we couldn't find anything
  593. {
  594. if(cur->ID==98) //town - set def
  595. {
  596. CGTownInstance *t = dynamic_cast<CGTownInstance*>(cur);
  597. if(t->hasCapitol())
  598. t->defInfo = capitols[t->subID];
  599. else if(t->hasFort())
  600. t->defInfo = forts[t->subID];
  601. else
  602. t->defInfo = villages[t->subID];
  603. }
  604. return;
  605. }
  606. else if(ran.first==34)//special code for hero
  607. {
  608. CGHeroInstance *h = dynamic_cast<CGHeroInstance *>(cur);
  609. if(!h) {std::cout<<"Wrong random hero at "<<cur->pos<<std::endl; return;}
  610. cur->ID = ran.first;
  611. h->portrait = cur->subID = ran.second;
  612. h->type = VLC->heroh->heroes[ran.second];
  613. map->heroes.push_back(h);
  614. return; //TODO: maybe we should do something with definfo?
  615. }
  616. else if(ran.first==98)//special code for town
  617. {
  618. CGTownInstance *t = dynamic_cast<CGTownInstance*>(cur);
  619. if(!t) {std::cout<<"Wrong random town at "<<cur->pos<<std::endl; return;}
  620. cur->ID = ran.first;
  621. cur->subID = ran.second;
  622. t->town = &VLC->townh->towns[ran.second];
  623. if(t->hasCapitol())
  624. t->defInfo = capitols[t->subID];
  625. else if(t->hasFort())
  626. t->defInfo = forts[t->subID];
  627. else
  628. t->defInfo = villages[t->subID];
  629. map->towns.push_back(t);
  630. return;
  631. }
  632. //we have to replace normal random object
  633. cur->ID = ran.first;
  634. cur->subID = ran.second;
  635. map->defs.insert(cur->defInfo = VLC->dobjinfo->gobjs[ran.first][ran.second]);
  636. if(!cur->defInfo)
  637. {
  638. std::cout<<"Missing def declaration for "<<cur->ID<<" "<<cur->subID<<std::endl;
  639. return;
  640. }
  641. }
  642. int CGameState::getDate(int mode) const
  643. {
  644. int temp;
  645. switch (mode)
  646. {
  647. case 0:
  648. return day;
  649. break;
  650. case 1:
  651. temp = (day)%7;
  652. if (temp)
  653. return temp;
  654. else return 7;
  655. break;
  656. case 2:
  657. temp = ((day-1)/7)+1;
  658. if (!(temp%4))
  659. return 4;
  660. else
  661. return (temp%4);
  662. break;
  663. case 3:
  664. return ((day-1)/28)+1;
  665. break;
  666. }
  667. return 0;
  668. }
  669. CGameState::CGameState()
  670. {
  671. mx = new boost::shared_mutex();
  672. }
  673. CGameState::~CGameState()
  674. {
  675. delete mx;
  676. }
  677. void CGameState::init(StartInfo * si, Mapa * map, int Seed)
  678. {
  679. day = 0;
  680. seed = Seed;
  681. ran.seed((boost::int32_t)seed);
  682. scenarioOps = si;
  683. this->map = map;
  684. for(int i=0;i<F_NUMBER;i++)
  685. {
  686. villages[i] = new CGDefInfo(*VLC->dobjinfo->castles[i]);
  687. forts[i] = VLC->dobjinfo->castles[i];
  688. capitols[i] = new CGDefInfo(*VLC->dobjinfo->castles[i]);
  689. }
  690. //picking random factions for players
  691. for(int i=0;i<scenarioOps->playerInfos.size();i++)
  692. {
  693. if(scenarioOps->playerInfos[i].castle==-1)
  694. {
  695. int f;
  696. do
  697. {
  698. f = ran()%F_NUMBER;
  699. }while(!(map->players[scenarioOps->playerInfos[i].color].allowedFactions & 1<<f));
  700. scenarioOps->playerInfos[i].castle = f;
  701. }
  702. }
  703. //randomizing objects
  704. for(int no=0; no<map->objects.size(); ++no)
  705. {
  706. randomizeObject(map->objects[no]);
  707. if(map->objects[no]->ID==26)
  708. map->objects[no]->defInfo->handler=NULL;
  709. map->objects[no]->hoverName = VLC->objh->names[map->objects[no]->ID];
  710. }
  711. //std::cout<<"\tRandomizing objects: "<<th.getDif()<<std::endl;
  712. //giving starting hero
  713. for(int i=0;i<PLAYER_LIMIT;i++)
  714. {
  715. if((map->players[i].generateHeroAtMainTown && map->players[i].hasMainTown) || (map->players[i].hasMainTown && map->version==RoE))
  716. {
  717. int3 hpos = map->players[i].posOfMainTown;
  718. hpos.x+=1;// hpos.y+=1;
  719. int j;
  720. for(j=0; j<scenarioOps->playerInfos.size(); j++)
  721. if(scenarioOps->playerInfos[j].color == i)
  722. break;
  723. if(j == scenarioOps->playerInfos.size())
  724. continue;
  725. int h=pickHero(i);
  726. CGHeroInstance * nnn = static_cast<CGHeroInstance*>(createObject(34,h,hpos,i));
  727. nnn->id = map->objects.size();
  728. //nnn->defInfo->handler = graphics->flags1[0];
  729. map->heroes.push_back(nnn);
  730. map->objects.push_back(nnn);
  731. }
  732. }
  733. //std::cout<<"\tGiving starting heroes: "<<th.getDif()<<std::endl;
  734. /*********creating players entries in gs****************************************/
  735. for (int i=0; i<scenarioOps->playerInfos.size();i++)
  736. {
  737. std::pair<int,PlayerState> ins(scenarioOps->playerInfos[i].color,PlayerState());
  738. ins.second.color=ins.first;
  739. ins.second.serial=i;
  740. players.insert(ins);
  741. }
  742. /******************RESOURCES****************************************************/
  743. //TODO: zeby komputer dostawal inaczej niz gracz
  744. std::vector<int> startres;
  745. std::ifstream tis("config/startres.txt");
  746. int k;
  747. for (int j=0;j<scenarioOps->difficulty;j++)
  748. {
  749. tis >> k;
  750. for (int z=0;z<RESOURCE_QUANTITY;z++)
  751. tis>>k;
  752. }
  753. tis >> k;
  754. for (int i=0;i<RESOURCE_QUANTITY;i++)
  755. {
  756. tis >> k;
  757. startres.push_back(k);
  758. }
  759. tis.close();
  760. for (std::map<ui8,PlayerState>::iterator i = players.begin(); i!=players.end(); i++)
  761. {
  762. (*i).second.resources.resize(RESOURCE_QUANTITY);
  763. for (int x=0;x<RESOURCE_QUANTITY;x++)
  764. (*i).second.resources[x] = startres[x];
  765. }
  766. /*************************HEROES************************************************/
  767. for (int i=0; i<map->heroes.size();i++) //heroes instances
  768. {
  769. if (map->heroes[i]->getOwner()<0)
  770. continue;
  771. CGHeroInstance * vhi = (map->heroes[i]);
  772. if(!vhi->type)
  773. vhi->type = VLC->heroh->heroes[vhi->subID];
  774. //vhi->subID = vhi->type->ID;
  775. if (vhi->level<1)
  776. {
  777. vhi->exp=40+ran()%50;
  778. vhi->level = 1;
  779. }
  780. if (vhi->level>1) ;//TODO dodac um dr, ale potrzebne los
  781. if ((!vhi->primSkills.size()) || (vhi->primSkills[0]<0))
  782. {
  783. if (vhi->primSkills.size()<PRIMARY_SKILLS)
  784. vhi->primSkills.resize(PRIMARY_SKILLS);
  785. vhi->primSkills[0] = vhi->type->heroClass->initialAttack;
  786. vhi->primSkills[1] = vhi->type->heroClass->initialDefence;
  787. vhi->primSkills[2] = vhi->type->heroClass->initialPower;
  788. vhi->primSkills[3] = vhi->type->heroClass->initialKnowledge;
  789. }
  790. vhi->mana = vhi->primSkills[3]*10;
  791. if (!vhi->name.length())
  792. {
  793. vhi->name = vhi->type->name;
  794. }
  795. if (!vhi->biography.length())
  796. {
  797. vhi->biography = vhi->type->biography;
  798. }
  799. if (vhi->portrait < 0)
  800. vhi->portrait = vhi->type->ID;
  801. //initial army
  802. if (!vhi->army.slots.size()) //standard army
  803. {
  804. int pom, pom2=0;
  805. for(int x=0;x<3;x++)
  806. {
  807. pom = (VLC->creh->nameToID[vhi->type->refTypeStack[x]]);
  808. if(pom>=145 && pom<=149) //war machine
  809. {
  810. pom2++;
  811. switch (pom)
  812. {
  813. case 145: //catapult
  814. vhi->artifWorn[16] = 3;
  815. break;
  816. default:
  817. pom-=145;
  818. vhi->artifWorn[13+pom] = 4+pom;
  819. break;
  820. }
  821. continue;
  822. }
  823. vhi->army.slots[x-pom2].first = pom;
  824. if((pom = (vhi->type->highStack[x]-vhi->type->lowStack[x])) > 0)
  825. vhi->army.slots[x-pom2].second = (ran()%pom)+vhi->type->lowStack[x];
  826. else
  827. vhi->army.slots[x-pom2].second = +vhi->type->lowStack[x];
  828. }
  829. }
  830. players[vhi->getOwner()].heroes.push_back(vhi);
  831. }
  832. /*************************FOG**OF**WAR******************************************/
  833. for(std::map<ui8, PlayerState>::iterator k=players.begin(); k!=players.end(); ++k)
  834. {
  835. k->second.fogOfWarMap.resize(map->width);
  836. for(int g=0; g<map->width; ++g)
  837. k->second.fogOfWarMap[g].resize(map->height);
  838. for(int g=-0; g<map->width; ++g)
  839. for(int h=0; h<map->height; ++h)
  840. k->second.fogOfWarMap[g][h].resize(map->twoLevel+1, 0);
  841. for(int g=0; g<map->width; ++g)
  842. for(int h=0; h<map->height; ++h)
  843. for(int v=0; v<map->twoLevel+1; ++v)
  844. k->second.fogOfWarMap[g][h][v] = 0;
  845. for(int xd=0; xd<map->width; ++xd) //revealing part of map around heroes
  846. {
  847. for(int yd=0; yd<map->height; ++yd)
  848. {
  849. for(int ch=0; ch<k->second.heroes.size(); ++ch)
  850. {
  851. int deltaX = (k->second.heroes[ch]->getPosition(false).x-xd)*(k->second.heroes[ch]->getPosition(false).x-xd);
  852. int deltaY = (k->second.heroes[ch]->getPosition(false).y-yd)*(k->second.heroes[ch]->getPosition(false).y-yd);
  853. if(deltaX+deltaY<k->second.heroes[ch]->getSightDistance()*k->second.heroes[ch]->getSightDistance())
  854. k->second.fogOfWarMap[xd][yd][k->second.heroes[ch]->getPosition(false).z] = 1;
  855. }
  856. }
  857. }
  858. }
  859. /****************************TOWNS************************************************/
  860. for (int i=0;i<map->towns.size();i++)
  861. {
  862. CGTownInstance * vti =(map->towns[i]);
  863. if(!vti->town)
  864. vti->town = &VLC->townh->towns[vti->subID];
  865. if (vti->name.length()==0) // if town hasn't name we draw it
  866. vti->name=vti->town->names[ran()%vti->town->names.size()];
  867. if(vti->builtBuildings.find(-50)!=vti->builtBuildings.end()) //give standard set of buildings
  868. {
  869. vti->builtBuildings.erase(-50);
  870. vti->builtBuildings.insert(10);
  871. vti->builtBuildings.insert(5);
  872. vti->builtBuildings.insert(30);
  873. if(ran()%2)
  874. vti->builtBuildings.insert(31);
  875. }
  876. players[vti->getOwner()].towns.push_back(vti);
  877. }
  878. for(std::map<ui8, PlayerState>::iterator k=players.begin(); k!=players.end(); ++k)
  879. {
  880. if(k->first==-1 || k->first==255)
  881. continue;
  882. for(int xd=0; xd<map->width; ++xd) //revealing part of map around towns
  883. {
  884. for(int yd=0; yd<map->height; ++yd)
  885. {
  886. for(int ch=0; ch<k->second.towns.size(); ++ch)
  887. {
  888. int deltaX = (k->second.towns[ch]->pos.x-xd)*(k->second.towns[ch]->pos.x-xd);
  889. int deltaY = (k->second.towns[ch]->pos.y-yd)*(k->second.towns[ch]->pos.y-yd);
  890. if(deltaX+deltaY<k->second.towns[ch]->getSightDistance()*k->second.towns[ch]->getSightDistance())
  891. k->second.fogOfWarMap[xd][yd][k->second.towns[ch]->pos.z] = 1;
  892. }
  893. }
  894. }
  895. //init visiting heroes
  896. for(int l=0; l<k->second.heroes.size();l++)
  897. {
  898. for(int m=0; m<k->second.towns.size();m++)
  899. {
  900. int3 vistile = k->second.towns[m]->pos; vistile.x--; //tile next to the entrance
  901. if(vistile == k->second.heroes[l]->pos)
  902. {
  903. k->second.towns[m]->visitingHero = k->second.heroes[l];
  904. break;
  905. }
  906. }
  907. }
  908. }
  909. }
  910. bool CGameState::battleShootCreatureStack(int ID, int dest)
  911. {
  912. return true;
  913. }
  914. int CGameState::battleGetStack(int pos)
  915. {
  916. for(int g=0; g<curB->stacks.size(); ++g)
  917. {
  918. if(curB->stacks[g]->position == pos ||
  919. ( curB->stacks[g]->creature->isDoubleWide() &&
  920. ( (curB->stacks[g]->attackerOwned && curB->stacks[g]->position-1 == pos) ||
  921. (!curB->stacks[g]->attackerOwned && curB->stacks[g]->position-1 == pos)
  922. )
  923. )
  924. )
  925. return curB->stacks[g]->ID;
  926. }
  927. return -1;
  928. }
  929. int BattleInfo::calculateDmg(const CStack* attacker, const CStack* defender)
  930. {
  931. int attackDefenseBonus = attacker->creature->attack - defender->creature->defence;
  932. int damageBase = 0;
  933. if(attacker->creature->damageMax == attacker->creature->damageMin) //constant damage
  934. {
  935. damageBase = attacker->creature->damageMin;
  936. }
  937. else
  938. {
  939. damageBase = rand()%(attacker->creature->damageMax - attacker->creature->damageMin) + attacker->creature->damageMin + 1;
  940. }
  941. float dmgBonusMultiplier = 1.0;
  942. if(attackDefenseBonus < 0) //decreasing dmg
  943. {
  944. if(0.02f * (-attackDefenseBonus) > 0.3f)
  945. {
  946. dmgBonusMultiplier += -0.3f;
  947. }
  948. else
  949. {
  950. dmgBonusMultiplier += 0.02f * attackDefenseBonus;
  951. }
  952. }
  953. else //increasing dmg
  954. {
  955. if(0.05f * attackDefenseBonus > 4.0f)
  956. {
  957. dmgBonusMultiplier += 4.0f;
  958. }
  959. else
  960. {
  961. dmgBonusMultiplier += 0.05f * attackDefenseBonus;
  962. }
  963. }
  964. return (float)damageBase * (float)attacker->amount * dmgBonusMultiplier;
  965. }