CGameHandler.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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 neighbours(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) * neighbours(start,end);
  53. TryMoveHero tmh;
  54. tmh.id = id;
  55. tmh.start = tmh.end = 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. (neighbours(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. //we start moving
  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. if(blockvis)//interaction with blocking object (like resources)
  78. {
  79. gs->apply(&tmh);
  80. sendToAllClients(&tmh);
  81. BOOST_FOREACH(CGObjectInstance *obj, t.visitableObjects)
  82. {
  83. if (obj->blockVisit)
  84. {
  85. if(gs->checkFunc(obj->ID,"heroVisit")) //script function
  86. gs->objscr[obj->ID]["heroVisit"]->onHeroVisit(obj,h->subID);
  87. if(obj->state) //hard-coded function
  88. obj->state->onHeroVisit(obj,h->subID);
  89. }
  90. }
  91. break;
  92. }
  93. else //normal move
  94. {
  95. tmh.result = 1;
  96. BOOST_FOREACH(CGObjectInstance *obj, ((start.z) ? (gs->map->undergroungTerrain[start.x][start.y]) : (gs->map->terrain[start.x][start.y])).visitableObjects)
  97. {
  98. //TODO: allow to handle this in script-languages
  99. if(obj->state) //hard-coded function
  100. obj->state->onHeroLeave(obj,h->subID);
  101. }
  102. //reveal fog of war
  103. int heroSight = h->getSightDistance();
  104. int xbeg = start.x - heroSight - 2;
  105. if(xbeg < 0)
  106. xbeg = 0;
  107. int xend = start.x + heroSight + 2;
  108. if(xend >= gs->map->width)
  109. xend = gs->map->width;
  110. int ybeg = start.y - heroSight - 2;
  111. if(ybeg < 0)
  112. ybeg = 0;
  113. int yend = start.y + heroSight + 2;
  114. if(yend >= gs->map->height)
  115. yend = gs->map->height;
  116. for(int xd=xbeg; xd<xend; ++xd) //revealing part of map around heroes
  117. {
  118. for(int yd=ybeg; yd<yend; ++yd)
  119. {
  120. int deltaX = (hmpos.x-xd)*(hmpos.x-xd);
  121. int deltaY = (hmpos.y-yd)*(hmpos.y-yd);
  122. if(deltaX+deltaY<h->getSightDistance()*h->getSightDistance())
  123. {
  124. if(gs->players[h->getOwner()].fogOfWarMap[xd][yd][hmpos.z] == 0)
  125. {
  126. tmh.fowRevealed.insert(int3(xd,yd,hmpos.z));
  127. }
  128. }
  129. }
  130. }
  131. gs->apply(&tmh);
  132. sendToAllClients(&tmh);
  133. //call objects if they arevisited
  134. BOOST_FOREACH(CGObjectInstance *obj, t.visitableObjects)
  135. {
  136. if(gs->checkFunc(obj->ID,"heroVisit")) //script function
  137. gs->objscr[obj->ID]["heroVisit"]->onHeroVisit(obj,h->subID);
  138. if(obj->state) //hard-coded function
  139. obj->state->onHeroVisit(obj,h->subID);
  140. }
  141. }
  142. break;
  143. fail:
  144. gs->apply(&tmh);
  145. sendToAllClients(&tmh);
  146. break;
  147. }
  148. default:
  149. throw std::exception("Not supported client message!");
  150. break;
  151. }
  152. }
  153. }
  154. catch (const std::exception& e)
  155. {
  156. std::cerr << e.what() << std::endl;
  157. end = true;
  158. }
  159. catch (const std::exception * e)
  160. {
  161. std::cerr << e->what()<< std::endl;
  162. end = true;
  163. delete e;
  164. }
  165. catch(...)
  166. {
  167. end = true;
  168. }
  169. }
  170. template <typename T>void CGameHandler::sendToAllClients(CPack<T> * info)
  171. {
  172. BOOST_FOREACH(CConnection* c, conns)
  173. *c << info->getType() << *info->This();
  174. }
  175. CGameHandler::CGameHandler(void)
  176. {
  177. gs = NULL;
  178. }
  179. CGameHandler::~CGameHandler(void)
  180. {
  181. delete gs;
  182. }
  183. void CGameHandler::init(StartInfo *si, int Seed)
  184. {
  185. Mapa *map = new Mapa(si->mapname);
  186. gs = new CGameState();
  187. gs->init(si,map,Seed);
  188. }
  189. int lowestSpeed(CGHeroInstance * chi)
  190. {
  191. std::map<si32,std::pair<CCreature*,si32> >::iterator i = chi->army.slots.begin();
  192. int ret = (*i++).second.first->speed;
  193. for (;i!=chi->army.slots.end();i++)
  194. {
  195. ret = min(ret,(*i).second.first->speed);
  196. }
  197. return ret;
  198. }
  199. int valMovePoints(CGHeroInstance * chi)
  200. {
  201. int ret = 1270+70*lowestSpeed(chi);
  202. if (ret>2000)
  203. ret=2000;
  204. //TODO: additional bonuses (but they aren't currently stored in chi)
  205. return ret;
  206. }
  207. void CGameHandler::newTurn()
  208. {
  209. NewTurn n;
  210. n.day = gs->day + 1;
  211. for ( std::map<ui8, PlayerState>::iterator i=gs->players.begin() ; i!=gs->players.end();i++)
  212. {
  213. if(i->first>=PLAYER_LIMIT) continue;
  214. NewTurn::Resources r;
  215. r.player = i->first;
  216. for(int j=0;j<RESOURCE_QUANTITY;j++)
  217. r.resources[j] = i->second.resources[j];
  218. for (unsigned j=0;j<(*i).second.heroes.size();j++) //handle heroes
  219. {
  220. NewTurn::Hero h;
  221. h.id = (*i).second.heroes[j]->id;
  222. h.move = valMovePoints((*i).second.heroes[j]);
  223. h.mana = (*i).second.heroes[j]->mana;
  224. n.heroes.insert(h);
  225. }
  226. for(unsigned j=0;j<i->second.towns.size();j++)//handle towns
  227. {
  228. i->second.towns[j]->builded=0;
  229. //if(gs->getDate(1)==1) //first day of week
  230. //{
  231. // for(int k=0;k<CREATURES_PER_TOWN;k++) //creature growths
  232. // {
  233. // if(i->second.towns[j]->creatureDwelling(k))//there is dwelling (k-level)
  234. // i->second.towns[j]->strInfo.creatures[k]+=i->second.towns[j]->creatureGrowth(k);
  235. // }
  236. //}
  237. if((gs->day) && i->first<PLAYER_LIMIT)//not the first day and town not neutral
  238. r.resources[6] += i->second.towns[j]->dailyIncome();
  239. }
  240. n.res.insert(r);
  241. }
  242. gs->apply(&n);
  243. sendToAllClients(&n);
  244. //for (std::set<CCPPObjectScript *>::iterator i=gs->cppscripts.begin();i!=gs->cppscripts.end();i++)
  245. //{
  246. // (*i)->newTurn();
  247. //}
  248. }
  249. void CGameHandler::run()
  250. {
  251. BOOST_FOREACH(CConnection *cc, conns)
  252. {//init conn.
  253. ui8 quantity, pom;
  254. //ui32 seed;
  255. (*cc) << gs->scenarioOps->mapname << gs->map->checksum << gs->seed;
  256. (*cc) >> quantity;
  257. for(int i=0;i<quantity;i++)
  258. {
  259. (*cc) >> pom;
  260. gsm.lock();
  261. connections[pom] = cc;
  262. gsm.unlock();
  263. }
  264. }
  265. for(std::set<CConnection*>::iterator i = conns.begin(); i!=conns.end();i++)
  266. {
  267. std::set<int> pom;
  268. for(std::map<int,CConnection*>::iterator j = connections.begin(); j!=connections.end();j++)
  269. if(j->second == *i)
  270. pom.insert(j->first);
  271. boost::thread(boost::bind(&CGameHandler::handleConnection,this,pom,boost::ref(**i)));
  272. }
  273. while (!end)
  274. {
  275. newTurn();
  276. for(std::map<ui8,PlayerState>::iterator i = gs->players.begin(); i != gs->players.end(); i++)
  277. {
  278. if((i->second.towns.size()==0 && i->second.heroes.size()==0) || i->second.color<0) continue; //players has not towns/castle - loser
  279. makingTurn = true;
  280. gs->currentPlayer = i->first;
  281. *connections[i->first] << ui16(100) << i->first;
  282. //wait till turn is done
  283. boost::unique_lock<boost::mutex> lock(mTurn);
  284. while(makingTurn && !end)
  285. {
  286. boost::posix_time::time_duration p;
  287. p= boost::posix_time::seconds(1);
  288. cTurn.timed_wait(lock,p);
  289. }
  290. }
  291. }
  292. }