CScriptCallback.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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::setOwner(int objid, ui8 owner)
  38. {
  39. SetObjectProperty sop(objid,1,owner);
  40. gh->sendAndApply(&sop);
  41. }
  42. const CGObjectInstance* CScriptCallback::getObj(int objid)
  43. {
  44. return gh->gs->map->objects[objid];
  45. }
  46. const CGHeroInstance* CScriptCallback::getHero(int objid)
  47. {
  48. return static_cast<const CGHeroInstance*>(gh->gs->map->objects[objid]);
  49. }
  50. const CGTownInstance* CScriptCallback::getTown(int objid)
  51. {
  52. return static_cast<const CGTownInstance*>(gh->gs->map->objects[objid]);
  53. }
  54. void CScriptCallback::setHoverName(int objid, MetaString* name)
  55. {
  56. SetHoverName shn(objid, *name);
  57. gh->sendAndApply(&shn);
  58. }
  59. int3 CScriptCallback::getPos(CGObjectInstance * ob)
  60. {
  61. return ob->pos;
  62. }
  63. void CScriptCallback::changePrimSkill(int ID, int which, int val, bool abs)
  64. {
  65. gh->changePrimSkill(ID, which, val, abs);
  66. }
  67. int CScriptCallback::getOwner(int heroID)
  68. {
  69. return gh->gs->map->objects[heroID]->tempOwner;
  70. }
  71. int CScriptCallback::getResource(int player, int which)
  72. {
  73. return gh->gs->players[player].resources[which];
  74. }
  75. void CScriptCallback::showInfoDialog(InfoWindow *iw)
  76. {
  77. gh->sendToAllClients(iw);
  78. }
  79. void CScriptCallback::showYesNoDialog( YesNoDialog *iw, const CFunctionList<void(ui32)> &callback )
  80. {
  81. gh->ask(iw,iw->player,callback);
  82. }
  83. void CScriptCallback::showSelectionDialog(SelectionDialog *iw, const CFunctionList<void(ui32)> &callback)
  84. {
  85. gh->ask(iw,iw->player,callback);
  86. }
  87. int CScriptCallback::getSelectedHero()
  88. {
  89. //int ret;
  90. //if (LOCPLINT->adventureInt->selection->ID == HEROI_TYPE)
  91. // ret = ((CGHeroInstance*)(LOCPLINT->adventureInt->selection))->subID;
  92. //else
  93. // ret = -1;;
  94. return -1;
  95. }
  96. int CScriptCallback::getDate(int mode)
  97. {
  98. return gh->gs->getDate(mode);
  99. }
  100. void CScriptCallback::giveResource(int player, int which, int val)
  101. {
  102. SetResource sr;
  103. sr.player = player;
  104. sr.resid = which;
  105. sr.val = (gh->gs->players[player].resources[which]+val);
  106. gh->sendAndApply(&sr);
  107. }
  108. void CScriptCallback::showCompInfo(ShowInInfobox * comp)
  109. {
  110. gh->sendToAllClients(comp);
  111. }
  112. void CScriptCallback::heroVisitCastle(int obj, int heroID)
  113. {
  114. HeroVisitCastle vc;
  115. vc.hid = heroID;
  116. vc.tid = obj;
  117. vc.flags |= 1;
  118. gh->sendAndApply(&vc);
  119. gh->giveSpells(getTown(obj),getHero(heroID));
  120. }
  121. void CScriptCallback::stopHeroVisitCastle(int obj, int heroID)
  122. {
  123. HeroVisitCastle vc;
  124. vc.hid = heroID;
  125. vc.tid = obj;
  126. gh->sendAndApply(&vc);
  127. }
  128. void CScriptCallback::giveHeroArtifact(int artid, int hid, int position) //pos==-1 - first free slot in backpack
  129. {
  130. const CGHeroInstance* h = getHero(hid);
  131. SetHeroArtifacts sha;
  132. sha.hid = hid;
  133. sha.artifacts = h->artifacts;
  134. sha.artifWorn = h->artifWorn;
  135. if(position<0)
  136. sha.artifacts.push_back(artid);
  137. else
  138. if(!vstd::contains(sha.artifWorn,ui16(position)))
  139. sha.artifWorn[position] = artid;
  140. else
  141. sha.artifacts.push_back(artid);
  142. gh->sendAndApply(&sha);
  143. }
  144. 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
  145. {
  146. boost::thread(boost::bind(&CGameHandler::startBattle,gh,*(CCreatureSet *)army1,*(CCreatureSet *)army2,tile,(CGHeroInstance *)hero1,(CGHeroInstance *)hero2,cb));
  147. }
  148. void CScriptCallback::startBattle(int heroID, CCreatureSet army, int3 tile, boost::function<void(BattleResult*)> cb) //for hero<=>neutral army
  149. {
  150. CGHeroInstance* h = const_cast<CGHeroInstance*>(getHero(heroID));
  151. startBattle(&h->army,&army,tile,h,NULL,cb);
  152. //gh->gs->battle(&h->army,army,tile,h,NULL);
  153. }
  154. void CScriptCallback::changeSpells( int hid, bool give, const std::set<ui32> &spells )
  155. {
  156. ChangeSpells cs;
  157. cs.hid = hid;
  158. cs.spells = spells;
  159. cs.learn = give;
  160. gh->sendAndApply(&cs);
  161. }
  162. void CLuaCallback::registerFuncs(lua_State * L)
  163. {
  164. // lua_newtable(L);
  165. //
  166. //#define REGISTER_C_FUNC(x) \
  167. // lua_pushstring(L, #x); \
  168. // lua_pushcfunction(L, x); \
  169. // lua_rawset(L, -3)
  170. //
  171. // REGISTER_C_FUNC(getPos);
  172. // REGISTER_C_FUNC(changePrimSkill);
  173. // REGISTER_C_FUNC(getGnrlText);
  174. // REGISTER_C_FUNC(getSelectedHero);
  175. //
  176. // lua_setglobal(L, "vcmi");
  177. // #undef REGISTER_C_FUNC
  178. }
  179. int CLuaCallback::getPos(lua_State * L)//(CGObjectInstance * object);
  180. {
  181. //const int args = lua_gettop(L); // number of arguments
  182. //if ((args < 1) || !lua_isnumber(L, 1) )
  183. // luaL_error(L,
  184. // "Incorrect arguments to getPos([Object address])");
  185. //CGObjectInstance * object = (CGObjectInstance *)(lua_tointeger(L, 1));
  186. //lua_pushinteger(L,object->pos.x);
  187. //lua_pushinteger(L,object->pos.y);
  188. //lua_pushinteger(L,object->pos.z);
  189. return 3;
  190. }
  191. int CLuaCallback::changePrimSkill(lua_State * L)//(int ID, int which, int val);
  192. {
  193. //const int args = lua_gettop(L); // number of arguments
  194. //if ((args < 1) || !lua_isnumber(L, 1) ||
  195. // ((args >= 2) && !lua_isnumber(L, 2)) ||
  196. // ((args >= 3) && !lua_isnumber(L, 3)) )
  197. //{
  198. // luaL_error(L,
  199. // "Incorrect arguments to changePrimSkill([Hero ID], [Which Primary skill], [Change by])");
  200. //}
  201. //int ID = lua_tointeger(L, 1),
  202. // which = lua_tointeger(L, 2),
  203. // val = lua_tointeger(L, 3);
  204. //CScriptCallback::changePrimSkill(ID,which,val);
  205. return 0;
  206. }
  207. int CLuaCallback::getGnrlText(lua_State * L) //(int which),returns string
  208. {
  209. //const int args = lua_gettop(L); // number of arguments
  210. //if ((args < 1) || !lua_isnumber(L, 1) )
  211. // luaL_error(L,
  212. // "Incorrect arguments to getGnrlText([Text ID])");
  213. //int which = lua_tointeger(L,1);
  214. //lua_pushstring(L,CGI->generaltexth->allTexts[which].c_str());
  215. return 1;
  216. }
  217. int CLuaCallback::getSelectedHero(lua_State * L) //(),returns int (ID of hero, -1 if no hero is seleceted)
  218. {
  219. //int ret;
  220. //if (LOCPLINT->adventureInt->selection->ID == HEROI_TYPE)
  221. // ret = ((CGHeroInstance*)(LOCPLINT->adventureInt->selection))->subID;
  222. //else
  223. // ret = -1;
  224. //lua_pushinteger(L,ret);
  225. return 1;
  226. }