CGameHandler.cpp 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260
  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, boost::function<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].first)
  448. S1->slots.erase(p1);
  449. if(!S2->slots[p2].first)
  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. //town will be empty
  663. SetGarrisons sg;
  664. sg.garrs[tid] = CCreatureSet();
  665. sendAndApply(&sg);
  666. SetHeroesInTown intown;
  667. intown.tid = tid;
  668. intown.garrison = -1;
  669. intown.visiting = town->garrisonHero->id;
  670. sendAndApply(&intown);
  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. sendAndApply(&sg);
  677. SetHeroesInTown intown;
  678. intown.tid = tid;
  679. intown.garrison = town->visitingHero->id;
  680. intown.visiting = town->garrisonHero->id;
  681. sendAndApply(&intown);
  682. }
  683. else
  684. {
  685. std::cout << "Warning, wrong garrison swap command for " << tid << std::endl;
  686. }
  687. break;
  688. }
  689. case 509:
  690. {
  691. si32 hid1, hid2;
  692. ui16 slot1, slot2;
  693. c >> hid1 >> slot1 >> hid2 >> slot2;
  694. CGHeroInstance *h1 = gs->getHero(hid1), *h2 = gs->getHero(hid2);
  695. if((distance(h1->pos,h2->pos) > 1.0) || (h1->tempOwner != h2->tempOwner))
  696. break;
  697. int a1=h1->getArtAtPos(slot1), a2=h2->getArtAtPos(slot2);
  698. h2->setArtAtPos(slot2,a1);
  699. h1->setArtAtPos(slot1,a2);
  700. // if(std::max(slot1,slot2) < 19)
  701. // {
  702. // if(vstd::contains(h1->artifWorn,slot1) && vstd::contains(h1->artifWorn,slot2))
  703. // std::swap(h1->artifWorn[slot1],h2->artifWorn[slot2]);
  704. // if(vstd::contains(h1->artifWorn,slot1))
  705. // {
  706. // h2->artifWorn[slot2] = h1->artifWorn[slot1];
  707. // h1->artifWorn.erase(slot1);
  708. // }
  709. // else if(vstd::contains(h2->artifWorn,slot2))
  710. // {
  711. // h1->artifWorn[slot1] = h2->artifWorn[slot2];
  712. // h2->artifWorn.erase(slot2);
  713. // }
  714. // else
  715. // {
  716. // std::cout << "Warning, wrong artifact swap command!" << std::endl;
  717. // }
  718. // }
  719. // else
  720. // {
  721. // }
  722. SetHeroArtifacts sha;
  723. sha.hid = hid1;
  724. sha.artifacts = h1->artifacts;
  725. sha.artifWorn = h1->artifWorn;
  726. sendAndApply(&sha);
  727. if(hid1 != hid2)
  728. {
  729. sha.hid = hid2;
  730. sha.artifacts = h2->artifacts;
  731. sha.artifWorn = h2->artifWorn;
  732. sendAndApply(&sha);
  733. }
  734. }
  735. case 2001:
  736. {
  737. ui32 qid, answer;
  738. c >> qid >> answer;
  739. gsm.lock();
  740. boost::function<void(ui32)> callb = callbacks[qid];
  741. callbacks.erase(qid);
  742. gsm.unlock();
  743. callb(answer);
  744. break;
  745. }
  746. case 3002:
  747. {
  748. BattleAction ba;
  749. c >> ba;
  750. switch(ba.actionType)
  751. {
  752. case 2: //walk
  753. {
  754. moveStack(ba.stackNumber,ba.destinationTile);
  755. break;
  756. }
  757. case 3: //defend
  758. {
  759. break;
  760. }
  761. case 4: //retreat/flee
  762. {
  763. //TODO: check if fleeing is possible (e.g. enemy may have Shackles of War)
  764. //TODO: calculate casualties
  765. //TODO: remove retreating hero from map and place it in recrutation list
  766. BattleResult *br = new BattleResult;
  767. br->result = 1;
  768. battleResult.set(br);
  769. break;
  770. }
  771. case 6: //walk or attack
  772. {
  773. moveStack(ba.stackNumber,ba.destinationTile);
  774. CStack *curStack = gs->curB->getStack(ba.stackNumber),
  775. *stackAtEnd = gs->curB->getStackT(ba.additionalInfo);
  776. if((curStack->position != ba.destinationTile) || //we wasn't able to reach destination tile
  777. (BattleInfo::mutualPosition(ba.destinationTile,ba.additionalInfo)<0) ) //destination tile is not neighbouring with enemy stack
  778. return;
  779. BattleAttack bat;
  780. prepareAttack(bat,curStack,stackAtEnd);
  781. sendAndApply(&bat);
  782. break;
  783. }
  784. case 7: //shoot
  785. {
  786. CStack *curStack = gs->curB->getStack(ba.stackNumber),
  787. *destStack= gs->curB->getStackT(ba.destinationTile);
  788. BattleAttack bat;
  789. prepareAttack(bat,curStack,destStack);
  790. bat.flags |= 1;
  791. sendAndApply(&bat);
  792. break;
  793. }
  794. }
  795. battleMadeAction.setn(true);
  796. //sprawdzic czy po tej akcji ktoras strona nie wygrala bitwy
  797. break;
  798. }
  799. default:
  800. #ifndef __GNUC__
  801. throw std::exception("Not supported client message!");
  802. #else
  803. throw std::exception();
  804. #endif
  805. break;
  806. }
  807. }
  808. }
  809. catch (const std::exception& e)
  810. {
  811. std::cerr << e.what() << std::endl;
  812. end2 = true;
  813. }
  814. catch (const std::exception * e)
  815. {
  816. std::cerr << e->what()<< std::endl;
  817. end2 = true;
  818. delete e;
  819. }
  820. catch(...)
  821. {
  822. end2 = true;
  823. }
  824. handleConEnd:
  825. ;
  826. }
  827. void CGameHandler::moveStack(int stack, int dest)
  828. {
  829. CStack *curStack = gs->curB->getStack(stack),
  830. *stackAtEnd = gs->curB->getStackT(dest);
  831. //initing necessary tables
  832. bool accessibility[187];
  833. if(curStack->creature->isDoubleWide())
  834. gs->curB->getAccessibilityMapForTwoHex(accessibility,curStack->attackerOwned,curStack->ID);
  835. else
  836. gs->curB->getAccessibilityMap(accessibility,curStack->ID);
  837. if((stackAtEnd && stackAtEnd->alive) || !accessibility[dest])
  838. return;
  839. //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
  840. // return false;
  841. std::vector<int> path = gs->curB->getPath(curStack->position,dest,accessibility);
  842. int tilesToMove = std::max((int)path.size()-curStack->creature->speed, 0);
  843. for(int v=path.size()-1; v>=tilesToMove; --v)
  844. {
  845. //inform clients about move
  846. BattleStackMoved sm;
  847. sm.stack = curStack->ID;
  848. sm.tile = path[v];
  849. if(v==path.size()-1) //move start - set flag
  850. sm.flags |= 1;
  851. if(v==0) //move end - set flag
  852. sm.flags |= 2;
  853. sendAndApply(&sm);
  854. }
  855. }
  856. CGameHandler::CGameHandler(void)
  857. {
  858. gs = NULL;
  859. }
  860. CGameHandler::~CGameHandler(void)
  861. {
  862. delete gs;
  863. }
  864. void CGameHandler::init(StartInfo *si, int Seed)
  865. {
  866. Mapa *map = new Mapa(si->mapname);
  867. gs = new CGameState();
  868. gs->init(si,map,Seed);
  869. /****************************LUA OBJECT SCRIPTS************************************************/
  870. //std::vector<std::string> * lf = CLuaHandler::searchForScripts("scripts/lua/objects"); //files
  871. //for (int i=0; i<lf->size(); i++)
  872. //{
  873. // try
  874. // {
  875. // std::vector<std::string> * temp = CLuaHandler::functionList((*lf)[i]);
  876. // CLuaObjectScript * objs = new CLuaObjectScript((*lf)[i]);
  877. // CLuaCallback::registerFuncs(objs->is);
  878. // //objs
  879. // for (int j=0; j<temp->size(); j++)
  880. // {
  881. // int obid ; //obj ID
  882. // int dspos = (*temp)[j].find_first_of('_');
  883. // obid = atoi((*temp)[j].substr(dspos+1,(*temp)[j].size()-dspos-1).c_str());
  884. // std::string fname = (*temp)[j].substr(0,dspos);
  885. // if (skrypty->find(obid)==skrypty->end())
  886. // skrypty->insert(std::pair<int, std::map<std::string, CObjectScript*> >(obid,std::map<std::string,CObjectScript*>()));
  887. // (*skrypty)[obid].insert(std::pair<std::string, CObjectScript*>(fname,objs));
  888. // }
  889. // delete temp;
  890. // }HANDLE_EXCEPTION
  891. //}
  892. //delete lf;
  893. }
  894. int lowestSpeed(CGHeroInstance * chi)
  895. {
  896. std::map<si32,std::pair<ui32,si32> >::iterator i = chi->army.slots.begin();
  897. int ret = VLC->creh->creatures[(*i++).second.first].speed;
  898. for (;i!=chi->army.slots.end();i++)
  899. {
  900. ret = std::min(ret,VLC->creh->creatures[(*i).second.first].speed);
  901. }
  902. return ret;
  903. }
  904. int valMovePoints(CGHeroInstance * chi)
  905. {
  906. int ret = 1270+70*lowestSpeed(chi);
  907. if (ret>2000)
  908. ret=2000;
  909. //TODO: additional bonuses (but they aren't currently stored in chi)
  910. return ret;
  911. }
  912. void CGameHandler::newTurn()
  913. {
  914. NewTurn n;
  915. n.day = gs->day + 1;
  916. n.resetBuilded = true;
  917. for ( std::map<ui8, PlayerState>::iterator i=gs->players.begin() ; i!=gs->players.end();i++)
  918. {
  919. if(i->first>=PLAYER_LIMIT) continue;
  920. SetResources r;
  921. r.player = i->first;
  922. for(int j=0;j<RESOURCE_QUANTITY;j++)
  923. r.res[j] = i->second.resources[j];
  924. for (unsigned j=0;j<(*i).second.heroes.size();j++) //handle heroes
  925. {
  926. NewTurn::Hero h;
  927. h.id = (*i).second.heroes[j]->id;
  928. h.move = valMovePoints((*i).second.heroes[j]);
  929. h.mana = (*i).second.heroes[j]->mana;
  930. n.heroes.insert(h);
  931. }
  932. for(std::vector<CGTownInstance *>::iterator j=i->second.towns.begin();j!=i->second.towns.end();j++)//handle towns
  933. {
  934. if(gs->getDate(1)==7) //first day of week
  935. {
  936. SetAvailableCreatures sac;
  937. sac.tid = (**j).id;
  938. sac.creatures = (**j).strInfo.creatures;
  939. for(int k=0;k<CREATURES_PER_TOWN;k++) //creature growths
  940. {
  941. if((**j).creatureDwelling(k))//there is dwelling (k-level)
  942. sac.creatures[k] += (**j).creatureGrowth(k);
  943. }
  944. n.cres.push_back(sac);
  945. }
  946. if((gs->day) && i->first<PLAYER_LIMIT)//not the first day and town not neutral
  947. r.res[6] += (**j).dailyIncome();
  948. }
  949. n.res.push_back(r);
  950. }
  951. sendAndApply(&n);
  952. for (std::set<CCPPObjectScript *>::iterator i=cppscripts.begin();i!=cppscripts.end();i++)
  953. {
  954. (*i)->newTurn();
  955. }
  956. }
  957. void CGameHandler::run()
  958. {
  959. BOOST_FOREACH(CConnection *cc, conns)
  960. {//init conn.
  961. ui8 quantity, pom;
  962. //ui32 seed;
  963. (*cc) << gs->scenarioOps->mapname << gs->map->checksum << gs->seed;
  964. (*cc) >> quantity;
  965. for(int i=0;i<quantity;i++)
  966. {
  967. (*cc) >> pom;
  968. gsm.lock();
  969. connections[pom] = cc;
  970. gsm.unlock();
  971. }
  972. }
  973. for(std::set<CConnection*>::iterator i = conns.begin(); i!=conns.end();i++)
  974. {
  975. std::set<int> pom;
  976. for(std::map<int,CConnection*>::iterator j = connections.begin(); j!=connections.end();j++)
  977. if(j->second == *i)
  978. pom.insert(j->first);
  979. boost::thread(boost::bind(&CGameHandler::handleConnection,this,pom,boost::ref(**i)));
  980. }
  981. /****************************SCRIPTS************************************************/
  982. //std::map<int, std::map<std::string, CObjectScript*> > * skrypty = &objscr; //alias for easier access
  983. /****************************C++ OBJECT SCRIPTS************************************************/
  984. std::map<int,CCPPObjectScript*> scripts;
  985. CScriptCallback * csc = new CScriptCallback();
  986. csc->gh = this;
  987. handleCPPObjS(&scripts,new CVisitableOPH(csc));
  988. handleCPPObjS(&scripts,new CVisitableOPW(csc));
  989. handleCPPObjS(&scripts,new CPickable(csc));
  990. handleCPPObjS(&scripts,new CMines(csc));
  991. handleCPPObjS(&scripts,new CTownScript(csc));
  992. handleCPPObjS(&scripts,new CHeroScript(csc));
  993. handleCPPObjS(&scripts,new CMonsterS(csc));
  994. handleCPPObjS(&scripts,new CCreatureGen(csc));
  995. /****************************INITIALIZING OBJECT SCRIPTS************************************************/
  996. //std::string temps("newObject");
  997. for (unsigned i=0; i<gs->map->objects.size(); i++)
  998. {
  999. //c++ scripts
  1000. if (scripts.find(gs->map->objects[i]->ID) != scripts.end())
  1001. {
  1002. gs->map->objects[i]->state = scripts[gs->map->objects[i]->ID];
  1003. gs->map->objects[i]->state->newObject(gs->map->objects[i]->id);
  1004. }
  1005. else
  1006. {
  1007. gs->map->objects[i]->state = NULL;
  1008. }
  1009. //// lua scripts
  1010. //if(checkFunc(map->objects[i]->ID,temps))
  1011. // (*skrypty)[map->objects[i]->ID][temps]->newObject(map->objects[i]);
  1012. }
  1013. for(std::map<ui8,PlayerState>::iterator i = gs->players.begin(); i != gs->players.end(); i++)
  1014. states.addPlayer(i->first);
  1015. while (!end2)
  1016. {
  1017. newTurn();
  1018. for(std::map<ui8,PlayerState>::iterator i = gs->players.begin(); i != gs->players.end(); i++)
  1019. {
  1020. 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
  1021. states.setFlag(i->first,&PlayerStatus::makingTurn,true);
  1022. gs->currentPlayer = i->first;
  1023. *connections[i->first] << ui16(100) << i->first;
  1024. //wait till turn is done
  1025. boost::unique_lock<boost::mutex> lock(states.mx);
  1026. while(states.players[i->first].makingTurn && !end2)
  1027. {
  1028. boost::posix_time::time_duration p;
  1029. p= boost::posix_time::seconds(1);
  1030. #ifdef _MSC_VER
  1031. states.cv.timed_wait(lock,p);
  1032. #else
  1033. boost::xtime time={0,0};
  1034. time.sec = static_cast<boost::xtime::xtime_sec_t>(p.total_seconds());
  1035. states.cv.timed_wait(lock,time);
  1036. #endif
  1037. }
  1038. }
  1039. }
  1040. }
  1041. void CGameHandler::setupBattle( BattleInfo * curB, int3 tile, CCreatureSet &army1, CCreatureSet &army2, CGHeroInstance * hero1, CGHeroInstance * hero2 )
  1042. {
  1043. battleResult.set(NULL);
  1044. std::vector<CStack*> & stacks = (curB->stacks);
  1045. curB->tile = tile;
  1046. curB->siege = 0; //TODO: add sieges
  1047. curB->army1=army1;
  1048. curB->army2=army2;
  1049. curB->hero1=(hero1)?(hero1->id):(-1);
  1050. curB->hero2=(hero2)?(hero2->id):(-1);
  1051. curB->side1=(hero1)?(hero1->tempOwner):(-1);
  1052. curB->side2=(hero2)?(hero2->tempOwner):(-1);
  1053. curB->round = -2;
  1054. curB->activeStack = -1;
  1055. for(std::map<si32,std::pair<ui32,si32> >::iterator i = army1.slots.begin(); i!=army1.slots.end(); i++)
  1056. {
  1057. stacks.push_back(new CStack(&VLC->creh->creatures[i->second.first],i->second.second,hero1->tempOwner, stacks.size(), true));
  1058. stacks[stacks.size()-1]->ID = stacks.size()-1;
  1059. }
  1060. //initialization of positions
  1061. switch(army1.slots.size()) //for attacker
  1062. {
  1063. case 0:
  1064. break;
  1065. case 1:
  1066. stacks[0]->position = 86; //6
  1067. break;
  1068. case 2:
  1069. stacks[0]->position = 35; //3
  1070. stacks[1]->position = 137; //9
  1071. break;
  1072. case 3:
  1073. stacks[0]->position = 35; //3
  1074. stacks[1]->position = 86; //6
  1075. stacks[2]->position = 137; //9
  1076. break;
  1077. case 4:
  1078. stacks[0]->position = 1; //1
  1079. stacks[1]->position = 69; //5
  1080. stacks[2]->position = 103; //7
  1081. stacks[3]->position = 171; //11
  1082. break;
  1083. case 5:
  1084. stacks[0]->position = 1; //1
  1085. stacks[1]->position = 35; //3
  1086. stacks[2]->position = 86; //6
  1087. stacks[3]->position = 137; //9
  1088. stacks[4]->position = 171; //11
  1089. break;
  1090. case 6:
  1091. stacks[0]->position = 1; //1
  1092. stacks[1]->position = 35; //3
  1093. stacks[2]->position = 69; //5
  1094. stacks[3]->position = 103; //7
  1095. stacks[4]->position = 137; //9
  1096. stacks[5]->position = 171; //11
  1097. break;
  1098. case 7:
  1099. stacks[0]->position = 1; //1
  1100. stacks[1]->position = 35; //3
  1101. stacks[2]->position = 69; //5
  1102. stacks[3]->position = 86; //6
  1103. stacks[4]->position = 103; //7
  1104. stacks[5]->position = 137; //9
  1105. stacks[6]->position = 171; //11
  1106. break;
  1107. default: //fault
  1108. break;
  1109. }
  1110. for(std::map<si32,std::pair<ui32,si32> >::iterator i = army2.slots.begin(); i!=army2.slots.end(); i++)
  1111. stacks.push_back(new CStack(&VLC->creh->creatures[i->second.first],i->second.second,hero2 ? hero2->tempOwner : 255, stacks.size(), false));
  1112. switch(army2.slots.size()) //for defender
  1113. {
  1114. case 0:
  1115. break;
  1116. case 1:
  1117. stacks[0+army1.slots.size()]->position = 100; //6
  1118. break;
  1119. case 2:
  1120. stacks[0+army1.slots.size()]->position = 49; //3
  1121. stacks[1+army1.slots.size()]->position = 151; //9
  1122. break;
  1123. case 3:
  1124. stacks[0+army1.slots.size()]->position = 49; //3
  1125. stacks[1+army1.slots.size()]->position = 100; //6
  1126. stacks[2+army1.slots.size()]->position = 151; //9
  1127. break;
  1128. case 4:
  1129. stacks[0+army1.slots.size()]->position = 15; //1
  1130. stacks[1+army1.slots.size()]->position = 83; //5
  1131. stacks[2+army1.slots.size()]->position = 117; //7
  1132. stacks[3+army1.slots.size()]->position = 185; //11
  1133. break;
  1134. case 5:
  1135. stacks[0+army1.slots.size()]->position = 15; //1
  1136. stacks[1+army1.slots.size()]->position = 49; //3
  1137. stacks[2+army1.slots.size()]->position = 100; //6
  1138. stacks[3+army1.slots.size()]->position = 151; //9
  1139. stacks[4+army1.slots.size()]->position = 185; //11
  1140. break;
  1141. case 6:
  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 = 83; //5
  1145. stacks[3+army1.slots.size()]->position = 117; //7
  1146. stacks[4+army1.slots.size()]->position = 151; //9
  1147. stacks[5+army1.slots.size()]->position = 185; //11
  1148. break;
  1149. case 7:
  1150. stacks[0+army1.slots.size()]->position = 15; //1
  1151. stacks[1+army1.slots.size()]->position = 49; //3
  1152. stacks[2+army1.slots.size()]->position = 83; //5
  1153. stacks[3+army1.slots.size()]->position = 100; //6
  1154. stacks[4+army1.slots.size()]->position = 117; //7
  1155. stacks[5+army1.slots.size()]->position = 151; //9
  1156. stacks[6+army1.slots.size()]->position = 185; //11
  1157. break;
  1158. default: //fault
  1159. break;
  1160. }
  1161. for(unsigned g=0; g<stacks.size(); ++g) //shifting positions of two-hex creatures
  1162. {
  1163. if((stacks[g]->position%17)==1 && stacks[g]->creature->isDoubleWide())
  1164. {
  1165. stacks[g]->position += 1;
  1166. }
  1167. else if((stacks[g]->position%17)==15 && stacks[g]->creature->isDoubleWide())
  1168. {
  1169. stacks[g]->position -= 1;
  1170. }
  1171. }
  1172. std::stable_sort(stacks.begin(),stacks.end(),cmpst);
  1173. //block engaged players
  1174. if(hero1->tempOwner<PLAYER_LIMIT)
  1175. states.setFlag(hero1->tempOwner,&PlayerStatus::engagedIntoBattle,true);
  1176. if(hero2 && hero2->tempOwner<PLAYER_LIMIT)
  1177. states.setFlag(hero2->tempOwner,&PlayerStatus::engagedIntoBattle,true);
  1178. //send info about battles
  1179. BattleStart bs;
  1180. bs.info = curB;
  1181. sendAndApply(&bs);
  1182. }