CLua.cpp 14 KB

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