CLua.cpp 14 KB

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