CCallback.cpp 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095
  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. if(i->second.towns[j]->creatureDwelling(k))//there is dwelling
  60. i->second.towns[j]->strInfo.creatures[k]+=i->second.towns[j]->creatureGrowth(k);
  61. }
  62. }
  63. if((gs->day>1) && i->first<PLAYER_LIMIT)
  64. i->second.resources[6]+=i->second.towns[j]->dailyIncome();
  65. }
  66. }
  67. }
  68. bool CCallback::moveHero(int ID, CPath * path, int idtype, int pathType)
  69. {
  70. CGHeroInstance * hero = NULL;
  71. if (idtype==0)
  72. {
  73. if (player==-1)
  74. hero=gs->players[player+1].heroes[ID];
  75. else
  76. hero=gs->players[player].heroes[ID];
  77. }
  78. else if (idtype==1 && player>=0) //looking for it in local area
  79. {
  80. for (int i=0; i<gs->players[player].heroes.size();i++)
  81. {
  82. if (gs->players[player].heroes[i]->type->ID == ID)
  83. hero = gs->players[player].heroes[i];
  84. }
  85. }
  86. else //idtype==1; player<0
  87. {
  88. for(std::map<int, PlayerState>::iterator j=CGI->state->players.begin(); j!=CGI->state->players.end(); ++j)
  89. {
  90. for (int i=0; i<(*j).second.heroes.size();i++)
  91. {
  92. if ((*j).second.heroes[i]->type->ID == ID)
  93. {
  94. hero = (*j).second.heroes[i];
  95. }
  96. }
  97. }
  98. }
  99. if (!hero)
  100. return false; //can't find hero
  101. if(!verifyPath(path,!hero->canWalkOnSea()))//TODO: not check sea, if hero has flying or walking on water
  102. return false; //invalid path
  103. //check path format
  104. if (pathType==0)
  105. CPathfinder::convertPath(path,pathType);
  106. if (pathType>1)
  107. throw std::exception("Unknown path format");
  108. CPath * ourPath = path;
  109. if(!ourPath)
  110. return false;
  111. for(int i=ourPath->nodes.size()-1; i>0; i--)
  112. {
  113. int3 stpos, endpos;
  114. stpos = int3(ourPath->nodes[i].coord.x, ourPath->nodes[i].coord.y, hero->pos.z);
  115. endpos = int3(ourPath->nodes[i-1].coord.x, ourPath->nodes[i-1].coord.y, hero->pos.z);
  116. HeroMoveDetails curd;
  117. curd.src = stpos;
  118. curd.dst = endpos;
  119. curd.ho = hero;
  120. curd.owner = hero->getOwner();
  121. /*if(player!=-1)
  122. {
  123. hero->pos = endpos;
  124. }*/
  125. if((hero->movement>=CGI->mh->getCost(int3(stpos.x-1, stpos.y, stpos.z), int3(endpos.x-1, endpos.y, endpos.z), hero)) || player==-1)
  126. { //performing move
  127. hero->movement-=CGI->mh->getCost(int3(stpos.x-1, stpos.y, stpos.z), int3(endpos.x-1, endpos.y, endpos.z), hero);
  128. std::vector< CGObjectInstance * > vis = CGI->mh->getVisitableObjs(CGHeroInstance::convertPosition(curd.dst,false));
  129. bool blockvis = false;
  130. for (int pit = 0; pit<vis.size();pit++)
  131. if (vis[pit]->blockVisit)
  132. blockvis = true;
  133. if (!blockvis)
  134. {
  135. curd.successful = true;
  136. hero->pos = curd.dst;
  137. //inform leaved objects
  138. std::vector< CGObjectInstance * > leave = CGI->mh->getVisitableObjs(CGHeroInstance::convertPosition(curd.src,false));
  139. for (int iii=0; iii<leave.size(); iii++) //if object is visitable we call onHeroVisit
  140. {
  141. //TODO: allow to handle this in LUA
  142. if(leave[iii]->state) //hard-coded function
  143. leave[iii]->state->onHeroLeave(leave[iii],curd.ho->subID);
  144. }
  145. //reveal fog of war
  146. int heroSight = hero->getSightDistance();
  147. int xbeg = stpos.x - heroSight - 2;
  148. if(xbeg < 0)
  149. xbeg = 0;
  150. int xend = stpos.x + heroSight + 2;
  151. if(xend >= CGI->ac->map.width)
  152. xend = CGI->ac->map.width;
  153. int ybeg = stpos.y - heroSight - 2;
  154. if(ybeg < 0)
  155. ybeg = 0;
  156. int yend = stpos.y + heroSight + 2;
  157. if(yend >= CGI->ac->map.height)
  158. yend = CGI->ac->map.height;
  159. for(int xd=xbeg; xd<xend; ++xd) //revealing part of map around heroes
  160. {
  161. for(int yd=ybeg; yd<yend; ++yd)
  162. {
  163. int deltaX = (hero->getPosition(false).x-xd)*(hero->getPosition(false).x-xd);
  164. int deltaY = (hero->getPosition(false).y-yd)*(hero->getPosition(false).y-yd);
  165. if(deltaX+deltaY<hero->getSightDistance()*hero->getSightDistance())
  166. {
  167. if(gs->players[player].fogOfWarMap[xd][yd][hero->getPosition(false).z] == 0)
  168. {
  169. CGI->playerint[gs->players[player].serial]->tileRevealed(int3(xd, yd, hero->getPosition(false).z));
  170. }
  171. gs->players[player].fogOfWarMap[xd][yd][hero->getPosition(false).z] = 1;
  172. }
  173. }
  174. }
  175. //notify interfacesabout move
  176. int nn=0; //number of interfece of currently browsed player
  177. for(std::map<int, PlayerState>::iterator j=CGI->state->players.begin(); j!=CGI->state->players.end(); ++j)//CGI->state->players.size(); ++j) //for testing
  178. {
  179. if (j->first > PLAYER_LIMIT)
  180. break;
  181. if(j->second.fogOfWarMap[stpos.x-1][stpos.y][stpos.z] || j->second.fogOfWarMap[endpos.x-1][endpos.y][endpos.z])
  182. { //player should be notified
  183. CGI->playerint[j->second.serial]->heroMoved(curd);
  184. }
  185. ++nn;
  186. }
  187. //call objects if they arevisited
  188. for (int iii=0; iii<vis.size(); iii++) //if object is visitable we call onHeroVisit
  189. {
  190. if(gs->checkFunc(vis[iii]->ID,"heroVisit")) //script function
  191. gs->objscr[vis[iii]->ID]["heroVisit"]->onHeroVisit(vis[iii],curd.ho->subID);
  192. if(vis[iii]->state) //hard-coded function
  193. vis[iii]->state->onHeroVisit(vis[iii],curd.ho->subID);
  194. }
  195. }
  196. else //interaction with blocking object (like resources)
  197. {
  198. curd.successful = false;
  199. CGI->playerint[gs->players[hero->getOwner()].serial]->heroMoved(curd);
  200. for (int iii=0; iii<vis.size(); iii++) //if object is visitable we call onHeroVisit
  201. {
  202. if (vis[iii]->blockVisit)
  203. {
  204. if(gs->checkFunc(vis[iii]->ID,"heroVisit")) //script function
  205. gs->objscr[vis[iii]->ID]["heroVisit"]->onHeroVisit(vis[iii],curd.ho->subID);
  206. if(vis[iii]->state) //hard-coded function
  207. vis[iii]->state->onHeroVisit(vis[iii],curd.ho->subID);
  208. }
  209. }
  210. return false;
  211. }
  212. }
  213. else
  214. return true; //move ended - no more movement points
  215. }
  216. return true;
  217. }
  218. void CCallback::selectionMade(int selection, int asker)
  219. {
  220. //todo - jak bedzie multiplayer po sieci, to moze wymagac przerobek zaleznych od obranego modelu
  221. IChosen * ask = (IChosen *)asker;
  222. ask->chosen(selection);
  223. }
  224. void CCallback::recruitCreatures(const CGObjectInstance *obj, int ID, int amount)
  225. {
  226. if(amount<=0) return;
  227. if(obj->ID==98) //recruiting from town
  228. {
  229. int ser=-1; //used dwelling level
  230. CGTownInstance *t = const_cast<CGTownInstance*>(static_cast<const CGTownInstance*>(obj));
  231. //verify
  232. bool found = false;
  233. typedef std::pair<const int,int> Parka;
  234. for(std::map<int,int>::iterator av=t->strInfo.creatures.begin();av!=t->strInfo.creatures.end();av++)
  235. {
  236. if( ( found = (ID == t->town->basicCreatures[av->first]) ) //creature is available among basic cretures
  237. || (found = (ID == t->town->upgradedCreatures[av->first])) )//creature is available among upgraded cretures
  238. {
  239. amount = std::min(amount,av->second); //reduce recruited amount up to available amount
  240. ser = av->first;
  241. break;
  242. }
  243. }
  244. if(!found) //no such creature
  245. return;
  246. if(amount > CGI->creh->creatures[ID].maxAmount(gs->players[player].resources))
  247. return; //not enough resources
  248. //for(int i=0;i<RESOURCE_QUANTITY;i++)
  249. // if (gs->players[player].resources[i] < (CGI->creh->creatures[ID].cost[i] * amount))
  250. // return; //not enough resources
  251. if(amount<=0) return;
  252. //recruit
  253. int slot = -1; //slot ID
  254. std::pair<int,std::pair<CCreature*,int> > parb;
  255. for(int i=0;i<7;i++) //TODO: if there is already stack of same creatures it should be used always
  256. {
  257. if((!t->army.slots[i].first) || (t->army.slots[i].first->idNumber == ID)) //slot is free or there is saem creature
  258. {
  259. slot = i;
  260. break;
  261. }
  262. }
  263. if(slot<0) //no free slot
  264. return;
  265. for(int i=0;i<RESOURCE_QUANTITY;i++)
  266. gs->players[player].resources[i] -= (CGI->creh->creatures[ID].cost[i] * amount);
  267. t->strInfo.creatures[ser] -= amount;
  268. if(t->army.slots[slot].first) //add new creatures to the existing stack
  269. {
  270. t->army.slots[slot].second += amount;
  271. }
  272. else //create new stack in the garrison
  273. {
  274. t->army.slots[slot].first = &CGI->creh->creatures[ID];
  275. t->army.slots[slot].second = amount;
  276. }
  277. CGI->playerint[gs->players[player].serial]->garrisonChanged(obj);
  278. }
  279. //TODO: recruit from dwellings on the adventure map
  280. }
  281. bool CCallback::dismissCreature(const CArmedInstance *obj, int stackPos)
  282. {
  283. if((player>=0) && obj->tempOwner != player)
  284. return false;
  285. CArmedInstance *ob = const_cast<CArmedInstance*>(obj);
  286. ob->army.slots.erase(stackPos);
  287. CGI->playerint[gs->players[player].serial]->garrisonChanged(obj);
  288. return true;
  289. }
  290. bool CCallback::upgradeCreature(const CArmedInstance *obj, int stackPos, int newID)
  291. {
  292. //TODO: write
  293. return false;
  294. }
  295. UpgradeInfo CCallback::getUpgradeInfo(const CArmedInstance *obj, int stackPos)
  296. {
  297. UpgradeInfo ret;
  298. CCreature *base = ((CArmedInstance*)obj)->army.slots[stackPos].first;
  299. if((obj->ID == 98) || ((obj->ID == 34) && static_cast<const CGHeroInstance*>(obj)->visitedTown))
  300. {
  301. CGTownInstance * t;
  302. if(obj->ID == 98)
  303. t = static_cast<CGTownInstance *>(const_cast<CArmedInstance *>(obj));
  304. else
  305. t = static_cast<const CGHeroInstance*>(obj)->visitedTown;
  306. for(std::set<int>::iterator i=t->builtBuildings.begin(); i!=t->builtBuildings.end(); i++)
  307. {
  308. if( (*i) >= 37 && (*i) < 44 ) //upgraded creature dwelling
  309. {
  310. int nid = t->town->upgradedCreatures[(*i)-37]; //upgrade offered by that building
  311. if(base->upgrades.find(nid) != base->upgrades.end()) //possible upgrade
  312. {
  313. ret.newID.push_back(nid);
  314. ret.cost.push_back(std::set<std::pair<int,int> >());
  315. for(int j=0;j<RESOURCE_QUANTITY;j++)
  316. {
  317. int dif = CGI->creh->creatures[nid].cost[j] - base->cost[j];
  318. if(dif)
  319. ret.cost[ret.cost.size()-1].insert(std::make_pair(j,dif));
  320. }
  321. }
  322. }
  323. }//end for
  324. }
  325. //TODO: check if hero ability makes some upgrades possible
  326. if(ret.newID.size())
  327. ret.oldID = base->idNumber;
  328. return ret;
  329. }
  330. int CCallback::howManyTowns()
  331. {
  332. return gs->players[gs->currentPlayer].towns.size();
  333. }
  334. const CGTownInstance * CCallback::getTownInfo(int val, bool mode) //mode = 0 -> val = serial; mode = 1 -> val = ID
  335. {
  336. if (!mode)
  337. return gs->players[gs->currentPlayer].towns[val];
  338. else
  339. {
  340. //TODO: add some smart ID to the CTownInstance
  341. //for (int i=0; i<gs->players[gs->currentPlayer].towns.size();i++)
  342. //{
  343. // if (gs->players[gs->currentPlayer].towns[i]->someID==val)
  344. // return gs->players[gs->currentPlayer].towns[i];
  345. //}
  346. return NULL;
  347. }
  348. return NULL;
  349. }
  350. int CCallback::howManyHeroes()
  351. {
  352. return gs->players[player].heroes.size();
  353. }
  354. const CGHeroInstance * CCallback::getHeroInfo(int player, int val, bool mode) //mode = 0 -> val = serial; mode = 1 -> val = ID
  355. {
  356. if (gs->currentPlayer!=player) //TODO: checking if we are allowed to give that info
  357. return NULL;
  358. if (!mode)
  359. if(val<gs->players[player].heroes.size())
  360. return gs->players[player].heroes[val];
  361. else return NULL;
  362. else
  363. {
  364. for (int i=0; i<gs->players[player].heroes.size();i++)
  365. {
  366. if (gs->players[player].heroes[i]->type->ID==val)
  367. return gs->players[player].heroes[i];
  368. }
  369. }
  370. return NULL;
  371. }
  372. int CCallback::getResourceAmount(int type)
  373. {
  374. return gs->players[player].resources[type];
  375. }
  376. std::vector<int> CCallback::getResourceAmount()
  377. {
  378. return gs->players[player].resources;
  379. }
  380. int CCallback::getDate(int mode)
  381. {
  382. int temp;
  383. switch (mode)
  384. {
  385. case 0:
  386. return gs->day;
  387. break;
  388. case 1:
  389. temp = (gs->day)%7;
  390. if (temp)
  391. return temp;
  392. else return 7;
  393. break;
  394. case 2:
  395. temp = ((gs->day-1)/7)+1;
  396. if (!(temp%4))
  397. return 4;
  398. else
  399. return (temp%4);
  400. break;
  401. case 3:
  402. return ((gs->day-1)/28)+1;
  403. break;
  404. }
  405. return 0;
  406. }
  407. bool CCallback::verifyPath(CPath * path, bool blockSea)
  408. {
  409. for (int i=0;i<path->nodes.size();i++)
  410. {
  411. if ( CGI->mh->ttiles[path->nodes[i].coord.x][path->nodes[i].coord.y][path->nodes[i].coord.z].blocked
  412. && (! (CGI->mh->ttiles[path->nodes[i].coord.x][path->nodes[i].coord.y][path->nodes[i].coord.z].visitable)))
  413. return false; //path is wrong - one of the tiles is blocked
  414. if (blockSea)
  415. {
  416. if (i==0)
  417. continue;
  418. if (
  419. ((CGI->mh->ttiles[path->nodes[i].coord.x][path->nodes[i].coord.y][path->nodes[i].coord.z].terType==EterrainType::water)
  420. &&
  421. (CGI->mh->ttiles[path->nodes[i-1].coord.x][path->nodes[i-1].coord.y][path->nodes[i-1].coord.z].terType!=EterrainType::water))
  422. ||
  423. ((CGI->mh->ttiles[path->nodes[i].coord.x][path->nodes[i].coord.y][path->nodes[i].coord.z].terType!=EterrainType::water)
  424. &&
  425. (CGI->mh->ttiles[path->nodes[i-1].coord.x][path->nodes[i-1].coord.y][path->nodes[i-1].coord.z].terType==EterrainType::water))
  426. ||
  427. (CGI->mh->ttiles[path->nodes[i-1].coord.x][path->nodes[i-1].coord.y][path->nodes[i-1].coord.z].terType==EterrainType::rock)
  428. )
  429. return false;
  430. }
  431. }
  432. return true;
  433. }
  434. std::vector < std::string > CCallback::getObjDescriptions(int3 pos)
  435. {
  436. if(gs->players[player].fogOfWarMap[pos.x][pos.y][pos.z])
  437. return CGI->mh->getObjDescriptions(pos);
  438. else return std::vector< std::string > ();
  439. }
  440. PseudoV< PseudoV< PseudoV<unsigned char> > > & CCallback::getVisibilityMap()
  441. {
  442. return gs->players[player].fogOfWarMap;
  443. }
  444. bool CCallback::isVisible(int3 pos, int Player)
  445. {
  446. return gs->players[Player].fogOfWarMap[pos.x][pos.y][pos.z];
  447. }
  448. std::vector < const CGTownInstance *> CCallback::getTownsInfo(bool onlyOur)
  449. {
  450. std::vector < const CGTownInstance *> ret = std::vector < const CGTownInstance *>();
  451. for ( std::map<int, PlayerState>::iterator i=gs->players.begin() ; i!=gs->players.end();i++)
  452. {
  453. for (int j=0;j<(*i).second.towns.size();j++)
  454. {
  455. if ( ( isVisible((*i).second.towns[j]->pos,player) ) || (*i).first==player)
  456. {
  457. ret.push_back((*i).second.towns[j]);
  458. }
  459. }
  460. } // for ( std::map<int, PlayerState>::iterator i=gs->players.begin() ; i!=gs->players.end();i++)
  461. return ret;
  462. }
  463. std::vector < const CGHeroInstance *> CCallback::getHeroesInfo(bool onlyOur)
  464. {
  465. std::vector < const CGHeroInstance *> ret = std::vector < const CGHeroInstance *>();
  466. for ( std::map<int, PlayerState>::iterator i=gs->players.begin() ; i!=gs->players.end();i++)
  467. {
  468. for (int j=0;j<(*i).second.heroes.size();j++)
  469. {
  470. if ( ( isVisible((*i).second.heroes[j]->getPosition(false),player) ) || (*i).first==player)
  471. {
  472. ret.push_back((*i).second.heroes[j]);
  473. }
  474. }
  475. } // for ( std::map<int, PlayerState>::iterator i=gs->players.begin() ; i!=gs->players.end();i++)
  476. return ret;
  477. }
  478. bool CCallback::isVisible(int3 pos)
  479. {
  480. return isVisible(pos,player);
  481. }
  482. int CCallback::getMyColor()
  483. {
  484. return player;
  485. }
  486. int CCallback::getHeroSerial(const CGHeroInstance * hero)
  487. {
  488. for (int i=0; i<gs->players[player].heroes.size();i++)
  489. {
  490. if (gs->players[player].heroes[i]==hero)
  491. return i;
  492. }
  493. return -1;
  494. }
  495. const CCreatureSet* CCallback::getGarrison(const CGObjectInstance *obj)
  496. {
  497. if(!obj)
  498. return NULL;
  499. if(obj->ID == 34)
  500. return &(dynamic_cast<const CGHeroInstance*>(obj))->army;
  501. else if(obj->ID == 98)
  502. return &(dynamic_cast<const CGTownInstance*>(obj)->army);
  503. else return NULL;
  504. }
  505. int CCallback::swapCreatures(const CGObjectInstance *s1, const CGObjectInstance *s2, int p1, int p2)
  506. {
  507. CCreatureSet *S1 = const_cast<CCreatureSet*>(getGarrison(s1)), *S2 = const_cast<CCreatureSet*>(getGarrison(s2));
  508. if (false)
  509. {
  510. //TODO: check if we are allowed to swap these creatures
  511. return -1;
  512. }
  513. CCreature * pom = S2->slots[p2].first;
  514. S2->slots[p2].first = S1->slots[p1].first;
  515. S1->slots[p1].first = pom;
  516. int pom2 = S2->slots[p2].second;
  517. S2->slots[p2].second = S1->slots[p1].second;
  518. S1->slots[p1].second = pom2;
  519. if(!S1->slots[p1].first)
  520. S1->slots.erase(p1);
  521. if(!S2->slots[p2].first)
  522. S2->slots.erase(p2);
  523. if(s1->tempOwner<PLAYER_LIMIT)
  524. {
  525. for(int b=0; b<CGI->playerint.size(); ++b)
  526. {
  527. if(CGI->playerint[b]->playerID == s1->tempOwner)
  528. {
  529. CGI->playerint[b]->garrisonChanged(s1);
  530. break;
  531. }
  532. }
  533. }
  534. if((s2->tempOwner<PLAYER_LIMIT) && (s2 != s1))
  535. {
  536. for(int b=0; b<CGI->playerint.size(); ++b)
  537. {
  538. if(CGI->playerint[b]->playerID == s2->tempOwner)
  539. {
  540. CGI->playerint[b]->garrisonChanged(s2);
  541. break;
  542. }
  543. }
  544. }
  545. return 0;
  546. }
  547. int CCallback::mergeStacks(const CGObjectInstance *s1, const CGObjectInstance *s2, int p1, int p2)
  548. {
  549. CCreatureSet *S1 = const_cast<CCreatureSet*>(getGarrison(s1)), *S2 = const_cast<CCreatureSet*>(getGarrison(s2));
  550. if ((S1->slots[p1].first != S2->slots[p2].first) && (true /*we are allowed to*/))
  551. {
  552. return -1;
  553. }
  554. S2->slots[p2].second += S1->slots[p1].second;
  555. S1->slots[p1].first = NULL;
  556. S1->slots[p1].second = 0;
  557. S1->slots.erase(p1);
  558. if(s1->tempOwner<PLAYER_LIMIT)
  559. {
  560. for(int b=0; b<CGI->playerint.size(); ++b)
  561. {
  562. if(CGI->playerint[b]->playerID == s1->tempOwner)
  563. {
  564. CGI->playerint[b]->garrisonChanged(s1);
  565. break;
  566. }
  567. }
  568. }
  569. if((s2->tempOwner<PLAYER_LIMIT) && (s2 != s1))
  570. {
  571. for(int b=0; b<CGI->playerint.size(); ++b)
  572. {
  573. if(CGI->playerint[b]->playerID == s2->tempOwner)
  574. {
  575. CGI->playerint[b]->garrisonChanged(s2);
  576. break;
  577. }
  578. }
  579. }
  580. return 0;
  581. }
  582. int CCallback::splitStack(const CGObjectInstance *s1, const CGObjectInstance *s2, int p1, int p2, int val)
  583. {
  584. if(!val)
  585. return -1;
  586. CCreatureSet *S1 = const_cast<CCreatureSet*>(getGarrison(s1)), *S2 = const_cast<CCreatureSet*>(getGarrison(s2));
  587. if ((S1->slots[p1].second<val) && (true /*we are allowed to*/))
  588. {
  589. return -1;
  590. }
  591. S2->slots[p2].first = S1->slots[p1].first;
  592. S2->slots[p2].second = val;
  593. S1->slots[p1].second -= val;
  594. if(!S1->slots[p1].second) //if we've moved all creatures
  595. S1->slots.erase(p1);
  596. if(s1->tempOwner<PLAYER_LIMIT)
  597. {
  598. for(int b=0; b<CGI->playerint.size(); ++b)
  599. {
  600. if(CGI->playerint[b]->playerID == s1->tempOwner)
  601. {
  602. CGI->playerint[b]->garrisonChanged(s1);
  603. break;
  604. }
  605. }
  606. }
  607. if((s2->tempOwner<PLAYER_LIMIT) && (s2 != s1))
  608. {
  609. for(int b=0; b<CGI->playerint.size(); ++b)
  610. {
  611. if(CGI->playerint[b]->playerID == s2->tempOwner)
  612. {
  613. CGI->playerint[b]->garrisonChanged(s2);
  614. break;
  615. }
  616. }
  617. }
  618. return 0;
  619. }
  620. bool CCallback::dismissHero(const CGHeroInstance *hero)
  621. {
  622. CGHeroInstance * Vhero = const_cast<CGHeroInstance *>(hero);
  623. CGI->mh->removeObject(Vhero);
  624. std::vector<CGHeroInstance*>::iterator nitr = find(CGI->state->players[player].heroes.begin(), CGI->state->players[player].heroes.end(), Vhero);
  625. CGI->state->players[player].heroes.erase(nitr);
  626. LOCPLINT->adventureInt->heroList.updateHList();
  627. return false;
  628. }
  629. int CCallback::getMySerial()
  630. {
  631. return gs->players[player].serial;
  632. }
  633. bool CCallback::swapArifacts(const CGHeroInstance * hero1, bool worn1, int pos1, const CGHeroInstance * hero2, bool worn2, int pos2)
  634. {
  635. if(!hero1 || !hero2) //incorrect data
  636. return false;
  637. CGHeroInstance * Uhero1 = const_cast<CGHeroInstance *>(hero1);
  638. CGHeroInstance * Uhero2 = const_cast<CGHeroInstance *>(hero2);
  639. if(worn1 && worn2)
  640. {
  641. std::swap(Uhero1->artifWorn[pos1], Uhero2->artifWorn[pos2]);
  642. }
  643. else if(worn1 && !worn2)
  644. {
  645. std::swap(Uhero1->artifWorn[pos1], Uhero2->artifacts[pos2]);
  646. }
  647. else if(!worn1 && worn2)
  648. {
  649. std::swap(Uhero1->artifacts[pos1], Uhero2->artifWorn[pos2]);
  650. }
  651. else
  652. {
  653. std::swap(Uhero1->artifacts[pos1], Uhero2->artifacts[pos2]);
  654. }
  655. return true;
  656. }
  657. bool CCallback::buildBuilding(const CGTownInstance *town, int buildingID)
  658. {
  659. CGTownInstance * t = const_cast<CGTownInstance *>(town);
  660. CBuilding *b = CGI->buildh->buildings[t->subID][buildingID];
  661. if(0/*not allowed*/)//TODO: check if we are allowed to build
  662. return false;
  663. if(buildingID>36) //upg dwelling
  664. {
  665. if(t->getHordeLevel(0) == (buildingID-37))
  666. t->builtBuildings.insert(19);
  667. else if(t->getHordeLevel(1) == (buildingID-37))
  668. t->builtBuildings.insert(25);
  669. }
  670. else if(buildingID >= 30) //bas. dwelling
  671. {
  672. t->strInfo.creatures[buildingID-30] = CGI->creh->creatures[t->town->basicCreatures[buildingID-30]].growth;
  673. }
  674. t->builtBuildings.insert(buildingID);
  675. for(int i=0;i<7;i++)
  676. gs->players[player].resources[i]-=b->resources[i];
  677. t->builded++;
  678. CGI->playerint[CGI->state->players[player].serial]->buildChanged(town,buildingID,1);
  679. return true;
  680. }
  681. int CCallback::battleGetBattlefieldType()
  682. {
  683. return CGI->mh->ttiles[CGI->state->curB->tile.x][CGI->state->curB->tile.y][CGI->state->curB->tile.z].terType;
  684. }
  685. int CCallback::battleGetObstaclesAtTile(int tile) //returns bitfield
  686. {
  687. //TODO - write
  688. return -1;
  689. }
  690. int CCallback::battleGetStack(int pos)
  691. {
  692. for(int g=0; g<CGI->state->curB->stacks.size(); ++g)
  693. {
  694. if(CGI->state->curB->stacks[g]->position == pos)
  695. return CGI->state->curB->stacks[g]->ID;
  696. }
  697. return -1;
  698. }
  699. CStack CCallback::battleGetStackByID(int ID)
  700. {
  701. for(int g=0; g<CGI->state->curB->stacks.size(); ++g)
  702. {
  703. if(CGI->state->curB->stacks[g]->ID == ID)
  704. return *(CGI->state->curB->stacks[g]);
  705. }
  706. return CStack();
  707. }
  708. int CCallback::battleGetPos(int stack)
  709. {
  710. for(int g=0; g<CGI->state->curB->stacks.size(); ++g)
  711. {
  712. if(CGI->state->curB->stacks[g]->ID == stack)
  713. return CGI->state->curB->stacks[g]->position;
  714. }
  715. return -1;
  716. }
  717. std::map<int, CStack> CCallback::battleGetStacks()
  718. {
  719. std::map<int, CStack> ret;
  720. for(int g=0; g<CGI->state->curB->stacks.size(); ++g)
  721. {
  722. ret[CGI->state->curB->stacks[g]->ID] = *(CGI->state->curB->stacks[g]);
  723. }
  724. return ret;
  725. }
  726. CCreature CCallback::battleGetCreature(int number)
  727. {
  728. for(int h=0; h<CGI->state->curB->stacks.size(); ++h)
  729. {
  730. if(CGI->state->curB->stacks[h]->ID == number) //creature found
  731. return *(CGI->state->curB->stacks[h]->creature);
  732. }
  733. throw new std::exception("Cannot find the creature");
  734. }
  735. std::vector<int> CCallback::battleGetAvailableHexes(int ID)
  736. {
  737. return CGI->state->battleGetRange(ID);
  738. }
  739. bool CCallback::battleIsStackMine(int ID)
  740. {
  741. for(int h=0; h<CGI->state->curB->stacks.size(); ++h)
  742. {
  743. if(CGI->state->curB->stacks[h]->ID == ID) //creature found
  744. return CGI->state->curB->stacks[h]->owner == player;
  745. }
  746. return false;
  747. }
  748. int3 CScriptCallback::getPos(CGObjectInstance * ob)
  749. {
  750. return ob->pos;
  751. }
  752. void CScriptCallback::changePrimSkill(int ID, int which, int val)
  753. {
  754. CGHeroInstance * hero = CGI->state->getHero(ID,0);
  755. if (which<PRIMARY_SKILLS)
  756. {
  757. hero->primSkills[which]+=val;
  758. for (int i=0; i<CGI->playerint.size(); i++)
  759. {
  760. if (CGI->playerint[i]->playerID == hero->getOwner())
  761. {
  762. CGI->playerint[i]->heroPrimarySkillChanged(hero, which, val);
  763. break;
  764. }
  765. }
  766. }
  767. else if (which==4)
  768. {
  769. hero->exp+=val;
  770. if(hero->exp >= CGI->heroh->reqExp(hero->level+1)) //new level
  771. {
  772. hero->level++;
  773. std::cout << hero->name <<" got level "<<hero->level<<std::endl;
  774. int r = rand()%100, pom=0, x=0;
  775. int std::pair<int,int>::*g = (hero->level>9) ? (&std::pair<int,int>::second) : (&std::pair<int,int>::first);
  776. for(;x<PRIMARY_SKILLS;x++)
  777. {
  778. pom += hero->type->heroClass->primChance[x].*g;
  779. if(r<pom)
  780. break;
  781. }
  782. std::cout << "Bohater dostaje umiejetnosc pierwszorzedna " << x << " (wynik losowania "<<r<<")"<<std::endl;
  783. hero->primSkills[x]++;
  784. //TODO: dac dwie umiejetnosci 2-rzedne to wyboru
  785. }
  786. //TODO - powiadomic interfejsy, sprawdzic czy nie ma awansu itp
  787. }
  788. }
  789. int CScriptCallback::getHeroOwner(int heroID)
  790. {
  791. CGHeroInstance * hero = CGI->state->getHero(heroID,0);
  792. return hero->getOwner();
  793. }
  794. void CScriptCallback::showInfoDialog(int player, std::string text, std::vector<SComponent*> * components)
  795. {
  796. //TODO: upewniac sie ze mozemy to zrzutowac (przy customowych interfejsach cos moze sie kopnac)
  797. if (player>=0)
  798. {
  799. CGameInterface * temp = CGI->playerint[CGI->state->players[player].serial];
  800. if (temp->human)
  801. ((CPlayerInterface*)(temp))->showInfoDialog(text,*components);
  802. return;
  803. }
  804. else
  805. {
  806. for (int i=0; i<CGI->playerint.size();i++)
  807. {
  808. if (CGI->playerint[i]->human)
  809. ((CPlayerInterface*)(CGI->playerint[i]))->showInfoDialog(text,*components);
  810. }
  811. }
  812. }
  813. void CScriptCallback::showSelDialog(int player, std::string text, std::vector<CSelectableComponent*>*components, IChosen * asker)
  814. {
  815. CGameInterface * temp = CGI->playerint[CGI->state->players[player].serial];
  816. if (temp->human)
  817. ((CPlayerInterface*)(temp))->showSelDialog(text,*components,(int)asker);
  818. return;
  819. }
  820. int CScriptCallback::getSelectedHero()
  821. {
  822. int ret;
  823. if (LOCPLINT->adventureInt->selection.type == HEROI_TYPE)
  824. ret = ((CGHeroInstance*)(LOCPLINT->adventureInt->selection.selected))->subID;
  825. else
  826. ret = -1;;
  827. return ret;
  828. }
  829. int CScriptCallback::getDate(int mode)
  830. {
  831. int temp;
  832. switch (mode)
  833. {
  834. case 0:
  835. return gs->day;
  836. break;
  837. case 1:
  838. temp = (gs->day)%7;
  839. if (temp)
  840. return temp;
  841. else return 7;
  842. break;
  843. case 2:
  844. temp = ((gs->day-1)/7)+1;
  845. if (!(temp%4))
  846. return 4;
  847. else
  848. return (temp%4);
  849. break;
  850. case 3:
  851. return ((gs->day-1)/28)+1;
  852. break;
  853. }
  854. return 0;
  855. }
  856. void CScriptCallback::giveResource(int player, int which, int val)
  857. {
  858. gs->players[player].resources[which]+=val;
  859. CGI->playerint[gs->players[player].serial]->receivedResource(which,val);
  860. }
  861. void CScriptCallback::showCompInfo(int player, SComponent * comp)
  862. {
  863. CPlayerInterface * i = dynamic_cast<CPlayerInterface*>(CGI->playerint[gs->players[player].serial]);
  864. if(i)
  865. i->showComp(*comp);
  866. }
  867. void CScriptCallback::heroVisitCastle(CGObjectInstance * ob, int heroID)
  868. {
  869. CGTownInstance * n;
  870. if(n = dynamic_cast<CGTownInstance*>(ob))
  871. {
  872. n->visitingHero = CGI->state->getHero(heroID,0);
  873. CGI->state->getHero(heroID,0)->visitedTown = n;
  874. for(int b=0; b<CGI->playerint.size(); ++b)
  875. {
  876. if(CGI->playerint[b]->playerID == getHeroOwner(heroID))
  877. {
  878. CGI->playerint[b]->heroVisitsTown(CGI->state->getHero(heroID,0),n);
  879. break;
  880. }
  881. }
  882. }
  883. else
  884. return;
  885. }
  886. void CScriptCallback::stopHeroVisitCastle(CGObjectInstance * ob, int heroID)
  887. {
  888. CGTownInstance * n;
  889. if(n = dynamic_cast<CGTownInstance*>(ob))
  890. {
  891. CGI->state->getHero(heroID,0)->visitedTown = NULL;
  892. if(n->visitingHero && n->visitingHero->type->ID == heroID)
  893. n->visitingHero = NULL;
  894. return;
  895. }
  896. else
  897. return;
  898. }
  899. void CScriptCallback::giveHeroArtifact(int artid, int hid, int position) //pos==-1 - first free slot in backpack
  900. {
  901. CGHeroInstance* h = gs->getHero(hid,0);
  902. if(position<0)
  903. {
  904. for(int i=0;i<h->artifacts.size();i++)
  905. {
  906. if(!h->artifacts[i])
  907. {
  908. h->artifacts[i] = &CGI->arth->artifacts[artid];
  909. return;
  910. }
  911. }
  912. h->artifacts.push_back(&CGI->arth->artifacts[artid]);
  913. return;
  914. }
  915. else
  916. {
  917. if(h->artifWorn[position]) //slot is occupied
  918. {
  919. giveHeroArtifact(h->artifWorn[position]->id,hid,-1);
  920. }
  921. h->artifWorn[position] = &CGI->arth->artifacts[artid];
  922. }
  923. }
  924. void CScriptCallback::startBattle(CCreatureSet * army1, CCreatureSet * army2, int3 tile, CGHeroInstance *hero1, CGHeroInstance *hero2) //use hero=NULL for no hero
  925. {
  926. gs->battle(army1,army2,tile,hero1,hero2);
  927. }
  928. void CScriptCallback::startBattle(int heroID, CCreatureSet * army, int3 tile) //for hero<=>neutral army
  929. {
  930. CGHeroInstance* h = gs->getHero(heroID,0);
  931. gs->battle(&h->army,army,tile,h,NULL);
  932. }
  933. void CLuaCallback::registerFuncs(lua_State * L)
  934. {
  935. lua_newtable(L);
  936. #define REGISTER_C_FUNC(x) \
  937. lua_pushstring(L, #x); \
  938. lua_pushcfunction(L, x); \
  939. lua_rawset(L, -3)
  940. REGISTER_C_FUNC(getPos);
  941. REGISTER_C_FUNC(changePrimSkill);
  942. REGISTER_C_FUNC(getGnrlText);
  943. REGISTER_C_FUNC(getSelectedHero);
  944. /*
  945. REGISTER_C_FUNC(changePrimSkill);
  946. REGISTER_C_FUNC(getGnrlText);
  947. REGISTER_C_FUNC(changePrimSkill);
  948. REGISTER_C_FUNC(getGnrlText);
  949. REGISTER_C_FUNC(changePrimSkill);
  950. REGISTER_C_FUNC(getGnrlText);*/
  951. lua_setglobal(L, "vcmi");
  952. #undef REGISTER_C_FUNC
  953. }
  954. int CLuaCallback::getPos(lua_State * L)//(CGObjectInstance * object);
  955. {
  956. const int args = lua_gettop(L); // number of arguments
  957. if ((args < 1) || !lua_isnumber(L, 1) )
  958. luaL_error(L,
  959. "Incorrect arguments to getPos([Object address])");
  960. CGObjectInstance * object = (CGObjectInstance *)(lua_tointeger(L, 1));
  961. lua_pushinteger(L,object->pos.x);
  962. lua_pushinteger(L,object->pos.y);
  963. lua_pushinteger(L,object->pos.z);
  964. return 3;
  965. }
  966. int CLuaCallback::changePrimSkill(lua_State * L)//(int ID, int which, int val);
  967. {
  968. const int args = lua_gettop(L); // number of arguments
  969. if ((args < 1) || !lua_isnumber(L, 1) ||
  970. ((args >= 2) && !lua_isnumber(L, 2)) ||
  971. ((args >= 3) && !lua_isnumber(L, 3)) )
  972. {
  973. luaL_error(L,
  974. "Incorrect arguments to changePrimSkill([Hero ID], [Which Primary skill], [Change by])");
  975. }
  976. int ID = lua_tointeger(L, 1),
  977. which = lua_tointeger(L, 2),
  978. val = lua_tointeger(L, 3);
  979. CScriptCallback::changePrimSkill(ID,which,val);
  980. return 0;
  981. }
  982. int CLuaCallback::getGnrlText(lua_State * L) //(int which),returns string
  983. {
  984. const int args = lua_gettop(L); // number of arguments
  985. if ((args < 1) || !lua_isnumber(L, 1) )
  986. luaL_error(L,
  987. "Incorrect arguments to getGnrlText([Text ID])");
  988. int which = lua_tointeger(L,1);
  989. lua_pushstring(L,CGI->generaltexth->allTexts[which].c_str());
  990. return 1;
  991. }
  992. int CLuaCallback::getSelectedHero(lua_State * L) //(),returns int (ID of hero, -1 if no hero is seleceted)
  993. {
  994. int ret;
  995. if (LOCPLINT->adventureInt->selection.type == HEROI_TYPE)
  996. ret = ((CGHeroInstance*)(LOCPLINT->adventureInt->selection.selected))->subID;
  997. else
  998. ret = -1;
  999. lua_pushinteger(L,ret);
  1000. return 1;
  1001. }