CLua.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. #include "stdafx.h"
  2. #include "CLua.h"
  3. #include "CLuaHandler.h"
  4. #include "hch/CHeroHandler.h"
  5. #include "lua.h"
  6. #include "lualib.h"
  7. #include "lauxlib.h"
  8. #include "lobject.h"
  9. #include "lgc.h"
  10. #include "lapi.h"
  11. #include "CGameInfo.h"
  12. #include "CGameState.h"
  13. #include <sstream>
  14. #include "hch/CObjectHandler.h"
  15. #include "CCallback.h"
  16. #include "hch/CGeneralTextHandler.h"
  17. #include <sstream>
  18. #include "CPlayerInterface.h"
  19. #pragma warning (disable : 4311)
  20. bool getGlobalFunc(lua_State * L, std::string fname)
  21. {
  22. unsigned int hash = lua_calchash(fname.c_str(), fname.size());
  23. lua_pushhstring(L, hash, fname.c_str(), fname.size());
  24. lua_gettable(L, LUA_GLOBALSINDEX);
  25. return lua_isfunction(L, -1);
  26. }
  27. CObjectScript::CObjectScript()
  28. {
  29. language = ESLan::UNDEF;
  30. //std::cout << "Tworze obiekt objectscript "<<this<<std::endl;
  31. }
  32. CObjectScript::~CObjectScript()
  33. {
  34. //std::cout << "Usuwam obiekt objectscript "<<this<<std::endl;
  35. }
  36. CScript::CScript()
  37. {
  38. //std::cout << "Tworze obiekt CScript "<<this<<std::endl;
  39. }
  40. CScript::~CScript()
  41. {
  42. //std::cout << "Usuwam obiekt CScript "<<this<<std::endl;
  43. }
  44. #define LST (is)
  45. CLua::CLua(std::string initpath)
  46. {
  47. opened=false;
  48. open(initpath);
  49. }
  50. CLua::CLua()
  51. {
  52. //std::cout << "Tworze obiekt clua "<<this<<std::endl;
  53. opened=false;
  54. }
  55. void CLua::open(std::string initpath)
  56. {
  57. LST = lua_open();
  58. opened = true;
  59. LUA_OPEN_LIB(LST, luaopen_base);
  60. LUA_OPEN_LIB(LST, luaopen_io);
  61. if ((luaL_loadfile (LST, initpath.c_str())) == 0)
  62. {
  63. lua_pcall (LST, 0, LUA_MULTRET, 0);
  64. }
  65. else
  66. {
  67. std::string temp = "Cannot open script ";
  68. temp += initpath;
  69. throw std::exception(temp.c_str());
  70. }
  71. }
  72. void CLua::registerCLuaCallback()
  73. {
  74. }
  75. CLua::~CLua()
  76. {
  77. //std::cout << "Usuwam obiekt clua "<<this<<std::endl;
  78. if (opened)
  79. {
  80. std::cout << "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"<<std::endl;
  81. lua_close(LST);
  82. }
  83. }
  84. void CLua::findF(std::string fname)
  85. {
  86. lua_getfield(is, LUA_GLOBALSINDEX, fname.c_str()); /* function to be called */
  87. }
  88. void CLua::findF2(std::string fname)
  89. {
  90. lua_pushstring (is, fname.c_str());
  91. lua_gettable (is, LUA_GLOBALSINDEX);
  92. }
  93. void CLua::findFS(std::string fname)
  94. {
  95. lua_settop(is, 0);
  96. if (!getGlobalFunc(is,fname))
  97. {
  98. lua_settop(is, 0);
  99. throw new std::exception((fname + ": function not defined").c_str()); // the call is not defined
  100. }
  101. }
  102. #undef LST
  103. CLuaObjectScript::CLuaObjectScript(std::string filename)
  104. {
  105. language = ESLan::LUA;
  106. open(filename);
  107. //binit = bnewobject = bonherovisit = brightext = false;
  108. //std::cout << "Tworze obiekt CLuaObjectScript "<<this<<std::endl;
  109. }
  110. CLuaObjectScript::~CLuaObjectScript()
  111. {
  112. //std::cout << "Usuwam obiekt CLuaObjectScript "<<this<<std::endl;
  113. }
  114. void CLuaObjectScript::init()
  115. {
  116. }
  117. std::string CLuaObjectScript::genFN(std::string base, int ID)
  118. {
  119. std::stringstream sts;
  120. sts<<base<<"_"<<ID;
  121. return sts.str();
  122. }
  123. void CLuaObjectScript::newObject(CGObjectInstance *os)
  124. {
  125. findF(genFN("newObject",os->ID));
  126. lua_pushinteger(is, (int)os);
  127. if (lua_pcall (is, 1, 0, 0))
  128. {
  129. lua_settop(is, 0);
  130. throw new std::exception(("Failed to call "+genFN("newObject",os->ID)+" function in lua script.").c_str());
  131. }
  132. lua_settop(is, 0);
  133. return;
  134. }
  135. void CLuaObjectScript::onHeroVisit(CGObjectInstance *os, int heroID)
  136. {
  137. findF(genFN("heroVisit",os->ID));
  138. lua_pushinteger(is, (int)os);
  139. lua_pushinteger(is, heroID);
  140. if (lua_pcall (is, 2, 0, 0))
  141. {
  142. lua_settop(is, 0);
  143. throw new std::exception(("Failed to call "+genFN("heroVisit",os->ID)+" function in lua script.").c_str());
  144. }
  145. lua_settop(is, 0);
  146. }
  147. std::string CLuaObjectScript::hoverText(CGObjectInstance *os)
  148. {
  149. findF(genFN("hoverText",os->ID));
  150. lua_pushinteger(is, (int)os);
  151. if (lua_pcall (is, 1, 1, 0))
  152. {
  153. lua_settop(is, 0);
  154. throw new std::exception(("Failed to call "+genFN("hoverText",os->ID)+" function in lua script.").c_str());
  155. }
  156. std::string ret = lua_tostring(is,1);
  157. lua_settop(is, 0);
  158. return ret;
  159. }
  160. std::string CCPPObjectScript::hoverText(CGObjectInstance *os)
  161. {
  162. return CGI->objh->objects[os->defInfo->id].name;
  163. }
  164. void CVisitableOPH::newObject(CGObjectInstance *os)
  165. {
  166. visitors.insert
  167. (std::pair<CGObjectInstance*,std::set<int> >(os,std::set<int>()));
  168. };
  169. void CVisitableOPH::onHeroVisit(CGObjectInstance *os, int heroID)
  170. {
  171. if (visitors.find(os)!=visitors.end())
  172. {
  173. if(visitors[os].find(heroID)==visitors[os].end())
  174. {
  175. onNAHeroVisit(os,heroID, false);
  176. visitors[os].insert(heroID);
  177. }
  178. else
  179. {
  180. onNAHeroVisit(os,heroID, true);
  181. }
  182. }
  183. else
  184. {
  185. throw new std::exception("Skrypt nie zainicjalizowal instancji tego obiektu. :(");
  186. }
  187. };
  188. void CVisitableOPH::onNAHeroVisit(CGObjectInstance *os, int heroID, bool alreadyVisited)
  189. {
  190. int w=0, ot=0;
  191. switch(os->ID)
  192. {
  193. case 51:
  194. w=0;
  195. ot=80;
  196. break;
  197. case 23:
  198. w=1;
  199. ot=39;
  200. break;
  201. case 61:
  202. w=2;
  203. ot=100;
  204. break;
  205. case 32:
  206. w=3;
  207. ot=59;
  208. break;
  209. }
  210. if (!alreadyVisited)
  211. {
  212. switch (os->ID)
  213. {
  214. case 51:
  215. case 23:
  216. case 61:
  217. case 32:
  218. {
  219. cb->changePrimSkill(heroID,w,1);
  220. std::vector<SComponent*> weko;
  221. weko.push_back(new SComponent(SComponent::primskill,w,1));
  222. cb->showInfoDialog(cb->getHeroOwner(heroID),CGI->objh->advobtxt[ot],&weko); //TODO: maybe we have memory leak with these windows
  223. //for (int ii=0; ii<weko.size();ii++)
  224. // delete weko[ii];
  225. break;
  226. }
  227. }
  228. }
  229. else
  230. {
  231. ot++;
  232. cb->showInfoDialog(cb->getHeroOwner(heroID),CGI->objh->advobtxt[ot],&std::vector<SComponent*>());
  233. }
  234. }
  235. std::vector<int> CVisitableOPH::yourObjects()
  236. {
  237. std::vector<int> ret(4);
  238. ret.push_back(51);
  239. ret.push_back(23);
  240. ret.push_back(61);
  241. ret.push_back(32);
  242. return ret;
  243. }
  244. std::string CVisitableOPH::hoverText(CGObjectInstance *os)
  245. {
  246. std::string add;
  247. int pom;
  248. switch(os->ID)
  249. {
  250. case 51:
  251. pom = 8;
  252. break;
  253. case 23:
  254. pom = 7;
  255. break;
  256. case 61:
  257. pom = 11;
  258. break;
  259. case 32:
  260. pom = 4;
  261. break;
  262. default:
  263. throw new std::exception("Unsupported ID in CVisitableOPH::hoverText");
  264. }
  265. add = " " + CGI->objh->xtrainfo[pom] + " ";
  266. int heroID = cb->getSelectedHero();
  267. if (heroID>=0)
  268. {
  269. add += ( (visitors[os].find(heroID) == visitors[os].end())
  270. ?
  271. (CGI->generaltexth->allTexts[353]) //not visited
  272. :
  273. ( CGI->generaltexth->allTexts[352]) ); //visited
  274. }
  275. return CGI->objh->objects[os->defInfo->id].name + add;
  276. }
  277. void CVisitableOPW::onNAHeroVisit(CGObjectInstance *os, int heroID, bool alreadyVisited)
  278. {
  279. int mid;
  280. switch (os->ID)
  281. {
  282. case 55:
  283. mid = 92;
  284. break;
  285. case 112:
  286. mid = 170;
  287. break;
  288. case 109:
  289. mid = 164;
  290. break;
  291. }
  292. if (alreadyVisited)
  293. {
  294. if (os->ID!=112)
  295. mid++;
  296. else
  297. mid--;
  298. cb->showInfoDialog(cb->getHeroOwner(heroID),CGI->objh->advobtxt[mid],&std::vector<SComponent*>()); //TODO: maybe we have memory leak with these windows
  299. }
  300. else
  301. {
  302. int type, sub, val;
  303. type = SComponent::resource;
  304. switch (os->ID)
  305. {
  306. case 55:
  307. if (rand()%2)
  308. {
  309. sub = 5;
  310. val = 5;
  311. }
  312. else
  313. {
  314. sub = 6;
  315. val = 500;
  316. }
  317. break;
  318. case 112:
  319. mid = 170;
  320. sub = rand() % 6;
  321. val = (rand() % 4) + 3;
  322. break;
  323. case 109:
  324. mid = 164;
  325. sub = 6;
  326. if(cb->getDate(2)<2)
  327. val = 500;
  328. else
  329. val = 1000;
  330. }
  331. SComponent * com = new SComponent((SComponent::Etype)type,sub,val);
  332. std::vector<SComponent*> weko;
  333. weko.push_back(com);
  334. cb->giveResource(cb->getHeroOwner(heroID),sub,val);
  335. cb->showInfoDialog(cb->getHeroOwner(heroID),CGI->objh->advobtxt[mid],&weko);
  336. visited[os] = true;
  337. }
  338. }
  339. void CVisitableOPW::newTurn ()
  340. {
  341. if (cb->getDate(1)==1)
  342. {
  343. for (std::map<CGObjectInstance*,bool>::iterator i = visited.begin(); i != visited.end(); i++)
  344. {
  345. (*i).second = false;
  346. }
  347. }
  348. }
  349. void CVisitableOPW::newObject(CGObjectInstance *os)
  350. {
  351. visited.insert(std::pair<CGObjectInstance*,bool>(os,false));
  352. }
  353. void CVisitableOPW::onHeroVisit(CGObjectInstance *os, int heroID)
  354. {
  355. if(visited[os])
  356. onNAHeroVisit(os,heroID,true);
  357. else
  358. onNAHeroVisit(os,heroID,false);
  359. }
  360. std::vector<int> CVisitableOPW::yourObjects() //returns IDs of objects which are handled by script
  361. {
  362. std::vector<int> ret(3);
  363. ret.push_back(55); //mystical garden
  364. ret.push_back(112); //windmill
  365. ret.push_back(109); //water wheel
  366. return ret;
  367. }
  368. std::string CVisitableOPW::hoverText(CGObjectInstance *os)
  369. {
  370. return CGI->objh->objects[os->defInfo->id].name + " " + ( (visited[os]) ? (CGI->generaltexth->allTexts[352]) : (CGI->generaltexth->allTexts[353])) ;
  371. }
  372. void CMines::newObject(CGObjectInstance *os)
  373. {
  374. ourObjs.push_back(os);
  375. os->tempOwner = NEUTRAL_PLAYER;
  376. }
  377. void CMines::onHeroVisit(CGObjectInstance *os, int heroID)
  378. {
  379. int vv = 1;
  380. if (os->subID==0 || os->subID==2)
  381. vv++;
  382. else if (os->subID==6)
  383. vv = 1000;
  384. if (os->tempOwner == cb->getHeroOwner(heroID))
  385. {
  386. //TODO: garrison
  387. }
  388. else
  389. {
  390. if (os->subID==7)
  391. return; //TODO: support for abandoned mine
  392. os->tempOwner = cb->getHeroOwner(heroID);
  393. SComponent * com = new SComponent(SComponent::Etype::resource,os->subID,vv);
  394. com->subtitle+=CGI->generaltexth->allTexts[3].substr(2,CGI->generaltexth->allTexts[3].length()-2);
  395. std::vector<SComponent*> weko;
  396. weko.push_back(com);
  397. cb->showInfoDialog(cb->getHeroOwner(heroID),CGI->objh->mines[os->subID].second,&weko);
  398. }
  399. }
  400. std::vector<int> CMines::yourObjects()
  401. {
  402. std::vector<int> ret(1);
  403. ret.push_back(53);
  404. return ret;
  405. }
  406. std::string CMines::hoverText(CGObjectInstance *os)
  407. {
  408. if (os->tempOwner == NEUTRAL_PLAYER)
  409. return CGI->objh->mines[os->subID].first;
  410. else
  411. return CGI->objh->mines[os->subID].first + " " + CGI->generaltexth->arraytxt[23+os->tempOwner];
  412. }
  413. void CMines::newTurn ()
  414. {
  415. for (int i=0;i<ourObjs.size();i++)
  416. {
  417. if (ourObjs[i]->tempOwner == NEUTRAL_PLAYER)
  418. continue;
  419. int vv = 1;
  420. if (ourObjs[i]->subID==0 || ourObjs[i]->subID==2)
  421. vv++;
  422. else if (ourObjs[i]->subID==6)
  423. vv = 1000;
  424. cb->giveResource(ourObjs[i]->tempOwner,ourObjs[i]->subID,vv);
  425. }
  426. }
  427. void CPickable::newObject(CGObjectInstance *os)
  428. {
  429. os->blockVisit = true;
  430. }
  431. void CPickable::onHeroVisit(CGObjectInstance *os, int heroID)
  432. {
  433. switch(os->ID)
  434. {
  435. case 79:
  436. {
  437. int val;
  438. switch(os->subID)
  439. {
  440. case 6:
  441. val = 500 + (rand()%6)*100;
  442. break;
  443. case 0: case 2:
  444. val = 6 + (rand()%5);
  445. break;
  446. default:
  447. val = 3 + (rand()%3);
  448. break;
  449. }
  450. cb->giveResource(cb->getHeroOwner(heroID),os->subID,val);
  451. break;
  452. }
  453. }
  454. CGI->mh->removeObject(os);
  455. }
  456. std::string CPickable::hoverText(CGObjectInstance *os)
  457. {
  458. switch (os->ID)
  459. {
  460. case 79:
  461. return CGI->objh->restypes[os->subID];
  462. break;
  463. case 5:
  464. return CGI->arth->artifacts[os->subID].name;
  465. break;
  466. default:
  467. return CGI->objh->objects[os->defInfo->id].name;
  468. break;
  469. }
  470. }
  471. std::vector<int> CPickable::yourObjects() //returns IDs of objects which are handled by script
  472. {
  473. std::vector<int> ret(3);
  474. ret.push_back(79); //resource
  475. ret.push_back(5); //artifact
  476. ret.push_back(101); //treasure chest / commander stone
  477. return ret;
  478. }