CCallback.cpp 31 KB

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