CCallback.cpp 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106
  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. ( CGI->state->curB->stacks[g]->creature->isDoubleWide() &&
  696. ( (CGI->state->curB->stacks[g]->attackerOwned && CGI->state->curB->stacks[g]->position-1 == pos) ||
  697. (!CGI->state->curB->stacks[g]->attackerOwned && CGI->state->curB->stacks[g]->position+1 == pos)
  698. )
  699. )
  700. )
  701. return CGI->state->curB->stacks[g]->ID;
  702. }
  703. return -1;
  704. }
  705. CStack CCallback::battleGetStackByID(int ID)
  706. {
  707. for(int g=0; g<CGI->state->curB->stacks.size(); ++g)
  708. {
  709. if(CGI->state->curB->stacks[g]->ID == ID)
  710. return *(CGI->state->curB->stacks[g]);
  711. }
  712. return CStack();
  713. }
  714. CStack CCallback::battleGetStackByPos(int pos)
  715. {
  716. return battleGetStackByID(battleGetStack(pos));
  717. }
  718. int CCallback::battleGetPos(int stack)
  719. {
  720. for(int g=0; g<CGI->state->curB->stacks.size(); ++g)
  721. {
  722. if(CGI->state->curB->stacks[g]->ID == stack)
  723. return CGI->state->curB->stacks[g]->position;
  724. }
  725. return -1;
  726. }
  727. std::map<int, CStack> CCallback::battleGetStacks()
  728. {
  729. std::map<int, CStack> ret;
  730. for(int g=0; g<CGI->state->curB->stacks.size(); ++g)
  731. {
  732. ret[CGI->state->curB->stacks[g]->ID] = *(CGI->state->curB->stacks[g]);
  733. }
  734. return ret;
  735. }
  736. CCreature CCallback::battleGetCreature(int number)
  737. {
  738. for(int h=0; h<CGI->state->curB->stacks.size(); ++h)
  739. {
  740. if(CGI->state->curB->stacks[h]->ID == number) //creature found
  741. return *(CGI->state->curB->stacks[h]->creature);
  742. }
  743. throw new std::exception("Cannot find the creature");
  744. }
  745. std::vector<int> CCallback::battleGetAvailableHexes(int ID)
  746. {
  747. return CGI->state->battleGetRange(ID);
  748. }
  749. bool CCallback::battleIsStackMine(int ID)
  750. {
  751. for(int h=0; h<CGI->state->curB->stacks.size(); ++h)
  752. {
  753. if(CGI->state->curB->stacks[h]->ID == ID) //creature found
  754. return CGI->state->curB->stacks[h]->owner == player;
  755. }
  756. return false;
  757. }
  758. int3 CScriptCallback::getPos(CGObjectInstance * ob)
  759. {
  760. return ob->pos;
  761. }
  762. void CScriptCallback::changePrimSkill(int ID, int which, int val)
  763. {
  764. CGHeroInstance * hero = CGI->state->getHero(ID,0);
  765. if (which<PRIMARY_SKILLS)
  766. {
  767. hero->primSkills[which]+=val;
  768. for (int i=0; i<CGI->playerint.size(); i++)
  769. {
  770. if (CGI->playerint[i]->playerID == hero->getOwner())
  771. {
  772. CGI->playerint[i]->heroPrimarySkillChanged(hero, which, val);
  773. break;
  774. }
  775. }
  776. }
  777. else if (which==4)
  778. {
  779. hero->exp+=val;
  780. if(hero->exp >= CGI->heroh->reqExp(hero->level+1)) //new level
  781. {
  782. hero->level++;
  783. std::cout << hero->name <<" got level "<<hero->level<<std::endl;
  784. int r = rand()%100, pom=0, x=0;
  785. int std::pair<int,int>::*g = (hero->level>9) ? (&std::pair<int,int>::second) : (&std::pair<int,int>::first);
  786. for(;x<PRIMARY_SKILLS;x++)
  787. {
  788. pom += hero->type->heroClass->primChance[x].*g;
  789. if(r<pom)
  790. break;
  791. }
  792. std::cout << "Bohater dostaje umiejetnosc pierwszorzedna " << x << " (wynik losowania "<<r<<")"<<std::endl;
  793. hero->primSkills[x]++;
  794. //TODO: dac dwie umiejetnosci 2-rzedne to wyboru
  795. }
  796. //TODO - powiadomic interfejsy, sprawdzic czy nie ma awansu itp
  797. }
  798. }
  799. int CScriptCallback::getHeroOwner(int heroID)
  800. {
  801. CGHeroInstance * hero = CGI->state->getHero(heroID,0);
  802. return hero->getOwner();
  803. }
  804. void CScriptCallback::showInfoDialog(int player, std::string text, std::vector<SComponent*> * components)
  805. {
  806. //TODO: upewniac sie ze mozemy to zrzutowac (przy customowych interfejsach cos moze sie kopnac)
  807. if (player>=0)
  808. {
  809. CGameInterface * temp = CGI->playerint[CGI->state->players[player].serial];
  810. if (temp->human)
  811. ((CPlayerInterface*)(temp))->showInfoDialog(text,*components);
  812. return;
  813. }
  814. else
  815. {
  816. for (int i=0; i<CGI->playerint.size();i++)
  817. {
  818. if (CGI->playerint[i]->human)
  819. ((CPlayerInterface*)(CGI->playerint[i]))->showInfoDialog(text,*components);
  820. }
  821. }
  822. }
  823. void CScriptCallback::showSelDialog(int player, std::string text, std::vector<CSelectableComponent*>*components, IChosen * asker)
  824. {
  825. CGameInterface * temp = CGI->playerint[CGI->state->players[player].serial];
  826. if (temp->human)
  827. ((CPlayerInterface*)(temp))->showSelDialog(text,*components,(int)asker);
  828. return;
  829. }
  830. int CScriptCallback::getSelectedHero()
  831. {
  832. int ret;
  833. if (LOCPLINT->adventureInt->selection.type == HEROI_TYPE)
  834. ret = ((CGHeroInstance*)(LOCPLINT->adventureInt->selection.selected))->subID;
  835. else
  836. ret = -1;;
  837. return ret;
  838. }
  839. int CScriptCallback::getDate(int mode)
  840. {
  841. int temp;
  842. switch (mode)
  843. {
  844. case 0:
  845. return gs->day;
  846. break;
  847. case 1:
  848. temp = (gs->day)%7;
  849. if (temp)
  850. return temp;
  851. else return 7;
  852. break;
  853. case 2:
  854. temp = ((gs->day-1)/7)+1;
  855. if (!(temp%4))
  856. return 4;
  857. else
  858. return (temp%4);
  859. break;
  860. case 3:
  861. return ((gs->day-1)/28)+1;
  862. break;
  863. }
  864. return 0;
  865. }
  866. void CScriptCallback::giveResource(int player, int which, int val)
  867. {
  868. gs->players[player].resources[which]+=val;
  869. CGI->playerint[gs->players[player].serial]->receivedResource(which,val);
  870. }
  871. void CScriptCallback::showCompInfo(int player, SComponent * comp)
  872. {
  873. CPlayerInterface * i = dynamic_cast<CPlayerInterface*>(CGI->playerint[gs->players[player].serial]);
  874. if(i)
  875. i->showComp(*comp);
  876. }
  877. void CScriptCallback::heroVisitCastle(CGObjectInstance * ob, int heroID)
  878. {
  879. CGTownInstance * n;
  880. if(n = dynamic_cast<CGTownInstance*>(ob))
  881. {
  882. n->visitingHero = CGI->state->getHero(heroID,0);
  883. CGI->state->getHero(heroID,0)->visitedTown = n;
  884. for(int b=0; b<CGI->playerint.size(); ++b)
  885. {
  886. if(CGI->playerint[b]->playerID == getHeroOwner(heroID))
  887. {
  888. CGI->playerint[b]->heroVisitsTown(CGI->state->getHero(heroID,0),n);
  889. break;
  890. }
  891. }
  892. }
  893. else
  894. return;
  895. }
  896. void CScriptCallback::stopHeroVisitCastle(CGObjectInstance * ob, int heroID)
  897. {
  898. CGTownInstance * n;
  899. if(n = dynamic_cast<CGTownInstance*>(ob))
  900. {
  901. CGI->state->getHero(heroID,0)->visitedTown = NULL;
  902. if(n->visitingHero && n->visitingHero->type->ID == heroID)
  903. n->visitingHero = NULL;
  904. return;
  905. }
  906. else
  907. return;
  908. }
  909. void CScriptCallback::giveHeroArtifact(int artid, int hid, int position) //pos==-1 - first free slot in backpack
  910. {
  911. CGHeroInstance* h = gs->getHero(hid,0);
  912. if(position<0)
  913. {
  914. for(int i=0;i<h->artifacts.size();i++)
  915. {
  916. if(!h->artifacts[i])
  917. {
  918. h->artifacts[i] = artid;
  919. return;
  920. }
  921. }
  922. h->artifacts.push_back(artid);
  923. return;
  924. }
  925. else
  926. {
  927. if(h->artifWorn[position]) //slot is occupied
  928. {
  929. giveHeroArtifact(h->artifWorn[position],hid,-1);
  930. }
  931. h->artifWorn[position] = artid;
  932. }
  933. }
  934. void CScriptCallback::startBattle(CCreatureSet * army1, CCreatureSet * army2, int3 tile, CGHeroInstance *hero1, CGHeroInstance *hero2) //use hero=NULL for no hero
  935. {
  936. gs->battle(army1,army2,tile,hero1,hero2);
  937. }
  938. void CScriptCallback::startBattle(int heroID, CCreatureSet * army, int3 tile) //for hero<=>neutral army
  939. {
  940. CGHeroInstance* h = gs->getHero(heroID,0);
  941. gs->battle(&h->army,army,tile,h,NULL);
  942. }
  943. void CLuaCallback::registerFuncs(lua_State * L)
  944. {
  945. lua_newtable(L);
  946. #define REGISTER_C_FUNC(x) \
  947. lua_pushstring(L, #x); \
  948. lua_pushcfunction(L, x); \
  949. lua_rawset(L, -3)
  950. REGISTER_C_FUNC(getPos);
  951. REGISTER_C_FUNC(changePrimSkill);
  952. REGISTER_C_FUNC(getGnrlText);
  953. REGISTER_C_FUNC(getSelectedHero);
  954. /*
  955. REGISTER_C_FUNC(changePrimSkill);
  956. REGISTER_C_FUNC(getGnrlText);
  957. REGISTER_C_FUNC(changePrimSkill);
  958. REGISTER_C_FUNC(getGnrlText);
  959. REGISTER_C_FUNC(changePrimSkill);
  960. REGISTER_C_FUNC(getGnrlText);*/
  961. lua_setglobal(L, "vcmi");
  962. #undef REGISTER_C_FUNC
  963. }
  964. int CLuaCallback::getPos(lua_State * L)//(CGObjectInstance * object);
  965. {
  966. const int args = lua_gettop(L); // number of arguments
  967. if ((args < 1) || !lua_isnumber(L, 1) )
  968. luaL_error(L,
  969. "Incorrect arguments to getPos([Object address])");
  970. CGObjectInstance * object = (CGObjectInstance *)(lua_tointeger(L, 1));
  971. lua_pushinteger(L,object->pos.x);
  972. lua_pushinteger(L,object->pos.y);
  973. lua_pushinteger(L,object->pos.z);
  974. return 3;
  975. }
  976. int CLuaCallback::changePrimSkill(lua_State * L)//(int ID, int which, int val);
  977. {
  978. const int args = lua_gettop(L); // number of arguments
  979. if ((args < 1) || !lua_isnumber(L, 1) ||
  980. ((args >= 2) && !lua_isnumber(L, 2)) ||
  981. ((args >= 3) && !lua_isnumber(L, 3)) )
  982. {
  983. luaL_error(L,
  984. "Incorrect arguments to changePrimSkill([Hero ID], [Which Primary skill], [Change by])");
  985. }
  986. int ID = lua_tointeger(L, 1),
  987. which = lua_tointeger(L, 2),
  988. val = lua_tointeger(L, 3);
  989. CScriptCallback::changePrimSkill(ID,which,val);
  990. return 0;
  991. }
  992. int CLuaCallback::getGnrlText(lua_State * L) //(int which),returns string
  993. {
  994. const int args = lua_gettop(L); // number of arguments
  995. if ((args < 1) || !lua_isnumber(L, 1) )
  996. luaL_error(L,
  997. "Incorrect arguments to getGnrlText([Text ID])");
  998. int which = lua_tointeger(L,1);
  999. lua_pushstring(L,CGI->generaltexth->allTexts[which].c_str());
  1000. return 1;
  1001. }
  1002. int CLuaCallback::getSelectedHero(lua_State * L) //(),returns int (ID of hero, -1 if no hero is seleceted)
  1003. {
  1004. int ret;
  1005. if (LOCPLINT->adventureInt->selection.type == HEROI_TYPE)
  1006. ret = ((CGHeroInstance*)(LOCPLINT->adventureInt->selection.selected))->subID;
  1007. else
  1008. ret = -1;
  1009. lua_pushinteger(L,ret);
  1010. return 1;
  1011. }