CGameHandler.cpp 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268
  1. #include <boost/foreach.hpp>
  2. #include <boost/thread.hpp>
  3. #include <boost/thread/shared_mutex.hpp>
  4. #include <boost/bind.hpp>
  5. #include "CGameHandler.h"
  6. #include "CScriptCallback.h"
  7. #include "../CLua.h"
  8. #include "../CGameState.h"
  9. #include "../StartInfo.h"
  10. #include "../map.h"
  11. #include "../lib/NetPacks.h"
  12. #include "../lib/Connection.h"
  13. #include "../hch/CObjectHandler.h"
  14. #include "../hch/CTownHandler.h"
  15. #include "../hch/CBuildingHandler.h"
  16. #include "../hch/CHeroHandler.h"
  17. #include "boost/date_time/posix_time/posix_time_types.hpp" //no i/o just types
  18. #include "../lib/VCMI_Lib.h"
  19. #include "../lib/CondSh.h"
  20. #ifndef _MSC_VER
  21. #include <boost/thread/xtime.hpp>
  22. #endif
  23. extern bool end2;
  24. #include "../lib/BattleAction.h"
  25. #ifdef min
  26. #undef min
  27. #endif
  28. #ifdef max
  29. #undef max
  30. #endif
  31. #define NEW_ROUND BattleNextRound bnr;\
  32. bnr.round = gs->curB->round + 1;\
  33. sendAndApply(&bnr);
  34. boost::mutex gsm;
  35. ui32 CGameHandler::QID = 1;
  36. CondSh<bool> battleMadeAction;
  37. CondSh<BattleResult *> battleResult(NULL);
  38. std::map<ui32, CFunctionList<void(ui32)> > callbacks; //question id => callback function - for selection dialogs
  39. class CMP_stack
  40. {
  41. public:
  42. bool operator ()(const CStack* a, const CStack* b)
  43. {
  44. return (a->creature->speed)>(b->creature->speed);
  45. }
  46. } cmpst ;
  47. double distance(int3 a, int3 b)
  48. {
  49. return std::sqrt( (double)(a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y) );
  50. }
  51. //bool CGameState::checkFunc(int obid, std::string name)
  52. //{
  53. // if (objscr.find(obid)!=objscr.end())
  54. // {
  55. // if(objscr[obid].find(name)!=objscr[obid].end())
  56. // {
  57. // return true;
  58. // }
  59. // }
  60. // return false;
  61. //}
  62. PlayerStatus PlayerStatuses::operator[](ui8 player)
  63. {
  64. boost::unique_lock<boost::mutex> l(mx);
  65. if(players.find(player) != players.end())
  66. {
  67. return players[player];
  68. }
  69. else
  70. {
  71. throw std::string("No such player!");
  72. }
  73. }
  74. void PlayerStatuses::addPlayer(ui8 player)
  75. {
  76. boost::unique_lock<boost::mutex> l(mx);
  77. players[player];
  78. }
  79. bool PlayerStatuses::hasQueries(ui8 player)
  80. {
  81. boost::unique_lock<boost::mutex> l(mx);
  82. if(players.find(player) != players.end())
  83. {
  84. return players[player].queries.size();
  85. }
  86. else
  87. {
  88. throw std::string("No such player!");
  89. }
  90. }
  91. bool PlayerStatuses::checkFlag(ui8 player, bool PlayerStatus::*flag)
  92. {
  93. boost::unique_lock<boost::mutex> l(mx);
  94. if(players.find(player) != players.end())
  95. {
  96. return players[player].*flag;
  97. }
  98. else
  99. {
  100. throw std::string("No such player!");
  101. }
  102. }
  103. void PlayerStatuses::setFlag(ui8 player, bool PlayerStatus::*flag, bool val)
  104. {
  105. boost::unique_lock<boost::mutex> l(mx);
  106. if(players.find(player) != players.end())
  107. {
  108. players[player].*flag = val;
  109. }
  110. else
  111. {
  112. throw std::string("No such player!");
  113. }
  114. cv.notify_all();
  115. }
  116. void PlayerStatuses::addQuery(ui8 player, ui32 id)
  117. {
  118. boost::unique_lock<boost::mutex> l(mx);
  119. if(players.find(player) != players.end())
  120. {
  121. players[player].queries.insert(id);
  122. }
  123. else
  124. {
  125. throw std::string("No such player!");
  126. }
  127. cv.notify_all();
  128. }
  129. void PlayerStatuses::removeQuery(ui8 player, ui32 id)
  130. {
  131. boost::unique_lock<boost::mutex> l(mx);
  132. if(players.find(player) != players.end())
  133. {
  134. players[player].queries.erase(id);
  135. }
  136. else
  137. {
  138. throw std::string("No such player!");
  139. }
  140. cv.notify_all();
  141. }
  142. void CGameHandler::handleCPPObjS(std::map<int,CCPPObjectScript*> * mapa, CCPPObjectScript * script)
  143. {
  144. std::vector<int> tempv = script->yourObjects();
  145. for (unsigned i=0;i<tempv.size();i++)
  146. {
  147. (*mapa)[tempv[i]]=script;
  148. }
  149. cppscripts.insert(script);
  150. }
  151. template <typename T>
  152. void callWith(std::vector<T> args, boost::function<void(T)> fun, ui32 which)
  153. {
  154. fun(args[which]);
  155. }
  156. void CGameHandler::changeSecSkill(int ID, ui16 which, int val, bool abs)
  157. {
  158. SetSecSkill sps;
  159. sps.id = ID;
  160. sps.which = which;
  161. sps.abs = abs;
  162. sps.val = val;
  163. sendAndApply(&sps);
  164. }
  165. void CGameHandler::changePrimSkill(int ID, int which, int val, bool abs)
  166. {
  167. SetPrimSkill sps;
  168. sps.id = ID;
  169. sps.which = which;
  170. sps.abs = abs;
  171. sps.val = val;
  172. sendAndApply(&sps);
  173. if(which==4) //only for exp - hero may level up
  174. {
  175. CGHeroInstance *hero = static_cast<CGHeroInstance *>(gs->map->objects[ID]);
  176. if(hero->exp >= VLC->heroh->reqExp(hero->level+1)) //new level
  177. {
  178. //give prim skill
  179. std::cout << hero->name <<" got level "<<hero->level<<std::endl;
  180. int r = rand()%100, pom=0, x=0;
  181. int std::pair<int,int>::*g = (hero->level>9) ? (&std::pair<int,int>::second) : (&std::pair<int,int>::first);
  182. for(;x<PRIMARY_SKILLS;x++)
  183. {
  184. pom += hero->type->heroClass->primChance[x].*g;
  185. if(r<pom)
  186. break;
  187. }
  188. std::cout << "Bohater dostaje umiejetnosc pierwszorzedna " << x << " (wynik losowania "<<r<<")"<<std::endl;
  189. SetPrimSkill sps;
  190. sps.id = ID;
  191. sps.which = x;
  192. sps.abs = false;
  193. sps.val = 1;
  194. sendAndApply(&sps);
  195. HeroLevelUp hlu;
  196. hlu.heroid = ID;
  197. hlu.primskill = x;
  198. hlu.level = hero->level+1;
  199. //picking sec. skills for choice
  200. std::set<int> basicAndAdv, expert, none;
  201. for(int i=0;i<SKILL_QUANTITY;i++) none.insert(i);
  202. for(unsigned i=0;i<hero->secSkills.size();i++)
  203. {
  204. if(hero->secSkills[i].second < 2)
  205. basicAndAdv.insert(hero->secSkills[i].first);
  206. else
  207. expert.insert(hero->secSkills[i].first);
  208. none.erase(hero->secSkills[i].first);
  209. }
  210. if(hero->secSkills.size() < hero->type->heroClass->skillLimit) //free skill slot
  211. {
  212. hlu.skills.push_back(hero->type->heroClass->chooseSecSkill(none)); //new skill
  213. }
  214. else
  215. {
  216. int s = hero->type->heroClass->chooseSecSkill(basicAndAdv);
  217. hlu.skills.push_back(s);
  218. basicAndAdv.erase(s);
  219. }
  220. if(basicAndAdv.size())
  221. {
  222. hlu.skills.push_back(hero->type->heroClass->chooseSecSkill(basicAndAdv)); //new skill
  223. }
  224. else if(hero->secSkills.size() < hero->type->heroClass->skillLimit)
  225. {
  226. hlu.skills.push_back(hero->type->heroClass->chooseSecSkill(none)); //new skill
  227. }
  228. boost::function<void(ui32)> callback = boost::function<void(ui32)>(boost::bind(callWith<ui16>,hlu.skills,boost::function<void(ui16)>(boost::bind(&CGameHandler::changeSecSkill,this,ID,_1,1,0)),_1));
  229. applyAndAsk(&hlu,hero->tempOwner,callback); //call changeSecSkill with appropriate args when client responds
  230. }
  231. }
  232. }
  233. void CGameHandler::startBattle(CCreatureSet army1, CCreatureSet army2, int3 tile, CGHeroInstance *hero1, CGHeroInstance *hero2)
  234. {
  235. BattleInfo *curB = new BattleInfo;
  236. setupBattle(curB, tile, army1, army2, hero1, hero2); //battle start
  237. NEW_ROUND;
  238. //tactic round
  239. {
  240. NEW_ROUND;
  241. if( (hero1 && hero1->getSecSkillLevel(19)>=0) ||
  242. ( hero2 && hero2->getSecSkillLevel(19)>=0) )//someone has tactics
  243. {
  244. //TODO: tactic round (round -1)
  245. }
  246. }
  247. //main loop
  248. while(!battleResult.get()) //till the end of the battle ;]
  249. {
  250. NEW_ROUND;
  251. std::vector<CStack*> & stacks = (gs->curB->stacks);
  252. const BattleInfo & curB = *gs->curB;
  253. //stack loop
  254. for(unsigned i=0;i<stacks.size() && !battleResult.get();i++)
  255. {
  256. if(!stacks[i]->alive) continue;//indicates imposiibility of making action for this dead unit
  257. BattleSetActiveStack sas;
  258. sas.stack = stacks[i]->ID;
  259. sendAndApply(&sas);
  260. //wait for response about battle action
  261. boost::unique_lock<boost::mutex> lock(battleMadeAction.mx);
  262. while(!battleMadeAction.data)
  263. battleMadeAction.cond.wait(lock);
  264. battleMadeAction.data = false;
  265. }
  266. }
  267. //unblock engaged players
  268. if(hero1->tempOwner<PLAYER_LIMIT)
  269. states.setFlag(hero1->tempOwner,&PlayerStatus::engagedIntoBattle,false);
  270. if(hero2 && hero2->tempOwner<PLAYER_LIMIT)
  271. states.setFlag(hero2->tempOwner,&PlayerStatus::engagedIntoBattle,false);
  272. //end battle, remove all info, free memory
  273. sendAndApply(battleResult.data);
  274. delete battleResult.data;
  275. //for(int i=0;i<stacks.size();i++)
  276. // delete stacks[i];
  277. //delete curB;
  278. //curB = NULL;
  279. }
  280. void prepareAttack(BattleAttack &bat, CStack *att, CStack *def)
  281. {
  282. bat.stackAttacking = att->ID;
  283. bat.bsa.stackAttacked = def->ID;
  284. bat.bsa.damageAmount = BattleInfo::calculateDmg(att, def);//counting dealt damage
  285. //applying damages
  286. bat.bsa.killedAmount = bat.bsa.damageAmount / def->creature->hitPoints;
  287. unsigned damageFirst = bat.bsa.damageAmount % def->creature->hitPoints;
  288. if( def->firstHPleft <= damageFirst )
  289. {
  290. bat.bsa.killedAmount++;
  291. bat.bsa.newHP = def->firstHPleft + def->creature->hitPoints - damageFirst;
  292. }
  293. else
  294. {
  295. bat.bsa.newHP = def->firstHPleft - damageFirst;
  296. }
  297. if(def->amount <= bat.bsa.killedAmount) //stack killed
  298. {
  299. bat.bsa.newAmount = 0;
  300. bat.bsa.flags |= 1;
  301. }
  302. else
  303. {
  304. bat.bsa.newAmount = def->amount - bat.bsa.killedAmount;
  305. }
  306. }
  307. void CGameHandler::handleConnection(std::set<int> players, CConnection &c)
  308. {
  309. try
  310. {
  311. ui16 pom;
  312. while(!end2)
  313. {
  314. c >> pom;
  315. bool blockvis = false;
  316. switch(pom)
  317. {
  318. case 100: //my interface ended its turn
  319. {
  320. states.setFlag(gs->currentPlayer,&PlayerStatus::makingTurn,false);
  321. break;
  322. }
  323. case 500:
  324. {
  325. si32 id;
  326. c >> id;
  327. RemoveObject rh(id);
  328. sendAndApply(&rh);
  329. break;
  330. }
  331. case 501://interface wants to move hero
  332. {
  333. int3 start, end;
  334. si32 id;
  335. c >> id >> start >> end;
  336. int3 hmpos = end + int3(-1,0,0);
  337. TerrainTile t = gs->map->terrain[hmpos.x][hmpos.y][hmpos.z];
  338. CGHeroInstance *h = static_cast<CGHeroInstance *>(gs->map->objects[id]);
  339. int cost = (int)((double)h->getTileCost(t.tertype,t.malle,t.nuine) * distance(start,end));
  340. TryMoveHero tmh;
  341. tmh.id = id;
  342. tmh.start = start;
  343. tmh.end = end;
  344. tmh.result = 0;
  345. tmh.movePoints = h->movement;
  346. if((h->getOwner() != gs->currentPlayer) || //not turn of that hero
  347. (distance(start,end)>=1.5) || //tiles are not neighouring
  348. (h->movement < cost) || //lack of movement points
  349. (t.tertype == rock) || //rock
  350. (!h->canWalkOnSea() && t.tertype == water) ||
  351. (t.blocked && !t.visitable) ) //tile is blocked andnot visitable
  352. goto fail;
  353. //check if there is blocking visitable object
  354. blockvis = false;
  355. tmh.movePoints = h->movement = (h->movement-cost); //take move points
  356. BOOST_FOREACH(CGObjectInstance *obj, t.visitableObjects)
  357. {
  358. if(obj->blockVisit)
  359. {
  360. blockvis = true;
  361. break;
  362. }
  363. }
  364. //we start moving
  365. if(blockvis)//interaction with blocking object (like resources)
  366. {
  367. sendAndApply(&tmh); //failed to move to that tile but we visit object
  368. BOOST_FOREACH(CGObjectInstance *obj, t.visitableObjects)
  369. {
  370. if (obj->blockVisit)
  371. {
  372. //if(gs->checkFunc(obj->ID,"heroVisit")) //script function
  373. // gs->objscr[obj->ID]["heroVisit"]->onHeroVisit(obj,h->subID);
  374. if(obj->state) //hard-coded function
  375. obj->state->onHeroVisit(obj->id,h->id);
  376. }
  377. }
  378. break;
  379. }
  380. else //normal move
  381. {
  382. tmh.result = 1;
  383. BOOST_FOREACH(CGObjectInstance *obj, gs->map->terrain[start.x-1][start.y][start.z].visitableObjects)
  384. {
  385. //TODO: allow to handle this in script-languages
  386. if(obj->state) //hard-coded function
  387. obj->state->onHeroLeave(obj->id,h->id);
  388. }
  389. //reveal fog of war
  390. int heroSight = h->getSightDistance();
  391. int xbeg = start.x - heroSight - 2;
  392. if(xbeg < 0)
  393. xbeg = 0;
  394. int xend = start.x + heroSight + 2;
  395. if(xend >= gs->map->width)
  396. xend = gs->map->width;
  397. int ybeg = start.y - heroSight - 2;
  398. if(ybeg < 0)
  399. ybeg = 0;
  400. int yend = start.y + heroSight + 2;
  401. if(yend >= gs->map->height)
  402. yend = gs->map->height;
  403. for(int xd=xbeg; xd<xend; ++xd) //revealing part of map around heroes
  404. {
  405. for(int yd=ybeg; yd<yend; ++yd)
  406. {
  407. int deltaX = (hmpos.x-xd)*(hmpos.x-xd);
  408. int deltaY = (hmpos.y-yd)*(hmpos.y-yd);
  409. if(deltaX+deltaY<h->getSightDistance()*h->getSightDistance())
  410. {
  411. if(gs->players[h->getOwner()].fogOfWarMap[xd][yd][hmpos.z] == 0)
  412. {
  413. tmh.fowRevealed.insert(int3(xd,yd,hmpos.z));
  414. }
  415. }
  416. }
  417. }
  418. sendAndApply(&tmh);
  419. BOOST_FOREACH(CGObjectInstance *obj, t.visitableObjects)//call objects if they are visited
  420. {
  421. //if(gs->checkFunc(obj->ID,"heroVisit")) //script function
  422. // gs->objscr[obj->ID]["heroVisit"]->onHeroVisit(obj,h->subID);
  423. if(obj->state) //hard-coded function
  424. obj->state->onHeroVisit(obj->id,h->id);
  425. }
  426. }
  427. break;
  428. fail:
  429. sendAndApply(&tmh);
  430. break;
  431. }
  432. case 502: //swap creatures in garrison
  433. {
  434. ui8 what, p1, p2; si32 id1, id2;
  435. c >> what >> id1 >> p1 >> id2 >> p2;
  436. CArmedInstance *s1 = static_cast<CArmedInstance*>(gs->map->objects[id1]),
  437. *s2 = static_cast<CArmedInstance*>(gs->map->objects[id2]);
  438. CCreatureSet *S1 = &s1->army, *S2 = &s2->army;
  439. if(what==1) //swap
  440. {
  441. int pom = S2->slots[p2].first;
  442. S2->slots[p2].first = S1->slots[p1].first;
  443. S1->slots[p1].first = pom;
  444. int pom2 = S2->slots[p2].second;
  445. S2->slots[p2].second = S1->slots[p1].second;
  446. S1->slots[p1].second = pom2;
  447. if(!S1->slots[p1].second)
  448. S1->slots.erase(p1);
  449. if(!S2->slots[p2].second)
  450. S2->slots.erase(p2);
  451. }
  452. else if(what==2)//merge
  453. {
  454. if(S1->slots[p1].first != S2->slots[p2].first) break; //not same creature
  455. S2->slots[p2].second += S1->slots[p1].second;
  456. S1->slots[p1].first = NULL;
  457. S1->slots[p1].second = 0;
  458. S1->slots.erase(p1);
  459. }
  460. else if(what==3) //split
  461. {
  462. si32 val;
  463. c >> val;
  464. if(S2->slots.find(p2) != S2->slots.end()) break; //slot not free
  465. S2->slots[p2].first = S1->slots[p1].first;
  466. S2->slots[p2].second = val;
  467. S1->slots[p1].second -= val;
  468. if(!S1->slots[p1].second) //if we've moved all creatures
  469. S1->slots.erase(p1);
  470. }
  471. if((s1->ID==34 && !S1->slots.size()) //it's not allowed to take last stack from hero army!
  472. || (s2->ID==34 && !S2->slots.size()))
  473. {
  474. break;
  475. }
  476. SetGarrisons sg;
  477. sg.garrs[id1] = *S1;
  478. if(s1 != s2)
  479. sg.garrs[id2] = *S2;
  480. sendAndApply(&sg);
  481. break;
  482. }
  483. case 503:
  484. {
  485. si32 id;
  486. ui8 pos;
  487. c >> id >> pos;
  488. CArmedInstance *s1 = static_cast<CArmedInstance*>(gs->map->objects[id]);
  489. s1->army.slots.erase(pos);
  490. SetGarrisons sg;
  491. sg.garrs[id] = s1->army;
  492. sendAndApply(&sg);
  493. break;
  494. }
  495. case 504:
  496. {
  497. si32 tid, bid;
  498. c >> tid >> bid;
  499. CGTownInstance * t = static_cast<CGTownInstance*>(gs->map->objects[tid]);
  500. CBuilding * b = VLC->buildh->buildings[t->subID][bid];
  501. for(int i=0;i<RESOURCE_QUANTITY;i++)
  502. if(b->resources[i] > gs->players[t->tempOwner].resources[i])
  503. break; //no res
  504. //TODO: check requirements
  505. //TODO: check if building isn't forbidden
  506. NewStructures ns;
  507. ns.tid = tid;
  508. if(bid>36) //upg dwelling
  509. {
  510. if(t->getHordeLevel(0) == (bid-37))
  511. ns.bid.insert(19);
  512. else if(t->getHordeLevel(1) == (bid-37))
  513. ns.bid.insert(25);
  514. }
  515. else if(bid >= 30) //bas. dwelling
  516. {
  517. SetAvailableCreatures ssi;
  518. ssi.tid = tid;
  519. ssi.creatures = t->strInfo.creatures;
  520. ssi.creatures[bid-30] = VLC->creh->creatures[t->town->basicCreatures[bid-30]].growth;
  521. sendAndApply(&ssi);
  522. }
  523. ns.bid.insert(bid);
  524. ns.builded = t->builded + 1;
  525. sendAndApply(&ns);
  526. SetResources sr;
  527. sr.player = t->tempOwner;
  528. sr.res = gs->players[t->tempOwner].resources;
  529. for(int i=0;i<7;i++)
  530. sr.res[i]-=b->resources[i];
  531. sendAndApply(&sr);
  532. break;
  533. }
  534. case 506: //recruit creature
  535. {
  536. si32 objid, ser=-1; //ser - used dwelling level
  537. ui32 crid, cram; //recruited creature id and amount
  538. c >> objid >> crid >> cram;
  539. CGTownInstance * t = static_cast<CGTownInstance*>(gs->map->objects[objid]);
  540. //verify
  541. bool found = false;
  542. typedef std::pair<const int,int> Parka;
  543. for(std::map<si32,ui32>::iterator av = t->strInfo.creatures.begin(); av!=t->strInfo.creatures.end(); av++)
  544. {
  545. if( ( found = (crid == t->town->basicCreatures[av->first]) ) //creature is available among basic cretures
  546. || (found = (crid == t->town->upgradedCreatures[av->first])) )//creature is available among upgraded cretures
  547. {
  548. cram = std::min(cram,av->second); //reduce recruited amount up to available amount
  549. ser = av->first;
  550. break;
  551. }
  552. }
  553. int slot = t->army.getSlotFor(crid);
  554. if(!found || //no such creature
  555. cram > VLC->creh->creatures[crid].maxAmount(gs->players[t->tempOwner].resources) || //lack of resources
  556. cram<=0 ||
  557. slot<0 )
  558. break;
  559. //recruit
  560. SetResources sr;
  561. sr.player = t->tempOwner;
  562. for(int i=0;i<RESOURCE_QUANTITY;i++)
  563. sr.res[i] = gs->players[t->tempOwner].resources[i] - (VLC->creh->creatures[crid].cost[i] * cram);
  564. SetAvailableCreatures sac;
  565. sac.tid = objid;
  566. sac.creatures = t->strInfo.creatures;
  567. sac.creatures[ser] -= cram;
  568. SetGarrisons sg;
  569. sg.garrs[objid] = t->army;
  570. if(sg.garrs[objid].slots.find(slot) == sg.garrs[objid].slots.end()) //take a free slot
  571. {
  572. sg.garrs[objid].slots[slot] = std::make_pair(crid,cram);
  573. }
  574. else //add creatures to a already existing stack
  575. {
  576. sg.garrs[objid].slots[slot].second += cram;
  577. }
  578. sendAndApply(&sr);
  579. sendAndApply(&sac);
  580. sendAndApply(&sg);
  581. break;
  582. }
  583. case 507://upgrade creature
  584. {
  585. ui32 objid, upgID;
  586. ui8 pos;
  587. c >> objid >> pos >> upgID;
  588. CArmedInstance *obj = static_cast<CArmedInstance*>(gs->map->objects[objid]);
  589. UpgradeInfo ui = gs->getUpgradeInfo(obj,pos);
  590. int player = obj->tempOwner;
  591. int crQuantity = obj->army.slots[pos].second;
  592. //check if upgrade is possible
  593. if(ui.oldID<0 || !vstd::contains(ui.newID,upgID))
  594. break;
  595. //check if player has enough resources
  596. for(int i=0;i<ui.cost.size();i++)
  597. {
  598. for (std::set<std::pair<int,int> >::iterator j=ui.cost[i].begin(); j!=ui.cost[i].end(); j++)
  599. {
  600. if(gs->players[player].resources[j->first] < j->second*crQuantity)
  601. goto upgend;
  602. }
  603. }
  604. //take resources
  605. for(int i=0;i<ui.cost.size();i++)
  606. {
  607. for (std::set<std::pair<int,int> >::iterator j=ui.cost[i].begin(); j!=ui.cost[i].end(); j++)
  608. {
  609. SetResource sr;
  610. sr.player = player;
  611. sr.resid = j->first;
  612. sr.val = gs->players[player].resources[j->first] - j->second*crQuantity;
  613. sendAndApply(&sr);
  614. }
  615. }
  616. {
  617. //upgrade creature
  618. SetGarrisons sg;
  619. sg.garrs[objid] = obj->army;
  620. sg.garrs[objid].slots[pos].first = upgID;
  621. sendAndApply(&sg);
  622. }
  623. upgend:
  624. break;
  625. }
  626. case 508: //garrison swap
  627. {
  628. si32 tid;
  629. c >> tid;
  630. CGTownInstance *town = gs->getTown(tid);
  631. if(!town->garrisonHero && town->visitingHero) //visiting => garrison, merge armies
  632. {
  633. CCreatureSet csn = town->visitingHero->army, cso = town->army;
  634. while(!cso.slots.empty())//while there are unmoved creatures
  635. {
  636. int pos = csn.getSlotFor(cso.slots.begin()->second.first);
  637. if(pos<0)
  638. goto handleConEnd;
  639. if(csn.slots.find(pos)!=csn.slots.end()) //add creatures to the existing stack
  640. {
  641. csn.slots[pos].second += cso.slots.begin()->second.second;
  642. }
  643. else //move stack on the free pos
  644. {
  645. csn.slots[pos].first = cso.slots.begin()->second.first;
  646. csn.slots[pos].second = cso.slots.begin()->second.second;
  647. }
  648. cso.slots.erase(cso.slots.begin());
  649. }
  650. SetGarrisons sg;
  651. sg.garrs[town->visitingHero->id] = csn;
  652. sg.garrs[town->id] = csn;
  653. sendAndApply(&sg);
  654. SetHeroesInTown intown;
  655. intown.tid = tid;
  656. intown.visiting = -1;
  657. intown.garrison = town->visitingHero->id;
  658. sendAndApply(&intown);
  659. }
  660. else if (town->garrisonHero && !town->visitingHero) //move hero out of the garrison
  661. {
  662. SetHeroesInTown intown;
  663. intown.tid = tid;
  664. intown.garrison = -1;
  665. intown.visiting = town->garrisonHero->id;
  666. sendAndApply(&intown);
  667. //town will be empty
  668. SetGarrisons sg;
  669. sg.garrs[tid] = CCreatureSet();
  670. sendAndApply(&sg);
  671. }
  672. else if (town->garrisonHero && town->visitingHero) //swap visiting and garrison hero
  673. {
  674. SetGarrisons sg;
  675. sg.garrs[town->id] = town->visitingHero->army;
  676. sg.garrs[town->garrisonHero->id] = town->garrisonHero->army;
  677. //sg.garrs[town->visitingHero->id] = town->visitingHero->army;
  678. SetHeroesInTown intown;
  679. intown.tid = tid;
  680. intown.garrison = town->visitingHero->id;
  681. intown.visiting = town->garrisonHero->id;
  682. sendAndApply(&intown);
  683. sendAndApply(&sg);
  684. }
  685. else
  686. {
  687. std::cout << "Warning, wrong garrison swap command for " << tid << std::endl;
  688. }
  689. break;
  690. }
  691. case 509: //swap artifacts
  692. {
  693. si32 hid1, hid2;
  694. ui16 slot1, slot2;
  695. c >> hid1 >> slot1 >> hid2 >> slot2;
  696. CGHeroInstance *h1 = gs->getHero(hid1), *h2 = gs->getHero(hid2);
  697. if((distance(h1->pos,h2->pos) > 1.0) || (h1->tempOwner != h2->tempOwner))
  698. break;
  699. int a1=h1->getArtAtPos(slot1), a2=h2->getArtAtPos(slot2);
  700. h2->setArtAtPos(slot2,a1);
  701. h1->setArtAtPos(slot1,a2);
  702. SetHeroArtifacts sha;
  703. sha.hid = hid1;
  704. sha.artifacts = h1->artifacts;
  705. sha.artifWorn = h1->artifWorn;
  706. sendAndApply(&sha);
  707. if(hid1 != hid2)
  708. {
  709. sha.hid = hid2;
  710. sha.artifacts = h2->artifacts;
  711. sha.artifWorn = h2->artifWorn;
  712. sendAndApply(&sha);
  713. }
  714. break;
  715. }
  716. case 510: //buy artifact
  717. {
  718. ui32 hid;
  719. si32 aid, bid;
  720. c >> hid >> aid;
  721. CGHeroInstance *hero = gs->getHero(hid);
  722. CGTownInstance *town = hero->visitedTown;
  723. if(aid==0)
  724. {
  725. if(!vstd::contains(town->builtBuildings,si32(0)))
  726. break;
  727. SetResource sr;
  728. sr.player = hero->tempOwner;
  729. sr.resid = 6;
  730. sr.val = gs->players[hero->tempOwner].resources[6] - 500;
  731. sendAndApply(&sr);
  732. SetHeroArtifacts sha;
  733. sha.hid = hid;
  734. sha.artifacts = hero->artifacts;
  735. sha.artifWorn = hero->artifWorn;
  736. sha.artifWorn[17] = 0;
  737. sendAndApply(&sha);
  738. }
  739. //TODO: war machines
  740. break;
  741. }
  742. case 2001:
  743. {
  744. ui32 qid, answer;
  745. c >> qid >> answer;
  746. gsm.lock();
  747. CFunctionList<void(ui32)> callb = callbacks[qid];
  748. callbacks.erase(qid);
  749. gsm.unlock();
  750. callb(answer);
  751. break;
  752. }
  753. case 3002:
  754. {
  755. BattleAction ba;
  756. c >> ba;
  757. switch(ba.actionType)
  758. {
  759. case 2: //walk
  760. {
  761. moveStack(ba.stackNumber,ba.destinationTile);
  762. break;
  763. }
  764. case 3: //defend
  765. {
  766. break;
  767. }
  768. case 4: //retreat/flee
  769. {
  770. //TODO: check if fleeing is possible (e.g. enemy may have Shackles of War)
  771. //TODO: calculate casualties
  772. //TODO: remove retreating hero from map and place it in recrutation list
  773. BattleResult *br = new BattleResult;
  774. br->result = 1;
  775. battleResult.set(br);
  776. break;
  777. }
  778. case 6: //walk or attack
  779. {
  780. moveStack(ba.stackNumber,ba.destinationTile);
  781. CStack *curStack = gs->curB->getStack(ba.stackNumber),
  782. *stackAtEnd = gs->curB->getStackT(ba.additionalInfo);
  783. if((curStack->position != ba.destinationTile) || //we wasn't able to reach destination tile
  784. (BattleInfo::mutualPosition(ba.destinationTile,ba.additionalInfo)<0) ) //destination tile is not neighbouring with enemy stack
  785. return;
  786. BattleAttack bat;
  787. prepareAttack(bat,curStack,stackAtEnd);
  788. sendAndApply(&bat);
  789. break;
  790. }
  791. case 7: //shoot
  792. {
  793. CStack *curStack = gs->curB->getStack(ba.stackNumber),
  794. *destStack= gs->curB->getStackT(ba.destinationTile);
  795. BattleAttack bat;
  796. prepareAttack(bat,curStack,destStack);
  797. bat.flags |= 1;
  798. sendAndApply(&bat);
  799. break;
  800. }
  801. }
  802. battleMadeAction.setn(true);
  803. //sprawdzic czy po tej akcji ktoras strona nie wygrala bitwy
  804. break;
  805. }
  806. default:
  807. #ifndef __GNUC__
  808. throw std::exception("Not supported client message!");
  809. #else
  810. throw std::exception();
  811. #endif
  812. break;
  813. }
  814. }
  815. }
  816. catch (const std::exception& e)
  817. {
  818. std::cerr << e.what() << std::endl;
  819. end2 = true;
  820. }
  821. catch (const std::exception * e)
  822. {
  823. std::cerr << e->what()<< std::endl;
  824. end2 = true;
  825. delete e;
  826. }
  827. catch(...)
  828. {
  829. end2 = true;
  830. }
  831. handleConEnd:
  832. ;
  833. }
  834. void CGameHandler::moveStack(int stack, int dest)
  835. {
  836. CStack *curStack = gs->curB->getStack(stack),
  837. *stackAtEnd = gs->curB->getStackT(dest);
  838. //initing necessary tables
  839. bool accessibility[187];
  840. if(curStack->creature->isDoubleWide())
  841. gs->curB->getAccessibilityMapForTwoHex(accessibility,curStack->attackerOwned,curStack->ID);
  842. else
  843. gs->curB->getAccessibilityMap(accessibility,curStack->ID);
  844. if((stackAtEnd && stackAtEnd->alive) || !accessibility[dest])
  845. return;
  846. //if(dists[dest] > curStack->creature->speed && !(stackAtEnd && dists[dest] == curStack->creature->speed+1)) //we can attack a stack if we can go to adjacent hex
  847. // return false;
  848. std::vector<int> path = gs->curB->getPath(curStack->position,dest,accessibility);
  849. int tilesToMove = std::max((int)path.size()-curStack->creature->speed, 0);
  850. for(int v=path.size()-1; v>=tilesToMove; --v)
  851. {
  852. //inform clients about move
  853. BattleStackMoved sm;
  854. sm.stack = curStack->ID;
  855. sm.tile = path[v];
  856. if(v==path.size()-1) //move start - set flag
  857. sm.flags |= 1;
  858. if(v==0) //move end - set flag
  859. sm.flags |= 2;
  860. sendAndApply(&sm);
  861. }
  862. }
  863. CGameHandler::CGameHandler(void)
  864. {
  865. gs = NULL;
  866. }
  867. CGameHandler::~CGameHandler(void)
  868. {
  869. delete gs;
  870. }
  871. void CGameHandler::init(StartInfo *si, int Seed)
  872. {
  873. Mapa *map = new Mapa(si->mapname);
  874. gs = new CGameState();
  875. gs->init(si,map,Seed);
  876. /****************************LUA OBJECT SCRIPTS************************************************/
  877. //std::vector<std::string> * lf = CLuaHandler::searchForScripts("scripts/lua/objects"); //files
  878. //for (int i=0; i<lf->size(); i++)
  879. //{
  880. // try
  881. // {
  882. // std::vector<std::string> * temp = CLuaHandler::functionList((*lf)[i]);
  883. // CLuaObjectScript * objs = new CLuaObjectScript((*lf)[i]);
  884. // CLuaCallback::registerFuncs(objs->is);
  885. // //objs
  886. // for (int j=0; j<temp->size(); j++)
  887. // {
  888. // int obid ; //obj ID
  889. // int dspos = (*temp)[j].find_first_of('_');
  890. // obid = atoi((*temp)[j].substr(dspos+1,(*temp)[j].size()-dspos-1).c_str());
  891. // std::string fname = (*temp)[j].substr(0,dspos);
  892. // if (skrypty->find(obid)==skrypty->end())
  893. // skrypty->insert(std::pair<int, std::map<std::string, CObjectScript*> >(obid,std::map<std::string,CObjectScript*>()));
  894. // (*skrypty)[obid].insert(std::pair<std::string, CObjectScript*>(fname,objs));
  895. // }
  896. // delete temp;
  897. // }HANDLE_EXCEPTION
  898. //}
  899. //delete lf;
  900. }
  901. int lowestSpeed(CGHeroInstance * chi)
  902. {
  903. std::map<si32,std::pair<ui32,si32> >::iterator i = chi->army.slots.begin();
  904. int ret = VLC->creh->creatures[(*i++).second.first].speed;
  905. for (;i!=chi->army.slots.end();i++)
  906. {
  907. ret = std::min(ret,VLC->creh->creatures[(*i).second.first].speed);
  908. }
  909. return ret;
  910. }
  911. int valMovePoints(CGHeroInstance * chi)
  912. {
  913. int ret = 1270+70*lowestSpeed(chi);
  914. if (ret>2000)
  915. ret=2000;
  916. //TODO: additional bonuses (but they aren't currently stored in chi)
  917. return ret;
  918. }
  919. void CGameHandler::newTurn()
  920. {
  921. NewTurn n;
  922. n.day = gs->day + 1;
  923. n.resetBuilded = true;
  924. for ( std::map<ui8, PlayerState>::iterator i=gs->players.begin() ; i!=gs->players.end();i++)
  925. {
  926. if(i->first>=PLAYER_LIMIT) continue;
  927. SetResources r;
  928. r.player = i->first;
  929. for(int j=0;j<RESOURCE_QUANTITY;j++)
  930. r.res[j] = i->second.resources[j];
  931. for (unsigned j=0;j<(*i).second.heroes.size();j++) //handle heroes
  932. {
  933. NewTurn::Hero h;
  934. h.id = (*i).second.heroes[j]->id;
  935. h.move = valMovePoints((*i).second.heroes[j]);
  936. h.mana = (*i).second.heroes[j]->mana;
  937. n.heroes.insert(h);
  938. }
  939. for(std::vector<CGTownInstance *>::iterator j=i->second.towns.begin();j!=i->second.towns.end();j++)//handle towns
  940. {
  941. if(gs->getDate(1)==7) //first day of week
  942. {
  943. SetAvailableCreatures sac;
  944. sac.tid = (**j).id;
  945. sac.creatures = (**j).strInfo.creatures;
  946. for(int k=0;k<CREATURES_PER_TOWN;k++) //creature growths
  947. {
  948. if((**j).creatureDwelling(k))//there is dwelling (k-level)
  949. sac.creatures[k] += (**j).creatureGrowth(k);
  950. }
  951. n.cres.push_back(sac);
  952. }
  953. if((gs->day) && i->first<PLAYER_LIMIT)//not the first day and town not neutral
  954. r.res[6] += (**j).dailyIncome();
  955. }
  956. n.res.push_back(r);
  957. }
  958. sendAndApply(&n);
  959. for (std::set<CCPPObjectScript *>::iterator i=cppscripts.begin();i!=cppscripts.end();i++)
  960. {
  961. (*i)->newTurn();
  962. }
  963. }
  964. void CGameHandler::run()
  965. {
  966. BOOST_FOREACH(CConnection *cc, conns)
  967. {//init conn.
  968. ui8 quantity, pom;
  969. //ui32 seed;
  970. (*cc) << gs->scenarioOps->mapname << gs->map->checksum << gs->seed;
  971. (*cc) >> quantity;
  972. for(int i=0;i<quantity;i++)
  973. {
  974. (*cc) >> pom;
  975. gsm.lock();
  976. connections[pom] = cc;
  977. gsm.unlock();
  978. }
  979. }
  980. for(std::set<CConnection*>::iterator i = conns.begin(); i!=conns.end();i++)
  981. {
  982. std::set<int> pom;
  983. for(std::map<int,CConnection*>::iterator j = connections.begin(); j!=connections.end();j++)
  984. if(j->second == *i)
  985. pom.insert(j->first);
  986. boost::thread(boost::bind(&CGameHandler::handleConnection,this,pom,boost::ref(**i)));
  987. }
  988. /****************************SCRIPTS************************************************/
  989. //std::map<int, std::map<std::string, CObjectScript*> > * skrypty = &objscr; //alias for easier access
  990. /****************************C++ OBJECT SCRIPTS************************************************/
  991. std::map<int,CCPPObjectScript*> scripts;
  992. CScriptCallback * csc = new CScriptCallback();
  993. csc->gh = this;
  994. handleCPPObjS(&scripts,new CVisitableOPH(csc));
  995. handleCPPObjS(&scripts,new CVisitableOPW(csc));
  996. handleCPPObjS(&scripts,new CPickable(csc));
  997. handleCPPObjS(&scripts,new CMines(csc));
  998. handleCPPObjS(&scripts,new CTownScript(csc));
  999. handleCPPObjS(&scripts,new CHeroScript(csc));
  1000. handleCPPObjS(&scripts,new CMonsterS(csc));
  1001. handleCPPObjS(&scripts,new CCreatureGen(csc));
  1002. /****************************INITIALIZING OBJECT SCRIPTS************************************************/
  1003. //std::string temps("newObject");
  1004. for (unsigned i=0; i<gs->map->objects.size(); i++)
  1005. {
  1006. //c++ scripts
  1007. if (scripts.find(gs->map->objects[i]->ID) != scripts.end())
  1008. {
  1009. gs->map->objects[i]->state = scripts[gs->map->objects[i]->ID];
  1010. gs->map->objects[i]->state->newObject(gs->map->objects[i]->id);
  1011. }
  1012. else
  1013. {
  1014. gs->map->objects[i]->state = NULL;
  1015. }
  1016. //// lua scripts
  1017. //if(checkFunc(map->objects[i]->ID,temps))
  1018. // (*skrypty)[map->objects[i]->ID][temps]->newObject(map->objects[i]);
  1019. }
  1020. for(std::map<ui8,PlayerState>::iterator i = gs->players.begin(); i != gs->players.end(); i++)
  1021. states.addPlayer(i->first);
  1022. while (!end2)
  1023. {
  1024. newTurn();
  1025. for(std::map<ui8,PlayerState>::iterator i = gs->players.begin(); i != gs->players.end(); i++)
  1026. {
  1027. if((i->second.towns.size()==0 && i->second.heroes.size()==0) || i->second.color<0 || i->first>=PLAYER_LIMIT ) continue; //players has not towns/castle - loser
  1028. states.setFlag(i->first,&PlayerStatus::makingTurn,true);
  1029. gs->currentPlayer = i->first;
  1030. *connections[i->first] << ui16(100) << i->first;
  1031. //wait till turn is done
  1032. boost::unique_lock<boost::mutex> lock(states.mx);
  1033. while(states.players[i->first].makingTurn && !end2)
  1034. {
  1035. boost::posix_time::time_duration p;
  1036. p = boost::posix_time::milliseconds(200);
  1037. #ifdef _MSC_VER
  1038. states.cv.timed_wait(lock,p);
  1039. #else
  1040. boost::xtime time={0,0};
  1041. time.nsec = static_cast<boost::xtime::xtime_nsec_t>(p.total_nanoseconds());
  1042. states.cv.timed_wait(lock,time);
  1043. #endif
  1044. }
  1045. }
  1046. }
  1047. }
  1048. void CGameHandler::setupBattle( BattleInfo * curB, int3 tile, CCreatureSet &army1, CCreatureSet &army2, CGHeroInstance * hero1, CGHeroInstance * hero2 )
  1049. {
  1050. battleResult.set(NULL);
  1051. std::vector<CStack*> & stacks = (curB->stacks);
  1052. curB->tile = tile;
  1053. curB->siege = 0; //TODO: add sieges
  1054. curB->army1=army1;
  1055. curB->army2=army2;
  1056. curB->hero1=(hero1)?(hero1->id):(-1);
  1057. curB->hero2=(hero2)?(hero2->id):(-1);
  1058. curB->side1=(hero1)?(hero1->tempOwner):(-1);
  1059. curB->side2=(hero2)?(hero2->tempOwner):(-1);
  1060. curB->round = -2;
  1061. curB->activeStack = -1;
  1062. for(std::map<si32,std::pair<ui32,si32> >::iterator i = army1.slots.begin(); i!=army1.slots.end(); i++)
  1063. {
  1064. stacks.push_back(new CStack(&VLC->creh->creatures[i->second.first],i->second.second,hero1->tempOwner, stacks.size(), true));
  1065. stacks[stacks.size()-1]->ID = stacks.size()-1;
  1066. }
  1067. //initialization of positions
  1068. switch(army1.slots.size()) //for attacker
  1069. {
  1070. case 0:
  1071. break;
  1072. case 1:
  1073. stacks[0]->position = 86; //6
  1074. break;
  1075. case 2:
  1076. stacks[0]->position = 35; //3
  1077. stacks[1]->position = 137; //9
  1078. break;
  1079. case 3:
  1080. stacks[0]->position = 35; //3
  1081. stacks[1]->position = 86; //6
  1082. stacks[2]->position = 137; //9
  1083. break;
  1084. case 4:
  1085. stacks[0]->position = 1; //1
  1086. stacks[1]->position = 69; //5
  1087. stacks[2]->position = 103; //7
  1088. stacks[3]->position = 171; //11
  1089. break;
  1090. case 5:
  1091. stacks[0]->position = 1; //1
  1092. stacks[1]->position = 35; //3
  1093. stacks[2]->position = 86; //6
  1094. stacks[3]->position = 137; //9
  1095. stacks[4]->position = 171; //11
  1096. break;
  1097. case 6:
  1098. stacks[0]->position = 1; //1
  1099. stacks[1]->position = 35; //3
  1100. stacks[2]->position = 69; //5
  1101. stacks[3]->position = 103; //7
  1102. stacks[4]->position = 137; //9
  1103. stacks[5]->position = 171; //11
  1104. break;
  1105. case 7:
  1106. stacks[0]->position = 1; //1
  1107. stacks[1]->position = 35; //3
  1108. stacks[2]->position = 69; //5
  1109. stacks[3]->position = 86; //6
  1110. stacks[4]->position = 103; //7
  1111. stacks[5]->position = 137; //9
  1112. stacks[6]->position = 171; //11
  1113. break;
  1114. default: //fault
  1115. break;
  1116. }
  1117. for(std::map<si32,std::pair<ui32,si32> >::iterator i = army2.slots.begin(); i!=army2.slots.end(); i++)
  1118. stacks.push_back(new CStack(&VLC->creh->creatures[i->second.first],i->second.second,hero2 ? hero2->tempOwner : 255, stacks.size(), false));
  1119. switch(army2.slots.size()) //for defender
  1120. {
  1121. case 0:
  1122. break;
  1123. case 1:
  1124. stacks[0+army1.slots.size()]->position = 100; //6
  1125. break;
  1126. case 2:
  1127. stacks[0+army1.slots.size()]->position = 49; //3
  1128. stacks[1+army1.slots.size()]->position = 151; //9
  1129. break;
  1130. case 3:
  1131. stacks[0+army1.slots.size()]->position = 49; //3
  1132. stacks[1+army1.slots.size()]->position = 100; //6
  1133. stacks[2+army1.slots.size()]->position = 151; //9
  1134. break;
  1135. case 4:
  1136. stacks[0+army1.slots.size()]->position = 15; //1
  1137. stacks[1+army1.slots.size()]->position = 83; //5
  1138. stacks[2+army1.slots.size()]->position = 117; //7
  1139. stacks[3+army1.slots.size()]->position = 185; //11
  1140. break;
  1141. case 5:
  1142. stacks[0+army1.slots.size()]->position = 15; //1
  1143. stacks[1+army1.slots.size()]->position = 49; //3
  1144. stacks[2+army1.slots.size()]->position = 100; //6
  1145. stacks[3+army1.slots.size()]->position = 151; //9
  1146. stacks[4+army1.slots.size()]->position = 185; //11
  1147. break;
  1148. case 6:
  1149. stacks[0+army1.slots.size()]->position = 15; //1
  1150. stacks[1+army1.slots.size()]->position = 49; //3
  1151. stacks[2+army1.slots.size()]->position = 83; //5
  1152. stacks[3+army1.slots.size()]->position = 117; //7
  1153. stacks[4+army1.slots.size()]->position = 151; //9
  1154. stacks[5+army1.slots.size()]->position = 185; //11
  1155. break;
  1156. case 7:
  1157. stacks[0+army1.slots.size()]->position = 15; //1
  1158. stacks[1+army1.slots.size()]->position = 49; //3
  1159. stacks[2+army1.slots.size()]->position = 83; //5
  1160. stacks[3+army1.slots.size()]->position = 100; //6
  1161. stacks[4+army1.slots.size()]->position = 117; //7
  1162. stacks[5+army1.slots.size()]->position = 151; //9
  1163. stacks[6+army1.slots.size()]->position = 185; //11
  1164. break;
  1165. default: //fault
  1166. break;
  1167. }
  1168. for(unsigned g=0; g<stacks.size(); ++g) //shifting positions of two-hex creatures
  1169. {
  1170. if((stacks[g]->position%17)==1 && stacks[g]->creature->isDoubleWide())
  1171. {
  1172. stacks[g]->position += 1;
  1173. }
  1174. else if((stacks[g]->position%17)==15 && stacks[g]->creature->isDoubleWide())
  1175. {
  1176. stacks[g]->position -= 1;
  1177. }
  1178. }
  1179. std::stable_sort(stacks.begin(),stacks.end(),cmpst);
  1180. //block engaged players
  1181. if(hero1->tempOwner<PLAYER_LIMIT)
  1182. states.setFlag(hero1->tempOwner,&PlayerStatus::engagedIntoBattle,true);
  1183. if(hero2 && hero2->tempOwner<PLAYER_LIMIT)
  1184. states.setFlag(hero2->tempOwner,&PlayerStatus::engagedIntoBattle,true);
  1185. //send info about battles
  1186. BattleStart bs;
  1187. bs.info = curB;
  1188. sendAndApply(&bs);
  1189. }