CGameHandler.cpp 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209
  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. SetGarrisons sg;
  472. sg.garrs[id1] = *S1;
  473. if(s1 != s2)
  474. sg.garrs[id2] = *S2;
  475. sendAndApply(&sg);
  476. break;
  477. }
  478. case 503:
  479. {
  480. si32 id;
  481. ui8 pos;
  482. c >> id >> pos;
  483. CArmedInstance *s1 = static_cast<CArmedInstance*>(gs->map->objects[id]);
  484. s1->army.slots.erase(pos);
  485. SetGarrisons sg;
  486. sg.garrs[id] = s1->army;
  487. sendAndApply(&sg);
  488. break;
  489. }
  490. case 504:
  491. {
  492. si32 tid, bid;
  493. c >> tid >> bid;
  494. CGTownInstance * t = static_cast<CGTownInstance*>(gs->map->objects[tid]);
  495. CBuilding * b = VLC->buildh->buildings[t->subID][bid];
  496. for(int i=0;i<RESOURCE_QUANTITY;i++)
  497. if(b->resources[i] > gs->players[t->tempOwner].resources[i])
  498. break; //no res
  499. //TODO: check requirements
  500. //TODO: check if building isn't forbidden
  501. NewStructures ns;
  502. ns.tid = tid;
  503. if(bid>36) //upg dwelling
  504. {
  505. if(t->getHordeLevel(0) == (bid-37))
  506. ns.bid.insert(19);
  507. else if(t->getHordeLevel(1) == (bid-37))
  508. ns.bid.insert(25);
  509. }
  510. else if(bid >= 30) //bas. dwelling
  511. {
  512. SetAvailableCreatures ssi;
  513. ssi.tid = tid;
  514. ssi.creatures = t->strInfo.creatures;
  515. ssi.creatures[bid-30] = VLC->creh->creatures[t->town->basicCreatures[bid-30]].growth;
  516. sendAndApply(&ssi);
  517. }
  518. ns.bid.insert(bid);
  519. ns.builded = t->builded + 1;
  520. sendAndApply(&ns);
  521. SetResources sr;
  522. sr.player = t->tempOwner;
  523. sr.res = gs->players[t->tempOwner].resources;
  524. for(int i=0;i<7;i++)
  525. sr.res[i]-=b->resources[i];
  526. sendAndApply(&sr);
  527. break;
  528. }
  529. case 506: //recruit creature
  530. {
  531. si32 objid, ser=-1; //ser - used dwelling level
  532. ui32 crid, cram; //recruited creature id and amount
  533. c >> objid >> crid >> cram;
  534. CGTownInstance * t = static_cast<CGTownInstance*>(gs->map->objects[objid]);
  535. //verify
  536. bool found = false;
  537. typedef std::pair<const int,int> Parka;
  538. for(std::map<si32,ui32>::iterator av = t->strInfo.creatures.begin(); av!=t->strInfo.creatures.end(); av++)
  539. {
  540. if( ( found = (crid == t->town->basicCreatures[av->first]) ) //creature is available among basic cretures
  541. || (found = (crid == t->town->upgradedCreatures[av->first])) )//creature is available among upgraded cretures
  542. {
  543. cram = std::min(cram,av->second); //reduce recruited amount up to available amount
  544. ser = av->first;
  545. break;
  546. }
  547. }
  548. int slot = t->army.getSlotFor(crid);
  549. if(!found || //no such creature
  550. cram > VLC->creh->creatures[crid].maxAmount(gs->players[t->tempOwner].resources) || //lack of resources
  551. cram<=0 ||
  552. slot<0 )
  553. break;
  554. //recruit
  555. SetResources sr;
  556. sr.player = t->tempOwner;
  557. for(int i=0;i<RESOURCE_QUANTITY;i++)
  558. sr.res[i] = gs->players[t->tempOwner].resources[i] - (VLC->creh->creatures[crid].cost[i] * cram);
  559. SetAvailableCreatures sac;
  560. sac.tid = objid;
  561. sac.creatures = t->strInfo.creatures;
  562. sac.creatures[ser] -= cram;
  563. SetGarrisons sg;
  564. sg.garrs[objid] = t->army;
  565. if(sg.garrs[objid].slots.find(slot) == sg.garrs[objid].slots.end()) //take a free slot
  566. {
  567. sg.garrs[objid].slots[slot] = std::make_pair(crid,cram);
  568. }
  569. else //add creatures to a already existing stack
  570. {
  571. sg.garrs[objid].slots[slot].second += cram;
  572. }
  573. sendAndApply(&sr);
  574. sendAndApply(&sac);
  575. sendAndApply(&sg);
  576. break;
  577. }
  578. case 507://upgrade creature
  579. {
  580. ui32 objid, upgID;
  581. ui8 pos;
  582. c >> objid >> pos >> upgID;
  583. CArmedInstance *obj = static_cast<CArmedInstance*>(gs->map->objects[objid]);
  584. UpgradeInfo ui = gs->getUpgradeInfo(obj,pos);
  585. int player = obj->tempOwner;
  586. int crQuantity = obj->army.slots[pos].second;
  587. //check if upgrade is possible
  588. if(ui.oldID<0 || !vstd::contains(ui.newID,upgID))
  589. break;
  590. //check if player has enough resources
  591. for(int i=0;i<ui.cost.size();i++)
  592. {
  593. for (std::set<std::pair<int,int> >::iterator j=ui.cost[i].begin(); j!=ui.cost[i].end(); j++)
  594. {
  595. if(gs->players[player].resources[j->first] < j->second*crQuantity)
  596. goto upgend;
  597. }
  598. }
  599. //take resources
  600. for(int i=0;i<ui.cost.size();i++)
  601. {
  602. for (std::set<std::pair<int,int> >::iterator j=ui.cost[i].begin(); j!=ui.cost[i].end(); j++)
  603. {
  604. SetResource sr;
  605. sr.player = player;
  606. sr.resid = j->first;
  607. sr.val = gs->players[player].resources[j->first] - j->second*crQuantity;
  608. sendAndApply(&sr);
  609. }
  610. }
  611. {
  612. //upgrade creature
  613. SetGarrisons sg;
  614. sg.garrs[objid] = obj->army;
  615. sg.garrs[objid].slots[pos].first = upgID;
  616. sendAndApply(&sg);
  617. }
  618. upgend:
  619. break;
  620. }
  621. case 508: //garrison swap
  622. {
  623. si32 tid;
  624. c >> tid;
  625. CGTownInstance *town = gs->getTown(tid);
  626. if(!town->garrisonHero && town->visitingHero) //visiting => garrison, merge armies
  627. {
  628. CCreatureSet csn = town->visitingHero->army, cso = town->army;
  629. while(!cso.slots.empty())//while there are unmoved creatures
  630. {
  631. int pos = csn.getSlotFor(cso.slots.begin()->first);
  632. if(pos<0)
  633. goto handleConEnd;
  634. if(csn.slots.find(pos)!=csn.slots.end()) //add creatures to the existing stack
  635. {
  636. csn.slots[pos].second += cso.slots.begin()->second.second;
  637. }
  638. else //move stack on the free pos
  639. {
  640. csn.slots[pos].first = cso.slots.begin()->second.first;
  641. csn.slots[pos].second = cso.slots.begin()->second.second;
  642. }
  643. cso.slots.erase(cso.slots.begin());
  644. }
  645. SetGarrisons sg;
  646. sg.garrs[town->visitingHero->id] = csn;
  647. sg.garrs[town->id] = csn;
  648. sendAndApply(&sg);
  649. SetHeroesInTown intown;
  650. intown.tid = tid;
  651. intown.visiting = -1;
  652. intown.garrison = town->visitingHero->id;
  653. sendAndApply(&intown);
  654. }
  655. else if (town->garrisonHero && !town->visitingHero) //move hero out of the garrison
  656. {
  657. //town will be empty
  658. SetGarrisons sg;
  659. sg.garrs[tid] = CCreatureSet();
  660. sendAndApply(&sg);
  661. SetHeroesInTown intown;
  662. intown.tid = tid;
  663. intown.garrison = -1;
  664. intown.visiting = town->garrisonHero->id;
  665. sendAndApply(&intown);
  666. }
  667. else if (town->garrisonHero && town->visitingHero) //swap visiting and garrison hero
  668. {
  669. SetGarrisons sg;
  670. sg.garrs[town->id] = town->visitingHero->army;
  671. sendAndApply(&sg);
  672. SetHeroesInTown intown;
  673. intown.tid = tid;
  674. intown.garrison = town->visitingHero->id;
  675. intown.visiting = town->garrisonHero->id;
  676. sendAndApply(&intown);
  677. }
  678. else
  679. {
  680. std::cout << "Warning, wrong garrison swap command for " << tid << std::endl;
  681. }
  682. break;
  683. }
  684. case 2001:
  685. {
  686. ui32 qid, answer;
  687. c >> qid >> answer;
  688. gsm.lock();
  689. boost::function<void(ui32)> callb = callbacks[qid];
  690. callbacks.erase(qid);
  691. gsm.unlock();
  692. callb(answer);
  693. break;
  694. }
  695. case 3002:
  696. {
  697. BattleAction ba;
  698. c >> ba;
  699. switch(ba.actionType)
  700. {
  701. case 2: //walk
  702. {
  703. moveStack(ba.stackNumber,ba.destinationTile);
  704. break;
  705. }
  706. case 3: //defend
  707. {
  708. break;
  709. }
  710. case 4: //retreat/flee
  711. {
  712. //TODO: check if fleeing is possible (e.g. enemy may have Shackles of War)
  713. //TODO: calculate casualties
  714. //TODO: remove retreating hero from map and place it in recrutation list
  715. BattleResult *br = new BattleResult;
  716. br->result = 1;
  717. battleResult.set(br);
  718. break;
  719. }
  720. case 6: //walk or attack
  721. {
  722. moveStack(ba.stackNumber,ba.destinationTile);
  723. CStack *curStack = gs->curB->getStack(ba.stackNumber),
  724. *stackAtEnd = gs->curB->getStackT(ba.additionalInfo);
  725. if((curStack->position != ba.destinationTile) || //we wasn't able to reach destination tile
  726. (BattleInfo::mutualPosition(ba.destinationTile,ba.additionalInfo)<0) ) //destination tile is not neighbouring with enemy stack
  727. return;
  728. BattleAttack bat;
  729. prepareAttack(bat,curStack,stackAtEnd);
  730. sendAndApply(&bat);
  731. break;
  732. }
  733. case 7: //shoot
  734. {
  735. CStack *curStack = gs->curB->getStack(ba.stackNumber),
  736. *destStack= gs->curB->getStackT(ba.destinationTile);
  737. BattleAttack bat;
  738. prepareAttack(bat,curStack,destStack);
  739. bat.flags |= 1;
  740. sendAndApply(&bat);
  741. break;
  742. }
  743. }
  744. battleMadeAction.setn(true);
  745. //sprawdzic czy po tej akcji ktoras strona nie wygrala bitwy
  746. break;
  747. }
  748. default:
  749. #ifndef __GNUC__
  750. throw std::exception("Not supported client message!");
  751. #else
  752. throw std::exception();
  753. #endif
  754. break;
  755. }
  756. }
  757. }
  758. catch (const std::exception& e)
  759. {
  760. std::cerr << e.what() << std::endl;
  761. end2 = true;
  762. }
  763. catch (const std::exception * e)
  764. {
  765. std::cerr << e->what()<< std::endl;
  766. end2 = true;
  767. delete e;
  768. }
  769. catch(...)
  770. {
  771. end2 = true;
  772. }
  773. handleConEnd:
  774. ;
  775. }
  776. void CGameHandler::moveStack(int stack, int dest)
  777. {
  778. CStack *curStack = gs->curB->getStack(stack),
  779. *stackAtEnd = gs->curB->getStackT(dest);
  780. //initing necessary tables
  781. bool accessibility[187];
  782. if(curStack->creature->isDoubleWide())
  783. gs->curB->getAccessibilityMapForTwoHex(accessibility,curStack->attackerOwned,curStack->ID);
  784. else
  785. gs->curB->getAccessibilityMap(accessibility,curStack->ID);
  786. if((stackAtEnd && stackAtEnd->alive) || !accessibility[dest])
  787. return;
  788. //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
  789. // return false;
  790. std::vector<int> path = gs->curB->getPath(curStack->position,dest,accessibility);
  791. int tilesToMove = std::max((int)path.size()-curStack->creature->speed, 0);
  792. for(int v=path.size()-1; v>=tilesToMove; --v)
  793. {
  794. //inform clients about move
  795. BattleStackMoved sm;
  796. sm.stack = curStack->ID;
  797. sm.tile = path[v];
  798. if(v==path.size()-1) //move start - set flag
  799. sm.flags |= 1;
  800. if(v==0) //move end - set flag
  801. sm.flags |= 2;
  802. sendAndApply(&sm);
  803. }
  804. }
  805. CGameHandler::CGameHandler(void)
  806. {
  807. gs = NULL;
  808. }
  809. CGameHandler::~CGameHandler(void)
  810. {
  811. delete gs;
  812. }
  813. void CGameHandler::init(StartInfo *si, int Seed)
  814. {
  815. Mapa *map = new Mapa(si->mapname);
  816. gs = new CGameState();
  817. gs->init(si,map,Seed);
  818. /****************************LUA OBJECT SCRIPTS************************************************/
  819. //std::vector<std::string> * lf = CLuaHandler::searchForScripts("scripts/lua/objects"); //files
  820. //for (int i=0; i<lf->size(); i++)
  821. //{
  822. // try
  823. // {
  824. // std::vector<std::string> * temp = CLuaHandler::functionList((*lf)[i]);
  825. // CLuaObjectScript * objs = new CLuaObjectScript((*lf)[i]);
  826. // CLuaCallback::registerFuncs(objs->is);
  827. // //objs
  828. // for (int j=0; j<temp->size(); j++)
  829. // {
  830. // int obid ; //obj ID
  831. // int dspos = (*temp)[j].find_first_of('_');
  832. // obid = atoi((*temp)[j].substr(dspos+1,(*temp)[j].size()-dspos-1).c_str());
  833. // std::string fname = (*temp)[j].substr(0,dspos);
  834. // if (skrypty->find(obid)==skrypty->end())
  835. // skrypty->insert(std::pair<int, std::map<std::string, CObjectScript*> >(obid,std::map<std::string,CObjectScript*>()));
  836. // (*skrypty)[obid].insert(std::pair<std::string, CObjectScript*>(fname,objs));
  837. // }
  838. // delete temp;
  839. // }HANDLE_EXCEPTION
  840. //}
  841. //delete lf;
  842. }
  843. int lowestSpeed(CGHeroInstance * chi)
  844. {
  845. std::map<si32,std::pair<ui32,si32> >::iterator i = chi->army.slots.begin();
  846. int ret = VLC->creh->creatures[(*i++).second.first].speed;
  847. for (;i!=chi->army.slots.end();i++)
  848. {
  849. ret = std::min(ret,VLC->creh->creatures[(*i).second.first].speed);
  850. }
  851. return ret;
  852. }
  853. int valMovePoints(CGHeroInstance * chi)
  854. {
  855. int ret = 1270+70*lowestSpeed(chi);
  856. if (ret>2000)
  857. ret=2000;
  858. //TODO: additional bonuses (but they aren't currently stored in chi)
  859. return ret;
  860. }
  861. void CGameHandler::newTurn()
  862. {
  863. NewTurn n;
  864. n.day = gs->day + 1;
  865. n.resetBuilded = true;
  866. for ( std::map<ui8, PlayerState>::iterator i=gs->players.begin() ; i!=gs->players.end();i++)
  867. {
  868. if(i->first>=PLAYER_LIMIT) continue;
  869. SetResources r;
  870. r.player = i->first;
  871. for(int j=0;j<RESOURCE_QUANTITY;j++)
  872. r.res[j] = i->second.resources[j];
  873. for (unsigned j=0;j<(*i).second.heroes.size();j++) //handle heroes
  874. {
  875. NewTurn::Hero h;
  876. h.id = (*i).second.heroes[j]->id;
  877. h.move = valMovePoints((*i).second.heroes[j]);
  878. h.mana = (*i).second.heroes[j]->mana;
  879. n.heroes.insert(h);
  880. }
  881. for(std::vector<CGTownInstance *>::iterator j=i->second.towns.begin();j!=i->second.towns.end();j++)//handle towns
  882. {
  883. if(gs->getDate(1)==7) //first day of week
  884. {
  885. SetAvailableCreatures sac;
  886. sac.tid = (**j).id;
  887. sac.creatures = (**j).strInfo.creatures;
  888. for(int k=0;k<CREATURES_PER_TOWN;k++) //creature growths
  889. {
  890. if((**j).creatureDwelling(k))//there is dwelling (k-level)
  891. sac.creatures[k] += (**j).creatureGrowth(k);
  892. }
  893. n.cres.push_back(sac);
  894. }
  895. if((gs->day) && i->first<PLAYER_LIMIT)//not the first day and town not neutral
  896. r.res[6] += (**j).dailyIncome();
  897. }
  898. n.res.push_back(r);
  899. }
  900. sendAndApply(&n);
  901. for (std::set<CCPPObjectScript *>::iterator i=cppscripts.begin();i!=cppscripts.end();i++)
  902. {
  903. (*i)->newTurn();
  904. }
  905. }
  906. void CGameHandler::run()
  907. {
  908. BOOST_FOREACH(CConnection *cc, conns)
  909. {//init conn.
  910. ui8 quantity, pom;
  911. //ui32 seed;
  912. (*cc) << gs->scenarioOps->mapname << gs->map->checksum << gs->seed;
  913. (*cc) >> quantity;
  914. for(int i=0;i<quantity;i++)
  915. {
  916. (*cc) >> pom;
  917. gsm.lock();
  918. connections[pom] = cc;
  919. gsm.unlock();
  920. }
  921. }
  922. for(std::set<CConnection*>::iterator i = conns.begin(); i!=conns.end();i++)
  923. {
  924. std::set<int> pom;
  925. for(std::map<int,CConnection*>::iterator j = connections.begin(); j!=connections.end();j++)
  926. if(j->second == *i)
  927. pom.insert(j->first);
  928. boost::thread(boost::bind(&CGameHandler::handleConnection,this,pom,boost::ref(**i)));
  929. }
  930. /****************************SCRIPTS************************************************/
  931. //std::map<int, std::map<std::string, CObjectScript*> > * skrypty = &objscr; //alias for easier access
  932. /****************************C++ OBJECT SCRIPTS************************************************/
  933. std::map<int,CCPPObjectScript*> scripts;
  934. CScriptCallback * csc = new CScriptCallback();
  935. csc->gh = this;
  936. handleCPPObjS(&scripts,new CVisitableOPH(csc));
  937. handleCPPObjS(&scripts,new CVisitableOPW(csc));
  938. handleCPPObjS(&scripts,new CPickable(csc));
  939. handleCPPObjS(&scripts,new CMines(csc));
  940. handleCPPObjS(&scripts,new CTownScript(csc));
  941. handleCPPObjS(&scripts,new CHeroScript(csc));
  942. handleCPPObjS(&scripts,new CMonsterS(csc));
  943. handleCPPObjS(&scripts,new CCreatureGen(csc));
  944. /****************************INITIALIZING OBJECT SCRIPTS************************************************/
  945. //std::string temps("newObject");
  946. for (unsigned i=0; i<gs->map->objects.size(); i++)
  947. {
  948. //c++ scripts
  949. if (scripts.find(gs->map->objects[i]->ID) != scripts.end())
  950. {
  951. gs->map->objects[i]->state = scripts[gs->map->objects[i]->ID];
  952. gs->map->objects[i]->state->newObject(gs->map->objects[i]->id);
  953. }
  954. else
  955. {
  956. gs->map->objects[i]->state = NULL;
  957. }
  958. //// lua scripts
  959. //if(checkFunc(map->objects[i]->ID,temps))
  960. // (*skrypty)[map->objects[i]->ID][temps]->newObject(map->objects[i]);
  961. }
  962. for(std::map<ui8,PlayerState>::iterator i = gs->players.begin(); i != gs->players.end(); i++)
  963. states.addPlayer(i->first);
  964. while (!end2)
  965. {
  966. newTurn();
  967. for(std::map<ui8,PlayerState>::iterator i = gs->players.begin(); i != gs->players.end(); i++)
  968. {
  969. 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
  970. states.setFlag(i->first,&PlayerStatus::makingTurn,true);
  971. gs->currentPlayer = i->first;
  972. *connections[i->first] << ui16(100) << i->first;
  973. //wait till turn is done
  974. boost::unique_lock<boost::mutex> lock(states.mx);
  975. while(states.players[i->first].makingTurn && !end2)
  976. {
  977. boost::posix_time::time_duration p;
  978. p= boost::posix_time::seconds(1);
  979. #ifdef _MSC_VER
  980. states.cv.timed_wait(lock,p);
  981. #else
  982. boost::xtime time={0,0};
  983. time.sec = static_cast<boost::xtime::xtime_sec_t>(p.total_seconds());
  984. states.cv.timed_wait(lock,time);
  985. #endif
  986. }
  987. }
  988. }
  989. }
  990. void CGameHandler::setupBattle( BattleInfo * curB, int3 tile, CCreatureSet &army1, CCreatureSet &army2, CGHeroInstance * hero1, CGHeroInstance * hero2 )
  991. {
  992. battleResult.set(NULL);
  993. std::vector<CStack*> & stacks = (curB->stacks);
  994. curB->tile = tile;
  995. curB->siege = 0; //TODO: add sieges
  996. curB->army1=army1;
  997. curB->army2=army2;
  998. curB->hero1=(hero1)?(hero1->id):(-1);
  999. curB->hero2=(hero2)?(hero2->id):(-1);
  1000. curB->side1=(hero1)?(hero1->tempOwner):(-1);
  1001. curB->side2=(hero2)?(hero2->tempOwner):(-1);
  1002. curB->round = -2;
  1003. curB->activeStack = -1;
  1004. for(std::map<si32,std::pair<ui32,si32> >::iterator i = army1.slots.begin(); i!=army1.slots.end(); i++)
  1005. {
  1006. stacks.push_back(new CStack(&VLC->creh->creatures[i->second.first],i->second.second,hero1->tempOwner, stacks.size(), true));
  1007. stacks[stacks.size()-1]->ID = stacks.size()-1;
  1008. }
  1009. //initialization of positions
  1010. switch(army1.slots.size()) //for attacker
  1011. {
  1012. case 0:
  1013. break;
  1014. case 1:
  1015. stacks[0]->position = 86; //6
  1016. break;
  1017. case 2:
  1018. stacks[0]->position = 35; //3
  1019. stacks[1]->position = 137; //9
  1020. break;
  1021. case 3:
  1022. stacks[0]->position = 35; //3
  1023. stacks[1]->position = 86; //6
  1024. stacks[2]->position = 137; //9
  1025. break;
  1026. case 4:
  1027. stacks[0]->position = 1; //1
  1028. stacks[1]->position = 69; //5
  1029. stacks[2]->position = 103; //7
  1030. stacks[3]->position = 171; //11
  1031. break;
  1032. case 5:
  1033. stacks[0]->position = 1; //1
  1034. stacks[1]->position = 35; //3
  1035. stacks[2]->position = 86; //6
  1036. stacks[3]->position = 137; //9
  1037. stacks[4]->position = 171; //11
  1038. break;
  1039. case 6:
  1040. stacks[0]->position = 1; //1
  1041. stacks[1]->position = 35; //3
  1042. stacks[2]->position = 69; //5
  1043. stacks[3]->position = 103; //7
  1044. stacks[4]->position = 137; //9
  1045. stacks[5]->position = 171; //11
  1046. break;
  1047. case 7:
  1048. stacks[0]->position = 1; //1
  1049. stacks[1]->position = 35; //3
  1050. stacks[2]->position = 69; //5
  1051. stacks[3]->position = 86; //6
  1052. stacks[4]->position = 103; //7
  1053. stacks[5]->position = 137; //9
  1054. stacks[6]->position = 171; //11
  1055. break;
  1056. default: //fault
  1057. break;
  1058. }
  1059. for(std::map<si32,std::pair<ui32,si32> >::iterator i = army2.slots.begin(); i!=army2.slots.end(); i++)
  1060. stacks.push_back(new CStack(&VLC->creh->creatures[i->second.first],i->second.second,hero2 ? hero2->tempOwner : 255, stacks.size(), false));
  1061. switch(army2.slots.size()) //for defender
  1062. {
  1063. case 0:
  1064. break;
  1065. case 1:
  1066. stacks[0+army1.slots.size()]->position = 100; //6
  1067. break;
  1068. case 2:
  1069. stacks[0+army1.slots.size()]->position = 49; //3
  1070. stacks[1+army1.slots.size()]->position = 151; //9
  1071. break;
  1072. case 3:
  1073. stacks[0+army1.slots.size()]->position = 49; //3
  1074. stacks[1+army1.slots.size()]->position = 100; //6
  1075. stacks[2+army1.slots.size()]->position = 151; //9
  1076. break;
  1077. case 4:
  1078. stacks[0+army1.slots.size()]->position = 15; //1
  1079. stacks[1+army1.slots.size()]->position = 83; //5
  1080. stacks[2+army1.slots.size()]->position = 117; //7
  1081. stacks[3+army1.slots.size()]->position = 185; //11
  1082. break;
  1083. case 5:
  1084. stacks[0+army1.slots.size()]->position = 15; //1
  1085. stacks[1+army1.slots.size()]->position = 49; //3
  1086. stacks[2+army1.slots.size()]->position = 100; //6
  1087. stacks[3+army1.slots.size()]->position = 151; //9
  1088. stacks[4+army1.slots.size()]->position = 185; //11
  1089. break;
  1090. case 6:
  1091. stacks[0+army1.slots.size()]->position = 15; //1
  1092. stacks[1+army1.slots.size()]->position = 49; //3
  1093. stacks[2+army1.slots.size()]->position = 83; //5
  1094. stacks[3+army1.slots.size()]->position = 117; //7
  1095. stacks[4+army1.slots.size()]->position = 151; //9
  1096. stacks[5+army1.slots.size()]->position = 185; //11
  1097. break;
  1098. case 7:
  1099. stacks[0+army1.slots.size()]->position = 15; //1
  1100. stacks[1+army1.slots.size()]->position = 49; //3
  1101. stacks[2+army1.slots.size()]->position = 83; //5
  1102. stacks[3+army1.slots.size()]->position = 100; //6
  1103. stacks[4+army1.slots.size()]->position = 117; //7
  1104. stacks[5+army1.slots.size()]->position = 151; //9
  1105. stacks[6+army1.slots.size()]->position = 185; //11
  1106. break;
  1107. default: //fault
  1108. break;
  1109. }
  1110. for(unsigned g=0; g<stacks.size(); ++g) //shifting positions of two-hex creatures
  1111. {
  1112. if((stacks[g]->position%17)==1 && stacks[g]->creature->isDoubleWide())
  1113. {
  1114. stacks[g]->position += 1;
  1115. }
  1116. else if((stacks[g]->position%17)==15 && stacks[g]->creature->isDoubleWide())
  1117. {
  1118. stacks[g]->position -= 1;
  1119. }
  1120. }
  1121. std::stable_sort(stacks.begin(),stacks.end(),cmpst);
  1122. //block engaged players
  1123. if(hero1->tempOwner<PLAYER_LIMIT)
  1124. states.setFlag(hero1->tempOwner,&PlayerStatus::engagedIntoBattle,true);
  1125. if(hero2 && hero2->tempOwner<PLAYER_LIMIT)
  1126. states.setFlag(hero2->tempOwner,&PlayerStatus::engagedIntoBattle,true);
  1127. //send info about battles
  1128. BattleStart bs;
  1129. bs.info = curB;
  1130. sendAndApply(&bs);
  1131. }