CCallback.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. #include "stdafx.h"
  2. #include "CCallback.h"
  3. #include "CPathfinder.h"
  4. #include "hch\CHeroHandler.h"
  5. #include "hch\CTownHandler.h"
  6. #include "CGameInfo.h"
  7. #include "hch\CAmbarCendamo.h"
  8. #include "mapHandler.h"
  9. #include "CGameState.h"
  10. #include "CPlayerInterface.h"
  11. #include "CLua.h"
  12. #include "hch/CGeneralTextHandler.h"
  13. #include "CAdvmapInterface.h"
  14. #include "CPlayerInterface.h"
  15. LUALIB_API int (luaL_error) (lua_State *L, const char *fmt, ...);
  16. int CCallback::lowestSpeed(CGHeroInstance * chi)
  17. {
  18. int min = 150;
  19. for ( std::map<int,std::pair<CCreature*,int> >::iterator i = chi->army.slots.begin();
  20. i!=chi->army.slots.end(); i++ )
  21. {
  22. if (min>(*i).second.first->speed)
  23. min = (*i).second.first->speed;
  24. }
  25. return min;
  26. }
  27. int CCallback::valMovePoints(CGHeroInstance * chi)
  28. {
  29. int ret = 1270+70*lowestSpeed(chi);
  30. if (ret>2000)
  31. ret=2000;
  32. //TODO: additional bonuses (but they aren't currently stored in chi)
  33. return ret;
  34. }
  35. void CCallback::newTurn()
  36. {
  37. //std::map<int, PlayerState>::iterator i = gs->players.begin() ;
  38. gs->day++;
  39. for ( std::map<int, PlayerState>::iterator i=gs->players.begin() ; i!=gs->players.end();i++)
  40. {
  41. for (int j=0;j<(*i).second.heroes.size();j++)
  42. {
  43. (*i).second.heroes[j]->movement = valMovePoints((*i).second.heroes[j]);
  44. }
  45. }
  46. }
  47. bool CCallback::moveHero(int ID, CPath * path, int idtype, int pathType)
  48. {
  49. CGHeroInstance * hero = NULL;
  50. if (idtype==0)
  51. {
  52. if (player==-1)
  53. hero=gs->players[player+1].heroes[ID];
  54. else
  55. hero=gs->players[player].heroes[ID];
  56. }
  57. else if (idtype==1 && player>=0) //looking for it in local area
  58. {
  59. for (int i=0; i<gs->players[player].heroes.size();i++)
  60. {
  61. if (gs->players[player].heroes[i]->type->ID == ID)
  62. hero = gs->players[player].heroes[i];
  63. }
  64. }
  65. else //idtype==1; player<0
  66. {
  67. for(std::map<int, PlayerState>::iterator j=CGI->state->players.begin(); j!=CGI->state->players.end(); ++j)
  68. {
  69. for (int i=0; i<(*j).second.heroes.size();i++)
  70. {
  71. if ((*j).second.heroes[i]->type->ID == ID)
  72. {
  73. hero = (*j).second.heroes[i];
  74. }
  75. }
  76. }
  77. }
  78. if (!hero)
  79. return false; //can't find hero
  80. if(!verifyPath(path,!hero->canWalkOnSea()))//TODO: not check sea, if hero has flying or walking on water
  81. return false; //invalid path
  82. //check path format
  83. if (pathType==0)
  84. CPathfinder::convertPath(path,pathType);
  85. if (pathType>1)
  86. throw std::exception("Unknown path format");
  87. CPath * ourPath = path;
  88. if(!ourPath)
  89. return false;
  90. for(int i=ourPath->nodes.size()-1; i>0; i--)
  91. {
  92. int3 stpos, endpos;
  93. stpos = int3(ourPath->nodes[i].coord.x, ourPath->nodes[i].coord.y, hero->pos.z);
  94. endpos = int3(ourPath->nodes[i-1].coord.x, ourPath->nodes[i-1].coord.y, hero->pos.z);
  95. HeroMoveDetails curd;
  96. curd.src = stpos;
  97. curd.dst = endpos;
  98. curd.ho = hero;
  99. curd.owner = hero->getOwner();
  100. /*if(player!=-1)
  101. {
  102. hero->pos = endpos;
  103. }*/
  104. if((hero->movement>=CGI->mh->getCost(stpos, endpos, hero)) || player==-1)
  105. { //performing move
  106. hero->movement-=CGI->mh->getCost(stpos, endpos, hero);
  107. int heroSight = hero->getSightDistance();
  108. int xbeg = stpos.x - heroSight - 2;
  109. if(xbeg < 0)
  110. xbeg = 0;
  111. int xend = stpos.x + heroSight + 2;
  112. if(xend >= CGI->ac->map.width)
  113. xend = CGI->ac->map.width;
  114. int ybeg = stpos.y - heroSight - 2;
  115. if(ybeg < 0)
  116. ybeg = 0;
  117. int yend = stpos.y + heroSight + 2;
  118. if(yend >= CGI->ac->map.height)
  119. yend = CGI->ac->map.height;
  120. for(int xd=xbeg; xd<xend; ++xd) //revealing part of map around heroes
  121. {
  122. for(int yd=ybeg; yd<yend; ++yd)
  123. {
  124. int deltaX = (hero->getPosition(false).x-xd)*(hero->getPosition(false).x-xd);
  125. int deltaY = (hero->getPosition(false).y-yd)*(hero->getPosition(false).y-yd);
  126. if(deltaX+deltaY<hero->getSightDistance()*hero->getSightDistance())
  127. gs->players[player].fogOfWarMap[xd][yd][hero->getPosition(false).z] = 1;
  128. }
  129. }
  130. hero->pos = curd.dst;
  131. int nn=0; //number of interfece of currently browsed player
  132. for(std::map<int, PlayerState>::iterator j=CGI->state->players.begin(); j!=CGI->state->players.end(); ++j)//CGI->state->players.size(); ++j) //for testing
  133. {
  134. if (j->first > PLAYER_LIMIT)
  135. break;
  136. if(j->second.fogOfWarMap[stpos.x-1][stpos.y][stpos.z] || j->second.fogOfWarMap[endpos.x-1][endpos.y][endpos.z])
  137. { //player should be notified
  138. CGI->playerint[j->second.serial]->heroMoved(curd);
  139. }
  140. ++nn;
  141. }
  142. std::vector< CGObjectInstance * > vis = CGI->mh->getVisitableObjs(hero->getPosition(false));
  143. for (int iii=0; iii<vis.size(); iii++)
  144. {
  145. if(gs->checkFunc(vis[iii]->ID,"heroVisit"))
  146. gs->objscr[vis[iii]->ID]["heroVisit"]->onHeroVisit(vis[iii],curd.ho->subID);
  147. if(vis[iii]->state)
  148. vis[iii]->state->onHeroVisit(vis[iii],curd.ho->subID);
  149. //std::cout<< CGI->objh->objects[vis[iii]->ID].name<<std::endl;
  150. }
  151. }
  152. else
  153. return true; //move ended - no more movement points
  154. //hero->pos = curd.dst;
  155. }
  156. return true;
  157. }
  158. int CCallback::howManyTowns()
  159. {
  160. return gs->players[gs->currentPlayer].towns.size();
  161. }
  162. const CGTownInstance * CCallback::getTownInfo(int val, bool mode) //mode = 0 -> val = serial; mode = 1 -> val = ID
  163. {
  164. if (!mode)
  165. return gs->players[gs->currentPlayer].towns[val];
  166. else
  167. {
  168. //TODO: add some smart ID to the CTownInstance
  169. //for (int i=0; i<gs->players[gs->currentPlayer].towns.size();i++)
  170. //{
  171. // if (gs->players[gs->currentPlayer].towns[i]->someID==val)
  172. // return gs->players[gs->currentPlayer].towns[i];
  173. //}
  174. return NULL;
  175. }
  176. return NULL;
  177. }
  178. int CCallback::howManyHeroes()
  179. {
  180. return gs->players[player].heroes.size();
  181. }
  182. const CGHeroInstance * CCallback::getHeroInfo(int player, int val, bool mode) //mode = 0 -> val = serial; mode = 1 -> val = ID
  183. {
  184. if (gs->currentPlayer!=player) //TODO: checking if we are allowed to give that info
  185. return NULL;
  186. if (!mode)
  187. return gs->players[player].heroes[val];
  188. else
  189. {
  190. for (int i=0; i<gs->players[player].heroes.size();i++)
  191. {
  192. if (gs->players[player].heroes[i]->type->ID==val)
  193. return gs->players[player].heroes[i];
  194. }
  195. }
  196. return NULL;
  197. }
  198. int CCallback::getResourceAmount(int type)
  199. {
  200. return gs->players[gs->currentPlayer].resources[type];
  201. }
  202. int CCallback::getDate(int mode)
  203. {
  204. int temp;
  205. switch (mode)
  206. {
  207. case 0:
  208. return gs->day;
  209. break;
  210. case 1:
  211. temp = (gs->day)%7;
  212. if (temp)
  213. return temp;
  214. else return 7;
  215. break;
  216. case 2:
  217. temp = ((gs->day-1)/7)+1;
  218. if (!(temp%4))
  219. return 4;
  220. else
  221. return (temp%4);
  222. break;
  223. case 3:
  224. return ((gs->day-1)/28)+1;
  225. break;
  226. }
  227. return 0;
  228. }
  229. bool CCallback::verifyPath(CPath * path, bool blockSea)
  230. {
  231. for (int i=0;i<path->nodes.size();i++)
  232. {
  233. if ( CGI->mh->ttiles[path->nodes[i].coord.x][path->nodes[i].coord.y][path->nodes[i].coord.z].blocked
  234. && (! (CGI->mh->ttiles[path->nodes[i].coord.x][path->nodes[i].coord.y][path->nodes[i].coord.z].visitable)))
  235. return false; //path is wrong - one of the tiles is blocked
  236. if (blockSea)
  237. {
  238. if (i==0)
  239. continue;
  240. if (
  241. ((CGI->mh->ttiles[path->nodes[i].coord.x][path->nodes[i].coord.y][path->nodes[i].coord.z].terType==EterrainType::water)
  242. &&
  243. (CGI->mh->ttiles[path->nodes[i-1].coord.x][path->nodes[i-1].coord.y][path->nodes[i-1].coord.z].terType!=EterrainType::water))
  244. ||
  245. ((CGI->mh->ttiles[path->nodes[i].coord.x][path->nodes[i].coord.y][path->nodes[i].coord.z].terType!=EterrainType::water)
  246. &&
  247. (CGI->mh->ttiles[path->nodes[i-1].coord.x][path->nodes[i-1].coord.y][path->nodes[i-1].coord.z].terType==EterrainType::water))
  248. ||
  249. (CGI->mh->ttiles[path->nodes[i-1].coord.x][path->nodes[i-1].coord.y][path->nodes[i-1].coord.z].terType==EterrainType::rock)
  250. )
  251. return false;
  252. }
  253. }
  254. return true;
  255. }
  256. std::vector < std::string > CCallback::getObjDescriptions(int3 pos)
  257. {
  258. if(gs->players[player].fogOfWarMap[pos.x][pos.y][pos.z])
  259. return CGI->mh->getObjDescriptions(pos);
  260. else return std::vector< std::string > ();
  261. }
  262. PseudoV< PseudoV< PseudoV<unsigned char> > > & CCallback::getVisibilityMap()
  263. {
  264. return gs->players[player].fogOfWarMap;
  265. }
  266. bool CCallback::isVisible(int3 pos, int Player)
  267. {
  268. return gs->players[Player].fogOfWarMap[pos.x][pos.y][pos.z];
  269. }
  270. std::vector < const CGHeroInstance *> * CCallback::getHeroesInfo(bool onlyOur)
  271. {
  272. std::vector < const CGHeroInstance *> * ret = new std::vector < const CGHeroInstance *>();
  273. for ( std::map<int, PlayerState>::iterator i=gs->players.begin() ; i!=gs->players.end();i++)
  274. {
  275. for (int j=0;j<(*i).second.heroes.size();j++)
  276. {
  277. if ( ( isVisible((*i).second.heroes[j]->getPosition(false),player) ) || (*i).first==player)
  278. {
  279. ret->push_back((*i).second.heroes[j]);
  280. }
  281. }
  282. } // for ( std::map<int, PlayerState>::iterator i=gs->players.begin() ; i!=gs->players.end();i++)
  283. return ret;
  284. }
  285. bool CCallback::isVisible(int3 pos)
  286. {
  287. return isVisible(pos,player);
  288. }
  289. int3 CScriptCallback::getPos(CGObjectInstance * ob)
  290. {
  291. return ob->pos;
  292. }
  293. void CScriptCallback::changePrimSkill(int ID, int which, int val)
  294. {
  295. CGHeroInstance * hero = CGI->state->getHero(ID,0);
  296. hero->primSkills[which]+=val;
  297. for (int i=0; i<CGI->playerint.size(); i++)
  298. {
  299. if (CGI->playerint[i]->playerID == hero->getOwner())
  300. {
  301. CGI->playerint[i]->heroPrimarySkillChanged(hero, which, val);
  302. break;
  303. }
  304. }
  305. }
  306. int CScriptCallback::getHeroOwner(int heroID)
  307. {
  308. CGHeroInstance * hero = CGI->state->getHero(heroID,0);
  309. return hero->getOwner();
  310. }
  311. void CScriptCallback::showInfoDialog(int player, std::string text, std::vector<SComponent*> * components)
  312. {
  313. //TODO: upewniac sie ze mozemy to zrzutowac (przy customowych interfejsach cos moze sie kopnac)
  314. if (player>=0)
  315. {
  316. CGameInterface * temp = CGI->playerint[CGI->state->players[player].serial];
  317. if (temp->human)
  318. ((CPlayerInterface*)(temp))->showInfoDialog(text,*components);
  319. return;
  320. }
  321. else
  322. {
  323. for (int i=0; i<CGI->playerint.size();i++)
  324. {
  325. if (CGI->playerint[i]->human)
  326. ((CPlayerInterface*)(CGI->playerint[i]))->showInfoDialog(text,*components);
  327. }
  328. }
  329. }
  330. int CScriptCallback::getSelectedHero()
  331. {
  332. int ret;
  333. if (LOCPLINT->adventureInt->selection.type == HEROI_TYPE)
  334. ret = ((CGHeroInstance*)(LOCPLINT->adventureInt->selection.selected))->subID;
  335. else
  336. ret = -1;;
  337. return ret;
  338. }
  339. void CLuaCallback::registerFuncs(lua_State * L)
  340. {
  341. lua_newtable(L);
  342. #define REGISTER_C_FUNC(x) \
  343. lua_pushstring(L, #x); \
  344. lua_pushcfunction(L, x); \
  345. lua_rawset(L, -3)
  346. REGISTER_C_FUNC(getPos);
  347. REGISTER_C_FUNC(changePrimSkill);
  348. REGISTER_C_FUNC(getGnrlText);
  349. REGISTER_C_FUNC(getSelectedHero);
  350. /*
  351. REGISTER_C_FUNC(changePrimSkill);
  352. REGISTER_C_FUNC(getGnrlText);
  353. REGISTER_C_FUNC(changePrimSkill);
  354. REGISTER_C_FUNC(getGnrlText);
  355. REGISTER_C_FUNC(changePrimSkill);
  356. REGISTER_C_FUNC(getGnrlText);*/
  357. lua_setglobal(L, "vcmi");
  358. #undef REGISTER_C_FUNC(x)
  359. }
  360. int CLuaCallback::getPos(lua_State * L)//(CGObjectInstance * object);
  361. {
  362. const int args = lua_gettop(L); // number of arguments
  363. if ((args < 1) || !lua_isnumber(L, 1) )
  364. luaL_error(L,
  365. "Incorrect arguments to getPos([Object address])");
  366. CGObjectInstance * object = (CGObjectInstance *)(lua_tointeger(L, 1));
  367. lua_pushinteger(L,object->pos.x);
  368. lua_pushinteger(L,object->pos.y);
  369. lua_pushinteger(L,object->pos.z);
  370. return 3;
  371. }
  372. int CLuaCallback::changePrimSkill(lua_State * L)//(int ID, int which, int val);
  373. {
  374. const int args = lua_gettop(L); // number of arguments
  375. if ((args < 1) || !lua_isnumber(L, 1) ||
  376. ((args >= 2) && !lua_isnumber(L, 2)) ||
  377. ((args >= 3) && !lua_isnumber(L, 3)) )
  378. {
  379. luaL_error(L,
  380. "Incorrect arguments to changePrimSkill([Hero ID], [Which Primary skill], [Change by])");
  381. }
  382. int ID = lua_tointeger(L, 1),
  383. which = lua_tointeger(L, 2),
  384. val = lua_tointeger(L, 3);
  385. CScriptCallback::changePrimSkill(ID,which,val);
  386. return 0;
  387. }
  388. int CLuaCallback::getGnrlText(lua_State * L) //(int which),returns string
  389. {
  390. const int args = lua_gettop(L); // number of arguments
  391. if ((args < 1) || !lua_isnumber(L, 1) )
  392. luaL_error(L,
  393. "Incorrect arguments to getGnrlText([Text ID])");
  394. int which = lua_tointeger(L,1);
  395. lua_pushstring(L,CGI->generaltexth->allTexts[which].c_str());
  396. return 1;
  397. }
  398. int CLuaCallback::getSelectedHero(lua_State * L) //(),returns int (ID of hero, -1 if no hero is seleceted)
  399. {
  400. int ret;
  401. if (LOCPLINT->adventureInt->selection.type == HEROI_TYPE)
  402. ret = ((CGHeroInstance*)(LOCPLINT->adventureInt->selection.selected))->subID;
  403. else
  404. ret = -1;
  405. lua_pushinteger(L,ret);
  406. return 1;
  407. }