CObjectHandler.cpp 83 KB

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