CCallback.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  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. std::vector< CGObjectInstance * > vis = CGI->mh->getVisitableObjs(CHeroInstance::convertPosition(curd.dst,false));
  112. bool blockvis = false;
  113. for (int pit = 0; pit<vis.size();pit++)
  114. if (vis[pit]->blockVisit)
  115. blockvis = true;
  116. if (!blockvis)
  117. {
  118. curd.successful = true;
  119. hero->pos = curd.dst;
  120. int heroSight = hero->getSightDistance();
  121. int xbeg = stpos.x - heroSight - 2;
  122. if(xbeg < 0)
  123. xbeg = 0;
  124. int xend = stpos.x + heroSight + 2;
  125. if(xend >= CGI->ac->map.width)
  126. xend = CGI->ac->map.width;
  127. int ybeg = stpos.y - heroSight - 2;
  128. if(ybeg < 0)
  129. ybeg = 0;
  130. int yend = stpos.y + heroSight + 2;
  131. if(yend >= CGI->ac->map.height)
  132. yend = CGI->ac->map.height;
  133. for(int xd=xbeg; xd<xend; ++xd) //revealing part of map around heroes
  134. {
  135. for(int yd=ybeg; yd<yend; ++yd)
  136. {
  137. int deltaX = (hero->getPosition(false).x-xd)*(hero->getPosition(false).x-xd);
  138. int deltaY = (hero->getPosition(false).y-yd)*(hero->getPosition(false).y-yd);
  139. if(deltaX+deltaY<hero->getSightDistance()*hero->getSightDistance())
  140. {
  141. if(gs->players[player].fogOfWarMap[xd][yd][hero->getPosition(false).z] == 0)
  142. {
  143. CGI->playerint[gs->players[player].serial]->tileRevealed(int3(xd, yd, hero->getPosition(false).z));
  144. }
  145. gs->players[player].fogOfWarMap[xd][yd][hero->getPosition(false).z] = 1;
  146. }
  147. }
  148. }
  149. int nn=0; //number of interfece of currently browsed player
  150. for(std::map<int, PlayerState>::iterator j=CGI->state->players.begin(); j!=CGI->state->players.end(); ++j)//CGI->state->players.size(); ++j) //for testing
  151. {
  152. if (j->first > PLAYER_LIMIT)
  153. break;
  154. if(j->second.fogOfWarMap[stpos.x-1][stpos.y][stpos.z] || j->second.fogOfWarMap[endpos.x-1][endpos.y][endpos.z])
  155. { //player should be notified
  156. CGI->playerint[j->second.serial]->heroMoved(curd);
  157. }
  158. ++nn;
  159. }
  160. for (int iii=0; iii<vis.size(); iii++) //if object is visitable we call onHeroVisit
  161. {
  162. if(gs->checkFunc(vis[iii]->ID,"heroVisit")) //script function
  163. gs->objscr[vis[iii]->ID]["heroVisit"]->onHeroVisit(vis[iii],curd.ho->subID);
  164. if(vis[iii]->state) //hard-coded function
  165. vis[iii]->state->onHeroVisit(vis[iii],curd.ho->subID);
  166. }
  167. }
  168. else //interaction with blocking object (like resources)
  169. {
  170. curd.successful = false;
  171. CGI->playerint[gs->players[hero->getOwner()].serial]->heroMoved(curd);
  172. for (int iii=0; iii<vis.size(); iii++) //if object is visitable we call onHeroVisit
  173. {
  174. if (vis[iii]->blockVisit)
  175. {
  176. if(gs->checkFunc(vis[iii]->ID,"heroVisit")) //script function
  177. gs->objscr[vis[iii]->ID]["heroVisit"]->onHeroVisit(vis[iii],curd.ho->subID);
  178. if(vis[iii]->state) //hard-coded function
  179. vis[iii]->state->onHeroVisit(vis[iii],curd.ho->subID);
  180. }
  181. }
  182. return false;
  183. }
  184. }
  185. else
  186. return true; //move ended - no more movement points
  187. }
  188. return true;
  189. }
  190. void CCallback::selectionMade(int selection, int asker)
  191. {
  192. //todo - jak bedzie multiplayer po sieci, to moze wymagac przerobek zaleznych od obranego modelu
  193. IChosen * ask = (IChosen *)asker;
  194. ask->chosen(selection);
  195. }
  196. int CCallback::howManyTowns()
  197. {
  198. return gs->players[gs->currentPlayer].towns.size();
  199. }
  200. const CGTownInstance * CCallback::getTownInfo(int val, bool mode) //mode = 0 -> val = serial; mode = 1 -> val = ID
  201. {
  202. if (!mode)
  203. return gs->players[gs->currentPlayer].towns[val];
  204. else
  205. {
  206. //TODO: add some smart ID to the CTownInstance
  207. //for (int i=0; i<gs->players[gs->currentPlayer].towns.size();i++)
  208. //{
  209. // if (gs->players[gs->currentPlayer].towns[i]->someID==val)
  210. // return gs->players[gs->currentPlayer].towns[i];
  211. //}
  212. return NULL;
  213. }
  214. return NULL;
  215. }
  216. int CCallback::howManyHeroes()
  217. {
  218. return gs->players[player].heroes.size();
  219. }
  220. const CGHeroInstance * CCallback::getHeroInfo(int player, int val, bool mode) //mode = 0 -> val = serial; mode = 1 -> val = ID
  221. {
  222. if (gs->currentPlayer!=player) //TODO: checking if we are allowed to give that info
  223. return NULL;
  224. if (!mode)
  225. return gs->players[player].heroes[val];
  226. else
  227. {
  228. for (int i=0; i<gs->players[player].heroes.size();i++)
  229. {
  230. if (gs->players[player].heroes[i]->type->ID==val)
  231. return gs->players[player].heroes[i];
  232. }
  233. }
  234. return NULL;
  235. }
  236. int CCallback::getResourceAmount(int type)
  237. {
  238. return gs->players[player].resources[type];
  239. }
  240. int CCallback::getDate(int mode)
  241. {
  242. int temp;
  243. switch (mode)
  244. {
  245. case 0:
  246. return gs->day;
  247. break;
  248. case 1:
  249. temp = (gs->day)%7;
  250. if (temp)
  251. return temp;
  252. else return 7;
  253. break;
  254. case 2:
  255. temp = ((gs->day-1)/7)+1;
  256. if (!(temp%4))
  257. return 4;
  258. else
  259. return (temp%4);
  260. break;
  261. case 3:
  262. return ((gs->day-1)/28)+1;
  263. break;
  264. }
  265. return 0;
  266. }
  267. bool CCallback::verifyPath(CPath * path, bool blockSea)
  268. {
  269. for (int i=0;i<path->nodes.size();i++)
  270. {
  271. if ( CGI->mh->ttiles[path->nodes[i].coord.x][path->nodes[i].coord.y][path->nodes[i].coord.z].blocked
  272. && (! (CGI->mh->ttiles[path->nodes[i].coord.x][path->nodes[i].coord.y][path->nodes[i].coord.z].visitable)))
  273. return false; //path is wrong - one of the tiles is blocked
  274. if (blockSea)
  275. {
  276. if (i==0)
  277. continue;
  278. if (
  279. ((CGI->mh->ttiles[path->nodes[i].coord.x][path->nodes[i].coord.y][path->nodes[i].coord.z].terType==EterrainType::water)
  280. &&
  281. (CGI->mh->ttiles[path->nodes[i-1].coord.x][path->nodes[i-1].coord.y][path->nodes[i-1].coord.z].terType!=EterrainType::water))
  282. ||
  283. ((CGI->mh->ttiles[path->nodes[i].coord.x][path->nodes[i].coord.y][path->nodes[i].coord.z].terType!=EterrainType::water)
  284. &&
  285. (CGI->mh->ttiles[path->nodes[i-1].coord.x][path->nodes[i-1].coord.y][path->nodes[i-1].coord.z].terType==EterrainType::water))
  286. ||
  287. (CGI->mh->ttiles[path->nodes[i-1].coord.x][path->nodes[i-1].coord.y][path->nodes[i-1].coord.z].terType==EterrainType::rock)
  288. )
  289. return false;
  290. }
  291. }
  292. return true;
  293. }
  294. std::vector < std::string > CCallback::getObjDescriptions(int3 pos)
  295. {
  296. if(gs->players[player].fogOfWarMap[pos.x][pos.y][pos.z])
  297. return CGI->mh->getObjDescriptions(pos);
  298. else return std::vector< std::string > ();
  299. }
  300. PseudoV< PseudoV< PseudoV<unsigned char> > > & CCallback::getVisibilityMap()
  301. {
  302. return gs->players[player].fogOfWarMap;
  303. }
  304. bool CCallback::isVisible(int3 pos, int Player)
  305. {
  306. return gs->players[Player].fogOfWarMap[pos.x][pos.y][pos.z];
  307. }
  308. std::vector < const CGHeroInstance *> CCallback::getHeroesInfo(bool onlyOur)
  309. {
  310. std::vector < const CGHeroInstance *> ret = std::vector < const CGHeroInstance *>();
  311. for ( std::map<int, PlayerState>::iterator i=gs->players.begin() ; i!=gs->players.end();i++)
  312. {
  313. for (int j=0;j<(*i).second.heroes.size();j++)
  314. {
  315. if ( ( isVisible((*i).second.heroes[j]->getPosition(false),player) ) || (*i).first==player)
  316. {
  317. ret.push_back((*i).second.heroes[j]);
  318. }
  319. }
  320. } // for ( std::map<int, PlayerState>::iterator i=gs->players.begin() ; i!=gs->players.end();i++)
  321. return ret;
  322. }
  323. bool CCallback::isVisible(int3 pos)
  324. {
  325. return isVisible(pos,player);
  326. }
  327. int CCallback::getMyColor()
  328. {
  329. return player;
  330. }
  331. int CCallback::getHeroSerial(const CGHeroInstance * hero)
  332. {
  333. for (int i=0; i<gs->players[player].heroes.size();i++)
  334. {
  335. if (gs->players[player].heroes[i]==hero)
  336. return i;
  337. }
  338. return -1;
  339. }
  340. int CCallback::getMySerial()
  341. {
  342. return gs->players[player].serial;
  343. }
  344. int3 CScriptCallback::getPos(CGObjectInstance * ob)
  345. {
  346. return ob->pos;
  347. }
  348. void CScriptCallback::changePrimSkill(int ID, int which, int val)
  349. {
  350. CGHeroInstance * hero = CGI->state->getHero(ID,0);
  351. if (which<PRIMARY_SKILLS)
  352. {
  353. hero->primSkills[which]+=val;
  354. for (int i=0; i<CGI->playerint.size(); i++)
  355. {
  356. if (CGI->playerint[i]->playerID == hero->getOwner())
  357. {
  358. CGI->playerint[i]->heroPrimarySkillChanged(hero, which, val);
  359. break;
  360. }
  361. }
  362. }
  363. else if (which==4)
  364. {
  365. hero->exp+=val;
  366. std::cout << "Bohater o ID " << ID <<" (" <<CGI->heroh->heroes[ID]->name <<") dostaje "<<val<<" expa, ale nic z tym nie umiem zrobic :("<<std::endl;
  367. //TODO - powiadomic interfejsy, sprawdzic czy nie ma awansu itp
  368. }
  369. }
  370. int CScriptCallback::getHeroOwner(int heroID)
  371. {
  372. CGHeroInstance * hero = CGI->state->getHero(heroID,0);
  373. return hero->getOwner();
  374. }
  375. void CScriptCallback::showInfoDialog(int player, std::string text, std::vector<SComponent*> * components)
  376. {
  377. //TODO: upewniac sie ze mozemy to zrzutowac (przy customowych interfejsach cos moze sie kopnac)
  378. if (player>=0)
  379. {
  380. CGameInterface * temp = CGI->playerint[CGI->state->players[player].serial];
  381. if (temp->human)
  382. ((CPlayerInterface*)(temp))->showInfoDialog(text,*components);
  383. return;
  384. }
  385. else
  386. {
  387. for (int i=0; i<CGI->playerint.size();i++)
  388. {
  389. if (CGI->playerint[i]->human)
  390. ((CPlayerInterface*)(CGI->playerint[i]))->showInfoDialog(text,*components);
  391. }
  392. }
  393. }
  394. void CScriptCallback::showSelDialog(int player, std::string text, std::vector<CSelectableComponent*>*components, IChosen * asker)
  395. {
  396. CGameInterface * temp = CGI->playerint[CGI->state->players[player].serial];
  397. if (temp->human)
  398. ((CPlayerInterface*)(temp))->showSelDialog(text,*components,(int)asker);
  399. return;
  400. }
  401. int CScriptCallback::getSelectedHero()
  402. {
  403. int ret;
  404. if (LOCPLINT->adventureInt->selection.type == HEROI_TYPE)
  405. ret = ((CGHeroInstance*)(LOCPLINT->adventureInt->selection.selected))->subID;
  406. else
  407. ret = -1;;
  408. return ret;
  409. }
  410. int CScriptCallback::getDate(int mode)
  411. {
  412. int temp;
  413. switch (mode)
  414. {
  415. case 0:
  416. return gs->day;
  417. break;
  418. case 1:
  419. temp = (gs->day)%7;
  420. if (temp)
  421. return temp;
  422. else return 7;
  423. break;
  424. case 2:
  425. temp = ((gs->day-1)/7)+1;
  426. if (!(temp%4))
  427. return 4;
  428. else
  429. return (temp%4);
  430. break;
  431. case 3:
  432. return ((gs->day-1)/28)+1;
  433. break;
  434. }
  435. return 0;
  436. }
  437. void CScriptCallback::giveResource(int player, int which, int val)
  438. {
  439. gs->players[player].resources[which]+=val;
  440. CGI->playerint[gs->players[player].serial]->receivedResource(which,val);
  441. }
  442. void CScriptCallback::showCompInfo(int player, SComponent * comp)
  443. {
  444. CPlayerInterface * i = dynamic_cast<CPlayerInterface*>(CGI->playerint[gs->players[player].serial]);
  445. if(i)
  446. i->showComp(*comp);
  447. }
  448. void CLuaCallback::registerFuncs(lua_State * L)
  449. {
  450. lua_newtable(L);
  451. #define REGISTER_C_FUNC(x) \
  452. lua_pushstring(L, #x); \
  453. lua_pushcfunction(L, x); \
  454. lua_rawset(L, -3)
  455. REGISTER_C_FUNC(getPos);
  456. REGISTER_C_FUNC(changePrimSkill);
  457. REGISTER_C_FUNC(getGnrlText);
  458. REGISTER_C_FUNC(getSelectedHero);
  459. /*
  460. REGISTER_C_FUNC(changePrimSkill);
  461. REGISTER_C_FUNC(getGnrlText);
  462. REGISTER_C_FUNC(changePrimSkill);
  463. REGISTER_C_FUNC(getGnrlText);
  464. REGISTER_C_FUNC(changePrimSkill);
  465. REGISTER_C_FUNC(getGnrlText);*/
  466. lua_setglobal(L, "vcmi");
  467. #undef REGISTER_C_FUNC(x)
  468. }
  469. int CLuaCallback::getPos(lua_State * L)//(CGObjectInstance * object);
  470. {
  471. const int args = lua_gettop(L); // number of arguments
  472. if ((args < 1) || !lua_isnumber(L, 1) )
  473. luaL_error(L,
  474. "Incorrect arguments to getPos([Object address])");
  475. CGObjectInstance * object = (CGObjectInstance *)(lua_tointeger(L, 1));
  476. lua_pushinteger(L,object->pos.x);
  477. lua_pushinteger(L,object->pos.y);
  478. lua_pushinteger(L,object->pos.z);
  479. return 3;
  480. }
  481. int CLuaCallback::changePrimSkill(lua_State * L)//(int ID, int which, int val);
  482. {
  483. const int args = lua_gettop(L); // number of arguments
  484. if ((args < 1) || !lua_isnumber(L, 1) ||
  485. ((args >= 2) && !lua_isnumber(L, 2)) ||
  486. ((args >= 3) && !lua_isnumber(L, 3)) )
  487. {
  488. luaL_error(L,
  489. "Incorrect arguments to changePrimSkill([Hero ID], [Which Primary skill], [Change by])");
  490. }
  491. int ID = lua_tointeger(L, 1),
  492. which = lua_tointeger(L, 2),
  493. val = lua_tointeger(L, 3);
  494. CScriptCallback::changePrimSkill(ID,which,val);
  495. return 0;
  496. }
  497. int CLuaCallback::getGnrlText(lua_State * L) //(int which),returns string
  498. {
  499. const int args = lua_gettop(L); // number of arguments
  500. if ((args < 1) || !lua_isnumber(L, 1) )
  501. luaL_error(L,
  502. "Incorrect arguments to getGnrlText([Text ID])");
  503. int which = lua_tointeger(L,1);
  504. lua_pushstring(L,CGI->generaltexth->allTexts[which].c_str());
  505. return 1;
  506. }
  507. int CLuaCallback::getSelectedHero(lua_State * L) //(),returns int (ID of hero, -1 if no hero is seleceted)
  508. {
  509. int ret;
  510. if (LOCPLINT->adventureInt->selection.type == HEROI_TYPE)
  511. ret = ((CGHeroInstance*)(LOCPLINT->adventureInt->selection.selected))->subID;
  512. else
  513. ret = -1;
  514. lua_pushinteger(L,ret);
  515. return 1;
  516. }