CGameHandler.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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/CHeroHandler.h"
  17. #include "boost/date_time/posix_time/posix_time_types.hpp" //no i/o just types
  18. extern bool end;
  19. bool makingTurn;
  20. boost::condition_variable cTurn;
  21. boost::mutex mTurn;
  22. boost::shared_mutex gsm;
  23. double distance(int3 a, int3 b)
  24. {
  25. return std::sqrt( (double)(a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y) );
  26. }
  27. //bool CGameState::checkFunc(int obid, std::string name)
  28. //{
  29. // if (objscr.find(obid)!=objscr.end())
  30. // {
  31. // if(objscr[obid].find(name)!=objscr[obid].end())
  32. // {
  33. // return true;
  34. // }
  35. // }
  36. // return false;
  37. //}
  38. void CGameHandler::handleCPPObjS(std::map<int,CCPPObjectScript*> * mapa, CCPPObjectScript * script)
  39. {
  40. std::vector<int> tempv = script->yourObjects();
  41. for (unsigned i=0;i<tempv.size();i++)
  42. {
  43. (*mapa)[tempv[i]]=script;
  44. }
  45. cppscripts.insert(script);
  46. }
  47. void CGameHandler::handleConnection(std::set<int> players, CConnection &c)
  48. {
  49. try
  50. {
  51. ui16 pom;
  52. while(!end)
  53. {
  54. c >> pom;
  55. switch(pom)
  56. {
  57. case 100: //my interface ended its turn
  58. {
  59. mTurn.lock();
  60. makingTurn = false;
  61. mTurn.unlock();
  62. cTurn.notify_all();
  63. break;
  64. }
  65. case 501://interface wants to move hero
  66. {
  67. int3 start, end;
  68. si32 id;
  69. c >> id >> start >> end;
  70. int3 hmpos = end + int3(-1,0,0);
  71. TerrainTile t = gs->map->terrain[hmpos.x][hmpos.y][hmpos.z];
  72. CGHeroInstance *h = static_cast<CGHeroInstance *>(gs->map->objects[id]);
  73. int cost = (int)((double)h->getTileCost(t.tertype,t.malle,t.nuine) * distance(start,end));
  74. TryMoveHero tmh;
  75. tmh.id = id;
  76. tmh.start = start;
  77. tmh.end = end;
  78. tmh.result = 0;
  79. tmh.movePoints = h->movement;
  80. if((h->getOwner() != gs->currentPlayer) || //not turn of that hero
  81. (distance(start,end)>=1.5) || //tiles are not neighouring
  82. (h->movement < cost) || //lack of movement points
  83. (t.tertype == rock) || //rock
  84. (!h->canWalkOnSea() && t.tertype == water) ||
  85. (t.blocked && !t.visitable) ) //tile is blocked andnot visitable
  86. goto fail;
  87. //check if there is blocking visitable object
  88. bool blockvis = false;
  89. tmh.movePoints = h->movement = (h->movement-cost); //take move points
  90. BOOST_FOREACH(CGObjectInstance *obj, t.visitableObjects)
  91. {
  92. if(obj->blockVisit)
  93. {
  94. blockvis = true;
  95. break;
  96. }
  97. }
  98. //we start moving
  99. if(blockvis)//interaction with blocking object (like resources)
  100. {
  101. sendAndApply(&tmh); //failed to move to that tile but we visit object
  102. BOOST_FOREACH(CGObjectInstance *obj, t.visitableObjects)
  103. {
  104. if (obj->blockVisit)
  105. {
  106. //if(gs->checkFunc(obj->ID,"heroVisit")) //script function
  107. // gs->objscr[obj->ID]["heroVisit"]->onHeroVisit(obj,h->subID);
  108. if(obj->state) //hard-coded function
  109. obj->state->onHeroVisit(obj->id,h->subID);
  110. }
  111. }
  112. break;
  113. }
  114. else //normal move
  115. {
  116. tmh.result = 1;
  117. BOOST_FOREACH(CGObjectInstance *obj, gs->map->terrain[start.x][start.y][start.z].visitableObjects)
  118. {
  119. //TODO: allow to handle this in script-languages
  120. if(obj->state) //hard-coded function
  121. obj->state->onHeroLeave(obj->id,h->subID);
  122. }
  123. //reveal fog of war
  124. int heroSight = h->getSightDistance();
  125. int xbeg = start.x - heroSight - 2;
  126. if(xbeg < 0)
  127. xbeg = 0;
  128. int xend = start.x + heroSight + 2;
  129. if(xend >= gs->map->width)
  130. xend = gs->map->width;
  131. int ybeg = start.y - heroSight - 2;
  132. if(ybeg < 0)
  133. ybeg = 0;
  134. int yend = start.y + heroSight + 2;
  135. if(yend >= gs->map->height)
  136. yend = gs->map->height;
  137. for(int xd=xbeg; xd<xend; ++xd) //revealing part of map around heroes
  138. {
  139. for(int yd=ybeg; yd<yend; ++yd)
  140. {
  141. int deltaX = (hmpos.x-xd)*(hmpos.x-xd);
  142. int deltaY = (hmpos.y-yd)*(hmpos.y-yd);
  143. if(deltaX+deltaY<h->getSightDistance()*h->getSightDistance())
  144. {
  145. if(gs->players[h->getOwner()].fogOfWarMap[xd][yd][hmpos.z] == 0)
  146. {
  147. tmh.fowRevealed.insert(int3(xd,yd,hmpos.z));
  148. }
  149. }
  150. }
  151. }
  152. sendAndApply(&tmh);
  153. BOOST_FOREACH(CGObjectInstance *obj, t.visitableObjects)//call objects if they are visited
  154. {
  155. //if(gs->checkFunc(obj->ID,"heroVisit")) //script function
  156. // gs->objscr[obj->ID]["heroVisit"]->onHeroVisit(obj,h->subID);
  157. if(obj->state) //hard-coded function
  158. obj->state->onHeroVisit(obj->id,h->id);
  159. }
  160. }
  161. break;
  162. fail:
  163. sendAndApply(&tmh);
  164. break;
  165. }
  166. default:
  167. throw std::exception("Not supported client message!");
  168. break;
  169. }
  170. }
  171. }
  172. catch (const std::exception& e)
  173. {
  174. std::cerr << e.what() << std::endl;
  175. end = true;
  176. }
  177. catch (const std::exception * e)
  178. {
  179. std::cerr << e->what()<< std::endl;
  180. end = true;
  181. delete e;
  182. }
  183. catch(...)
  184. {
  185. end = true;
  186. }
  187. }
  188. CGameHandler::CGameHandler(void)
  189. {
  190. gs = NULL;
  191. }
  192. CGameHandler::~CGameHandler(void)
  193. {
  194. delete gs;
  195. }
  196. void CGameHandler::init(StartInfo *si, int Seed)
  197. {
  198. Mapa *map = new Mapa(si->mapname);
  199. gs = new CGameState();
  200. gs->init(si,map,Seed);
  201. /****************************SCRIPTS************************************************/
  202. //std::map<int, std::map<std::string, CObjectScript*> > * skrypty = &objscr; //alias for easier access
  203. /****************************C++ OBJECT SCRIPTS************************************************/
  204. std::map<int,CCPPObjectScript*> scripts;
  205. CScriptCallback * csc = new CScriptCallback();
  206. csc->gh = this;
  207. handleCPPObjS(&scripts,new CVisitableOPH(csc));
  208. handleCPPObjS(&scripts,new CVisitableOPW(csc));
  209. handleCPPObjS(&scripts,new CPickable(csc));
  210. handleCPPObjS(&scripts,new CMines(csc));
  211. handleCPPObjS(&scripts,new CTownScript(csc));
  212. handleCPPObjS(&scripts,new CHeroScript(csc));
  213. handleCPPObjS(&scripts,new CMonsterS(csc));
  214. handleCPPObjS(&scripts,new CCreatureGen(csc));
  215. //created map
  216. /****************************LUA OBJECT SCRIPTS************************************************/
  217. //std::vector<std::string> * lf = CLuaHandler::searchForScripts("scripts/lua/objects"); //files
  218. //for (int i=0; i<lf->size(); i++)
  219. //{
  220. // try
  221. // {
  222. // std::vector<std::string> * temp = CLuaHandler::functionList((*lf)[i]);
  223. // CLuaObjectScript * objs = new CLuaObjectScript((*lf)[i]);
  224. // CLuaCallback::registerFuncs(objs->is);
  225. // //objs
  226. // for (int j=0; j<temp->size(); j++)
  227. // {
  228. // int obid ; //obj ID
  229. // int dspos = (*temp)[j].find_first_of('_');
  230. // obid = atoi((*temp)[j].substr(dspos+1,(*temp)[j].size()-dspos-1).c_str());
  231. // std::string fname = (*temp)[j].substr(0,dspos);
  232. // if (skrypty->find(obid)==skrypty->end())
  233. // skrypty->insert(std::pair<int, std::map<std::string, CObjectScript*> >(obid,std::map<std::string,CObjectScript*>()));
  234. // (*skrypty)[obid].insert(std::pair<std::string, CObjectScript*>(fname,objs));
  235. // }
  236. // delete temp;
  237. // }HANDLE_EXCEPTION
  238. //}
  239. /****************************INITIALIZING OBJECT SCRIPTS************************************************/
  240. //std::string temps("newObject");
  241. for (unsigned i=0; i<map->objects.size(); i++)
  242. {
  243. //c++ scripts
  244. if (scripts.find(map->objects[i]->ID) != scripts.end())
  245. {
  246. map->objects[i]->state = scripts[map->objects[i]->ID];
  247. map->objects[i]->state->newObject(map->objects[i]->id);
  248. }
  249. else
  250. {
  251. map->objects[i]->state = NULL;
  252. }
  253. //// lua scripts
  254. //if(checkFunc(map->objects[i]->ID,temps))
  255. // (*skrypty)[map->objects[i]->ID][temps]->newObject(map->objects[i]);
  256. }
  257. //delete lf;
  258. }
  259. int lowestSpeed(CGHeroInstance * chi)
  260. {
  261. std::map<si32,std::pair<CCreature*,si32> >::iterator i = chi->army.slots.begin();
  262. int ret = (*i++).second.first->speed;
  263. for (;i!=chi->army.slots.end();i++)
  264. {
  265. ret = min(ret,(*i).second.first->speed);
  266. }
  267. return ret;
  268. }
  269. int valMovePoints(CGHeroInstance * chi)
  270. {
  271. int ret = 1270+70*lowestSpeed(chi);
  272. if (ret>2000)
  273. ret=2000;
  274. //TODO: additional bonuses (but they aren't currently stored in chi)
  275. return ret;
  276. }
  277. void CGameHandler::newTurn()
  278. {
  279. NewTurn n;
  280. n.day = gs->day + 1;
  281. for ( std::map<ui8, PlayerState>::iterator i=gs->players.begin() ; i!=gs->players.end();i++)
  282. {
  283. if(i->first>=PLAYER_LIMIT) continue;
  284. NewTurn::Resources r;
  285. r.player = i->first;
  286. for(int j=0;j<RESOURCE_QUANTITY;j++)
  287. r.resources[j] = i->second.resources[j];
  288. for (unsigned j=0;j<(*i).second.heroes.size();j++) //handle heroes
  289. {
  290. NewTurn::Hero h;
  291. h.id = (*i).second.heroes[j]->id;
  292. h.move = valMovePoints((*i).second.heroes[j]);
  293. h.mana = (*i).second.heroes[j]->mana;
  294. n.heroes.insert(h);
  295. }
  296. for(unsigned j=0;j<i->second.towns.size();j++)//handle towns
  297. {
  298. i->second.towns[j]->builded=0;
  299. //if(gs->getDate(1)==1) //first day of week
  300. //{
  301. // for(int k=0;k<CREATURES_PER_TOWN;k++) //creature growths
  302. // {
  303. // if(i->second.towns[j]->creatureDwelling(k))//there is dwelling (k-level)
  304. // i->second.towns[j]->strInfo.creatures[k]+=i->second.towns[j]->creatureGrowth(k);
  305. // }
  306. //}
  307. if((gs->day) && i->first<PLAYER_LIMIT)//not the first day and town not neutral
  308. r.resources[6] += i->second.towns[j]->dailyIncome();
  309. }
  310. n.res.insert(r);
  311. }
  312. sendAndApply(&n);
  313. //for (std::set<CCPPObjectScript *>::iterator i=gs->cppscripts.begin();i!=gs->cppscripts.end();i++)
  314. //{
  315. // (*i)->newTurn();
  316. //}
  317. }
  318. void CGameHandler::run()
  319. {
  320. BOOST_FOREACH(CConnection *cc, conns)
  321. {//init conn.
  322. ui8 quantity, pom;
  323. //ui32 seed;
  324. (*cc) << gs->scenarioOps->mapname << gs->map->checksum << gs->seed;
  325. (*cc) >> quantity;
  326. for(int i=0;i<quantity;i++)
  327. {
  328. (*cc) >> pom;
  329. gsm.lock();
  330. connections[pom] = cc;
  331. gsm.unlock();
  332. }
  333. }
  334. for(std::set<CConnection*>::iterator i = conns.begin(); i!=conns.end();i++)
  335. {
  336. std::set<int> pom;
  337. for(std::map<int,CConnection*>::iterator j = connections.begin(); j!=connections.end();j++)
  338. if(j->second == *i)
  339. pom.insert(j->first);
  340. boost::thread(boost::bind(&CGameHandler::handleConnection,this,pom,boost::ref(**i)));
  341. }
  342. while (!end)
  343. {
  344. newTurn();
  345. for(std::map<ui8,PlayerState>::iterator i = gs->players.begin(); i != gs->players.end(); i++)
  346. {
  347. if((i->second.towns.size()==0 && i->second.heroes.size()==0) || i->second.color<0) continue; //players has not towns/castle - loser
  348. makingTurn = true;
  349. gs->currentPlayer = i->first;
  350. *connections[i->first] << ui16(100) << i->first;
  351. //wait till turn is done
  352. boost::unique_lock<boost::mutex> lock(mTurn);
  353. while(makingTurn && !end)
  354. {
  355. boost::posix_time::time_duration p;
  356. p= boost::posix_time::seconds(1);
  357. cTurn.timed_wait(lock,p);
  358. }
  359. }
  360. }
  361. }