CScriptCallback.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. #include "CScriptCallback.h"
  2. #include "../lib/Connection.h"
  3. #include "CVCMIServer.h"
  4. #include "CGameHandler.h"
  5. #include "../CGameState.h"
  6. #include "../map.h"
  7. #include "../hch/CArtHandler.h"
  8. #include "../hch/CObjectHandler.h"
  9. #include "../hch/CTownHandler.h"
  10. #include "../hch/CHeroHandler.h"
  11. #include "../lib/NetPacks.h"
  12. #include "../lib/VCMI_Lib.h"
  13. #include <boost/bind.hpp>
  14. #include <boost/foreach.hpp>
  15. #include <boost/thread.hpp>
  16. CScriptCallback::CScriptCallback(void)
  17. {
  18. }
  19. CScriptCallback::~CScriptCallback(void)
  20. {
  21. }
  22. void CScriptCallback::setBlockVis(int objid, bool bv)
  23. {
  24. SetObjectProperty sop(objid,2,bv);
  25. gh->sendAndApply(&sop);
  26. }
  27. void CScriptCallback::removeObject(int objid)
  28. {
  29. RemoveObject ro;
  30. ro.id = objid;
  31. gh->sendAndApply(&ro);
  32. }
  33. void CScriptCallback::setAmount(int objid, ui32 val)
  34. {
  35. SetObjectProperty sop(objid,3,val);
  36. gh->sendAndApply(&sop);
  37. }
  38. void CScriptCallback::moveHero(int hid, int3 pos, bool instant)
  39. {
  40. if(!instant)
  41. {
  42. tlog1 << "Not supported call to CScriptCallback::moveHero\n";
  43. return;
  44. }
  45. CGHeroInstance *h = const_cast<CGHeroInstance *>(getHero(hid));
  46. //check if destination tile is free
  47. BOOST_FOREACH(CGObjectInstance* obj, gh->gs->map->terrain[pos.x-1][pos.y][pos.z].blockingObjects)
  48. {
  49. if(obj->ID==34)
  50. {
  51. if(obj->tempOwner==h->tempOwner)
  52. return;//TODO: exchange
  53. //TODO: check for ally
  54. CGHeroInstance *dh = static_cast<CGHeroInstance *>(obj);
  55. startBattle(&h->army,&dh->army,pos,h,dh,0);
  56. return;
  57. }
  58. }
  59. TryMoveHero tmh;
  60. tmh.start = h->pos;
  61. tmh.end = pos;
  62. tmh.id = hid;
  63. tmh.movePoints = h->movement;
  64. tmh.result = instant+1;
  65. tmh.fowRevealed = gh->gs->tilesToReveal(CGHeroInstance::convertPosition(pos,false),h->getSightDistance(),h->tempOwner);
  66. gh->sendAndApply(&tmh);
  67. }
  68. void CScriptCallback::setOwner(int objid, ui8 owner)
  69. {
  70. SetObjectProperty sop(objid,1,owner);
  71. gh->sendAndApply(&sop);
  72. }
  73. const CGObjectInstance* CScriptCallback::getObj(int objid)
  74. {
  75. return gh->gs->map->objects[objid];
  76. }
  77. const CGHeroInstance* CScriptCallback::getHero(int objid)
  78. {
  79. return static_cast<const CGHeroInstance*>(gh->gs->map->objects[objid]);
  80. }
  81. const CGTownInstance* CScriptCallback::getTown(int objid)
  82. {
  83. return static_cast<const CGTownInstance*>(gh->gs->map->objects[objid]);
  84. }
  85. void CScriptCallback::setHoverName(int objid, MetaString* name)
  86. {
  87. SetHoverName shn(objid, *name);
  88. gh->sendAndApply(&shn);
  89. }
  90. int3 CScriptCallback::getPos(CGObjectInstance * ob)
  91. {
  92. return ob->pos;
  93. }
  94. void CScriptCallback::changePrimSkill(int ID, int which, int val, bool abs)
  95. {
  96. gh->changePrimSkill(ID, which, val, abs);
  97. }
  98. int CScriptCallback::getOwner(int heroID)
  99. {
  100. return gh->gs->map->objects[heroID]->tempOwner;
  101. }
  102. int CScriptCallback::getResource(int player, int which)
  103. {
  104. return gh->gs->players[player].resources[which];
  105. }
  106. void CScriptCallback::showInfoDialog(InfoWindow *iw)
  107. {
  108. gh->sendToAllClients(iw);
  109. }
  110. void CScriptCallback::showYesNoDialog( YesNoDialog *iw, const CFunctionList<void(ui32)> &callback )
  111. {
  112. gh->ask(iw,iw->player,callback);
  113. }
  114. void CScriptCallback::showSelectionDialog(SelectionDialog *iw, const CFunctionList<void(ui32)> &callback)
  115. {
  116. gh->ask(iw,iw->player,callback);
  117. }
  118. int CScriptCallback::getSelectedHero()
  119. {
  120. //int ret;
  121. //if (LOCPLINT->adventureInt->selection->ID == HEROI_TYPE)
  122. // ret = ((CGHeroInstance*)(LOCPLINT->adventureInt->selection))->subID;
  123. //else
  124. // ret = -1;;
  125. return -1;
  126. }
  127. int CScriptCallback::getDate(int mode)
  128. {
  129. return gh->gs->getDate(mode);
  130. }
  131. void CScriptCallback::giveResource(int player, int which, int val)
  132. {
  133. SetResource sr;
  134. sr.player = player;
  135. sr.resid = which;
  136. sr.val = (gh->gs->players[player].resources[which]+val);
  137. gh->sendAndApply(&sr);
  138. }
  139. void CScriptCallback::showCompInfo(ShowInInfobox * comp)
  140. {
  141. gh->sendToAllClients(comp);
  142. }
  143. void CScriptCallback::heroVisitCastle(int obj, int heroID)
  144. {
  145. HeroVisitCastle vc;
  146. vc.hid = heroID;
  147. vc.tid = obj;
  148. vc.flags |= 1;
  149. gh->sendAndApply(&vc);
  150. gh->giveSpells(getTown(obj),getHero(heroID));
  151. }
  152. void CScriptCallback::stopHeroVisitCastle(int obj, int heroID)
  153. {
  154. HeroVisitCastle vc;
  155. vc.hid = heroID;
  156. vc.tid = obj;
  157. gh->sendAndApply(&vc);
  158. }
  159. void CScriptCallback::giveHeroArtifact(int artid, int hid, int position) //pos==-1 - first free slot in backpack
  160. {
  161. const CGHeroInstance* h = getHero(hid);
  162. SetHeroArtifacts sha;
  163. sha.hid = hid;
  164. sha.artifacts = h->artifacts;
  165. sha.artifWorn = h->artifWorn;
  166. if(position<0)
  167. {
  168. if(position == -2)
  169. {
  170. int i;
  171. for(i=0; i<VLC->arth->artifacts[artid].possibleSlots.size(); i++) //try to put artifact into first avaialble slot
  172. {
  173. if( !vstd::contains(sha.artifWorn,VLC->arth->artifacts[artid].possibleSlots[i]) )
  174. {
  175. sha.artifWorn[VLC->arth->artifacts[artid].possibleSlots[i]] = artid;
  176. break;
  177. }
  178. }
  179. if(i==VLC->arth->artifacts[artid].possibleSlots.size()) //if haven't find proper slot, use backpack
  180. sha.artifacts.push_back(artid);
  181. }
  182. else //should be -1 => putartifact into backpack
  183. {
  184. sha.artifacts.push_back(artid);
  185. }
  186. }
  187. else
  188. {
  189. if(!vstd::contains(sha.artifWorn,ui16(position)))
  190. sha.artifWorn[position] = artid;
  191. else
  192. sha.artifacts.push_back(artid);
  193. }
  194. gh->sendAndApply(&sha);
  195. }
  196. void CScriptCallback::startBattle(const CCreatureSet * army1, const CCreatureSet * army2, int3 tile, const CGHeroInstance *hero1, const CGHeroInstance *hero2, boost::function<void(BattleResult*)> cb) //use hero=NULL for no hero
  197. {
  198. boost::thread(boost::bind(&CGameHandler::startBattle,gh,*(CCreatureSet *)army1,*(CCreatureSet *)army2,tile,(CGHeroInstance *)hero1,(CGHeroInstance *)hero2,cb));
  199. }
  200. void CScriptCallback::startBattle(int heroID, CCreatureSet army, int3 tile, boost::function<void(BattleResult*)> cb) //for hero<=>neutral army
  201. {
  202. CGHeroInstance* h = const_cast<CGHeroInstance*>(getHero(heroID));
  203. startBattle(&h->army,&army,tile,h,NULL,cb);
  204. //gh->gs->battle(&h->army,army,tile,h,NULL);
  205. }
  206. void CScriptCallback::changeSpells( int hid, bool give, const std::set<ui32> &spells )
  207. {
  208. ChangeSpells cs;
  209. cs.hid = hid;
  210. cs.spells = spells;
  211. cs.learn = give;
  212. gh->sendAndApply(&cs);
  213. }
  214. void CLuaCallback::registerFuncs(lua_State * L)
  215. {
  216. // lua_newtable(L);
  217. //
  218. //#define REGISTER_C_FUNC(x) \
  219. // lua_pushstring(L, #x); \
  220. // lua_pushcfunction(L, x); \
  221. // lua_rawset(L, -3)
  222. //
  223. // REGISTER_C_FUNC(getPos);
  224. // REGISTER_C_FUNC(changePrimSkill);
  225. // REGISTER_C_FUNC(getGnrlText);
  226. // REGISTER_C_FUNC(getSelectedHero);
  227. //
  228. // lua_setglobal(L, "vcmi");
  229. // #undef REGISTER_C_FUNC
  230. }
  231. int CLuaCallback::getPos(lua_State * L)//(CGObjectInstance * object);
  232. {
  233. //const int args = lua_gettop(L); // number of arguments
  234. //if ((args < 1) || !lua_isnumber(L, 1) )
  235. // luaL_error(L,
  236. // "Incorrect arguments to getPos([Object address])");
  237. //CGObjectInstance * object = (CGObjectInstance *)(lua_tointeger(L, 1));
  238. //lua_pushinteger(L,object->pos.x);
  239. //lua_pushinteger(L,object->pos.y);
  240. //lua_pushinteger(L,object->pos.z);
  241. return 3;
  242. }
  243. int CLuaCallback::changePrimSkill(lua_State * L)//(int ID, int which, int val);
  244. {
  245. //const int args = lua_gettop(L); // number of arguments
  246. //if ((args < 1) || !lua_isnumber(L, 1) ||
  247. // ((args >= 2) && !lua_isnumber(L, 2)) ||
  248. // ((args >= 3) && !lua_isnumber(L, 3)) )
  249. //{
  250. // luaL_error(L,
  251. // "Incorrect arguments to changePrimSkill([Hero ID], [Which Primary skill], [Change by])");
  252. //}
  253. //int ID = lua_tointeger(L, 1),
  254. // which = lua_tointeger(L, 2),
  255. // val = lua_tointeger(L, 3);
  256. //CScriptCallback::changePrimSkill(ID,which,val);
  257. return 0;
  258. }
  259. int CLuaCallback::getGnrlText(lua_State * L) //(int which),returns string
  260. {
  261. //const int args = lua_gettop(L); // number of arguments
  262. //if ((args < 1) || !lua_isnumber(L, 1) )
  263. // luaL_error(L,
  264. // "Incorrect arguments to getGnrlText([Text ID])");
  265. //int which = lua_tointeger(L,1);
  266. //lua_pushstring(L,CGI->generaltexth->allTexts[which].c_str());
  267. return 1;
  268. }
  269. int CLuaCallback::getSelectedHero(lua_State * L) //(),returns int (ID of hero, -1 if no hero is seleceted)
  270. {
  271. //int ret;
  272. //if (LOCPLINT->adventureInt->selection->ID == HEROI_TYPE)
  273. // ret = ((CGHeroInstance*)(LOCPLINT->adventureInt->selection))->subID;
  274. //else
  275. // ret = -1;
  276. //lua_pushinteger(L,ret);
  277. return 1;
  278. }