CObjectHandler.cpp 78 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191
  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. recreateArtBonuses();
  530. }
  531. void CGHeroInstance::initHeroDefInfo()
  532. {
  533. if(!defInfo || defInfo->id != HEROI_TYPE)
  534. {
  535. defInfo = new CGDefInfo();
  536. defInfo->id = HEROI_TYPE;
  537. defInfo->subid = subID;
  538. defInfo->printPriority = 0;
  539. defInfo->visitDir = 0xff;
  540. }
  541. for(int i=0;i<6;i++)
  542. {
  543. defInfo->blockMap[i]=255;
  544. defInfo->visitMap[i]=0;
  545. }
  546. defInfo->handler=NULL;
  547. defInfo->blockMap[5] = 253;
  548. defInfo->visitMap[5] = 2;
  549. }
  550. CGHeroInstance::~CGHeroInstance()
  551. {
  552. }
  553. bool CGHeroInstance::needsLastStack() const
  554. {
  555. return true;
  556. }
  557. void CGHeroInstance::onHeroVisit(const CGHeroInstance * h) const
  558. {
  559. if(h == this) return; //exclude potential self-visiting
  560. if (ID == HEROI_TYPE) //hero
  561. {
  562. //TODO: check for allies
  563. if(tempOwner == h->tempOwner) //our hero
  564. {
  565. //exchange
  566. cb->heroExchange(id, h->id);
  567. }
  568. else
  569. {
  570. //battle
  571. cb->startBattleI(
  572. &h->army,
  573. &army,
  574. h->pos,
  575. h,
  576. this,
  577. 0);
  578. }
  579. }
  580. else if(ID == 62) //prison
  581. {
  582. InfoWindow iw;
  583. iw.player = h->tempOwner;
  584. iw.soundID = soundBase::ROGUE;
  585. if(cb->getHeroCount(h->tempOwner,false) < 8) //free hero slot
  586. {
  587. cb->changeObjPos(id,pos+int3(1,0,0),0);
  588. cb->setObjProperty(id,6,HEROI_TYPE); //set ID to 34
  589. cb->giveHero(id,h->tempOwner); //recreates def and adds hero to player
  590. iw.text << std::pair<ui8,ui32>(11,102);
  591. }
  592. else //already 8 wandering heroes
  593. {
  594. iw.text << std::pair<ui8,ui32>(11,103);
  595. }
  596. cb->showInfoDialog(&iw);
  597. }
  598. }
  599. const std::string & CGHeroInstance::getBiography() const
  600. {
  601. if (biography.length())
  602. return biography;
  603. else
  604. return VLC->generaltexth->hTxts[subID].biography;
  605. }
  606. void CGHeroInstance::initObj()
  607. {
  608. blockVisit = true;
  609. }
  610. int CGHeroInstance::getCurrentMorale( int stack, bool town ) const
  611. {
  612. int ret = 0;
  613. std::vector<std::pair<int,std::string> > mods = getCurrentMoraleModifiers(stack,town);
  614. for(int i=0; i < mods.size(); i++)
  615. ret += mods[i].first;
  616. if(ret > 3)
  617. return 3;
  618. if(ret < -3)
  619. return -3;
  620. return ret;
  621. }
  622. std::vector<std::pair<int,std::string> > CGHeroInstance::getCurrentMoraleModifiers( int stack/*=-1*/, bool town/*=false*/ ) const
  623. {
  624. //TODO: check if stack is undead/mechanic/elemental => always neutrl morale
  625. std::vector<std::pair<int,std::string> > ret;
  626. //various morale bonuses (from buildings, artifacts, etc)
  627. for(std::list<HeroBonus>::const_iterator i=bonuses.begin(); i != bonuses.end(); i++)
  628. {
  629. if(i->type == HeroBonus::MORALE || i->type == HeroBonus::MORALE_AND_LUCK)
  630. {
  631. ret.push_back(std::make_pair(i->val, i->description));
  632. }
  633. }
  634. //leadership
  635. if(getSecSkillLevel(6))
  636. ret.push_back(std::make_pair(getSecSkillLevel(6),VLC->generaltexth->arraytxt[104+getSecSkillLevel(6)]));
  637. //town structures
  638. if(town && visitedTown)
  639. {
  640. if(visitedTown->subID == 0 && vstd::contains(visitedTown->builtBuildings,22)) //castle, brotherhood of sword built
  641. ret.push_back(std::pair<int,std::string>(2,VLC->generaltexth->buildings[0][22].first + " +2"));
  642. else if(vstd::contains(visitedTown->builtBuildings,5)) //tavern is built
  643. ret.push_back(std::pair<int,std::string>(2,VLC->generaltexth->buildings[0][5].first + " +1"));
  644. }
  645. //number of alignments and presence of undead
  646. if(stack>=0)
  647. {
  648. bool archangelInArmy = false;
  649. std::set<si8> factions;
  650. for(std::map<si32,std::pair<ui32,si32> >::const_iterator i=army.slots.begin(); i!=army.slots.end(); i++)
  651. {
  652. factions.insert(VLC->creh->creatures[i->second.first].faction);
  653. if(i->second.first == 13)
  654. archangelInArmy = true;
  655. }
  656. if(factions.size() == 1)
  657. ret.push_back(std::pair<int,std::string>(1,VLC->generaltexth->arraytxt[115])); //All troops of one alignment +1
  658. else
  659. {
  660. if(VLC->generaltexth->arraytxt[114].length() <= 100)
  661. {
  662. char buf[150];
  663. std::sprintf(buf,VLC->generaltexth->arraytxt[114].c_str(),factions.size(),2-factions.size());
  664. ret.push_back(std::pair<int,std::string>(2-factions.size(),buf)); //Troops of %d alignments %d
  665. }
  666. else
  667. {
  668. ret.push_back(std::pair<int,std::string>(2-factions.size(),"")); //Troops of %d alignments %d
  669. }
  670. }
  671. if(vstd::contains(factions,4))
  672. ret.push_back(std::pair<int,std::string>(-1,VLC->generaltexth->arraytxt[116])); //Undead in group -1
  673. if(archangelInArmy)
  674. {
  675. char buf[100];
  676. sprintf(buf,VLC->generaltexth->arraytxt[117].c_str(),VLC->creh->creatures[13].namePl.c_str());
  677. ret.push_back(std::pair<int,std::string>(1,buf)); //%s in group +1
  678. }
  679. }
  680. return ret;
  681. }
  682. int CGHeroInstance::getCurrentLuck( int stack/*=-1*/, bool town/*=false*/ ) const
  683. {
  684. int ret = 0;
  685. std::vector<std::pair<int,std::string> > mods = getCurrentLuckModifiers(stack,town);
  686. for(int i=0; i < mods.size(); i++)
  687. ret += mods[i].first;
  688. if(ret > 3)
  689. return 3;
  690. if(ret < -3)
  691. return -3;
  692. return ret;
  693. }
  694. std::vector<std::pair<int,std::string> > CGHeroInstance::getCurrentLuckModifiers( int stack/*=-1*/, bool town/*=false*/ ) const
  695. {
  696. std::vector<std::pair<int,std::string> > ret;
  697. //various morale bonuses (from buildings, artifacts, etc)
  698. for(std::list<HeroBonus>::const_iterator i=bonuses.begin(); i != bonuses.end(); i++)
  699. if(i->type == HeroBonus::LUCK || i->type == HeroBonus::MORALE_AND_LUCK)
  700. ret.push_back(std::make_pair(i->val, i->description));
  701. //luck skill
  702. if(getSecSkillLevel(9))
  703. ret.push_back(std::make_pair(getSecSkillLevel(9),VLC->generaltexth->arraytxt[73+getSecSkillLevel(9)]));
  704. return ret;
  705. }
  706. const HeroBonus * CGHeroInstance::getBonus( int from, int id ) const
  707. {
  708. for (std::list<HeroBonus>::const_iterator i=bonuses.begin(); i!=bonuses.end(); i++)
  709. if(i->source == from && i->id == id)
  710. return &*i;
  711. return NULL;
  712. }
  713. void CGHeroInstance::setPropertyDer( ui8 what, ui32 val )
  714. {
  715. if(what == 3)
  716. army.slots[0].second = val;
  717. }
  718. double CGHeroInstance::getHeroStrength() const
  719. {
  720. return sqrt((1.0 + 0.05*getPrimSkillLevel(0)) * (1.0 + 0.05*getPrimSkillLevel(1)));
  721. }
  722. int CGHeroInstance::getTotalStrength() const
  723. {
  724. double ret = getHeroStrength() * getArmyStrength();
  725. return (int) ret;
  726. }
  727. ui8 CGHeroInstance::getSpellSchoolLevel(const CSpell * spell) const
  728. {
  729. //TODO: skill level may be different on special terrain
  730. ui8 skill = 0; //skill level
  731. if(spell->fire)
  732. skill = std::max(skill,getSecSkillLevel(14));
  733. if(spell->air)
  734. skill = std::max(skill,getSecSkillLevel(15));
  735. if(spell->water)
  736. skill = std::max(skill,getSecSkillLevel(16));
  737. if(spell->earth)
  738. skill = std::max(skill,getSecSkillLevel(17));
  739. return skill;
  740. }
  741. bool CGHeroInstance::canCastThisSpell(const CSpell * spell) const
  742. {
  743. if(!getArt(17)) //if hero has no spellbook
  744. return false;
  745. if(vstd::contains(spells, spell->id) //hero does not have this spell in spellbook
  746. || (spell->air && hasBonusOfType(HeroBonus::AIR_SPELLS)) // this is air spell and hero can cast all air spells
  747. || (spell->fire && hasBonusOfType(HeroBonus::FIRE_SPELLS)) // this is fire spell and hero can cast all fire spells
  748. || (spell->water && hasBonusOfType(HeroBonus::WATER_SPELLS)) // this is water spell and hero can cast all water spells
  749. || (spell->earth && hasBonusOfType(HeroBonus::EARTH_SPELLS)) // this is earth spell and hero can cast all earth spells
  750. )
  751. return true;
  752. for(std::list<HeroBonus>::const_iterator it = bonuses.begin(); it != bonuses.end(); ++it)
  753. {
  754. if(it->type == HeroBonus::SPELL && it->subtype == spell->id)
  755. {
  756. return true;
  757. }
  758. if(it->type == HeroBonus::SPELLS_OF_LEVEL && it->subtype == spell->level)
  759. {
  760. return true;
  761. }
  762. }
  763. return false;
  764. }
  765. int3 CGHeroInstance::getSightCenter() const
  766. {
  767. return getPosition(false);
  768. }
  769. int CGHeroInstance::getSightRadious() const
  770. {
  771. return 5 + getSecSkillLevel(3) + valOfBonuses(HeroBonus::SIGHT_RADIOUS); //default + scouting
  772. }
  773. si32 CGHeroInstance::manaRegain() const
  774. {
  775. return 1 + getSecSkillLevel(8) + valOfBonuses(HeroBonus::MANA_REGENERATION); //1 + Mysticism level
  776. }
  777. int CGHeroInstance::valOfBonuses( HeroBonus::BonusType type, int subtype /*= -1*/ ) const
  778. {
  779. int ret = 0;
  780. if(subtype == -1)
  781. {
  782. for(std::list<HeroBonus>::const_iterator i=bonuses.begin(); i != bonuses.end(); i++)
  783. if(i->type == type)
  784. ret += i->val;
  785. }
  786. else
  787. {
  788. for(std::list<HeroBonus>::const_iterator i=bonuses.begin(); i != bonuses.end(); i++)
  789. if(i->type == type && i->subtype == subtype)
  790. ret += i->val;
  791. }
  792. return ret;
  793. }
  794. bool CGHeroInstance::hasBonusOfType(HeroBonus::BonusType type, int subtype /*= -1*/) const
  795. {
  796. if(subtype == -1) //any subtype
  797. {
  798. for(std::list<HeroBonus>::const_iterator i=bonuses.begin(); i != bonuses.end(); i++)
  799. if(i->type == type)
  800. return true;
  801. }
  802. else //given subtype
  803. {
  804. for(std::list<HeroBonus>::const_iterator i=bonuses.begin(); i != bonuses.end(); i++)
  805. if(i->type == type && i->subtype == subtype)
  806. return true;
  807. }
  808. return false;
  809. }
  810. si32 CGHeroInstance::getArtPos(int aid) const
  811. {
  812. for(std::map<ui16,ui32>::const_iterator i = artifWorn.begin(); i != artifWorn.end(); i++)
  813. if(i->second == aid)
  814. return i->first;
  815. return -1;
  816. }
  817. void CGHeroInstance::recreateArtBonuses()
  818. {
  819. //clear all bonuses from artifacts (if present) and give them again
  820. std::remove_if(bonuses.begin(), bonuses.end(), boost::bind(HeroBonus::IsFrom,_1,HeroBonus::ARTIFACT,0xffffff));
  821. for (std::map<ui16,ui32>::iterator ari = artifWorn.begin(); ari != artifWorn.end(); ari++)
  822. {
  823. CArtifact &art = VLC->arth->artifacts[ari->second];
  824. for(std::list<HeroBonus>::iterator i = art.bonuses.begin(); i != art.bonuses.end(); i++)
  825. bonuses.push_back(*i);
  826. }
  827. }
  828. void CGDwelling::initObj()
  829. {
  830. switch(ID)
  831. {
  832. case 17:
  833. {
  834. int crid = VLC->objh->cregens[subID];
  835. CCreature *crs = &VLC->creh->creatures[crid];
  836. creatures.resize(1);
  837. creatures[0].second.push_back(crid);
  838. hoverName = VLC->generaltexth->creGens[subID];
  839. if(crs->level > 4)
  840. {
  841. army.slots[0].first = crs->idNumber;
  842. army.slots[0].second = crs->getRandomAmount(ran);
  843. }
  844. }
  845. break;
  846. case 20:
  847. creatures.resize(4);
  848. if(subID == 0) // Elemental Conflux
  849. {
  850. creatures[0].second.push_back(32); //Stone Golem
  851. creatures[1].second.push_back(33); //Iron Golem
  852. creatures[2].second.push_back(116); //Gold Golem
  853. creatures[3].second.push_back(117); //Diamond Golem
  854. }
  855. else if(subID == 1) //Golem Factory
  856. {
  857. creatures[0].second.push_back(112); //Air Elemental
  858. creatures[1].second.push_back(113); //Earth Elemental
  859. creatures[2].second.push_back(114); //Fire Elemental
  860. creatures[3].second.push_back(115); //Water Elemental
  861. }
  862. else
  863. {
  864. assert(0);
  865. }
  866. hoverName = VLC->generaltexth->creGens4[subID];
  867. break;
  868. default:
  869. assert(0);
  870. break;
  871. }
  872. }
  873. void CGDwelling::onHeroVisit( const CGHeroInstance * h ) const
  874. {
  875. if(h->tempOwner != tempOwner && army) //object is guarded
  876. {
  877. BlockingDialog bd;
  878. bd.player = h->tempOwner;
  879. bd.flags = BlockingDialog::ALLOW_CANCEL;
  880. bd.text.addTxt(MetaString::GENERAL_TXT, 421); //Much to your dismay, the %s is guarded by %s %s. Do you wish to fight the guards?
  881. bd.text.addReplacement(MetaString::CREGENS, subID);
  882. bd.text.addReplacement(MetaString::ARRAY_TXT, 176 + CCreature::getQuantityID(army.slots.begin()->second.second)*3);
  883. bd.text.addReplacement(MetaString::CRE_PL_NAMES, creatures[0].second[0]);
  884. cb->showBlockingDialog(&bd, boost::bind(&CGDwelling::wantsFight, this, h, _1));
  885. return;
  886. }
  887. if(h->tempOwner != tempOwner)
  888. cb->setOwner(id, h->tempOwner);
  889. BlockingDialog bd;
  890. bd.player = h->tempOwner;
  891. bd.flags = BlockingDialog::ALLOW_CANCEL;
  892. bd.text.addTxt(MetaString::ADVOB_TXT, 35); //{%s} Would you like to recruit %s?
  893. bd.text.addReplacement(MetaString::CREGENS, subID);
  894. bd.text.addReplacement(MetaString::CRE_PL_NAMES, creatures[0].second[0]);
  895. cb->showBlockingDialog(&bd, boost::bind(&CGDwelling::heroAcceptsCreatures, this, h, _1));
  896. }
  897. void CGDwelling::newTurn() const
  898. {
  899. if(cb->getDate(1) != 1) //not first day of week
  900. return;
  901. bool change = false;
  902. SetAvailableCreatures sac;
  903. sac.creatures = creatures;
  904. sac.tid = id;
  905. for (size_t i = 0; i < creatures.size(); i++)
  906. {
  907. if(creatures[i].second.size())
  908. {
  909. sac.creatures[i].first += VLC->creh->creatures[creatures[i].second[0]].growth;
  910. change = true;
  911. }
  912. }
  913. if(change)
  914. cb->sendAndApply(&sac);
  915. }
  916. void CGDwelling::heroAcceptsCreatures( const CGHeroInstance *h, ui32 answer ) const
  917. {
  918. if(!answer)
  919. return;
  920. int crid = creatures[0].second[0];
  921. CCreature *crs = &VLC->creh->creatures[crid];
  922. if(crs->level == 1) //first level - creatures are for free
  923. {
  924. if(creatures[0].first) //there are available creatures
  925. {
  926. int slot = h->army.getSlotFor(crid);
  927. if(slot < 0) //no available slot
  928. {
  929. InfoWindow iw;
  930. iw.player = h->tempOwner;
  931. iw.text.addTxt(MetaString::GENERAL_TXT, 425);//The %s would join your hero, but there aren't enough provisions to support them.
  932. iw.text.addReplacement(MetaString::CRE_PL_NAMES, crid);
  933. cb->showInfoDialog(&iw);
  934. }
  935. else //give creatures
  936. {
  937. SetAvailableCreatures sac;
  938. sac.tid = id;
  939. sac.creatures = creatures;
  940. sac.creatures[0].first = 0;
  941. SetGarrisons sg;
  942. sg.garrs[h->id] = h->army;
  943. sg.garrs[h->id].slots[slot].first = crid;
  944. sg.garrs[h->id].slots[slot].second += creatures[0].first;
  945. InfoWindow iw;
  946. iw.player = h->tempOwner;
  947. iw.text.addTxt(MetaString::GENERAL_TXT, 423); //%d %s join your army.
  948. iw.text.addReplacement(creatures[0].first);
  949. iw.text.addReplacement(MetaString::CRE_PL_NAMES, crid);
  950. cb->showInfoDialog(&iw);
  951. cb->sendAndApply(&sac);
  952. cb->sendAndApply(&sg);
  953. }
  954. }
  955. else //there no creatures
  956. {
  957. InfoWindow iw;
  958. iw.text.addTxt(MetaString::GENERAL_TXT, 422); //There are no %s here to recruit.
  959. iw.text.addReplacement(MetaString::CRE_PL_NAMES, crid);
  960. iw.player = h->tempOwner;
  961. cb->sendAndApply(&iw);
  962. }
  963. }
  964. else if(ID == 17)
  965. {
  966. OpenWindow ow;
  967. ow.id1 = id;
  968. ow.id2 = id;
  969. ow.window = OpenWindow::RECRUITMENT_FIRST;
  970. cb->sendAndApply(&ow);
  971. }
  972. }
  973. void CGDwelling::wantsFight( const CGHeroInstance *h, ui32 answer ) const
  974. {
  975. if(answer)
  976. cb->startBattleI(h->id,army,pos,boost::bind(&CGDwelling::fightOver, this, h, _1));
  977. }
  978. void CGDwelling::fightOver(const CGHeroInstance *h, BattleResult *result) const
  979. {
  980. if (result->winner == 0)
  981. {
  982. onHeroVisit(h);
  983. }
  984. }
  985. int CGTownInstance::getSightRadious() const //returns sight distance
  986. {
  987. return 5;
  988. }
  989. int CGTownInstance::fortLevel() const //0 - none, 1 - fort, 2 - citadel, 3 - castle
  990. {
  991. if((builtBuildings.find(9))!=builtBuildings.end())
  992. return 3;
  993. if((builtBuildings.find(8))!=builtBuildings.end())
  994. return 2;
  995. if((builtBuildings.find(7))!=builtBuildings.end())
  996. return 1;
  997. return 0;
  998. }
  999. int CGTownInstance::hallLevel() const // -1 - none, 0 - village, 1 - town, 2 - city, 3 - capitol
  1000. {
  1001. if ((builtBuildings.find(13))!=builtBuildings.end())
  1002. return 3;
  1003. if ((builtBuildings.find(12))!=builtBuildings.end())
  1004. return 2;
  1005. if ((builtBuildings.find(11))!=builtBuildings.end())
  1006. return 1;
  1007. if ((builtBuildings.find(10))!=builtBuildings.end())
  1008. return 0;
  1009. return -1;
  1010. }
  1011. int CGTownInstance::mageGuildLevel() const
  1012. {
  1013. if ((builtBuildings.find(4))!=builtBuildings.end())
  1014. return 5;
  1015. if ((builtBuildings.find(3))!=builtBuildings.end())
  1016. return 4;
  1017. if ((builtBuildings.find(2))!=builtBuildings.end())
  1018. return 3;
  1019. if ((builtBuildings.find(1))!=builtBuildings.end())
  1020. return 2;
  1021. if ((builtBuildings.find(0))!=builtBuildings.end())
  1022. return 1;
  1023. return 0;
  1024. }
  1025. bool CGTownInstance::creatureDwelling(const int & level, bool upgraded) const
  1026. {
  1027. return builtBuildings.find(30+level+upgraded*7)!=builtBuildings.end();
  1028. }
  1029. int CGTownInstance::getHordeLevel(const int & HID) const//HID - 0 or 1; returns creature level or -1 if that horde structure is not present
  1030. {
  1031. return town->hordeLvl[HID];
  1032. }
  1033. int CGTownInstance::creatureGrowth(const int & level) const
  1034. {
  1035. int ret = VLC->creh->creatures[town->basicCreatures[level]].growth;
  1036. switch(fortLevel())
  1037. {
  1038. case 3:
  1039. ret*=2;break;
  1040. case 2:
  1041. ret*=(1.5); break;
  1042. }
  1043. if(builtBuildings.find(26)!=builtBuildings.end()) //grail
  1044. ret+=VLC->creh->creatures[town->basicCreatures[level]].growth;
  1045. if(getHordeLevel(0)==level)
  1046. if((builtBuildings.find(18)!=builtBuildings.end()) || (builtBuildings.find(19)!=builtBuildings.end()))
  1047. ret+=VLC->creh->creatures[town->basicCreatures[level]].hordeGrowth;
  1048. if(getHordeLevel(1)==level)
  1049. if((builtBuildings.find(24)!=builtBuildings.end()) || (builtBuildings.find(25)!=builtBuildings.end()))
  1050. ret+=VLC->creh->creatures[town->basicCreatures[level]].hordeGrowth;
  1051. return ret;
  1052. }
  1053. int CGTownInstance::dailyIncome() const
  1054. {
  1055. int ret = 0;
  1056. if ((builtBuildings.find(26))!=builtBuildings.end())
  1057. ret+=5000;
  1058. if ((builtBuildings.find(13))!=builtBuildings.end())
  1059. ret+=4000;
  1060. else if ((builtBuildings.find(12))!=builtBuildings.end())
  1061. ret+=2000;
  1062. else if ((builtBuildings.find(11))!=builtBuildings.end())
  1063. ret+=1000;
  1064. else if ((builtBuildings.find(10))!=builtBuildings.end())
  1065. ret+=500;
  1066. return ret;
  1067. }
  1068. bool CGTownInstance::hasFort() const
  1069. {
  1070. return (builtBuildings.find(7))!=builtBuildings.end();
  1071. }
  1072. bool CGTownInstance::hasCapitol() const
  1073. {
  1074. return (builtBuildings.find(13))!=builtBuildings.end();
  1075. }
  1076. CGTownInstance::CGTownInstance()
  1077. {
  1078. builded=-1;
  1079. destroyed=-1;
  1080. garrisonHero=NULL;
  1081. town=NULL;
  1082. visitingHero = NULL;
  1083. }
  1084. CGTownInstance::~CGTownInstance()
  1085. {}
  1086. int CGTownInstance::spellsAtLevel(int level, bool checkGuild) const
  1087. {
  1088. if(checkGuild && mageGuildLevel() < level)
  1089. return 0;
  1090. int ret = 6 - level; //how many spells are available at this level
  1091. if(subID == 2 && vstd::contains(builtBuildings,22)) //magic library in Tower
  1092. ret++;
  1093. return ret;
  1094. }
  1095. bool CGTownInstance::needsLastStack() const
  1096. {
  1097. if(garrisonHero)
  1098. return true;
  1099. else return false;
  1100. }
  1101. void CGTownInstance::onHeroVisit(const CGHeroInstance * h) const
  1102. {
  1103. if(getOwner() != h->getOwner())
  1104. {
  1105. return;
  1106. }
  1107. cb->heroVisitCastle(id,h->id);
  1108. }
  1109. void CGTownInstance::onHeroLeave(const CGHeroInstance * h) const
  1110. {
  1111. cb->stopHeroVisitCastle(id,h->id);
  1112. }
  1113. void CGTownInstance::initObj()
  1114. {
  1115. hoverName = name + ", " + town->Name();
  1116. creatures.resize(CREATURES_PER_TOWN);
  1117. for (int i = 0; i < CREATURES_PER_TOWN; i++)
  1118. {
  1119. if(creatureDwelling(i,false))
  1120. creatures[i].second.push_back(town->basicCreatures[i]);
  1121. if(creatureDwelling(i,true))
  1122. creatures[i].second.push_back(town->upgradedCreatures[i]);
  1123. }
  1124. }
  1125. int3 CGTownInstance::getSightCenter() const
  1126. {
  1127. return pos - int3(2,0,0);
  1128. }
  1129. void CGVisitableOPH::onHeroVisit( const CGHeroInstance * h ) const
  1130. {
  1131. if(visitors.find(h->id)==visitors.end())
  1132. {
  1133. onNAHeroVisit(h->id, false);
  1134. switch(ID)
  1135. {
  1136. case 102: //tree
  1137. case 4: //arena
  1138. case 41://library
  1139. case 47: //School of Magic
  1140. case 107://School of War
  1141. break;
  1142. default:
  1143. cb->setObjProperty(id,4,h->id); //add to the visitors
  1144. break;
  1145. }
  1146. }
  1147. else
  1148. {
  1149. onNAHeroVisit(h->id, true);
  1150. }
  1151. }
  1152. void CGVisitableOPH::initObj()
  1153. {
  1154. if(ID==102)
  1155. ttype = ran()%3;
  1156. else
  1157. ttype = -1;
  1158. }
  1159. void CGVisitableOPH::treeSelected( int heroID, int resType, int resVal, int expVal, ui32 result ) const
  1160. {
  1161. if(result) //player agreed to give res for exp
  1162. {
  1163. cb->giveResource(cb->getOwner(heroID),resType,-resVal); //take resource
  1164. cb->changePrimSkill(heroID,4,expVal); //give exp
  1165. cb->setObjProperty(id,4,heroID); //add to the visitors
  1166. }
  1167. }
  1168. void CGVisitableOPH::onNAHeroVisit(int heroID, bool alreadyVisited) const
  1169. {
  1170. int id=0, subid=0, ot=0, val=1, sound = 0;
  1171. switch(ID)
  1172. {
  1173. case 4: //arena
  1174. sound = soundBase::NOMAD;
  1175. ot = 0;
  1176. break;
  1177. case 51: //mercenary camp
  1178. sound = soundBase::NOMAD;
  1179. subid=0;
  1180. ot=80;
  1181. break;
  1182. case 23: //marletto tower
  1183. sound = soundBase::NOMAD;
  1184. subid=1;
  1185. ot=39;
  1186. break;
  1187. case 61:
  1188. sound = soundBase::gazebo;
  1189. subid=2;
  1190. ot=100;
  1191. break;
  1192. case 32:
  1193. sound = soundBase::GETPROTECTION;
  1194. subid=3;
  1195. ot=59;
  1196. break;
  1197. case 100:
  1198. sound = soundBase::gazebo;
  1199. id=5;
  1200. ot=143;
  1201. val=1000;
  1202. break;
  1203. case 102:
  1204. sound = soundBase::gazebo;
  1205. id = 5;
  1206. subid = 1;
  1207. ot = 146;
  1208. val = 1;
  1209. break;
  1210. case 41:
  1211. sound = soundBase::gazebo;
  1212. ot = 66;
  1213. break;
  1214. case 47: //School of Magic
  1215. sound = soundBase::faerie;
  1216. ot = 71;
  1217. break;
  1218. case 107://School of War
  1219. sound = soundBase::MILITARY;
  1220. ot = 158;
  1221. break;
  1222. }
  1223. if (!alreadyVisited)
  1224. {
  1225. switch (ID)
  1226. {
  1227. case 4: //arena
  1228. {
  1229. BlockingDialog sd(false,true);
  1230. sd.soundID = sound;
  1231. sd.text << std::pair<ui8,ui32>(11,ot);
  1232. sd.components.push_back(Component(0,0,2,0));
  1233. sd.components.push_back(Component(0,1,2,0));
  1234. sd.player = cb->getOwner(heroID);
  1235. cb->showBlockingDialog(&sd,boost::bind(&CGVisitableOPH::arenaSelected,this,heroID,_1));
  1236. return;
  1237. }
  1238. case 51:
  1239. case 23:
  1240. case 61:
  1241. case 32:
  1242. {
  1243. cb->changePrimSkill(heroID,subid,val);
  1244. InfoWindow iw;
  1245. iw.soundID = sound;
  1246. iw.components.push_back(Component(0,subid,val,0));
  1247. iw.text << std::pair<ui8,ui32>(11,ot);
  1248. iw.player = cb->getOwner(heroID);
  1249. cb->showInfoDialog(&iw);
  1250. break;
  1251. }
  1252. case 100: //give exp
  1253. {
  1254. InfoWindow iw;
  1255. iw.soundID = sound;
  1256. iw.components.push_back(Component(id,subid,val,0));
  1257. iw.player = cb->getOwner(heroID);
  1258. iw.text << std::pair<ui8,ui32>(11,ot);
  1259. iw.soundID = soundBase::gazebo;
  1260. cb->showInfoDialog(&iw);
  1261. cb->changePrimSkill(heroID,4,val);
  1262. break;
  1263. }
  1264. case 102://tree
  1265. {
  1266. const CGHeroInstance *h = cb->getHero(heroID);
  1267. val = VLC->heroh->reqExp(h->level+val) - VLC->heroh->reqExp(h->level);
  1268. if(!ttype)
  1269. {
  1270. cb->setObjProperty(this->id,4,heroID); //add to the visitors
  1271. InfoWindow iw;
  1272. iw.soundID = sound;
  1273. iw.components.push_back(Component(id,subid,1,0));
  1274. iw.player = cb->getOwner(heroID);
  1275. iw.text << std::pair<ui8,ui32>(11,148);
  1276. cb->showInfoDialog(&iw);
  1277. cb->changePrimSkill(heroID,4,val);
  1278. break;
  1279. }
  1280. else
  1281. {
  1282. int res, resval;
  1283. if(ttype==1)
  1284. {
  1285. res = 6;
  1286. resval = 2000;
  1287. ot = 149;
  1288. }
  1289. else
  1290. {
  1291. res = 5;
  1292. resval = 10;
  1293. ot = 151;
  1294. }
  1295. if(cb->getResource(h->tempOwner,res) < resval) //not enough resources
  1296. {
  1297. ot++;
  1298. InfoWindow iw;
  1299. iw.soundID = sound;
  1300. iw.player = h->tempOwner;
  1301. iw.text << std::pair<ui8,ui32>(11,ot);
  1302. cb->showInfoDialog(&iw);
  1303. return;
  1304. }
  1305. BlockingDialog sd(true,false);
  1306. sd.soundID = sound;
  1307. sd.player = cb->getOwner(heroID);
  1308. sd.text << std::pair<ui8,ui32>(11,ot);
  1309. sd.components.push_back(Component(id,subid,val,0));
  1310. cb->showBlockingDialog(&sd,boost::bind(&CGVisitableOPH::treeSelected,this,heroID,res,resval,val,_1));
  1311. }
  1312. break;
  1313. }
  1314. case 41://library of enlightenment
  1315. {
  1316. const CGHeroInstance *h = cb->getHero(heroID);
  1317. if(h->level < 10 - 2*h->getSecSkillLevel(4)) //not enough level
  1318. {
  1319. InfoWindow iw;
  1320. iw.soundID = sound;
  1321. iw.player = cb->getOwner(heroID);
  1322. iw.text << std::pair<ui8,ui32>(11,68);
  1323. cb->showInfoDialog(&iw);
  1324. }
  1325. else
  1326. {
  1327. cb->setObjProperty(this->id,4,heroID); //add to the visitors
  1328. cb->changePrimSkill(heroID,0,2);
  1329. cb->changePrimSkill(heroID,1,2);
  1330. cb->changePrimSkill(heroID,2,2);
  1331. cb->changePrimSkill(heroID,3,2);
  1332. InfoWindow iw;
  1333. iw.soundID = sound;
  1334. iw.player = cb->getOwner(heroID);
  1335. iw.text << std::pair<ui8,ui32>(11,66);
  1336. cb->showInfoDialog(&iw);
  1337. }
  1338. break;
  1339. }
  1340. case 47: //School of Magic
  1341. case 107://School of War
  1342. {
  1343. int skill = (ID==47 ? 2 : 0);
  1344. if(cb->getResource(cb->getOwner(heroID),6) < 1000) //not enough resources
  1345. {
  1346. InfoWindow iw;
  1347. iw.soundID = sound;
  1348. iw.player = cb->getOwner(heroID);
  1349. iw.text << std::pair<ui8,ui32>(MetaString::ADVOB_TXT,ot+2);
  1350. cb->showInfoDialog(&iw);
  1351. }
  1352. else
  1353. {
  1354. BlockingDialog sd(true,true);
  1355. sd.soundID = sound;
  1356. sd.player = cb->getOwner(heroID);
  1357. sd.text << std::pair<ui8,ui32>(11,ot);
  1358. sd.components.push_back(Component(Component::PRIM_SKILL, skill, +1, 0));
  1359. sd.components.push_back(Component(Component::PRIM_SKILL, skill+1, +1, 0));
  1360. cb->showBlockingDialog(&sd,boost::bind(&CGVisitableOPH::schoolSelected,this,heroID,_1));
  1361. }
  1362. }
  1363. break;
  1364. }
  1365. }
  1366. else
  1367. {
  1368. ot++;
  1369. InfoWindow iw;
  1370. iw.soundID = sound;
  1371. iw.player = cb->getOwner(heroID);
  1372. iw.text << std::pair<ui8,ui32>(11,ot);
  1373. cb->showInfoDialog(&iw);
  1374. }
  1375. }
  1376. const std::string & CGVisitableOPH::getHoverText() const
  1377. {
  1378. int pom = -1;
  1379. switch(ID)
  1380. {
  1381. case 4:
  1382. pom = -1;
  1383. break;
  1384. case 51:
  1385. pom = 8;
  1386. break;
  1387. case 23:
  1388. pom = 7;
  1389. break;
  1390. case 61:
  1391. pom = 11;
  1392. break;
  1393. case 32:
  1394. pom = 4;
  1395. break;
  1396. case 100:
  1397. pom = 5;
  1398. break;
  1399. case 102:
  1400. pom = 18;
  1401. break;
  1402. case 41:
  1403. break;
  1404. case 47: //School of Magic
  1405. pom = 9;
  1406. break;
  1407. case 107://School of War
  1408. pom = 10;
  1409. break;
  1410. default:
  1411. throw std::string("Wrong CGVisitableOPH object ID!\n");
  1412. }
  1413. hoverName = VLC->generaltexth->names[ID];
  1414. if(pom >= 0)
  1415. hoverName += (" " + VLC->generaltexth->xtrainfo[pom]);
  1416. const CGHeroInstance *h = cb->getSelectedHero(cb->getCurrentPlayer());
  1417. if(h)
  1418. {
  1419. hoverName += ' ';
  1420. hoverName += (vstd::contains(visitors,h->id))
  1421. ? (VLC->generaltexth->allTexts[352]) //visited
  1422. : ( VLC->generaltexth->allTexts[353]); //not visited
  1423. }
  1424. return hoverName;
  1425. }
  1426. void CGVisitableOPH::arenaSelected( int heroID, int primSkill ) const
  1427. {
  1428. cb->setObjProperty(id,4,heroID); //add to the visitors
  1429. cb->changePrimSkill(heroID,primSkill-1,2);
  1430. }
  1431. void CGVisitableOPH::setPropertyDer( ui8 what, ui32 val )
  1432. {
  1433. if(what == 4)
  1434. visitors.insert(val);
  1435. }
  1436. void CGVisitableOPH::schoolSelected(int heroID, ui32 which) const
  1437. {
  1438. if(!which) //player refused to pay
  1439. return;
  1440. int base = (ID == 47 ? 2 : 0);
  1441. cb->setObjProperty(id,4,heroID); //add to the visitors
  1442. cb->giveResource(cb->getOwner(heroID),6,-1000); //take 1000 gold
  1443. cb->changePrimSkill(heroID, base + which-1, +1); //give appropriate skill
  1444. }
  1445. bool CArmedInstance::needsLastStack() const
  1446. {
  1447. return false;
  1448. }
  1449. int CArmedInstance::getArmyStrength() const
  1450. {
  1451. int ret = 0;
  1452. for(std::map<si32,std::pair<ui32,si32> >::const_iterator i=army.slots.begin(); i!=army.slots.end(); i++)
  1453. ret += VLC->creh->creatures[i->second.first].AIValue * i->second.second;
  1454. return ret;
  1455. }
  1456. void CGCreature::onHeroVisit( const CGHeroInstance * h ) const
  1457. {
  1458. int action = takenAction(h);
  1459. switch( action ) //decide what we do...
  1460. {
  1461. case -2: //fight
  1462. fight(h);
  1463. break;
  1464. case -1: //flee
  1465. {
  1466. flee(h);
  1467. break;
  1468. }
  1469. case 0: //join for free
  1470. {
  1471. BlockingDialog ynd(true,false);
  1472. ynd.player = h->tempOwner;
  1473. ynd.text << std::pair<ui8,ui32>(MetaString::ADVOB_TXT, 86);
  1474. ynd.text.addReplacement(MetaString::CRE_PL_NAMES, subID);
  1475. cb->showBlockingDialog(&ynd,boost::bind(&CGCreature::joinDecision,this,h,0,_1));
  1476. break;
  1477. }
  1478. default: //join for gold
  1479. {
  1480. assert(action > 0);
  1481. //ask if player agrees to pay gold
  1482. BlockingDialog ynd(true,false);
  1483. ynd.player = h->tempOwner;
  1484. std::string tmp = VLC->generaltexth->advobtxt[90];
  1485. boost::algorithm::replace_first(tmp,"%d",boost::lexical_cast<std::string>(army.slots.find(0)->second.second));
  1486. boost::algorithm::replace_first(tmp,"%d",boost::lexical_cast<std::string>(action));
  1487. boost::algorithm::replace_first(tmp,"%s",VLC->creh->creatures[subID].namePl);
  1488. ynd.text << tmp;
  1489. cb->showBlockingDialog(&ynd,boost::bind(&CGCreature::joinDecision,this,h,action,_1));
  1490. break;
  1491. }
  1492. }
  1493. }
  1494. void CGCreature::endBattle( BattleResult *result ) const
  1495. {
  1496. if(result->winner==0)
  1497. {
  1498. cb->removeObject(id);
  1499. }
  1500. else
  1501. {
  1502. int killedAmount=0;
  1503. for(std::set<std::pair<ui32,si32> >::iterator i=result->casualties[1].begin(); i!=result->casualties[1].end(); i++)
  1504. if(i->first == subID)
  1505. killedAmount += i->second;
  1506. cb->setAmount(id, army.slots.find(0)->second.second - killedAmount);
  1507. MetaString ms;
  1508. int pom = CCreature::getQuantityID(army.slots.find(0)->second.second);
  1509. pom = 174 + 3*pom + 1;
  1510. ms << std::pair<ui8,ui32>(6,pom) << " " << std::pair<ui8,ui32>(7,subID);
  1511. cb->setHoverName(id,&ms);
  1512. }
  1513. }
  1514. void CGCreature::initObj()
  1515. {
  1516. blockVisit = true;
  1517. switch(character)
  1518. {
  1519. case 0:
  1520. character = 0;
  1521. break;
  1522. case 1:
  1523. character = 1 + ran()%7;
  1524. break;
  1525. case 2:
  1526. character = 1 + ran()%10;
  1527. break;
  1528. case 3:
  1529. character = 4 + ran()%7;
  1530. break;
  1531. case 4:
  1532. character = 10;
  1533. break;
  1534. }
  1535. army.slots[0].first = subID;
  1536. si32 &amount = army.slots[0].second;
  1537. CCreature &c = VLC->creh->creatures[subID];
  1538. if(!amount)
  1539. if(c.ammMax == c.ammMin)
  1540. amount = c.ammMax;
  1541. else
  1542. amount = c.ammMin + (ran() % (c.ammMax - c.ammMin));
  1543. MetaString ms;
  1544. int pom = CCreature::getQuantityID(army.slots.find(0)->second.second);
  1545. pom = 174 + 3*pom + 1;
  1546. ms << std::pair<ui8,ui32>(6,pom) << " " << std::pair<ui8,ui32>(7,subID);
  1547. ms.toString(hoverName);
  1548. }
  1549. int CGCreature::takenAction(const CGHeroInstance *h, bool allowJoin) const
  1550. {
  1551. double hlp = h->getTotalStrength() / getArmyStrength();
  1552. if(!character) //compliant creatures will always join
  1553. return 0;
  1554. else if(allowJoin)//test for joining
  1555. {
  1556. int factor;
  1557. if(hlp >= 7)
  1558. factor = 11;
  1559. else if(hlp >= 1)
  1560. factor = 2*(hlp-1);
  1561. else if(hlp >= 0.5)
  1562. factor = -1;
  1563. else if(hlp >= 0.333)
  1564. factor = -2;
  1565. else
  1566. factor = -3;
  1567. int sympathy = 0;
  1568. std::set<ui32> myKindCres; //what creatures are the same kind as we
  1569. myKindCres.insert(subID); //we
  1570. myKindCres.insert(VLC->creh->creatures[subID].upgrades.begin(),VLC->creh->creatures[subID].upgrades.end()); //our upgrades
  1571. for(std::vector<CCreature>::iterator i=VLC->creh->creatures.begin(); i!=VLC->creh->creatures.end(); i++)
  1572. if(vstd::contains(i->upgrades,id)) //it's our base creatures
  1573. myKindCres.insert(i->idNumber);
  1574. int count = 0, //how many creatures of our kind has hero
  1575. totalCount = 0;
  1576. for (std::map<si32,std::pair<ui32,si32> >::const_iterator i = h->army.slots.begin(); i != h->army.slots.end(); i++)
  1577. {
  1578. if(vstd::contains(myKindCres,i->second.first))
  1579. count += i->second.second;
  1580. totalCount += i->second.second;
  1581. }
  1582. if(count*2 > totalCount)
  1583. sympathy++;
  1584. if(count)
  1585. sympathy++;
  1586. int charisma = factor + h->getSecSkillLevel(4) + sympathy;
  1587. if(charisma >= character) //creatures might join...
  1588. {
  1589. if(h->getSecSkillLevel(4) + sympathy + 1 >= character)
  1590. return 0; //join for free
  1591. else if(h->getSecSkillLevel(4) * 2 + sympathy + 1 >= character)
  1592. return VLC->creh->creatures[subID].cost[6] * army.slots.find(0)->second.second; //join for gold
  1593. }
  1594. }
  1595. //we are still here - creatures not joined heroes, test for fleeing
  1596. //TODO: it's provisional formula, should be replaced with original one (or something closer to it)
  1597. //TODO: should be deterministic (will be needed for Vision spell)
  1598. int hlp2 = (int) (hlp - 2)*1000;
  1599. if(!neverFlees
  1600. && hlp2 >= 0
  1601. && rand()%2000 < hlp2
  1602. )
  1603. return -1; //flee
  1604. else
  1605. return -2; //fight
  1606. }
  1607. void CGCreature::fleeDecision(const CGHeroInstance *h, ui32 pursue) const
  1608. {
  1609. if(pursue)
  1610. {
  1611. fight(h);
  1612. }
  1613. else
  1614. {
  1615. cb->removeObject(id);
  1616. }
  1617. }
  1618. void CGCreature::joinDecision(const CGHeroInstance *h, int cost, ui32 accept) const
  1619. {
  1620. if(!accept)
  1621. {
  1622. if(takenAction(h,false) == -1) //they flee
  1623. {
  1624. flee(h);
  1625. }
  1626. else //they fight
  1627. {
  1628. InfoWindow iw;
  1629. iw.player = h->tempOwner;
  1630. iw.text << std::pair<ui8,ui32>(11,87); //Insulted by your refusal of their offer, the monsters attack!
  1631. cb->showInfoDialog(&iw);
  1632. fight(h);
  1633. }
  1634. }
  1635. else //accepted
  1636. {
  1637. if (cb->getResource(h->tempOwner,6) < cost) //player don't have enough gold!
  1638. {
  1639. InfoWindow iw;
  1640. iw.player = h->tempOwner;
  1641. iw.text << std::pair<ui8,ui32>(1,29); //You don't have enough gold
  1642. cb->showInfoDialog(&iw);
  1643. //act as if player refused
  1644. joinDecision(h,cost,true);
  1645. return;
  1646. }
  1647. //take gold
  1648. if(cost)
  1649. cb->giveResource(h->tempOwner,6,-cost);
  1650. int slot = h->army.getSlotFor(subID);
  1651. if(slot >= 0) //there is place
  1652. {
  1653. //add creatures
  1654. SetGarrisons sg;
  1655. sg.garrs[h->id] = h->army;
  1656. if(vstd::contains(h->army.slots,slot)) //add to already present stack
  1657. {
  1658. sg.garrs[h->id].slots[slot].second += army.slots.find(0)->second.second;
  1659. }
  1660. else //add as a new stack
  1661. {
  1662. sg.garrs[h->id].slots[slot] = army.slots.find(0)->second;
  1663. }
  1664. cb->sendAndApply(&sg);
  1665. cb->removeObject(id);
  1666. }
  1667. else
  1668. {
  1669. cb->showGarrisonDialog(id,h->id,boost::bind(&IGameCallback::removeObject,cb,id)); //show garrison window and remove ourselves from map when player ends
  1670. }
  1671. }
  1672. }
  1673. void CGCreature::fight( const CGHeroInstance *h ) const
  1674. {
  1675. cb->startBattleI(h->id,army,pos,boost::bind(&CGCreature::endBattle,this,_1));
  1676. }
  1677. void CGCreature::flee( const CGHeroInstance * h ) const
  1678. {
  1679. BlockingDialog ynd(true,false);
  1680. ynd.player = h->tempOwner;
  1681. ynd.text << std::pair<ui8,ui32>(11,91);
  1682. ynd.text.addReplacement(MetaString::CRE_PL_NAMES, subID);
  1683. cb->showBlockingDialog(&ynd,boost::bind(&CGCreature::fleeDecision,this,h,_1));
  1684. }
  1685. void CGMine::onHeroVisit( const CGHeroInstance * h ) const
  1686. {
  1687. if(subID == 7) //TODO: support for abandoned mine
  1688. return;
  1689. if(h->tempOwner == tempOwner) //we're visiting our mine
  1690. {
  1691. cb->showGarrisonDialog(id,h->id,0);
  1692. return;
  1693. }
  1694. //TODO: check if mine is guarded
  1695. cb->setOwner(id,h->tempOwner); //not ours? flag it!
  1696. MetaString ms;
  1697. ms << std::pair<ui8,ui32>(9,subID) << " (" << std::pair<ui8,ui32>(6,23+h->tempOwner) << ")";
  1698. cb->setHoverName(id,&ms);
  1699. int vv=1; //amount of resource per turn
  1700. if (subID==0 || subID==2)
  1701. vv++;
  1702. else if (subID==6)
  1703. vv = 1000;
  1704. InfoWindow iw;
  1705. iw.soundID = soundBase::FLAGMINE;
  1706. iw.text << std::pair<ui8,ui32>(10,subID);
  1707. iw.player = h->tempOwner;
  1708. iw.components.push_back(Component(2,subID,vv,-1));
  1709. cb->showInfoDialog(&iw);
  1710. }
  1711. void CGMine::newTurn() const
  1712. {
  1713. if(cb->getDate() == 1)
  1714. return;
  1715. if (tempOwner == NEUTRAL_PLAYER)
  1716. return;
  1717. int vv = 1;
  1718. if (subID==0 || subID==2)
  1719. vv++;
  1720. else if (subID==6)
  1721. vv = 1000;
  1722. cb->giveResource(tempOwner,subID,vv);
  1723. }
  1724. void CGMine::initObj()
  1725. {
  1726. MetaString ms;
  1727. ms << std::pair<ui8,ui32>(9,subID);
  1728. if(tempOwner >= PLAYER_LIMIT)
  1729. tempOwner = NEUTRAL_PLAYER;
  1730. else
  1731. ms << " (" << std::pair<ui8,ui32>(6,23+tempOwner) << ")";
  1732. ms.toString(hoverName);
  1733. }
  1734. void CGResource::initObj()
  1735. {
  1736. blockVisit = true;
  1737. hoverName = VLC->generaltexth->restypes[subID];
  1738. if(!amount)
  1739. {
  1740. switch(subID)
  1741. {
  1742. case 6:
  1743. amount = 500 + (rand()%6)*100;
  1744. break;
  1745. case 0: case 2:
  1746. amount = 6 + (rand()%5);
  1747. break;
  1748. default:
  1749. amount = 3 + (rand()%3);
  1750. break;
  1751. }
  1752. }
  1753. }
  1754. void CGResource::onHeroVisit( const CGHeroInstance * h ) const
  1755. {
  1756. if(army.slots.size())
  1757. {
  1758. if(message.size())
  1759. {
  1760. BlockingDialog ynd(true,false);
  1761. ynd.player = h->getOwner();
  1762. ynd.text << message;
  1763. cb->showBlockingDialog(&ynd,boost::bind(&CGResource::fightForRes,this,_1,h));
  1764. }
  1765. else
  1766. {
  1767. fightForRes(1,h);
  1768. }
  1769. }
  1770. else
  1771. {
  1772. if(message.length())
  1773. {
  1774. InfoWindow iw;
  1775. iw.player = h->tempOwner;
  1776. iw.text << message;
  1777. cb->showInfoDialog(&iw);
  1778. }
  1779. collectRes(h->getOwner());
  1780. }
  1781. }
  1782. void CGResource::collectRes( int player ) const
  1783. {
  1784. cb->giveResource(player,subID,amount);
  1785. ShowInInfobox sii;
  1786. sii.player = player;
  1787. sii.c = Component(2,subID,amount,0);
  1788. sii.text << std::pair<ui8,ui32>(11,113);
  1789. sii.text.addReplacement(MetaString::RES_NAMES, subID);
  1790. cb->showCompInfo(&sii);
  1791. cb->removeObject(id);
  1792. }
  1793. void CGResource::fightForRes(ui32 agreed, const CGHeroInstance *h) const
  1794. {
  1795. if(agreed)
  1796. cb->startBattleI(h->id,army,pos,boost::bind(&CGResource::endBattle,this,_1,h));
  1797. }
  1798. void CGResource::endBattle( BattleResult *result, const CGHeroInstance *h ) const
  1799. {
  1800. if(result->winner == 0) //attacker won
  1801. collectRes(h->getOwner());
  1802. }
  1803. void CGVisitableOPW::newTurn() const
  1804. {
  1805. if (cb->getDate(1)==1) //first day of week
  1806. {
  1807. cb->setObjProperty(id,5,false);
  1808. MetaString ms; //set text to "not visited"
  1809. ms << std::pair<ui8,ui32>(3,ID) << " " << std::pair<ui8,ui32>(1,353);
  1810. cb->setHoverName(id,&ms);
  1811. }
  1812. }
  1813. void CGVisitableOPW::onHeroVisit( const CGHeroInstance * h ) const
  1814. {
  1815. int mid, sound = 0;
  1816. switch (ID)
  1817. {
  1818. case 55: //mystical garden
  1819. sound = soundBase::experience;
  1820. mid = 92;
  1821. break;
  1822. case 112://windmill
  1823. sound = soundBase::GENIE;
  1824. mid = 170;
  1825. break;
  1826. case 109://waterwheel
  1827. sound = soundBase::GENIE;
  1828. mid = 164;
  1829. break;
  1830. }
  1831. if (visited)
  1832. {
  1833. if (ID!=112)
  1834. mid++;
  1835. else
  1836. mid--;
  1837. InfoWindow iw;
  1838. iw.soundID = sound;
  1839. iw.player = h->tempOwner;
  1840. iw.text << std::pair<ui8,ui32>(11,mid);
  1841. cb->showInfoDialog(&iw);
  1842. }
  1843. else
  1844. {
  1845. int type, sub, val;
  1846. type = 2;
  1847. switch (ID)
  1848. {
  1849. case 55:
  1850. if (rand()%2)
  1851. {
  1852. sub = 5;
  1853. val = 5;
  1854. }
  1855. else
  1856. {
  1857. sub = 6;
  1858. val = 500;
  1859. }
  1860. break;
  1861. case 112:
  1862. mid = 170;
  1863. sub = (rand() % 5) + 1;
  1864. val = (rand() % 4) + 3;
  1865. break;
  1866. case 109:
  1867. mid = 164;
  1868. sub = 6;
  1869. if(cb->getDate(0)<8)
  1870. val = 500;
  1871. else
  1872. val = 1000;
  1873. }
  1874. cb->giveResource(h->tempOwner,sub,val);
  1875. InfoWindow iw;
  1876. iw.soundID = sound;
  1877. iw.player = h->tempOwner;
  1878. iw.components.push_back(Component(type,sub,val,0));
  1879. iw.text << std::pair<ui8,ui32>(11,mid);
  1880. cb->showInfoDialog(&iw);
  1881. cb->setObjProperty(id,5,true);
  1882. MetaString ms; //set text to "visited"
  1883. ms << std::pair<ui8,ui32>(3,ID) << " " << std::pair<ui8,ui32>(1,352);
  1884. cb->setHoverName(id,&ms);
  1885. }
  1886. }
  1887. void CGVisitableOPW::setPropertyDer( ui8 what, ui32 val )
  1888. {
  1889. if(what == 5)
  1890. visited = val;
  1891. }
  1892. void CGTeleport::onHeroVisit( const CGHeroInstance * h ) const
  1893. {
  1894. int destinationid=-1;
  1895. switch(ID)
  1896. {
  1897. case 43: //one way - find correspong exit monolith
  1898. if(vstd::contains(objs,44) && vstd::contains(objs[44],subID) && objs[44][subID].size())
  1899. destinationid = objs[44][subID][rand()%objs[44][subID].size()];
  1900. else
  1901. tlog2 << "Cannot find corresponding exit monolith for "<< id << std::endl;
  1902. break;
  1903. case 45: //two way monolith - pick any other one
  1904. if(vstd::contains(objs,45) && vstd::contains(objs[45],subID) && objs[45][subID].size()>1)
  1905. while ((destinationid = objs[45][subID][rand()%objs[45][subID].size()])==id);
  1906. else
  1907. tlog2 << "Cannot find corresponding exit monolith for "<< id << std::endl;
  1908. break;
  1909. case 103: //find nearest subterranean gate on the other level
  1910. {
  1911. std::pair<int,double> best(-1,150000); //pair<id,dist>
  1912. for(int i=0; i<objs[103][0].size(); i++)
  1913. {
  1914. if(cb->getObj(objs[103][0][i])->pos.z == pos.z) continue; //gates on our level are not interesting
  1915. double hlp = cb->getObj(objs[103][0][i])->pos.dist2d(pos);
  1916. if(hlp<best.second)
  1917. {
  1918. best.first = objs[103][0][i];
  1919. best.second = hlp;
  1920. }
  1921. }
  1922. if(best.first<0)
  1923. return;
  1924. else
  1925. destinationid = best.first;
  1926. break;
  1927. }
  1928. }
  1929. if(destinationid < 0)
  1930. {
  1931. tlog2 << "Cannot find exit... :( \n";
  1932. return;
  1933. }
  1934. cb->moveHero(h->id,
  1935. (ID!=103)
  1936. ? (CGHeroInstance::convertPosition(cb->getObj(destinationid)->pos,true))
  1937. : (cb->getObj(destinationid)->pos),
  1938. true);
  1939. }
  1940. void CGTeleport::initObj()
  1941. {
  1942. objs[ID][subID].push_back(id);
  1943. }
  1944. void CGArtifact::initObj()
  1945. {
  1946. blockVisit = true;
  1947. if(ID == 5)
  1948. hoverName = VLC->arth->artifacts[subID].Name();
  1949. }
  1950. void CGArtifact::onHeroVisit( const CGHeroInstance * h ) const
  1951. {
  1952. if(!army.slots.size())
  1953. {
  1954. if(ID == 5)
  1955. {
  1956. InfoWindow iw;
  1957. iw.player = h->tempOwner;
  1958. iw.components.push_back(Component(4,subID,0,0));
  1959. if(message.length())
  1960. iw.text << message;
  1961. else
  1962. iw.text << std::pair<ui8,ui32>(12,subID);
  1963. cb->showInfoDialog(&iw);
  1964. }
  1965. pick(h);
  1966. }
  1967. else
  1968. {
  1969. if(message.size())
  1970. {
  1971. BlockingDialog ynd(true,false);
  1972. ynd.player = h->getOwner();
  1973. ynd.text << message;
  1974. cb->showBlockingDialog(&ynd,boost::bind(&CGArtifact::fightForArt,this,_1,h));
  1975. }
  1976. else
  1977. {
  1978. fightForArt(0,h);
  1979. }
  1980. }
  1981. }
  1982. void CGArtifact::pick(const CGHeroInstance * h) const
  1983. {
  1984. if(ID == 5) //Artifact
  1985. {
  1986. cb->giveHeroArtifact(subID,h->id,-2);
  1987. }
  1988. else if(ID == 93) // Spell scroll
  1989. {
  1990. //TODO: support for the spell scroll
  1991. }
  1992. cb->removeObject(id);
  1993. }
  1994. void CGArtifact::fightForArt( ui32 agreed, const CGHeroInstance *h ) const
  1995. {
  1996. if(agreed)
  1997. cb->startBattleI(h->id,army,pos,boost::bind(&CGArtifact::endBattle,this,_1,h));
  1998. }
  1999. void CGArtifact::endBattle( BattleResult *result, const CGHeroInstance *h ) const
  2000. {
  2001. if(result->winner == 0) //attacker won
  2002. pick(h);
  2003. }
  2004. void CGPickable::initObj()
  2005. {
  2006. blockVisit = true;
  2007. switch(ID)
  2008. {
  2009. case 12: //campfire
  2010. val2 = (ran()%3) + 4; //4 - 6
  2011. val1 = val2 * 100;
  2012. type = ran()%6; //given resource
  2013. break;
  2014. case 101: //treasure chest
  2015. {
  2016. int hlp = ran()%100;
  2017. if(hlp >= 95)
  2018. {
  2019. type = 1;
  2020. val1 = VLC->arth->treasures[ran()%VLC->arth->treasures.size()]->id;
  2021. return;
  2022. }
  2023. else if (hlp >= 65)
  2024. {
  2025. val1 = 2000;
  2026. }
  2027. else if(hlp >= 33)
  2028. {
  2029. val1 = 1500;
  2030. }
  2031. else
  2032. {
  2033. val1 = 1000;
  2034. }
  2035. val2 = val1 - 500;
  2036. type = 0;
  2037. break;
  2038. }
  2039. }
  2040. }
  2041. void CGPickable::onHeroVisit( const CGHeroInstance * h ) const
  2042. {
  2043. switch(ID)
  2044. {
  2045. case 12: //campfire
  2046. {
  2047. cb->giveResource(h->tempOwner,type,val2); //non-gold resource
  2048. cb->giveResource(h->tempOwner,6,val1);//gold
  2049. InfoWindow iw;
  2050. iw.soundID = soundBase::experience;
  2051. iw.player = h->tempOwner;
  2052. iw.components.push_back(Component(2,6,val1,0));
  2053. iw.components.push_back(Component(2,type,val2,0));
  2054. iw.text << std::pair<ui8,ui32>(11,23);
  2055. cb->showInfoDialog(&iw);
  2056. break;
  2057. }
  2058. case 101: //treasure chest
  2059. {
  2060. if (subID) //not OH3 treasure chest
  2061. {
  2062. tlog2 << "Not supported WoG treasure chest!\n";
  2063. return;
  2064. }
  2065. if(type) //there is an artifact
  2066. {
  2067. cb->giveHeroArtifact(val1,h->id,-2);
  2068. InfoWindow iw;
  2069. iw.soundID = soundBase::treasure;
  2070. iw.player = h->tempOwner;
  2071. iw.components.push_back(Component(4,val1,1,0));
  2072. iw.text << std::pair<ui8,ui32>(11,145);
  2073. iw.text.addReplacement(MetaString::ART_NAMES, val1);
  2074. cb->showInfoDialog(&iw);
  2075. break;
  2076. }
  2077. else
  2078. {
  2079. BlockingDialog sd(false,true);
  2080. sd.player = h->tempOwner;
  2081. sd.text << std::pair<ui8,ui32>(11,146);
  2082. sd.components.push_back(Component(2,6,val1,0));
  2083. sd.components.push_back(Component(5,0,val2,0));
  2084. sd.soundID = soundBase::chest;
  2085. boost::function<void(ui32)> fun = boost::bind(&CGPickable::chosen,this,_1,h->id);
  2086. cb->showBlockingDialog(&sd,fun);
  2087. return;
  2088. }
  2089. }
  2090. }
  2091. cb->removeObject(id);
  2092. }
  2093. void CGPickable::chosen( int which, int heroID ) const
  2094. {
  2095. switch(which)
  2096. {
  2097. case 1: //player pick gold
  2098. cb->giveResource(cb->getOwner(heroID),6,val1);
  2099. break;
  2100. case 2: //player pick exp
  2101. cb->changePrimSkill(heroID, 4, val2);
  2102. break;
  2103. default:
  2104. throw std::string("Unhandled treasure choice");
  2105. }
  2106. cb->removeObject(id);
  2107. }
  2108. void CGWitchHut::initObj()
  2109. {
  2110. ability = allowedAbilities[ran()%allowedAbilities.size()];
  2111. }
  2112. void CGWitchHut::onHeroVisit( const CGHeroInstance * h ) const
  2113. {
  2114. InfoWindow iw;
  2115. iw.soundID = soundBase::gazebo;
  2116. iw.player = h->getOwner();
  2117. if(!hasVisited(h->tempOwner))
  2118. cb->setObjProperty(id,10,h->tempOwner);
  2119. if(h->getSecSkillLevel(ability)) //you alredy know this skill
  2120. {
  2121. iw.text << std::pair<ui8,ui32>(11,172);
  2122. iw.text.addReplacement(MetaString::SEC_SKILL_NAME, ability);
  2123. }
  2124. else if(h->secSkills.size() >= SKILL_PER_HERO) //already all skills slots used
  2125. {
  2126. iw.text << std::pair<ui8,ui32>(11,173);
  2127. iw.text.addReplacement(MetaString::SEC_SKILL_NAME, ability);
  2128. }
  2129. else //give sec skill
  2130. {
  2131. iw.components.push_back(Component(1, ability, 1, 0));
  2132. iw.text << std::pair<ui8,ui32>(11,171);
  2133. iw.text.addReplacement(MetaString::SEC_SKILL_NAME, ability);
  2134. cb->changeSecSkill(h->id,ability,1,true);
  2135. }
  2136. cb->showInfoDialog(&iw);
  2137. }
  2138. const std::string & CGWitchHut::getHoverText() const
  2139. {
  2140. hoverName = VLC->generaltexth->names[ID];
  2141. if(hasVisited(cb->getCurrentPlayer())) //TODO: use local player, not current
  2142. {
  2143. hoverName += "\n" + VLC->generaltexth->allTexts[356]; // + (learn %s)
  2144. boost::algorithm::replace_first(hoverName,"%s",VLC->generaltexth->skillName[ability]);
  2145. if(cb->getSelectedHero(cb->getCurrentPlayer())->getSecSkillLevel(ability)) //hero knows that ability
  2146. hoverName += "\n\n" + VLC->generaltexth->allTexts[357]; // (Already learned)
  2147. }
  2148. return hoverName;
  2149. }
  2150. void CGBonusingObject::onHeroVisit( const CGHeroInstance * h ) const
  2151. {
  2152. bool visited = h->getBonus(HeroBonus::OBJECT,ID);
  2153. int messageID, bonusType, bonusVal;
  2154. int bonusMove = 0, sound = -1;
  2155. InfoWindow iw;
  2156. iw.player = h->tempOwner;
  2157. GiveBonus gbonus;
  2158. gbonus.hid = h->id;
  2159. gbonus.bonus.duration = HeroBonus::ONE_BATTLE;
  2160. gbonus.bonus.source = HeroBonus::OBJECT;
  2161. gbonus.bonus.id = ID;
  2162. switch(ID)
  2163. {
  2164. case 14: //swan pond
  2165. messageID = 29;
  2166. sound = soundBase::LUCK;
  2167. gbonus.bonus.type = HeroBonus::LUCK;
  2168. gbonus.bonus.val = 2;
  2169. gbonus.bdescr << std::pair<ui8,ui32>(6,67);
  2170. bonusMove = -h->movement;
  2171. break;
  2172. case 28: //Faerie Ring
  2173. messageID = 49;
  2174. sound = soundBase::LUCK;
  2175. gbonus.bonus.type = HeroBonus::LUCK;
  2176. gbonus.bonus.val = 1;
  2177. gbonus.bdescr << std::pair<ui8,ui32>(6,71);
  2178. break;
  2179. case 30: //fountain of fortune
  2180. messageID = 55;
  2181. sound = soundBase::LUCK;
  2182. gbonus.bonus.type = HeroBonus::LUCK;
  2183. gbonus.bonus.val = rand()%5 - 1;
  2184. gbonus.bdescr << std::pair<ui8,ui32>(6,69);
  2185. gbonus.bdescr.addReplacement((gbonus.bonus.val<0 ? "-" : "+") + boost::lexical_cast<std::string>(gbonus.bonus.val));
  2186. break;
  2187. case 38: //idol of fortune
  2188. messageID = 62;
  2189. iw.soundID = soundBase::experience;
  2190. if(cb->getDate(1) == 7) //7th day of week
  2191. gbonus.bonus.type = HeroBonus::MORALE_AND_LUCK;
  2192. else
  2193. gbonus.bonus.type = (cb->getDate(1)%2) ? HeroBonus::LUCK : HeroBonus::MORALE;
  2194. gbonus.bonus.val = 1;
  2195. gbonus.bdescr << std::pair<ui8,ui32>(6,68);
  2196. break;
  2197. case 64: //Rally Flag
  2198. sound = soundBase::MORALE;
  2199. messageID = 111;
  2200. gbonus.bonus.type = HeroBonus::MORALE_AND_LUCK;
  2201. gbonus.bonus.val = 1;
  2202. gbonus.bdescr << std::pair<ui8,ui32>(6,102);
  2203. bonusMove = 400;
  2204. break;
  2205. case 56: //oasis
  2206. messageID = 95;
  2207. gbonus.bonus.type = HeroBonus::MORALE;
  2208. gbonus.bonus.val = 1;
  2209. gbonus.bdescr << std::pair<ui8,ui32>(6,95);
  2210. bonusMove = 800;
  2211. break;
  2212. case 96: //temple
  2213. messageID = 140;
  2214. iw.soundID = soundBase::temple;
  2215. gbonus.bonus.type = HeroBonus::MORALE;
  2216. if(cb->getDate(1)==7) //sunday
  2217. {
  2218. gbonus.bonus.val = 2;
  2219. gbonus.bdescr << std::pair<ui8,ui32>(6,97);
  2220. }
  2221. else
  2222. {
  2223. gbonus.bonus.val = 1;
  2224. gbonus.bdescr << std::pair<ui8,ui32>(6,96);
  2225. }
  2226. break;
  2227. case 110://Watering Hole
  2228. sound = soundBase::MORALE;
  2229. messageID = 166;
  2230. gbonus.bonus.type = HeroBonus::MORALE;
  2231. gbonus.bonus.val = 1;
  2232. gbonus.bdescr << std::pair<ui8,ui32>(6,100);
  2233. bonusMove = 400;
  2234. break;
  2235. case 31: //Fountain of Youth
  2236. sound = soundBase::MORALE;
  2237. messageID = 57;
  2238. gbonus.bonus.type = HeroBonus::MORALE;
  2239. gbonus.bonus.val = 1;
  2240. gbonus.bdescr << std::pair<ui8,ui32>(6,103);
  2241. bonusMove = 400;
  2242. break;
  2243. }
  2244. if(visited)
  2245. {
  2246. if(ID==64 || ID==96 || ID==56)
  2247. messageID--;
  2248. else
  2249. messageID++;
  2250. }
  2251. else
  2252. {
  2253. if(gbonus.bonus.type == HeroBonus::MORALE || gbonus.bonus.type == HeroBonus::MORALE_AND_LUCK)
  2254. iw.components.push_back(Component(8,0,gbonus.bonus.val,0));
  2255. if(gbonus.bonus.type == HeroBonus::LUCK || gbonus.bonus.type == HeroBonus::MORALE_AND_LUCK)
  2256. iw.components.push_back(Component(9,0,gbonus.bonus.val,0));
  2257. cb->giveHeroBonus(&gbonus);
  2258. if(bonusMove) //swan pond - take all move points
  2259. {
  2260. SetMovePoints smp;
  2261. smp.hid = h->id;
  2262. smp.val = h->movement + bonusMove;
  2263. cb->setMovePoints(&smp);
  2264. }
  2265. }
  2266. iw.soundID = sound;
  2267. iw.text << std::pair<ui8,ui32>(11,messageID);
  2268. cb->showInfoDialog(&iw);
  2269. }
  2270. const std::string & CGBonusingObject::getHoverText() const
  2271. {
  2272. const CGHeroInstance *h = cb->getSelectedHero(cb->getCurrentPlayer());
  2273. hoverName = VLC->generaltexth->names[ID];
  2274. if(h)
  2275. {
  2276. if(!h->getBonus(HeroBonus::OBJECT,ID))
  2277. hoverName += " " + VLC->generaltexth->allTexts[353]; //not visited
  2278. else
  2279. hoverName += " " + VLC->generaltexth->allTexts[352]; //visited
  2280. }
  2281. return hoverName;
  2282. }
  2283. void CGMagicWell::onHeroVisit( const CGHeroInstance * h ) const
  2284. {
  2285. int message;
  2286. InfoWindow iw;
  2287. iw.soundID = soundBase::faerie;
  2288. iw.player = h->tempOwner;
  2289. if(h->getBonus(HeroBonus::OBJECT,ID)) //has already visited Well today
  2290. {
  2291. message = 78;
  2292. }
  2293. else if(h->mana < h->manaLimit())
  2294. {
  2295. GiveBonus gbonus;
  2296. gbonus.bonus.type = HeroBonus::NONE;
  2297. gbonus.hid = h->id;
  2298. gbonus.bonus.duration = HeroBonus::ONE_DAY;
  2299. gbonus.bonus.source = HeroBonus::OBJECT;
  2300. gbonus.bonus.id = ID;
  2301. cb->giveHeroBonus(&gbonus);
  2302. cb->setManaPoints(h->id,h->manaLimit());
  2303. message = 77;
  2304. }
  2305. else
  2306. {
  2307. message = 79;
  2308. }
  2309. iw.text << std::pair<ui8,ui32>(11,message); //"A second drink at the well in one day will not help you."
  2310. cb->showInfoDialog(&iw);
  2311. }
  2312. const std::string & CGMagicWell::getHoverText() const
  2313. {
  2314. const CGHeroInstance *h = cb->getSelectedHero(cb->getCurrentPlayer());
  2315. hoverName = VLC->generaltexth->names[ID];
  2316. if(h)
  2317. {
  2318. if(!h->getBonus(HeroBonus::OBJECT,ID))
  2319. hoverName += " " + VLC->generaltexth->allTexts[353]; //not visited
  2320. else
  2321. hoverName += " " + VLC->generaltexth->allTexts[352]; //visited
  2322. }
  2323. return hoverName;
  2324. }
  2325. void CGEvent::onHeroVisit( const CGHeroInstance * h ) const
  2326. {
  2327. if(!(availableFor & (1 << h->tempOwner)))
  2328. return;
  2329. if(cb->getPlayerSettings(h->tempOwner)->human)
  2330. {
  2331. if(humanActivate)
  2332. activated(h);
  2333. }
  2334. else if(computerActivate)
  2335. activated(h);
  2336. }
  2337. void CGEvent::endBattle( const CGHeroInstance *h, BattleResult *result ) const
  2338. {
  2339. if(result->winner)
  2340. return;
  2341. giveContents(h,true);
  2342. }
  2343. void CGEvent::activated( const CGHeroInstance * h ) const
  2344. {
  2345. if(army)
  2346. {
  2347. InfoWindow iw;
  2348. iw.player = h->tempOwner;
  2349. iw.text << message;
  2350. cb->showInfoDialog(&iw);
  2351. cb->startBattleI(h->id,army,pos,boost::bind(&CGEvent::endBattle,this,h,_1));
  2352. }
  2353. else
  2354. {
  2355. giveContents(h,false);
  2356. }
  2357. }
  2358. void CGEvent::giveContents( const CGHeroInstance *h, bool afterBattle ) const
  2359. {
  2360. InfoWindow iw;
  2361. iw.player = h->getOwner();
  2362. bool changesPrimSkill = false;
  2363. for (int i = 0; i < primskills.size(); i++)
  2364. {
  2365. if(primskills[i])
  2366. {
  2367. changesPrimSkill = true;
  2368. break;
  2369. }
  2370. }
  2371. if(gainedExp || changesPrimSkill || abilities.size())
  2372. {
  2373. getText(iw,afterBattle,175,h);
  2374. if(gainedExp)
  2375. iw.components.push_back(Component(Component::EXPERIENCE,0,gainedExp,0));
  2376. for(int i=0; i<primskills.size(); i++)
  2377. if(primskills[i])
  2378. iw.components.push_back(Component(Component::PRIM_SKILL,i,primskills[i],0));
  2379. for(int i=0; i<abilities.size(); i++)
  2380. iw.components.push_back(Component(Component::SEC_SKILL,abilities[i],abilityLevels[i],0));
  2381. cb->showInfoDialog(&iw);
  2382. //give exp
  2383. if(gainedExp)
  2384. cb->changePrimSkill(h->id,5,gainedExp,false);
  2385. //give prim skills
  2386. for(int i=0; i<primskills.size(); i++)
  2387. if(primskills[i])
  2388. cb->changePrimSkill(h->id,i,primskills[i],false);
  2389. //give sec skills
  2390. for(int i=0; i<abilities.size(); i++)
  2391. {
  2392. int curLev = h->getSecSkillLevel(abilities[i]);
  2393. if(curLev && curLev < abilityLevels[i]
  2394. || h->secSkills.size() < SKILL_PER_HERO )
  2395. {
  2396. cb->changeSecSkill(h->id,abilities[i],abilityLevels[i],true);
  2397. }
  2398. }
  2399. }
  2400. if(spells.size())
  2401. {
  2402. std::set<ui32> spellsToGive;
  2403. iw.components.clear();
  2404. for(int i=0; i<spells.size(); i++)
  2405. {
  2406. iw.components.push_back(Component(Component::SPELL,spells[i],0,0));
  2407. spellsToGive.insert(spells[i]);
  2408. }
  2409. if(spellsToGive.size())
  2410. {
  2411. cb->changeSpells(h->id,true,spellsToGive);
  2412. cb->showInfoDialog(&iw);
  2413. }
  2414. }
  2415. if(manaDiff)
  2416. {
  2417. getText(iw,afterBattle,manaDiff,176,177,h);
  2418. iw.components.push_back(Component(Component::PRIM_SKILL,5,manaDiff,0));
  2419. cb->showInfoDialog(&iw);
  2420. cb->setManaPoints(h->id, h->mana + manaDiff);
  2421. }
  2422. if(moraleDiff)
  2423. {
  2424. getText(iw,afterBattle,moraleDiff,178,179,h);
  2425. iw.components.push_back(Component(Component::MORALE,0,moraleDiff,0));
  2426. cb->showInfoDialog(&iw);
  2427. GiveBonus gb;
  2428. gb.bonus = HeroBonus(HeroBonus::ONE_BATTLE,HeroBonus::MORALE,HeroBonus::OBJECT,moraleDiff,id,"");
  2429. gb.hid = h->id;
  2430. cb->giveHeroBonus(&gb);
  2431. }
  2432. if(luckDiff)
  2433. {
  2434. getText(iw,afterBattle,luckDiff,180,181,h);
  2435. iw.components.push_back(Component(Component::LUCK,0,luckDiff,0));
  2436. cb->showInfoDialog(&iw);
  2437. GiveBonus gb;
  2438. gb.bonus = HeroBonus(HeroBonus::ONE_BATTLE,HeroBonus::LUCK,HeroBonus::OBJECT,luckDiff,id,"");
  2439. gb.hid = h->id;
  2440. cb->giveHeroBonus(&gb);
  2441. }
  2442. iw.components.clear();
  2443. iw.text.clear();
  2444. for(int i=0; i<resources.size(); i++)
  2445. {
  2446. if(resources[i] < 0)
  2447. iw.components.push_back(Component(Component::RESOURCE,i,resources[i],0));
  2448. }
  2449. if(iw.components.size())
  2450. {
  2451. getText(iw,afterBattle,182,h);
  2452. cb->showInfoDialog(&iw);
  2453. }
  2454. iw.components.clear();
  2455. iw.text.clear();
  2456. for(int i=0; i<resources.size(); i++)
  2457. {
  2458. if(resources[i] > 0)
  2459. iw.components.push_back(Component(Component::RESOURCE,i,resources[i],0));
  2460. }
  2461. if(iw.components.size())
  2462. {
  2463. getText(iw,afterBattle,183,h);
  2464. cb->showInfoDialog(&iw);
  2465. }
  2466. iw.components.clear();
  2467. for(int i=0; i<artifacts.size(); i++)
  2468. {
  2469. iw.components.push_back(Component(Component::ARTIFACT,artifacts[i],0,0));
  2470. }
  2471. if(iw.components.size())
  2472. {
  2473. cb->showInfoDialog(&iw);
  2474. }
  2475. for(int i=0; i<resources.size(); i++)
  2476. if(resources[i])
  2477. cb->giveResource(h->getOwner(),i,resources[i]);
  2478. for(int i=0; i<artifacts.size(); i++)
  2479. cb->giveHeroArtifact(artifacts[i],h->id,-2);
  2480. //show dialog with given creatures
  2481. iw.components.clear();
  2482. iw.text.clear();
  2483. for(std::map<si32,std::pair<ui32,si32> >::const_iterator i = creatures.slots.begin(); i != creatures.slots.end(); i++)
  2484. {
  2485. iw.components.push_back(Component(Component::CREATURE,i->second.first,i->second.second,0));
  2486. }
  2487. if(iw.components.size())
  2488. {
  2489. if(afterBattle)
  2490. {
  2491. if(iw.components.front().val == 1)
  2492. {
  2493. iw.text.addTxt(MetaString::ADVOB_TXT,185);//A %s joins %s's army.
  2494. iw.text.addReplacement(MetaString::CRE_SING_NAMES, iw.components.front().subtype);
  2495. }
  2496. else
  2497. {
  2498. iw.text.addTxt(MetaString::ADVOB_TXT,186);//%s join %s's army.
  2499. iw.text.addReplacement(MetaString::CRE_PL_NAMES, iw.components.front().subtype);
  2500. }
  2501. iw.text.addReplacement(h->name);
  2502. }
  2503. else
  2504. {
  2505. iw.text << message;
  2506. afterBattle = true;
  2507. }
  2508. cb->showInfoDialog(&iw);
  2509. }
  2510. //check if creatures can be moved to hero army
  2511. CCreatureSet heroArmy = h->army;
  2512. CCreatureSet ourArmy = creatures;
  2513. while(ourArmy)
  2514. {
  2515. int slot = heroArmy.getSlotFor(ourArmy.slots.begin()->second.first);
  2516. if(slot < 0)
  2517. break;
  2518. heroArmy.slots[slot].first = ourArmy.slots.begin()->second.first;
  2519. heroArmy.slots[slot].second += ourArmy.slots.begin()->second.second;
  2520. ourArmy.slots.erase(ourArmy.slots.begin());
  2521. }
  2522. if(!ourArmy) //all creatures can be moved to hero army - do that
  2523. {
  2524. SetGarrisons sg;
  2525. sg.garrs[h->id] = heroArmy;
  2526. cb->sendAndApply(&sg);
  2527. }
  2528. else //show garrison window and let player pick creatures
  2529. {
  2530. SetGarrisons sg;
  2531. sg.garrs[id] = creatures;
  2532. cb->sendAndApply(&sg);
  2533. if(removeAfterVisit)
  2534. cb->showGarrisonDialog(id,h->id,boost::bind(&IGameCallback::removeObject,cb,id));
  2535. else
  2536. cb->showGarrisonDialog(id,h->id,0);
  2537. return;
  2538. }
  2539. if(!afterBattle)
  2540. {
  2541. iw.text << message;
  2542. cb->showInfoDialog(&iw);
  2543. }
  2544. if(removeAfterVisit)
  2545. cb->removeObject(id);
  2546. }
  2547. void CGEvent::getText( InfoWindow &iw, bool &afterBattle, int text, const CGHeroInstance * h ) const
  2548. {
  2549. if(afterBattle)
  2550. {
  2551. iw.text.addTxt(MetaString::ADVOB_TXT,text);//%s has lost treasure.
  2552. iw.text.addReplacement(h->name);
  2553. }
  2554. else
  2555. {
  2556. iw.text << message;
  2557. afterBattle = true;
  2558. }
  2559. }
  2560. void CGEvent::getText( InfoWindow &iw, bool &afterBattle, int val, int positive, int negative, const CGHeroInstance * h ) const
  2561. {
  2562. iw.components.clear();
  2563. iw.text.clear();
  2564. if(afterBattle)
  2565. {
  2566. iw.text.addTxt(MetaString::ADVOB_TXT,val < 0 ? negative : positive); //%s's luck takes a turn for the worse / %s's luck increases
  2567. iw.text.addReplacement(h->name);
  2568. }
  2569. else
  2570. {
  2571. iw.text << message;
  2572. afterBattle = true;
  2573. }
  2574. }
  2575. void CGObservatory::onHeroVisit( const CGHeroInstance * h ) const
  2576. {
  2577. InfoWindow iw;
  2578. iw.soundID = soundBase::LIGHTHOUSE;
  2579. iw.player = h->tempOwner;
  2580. iw.text.addTxt(MetaString::ADVOB_TXT,98 + (ID==60));
  2581. cb->showInfoDialog(&iw);
  2582. FoWChange fw;
  2583. fw.player = h->tempOwner;
  2584. fw.mode = 1;
  2585. cb->getTilesInRange(fw.tiles,pos,20,h->tempOwner,1);
  2586. cb->sendAndApply(&fw);
  2587. }
  2588. void CGShrine::onHeroVisit( const CGHeroInstance * h ) const
  2589. {
  2590. if(spell == 255)
  2591. {
  2592. tlog1 << "Not initialized shrine visited!\n";
  2593. return;
  2594. }
  2595. if(!hasVisited(h->tempOwner))
  2596. cb->setObjProperty(id,10,h->tempOwner);
  2597. InfoWindow iw;
  2598. iw.soundID = soundBase::temple;
  2599. iw.player = h->getOwner();
  2600. iw.text.addTxt(MetaString::ADVOB_TXT,127 + ID - 88);
  2601. iw.text.addTxt(MetaString::SPELL_NAME,spell);
  2602. iw.text << ".";
  2603. if(!h->getArt(17)) //no spellbook
  2604. {
  2605. iw.text.addTxt(MetaString::ADVOB_TXT,131);
  2606. }
  2607. else if(ID == 90 && !h->getSecSkillLevel(7)) //it's third level spell and hero doesn't have wisdom
  2608. {
  2609. iw.text.addTxt(MetaString::ADVOB_TXT,130);
  2610. }
  2611. else if(vstd::contains(h->spells,spell))//hero already knows the spell
  2612. {
  2613. iw.text.addTxt(MetaString::ADVOB_TXT,174);
  2614. }
  2615. else //give spell
  2616. {
  2617. std::set<ui32> spells;
  2618. spells.insert(spell);
  2619. cb->changeSpells(h->id,true,spells);
  2620. iw.components.push_back(Component(Component::SPELL,spell,0,0));
  2621. }
  2622. cb->showInfoDialog(&iw);
  2623. }
  2624. void CGShrine::initObj()
  2625. {
  2626. if(spell == 255) //spell not set
  2627. {
  2628. int level = ID-87;
  2629. std::vector<ui32> possibilities;
  2630. //add all allowed spells of wanted level
  2631. for(int i=0; i<SPELLS_QUANTITY; i++)
  2632. {
  2633. if(VLC->spellh->spells[i].level == level
  2634. && cb->isAllowed(0,VLC->spellh->spells[i].id))
  2635. {
  2636. possibilities.push_back(VLC->spellh->spells[i].id);
  2637. }
  2638. }
  2639. if(!possibilities.size())
  2640. {
  2641. tlog1 << "Error: cannot init shrine, no allowed spells!\n";
  2642. return;
  2643. }
  2644. spell = possibilities[ran() % possibilities.size()];
  2645. }
  2646. }
  2647. const std::string & CGShrine::getHoverText() const
  2648. {
  2649. hoverName = VLC->generaltexth->names[ID];
  2650. if(hasVisited(cb->getCurrentPlayer())) //TODO: use local player, not current
  2651. {
  2652. hoverName += "\n" + VLC->generaltexth->allTexts[355]; // + (learn %s)
  2653. boost::algorithm::replace_first(hoverName,"%s",VLC->spellh->spells[spell].name);
  2654. const CGHeroInstance *h = cb->getSelectedHero(cb->getCurrentPlayer());
  2655. if(h && vstd::contains(h->spells,spell)) //hero knows that ability
  2656. hoverName += "\n\n" + VLC->generaltexth->allTexts[354]; // (Already learned)
  2657. }
  2658. return hoverName;
  2659. }
  2660. void CGSignBottle::initObj()
  2661. {
  2662. //if no text is set than we pick random from the predefined ones
  2663. if(!message.size())
  2664. message = VLC->generaltexth->randsign[ran()%VLC->generaltexth->randsign.size()];
  2665. }
  2666. void CGSignBottle::onHeroVisit( const CGHeroInstance * h ) const
  2667. {
  2668. InfoWindow iw;
  2669. iw.soundID = soundBase::STORE;
  2670. iw.player = h->getOwner();
  2671. iw.text << message;
  2672. cb->showInfoDialog(&iw);
  2673. }
  2674. void CGScholar::giveAnyBonus( const CGHeroInstance * h ) const
  2675. {
  2676. }
  2677. void CGScholar::onHeroVisit( const CGHeroInstance * h ) const
  2678. {
  2679. int type = bonusType;
  2680. int bid = bonusID;
  2681. //check if the bonus if applicable, if not - give primary skill (always possible)
  2682. int ssl = h->getSecSkillLevel(bid); //current sec skill level, used if bonusType == 1
  2683. if((type == 1
  2684. && ((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)
  2685. || (type == 2 && (!h->getArt(17) || vstd::contains(h->spells,bid)))) //hero doesn't have a spellbook or already knows the spell
  2686. {
  2687. type = 0;
  2688. bid = ran() % PRIMARY_SKILLS;
  2689. }
  2690. InfoWindow iw;
  2691. iw.soundID = soundBase::gazebo;
  2692. iw.player = h->getOwner();
  2693. iw.text.addTxt(MetaString::ADVOB_TXT,115);
  2694. switch (type)
  2695. {
  2696. case 0:
  2697. cb->changePrimSkill(h->id,bid,+1);
  2698. iw.components.push_back(Component(Component::PRIM_SKILL,bid,ssl+1,0));
  2699. break;
  2700. case 1:
  2701. cb->changeSecSkill(h->id,bid,+1);
  2702. iw.components.push_back(Component(Component::SEC_SKILL,bid,ssl+1,0));
  2703. break;
  2704. case 2:
  2705. {
  2706. std::set<ui32> hlp;
  2707. hlp.insert(bid);
  2708. cb->changeSpells(h->id,true,hlp);
  2709. iw.components.push_back(Component(Component::SPELL,bid,0,0));
  2710. }
  2711. break;
  2712. default:
  2713. tlog1 << "Error: wrong bonustype (" << (int)type << ") for Scholar!\n";
  2714. return;
  2715. }
  2716. cb->showInfoDialog(&iw);
  2717. cb->removeObject(id);
  2718. }
  2719. void CGScholar::initObj()
  2720. {
  2721. blockVisit = true;
  2722. if(bonusType == 255)
  2723. {
  2724. bonusType = ran()%3;
  2725. switch(bonusType)
  2726. {
  2727. case 0:
  2728. bonusID = ran() % PRIMARY_SKILLS;
  2729. break;
  2730. case 1:
  2731. bonusID = ran() % SKILL_QUANTITY;
  2732. break;
  2733. case 2:
  2734. bonusID = ran() % SPELLS_QUANTITY;
  2735. break;
  2736. }
  2737. }
  2738. }
  2739. void CGOnceVisitable::onHeroVisit( const CGHeroInstance * h ) const
  2740. {
  2741. int sound = 0;
  2742. int txtid = -1;
  2743. switch(ID)
  2744. {
  2745. case 22: //Corpse
  2746. txtid = 37;
  2747. sound = soundBase::MYSTERY;
  2748. break;
  2749. case 39: //Lean To
  2750. sound = soundBase::GENIE;
  2751. txtid = 64;
  2752. break;
  2753. case 105://Wagon
  2754. sound = soundBase::GENIE;
  2755. txtid = 154;
  2756. break;
  2757. case 108:
  2758. break;
  2759. default:
  2760. tlog1 << "Error: Unknown object (" << ID <<") treated as CGOnceVisitable!\n";
  2761. return;
  2762. }
  2763. if(ID == 108)//Warrior's Tomb
  2764. {
  2765. //ask if player wants to search the Tomb
  2766. BlockingDialog bd(true, false);
  2767. sound = soundBase::GRAVEYARD;
  2768. bd.player = h->getOwner();
  2769. bd.text.addTxt(MetaString::ADVOB_TXT,161);
  2770. cb->showBlockingDialog(&bd,boost::bind(&CGOnceVisitable::searchTomb,this,h,_1));
  2771. return;
  2772. }
  2773. InfoWindow iw;
  2774. iw.soundID = sound;
  2775. iw.player = h->getOwner();
  2776. if(players.size()) //we have been already visited...
  2777. {
  2778. txtid++;
  2779. if(ID == 105) //wagon has extra text (for finding art) we need to ommit
  2780. txtid++;
  2781. iw.text.addTxt(MetaString::ADVOB_TXT, txtid);
  2782. }
  2783. else //first visit - give bonus!
  2784. {
  2785. if(ID == 105 && artOrRes == 1)
  2786. {
  2787. txtid++;
  2788. iw.text.addReplacement(MetaString::ART_NAMES, bonusType);
  2789. }
  2790. switch(artOrRes)
  2791. {
  2792. case 0:
  2793. txtid++;
  2794. break;
  2795. case 1: //art
  2796. iw.components.push_back(Component(Component::ARTIFACT,bonusType,0,0));
  2797. cb->giveHeroArtifact(bonusType,h->id,-2);
  2798. break;
  2799. case 2: //res
  2800. iw.components.push_back(Component(Component::RESOURCE,bonusType,bonusVal,0));
  2801. cb->giveResource(h->getOwner(),bonusType,bonusVal);
  2802. break;
  2803. }
  2804. iw.text.addTxt(MetaString::ADVOB_TXT, txtid);
  2805. }
  2806. cb->showInfoDialog(&iw);
  2807. cb->setObjProperty(id,10,h->getOwner());
  2808. }
  2809. const std::string & CGOnceVisitable::getHoverText() const
  2810. {
  2811. hoverName = VLC->generaltexth->names[ID] + " ";
  2812. hoverName += (hasVisited(cb->getCurrentPlayer())
  2813. ? (VLC->generaltexth->allTexts[352]) //visited
  2814. : ( VLC->generaltexth->allTexts[353])); //not visited
  2815. return hoverName;
  2816. }
  2817. void CGOnceVisitable::initObj()
  2818. {
  2819. switch(ID)
  2820. {
  2821. case 22: //Corpse
  2822. {
  2823. blockVisit = true;
  2824. int hlp = ran()%100;
  2825. if(hlp < 20)
  2826. {
  2827. artOrRes = 1;
  2828. std::vector<CArtifact*> arts;
  2829. cb->getAllowed(arts, CArtifact::ART_TREASURE | CArtifact::ART_MINOR | CArtifact::ART_MAJOR);
  2830. bonusType = arts[ran() % arts.size()]->id;
  2831. }
  2832. else
  2833. {
  2834. artOrRes = 0;
  2835. }
  2836. }
  2837. break;
  2838. case 39: //Lean To
  2839. {
  2840. artOrRes = 2;
  2841. bonusType = ran()%6; //any basic resource without gold
  2842. bonusVal = ran()%4 + 1;
  2843. break;
  2844. }
  2845. case 108://Warrior's Tomb
  2846. {
  2847. artOrRes = 1;
  2848. std::vector<CArtifact*> arts;
  2849. int hlp = ran()%100;
  2850. if(hlp < 30)
  2851. cb->getAllowed(arts,CArtifact::ART_TREASURE);
  2852. else if(hlp < 80)
  2853. cb->getAllowed(arts,CArtifact::ART_MINOR);
  2854. else if(hlp < 95)
  2855. cb->getAllowed(arts,CArtifact::ART_MAJOR);
  2856. else
  2857. cb->getAllowed(arts,CArtifact::ART_RELIC);
  2858. bonusType = arts[ran() % arts.size()]->id;
  2859. }
  2860. break;
  2861. case 105://Wagon
  2862. {
  2863. int hlp = ran()%100;
  2864. if(hlp < 10)
  2865. {
  2866. artOrRes = 0; // nothing... :(
  2867. }
  2868. else if(hlp < 50) //minor or treasure art
  2869. {
  2870. artOrRes = 1;
  2871. std::vector<CArtifact*> arts;
  2872. cb->getAllowed(arts, CArtifact::ART_TREASURE | CArtifact::ART_MINOR);
  2873. bonusType = arts[ran() % arts.size()]->id;
  2874. }
  2875. else //2 - 5 of non-gold resource
  2876. {
  2877. artOrRes = 2;
  2878. bonusType = ran()%6;
  2879. bonusVal = ran()%4 + 2;
  2880. }
  2881. break;
  2882. }
  2883. }
  2884. }
  2885. void CGOnceVisitable::searchTomb(const CGHeroInstance *h, ui32 accept) const
  2886. {
  2887. if(accept)
  2888. {
  2889. InfoWindow iw;
  2890. iw.player = h->getOwner();
  2891. iw.components.push_back(Component(Component::MORALE,0,-3,0));
  2892. if(players.size()) //we've been already visited, player found nothing
  2893. {
  2894. iw.text.addTxt(MetaString::ADVOB_TXT,163);
  2895. }
  2896. else //first visit - give artifact
  2897. {
  2898. iw.text.addTxt(MetaString::ADVOB_TXT,162);
  2899. iw.components.push_back(Component(Component::ARTIFACT,bonusType,0,0));
  2900. iw.text.addReplacement(MetaString::ART_NAMES, bonusType);
  2901. cb->giveHeroArtifact(bonusType,h->id,-2);
  2902. }
  2903. if(!h->getBonus(HeroBonus::OBJECT,ID)) //we don't have modifier from this object yet
  2904. {
  2905. //ruin morale
  2906. GiveBonus gb;
  2907. gb.hid = h->id;
  2908. gb.bonus = HeroBonus(HeroBonus::ONE_BATTLE,HeroBonus::MORALE,HeroBonus::OBJECT,-3,id,"");
  2909. gb.bdescr.addTxt(MetaString::ARRAY_TXT,104); //Warrior Tomb Visited -3
  2910. cb->giveHeroBonus(&gb);
  2911. }
  2912. cb->showInfoDialog(&iw);
  2913. //add player to the visitors (for visited tooltop)
  2914. cb->setObjProperty(id,10,h->getOwner());
  2915. }
  2916. }