CCallback.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728
  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(CGHeroInstance::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. //inform leaved objects
  121. std::vector< CGObjectInstance * > leave = CGI->mh->getVisitableObjs(CGHeroInstance::convertPosition(curd.src,false));
  122. for (int iii=0; iii<leave.size(); iii++) //if object is visitable we call onHeroVisit
  123. {
  124. //TODO: allow to handle this in LUA
  125. if(leave[iii]->state) //hard-coded function
  126. leave[iii]->state->onHeroLeave(leave[iii],curd.ho->subID);
  127. }
  128. //reveal fog of war
  129. int heroSight = hero->getSightDistance();
  130. int xbeg = stpos.x - heroSight - 2;
  131. if(xbeg < 0)
  132. xbeg = 0;
  133. int xend = stpos.x + heroSight + 2;
  134. if(xend >= CGI->ac->map.width)
  135. xend = CGI->ac->map.width;
  136. int ybeg = stpos.y - heroSight - 2;
  137. if(ybeg < 0)
  138. ybeg = 0;
  139. int yend = stpos.y + heroSight + 2;
  140. if(yend >= CGI->ac->map.height)
  141. yend = CGI->ac->map.height;
  142. for(int xd=xbeg; xd<xend; ++xd) //revealing part of map around heroes
  143. {
  144. for(int yd=ybeg; yd<yend; ++yd)
  145. {
  146. int deltaX = (hero->getPosition(false).x-xd)*(hero->getPosition(false).x-xd);
  147. int deltaY = (hero->getPosition(false).y-yd)*(hero->getPosition(false).y-yd);
  148. if(deltaX+deltaY<hero->getSightDistance()*hero->getSightDistance())
  149. {
  150. if(gs->players[player].fogOfWarMap[xd][yd][hero->getPosition(false).z] == 0)
  151. {
  152. CGI->playerint[gs->players[player].serial]->tileRevealed(int3(xd, yd, hero->getPosition(false).z));
  153. }
  154. gs->players[player].fogOfWarMap[xd][yd][hero->getPosition(false).z] = 1;
  155. }
  156. }
  157. }
  158. //notify interfacesabout move
  159. int nn=0; //number of interfece of currently browsed player
  160. for(std::map<int, PlayerState>::iterator j=CGI->state->players.begin(); j!=CGI->state->players.end(); ++j)//CGI->state->players.size(); ++j) //for testing
  161. {
  162. if (j->first > PLAYER_LIMIT)
  163. break;
  164. if(j->second.fogOfWarMap[stpos.x-1][stpos.y][stpos.z] || j->second.fogOfWarMap[endpos.x-1][endpos.y][endpos.z])
  165. { //player should be notified
  166. CGI->playerint[j->second.serial]->heroMoved(curd);
  167. }
  168. ++nn;
  169. }
  170. //call objects if they arevisited
  171. for (int iii=0; iii<vis.size(); iii++) //if object is visitable we call onHeroVisit
  172. {
  173. if(gs->checkFunc(vis[iii]->ID,"heroVisit")) //script function
  174. gs->objscr[vis[iii]->ID]["heroVisit"]->onHeroVisit(vis[iii],curd.ho->subID);
  175. if(vis[iii]->state) //hard-coded function
  176. vis[iii]->state->onHeroVisit(vis[iii],curd.ho->subID);
  177. }
  178. }
  179. else //interaction with blocking object (like resources)
  180. {
  181. curd.successful = false;
  182. CGI->playerint[gs->players[hero->getOwner()].serial]->heroMoved(curd);
  183. for (int iii=0; iii<vis.size(); iii++) //if object is visitable we call onHeroVisit
  184. {
  185. if (vis[iii]->blockVisit)
  186. {
  187. if(gs->checkFunc(vis[iii]->ID,"heroVisit")) //script function
  188. gs->objscr[vis[iii]->ID]["heroVisit"]->onHeroVisit(vis[iii],curd.ho->subID);
  189. if(vis[iii]->state) //hard-coded function
  190. vis[iii]->state->onHeroVisit(vis[iii],curd.ho->subID);
  191. }
  192. }
  193. return false;
  194. }
  195. }
  196. else
  197. return true; //move ended - no more movement points
  198. }
  199. return true;
  200. }
  201. void CCallback::selectionMade(int selection, int asker)
  202. {
  203. //todo - jak bedzie multiplayer po sieci, to moze wymagac przerobek zaleznych od obranego modelu
  204. IChosen * ask = (IChosen *)asker;
  205. ask->chosen(selection);
  206. }
  207. int CCallback::howManyTowns()
  208. {
  209. return gs->players[gs->currentPlayer].towns.size();
  210. }
  211. const CGTownInstance * CCallback::getTownInfo(int val, bool mode) //mode = 0 -> val = serial; mode = 1 -> val = ID
  212. {
  213. if (!mode)
  214. return gs->players[gs->currentPlayer].towns[val];
  215. else
  216. {
  217. //TODO: add some smart ID to the CTownInstance
  218. //for (int i=0; i<gs->players[gs->currentPlayer].towns.size();i++)
  219. //{
  220. // if (gs->players[gs->currentPlayer].towns[i]->someID==val)
  221. // return gs->players[gs->currentPlayer].towns[i];
  222. //}
  223. return NULL;
  224. }
  225. return NULL;
  226. }
  227. int CCallback::howManyHeroes()
  228. {
  229. return gs->players[player].heroes.size();
  230. }
  231. const CGHeroInstance * CCallback::getHeroInfo(int player, int val, bool mode) //mode = 0 -> val = serial; mode = 1 -> val = ID
  232. {
  233. if (gs->currentPlayer!=player) //TODO: checking if we are allowed to give that info
  234. return NULL;
  235. if (!mode)
  236. if(val<gs->players[player].heroes.size())
  237. return gs->players[player].heroes[val];
  238. else return NULL;
  239. else
  240. {
  241. for (int i=0; i<gs->players[player].heroes.size();i++)
  242. {
  243. if (gs->players[player].heroes[i]->type->ID==val)
  244. return gs->players[player].heroes[i];
  245. }
  246. }
  247. return NULL;
  248. }
  249. int CCallback::getResourceAmount(int type)
  250. {
  251. return gs->players[player].resources[type];
  252. }
  253. int CCallback::getDate(int mode)
  254. {
  255. int temp;
  256. switch (mode)
  257. {
  258. case 0:
  259. return gs->day;
  260. break;
  261. case 1:
  262. temp = (gs->day)%7;
  263. if (temp)
  264. return temp;
  265. else return 7;
  266. break;
  267. case 2:
  268. temp = ((gs->day-1)/7)+1;
  269. if (!(temp%4))
  270. return 4;
  271. else
  272. return (temp%4);
  273. break;
  274. case 3:
  275. return ((gs->day-1)/28)+1;
  276. break;
  277. }
  278. return 0;
  279. }
  280. bool CCallback::verifyPath(CPath * path, bool blockSea)
  281. {
  282. for (int i=0;i<path->nodes.size();i++)
  283. {
  284. if ( CGI->mh->ttiles[path->nodes[i].coord.x][path->nodes[i].coord.y][path->nodes[i].coord.z].blocked
  285. && (! (CGI->mh->ttiles[path->nodes[i].coord.x][path->nodes[i].coord.y][path->nodes[i].coord.z].visitable)))
  286. return false; //path is wrong - one of the tiles is blocked
  287. if (blockSea)
  288. {
  289. if (i==0)
  290. continue;
  291. if (
  292. ((CGI->mh->ttiles[path->nodes[i].coord.x][path->nodes[i].coord.y][path->nodes[i].coord.z].terType==EterrainType::water)
  293. &&
  294. (CGI->mh->ttiles[path->nodes[i-1].coord.x][path->nodes[i-1].coord.y][path->nodes[i-1].coord.z].terType!=EterrainType::water))
  295. ||
  296. ((CGI->mh->ttiles[path->nodes[i].coord.x][path->nodes[i].coord.y][path->nodes[i].coord.z].terType!=EterrainType::water)
  297. &&
  298. (CGI->mh->ttiles[path->nodes[i-1].coord.x][path->nodes[i-1].coord.y][path->nodes[i-1].coord.z].terType==EterrainType::water))
  299. ||
  300. (CGI->mh->ttiles[path->nodes[i-1].coord.x][path->nodes[i-1].coord.y][path->nodes[i-1].coord.z].terType==EterrainType::rock)
  301. )
  302. return false;
  303. }
  304. }
  305. return true;
  306. }
  307. std::vector < std::string > CCallback::getObjDescriptions(int3 pos)
  308. {
  309. if(gs->players[player].fogOfWarMap[pos.x][pos.y][pos.z])
  310. return CGI->mh->getObjDescriptions(pos);
  311. else return std::vector< std::string > ();
  312. }
  313. PseudoV< PseudoV< PseudoV<unsigned char> > > & CCallback::getVisibilityMap()
  314. {
  315. return gs->players[player].fogOfWarMap;
  316. }
  317. bool CCallback::isVisible(int3 pos, int Player)
  318. {
  319. return gs->players[Player].fogOfWarMap[pos.x][pos.y][pos.z];
  320. }
  321. std::vector < const CGTownInstance *> CCallback::getTownsInfo(bool onlyOur)
  322. {
  323. std::vector < const CGTownInstance *> ret = std::vector < const CGTownInstance *>();
  324. for ( std::map<int, PlayerState>::iterator i=gs->players.begin() ; i!=gs->players.end();i++)
  325. {
  326. for (int j=0;j<(*i).second.towns.size();j++)
  327. {
  328. if ( ( isVisible((*i).second.towns[j]->pos,player) ) || (*i).first==player)
  329. {
  330. ret.push_back((*i).second.towns[j]);
  331. }
  332. }
  333. } // for ( std::map<int, PlayerState>::iterator i=gs->players.begin() ; i!=gs->players.end();i++)
  334. return ret;
  335. }
  336. std::vector < const CGHeroInstance *> CCallback::getHeroesInfo(bool onlyOur)
  337. {
  338. std::vector < const CGHeroInstance *> ret = std::vector < const CGHeroInstance *>();
  339. for ( std::map<int, PlayerState>::iterator i=gs->players.begin() ; i!=gs->players.end();i++)
  340. {
  341. for (int j=0;j<(*i).second.heroes.size();j++)
  342. {
  343. if ( ( isVisible((*i).second.heroes[j]->getPosition(false),player) ) || (*i).first==player)
  344. {
  345. ret.push_back((*i).second.heroes[j]);
  346. }
  347. }
  348. } // for ( std::map<int, PlayerState>::iterator i=gs->players.begin() ; i!=gs->players.end();i++)
  349. return ret;
  350. }
  351. bool CCallback::isVisible(int3 pos)
  352. {
  353. return isVisible(pos,player);
  354. }
  355. int CCallback::getMyColor()
  356. {
  357. return player;
  358. }
  359. int CCallback::getHeroSerial(const CGHeroInstance * hero)
  360. {
  361. for (int i=0; i<gs->players[player].heroes.size();i++)
  362. {
  363. if (gs->players[player].heroes[i]==hero)
  364. return i;
  365. }
  366. return -1;
  367. }
  368. const CCreatureSet* CCallback::getGarrison(const CGObjectInstance *obj)
  369. {
  370. if(!obj)
  371. return NULL;
  372. if(obj->ID == 34)
  373. return &(dynamic_cast<const CGHeroInstance*>(obj))->army;
  374. else if(obj->ID == 98)
  375. return &(dynamic_cast<const CGTownInstance*>(obj)->garrison);
  376. else return NULL;
  377. }
  378. int CCallback::swapCreatures(const CGObjectInstance *s1, const CGObjectInstance *s2, int p1, int p2)
  379. {
  380. CCreatureSet *S1 = const_cast<CCreatureSet*>(getGarrison(s1)), *S2 = const_cast<CCreatureSet*>(getGarrison(s2));
  381. if (false)
  382. {
  383. //TODO: check if we are allowed to swap these creatures
  384. return -1;
  385. }
  386. //if(S1->slots[p1].first)
  387. {
  388. //if(s2->slots[p2].first)
  389. {
  390. CCreature * pom = S2->slots[p2].first;
  391. S2->slots[p2].first = S1->slots[p1].first;
  392. S1->slots[p1].first = pom;
  393. int pom2 = S2->slots[p2].second;
  394. S2->slots[p2].second = S1->slots[p1].second;
  395. S1->slots[p1].second = pom2;
  396. if(!S1->slots[p1].first)
  397. S1->slots.erase(p1);
  398. if(!S2->slots[p2].first)
  399. S2->slots.erase(p2);
  400. if(s1->tempOwner<PLAYER_LIMIT)
  401. CGI->playerint[s1->tempOwner]->garrisonChanged(s1);
  402. if((s2->tempOwner<PLAYER_LIMIT) && (s2 != s1))
  403. CGI->playerint[s2->tempOwner]->garrisonChanged(s2);
  404. return 0;
  405. }
  406. }
  407. return -1;
  408. }
  409. int CCallback::mergeStacks(const CGObjectInstance *s1, const CGObjectInstance *s2, int p1, int p2)
  410. {
  411. return -1;
  412. }
  413. int CCallback::splitStack(const CGObjectInstance *s1, const CGObjectInstance *s2, int p1, int p2, int val)
  414. {
  415. return -1;
  416. }
  417. bool CCallback::dismissHero(const CGHeroInstance *hero)
  418. {
  419. CGHeroInstance * Vhero = const_cast<CGHeroInstance *>(hero);
  420. CGI->mh->removeObject(Vhero);
  421. std::vector<CGHeroInstance*>::iterator nitr = find(CGI->state->players[player].heroes.begin(), CGI->state->players[player].heroes.end(), Vhero);
  422. CGI->state->players[player].heroes.erase(nitr);
  423. LOCPLINT->adventureInt->heroList.updateHList();
  424. return false;
  425. }
  426. int CCallback::getMySerial()
  427. {
  428. return gs->players[player].serial;
  429. }
  430. bool CCallback::swapArifacts(const CGHeroInstance * hero1, bool worn1, int pos1, const CGHeroInstance * hero2, bool worn2, int pos2)
  431. {
  432. if(!hero1 || !hero2) //incorrect data
  433. return false;
  434. CGHeroInstance * Uhero1 = const_cast<CGHeroInstance *>(hero1);
  435. CGHeroInstance * Uhero2 = const_cast<CGHeroInstance *>(hero2);
  436. if(worn1 && worn2)
  437. {
  438. std::swap(Uhero1->artifWorn[pos1], Uhero2->artifWorn[pos2]);
  439. }
  440. else if(worn1 && !worn2)
  441. {
  442. std::swap(Uhero1->artifWorn[pos1], Uhero2->artifacts[pos2]);
  443. }
  444. else if(!worn1 && worn2)
  445. {
  446. std::swap(Uhero1->artifacts[pos1], Uhero2->artifWorn[pos2]);
  447. }
  448. else
  449. {
  450. std::swap(Uhero1->artifacts[pos1], Uhero2->artifacts[pos2]);
  451. }
  452. return true;
  453. }
  454. int3 CScriptCallback::getPos(CGObjectInstance * ob)
  455. {
  456. return ob->pos;
  457. }
  458. void CScriptCallback::changePrimSkill(int ID, int which, int val)
  459. {
  460. CGHeroInstance * hero = CGI->state->getHero(ID,0);
  461. if (which<PRIMARY_SKILLS)
  462. {
  463. hero->primSkills[which]+=val;
  464. for (int i=0; i<CGI->playerint.size(); i++)
  465. {
  466. if (CGI->playerint[i]->playerID == hero->getOwner())
  467. {
  468. CGI->playerint[i]->heroPrimarySkillChanged(hero, which, val);
  469. break;
  470. }
  471. }
  472. }
  473. else if (which==4)
  474. {
  475. hero->exp+=val;
  476. std::cout << "Bohater o ID " << ID <<" (" <<CGI->heroh->heroes[ID]->name <<") dostaje "<<val<<" expa, ale nic z tym nie umiem zrobic :("<<std::endl;
  477. //TODO - powiadomic interfejsy, sprawdzic czy nie ma awansu itp
  478. }
  479. }
  480. int CScriptCallback::getHeroOwner(int heroID)
  481. {
  482. CGHeroInstance * hero = CGI->state->getHero(heroID,0);
  483. return hero->getOwner();
  484. }
  485. void CScriptCallback::showInfoDialog(int player, std::string text, std::vector<SComponent*> * components)
  486. {
  487. //TODO: upewniac sie ze mozemy to zrzutowac (przy customowych interfejsach cos moze sie kopnac)
  488. if (player>=0)
  489. {
  490. CGameInterface * temp = CGI->playerint[CGI->state->players[player].serial];
  491. if (temp->human)
  492. ((CPlayerInterface*)(temp))->showInfoDialog(text,*components);
  493. return;
  494. }
  495. else
  496. {
  497. for (int i=0; i<CGI->playerint.size();i++)
  498. {
  499. if (CGI->playerint[i]->human)
  500. ((CPlayerInterface*)(CGI->playerint[i]))->showInfoDialog(text,*components);
  501. }
  502. }
  503. }
  504. void CScriptCallback::showSelDialog(int player, std::string text, std::vector<CSelectableComponent*>*components, IChosen * asker)
  505. {
  506. CGameInterface * temp = CGI->playerint[CGI->state->players[player].serial];
  507. if (temp->human)
  508. ((CPlayerInterface*)(temp))->showSelDialog(text,*components,(int)asker);
  509. return;
  510. }
  511. int CScriptCallback::getSelectedHero()
  512. {
  513. int ret;
  514. if (LOCPLINT->adventureInt->selection.type == HEROI_TYPE)
  515. ret = ((CGHeroInstance*)(LOCPLINT->adventureInt->selection.selected))->subID;
  516. else
  517. ret = -1;;
  518. return ret;
  519. }
  520. int CScriptCallback::getDate(int mode)
  521. {
  522. int temp;
  523. switch (mode)
  524. {
  525. case 0:
  526. return gs->day;
  527. break;
  528. case 1:
  529. temp = (gs->day)%7;
  530. if (temp)
  531. return temp;
  532. else return 7;
  533. break;
  534. case 2:
  535. temp = ((gs->day-1)/7)+1;
  536. if (!(temp%4))
  537. return 4;
  538. else
  539. return (temp%4);
  540. break;
  541. case 3:
  542. return ((gs->day-1)/28)+1;
  543. break;
  544. }
  545. return 0;
  546. }
  547. void CScriptCallback::giveResource(int player, int which, int val)
  548. {
  549. gs->players[player].resources[which]+=val;
  550. CGI->playerint[gs->players[player].serial]->receivedResource(which,val);
  551. }
  552. void CScriptCallback::showCompInfo(int player, SComponent * comp)
  553. {
  554. CPlayerInterface * i = dynamic_cast<CPlayerInterface*>(CGI->playerint[gs->players[player].serial]);
  555. if(i)
  556. i->showComp(*comp);
  557. }
  558. void CScriptCallback::heroVisitCastle(CGObjectInstance * ob, int heroID)
  559. {
  560. CGTownInstance * n;
  561. if(n = dynamic_cast<CGTownInstance*>(ob))
  562. {
  563. n->visitingHero = CGI->state->getHero(heroID,0);
  564. CGI->playerint[getHeroOwner(heroID)]->heroVisitsTown(CGI->state->getHero(heroID,0),n);
  565. }
  566. else
  567. return;
  568. }
  569. void CScriptCallback::stopHeroVisitCastle(CGObjectInstance * ob, int heroID)
  570. {
  571. CGTownInstance * n;
  572. if(n = dynamic_cast<CGTownInstance*>(ob))
  573. {
  574. if(n->visitingHero->type->ID == heroID)
  575. n->visitingHero = NULL;
  576. }
  577. else
  578. return;
  579. }
  580. void CScriptCallback::giveHeroArtifact(int artid, int hid, int position) //pos==-1 - first free slot in backpack
  581. {
  582. CGHeroInstance* h = gs->getHero(hid,0);
  583. if(position<0)
  584. {
  585. for(int i=0;i<h->artifacts.size();i++)
  586. {
  587. if(!h->artifacts[i])
  588. {
  589. h->artifacts[i] = &CGI->arth->artifacts[artid];
  590. return;
  591. }
  592. }
  593. h->artifacts.push_back(&CGI->arth->artifacts[artid]);
  594. return;
  595. }
  596. else
  597. {
  598. if(h->artifWorn[position]) //slot is occupied
  599. {
  600. giveHeroArtifact(h->artifWorn[position]->id,hid,-1);
  601. }
  602. h->artifWorn[position] = &CGI->arth->artifacts[artid];
  603. }
  604. }
  605. void CLuaCallback::registerFuncs(lua_State * L)
  606. {
  607. lua_newtable(L);
  608. #define REGISTER_C_FUNC(x) \
  609. lua_pushstring(L, #x); \
  610. lua_pushcfunction(L, x); \
  611. lua_rawset(L, -3)
  612. REGISTER_C_FUNC(getPos);
  613. REGISTER_C_FUNC(changePrimSkill);
  614. REGISTER_C_FUNC(getGnrlText);
  615. REGISTER_C_FUNC(getSelectedHero);
  616. /*
  617. REGISTER_C_FUNC(changePrimSkill);
  618. REGISTER_C_FUNC(getGnrlText);
  619. REGISTER_C_FUNC(changePrimSkill);
  620. REGISTER_C_FUNC(getGnrlText);
  621. REGISTER_C_FUNC(changePrimSkill);
  622. REGISTER_C_FUNC(getGnrlText);*/
  623. lua_setglobal(L, "vcmi");
  624. #undef REGISTER_C_FUNC
  625. }
  626. int CLuaCallback::getPos(lua_State * L)//(CGObjectInstance * object);
  627. {
  628. const int args = lua_gettop(L); // number of arguments
  629. if ((args < 1) || !lua_isnumber(L, 1) )
  630. luaL_error(L,
  631. "Incorrect arguments to getPos([Object address])");
  632. CGObjectInstance * object = (CGObjectInstance *)(lua_tointeger(L, 1));
  633. lua_pushinteger(L,object->pos.x);
  634. lua_pushinteger(L,object->pos.y);
  635. lua_pushinteger(L,object->pos.z);
  636. return 3;
  637. }
  638. int CLuaCallback::changePrimSkill(lua_State * L)//(int ID, int which, int val);
  639. {
  640. const int args = lua_gettop(L); // number of arguments
  641. if ((args < 1) || !lua_isnumber(L, 1) ||
  642. ((args >= 2) && !lua_isnumber(L, 2)) ||
  643. ((args >= 3) && !lua_isnumber(L, 3)) )
  644. {
  645. luaL_error(L,
  646. "Incorrect arguments to changePrimSkill([Hero ID], [Which Primary skill], [Change by])");
  647. }
  648. int ID = lua_tointeger(L, 1),
  649. which = lua_tointeger(L, 2),
  650. val = lua_tointeger(L, 3);
  651. CScriptCallback::changePrimSkill(ID,which,val);
  652. return 0;
  653. }
  654. int CLuaCallback::getGnrlText(lua_State * L) //(int which),returns string
  655. {
  656. const int args = lua_gettop(L); // number of arguments
  657. if ((args < 1) || !lua_isnumber(L, 1) )
  658. luaL_error(L,
  659. "Incorrect arguments to getGnrlText([Text ID])");
  660. int which = lua_tointeger(L,1);
  661. lua_pushstring(L,CGI->generaltexth->allTexts[which].c_str());
  662. return 1;
  663. }
  664. int CLuaCallback::getSelectedHero(lua_State * L) //(),returns int (ID of hero, -1 if no hero is seleceted)
  665. {
  666. int ret;
  667. if (LOCPLINT->adventureInt->selection.type == HEROI_TYPE)
  668. ret = ((CGHeroInstance*)(LOCPLINT->adventureInt->selection.selected))->subID;
  669. else
  670. ret = -1;
  671. lua_pushinteger(L,ret);
  672. return 1;
  673. }