CScriptCallback.cpp 8.2 KB

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