2
0

CLua.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835
  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->getHeroOwner(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->getHeroOwner(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->getHeroOwner(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->getHeroOwner(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->getHeroOwner(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->getHeroOwner(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->getHeroOwner(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. const CGHeroInstance *h = cb->getHero(heroID);
  495. if(h->tempOwner == os->tempOwner)
  496. return; //TODO: leaving garrison
  497. cb->setOwner(objid,h->tempOwner);
  498. MetaString ms;
  499. ms << std::pair<ui8,ui32>(9,os->subID) << " " << std::pair<ui8,ui32>(6,23+h->tempOwner);
  500. cb->setHoverName(objid,&ms);
  501. ms.clear();
  502. int vv=1; //amount of resource per turn
  503. if (os->subID==0 || os->subID==2)
  504. vv++;
  505. else if (os->subID==6)
  506. vv = 1000;
  507. InfoWindow iw;
  508. iw.text << std::pair<ui8,ui32>(10,os->subID);
  509. iw.player = h->tempOwner;
  510. iw.components.push_back(Component(2,os->subID,vv,-1));
  511. cb->showInfoDialog(&iw);
  512. }
  513. std::vector<int> CMines::yourObjects()
  514. {
  515. std::vector<int> ret;
  516. ret.push_back(53);
  517. return ret;
  518. }
  519. void CMines::newTurn ()
  520. {
  521. const CGObjectInstance * obj;
  522. for (unsigned i=0;i<ourObjs.size();i++)
  523. {
  524. obj = cb->getObj(ourObjs[i]);
  525. if (obj->tempOwner == NEUTRAL_PLAYER)
  526. continue;
  527. int vv = 1;
  528. if (obj->subID==0 || obj->subID==2)
  529. vv++;
  530. else if (obj->subID==6)
  531. vv = 1000;
  532. cb->giveResource(obj->tempOwner,obj->subID,vv);
  533. }
  534. }
  535. void CPickable::newObject(int objid)
  536. {
  537. cb->setBlockVis(objid,true);
  538. MetaString ms;
  539. DEFOS;
  540. switch (os->ID)
  541. {
  542. case 79:
  543. ms << std::pair<ui8,ui32>(4,os->subID);
  544. break;
  545. case 5:
  546. ms << std::pair<ui8,ui32>(5,os->subID);
  547. break;
  548. default:
  549. ms << std::pair<ui8,ui32>(3,os->ID);
  550. break;
  551. }
  552. cb->setHoverName(objid,&ms);
  553. }
  554. void CPickable::onHeroVisit(int objid, int heroID)
  555. {
  556. DEFOS;
  557. switch(os->ID)
  558. {
  559. case 5: //artifact
  560. {
  561. cb->giveHeroArtifact(os->subID,heroID,-1); //TODO: na pozycje
  562. break;
  563. }
  564. case 79: //resource
  565. {
  566. //TODO: handle guards (when battles are finished)
  567. CResourceObjInfo * t2 = static_cast<CResourceObjInfo *>(os->info);
  568. int val;
  569. if(t2->amount)
  570. val = t2->amount;
  571. else
  572. {
  573. switch(os->subID)
  574. {
  575. case 6:
  576. val = 500 + (rand()%6)*100;
  577. break;
  578. case 0: case 2:
  579. val = 6 + (rand()%5);
  580. break;
  581. default:
  582. val = 3 + (rand()%3);
  583. break;
  584. }
  585. }
  586. if(t2->message.length())
  587. {
  588. InfoWindow iw;
  589. iw.player = cb->getHero(heroID)->tempOwner;
  590. iw.text << t2->message;
  591. cb->showInfoDialog(&iw);
  592. }
  593. cb->giveResource(cb->getHeroOwner(heroID),os->subID,val);
  594. ShowInInfobox sii;
  595. sii.player = cb->getHeroOwner(heroID);
  596. sii.c = Component(2,os->subID,val,0);
  597. sii.text << std::pair<ui8,ui32>(11,113);
  598. sii.text.replacements.push_back(VLC->objh->restypes[os->subID]);
  599. cb->showCompInfo(&sii);
  600. break;
  601. }
  602. case 101: //treasure chest
  603. {
  604. if (os->subID) //not OH3 treasure chest
  605. break;
  606. int wyn = rand()%100, val=0;
  607. if (wyn<32) //1k/0.5k
  608. {
  609. val = 1000;
  610. }
  611. else if(wyn<64) //1.5k/1k
  612. {
  613. val = 1500;
  614. }
  615. else if(wyn<95) //2k/1.5k
  616. {
  617. val = 2000;
  618. }
  619. else //random treasure artifact, or (if backapack is full) 1k/0.5k
  620. {
  621. if (1/*TODO: backpack is full*/)
  622. {
  623. val = 1000;
  624. }
  625. else
  626. {
  627. //TODO: give treasure artifact
  628. break;
  629. }
  630. }
  631. SelectionDialog sd;
  632. sd.player = cb->getHeroOwner(heroID);
  633. sd.text << std::pair<ui8,ui32>(11,146);
  634. sd.components.push_back(Component(2,6,val,0));
  635. sd.components.push_back(Component(5,0,val-500,0));
  636. boost::function<void(ui32)> fun = boost::bind(&CPickable::chosen,this,_1,heroID,val);
  637. cb->showSelectionDialog(&sd,fun);
  638. break;
  639. }
  640. }
  641. cb->removeObject(objid);
  642. }
  643. void CPickable::chosen(ui32 which, int heroid, int val)
  644. {
  645. switch(which)
  646. {
  647. case 0: //player pick gold
  648. cb->giveResource(cb->getHeroOwner(heroid),6,val);
  649. break;
  650. case 1: //player pick exp
  651. cb->changePrimSkill(heroid, 4, val-500);
  652. break;
  653. default:
  654. throw std::string("Unhandled choice");
  655. }
  656. }
  657. std::vector<int> CPickable::yourObjects() //returns IDs of objects which are handled by script
  658. {
  659. std::vector<int> ret;
  660. ret.push_back(79); //resource
  661. ret.push_back(5); //artifact
  662. ret.push_back(101); //treasure chest / commander stone
  663. return ret;
  664. }
  665. void CTownScript::onHeroVisit(int objid, int heroID)
  666. {
  667. cb->heroVisitCastle(objid,heroID);
  668. }
  669. void CTownScript::newObject(int objid)
  670. {
  671. MetaString ms;
  672. const CGTownInstance * n = cb->getTown(objid);
  673. ms << n->name << ", " << n->town->name;
  674. cb->setHoverName(objid,&ms);
  675. }
  676. void CTownScript::onHeroLeave(int objid, int heroID)
  677. {
  678. cb->stopHeroVisitCastle(objid,heroID);
  679. }
  680. std::vector<int> CTownScript::yourObjects() //returns IDs of objects which are handled by script
  681. {
  682. std::vector<int> ret(1);
  683. ret.push_back(98); //town
  684. return ret;
  685. }
  686. void CHeroScript::newObject(int objid)
  687. {
  688. cb->setBlockVis(objid,true);
  689. MetaString ms;
  690. ms << std::pair<ui8,ui32>(1,15);
  691. ms.replacements.push_back(cb->getHero(objid)->name);
  692. ms.replacements.push_back(cb->getHero(objid)->type->heroClass->name);
  693. cb->setHoverName(objid,&ms);
  694. }
  695. void CHeroScript::onHeroVisit(int objid, int heroID)
  696. {
  697. //TODO: check for allies
  698. const CGHeroInstance *my = cb->getHero(objid),
  699. *vis = cb->getHero(objid);
  700. if(my->tempOwner == vis->tempOwner) //one of allied cases
  701. {
  702. //exchange
  703. }
  704. else
  705. {
  706. cb->startBattle(
  707. &my->army,
  708. &vis->army,
  709. my->pos,
  710. my,
  711. vis);
  712. }
  713. }
  714. std::vector<int> CHeroScript::yourObjects() //returns IDs of objects which are handled by script
  715. {
  716. std::vector<int> ret;
  717. ret.push_back(34); //hero
  718. return ret;
  719. }
  720. void CMonsterS::newObject(int objid)
  721. {
  722. //os->blockVisit = true;
  723. DEFOS;
  724. switch(VLC->creh->creatures[os->subID].level)
  725. {
  726. case 1:
  727. amounts[objid] = rand()%31+20;
  728. break;
  729. case 2:
  730. amounts[objid] = rand()%16+15;
  731. break;
  732. case 3:
  733. amounts[objid] = rand()%16+10;
  734. break;
  735. case 4:
  736. amounts[objid] = rand()%11+10;
  737. break;
  738. case 5:
  739. amounts[objid] = rand()%9+8;
  740. break;
  741. case 6:
  742. amounts[objid] = rand()%8+5;
  743. break;
  744. case 7:
  745. amounts[objid] = rand()%7+3;
  746. break;
  747. case 8:
  748. amounts[objid] = rand()%4+2;
  749. break;
  750. case 9:
  751. amounts[objid] = rand()%3+2;
  752. break;
  753. case 10:
  754. amounts[objid] = rand()%3+1;
  755. break;
  756. }
  757. MetaString ms;
  758. int pom = CCreature::getQuantityID(amounts[objid]);
  759. pom = 174 + 3*pom + 1;
  760. ms << std::pair<ui8,ui32>(6,pom) << " " << std::pair<ui8,ui32>(7,os->subID);
  761. cb->setHoverName(objid,&ms);
  762. }
  763. void CMonsterS::onHeroVisit(int objid, int heroID)
  764. {
  765. DEFOS;
  766. CCreatureSet set;
  767. //TODO: zrobic secik w sposob wyrafinowany
  768. set.slots[0] = std::pair<ui32,si32>(os->subID,amounts[objid]);
  769. cb->startBattle(heroID,set,os->pos);
  770. }
  771. std::vector<int> CMonsterS::yourObjects() //returns IDs of objects which are handled by script
  772. {
  773. std::vector<int> ret;
  774. ret.push_back(54); //monster
  775. return ret;
  776. }
  777. void CCreatureGen::newObject(int objid)
  778. {
  779. DEFOS;
  780. amount[objid] = VLC->creh->creatures[VLC->objh->cregens[os->subID]].growth;
  781. MetaString ms;
  782. ms << std::pair<ui8,ui32>(8,os->subID);
  783. cb->setHoverName(objid,&ms);
  784. }
  785. void CCreatureGen::onHeroVisit(int objid, int heroID)
  786. {
  787. }
  788. std::vector<int> CCreatureGen::yourObjects() //returns IDs of objects which are handled by script
  789. {
  790. std::vector<int> ret(1);
  791. ret.push_back(17); //cregen1
  792. return ret;
  793. }