CLua.cpp 14 KB

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