CLua.cpp 17 KB

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