CObjectHandler.cpp 79 KB

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