2
0

CScriptCallback.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. }
  115. void CScriptCallback::stopHeroVisitCastle(int obj, int heroID)
  116. {
  117. HeroVisitCastle vc;
  118. vc.hid = heroID;
  119. vc.tid = obj;
  120. gh->sendAndApply(&vc);
  121. }
  122. void CScriptCallback::giveHeroArtifact(int artid, int hid, int position) //pos==-1 - first free slot in backpack
  123. {
  124. const CGHeroInstance* h = getHero(hid);
  125. SetHeroArtifacts sha;
  126. sha.hid = hid;
  127. sha.artifacts = h->artifacts;
  128. sha.artifWorn = h->artifWorn;
  129. if(position<0)
  130. sha.artifacts.push_back(artid);
  131. else
  132. if(!vstd::contains(sha.artifWorn,ui16(position)))
  133. sha.artifWorn[position] = artid;
  134. else
  135. sha.artifacts.push_back(artid);
  136. gh->sendAndApply(&sha);
  137. }
  138. void CScriptCallback::startBattle(const CCreatureSet * army1, const CCreatureSet * army2, int3 tile, const CGHeroInstance *hero1, const CGHeroInstance *hero2) //use hero=NULL for no hero
  139. {
  140. boost::thread(boost::bind(&CGameHandler::startBattle,gh,*(CCreatureSet *)army1,*(CCreatureSet *)army2,tile,(CGHeroInstance *)hero1,(CGHeroInstance *)hero2));
  141. }
  142. void CScriptCallback::startBattle(int heroID, CCreatureSet army, int3 tile) //for hero<=>neutral army
  143. {
  144. CGHeroInstance* h = const_cast<CGHeroInstance*>(getHero(heroID));
  145. startBattle(&h->army,&army,tile,h,NULL);
  146. //gh->gs->battle(&h->army,army,tile,h,NULL);
  147. }
  148. void CLuaCallback::registerFuncs(lua_State * L)
  149. {
  150. // lua_newtable(L);
  151. //
  152. //#define REGISTER_C_FUNC(x) \
  153. // lua_pushstring(L, #x); \
  154. // lua_pushcfunction(L, x); \
  155. // lua_rawset(L, -3)
  156. //
  157. // REGISTER_C_FUNC(getPos);
  158. // REGISTER_C_FUNC(changePrimSkill);
  159. // REGISTER_C_FUNC(getGnrlText);
  160. // REGISTER_C_FUNC(getSelectedHero);
  161. //
  162. // lua_setglobal(L, "vcmi");
  163. // #undef REGISTER_C_FUNC
  164. }
  165. int CLuaCallback::getPos(lua_State * L)//(CGObjectInstance * object);
  166. {
  167. //const int args = lua_gettop(L); // number of arguments
  168. //if ((args < 1) || !lua_isnumber(L, 1) )
  169. // luaL_error(L,
  170. // "Incorrect arguments to getPos([Object address])");
  171. //CGObjectInstance * object = (CGObjectInstance *)(lua_tointeger(L, 1));
  172. //lua_pushinteger(L,object->pos.x);
  173. //lua_pushinteger(L,object->pos.y);
  174. //lua_pushinteger(L,object->pos.z);
  175. return 3;
  176. }
  177. int CLuaCallback::changePrimSkill(lua_State * L)//(int ID, int which, int val);
  178. {
  179. //const int args = lua_gettop(L); // number of arguments
  180. //if ((args < 1) || !lua_isnumber(L, 1) ||
  181. // ((args >= 2) && !lua_isnumber(L, 2)) ||
  182. // ((args >= 3) && !lua_isnumber(L, 3)) )
  183. //{
  184. // luaL_error(L,
  185. // "Incorrect arguments to changePrimSkill([Hero ID], [Which Primary skill], [Change by])");
  186. //}
  187. //int ID = lua_tointeger(L, 1),
  188. // which = lua_tointeger(L, 2),
  189. // val = lua_tointeger(L, 3);
  190. //CScriptCallback::changePrimSkill(ID,which,val);
  191. return 0;
  192. }
  193. int CLuaCallback::getGnrlText(lua_State * L) //(int which),returns string
  194. {
  195. //const int args = lua_gettop(L); // number of arguments
  196. //if ((args < 1) || !lua_isnumber(L, 1) )
  197. // luaL_error(L,
  198. // "Incorrect arguments to getGnrlText([Text ID])");
  199. //int which = lua_tointeger(L,1);
  200. //lua_pushstring(L,CGI->generaltexth->allTexts[which].c_str());
  201. return 1;
  202. }
  203. int CLuaCallback::getSelectedHero(lua_State * L) //(),returns int (ID of hero, -1 if no hero is seleceted)
  204. {
  205. //int ret;
  206. //if (LOCPLINT->adventureInt->selection->ID == HEROI_TYPE)
  207. // ret = ((CGHeroInstance*)(LOCPLINT->adventureInt->selection))->subID;
  208. //else
  209. // ret = -1;
  210. //lua_pushinteger(L,ret);
  211. return 1;
  212. }