CScriptCallback.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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. //CGHeroInstance * hero = CGI->state->map->getHero(heroID,0);
  83. //return hero->getOwner();
  84. return -1;
  85. }
  86. void CScriptCallback::showInfoDialog(InfoWindow *iw)
  87. {
  88. gh->sendToAllClients(iw);
  89. //TODO: upewniac sie ze mozemy to zrzutowac (przy customowych interfejsach cos moze sie kopnac)
  90. //if (player>=0)
  91. //{
  92. // CGameInterface * temp = sv->playerint[player];
  93. // if (temp->human)
  94. // ((CPlayerInterface*)(temp))->showInfoDialog(text,*components);
  95. // return;
  96. //}
  97. //else
  98. //{
  99. // typedef std::pair<const ui8, CGameInterface*> intf;
  100. // BOOST_FOREACH(intf & i, sv->playerint)
  101. // {
  102. // if (i.second->human)
  103. // ((CPlayerInterface*)(i.second))->showInfoDialog(text,*components);
  104. // }
  105. //}
  106. }
  107. void CScriptCallback::showSelDialog(int player, std::string text, std::vector<CSelectableComponent*>*components, IChosen * asker)
  108. {
  109. //CGameInterface * temp = sv->playerint[player];
  110. //if (temp->human)
  111. // ((CPlayerInterface*)(temp))->showSelDialog(text,*components,(int)asker);
  112. return;
  113. }
  114. int CScriptCallback::getSelectedHero()
  115. {
  116. int ret;
  117. //if (LOCPLINT->adventureInt->selection.type == HEROI_TYPE)
  118. // ret = ((CGHeroInstance*)(LOCPLINT->adventureInt->selection.selected))->subID;
  119. //else
  120. // ret = -1;;
  121. return ret;
  122. }
  123. int CScriptCallback::getDate(int mode)
  124. {
  125. int temp;
  126. switch (mode)
  127. {
  128. case 0:
  129. return gh->gs->day;
  130. break;
  131. case 1:
  132. temp = (gh->gs->day)%7;
  133. if (temp)
  134. return temp;
  135. else return 7;
  136. break;
  137. case 2:
  138. temp = ((gh->gs->day-1)/7)+1;
  139. if (!(temp%4))
  140. return 4;
  141. else
  142. return (temp%4);
  143. break;
  144. case 3:
  145. return ((gh->gs->day-1)/28)+1;
  146. break;
  147. }
  148. return 0;
  149. }
  150. void CScriptCallback::giveResource(int player, int which, int val)
  151. {
  152. SetResource sr;
  153. sr.player = player;
  154. sr.resid = which;
  155. sr.val = (gh->gs->players[player].resources[which]+val);
  156. gh->sendAndApply(&sr);
  157. //gh->gs->players[player].resources[which]+=val;
  158. //sv->playerint[player]->receivedResource(which,val);
  159. }
  160. void CScriptCallback::showCompInfo(int player, SComponent * comp)
  161. {
  162. //CPlayerInterface * i = dynamic_cast<CPlayerInterface*>(sv->playerint[player]);
  163. //if(i)
  164. // i->showComp(*comp);
  165. }
  166. void CScriptCallback::heroVisitCastle(int obj, int heroID)
  167. {
  168. //CGTownInstance * n;
  169. //if(n = dynamic_cast<CGTownInstance*>(ob))
  170. //{
  171. // n->visitingHero = CGI->state->map->getHero(heroID,0);
  172. // gh->gs->map->getHero(heroID,0)->visitedTown = n;
  173. // sv->playerint[getHeroOwner(heroID)]->heroVisitsTown(CGI->state->map->getHero(heroID,0),n);
  174. //}
  175. //else
  176. // return;
  177. }
  178. void CScriptCallback::stopHeroVisitCastle(int obj, int heroID)
  179. {
  180. //CGTownInstance * n;
  181. //if(n = dynamic_cast<CGTownInstance*>(ob))
  182. //{
  183. // CGI->state->map->getHero(heroID,0)->visitedTown = NULL;
  184. // if(n->visitingHero && n->visitingHero->type->ID == heroID)
  185. // n->visitingHero = NULL;
  186. // return;
  187. //}
  188. //else
  189. // return;
  190. }
  191. void CScriptCallback::giveHeroArtifact(int artid, int hid, int position) //pos==-1 - first free slot in backpack
  192. {
  193. CGHeroInstance* h = gh->gs->map->getHero(hid,0);
  194. if(position<0)
  195. {
  196. for(unsigned i=0;i<h->artifacts.size();i++)
  197. {
  198. if(!h->artifacts[i])
  199. {
  200. h->artifacts[i] = artid;
  201. return;
  202. }
  203. }
  204. h->artifacts.push_back(artid);
  205. return;
  206. }
  207. else
  208. {
  209. if(h->artifWorn[position]) //slot is occupied
  210. {
  211. giveHeroArtifact(h->artifWorn[position],hid,-1);
  212. }
  213. h->artifWorn[position] = artid;
  214. }
  215. }
  216. void CScriptCallback::startBattle(const CCreatureSet * army1, const CCreatureSet * army2, int3 tile, const CGHeroInstance *hero1, const CGHeroInstance *hero2) //use hero=NULL for no hero
  217. {
  218. //gh->gs->battle(army1,army2,tile,hero1,hero2);
  219. }
  220. void CScriptCallback::startBattle(int heroID, CCreatureSet * army, int3 tile) //for hero<=>neutral army
  221. {
  222. //CGHeroInstance* h = gh->gs->map->getHero(heroID,0);
  223. //gh->gs->battle(&h->army,army,tile,h,NULL);
  224. }
  225. void CLuaCallback::registerFuncs(lua_State * L)
  226. {
  227. // lua_newtable(L);
  228. //
  229. //#define REGISTER_C_FUNC(x) \
  230. // lua_pushstring(L, #x); \
  231. // lua_pushcfunction(L, x); \
  232. // lua_rawset(L, -3)
  233. //
  234. // REGISTER_C_FUNC(getPos);
  235. // REGISTER_C_FUNC(changePrimSkill);
  236. // REGISTER_C_FUNC(getGnrlText);
  237. // REGISTER_C_FUNC(getSelectedHero);
  238. //
  239. // lua_setglobal(L, "vcmi");
  240. // #undef REGISTER_C_FUNC
  241. }
  242. int CLuaCallback::getPos(lua_State * L)//(CGObjectInstance * object);
  243. {
  244. //const int args = lua_gettop(L); // number of arguments
  245. //if ((args < 1) || !lua_isnumber(L, 1) )
  246. // luaL_error(L,
  247. // "Incorrect arguments to getPos([Object address])");
  248. //CGObjectInstance * object = (CGObjectInstance *)(lua_tointeger(L, 1));
  249. //lua_pushinteger(L,object->pos.x);
  250. //lua_pushinteger(L,object->pos.y);
  251. //lua_pushinteger(L,object->pos.z);
  252. return 3;
  253. }
  254. int CLuaCallback::changePrimSkill(lua_State * L)//(int ID, int which, int val);
  255. {
  256. //const int args = lua_gettop(L); // number of arguments
  257. //if ((args < 1) || !lua_isnumber(L, 1) ||
  258. // ((args >= 2) && !lua_isnumber(L, 2)) ||
  259. // ((args >= 3) && !lua_isnumber(L, 3)) )
  260. //{
  261. // luaL_error(L,
  262. // "Incorrect arguments to changePrimSkill([Hero ID], [Which Primary skill], [Change by])");
  263. //}
  264. //int ID = lua_tointeger(L, 1),
  265. // which = lua_tointeger(L, 2),
  266. // val = lua_tointeger(L, 3);
  267. //CScriptCallback::changePrimSkill(ID,which,val);
  268. return 0;
  269. }
  270. int CLuaCallback::getGnrlText(lua_State * L) //(int which),returns string
  271. {
  272. //const int args = lua_gettop(L); // number of arguments
  273. //if ((args < 1) || !lua_isnumber(L, 1) )
  274. // luaL_error(L,
  275. // "Incorrect arguments to getGnrlText([Text ID])");
  276. //int which = lua_tointeger(L,1);
  277. //lua_pushstring(L,CGI->generaltexth->allTexts[which].c_str());
  278. return 1;
  279. }
  280. int CLuaCallback::getSelectedHero(lua_State * L) //(),returns int (ID of hero, -1 if no hero is seleceted)
  281. {
  282. //int ret;
  283. //if (LOCPLINT->adventureInt->selection.type == HEROI_TYPE)
  284. // ret = ((CGHeroInstance*)(LOCPLINT->adventureInt->selection.selected))->subID;
  285. //else
  286. // ret = -1;
  287. //lua_pushinteger(L,ret);
  288. return 1;
  289. }