CObjectHandler.cpp 73 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011
  1. #define VCMI_DLL
  2. #include "../stdafx.h"
  3. #include "CObjectHandler.h"
  4. #include "CDefObjInfoHandler.h"
  5. #include "CLodHandler.h"
  6. #include "CGeneralTextHandler.h"
  7. #include "CDefObjInfoHandler.h"
  8. #include "CHeroHandler.h"
  9. #include "CSpellHandler.h"
  10. #include <boost/bind.hpp>
  11. #include <boost/algorithm/string/replace.hpp>
  12. #include <boost/lexical_cast.hpp>
  13. #include <boost/random/linear_congruential.hpp>
  14. #include "CTownHandler.h"
  15. #include "CArtHandler.h"
  16. #include "CSoundBase.h"
  17. #include "../lib/VCMI_Lib.h"
  18. #include "../lib/IGameCallback.h"
  19. #include "../lib/CGameState.h"
  20. #include "../lib/NetPacks.h"
  21. #include "../StartInfo.h"
  22. #include "../lib/map.h"
  23. /*
  24. * CObjectHandler.cpp, part of VCMI engine
  25. *
  26. * Authors: listed in file AUTHORS in main folder
  27. *
  28. * License: GNU General Public License v2.0 or later
  29. * Full text of license available in license.txt file, in main folder
  30. *
  31. */
  32. std::map<int,std::map<int, std::vector<int> > > CGTeleport::objs;
  33. IGameCallback * IObjectInterface::cb = NULL;
  34. DLL_EXPORT void loadToIt(std::string &dest, std::string &src, int &iter, int mode);
  35. extern CLodHandler * bitmaph;
  36. extern boost::rand48 ran;
  37. void IObjectInterface::onHeroVisit(const CGHeroInstance * h) const
  38. {};
  39. void IObjectInterface::onHeroLeave(const CGHeroInstance * h) const
  40. {};
  41. void IObjectInterface::newTurn () const
  42. {};
  43. IObjectInterface::~IObjectInterface()
  44. {}
  45. IObjectInterface::IObjectInterface()
  46. {}
  47. void IObjectInterface::initObj()
  48. {}
  49. void IObjectInterface::setProperty( ui8 what, ui32 val )
  50. {}
  51. void CPlayersVisited::setPropertyDer( ui8 what, ui32 val )
  52. {
  53. if(what == 10)
  54. players.insert(val);
  55. }
  56. bool CPlayersVisited::hasVisited( ui8 player ) const
  57. {
  58. return vstd::contains(players,player);
  59. }
  60. void CObjectHandler::loadObjects()
  61. {
  62. tlog5 << "\t\tReading cregens \n";
  63. cregens.resize(110); //TODO: hardcoded value - change
  64. for(size_t i=0; i < cregens.size(); ++i)
  65. {
  66. cregens[i]=-1;
  67. }
  68. std::ifstream ifs("config/cregens.txt");
  69. while(!ifs.eof())
  70. {
  71. int dw, cr;
  72. ifs >> dw >> cr;
  73. cregens[dw]=cr;
  74. }
  75. ifs.close();
  76. ifs.clear();
  77. tlog5 << "\t\tDone loading objects!\n";
  78. }
  79. int CGObjectInstance::getOwner() const
  80. {
  81. //if (state)
  82. // return state->owner;
  83. //else
  84. return tempOwner; //won't have owner
  85. }
  86. CGObjectInstance::CGObjectInstance(): animPhaseShift(rand()%0xff)
  87. {
  88. pos = int3(-1,-1,-1);
  89. //std::cout << "Tworze obiekt "<<this<<std::endl;
  90. //state = new CLuaObjectScript();
  91. ID = subID = id = -1;
  92. defInfo = NULL;
  93. info = NULL;
  94. tempOwner = 254;
  95. blockVisit = false;
  96. }
  97. CGObjectInstance::~CGObjectInstance()
  98. {
  99. //std::cout << "Usuwam obiekt "<<this<<std::endl;
  100. //if (state)
  101. // delete state;
  102. //state=NULL;
  103. }
  104. //CGObjectInstance::CGObjectInstance(const CGObjectInstance & right)
  105. //{
  106. // pos = right.pos;
  107. // ID = right.ID;
  108. // subID = right.subID;
  109. // id = right.id;
  110. // defInfo = right.defInfo;
  111. // info = right.info;
  112. // blockVisit = right.blockVisit;
  113. // //state = new CLuaObjectScript(right.state->);
  114. // //*state = *right.state;
  115. // //state = right.state;
  116. // tempOwner = right.tempOwner;
  117. //}
  118. //CGObjectInstance& CGObjectInstance::operator=(const CGObjectInstance & right)
  119. //{
  120. // pos = right.pos;
  121. // ID = right.ID;
  122. // subID = right.subID;
  123. // id = right.id;
  124. // defInfo = right.defInfo;
  125. // info = right.info;
  126. // blockVisit = right.blockVisit;
  127. // //state = new CLuaObjectScript();
  128. // //*state = *right.state;
  129. // tempOwner = right.tempOwner;
  130. // return *this;
  131. //}
  132. const std::string & CGObjectInstance::getHoverText() const
  133. {
  134. return hoverName;
  135. }
  136. void CGObjectInstance::setOwner(int ow)
  137. {
  138. //if (state)
  139. // state->owner = ow;
  140. //else
  141. tempOwner = ow;
  142. }
  143. int CGObjectInstance::getWidth() const//returns width of object graphic in tiles
  144. {
  145. return defInfo->width;
  146. }
  147. int CGObjectInstance::getHeight() const //returns height of object graphic in tiles
  148. {
  149. return defInfo->height;
  150. }
  151. bool CGObjectInstance::visitableAt(int x, int y) const //returns true if object is visitable at location (x, y) form left top tile of image (x, y in tiles)
  152. {
  153. if(defInfo==NULL)
  154. {
  155. tlog2 << "Warning: VisitableAt for obj "<<id<<": NULL defInfo!\n";
  156. return false;
  157. }
  158. if((defInfo->visitMap[y] >> (7-x) ) & 1)
  159. {
  160. return true;
  161. }
  162. return false;
  163. }
  164. bool CGObjectInstance::blockingAt(int x, int y) const
  165. {
  166. if(x<0 || y<0 || x>=getWidth() || y>=getHeight() || defInfo==NULL)
  167. return false;
  168. if((defInfo->blockMap[y+6-getHeight()] >> (7-(8-getWidth()+x) )) & 1)
  169. return true;
  170. return false;
  171. }
  172. bool CGObjectInstance::coveringAt(int x, int y) const
  173. {
  174. if((defInfo->coverageMap[y] >> (7-(x) )) & 1)
  175. return true;
  176. return false;
  177. }
  178. bool CGObjectInstance::operator<(const CGObjectInstance & cmp) const //screen printing priority comparing
  179. {
  180. if(defInfo->printPriority==1 && cmp.defInfo->printPriority==0)
  181. return true;
  182. if(cmp.defInfo->printPriority==1 && defInfo->printPriority==0)
  183. return false;
  184. if(this->pos.y<cmp.pos.y)
  185. return true;
  186. if(this->pos.y>cmp.pos.y)
  187. return false;
  188. if(cmp.ID==HEROI_TYPE && ID!=HEROI_TYPE)
  189. return true;
  190. if(cmp.ID!=HEROI_TYPE && ID==HEROI_TYPE)
  191. return false;
  192. if(!defInfo->isVisitable() && cmp.defInfo->isVisitable())
  193. return true;
  194. if(!cmp.defInfo->isVisitable() && defInfo->isVisitable())
  195. return false;
  196. if(this->pos.x<cmp.pos.x)
  197. return true;
  198. return false;
  199. }
  200. void CGObjectInstance::initObj()
  201. {
  202. }
  203. void CGObjectInstance::setProperty( ui8 what, ui32 val )
  204. {
  205. switch(what)
  206. {
  207. case 1:
  208. tempOwner = val;
  209. break;
  210. case 2:
  211. blockVisit = val;
  212. break;
  213. case 6:
  214. ID = val;
  215. break;
  216. }
  217. setPropertyDer(what, val);
  218. }
  219. void CGObjectInstance::setPropertyDer( ui8 what, ui32 val )
  220. {}
  221. int3 CGObjectInstance::getSightCenter() const
  222. {
  223. //return vistiable tile if possible
  224. for(int i=0; i < 8; i++)
  225. for(int j=0; j < 6; j++)
  226. if(visitableAt(i,j))
  227. return(pos + int3(i-7, j-5, 0));
  228. return pos;
  229. }
  230. int CGObjectInstance::getSightRadious() const
  231. {
  232. return 3;
  233. }
  234. static int lowestSpeed(const CGHeroInstance * chi)
  235. {
  236. if(!chi->army.slots.size())
  237. {
  238. tlog1 << "Error! Hero " << chi->id << " ("<<chi->name<<") has no army!\n";
  239. return 20;
  240. }
  241. std::map<si32,std::pair<ui32,si32> >::const_iterator i = chi->army.slots.begin();
  242. ui32 ret = VLC->creh->creatures[(*i++).second.first].speed;
  243. for (;i!=chi->army.slots.end();i++)
  244. {
  245. ret = std::min(ret,VLC->creh->creatures[(*i).second.first].speed);
  246. }
  247. return ret;
  248. }
  249. unsigned int CGHeroInstance::getTileCost(const TerrainTile &dest, const TerrainTile &from) const
  250. {
  251. //TODO: check if all creatures are on its native terrain and change cost appropriately
  252. //base move cost
  253. unsigned ret = 100;
  254. //if there is road both on dest and src tiles - use road movement cost
  255. if(dest.malle && from.malle)
  256. {
  257. int road = std::min(dest.malle,from.malle); //used road ID
  258. switch(road)
  259. {
  260. case TerrainTile::dirtRoad:
  261. ret = 75;
  262. break;
  263. case TerrainTile::grazvelRoad:
  264. ret = 65;
  265. break;
  266. case TerrainTile::cobblestoneRoad:
  267. ret = 50;
  268. break;
  269. default:
  270. tlog1 << "Unknown road type: " << road << "... Something wrong!\n";
  271. break;
  272. }
  273. }
  274. else
  275. {
  276. ret = std::max(type->heroClass->terrCosts[dest.tertype],type->heroClass->terrCosts[from.tertype]); //take cost of worse of two tiles
  277. ret = std::max(ret - 25*unsigned(getSecSkillLevel(0)), 100u); //reduce 25% of terrain penalty for each pathfinding level
  278. }
  279. return ret;
  280. }
  281. unsigned int CGHeroInstance::getLowestCreatureSpeed() const
  282. {
  283. unsigned int sl = 100;
  284. for(size_t h=0; h < army.slots.size(); ++h)
  285. {
  286. if(VLC->creh->creatures[army.slots.find(h)->first].speed<sl)
  287. sl = VLC->creh->creatures[army.slots.find(h)->first].speed;
  288. }
  289. return sl;
  290. }
  291. int3 CGHeroInstance::convertPosition(int3 src, bool toh3m) //toh3m=true: manifest->h3m; toh3m=false: h3m->manifest
  292. {
  293. if (toh3m)
  294. {
  295. src.x+=1;
  296. return src;
  297. }
  298. else
  299. {
  300. src.x-=1;
  301. return src;
  302. }
  303. }
  304. int3 CGHeroInstance::getPosition(bool h3m) const //h3m=true - returns position of hero object; h3m=false - returns position of hero 'manifestation'
  305. {
  306. if (h3m)
  307. {
  308. return pos;
  309. }
  310. else
  311. {
  312. return convertPosition(pos,false);
  313. }
  314. }
  315. si32 CGHeroInstance::manaLimit() const
  316. {
  317. double modifier = 1.0;
  318. switch(getSecSkillLevel(24)) //intelligence level
  319. {
  320. case 1: modifier+=0.25; break;
  321. case 2: modifier+=0.5; break;
  322. case 3: modifier+=1.0; break;
  323. }
  324. return si32(10*getPrimSkillLevel(3)*modifier);
  325. }
  326. //void CGHeroInstance::setPosition(int3 Pos, bool h3m) //as above, but sets position
  327. //{
  328. // if (h3m)
  329. // pos = Pos;
  330. // else
  331. // pos = convertPosition(Pos,true);
  332. //}
  333. bool CGHeroInstance::canWalkOnSea() const
  334. {
  335. //TODO: write it - it should check if hero is flying, or something similiar
  336. return false;
  337. }
  338. int CGHeroInstance::getPrimSkillLevel(int id) const
  339. {
  340. int ret = primSkills[id];
  341. for(std::list<HeroBonus>::const_iterator i=bonuses.begin(); i != bonuses.end(); i++)
  342. if(i->type == HeroBonus::PRIMARY_SKILL && i->subtype==id)
  343. ret += i->val;
  344. return ret;
  345. }
  346. ui8 CGHeroInstance::getSecSkillLevel(const int & ID) const
  347. {
  348. for(size_t i=0; i < secSkills.size(); ++i)
  349. if(secSkills[i].first==ID)
  350. return secSkills[i].second;
  351. return 0;
  352. }
  353. int CGHeroInstance::maxMovePoints(bool onLand) const
  354. {
  355. int ret = std::min(2000, 1270+70*lowestSpeed(this)),
  356. bonus = valOfBonuses(HeroBonus::MOVEMENT) + (onLand ? valOfBonuses(HeroBonus::LAND_MOVEMENT) : valOfBonuses(HeroBonus::SEA_MOVEMENT));
  357. double modifier = 0;
  358. if(onLand)
  359. {
  360. //logistics:
  361. switch(getSecSkillLevel(2))
  362. {
  363. case 1:
  364. modifier = 0.1;
  365. break;
  366. case 2:
  367. modifier = 0.2;
  368. break;
  369. case 3:
  370. modifier = 0.3;
  371. break;
  372. }
  373. }
  374. else
  375. {
  376. //navigation:
  377. switch(getSecSkillLevel(2))
  378. {
  379. case 1:
  380. modifier = 0.5;
  381. break;
  382. case 2:
  383. modifier = 1.0;
  384. break;
  385. case 3:
  386. modifier = 1.5;
  387. break;
  388. }
  389. }
  390. return int(ret + ret*modifier) + bonus;
  391. }
  392. ui32 CGHeroInstance::getArtAtPos(ui16 pos) const
  393. {
  394. if(pos<19)
  395. if(vstd::contains(artifWorn,pos))
  396. return artifWorn.find(pos)->second;
  397. else
  398. return -1;
  399. else
  400. if(pos-19 < artifacts.size())
  401. return artifacts[pos-19];
  402. else
  403. return -1;
  404. }
  405. const CArtifact * CGHeroInstance::getArt(int pos) const
  406. {
  407. int id = getArtAtPos(pos);
  408. if(id>=0)
  409. return &VLC->arth->artifacts[id];
  410. else
  411. return NULL;
  412. }
  413. int CGHeroInstance::getSpellSecLevel(int spell) const
  414. {
  415. int bestslvl = 0;
  416. if(VLC->spellh->spells[spell].air)
  417. if(getSecSkillLevel(15) >= bestslvl)
  418. {
  419. bestslvl = getSecSkillLevel(15);
  420. }
  421. if(VLC->spellh->spells[spell].fire)
  422. if(getSecSkillLevel(14) >= bestslvl)
  423. {
  424. bestslvl = getSecSkillLevel(14);
  425. }
  426. if(VLC->spellh->spells[spell].water)
  427. if(getSecSkillLevel(16) >= bestslvl)
  428. {
  429. bestslvl = getSecSkillLevel(16);
  430. }
  431. if(VLC->spellh->spells[spell].earth)
  432. if(getSecSkillLevel(17) >= bestslvl)
  433. {
  434. bestslvl = getSecSkillLevel(17);
  435. }
  436. return bestslvl;
  437. }
  438. CGHeroInstance::CGHeroInstance()
  439. {
  440. ID = HEROI_TYPE;
  441. tacticFormationEnabled = inTownGarrison = false;
  442. mana = movement = portrait = level = -1;
  443. isStanding = true;
  444. moveDir = 4;
  445. exp = 0xffffffff;
  446. visitedTown = NULL;
  447. type = NULL;
  448. secSkills.push_back(std::make_pair(-1, -1));
  449. }
  450. void CGHeroInstance::initHero(int SUBID)
  451. {
  452. subID = SUBID;
  453. initHero();
  454. }
  455. void CGHeroInstance::initHero()
  456. {
  457. if(ID == HEROI_TYPE)
  458. initHeroDefInfo();
  459. if(!type)
  460. type = VLC->heroh->heroes[subID];
  461. artifWorn[16] = 3;
  462. if(type->startingSpell >= 0) //hero starts with a spell
  463. {
  464. artifWorn[17] = 0; //give him spellbook
  465. spells.insert(type->startingSpell);
  466. }
  467. if(portrait < 0 || portrait == 255)
  468. portrait = subID;
  469. if((!primSkills.size()) || (getPrimSkillLevel(0)<0))
  470. {
  471. primSkills.resize(4);
  472. primSkills[0] = type->heroClass->initialAttack;
  473. primSkills[1] = type->heroClass->initialDefence;
  474. primSkills[2] = type->heroClass->initialPower;
  475. primSkills[3] = type->heroClass->initialKnowledge;
  476. }
  477. if(secSkills.size() == 1 && secSkills[0] == std::pair<ui8,ui8>(-1, -1)) //set secondary skills to default
  478. secSkills = type->secSkillsInit;
  479. if(mana < 0)
  480. mana = manaLimit();
  481. if (!name.length())
  482. name = type->name;
  483. if (exp == 0xffffffff)
  484. {
  485. exp=40+ (ran()) % 50;
  486. level = 1;
  487. }
  488. else
  489. {
  490. level = VLC->heroh->level(exp);
  491. }
  492. if (!army.slots.size()) //standard army//initial army
  493. {
  494. int pom, pom2=0;
  495. int x = 0; //how many stacks will hero receives <1 - 3>
  496. pom = ran()%100;
  497. if(pom < 9)
  498. x = 1;
  499. else if(pom < 79)
  500. x = 2;
  501. else
  502. x = 3;
  503. for(int x=0;x<3;x++)
  504. {
  505. pom = (VLC->creh->nameToID[type->refTypeStack[x]]);
  506. if(pom>=145 && pom<=149) //war machine
  507. {
  508. pom2++;
  509. switch (pom)
  510. {
  511. case 145: //catapult
  512. artifWorn[16] = 3;
  513. break;
  514. default:
  515. artifWorn[9+CArtHandler::convertMachineID(pom,true)] = CArtHandler::convertMachineID(pom,true);
  516. break;
  517. }
  518. continue;
  519. }
  520. army.slots[x-pom2].first = pom;
  521. pom = type->highStack[x] - type->lowStack[x];
  522. army.slots[x-pom2].second = ran()%(pom+1) + type->lowStack[x];
  523. army.formation = false;
  524. }
  525. }
  526. hoverName = VLC->generaltexth->allTexts[15];
  527. boost::algorithm::replace_first(hoverName,"%s",name);
  528. boost::algorithm::replace_first(hoverName,"%s", type->heroClass->name);
  529. //clear all bonuses from artifacts (if present) and give them again
  530. std::remove_if(bonuses.begin(), bonuses.end(), boost::bind(HeroBonus::IsFrom,_1,HeroBonus::ARTIFACT,0xffffff));
  531. for (std::map<ui16,ui32>::iterator ari = artifWorn.begin(); ari != artifWorn.end(); ari++)
  532. {
  533. CArtifact &art = VLC->arth->artifacts[ari->second];
  534. for(std::list<HeroBonus>::iterator i = art.bonuses.begin(); i != art.bonuses.end(); i++)
  535. bonuses.push_back(*i);
  536. }
  537. }
  538. void CGHeroInstance::initHeroDefInfo()
  539. {
  540. if(!defInfo || defInfo->id != HEROI_TYPE)
  541. {
  542. defInfo = new CGDefInfo();
  543. defInfo->id = HEROI_TYPE;
  544. defInfo->subid = subID;
  545. defInfo->printPriority = 0;
  546. defInfo->visitDir = 0xff;
  547. }
  548. for(int i=0;i<6;i++)
  549. {
  550. defInfo->blockMap[i]=255;
  551. defInfo->visitMap[i]=0;
  552. }
  553. defInfo->handler=NULL;
  554. defInfo->blockMap[5] = 253;
  555. defInfo->visitMap[5] = 2;
  556. }
  557. CGHeroInstance::~CGHeroInstance()
  558. {
  559. }
  560. bool CGHeroInstance::needsLastStack() const
  561. {
  562. return true;
  563. }
  564. void CGHeroInstance::onHeroVisit(const CGHeroInstance * h) const
  565. {
  566. if(h == this) return; //exclude potential self-visiting
  567. if (ID == HEROI_TYPE) //hero
  568. {
  569. //TODO: check for allies
  570. if(tempOwner == h->tempOwner) //our hero
  571. {
  572. //exchange
  573. cb->heroExchange(id, h->id);
  574. }
  575. else
  576. {
  577. //battle
  578. cb->startBattleI(
  579. &h->army,
  580. &army,
  581. h->pos,
  582. h,
  583. this,
  584. 0);
  585. }
  586. }
  587. else if(ID == 62) //prison
  588. {
  589. InfoWindow iw;
  590. iw.player = h->tempOwner;
  591. iw.soundID = soundBase::ROGUE;
  592. if(cb->getHeroCount(h->tempOwner,false) < 8) //free hero slot
  593. {
  594. cb->changeObjPos(id,pos+int3(1,0,0),0);
  595. cb->setObjProperty(id,6,HEROI_TYPE); //set ID to 34
  596. cb->giveHero(id,h->tempOwner); //recreates def and adds hero to player
  597. iw.text << std::pair<ui8,ui32>(11,102);
  598. }
  599. else //already 8 wandering heroes
  600. {
  601. iw.text << std::pair<ui8,ui32>(11,103);
  602. }
  603. cb->showInfoDialog(&iw);
  604. }
  605. }
  606. const std::string & CGHeroInstance::getBiography() const
  607. {
  608. if (biography.length())
  609. return biography;
  610. else
  611. return VLC->generaltexth->hTxts[subID].biography;
  612. }
  613. void CGHeroInstance::initObj()
  614. {
  615. blockVisit = true;
  616. }
  617. int CGHeroInstance::getCurrentMorale( int stack, bool town ) const
  618. {
  619. int ret = 0;
  620. std::vector<std::pair<int,std::string> > mods = getCurrentMoraleModifiers(stack,town);
  621. for(int i=0; i < mods.size(); i++)
  622. ret += mods[i].first;
  623. if(ret > 3)
  624. return 3;
  625. if(ret < -3)
  626. return -3;
  627. return ret;
  628. }
  629. std::vector<std::pair<int,std::string> > CGHeroInstance::getCurrentMoraleModifiers( int stack/*=-1*/, bool town/*=false*/ ) const
  630. {
  631. //TODO: check if stack is undead/mechanic/elemental => always neutrl morale
  632. std::vector<std::pair<int,std::string> > ret;
  633. //various morale bonuses (from buildings, artifacts, etc)
  634. for(std::list<HeroBonus>::const_iterator i=bonuses.begin(); i != bonuses.end(); i++)
  635. {
  636. if(i->type == HeroBonus::MORALE || i->type == HeroBonus::MORALE_AND_LUCK)
  637. {
  638. ret.push_back(std::make_pair(i->val, i->description));
  639. }
  640. }
  641. //leadership
  642. if(getSecSkillLevel(6))
  643. ret.push_back(std::make_pair(getSecSkillLevel(6),VLC->generaltexth->arraytxt[104+getSecSkillLevel(6)]));
  644. //town structures
  645. if(town && visitedTown)
  646. {
  647. if(visitedTown->subID == 0 && vstd::contains(visitedTown->builtBuildings,22)) //castle, brotherhood of sword built
  648. ret.push_back(std::pair<int,std::string>(2,VLC->generaltexth->buildings[0][22].first + " +2"));
  649. else if(vstd::contains(visitedTown->builtBuildings,5)) //tavern is built
  650. ret.push_back(std::pair<int,std::string>(2,VLC->generaltexth->buildings[0][5].first + " +1"));
  651. }
  652. //number of alignments and presence of undead
  653. if(stack>=0)
  654. {
  655. bool archangelInArmy = false;
  656. std::set<si8> factions;
  657. for(std::map<si32,std::pair<ui32,si32> >::const_iterator i=army.slots.begin(); i!=army.slots.end(); i++)
  658. {
  659. factions.insert(VLC->creh->creatures[i->second.first].faction);
  660. if(i->second.first == 13)
  661. archangelInArmy = true;
  662. }
  663. if(factions.size() == 1)
  664. ret.push_back(std::pair<int,std::string>(1,VLC->generaltexth->arraytxt[115])); //All troops of one alignment +1
  665. else
  666. {
  667. if(VLC->generaltexth->arraytxt[114].length() <= 100)
  668. {
  669. char buf[150];
  670. std::sprintf(buf,VLC->generaltexth->arraytxt[114].c_str(),factions.size(),2-factions.size());
  671. ret.push_back(std::pair<int,std::string>(2-factions.size(),buf)); //Troops of %d alignments %d
  672. }
  673. else
  674. {
  675. ret.push_back(std::pair<int,std::string>(2-factions.size(),"")); //Troops of %d alignments %d
  676. }
  677. }
  678. if(vstd::contains(factions,4))
  679. ret.push_back(std::pair<int,std::string>(-1,VLC->generaltexth->arraytxt[116])); //Undead in group -1
  680. if(archangelInArmy)
  681. {
  682. char buf[100];
  683. sprintf(buf,VLC->generaltexth->arraytxt[117].c_str(),VLC->creh->creatures[13].namePl.c_str());
  684. ret.push_back(std::pair<int,std::string>(1,buf)); //%s in group +1
  685. }
  686. }
  687. return ret;
  688. }
  689. int CGHeroInstance::getCurrentLuck( int stack/*=-1*/, bool town/*=false*/ ) const
  690. {
  691. int ret = 0;
  692. std::vector<std::pair<int,std::string> > mods = getCurrentLuckModifiers(stack,town);
  693. for(int i=0; i < mods.size(); i++)
  694. ret += mods[i].first;
  695. if(ret > 3)
  696. return 3;
  697. if(ret < -3)
  698. return -3;
  699. return ret;
  700. }
  701. std::vector<std::pair<int,std::string> > CGHeroInstance::getCurrentLuckModifiers( int stack/*=-1*/, bool town/*=false*/ ) const
  702. {
  703. std::vector<std::pair<int,std::string> > ret;
  704. //various morale bonuses (from buildings, artifacts, etc)
  705. for(std::list<HeroBonus>::const_iterator i=bonuses.begin(); i != bonuses.end(); i++)
  706. if(i->type == HeroBonus::LUCK || i->type == HeroBonus::MORALE_AND_LUCK)
  707. ret.push_back(std::make_pair(i->val, i->description));
  708. //luck skill
  709. if(getSecSkillLevel(9))
  710. ret.push_back(std::make_pair(getSecSkillLevel(9),VLC->generaltexth->arraytxt[73+getSecSkillLevel(9)]));
  711. return ret;
  712. }
  713. const HeroBonus * CGHeroInstance::getBonus( int from, int id ) const
  714. {
  715. for (std::list<HeroBonus>::const_iterator i=bonuses.begin(); i!=bonuses.end(); i++)
  716. if(i->source == from && i->id == id)
  717. return &*i;
  718. return NULL;
  719. }
  720. void CGHeroInstance::setPropertyDer( ui8 what, ui32 val )
  721. {
  722. if(what == 3)
  723. army.slots[0].second = val;
  724. }
  725. double CGHeroInstance::getHeroStrength() const
  726. {
  727. return sqrt((1.0 + 0.05*getPrimSkillLevel(0)) * (1.0 + 0.05*getPrimSkillLevel(1)));
  728. }
  729. int CGHeroInstance::getTotalStrength() const
  730. {
  731. double ret = getHeroStrength() * getArmyStrength();
  732. return (int) ret;
  733. }
  734. ui8 CGHeroInstance::getSpellSchoolLevel(const CSpell * spell) const
  735. {
  736. //TODO: skill level may be different on special terrain
  737. ui8 skill = 0; //skill level
  738. if(spell->fire)
  739. skill = std::max(skill,getSecSkillLevel(14));
  740. if(spell->air)
  741. skill = std::max(skill,getSecSkillLevel(15));
  742. if(spell->water)
  743. skill = std::max(skill,getSecSkillLevel(16));
  744. if(spell->earth)
  745. skill = std::max(skill,getSecSkillLevel(17));
  746. return skill;
  747. }
  748. bool CGHeroInstance::canCastThisSpell(const CSpell * spell) const
  749. {
  750. if(!getArt(17)) //if hero has no spellbook
  751. return false;
  752. if(vstd::contains(spells, spell->id) //hero does not have this spell in spellbook
  753. || (spell->air && hasBonusOfType(HeroBonus::AIR_SPELLS)) // this is air spell and hero can cast all air spells
  754. || (spell->fire && hasBonusOfType(HeroBonus::FIRE_SPELLS)) // this is fire spell and hero can cast all fire spells
  755. || (spell->water && hasBonusOfType(HeroBonus::WATER_SPELLS)) // this is water spell and hero can cast all water spells
  756. || (spell->earth && hasBonusOfType(HeroBonus::EARTH_SPELLS)) // this is earth spell and hero can cast all earth spells
  757. )
  758. return true;
  759. for(std::list<HeroBonus>::const_iterator it = bonuses.begin(); it != bonuses.end(); ++it)
  760. {
  761. if(it->type == HeroBonus::SPELL && it->subtype == spell->id)
  762. {
  763. return true;
  764. }
  765. if(it->type == HeroBonus::SPELLS_OF_LEVEL && it->subtype == spell->level)
  766. {
  767. return true;
  768. }
  769. }
  770. return false;
  771. }
  772. int3 CGHeroInstance::getSightCenter() const
  773. {
  774. return getPosition(false);
  775. }
  776. int CGHeroInstance::getSightRadious() const
  777. {
  778. return 5 + getSecSkillLevel(3) + valOfBonuses(HeroBonus::SIGHT_RADIOUS); //default + scouting
  779. }
  780. si32 CGHeroInstance::manaRegain() const
  781. {
  782. return 1 + getSecSkillLevel(8) + valOfBonuses(HeroBonus::MANA_REGENERATION); //1 + Mysticism level
  783. }
  784. int CGHeroInstance::valOfBonuses( HeroBonus::BonusType type, int subtype /*= -1*/ ) const
  785. {
  786. int ret = 0;
  787. if(subtype == -1)
  788. {
  789. for(std::list<HeroBonus>::const_iterator i=bonuses.begin(); i != bonuses.end(); i++)
  790. if(i->type == type)
  791. ret += i->val;
  792. }
  793. else
  794. {
  795. for(std::list<HeroBonus>::const_iterator i=bonuses.begin(); i != bonuses.end(); i++)
  796. if(i->type == type && i->subtype == subtype)
  797. ret += i->val;
  798. }
  799. return ret;
  800. }
  801. bool CGHeroInstance::hasBonusOfType(HeroBonus::BonusType type, int subtype /*= -1*/) const
  802. {
  803. if(subtype == -1) //any subtype
  804. {
  805. for(std::list<HeroBonus>::const_iterator i=bonuses.begin(); i != bonuses.end(); i++)
  806. if(i->type == type)
  807. return true;
  808. }
  809. else //given subtype
  810. {
  811. for(std::list<HeroBonus>::const_iterator i=bonuses.begin(); i != bonuses.end(); i++)
  812. if(i->type == type && i->subtype == subtype)
  813. return true;
  814. }
  815. return false;
  816. }
  817. si32 CGHeroInstance::getArtPos(int aid) const
  818. {
  819. for(std::map<ui16,ui32>::const_iterator i = artifWorn.begin(); i != artifWorn.end(); i++)
  820. if(i->second == aid)
  821. return i->first;
  822. return -1;
  823. }
  824. int CGTownInstance::getSightRadious() const //returns sight distance
  825. {
  826. return 5;
  827. }
  828. int CGTownInstance::fortLevel() const //0 - none, 1 - fort, 2 - citadel, 3 - castle
  829. {
  830. if((builtBuildings.find(9))!=builtBuildings.end())
  831. return 3;
  832. if((builtBuildings.find(8))!=builtBuildings.end())
  833. return 2;
  834. if((builtBuildings.find(7))!=builtBuildings.end())
  835. return 1;
  836. return 0;
  837. }
  838. int CGTownInstance::hallLevel() const // -1 - none, 0 - village, 1 - town, 2 - city, 3 - capitol
  839. {
  840. if ((builtBuildings.find(13))!=builtBuildings.end())
  841. return 3;
  842. if ((builtBuildings.find(12))!=builtBuildings.end())
  843. return 2;
  844. if ((builtBuildings.find(11))!=builtBuildings.end())
  845. return 1;
  846. if ((builtBuildings.find(10))!=builtBuildings.end())
  847. return 0;
  848. return -1;
  849. }
  850. int CGTownInstance::mageGuildLevel() const
  851. {
  852. if ((builtBuildings.find(4))!=builtBuildings.end())
  853. return 5;
  854. if ((builtBuildings.find(3))!=builtBuildings.end())
  855. return 4;
  856. if ((builtBuildings.find(2))!=builtBuildings.end())
  857. return 3;
  858. if ((builtBuildings.find(1))!=builtBuildings.end())
  859. return 2;
  860. if ((builtBuildings.find(0))!=builtBuildings.end())
  861. return 1;
  862. return 0;
  863. }
  864. bool CGTownInstance::creatureDwelling(const int & level, bool upgraded) const
  865. {
  866. return builtBuildings.find(30+level+upgraded*7)!=builtBuildings.end();
  867. }
  868. int CGTownInstance::getHordeLevel(const int & HID) const//HID - 0 or 1; returns creature level or -1 if that horde structure is not present
  869. {
  870. return town->hordeLvl[HID];
  871. }
  872. int CGTownInstance::creatureGrowth(const int & level) const
  873. {
  874. int ret = VLC->creh->creatures[town->basicCreatures[level]].growth;
  875. switch(fortLevel())
  876. {
  877. case 3:
  878. ret*=2;break;
  879. case 2:
  880. ret*=(1.5); break;
  881. }
  882. if(builtBuildings.find(26)!=builtBuildings.end()) //grail
  883. ret+=VLC->creh->creatures[town->basicCreatures[level]].growth;
  884. if(getHordeLevel(0)==level)
  885. if((builtBuildings.find(18)!=builtBuildings.end()) || (builtBuildings.find(19)!=builtBuildings.end()))
  886. ret+=VLC->creh->creatures[town->basicCreatures[level]].hordeGrowth;
  887. if(getHordeLevel(1)==level)
  888. if((builtBuildings.find(24)!=builtBuildings.end()) || (builtBuildings.find(25)!=builtBuildings.end()))
  889. ret+=VLC->creh->creatures[town->basicCreatures[level]].hordeGrowth;
  890. return ret;
  891. }
  892. int CGTownInstance::dailyIncome() const
  893. {
  894. int ret = 0;
  895. if ((builtBuildings.find(26))!=builtBuildings.end())
  896. ret+=5000;
  897. if ((builtBuildings.find(13))!=builtBuildings.end())
  898. ret+=4000;
  899. else if ((builtBuildings.find(12))!=builtBuildings.end())
  900. ret+=2000;
  901. else if ((builtBuildings.find(11))!=builtBuildings.end())
  902. ret+=1000;
  903. else if ((builtBuildings.find(10))!=builtBuildings.end())
  904. ret+=500;
  905. return ret;
  906. }
  907. bool CGTownInstance::hasFort() const
  908. {
  909. return (builtBuildings.find(7))!=builtBuildings.end();
  910. }
  911. bool CGTownInstance::hasCapitol() const
  912. {
  913. return (builtBuildings.find(13))!=builtBuildings.end();
  914. }
  915. CGTownInstance::CGTownInstance()
  916. {
  917. builded=-1;
  918. destroyed=-1;
  919. garrisonHero=NULL;
  920. town=NULL;
  921. visitingHero = NULL;
  922. }
  923. CGTownInstance::~CGTownInstance()
  924. {}
  925. int CGTownInstance::spellsAtLevel(int level, bool checkGuild) const
  926. {
  927. if(checkGuild && mageGuildLevel() < level)
  928. return 0;
  929. int ret = 6 - level; //how many spells are available at this level
  930. if(subID == 2 && vstd::contains(builtBuildings,22)) //magic library in Tower
  931. ret++;
  932. return ret;
  933. }
  934. bool CGTownInstance::needsLastStack() const
  935. {
  936. if(garrisonHero)
  937. return true;
  938. else return false;
  939. }
  940. void CGTownInstance::onHeroVisit(const CGHeroInstance * h) const
  941. {
  942. if(getOwner() != h->getOwner())
  943. {
  944. return;
  945. }
  946. cb->heroVisitCastle(id,h->id);
  947. }
  948. void CGTownInstance::onHeroLeave(const CGHeroInstance * h) const
  949. {
  950. cb->stopHeroVisitCastle(id,h->id);
  951. }
  952. void CGTownInstance::initObj()
  953. {
  954. MetaString ms;
  955. ms << name << ", " << town->Name();
  956. hoverName = toString(ms);
  957. }
  958. int3 CGTownInstance::getSightCenter() const
  959. {
  960. return pos - int3(2,0,0);
  961. }
  962. void CGVisitableOPH::onHeroVisit( const CGHeroInstance * h ) const
  963. {
  964. if(visitors.find(h->id)==visitors.end())
  965. {
  966. onNAHeroVisit(h->id, false);
  967. switch(ID)
  968. {
  969. case 102: //tree
  970. case 4: //arena
  971. case 41://library
  972. case 47: //School of Magic
  973. case 107://School of War
  974. break;
  975. default:
  976. cb->setObjProperty(id,4,h->id); //add to the visitors
  977. break;
  978. }
  979. }
  980. else
  981. {
  982. onNAHeroVisit(h->id, true);
  983. }
  984. }
  985. void CGVisitableOPH::initObj()
  986. {
  987. if(ID==102)
  988. ttype = ran()%3;
  989. else
  990. ttype = -1;
  991. }
  992. void CGVisitableOPH::treeSelected( int heroID, int resType, int resVal, int expVal, ui32 result ) const
  993. {
  994. if(result) //player agreed to give res for exp
  995. {
  996. cb->giveResource(cb->getOwner(heroID),resType,-resVal); //take resource
  997. cb->changePrimSkill(heroID,4,expVal); //give exp
  998. cb->setObjProperty(id,4,heroID); //add to the visitors
  999. }
  1000. }
  1001. void CGVisitableOPH::onNAHeroVisit(int heroID, bool alreadyVisited) const
  1002. {
  1003. int id=0, subid=0, ot=0, val=1, sound = 0;
  1004. switch(ID)
  1005. {
  1006. case 4: //arena
  1007. sound = soundBase::NOMAD;
  1008. ot = 0;
  1009. break;
  1010. case 51: //mercenary camp
  1011. sound = soundBase::NOMAD;
  1012. subid=0;
  1013. ot=80;
  1014. break;
  1015. case 23: //marletto tower
  1016. sound = soundBase::NOMAD;
  1017. subid=1;
  1018. ot=39;
  1019. break;
  1020. case 61:
  1021. sound = soundBase::gazebo;
  1022. subid=2;
  1023. ot=100;
  1024. break;
  1025. case 32:
  1026. sound = soundBase::GETPROTECTION;
  1027. subid=3;
  1028. ot=59;
  1029. break;
  1030. case 100:
  1031. sound = soundBase::gazebo;
  1032. id=5;
  1033. ot=143;
  1034. val=1000;
  1035. break;
  1036. case 102:
  1037. sound = soundBase::gazebo;
  1038. id = 5;
  1039. subid = 1;
  1040. ot = 146;
  1041. val = 1;
  1042. break;
  1043. case 41:
  1044. sound = soundBase::gazebo;
  1045. ot = 66;
  1046. break;
  1047. case 47: //School of Magic
  1048. sound = soundBase::faerie;
  1049. ot = 71;
  1050. break;
  1051. case 107://School of War
  1052. sound = soundBase::MILITARY;
  1053. ot = 158;
  1054. break;
  1055. }
  1056. if (!alreadyVisited)
  1057. {
  1058. switch (ID)
  1059. {
  1060. case 4: //arena
  1061. {
  1062. BlockingDialog sd(false,true);
  1063. sd.soundID = sound;
  1064. sd.text << std::pair<ui8,ui32>(11,ot);
  1065. sd.components.push_back(Component(0,0,2,0));
  1066. sd.components.push_back(Component(0,1,2,0));
  1067. sd.player = cb->getOwner(heroID);
  1068. cb->showBlockingDialog(&sd,boost::bind(&CGVisitableOPH::arenaSelected,this,heroID,_1));
  1069. return;
  1070. }
  1071. case 51:
  1072. case 23:
  1073. case 61:
  1074. case 32:
  1075. {
  1076. cb->changePrimSkill(heroID,subid,val);
  1077. InfoWindow iw;
  1078. iw.soundID = sound;
  1079. iw.components.push_back(Component(0,subid,val,0));
  1080. iw.text << std::pair<ui8,ui32>(11,ot);
  1081. iw.player = cb->getOwner(heroID);
  1082. cb->showInfoDialog(&iw);
  1083. break;
  1084. }
  1085. case 100: //give exp
  1086. {
  1087. InfoWindow iw;
  1088. iw.soundID = sound;
  1089. iw.components.push_back(Component(id,subid,val,0));
  1090. iw.player = cb->getOwner(heroID);
  1091. iw.text << std::pair<ui8,ui32>(11,ot);
  1092. iw.soundID = soundBase::gazebo;
  1093. cb->showInfoDialog(&iw);
  1094. cb->changePrimSkill(heroID,4,val);
  1095. break;
  1096. }
  1097. case 102://tree
  1098. {
  1099. const CGHeroInstance *h = cb->getHero(heroID);
  1100. val = VLC->heroh->reqExp(h->level+val) - VLC->heroh->reqExp(h->level);
  1101. if(!ttype)
  1102. {
  1103. cb->setObjProperty(this->id,4,heroID); //add to the visitors
  1104. InfoWindow iw;
  1105. iw.soundID = sound;
  1106. iw.components.push_back(Component(id,subid,1,0));
  1107. iw.player = cb->getOwner(heroID);
  1108. iw.text << std::pair<ui8,ui32>(11,148);
  1109. cb->showInfoDialog(&iw);
  1110. cb->changePrimSkill(heroID,4,val);
  1111. break;
  1112. }
  1113. else
  1114. {
  1115. int res, resval;
  1116. if(ttype==1)
  1117. {
  1118. res = 6;
  1119. resval = 2000;
  1120. ot = 149;
  1121. }
  1122. else
  1123. {
  1124. res = 5;
  1125. resval = 10;
  1126. ot = 151;
  1127. }
  1128. if(cb->getResource(h->tempOwner,res) < resval) //not enough resources
  1129. {
  1130. ot++;
  1131. InfoWindow iw;
  1132. iw.soundID = sound;
  1133. iw.player = h->tempOwner;
  1134. iw.text << std::pair<ui8,ui32>(11,ot);
  1135. cb->showInfoDialog(&iw);
  1136. return;
  1137. }
  1138. BlockingDialog sd(true,false);
  1139. sd.soundID = sound;
  1140. sd.player = cb->getOwner(heroID);
  1141. sd.text << std::pair<ui8,ui32>(11,ot);
  1142. sd.components.push_back(Component(id,subid,val,0));
  1143. cb->showBlockingDialog(&sd,boost::bind(&CGVisitableOPH::treeSelected,this,heroID,res,resval,val,_1));
  1144. }
  1145. break;
  1146. }
  1147. case 41://library of enlightenment
  1148. {
  1149. const CGHeroInstance *h = cb->getHero(heroID);
  1150. if(h->level < 10 - 2*h->getSecSkillLevel(4)) //not enough level
  1151. {
  1152. InfoWindow iw;
  1153. iw.soundID = sound;
  1154. iw.player = cb->getOwner(heroID);
  1155. iw.text << std::pair<ui8,ui32>(11,68);
  1156. cb->showInfoDialog(&iw);
  1157. }
  1158. else
  1159. {
  1160. cb->setObjProperty(this->id,4,heroID); //add to the visitors
  1161. cb->changePrimSkill(heroID,0,2);
  1162. cb->changePrimSkill(heroID,1,2);
  1163. cb->changePrimSkill(heroID,2,2);
  1164. cb->changePrimSkill(heroID,3,2);
  1165. InfoWindow iw;
  1166. iw.soundID = sound;
  1167. iw.player = cb->getOwner(heroID);
  1168. iw.text << std::pair<ui8,ui32>(11,66);
  1169. cb->showInfoDialog(&iw);
  1170. }
  1171. break;
  1172. }
  1173. case 47: //School of Magic
  1174. case 107://School of War
  1175. {
  1176. int skill = (ID==47 ? 2 : 0);
  1177. if(cb->getResource(cb->getOwner(heroID),6) < 1000) //not enough resources
  1178. {
  1179. InfoWindow iw;
  1180. iw.soundID = sound;
  1181. iw.player = cb->getOwner(heroID);
  1182. iw.text << std::pair<ui8,ui32>(MetaString::ADVOB_TXT,ot+2);
  1183. cb->showInfoDialog(&iw);
  1184. }
  1185. else
  1186. {
  1187. BlockingDialog sd(true,true);
  1188. sd.soundID = sound;
  1189. sd.player = cb->getOwner(heroID);
  1190. sd.text << std::pair<ui8,ui32>(11,ot);
  1191. sd.components.push_back(Component(Component::PRIM_SKILL, skill, +1, 0));
  1192. sd.components.push_back(Component(Component::PRIM_SKILL, skill+1, +1, 0));
  1193. cb->showBlockingDialog(&sd,boost::bind(&CGVisitableOPH::schoolSelected,this,heroID,_1));
  1194. }
  1195. }
  1196. break;
  1197. }
  1198. }
  1199. else
  1200. {
  1201. ot++;
  1202. InfoWindow iw;
  1203. iw.soundID = sound;
  1204. iw.player = cb->getOwner(heroID);
  1205. iw.text << std::pair<ui8,ui32>(11,ot);
  1206. cb->showInfoDialog(&iw);
  1207. }
  1208. }
  1209. const std::string & CGVisitableOPH::getHoverText() const
  1210. {
  1211. int pom = -1;
  1212. switch(ID)
  1213. {
  1214. case 4:
  1215. pom = -1;
  1216. break;
  1217. case 51:
  1218. pom = 8;
  1219. break;
  1220. case 23:
  1221. pom = 7;
  1222. break;
  1223. case 61:
  1224. pom = 11;
  1225. break;
  1226. case 32:
  1227. pom = 4;
  1228. break;
  1229. case 100:
  1230. pom = 5;
  1231. break;
  1232. case 102:
  1233. pom = 18;
  1234. break;
  1235. case 41:
  1236. break;
  1237. case 47: //School of Magic
  1238. pom = 9;
  1239. break;
  1240. case 107://School of War
  1241. pom = 10;
  1242. break;
  1243. default:
  1244. throw std::string("Wrong CGVisitableOPH object ID!\n");
  1245. }
  1246. hoverName = VLC->generaltexth->names[ID];
  1247. if(pom >= 0)
  1248. hoverName += (" " + VLC->generaltexth->xtrainfo[pom]);
  1249. const CGHeroInstance *h = cb->getSelectedHero(cb->getCurrentPlayer());
  1250. if(h)
  1251. {
  1252. hoverName += ' ';
  1253. hoverName += (vstd::contains(visitors,h->id))
  1254. ? (VLC->generaltexth->allTexts[352]) //visited
  1255. : ( VLC->generaltexth->allTexts[353]); //not visited
  1256. }
  1257. return hoverName;
  1258. }
  1259. void CGVisitableOPH::arenaSelected( int heroID, int primSkill ) const
  1260. {
  1261. cb->setObjProperty(id,4,heroID); //add to the visitors
  1262. cb->changePrimSkill(heroID,primSkill-1,2);
  1263. }
  1264. void CGVisitableOPH::setPropertyDer( ui8 what, ui32 val )
  1265. {
  1266. if(what == 4)
  1267. visitors.insert(val);
  1268. }
  1269. void CGVisitableOPH::schoolSelected(int heroID, ui32 which) const
  1270. {
  1271. if(!which) //player refused to pay
  1272. return;
  1273. int base = (ID == 47 ? 2 : 0);
  1274. cb->setObjProperty(id,4,heroID); //add to the visitors
  1275. cb->giveResource(cb->getOwner(heroID),6,-1000); //take 1000 gold
  1276. cb->changePrimSkill(heroID, base + which-1, +1); //give appropriate skill
  1277. }
  1278. bool CArmedInstance::needsLastStack() const
  1279. {
  1280. return false;
  1281. }
  1282. int CArmedInstance::getArmyStrength() const
  1283. {
  1284. int ret = 0;
  1285. for(std::map<si32,std::pair<ui32,si32> >::const_iterator i=army.slots.begin(); i!=army.slots.end(); i++)
  1286. ret += VLC->creh->creatures[i->second.first].AIValue * i->second.second;
  1287. return ret;
  1288. }
  1289. void CGCreature::onHeroVisit( const CGHeroInstance * h ) const
  1290. {
  1291. int action = takenAction(h);
  1292. switch( action ) //decide what we do...
  1293. {
  1294. case -2: //fight
  1295. fight(h);
  1296. break;
  1297. case -1: //flee
  1298. {
  1299. flee(h);
  1300. break;
  1301. }
  1302. case 0: //join for free
  1303. {
  1304. BlockingDialog ynd(true,false);
  1305. ynd.player = h->tempOwner;
  1306. ynd.text << std::pair<ui8,ui32>(11,86);
  1307. ynd.text.replacements.push_back(VLC->creh->creatures[subID].namePl);
  1308. cb->showBlockingDialog(&ynd,boost::bind(&CGCreature::joinDecision,this,h,0,_1));
  1309. break;
  1310. }
  1311. default: //join for gold
  1312. {
  1313. assert(action > 0);
  1314. //ask if player agrees to pay gold
  1315. BlockingDialog ynd(true,false);
  1316. ynd.player = h->tempOwner;
  1317. std::string tmp = VLC->generaltexth->advobtxt[90];
  1318. boost::algorithm::replace_first(tmp,"%d",boost::lexical_cast<std::string>(army.slots.find(0)->second.second));
  1319. boost::algorithm::replace_first(tmp,"%d",boost::lexical_cast<std::string>(action));
  1320. boost::algorithm::replace_first(tmp,"%s",VLC->creh->creatures[subID].namePl);
  1321. ynd.text << tmp;
  1322. cb->showBlockingDialog(&ynd,boost::bind(&CGCreature::joinDecision,this,h,action,_1));
  1323. break;
  1324. }
  1325. }
  1326. }
  1327. void CGCreature::endBattle( BattleResult *result ) const
  1328. {
  1329. if(result->winner==0)
  1330. {
  1331. cb->removeObject(id);
  1332. }
  1333. else
  1334. {
  1335. int killedAmount=0;
  1336. for(std::set<std::pair<ui32,si32> >::iterator i=result->casualties[1].begin(); i!=result->casualties[1].end(); i++)
  1337. if(i->first == subID)
  1338. killedAmount += i->second;
  1339. cb->setAmount(id, army.slots.find(0)->second.second - killedAmount);
  1340. MetaString ms;
  1341. int pom = CCreature::getQuantityID(army.slots.find(0)->second.second);
  1342. pom = 174 + 3*pom + 1;
  1343. ms << std::pair<ui8,ui32>(6,pom) << " " << std::pair<ui8,ui32>(7,subID);
  1344. cb->setHoverName(id,&ms);
  1345. }
  1346. }
  1347. void CGCreature::initObj()
  1348. {
  1349. blockVisit = true;
  1350. switch(character)
  1351. {
  1352. case 0:
  1353. character = 0;
  1354. break;
  1355. case 1:
  1356. character = 1 + ran()%7;
  1357. break;
  1358. case 2:
  1359. character = 1 + ran()%10;
  1360. break;
  1361. case 3:
  1362. character = 4 + ran()%7;
  1363. break;
  1364. case 4:
  1365. character = 10;
  1366. break;
  1367. }
  1368. army.slots[0].first = subID;
  1369. si32 &amount = army.slots[0].second;
  1370. CCreature &c = VLC->creh->creatures[subID];
  1371. if(!amount)
  1372. if(c.ammMax == c.ammMin)
  1373. amount = c.ammMax;
  1374. else
  1375. amount = c.ammMin + (ran() % (c.ammMax - c.ammMin));
  1376. MetaString ms;
  1377. int pom = CCreature::getQuantityID(army.slots.find(0)->second.second);
  1378. pom = 174 + 3*pom + 1;
  1379. ms << std::pair<ui8,ui32>(6,pom) << " " << std::pair<ui8,ui32>(7,subID);
  1380. hoverName = toString(ms);
  1381. }
  1382. int CGCreature::takenAction(const CGHeroInstance *h, bool allowJoin) const
  1383. {
  1384. double hlp = h->getTotalStrength() / getArmyStrength();
  1385. if(!character) //compliant creatures will always join
  1386. return 0;
  1387. else if(allowJoin)//test for joining
  1388. {
  1389. int factor;
  1390. if(hlp >= 7)
  1391. factor = 11;
  1392. else if(hlp >= 1)
  1393. factor = 2*(hlp-1);
  1394. else if(hlp >= 0.5)
  1395. factor = -1;
  1396. else if(hlp >= 0.333)
  1397. factor = -2;
  1398. else
  1399. factor = -3;
  1400. int sympathy = 0;
  1401. std::set<ui32> myKindCres; //what creatures are the same kind as we
  1402. myKindCres.insert(subID); //we
  1403. myKindCres.insert(VLC->creh->creatures[subID].upgrades.begin(),VLC->creh->creatures[subID].upgrades.end()); //our upgrades
  1404. for(std::vector<CCreature>::iterator i=VLC->creh->creatures.begin(); i!=VLC->creh->creatures.end(); i++)
  1405. if(vstd::contains(i->upgrades,id)) //it's our base creatures
  1406. myKindCres.insert(i->idNumber);
  1407. int count = 0, //how many creatures of our kind has hero
  1408. totalCount = 0;
  1409. for (std::map<si32,std::pair<ui32,si32> >::const_iterator i = h->army.slots.begin(); i != h->army.slots.end(); i++)
  1410. {
  1411. if(vstd::contains(myKindCres,i->second.first))
  1412. count += i->second.second;
  1413. totalCount += i->second.second;
  1414. }
  1415. if(count*2 > totalCount)
  1416. sympathy++;
  1417. if(count)
  1418. sympathy++;
  1419. int charisma = factor + h->getSecSkillLevel(4) + sympathy;
  1420. if(charisma >= character) //creatures might join...
  1421. {
  1422. if(h->getSecSkillLevel(4) + sympathy + 1 >= character)
  1423. return 0; //join for free
  1424. else if(h->getSecSkillLevel(4) * 2 + sympathy + 1 >= character)
  1425. return VLC->creh->creatures[subID].cost[6] * army.slots.find(0)->second.second; //join for gold
  1426. }
  1427. }
  1428. //we are still here - creatures not joined heroes, test for fleeing
  1429. //TODO: it's provisional formula, should be replaced with original one (or something closer to it)
  1430. //TODO: should be deterministic (will be needed for Vision spell)
  1431. int hlp2 = (int) (hlp - 2)*1000;
  1432. if(!neverFlees
  1433. && hlp2 >= 0
  1434. && rand()%2000 < hlp2
  1435. )
  1436. return -1; //flee
  1437. else
  1438. return -2; //fight
  1439. }
  1440. void CGCreature::fleeDecision(const CGHeroInstance *h, ui32 pursue) const
  1441. {
  1442. if(pursue)
  1443. {
  1444. fight(h);
  1445. }
  1446. else
  1447. {
  1448. cb->removeObject(id);
  1449. }
  1450. }
  1451. void CGCreature::joinDecision(const CGHeroInstance *h, int cost, ui32 accept) const
  1452. {
  1453. if(!accept)
  1454. {
  1455. if(takenAction(h,false) == -1) //they flee
  1456. {
  1457. flee(h);
  1458. }
  1459. else //they fight
  1460. {
  1461. InfoWindow iw;
  1462. iw.player = h->tempOwner;
  1463. iw.text << std::pair<ui8,ui32>(11,87); //Insulted by your refusal of their offer, the monsters attack!
  1464. cb->showInfoDialog(&iw);
  1465. fight(h);
  1466. }
  1467. }
  1468. else //accepted
  1469. {
  1470. if (cb->getResource(h->tempOwner,6) < cost) //player don't have enough gold!
  1471. {
  1472. InfoWindow iw;
  1473. iw.player = h->tempOwner;
  1474. iw.text << std::pair<ui8,ui32>(1,29); //You don't have enough gold
  1475. cb->showInfoDialog(&iw);
  1476. //act as if player refused
  1477. joinDecision(h,cost,true);
  1478. return;
  1479. }
  1480. //take gold
  1481. if(cost)
  1482. cb->giveResource(h->tempOwner,6,-cost);
  1483. int slot = h->army.getSlotFor(subID);
  1484. if(slot >= 0) //there is place
  1485. {
  1486. //add creatures
  1487. SetGarrisons sg;
  1488. sg.garrs[h->id] = h->army;
  1489. if(vstd::contains(h->army.slots,slot)) //add to already present stack
  1490. {
  1491. sg.garrs[h->id].slots[slot].second += army.slots.find(0)->second.second;
  1492. }
  1493. else //add as a new stack
  1494. {
  1495. sg.garrs[h->id].slots[slot] = army.slots.find(0)->second;
  1496. }
  1497. cb->sendAndApply(&sg);
  1498. cb->removeObject(id);
  1499. }
  1500. else
  1501. {
  1502. cb->showGarrisonDialog(id,h->id,boost::bind(&IGameCallback::removeObject,cb,id)); //show garrison window and remove ourselves from map when player ends
  1503. }
  1504. }
  1505. }
  1506. void CGCreature::fight( const CGHeroInstance *h ) const
  1507. {
  1508. cb->startBattleI(h->id,army,pos,boost::bind(&CGCreature::endBattle,this,_1));
  1509. }
  1510. void CGCreature::flee( const CGHeroInstance * h ) const
  1511. {
  1512. BlockingDialog ynd(true,false);
  1513. ynd.player = h->tempOwner;
  1514. ynd.text << std::pair<ui8,ui32>(11,91);
  1515. ynd.text.replacements.push_back(VLC->creh->creatures[subID].namePl);
  1516. cb->showBlockingDialog(&ynd,boost::bind(&CGCreature::fleeDecision,this,h,_1));
  1517. }
  1518. void CGMine::onHeroVisit( const CGHeroInstance * h ) const
  1519. {
  1520. if(subID == 7) //TODO: support for abandoned mine
  1521. return;
  1522. if(h->tempOwner == tempOwner) //we're visiting our mine
  1523. {
  1524. cb->showGarrisonDialog(id,h->id,0);
  1525. return;
  1526. }
  1527. //TODO: check if mine is guarded
  1528. cb->setOwner(id,h->tempOwner); //not ours? flag it!
  1529. MetaString ms;
  1530. ms << std::pair<ui8,ui32>(9,subID) << " (" << std::pair<ui8,ui32>(6,23+h->tempOwner) << ")";
  1531. cb->setHoverName(id,&ms);
  1532. int vv=1; //amount of resource per turn
  1533. if (subID==0 || subID==2)
  1534. vv++;
  1535. else if (subID==6)
  1536. vv = 1000;
  1537. InfoWindow iw;
  1538. iw.soundID = soundBase::FLAGMINE;
  1539. iw.text << std::pair<ui8,ui32>(10,subID);
  1540. iw.player = h->tempOwner;
  1541. iw.components.push_back(Component(2,subID,vv,-1));
  1542. cb->showInfoDialog(&iw);
  1543. }
  1544. void CGMine::newTurn() const
  1545. {
  1546. if(cb->getDate() == 1)
  1547. return;
  1548. if (tempOwner == NEUTRAL_PLAYER)
  1549. return;
  1550. int vv = 1;
  1551. if (subID==0 || subID==2)
  1552. vv++;
  1553. else if (subID==6)
  1554. vv = 1000;
  1555. cb->giveResource(tempOwner,subID,vv);
  1556. }
  1557. void CGMine::initObj()
  1558. {
  1559. MetaString ms;
  1560. ms << std::pair<ui8,ui32>(9,subID);
  1561. if(tempOwner >= PLAYER_LIMIT)
  1562. tempOwner = NEUTRAL_PLAYER;
  1563. else
  1564. ms << " (" << std::pair<ui8,ui32>(6,23+tempOwner) << ")";
  1565. hoverName = toString(ms);
  1566. }
  1567. void CGResource::initObj()
  1568. {
  1569. blockVisit = true;
  1570. hoverName = VLC->generaltexth->restypes[subID];
  1571. if(!amount)
  1572. {
  1573. switch(subID)
  1574. {
  1575. case 6:
  1576. amount = 500 + (rand()%6)*100;
  1577. break;
  1578. case 0: case 2:
  1579. amount = 6 + (rand()%5);
  1580. break;
  1581. default:
  1582. amount = 3 + (rand()%3);
  1583. break;
  1584. }
  1585. }
  1586. }
  1587. void CGResource::onHeroVisit( const CGHeroInstance * h ) const
  1588. {
  1589. if(army.slots.size())
  1590. {
  1591. if(message.size())
  1592. {
  1593. BlockingDialog ynd(true,false);
  1594. ynd.player = h->getOwner();
  1595. ynd.text << message;
  1596. cb->showBlockingDialog(&ynd,boost::bind(&CGResource::fightForRes,this,_1,h));
  1597. }
  1598. else
  1599. {
  1600. fightForRes(1,h);
  1601. }
  1602. }
  1603. else
  1604. {
  1605. if(message.length())
  1606. {
  1607. InfoWindow iw;
  1608. iw.player = h->tempOwner;
  1609. iw.text << message;
  1610. cb->showInfoDialog(&iw);
  1611. }
  1612. collectRes(h->getOwner());
  1613. }
  1614. }
  1615. void CGResource::collectRes( int player ) const
  1616. {
  1617. cb->giveResource(player,subID,amount);
  1618. ShowInInfobox sii;
  1619. sii.player = player;
  1620. sii.c = Component(2,subID,amount,0);
  1621. sii.text << std::pair<ui8,ui32>(11,113);
  1622. sii.text.replacements.push_back(VLC->generaltexth->restypes[subID]);
  1623. cb->showCompInfo(&sii);
  1624. cb->removeObject(id);
  1625. }
  1626. void CGResource::fightForRes(ui32 agreed, const CGHeroInstance *h) const
  1627. {
  1628. if(agreed)
  1629. cb->startBattleI(h->id,army,pos,boost::bind(&CGResource::endBattle,this,_1,h));
  1630. }
  1631. void CGResource::endBattle( BattleResult *result, const CGHeroInstance *h ) const
  1632. {
  1633. if(result->winner == 0) //attacker won
  1634. collectRes(h->getOwner());
  1635. }
  1636. void CGVisitableOPW::newTurn() const
  1637. {
  1638. if (cb->getDate(1)==1) //first day of week
  1639. {
  1640. cb->setObjProperty(id,5,false);
  1641. MetaString ms; //set text to "not visited"
  1642. ms << std::pair<ui8,ui32>(3,ID) << " " << std::pair<ui8,ui32>(1,353);
  1643. cb->setHoverName(id,&ms);
  1644. }
  1645. }
  1646. void CGVisitableOPW::onHeroVisit( const CGHeroInstance * h ) const
  1647. {
  1648. int mid, sound = 0;
  1649. switch (ID)
  1650. {
  1651. case 55: //mystical garden
  1652. sound = soundBase::experience;
  1653. mid = 92;
  1654. break;
  1655. case 112://windmill
  1656. sound = soundBase::GENIE;
  1657. mid = 170;
  1658. break;
  1659. case 109://waterwheel
  1660. sound = soundBase::GENIE;
  1661. mid = 164;
  1662. break;
  1663. }
  1664. if (visited)
  1665. {
  1666. if (ID!=112)
  1667. mid++;
  1668. else
  1669. mid--;
  1670. InfoWindow iw;
  1671. iw.soundID = sound;
  1672. iw.player = h->tempOwner;
  1673. iw.text << std::pair<ui8,ui32>(11,mid);
  1674. cb->showInfoDialog(&iw);
  1675. }
  1676. else
  1677. {
  1678. int type, sub, val;
  1679. type = 2;
  1680. switch (ID)
  1681. {
  1682. case 55:
  1683. if (rand()%2)
  1684. {
  1685. sub = 5;
  1686. val = 5;
  1687. }
  1688. else
  1689. {
  1690. sub = 6;
  1691. val = 500;
  1692. }
  1693. break;
  1694. case 112:
  1695. mid = 170;
  1696. sub = (rand() % 5) + 1;
  1697. val = (rand() % 4) + 3;
  1698. break;
  1699. case 109:
  1700. mid = 164;
  1701. sub = 6;
  1702. if(cb->getDate(0)<8)
  1703. val = 500;
  1704. else
  1705. val = 1000;
  1706. }
  1707. cb->giveResource(h->tempOwner,sub,val);
  1708. InfoWindow iw;
  1709. iw.soundID = sound;
  1710. iw.player = h->tempOwner;
  1711. iw.components.push_back(Component(type,sub,val,0));
  1712. iw.text << std::pair<ui8,ui32>(11,mid);
  1713. cb->showInfoDialog(&iw);
  1714. cb->setObjProperty(id,5,true);
  1715. MetaString ms; //set text to "visited"
  1716. ms << std::pair<ui8,ui32>(3,ID) << " " << std::pair<ui8,ui32>(1,352);
  1717. cb->setHoverName(id,&ms);
  1718. }
  1719. }
  1720. void CGVisitableOPW::setPropertyDer( ui8 what, ui32 val )
  1721. {
  1722. if(what == 5)
  1723. visited = val;
  1724. }
  1725. void CGTeleport::onHeroVisit( const CGHeroInstance * h ) const
  1726. {
  1727. int destinationid=-1;
  1728. switch(ID)
  1729. {
  1730. case 43: //one way - find correspong exit monolith
  1731. if(vstd::contains(objs,44) && vstd::contains(objs[44],subID) && objs[44][subID].size())
  1732. destinationid = objs[44][subID][rand()%objs[44][subID].size()];
  1733. else
  1734. tlog2 << "Cannot find corresponding exit monolith for "<< id << std::endl;
  1735. break;
  1736. case 45: //two way monolith - pick any other one
  1737. if(vstd::contains(objs,45) && vstd::contains(objs[45],subID) && objs[45][subID].size()>1)
  1738. while ((destinationid = objs[45][subID][rand()%objs[45][subID].size()])==id);
  1739. else
  1740. tlog2 << "Cannot find corresponding exit monolith for "<< id << std::endl;
  1741. break;
  1742. case 103: //find nearest subterranean gate on the other level
  1743. {
  1744. std::pair<int,double> best(-1,150000); //pair<id,dist>
  1745. for(int i=0; i<objs[103][0].size(); i++)
  1746. {
  1747. if(cb->getObj(objs[103][0][i])->pos.z == pos.z) continue; //gates on our level are not interesting
  1748. double hlp = cb->getObj(objs[103][0][i])->pos.dist2d(pos);
  1749. if(hlp<best.second)
  1750. {
  1751. best.first = objs[103][0][i];
  1752. best.second = hlp;
  1753. }
  1754. }
  1755. if(best.first<0)
  1756. return;
  1757. else
  1758. destinationid = best.first;
  1759. break;
  1760. }
  1761. }
  1762. if(destinationid < 0)
  1763. {
  1764. tlog2 << "Cannot find exit... :( \n";
  1765. return;
  1766. }
  1767. cb->moveHero(h->id,
  1768. (ID!=103)
  1769. ? (CGHeroInstance::convertPosition(cb->getObj(destinationid)->pos,true))
  1770. : (cb->getObj(destinationid)->pos),
  1771. true);
  1772. }
  1773. void CGTeleport::initObj()
  1774. {
  1775. objs[ID][subID].push_back(id);
  1776. }
  1777. void CGArtifact::initObj()
  1778. {
  1779. blockVisit = true;
  1780. if(ID == 5)
  1781. hoverName = VLC->arth->artifacts[subID].Name();
  1782. }
  1783. void CGArtifact::onHeroVisit( const CGHeroInstance * h ) const
  1784. {
  1785. if(!army.slots.size())
  1786. {
  1787. if(ID == 5)
  1788. {
  1789. InfoWindow iw;
  1790. iw.player = h->tempOwner;
  1791. iw.components.push_back(Component(4,subID,0,0));
  1792. if(message.length())
  1793. iw.text << message;
  1794. else
  1795. iw.text << std::pair<ui8,ui32>(12,subID);
  1796. cb->showInfoDialog(&iw);
  1797. }
  1798. pick(h);
  1799. }
  1800. else
  1801. {
  1802. if(message.size())
  1803. {
  1804. BlockingDialog ynd(true,false);
  1805. ynd.player = h->getOwner();
  1806. ynd.text << message;
  1807. cb->showBlockingDialog(&ynd,boost::bind(&CGArtifact::fightForArt,this,_1,h));
  1808. }
  1809. else
  1810. {
  1811. fightForArt(0,h);
  1812. }
  1813. }
  1814. }
  1815. void CGArtifact::pick(const CGHeroInstance * h) const
  1816. {
  1817. if(ID == 5) //Artifact
  1818. {
  1819. cb->giveHeroArtifact(subID,h->id,-2);
  1820. }
  1821. else if(ID == 93) // Spell scroll
  1822. {
  1823. //TODO: support for the spell scroll
  1824. }
  1825. cb->removeObject(id);
  1826. }
  1827. void CGArtifact::fightForArt( ui32 agreed, const CGHeroInstance *h ) const
  1828. {
  1829. if(agreed)
  1830. cb->startBattleI(h->id,army,pos,boost::bind(&CGArtifact::endBattle,this,_1,h));
  1831. }
  1832. void CGArtifact::endBattle( BattleResult *result, const CGHeroInstance *h ) const
  1833. {
  1834. if(result->winner == 0) //attacker won
  1835. pick(h);
  1836. }
  1837. void CGPickable::initObj()
  1838. {
  1839. blockVisit = true;
  1840. switch(ID)
  1841. {
  1842. case 12: //campfire
  1843. val2 = (ran()%3) + 4; //4 - 6
  1844. val1 = val2 * 100;
  1845. type = ran()%6; //given resource
  1846. break;
  1847. case 101: //treasure chest
  1848. {
  1849. int hlp = ran()%100;
  1850. if(hlp >= 95)
  1851. {
  1852. type = 1;
  1853. val1 = VLC->arth->treasures[ran()%VLC->arth->treasures.size()]->id;
  1854. return;
  1855. }
  1856. else if (hlp >= 65)
  1857. {
  1858. val1 = 2000;
  1859. }
  1860. else if(hlp >= 33)
  1861. {
  1862. val1 = 1500;
  1863. }
  1864. else
  1865. {
  1866. val1 = 1000;
  1867. }
  1868. val2 = val1 - 500;
  1869. type = 0;
  1870. break;
  1871. }
  1872. }
  1873. }
  1874. void CGPickable::onHeroVisit( const CGHeroInstance * h ) const
  1875. {
  1876. switch(ID)
  1877. {
  1878. case 12: //campfire
  1879. {
  1880. cb->giveResource(h->tempOwner,type,val2); //non-gold resource
  1881. cb->giveResource(h->tempOwner,6,val1);//gold
  1882. InfoWindow iw;
  1883. iw.soundID = soundBase::experience;
  1884. iw.player = h->tempOwner;
  1885. iw.components.push_back(Component(2,6,val1,0));
  1886. iw.components.push_back(Component(2,type,val2,0));
  1887. iw.text << std::pair<ui8,ui32>(11,23);
  1888. cb->showInfoDialog(&iw);
  1889. break;
  1890. }
  1891. case 101: //treasure chest
  1892. {
  1893. if (subID) //not OH3 treasure chest
  1894. {
  1895. tlog2 << "Not supported WoG treasure chest!\n";
  1896. return;
  1897. }
  1898. if(type) //there is an artifact
  1899. {
  1900. cb->giveHeroArtifact(val1,h->id,-2);
  1901. InfoWindow iw;
  1902. iw.soundID = soundBase::treasure;
  1903. iw.player = h->tempOwner;
  1904. iw.components.push_back(Component(4,val1,1,0));
  1905. iw.text << std::pair<ui8,ui32>(11,145);
  1906. iw.text.replacements.push_back(VLC->arth->artifacts[val1].Name());
  1907. cb->showInfoDialog(&iw);
  1908. break;
  1909. }
  1910. else
  1911. {
  1912. BlockingDialog sd(false,true);
  1913. sd.player = h->tempOwner;
  1914. sd.text << std::pair<ui8,ui32>(11,146);
  1915. sd.components.push_back(Component(2,6,val1,0));
  1916. sd.components.push_back(Component(5,0,val2,0));
  1917. sd.soundID = soundBase::chest;
  1918. boost::function<void(ui32)> fun = boost::bind(&CGPickable::chosen,this,_1,h->id);
  1919. cb->showBlockingDialog(&sd,fun);
  1920. return;
  1921. }
  1922. }
  1923. }
  1924. cb->removeObject(id);
  1925. }
  1926. void CGPickable::chosen( int which, int heroID ) const
  1927. {
  1928. switch(which)
  1929. {
  1930. case 1: //player pick gold
  1931. cb->giveResource(cb->getOwner(heroID),6,val1);
  1932. break;
  1933. case 2: //player pick exp
  1934. cb->changePrimSkill(heroID, 4, val2);
  1935. break;
  1936. default:
  1937. throw std::string("Unhandled treasure choice");
  1938. }
  1939. cb->removeObject(id);
  1940. }
  1941. void CGWitchHut::initObj()
  1942. {
  1943. ability = allowedAbilities[ran()%allowedAbilities.size()];
  1944. }
  1945. void CGWitchHut::onHeroVisit( const CGHeroInstance * h ) const
  1946. {
  1947. InfoWindow iw;
  1948. iw.soundID = soundBase::gazebo;
  1949. iw.player = h->getOwner();
  1950. if(!hasVisited(h->tempOwner))
  1951. cb->setObjProperty(id,10,h->tempOwner);
  1952. if(h->getSecSkillLevel(ability)) //you alredy know this skill
  1953. {
  1954. iw.text << std::pair<ui8,ui32>(11,172);
  1955. iw.text.replacements.push_back(VLC->generaltexth->skillName[ability]);
  1956. }
  1957. else if(h->secSkills.size() >= SKILL_PER_HERO) //already all skills slots used
  1958. {
  1959. iw.text << std::pair<ui8,ui32>(11,173);
  1960. iw.text.replacements.push_back(VLC->generaltexth->skillName[ability]);
  1961. }
  1962. else //give sec skill
  1963. {
  1964. iw.components.push_back(Component(1, ability, 1, 0));
  1965. iw.text << std::pair<ui8,ui32>(11,171);
  1966. iw.text.replacements.push_back(VLC->generaltexth->skillName[ability]);
  1967. cb->changeSecSkill(h->id,ability,1,true);
  1968. }
  1969. cb->showInfoDialog(&iw);
  1970. }
  1971. const std::string & CGWitchHut::getHoverText() const
  1972. {
  1973. hoverName = VLC->generaltexth->names[ID];
  1974. if(hasVisited(cb->getCurrentPlayer())) //TODO: use local player, not current
  1975. {
  1976. hoverName += "\n" + VLC->generaltexth->allTexts[356]; // + (learn %s)
  1977. boost::algorithm::replace_first(hoverName,"%s",VLC->generaltexth->skillName[ability]);
  1978. if(cb->getSelectedHero(cb->getCurrentPlayer())->getSecSkillLevel(ability)) //hero knows that ability
  1979. hoverName += "\n\n" + VLC->generaltexth->allTexts[357]; // (Already learned)
  1980. }
  1981. return hoverName;
  1982. }
  1983. void CGDwelling::onHeroVisit( const CGHeroInstance * h ) const
  1984. {
  1985. }
  1986. void CGDwelling::initObj()
  1987. {
  1988. }
  1989. void CGBonusingObject::onHeroVisit( const CGHeroInstance * h ) const
  1990. {
  1991. bool visited = h->getBonus(HeroBonus::OBJECT,ID);
  1992. int messageID, bonusType, bonusVal;
  1993. int bonusMove = 0, sound = -1;
  1994. InfoWindow iw;
  1995. iw.player = h->tempOwner;
  1996. GiveBonus gbonus;
  1997. gbonus.hid = h->id;
  1998. gbonus.bonus.duration = HeroBonus::ONE_BATTLE;
  1999. gbonus.bonus.source = HeroBonus::OBJECT;
  2000. gbonus.bonus.id = ID;
  2001. switch(ID)
  2002. {
  2003. case 14: //swan pond
  2004. messageID = 29;
  2005. sound = soundBase::LUCK;
  2006. gbonus.bonus.type = HeroBonus::LUCK;
  2007. gbonus.bonus.val = 2;
  2008. gbonus.bdescr << std::pair<ui8,ui32>(6,67);
  2009. bonusMove = -h->movement;
  2010. break;
  2011. case 28: //Faerie Ring
  2012. messageID = 49;
  2013. sound = soundBase::LUCK;
  2014. gbonus.bonus.type = HeroBonus::LUCK;
  2015. gbonus.bonus.val = 1;
  2016. gbonus.bdescr << std::pair<ui8,ui32>(6,71);
  2017. break;
  2018. case 30: //fountain of fortune
  2019. messageID = 55;
  2020. sound = soundBase::LUCK;
  2021. gbonus.bonus.type = HeroBonus::LUCK;
  2022. gbonus.bonus.val = rand()%5 - 1;
  2023. gbonus.bdescr << std::pair<ui8,ui32>(6,69);
  2024. gbonus.bdescr.replacements.push_back((gbonus.bonus.val<0 ? "-" : "+") + boost::lexical_cast<std::string>(gbonus.bonus.val));
  2025. break;
  2026. case 38: //idol of fortune
  2027. messageID = 62;
  2028. iw.soundID = soundBase::experience;
  2029. if(cb->getDate(1) == 7) //7th day of week
  2030. gbonus.bonus.type = HeroBonus::MORALE_AND_LUCK;
  2031. else
  2032. gbonus.bonus.type = (cb->getDate(1)%2) ? HeroBonus::LUCK : HeroBonus::MORALE;
  2033. gbonus.bonus.val = 1;
  2034. gbonus.bdescr << std::pair<ui8,ui32>(6,68);
  2035. break;
  2036. case 64: //Rally Flag
  2037. sound = soundBase::MORALE;
  2038. messageID = 111;
  2039. gbonus.bonus.type = HeroBonus::MORALE_AND_LUCK;
  2040. gbonus.bonus.val = 1;
  2041. gbonus.bdescr << std::pair<ui8,ui32>(6,102);
  2042. bonusMove = 400;
  2043. break;
  2044. case 56: //oasis
  2045. messageID = 95;
  2046. gbonus.bonus.type = HeroBonus::MORALE;
  2047. gbonus.bonus.val = 1;
  2048. gbonus.bdescr << std::pair<ui8,ui32>(6,95);
  2049. bonusMove = 800;
  2050. break;
  2051. case 96: //temple
  2052. messageID = 140;
  2053. iw.soundID = soundBase::temple;
  2054. gbonus.bonus.type = HeroBonus::MORALE;
  2055. if(cb->getDate(1)==7) //sunday
  2056. {
  2057. gbonus.bonus.val = 2;
  2058. gbonus.bdescr << std::pair<ui8,ui32>(6,97);
  2059. }
  2060. else
  2061. {
  2062. gbonus.bonus.val = 1;
  2063. gbonus.bdescr << std::pair<ui8,ui32>(6,96);
  2064. }
  2065. break;
  2066. case 110://Watering Hole
  2067. sound = soundBase::MORALE;
  2068. messageID = 166;
  2069. gbonus.bonus.type = HeroBonus::MORALE;
  2070. gbonus.bonus.val = 1;
  2071. gbonus.bdescr << std::pair<ui8,ui32>(6,100);
  2072. bonusMove = 400;
  2073. break;
  2074. case 31: //Fountain of Youth
  2075. sound = soundBase::MORALE;
  2076. messageID = 57;
  2077. gbonus.bonus.type = HeroBonus::MORALE;
  2078. gbonus.bonus.val = 1;
  2079. gbonus.bdescr << std::pair<ui8,ui32>(6,103);
  2080. bonusMove = 400;
  2081. break;
  2082. }
  2083. if(visited)
  2084. {
  2085. if(ID==64 || ID==96 || ID==56)
  2086. messageID--;
  2087. else
  2088. messageID++;
  2089. }
  2090. else
  2091. {
  2092. if(gbonus.bonus.type == HeroBonus::MORALE || gbonus.bonus.type == HeroBonus::MORALE_AND_LUCK)
  2093. iw.components.push_back(Component(8,0,gbonus.bonus.val,0));
  2094. if(gbonus.bonus.type == HeroBonus::LUCK || gbonus.bonus.type == HeroBonus::MORALE_AND_LUCK)
  2095. iw.components.push_back(Component(9,0,gbonus.bonus.val,0));
  2096. cb->giveHeroBonus(&gbonus);
  2097. if(bonusMove) //swan pond - take all move points
  2098. {
  2099. SetMovePoints smp;
  2100. smp.hid = h->id;
  2101. smp.val = h->movement + bonusMove;
  2102. cb->setMovePoints(&smp);
  2103. }
  2104. }
  2105. iw.soundID = sound;
  2106. iw.text << std::pair<ui8,ui32>(11,messageID);
  2107. cb->showInfoDialog(&iw);
  2108. }
  2109. const std::string & CGBonusingObject::getHoverText() const
  2110. {
  2111. const CGHeroInstance *h = cb->getSelectedHero(cb->getCurrentPlayer());
  2112. hoverName = VLC->generaltexth->names[ID];
  2113. if(h)
  2114. {
  2115. if(!h->getBonus(HeroBonus::OBJECT,ID))
  2116. hoverName += " " + VLC->generaltexth->allTexts[353]; //not visited
  2117. else
  2118. hoverName += " " + VLC->generaltexth->allTexts[352]; //visited
  2119. }
  2120. return hoverName;
  2121. }
  2122. void CGMagicWell::onHeroVisit( const CGHeroInstance * h ) const
  2123. {
  2124. int message;
  2125. InfoWindow iw;
  2126. iw.soundID = soundBase::faerie;
  2127. iw.player = h->tempOwner;
  2128. if(h->getBonus(HeroBonus::OBJECT,ID)) //has already visited Well today
  2129. {
  2130. message = 78;
  2131. }
  2132. else if(h->mana < h->manaLimit())
  2133. {
  2134. GiveBonus gbonus;
  2135. gbonus.bonus.type = HeroBonus::NONE;
  2136. gbonus.hid = h->id;
  2137. gbonus.bonus.duration = HeroBonus::ONE_DAY;
  2138. gbonus.bonus.source = HeroBonus::OBJECT;
  2139. gbonus.bonus.id = ID;
  2140. cb->giveHeroBonus(&gbonus);
  2141. cb->setManaPoints(h->id,h->manaLimit());
  2142. message = 77;
  2143. }
  2144. else
  2145. {
  2146. message = 79;
  2147. }
  2148. iw.text << std::pair<ui8,ui32>(11,message); //"A second drink at the well in one day will not help you."
  2149. cb->showInfoDialog(&iw);
  2150. }
  2151. const std::string & CGMagicWell::getHoverText() const
  2152. {
  2153. const CGHeroInstance *h = cb->getSelectedHero(cb->getCurrentPlayer());
  2154. hoverName = VLC->generaltexth->names[ID];
  2155. if(h)
  2156. {
  2157. if(!h->getBonus(HeroBonus::OBJECT,ID))
  2158. hoverName += " " + VLC->generaltexth->allTexts[353]; //not visited
  2159. else
  2160. hoverName += " " + VLC->generaltexth->allTexts[352]; //visited
  2161. }
  2162. return hoverName;
  2163. }
  2164. void CGEvent::onHeroVisit( const CGHeroInstance * h ) const
  2165. {
  2166. if(!(availableFor & (1 << h->tempOwner)))
  2167. return;
  2168. if(cb->getPlayerSettings(h->tempOwner)->human)
  2169. {
  2170. if(humanActivate)
  2171. activated(h);
  2172. }
  2173. else if(computerActivate)
  2174. activated(h);
  2175. }
  2176. void CGEvent::endBattle( const CGHeroInstance *h, BattleResult *result ) const
  2177. {
  2178. if(result->winner)
  2179. return;
  2180. giveContents(h,true);
  2181. }
  2182. void CGEvent::activated( const CGHeroInstance * h ) const
  2183. {
  2184. if(army)
  2185. {
  2186. InfoWindow iw;
  2187. iw.player = h->tempOwner;
  2188. iw.text << message;
  2189. cb->showInfoDialog(&iw);
  2190. cb->startBattleI(h->id,army,pos,boost::bind(&CGEvent::endBattle,this,h,_1));
  2191. }
  2192. else
  2193. {
  2194. giveContents(h,false);
  2195. }
  2196. }
  2197. void CGEvent::giveContents( const CGHeroInstance *h, bool afterBattle ) const
  2198. {
  2199. InfoWindow iw;
  2200. iw.player = h->getOwner();
  2201. bool changesPrimSkill = false;
  2202. for (int i = 0; i < primskills.size(); i++)
  2203. {
  2204. if(primskills[i])
  2205. {
  2206. changesPrimSkill = true;
  2207. break;
  2208. }
  2209. }
  2210. if(gainedExp || changesPrimSkill || abilities.size())
  2211. {
  2212. getText(iw,afterBattle,175,h);
  2213. if(gainedExp)
  2214. iw.components.push_back(Component(Component::EXPERIENCE,0,gainedExp,0));
  2215. for(int i=0; i<primskills.size(); i++)
  2216. if(primskills[i])
  2217. iw.components.push_back(Component(Component::PRIM_SKILL,i,primskills[i],0));
  2218. for(int i=0; i<abilities.size(); i++)
  2219. iw.components.push_back(Component(Component::SEC_SKILL,abilities[i],abilityLevels[i],0));
  2220. cb->showInfoDialog(&iw);
  2221. //give exp
  2222. if(gainedExp)
  2223. cb->changePrimSkill(h->id,5,gainedExp,false);
  2224. //give prim skills
  2225. for(int i=0; i<primskills.size(); i++)
  2226. if(primskills[i])
  2227. cb->changePrimSkill(h->id,i,primskills[i],false);
  2228. //give sec skills
  2229. for(int i=0; i<abilities.size(); i++)
  2230. {
  2231. int curLev = h->getSecSkillLevel(abilities[i]);
  2232. if(curLev && curLev < abilityLevels[i]
  2233. || h->secSkills.size() < SKILL_PER_HERO )
  2234. {
  2235. cb->changeSecSkill(h->id,abilities[i],abilityLevels[i],true);
  2236. }
  2237. }
  2238. }
  2239. if(spells.size())
  2240. {
  2241. std::set<ui32> spellsToGive;
  2242. iw.components.clear();
  2243. for(int i=0; i<spells.size(); i++)
  2244. {
  2245. iw.components.push_back(Component(Component::SPELL,spells[i],0,0));
  2246. spellsToGive.insert(spells[i]);
  2247. }
  2248. if(spellsToGive.size())
  2249. {
  2250. cb->changeSpells(h->id,true,spellsToGive);
  2251. cb->showInfoDialog(&iw);
  2252. }
  2253. }
  2254. if(manaDiff)
  2255. {
  2256. getText(iw,afterBattle,manaDiff,176,177,h);
  2257. iw.components.push_back(Component(Component::PRIM_SKILL,5,manaDiff,0));
  2258. cb->showInfoDialog(&iw);
  2259. cb->setManaPoints(h->id, h->mana + manaDiff);
  2260. }
  2261. if(moraleDiff)
  2262. {
  2263. getText(iw,afterBattle,moraleDiff,178,179,h);
  2264. iw.components.push_back(Component(Component::MORALE,0,moraleDiff,0));
  2265. cb->showInfoDialog(&iw);
  2266. GiveBonus gb;
  2267. gb.bonus = HeroBonus(HeroBonus::ONE_BATTLE,HeroBonus::MORALE,HeroBonus::OBJECT,moraleDiff,id,"");
  2268. gb.hid = h->id;
  2269. cb->giveHeroBonus(&gb);
  2270. }
  2271. if(luckDiff)
  2272. {
  2273. getText(iw,afterBattle,luckDiff,180,181,h);
  2274. iw.components.push_back(Component(Component::LUCK,0,luckDiff,0));
  2275. cb->showInfoDialog(&iw);
  2276. GiveBonus gb;
  2277. gb.bonus = HeroBonus(HeroBonus::ONE_BATTLE,HeroBonus::LUCK,HeroBonus::OBJECT,luckDiff,id,"");
  2278. gb.hid = h->id;
  2279. cb->giveHeroBonus(&gb);
  2280. }
  2281. iw.components.clear();
  2282. iw.text.clear();
  2283. for(int i=0; i<resources.size(); i++)
  2284. {
  2285. if(resources[i] < 0)
  2286. iw.components.push_back(Component(Component::RESOURCE,i,resources[i],0));
  2287. }
  2288. if(iw.components.size())
  2289. {
  2290. getText(iw,afterBattle,182,h);
  2291. cb->showInfoDialog(&iw);
  2292. }
  2293. iw.components.clear();
  2294. iw.text.clear();
  2295. for(int i=0; i<resources.size(); i++)
  2296. {
  2297. if(resources[i] > 0)
  2298. iw.components.push_back(Component(Component::RESOURCE,i,resources[i],0));
  2299. }
  2300. if(iw.components.size())
  2301. {
  2302. getText(iw,afterBattle,183,h);
  2303. cb->showInfoDialog(&iw);
  2304. }
  2305. iw.components.clear();
  2306. for(int i=0; i<artifacts.size(); i++)
  2307. {
  2308. iw.components.push_back(Component(Component::ARTIFACT,artifacts[i],0,0));
  2309. }
  2310. if(iw.components.size())
  2311. {
  2312. cb->showInfoDialog(&iw);
  2313. }
  2314. for(int i=0; i<resources.size(); i++)
  2315. if(resources[i])
  2316. cb->giveResource(h->getOwner(),i,resources[i]);
  2317. for(int i=0; i<artifacts.size(); i++)
  2318. cb->giveHeroArtifact(artifacts[i],h->id,-2);
  2319. //show dialog with given creatures
  2320. iw.components.clear();
  2321. iw.text.clear();
  2322. for(std::map<si32,std::pair<ui32,si32> >::const_iterator i = creatures.slots.begin(); i != creatures.slots.end(); i++)
  2323. {
  2324. iw.components.push_back(Component(Component::CREATURE,i->second.first,i->second.second,0));
  2325. }
  2326. if(iw.components.size())
  2327. {
  2328. if(afterBattle)
  2329. {
  2330. if(iw.components.front().val == 1)
  2331. {
  2332. iw.text.addTxt(MetaString::ADVOB_TXT,185);//A %s joins %s's army.
  2333. iw.text.replacements.push_back(VLC->creh->creatures[iw.components.front().subtype].nameSing);
  2334. }
  2335. else
  2336. {
  2337. iw.text.addTxt(MetaString::ADVOB_TXT,186);//%s join %s's army.
  2338. iw.text.replacements.push_back(VLC->creh->creatures[iw.components.front().subtype].namePl);
  2339. }
  2340. iw.text.replacements.push_back(h->name);
  2341. }
  2342. else
  2343. {
  2344. iw.text << message;
  2345. afterBattle = true;
  2346. }
  2347. cb->showInfoDialog(&iw);
  2348. }
  2349. //check if creatures can be moved to hero army
  2350. CCreatureSet heroArmy = h->army;
  2351. CCreatureSet ourArmy = creatures;
  2352. while(ourArmy)
  2353. {
  2354. int slot = heroArmy.getSlotFor(ourArmy.slots.begin()->second.first);
  2355. if(slot < 0)
  2356. break;
  2357. heroArmy.slots[slot].first = ourArmy.slots.begin()->second.first;
  2358. heroArmy.slots[slot].second += ourArmy.slots.begin()->second.second;
  2359. ourArmy.slots.erase(ourArmy.slots.begin());
  2360. }
  2361. if(!ourArmy) //all creatures can be moved to hero army - do that
  2362. {
  2363. SetGarrisons sg;
  2364. sg.garrs[h->id] = heroArmy;
  2365. cb->sendAndApply(&sg);
  2366. }
  2367. else //show garrison window and let player pick creatures
  2368. {
  2369. SetGarrisons sg;
  2370. sg.garrs[id] = creatures;
  2371. cb->sendAndApply(&sg);
  2372. if(removeAfterVisit)
  2373. cb->showGarrisonDialog(id,h->id,boost::bind(&IGameCallback::removeObject,cb,id));
  2374. else
  2375. cb->showGarrisonDialog(id,h->id,0);
  2376. return;
  2377. }
  2378. if(!afterBattle)
  2379. {
  2380. iw.text << message;
  2381. cb->showInfoDialog(&iw);
  2382. }
  2383. if(removeAfterVisit)
  2384. cb->removeObject(id);
  2385. }
  2386. void CGEvent::getText( InfoWindow &iw, bool &afterBattle, int text, const CGHeroInstance * h ) const
  2387. {
  2388. if(afterBattle)
  2389. {
  2390. iw.text.addTxt(MetaString::ADVOB_TXT,text);//%s has lost treasure.
  2391. iw.text.replacements.push_back(h->name);
  2392. }
  2393. else
  2394. {
  2395. iw.text << message;
  2396. afterBattle = true;
  2397. }
  2398. }
  2399. void CGEvent::getText( InfoWindow &iw, bool &afterBattle, int val, int positive, int negative, const CGHeroInstance * h ) const
  2400. {
  2401. iw.components.clear();
  2402. iw.text.clear();
  2403. if(afterBattle)
  2404. {
  2405. iw.text.addTxt(MetaString::ADVOB_TXT,val < 0 ? negative : positive); //%s's luck takes a turn for the worse / %s's luck increases
  2406. iw.text.replacements.push_back(h->name);
  2407. }
  2408. else
  2409. {
  2410. iw.text << message;
  2411. afterBattle = true;
  2412. }
  2413. }
  2414. void CGObservatory::onHeroVisit( const CGHeroInstance * h ) const
  2415. {
  2416. InfoWindow iw;
  2417. iw.soundID = soundBase::LIGHTHOUSE;
  2418. iw.player = h->tempOwner;
  2419. iw.text.addTxt(MetaString::ADVOB_TXT,98 + (ID==60));
  2420. cb->showInfoDialog(&iw);
  2421. FoWChange fw;
  2422. fw.player = h->tempOwner;
  2423. fw.mode = 1;
  2424. cb->getTilesInRange(fw.tiles,pos,20,h->tempOwner,1);
  2425. cb->sendAndApply(&fw);
  2426. }
  2427. void CGShrine::onHeroVisit( const CGHeroInstance * h ) const
  2428. {
  2429. if(spell == 255)
  2430. {
  2431. tlog1 << "Not initialized shrine visited!\n";
  2432. return;
  2433. }
  2434. if(!hasVisited(h->tempOwner))
  2435. cb->setObjProperty(id,10,h->tempOwner);
  2436. InfoWindow iw;
  2437. iw.soundID = soundBase::temple;
  2438. iw.player = h->getOwner();
  2439. iw.text.addTxt(MetaString::ADVOB_TXT,127 + ID - 88);
  2440. iw.text.addTxt(MetaString::SPELL_NAME,spell);
  2441. iw.text << ".";
  2442. if(!h->getArt(17)) //no spellbook
  2443. {
  2444. iw.text.addTxt(MetaString::ADVOB_TXT,131);
  2445. }
  2446. else if(ID == 90 && !h->getSecSkillLevel(7)) //it's third level spell and hero doesn't have wisdom
  2447. {
  2448. iw.text.addTxt(MetaString::ADVOB_TXT,130);
  2449. }
  2450. else if(vstd::contains(h->spells,spell))//hero already knows the spell
  2451. {
  2452. iw.text.addTxt(MetaString::ADVOB_TXT,174);
  2453. }
  2454. else //give spell
  2455. {
  2456. std::set<ui32> spells;
  2457. spells.insert(spell);
  2458. cb->changeSpells(h->id,true,spells);
  2459. iw.components.push_back(Component(Component::SPELL,spell,0,0));
  2460. }
  2461. cb->showInfoDialog(&iw);
  2462. }
  2463. void CGShrine::initObj()
  2464. {
  2465. if(spell == 255) //spell not set
  2466. {
  2467. int level = ID-87;
  2468. std::vector<ui32> possibilities;
  2469. //add all allowed spells of wanted level
  2470. for(int i=0; i<SPELLS_QUANTITY; i++)
  2471. {
  2472. if(VLC->spellh->spells[i].level == level
  2473. && cb->isAllowed(0,VLC->spellh->spells[i].id))
  2474. {
  2475. possibilities.push_back(VLC->spellh->spells[i].id);
  2476. }
  2477. }
  2478. if(!possibilities.size())
  2479. {
  2480. tlog1 << "Error: cannot init shrine, no allowed spells!\n";
  2481. return;
  2482. }
  2483. spell = possibilities[ran() % possibilities.size()];
  2484. }
  2485. }
  2486. const std::string & CGShrine::getHoverText() const
  2487. {
  2488. hoverName = VLC->generaltexth->names[ID];
  2489. if(hasVisited(cb->getCurrentPlayer())) //TODO: use local player, not current
  2490. {
  2491. hoverName += "\n" + VLC->generaltexth->allTexts[355]; // + (learn %s)
  2492. boost::algorithm::replace_first(hoverName,"%s",VLC->spellh->spells[spell].name);
  2493. const CGHeroInstance *h = cb->getSelectedHero(cb->getCurrentPlayer());
  2494. if(h && vstd::contains(h->spells,spell)) //hero knows that ability
  2495. hoverName += "\n\n" + VLC->generaltexth->allTexts[354]; // (Already learned)
  2496. }
  2497. return hoverName;
  2498. }
  2499. void CGSignBottle::initObj()
  2500. {
  2501. //if no text is set than we pick random from the predefined ones
  2502. if(!message.size())
  2503. message = VLC->generaltexth->randsign[ran()%VLC->generaltexth->randsign.size()];
  2504. }
  2505. void CGSignBottle::onHeroVisit( const CGHeroInstance * h ) const
  2506. {
  2507. InfoWindow iw;
  2508. iw.soundID = soundBase::STORE;
  2509. iw.player = h->getOwner();
  2510. iw.text << message;
  2511. cb->showInfoDialog(&iw);
  2512. }
  2513. void CGScholar::giveAnyBonus( const CGHeroInstance * h ) const
  2514. {
  2515. }
  2516. void CGScholar::onHeroVisit( const CGHeroInstance * h ) const
  2517. {
  2518. int type = bonusType;
  2519. int bid = bonusID;
  2520. //check if the bonus if applicable, if not - give primary skill (always possible)
  2521. int ssl = h->getSecSkillLevel(bid); //current sec skill level, used if bonusType == 1
  2522. if((type == 1
  2523. && ((ssl == 3) || (!ssl && h->secSkills.size() == SKILL_PER_HERO))) ////hero already has expert level in the skill or (don't know skill and doesn't have free slot)
  2524. || (type == 2 && (!h->getArt(17) || vstd::contains(h->spells,bid)))) //hero doesn't have a spellbook or already knows the spell
  2525. {
  2526. type = 0;
  2527. bid = ran() % PRIMARY_SKILLS;
  2528. }
  2529. InfoWindow iw;
  2530. iw.soundID = soundBase::gazebo;
  2531. iw.player = h->getOwner();
  2532. iw.text.addTxt(MetaString::ADVOB_TXT,115);
  2533. switch (type)
  2534. {
  2535. case 0:
  2536. cb->changePrimSkill(h->id,bid,+1);
  2537. iw.components.push_back(Component(Component::PRIM_SKILL,bid,ssl+1,0));
  2538. break;
  2539. case 1:
  2540. cb->changeSecSkill(h->id,bid,+1);
  2541. iw.components.push_back(Component(Component::SEC_SKILL,bid,ssl+1,0));
  2542. break;
  2543. case 2:
  2544. {
  2545. std::set<ui32> hlp;
  2546. hlp.insert(bid);
  2547. cb->changeSpells(h->id,true,hlp);
  2548. iw.components.push_back(Component(Component::SPELL,bid,0,0));
  2549. }
  2550. break;
  2551. default:
  2552. tlog1 << "Error: wrong bonustype (" << (int)type << ") for Scholar!\n";
  2553. return;
  2554. }
  2555. cb->showInfoDialog(&iw);
  2556. cb->removeObject(id);
  2557. }
  2558. void CGScholar::initObj()
  2559. {
  2560. blockVisit = true;
  2561. if(bonusType == 255)
  2562. {
  2563. bonusType = ran()%3;
  2564. switch(bonusType)
  2565. {
  2566. case 0:
  2567. bonusID = ran() % PRIMARY_SKILLS;
  2568. break;
  2569. case 1:
  2570. bonusID = ran() % SKILL_QUANTITY;
  2571. break;
  2572. case 2:
  2573. bonusID = ran() % SPELLS_QUANTITY;
  2574. break;
  2575. }
  2576. }
  2577. }
  2578. void CGOnceVisitable::onHeroVisit( const CGHeroInstance * h ) const
  2579. {
  2580. int sound = 0;
  2581. int txtid = -1;
  2582. switch(ID)
  2583. {
  2584. case 22: //Corpse
  2585. txtid = 37;
  2586. sound = soundBase::MYSTERY;
  2587. break;
  2588. case 39: //Lean To
  2589. sound = soundBase::GENIE;
  2590. txtid = 64;
  2591. break;
  2592. case 105://Wagon
  2593. sound = soundBase::GENIE;
  2594. txtid = 154;
  2595. break;
  2596. case 108:
  2597. break;
  2598. default:
  2599. tlog1 << "Error: Unknown object (" << ID <<") treated as CGOnceVisitable!\n";
  2600. return;
  2601. }
  2602. if(ID == 108)//Warrior's Tomb
  2603. {
  2604. //ask if player wants to search the Tomb
  2605. BlockingDialog bd(true, false);
  2606. sound = soundBase::GRAVEYARD;
  2607. bd.player = h->getOwner();
  2608. bd.text.addTxt(MetaString::ADVOB_TXT,161);
  2609. cb->showBlockingDialog(&bd,boost::bind(&CGOnceVisitable::searchTomb,this,h,_1));
  2610. return;
  2611. }
  2612. InfoWindow iw;
  2613. iw.soundID = sound;
  2614. iw.player = h->getOwner();
  2615. if(players.size()) //we have been already visited...
  2616. {
  2617. txtid++;
  2618. if(ID == 105) //wagon has extra text (for finding art) we need to ommit
  2619. txtid++;
  2620. iw.text.addTxt(MetaString::ADVOB_TXT, txtid);
  2621. }
  2622. else //first visit - give bonus!
  2623. {
  2624. if(ID == 105 && artOrRes == 1)
  2625. {
  2626. txtid++;
  2627. iw.text.replacements.push_back(VLC->arth->artifacts[bonusType].Name());
  2628. }
  2629. switch(artOrRes)
  2630. {
  2631. case 0:
  2632. txtid++;
  2633. break;
  2634. case 1: //art
  2635. iw.components.push_back(Component(Component::ARTIFACT,bonusType,0,0));
  2636. cb->giveHeroArtifact(bonusType,h->id,-2);
  2637. break;
  2638. case 2: //res
  2639. iw.components.push_back(Component(Component::RESOURCE,bonusType,bonusVal,0));
  2640. cb->giveResource(h->getOwner(),bonusType,bonusVal);
  2641. break;
  2642. }
  2643. iw.text.addTxt(MetaString::ADVOB_TXT, txtid);
  2644. }
  2645. cb->showInfoDialog(&iw);
  2646. cb->setObjProperty(id,10,h->getOwner());
  2647. }
  2648. const std::string & CGOnceVisitable::getHoverText() const
  2649. {
  2650. hoverName = VLC->generaltexth->names[ID] + " ";
  2651. hoverName += (hasVisited(cb->getCurrentPlayer())
  2652. ? (VLC->generaltexth->allTexts[352]) //visited
  2653. : ( VLC->generaltexth->allTexts[353])); //not visited
  2654. return hoverName;
  2655. }
  2656. void CGOnceVisitable::initObj()
  2657. {
  2658. switch(ID)
  2659. {
  2660. case 22: //Corpse
  2661. {
  2662. blockVisit = true;
  2663. int hlp = ran()%100;
  2664. if(hlp < 20)
  2665. {
  2666. artOrRes = 1;
  2667. std::vector<CArtifact*> arts;
  2668. cb->getAllowed(arts, CArtifact::ART_TREASURE | CArtifact::ART_MINOR | CArtifact::ART_MAJOR);
  2669. bonusType = arts[ran() % arts.size()]->id;
  2670. }
  2671. else
  2672. {
  2673. artOrRes = 0;
  2674. }
  2675. }
  2676. break;
  2677. case 39: //Lean To
  2678. {
  2679. artOrRes = 2;
  2680. bonusType = ran()%6; //any basic resource without gold
  2681. bonusVal = ran()%4 + 1;
  2682. break;
  2683. }
  2684. case 108://Warrior's Tomb
  2685. {
  2686. artOrRes = 1;
  2687. std::vector<CArtifact*> arts;
  2688. int hlp = ran()%100;
  2689. if(hlp < 30)
  2690. cb->getAllowed(arts,CArtifact::ART_TREASURE);
  2691. else if(hlp < 80)
  2692. cb->getAllowed(arts,CArtifact::ART_MINOR);
  2693. else if(hlp < 95)
  2694. cb->getAllowed(arts,CArtifact::ART_MAJOR);
  2695. else
  2696. cb->getAllowed(arts,CArtifact::ART_RELIC);
  2697. bonusType = arts[ran() % arts.size()]->id;
  2698. }
  2699. break;
  2700. case 105://Wagon
  2701. {
  2702. int hlp = ran()%100;
  2703. if(hlp < 10)
  2704. {
  2705. artOrRes = 0; // nothing... :(
  2706. }
  2707. else if(hlp < 50) //minor or treasure art
  2708. {
  2709. artOrRes = 1;
  2710. std::vector<CArtifact*> arts;
  2711. cb->getAllowed(arts, CArtifact::ART_TREASURE | CArtifact::ART_MINOR);
  2712. bonusType = arts[ran() % arts.size()]->id;
  2713. }
  2714. else //2 - 5 of non-gold resource
  2715. {
  2716. artOrRes = 2;
  2717. bonusType = ran()%6;
  2718. bonusVal = ran()%4 + 2;
  2719. }
  2720. break;
  2721. }
  2722. }
  2723. }
  2724. void CGOnceVisitable::searchTomb(const CGHeroInstance *h, ui32 accept) const
  2725. {
  2726. if(accept)
  2727. {
  2728. InfoWindow iw;
  2729. iw.player = h->getOwner();
  2730. iw.components.push_back(Component(Component::MORALE,0,-3,0));
  2731. if(players.size()) //we've been already visited, player found nothing
  2732. {
  2733. iw.text.addTxt(MetaString::ADVOB_TXT,163);
  2734. }
  2735. else //first visit - give artifact
  2736. {
  2737. iw.text.addTxt(MetaString::ADVOB_TXT,162);
  2738. iw.components.push_back(Component(Component::ARTIFACT,bonusType,0,0));
  2739. iw.text.replacements.push_back(VLC->arth->artifacts[bonusType].Name());
  2740. cb->giveHeroArtifact(bonusType,h->id,-2);
  2741. }
  2742. if(!h->getBonus(HeroBonus::OBJECT,ID)) //we don't have modifier from this object yet
  2743. {
  2744. //ruin morale
  2745. GiveBonus gb;
  2746. gb.hid = h->id;
  2747. gb.bonus = HeroBonus(HeroBonus::ONE_BATTLE,HeroBonus::MORALE,HeroBonus::OBJECT,-3,id,"");
  2748. gb.bdescr.addTxt(MetaString::ARRAY_TXT,104); //Warrior Tomb Visited -3
  2749. cb->giveHeroBonus(&gb);
  2750. }
  2751. cb->showInfoDialog(&iw);
  2752. //add player to the visitors (for visited tooltop)
  2753. cb->setObjProperty(id,10,h->getOwner());
  2754. }
  2755. }