CScriptCallback.cpp 7.6 KB

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