CCallback.cpp 13 KB

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