CLua.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768
  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. default:
  192. #ifndef __GNUC__
  193. throw new std::exception("Unsupported ID in CVisitableOPH::hoverText");
  194. #else
  195. throw new std::exception();
  196. #endif
  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. if (visitors.find(objid)!=visitors.end())
  213. {
  214. if(visitors[objid].find(heroID)==visitors[objid].end())
  215. {
  216. onNAHeroVisit(objid,heroID, false);
  217. visitors[objid].insert(heroID);
  218. }
  219. else
  220. {
  221. onNAHeroVisit(objid,heroID, true);
  222. }
  223. }
  224. else
  225. {
  226. #ifndef __GNUC__
  227. throw new std::exception("Skrypt nie zainicjalizowal instancji tego obiektu. :(");
  228. #else
  229. throw new std::exception();
  230. #endif
  231. }
  232. };
  233. void CVisitableOPH::onNAHeroVisit(int objid, int heroID, bool alreadyVisited)
  234. {
  235. const CGObjectInstance *os = cb->getObj(objid);
  236. int w=0, ot=0, vvv=1;
  237. switch(os->ID)
  238. {
  239. case 51:
  240. w=0;
  241. ot=80;
  242. break;
  243. case 23:
  244. w=1;
  245. ot=39;
  246. break;
  247. case 61:
  248. w=2;
  249. ot=100;
  250. break;
  251. case 32:
  252. w=3;
  253. ot=59;
  254. break;
  255. case 100:
  256. w=4;
  257. ot=143;
  258. vvv=1000;
  259. break;
  260. }
  261. if (!alreadyVisited)
  262. {
  263. switch (os->ID)
  264. {
  265. case 51:
  266. case 23:
  267. case 61:
  268. case 32:
  269. {
  270. cb->changePrimSkill(heroID,w,vvv);
  271. InfoWindow iw;
  272. iw.components.push_back(Component(0,w,vvv,0));
  273. iw.text << std::pair<ui8,ui32>(11,ot);
  274. iw.player = cb->getHeroOwner(heroID);
  275. cb->showInfoDialog(&iw);
  276. break;
  277. }
  278. case 100: //give 1000 exp
  279. {
  280. cb->changePrimSkill(heroID,w,vvv);
  281. InfoWindow iw;
  282. iw.components.push_back(Component(0,4,vvv,0));
  283. iw.player = cb->getHeroOwner(heroID);
  284. iw.text << std::pair<ui8,ui32>(11,ot);
  285. cb->showInfoDialog(&iw);
  286. break;
  287. }
  288. }
  289. }
  290. else
  291. {
  292. ot++;
  293. InfoWindow iw;
  294. iw.player = cb->getHeroOwner(heroID);
  295. iw.text << std::pair<ui8,ui32>(11,ot);
  296. cb->showInfoDialog(&iw);
  297. }
  298. }
  299. std::vector<int> CVisitableOPH::yourObjects()
  300. {
  301. std::vector<int> ret(5);
  302. ret.push_back(51);
  303. ret.push_back(23);
  304. ret.push_back(61);
  305. ret.push_back(32);
  306. ret.push_back(100);
  307. return ret;
  308. }
  309. void CVisitableOPW::onNAHeroVisit(int objid, int heroID, bool alreadyVisited)
  310. {
  311. DEFOS;
  312. int mid;
  313. switch (os->ID)
  314. {
  315. case 55:
  316. mid = 92;
  317. break;
  318. case 112:
  319. mid = 170;
  320. break;
  321. case 109:
  322. mid = 164;
  323. break;
  324. }
  325. if (alreadyVisited)
  326. {
  327. if (os->ID!=112)
  328. mid++;
  329. else
  330. mid--;
  331. InfoWindow iw;
  332. iw.player = cb->getHero(heroID)->tempOwner;
  333. iw.text << std::pair<ui8,ui32>(11,mid);
  334. cb->showInfoDialog(&iw);
  335. }
  336. else
  337. {
  338. int type, sub, val;
  339. type = 2;
  340. switch (os->ID)
  341. {
  342. case 55:
  343. if (rand()%2)
  344. {
  345. sub = 5;
  346. val = 5;
  347. }
  348. else
  349. {
  350. sub = 6;
  351. val = 500;
  352. }
  353. break;
  354. case 112:
  355. mid = 170;
  356. sub = (rand() % 5) + 1;
  357. val = (rand() % 4) + 3;
  358. break;
  359. case 109:
  360. mid = 164;
  361. sub = 6;
  362. if(cb->getDate(2)<2)
  363. val = 500;
  364. else
  365. val = 1000;
  366. }
  367. int player = cb->getHeroOwner(heroID);
  368. cb->giveResource(player,sub,val);
  369. InfoWindow iw;
  370. iw.player = player;
  371. iw.components.push_back(Component(type,sub,val,0));
  372. iw.text << std::pair<ui8,ui32>(11,mid);
  373. cb->showInfoDialog(&iw);
  374. visited[objid] = true;
  375. MetaString ms; //set text to "visited"
  376. ms << std::pair<ui8,ui32>(3,os->ID) << " " << std::pair<ui8,ui32>(1,352);
  377. cb->setHoverName(objid,&ms);
  378. }
  379. }
  380. void CVisitableOPW::newTurn ()
  381. {
  382. if (cb->getDate(1)==1) //first day of week
  383. {
  384. for (std::map<int,bool>::iterator i = visited.begin(); i != visited.end(); i++)
  385. {
  386. (*i).second = false;
  387. MetaString ms; //set text to "not visited"
  388. ms << std::pair<ui8,ui32>(3,cb->getObj(i->first)->ID) << " " << std::pair<ui8,ui32>(1,353);
  389. cb->setHoverName(i->first,&ms);
  390. }
  391. }
  392. }
  393. void CVisitableOPW::newObject(int objid)
  394. {
  395. visited.insert(std::pair<int,bool>(objid,false));
  396. DEFOS;
  397. MetaString ms;
  398. ms << std::pair<ui8,ui32>(3,os->ID) << " " << std::pair<ui8,ui32>(1,visited[objid] ? 352 : 353);
  399. cb->setHoverName(objid,&ms);
  400. }
  401. void CVisitableOPW::onHeroVisit(int objid, int heroID)
  402. {
  403. if(visited[objid])
  404. onNAHeroVisit(objid,heroID,true);
  405. else
  406. onNAHeroVisit(objid,heroID,false);
  407. }
  408. std::vector<int> CVisitableOPW::yourObjects() //returns IDs of objects which are handled by script
  409. {
  410. std::vector<int> ret(3);
  411. ret.push_back(55); //mystical garden
  412. ret.push_back(112); //windmill
  413. ret.push_back(109); //water wheel
  414. return ret;
  415. }
  416. void CMines::newObject(int objid)
  417. {
  418. ourObjs.push_back(objid);
  419. cb->setOwner(objid,NEUTRAL_PLAYER);
  420. DEFOS;
  421. MetaString ms;
  422. ms << std::pair<ui8,ui32>(3,os->ID);
  423. cb->setHoverName(objid,&ms);
  424. }
  425. void CMines::onHeroVisit(int objid, int heroID)
  426. {
  427. //TODO: this is code for standard mines, no support for abandoned mine (subId==7)
  428. DEFOS;
  429. const CGHeroInstance *h = cb->getHero(heroID);
  430. if(h->tempOwner == os->tempOwner)
  431. return; //TODO: leaving garrison
  432. cb->setOwner(objid,h->tempOwner);
  433. MetaString ms;
  434. ms << std::pair<ui8,ui32>(9,os->subID) << " " << std::pair<ui8,ui32>(6,23+h->tempOwner);
  435. cb->setHoverName(objid,&ms);
  436. ms.clear();
  437. int vv=1; //amount of resource per turn
  438. if (os->subID==0 || os->subID==2)
  439. vv++;
  440. else if (os->subID==6)
  441. vv = 1000;
  442. InfoWindow iw;
  443. iw.text << std::pair<ui8,ui32>(10,os->subID);
  444. iw.player = h->tempOwner;
  445. iw.components.push_back(Component(2,os->subID,vv,-1));
  446. cb->showInfoDialog(&iw);
  447. }
  448. std::vector<int> CMines::yourObjects()
  449. {
  450. std::vector<int> ret;
  451. ret.push_back(53);
  452. return ret;
  453. }
  454. void CMines::newTurn ()
  455. {
  456. const CGObjectInstance * obj;
  457. for (unsigned i=0;i<ourObjs.size();i++)
  458. {
  459. obj = cb->getObj(ourObjs[i]);
  460. if (obj->tempOwner == NEUTRAL_PLAYER)
  461. continue;
  462. int vv = 1;
  463. if (obj->subID==0 || obj->subID==2)
  464. vv++;
  465. else if (obj->subID==6)
  466. vv = 1000;
  467. cb->giveResource(obj->tempOwner,obj->subID,vv);
  468. }
  469. }
  470. void CPickable::newObject(int objid)
  471. {
  472. cb->setBlockVis(objid,true);
  473. MetaString ms;
  474. DEFOS;
  475. switch (os->ID)
  476. {
  477. case 79:
  478. ms << std::pair<ui8,ui32>(4,os->subID);
  479. break;
  480. case 5:
  481. ms << std::pair<ui8,ui32>(5,os->subID);
  482. break;
  483. default:
  484. ms << std::pair<ui8,ui32>(3,os->ID);
  485. break;
  486. }
  487. cb->setHoverName(objid,&ms);
  488. }
  489. void CPickable::onHeroVisit(int objid, int heroID)
  490. {
  491. DEFOS;
  492. switch(os->ID)
  493. {
  494. case 5: //artifact
  495. {
  496. cb->giveHeroArtifact(os->subID,heroID,-1); //TODO: na pozycje
  497. break;
  498. }
  499. case 79: //resource
  500. {
  501. //TODO: handle guards (when battles are finished)
  502. CResourceObjInfo * t2 = static_cast<CResourceObjInfo *>(os->info);
  503. int val;
  504. if(t2->amount)
  505. val = t2->amount;
  506. else
  507. {
  508. switch(os->subID)
  509. {
  510. case 6:
  511. val = 500 + (rand()%6)*100;
  512. break;
  513. case 0: case 2:
  514. val = 6 + (rand()%5);
  515. break;
  516. default:
  517. val = 3 + (rand()%3);
  518. break;
  519. }
  520. }
  521. if(t2->message.length())
  522. {
  523. InfoWindow iw;
  524. iw.player = cb->getHero(heroID)->tempOwner;
  525. iw.text << t2->message;
  526. cb->showInfoDialog(&iw);
  527. }
  528. cb->giveResource(cb->getHeroOwner(heroID),os->subID,val);
  529. ShowInInfobox sii;
  530. sii.player = cb->getHeroOwner(heroID);
  531. sii.c = Component(2,os->subID,val,0);
  532. sii.text << std::pair<ui8,ui32>(11,113);
  533. sii.text.replacements.push_back(VLC->objh->restypes[os->subID]);
  534. cb->showCompInfo(&sii);
  535. break;
  536. }
  537. case 101: //treasure chest
  538. {
  539. if (os->subID) //not OH3 treasure chest
  540. break;
  541. int wyn = rand()%100, val=0;
  542. if (wyn<32) //1k/0.5k
  543. {
  544. val = 1000;
  545. }
  546. else if(wyn<64) //1.5k/1k
  547. {
  548. val = 1500;
  549. }
  550. else if(wyn<95) //2k/1.5k
  551. {
  552. val = 2000;
  553. }
  554. else //random treasure artifact, or (if backapack is full) 1k/0.5k
  555. {
  556. if (1/*TODO: backpack is full*/)
  557. {
  558. val = 1000;
  559. }
  560. else
  561. {
  562. //TODO: give treasure artifact
  563. break;
  564. }
  565. }
  566. SelectionDialog sd;
  567. sd.player = cb->getHeroOwner(heroID);
  568. sd.text << std::pair<ui8,ui32>(11,146);
  569. sd.components.push_back(Component(2,6,val,0));
  570. sd.components.push_back(Component(5,0,val-500,0));
  571. boost::function<void(ui32)> fun = boost::bind(&CPickable::chosen,this,_1,heroID,val);
  572. cb->showSelectionDialog(&sd,fun);
  573. break;
  574. }
  575. }
  576. cb->removeObject(objid);
  577. }
  578. void CPickable::chosen(ui32 which, int heroid, int val)
  579. {
  580. switch(which)
  581. {
  582. case 0: //player pick gold
  583. cb->giveResource(cb->getHeroOwner(heroid),6,val);
  584. break;
  585. case 1: //player pick exp
  586. cb->changePrimSkill(heroid, 4, val-500);
  587. break;
  588. default:
  589. throw std::string("Unhandled choice");
  590. }
  591. }
  592. std::vector<int> CPickable::yourObjects() //returns IDs of objects which are handled by script
  593. {
  594. std::vector<int> ret(3);
  595. ret.push_back(79); //resource
  596. ret.push_back(5); //artifact
  597. ret.push_back(101); //treasure chest / commander stone
  598. return ret;
  599. }
  600. void CTownScript::onHeroVisit(int objid, int heroID)
  601. {
  602. cb->heroVisitCastle(objid,heroID);
  603. }
  604. void CTownScript::newObject(int objid)
  605. {
  606. MetaString ms;
  607. const CGTownInstance * n = cb->getTown(objid);
  608. ms << n->name << ", " << n->town->name;
  609. cb->setHoverName(objid,&ms);
  610. }
  611. void CTownScript::onHeroLeave(int objid, int heroID)
  612. {
  613. cb->stopHeroVisitCastle(objid,heroID);
  614. }
  615. std::vector<int> CTownScript::yourObjects() //returns IDs of objects which are handled by script
  616. {
  617. std::vector<int> ret(1);
  618. ret.push_back(98); //town
  619. return ret;
  620. }
  621. void CHeroScript::newObject(int objid)
  622. {
  623. cb->setBlockVis(objid,true);
  624. MetaString ms;
  625. ms << std::pair<ui8,ui32>(1,15);
  626. ms.replacements.push_back(cb->getHero(objid)->name);
  627. ms.replacements.push_back(cb->getHero(objid)->type->heroClass->name);
  628. cb->setHoverName(objid,&ms);
  629. }
  630. void CHeroScript::onHeroVisit(int objid, int heroID)
  631. {
  632. //TODO: check for allies
  633. const CGHeroInstance *my = cb->getHero(objid),
  634. *vis = cb->getHero(objid);
  635. if(my->tempOwner == vis->tempOwner) //one of allied cases
  636. {
  637. //exchange
  638. }
  639. else
  640. {
  641. cb->startBattle(
  642. &my->army,
  643. &vis->army,
  644. my->pos,
  645. my,
  646. vis);
  647. }
  648. }
  649. std::vector<int> CHeroScript::yourObjects() //returns IDs of objects which are handled by script
  650. {
  651. std::vector<int> ret(1);
  652. ret.push_back(34); //hero
  653. return ret;
  654. }
  655. void CMonsterS::newObject(int objid)
  656. {
  657. //os->blockVisit = true;
  658. DEFOS;
  659. switch(VLC->creh->creatures[os->subID].level)
  660. {
  661. case 1:
  662. amounts[objid] = rand()%31+20;
  663. break;
  664. case 2:
  665. amounts[objid] = rand()%16+15;
  666. break;
  667. case 3:
  668. amounts[objid] = rand()%16+10;
  669. break;
  670. case 4:
  671. amounts[objid] = rand()%11+10;
  672. break;
  673. case 5:
  674. amounts[objid] = rand()%9+8;
  675. break;
  676. case 6:
  677. amounts[objid] = rand()%8+5;
  678. break;
  679. case 7:
  680. amounts[objid] = rand()%7+3;
  681. break;
  682. case 8:
  683. amounts[objid] = rand()%4+2;
  684. break;
  685. case 9:
  686. amounts[objid] = rand()%3+2;
  687. break;
  688. case 10:
  689. amounts[objid] = rand()%3+1;
  690. break;
  691. }
  692. MetaString ms;
  693. int pom = CCreature::getQuantityID(amounts[objid]);
  694. pom = 174 + 3*pom + 1;
  695. ms << std::pair<ui8,ui32>(6,pom) << " " << std::pair<ui8,ui32>(7,os->subID);
  696. cb->setHoverName(objid,&ms);
  697. }
  698. void CMonsterS::onHeroVisit(int objid, int heroID)
  699. {
  700. DEFOS;
  701. CCreatureSet set;
  702. //TODO: zrobic secik w sposob wyrafinowany
  703. set.slots[0] = std::pair<ui32,si32>(os->subID,amounts[objid]);
  704. cb->startBattle(heroID,set,os->pos);
  705. }
  706. std::vector<int> CMonsterS::yourObjects() //returns IDs of objects which are handled by script
  707. {
  708. std::vector<int> ret(1);
  709. ret.push_back(54); //monster
  710. return ret;
  711. }
  712. void CCreatureGen::newObject(int objid)
  713. {
  714. DEFOS;
  715. amount[objid] = VLC->creh->creatures[VLC->objh->cregens[os->subID]].growth;
  716. MetaString ms;
  717. ms << std::pair<ui8,ui32>(8,os->subID);
  718. cb->setHoverName(objid,&ms);
  719. }
  720. void CCreatureGen::onHeroVisit(int objid, int heroID)
  721. {
  722. }
  723. std::vector<int> CCreatureGen::yourObjects() //returns IDs of objects which are handled by script
  724. {
  725. std::vector<int> ret(1);
  726. ret.push_back(17); //cregen1
  727. return ret;
  728. }