CCallback.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818
  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. #include "hch/CBuildingHandler.h"
  16. LUALIB_API int (luaL_error) (lua_State *L, const char *fmt, ...);
  17. int CCallback::lowestSpeed(CGHeroInstance * chi)
  18. {
  19. int min = 150;
  20. for ( std::map<int,std::pair<CCreature*,int> >::iterator i = chi->army.slots.begin();
  21. i!=chi->army.slots.end(); i++ )
  22. {
  23. if (min>(*i).second.first->speed)
  24. min = (*i).second.first->speed;
  25. }
  26. return min;
  27. }
  28. int CCallback::valMovePoints(CGHeroInstance * chi)
  29. {
  30. int ret = 1270+70*lowestSpeed(chi);
  31. if (ret>2000)
  32. ret=2000;
  33. //TODO: additional bonuses (but they aren't currently stored in chi)
  34. return ret;
  35. }
  36. void CCallback::newTurn()
  37. {
  38. //std::map<int, PlayerState>::iterator i = gs->players.begin() ;
  39. gs->day++;
  40. for (std::set<CCPPObjectScript *>::iterator i=gs->cppscripts.begin();i!=gs->cppscripts.end();i++)
  41. {
  42. (*i)->newTurn();
  43. }
  44. for ( std::map<int, PlayerState>::iterator i=gs->players.begin() ; i!=gs->players.end();i++)
  45. {
  46. for (int j=0;j<(*i).second.heroes.size();j++)
  47. {
  48. (*i).second.heroes[j]->movement = valMovePoints((*i).second.heroes[j]);
  49. }
  50. for(int j=0;j<i->second.towns.size();j++)
  51. {
  52. i->second.towns[j]->builded=0;
  53. }
  54. }
  55. }
  56. bool CCallback::moveHero(int ID, CPath * path, int idtype, int pathType)
  57. {
  58. CGHeroInstance * hero = NULL;
  59. if (idtype==0)
  60. {
  61. if (player==-1)
  62. hero=gs->players[player+1].heroes[ID];
  63. else
  64. hero=gs->players[player].heroes[ID];
  65. }
  66. else if (idtype==1 && player>=0) //looking for it in local area
  67. {
  68. for (int i=0; i<gs->players[player].heroes.size();i++)
  69. {
  70. if (gs->players[player].heroes[i]->type->ID == ID)
  71. hero = gs->players[player].heroes[i];
  72. }
  73. }
  74. else //idtype==1; player<0
  75. {
  76. for(std::map<int, PlayerState>::iterator j=CGI->state->players.begin(); j!=CGI->state->players.end(); ++j)
  77. {
  78. for (int i=0; i<(*j).second.heroes.size();i++)
  79. {
  80. if ((*j).second.heroes[i]->type->ID == ID)
  81. {
  82. hero = (*j).second.heroes[i];
  83. }
  84. }
  85. }
  86. }
  87. if (!hero)
  88. return false; //can't find hero
  89. if(!verifyPath(path,!hero->canWalkOnSea()))//TODO: not check sea, if hero has flying or walking on water
  90. return false; //invalid path
  91. //check path format
  92. if (pathType==0)
  93. CPathfinder::convertPath(path,pathType);
  94. if (pathType>1)
  95. throw std::exception("Unknown path format");
  96. CPath * ourPath = path;
  97. if(!ourPath)
  98. return false;
  99. for(int i=ourPath->nodes.size()-1; i>0; i--)
  100. {
  101. int3 stpos, endpos;
  102. stpos = int3(ourPath->nodes[i].coord.x, ourPath->nodes[i].coord.y, hero->pos.z);
  103. endpos = int3(ourPath->nodes[i-1].coord.x, ourPath->nodes[i-1].coord.y, hero->pos.z);
  104. HeroMoveDetails curd;
  105. curd.src = stpos;
  106. curd.dst = endpos;
  107. curd.ho = hero;
  108. curd.owner = hero->getOwner();
  109. /*if(player!=-1)
  110. {
  111. hero->pos = endpos;
  112. }*/
  113. if((hero->movement>=CGI->mh->getCost(stpos, endpos, hero)) || player==-1)
  114. { //performing move
  115. hero->movement-=CGI->mh->getCost(stpos, endpos, hero);
  116. std::vector< CGObjectInstance * > vis = CGI->mh->getVisitableObjs(CGHeroInstance::convertPosition(curd.dst,false));
  117. bool blockvis = false;
  118. for (int pit = 0; pit<vis.size();pit++)
  119. if (vis[pit]->blockVisit)
  120. blockvis = true;
  121. if (!blockvis)
  122. {
  123. curd.successful = true;
  124. hero->pos = curd.dst;
  125. //inform leaved objects
  126. std::vector< CGObjectInstance * > leave = CGI->mh->getVisitableObjs(CGHeroInstance::convertPosition(curd.src,false));
  127. for (int iii=0; iii<leave.size(); iii++) //if object is visitable we call onHeroVisit
  128. {
  129. //TODO: allow to handle this in LUA
  130. if(leave[iii]->state) //hard-coded function
  131. leave[iii]->state->onHeroLeave(leave[iii],curd.ho->subID);
  132. }
  133. //reveal fog of war
  134. int heroSight = hero->getSightDistance();
  135. int xbeg = stpos.x - heroSight - 2;
  136. if(xbeg < 0)
  137. xbeg = 0;
  138. int xend = stpos.x + heroSight + 2;
  139. if(xend >= CGI->ac->map.width)
  140. xend = CGI->ac->map.width;
  141. int ybeg = stpos.y - heroSight - 2;
  142. if(ybeg < 0)
  143. ybeg = 0;
  144. int yend = stpos.y + heroSight + 2;
  145. if(yend >= CGI->ac->map.height)
  146. yend = CGI->ac->map.height;
  147. for(int xd=xbeg; xd<xend; ++xd) //revealing part of map around heroes
  148. {
  149. for(int yd=ybeg; yd<yend; ++yd)
  150. {
  151. int deltaX = (hero->getPosition(false).x-xd)*(hero->getPosition(false).x-xd);
  152. int deltaY = (hero->getPosition(false).y-yd)*(hero->getPosition(false).y-yd);
  153. if(deltaX+deltaY<hero->getSightDistance()*hero->getSightDistance())
  154. {
  155. if(gs->players[player].fogOfWarMap[xd][yd][hero->getPosition(false).z] == 0)
  156. {
  157. CGI->playerint[gs->players[player].serial]->tileRevealed(int3(xd, yd, hero->getPosition(false).z));
  158. }
  159. gs->players[player].fogOfWarMap[xd][yd][hero->getPosition(false).z] = 1;
  160. }
  161. }
  162. }
  163. //notify interfacesabout move
  164. int nn=0; //number of interfece of currently browsed player
  165. for(std::map<int, PlayerState>::iterator j=CGI->state->players.begin(); j!=CGI->state->players.end(); ++j)//CGI->state->players.size(); ++j) //for testing
  166. {
  167. if (j->first > PLAYER_LIMIT)
  168. break;
  169. if(j->second.fogOfWarMap[stpos.x-1][stpos.y][stpos.z] || j->second.fogOfWarMap[endpos.x-1][endpos.y][endpos.z])
  170. { //player should be notified
  171. CGI->playerint[j->second.serial]->heroMoved(curd);
  172. }
  173. ++nn;
  174. }
  175. //call objects if they arevisited
  176. for (int iii=0; iii<vis.size(); iii++) //if object is visitable we call onHeroVisit
  177. {
  178. if(gs->checkFunc(vis[iii]->ID,"heroVisit")) //script function
  179. gs->objscr[vis[iii]->ID]["heroVisit"]->onHeroVisit(vis[iii],curd.ho->subID);
  180. if(vis[iii]->state) //hard-coded function
  181. vis[iii]->state->onHeroVisit(vis[iii],curd.ho->subID);
  182. }
  183. }
  184. else //interaction with blocking object (like resources)
  185. {
  186. curd.successful = false;
  187. CGI->playerint[gs->players[hero->getOwner()].serial]->heroMoved(curd);
  188. for (int iii=0; iii<vis.size(); iii++) //if object is visitable we call onHeroVisit
  189. {
  190. if (vis[iii]->blockVisit)
  191. {
  192. if(gs->checkFunc(vis[iii]->ID,"heroVisit")) //script function
  193. gs->objscr[vis[iii]->ID]["heroVisit"]->onHeroVisit(vis[iii],curd.ho->subID);
  194. if(vis[iii]->state) //hard-coded function
  195. vis[iii]->state->onHeroVisit(vis[iii],curd.ho->subID);
  196. }
  197. }
  198. return false;
  199. }
  200. }
  201. else
  202. return true; //move ended - no more movement points
  203. }
  204. return true;
  205. }
  206. void CCallback::selectionMade(int selection, int asker)
  207. {
  208. //todo - jak bedzie multiplayer po sieci, to moze wymagac przerobek zaleznych od obranego modelu
  209. IChosen * ask = (IChosen *)asker;
  210. ask->chosen(selection);
  211. }
  212. int CCallback::howManyTowns()
  213. {
  214. return gs->players[gs->currentPlayer].towns.size();
  215. }
  216. const CGTownInstance * CCallback::getTownInfo(int val, bool mode) //mode = 0 -> val = serial; mode = 1 -> val = ID
  217. {
  218. if (!mode)
  219. return gs->players[gs->currentPlayer].towns[val];
  220. else
  221. {
  222. //TODO: add some smart ID to the CTownInstance
  223. //for (int i=0; i<gs->players[gs->currentPlayer].towns.size();i++)
  224. //{
  225. // if (gs->players[gs->currentPlayer].towns[i]->someID==val)
  226. // return gs->players[gs->currentPlayer].towns[i];
  227. //}
  228. return NULL;
  229. }
  230. return NULL;
  231. }
  232. int CCallback::howManyHeroes()
  233. {
  234. return gs->players[player].heroes.size();
  235. }
  236. const CGHeroInstance * CCallback::getHeroInfo(int player, int val, bool mode) //mode = 0 -> val = serial; mode = 1 -> val = ID
  237. {
  238. if (gs->currentPlayer!=player) //TODO: checking if we are allowed to give that info
  239. return NULL;
  240. if (!mode)
  241. if(val<gs->players[player].heroes.size())
  242. return gs->players[player].heroes[val];
  243. else return NULL;
  244. else
  245. {
  246. for (int i=0; i<gs->players[player].heroes.size();i++)
  247. {
  248. if (gs->players[player].heroes[i]->type->ID==val)
  249. return gs->players[player].heroes[i];
  250. }
  251. }
  252. return NULL;
  253. }
  254. int CCallback::getResourceAmount(int type)
  255. {
  256. return gs->players[player].resources[type];
  257. }
  258. int CCallback::getDate(int mode)
  259. {
  260. int temp;
  261. switch (mode)
  262. {
  263. case 0:
  264. return gs->day;
  265. break;
  266. case 1:
  267. temp = (gs->day)%7;
  268. if (temp)
  269. return temp;
  270. else return 7;
  271. break;
  272. case 2:
  273. temp = ((gs->day-1)/7)+1;
  274. if (!(temp%4))
  275. return 4;
  276. else
  277. return (temp%4);
  278. break;
  279. case 3:
  280. return ((gs->day-1)/28)+1;
  281. break;
  282. }
  283. return 0;
  284. }
  285. bool CCallback::verifyPath(CPath * path, bool blockSea)
  286. {
  287. for (int i=0;i<path->nodes.size();i++)
  288. {
  289. if ( CGI->mh->ttiles[path->nodes[i].coord.x][path->nodes[i].coord.y][path->nodes[i].coord.z].blocked
  290. && (! (CGI->mh->ttiles[path->nodes[i].coord.x][path->nodes[i].coord.y][path->nodes[i].coord.z].visitable)))
  291. return false; //path is wrong - one of the tiles is blocked
  292. if (blockSea)
  293. {
  294. if (i==0)
  295. continue;
  296. if (
  297. ((CGI->mh->ttiles[path->nodes[i].coord.x][path->nodes[i].coord.y][path->nodes[i].coord.z].terType==EterrainType::water)
  298. &&
  299. (CGI->mh->ttiles[path->nodes[i-1].coord.x][path->nodes[i-1].coord.y][path->nodes[i-1].coord.z].terType!=EterrainType::water))
  300. ||
  301. ((CGI->mh->ttiles[path->nodes[i].coord.x][path->nodes[i].coord.y][path->nodes[i].coord.z].terType!=EterrainType::water)
  302. &&
  303. (CGI->mh->ttiles[path->nodes[i-1].coord.x][path->nodes[i-1].coord.y][path->nodes[i-1].coord.z].terType==EterrainType::water))
  304. ||
  305. (CGI->mh->ttiles[path->nodes[i-1].coord.x][path->nodes[i-1].coord.y][path->nodes[i-1].coord.z].terType==EterrainType::rock)
  306. )
  307. return false;
  308. }
  309. }
  310. return true;
  311. }
  312. std::vector < std::string > CCallback::getObjDescriptions(int3 pos)
  313. {
  314. if(gs->players[player].fogOfWarMap[pos.x][pos.y][pos.z])
  315. return CGI->mh->getObjDescriptions(pos);
  316. else return std::vector< std::string > ();
  317. }
  318. PseudoV< PseudoV< PseudoV<unsigned char> > > & CCallback::getVisibilityMap()
  319. {
  320. return gs->players[player].fogOfWarMap;
  321. }
  322. bool CCallback::isVisible(int3 pos, int Player)
  323. {
  324. return gs->players[Player].fogOfWarMap[pos.x][pos.y][pos.z];
  325. }
  326. std::vector < const CGTownInstance *> CCallback::getTownsInfo(bool onlyOur)
  327. {
  328. std::vector < const CGTownInstance *> ret = std::vector < const CGTownInstance *>();
  329. for ( std::map<int, PlayerState>::iterator i=gs->players.begin() ; i!=gs->players.end();i++)
  330. {
  331. for (int j=0;j<(*i).second.towns.size();j++)
  332. {
  333. if ( ( isVisible((*i).second.towns[j]->pos,player) ) || (*i).first==player)
  334. {
  335. ret.push_back((*i).second.towns[j]);
  336. }
  337. }
  338. } // for ( std::map<int, PlayerState>::iterator i=gs->players.begin() ; i!=gs->players.end();i++)
  339. return ret;
  340. }
  341. std::vector < const CGHeroInstance *> CCallback::getHeroesInfo(bool onlyOur)
  342. {
  343. std::vector < const CGHeroInstance *> ret = std::vector < const CGHeroInstance *>();
  344. for ( std::map<int, PlayerState>::iterator i=gs->players.begin() ; i!=gs->players.end();i++)
  345. {
  346. for (int j=0;j<(*i).second.heroes.size();j++)
  347. {
  348. if ( ( isVisible((*i).second.heroes[j]->getPosition(false),player) ) || (*i).first==player)
  349. {
  350. ret.push_back((*i).second.heroes[j]);
  351. }
  352. }
  353. } // for ( std::map<int, PlayerState>::iterator i=gs->players.begin() ; i!=gs->players.end();i++)
  354. return ret;
  355. }
  356. bool CCallback::isVisible(int3 pos)
  357. {
  358. return isVisible(pos,player);
  359. }
  360. int CCallback::getMyColor()
  361. {
  362. return player;
  363. }
  364. int CCallback::getHeroSerial(const CGHeroInstance * hero)
  365. {
  366. for (int i=0; i<gs->players[player].heroes.size();i++)
  367. {
  368. if (gs->players[player].heroes[i]==hero)
  369. return i;
  370. }
  371. return -1;
  372. }
  373. const CCreatureSet* CCallback::getGarrison(const CGObjectInstance *obj)
  374. {
  375. if(!obj)
  376. return NULL;
  377. if(obj->ID == 34)
  378. return &(dynamic_cast<const CGHeroInstance*>(obj))->army;
  379. else if(obj->ID == 98)
  380. return &(dynamic_cast<const CGTownInstance*>(obj)->army);
  381. else return NULL;
  382. }
  383. int CCallback::swapCreatures(const CGObjectInstance *s1, const CGObjectInstance *s2, int p1, int p2)
  384. {
  385. CCreatureSet *S1 = const_cast<CCreatureSet*>(getGarrison(s1)), *S2 = const_cast<CCreatureSet*>(getGarrison(s2));
  386. if (false)
  387. {
  388. //TODO: check if we are allowed to swap these creatures
  389. return -1;
  390. }
  391. //if(S1->slots[p1].first)
  392. {
  393. //if(s2->slots[p2].first)
  394. {
  395. CCreature * pom = S2->slots[p2].first;
  396. S2->slots[p2].first = S1->slots[p1].first;
  397. S1->slots[p1].first = pom;
  398. int pom2 = S2->slots[p2].second;
  399. S2->slots[p2].second = S1->slots[p1].second;
  400. S1->slots[p1].second = pom2;
  401. if(!S1->slots[p1].first)
  402. S1->slots.erase(p1);
  403. if(!S2->slots[p2].first)
  404. S2->slots.erase(p2);
  405. if(s1->tempOwner<PLAYER_LIMIT)
  406. {
  407. for(int b=0; b<CGI->playerint.size(); ++b)
  408. {
  409. if(CGI->playerint[b]->playerID == s1->tempOwner)
  410. {
  411. CGI->playerint[b]->garrisonChanged(s1);
  412. break;
  413. }
  414. }
  415. }
  416. if((s2->tempOwner<PLAYER_LIMIT) && (s2 != s1))
  417. {
  418. for(int b=0; b<CGI->playerint.size(); ++b)
  419. {
  420. if(CGI->playerint[b]->playerID == s2->tempOwner)
  421. {
  422. CGI->playerint[b]->garrisonChanged(s2);
  423. break;
  424. }
  425. }
  426. }
  427. return 0;
  428. }
  429. }
  430. return -1;
  431. }
  432. int CCallback::mergeStacks(const CGObjectInstance *s1, const CGObjectInstance *s2, int p1, int p2)
  433. {
  434. return -1;
  435. }
  436. int CCallback::splitStack(const CGObjectInstance *s1, const CGObjectInstance *s2, int p1, int p2, int val)
  437. {
  438. return -1;
  439. }
  440. bool CCallback::dismissHero(const CGHeroInstance *hero)
  441. {
  442. CGHeroInstance * Vhero = const_cast<CGHeroInstance *>(hero);
  443. CGI->mh->removeObject(Vhero);
  444. std::vector<CGHeroInstance*>::iterator nitr = find(CGI->state->players[player].heroes.begin(), CGI->state->players[player].heroes.end(), Vhero);
  445. CGI->state->players[player].heroes.erase(nitr);
  446. LOCPLINT->adventureInt->heroList.updateHList();
  447. return false;
  448. }
  449. int CCallback::getMySerial()
  450. {
  451. return gs->players[player].serial;
  452. }
  453. bool CCallback::swapArifacts(const CGHeroInstance * hero1, bool worn1, int pos1, const CGHeroInstance * hero2, bool worn2, int pos2)
  454. {
  455. if(!hero1 || !hero2) //incorrect data
  456. return false;
  457. CGHeroInstance * Uhero1 = const_cast<CGHeroInstance *>(hero1);
  458. CGHeroInstance * Uhero2 = const_cast<CGHeroInstance *>(hero2);
  459. if(worn1 && worn2)
  460. {
  461. std::swap(Uhero1->artifWorn[pos1], Uhero2->artifWorn[pos2]);
  462. }
  463. else if(worn1 && !worn2)
  464. {
  465. std::swap(Uhero1->artifWorn[pos1], Uhero2->artifacts[pos2]);
  466. }
  467. else if(!worn1 && worn2)
  468. {
  469. std::swap(Uhero1->artifacts[pos1], Uhero2->artifWorn[pos2]);
  470. }
  471. else
  472. {
  473. std::swap(Uhero1->artifacts[pos1], Uhero2->artifacts[pos2]);
  474. }
  475. return true;
  476. }
  477. bool CCallback::buildBuilding(const CGTownInstance *town, int buildingID)
  478. {
  479. CGTownInstance * t = const_cast<CGTownInstance *>(town);
  480. CBuilding *b = CGI->buildh->buildings[t->subID][buildingID];
  481. if(0/*not allowed*/)//TODO: check if we are allowed to build
  482. return false;
  483. t->builtBuildings.insert(buildingID);
  484. for(int i=0;i<7;i++)
  485. gs->players[player].resources[i]-=b->resources[i];
  486. t->builded++;
  487. CGI->playerint[CGI->state->players[player].serial]->buildChanged(town,buildingID,1);
  488. return true;
  489. }
  490. int CCallback::battleGetBattlefieldType()
  491. {
  492. return CGI->mh->ttiles[CGI->state->curB->tile.x][CGI->state->curB->tile.y][CGI->state->curB->tile.z].terType;
  493. }
  494. std::map<int, CStack> CCallback::battleGetStacks()
  495. {
  496. std::map<int, CStack> ret;
  497. for(int g=0; g<CGI->state->curB->stacks.size(); ++g)
  498. {
  499. ret[CGI->state->curB->stacks[g]->ID] = *(CGI->state->curB->stacks[g]);
  500. }
  501. return ret;
  502. }
  503. CCreature CCallback::battleGetCreature(int number)
  504. {
  505. for(int h=0; h<CGI->state->curB->stacks.size(); ++h)
  506. {
  507. if(CGI->state->curB->stacks[h]->ID == number) //creature found
  508. return *(CGI->state->curB->stacks[h]->creature);
  509. }
  510. }
  511. bool CCallback::battleMoveCreature(int ID, int dest)
  512. {
  513. //checking parameters
  514. if(dest<0 || dest > 187)
  515. return false;
  516. return CGI->state->battleMoveCreatureStack(ID, dest); //everything finished successfully
  517. }
  518. int3 CScriptCallback::getPos(CGObjectInstance * ob)
  519. {
  520. return ob->pos;
  521. }
  522. void CScriptCallback::changePrimSkill(int ID, int which, int val)
  523. {
  524. CGHeroInstance * hero = CGI->state->getHero(ID,0);
  525. if (which<PRIMARY_SKILLS)
  526. {
  527. hero->primSkills[which]+=val;
  528. for (int i=0; i<CGI->playerint.size(); i++)
  529. {
  530. if (CGI->playerint[i]->playerID == hero->getOwner())
  531. {
  532. CGI->playerint[i]->heroPrimarySkillChanged(hero, which, val);
  533. break;
  534. }
  535. }
  536. }
  537. else if (which==4)
  538. {
  539. hero->exp+=val;
  540. std::cout << "Bohater o ID " << ID <<" (" <<CGI->heroh->heroes[ID]->name <<") dostaje "<<val<<" expa, ale nic z tym nie umiem zrobic :("<<std::endl;
  541. //TODO - powiadomic interfejsy, sprawdzic czy nie ma awansu itp
  542. }
  543. }
  544. int CScriptCallback::getHeroOwner(int heroID)
  545. {
  546. CGHeroInstance * hero = CGI->state->getHero(heroID,0);
  547. return hero->getOwner();
  548. }
  549. void CScriptCallback::showInfoDialog(int player, std::string text, std::vector<SComponent*> * components)
  550. {
  551. //TODO: upewniac sie ze mozemy to zrzutowac (przy customowych interfejsach cos moze sie kopnac)
  552. if (player>=0)
  553. {
  554. CGameInterface * temp = CGI->playerint[CGI->state->players[player].serial];
  555. if (temp->human)
  556. ((CPlayerInterface*)(temp))->showInfoDialog(text,*components);
  557. return;
  558. }
  559. else
  560. {
  561. for (int i=0; i<CGI->playerint.size();i++)
  562. {
  563. if (CGI->playerint[i]->human)
  564. ((CPlayerInterface*)(CGI->playerint[i]))->showInfoDialog(text,*components);
  565. }
  566. }
  567. }
  568. void CScriptCallback::showSelDialog(int player, std::string text, std::vector<CSelectableComponent*>*components, IChosen * asker)
  569. {
  570. CGameInterface * temp = CGI->playerint[CGI->state->players[player].serial];
  571. if (temp->human)
  572. ((CPlayerInterface*)(temp))->showSelDialog(text,*components,(int)asker);
  573. return;
  574. }
  575. int CScriptCallback::getSelectedHero()
  576. {
  577. int ret;
  578. if (LOCPLINT->adventureInt->selection.type == HEROI_TYPE)
  579. ret = ((CGHeroInstance*)(LOCPLINT->adventureInt->selection.selected))->subID;
  580. else
  581. ret = -1;;
  582. return ret;
  583. }
  584. int CScriptCallback::getDate(int mode)
  585. {
  586. int temp;
  587. switch (mode)
  588. {
  589. case 0:
  590. return gs->day;
  591. break;
  592. case 1:
  593. temp = (gs->day)%7;
  594. if (temp)
  595. return temp;
  596. else return 7;
  597. break;
  598. case 2:
  599. temp = ((gs->day-1)/7)+1;
  600. if (!(temp%4))
  601. return 4;
  602. else
  603. return (temp%4);
  604. break;
  605. case 3:
  606. return ((gs->day-1)/28)+1;
  607. break;
  608. }
  609. return 0;
  610. }
  611. void CScriptCallback::giveResource(int player, int which, int val)
  612. {
  613. gs->players[player].resources[which]+=val;
  614. CGI->playerint[gs->players[player].serial]->receivedResource(which,val);
  615. }
  616. void CScriptCallback::showCompInfo(int player, SComponent * comp)
  617. {
  618. CPlayerInterface * i = dynamic_cast<CPlayerInterface*>(CGI->playerint[gs->players[player].serial]);
  619. if(i)
  620. i->showComp(*comp);
  621. }
  622. void CScriptCallback::heroVisitCastle(CGObjectInstance * ob, int heroID)
  623. {
  624. CGTownInstance * n;
  625. if(n = dynamic_cast<CGTownInstance*>(ob))
  626. {
  627. n->visitingHero = CGI->state->getHero(heroID,0);
  628. for(int b=0; b<CGI->playerint.size(); ++b)
  629. {
  630. if(CGI->playerint[b]->playerID == getHeroOwner(heroID))
  631. {
  632. CGI->playerint[b]->heroVisitsTown(CGI->state->getHero(heroID,0),n);
  633. break;
  634. }
  635. }
  636. }
  637. else
  638. return;
  639. }
  640. void CScriptCallback::stopHeroVisitCastle(CGObjectInstance * ob, int heroID)
  641. {
  642. CGTownInstance * n;
  643. if(n = dynamic_cast<CGTownInstance*>(ob))
  644. {
  645. if(n->visitingHero && n->visitingHero->type->ID == heroID)
  646. n->visitingHero = NULL;
  647. return;
  648. }
  649. else
  650. return;
  651. }
  652. void CScriptCallback::giveHeroArtifact(int artid, int hid, int position) //pos==-1 - first free slot in backpack
  653. {
  654. CGHeroInstance* h = gs->getHero(hid,0);
  655. if(position<0)
  656. {
  657. for(int i=0;i<h->artifacts.size();i++)
  658. {
  659. if(!h->artifacts[i])
  660. {
  661. h->artifacts[i] = &CGI->arth->artifacts[artid];
  662. return;
  663. }
  664. }
  665. h->artifacts.push_back(&CGI->arth->artifacts[artid]);
  666. return;
  667. }
  668. else
  669. {
  670. if(h->artifWorn[position]) //slot is occupied
  671. {
  672. giveHeroArtifact(h->artifWorn[position]->id,hid,-1);
  673. }
  674. h->artifWorn[position] = &CGI->arth->artifacts[artid];
  675. }
  676. }
  677. void CScriptCallback::startBattle(CCreatureSet * army1, CCreatureSet * army2, int3 tile, CGHeroInstance *hero1, CGHeroInstance *hero2) //use hero=NULL for no hero
  678. {
  679. gs->battle(army1,army2,tile,hero1,hero2);
  680. }
  681. void CScriptCallback::startBattle(int heroID, CCreatureSet * army, int3 tile) //for hero<=>neutral army
  682. {
  683. CGHeroInstance* h = gs->getHero(heroID,0);
  684. gs->battle(&h->army,army,tile,h,NULL);
  685. }
  686. void CLuaCallback::registerFuncs(lua_State * L)
  687. {
  688. lua_newtable(L);
  689. #define REGISTER_C_FUNC(x) \
  690. lua_pushstring(L, #x); \
  691. lua_pushcfunction(L, x); \
  692. lua_rawset(L, -3)
  693. REGISTER_C_FUNC(getPos);
  694. REGISTER_C_FUNC(changePrimSkill);
  695. REGISTER_C_FUNC(getGnrlText);
  696. REGISTER_C_FUNC(getSelectedHero);
  697. /*
  698. REGISTER_C_FUNC(changePrimSkill);
  699. REGISTER_C_FUNC(getGnrlText);
  700. REGISTER_C_FUNC(changePrimSkill);
  701. REGISTER_C_FUNC(getGnrlText);
  702. REGISTER_C_FUNC(changePrimSkill);
  703. REGISTER_C_FUNC(getGnrlText);*/
  704. lua_setglobal(L, "vcmi");
  705. #undef REGISTER_C_FUNC
  706. }
  707. int CLuaCallback::getPos(lua_State * L)//(CGObjectInstance * object);
  708. {
  709. const int args = lua_gettop(L); // number of arguments
  710. if ((args < 1) || !lua_isnumber(L, 1) )
  711. luaL_error(L,
  712. "Incorrect arguments to getPos([Object address])");
  713. CGObjectInstance * object = (CGObjectInstance *)(lua_tointeger(L, 1));
  714. lua_pushinteger(L,object->pos.x);
  715. lua_pushinteger(L,object->pos.y);
  716. lua_pushinteger(L,object->pos.z);
  717. return 3;
  718. }
  719. int CLuaCallback::changePrimSkill(lua_State * L)//(int ID, int which, int val);
  720. {
  721. const int args = lua_gettop(L); // number of arguments
  722. if ((args < 1) || !lua_isnumber(L, 1) ||
  723. ((args >= 2) && !lua_isnumber(L, 2)) ||
  724. ((args >= 3) && !lua_isnumber(L, 3)) )
  725. {
  726. luaL_error(L,
  727. "Incorrect arguments to changePrimSkill([Hero ID], [Which Primary skill], [Change by])");
  728. }
  729. int ID = lua_tointeger(L, 1),
  730. which = lua_tointeger(L, 2),
  731. val = lua_tointeger(L, 3);
  732. CScriptCallback::changePrimSkill(ID,which,val);
  733. return 0;
  734. }
  735. int CLuaCallback::getGnrlText(lua_State * L) //(int which),returns string
  736. {
  737. const int args = lua_gettop(L); // number of arguments
  738. if ((args < 1) || !lua_isnumber(L, 1) )
  739. luaL_error(L,
  740. "Incorrect arguments to getGnrlText([Text ID])");
  741. int which = lua_tointeger(L,1);
  742. lua_pushstring(L,CGI->generaltexth->allTexts[which].c_str());
  743. return 1;
  744. }
  745. int CLuaCallback::getSelectedHero(lua_State * L) //(),returns int (ID of hero, -1 if no hero is seleceted)
  746. {
  747. int ret;
  748. if (LOCPLINT->adventureInt->selection.type == HEROI_TYPE)
  749. ret = ((CGHeroInstance*)(LOCPLINT->adventureInt->selection.selected))->subID;
  750. else
  751. ret = -1;
  752. lua_pushinteger(L,ret);
  753. return 1;
  754. }