CGameHandler.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833
  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 "../CLua.h"
  14. #include "../hch/CObjectHandler.h"
  15. #include "../hch/CTownHandler.h"
  16. #include "../hch/CBuildingHandler.h"
  17. #include "../hch/CHeroHandler.h"
  18. #include "boost/date_time/posix_time/posix_time_types.hpp" //no i/o just types
  19. #include "../lib/VCMI_Lib.h"
  20. #include "../lib/CondSh.h"
  21. #include <boost/thread/xtime.hpp>
  22. extern bool end2;
  23. #include "../lib/BattleAction.h"
  24. #ifdef min
  25. #undef min
  26. #endif
  27. #ifdef max
  28. #undef max
  29. #endif
  30. #define NEW_ROUND BattleNextRound bnr;\
  31. bnr.round = gs->curB->round + 1;\
  32. sendAndApply(&bnr);
  33. boost::condition_variable cTurn;
  34. boost::mutex mTurn;
  35. boost::shared_mutex gsm;
  36. CondSh<bool> battleMadeAction;
  37. CondSh<BattleResult *> battleResult(NULL);
  38. class CMP_stack
  39. {
  40. public:
  41. bool operator ()(const CStack* a, const CStack* b)
  42. {
  43. return (a->creature->speed)>(b->creature->speed);
  44. }
  45. } cmpst ;
  46. double distance(int3 a, int3 b)
  47. {
  48. return std::sqrt( (double)(a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y) );
  49. }
  50. //bool CGameState::checkFunc(int obid, std::string name)
  51. //{
  52. // if (objscr.find(obid)!=objscr.end())
  53. // {
  54. // if(objscr[obid].find(name)!=objscr[obid].end())
  55. // {
  56. // return true;
  57. // }
  58. // }
  59. // return false;
  60. //}
  61. void CGameHandler::handleCPPObjS(std::map<int,CCPPObjectScript*> * mapa, CCPPObjectScript * script)
  62. {
  63. std::vector<int> tempv = script->yourObjects();
  64. for (unsigned i=0;i<tempv.size();i++)
  65. {
  66. (*mapa)[tempv[i]]=script;
  67. }
  68. cppscripts.insert(script);
  69. }
  70. void CGameHandler::changePrimSkill(int ID, int which, int val, bool abs)
  71. {
  72. SetPrimSkill sps;
  73. sps.id = ID;
  74. sps.which = which;
  75. sps.abs = abs;
  76. sps.val = val;
  77. sendAndApply(&sps);
  78. if(which==4)
  79. {
  80. CGHeroInstance *hero = static_cast<CGHeroInstance *>(gs->map->objects[ID]);
  81. if(hero->exp >= VLC->heroh->reqExp(hero->level+1)) //new level
  82. {
  83. //hero->level++;
  84. //give prim skill
  85. std::cout << hero->name <<" got level "<<hero->level<<std::endl;
  86. int r = rand()%100, pom=0, x=0;
  87. int std::pair<int,int>::*g = (hero->level>9) ? (&std::pair<int,int>::second) : (&std::pair<int,int>::first);
  88. for(;x<PRIMARY_SKILLS;x++)
  89. {
  90. pom += hero->type->heroClass->primChance[x].*g;
  91. if(r<pom)
  92. break;
  93. }
  94. std::cout << "Bohater dostaje umiejetnosc pierwszorzedna " << x << " (wynik losowania "<<r<<")"<<std::endl;
  95. SetPrimSkill sps;
  96. sps.id = ID;
  97. sps.which = x;
  98. sps.abs = false;
  99. sps.val = 1;
  100. sendAndApply(&sps);
  101. hero->primSkills[x]++;
  102. std::set<ui16> choice;
  103. std::set<int> basicAndAdv, expert, none;
  104. for(int i=0;i<SKILL_QUANTITY;i++) none.insert(i);
  105. for(unsigned i=0;i<hero->secSkills.size();i++)
  106. {
  107. if(hero->secSkills[i].second < 2)
  108. basicAndAdv.insert(hero->secSkills[i].first);
  109. else
  110. expert.insert(hero->secSkills[i].first);
  111. none.erase(hero->secSkills[i].first);
  112. }
  113. if(hero->secSkills.size() < hero->type->heroClass->skillLimit) //free skill slot
  114. {
  115. choice.insert(hero->type->heroClass->chooseSecSkill(none)); //new skill
  116. }
  117. else
  118. {
  119. int s = hero->type->heroClass->chooseSecSkill(basicAndAdv);
  120. choice.insert(s);
  121. basicAndAdv.erase(s);
  122. }
  123. if(basicAndAdv.size())
  124. {
  125. choice.insert(hero->type->heroClass->chooseSecSkill(basicAndAdv)); //new skill
  126. }
  127. else if(hero->secSkills.size() < hero->type->heroClass->skillLimit)
  128. {
  129. choice.insert(hero->type->heroClass->chooseSecSkill(none)); //new skill
  130. }
  131. }
  132. //TODO - powiadomic interfejsy, sprawdzic czy nie ma awansu itp
  133. }
  134. }
  135. void CGameHandler::startBattle(CCreatureSet army1, CCreatureSet army2, int3 tile, CGHeroInstance *hero1, CGHeroInstance *hero2)
  136. {
  137. BattleInfo *curB = new BattleInfo;
  138. //battle start
  139. {
  140. battleResult.set(NULL);
  141. std::vector<CStack*> & stacks = (curB->stacks);
  142. curB->army1=army1;
  143. curB->army2=army2;
  144. curB->hero1=(hero1)?(hero1->id):(-1);
  145. curB->hero2=(hero1)?(hero1->id):(-1);
  146. curB->side1=(hero1)?(hero1->tempOwner):(-1);
  147. curB->side2=(hero2)?(hero2->tempOwner):(-1);
  148. curB->round = -2;
  149. curB->activeStack = -1;
  150. for(std::map<si32,std::pair<ui32,si32> >::iterator i = army1.slots.begin(); i!=army1.slots.end(); i++)
  151. {
  152. stacks.push_back(new CStack(&VLC->creh->creatures[i->second.first],i->second.second,hero1->tempOwner, stacks.size(), true));
  153. stacks[stacks.size()-1]->ID = stacks.size()-1;
  154. }
  155. //initialization of positions
  156. switch(army1.slots.size()) //for attacker
  157. {
  158. case 0:
  159. break;
  160. case 1:
  161. stacks[0]->position = 86; //6
  162. break;
  163. case 2:
  164. stacks[0]->position = 35; //3
  165. stacks[1]->position = 137; //9
  166. break;
  167. case 3:
  168. stacks[0]->position = 35; //3
  169. stacks[1]->position = 86; //6
  170. stacks[2]->position = 137; //9
  171. break;
  172. case 4:
  173. stacks[0]->position = 1; //1
  174. stacks[1]->position = 69; //5
  175. stacks[2]->position = 103; //7
  176. stacks[3]->position = 171; //11
  177. break;
  178. case 5:
  179. stacks[0]->position = 1; //1
  180. stacks[1]->position = 35; //3
  181. stacks[2]->position = 86; //6
  182. stacks[3]->position = 137; //9
  183. stacks[4]->position = 171; //11
  184. break;
  185. case 6:
  186. stacks[0]->position = 1; //1
  187. stacks[1]->position = 35; //3
  188. stacks[2]->position = 69; //5
  189. stacks[3]->position = 103; //7
  190. stacks[4]->position = 137; //9
  191. stacks[5]->position = 171; //11
  192. break;
  193. case 7:
  194. stacks[0]->position = 1; //1
  195. stacks[1]->position = 35; //3
  196. stacks[2]->position = 69; //5
  197. stacks[3]->position = 86; //6
  198. stacks[4]->position = 103; //7
  199. stacks[5]->position = 137; //9
  200. stacks[6]->position = 171; //11
  201. break;
  202. default: //fault
  203. break;
  204. }
  205. for(std::map<si32,std::pair<ui32,si32> >::iterator i = army2.slots.begin(); i!=army2.slots.end(); i++)
  206. stacks.push_back(new CStack(&VLC->creh->creatures[i->second.first],i->second.second,hero2 ? hero2->tempOwner : 255, stacks.size(), false));
  207. switch(army2.slots.size()) //for defender
  208. {
  209. case 0:
  210. break;
  211. case 1:
  212. stacks[0+army1.slots.size()]->position = 100; //6
  213. break;
  214. case 2:
  215. stacks[0+army1.slots.size()]->position = 49; //3
  216. stacks[1+army1.slots.size()]->position = 151; //9
  217. break;
  218. case 3:
  219. stacks[0+army1.slots.size()]->position = 49; //3
  220. stacks[1+army1.slots.size()]->position = 100; //6
  221. stacks[2+army1.slots.size()]->position = 151; //9
  222. break;
  223. case 4:
  224. stacks[0+army1.slots.size()]->position = 15; //1
  225. stacks[1+army1.slots.size()]->position = 83; //5
  226. stacks[2+army1.slots.size()]->position = 117; //7
  227. stacks[3+army1.slots.size()]->position = 185; //11
  228. break;
  229. case 5:
  230. stacks[0+army1.slots.size()]->position = 15; //1
  231. stacks[1+army1.slots.size()]->position = 49; //3
  232. stacks[2+army1.slots.size()]->position = 100; //6
  233. stacks[3+army1.slots.size()]->position = 151; //9
  234. stacks[4+army1.slots.size()]->position = 185; //11
  235. break;
  236. case 6:
  237. stacks[0+army1.slots.size()]->position = 15; //1
  238. stacks[1+army1.slots.size()]->position = 49; //3
  239. stacks[2+army1.slots.size()]->position = 83; //5
  240. stacks[3+army1.slots.size()]->position = 117; //7
  241. stacks[4+army1.slots.size()]->position = 151; //9
  242. stacks[5+army1.slots.size()]->position = 185; //11
  243. break;
  244. case 7:
  245. stacks[0+army1.slots.size()]->position = 15; //1
  246. stacks[1+army1.slots.size()]->position = 49; //3
  247. stacks[2+army1.slots.size()]->position = 83; //5
  248. stacks[3+army1.slots.size()]->position = 100; //6
  249. stacks[4+army1.slots.size()]->position = 117; //7
  250. stacks[5+army1.slots.size()]->position = 151; //9
  251. stacks[6+army1.slots.size()]->position = 185; //11
  252. break;
  253. default: //fault
  254. break;
  255. }
  256. for(unsigned g=0; g<stacks.size(); ++g) //shifting positions of two-hex creatures
  257. {
  258. if((stacks[g]->position%17)==1 && stacks[g]->creature->isDoubleWide())
  259. {
  260. stacks[g]->position += 1;
  261. }
  262. else if((stacks[g]->position%17)==15 && stacks[g]->creature->isDoubleWide())
  263. {
  264. stacks[g]->position -= 1;
  265. }
  266. }
  267. std::stable_sort(stacks.begin(),stacks.end(),cmpst);
  268. //block engaged players
  269. if(hero1->tempOwner<PLAYER_LIMIT)
  270. states[hero1->tempOwner] += 10;
  271. if(hero2 && hero2->tempOwner<PLAYER_LIMIT)
  272. states[hero2->tempOwner] += 10;
  273. //send info about battles
  274. BattleStart bs;
  275. bs.info = curB;
  276. sendAndApply(&bs);
  277. NEW_ROUND;
  278. }
  279. //tactic round
  280. {
  281. NEW_ROUND;
  282. if( (hero1 && hero1->getSecSkillLevel(19)>=0) ||
  283. ( hero2 && hero2->getSecSkillLevel(19)>=0) )//someone has tactics
  284. {
  285. //TODO: tactic round (round -1)
  286. }
  287. }
  288. //main loop
  289. while(!battleResult.get()) //till the end of the battle ;]
  290. {
  291. NEW_ROUND;
  292. std::vector<CStack*> & stacks = (gs->curB->stacks);
  293. const BattleInfo & curB = *gs->curB;
  294. //stack loop
  295. for(unsigned i=0;i<stacks.size() && !battleResult.get();i++)
  296. {
  297. if(!stacks[i]->alive) continue;//indicates imposiibility of making action for this dead unit
  298. BattleSetActiveStack sas;
  299. sas.stack = i;
  300. sendAndApply(&sas);
  301. //wait for response about battle action
  302. boost::unique_lock<boost::mutex> lock(battleMadeAction.mx);
  303. while(!battleMadeAction.data)
  304. battleMadeAction.cond.wait(lock);
  305. }
  306. }
  307. //end battle, remove all info, free memory
  308. sendAndApply(battleResult.data);
  309. delete battleResult.data;
  310. //for(int i=0;i<stacks.size();i++)
  311. // delete stacks[i];
  312. //delete curB;
  313. //curB = NULL;
  314. }
  315. void CGameHandler::handleConnection(std::set<int> players, CConnection &c)
  316. {
  317. try
  318. {
  319. ui16 pom;
  320. while(!end2)
  321. {
  322. c >> pom;
  323. bool blockvis = false;
  324. switch(pom)
  325. {
  326. case 100: //my interface ended its turn
  327. {
  328. mTurn.lock();
  329. states[gs->currentPlayer] = 0;
  330. mTurn.unlock();
  331. cTurn.notify_all();
  332. break;
  333. }
  334. case 500:
  335. {
  336. si32 id;
  337. c >> id;
  338. RemoveHero rh(id);
  339. sendAndApply(&rh);
  340. break;
  341. }
  342. case 501://interface wants to move hero
  343. {
  344. int3 start, end;
  345. si32 id;
  346. c >> id >> start >> end;
  347. int3 hmpos = end + int3(-1,0,0);
  348. TerrainTile t = gs->map->terrain[hmpos.x][hmpos.y][hmpos.z];
  349. CGHeroInstance *h = static_cast<CGHeroInstance *>(gs->map->objects[id]);
  350. int cost = (int)((double)h->getTileCost(t.tertype,t.malle,t.nuine) * distance(start,end));
  351. TryMoveHero tmh;
  352. tmh.id = id;
  353. tmh.start = start;
  354. tmh.end = end;
  355. tmh.result = 0;
  356. tmh.movePoints = h->movement;
  357. if((h->getOwner() != gs->currentPlayer) || //not turn of that hero
  358. (distance(start,end)>=1.5) || //tiles are not neighouring
  359. (h->movement < cost) || //lack of movement points
  360. (t.tertype == rock) || //rock
  361. (!h->canWalkOnSea() && t.tertype == water) ||
  362. (t.blocked && !t.visitable) ) //tile is blocked andnot visitable
  363. goto fail;
  364. //check if there is blocking visitable object
  365. blockvis = false;
  366. tmh.movePoints = h->movement = (h->movement-cost); //take move points
  367. BOOST_FOREACH(CGObjectInstance *obj, t.visitableObjects)
  368. {
  369. if(obj->blockVisit)
  370. {
  371. blockvis = true;
  372. break;
  373. }
  374. }
  375. //we start moving
  376. if(blockvis)//interaction with blocking object (like resources)
  377. {
  378. sendAndApply(&tmh); //failed to move to that tile but we visit object
  379. BOOST_FOREACH(CGObjectInstance *obj, t.visitableObjects)
  380. {
  381. if (obj->blockVisit)
  382. {
  383. //if(gs->checkFunc(obj->ID,"heroVisit")) //script function
  384. // gs->objscr[obj->ID]["heroVisit"]->onHeroVisit(obj,h->subID);
  385. if(obj->state) //hard-coded function
  386. obj->state->onHeroVisit(obj->id,h->subID);
  387. }
  388. }
  389. break;
  390. }
  391. else //normal move
  392. {
  393. tmh.result = 1;
  394. BOOST_FOREACH(CGObjectInstance *obj, gs->map->terrain[start.x][start.y][start.z].visitableObjects)
  395. {
  396. //TODO: allow to handle this in script-languages
  397. if(obj->state) //hard-coded function
  398. obj->state->onHeroLeave(obj->id,h->subID);
  399. }
  400. //reveal fog of war
  401. int heroSight = h->getSightDistance();
  402. int xbeg = start.x - heroSight - 2;
  403. if(xbeg < 0)
  404. xbeg = 0;
  405. int xend = start.x + heroSight + 2;
  406. if(xend >= gs->map->width)
  407. xend = gs->map->width;
  408. int ybeg = start.y - heroSight - 2;
  409. if(ybeg < 0)
  410. ybeg = 0;
  411. int yend = start.y + heroSight + 2;
  412. if(yend >= gs->map->height)
  413. yend = gs->map->height;
  414. for(int xd=xbeg; xd<xend; ++xd) //revealing part of map around heroes
  415. {
  416. for(int yd=ybeg; yd<yend; ++yd)
  417. {
  418. int deltaX = (hmpos.x-xd)*(hmpos.x-xd);
  419. int deltaY = (hmpos.y-yd)*(hmpos.y-yd);
  420. if(deltaX+deltaY<h->getSightDistance()*h->getSightDistance())
  421. {
  422. if(gs->players[h->getOwner()].fogOfWarMap[xd][yd][hmpos.z] == 0)
  423. {
  424. tmh.fowRevealed.insert(int3(xd,yd,hmpos.z));
  425. }
  426. }
  427. }
  428. }
  429. sendAndApply(&tmh);
  430. BOOST_FOREACH(CGObjectInstance *obj, t.visitableObjects)//call objects if they are visited
  431. {
  432. //if(gs->checkFunc(obj->ID,"heroVisit")) //script function
  433. // gs->objscr[obj->ID]["heroVisit"]->onHeroVisit(obj,h->subID);
  434. if(obj->state) //hard-coded function
  435. obj->state->onHeroVisit(obj->id,h->id);
  436. }
  437. }
  438. break;
  439. fail:
  440. sendAndApply(&tmh);
  441. break;
  442. }
  443. case 502: //swap creatures in garrison
  444. {
  445. ui8 what, p1, p2; si32 id1, id2;
  446. c >> what >> id1 >> p1 >> id2 >> p2;
  447. CArmedInstance *s1 = static_cast<CArmedInstance*>(gs->map->objects[id1]),
  448. *s2 = static_cast<CArmedInstance*>(gs->map->objects[id2]);
  449. CCreatureSet *S1 = &s1->army, *S2 = &s2->army;
  450. if(what==1) //swap
  451. {
  452. int pom = S2->slots[p2].first;
  453. S2->slots[p2].first = S1->slots[p1].first;
  454. S1->slots[p1].first = pom;
  455. int pom2 = S2->slots[p2].second;
  456. S2->slots[p2].second = S1->slots[p1].second;
  457. S1->slots[p1].second = pom2;
  458. if(!S1->slots[p1].first)
  459. S1->slots.erase(p1);
  460. if(!S2->slots[p2].first)
  461. S2->slots.erase(p2);
  462. }
  463. else if(what==2)//merge
  464. {
  465. if(S1->slots[p1].first != S2->slots[p2].first) break; //not same creature
  466. S2->slots[p2].second += S1->slots[p1].second;
  467. S1->slots[p1].first = NULL;
  468. S1->slots[p1].second = 0;
  469. S1->slots.erase(p1);
  470. }
  471. else if(what==3) //split
  472. {
  473. si32 val;
  474. c >> val;
  475. if(S2->slots.find(p2) != S2->slots.end()) break; //slot not free
  476. S2->slots[p2].first = S1->slots[p1].first;
  477. S2->slots[p2].second = val;
  478. S1->slots[p1].second -= val;
  479. if(!S1->slots[p1].second) //if we've moved all creatures
  480. S1->slots.erase(p1);
  481. }
  482. SetGarrisons sg;
  483. sg.garrs[id1] = *S1;
  484. if(s1 != s2)
  485. sg.garrs[id2] = *S2;
  486. sendAndApply(&sg);
  487. break;
  488. }
  489. case 503:
  490. {
  491. si32 id;
  492. ui8 pos;
  493. c >> id >> pos;
  494. CArmedInstance *s1 = static_cast<CArmedInstance*>(gs->map->objects[id]);
  495. s1->army.slots.erase(pos);
  496. SetGarrisons sg;
  497. sg.garrs[id] = s1->army;
  498. sendAndApply(&sg);
  499. break;
  500. }
  501. case 504:
  502. {
  503. si32 tid, bid;
  504. c >> tid >> bid;
  505. CGTownInstance * t = static_cast<CGTownInstance*>(gs->map->objects[tid]);
  506. CBuilding * b = VLC->buildh->buildings[t->subID][bid];
  507. for(int i=0;i<RESOURCE_QUANTITY;i++)
  508. if(b->resources[i] > gs->players[t->tempOwner].resources[i])
  509. break; //no res
  510. //TODO: check requirements
  511. //TODO: check if building isn't forbidden
  512. NewStructures ns;
  513. ns.tid = tid;
  514. if(bid>36) //upg dwelling
  515. {
  516. if(t->getHordeLevel(0) == (bid-37))
  517. ns.bid.insert(19);
  518. else if(t->getHordeLevel(1) == (bid-37))
  519. ns.bid.insert(25);
  520. }
  521. else if(bid >= 30) //bas. dwelling
  522. {
  523. SetStrInfo ssi;
  524. ssi.tid = tid;
  525. ssi.cres = t->strInfo.creatures;
  526. ssi.cres[bid-30] = VLC->creh->creatures[t->town->basicCreatures[bid-30]].growth;
  527. sendAndApply(&ssi);
  528. }
  529. ns.bid.insert(bid);
  530. ns.builded = t->builded + 1;
  531. sendAndApply(&ns);
  532. SetResources sr;
  533. sr.player = t->tempOwner;
  534. sr.res = gs->players[t->tempOwner].resources;
  535. for(int i=0;i<7;i++)
  536. sr.res[i]-=b->resources[i];
  537. sendAndApply(&sr);
  538. break;
  539. }
  540. case 3002:
  541. {
  542. BattleAction ba;
  543. c >> ba;
  544. switch(ba.actionType)
  545. {
  546. case 2: //walk
  547. {
  548. //battleMoveCreatureStack(ba.stackNumber, ba.destinationTile);
  549. break;
  550. }
  551. case 3: //defend
  552. {
  553. break;
  554. }
  555. case 4: //retreat/flee
  556. {
  557. //TODO: check if fleeing is possible (e.g. enemy may have Shackles of War)
  558. //TODO: calculate casualties
  559. //TODO: remove retreating hero from map and place it in recrutation list
  560. BattleResult *br = new BattleResult;
  561. br->result = 1;
  562. battleResult.set(br);
  563. break;
  564. }
  565. case 6: //walk or attack
  566. {
  567. //battleMoveCreatureStack(ba.stackNumber, ba.destinationTile);
  568. //battleAttackCreatureStack(ba.stackNumber, ba.destinationTile);
  569. break;
  570. }
  571. case 7: //shoot
  572. {
  573. //battleShootCreatureStack(ba.stackNumber, ba.destinationTile);
  574. break;
  575. }
  576. }
  577. battleMadeAction.setn(true);
  578. //sprawdzic czy po tej akcji ktoras strona nie wygrala bitwy
  579. break;
  580. }
  581. default:
  582. #ifndef __GNUC__
  583. throw std::exception("Not supported client message!");
  584. #else
  585. throw std::exception();
  586. #endif
  587. break;
  588. }
  589. }
  590. }
  591. catch (const std::exception& e)
  592. {
  593. std::cerr << e.what() << std::endl;
  594. end2 = true;
  595. }
  596. catch (const std::exception * e)
  597. {
  598. std::cerr << e->what()<< std::endl;
  599. end2 = true;
  600. delete e;
  601. }
  602. catch(...)
  603. {
  604. end2 = true;
  605. }
  606. }
  607. CGameHandler::CGameHandler(void)
  608. {
  609. gs = NULL;
  610. }
  611. CGameHandler::~CGameHandler(void)
  612. {
  613. delete gs;
  614. }
  615. void CGameHandler::init(StartInfo *si, int Seed)
  616. {
  617. Mapa *map = new Mapa(si->mapname);
  618. gs = new CGameState();
  619. gs->init(si,map,Seed);
  620. /****************************LUA OBJECT SCRIPTS************************************************/
  621. //std::vector<std::string> * lf = CLuaHandler::searchForScripts("scripts/lua/objects"); //files
  622. //for (int i=0; i<lf->size(); i++)
  623. //{
  624. // try
  625. // {
  626. // std::vector<std::string> * temp = CLuaHandler::functionList((*lf)[i]);
  627. // CLuaObjectScript * objs = new CLuaObjectScript((*lf)[i]);
  628. // CLuaCallback::registerFuncs(objs->is);
  629. // //objs
  630. // for (int j=0; j<temp->size(); j++)
  631. // {
  632. // int obid ; //obj ID
  633. // int dspos = (*temp)[j].find_first_of('_');
  634. // obid = atoi((*temp)[j].substr(dspos+1,(*temp)[j].size()-dspos-1).c_str());
  635. // std::string fname = (*temp)[j].substr(0,dspos);
  636. // if (skrypty->find(obid)==skrypty->end())
  637. // skrypty->insert(std::pair<int, std::map<std::string, CObjectScript*> >(obid,std::map<std::string,CObjectScript*>()));
  638. // (*skrypty)[obid].insert(std::pair<std::string, CObjectScript*>(fname,objs));
  639. // }
  640. // delete temp;
  641. // }HANDLE_EXCEPTION
  642. //}
  643. //delete lf;
  644. }
  645. int lowestSpeed(CGHeroInstance * chi)
  646. {
  647. std::map<si32,std::pair<ui32,si32> >::iterator i = chi->army.slots.begin();
  648. int ret = VLC->creh->creatures[(*i++).second.first].speed;
  649. for (;i!=chi->army.slots.end();i++)
  650. {
  651. ret = std::min(ret,VLC->creh->creatures[(*i).second.first].speed);
  652. }
  653. return ret;
  654. }
  655. int valMovePoints(CGHeroInstance * chi)
  656. {
  657. int ret = 1270+70*lowestSpeed(chi);
  658. if (ret>2000)
  659. ret=2000;
  660. //TODO: additional bonuses (but they aren't currently stored in chi)
  661. return ret;
  662. }
  663. void CGameHandler::newTurn()
  664. {
  665. NewTurn n;
  666. n.day = gs->day + 1;
  667. for ( std::map<ui8, PlayerState>::iterator i=gs->players.begin() ; i!=gs->players.end();i++)
  668. {
  669. if(i->first>=PLAYER_LIMIT) continue;
  670. NewTurn::Resources r;
  671. r.player = i->first;
  672. for(int j=0;j<RESOURCE_QUANTITY;j++)
  673. r.resources[j] = i->second.resources[j];
  674. for (unsigned j=0;j<(*i).second.heroes.size();j++) //handle heroes
  675. {
  676. NewTurn::Hero h;
  677. h.id = (*i).second.heroes[j]->id;
  678. h.move = valMovePoints((*i).second.heroes[j]);
  679. h.mana = (*i).second.heroes[j]->mana;
  680. n.heroes.insert(h);
  681. }
  682. for(unsigned j=0;j<i->second.towns.size();j++)//handle towns
  683. {
  684. i->second.towns[j]->builded=0;
  685. //if(gs->getDate(1)==1) //first day of week
  686. //{
  687. // for(int k=0;k<CREATURES_PER_TOWN;k++) //creature growths
  688. // {
  689. // if(i->second.towns[j]->creatureDwelling(k))//there is dwelling (k-level)
  690. // i->second.towns[j]->strInfo.creatures[k]+=i->second.towns[j]->creatureGrowth(k);
  691. // }
  692. //}
  693. if((gs->day) && i->first<PLAYER_LIMIT)//not the first day and town not neutral
  694. r.resources[6] += i->second.towns[j]->dailyIncome();
  695. }
  696. n.res.insert(r);
  697. }
  698. sendAndApply(&n);
  699. for (std::set<CCPPObjectScript *>::iterator i=cppscripts.begin();i!=cppscripts.end();i++)
  700. {
  701. (*i)->newTurn();
  702. }
  703. }
  704. void CGameHandler::run()
  705. {
  706. BOOST_FOREACH(CConnection *cc, conns)
  707. {//init conn.
  708. ui8 quantity, pom;
  709. //ui32 seed;
  710. (*cc) << gs->scenarioOps->mapname << gs->map->checksum << gs->seed;
  711. (*cc) >> quantity;
  712. for(int i=0;i<quantity;i++)
  713. {
  714. (*cc) >> pom;
  715. gsm.lock();
  716. connections[pom] = cc;
  717. gsm.unlock();
  718. }
  719. }
  720. for(std::set<CConnection*>::iterator i = conns.begin(); i!=conns.end();i++)
  721. {
  722. std::set<int> pom;
  723. for(std::map<int,CConnection*>::iterator j = connections.begin(); j!=connections.end();j++)
  724. if(j->second == *i)
  725. pom.insert(j->first);
  726. boost::thread(boost::bind(&CGameHandler::handleConnection,this,pom,boost::ref(**i)));
  727. }
  728. /****************************SCRIPTS************************************************/
  729. //std::map<int, std::map<std::string, CObjectScript*> > * skrypty = &objscr; //alias for easier access
  730. /****************************C++ OBJECT SCRIPTS************************************************/
  731. std::map<int,CCPPObjectScript*> scripts;
  732. CScriptCallback * csc = new CScriptCallback();
  733. csc->gh = this;
  734. handleCPPObjS(&scripts,new CVisitableOPH(csc));
  735. handleCPPObjS(&scripts,new CVisitableOPW(csc));
  736. handleCPPObjS(&scripts,new CPickable(csc));
  737. handleCPPObjS(&scripts,new CMines(csc));
  738. handleCPPObjS(&scripts,new CTownScript(csc));
  739. handleCPPObjS(&scripts,new CHeroScript(csc));
  740. handleCPPObjS(&scripts,new CMonsterS(csc));
  741. handleCPPObjS(&scripts,new CCreatureGen(csc));
  742. /****************************INITIALIZING OBJECT SCRIPTS************************************************/
  743. //std::string temps("newObject");
  744. for (unsigned i=0; i<gs->map->objects.size(); i++)
  745. {
  746. //c++ scripts
  747. if (scripts.find(gs->map->objects[i]->ID) != scripts.end())
  748. {
  749. gs->map->objects[i]->state = scripts[gs->map->objects[i]->ID];
  750. gs->map->objects[i]->state->newObject(gs->map->objects[i]->id);
  751. }
  752. else
  753. {
  754. gs->map->objects[i]->state = NULL;
  755. }
  756. //// lua scripts
  757. //if(checkFunc(map->objects[i]->ID,temps))
  758. // (*skrypty)[map->objects[i]->ID][temps]->newObject(map->objects[i]);
  759. }
  760. while (!end2)
  761. {
  762. newTurn();
  763. for(std::map<ui8,PlayerState>::iterator i = gs->players.begin(); i != gs->players.end(); i++)
  764. {
  765. 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
  766. states[i->first] = 1;
  767. gs->currentPlayer = i->first;
  768. *connections[i->first] << ui16(100) << i->first;
  769. //wait till turn is done
  770. boost::unique_lock<boost::mutex> lock(mTurn);
  771. while(states[i->first] && !end2)
  772. {
  773. boost::posix_time::time_duration p;
  774. p= boost::posix_time::seconds(1);
  775. boost::xtime time={0,0};
  776. time.sec = static_cast<boost::xtime::xtime_sec_t>(p.total_seconds());
  777. cTurn.timed_wait(lock,time);
  778. }
  779. }
  780. }
  781. }