CLua.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739
  1. #include "stdafx.h"
  2. #include "CLua.h"
  3. #include "CLuaHandler.h"
  4. #include "hch/CHeroHandler.h"
  5. #include "lua.h"
  6. #include "lualib.h"
  7. #include "lauxlib.h"
  8. #include "lobject.h"
  9. #include "lgc.h"
  10. #include "lapi.h"
  11. #include "CGameInfo.h"
  12. #include "CGameState.h"
  13. #include <sstream>
  14. #include "hch/CObjectHandler.h"
  15. #include "hch/CTownHandler.h"
  16. #include "hch/CArtHandler.h"
  17. #include "CCallback.h"
  18. #include "hch/CGeneralTextHandler.h"
  19. #include <sstream>
  20. #include "CPlayerInterface.h"
  21. #include <boost/algorithm/string.hpp>
  22. #include <boost/algorithm/string/replace.hpp>
  23. #include "hch/CDefObjInfoHandler.h"
  24. #pragma warning (disable : 4311)
  25. bool getGlobalFunc(lua_State * L, std::string fname)
  26. {
  27. unsigned int hash = lua_calchash(fname.c_str(), fname.size());
  28. lua_pushhstring(L, hash, fname.c_str(), fname.size());
  29. lua_gettable(L, LUA_GLOBALSINDEX);
  30. return lua_isfunction(L, -1);
  31. }
  32. CObjectScript::CObjectScript()
  33. {
  34. language = ESLan::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 = ESLan::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(CGObjectInstance *os)
  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(CGObjectInstance *os, 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(CGObjectInstance *os)
  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 ret;
  164. }
  165. std::string CCPPObjectScript::hoverText(CGObjectInstance *os)
  166. {
  167. return CGI->objh->objects[os->defInfo->id].name;
  168. }
  169. void CVisitableOPH::newObject(CGObjectInstance *os)
  170. {
  171. visitors.insert
  172. (std::pair<CGObjectInstance*,std::set<int> >(os,std::set<int>()));
  173. };
  174. void CVisitableOPH::onHeroVisit(CGObjectInstance *os, int heroID)
  175. {
  176. if (visitors.find(os)!=visitors.end())
  177. {
  178. if(visitors[os].find(heroID)==visitors[os].end())
  179. {
  180. onNAHeroVisit(os,heroID, false);
  181. visitors[os].insert(heroID);
  182. }
  183. else
  184. {
  185. onNAHeroVisit(os,heroID, true);
  186. }
  187. }
  188. else
  189. {
  190. throw new std::exception("Skrypt nie zainicjalizowal instancji tego obiektu. :(");
  191. }
  192. };
  193. void CVisitableOPH::onNAHeroVisit(CGObjectInstance *os, int heroID, bool alreadyVisited)
  194. {
  195. int w=0, ot=0, vvv=1;
  196. switch(os->ID)
  197. {
  198. case 51:
  199. w=0;
  200. ot=80;
  201. break;
  202. case 23:
  203. w=1;
  204. ot=39;
  205. break;
  206. case 61:
  207. w=2;
  208. ot=100;
  209. break;
  210. case 32:
  211. w=3;
  212. ot=59;
  213. break;
  214. case 100:
  215. w=4;
  216. ot=143;
  217. vvv=1000;
  218. break;
  219. }
  220. if (!alreadyVisited)
  221. {
  222. switch (os->ID)
  223. {
  224. case 51:
  225. case 23:
  226. case 61:
  227. case 32:
  228. {
  229. cb->changePrimSkill(heroID,w,vvv);
  230. std::vector<SComponent*> weko;
  231. weko.push_back(new SComponent(SComponent::primskill,w,vvv));
  232. cb->showInfoDialog(cb->getHeroOwner(heroID),CGI->objh->advobtxt[ot],&weko);
  233. //for (int ii=0; ii<weko.size();ii++)
  234. // delete weko[ii];
  235. break;
  236. }
  237. case 100:
  238. {
  239. cb->changePrimSkill(heroID,w,vvv);
  240. std::vector<SComponent*> weko;
  241. weko.push_back(new SComponent(SComponent::experience,0,vvv));
  242. cb->showInfoDialog(cb->getHeroOwner(heroID),CGI->objh->advobtxt[ot],&weko);
  243. //for (int ii=0; ii<weko.size();ii++)
  244. // delete weko[ii];
  245. break;
  246. }
  247. }
  248. }
  249. else
  250. {
  251. ot++;
  252. cb->showInfoDialog(cb->getHeroOwner(heroID),CGI->objh->advobtxt[ot],&std::vector<SComponent*>());
  253. }
  254. }
  255. std::vector<int> CVisitableOPH::yourObjects()
  256. {
  257. std::vector<int> ret(5);
  258. ret.push_back(51);
  259. ret.push_back(23);
  260. ret.push_back(61);
  261. ret.push_back(32);
  262. ret.push_back(100);
  263. return ret;
  264. }
  265. std::string CVisitableOPH::hoverText(CGObjectInstance *os)
  266. {
  267. std::string add;
  268. int pom;
  269. switch(os->ID)
  270. {
  271. case 51:
  272. pom = 8;
  273. break;
  274. case 23:
  275. pom = 7;
  276. break;
  277. case 61:
  278. pom = 11;
  279. break;
  280. case 32:
  281. pom = 4;
  282. break;
  283. case 100:
  284. pom = 5;
  285. break;
  286. default:
  287. throw new std::exception("Unsupported ID in CVisitableOPH::hoverText");
  288. }
  289. add = " " + CGI->objh->xtrainfo[pom] + " ";
  290. int heroID = cb->getSelectedHero();
  291. if (heroID>=0)
  292. {
  293. add += ( (visitors[os].find(heroID) == visitors[os].end())
  294. ?
  295. (CGI->generaltexth->allTexts[353]) //not visited
  296. :
  297. ( CGI->generaltexth->allTexts[352]) ); //visited
  298. }
  299. return CGI->objh->objects[os->defInfo->id].name + add;
  300. }
  301. void CVisitableOPW::onNAHeroVisit(CGObjectInstance *os, int heroID, bool alreadyVisited)
  302. {
  303. int mid;
  304. switch (os->ID)
  305. {
  306. case 55:
  307. mid = 92;
  308. break;
  309. case 112:
  310. mid = 170;
  311. break;
  312. case 109:
  313. mid = 164;
  314. break;
  315. }
  316. if (alreadyVisited)
  317. {
  318. if (os->ID!=112)
  319. mid++;
  320. else
  321. mid--;
  322. cb->showInfoDialog(cb->getHeroOwner(heroID),CGI->objh->advobtxt[mid],&std::vector<SComponent*>()); //TODO: maybe we have memory leak with these windows
  323. }
  324. else
  325. {
  326. int type, sub, val;
  327. type = SComponent::resource;
  328. switch (os->ID)
  329. {
  330. case 55:
  331. if (rand()%2)
  332. {
  333. sub = 5;
  334. val = 5;
  335. }
  336. else
  337. {
  338. sub = 6;
  339. val = 500;
  340. }
  341. break;
  342. case 112:
  343. mid = 170;
  344. sub = (rand() % 5) + 1;
  345. val = (rand() % 4) + 3;
  346. break;
  347. case 109:
  348. mid = 164;
  349. sub = 6;
  350. if(cb->getDate(2)<2)
  351. val = 500;
  352. else
  353. val = 1000;
  354. }
  355. SComponent * com = new SComponent((SComponent::Etype)type,sub,val);
  356. std::vector<SComponent*> weko;
  357. weko.push_back(com);
  358. cb->giveResource(cb->getHeroOwner(heroID),sub,val);
  359. cb->showInfoDialog(cb->getHeroOwner(heroID),CGI->objh->advobtxt[mid],&weko);
  360. visited[os] = true;
  361. }
  362. }
  363. void CVisitableOPW::newTurn ()
  364. {
  365. if (cb->getDate(1)==1)
  366. {
  367. for (std::map<CGObjectInstance*,bool>::iterator i = visited.begin(); i != visited.end(); i++)
  368. {
  369. (*i).second = false;
  370. }
  371. }
  372. }
  373. void CVisitableOPW::newObject(CGObjectInstance *os)
  374. {
  375. visited.insert(std::pair<CGObjectInstance*,bool>(os,false));
  376. }
  377. void CVisitableOPW::onHeroVisit(CGObjectInstance *os, int heroID)
  378. {
  379. if(visited[os])
  380. onNAHeroVisit(os,heroID,true);
  381. else
  382. onNAHeroVisit(os,heroID,false);
  383. }
  384. std::vector<int> CVisitableOPW::yourObjects() //returns IDs of objects which are handled by script
  385. {
  386. std::vector<int> ret(3);
  387. ret.push_back(55); //mystical garden
  388. ret.push_back(112); //windmill
  389. ret.push_back(109); //water wheel
  390. return ret;
  391. }
  392. std::string CVisitableOPW::hoverText(CGObjectInstance *os)
  393. {
  394. return CGI->objh->objects[os->defInfo->id].name + " " + ( (visited[os]) ? (CGI->generaltexth->allTexts[352]) : (CGI->generaltexth->allTexts[353])) ;
  395. }
  396. void CMines::newObject(CGObjectInstance *os)
  397. {
  398. ourObjs.push_back(os);
  399. os->tempOwner = NEUTRAL_PLAYER;
  400. }
  401. void CMines::onHeroVisit(CGObjectInstance *os, int heroID)
  402. {
  403. int vv = 1;
  404. if (os->subID==0 || os->subID==2)
  405. vv++;
  406. else if (os->subID==6)
  407. vv = 1000;
  408. if (os->tempOwner == cb->getHeroOwner(heroID))
  409. {
  410. //TODO: garrison
  411. }
  412. else
  413. {
  414. if (os->subID==7)
  415. return; //TODO: support for abandoned mine
  416. os->tempOwner = cb->getHeroOwner(heroID);
  417. SComponent * com = new SComponent(SComponent::Etype::resource,os->subID,vv);
  418. com->subtitle+=CGI->generaltexth->allTexts[3].substr(2,CGI->generaltexth->allTexts[3].length()-2);
  419. std::vector<SComponent*> weko;
  420. weko.push_back(com);
  421. cb->showInfoDialog(cb->getHeroOwner(heroID),CGI->objh->mines[os->subID].second,&weko);
  422. }
  423. }
  424. std::vector<int> CMines::yourObjects()
  425. {
  426. std::vector<int> ret(1);
  427. ret.push_back(53);
  428. return ret;
  429. }
  430. std::string CMines::hoverText(CGObjectInstance *os)
  431. {
  432. if (os->tempOwner == NEUTRAL_PLAYER)
  433. return CGI->objh->mines[os->subID].first;
  434. else
  435. return CGI->objh->mines[os->subID].first + " " + CGI->generaltexth->arraytxt[23+os->tempOwner];
  436. }
  437. void CMines::newTurn ()
  438. {
  439. for (int i=0;i<ourObjs.size();i++)
  440. {
  441. if (ourObjs[i]->tempOwner == NEUTRAL_PLAYER)
  442. continue;
  443. int vv = 1;
  444. if (ourObjs[i]->subID==0 || ourObjs[i]->subID==2)
  445. vv++;
  446. else if (ourObjs[i]->subID==6)
  447. vv = 1000;
  448. cb->giveResource(ourObjs[i]->tempOwner,ourObjs[i]->subID,vv);
  449. }
  450. }
  451. void CPickable::newObject(CGObjectInstance *os)
  452. {
  453. os->blockVisit = true;
  454. }
  455. void CPickable::onHeroVisit(CGObjectInstance *os, int heroID)
  456. {
  457. switch(os->ID)
  458. {
  459. case 5:
  460. {
  461. cb->giveHeroArtifact(os->subID,heroID,-1); //TODO: na pozycje
  462. break;
  463. }
  464. case 79:
  465. {
  466. //TODO: handle guards (when battles are finished)
  467. CResourceObjInfo * t2 = static_cast<CResourceObjInfo *>(os->info);
  468. int val;
  469. if(t2->amount)
  470. val = t2->amount;
  471. else
  472. {
  473. switch(os->subID)
  474. {
  475. case 6:
  476. val = 500 + (rand()%6)*100;
  477. break;
  478. case 0: case 2:
  479. val = 6 + (rand()%5);
  480. break;
  481. default:
  482. val = 3 + (rand()%3);
  483. break;
  484. }
  485. }
  486. if(t2->message.length())
  487. cb->showInfoDialog(cb->getHeroOwner(heroID),t2->message,&std::vector<SComponent*>());
  488. SComponent ccc(SComponent::resource,os->subID,val);
  489. ccc.description = CGI->objh->advobtxt[113];
  490. boost::algorithm::replace_first(ccc.description,"%s",CGI->objh->restypes[os->subID]);
  491. cb->giveResource(cb->getHeroOwner(heroID),os->subID,val);
  492. cb->showCompInfo(cb->getHeroOwner(heroID),&ccc);
  493. break;
  494. }
  495. case 101:
  496. {
  497. if (os->subID)
  498. break; //not OH3 treasure chest
  499. int wyn = rand()%100;
  500. if (wyn<32)
  501. {
  502. tempStore.push_back(new CSelectableComponent(SComponent::resource,6,1000));
  503. tempStore.push_back(new CSelectableComponent(SComponent::experience,0,500));
  504. }//1k/0.5k
  505. else if(wyn<64)
  506. {
  507. tempStore.push_back(new CSelectableComponent(SComponent::resource,6,1500));
  508. tempStore.push_back(new CSelectableComponent(SComponent::experience,0,1000));
  509. }//1.5k/1k
  510. else if(wyn<95)
  511. {
  512. tempStore.push_back(new CSelectableComponent(SComponent::resource,6,2000));
  513. tempStore.push_back(new CSelectableComponent(SComponent::experience,0,1500));
  514. }//2k/1.5k
  515. else
  516. {
  517. if (1/*TODO: backpack is full*/)
  518. {
  519. tempStore.push_back(new CSelectableComponent(SComponent::resource,6,1000));
  520. tempStore.push_back(new CSelectableComponent(SComponent::experience,0,500));
  521. }
  522. else
  523. {
  524. //TODO: give treasure artifact
  525. break;
  526. }
  527. }//random treasure artifact, or (if backapack is full) 1k/0.5k
  528. tempStore[1]->ID = heroID;
  529. player = cb->getHeroOwner(heroID);
  530. cb->showSelDialog(player,CGI->objh->advobtxt[146],&tempStore,this);
  531. break;
  532. }
  533. }
  534. CGI->mh->removeObject(os);
  535. }
  536. void CPickable::chosen(int which)
  537. {
  538. switch(tempStore[which]->type)
  539. {
  540. case SComponent::resource:
  541. cb->giveResource(player,tempStore[which]->subtype,tempStore[which]->val);
  542. break;
  543. case SComponent::experience:
  544. cb->changePrimSkill(tempStore[which]->ID,4,tempStore[which]->val);
  545. break;
  546. default:
  547. throw new std::exception("Unhandled choice");
  548. }
  549. for (int i=0;i<tempStore.size();i++)
  550. delete tempStore[i];
  551. tempStore.clear();
  552. }
  553. std::string CPickable::hoverText(CGObjectInstance *os)
  554. {
  555. switch (os->ID)
  556. {
  557. case 79:
  558. return CGI->objh->restypes[os->subID];
  559. break;
  560. case 5:
  561. return CGI->arth->artifacts[os->subID].name;
  562. break;
  563. default:
  564. return CGI->objh->objects[os->defInfo->id].name;
  565. break;
  566. }
  567. }
  568. std::vector<int> CPickable::yourObjects() //returns IDs of objects which are handled by script
  569. {
  570. std::vector<int> ret(3);
  571. ret.push_back(79); //resource
  572. ret.push_back(5); //artifact
  573. ret.push_back(101); //treasure chest / commander stone
  574. return ret;
  575. }
  576. void CTownScript::onHeroVisit(CGObjectInstance *os, int heroID)
  577. {
  578. cb->heroVisitCastle(os,heroID);
  579. }
  580. void CTownScript::onHeroLeave(CGObjectInstance *os, int heroID)
  581. {
  582. cb->stopHeroVisitCastle(os,heroID);
  583. }
  584. std::string CTownScript::hoverText(CGObjectInstance *os)
  585. {
  586. CGTownInstance * n;
  587. if(n = dynamic_cast<CGTownInstance*>(os))
  588. return n->name + ", " + n->town->name;
  589. else return "";
  590. }
  591. std::vector<int> CTownScript::yourObjects() //returns IDs of objects which are handled by script
  592. {
  593. std::vector<int> ret(1);
  594. ret.push_back(98); //town
  595. return ret;
  596. }
  597. void CHeroScript::newObject(CGObjectInstance *os)
  598. {
  599. os->blockVisit = true;
  600. heroes.insert(std::pair<int,CGObjectInstance*>(os->subID,os));
  601. }
  602. void CHeroScript::onHeroVisit(CGObjectInstance *os, int heroID)
  603. {
  604. //TODO: check for allies
  605. if(static_cast<CGHeroInstance*>(heroes[heroID])->tempOwner == static_cast<CGHeroInstance*>(os)->tempOwner) //one of allied cases
  606. {
  607. //exchange
  608. }
  609. else
  610. {
  611. cb->startBattle(
  612. &(static_cast<CGHeroInstance*>(heroes[heroID]))->army,
  613. &(static_cast<CGHeroInstance*>(os))->army,
  614. os->pos,
  615. static_cast<CGHeroInstance*>(heroes[heroID]),
  616. static_cast<CGHeroInstance*>(os));
  617. }
  618. }
  619. std::vector<int> CHeroScript::yourObjects() //returns IDs of objects which are handled by script
  620. {
  621. std::vector<int> ret(1);
  622. ret.push_back(34); //hero
  623. return ret;
  624. }
  625. std::string CHeroScript::hoverText(CGObjectInstance *os)
  626. {
  627. CGHeroInstance* h = static_cast<CGHeroInstance*>(os);
  628. std::string ret = CGI->generaltexth->allTexts[15];
  629. boost::algorithm::replace_first(ret,"%s",h->name);
  630. boost::algorithm::replace_first(ret,"%s",h->type->heroClass->name);
  631. return ret;
  632. }
  633. void CMonsterS::newObject(CGObjectInstance *os)
  634. {
  635. //os->blockVisit = true;
  636. switch(CGI->creh->creatures[os->subID].level)
  637. {
  638. case 1:
  639. ((CCreatureObjInfo*)os->info)->number = rand()%31+20;
  640. break;
  641. case 2:
  642. ((CCreatureObjInfo*)os->info)->number = rand()%16+15;
  643. break;
  644. case 3:
  645. ((CCreatureObjInfo*)os->info)->number = rand()%16+10;
  646. break;
  647. case 4:
  648. ((CCreatureObjInfo*)os->info)->number = rand()%11+10;
  649. break;
  650. case 5:
  651. ((CCreatureObjInfo*)os->info)->number = rand()%9+8;
  652. break;
  653. case 6:
  654. ((CCreatureObjInfo*)os->info)->number = rand()%8+5;
  655. break;
  656. case 7:
  657. ((CCreatureObjInfo*)os->info)->number = rand()%7+3;
  658. break;
  659. case 8:
  660. ((CCreatureObjInfo*)os->info)->number = rand()%4+2;
  661. break;
  662. case 9:
  663. ((CCreatureObjInfo*)os->info)->number = rand()%3+2;
  664. break;
  665. case 10:
  666. ((CCreatureObjInfo*)os->info)->number = rand()%3+1;
  667. break;
  668. }
  669. }
  670. std::string CMonsterS::hoverText(CGObjectInstance *os)
  671. {
  672. int pom = CCreature::getQuantityID(((CCreatureObjInfo*)os->info)->number);
  673. pom = 174 + 3*pom + 1;
  674. return CGI->generaltexth->arraytxt[pom] + " " + CGI->creh->creatures[os->subID].namePl;
  675. }
  676. void CMonsterS::onHeroVisit(CGObjectInstance *os, int heroID)
  677. {
  678. CCreatureSet set;
  679. //TODO: zrobic secik w sposob wyrafinowany
  680. set.slots[0] = std::pair<CCreature*,int>(&CGI->creh->creatures[os->subID],((CCreatureObjInfo*)os->info)->number);
  681. cb->startBattle(heroID,&set,os->pos);
  682. }
  683. std::vector<int> CMonsterS::yourObjects() //returns IDs of objects which are handled by script
  684. {
  685. std::vector<int> ret(1);
  686. ret.push_back(54); //monster
  687. return ret;
  688. }
  689. void CCreatureGen::newObject(CGObjectInstance *os)
  690. {
  691. amount[os] = CGI->creh->creatures[CGI->objh->cregens[os->subID]].growth;
  692. }
  693. std::string CCreatureGen::hoverText(CGObjectInstance *os)
  694. {
  695. return CGI->objh->creGens[os->subID];
  696. }
  697. void CCreatureGen::onHeroVisit(CGObjectInstance *os, int heroID)
  698. {
  699. }
  700. std::vector<int> CCreatureGen::yourObjects() //returns IDs of objects which are handled by script
  701. {
  702. std::vector<int> ret(1);
  703. ret.push_back(17); //cregen1
  704. return ret;
  705. }