CScriptCallback.cpp 6.6 KB

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