CScriptCallback.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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 <boost/foreach.hpp>
  12. #include <boost/thread.hpp>
  13. CScriptCallback::CScriptCallback(void)
  14. {
  15. }
  16. CScriptCallback::~CScriptCallback(void)
  17. {
  18. }
  19. void CScriptCallback::setBlockVis(int objid, bool bv)
  20. {
  21. SetObjectProperty sop(objid,2,bv);
  22. gh->sendAndApply(&sop);
  23. }
  24. void CScriptCallback::setOwner(int objid, ui8 owner)
  25. {
  26. SetObjectProperty sop(objid,1,owner);
  27. gh->sendAndApply(&sop);
  28. }
  29. const CGObjectInstance* CScriptCallback::getObj(int objid)
  30. {
  31. return gh->gs->map->objects[objid];
  32. }
  33. const CGHeroInstance* CScriptCallback::getHero(int objid)
  34. {
  35. return static_cast<const CGHeroInstance*>(gh->gs->map->objects[objid]);
  36. }
  37. const CGTownInstance* CScriptCallback::getTown(int objid)
  38. {
  39. return static_cast<const CGTownInstance*>(gh->gs->map->objects[objid]);
  40. }
  41. void CScriptCallback::setHoverName(int objid, MetaString* name)
  42. {
  43. SetHoverName shn(objid, *name);
  44. gh->sendAndApply(&shn);
  45. }
  46. int3 CScriptCallback::getPos(CGObjectInstance * ob)
  47. {
  48. return ob->pos;
  49. }
  50. void CScriptCallback::changePrimSkill(int ID, int which, int val)
  51. {
  52. //CGHeroInstance * hero = gh->gs->map->getHero(ID,0);
  53. //if (which<PRIMARY_SKILLS)
  54. //{
  55. // hero->primSkills[which]+=val;
  56. // sv->playerint[hero->getOwner()]->heroPrimarySkillChanged(hero, which, val);
  57. //}
  58. //else if (which==4)
  59. //{
  60. // hero->exp+=val;
  61. // if(hero->exp >= CGI->heroh->reqExp(hero->level+1)) //new level
  62. // {
  63. // hero->level++;
  64. // std::cout << hero->name <<" got level "<<hero->level<<std::endl;
  65. // int r = rand()%100, pom=0, x=0;
  66. // int std::pair<int,int>::*g = (hero->level>9) ? (&std::pair<int,int>::second) : (&std::pair<int,int>::first);
  67. // for(;x<PRIMARY_SKILLS;x++)
  68. // {
  69. // pom += hero->type->heroClass->primChance[x].*g;
  70. // if(r<pom)
  71. // break;
  72. // }
  73. // std::cout << "Bohater dostaje umiejetnosc pierwszorzedna " << x << " (wynik losowania "<<r<<")"<<std::endl;
  74. // hero->primSkills[x]++;
  75. // //TODO: dac dwie umiejetnosci 2-rzedne to wyboru
  76. // }
  77. // //TODO - powiadomic interfejsy, sprawdzic czy nie ma awansu itp
  78. //}
  79. }
  80. int CScriptCallback::getHeroOwner(int heroID)
  81. {
  82. return gh->gs->map->objects[heroID]->tempOwner;
  83. }
  84. void CScriptCallback::showInfoDialog(InfoWindow *iw)
  85. {
  86. gh->sendToAllClients(iw);
  87. //TODO: upewniac sie ze mozemy to zrzutowac (przy customowych interfejsach cos moze sie kopnac)
  88. //if (player>=0)
  89. //{
  90. // CGameInterface * temp = sv->playerint[player];
  91. // if (temp->human)
  92. // ((CPlayerInterface*)(temp))->showInfoDialog(text,*components);
  93. // return;
  94. //}
  95. //else
  96. //{
  97. // typedef std::pair<const ui8, CGameInterface*> intf;
  98. // BOOST_FOREACH(intf & i, sv->playerint)
  99. // {
  100. // if (i.second->human)
  101. // ((CPlayerInterface*)(i.second))->showInfoDialog(text,*components);
  102. // }
  103. //}
  104. }
  105. void CScriptCallback::showSelDialog(int player, std::string text, std::vector<CSelectableComponent*>*components, IChosen * asker)
  106. {
  107. //CGameInterface * temp = sv->playerint[player];
  108. //if (temp->human)
  109. // ((CPlayerInterface*)(temp))->showSelDialog(text,*components,(int)asker);
  110. return;
  111. }
  112. int CScriptCallback::getSelectedHero()
  113. {
  114. int ret;
  115. //if (LOCPLINT->adventureInt->selection.type == HEROI_TYPE)
  116. // ret = ((CGHeroInstance*)(LOCPLINT->adventureInt->selection.selected))->subID;
  117. //else
  118. // ret = -1;;
  119. return -1;
  120. }
  121. int CScriptCallback::getDate(int mode)
  122. {
  123. return gh->gs->getDate(mode);
  124. }
  125. void CScriptCallback::giveResource(int player, int which, int val)
  126. {
  127. SetResource sr;
  128. sr.player = player;
  129. sr.resid = which;
  130. sr.val = (gh->gs->players[player].resources[which]+val);
  131. gh->sendAndApply(&sr);
  132. }
  133. void CScriptCallback::showCompInfo(int player, SComponent * comp)
  134. {
  135. //CPlayerInterface * i = dynamic_cast<CPlayerInterface*>(sv->playerint[player]);
  136. //if(i)
  137. // i->showComp(*comp);
  138. }
  139. void CScriptCallback::heroVisitCastle(int obj, int heroID)
  140. {
  141. //CGTownInstance * n;
  142. //if(n = dynamic_cast<CGTownInstance*>(ob))
  143. //{
  144. // n->visitingHero = CGI->state->map->getHero(heroID,0);
  145. // gh->gs->map->getHero(heroID,0)->visitedTown = n;
  146. // sv->playerint[getHeroOwner(heroID)]->heroVisitsTown(CGI->state->map->getHero(heroID,0),n);
  147. //}
  148. //else
  149. // return;
  150. }
  151. void CScriptCallback::stopHeroVisitCastle(int obj, int heroID)
  152. {
  153. //CGTownInstance * n;
  154. //if(n = dynamic_cast<CGTownInstance*>(ob))
  155. //{
  156. // CGI->state->map->getHero(heroID,0)->visitedTown = NULL;
  157. // if(n->visitingHero && n->visitingHero->type->ID == heroID)
  158. // n->visitingHero = NULL;
  159. // return;
  160. //}
  161. //else
  162. // return;
  163. }
  164. void CScriptCallback::giveHeroArtifact(int artid, int hid, int position) //pos==-1 - first free slot in backpack
  165. {
  166. CGHeroInstance* h = gh->gs->map->getHero(hid,0);
  167. if(position<0)
  168. {
  169. for(unsigned i=0;i<h->artifacts.size();i++)
  170. {
  171. if(!h->artifacts[i])
  172. {
  173. h->artifacts[i] = artid;
  174. return;
  175. }
  176. }
  177. h->artifacts.push_back(artid);
  178. return;
  179. }
  180. else
  181. {
  182. if(h->artifWorn[position]) //slot is occupied
  183. {
  184. giveHeroArtifact(h->artifWorn[position],hid,-1);
  185. }
  186. h->artifWorn[position] = artid;
  187. }
  188. }
  189. void CScriptCallback::startBattle(const CCreatureSet * army1, const CCreatureSet * army2, int3 tile, const CGHeroInstance *hero1, const CGHeroInstance *hero2) //use hero=NULL for no hero
  190. {
  191. //gh->gs->battle(army1,army2,tile,hero1,hero2);
  192. }
  193. void CScriptCallback::startBattle(int heroID, CCreatureSet * army, int3 tile) //for hero<=>neutral army
  194. {
  195. //CGHeroInstance* h = gh->gs->map->getHero(heroID,0);
  196. //gh->gs->battle(&h->army,army,tile,h,NULL);
  197. }
  198. void CLuaCallback::registerFuncs(lua_State * L)
  199. {
  200. // lua_newtable(L);
  201. //
  202. //#define REGISTER_C_FUNC(x) \
  203. // lua_pushstring(L, #x); \
  204. // lua_pushcfunction(L, x); \
  205. // lua_rawset(L, -3)
  206. //
  207. // REGISTER_C_FUNC(getPos);
  208. // REGISTER_C_FUNC(changePrimSkill);
  209. // REGISTER_C_FUNC(getGnrlText);
  210. // REGISTER_C_FUNC(getSelectedHero);
  211. //
  212. // lua_setglobal(L, "vcmi");
  213. // #undef REGISTER_C_FUNC
  214. }
  215. int CLuaCallback::getPos(lua_State * L)//(CGObjectInstance * object);
  216. {
  217. //const int args = lua_gettop(L); // number of arguments
  218. //if ((args < 1) || !lua_isnumber(L, 1) )
  219. // luaL_error(L,
  220. // "Incorrect arguments to getPos([Object address])");
  221. //CGObjectInstance * object = (CGObjectInstance *)(lua_tointeger(L, 1));
  222. //lua_pushinteger(L,object->pos.x);
  223. //lua_pushinteger(L,object->pos.y);
  224. //lua_pushinteger(L,object->pos.z);
  225. return 3;
  226. }
  227. int CLuaCallback::changePrimSkill(lua_State * L)//(int ID, int which, int val);
  228. {
  229. //const int args = lua_gettop(L); // number of arguments
  230. //if ((args < 1) || !lua_isnumber(L, 1) ||
  231. // ((args >= 2) && !lua_isnumber(L, 2)) ||
  232. // ((args >= 3) && !lua_isnumber(L, 3)) )
  233. //{
  234. // luaL_error(L,
  235. // "Incorrect arguments to changePrimSkill([Hero ID], [Which Primary skill], [Change by])");
  236. //}
  237. //int ID = lua_tointeger(L, 1),
  238. // which = lua_tointeger(L, 2),
  239. // val = lua_tointeger(L, 3);
  240. //CScriptCallback::changePrimSkill(ID,which,val);
  241. return 0;
  242. }
  243. int CLuaCallback::getGnrlText(lua_State * L) //(int which),returns string
  244. {
  245. //const int args = lua_gettop(L); // number of arguments
  246. //if ((args < 1) || !lua_isnumber(L, 1) )
  247. // luaL_error(L,
  248. // "Incorrect arguments to getGnrlText([Text ID])");
  249. //int which = lua_tointeger(L,1);
  250. //lua_pushstring(L,CGI->generaltexth->allTexts[which].c_str());
  251. return 1;
  252. }
  253. int CLuaCallback::getSelectedHero(lua_State * L) //(),returns int (ID of hero, -1 if no hero is seleceted)
  254. {
  255. //int ret;
  256. //if (LOCPLINT->adventureInt->selection.type == HEROI_TYPE)
  257. // ret = ((CGHeroInstance*)(LOCPLINT->adventureInt->selection.selected))->subID;
  258. //else
  259. // ret = -1;
  260. //lua_pushinteger(L,ret);
  261. return 1;
  262. }