CLua.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879
  1. #include "stdafx.h"
  2. #include <sstream>
  3. #include <boost/bind.hpp>
  4. #include <boost/function.hpp>
  5. #include <boost/algorithm/string.hpp>
  6. #include <boost/algorithm/string/replace.hpp>
  7. #include "hch/CHeroHandler.h"
  8. #include "hch/CObjectHandler.h"
  9. #include "hch/CTownHandler.h"
  10. #include "hch/CArtHandler.h"
  11. #include "hch/CDefObjInfoHandler.h"
  12. //#include "lua.h"
  13. //#include "lualib.h"
  14. //#include "lauxlib.h"
  15. //#include "lobject.h"
  16. //#include "lgc.h"
  17. //#include "lapi.h"
  18. #include "CLua.h"
  19. #include "CGameState.h"
  20. #include "lib/VCMI_Lib.h"
  21. #include "map.h"
  22. #include "server/CScriptCallback.h"
  23. #include "lib/NetPacks.h"
  24. #pragma warning (disable : 4311)
  25. #define DEFOS const CGObjectInstance *os = cb->getObj(objid)
  26. bool getGlobalFunc(lua_State * L, std::string fname)
  27. {
  28. //unsigned int hash = lua_calchash(fname.c_str(), fname.size());
  29. //lua_pushhstring(L, hash, fname.c_str(), fname.size());
  30. //lua_gettable(L, LUA_GLOBALSINDEX);
  31. //return lua_isfunction(L, -1);
  32. return false;
  33. }
  34. CObjectScript::CObjectScript()
  35. {
  36. language = UNDEF;
  37. //std::cout << "Tworze obiekt objectscript "<<this<<std::endl;
  38. }
  39. CObjectScript::~CObjectScript()
  40. {
  41. //std::cout << "Usuwam obiekt objectscript "<<this<<std::endl;
  42. }
  43. CScript::CScript()
  44. {
  45. //std::cout << "Tworze obiekt CScript "<<this<<std::endl;
  46. }
  47. CScript::~CScript()
  48. {
  49. //std::cout << "Usuwam obiekt CScript "<<this<<std::endl;
  50. }
  51. #define LST (is)
  52. CLua::CLua(std::string initpath)
  53. {
  54. opened=false;
  55. open(initpath);
  56. }
  57. CLua::CLua()
  58. {
  59. //std::cout << "Tworze obiekt clua "<<this<<std::endl;
  60. opened=false;
  61. }
  62. void CLua::open(std::string initpath)
  63. {
  64. //LST = lua_open();
  65. //opened = true;
  66. //LUA_OPEN_LIB(LST, luaopen_base);
  67. //LUA_OPEN_LIB(LST, luaopen_io);
  68. //if ((luaL_loadfile (LST, initpath.c_str())) == 0)
  69. //{
  70. // lua_pcall (LST, 0, LUA_MULTRET, 0);
  71. //}
  72. //else
  73. //{
  74. // std::string temp = "Cannot open script ";
  75. // temp += initpath;
  76. // throw std::exception(temp.c_str());
  77. //}
  78. }
  79. void CLua::registerCLuaCallback()
  80. {
  81. }
  82. CLua::~CLua()
  83. {
  84. ////std::cout << "Usuwam obiekt clua "<<this<<std::endl;
  85. //if (opened)
  86. //{
  87. // std::cout << "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"<<std::endl;
  88. // lua_close(LST);
  89. //}
  90. }
  91. void CLua::findF(std::string fname)
  92. {
  93. // lua_getfield(is, LUA_GLOBALSINDEX, fname.c_str()); /* function to be called */
  94. }
  95. void CLua::findF2(std::string fname)
  96. {
  97. //lua_pushstring (is, fname.c_str());
  98. //lua_gettable (is, LUA_GLOBALSINDEX);
  99. }
  100. void CLua::findFS(std::string fname)
  101. {
  102. //lua_settop(is, 0);
  103. //if (!getGlobalFunc(is,fname))
  104. //{
  105. // lua_settop(is, 0);
  106. // throw new std::exception((fname + ": function not defined").c_str()); // the call is not defined
  107. //}
  108. }
  109. #undef LST
  110. CLuaObjectScript::CLuaObjectScript(std::string filename)
  111. {
  112. language = LUA;
  113. open(filename);
  114. //binit = bnewobject = bonherovisit = brightext = false;
  115. //std::cout << "Tworze obiekt CLuaObjectScript "<<this<<std::endl;
  116. }
  117. CLuaObjectScript::~CLuaObjectScript()
  118. {
  119. //std::cout << "Usuwam obiekt CLuaObjectScript "<<this<<std::endl;
  120. }
  121. void CLuaObjectScript::init()
  122. {
  123. }
  124. std::string CLuaObjectScript::genFN(std::string base, int ID)
  125. {
  126. std::stringstream sts;
  127. sts<<base<<"_"<<ID;
  128. return sts.str();
  129. }
  130. void CLuaObjectScript::newObject(int objid)
  131. {
  132. //findF(genFN("newObject",os->ID));
  133. //lua_pushinteger(is, (int)os);
  134. //if (lua_pcall (is, 1, 0, 0))
  135. //{
  136. // lua_settop(is, 0);
  137. // throw new std::exception(("Failed to call "+genFN("newObject",os->ID)+" function in lua script.").c_str());
  138. //}
  139. //lua_settop(is, 0);
  140. return;
  141. }
  142. void CLuaObjectScript::onHeroVisit(int objid, int heroID)
  143. {
  144. //findF(genFN("heroVisit",os->ID));
  145. //lua_pushinteger(is, (int)os);
  146. //lua_pushinteger(is, heroID);
  147. //if (lua_pcall (is, 2, 0, 0))
  148. //{
  149. // lua_settop(is, 0);
  150. // throw new std::exception(("Failed to call "+genFN("heroVisit",os->ID)+" function in lua script.").c_str());
  151. //}
  152. //lua_settop(is, 0);
  153. }
  154. //std::string CLuaObjectScript::hoverText(int objid)
  155. //{
  156. // //findF(genFN("hoverText",os->ID));
  157. // //lua_pushinteger(is, (int)os);
  158. // //if (lua_pcall (is, 1, 1, 0))
  159. // //{
  160. // // lua_settop(is, 0);
  161. // // throw new std::exception(("Failed to call "+genFN("hoverText",os->ID)+" function in lua script.").c_str());
  162. // //}
  163. // //std::string ret = lua_tostring(is,1);
  164. // //lua_settop(is, 0);
  165. // return "";
  166. //}
  167. void CVisitableOPH::newObject(int objid)
  168. {
  169. visitors.insert
  170. (std::pair<int,std::set<int> >(objid,std::set<int>()));
  171. DEFOS;
  172. MetaString hovername;
  173. int pom;
  174. switch(os->ID)
  175. {
  176. case 51:
  177. pom = 8;
  178. break;
  179. case 23:
  180. pom = 7;
  181. break;
  182. case 61:
  183. pom = 11;
  184. break;
  185. case 32:
  186. pom = 4;
  187. break;
  188. case 100:
  189. pom = 5;
  190. break;
  191. case 102:
  192. typeOfTree[objid] = rand()%3;
  193. return;
  194. default:
  195. std::cout << "Unsupported ID in CVisitableOPH::hoverText" << std::endl;
  196. return;
  197. }
  198. hovername << std::pair<ui8,ui32>(3,os->ID) << " " << std::pair<ui8,ui32>(2,pom);
  199. cb->setHoverName(objid,&hovername);
  200. //int heroID = cb->getSelectedHero();
  201. //if (heroID>=0)
  202. //{
  203. //add += ( (visitors[os].find(heroID) == visitors[os].end())
  204. // ?
  205. // (VLC->generaltexth->allTexts[353]) //not visited
  206. // :
  207. // ( VLC->generaltexth->allTexts[352]) ); //visited
  208. //}
  209. };
  210. void CVisitableOPH::onHeroVisit(int objid, int heroID)
  211. {
  212. DEFOS;
  213. if (visitors.find(objid)!=visitors.end())
  214. {
  215. if(visitors[objid].find(heroID)==visitors[objid].end())
  216. {
  217. onNAHeroVisit(objid,heroID, false);
  218. if(os->ID != 102)
  219. visitors[objid].insert(heroID);
  220. }
  221. else
  222. {
  223. onNAHeroVisit(objid,heroID, true);
  224. }
  225. }
  226. else
  227. {
  228. #ifndef __GNUC__
  229. throw new std::exception("Skrypt nie zainicjalizowal instancji tego obiektu. :(");
  230. #else
  231. throw new std::exception();
  232. #endif
  233. }
  234. };
  235. void CVisitableOPH::onNAHeroVisit(int objid, int heroID, bool alreadyVisited)
  236. {
  237. const CGObjectInstance *os = cb->getObj(objid);
  238. int id=0, subid=0, ot=0, val=1;
  239. switch(os->ID)
  240. {
  241. case 51:
  242. subid=0;
  243. ot=80;
  244. break;
  245. case 23:
  246. subid=1;
  247. ot=39;
  248. break;
  249. case 61:
  250. subid=2;
  251. ot=100;
  252. break;
  253. case 32:
  254. subid=3;
  255. ot=59;
  256. break;
  257. case 100:
  258. id=5;
  259. ot=143;
  260. val=1000;
  261. break;
  262. case 102:
  263. id = 5;
  264. subid = 1;
  265. ot = 146;
  266. val = 1;
  267. break;
  268. }
  269. if (!alreadyVisited)
  270. {
  271. switch (os->ID)
  272. {
  273. case 51:
  274. case 23:
  275. case 61:
  276. case 32:
  277. {
  278. cb->changePrimSkill(heroID,subid,val);
  279. InfoWindow iw;
  280. iw.components.push_back(Component(0,subid,val,0));
  281. iw.text << std::pair<ui8,ui32>(11,ot);
  282. iw.player = cb->getOwner(heroID);
  283. cb->showInfoDialog(&iw);
  284. break;
  285. }
  286. case 100: //give exp
  287. {
  288. InfoWindow iw;
  289. iw.components.push_back(Component(id,subid,val,0));
  290. iw.player = cb->getOwner(heroID);
  291. iw.text << std::pair<ui8,ui32>(11,ot);
  292. cb->showInfoDialog(&iw);
  293. cb->changePrimSkill(heroID,4,val);
  294. break;
  295. }
  296. case 102:
  297. {
  298. const CGHeroInstance *h = cb->getHero(heroID);
  299. val = VLC->heroh->reqExp(h->level) + VLC->heroh->reqExp(h->level+val);
  300. if(!typeOfTree[objid])
  301. {
  302. visitors[objid].insert(heroID);
  303. InfoWindow iw;
  304. iw.components.push_back(Component(id,subid,1,0));
  305. iw.player = cb->getOwner(heroID);
  306. iw.text << std::pair<ui8,ui32>(11,148);
  307. cb->showInfoDialog(&iw);
  308. cb->changePrimSkill(heroID,4,val);
  309. break;
  310. }
  311. else
  312. {
  313. int res, resval;
  314. if(typeOfTree[objid]==1)
  315. {
  316. res = 6;
  317. resval = 2000;
  318. ot = 149;
  319. }
  320. else
  321. {
  322. res = 5;
  323. resval = 10;
  324. ot = 151;
  325. }
  326. if(cb->getResource(h->tempOwner,res) < resval) //not enough resources
  327. {
  328. ot++;
  329. InfoWindow iw;
  330. iw.player = h->tempOwner;
  331. iw.text << std::pair<ui8,ui32>(11,ot);
  332. cb->showInfoDialog(&iw);
  333. return;
  334. }
  335. YesNoDialog sd;
  336. sd.player = cb->getOwner(heroID);
  337. sd.text << std::pair<ui8,ui32>(11,ot);
  338. sd.components.push_back(Component(id,subid,val,0));
  339. cb->showYesNoDialog(&sd,CFunctionList<void(ui32)>(boost::bind(&CVisitableOPH::treeSelected,this,objid,heroID,res,resval,val,_1)));
  340. }
  341. break;
  342. }
  343. }
  344. }
  345. else
  346. {
  347. ot++;
  348. InfoWindow iw;
  349. iw.player = cb->getOwner(heroID);
  350. iw.text << std::pair<ui8,ui32>(11,ot);
  351. cb->showInfoDialog(&iw);
  352. }
  353. }
  354. std::vector<int> CVisitableOPH::yourObjects()
  355. {
  356. std::vector<int> ret;
  357. ret.push_back(51);//camp
  358. ret.push_back(23);//tower
  359. ret.push_back(61);//axis
  360. ret.push_back(32);//garden
  361. ret.push_back(100);//stone
  362. ret.push_back(102);//tree
  363. return ret;
  364. }
  365. void CVisitableOPH::treeSelected( int objid, int heroID, int resType, int resVal, int expVal, ui32 result )
  366. {
  367. if(result==0) //player agreed to give res for exp
  368. {
  369. cb->giveResource(cb->getOwner(heroID),resType,-resVal); //take resource
  370. cb->changePrimSkill(heroID,4,expVal); //give exp
  371. visitors[objid].insert(heroID); //set state to visited
  372. }
  373. }
  374. void CVisitableOPW::onNAHeroVisit(int objid, int heroID, bool alreadyVisited)
  375. {
  376. DEFOS;
  377. int mid;
  378. switch (os->ID)
  379. {
  380. case 55:
  381. mid = 92;
  382. break;
  383. case 112:
  384. mid = 170;
  385. break;
  386. case 109:
  387. mid = 164;
  388. break;
  389. }
  390. if (alreadyVisited)
  391. {
  392. if (os->ID!=112)
  393. mid++;
  394. else
  395. mid--;
  396. InfoWindow iw;
  397. iw.player = cb->getHero(heroID)->tempOwner;
  398. iw.text << std::pair<ui8,ui32>(11,mid);
  399. cb->showInfoDialog(&iw);
  400. }
  401. else
  402. {
  403. int type, sub, val;
  404. type = 2;
  405. switch (os->ID)
  406. {
  407. case 55:
  408. if (rand()%2)
  409. {
  410. sub = 5;
  411. val = 5;
  412. }
  413. else
  414. {
  415. sub = 6;
  416. val = 500;
  417. }
  418. break;
  419. case 112:
  420. mid = 170;
  421. sub = (rand() % 5) + 1;
  422. val = (rand() % 4) + 3;
  423. break;
  424. case 109:
  425. mid = 164;
  426. sub = 6;
  427. if(cb->getDate(2)<2)
  428. val = 500;
  429. else
  430. val = 1000;
  431. }
  432. int player = cb->getOwner(heroID);
  433. cb->giveResource(player,sub,val);
  434. InfoWindow iw;
  435. iw.player = player;
  436. iw.components.push_back(Component(type,sub,val,0));
  437. iw.text << std::pair<ui8,ui32>(11,mid);
  438. cb->showInfoDialog(&iw);
  439. visited[objid] = true;
  440. MetaString ms; //set text to "visited"
  441. ms << std::pair<ui8,ui32>(3,os->ID) << " " << std::pair<ui8,ui32>(1,352);
  442. cb->setHoverName(objid,&ms);
  443. }
  444. }
  445. void CVisitableOPW::newTurn ()
  446. {
  447. if (cb->getDate(1)==1) //first day of week
  448. {
  449. for (std::map<int,bool>::iterator i = visited.begin(); i != visited.end(); i++)
  450. {
  451. (*i).second = false;
  452. MetaString ms; //set text to "not visited"
  453. ms << std::pair<ui8,ui32>(3,cb->getObj(i->first)->ID) << " " << std::pair<ui8,ui32>(1,353);
  454. cb->setHoverName(i->first,&ms);
  455. }
  456. }
  457. }
  458. void CVisitableOPW::newObject(int objid)
  459. {
  460. visited.insert(std::pair<int,bool>(objid,false));
  461. DEFOS;
  462. MetaString ms;
  463. ms << std::pair<ui8,ui32>(3,os->ID) << " " << std::pair<ui8,ui32>(1,visited[objid] ? 352 : 353);
  464. cb->setHoverName(objid,&ms);
  465. }
  466. void CVisitableOPW::onHeroVisit(int objid, int heroID)
  467. {
  468. if(visited[objid])
  469. onNAHeroVisit(objid,heroID,true);
  470. else
  471. onNAHeroVisit(objid,heroID,false);
  472. }
  473. std::vector<int> CVisitableOPW::yourObjects() //returns IDs of objects which are handled by script
  474. {
  475. std::vector<int> ret(3);
  476. ret.push_back(55); //mystical garden
  477. ret.push_back(112); //windmill
  478. ret.push_back(109); //water wheel
  479. return ret;
  480. }
  481. void CMines::newObject(int objid)
  482. {
  483. ourObjs.push_back(objid);
  484. cb->setOwner(objid,NEUTRAL_PLAYER);
  485. DEFOS;
  486. MetaString ms;
  487. ms << std::pair<ui8,ui32>(3,os->ID);
  488. cb->setHoverName(objid,&ms);
  489. }
  490. void CMines::onHeroVisit(int objid, int heroID)
  491. {
  492. //TODO: this is code for standard mines, no support for abandoned mine (subId==7)
  493. DEFOS;
  494. if(os->subID==7)
  495. return;
  496. const CGHeroInstance *h = cb->getHero(heroID);
  497. if(h->tempOwner == os->tempOwner)
  498. return; //TODO: leaving garrison
  499. cb->setOwner(objid,h->tempOwner);
  500. MetaString ms;
  501. ms << std::pair<ui8,ui32>(9,os->subID) << " " << std::pair<ui8,ui32>(6,23+h->tempOwner);
  502. cb->setHoverName(objid,&ms);
  503. ms.clear();
  504. int vv=1; //amount of resource per turn
  505. if (os->subID==0 || os->subID==2)
  506. vv++;
  507. else if (os->subID==6)
  508. vv = 1000;
  509. InfoWindow iw;
  510. iw.text << std::pair<ui8,ui32>(10,os->subID);
  511. iw.player = h->tempOwner;
  512. iw.components.push_back(Component(2,os->subID,vv,-1));
  513. cb->showInfoDialog(&iw);
  514. }
  515. std::vector<int> CMines::yourObjects()
  516. {
  517. std::vector<int> ret;
  518. ret.push_back(53);
  519. return ret;
  520. }
  521. void CMines::newTurn ()
  522. {
  523. const CGObjectInstance * obj;
  524. for (unsigned i=0;i<ourObjs.size();i++)
  525. {
  526. obj = cb->getObj(ourObjs[i]);
  527. if (obj->tempOwner == NEUTRAL_PLAYER)
  528. continue;
  529. int vv = 1;
  530. if (obj->subID==0 || obj->subID==2)
  531. vv++;
  532. else if (obj->subID==6)
  533. vv = 1000;
  534. cb->giveResource(obj->tempOwner,obj->subID,vv);
  535. }
  536. }
  537. void CPickable::newObject(int objid)
  538. {
  539. cb->setBlockVis(objid,true);
  540. MetaString ms;
  541. DEFOS;
  542. switch (os->ID)
  543. {
  544. case 79:
  545. ms << std::pair<ui8,ui32>(4,os->subID);
  546. break;
  547. case 5:
  548. ms << std::pair<ui8,ui32>(5,os->subID);
  549. break;
  550. default:
  551. ms << std::pair<ui8,ui32>(3,os->ID);
  552. break;
  553. }
  554. cb->setHoverName(objid,&ms);
  555. }
  556. void CPickable::onHeroVisit(int objid, int heroID)
  557. {
  558. DEFOS;
  559. switch(os->ID)
  560. {
  561. case 5: //artifact
  562. {
  563. cb->giveHeroArtifact(os->subID,heroID,-1); //TODO: na pozycje
  564. InfoWindow iw;
  565. iw.player = cb->getOwner(heroID);
  566. iw.components.push_back(Component(4,os->subID,0,0));
  567. iw.text << std::pair<ui8,ui32>(12,os->subID);
  568. cb->showInfoDialog(&iw);
  569. break;
  570. }
  571. case 12: //campfire
  572. {
  573. int val = (rand()%3) + 4, //4 - 6
  574. res = rand()%6,
  575. owner = cb->getOwner(heroID);
  576. cb->giveResource(owner,res,val); //non-gold resource
  577. cb->giveResource(owner,6,val*100);//gold
  578. InfoWindow iw;
  579. iw.player = owner;
  580. iw.components.push_back(Component(2,6,val*100,0));
  581. iw.components.push_back(Component(2,res,val,0));
  582. iw.text << std::pair<ui8,ui32>(11,23);
  583. cb->showInfoDialog(&iw);
  584. break;
  585. }
  586. case 79: //resource
  587. {
  588. //TODO: handle guards (when battles are finished)
  589. CResourceObjInfo * t2 = static_cast<CResourceObjInfo *>(os->info);
  590. int val;
  591. if(t2->amount)
  592. val = t2->amount;
  593. else
  594. {
  595. switch(os->subID)
  596. {
  597. case 6:
  598. val = 500 + (rand()%6)*100;
  599. break;
  600. case 0: case 2:
  601. val = 6 + (rand()%5);
  602. break;
  603. default:
  604. val = 3 + (rand()%3);
  605. break;
  606. }
  607. }
  608. if(t2->message.length())
  609. {
  610. InfoWindow iw;
  611. iw.player = cb->getHero(heroID)->tempOwner;
  612. iw.text << t2->message;
  613. cb->showInfoDialog(&iw);
  614. }
  615. cb->giveResource(cb->getOwner(heroID),os->subID,val);
  616. ShowInInfobox sii;
  617. sii.player = cb->getOwner(heroID);
  618. sii.c = Component(2,os->subID,val,0);
  619. sii.text << std::pair<ui8,ui32>(11,113);
  620. sii.text.replacements.push_back(VLC->objh->restypes[os->subID]);
  621. cb->showCompInfo(&sii);
  622. break;
  623. }
  624. case 101: //treasure chest
  625. {
  626. if (os->subID) //not OH3 treasure chest
  627. break;
  628. int wyn = rand()%100, val=0;
  629. if (wyn<32) //1k/0.5k
  630. {
  631. val = 1000;
  632. }
  633. else if(wyn<64) //1.5k/1k
  634. {
  635. val = 1500;
  636. }
  637. else if(wyn<95) //2k/1.5k
  638. {
  639. val = 2000;
  640. }
  641. else //random treasure artifact, or (if backapack is full) 1k/0.5k
  642. {
  643. if (1/*TODO: backpack is full*/)
  644. {
  645. val = 1000;
  646. }
  647. else
  648. {
  649. //TODO: give treasure artifact
  650. break;
  651. }
  652. }
  653. SelectionDialog sd;
  654. sd.player = cb->getOwner(heroID);
  655. sd.text << std::pair<ui8,ui32>(11,146);
  656. sd.components.push_back(Component(2,6,val,0));
  657. sd.components.push_back(Component(5,0,val-500,0));
  658. boost::function<void(ui32)> fun = boost::bind(&CPickable::chosen,this,_1,heroID,val);
  659. cb->showSelectionDialog(&sd,fun);
  660. break;
  661. }
  662. }
  663. cb->removeObject(objid);
  664. }
  665. void CPickable::chosen(ui32 which, int heroid, int val)
  666. {
  667. switch(which)
  668. {
  669. case 0: //player pick gold
  670. cb->giveResource(cb->getOwner(heroid),6,val);
  671. break;
  672. case 1: //player pick exp
  673. cb->changePrimSkill(heroid, 4, val-500);
  674. break;
  675. default:
  676. throw std::string("Unhandled choice");
  677. }
  678. }
  679. std::vector<int> CPickable::yourObjects() //returns IDs of objects which are handled by script
  680. {
  681. std::vector<int> ret;
  682. ret.push_back(79); //resource
  683. ret.push_back(5); //artifact
  684. ret.push_back(12); //resource
  685. ret.push_back(101); //treasure chest / commander stone
  686. return ret;
  687. }
  688. void CTownScript::onHeroVisit(int objid, int heroID)
  689. {
  690. DEFOS;
  691. if(cb->getOwner(objid)!=cb->getOwner(heroID))
  692. {
  693. return;
  694. }
  695. cb->heroVisitCastle(objid,heroID);
  696. }
  697. void CTownScript::newObject(int objid)
  698. {
  699. MetaString ms;
  700. const CGTownInstance * n = cb->getTown(objid);
  701. ms << n->name << ", " << n->town->name;
  702. cb->setHoverName(objid,&ms);
  703. }
  704. void CTownScript::onHeroLeave(int objid, int heroID)
  705. {
  706. cb->stopHeroVisitCastle(objid,heroID);
  707. }
  708. std::vector<int> CTownScript::yourObjects() //returns IDs of objects which are handled by script
  709. {
  710. std::vector<int> ret(1);
  711. ret.push_back(98); //town
  712. return ret;
  713. }
  714. void CHeroScript::newObject(int objid)
  715. {
  716. cb->setBlockVis(objid,true);
  717. MetaString ms;
  718. ms << std::pair<ui8,ui32>(1,15);
  719. ms.replacements.push_back(cb->getHero(objid)->name);
  720. ms.replacements.push_back(cb->getHero(objid)->type->heroClass->name);
  721. cb->setHoverName(objid,&ms);
  722. }
  723. void CHeroScript::onHeroVisit(int objid, int heroID)
  724. {
  725. //TODO: check for allies
  726. const CGHeroInstance *my = cb->getHero(objid),
  727. *vis = cb->getHero(heroID);
  728. if(my->tempOwner == vis->tempOwner) //one of allied cases
  729. {
  730. //exchange
  731. }
  732. else
  733. {
  734. cb->startBattle(
  735. &vis->army,
  736. &my->army,
  737. vis->pos,
  738. vis,
  739. my,
  740. 0);
  741. }
  742. }
  743. std::vector<int> CHeroScript::yourObjects() //returns IDs of objects which are handled by script
  744. {
  745. std::vector<int> ret;
  746. ret.push_back(34); //hero
  747. return ret;
  748. }
  749. void CMonsterS::newObject(int objid)
  750. {
  751. //os->blockVisit = true;
  752. DEFOS;
  753. switch(VLC->creh->creatures[os->subID].level)
  754. {
  755. case 1:
  756. cb->setAmount(objid,rand()%31+20);
  757. break;
  758. case 2:
  759. cb->setAmount(objid,rand()%16+15);
  760. break;
  761. case 3:
  762. cb->setAmount(objid,rand()%16+10);
  763. break;
  764. case 4:
  765. cb->setAmount(objid,rand()%11+10);
  766. break;
  767. case 5:
  768. cb->setAmount(objid,rand()%9+8);
  769. break;
  770. case 6:
  771. cb->setAmount(objid,rand()%8+5);
  772. break;
  773. case 7:
  774. cb->setAmount(objid,rand()%7+3);
  775. break;
  776. case 8:
  777. cb->setAmount(objid,rand()%4+2);
  778. break;
  779. case 9:
  780. cb->setAmount(objid,rand()%3+2);
  781. break;
  782. case 10:
  783. cb->setAmount(objid,rand()%3+1);
  784. break;
  785. }
  786. MetaString ms;
  787. int pom = CCreature::getQuantityID(((CCreatureObjInfo*)(os->info))->number);
  788. pom = 174 + 3*pom + 1;
  789. ms << std::pair<ui8,ui32>(6,pom) << " " << std::pair<ui8,ui32>(7,os->subID);
  790. cb->setHoverName(objid,&ms);
  791. }
  792. void CMonsterS::onHeroVisit(int objid, int heroID)
  793. {
  794. DEFOS;
  795. CCreatureSet set;
  796. //TODO: zrobic secik w sposob wyrafinowany
  797. set.slots[0] = std::pair<ui32,si32>(os->subID,((CCreatureObjInfo*)(os->info))->number);
  798. cb->startBattle(heroID,set,os->pos,boost::bind(&CMonsterS::endBattleWith,this,os,_1));
  799. }
  800. std::vector<int> CMonsterS::yourObjects() //returns IDs of objects which are handled by script
  801. {
  802. std::vector<int> ret;
  803. ret.push_back(54); //monster
  804. return ret;
  805. }
  806. void CMonsterS::endBattleWith(const CGObjectInstance *monster, BattleResult *result )
  807. {
  808. if(result->winner==0)
  809. {
  810. cb->removeObject(monster->id);
  811. }
  812. else
  813. {
  814. int killedAmount=0;
  815. for(std::set<std::pair<ui32,si32> >::iterator i=result->casualties[1].begin(); i!=result->casualties[1].end(); i++)
  816. if(i->first == monster->subID)
  817. killedAmount += i->second;
  818. cb->setAmount(monster->id,((CCreatureObjInfo*)(monster->info))->number - killedAmount);
  819. }
  820. }
  821. void CCreatureGen::newObject(int objid)
  822. {
  823. DEFOS;
  824. amount[objid] = VLC->creh->creatures[VLC->objh->cregens[os->subID]].growth;
  825. MetaString ms;
  826. ms << std::pair<ui8,ui32>(8,os->subID);
  827. cb->setHoverName(objid,&ms);
  828. }
  829. void CCreatureGen::onHeroVisit(int objid, int heroID)
  830. {
  831. }
  832. std::vector<int> CCreatureGen::yourObjects() //returns IDs of objects which are handled by script
  833. {
  834. std::vector<int> ret(1);
  835. ret.push_back(17); //cregen1
  836. return ret;
  837. }