CScriptCallback.cpp 6.4 KB

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