CGameHandler.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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 "../CLua.h"
  7. #include "../CGameState.h"
  8. #include "../StartInfo.h"
  9. #include "../map.h"
  10. #include "../lib/NetPacks.h"
  11. #include "../lib/Connection.h"
  12. #include "../CLua.h"
  13. #include "../hch/CObjectHandler.h"
  14. #include "../hch/CTownHandler.h"
  15. #include "../hch/CHeroHandler.h"
  16. #include "boost/date_time/posix_time/posix_time_types.hpp" //no i/o just types
  17. extern bool end;
  18. bool makingTurn;
  19. boost::condition_variable cTurn;
  20. boost::mutex mTurn;
  21. boost::shared_mutex gsm;
  22. double distance(int3 a, int3 b)
  23. {
  24. return std::sqrt( (double)(a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y) );
  25. }
  26. void CGameHandler::handleConnection(std::set<int> players, CConnection &c)
  27. {
  28. try
  29. {
  30. ui16 pom;
  31. while(!end)
  32. {
  33. c >> pom;
  34. switch(pom)
  35. {
  36. case 100: //my interface ended its turn
  37. {
  38. mTurn.lock();
  39. makingTurn = false;
  40. mTurn.unlock();
  41. cTurn.notify_all();
  42. break;
  43. }
  44. case 501://interface wants to move hero
  45. {
  46. int3 start, end;
  47. si32 id;
  48. c >> id >> start >> end;
  49. int3 hmpos = end + int3(-1,0,0);
  50. TerrainTile t = (hmpos.z) ? (gs->map->undergroungTerrain[hmpos.x][hmpos.y]) : (gs->map->terrain[hmpos.x][hmpos.y]);
  51. CGHeroInstance *h = static_cast<CGHeroInstance *>(gs->map->objects[id]);
  52. int cost = (double)h->getTileCost(t.tertype,t.malle,t.nuine) * distance(start,end);
  53. TryMoveHero tmh;
  54. tmh.id = id;
  55. tmh.start = start;
  56. tmh.end = end;
  57. tmh.result = 0;
  58. tmh.movePoints = h->movement;
  59. if((h->getOwner() != gs->currentPlayer) || //not turn of that hero
  60. (distance(start,end)>=1.5) || //tiles are not neighouring
  61. (h->movement < cost) || //lack of movement points
  62. (t.tertype == rock) || //rock
  63. (!h->canWalkOnSea() && t.tertype == water) ||
  64. (t.blocked && !t.visitable) ) //tile is blocked andnot visitable
  65. goto fail;
  66. //check if there is blocking visitable object
  67. bool blockvis = false;
  68. tmh.movePoints = h->movement = (h->movement-cost); //take move points
  69. BOOST_FOREACH(CGObjectInstance *obj, t.visitableObjects)
  70. {
  71. if(obj->blockVisit)
  72. {
  73. blockvis = true;
  74. break;
  75. }
  76. }
  77. //we start moving
  78. if(blockvis)//interaction with blocking object (like resources)
  79. {
  80. gs->apply(&tmh);
  81. sendToAllClients(&tmh); //failed to move to that tile but we visit object
  82. BOOST_FOREACH(CGObjectInstance *obj, t.visitableObjects)
  83. {
  84. if (obj->blockVisit)
  85. {
  86. if(gs->checkFunc(obj->ID,"heroVisit")) //script function
  87. gs->objscr[obj->ID]["heroVisit"]->onHeroVisit(obj,h->subID);
  88. if(obj->state) //hard-coded function
  89. obj->state->onHeroVisit(obj,h->subID);
  90. }
  91. }
  92. break;
  93. }
  94. else //normal move
  95. {
  96. tmh.result = 1;
  97. BOOST_FOREACH(CGObjectInstance *obj, ((start.z) ? (gs->map->undergroungTerrain[start.x][start.y]) : (gs->map->terrain[start.x][start.y])).visitableObjects)
  98. {
  99. //TODO: allow to handle this in script-languages
  100. if(obj->state) //hard-coded function
  101. obj->state->onHeroLeave(obj,h->subID);
  102. }
  103. //reveal fog of war
  104. int heroSight = h->getSightDistance();
  105. int xbeg = start.x - heroSight - 2;
  106. if(xbeg < 0)
  107. xbeg = 0;
  108. int xend = start.x + heroSight + 2;
  109. if(xend >= gs->map->width)
  110. xend = gs->map->width;
  111. int ybeg = start.y - heroSight - 2;
  112. if(ybeg < 0)
  113. ybeg = 0;
  114. int yend = start.y + heroSight + 2;
  115. if(yend >= gs->map->height)
  116. yend = gs->map->height;
  117. for(int xd=xbeg; xd<xend; ++xd) //revealing part of map around heroes
  118. {
  119. for(int yd=ybeg; yd<yend; ++yd)
  120. {
  121. int deltaX = (hmpos.x-xd)*(hmpos.x-xd);
  122. int deltaY = (hmpos.y-yd)*(hmpos.y-yd);
  123. if(deltaX+deltaY<h->getSightDistance()*h->getSightDistance())
  124. {
  125. if(gs->players[h->getOwner()].fogOfWarMap[xd][yd][hmpos.z] == 0)
  126. {
  127. tmh.fowRevealed.insert(int3(xd,yd,hmpos.z));
  128. }
  129. }
  130. }
  131. }
  132. gs->apply(&tmh);
  133. sendToAllClients(&tmh);
  134. //call objects if they arevisited
  135. BOOST_FOREACH(CGObjectInstance *obj, t.visitableObjects)
  136. {
  137. if(gs->checkFunc(obj->ID,"heroVisit")) //script function
  138. gs->objscr[obj->ID]["heroVisit"]->onHeroVisit(obj,h->subID);
  139. if(obj->state) //hard-coded function
  140. obj->state->onHeroVisit(obj,h->subID);
  141. }
  142. }
  143. break;
  144. fail:
  145. gs->apply(&tmh);
  146. sendToAllClients(&tmh);
  147. break;
  148. }
  149. default:
  150. throw std::exception("Not supported client message!");
  151. break;
  152. }
  153. }
  154. }
  155. catch (const std::exception& e)
  156. {
  157. std::cerr << e.what() << std::endl;
  158. end = true;
  159. }
  160. catch (const std::exception * e)
  161. {
  162. std::cerr << e->what()<< std::endl;
  163. end = true;
  164. delete e;
  165. }
  166. catch(...)
  167. {
  168. end = true;
  169. }
  170. }
  171. template <typename T>void CGameHandler::sendToAllClients(CPack<T> * info)
  172. {
  173. BOOST_FOREACH(CConnection* c, conns)
  174. *c << info->getType() << *info->This();
  175. }
  176. CGameHandler::CGameHandler(void)
  177. {
  178. gs = NULL;
  179. }
  180. CGameHandler::~CGameHandler(void)
  181. {
  182. delete gs;
  183. }
  184. void CGameHandler::init(StartInfo *si, int Seed)
  185. {
  186. Mapa *map = new Mapa(si->mapname);
  187. gs = new CGameState();
  188. gs->init(si,map,Seed);
  189. }
  190. int lowestSpeed(CGHeroInstance * chi)
  191. {
  192. std::map<si32,std::pair<CCreature*,si32> >::iterator i = chi->army.slots.begin();
  193. int ret = (*i++).second.first->speed;
  194. for (;i!=chi->army.slots.end();i++)
  195. {
  196. ret = min(ret,(*i).second.first->speed);
  197. }
  198. return ret;
  199. }
  200. int valMovePoints(CGHeroInstance * chi)
  201. {
  202. int ret = 1270+70*lowestSpeed(chi);
  203. if (ret>2000)
  204. ret=2000;
  205. //TODO: additional bonuses (but they aren't currently stored in chi)
  206. return ret;
  207. }
  208. void CGameHandler::newTurn()
  209. {
  210. NewTurn n;
  211. n.day = gs->day + 1;
  212. for ( std::map<ui8, PlayerState>::iterator i=gs->players.begin() ; i!=gs->players.end();i++)
  213. {
  214. if(i->first>=PLAYER_LIMIT) continue;
  215. NewTurn::Resources r;
  216. r.player = i->first;
  217. for(int j=0;j<RESOURCE_QUANTITY;j++)
  218. r.resources[j] = i->second.resources[j];
  219. for (unsigned j=0;j<(*i).second.heroes.size();j++) //handle heroes
  220. {
  221. NewTurn::Hero h;
  222. h.id = (*i).second.heroes[j]->id;
  223. h.move = valMovePoints((*i).second.heroes[j]);
  224. h.mana = (*i).second.heroes[j]->mana;
  225. n.heroes.insert(h);
  226. }
  227. for(unsigned j=0;j<i->second.towns.size();j++)//handle towns
  228. {
  229. i->second.towns[j]->builded=0;
  230. //if(gs->getDate(1)==1) //first day of week
  231. //{
  232. // for(int k=0;k<CREATURES_PER_TOWN;k++) //creature growths
  233. // {
  234. // if(i->second.towns[j]->creatureDwelling(k))//there is dwelling (k-level)
  235. // i->second.towns[j]->strInfo.creatures[k]+=i->second.towns[j]->creatureGrowth(k);
  236. // }
  237. //}
  238. if((gs->day) && i->first<PLAYER_LIMIT)//not the first day and town not neutral
  239. r.resources[6] += i->second.towns[j]->dailyIncome();
  240. }
  241. n.res.insert(r);
  242. }
  243. gs->apply(&n);
  244. sendToAllClients(&n);
  245. //for (std::set<CCPPObjectScript *>::iterator i=gs->cppscripts.begin();i!=gs->cppscripts.end();i++)
  246. //{
  247. // (*i)->newTurn();
  248. //}
  249. }
  250. void CGameHandler::run()
  251. {
  252. BOOST_FOREACH(CConnection *cc, conns)
  253. {//init conn.
  254. ui8 quantity, pom;
  255. //ui32 seed;
  256. (*cc) << gs->scenarioOps->mapname << gs->map->checksum << gs->seed;
  257. (*cc) >> quantity;
  258. for(int i=0;i<quantity;i++)
  259. {
  260. (*cc) >> pom;
  261. gsm.lock();
  262. connections[pom] = cc;
  263. gsm.unlock();
  264. }
  265. }
  266. for(std::set<CConnection*>::iterator i = conns.begin(); i!=conns.end();i++)
  267. {
  268. std::set<int> pom;
  269. for(std::map<int,CConnection*>::iterator j = connections.begin(); j!=connections.end();j++)
  270. if(j->second == *i)
  271. pom.insert(j->first);
  272. boost::thread(boost::bind(&CGameHandler::handleConnection,this,pom,boost::ref(**i)));
  273. }
  274. while (!end)
  275. {
  276. newTurn();
  277. for(std::map<ui8,PlayerState>::iterator i = gs->players.begin(); i != gs->players.end(); i++)
  278. {
  279. if((i->second.towns.size()==0 && i->second.heroes.size()==0) || i->second.color<0) continue; //players has not towns/castle - loser
  280. makingTurn = true;
  281. gs->currentPlayer = i->first;
  282. *connections[i->first] << ui16(100) << i->first;
  283. //wait till turn is done
  284. boost::unique_lock<boost::mutex> lock(mTurn);
  285. while(makingTurn && !end)
  286. {
  287. boost::posix_time::time_duration p;
  288. p= boost::posix_time::seconds(1);
  289. cTurn.timed_wait(lock,p);
  290. }
  291. }
  292. }
  293. }