CCallback.cpp 24 KB

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