CObjectHandler.cpp 126 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949
  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/assign/std/vector.hpp>
  13. #include <boost/lexical_cast.hpp>
  14. #include <boost/random/linear_congruential.hpp>
  15. #include "CTownHandler.h"
  16. #include "CArtHandler.h"
  17. #include "CSoundBase.h"
  18. #include "../lib/VCMI_Lib.h"
  19. #include "../lib/IGameCallback.h"
  20. #include "../lib/CGameState.h"
  21. #include "../lib/NetPacks.h"
  22. #include "../StartInfo.h"
  23. #include "../lib/map.h"
  24. #include <sstream>
  25. #include <SDL_stdinc.h>
  26. using namespace boost::assign;
  27. /*
  28. * CObjectHandler.cpp, part of VCMI engine
  29. *
  30. * Authors: listed in file AUTHORS in main folder
  31. *
  32. * License: GNU General Public License v2.0 or later
  33. * Full text of license available in license.txt file, in main folder
  34. *
  35. */
  36. std::map<int,std::map<int, std::vector<int> > > CGTeleport::objs;
  37. std::vector<std::pair<int, int> > CGTeleport::gates;
  38. IGameCallback * IObjectInterface::cb = NULL;
  39. DLL_EXPORT void loadToIt(std::string &dest, std::string &src, int &iter, int mode);
  40. extern CLodHandler * bitmaph;
  41. extern boost::rand48 ran;
  42. std::map <ui8, std::set <ui8> > CGKeys::playerKeyMap;
  43. std::map <si32, std::vector<si32> > CGMagi::eyelist;
  44. BankConfig CGPyramid::pyramidConfig;
  45. void IObjectInterface::onHeroVisit(const CGHeroInstance * h) const
  46. {};
  47. void IObjectInterface::onHeroLeave(const CGHeroInstance * h) const
  48. {};
  49. void IObjectInterface::newTurn () const
  50. {};
  51. IObjectInterface::~IObjectInterface()
  52. {}
  53. IObjectInterface::IObjectInterface()
  54. {}
  55. void IObjectInterface::initObj()
  56. {}
  57. void IObjectInterface::setProperty( ui8 what, ui32 val )
  58. {}
  59. void IObjectInterface::postInit()
  60. {}
  61. void IObjectInterface::preInit()
  62. {}
  63. void CPlayersVisited::setPropertyDer( ui8 what, ui32 val )
  64. {
  65. if(what == 10)
  66. players.insert(val);
  67. }
  68. bool CPlayersVisited::hasVisited( ui8 player ) const
  69. {
  70. return vstd::contains(players,player);
  71. }
  72. static void readCreatures(std::istream & is, BankConfig & bc, bool guards) //helper function for void CObjectHandler::loadObjects()
  73. {
  74. const int MAX_BUF = 5000;
  75. char buffer[MAX_BUF + 1];
  76. std::pair<si16, si32> guardInfo = std::make_pair(0, 0);
  77. std::string creName;
  78. is >> guardInfo.second;
  79. //one getline just does not work... probably a kind of left whitespace
  80. is.getline(buffer, MAX_BUF, '\t');
  81. is.getline(buffer, MAX_BUF, '\t');
  82. creName = buffer;
  83. if( std::string(buffer) == "None" ) //no creature to be added
  84. return;
  85. //look for the best creature that is described by given name
  86. if( vstd::contains(VLC->creh->nameToID, creName) )
  87. {
  88. guardInfo.first = VLC->creh->nameToID[creName];
  89. }
  90. else
  91. {
  92. for(int g=0; g<VLC->creh->creatures.size(); ++g)
  93. {
  94. if(VLC->creh->creatures[g].namePl == creName
  95. || VLC->creh->creatures[g].nameRef == creName
  96. || VLC->creh->creatures[g].nameSing == creName)
  97. {
  98. guardInfo.first = VLC->creh->creatures[g].idNumber;
  99. }
  100. }
  101. }
  102. if(guards)
  103. bc.guards.push_back(guardInfo);
  104. else //given creatures
  105. bc.creatures.push_back(guardInfo);
  106. }
  107. void CObjectHandler::loadObjects()
  108. {
  109. {
  110. tlog5 << "\t\tReading cregens \n";
  111. cregens.resize(110); //TODO: hardcoded value - change
  112. for(size_t i=0; i < cregens.size(); ++i)
  113. {
  114. cregens[i]=-1;
  115. }
  116. std::ifstream ifs(DATA_DIR "/config/cregens.txt");
  117. while(!ifs.eof())
  118. {
  119. int dw, cr;
  120. ifs >> dw >> cr;
  121. cregens[dw]=cr;
  122. }
  123. tlog5 << "\t\tDone loading objects!\n";
  124. }
  125. std::ifstream istr;
  126. istr.open(DATA_DIR "/config/bankconfig.txt", std::ios_base::binary);
  127. if(!istr.is_open())
  128. {
  129. tlog1 << "No config/bankconfig.txt file !!!\n";
  130. }
  131. const int MAX_BUF = 5000;
  132. char buffer[MAX_BUF + 1];
  133. //omitting unnecessary lines
  134. istr.getline(buffer, MAX_BUF);
  135. istr.getline(buffer, MAX_BUF);
  136. for(int g=0; g<21; ++g) //TODO: remove hardcoded value
  137. {
  138. //reading name
  139. istr.getline(buffer, MAX_BUF, '\t');
  140. creBanksNames[g] = std::string(buffer);
  141. //remove trailing new line characters
  142. while(creBanksNames[g][0] == 10 || creBanksNames[g][0] == 13)
  143. creBanksNames[g].erase(creBanksNames[g].begin());
  144. for(int i=0; i<4; ++i) //reading levels
  145. {
  146. banksInfo[g].push_back(new BankConfig);
  147. BankConfig &bc = *banksInfo[g].back();
  148. std::string buf;
  149. char dump;
  150. //bc.level is of type char and thus we cannot read directly to it; same for some othre variables
  151. istr >> buf;
  152. bc.level = atoi(buf.c_str());
  153. istr >> buf;
  154. bc.chance = atoi(buf.c_str());
  155. readCreatures(istr, bc, true);
  156. istr >> buf;
  157. bc.upgradeChance = atoi(buf.c_str());
  158. for(int b=0; b<3; ++b)
  159. readCreatures(istr, bc, true);
  160. istr >> bc.combatValue;
  161. bc.resources.resize(RESOURCE_QUANTITY);
  162. //a dirty trick to make it work if there is no 0 for 0 quantity (like in grotto - last entry)
  163. char buft[52];
  164. istr.getline(buft, 50, '\t');
  165. for(int h=0; h<7; ++h)
  166. {
  167. istr.getline(buft, 50, '\t');
  168. if(buft[0] == '\0')
  169. bc.resources[h] = 0;
  170. else
  171. bc.resources[h] = SDL_atoi(buft);
  172. }
  173. readCreatures(istr, bc, false);
  174. bc.artifacts.resize(4);
  175. for(int b=0; b<4; ++b)
  176. {
  177. istr >> bc.artifacts[b];
  178. }
  179. istr >> bc.value;
  180. istr >> bc.rewardDifficulty;
  181. istr >> buf;
  182. bc.easiest = atoi(buf.c_str());
  183. }
  184. }
  185. }
  186. int CGObjectInstance::getOwner() const
  187. {
  188. //if (state)
  189. // return state->owner;
  190. //else
  191. return tempOwner; //won't have owner
  192. }
  193. CGObjectInstance::CGObjectInstance(): animPhaseShift(rand()%0xff)
  194. {
  195. pos = int3(-1,-1,-1);
  196. //std::cout << "Tworze obiekt "<<this<<std::endl;
  197. //state = new CLuaObjectScript();
  198. ID = subID = id = -1;
  199. defInfo = NULL;
  200. info = NULL;
  201. tempOwner = 254;
  202. blockVisit = false;
  203. }
  204. CGObjectInstance::~CGObjectInstance()
  205. {
  206. //std::cout << "Usuwam obiekt "<<this<<std::endl;
  207. //if (state)
  208. // delete state;
  209. //state=NULL;
  210. }
  211. //CGObjectInstance::CGObjectInstance(const CGObjectInstance & right)
  212. //{
  213. // pos = right.pos;
  214. // ID = right.ID;
  215. // subID = right.subID;
  216. // id = right.id;
  217. // defInfo = right.defInfo;
  218. // info = right.info;
  219. // blockVisit = right.blockVisit;
  220. // //state = new CLuaObjectScript(right.state->);
  221. // //*state = *right.state;
  222. // //state = right.state;
  223. // tempOwner = right.tempOwner;
  224. //}
  225. //CGObjectInstance& CGObjectInstance::operator=(const CGObjectInstance & right)
  226. //{
  227. // pos = right.pos;
  228. // ID = right.ID;
  229. // subID = right.subID;
  230. // id = right.id;
  231. // defInfo = right.defInfo;
  232. // info = right.info;
  233. // blockVisit = right.blockVisit;
  234. // //state = new CLuaObjectScript();
  235. // //*state = *right.state;
  236. // tempOwner = right.tempOwner;
  237. // return *this;
  238. //}
  239. const std::string & CGObjectInstance::getHoverText() const
  240. {
  241. return hoverName;
  242. }
  243. void CGObjectInstance::setOwner(int ow)
  244. {
  245. //if (state)
  246. // state->owner = ow;
  247. //else
  248. tempOwner = ow;
  249. }
  250. int CGObjectInstance::getWidth() const//returns width of object graphic in tiles
  251. {
  252. return defInfo->width;
  253. }
  254. int CGObjectInstance::getHeight() const //returns height of object graphic in tiles
  255. {
  256. return defInfo->height;
  257. }
  258. 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)
  259. {
  260. if(defInfo==NULL)
  261. {
  262. tlog2 << "Warning: VisitableAt for obj "<<id<<": NULL defInfo!\n";
  263. return false;
  264. }
  265. if((defInfo->visitMap[y] >> (7-x) ) & 1)
  266. {
  267. return true;
  268. }
  269. return false;
  270. }
  271. bool CGObjectInstance::blockingAt(int x, int y) const
  272. {
  273. if(x<0 || y<0 || x>=getWidth() || y>=getHeight() || defInfo==NULL)
  274. return false;
  275. if((defInfo->blockMap[y+6-getHeight()] >> (7-(8-getWidth()+x) )) & 1)
  276. return false;
  277. return true;
  278. }
  279. bool CGObjectInstance::coveringAt(int x, int y) const
  280. {
  281. if((defInfo->coverageMap[y] >> (7-(x) )) & 1
  282. || (defInfo->shadowCoverage[y] >> (7-(x) )) & 1)
  283. return true;
  284. return false;
  285. }
  286. std::set<int3> CGObjectInstance::getBlockedPos() const
  287. {
  288. std::set<int3> ret;
  289. for(int w=0; w<getWidth(); ++w)
  290. {
  291. for(int h=0; h<getHeight(); ++h)
  292. {
  293. if(blockingAt(w, h))
  294. ret.insert(int3(pos.x - getWidth() + w + 1, pos.y - getHeight() + h + 1, pos.z));
  295. }
  296. }
  297. return ret;
  298. }
  299. bool CGObjectInstance::operator<(const CGObjectInstance & cmp) const //screen printing priority comparing
  300. {
  301. if(defInfo->printPriority==1 && cmp.defInfo->printPriority==0)
  302. return true;
  303. if(cmp.defInfo->printPriority==1 && defInfo->printPriority==0)
  304. return false;
  305. if(this->pos.y<cmp.pos.y)
  306. return true;
  307. if(this->pos.y>cmp.pos.y)
  308. return false;
  309. if(cmp.ID==HEROI_TYPE && ID!=HEROI_TYPE)
  310. return true;
  311. if(cmp.ID!=HEROI_TYPE && ID==HEROI_TYPE)
  312. return false;
  313. if(!defInfo->isVisitable() && cmp.defInfo->isVisitable())
  314. return true;
  315. if(!cmp.defInfo->isVisitable() && defInfo->isVisitable())
  316. return false;
  317. if(this->pos.x<cmp.pos.x)
  318. return true;
  319. return false;
  320. }
  321. void CGObjectInstance::initObj()
  322. {
  323. }
  324. void CGObjectInstance::setProperty( ui8 what, ui32 val )
  325. {
  326. switch(what)
  327. {
  328. case 1:
  329. tempOwner = val;
  330. break;
  331. case 2:
  332. blockVisit = val;
  333. break;
  334. case 6:
  335. ID = val;
  336. break;
  337. }
  338. setPropertyDer(what, val);
  339. }
  340. void CGObjectInstance::setPropertyDer( ui8 what, ui32 val )
  341. {}
  342. int3 CGObjectInstance::getSightCenter() const
  343. {
  344. //return vistiable tile if possible
  345. for(int i=0; i < 8; i++)
  346. for(int j=0; j < 6; j++)
  347. if(visitableAt(i,j))
  348. return(pos + int3(i-7, j-5, 0));
  349. return pos;
  350. }
  351. int CGObjectInstance::getSightRadious() const
  352. {
  353. return 3;
  354. }
  355. void CGObjectInstance::getSightTiles(std::set<int3> &tiles) const //returns reference to the set
  356. {
  357. cb->getTilesInRange(tiles, getSightCenter(), getSightRadious(), tempOwner, 1);
  358. }
  359. int3 CGObjectInstance::getVisitableOffset() const
  360. {
  361. for(int y = 0; y < 6; y++)
  362. for (int x = 0; x < 8; x++)
  363. if((defInfo->visitMap[5-y] >> x) & 1)
  364. return int3(x,y,0);
  365. tlog2 << "Warning: getVisitableOffset called on non-visitable obj!\n";
  366. return int3(-1,-1,-1);
  367. }
  368. void CGObjectInstance::getNameVis( std::string &hname ) const
  369. {
  370. const CGHeroInstance *h = cb->getSelectedHero(cb->getCurrentPlayer());
  371. hname = VLC->generaltexth->names[ID];
  372. if(h)
  373. {
  374. if(!h->getBonus(HeroBonus::OBJECT,ID))
  375. hname += " " + VLC->generaltexth->allTexts[353]; //not visited
  376. else
  377. hname += " " + VLC->generaltexth->allTexts[352]; //visited
  378. }
  379. }
  380. void CGObjectInstance::giveDummyBonus(int heroID, ui8 duration) const
  381. {
  382. GiveBonus gbonus;
  383. gbonus.bonus.type = HeroBonus::NONE;
  384. gbonus.hid = heroID;
  385. gbonus.bonus.duration = duration;
  386. gbonus.bonus.source = HeroBonus::OBJECT;
  387. gbonus.bonus.id = ID;
  388. cb->giveHeroBonus(&gbonus);
  389. }
  390. void CGObjectInstance::onHeroVisit( const CGHeroInstance * h ) const
  391. {
  392. }
  393. ui8 CGObjectInstance::getPassableness() const
  394. {
  395. return 0;
  396. }
  397. static int lowestSpeed(const CGHeroInstance * chi)
  398. {
  399. if(!chi->army.slots.size())
  400. {
  401. tlog1 << "Error! Hero " << chi->id << " ("<<chi->name<<") has no army!\n";
  402. return 20;
  403. }
  404. std::map<si32,std::pair<ui32,si32> >::const_iterator i = chi->army.slots.begin();
  405. ui32 ret = VLC->creh->creatures[(*i++).second.first].speed;
  406. for (;i!=chi->army.slots.end();i++)
  407. {
  408. ret = std::min(ret,VLC->creh->creatures[(*i).second.first].speed);
  409. }
  410. return ret;
  411. }
  412. unsigned int CGHeroInstance::getTileCost(const TerrainTile &dest, const TerrainTile &from) const
  413. {
  414. //TODO: check if all creatures are on its native terrain and change cost appropriately
  415. //base move cost
  416. unsigned ret = 100;
  417. //if there is road both on dest and src tiles - use road movement cost
  418. if(dest.malle && from.malle)
  419. {
  420. int road = std::min(dest.malle,from.malle); //used road ID
  421. switch(road)
  422. {
  423. case TerrainTile::dirtRoad:
  424. ret = 75;
  425. break;
  426. case TerrainTile::grazvelRoad:
  427. ret = 65;
  428. break;
  429. case TerrainTile::cobblestoneRoad:
  430. ret = 50;
  431. break;
  432. default:
  433. tlog1 << "Unknown road type: " << road << "... Something wrong!\n";
  434. break;
  435. }
  436. }
  437. else
  438. {
  439. ret = type->heroClass->terrCosts[from.tertype];
  440. ret = std::max(ret - 25*unsigned(getSecSkillLevel(0)), 100u); //reduce 25% of terrain penalty for each pathfinding level
  441. }
  442. return ret;
  443. }
  444. #if 0
  445. // Unused and buggy method.
  446. // - for loop is wrong. will not find all creatures. must use iterator instead.
  447. // - -> is the slot number. use second->first for creature index
  448. // Is lowestSpeed() the correct equivalent ?
  449. unsigned int CGHeroInstance::getLowestCreatureSpeed() const
  450. {
  451. unsigned int sl = 100;
  452. for(size_t h=0; h < army.slots.size(); ++h)
  453. {
  454. if(VLC->creh->creatures[army.slots.find(h)->first].speed<sl)
  455. sl = VLC->creh->creatures[army.slots.find(h)->first].speed;
  456. }
  457. return sl;
  458. }
  459. #endif
  460. int3 CGHeroInstance::convertPosition(int3 src, bool toh3m) //toh3m=true: manifest->h3m; toh3m=false: h3m->manifest
  461. {
  462. if (toh3m)
  463. {
  464. src.x+=1;
  465. return src;
  466. }
  467. else
  468. {
  469. src.x-=1;
  470. return src;
  471. }
  472. }
  473. int3 CGHeroInstance::getPosition(bool h3m) const //h3m=true - returns position of hero object; h3m=false - returns position of hero 'manifestation'
  474. {
  475. if (h3m)
  476. {
  477. return pos;
  478. }
  479. else
  480. {
  481. return convertPosition(pos,false);
  482. }
  483. }
  484. si32 CGHeroInstance::manaLimit() const
  485. {
  486. double modifier = 1.0;
  487. switch(getSecSkillLevel(24)) //intelligence level
  488. {
  489. case 1: modifier+=0.25; break;
  490. case 2: modifier+=0.5; break;
  491. case 3: modifier+=1.0; break;
  492. }
  493. return si32(10*getPrimSkillLevel(3)*modifier);
  494. }
  495. //void CGHeroInstance::setPosition(int3 Pos, bool h3m) //as above, but sets position
  496. //{
  497. // if (h3m)
  498. // pos = Pos;
  499. // else
  500. // pos = convertPosition(Pos,true);
  501. //}
  502. bool CGHeroInstance::canWalkOnSea() const
  503. {
  504. //TODO: write it - it should check if hero is flying, or something similar
  505. return false;
  506. }
  507. int CGHeroInstance::getPrimSkillLevel(int id) const
  508. {
  509. int ret = primSkills[id];
  510. for(std::list<HeroBonus>::const_iterator i=bonuses.begin(); i != bonuses.end(); i++)
  511. if(i->type == HeroBonus::PRIMARY_SKILL && i->subtype==id)
  512. ret += i->val;
  513. amax(ret, id/2);//minimum value for attack and defense is 0 and for spell power and knowledge - 1
  514. return ret;
  515. }
  516. ui8 CGHeroInstance::getSecSkillLevel(const int & ID) const
  517. {
  518. for(size_t i=0; i < secSkills.size(); ++i)
  519. if(secSkills[i].first==ID)
  520. return secSkills[i].second;
  521. return 0;
  522. }
  523. int CGHeroInstance::maxMovePoints(bool onLand) const
  524. {
  525. static const int moveForSpeed[] = { 1500, 1560, 1630, 1700, 1760, 1830, 1900, 1960, 2000 }; //first element for 3 and lower; last for 11 and more
  526. int index = lowestSpeed(this) - 3;
  527. amin(index, ARRAY_COUNT(moveForSpeed)-1);
  528. amax(index, 0);
  529. int ret = moveForSpeed[index],
  530. bonus = valOfBonuses(HeroBonus::MOVEMENT) + (onLand ? valOfBonuses(HeroBonus::LAND_MOVEMENT) : valOfBonuses(HeroBonus::SEA_MOVEMENT));
  531. double modifier = 0;
  532. if(onLand)
  533. {
  534. //logistics:
  535. switch(getSecSkillLevel(2))
  536. {
  537. case 1:
  538. modifier = 0.1;
  539. break;
  540. case 2:
  541. modifier = 0.2;
  542. break;
  543. case 3:
  544. modifier = 0.3;
  545. break;
  546. }
  547. }
  548. else
  549. {
  550. //navigation:
  551. switch(getSecSkillLevel(5))
  552. {
  553. case 1:
  554. modifier = 0.5;
  555. break;
  556. case 2:
  557. modifier = 1.0;
  558. break;
  559. case 3:
  560. modifier = 1.5;
  561. break;
  562. }
  563. }
  564. return int(ret + ret*modifier) + bonus;
  565. }
  566. ui32 CGHeroInstance::getArtAtPos(ui16 pos) const
  567. {
  568. if(pos<19)
  569. if(vstd::contains(artifWorn,pos))
  570. return artifWorn.find(pos)->second;
  571. else
  572. return -1;
  573. else
  574. if(pos-19 < artifacts.size())
  575. return artifacts[pos-19];
  576. else
  577. return -1;
  578. }
  579. const CArtifact * CGHeroInstance::getArt(int pos) const
  580. {
  581. int id = getArtAtPos(pos);
  582. if(id>=0)
  583. return &VLC->arth->artifacts[id];
  584. else
  585. return NULL;
  586. }
  587. int CGHeroInstance::getSpellSecLevel(int spell) const
  588. {
  589. int bestslvl = 0;
  590. if(VLC->spellh->spells[spell].air)
  591. if(getSecSkillLevel(15) >= bestslvl)
  592. {
  593. bestslvl = getSecSkillLevel(15);
  594. }
  595. if(VLC->spellh->spells[spell].fire)
  596. if(getSecSkillLevel(14) >= bestslvl)
  597. {
  598. bestslvl = getSecSkillLevel(14);
  599. }
  600. if(VLC->spellh->spells[spell].water)
  601. if(getSecSkillLevel(16) >= bestslvl)
  602. {
  603. bestslvl = getSecSkillLevel(16);
  604. }
  605. if(VLC->spellh->spells[spell].earth)
  606. if(getSecSkillLevel(17) >= bestslvl)
  607. {
  608. bestslvl = getSecSkillLevel(17);
  609. }
  610. return bestslvl;
  611. }
  612. CGHeroInstance::CGHeroInstance()
  613. {
  614. ID = HEROI_TYPE;
  615. tacticFormationEnabled = inTownGarrison = false;
  616. mana = movement = portrait = level = -1;
  617. isStanding = true;
  618. moveDir = 4;
  619. exp = 0xffffffff;
  620. visitedTown = NULL;
  621. type = NULL;
  622. boat = NULL;
  623. secSkills.push_back(std::make_pair(-1, -1));
  624. }
  625. void CGHeroInstance::initHero(int SUBID)
  626. {
  627. subID = SUBID;
  628. initHero();
  629. }
  630. void CGHeroInstance::initHero()
  631. {
  632. if(ID == HEROI_TYPE)
  633. initHeroDefInfo();
  634. if(!type)
  635. type = VLC->heroh->heroes[subID];
  636. if(!vstd::contains(spells, 0xffffffff) && type->startingSpell >= 0) //hero starts with a spell
  637. spells.insert(type->startingSpell);
  638. else //remove placeholder
  639. spells -= 0xffffffff;
  640. if(!vstd::contains(artifWorn, 16) && type->startingSpell >= 0) //no catapult means we haven't read pre-existant set
  641. {
  642. artifWorn[17] = 0; //give spellbook
  643. }
  644. artifWorn[16] = 3; //everyone has a catapult
  645. if(portrait < 0 || portrait == 255)
  646. portrait = subID;
  647. if((!primSkills.size()) || (getPrimSkillLevel(0)<0))
  648. {
  649. primSkills.resize(4);
  650. primSkills[0] = type->heroClass->initialAttack;
  651. primSkills[1] = type->heroClass->initialDefence;
  652. primSkills[2] = type->heroClass->initialPower;
  653. primSkills[3] = type->heroClass->initialKnowledge;
  654. }
  655. if(secSkills.size() == 1 && secSkills[0] == std::pair<ui8,ui8>(-1, -1)) //set secondary skills to default
  656. secSkills = type->secSkillsInit;
  657. if (!name.length())
  658. name = type->name;
  659. if (exp == 0xffffffff)
  660. {
  661. exp=40+ (ran()) % 50;
  662. level = 1;
  663. }
  664. else
  665. {
  666. level = VLC->heroh->level(exp);
  667. }
  668. if (!army.slots.size()) //standard army//initial army
  669. {
  670. int pom, pom2=0;
  671. int x = 0; //how many stacks will hero receives <1 - 3>
  672. pom = ran()%100;
  673. if(pom < 9)
  674. x = 1;
  675. else if(pom < 79)
  676. x = 2;
  677. else
  678. x = 3;
  679. for(int x=0;x<3;x++)
  680. {
  681. pom = (VLC->creh->nameToID[type->refTypeStack[x]]);
  682. if(pom>=145 && pom<=149) //war machine
  683. {
  684. pom2++;
  685. switch (pom)
  686. {
  687. case 145: //catapult
  688. artifWorn[16] = 3;
  689. break;
  690. default:
  691. artifWorn[9+CArtHandler::convertMachineID(pom,true)] = CArtHandler::convertMachineID(pom,true);
  692. break;
  693. }
  694. continue;
  695. }
  696. army.slots[x-pom2].first = pom;
  697. pom = type->highStack[x] - type->lowStack[x];
  698. army.slots[x-pom2].second = ran()%(pom+1) + type->lowStack[x];
  699. army.formation = false;
  700. }
  701. }
  702. hoverName = VLC->generaltexth->allTexts[15];
  703. boost::algorithm::replace_first(hoverName,"%s",name);
  704. boost::algorithm::replace_first(hoverName,"%s", type->heroClass->name);
  705. recreateArtBonuses();
  706. if(mana < 0)
  707. mana = manaLimit(); //after all bonuses are taken into account
  708. }
  709. void CGHeroInstance::initHeroDefInfo()
  710. {
  711. if(!defInfo || defInfo->id != HEROI_TYPE)
  712. {
  713. defInfo = new CGDefInfo();
  714. defInfo->id = HEROI_TYPE;
  715. defInfo->subid = subID;
  716. defInfo->printPriority = 0;
  717. defInfo->visitDir = 0xff;
  718. }
  719. for(int i=0;i<6;i++)
  720. {
  721. defInfo->blockMap[i] = 255;
  722. defInfo->visitMap[i] = 0;
  723. defInfo->coverageMap[i] = 0;
  724. defInfo->shadowCoverage[i] = 0;
  725. }
  726. defInfo->handler=NULL;
  727. defInfo->blockMap[5] = 253;
  728. defInfo->visitMap[5] = 2;
  729. defInfo->coverageMap[4] = defInfo->coverageMap[5] = 224;
  730. }
  731. CGHeroInstance::~CGHeroInstance()
  732. {
  733. }
  734. bool CGHeroInstance::needsLastStack() const
  735. {
  736. return true;
  737. }
  738. void CGHeroInstance::onHeroVisit(const CGHeroInstance * h) const
  739. {
  740. if(h == this) return; //exclude potential self-visiting
  741. if (ID == HEROI_TYPE) //hero
  742. {
  743. //TODO: check for allies
  744. if(tempOwner == h->tempOwner) //our hero
  745. {
  746. //exchange
  747. cb->heroExchange(id, h->id);
  748. }
  749. else //battle
  750. {
  751. if(visitedTown) //we're in town
  752. visitedTown->onHeroVisit(h); //town will handle attacking
  753. else
  754. cb->startBattleI(h, this);
  755. }
  756. }
  757. else if(ID == 62) //prison
  758. {
  759. InfoWindow iw;
  760. iw.player = h->tempOwner;
  761. iw.soundID = soundBase::ROGUE;
  762. if(cb->getHeroCount(h->tempOwner,false) < 8) //free hero slot
  763. {
  764. cb->changeObjPos(id,pos+int3(1,0,0),0);
  765. cb->setObjProperty(id,6,HEROI_TYPE); //set ID to 34
  766. cb->giveHero(id,h->tempOwner); //recreates def and adds hero to player
  767. iw.text << std::pair<ui8,ui32>(11,102);
  768. }
  769. else //already 8 wandering heroes
  770. {
  771. iw.text << std::pair<ui8,ui32>(11,103);
  772. }
  773. cb->showInfoDialog(&iw);
  774. }
  775. }
  776. const std::string & CGHeroInstance::getBiography() const
  777. {
  778. if (biography.length())
  779. return biography;
  780. else
  781. return VLC->generaltexth->hTxts[subID].biography;
  782. }
  783. void CGHeroInstance::initObj()
  784. {
  785. blockVisit = true;
  786. }
  787. int CGHeroInstance::getCurrentMorale( int stack, bool town ) const
  788. {
  789. int ret = 0;
  790. std::vector<std::pair<int,std::string> > mods = getCurrentMoraleModifiers(stack,town);
  791. for(int i=0; i < mods.size(); i++)
  792. ret += mods[i].first;
  793. if(ret > 3)
  794. return 3;
  795. if(ret < -3)
  796. return -3;
  797. return ret;
  798. }
  799. std::vector<std::pair<int,std::string> > CGHeroInstance::getCurrentMoraleModifiers( int stack/*=-1*/, bool town/*=false*/ ) const
  800. {
  801. //TODO: check if stack is undead/mechanic/elemental => always neutrl morale
  802. std::vector<std::pair<int,std::string> > ret;
  803. //various morale bonuses (from buildings, artifacts, etc)
  804. for(std::list<HeroBonus>::const_iterator i=bonuses.begin(); i != bonuses.end(); i++)
  805. {
  806. if(i->type == HeroBonus::MORALE || i->type == HeroBonus::MORALE_AND_LUCK)
  807. {
  808. ret.push_back(std::make_pair(i->val, i->description));
  809. }
  810. }
  811. //leadership
  812. if(getSecSkillLevel(6))
  813. ret.push_back(std::make_pair(getSecSkillLevel(6),VLC->generaltexth->arraytxt[104+getSecSkillLevel(6)]));
  814. //town structures
  815. if(town && visitedTown)
  816. {
  817. if(visitedTown->subID == 0 && vstd::contains(visitedTown->builtBuildings,22)) //castle, brotherhood of sword built
  818. ret.push_back(std::pair<int,std::string>(2,VLC->generaltexth->buildings[0][22].first + " +2"));
  819. else if(vstd::contains(visitedTown->builtBuildings,5)) //tavern is built
  820. ret.push_back(std::pair<int,std::string>(2,VLC->generaltexth->buildings[0][5].first + " +1"));
  821. }
  822. //number of alignments and presence of undead
  823. if(stack>=0)
  824. {
  825. bool archangelInArmy = false;
  826. std::set<si8> factions;
  827. for(std::map<si32,std::pair<ui32,si32> >::const_iterator i=army.slots.begin(); i!=army.slots.end(); i++)
  828. {
  829. // Take Angelic Alliance troop-mixing freedom of non-evil, non-Conflux units into account.
  830. const si8 faction = VLC->creh->creatures[i->second.first].faction;
  831. if (hasBonusOfType(HeroBonus::NONEVIL_ALIGNMENT_MIX)
  832. && ((faction >= 0 && faction <= 2) || faction == 6 || faction == 7))
  833. {
  834. factions.insert(0); // Insert a single faction of the affected group, Castle will do.
  835. }
  836. else
  837. {
  838. factions.insert(faction);
  839. }
  840. if(i->second.first == 13)
  841. archangelInArmy = true;
  842. }
  843. if(factions.size() == 1)
  844. ret.push_back(std::pair<int,std::string>(1,VLC->generaltexth->arraytxt[115])); //All troops of one alignment +1
  845. else
  846. {
  847. if(VLC->generaltexth->arraytxt[114].length() <= 100)
  848. {
  849. char buf[150];
  850. std::sprintf(buf,VLC->generaltexth->arraytxt[114].c_str(),factions.size(),2-factions.size());
  851. ret.push_back(std::pair<int,std::string>(2-factions.size(),buf)); //Troops of %d alignments %d
  852. }
  853. else
  854. {
  855. ret.push_back(std::pair<int,std::string>(2-factions.size(),"")); //Troops of %d alignments %d
  856. }
  857. }
  858. if(vstd::contains(factions,4))
  859. ret.push_back(std::pair<int,std::string>(-1,VLC->generaltexth->arraytxt[116])); //Undead in group -1
  860. if(archangelInArmy)
  861. {
  862. char buf[100];
  863. sprintf(buf,VLC->generaltexth->arraytxt[117].c_str(),VLC->creh->creatures[13].namePl.c_str());
  864. ret.push_back(std::pair<int,std::string>(1,buf)); //%s in group +1
  865. }
  866. }
  867. return ret;
  868. }
  869. int CGHeroInstance::getCurrentLuck( int stack/*=-1*/, bool town/*=false*/ ) const
  870. {
  871. int ret = 0;
  872. std::vector<std::pair<int,std::string> > mods = getCurrentLuckModifiers(stack,town);
  873. for(int i=0; i < mods.size(); i++)
  874. ret += mods[i].first;
  875. if(ret > 3)
  876. return 3;
  877. if(ret < -3)
  878. return -3;
  879. return ret;
  880. }
  881. std::vector<std::pair<int,std::string> > CGHeroInstance::getCurrentLuckModifiers( int stack/*=-1*/, bool town/*=false*/ ) const
  882. {
  883. std::vector<std::pair<int,std::string> > ret;
  884. //various morale bonuses (from buildings, artifacts, etc)
  885. for(std::list<HeroBonus>::const_iterator i=bonuses.begin(); i != bonuses.end(); i++)
  886. if(i->type == HeroBonus::LUCK || i->type == HeroBonus::MORALE_AND_LUCK)
  887. ret.push_back(std::make_pair(i->val, i->description));
  888. //luck skill
  889. if(getSecSkillLevel(9))
  890. ret.push_back(std::make_pair(getSecSkillLevel(9),VLC->generaltexth->arraytxt[73+getSecSkillLevel(9)]));
  891. return ret;
  892. }
  893. const HeroBonus * CGHeroInstance::getBonus( int from, int id ) const
  894. {
  895. for (std::list<HeroBonus>::const_iterator i=bonuses.begin(); i!=bonuses.end(); i++)
  896. if(i->source == from && i->id == id)
  897. return &*i;
  898. return NULL;
  899. }
  900. void CGHeroInstance::setPropertyDer( ui8 what, ui32 val )
  901. {
  902. if(what == 3)
  903. army.slots[0].second = val;
  904. }
  905. double CGHeroInstance::getHeroStrength() const
  906. {
  907. return sqrt((1.0 + 0.05*getPrimSkillLevel(0)) * (1.0 + 0.05*getPrimSkillLevel(1)));
  908. }
  909. int CGHeroInstance::getTotalStrength() const
  910. {
  911. double ret = getHeroStrength() * getArmyStrength();
  912. return (int) ret;
  913. }
  914. ui8 CGHeroInstance::getSpellSchoolLevel(const CSpell * spell) const
  915. {
  916. ui8 skill = 0; //skill level
  917. if(spell->fire)
  918. skill = std::max(skill,getSecSkillLevel(14));
  919. if(spell->air)
  920. skill = std::max(skill,getSecSkillLevel(15));
  921. if(spell->water)
  922. skill = std::max(skill,getSecSkillLevel(16));
  923. if(spell->earth)
  924. skill = std::max(skill,getSecSkillLevel(17));
  925. //bonuses (eg. from special terrains)
  926. skill = std::max<ui8>(skill, valOfBonuses(HeroBonus::MAGIC_SCHOOL_SKILL, 0)); //any school bonus
  927. if(spell->fire)
  928. skill = std::max<ui8>(skill, valOfBonuses(HeroBonus::MAGIC_SCHOOL_SKILL, 1));
  929. if(spell->air)
  930. skill = std::max<ui8>(skill, valOfBonuses(HeroBonus::MAGIC_SCHOOL_SKILL, 2));
  931. if(spell->water)
  932. skill = std::max<ui8>(skill, valOfBonuses(HeroBonus::MAGIC_SCHOOL_SKILL, 4));
  933. if(spell->earth)
  934. skill = std::max<ui8>(skill, valOfBonuses(HeroBonus::MAGIC_SCHOOL_SKILL, 8));
  935. return skill;
  936. }
  937. bool CGHeroInstance::canCastThisSpell(const CSpell * spell) const
  938. {
  939. if(!getArt(17)) //if hero has no spellbook
  940. return false;
  941. if(vstd::contains(spells, spell->id) //hero does not have this spell in spellbook
  942. || (spell->air && hasBonusOfType(HeroBonus::AIR_SPELLS)) // this is air spell and hero can cast all air spells
  943. || (spell->fire && hasBonusOfType(HeroBonus::FIRE_SPELLS)) // this is fire spell and hero can cast all fire spells
  944. || (spell->water && hasBonusOfType(HeroBonus::WATER_SPELLS)) // this is water spell and hero can cast all water spells
  945. || (spell->earth && hasBonusOfType(HeroBonus::EARTH_SPELLS)) // this is earth spell and hero can cast all earth spells
  946. )
  947. return true;
  948. for(std::list<HeroBonus>::const_iterator it = bonuses.begin(); it != bonuses.end(); ++it)
  949. {
  950. if(it->type == HeroBonus::SPELL && it->subtype == spell->id)
  951. {
  952. return true;
  953. }
  954. if(it->type == HeroBonus::SPELLS_OF_LEVEL && it->subtype == spell->level)
  955. {
  956. return true;
  957. }
  958. }
  959. return false;
  960. }
  961. /**
  962. * Calculates what creatures and how many to be raised from a battle.
  963. * @param battleResult The results of the battle.
  964. * @return Returns a pair with the first value indicating the ID of the creature
  965. * type and second value the amount. Both values are returned as -1 if necromancy
  966. * could not be applied.
  967. */
  968. std::pair<ui32, si32> CGHeroInstance::calculateNecromancy (const BattleResult &battleResult) const
  969. {
  970. const ui8 necromancyLevel = getSecSkillLevel(12);
  971. // Hero knows necromancy.
  972. if (necromancyLevel > 0) {
  973. double necromancySkill = necromancyLevel*0.1
  974. + valOfBonuses(HeroBonus::SECONDARY_SKILL_PREMY, 12)/100.0;
  975. const std::map<ui32,si32> &casualties = battleResult.casualties[!battleResult.winner];
  976. ui32 raisedUnits = 0;
  977. // Get lost enemy hit points convertible to units.
  978. for (std::map<ui32,si32>::const_iterator it = casualties.begin(); it != casualties.end(); it++)
  979. raisedUnits += VLC->creh->creatures[it->first].hitPoints*it->second;
  980. raisedUnits *= necromancySkill;
  981. // Figure out what to raise and how many.
  982. const ui32 creatureTypes[] = {56, 58, 60, 64}; // IDs for Skeletons, Walking Dead, Wights and Liches respectively.
  983. const bool improvedNecromancy = hasBonusOfType(HeroBonus::IMPROVED_NECROMANCY);
  984. CCreature *raisedUnitType = &VLC->creh->creatures[creatureTypes[improvedNecromancy ? necromancyLevel : 0]];
  985. raisedUnits /= raisedUnitType->hitPoints;
  986. // Make room for new units.
  987. int slot = army.getSlotFor(raisedUnitType->idNumber);
  988. if (slot == -1)
  989. {
  990. // If there's no room for unit, try it's upgraded version 2/3rds the size.
  991. raisedUnitType = &VLC->creh->creatures[*raisedUnitType->upgrades.begin()];
  992. raisedUnits = (raisedUnits*2)/3;
  993. slot = army.getSlotFor(raisedUnitType->idNumber);
  994. }
  995. if (raisedUnits <= 0)
  996. raisedUnits = 1;
  997. return std::pair<ui32, si32>(raisedUnitType->idNumber, raisedUnits);
  998. }
  999. return std::pair<ui32, si32>(-1, -1);
  1000. }
  1001. /**
  1002. * Show the necromancy dialog with information about units raised.
  1003. * @param raisedStack Pair where the first element represents ID of the raised creature
  1004. * and the second element the amount.
  1005. */
  1006. void CGHeroInstance::showNecromancyDialog (std::pair<ui32, si32> raisedStack) const
  1007. {
  1008. const CCreature &unitType = VLC->creh->creatures[raisedStack.first];
  1009. InfoWindow iw;
  1010. iw.soundID = soundBase::GENIE;
  1011. iw.player = tempOwner;
  1012. iw.components.push_back(Component(3, unitType.idNumber, raisedStack.second, 0));
  1013. if (raisedStack.second > 1) { // Practicing the dark arts of necromancy, ... (plural)
  1014. iw.text.addTxt(MetaString::GENERAL_TXT, 145);
  1015. iw.text.addReplacement(raisedStack.second);
  1016. iw.text.addReplacement(MetaString::CRE_PL_NAMES, unitType.idNumber);
  1017. } else { // Practicing the dark arts of necromancy, ... (singular)
  1018. iw.text.addTxt(MetaString::GENERAL_TXT, 146);
  1019. iw.text.addReplacement(MetaString::CRE_SING_NAMES, unitType.idNumber);
  1020. }
  1021. cb->showInfoDialog(&iw);
  1022. }
  1023. int3 CGHeroInstance::getSightCenter() const
  1024. {
  1025. return getPosition(false);
  1026. }
  1027. int CGHeroInstance::getSightRadious() const
  1028. {
  1029. return 5 + getSecSkillLevel(3) + valOfBonuses(HeroBonus::SIGHT_RADIOUS); //default + scouting
  1030. }
  1031. si32 CGHeroInstance::manaRegain() const
  1032. {
  1033. if (hasBonusOfType(HeroBonus::FULL_MANA_REGENERATION))
  1034. return manaLimit();
  1035. return 1 + getSecSkillLevel(8) + valOfBonuses(HeroBonus::MANA_REGENERATION); //1 + Mysticism level
  1036. }
  1037. int CGHeroInstance::valOfBonuses( HeroBonus::BonusType type, int subtype /*= -1*/ ) const
  1038. {
  1039. int ret = 0;
  1040. if(subtype == -1)
  1041. {
  1042. for(std::list<HeroBonus>::const_iterator i=bonuses.begin(); i != bonuses.end(); i++)
  1043. if(i->type == type)
  1044. ret += i->val;
  1045. }
  1046. else
  1047. {
  1048. for(std::list<HeroBonus>::const_iterator i=bonuses.begin(); i != bonuses.end(); i++)
  1049. if(i->type == type && i->subtype == subtype)
  1050. ret += i->val;
  1051. }
  1052. return ret;
  1053. }
  1054. bool CGHeroInstance::hasBonusOfType(HeroBonus::BonusType type, int subtype /*= -1*/) const
  1055. {
  1056. if(!this) //to allow calls on NULL and avoid checking duplication
  1057. return false; //if hero doesn't exist then bonus neither can
  1058. if(subtype == -1) //any subtype
  1059. {
  1060. for(std::list<HeroBonus>::const_iterator i=bonuses.begin(); i != bonuses.end(); i++)
  1061. if(i->type == type)
  1062. return true;
  1063. }
  1064. else //given subtype
  1065. {
  1066. for(std::list<HeroBonus>::const_iterator i=bonuses.begin(); i != bonuses.end(); i++)
  1067. if(i->type == type && i->subtype == subtype)
  1068. return true;
  1069. }
  1070. return false;
  1071. }
  1072. si32 CGHeroInstance::getArtPos(int aid) const
  1073. {
  1074. for(std::map<ui16,ui32>::const_iterator i = artifWorn.begin(); i != artifWorn.end(); i++)
  1075. if(i->second == aid)
  1076. return i->first;
  1077. return -1;
  1078. }
  1079. /**
  1080. * Places an artifact in hero's backpack. If it's a big artifact equips it
  1081. * or discards it if it cannot be equipped.
  1082. */
  1083. void CGHeroInstance::giveArtifact (ui32 aid)
  1084. {
  1085. const CArtifact &artifact = VLC->arth->artifacts[aid];
  1086. if (artifact.isBig()) {
  1087. for (std::vector<ui16>::const_iterator it = artifact.possibleSlots.begin(); it != artifact.possibleSlots.end(); ++it) {
  1088. if (artifWorn.find(*it) == artifWorn.end()) {
  1089. artifWorn[*it] = aid;
  1090. break;
  1091. }
  1092. }
  1093. } else {
  1094. artifacts.push_back(aid);
  1095. }
  1096. }
  1097. void CGHeroInstance::recreateArtBonuses()
  1098. {
  1099. //clear all bonuses from artifacts (if present) and give them again
  1100. std::remove_if(bonuses.begin(), bonuses.end(), boost::bind(HeroBonus::IsFrom,_1,HeroBonus::ARTIFACT,0xffffff));
  1101. for (std::map<ui16,ui32>::iterator ari = artifWorn.begin(); ari != artifWorn.end(); ari++)
  1102. {
  1103. CArtifact &art = VLC->arth->artifacts[ari->second];
  1104. for(std::list<HeroBonus>::iterator i = art.bonuses.begin(); i != art.bonuses.end(); i++)
  1105. bonuses.push_back(*i);
  1106. }
  1107. }
  1108. void CGDwelling::initObj()
  1109. {
  1110. switch(ID)
  1111. {
  1112. case 17:
  1113. {
  1114. int crid = VLC->objh->cregens[subID];
  1115. CCreature *crs = &VLC->creh->creatures[crid];
  1116. creatures.resize(1);
  1117. creatures[0].second.push_back(crid);
  1118. hoverName = VLC->generaltexth->creGens[subID];
  1119. if(crs->level > 4)
  1120. {
  1121. army.slots[0].first = crs->idNumber;
  1122. army.slots[0].second = (8 - crs->level) * 3;
  1123. }
  1124. if (getOwner() != 255)
  1125. cb->gameState()->players[getOwner()].dwellings.push_back (this);
  1126. }
  1127. break;
  1128. case 20:
  1129. creatures.resize(4);
  1130. if(subID == 1) //Golem Factory
  1131. {
  1132. creatures[0].second.push_back(32); //Stone Golem
  1133. creatures[1].second.push_back(33); //Iron Golem
  1134. creatures[2].second.push_back(116); //Gold Golem
  1135. creatures[3].second.push_back(117); //Diamond Golem
  1136. //guards
  1137. army.slots[0].first = 116;
  1138. army.slots[0].second = 9;
  1139. army.slots[1].first = 117;
  1140. army.slots[1].second = 6;
  1141. }
  1142. else if(subID == 0) // Elemental Conflux
  1143. {
  1144. creatures[0].second.push_back(112); //Air Elemental
  1145. creatures[1].second.push_back(114); //Fire Elemental
  1146. creatures[2].second.push_back(113); //Earth Elemental
  1147. creatures[3].second.push_back(115); //Water Elemental
  1148. //guards
  1149. army.slots[0].first = 113;
  1150. army.slots[0].second = 12;
  1151. }
  1152. else
  1153. {
  1154. assert(0);
  1155. }
  1156. hoverName = VLC->generaltexth->creGens4[subID];
  1157. break;
  1158. default:
  1159. assert(0);
  1160. break;
  1161. }
  1162. }
  1163. void CGDwelling::setProperty(ui8 what, ui32 val)
  1164. {
  1165. switch (what)
  1166. {
  1167. case 1: //change owner
  1168. if (ID == 17) //single generators
  1169. {
  1170. if (tempOwner != NEUTRAL_PLAYER)
  1171. {
  1172. std::vector<CGDwelling *>* dwellings = &cb->gameState()->players[tempOwner].dwellings;
  1173. dwellings->erase (std::find(dwellings->begin(), dwellings->end(), this));
  1174. }
  1175. if (val != NEUTRAL_PLAYER) //can new owner be neutral?
  1176. cb->gameState()->players[val].dwellings.push_back (this);
  1177. }
  1178. break;
  1179. }
  1180. CGObjectInstance::setProperty(what,val);
  1181. }
  1182. void CGDwelling::onHeroVisit( const CGHeroInstance * h ) const
  1183. {
  1184. if(h->tempOwner != tempOwner && army.slots.size() > 0) //object is guarded
  1185. {
  1186. BlockingDialog bd;
  1187. bd.player = h->tempOwner;
  1188. bd.flags = BlockingDialog::ALLOW_CANCEL;
  1189. 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?
  1190. bd.text.addReplacement(ID == 17 ? MetaString::CREGENS : MetaString::CREGENS4, subID);
  1191. bd.text.addReplacement(MetaString::ARRAY_TXT, 176 + CCreature::getQuantityID(army.slots.begin()->second.second)*3);
  1192. bd.text.addReplacement(MetaString::CRE_PL_NAMES, army.slots.begin()->second.first);
  1193. cb->showBlockingDialog(&bd, boost::bind(&CGDwelling::wantsFight, this, h, _1));
  1194. return;
  1195. }
  1196. if(h->tempOwner != tempOwner)
  1197. {
  1198. cb->setOwner(id, h->tempOwner);
  1199. }
  1200. BlockingDialog bd;
  1201. bd.player = h->tempOwner;
  1202. bd.flags = BlockingDialog::ALLOW_CANCEL;
  1203. bd.text.addTxt(MetaString::ADVOB_TXT, ID == 17 ? 35 : 36); //{%s} Would you like to recruit %s?
  1204. bd.text.addReplacement(ID == 17 ? MetaString::CREGENS : MetaString::CREGENS4, subID);
  1205. for(size_t i = 0; i < creatures.size(); i++)
  1206. bd.text.addReplacement(MetaString::CRE_PL_NAMES, creatures[i].second[0]);
  1207. cb->showBlockingDialog(&bd, boost::bind(&CGDwelling::heroAcceptsCreatures, this, h, _1));
  1208. }
  1209. void CGDwelling::newTurn() const
  1210. {
  1211. if(cb->getDate(1) != 1) //not first day of week
  1212. return;
  1213. //town growths are handled separately
  1214. if(ID == TOWNI_TYPE)
  1215. return;
  1216. bool change = false;
  1217. SetAvailableCreatures sac;
  1218. sac.creatures = creatures;
  1219. sac.tid = id;
  1220. for (size_t i = 0; i < creatures.size(); i++)
  1221. {
  1222. if(creatures[i].second.size())
  1223. {
  1224. if(false /*accumulate creatures*/)
  1225. sac.creatures[i].first += VLC->creh->creatures[creatures[i].second[0]].growth;
  1226. else
  1227. sac.creatures[i].first = VLC->creh->creatures[creatures[i].second[0]].growth;
  1228. change = true;
  1229. }
  1230. }
  1231. if(change)
  1232. cb->sendAndApply(&sac);
  1233. }
  1234. void CGDwelling::heroAcceptsCreatures( const CGHeroInstance *h, ui32 answer ) const
  1235. {
  1236. if(!answer)
  1237. return;
  1238. int crid = creatures[0].second[0];
  1239. CCreature *crs = &VLC->creh->creatures[crid];
  1240. if(crs->level == 1) //first level - creatures are for free
  1241. {
  1242. if(creatures[0].first) //there are available creatures
  1243. {
  1244. int slot = h->army.getSlotFor(crid);
  1245. if(slot < 0) //no available slot
  1246. {
  1247. InfoWindow iw;
  1248. iw.player = h->tempOwner;
  1249. iw.text.addTxt(MetaString::GENERAL_TXT, 425);//The %s would join your hero, but there aren't enough provisions to support them.
  1250. iw.text.addReplacement(MetaString::CRE_PL_NAMES, crid);
  1251. cb->showInfoDialog(&iw);
  1252. }
  1253. else //give creatures
  1254. {
  1255. SetAvailableCreatures sac;
  1256. sac.tid = id;
  1257. sac.creatures = creatures;
  1258. sac.creatures[0].first = 0;
  1259. SetGarrisons sg;
  1260. sg.garrs[h->id] = h->army;
  1261. sg.garrs[h->id].slots[slot].first = crid;
  1262. sg.garrs[h->id].slots[slot].second += creatures[0].first;
  1263. InfoWindow iw;
  1264. iw.player = h->tempOwner;
  1265. iw.text.addTxt(MetaString::GENERAL_TXT, 423); //%d %s join your army.
  1266. iw.text.addReplacement(creatures[0].first);
  1267. iw.text.addReplacement(MetaString::CRE_PL_NAMES, crid);
  1268. cb->showInfoDialog(&iw);
  1269. cb->sendAndApply(&sac);
  1270. cb->sendAndApply(&sg);
  1271. }
  1272. }
  1273. else //there no creatures
  1274. {
  1275. InfoWindow iw;
  1276. iw.text.addTxt(MetaString::GENERAL_TXT, 422); //There are no %s here to recruit.
  1277. iw.text.addReplacement(MetaString::CRE_PL_NAMES, crid);
  1278. iw.player = h->tempOwner;
  1279. cb->sendAndApply(&iw);
  1280. }
  1281. }
  1282. else
  1283. {
  1284. OpenWindow ow;
  1285. ow.id1 = id;
  1286. ow.id2 = h->id;
  1287. ow.window = ID == 17
  1288. ? OpenWindow::RECRUITMENT_FIRST
  1289. : OpenWindow::RECRUITMENT_ALL;
  1290. cb->sendAndApply(&ow);
  1291. }
  1292. }
  1293. void CGDwelling::wantsFight( const CGHeroInstance *h, ui32 answer ) const
  1294. {
  1295. if(answer)
  1296. cb->startBattleI(h, this, boost::bind(&CGDwelling::fightOver, this, h, _1));
  1297. }
  1298. void CGDwelling::fightOver(const CGHeroInstance *h, BattleResult *result) const
  1299. {
  1300. if (result->winner == 0)
  1301. {
  1302. onHeroVisit(h);
  1303. }
  1304. }
  1305. int CGTownInstance::getSightRadious() const //returns sight distance
  1306. {
  1307. if (subID == 2) //tower
  1308. {
  1309. if ((builtBuildings.find(17)) != builtBuildings.end()) //skyship
  1310. return -1; //entire map
  1311. else if ((builtBuildings.find(21)) != builtBuildings.end()) //lookout tower
  1312. return 20;
  1313. }
  1314. return 5;
  1315. }
  1316. void CGTownInstance::setPropertyDer(ui8 what, ui32 val)
  1317. {
  1318. ///this is freakin' overcomplicated solution
  1319. switch (what)
  1320. {
  1321. case 11: //add visitor of town building
  1322. bonusingBuildings[val]->setProperty (4, visitingHero->id);
  1323. break;
  1324. case 12:
  1325. bonusingBuildings[val]->setProperty (12, 0);
  1326. break;
  1327. case 13: //add garrisoned hero to visitors
  1328. bonusingBuildings[val]->setProperty (4, garrisonHero->id);
  1329. break;
  1330. }
  1331. }
  1332. int CGTownInstance::fortLevel() const //0 - none, 1 - fort, 2 - citadel, 3 - castle
  1333. {
  1334. if((builtBuildings.find(9))!=builtBuildings.end())
  1335. return 3;
  1336. if((builtBuildings.find(8))!=builtBuildings.end())
  1337. return 2;
  1338. if((builtBuildings.find(7))!=builtBuildings.end())
  1339. return 1;
  1340. return 0;
  1341. }
  1342. int CGTownInstance::hallLevel() const // -1 - none, 0 - village, 1 - town, 2 - city, 3 - capitol
  1343. {
  1344. if ((builtBuildings.find(13))!=builtBuildings.end())
  1345. return 3;
  1346. if ((builtBuildings.find(12))!=builtBuildings.end())
  1347. return 2;
  1348. if ((builtBuildings.find(11))!=builtBuildings.end())
  1349. return 1;
  1350. if ((builtBuildings.find(10))!=builtBuildings.end())
  1351. return 0;
  1352. return -1;
  1353. }
  1354. int CGTownInstance::mageGuildLevel() const
  1355. {
  1356. if ((builtBuildings.find(4))!=builtBuildings.end())
  1357. return 5;
  1358. if ((builtBuildings.find(3))!=builtBuildings.end())
  1359. return 4;
  1360. if ((builtBuildings.find(2))!=builtBuildings.end())
  1361. return 3;
  1362. if ((builtBuildings.find(1))!=builtBuildings.end())
  1363. return 2;
  1364. if ((builtBuildings.find(0))!=builtBuildings.end())
  1365. return 1;
  1366. return 0;
  1367. }
  1368. bool CGTownInstance::creatureDwelling(const int & level, bool upgraded) const
  1369. {
  1370. return builtBuildings.find(30+level+upgraded*7)!=builtBuildings.end();
  1371. }
  1372. int CGTownInstance::getHordeLevel(const int & HID) const//HID - 0 or 1; returns creature level or -1 if that horde structure is not present
  1373. {
  1374. return town->hordeLvl[HID];
  1375. }
  1376. int CGTownInstance::creatureGrowth(const int & level) const
  1377. {
  1378. int ret = VLC->creh->creatures[town->basicCreatures[level]].growth;
  1379. switch(fortLevel())
  1380. {
  1381. case 3:
  1382. ret*=2;break;
  1383. case 2:
  1384. ret*=(1.5); break;
  1385. }
  1386. if(getHordeLevel(0)==level)
  1387. if((builtBuildings.find(18)!=builtBuildings.end()) || (builtBuildings.find(19)!=builtBuildings.end()))
  1388. ret+=VLC->creh->creatures[town->basicCreatures[level]].hordeGrowth;
  1389. if(getHordeLevel(1)==level)
  1390. if((builtBuildings.find(24)!=builtBuildings.end()) || (builtBuildings.find(25)!=builtBuildings.end()))
  1391. ret+=VLC->creh->creatures[town->basicCreatures[level]].hordeGrowth;
  1392. //support for legs of legion etc.
  1393. if(garrisonHero)
  1394. ret += garrisonHero->valOfBonuses(HeroBonus::CREATURE_GROWTH, level);
  1395. if(visitingHero)
  1396. ret += visitingHero->valOfBonuses(HeroBonus::CREATURE_GROWTH, level);
  1397. for (std::vector<CGDwelling*>::const_iterator it = cb->gameState()->players[tempOwner].dwellings.begin(); it != cb->gameState()->players[tempOwner].dwellings.end(); ++it)
  1398. { //+1 for each dwelling
  1399. if (VLC->creh->creatures[town->basicCreatures[level]].idNumber == (*it)->creatures[0].second[0])
  1400. ++ret;
  1401. }
  1402. if(builtBuildings.find(26)!=builtBuildings.end()) //grail - +50% to ALL growth
  1403. ret*=1.5;
  1404. return ret;//check CCastleInterface.cpp->CCastleInterface::CCreaInfo::clickRight if this one will be modified
  1405. }
  1406. int CGTownInstance::dailyIncome() const
  1407. {
  1408. int ret = 0;
  1409. if ((builtBuildings.find(26))!=builtBuildings.end())
  1410. ret+=5000;
  1411. if ((builtBuildings.find(13))!=builtBuildings.end())
  1412. ret+=4000;
  1413. else if ((builtBuildings.find(12))!=builtBuildings.end())
  1414. ret+=2000;
  1415. else if ((builtBuildings.find(11))!=builtBuildings.end())
  1416. ret+=1000;
  1417. else if ((builtBuildings.find(10))!=builtBuildings.end())
  1418. ret+=500;
  1419. return ret;
  1420. }
  1421. bool CGTownInstance::hasFort() const
  1422. {
  1423. return (builtBuildings.find(7))!=builtBuildings.end();
  1424. }
  1425. bool CGTownInstance::hasCapitol() const
  1426. {
  1427. return (builtBuildings.find(13))!=builtBuildings.end();
  1428. }
  1429. CGTownInstance::CGTownInstance()
  1430. :IShipyard(this)
  1431. {
  1432. builded=-1;
  1433. destroyed=-1;
  1434. garrisonHero=NULL;
  1435. town=NULL;
  1436. visitingHero = NULL;
  1437. }
  1438. CGTownInstance::~CGTownInstance()
  1439. {
  1440. for (std::vector<CGTownBuilding*>::const_iterator i = bonusingBuildings.begin(); i != bonusingBuildings.end(); i++)
  1441. delete *i;
  1442. }
  1443. int CGTownInstance::spellsAtLevel(int level, bool checkGuild) const
  1444. {
  1445. if(checkGuild && mageGuildLevel() < level)
  1446. return 0;
  1447. int ret = 6 - level; //how many spells are available at this level
  1448. if(subID == 2 && vstd::contains(builtBuildings,22)) //magic library in Tower
  1449. ret++;
  1450. return ret;
  1451. }
  1452. bool CGTownInstance::needsLastStack() const
  1453. {
  1454. if(garrisonHero)
  1455. return true;
  1456. else return false;
  1457. }
  1458. void CGTownInstance::onHeroVisit(const CGHeroInstance * h) const
  1459. {
  1460. if(getOwner() != h->getOwner())
  1461. {
  1462. //TODO ally check
  1463. if(army.slots.size() > 0 || visitingHero)
  1464. {
  1465. const CGHeroInstance *defendingHero = NULL;
  1466. if(visitingHero)
  1467. defendingHero = visitingHero;
  1468. else if(garrisonHero)
  1469. defendingHero = garrisonHero;
  1470. const CArmedInstance *defendingArmy = this;
  1471. if(defendingHero)
  1472. defendingArmy = defendingHero;
  1473. bool outsideTown = (defendingHero == visitingHero && garrisonHero);
  1474. cb->startBattleI(h, defendingArmy, getSightCenter(), h, defendingHero, false, boost::bind(&CGTownInstance::fightOver, this, h, _1), (outsideTown ? NULL : this));
  1475. }
  1476. else
  1477. {
  1478. cb->setOwner(id, h->tempOwner);
  1479. removeCapitols (h->getOwner(), true);
  1480. cb->heroVisitCastle(id, h->id);
  1481. }
  1482. }
  1483. else
  1484. cb->heroVisitCastle(id, h->id);
  1485. }
  1486. void CGTownInstance::onHeroLeave(const CGHeroInstance * h) const
  1487. {
  1488. cb->stopHeroVisitCastle(id,h->id);
  1489. }
  1490. void CGTownInstance::initObj()
  1491. {
  1492. hoverName = name + ", " + town->Name();
  1493. creatures.resize(CREATURES_PER_TOWN);
  1494. for (int i = 0; i < CREATURES_PER_TOWN; i++)
  1495. {
  1496. if(creatureDwelling(i,false))
  1497. creatures[i].second.push_back(town->basicCreatures[i]);
  1498. if(creatureDwelling(i,true))
  1499. creatures[i].second.push_back(town->upgradedCreatures[i]);
  1500. }
  1501. switch (subID)
  1502. { //add new visitable objects
  1503. case 0:
  1504. bonusingBuildings.push_back (new COPWBonus(21, this)); //Stables
  1505. break;
  1506. case 2: case 3: case 5: case 6:
  1507. bonusingBuildings.push_back (new CTownBonus(23, this));
  1508. if (subID == 5)
  1509. bonusingBuildings.push_back (new COPWBonus(21, this)); //Vortex
  1510. break;
  1511. case 7:
  1512. bonusingBuildings.push_back (new CTownBonus(17, this));
  1513. break;
  1514. }
  1515. if (getOwner() != 255)
  1516. removeCapitols (getOwner(), false); // destroy other capitols
  1517. }
  1518. void CGTownInstance::newTurn() const
  1519. {
  1520. if (cb->getDate(0)%7 == 1) //reset on new week
  1521. {
  1522. for (std::vector<CGTownBuilding*>::const_iterator i = bonusingBuildings.begin(); i!=bonusingBuildings.end(); i++)
  1523. cb->setObjProperty (id, 12, 0); //reset visitors for Mana Vortex
  1524. }
  1525. }
  1526. int3 CGTownInstance::getSightCenter() const
  1527. {
  1528. return pos - int3(2,0,0);
  1529. }
  1530. void CGTownInstance::getOutOffsets( std::vector<int3> &offsets ) const
  1531. {
  1532. offsets += int3(-1,2,0), int3(-3,2,0);
  1533. }
  1534. void CGTownInstance::fightOver( const CGHeroInstance *h, BattleResult *result ) const
  1535. {
  1536. if(result->winner == 0)
  1537. {
  1538. removeCapitols (h->getOwner(), true);
  1539. cb->setOwner (id, h->tempOwner); //give control after checkout is done
  1540. }
  1541. }
  1542. void CGTownInstance::removeCapitols (ui8 owner, bool me) const
  1543. {
  1544. if (hasCapitol()) // search for older capitol
  1545. {
  1546. PlayerState* state = cb->gameState()->getPlayer (owner); //get all towns owned by player
  1547. for (std::vector<CGTownInstance*>::const_iterator i = state->towns.begin(); i < state->towns.end(); ++i)
  1548. {
  1549. if (*i != this && (*i)->hasCapitol())
  1550. {
  1551. if (me)
  1552. {
  1553. RazeStructures rs;
  1554. rs.tid = id;
  1555. rs.bid.insert(13);
  1556. rs.destroyed = destroyed;
  1557. cb->sendAndApply(&rs);
  1558. return;
  1559. }
  1560. else
  1561. (*i)->builtBuildings.erase(13); //destroy all other capitols at the beginning of game
  1562. }
  1563. }
  1564. }
  1565. }
  1566. void CGVisitableOPH::onHeroVisit( const CGHeroInstance * h ) const
  1567. {
  1568. if(visitors.find(h->id)==visitors.end())
  1569. {
  1570. onNAHeroVisit(h->id, false);
  1571. switch(ID)
  1572. {
  1573. case 102: //tree
  1574. case 4: //arena
  1575. case 41://library
  1576. case 47: //School of Magic
  1577. case 107://School of War
  1578. break;
  1579. default:
  1580. cb->setObjProperty(id,4,h->id); //add to the visitors
  1581. break;
  1582. }
  1583. }
  1584. else
  1585. {
  1586. onNAHeroVisit(h->id, true);
  1587. }
  1588. }
  1589. void CGVisitableOPH::initObj()
  1590. {
  1591. if(ID==102)
  1592. ttype = ran()%3;
  1593. else
  1594. ttype = -1;
  1595. }
  1596. void CGVisitableOPH::treeSelected( int heroID, int resType, int resVal, ui64 expVal, ui32 result ) const
  1597. {
  1598. if(result) //player agreed to give res for exp
  1599. {
  1600. cb->giveResource(cb->getOwner(heroID),resType,-resVal); //take resource
  1601. cb->changePrimSkill(heroID,4,expVal); //give exp
  1602. cb->setObjProperty(id,4,heroID); //add to the visitors
  1603. }
  1604. }
  1605. void CGVisitableOPH::onNAHeroVisit(int heroID, bool alreadyVisited) const
  1606. {
  1607. int id=0, subid=0, ot=0, val=1, sound = 0;
  1608. switch(ID)
  1609. {
  1610. case 4: //arena
  1611. sound = soundBase::NOMAD;
  1612. ot = 0;
  1613. break;
  1614. case 51: //mercenary camp
  1615. sound = soundBase::NOMAD;
  1616. subid=0;
  1617. ot=80;
  1618. break;
  1619. case 23: //marletto tower
  1620. sound = soundBase::NOMAD;
  1621. subid=1;
  1622. ot=39;
  1623. break;
  1624. case 61:
  1625. sound = soundBase::gazebo;
  1626. subid=2;
  1627. ot=100;
  1628. break;
  1629. case 32:
  1630. sound = soundBase::GETPROTECTION;
  1631. subid=3;
  1632. ot=59;
  1633. break;
  1634. case 100:
  1635. sound = soundBase::gazebo;
  1636. id=5;
  1637. ot=143;
  1638. val=1000;
  1639. break;
  1640. case 102:
  1641. sound = soundBase::gazebo;
  1642. id = 5;
  1643. subid = 1;
  1644. ot = 146;
  1645. val = 1;
  1646. break;
  1647. case 41:
  1648. sound = soundBase::gazebo;
  1649. ot = 66;
  1650. break;
  1651. case 47: //School of Magic
  1652. sound = soundBase::faerie;
  1653. ot = 71;
  1654. break;
  1655. case 107://School of War
  1656. sound = soundBase::MILITARY;
  1657. ot = 158;
  1658. break;
  1659. }
  1660. if (!alreadyVisited)
  1661. {
  1662. switch (ID)
  1663. {
  1664. case 4: //arena
  1665. {
  1666. BlockingDialog sd(false,true);
  1667. sd.soundID = sound;
  1668. sd.text << std::pair<ui8,ui32>(11,ot);
  1669. sd.components.push_back(Component(0,0,2,0));
  1670. sd.components.push_back(Component(0,1,2,0));
  1671. sd.player = cb->getOwner(heroID);
  1672. cb->showBlockingDialog(&sd,boost::bind(&CGVisitableOPH::arenaSelected,this,heroID,_1));
  1673. return;
  1674. }
  1675. case 51:
  1676. case 23:
  1677. case 61:
  1678. case 32:
  1679. {
  1680. cb->changePrimSkill(heroID,subid,val);
  1681. InfoWindow iw;
  1682. iw.soundID = sound;
  1683. iw.components.push_back(Component(0,subid,val,0));
  1684. iw.text << std::pair<ui8,ui32>(11,ot);
  1685. iw.player = cb->getOwner(heroID);
  1686. cb->showInfoDialog(&iw);
  1687. break;
  1688. }
  1689. case 100: //give exp
  1690. {
  1691. InfoWindow iw;
  1692. iw.soundID = sound;
  1693. iw.components.push_back(Component(id,subid,val,0));
  1694. iw.player = cb->getOwner(heroID);
  1695. iw.text << std::pair<ui8,ui32>(11,ot);
  1696. iw.soundID = soundBase::gazebo;
  1697. cb->showInfoDialog(&iw);
  1698. cb->changePrimSkill(heroID,4,val);
  1699. break;
  1700. }
  1701. case 102://tree
  1702. {
  1703. const CGHeroInstance *h = cb->getHero(heroID);
  1704. val = VLC->heroh->reqExp(h->level+val) - VLC->heroh->reqExp(h->level);
  1705. if(!ttype)
  1706. {
  1707. cb->setObjProperty(this->id,4,heroID); //add to the visitors
  1708. InfoWindow iw;
  1709. iw.soundID = sound;
  1710. iw.components.push_back(Component(id,subid,1,0));
  1711. iw.player = cb->getOwner(heroID);
  1712. iw.text << std::pair<ui8,ui32>(11,148);
  1713. cb->showInfoDialog(&iw);
  1714. cb->changePrimSkill(heroID,4,val);
  1715. break;
  1716. }
  1717. else
  1718. {
  1719. int res, resval;
  1720. if(ttype==1)
  1721. {
  1722. res = 6;
  1723. resval = 2000;
  1724. ot = 149;
  1725. }
  1726. else
  1727. {
  1728. res = 5;
  1729. resval = 10;
  1730. ot = 151;
  1731. }
  1732. if(cb->getResource(h->tempOwner,res) < resval) //not enough resources
  1733. {
  1734. ot++;
  1735. InfoWindow iw;
  1736. iw.soundID = sound;
  1737. iw.player = h->tempOwner;
  1738. iw.text << std::pair<ui8,ui32>(11,ot);
  1739. cb->showInfoDialog(&iw);
  1740. return;
  1741. }
  1742. BlockingDialog sd (true, false);
  1743. sd.soundID = sound;
  1744. sd.player = cb->getOwner(heroID);
  1745. sd.text << std::pair<ui8,ui32>(11,ot);
  1746. sd.components.push_back (Component (Component::RESOURCE, res, resval, 0));
  1747. cb->showBlockingDialog(&sd,boost::bind(&CGVisitableOPH::treeSelected,this,heroID,res,resval,val,_1));
  1748. }
  1749. break;
  1750. }
  1751. case 41://library of enlightenment
  1752. {
  1753. const CGHeroInstance *h = cb->getHero(heroID);
  1754. if(h->level < 10 - 2*h->getSecSkillLevel(4)) //not enough level
  1755. {
  1756. InfoWindow iw;
  1757. iw.soundID = sound;
  1758. iw.player = cb->getOwner(heroID);
  1759. iw.text << std::pair<ui8,ui32>(11,68);
  1760. cb->showInfoDialog(&iw);
  1761. }
  1762. else
  1763. {
  1764. cb->setObjProperty(this->id,4,heroID); //add to the visitors
  1765. cb->changePrimSkill(heroID,0,2);
  1766. cb->changePrimSkill(heroID,1,2);
  1767. cb->changePrimSkill(heroID,2,2);
  1768. cb->changePrimSkill(heroID,3,2);
  1769. InfoWindow iw;
  1770. iw.soundID = sound;
  1771. iw.player = cb->getOwner(heroID);
  1772. iw.text << std::pair<ui8,ui32>(11,66);
  1773. cb->showInfoDialog(&iw);
  1774. }
  1775. break;
  1776. }
  1777. case 47: //School of Magic
  1778. case 107://School of War
  1779. {
  1780. int skill = (ID==47 ? 2 : 0);
  1781. if(cb->getResource(cb->getOwner(heroID),6) < 1000) //not enough resources
  1782. {
  1783. InfoWindow iw;
  1784. iw.soundID = sound;
  1785. iw.player = cb->getOwner(heroID);
  1786. iw.text << std::pair<ui8,ui32>(MetaString::ADVOB_TXT,ot+2);
  1787. cb->showInfoDialog(&iw);
  1788. }
  1789. else
  1790. {
  1791. BlockingDialog sd(true,true);
  1792. sd.soundID = sound;
  1793. sd.player = cb->getOwner(heroID);
  1794. sd.text << std::pair<ui8,ui32>(11,ot);
  1795. sd.components.push_back(Component(Component::PRIM_SKILL, skill, +1, 0));
  1796. sd.components.push_back(Component(Component::PRIM_SKILL, skill+1, +1, 0));
  1797. cb->showBlockingDialog(&sd,boost::bind(&CGVisitableOPH::schoolSelected,this,heroID,_1));
  1798. }
  1799. }
  1800. break;
  1801. }
  1802. }
  1803. else
  1804. {
  1805. ot++;
  1806. InfoWindow iw;
  1807. iw.soundID = sound;
  1808. iw.player = cb->getOwner(heroID);
  1809. iw.text << std::pair<ui8,ui32>(11,ot);
  1810. cb->showInfoDialog(&iw);
  1811. }
  1812. }
  1813. const std::string & CGVisitableOPH::getHoverText() const
  1814. {
  1815. int pom = -1;
  1816. switch(ID)
  1817. {
  1818. case 4:
  1819. pom = -1;
  1820. break;
  1821. case 51:
  1822. pom = 8;
  1823. break;
  1824. case 23:
  1825. pom = 7;
  1826. break;
  1827. case 61:
  1828. pom = 11;
  1829. break;
  1830. case 32:
  1831. pom = 4;
  1832. break;
  1833. case 100:
  1834. pom = 5;
  1835. break;
  1836. case 102:
  1837. pom = 18;
  1838. break;
  1839. case 41:
  1840. break;
  1841. case 47: //School of Magic
  1842. pom = 9;
  1843. break;
  1844. case 107://School of War
  1845. pom = 10;
  1846. break;
  1847. default:
  1848. throw std::string("Wrong CGVisitableOPH object ID!\n");
  1849. }
  1850. hoverName = VLC->generaltexth->names[ID];
  1851. if(pom >= 0)
  1852. hoverName += (" " + VLC->generaltexth->xtrainfo[pom]);
  1853. const CGHeroInstance *h = cb->getSelectedHero(cb->getCurrentPlayer());
  1854. if(h)
  1855. {
  1856. hoverName += ' ';
  1857. hoverName += (vstd::contains(visitors,h->id))
  1858. ? (VLC->generaltexth->allTexts[352]) //visited
  1859. : ( VLC->generaltexth->allTexts[353]); //not visited
  1860. }
  1861. return hoverName;
  1862. }
  1863. void CGVisitableOPH::arenaSelected( int heroID, int primSkill ) const
  1864. {
  1865. cb->setObjProperty(id,4,heroID); //add to the visitors
  1866. cb->changePrimSkill(heroID,primSkill-1,2);
  1867. }
  1868. void CGVisitableOPH::setPropertyDer( ui8 what, ui32 val )
  1869. {
  1870. if(what == 4)
  1871. visitors.insert(val);
  1872. }
  1873. void CGVisitableOPH::schoolSelected(int heroID, ui32 which) const
  1874. {
  1875. if(!which) //player refused to pay
  1876. return;
  1877. int base = (ID == 47 ? 2 : 0);
  1878. cb->setObjProperty(id,4,heroID); //add to the visitors
  1879. cb->giveResource(cb->getOwner(heroID),6,-1000); //take 1000 gold
  1880. cb->changePrimSkill(heroID, base + which-1, +1); //give appropriate skill
  1881. }
  1882. COPWBonus::COPWBonus (int index, CGTownInstance *TOWN)
  1883. {
  1884. ID = index;
  1885. town = TOWN;
  1886. id = town->bonusingBuildings.size();
  1887. }
  1888. void COPWBonus::setProperty(ui8 what, ui32 val)
  1889. {
  1890. switch (what)
  1891. {
  1892. case 4:
  1893. visitors.insert(val);
  1894. break;
  1895. case 12:
  1896. visitors.clear();
  1897. break;
  1898. }
  1899. }
  1900. void COPWBonus::onHeroVisit (const CGHeroInstance * h) const
  1901. {
  1902. int heroID = h->id;
  1903. if (town->builtBuildings.find(ID) != town->builtBuildings.end())
  1904. {
  1905. InfoWindow iw;
  1906. iw.player = h->tempOwner;
  1907. switch (town->subID)
  1908. {
  1909. case 0: //Stables
  1910. if (!h->getBonus(HeroBonus::OBJECT, 94)) //no advMap Stables
  1911. {
  1912. GiveBonus gb;
  1913. gb.bonus = HeroBonus(HeroBonus::ONE_WEEK, HeroBonus::LAND_MOVEMENT, HeroBonus::OBJECT, 600, 94, VLC->generaltexth->arraytxt[100]);
  1914. gb.hid = heroID;
  1915. cb->giveHeroBonus(&gb);
  1916. iw.text << VLC->generaltexth->allTexts[580];
  1917. cb->showInfoDialog(&iw);
  1918. }
  1919. break;
  1920. case 5: //Mana Vortex
  1921. if (visitors.find(heroID) == visitors.end() && h->mana <= h->manaLimit())
  1922. {
  1923. cb->setManaPoints (heroID, 2 * h->manaLimit());
  1924. cb->setObjProperty (id, 5, true);
  1925. iw.text << VLC->generaltexth->allTexts[579];
  1926. cb->showInfoDialog(&iw);
  1927. cb->setObjProperty (town->id, 11, id); //add to visitors
  1928. }
  1929. break;
  1930. }
  1931. }
  1932. }
  1933. CTownBonus::CTownBonus (int index, CGTownInstance *TOWN)
  1934. {
  1935. ID = index;
  1936. town = TOWN;
  1937. id = town->bonusingBuildings.size();
  1938. }
  1939. void CTownBonus::setProperty (ui8 what, ui32 val)
  1940. {
  1941. if(what == 4)
  1942. visitors.insert(val);
  1943. }
  1944. void CTownBonus::onHeroVisit (const CGHeroInstance * h) const
  1945. {
  1946. int heroID = h->id;
  1947. if ((town->builtBuildings.find(ID) != town->builtBuildings.end()) && (visitors.find(heroID) == visitors.end()))
  1948. {
  1949. InfoWindow iw;
  1950. int what, val, mid;
  1951. switch (ID)
  1952. {
  1953. case 23:
  1954. switch(town->subID)
  1955. {
  1956. case 2: //wall
  1957. what = 3;
  1958. val = 1;
  1959. mid = 581;
  1960. iw.components.push_back (Component(Component::PRIM_SKILL, 3, 1, 0));
  1961. break;
  1962. case 3: //order of fire
  1963. what = 2;
  1964. val = 1;
  1965. mid = 582;
  1966. iw.components.push_back (Component(Component::PRIM_SKILL, 2, 1, 0));
  1967. break;
  1968. case 6://hall of valhalla
  1969. what = 0;
  1970. val = 1;
  1971. mid = 584;
  1972. iw.components.push_back (Component(Component::PRIM_SKILL, 0, 1, 0));
  1973. break;
  1974. case 5://academy of battle scholars
  1975. what = 4;
  1976. val = 1000;
  1977. mid = 583;
  1978. iw.components.push_back (Component(Component::EXPERIENCE, 0, 1000, 0));
  1979. break;
  1980. }
  1981. break;
  1982. case 17:
  1983. switch(town->subID)
  1984. {
  1985. case 7: //cage of warlords
  1986. what = 1;
  1987. val = 1;
  1988. mid = 585;
  1989. iw.components.push_back (Component(Component::PRIM_SKILL, 1, 1, 0));
  1990. break;
  1991. }
  1992. break;
  1993. }
  1994. iw.player = cb->getOwner(heroID);
  1995. iw.text << VLC->generaltexth->allTexts[mid];
  1996. cb->showInfoDialog(&iw);
  1997. cb->changePrimSkill (heroID, what, val);
  1998. if (town->visitingHero == h)
  1999. cb->setObjProperty (town->id, 11, id); //add to visitors
  2000. else
  2001. cb->setObjProperty (town->id, 13, id); //then it must be garrisoned hero
  2002. }
  2003. }
  2004. bool CArmedInstance::needsLastStack() const
  2005. {
  2006. return false;
  2007. }
  2008. int CArmedInstance::getArmyStrength() const
  2009. {
  2010. int ret = 0;
  2011. for(std::map<si32,std::pair<ui32,si32> >::const_iterator i=army.slots.begin(); i!=army.slots.end(); i++)
  2012. ret += VLC->creh->creatures[i->second.first].AIValue * i->second.second;
  2013. return ret;
  2014. }
  2015. ui64 CArmedInstance::getPower (TSlot slot) const
  2016. {
  2017. return VLC->creh->creatures[army.getCreature(slot)].AIValue * army.getAmount(slot);
  2018. }
  2019. std::string CArmedInstance::getRoughAmount (TSlot slot) const
  2020. {
  2021. return VLC->generaltexth->arraytxt[174 + 3*CCreature::getQuantityID(army.getAmount(slot))];
  2022. }
  2023. /*const std::string & CGCreature::getHoverText() const
  2024. {
  2025. hoverName = VLC->generaltexth->names[ID];
  2026. hoverName += "\n Power rating: ";
  2027. float ratio = ((float)getArmyStrength() / cb->getSelectedHero(cb->getCurrentPlayer())->getTotalStrength());
  2028. if (ratio < 0.1) hoverName += "Effortless";
  2029. else if (ratio < 0.3) hoverName += "Very Weak";
  2030. else if (ratio < 0.6) hoverName += "Weak";
  2031. else if (ratio < 0.9) hoverName += "A bit weaker";
  2032. else if (ratio < 1.1) hoverName += "Equal";
  2033. else if (ratio < 1.3) hoverName += "A bit stronger";
  2034. else if (ratio < 1.8) hoverName += "Strong";
  2035. else if (ratio < 2.5) hoverName += "Very Strong";
  2036. else if (ratio < 4) hoverName += "Challenging";
  2037. else if (ratio < 8) hoverName += "Overpowering";
  2038. else if (ratio < 20) hoverName += "Deadly";
  2039. else hoverName += "Impossible";
  2040. return hoverName;
  2041. }*/
  2042. void CGCreature::onHeroVisit( const CGHeroInstance * h ) const
  2043. {
  2044. int action = takenAction(h);
  2045. switch( action ) //decide what we do...
  2046. {
  2047. case -2: //fight
  2048. fight(h);
  2049. break;
  2050. case -1: //flee
  2051. {
  2052. flee(h);
  2053. break;
  2054. }
  2055. case 0: //join for free
  2056. {
  2057. BlockingDialog ynd(true,false);
  2058. ynd.player = h->tempOwner;
  2059. ynd.text << std::pair<ui8,ui32>(MetaString::ADVOB_TXT, 86);
  2060. ynd.text.addReplacement(MetaString::CRE_PL_NAMES, subID);
  2061. cb->showBlockingDialog(&ynd,boost::bind(&CGCreature::joinDecision,this,h,0,_1));
  2062. break;
  2063. }
  2064. default: //join for gold
  2065. {
  2066. assert(action > 0);
  2067. //ask if player agrees to pay gold
  2068. BlockingDialog ynd(true,false);
  2069. ynd.player = h->tempOwner;
  2070. std::string tmp = VLC->generaltexth->advobtxt[90];
  2071. boost::algorithm::replace_first(tmp,"%d",boost::lexical_cast<std::string>(army.slots.find(0)->second.second));
  2072. boost::algorithm::replace_first(tmp,"%d",boost::lexical_cast<std::string>(action));
  2073. boost::algorithm::replace_first(tmp,"%s",VLC->creh->creatures[subID].namePl);
  2074. ynd.text << tmp;
  2075. cb->showBlockingDialog(&ynd,boost::bind(&CGCreature::joinDecision,this,h,action,_1));
  2076. break;
  2077. }
  2078. }
  2079. }
  2080. void CGCreature::endBattle( BattleResult *result ) const
  2081. {
  2082. if(result->winner==0)
  2083. {
  2084. cb->removeObject(id);
  2085. }
  2086. else
  2087. {
  2088. //int killedAmount=0;
  2089. //for(std::set<std::pair<ui32,si32> >::iterator i=result->casualties[1].begin(); i!=result->casualties[1].end(); i++)
  2090. // if(i->first == subID)
  2091. // killedAmount += i->second;
  2092. //cb->setAmount(id, army.slots.find(0)->second.second - killedAmount);
  2093. MetaString ms;
  2094. int pom = CCreature::getQuantityID(army.slots.find(0)->second.second);
  2095. pom = 174 + 3*pom + 1;
  2096. ms << std::pair<ui8,ui32>(6,pom) << " " << std::pair<ui8,ui32>(7,subID);
  2097. cb->setHoverName(id,&ms);
  2098. }
  2099. }
  2100. void CGCreature::initObj()
  2101. {
  2102. blockVisit = true;
  2103. switch(character)
  2104. {
  2105. case 0:
  2106. character = 0;
  2107. break;
  2108. case 1:
  2109. character = 1 + ran()%7;
  2110. break;
  2111. case 2:
  2112. character = 1 + ran()%10;
  2113. break;
  2114. case 3:
  2115. character = 4 + ran()%7;
  2116. break;
  2117. case 4:
  2118. character = 10;
  2119. break;
  2120. }
  2121. army.slots[0].first = subID;
  2122. si32 &amount = army.slots[0].second;
  2123. CCreature &c = VLC->creh->creatures[subID];
  2124. if(!amount)
  2125. if(c.ammMax == c.ammMin)
  2126. amount = c.ammMax;
  2127. else
  2128. amount = c.ammMin + (ran() % (c.ammMax - c.ammMin));
  2129. MetaString ms;
  2130. int pom = CCreature::getQuantityID(army.slots.find(0)->second.second);
  2131. pom = 174 + 3*pom + 1;
  2132. ms << std::pair<ui8,ui32>(6,pom) << " " << std::pair<ui8,ui32>(7,subID);
  2133. ms.toString(hoverName);
  2134. }
  2135. int CGCreature::takenAction(const CGHeroInstance *h, bool allowJoin) const
  2136. {
  2137. double hlp = h->getTotalStrength() / getArmyStrength();
  2138. if(!character) //compliant creatures will always join
  2139. return 0;
  2140. else if(allowJoin)//test for joining
  2141. {
  2142. int factor;
  2143. if(hlp >= 7)
  2144. factor = 11;
  2145. else if(hlp >= 1)
  2146. factor = (int)(2*(hlp-1));
  2147. else if(hlp >= 0.5)
  2148. factor = -1;
  2149. else if(hlp >= 0.333)
  2150. factor = -2;
  2151. else
  2152. factor = -3;
  2153. int sympathy = 0;
  2154. std::set<ui32> myKindCres; //what creatures are the same kind as we
  2155. myKindCres.insert(subID); //we
  2156. myKindCres.insert(VLC->creh->creatures[subID].upgrades.begin(),VLC->creh->creatures[subID].upgrades.end()); //our upgrades
  2157. for(std::vector<CCreature>::iterator i=VLC->creh->creatures.begin(); i!=VLC->creh->creatures.end(); i++)
  2158. if(vstd::contains(i->upgrades, (ui32) id)) //it's our base creatures
  2159. myKindCres.insert(i->idNumber);
  2160. int count = 0, //how many creatures of our kind has hero
  2161. totalCount = 0;
  2162. for (std::map<si32,std::pair<ui32,si32> >::const_iterator i = h->army.slots.begin(); i != h->army.slots.end(); i++)
  2163. {
  2164. if(vstd::contains(myKindCres,i->second.first))
  2165. count += i->second.second;
  2166. totalCount += i->second.second;
  2167. }
  2168. if(count*2 > totalCount)
  2169. sympathy++;
  2170. if(count)
  2171. sympathy++;
  2172. int charisma = factor + h->getSecSkillLevel(4) + sympathy;
  2173. if(charisma >= character) //creatures might join...
  2174. {
  2175. if(h->getSecSkillLevel(4) + sympathy + 1 >= character)
  2176. return 0; //join for free
  2177. else if(h->getSecSkillLevel(4) * 2 + sympathy + 1 >= character)
  2178. return VLC->creh->creatures[subID].cost[6] * army.slots.find(0)->second.second; //join for gold
  2179. }
  2180. }
  2181. //we are still here - creatures not joined heroes, test for fleeing
  2182. //TODO: it's provisional formula, should be replaced with original one (or something closer to it)
  2183. //TODO: should be deterministic (will be needed for Vision spell)
  2184. int hlp2 = (int) (hlp - 2)*1000;
  2185. if(!neverFlees
  2186. && hlp2 >= 0
  2187. && rand()%2000 < hlp2
  2188. )
  2189. return -1; //flee
  2190. else
  2191. return -2; //fight
  2192. }
  2193. void CGCreature::fleeDecision(const CGHeroInstance *h, ui32 pursue) const
  2194. {
  2195. if(pursue)
  2196. {
  2197. fight(h);
  2198. }
  2199. else
  2200. {
  2201. cb->removeObject(id);
  2202. }
  2203. }
  2204. void CGCreature::joinDecision(const CGHeroInstance *h, int cost, ui32 accept) const
  2205. {
  2206. if(!accept)
  2207. {
  2208. if(takenAction(h,false) == -1) //they flee
  2209. {
  2210. flee(h);
  2211. }
  2212. else //they fight
  2213. {
  2214. InfoWindow iw;
  2215. iw.player = h->tempOwner;
  2216. iw.text << std::pair<ui8,ui32>(11,87); //Insulted by your refusal of their offer, the monsters attack!
  2217. cb->showInfoDialog(&iw);
  2218. fight(h);
  2219. }
  2220. }
  2221. else //accepted
  2222. {
  2223. if (cb->getResource(h->tempOwner,6) < cost) //player don't have enough gold!
  2224. {
  2225. InfoWindow iw;
  2226. iw.player = h->tempOwner;
  2227. iw.text << std::pair<ui8,ui32>(1,29); //You don't have enough gold
  2228. cb->showInfoDialog(&iw);
  2229. //act as if player refused
  2230. joinDecision(h,cost,false);
  2231. return;
  2232. }
  2233. //take gold
  2234. if(cost)
  2235. cb->giveResource(h->tempOwner,6,-cost);
  2236. int slot = h->army.getSlotFor(subID);
  2237. if(slot >= 0) //there is place
  2238. {
  2239. //add creatures
  2240. SetGarrisons sg;
  2241. sg.garrs[h->id] = h->army;
  2242. if(vstd::contains(h->army.slots,slot)) //add to already present stack
  2243. {
  2244. sg.garrs[h->id].slots[slot].second += army.slots.find(0)->second.second;
  2245. }
  2246. else //add as a new stack
  2247. {
  2248. sg.garrs[h->id].slots[slot] = army.slots.find(0)->second;
  2249. }
  2250. cb->sendAndApply(&sg);
  2251. cb->removeObject(id);
  2252. }
  2253. else
  2254. {
  2255. cb->showGarrisonDialog(id,h->id,true,boost::bind(&IGameCallback::removeObject,cb,id)); //show garrison window and remove ourselves from map when player ends
  2256. }
  2257. }
  2258. }
  2259. void CGCreature::fight( const CGHeroInstance *h ) const
  2260. {
  2261. cb->startBattleI(h, this, boost::bind(&CGCreature::endBattle,this,_1));
  2262. }
  2263. void CGCreature::flee( const CGHeroInstance * h ) const
  2264. {
  2265. BlockingDialog ynd(true,false);
  2266. ynd.player = h->tempOwner;
  2267. ynd.text << std::pair<ui8,ui32>(11,91);
  2268. ynd.text.addReplacement(MetaString::CRE_PL_NAMES, subID);
  2269. cb->showBlockingDialog(&ynd,boost::bind(&CGCreature::fleeDecision,this,h,_1));
  2270. }
  2271. void CGMine::onHeroVisit( const CGHeroInstance * h ) const
  2272. {
  2273. if(subID == 7) //TODO: support for abandoned mine
  2274. return;
  2275. if(h->tempOwner == tempOwner) //we're visiting our mine
  2276. {
  2277. cb->showGarrisonDialog(id,h->id,true,0);
  2278. return;
  2279. }
  2280. //TODO: check if mine is guarded
  2281. cb->setOwner(id,h->tempOwner); //not ours? flag it!
  2282. MetaString ms;
  2283. ms << std::pair<ui8,ui32>(9,subID) << " (" << std::pair<ui8,ui32>(6,23+h->tempOwner) << ")";
  2284. cb->setHoverName(id,&ms);
  2285. int vv=1; //amount of resource per turn
  2286. if (subID==0 || subID==2)
  2287. vv++;
  2288. else if (subID==6)
  2289. vv = 1000;
  2290. InfoWindow iw;
  2291. iw.soundID = soundBase::FLAGMINE;
  2292. iw.text << std::pair<ui8,ui32>(10,subID);
  2293. iw.player = h->tempOwner;
  2294. iw.components.push_back(Component(2,subID,vv,-1));
  2295. cb->showInfoDialog(&iw);
  2296. }
  2297. void CGMine::newTurn() const
  2298. {
  2299. if(cb->getDate() == 1)
  2300. return;
  2301. if (tempOwner == NEUTRAL_PLAYER)
  2302. return;
  2303. int vv = 1;
  2304. if (subID==0 || subID==2)
  2305. vv++;
  2306. else if (subID==6)
  2307. vv = 1000;
  2308. cb->giveResource(tempOwner,subID,vv);
  2309. }
  2310. void CGMine::initObj()
  2311. {
  2312. MetaString ms;
  2313. ms << std::pair<ui8,ui32>(9,subID);
  2314. if(tempOwner >= PLAYER_LIMIT)
  2315. tempOwner = NEUTRAL_PLAYER;
  2316. else
  2317. ms << " (" << std::pair<ui8,ui32>(6,23+tempOwner) << ")";
  2318. ms.toString(hoverName);
  2319. }
  2320. void CGResource::initObj()
  2321. {
  2322. blockVisit = true;
  2323. hoverName = VLC->generaltexth->restypes[subID];
  2324. if(!amount)
  2325. {
  2326. switch(subID)
  2327. {
  2328. case 6:
  2329. amount = 500 + (rand()%6)*100;
  2330. break;
  2331. case 0: case 2:
  2332. amount = 6 + (rand()%5);
  2333. break;
  2334. default:
  2335. amount = 3 + (rand()%3);
  2336. break;
  2337. }
  2338. }
  2339. }
  2340. void CGResource::onHeroVisit( const CGHeroInstance * h ) const
  2341. {
  2342. if(army.slots.size())
  2343. {
  2344. if(message.size())
  2345. {
  2346. BlockingDialog ynd(true,false);
  2347. ynd.player = h->getOwner();
  2348. ynd.text << message;
  2349. cb->showBlockingDialog(&ynd,boost::bind(&CGResource::fightForRes,this,_1,h));
  2350. }
  2351. else
  2352. {
  2353. fightForRes(1,h);
  2354. }
  2355. }
  2356. else
  2357. {
  2358. if(message.length())
  2359. {
  2360. InfoWindow iw;
  2361. iw.player = h->tempOwner;
  2362. iw.text << message;
  2363. cb->showInfoDialog(&iw);
  2364. }
  2365. collectRes(h->getOwner());
  2366. }
  2367. }
  2368. void CGResource::collectRes( int player ) const
  2369. {
  2370. cb->giveResource(player,subID,amount);
  2371. ShowInInfobox sii;
  2372. sii.player = player;
  2373. sii.c = Component(2,subID,amount,0);
  2374. sii.text << std::pair<ui8,ui32>(11,113);
  2375. sii.text.addReplacement(MetaString::RES_NAMES, subID);
  2376. cb->showCompInfo(&sii);
  2377. cb->removeObject(id);
  2378. }
  2379. void CGResource::fightForRes(ui32 agreed, const CGHeroInstance *h) const
  2380. {
  2381. if(agreed)
  2382. cb->startBattleI(h, this, boost::bind(&CGResource::endBattle,this,_1,h));
  2383. }
  2384. void CGResource::endBattle( BattleResult *result, const CGHeroInstance *h ) const
  2385. {
  2386. if(result->winner == 0) //attacker won
  2387. collectRes(h->getOwner());
  2388. }
  2389. void CGVisitableOPW::newTurn() const
  2390. {
  2391. if (cb->getDate(1)==1) //first day of week
  2392. {
  2393. cb->setObjProperty(id,5,false);
  2394. MetaString ms; //set text to "not visited"
  2395. ms << std::pair<ui8,ui32>(3,ID) << " " << std::pair<ui8,ui32>(1,353);
  2396. cb->setHoverName(id,&ms);
  2397. }
  2398. }
  2399. void CGVisitableOPW::onHeroVisit( const CGHeroInstance * h ) const
  2400. {
  2401. int mid, sound = 0;
  2402. switch (ID)
  2403. {
  2404. case 55: //mystical garden
  2405. sound = soundBase::experience;
  2406. mid = 92;
  2407. break;
  2408. case 112://windmill
  2409. sound = soundBase::GENIE;
  2410. mid = 170;
  2411. break;
  2412. case 109://waterwheel
  2413. sound = soundBase::GENIE;
  2414. mid = 164;
  2415. break;
  2416. }
  2417. if (visited)
  2418. {
  2419. if (ID!=112)
  2420. mid++;
  2421. else
  2422. mid--;
  2423. InfoWindow iw;
  2424. iw.soundID = sound;
  2425. iw.player = h->tempOwner;
  2426. iw.text << std::pair<ui8,ui32>(11,mid);
  2427. cb->showInfoDialog(&iw);
  2428. }
  2429. else
  2430. {
  2431. int type, sub, val;
  2432. type = 2;
  2433. switch (ID)
  2434. {
  2435. case 55:
  2436. if (rand()%2)
  2437. {
  2438. sub = 5;
  2439. val = 5;
  2440. }
  2441. else
  2442. {
  2443. sub = 6;
  2444. val = 500;
  2445. }
  2446. break;
  2447. case 112:
  2448. mid = 170;
  2449. sub = (rand() % 5) + 1;
  2450. val = (rand() % 4) + 3;
  2451. break;
  2452. case 109:
  2453. mid = 164;
  2454. sub = 6;
  2455. if(cb->getDate(0)<8)
  2456. val = 500;
  2457. else
  2458. val = 1000;
  2459. }
  2460. cb->giveResource(h->tempOwner,sub,val);
  2461. InfoWindow iw;
  2462. iw.soundID = sound;
  2463. iw.player = h->tempOwner;
  2464. iw.components.push_back(Component(type,sub,val,0));
  2465. iw.text << std::pair<ui8,ui32>(11,mid);
  2466. cb->showInfoDialog(&iw);
  2467. cb->setObjProperty(id,5,true);
  2468. MetaString ms; //set text to "visited"
  2469. ms << std::pair<ui8,ui32>(3,ID) << " " << std::pair<ui8,ui32>(1,352);
  2470. cb->setHoverName(id,&ms);
  2471. }
  2472. }
  2473. void CGVisitableOPW::setPropertyDer( ui8 what, ui32 val )
  2474. {
  2475. if(what == 5)
  2476. visited = val;
  2477. }
  2478. void CGTeleport::onHeroVisit( const CGHeroInstance * h ) const
  2479. {
  2480. int destinationid=-1;
  2481. switch(ID)
  2482. {
  2483. case 43: //one way - find corresponding exit monolith
  2484. if(vstd::contains(objs,44) && vstd::contains(objs[44],subID) && objs[44][subID].size())
  2485. destinationid = objs[44][subID][rand()%objs[44][subID].size()];
  2486. else
  2487. tlog2 << "Cannot find corresponding exit monolith for "<< id << std::endl;
  2488. break;
  2489. case 45: //two way monolith - pick any other one
  2490. if(vstd::contains(objs,45) && vstd::contains(objs[45],subID) && objs[45][subID].size()>1)
  2491. while ((destinationid = objs[45][subID][rand()%objs[45][subID].size()])==id);
  2492. else
  2493. tlog2 << "Cannot find corresponding exit monolith for "<< id << std::endl;
  2494. break;
  2495. case 103: //find nearest subterranean gate on the other level
  2496. {
  2497. for(int i=0; i < gates.size(); i++)
  2498. {
  2499. if(gates[i].first == id)
  2500. {
  2501. destinationid = gates[i].second;
  2502. break;
  2503. }
  2504. else if(gates[i].second == id)
  2505. {
  2506. destinationid = gates[i].first;
  2507. break;
  2508. }
  2509. }
  2510. break;
  2511. }
  2512. }
  2513. if(destinationid < 0)
  2514. {
  2515. tlog2 << "Cannot find exit... (obj at " << pos << ") :( \n";
  2516. return;
  2517. }
  2518. cb->moveHero(h->id,CGHeroInstance::convertPosition(cb->getObj(destinationid)->pos,true) - getVisitableOffset(),
  2519. true);
  2520. }
  2521. void CGTeleport::initObj()
  2522. {
  2523. int si = subID;
  2524. if(ID == 103) //ignore subterranean gates subid
  2525. si = 0;
  2526. objs[ID][si].push_back(id);
  2527. }
  2528. void CGTeleport::postInit() //matches subterranean gates into pairs
  2529. {
  2530. //split on underground and surface gates
  2531. std::vector<const CGObjectInstance *> gatesSplit[2]; //surface and underground gates
  2532. for(size_t i = 0; i < objs[103][0].size(); i++)
  2533. {
  2534. const CGObjectInstance *hlp = cb->getObj(objs[103][0][i]);
  2535. gatesSplit[hlp->pos.z].push_back(hlp);
  2536. }
  2537. //sort by position
  2538. std::sort(gatesSplit[0].begin(), gatesSplit[0].end(), boost::bind(&CGObjectInstance::pos, _1) < boost::bind(&CGObjectInstance::pos, _2));
  2539. for(size_t i = 0; i < gatesSplit[0].size(); i++)
  2540. {
  2541. const CGObjectInstance *cur = gatesSplit[0][i];
  2542. //find nearest underground exit
  2543. std::pair<int,double> best(-1,150000); //pair<pos_in_vector, distance>
  2544. for(int j = 0; j < gatesSplit[1].size(); j++)
  2545. {
  2546. const CGObjectInstance *checked = gatesSplit[1][j];
  2547. if(!checked)
  2548. continue;
  2549. double hlp = checked->pos.dist2d(cur->pos);
  2550. if(hlp < best.second)
  2551. {
  2552. best.first = j;
  2553. best.second = hlp;
  2554. }
  2555. }
  2556. gates.push_back(std::pair<int, int>(cur->id, gatesSplit[1][best.first]->id));
  2557. gatesSplit[1][best.first] = NULL;
  2558. }
  2559. objs.erase(103);
  2560. }
  2561. void CGArtifact::initObj()
  2562. {
  2563. blockVisit = true;
  2564. if(ID == 5)
  2565. hoverName = VLC->arth->artifacts[subID].Name();
  2566. }
  2567. void CGArtifact::onHeroVisit( const CGHeroInstance * h ) const
  2568. {
  2569. if(!army.slots.size())
  2570. {
  2571. if(ID == 5)
  2572. {
  2573. InfoWindow iw;
  2574. iw.soundID = soundBase::treasure;
  2575. iw.player = h->tempOwner;
  2576. iw.components.push_back(Component(4,subID,0,0));
  2577. if(message.length())
  2578. iw.text << message;
  2579. else
  2580. iw.text << std::pair<ui8,ui32>(12,subID);
  2581. cb->showInfoDialog(&iw);
  2582. }
  2583. pick(h);
  2584. }
  2585. else
  2586. {
  2587. if(message.size())
  2588. {
  2589. BlockingDialog ynd(true,false);
  2590. ynd.player = h->getOwner();
  2591. ynd.text << message;
  2592. cb->showBlockingDialog(&ynd,boost::bind(&CGArtifact::fightForArt,this,_1,h));
  2593. }
  2594. else
  2595. {
  2596. fightForArt(0,h);
  2597. }
  2598. }
  2599. }
  2600. void CGArtifact::pick(const CGHeroInstance * h) const
  2601. {
  2602. if(ID == 5) //Artifact
  2603. {
  2604. cb->giveHeroArtifact(subID,h->id,-2);
  2605. }
  2606. else if(ID == 93) // Spell scroll
  2607. {
  2608. //TODO: support for the spell scroll
  2609. }
  2610. cb->removeObject(id);
  2611. }
  2612. void CGArtifact::fightForArt( ui32 agreed, const CGHeroInstance *h ) const
  2613. {
  2614. if(agreed)
  2615. cb->startBattleI(h, this, boost::bind(&CGArtifact::endBattle,this,_1,h));
  2616. }
  2617. void CGArtifact::endBattle( BattleResult *result, const CGHeroInstance *h ) const
  2618. {
  2619. if(result->winner == 0) //attacker won
  2620. pick(h);
  2621. }
  2622. void CGPickable::initObj()
  2623. {
  2624. blockVisit = true;
  2625. switch(ID)
  2626. {
  2627. case 12: //campfire
  2628. val2 = (ran()%3) + 4; //4 - 6
  2629. val1 = val2 * 100;
  2630. type = ran()%6; //given resource
  2631. break;
  2632. case 29: //floatsam
  2633. switch(type = ran()%4)
  2634. {
  2635. case 0:
  2636. val1 = val2 = 0;
  2637. break;
  2638. case 1:
  2639. val1 = 5;
  2640. val2 = 0;
  2641. break;
  2642. case 2:
  2643. val1 = 5;
  2644. val2 = 200;
  2645. break;
  2646. case 3:
  2647. val1 = 10;
  2648. val2 = 500;
  2649. break;
  2650. }
  2651. break;
  2652. case 82: //sea chest
  2653. {
  2654. int hlp = ran()%100;
  2655. if(hlp < 20)
  2656. {
  2657. val1 = 0;
  2658. type = 0;
  2659. }
  2660. else if(hlp < 90)
  2661. {
  2662. val1 = 1500;
  2663. type = 2;
  2664. }
  2665. else
  2666. {
  2667. val1 = 1000;
  2668. val2 = cb->getRandomArt (CArtifact::ART_TREASURE);
  2669. type = 1;
  2670. }
  2671. }
  2672. break;
  2673. case 86: //Shipwreck Survivor
  2674. {
  2675. int hlp = ran()%100;
  2676. if(hlp < 55)
  2677. val1 = cb->getRandomArt (CArtifact::ART_TREASURE);
  2678. else if(hlp < 75)
  2679. val1 = cb->getRandomArt (CArtifact::ART_MINOR);
  2680. else if(hlp < 95)
  2681. val1 = cb->getRandomArt (CArtifact::ART_MAJOR);
  2682. else
  2683. val1 = cb->getRandomArt (CArtifact::ART_RELIC);
  2684. }
  2685. break;
  2686. case 101: //treasure chest
  2687. {
  2688. int hlp = ran()%100;
  2689. if(hlp >= 95)
  2690. {
  2691. type = 1;
  2692. val1 = cb->getRandomArt (CArtifact::ART_TREASURE);
  2693. return;
  2694. }
  2695. else if (hlp >= 65)
  2696. {
  2697. val1 = 2000;
  2698. }
  2699. else if(hlp >= 33)
  2700. {
  2701. val1 = 1500;
  2702. }
  2703. else
  2704. {
  2705. val1 = 1000;
  2706. }
  2707. val2 = val1 - 500;
  2708. type = 0;
  2709. break;
  2710. }
  2711. }
  2712. }
  2713. void CGPickable::onHeroVisit( const CGHeroInstance * h ) const
  2714. {
  2715. switch(ID)
  2716. {
  2717. case 12: //campfire
  2718. {
  2719. cb->giveResource(h->tempOwner,type,val2); //non-gold resource
  2720. cb->giveResource(h->tempOwner,6,val1);//gold
  2721. InfoWindow iw;
  2722. iw.soundID = soundBase::experience;
  2723. iw.player = h->tempOwner;
  2724. iw.components.push_back(Component(2,6,val1,0));
  2725. iw.components.push_back(Component(2,type,val2,0));
  2726. iw.text << std::pair<ui8,ui32>(11,23);
  2727. cb->showInfoDialog(&iw);
  2728. break;
  2729. }
  2730. case 29: //flotsam
  2731. {
  2732. cb->giveResource(h->tempOwner,0,val1); //wood
  2733. cb->giveResource(h->tempOwner,6,val2);//gold
  2734. InfoWindow iw;
  2735. iw.soundID = soundBase::GENIE;
  2736. iw.player = h->tempOwner;
  2737. if(val1)
  2738. iw.components.push_back(Component(2,0,val1,0));
  2739. if(val2)
  2740. iw.components.push_back(Component(2,6,val2,0));
  2741. iw.text.addTxt(MetaString::ADVOB_TXT, 51+type);
  2742. cb->showInfoDialog(&iw);
  2743. break;
  2744. }
  2745. case 82: //Sea Chest
  2746. {
  2747. InfoWindow iw;
  2748. iw.soundID = soundBase::chest;
  2749. iw.player = h->tempOwner;
  2750. iw.text.addTxt(MetaString::ADVOB_TXT, 116 + type);
  2751. if(val1) //there is gold
  2752. {
  2753. iw.components.push_back(Component(2,6,val1,0));
  2754. cb->giveResource(h->tempOwner,6,val1);
  2755. }
  2756. if(type == 1) //art
  2757. {
  2758. //TODO: what if no space in backpack?
  2759. iw.components.push_back(Component(4, val2, 1, 0));
  2760. iw.text.addReplacement(MetaString::ART_NAMES, val2);
  2761. cb->giveHeroArtifact(val2,h->id,-2);
  2762. }
  2763. cb->showInfoDialog(&iw);
  2764. break;
  2765. }
  2766. case 86: //Shipwreck Survivor
  2767. {
  2768. //TODO: what if no space in backpack?
  2769. InfoWindow iw;
  2770. iw.soundID = soundBase::experience;
  2771. iw.player = h->tempOwner;
  2772. iw.components.push_back(Component(4,val1,1,0));
  2773. iw.text.addTxt(MetaString::ADVOB_TXT, 125);
  2774. iw.text.addReplacement(MetaString::ART_NAMES, val1);
  2775. cb->giveHeroArtifact(val1,h->id,-2);
  2776. cb->showInfoDialog(&iw);
  2777. break;
  2778. }
  2779. case 101: //treasure chest
  2780. {
  2781. if (subID) //not OH3 treasure chest
  2782. {
  2783. tlog2 << "Not supported WoG treasure chest!\n";
  2784. return;
  2785. }
  2786. if(type) //there is an artifact
  2787. {
  2788. cb->giveHeroArtifact(val1,h->id,-2);
  2789. InfoWindow iw;
  2790. iw.soundID = soundBase::treasure;
  2791. iw.player = h->tempOwner;
  2792. iw.components.push_back(Component(4,val1,1,0));
  2793. iw.text << std::pair<ui8,ui32>(11,145);
  2794. iw.text.addReplacement(MetaString::ART_NAMES, val1);
  2795. cb->showInfoDialog(&iw);
  2796. break;
  2797. }
  2798. else
  2799. {
  2800. BlockingDialog sd(false,true);
  2801. sd.player = h->tempOwner;
  2802. sd.text << std::pair<ui8,ui32>(11,146);
  2803. sd.components.push_back(Component(2,6,val1,0));
  2804. sd.components.push_back(Component(5,0,val2,0));
  2805. sd.soundID = soundBase::chest;
  2806. boost::function<void(ui32)> fun = boost::bind(&CGPickable::chosen,this,_1,h->id);
  2807. cb->showBlockingDialog(&sd,fun);
  2808. return;
  2809. }
  2810. }
  2811. }
  2812. cb->removeObject(id);
  2813. }
  2814. void CGPickable::chosen( int which, int heroID ) const
  2815. {
  2816. switch(which)
  2817. {
  2818. case 1: //player pick gold
  2819. cb->giveResource(cb->getOwner(heroID),6,val1);
  2820. break;
  2821. case 2: //player pick exp
  2822. cb->changePrimSkill(heroID, 4, val2);
  2823. break;
  2824. default:
  2825. throw std::string("Unhandled treasure choice");
  2826. }
  2827. cb->removeObject(id);
  2828. }
  2829. const std::string & CGSeerHut::getHoverText() const
  2830. {
  2831. return VLC->generaltexth->names[ID]; //TODO
  2832. }
  2833. void CGWitchHut::initObj()
  2834. {
  2835. ability = allowedAbilities[ran()%allowedAbilities.size()];
  2836. }
  2837. void CGWitchHut::onHeroVisit( const CGHeroInstance * h ) const
  2838. {
  2839. InfoWindow iw;
  2840. iw.soundID = soundBase::gazebo;
  2841. iw.player = h->getOwner();
  2842. if(!hasVisited(h->tempOwner))
  2843. cb->setObjProperty(id,10,h->tempOwner);
  2844. if(h->getSecSkillLevel(ability)) //you alredy know this skill
  2845. {
  2846. iw.text << std::pair<ui8,ui32>(11,172);
  2847. iw.text.addReplacement(MetaString::SEC_SKILL_NAME, ability);
  2848. }
  2849. else if(h->secSkills.size() >= SKILL_PER_HERO) //already all skills slots used
  2850. {
  2851. iw.text << std::pair<ui8,ui32>(11,173);
  2852. iw.text.addReplacement(MetaString::SEC_SKILL_NAME, ability);
  2853. }
  2854. else //give sec skill
  2855. {
  2856. iw.components.push_back(Component(1, ability, 1, 0));
  2857. iw.text << std::pair<ui8,ui32>(11,171);
  2858. iw.text.addReplacement(MetaString::SEC_SKILL_NAME, ability);
  2859. cb->changeSecSkill(h->id,ability,1,true);
  2860. }
  2861. cb->showInfoDialog(&iw);
  2862. }
  2863. const std::string & CGWitchHut::getHoverText() const
  2864. {
  2865. hoverName = VLC->generaltexth->names[ID];
  2866. if(hasVisited(cb->getCurrentPlayer())) //TODO: use local player, not current
  2867. {
  2868. hoverName += "\n" + VLC->generaltexth->allTexts[356]; // + (learn %s)
  2869. boost::algorithm::replace_first(hoverName,"%s",VLC->generaltexth->skillName[ability]);
  2870. const CGHeroInstance *h = cb->getSelectedHero(cb->getCurrentPlayer());
  2871. if(h && h->getSecSkillLevel(ability)) //hero knows that ability
  2872. hoverName += "\n\n" + VLC->generaltexth->allTexts[357]; // (Already learned)
  2873. }
  2874. return hoverName;
  2875. }
  2876. void CGBonusingObject::onHeroVisit( const CGHeroInstance * h ) const
  2877. {
  2878. bool visited = h->getBonus(HeroBonus::OBJECT,ID);
  2879. int messageID, bonusType, bonusVal;
  2880. int bonusMove = 0, sound = -1;
  2881. InfoWindow iw;
  2882. iw.player = h->tempOwner;
  2883. GiveBonus gbonus;
  2884. gbonus.hid = h->id;
  2885. gbonus.bonus.duration = HeroBonus::ONE_BATTLE;
  2886. gbonus.bonus.source = HeroBonus::OBJECT;
  2887. gbonus.bonus.id = ID;
  2888. switch(ID)
  2889. {
  2890. case 11: //buoy
  2891. messageID = 21;
  2892. sound = soundBase::MORALE;
  2893. gbonus.bonus.type = HeroBonus::MORALE;
  2894. gbonus.bonus.val = +1;
  2895. gbonus.bdescr << std::pair<ui8,ui32>(6,94);
  2896. break;
  2897. case 14: //swan pond
  2898. messageID = 29;
  2899. sound = soundBase::LUCK;
  2900. gbonus.bonus.type = HeroBonus::LUCK;
  2901. gbonus.bonus.val = 2;
  2902. gbonus.bdescr << std::pair<ui8,ui32>(6,67);
  2903. bonusMove = -h->movement;
  2904. break;
  2905. case 28: //Faerie Ring
  2906. messageID = 49;
  2907. sound = soundBase::LUCK;
  2908. gbonus.bonus.type = HeroBonus::LUCK;
  2909. gbonus.bonus.val = 1;
  2910. gbonus.bdescr << std::pair<ui8,ui32>(6,71);
  2911. break;
  2912. case 30: //fountain of fortune
  2913. messageID = 55;
  2914. sound = soundBase::LUCK;
  2915. gbonus.bonus.type = HeroBonus::LUCK;
  2916. gbonus.bonus.val = rand()%5 - 1;
  2917. gbonus.bdescr << std::pair<ui8,ui32>(6,69);
  2918. gbonus.bdescr.addReplacement((gbonus.bonus.val<0 ? "-" : "+") + boost::lexical_cast<std::string>(gbonus.bonus.val));
  2919. break;
  2920. case 38: //idol of fortune
  2921. messageID = 62;
  2922. sound = soundBase::experience;
  2923. if(cb->getDate(1) == 7) //7th day of week
  2924. gbonus.bonus.type = HeroBonus::MORALE_AND_LUCK;
  2925. else
  2926. gbonus.bonus.type = (cb->getDate(1)%2) ? HeroBonus::LUCK : HeroBonus::MORALE;
  2927. gbonus.bonus.val = 1;
  2928. gbonus.bdescr << std::pair<ui8,ui32>(6,68);
  2929. break;
  2930. case 52: //Mermaid
  2931. messageID = 83;
  2932. sound = soundBase::LUCK;
  2933. gbonus.bonus.type = HeroBonus::LUCK;
  2934. gbonus.bonus.val = 1;
  2935. gbonus.bdescr << std::pair<ui8,ui32>(6,72);
  2936. break;
  2937. case 64: //Rally Flag
  2938. sound = soundBase::MORALE;
  2939. messageID = 111;
  2940. gbonus.bonus.type = HeroBonus::MORALE_AND_LUCK;
  2941. gbonus.bonus.val = 1;
  2942. gbonus.bdescr << std::pair<ui8,ui32>(6,102);
  2943. bonusMove = 400;
  2944. break;
  2945. case 56: //oasis
  2946. messageID = 95;
  2947. gbonus.bonus.type = HeroBonus::MORALE;
  2948. gbonus.bonus.val = 1;
  2949. gbonus.bdescr << std::pair<ui8,ui32>(6,95);
  2950. bonusMove = 800;
  2951. break;
  2952. case 96: //temple
  2953. messageID = 140;
  2954. iw.soundID = soundBase::temple;
  2955. gbonus.bonus.type = HeroBonus::MORALE;
  2956. if(cb->getDate(1)==7) //sunday
  2957. {
  2958. gbonus.bonus.val = 2;
  2959. gbonus.bdescr << std::pair<ui8,ui32>(6,97);
  2960. }
  2961. else
  2962. {
  2963. gbonus.bonus.val = 1;
  2964. gbonus.bdescr << std::pair<ui8,ui32>(6,96);
  2965. }
  2966. break;
  2967. case 110://Watering Hole
  2968. sound = soundBase::MORALE;
  2969. messageID = 166;
  2970. gbonus.bonus.type = HeroBonus::MORALE;
  2971. gbonus.bonus.val = 1;
  2972. gbonus.bdescr << std::pair<ui8,ui32>(6,100);
  2973. bonusMove = 400;
  2974. break;
  2975. case 31: //Fountain of Youth
  2976. sound = soundBase::MORALE;
  2977. messageID = 57;
  2978. gbonus.bonus.type = HeroBonus::MORALE;
  2979. gbonus.bonus.val = 1;
  2980. gbonus.bdescr << std::pair<ui8,ui32>(6,103);
  2981. bonusMove = 400;
  2982. break;
  2983. case 94: //Stables TODO: upgrade Cavaliers
  2984. sound = soundBase::horse20;
  2985. std::set<ui32> slots;
  2986. for (std::map<si32,std::pair<ui32,si32> >::const_iterator i = h->army.slots.begin(); i != h->army.slots.end(); ++i)
  2987. {
  2988. if(i->second.first == 10)
  2989. slots.insert(i->first);
  2990. }
  2991. if (!slots.empty())
  2992. {
  2993. for (std::set<ui32>::const_iterator i = slots.begin(); i != slots.end(); i++)
  2994. {
  2995. UpgradeCreature uc(*i, id, 11);
  2996. //uc.applyGh (&gh);
  2997. }
  2998. }
  2999. messageID = 137;
  3000. gbonus.bonus.type = HeroBonus::LAND_MOVEMENT;
  3001. gbonus.bonus.val = 600;
  3002. gbonus.bdescr << std::pair<ui8,ui32>(6, 100);
  3003. break;
  3004. }
  3005. if(visited)
  3006. {
  3007. if(ID==64 || ID==96 || ID==56 || ID==52 || ID==94)
  3008. messageID--;
  3009. else
  3010. messageID++;
  3011. }
  3012. else
  3013. {
  3014. if(gbonus.bonus.type == HeroBonus::MORALE || gbonus.bonus.type == HeroBonus::MORALE_AND_LUCK)
  3015. iw.components.push_back(Component(8,0,gbonus.bonus.val,0));
  3016. if(gbonus.bonus.type == HeroBonus::LUCK || gbonus.bonus.type == HeroBonus::MORALE_AND_LUCK)
  3017. iw.components.push_back(Component(9,0,gbonus.bonus.val,0));
  3018. cb->giveHeroBonus(&gbonus);
  3019. if(bonusMove) //swan pond - take all move points
  3020. {
  3021. SetMovePoints smp;
  3022. smp.hid = h->id;
  3023. smp.val = h->movement + bonusMove;
  3024. cb->setMovePoints(&smp);
  3025. }
  3026. }
  3027. iw.soundID = sound;
  3028. iw.text << std::pair<ui8,ui32>(11,messageID);
  3029. cb->showInfoDialog(&iw);
  3030. }
  3031. const std::string & CGBonusingObject::getHoverText() const
  3032. {
  3033. const CGHeroInstance *h = cb->getSelectedHero(cb->getCurrentPlayer());
  3034. hoverName = VLC->generaltexth->names[ID];
  3035. if(h)
  3036. {
  3037. if(!h->getBonus(HeroBonus::OBJECT,ID))
  3038. hoverName += " " + VLC->generaltexth->allTexts[353]; //not visited
  3039. else
  3040. hoverName += " " + VLC->generaltexth->allTexts[352]; //visited
  3041. }
  3042. return hoverName;
  3043. }
  3044. void CGBonusingObject::initObj()
  3045. {
  3046. if(ID == 11 || ID == 52) //Buoy / Mermaid
  3047. {
  3048. blockVisit = true;
  3049. }
  3050. }
  3051. void CGMagicSpring::onHeroVisit(const CGHeroInstance * h) const
  3052. {
  3053. int messageID;
  3054. InfoWindow iw;
  3055. iw.player = h->tempOwner;
  3056. iw.soundID = soundBase::GENIE;
  3057. if (!visited)
  3058. {
  3059. if (h->mana > h->manaLimit())
  3060. messageID = 76;
  3061. else
  3062. {
  3063. messageID = 74;
  3064. cb->setManaPoints (h->id, 2 * h->manaLimit());
  3065. cb->setObjProperty (id, 5, true);
  3066. }
  3067. }
  3068. else
  3069. messageID = 75;
  3070. iw.text << std::pair<ui8,ui32>(11,messageID);
  3071. cb->showInfoDialog(&iw);
  3072. }
  3073. const std::string & CGMagicSpring::getHoverText() const
  3074. {
  3075. hoverName = VLC->generaltexth->names[ID];
  3076. if(!visited)
  3077. hoverName += " " + VLC->generaltexth->allTexts[353]; //not visited
  3078. else
  3079. hoverName += " " + VLC->generaltexth->allTexts[352]; //visited
  3080. return hoverName;
  3081. }
  3082. void CGMagicWell::onHeroVisit( const CGHeroInstance * h ) const
  3083. {
  3084. int message;
  3085. InfoWindow iw;
  3086. iw.soundID = soundBase::faerie;
  3087. iw.player = h->tempOwner;
  3088. if(h->getBonus(HeroBonus::OBJECT,ID)) //has already visited Well today
  3089. {
  3090. message = 78;
  3091. }
  3092. else if(h->mana < h->manaLimit())
  3093. {
  3094. giveDummyBonus(h->id);
  3095. cb->setManaPoints(h->id,h->manaLimit());
  3096. message = 77;
  3097. }
  3098. else
  3099. {
  3100. message = 79;
  3101. }
  3102. iw.text << std::pair<ui8,ui32>(11,message); //"A second drink at the well in one day will not help you."
  3103. cb->showInfoDialog(&iw);
  3104. }
  3105. const std::string & CGMagicWell::getHoverText() const
  3106. {
  3107. getNameVis(hoverName);
  3108. return hoverName;
  3109. }
  3110. void CGPandoraBox::initObj()
  3111. {
  3112. blockVisit = (ID==6); //block only if it's really pandora's box (events also derive from that class)
  3113. }
  3114. void CGPandoraBox::onHeroVisit(const CGHeroInstance * h) const
  3115. {
  3116. BlockingDialog bd (true, false);
  3117. bd.player = h->getOwner();
  3118. bd.soundID = soundBase::QUEST;
  3119. bd.text.addTxt (MetaString::ADVOB_TXT, 14);
  3120. cb->showBlockingDialog (&bd, boost::bind (&CGPandoraBox::open, this, h, _1));
  3121. }
  3122. void CGPandoraBox::open( const CGHeroInstance * h, ui32 accept ) const
  3123. {
  3124. if (accept)
  3125. {
  3126. if (army.slots.size() > 0) //if pandora's box is protected by army
  3127. {
  3128. InfoWindow iw;
  3129. iw.player = h->tempOwner;
  3130. iw.text.addTxt(MetaString::ADVOB_TXT, 16);
  3131. cb->showInfoDialog(&iw);
  3132. cb->startBattleI(h, this, boost::bind(&CGPandoraBox::endBattle, this, h, _1)); //grants things after battle
  3133. }
  3134. else if (message.size() == 0 && resources.size() == 0
  3135. && primskills.size() == 0 && abilities.size() == 0
  3136. && abilityLevels.size() == 0 && artifacts.size() == 0
  3137. && spells.size() == 0 && creatures.slots.size() > 0
  3138. && gainedExp == 0 && manaDiff == 0 && moraleDiff == 0 && luckDiff == 0) //if it gives nothing without battle
  3139. {
  3140. InfoWindow iw;
  3141. iw.player = h->tempOwner;
  3142. iw.text.addTxt(MetaString::ADVOB_TXT, 15);
  3143. cb->showInfoDialog(&iw);
  3144. }
  3145. else //if it gives something without battle
  3146. {
  3147. giveContents (h, false);
  3148. }
  3149. }
  3150. }
  3151. void CGPandoraBox::endBattle( const CGHeroInstance *h, BattleResult *result ) const
  3152. {
  3153. if(result->winner)
  3154. return;
  3155. giveContents(h,true);
  3156. }
  3157. void CGPandoraBox::giveContents( const CGHeroInstance *h, bool afterBattle ) const
  3158. {
  3159. InfoWindow iw;
  3160. iw.player = h->getOwner();
  3161. bool changesPrimSkill = false;
  3162. for (int i = 0; i < primskills.size(); i++)
  3163. {
  3164. if(primskills[i])
  3165. {
  3166. changesPrimSkill = true;
  3167. break;
  3168. }
  3169. }
  3170. if(gainedExp || changesPrimSkill || abilities.size())
  3171. {
  3172. getText(iw,afterBattle,175,h);
  3173. if(gainedExp)
  3174. iw.components.push_back(Component(Component::EXPERIENCE,0,gainedExp,0));
  3175. for(int i=0; i<primskills.size(); i++)
  3176. if(primskills[i])
  3177. iw.components.push_back(Component(Component::PRIM_SKILL,i,primskills[i],0));
  3178. for(int i=0; i<abilities.size(); i++)
  3179. iw.components.push_back(Component(Component::SEC_SKILL,abilities[i],abilityLevels[i],0));
  3180. cb->showInfoDialog(&iw);
  3181. //give exp
  3182. if(gainedExp)
  3183. cb->changePrimSkill(h->id,4,gainedExp,false);
  3184. //give prim skills
  3185. for(int i=0; i<primskills.size(); i++)
  3186. if(primskills[i])
  3187. cb->changePrimSkill(h->id,i,primskills[i],false);
  3188. //give sec skills
  3189. for(int i=0; i<abilities.size(); i++)
  3190. {
  3191. int curLev = h->getSecSkillLevel(abilities[i]);
  3192. if(curLev && curLev < abilityLevels[i]
  3193. || h->secSkills.size() < SKILL_PER_HERO )
  3194. {
  3195. cb->changeSecSkill(h->id,abilities[i],abilityLevels[i],true);
  3196. }
  3197. }
  3198. }
  3199. if(spells.size())
  3200. {
  3201. std::set<ui32> spellsToGive;
  3202. iw.components.clear();
  3203. for(int i=0; i<spells.size(); i++)
  3204. {
  3205. iw.components.push_back(Component(Component::SPELL,spells[i],0,0));
  3206. spellsToGive.insert(spells[i]);
  3207. }
  3208. if(spellsToGive.size())
  3209. {
  3210. cb->changeSpells(h->id,true,spellsToGive);
  3211. cb->showInfoDialog(&iw);
  3212. }
  3213. }
  3214. if(manaDiff)
  3215. {
  3216. getText(iw,afterBattle,manaDiff,176,177,h);
  3217. iw.components.push_back(Component(Component::PRIM_SKILL,5,manaDiff,0));
  3218. cb->showInfoDialog(&iw);
  3219. cb->setManaPoints(h->id, h->mana + manaDiff);
  3220. }
  3221. if(moraleDiff)
  3222. {
  3223. getText(iw,afterBattle,moraleDiff,178,179,h);
  3224. iw.components.push_back(Component(Component::MORALE,0,moraleDiff,0));
  3225. cb->showInfoDialog(&iw);
  3226. GiveBonus gb;
  3227. gb.bonus = HeroBonus(HeroBonus::ONE_BATTLE,HeroBonus::MORALE,HeroBonus::OBJECT,moraleDiff,id,"");
  3228. gb.hid = h->id;
  3229. cb->giveHeroBonus(&gb);
  3230. }
  3231. if(luckDiff)
  3232. {
  3233. getText(iw,afterBattle,luckDiff,180,181,h);
  3234. iw.components.push_back(Component(Component::LUCK,0,luckDiff,0));
  3235. cb->showInfoDialog(&iw);
  3236. GiveBonus gb;
  3237. gb.bonus = HeroBonus(HeroBonus::ONE_BATTLE,HeroBonus::LUCK,HeroBonus::OBJECT,luckDiff,id,"");
  3238. gb.hid = h->id;
  3239. cb->giveHeroBonus(&gb);
  3240. }
  3241. iw.components.clear();
  3242. iw.text.clear();
  3243. for(int i=0; i<resources.size(); i++)
  3244. {
  3245. if(resources[i] < 0)
  3246. iw.components.push_back(Component(Component::RESOURCE,i,resources[i],0));
  3247. }
  3248. if(iw.components.size())
  3249. {
  3250. getText(iw,afterBattle,182,h);
  3251. cb->showInfoDialog(&iw);
  3252. }
  3253. iw.components.clear();
  3254. iw.text.clear();
  3255. for(int i=0; i<resources.size(); i++)
  3256. {
  3257. if(resources[i] > 0)
  3258. iw.components.push_back(Component(Component::RESOURCE,i,resources[i],0));
  3259. }
  3260. if(iw.components.size())
  3261. {
  3262. getText(iw,afterBattle,183,h);
  3263. cb->showInfoDialog(&iw);
  3264. }
  3265. iw.components.clear();
  3266. // getText(iw,afterBattle,183,h);
  3267. for(int i=0; i<artifacts.size(); i++)
  3268. {
  3269. iw.components.push_back(Component(Component::ARTIFACT,artifacts[i],0,0));
  3270. if(iw.components.size() >= 14)
  3271. {
  3272. cb->showInfoDialog(&iw);
  3273. iw.components.clear();
  3274. }
  3275. }
  3276. if(iw.components.size())
  3277. {
  3278. cb->showInfoDialog(&iw);
  3279. }
  3280. for(int i=0; i<resources.size(); i++)
  3281. if(resources[i])
  3282. cb->giveResource(h->getOwner(),i,resources[i]);
  3283. for(int i=0; i<artifacts.size(); i++)
  3284. cb->giveHeroArtifact(artifacts[i],h->id,-2);
  3285. iw.components.clear();
  3286. iw.text.clear();
  3287. if (creatures.slots.size())
  3288. { //this part is taken straight from creature bank
  3289. MetaString loot;
  3290. CCreatureSet ourArmy = creatures;
  3291. for (std::map<si32,std::pair<ui32,si32> >::const_iterator i = ourArmy.slots.begin(); i != ourArmy.slots.end(); i++)
  3292. { //build list of joined creatures
  3293. iw.components.push_back (Component(Component::CREATURE, i->second.first, i->second.second, 0));
  3294. loot << "%s";
  3295. if (i->second.second == 1)
  3296. loot.addReplacement (MetaString::CRE_SING_NAMES, i->second.first);
  3297. else
  3298. loot.addReplacement (MetaString::CRE_PL_NAMES, i->second.first);
  3299. }
  3300. if (ourArmy.slots.size() == 1 && ourArmy.slots.begin()->second.second == 1)
  3301. iw.text.addTxt (MetaString::ADVOB_TXT, 185);
  3302. else
  3303. iw.text.addTxt (MetaString::ADVOB_TXT, 186);
  3304. iw.text.addReplacement (loot.buildList());
  3305. iw.text.addReplacement (h->name);
  3306. cb->showInfoDialog(&iw);
  3307. cb->giveCreatures (id, h, ourArmy);
  3308. }
  3309. if(!afterBattle && message.size())
  3310. {
  3311. iw.text << message;
  3312. cb->showInfoDialog(&iw);
  3313. }
  3314. cb->removeObject(id);
  3315. }
  3316. void CGPandoraBox::getText( InfoWindow &iw, bool &afterBattle, int text, const CGHeroInstance * h ) const
  3317. {
  3318. if(afterBattle || !message.size())
  3319. {
  3320. iw.text.addTxt(MetaString::ADVOB_TXT,text);//%s has lost treasure.
  3321. iw.text.addReplacement(h->name);
  3322. }
  3323. else
  3324. {
  3325. iw.text << message;
  3326. afterBattle = true;
  3327. }
  3328. }
  3329. void CGPandoraBox::getText( InfoWindow &iw, bool &afterBattle, int val, int negative, int positive, const CGHeroInstance * h ) const
  3330. {
  3331. iw.components.clear();
  3332. iw.text.clear();
  3333. if(afterBattle || !message.size())
  3334. {
  3335. iw.text.addTxt(MetaString::ADVOB_TXT,val < 0 ? negative : positive); //%s's luck takes a turn for the worse / %s's luck increases
  3336. iw.text.addReplacement(h->name);
  3337. }
  3338. else
  3339. {
  3340. iw.text << message;
  3341. afterBattle = true;
  3342. }
  3343. }
  3344. void CGEvent::onHeroVisit( const CGHeroInstance * h ) const
  3345. {
  3346. if(!(availableFor & (1 << h->tempOwner)))
  3347. return;
  3348. if(cb->getPlayerSettings(h->tempOwner)->human)
  3349. {
  3350. if(humanActivate)
  3351. activated(h);
  3352. }
  3353. else if(computerActivate)
  3354. activated(h);
  3355. }
  3356. void CGEvent::activated( const CGHeroInstance * h ) const
  3357. {
  3358. if(army.slots.size() > 0)
  3359. {
  3360. InfoWindow iw;
  3361. iw.player = h->tempOwner;
  3362. if(message.size())
  3363. iw.text << message;
  3364. else
  3365. iw.text.addTxt(MetaString::ADVOB_TXT, 16);
  3366. cb->showInfoDialog(&iw);
  3367. cb->startBattleI(h, this, boost::bind(&CGEvent::endBattle,this,h,_1));
  3368. }
  3369. else
  3370. {
  3371. giveContents(h,false);
  3372. }
  3373. }
  3374. void CGObservatory::onHeroVisit( const CGHeroInstance * h ) const
  3375. {
  3376. InfoWindow iw;
  3377. iw.soundID = soundBase::LIGHTHOUSE;
  3378. iw.player = h->tempOwner;
  3379. iw.text.addTxt(MetaString::ADVOB_TXT,98 + (ID==60));
  3380. cb->showInfoDialog(&iw);
  3381. FoWChange fw;
  3382. fw.player = h->tempOwner;
  3383. fw.mode = 1;
  3384. cb->getTilesInRange(fw.tiles,pos,20,h->tempOwner,1);
  3385. cb->sendAndApply(&fw);
  3386. }
  3387. void CGShrine::onHeroVisit( const CGHeroInstance * h ) const
  3388. {
  3389. if(spell == 255)
  3390. {
  3391. tlog1 << "Not initialized shrine visited!\n";
  3392. return;
  3393. }
  3394. if(!hasVisited(h->tempOwner))
  3395. cb->setObjProperty(id,10,h->tempOwner);
  3396. InfoWindow iw;
  3397. iw.soundID = soundBase::temple;
  3398. iw.player = h->getOwner();
  3399. iw.text.addTxt(MetaString::ADVOB_TXT,127 + ID - 88);
  3400. iw.text.addTxt(MetaString::SPELL_NAME,spell);
  3401. iw.text << ".";
  3402. if(!h->getArt(17)) //no spellbook
  3403. {
  3404. iw.text.addTxt(MetaString::ADVOB_TXT,131);
  3405. }
  3406. else if(ID == 90 && !h->getSecSkillLevel(7)) //it's third level spell and hero doesn't have wisdom
  3407. {
  3408. iw.text.addTxt(MetaString::ADVOB_TXT,130);
  3409. }
  3410. else if(vstd::contains(h->spells,spell))//hero already knows the spell
  3411. {
  3412. iw.text.addTxt(MetaString::ADVOB_TXT,174);
  3413. }
  3414. else //give spell
  3415. {
  3416. std::set<ui32> spells;
  3417. spells.insert(spell);
  3418. cb->changeSpells(h->id,true,spells);
  3419. iw.components.push_back(Component(Component::SPELL,spell,0,0));
  3420. }
  3421. cb->showInfoDialog(&iw);
  3422. }
  3423. void CGShrine::initObj()
  3424. {
  3425. if(spell == 255) //spell not set
  3426. {
  3427. int level = ID-87;
  3428. std::vector<ui16> possibilities;
  3429. cb->getAllowedSpells (possibilities, level);
  3430. if(!possibilities.size())
  3431. {
  3432. tlog1 << "Error: cannot init shrine, no allowed spells!\n";
  3433. return;
  3434. }
  3435. spell = possibilities[ran() % possibilities.size()];
  3436. }
  3437. }
  3438. const std::string & CGShrine::getHoverText() const
  3439. {
  3440. hoverName = VLC->generaltexth->names[ID];
  3441. if(hasVisited(cb->getCurrentPlayer())) //TODO: use local player, not current
  3442. {
  3443. hoverName += "\n" + VLC->generaltexth->allTexts[355]; // + (learn %s)
  3444. boost::algorithm::replace_first(hoverName,"%s",VLC->spellh->spells[spell].name);
  3445. const CGHeroInstance *h = cb->getSelectedHero(cb->getCurrentPlayer());
  3446. if(h && vstd::contains(h->spells,spell)) //hero knows that ability
  3447. hoverName += "\n\n" + VLC->generaltexth->allTexts[354]; // (Already learned)
  3448. }
  3449. return hoverName;
  3450. }
  3451. void CGSignBottle::initObj()
  3452. {
  3453. //if no text is set than we pick random from the predefined ones
  3454. if(!message.size())
  3455. message = VLC->generaltexth->randsign[ran()%VLC->generaltexth->randsign.size()];
  3456. if(ID == 59)
  3457. {
  3458. blockVisit = true;
  3459. }
  3460. }
  3461. void CGSignBottle::onHeroVisit( const CGHeroInstance * h ) const
  3462. {
  3463. InfoWindow iw;
  3464. iw.soundID = soundBase::STORE;
  3465. iw.player = h->getOwner();
  3466. iw.text << message;
  3467. cb->showInfoDialog(&iw);
  3468. if(ID == 59)
  3469. cb->removeObject(id);
  3470. }
  3471. void CGScholar::giveAnyBonus( const CGHeroInstance * h ) const
  3472. {
  3473. }
  3474. void CGScholar::onHeroVisit( const CGHeroInstance * h ) const
  3475. {
  3476. int type = bonusType;
  3477. int bid = bonusID;
  3478. //check if the bonus if applicable, if not - give primary skill (always possible)
  3479. int ssl = h->getSecSkillLevel(bid); //current sec skill level, used if bonusType == 1
  3480. if((type == 1
  3481. && ((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)
  3482. || (type == 2 && (!h->getArt(17) || vstd::contains(h->spells, (ui32) bid)))) //hero doesn't have a spellbook or already knows the spell
  3483. {
  3484. type = 0;
  3485. bid = ran() % PRIMARY_SKILLS;
  3486. }
  3487. InfoWindow iw;
  3488. iw.soundID = soundBase::gazebo;
  3489. iw.player = h->getOwner();
  3490. iw.text.addTxt(MetaString::ADVOB_TXT,115);
  3491. switch (type)
  3492. {
  3493. case 0:
  3494. cb->changePrimSkill(h->id,bid,+1);
  3495. iw.components.push_back(Component(Component::PRIM_SKILL,bid,+1,0));
  3496. break;
  3497. case 1:
  3498. cb->changeSecSkill(h->id,bid,+1);
  3499. iw.components.push_back(Component(Component::SEC_SKILL,bid,ssl+1,0));
  3500. break;
  3501. case 2:
  3502. {
  3503. std::set<ui32> hlp;
  3504. hlp.insert(bid);
  3505. cb->changeSpells(h->id,true,hlp);
  3506. iw.components.push_back(Component(Component::SPELL,bid,0,0));
  3507. }
  3508. break;
  3509. default:
  3510. tlog1 << "Error: wrong bonustype (" << (int)type << ") for Scholar!\n";
  3511. return;
  3512. }
  3513. cb->showInfoDialog(&iw);
  3514. cb->removeObject(id);
  3515. }
  3516. void CGScholar::initObj()
  3517. {
  3518. blockVisit = true;
  3519. if(bonusType == 255)
  3520. {
  3521. bonusType = ran()%3;
  3522. switch(bonusType)
  3523. {
  3524. case 0:
  3525. bonusID = ran() % PRIMARY_SKILLS;
  3526. break;
  3527. case 1:
  3528. bonusID = ran() % SKILL_QUANTITY;
  3529. break;
  3530. case 2:
  3531. std::vector<ui16> possibilities;
  3532. for (int i = 1; i < 6; ++i)
  3533. cb->getAllowedSpells (possibilities, i);
  3534. bonusID = possibilities[ran() % possibilities.size()];
  3535. break;
  3536. }
  3537. }
  3538. }
  3539. void CGGarrison::onHeroVisit (const CGHeroInstance *h) const
  3540. {
  3541. if (h->tempOwner != tempOwner && army.slots.size() > 0) {
  3542. //TODO: Find a way to apply magic garrison effects in battle.
  3543. cb->startBattleI(h, this, boost::bind(&CGGarrison::fightOver, this, h, _1));
  3544. return;
  3545. }
  3546. //New owner.
  3547. if (h->tempOwner != tempOwner)
  3548. cb->setOwner(id, h->tempOwner);
  3549. cb->showGarrisonDialog(id, h->id, removableUnits, 0);
  3550. }
  3551. void CGGarrison::fightOver (const CGHeroInstance *h, BattleResult *result) const
  3552. {
  3553. if (result->winner == 0)
  3554. onHeroVisit(h);
  3555. }
  3556. ui8 CGGarrison::getPassableness() const
  3557. {
  3558. return 1<<tempOwner;
  3559. }
  3560. void CGOnceVisitable::onHeroVisit( const CGHeroInstance * h ) const
  3561. {
  3562. int sound = 0;
  3563. int txtid = -1;
  3564. switch(ID)
  3565. {
  3566. case 22: //Corpse
  3567. txtid = 37;
  3568. sound = soundBase::MYSTERY;
  3569. break;
  3570. case 39: //Lean To
  3571. sound = soundBase::GENIE;
  3572. txtid = 64;
  3573. break;
  3574. case 105://Wagon
  3575. sound = soundBase::GENIE;
  3576. txtid = 154;
  3577. break;
  3578. case 108:
  3579. break;
  3580. default:
  3581. tlog1 << "Error: Unknown object (" << ID <<") treated as CGOnceVisitable!\n";
  3582. return;
  3583. }
  3584. if(ID == 108)//Warrior's Tomb
  3585. {
  3586. //ask if player wants to search the Tomb
  3587. BlockingDialog bd(true, false);
  3588. sound = soundBase::GRAVEYARD;
  3589. bd.player = h->getOwner();
  3590. bd.text.addTxt(MetaString::ADVOB_TXT,161);
  3591. cb->showBlockingDialog(&bd,boost::bind(&CGOnceVisitable::searchTomb,this,h,_1));
  3592. return;
  3593. }
  3594. InfoWindow iw;
  3595. iw.soundID = sound;
  3596. iw.player = h->getOwner();
  3597. if(players.size()) //we have been already visited...
  3598. {
  3599. txtid++;
  3600. if(ID == 105) //wagon has extra text (for finding art) we need to omit
  3601. txtid++;
  3602. }
  3603. else //first visit - give bonus!
  3604. {
  3605. switch(artOrRes)
  3606. {
  3607. case 0: // first visit but empty
  3608. if (ID == 22) //Corpse
  3609. ++txtid;
  3610. else
  3611. txtid+=2;
  3612. iw.text.addTxt(MetaString::ADVOB_TXT, txtid);
  3613. break;
  3614. case 1: //art
  3615. iw.components.push_back(Component(Component::ARTIFACT,bonusType,0,0));
  3616. cb->giveHeroArtifact(bonusType,h->id,-2);
  3617. iw.text.addTxt(MetaString::ADVOB_TXT, txtid);
  3618. if (ID == 22) //Corpse
  3619. {
  3620. iw.text << "%s";
  3621. iw.text.addReplacement (MetaString::ART_NAMES, bonusType);
  3622. }
  3623. break;
  3624. case 2: //res
  3625. iw.text.addTxt(MetaString::ADVOB_TXT, txtid);
  3626. iw.components.push_back (Component(Component::RESOURCE, bonusType, bonusVal, 0));
  3627. cb->giveResource(h->getOwner(), bonusType, bonusVal);
  3628. break;
  3629. }
  3630. if(ID == 105 && artOrRes == 1)
  3631. {
  3632. iw.text.localStrings.back().second++;
  3633. iw.text.addReplacement(MetaString::ART_NAMES, bonusType);
  3634. }
  3635. }
  3636. cb->showInfoDialog(&iw);
  3637. cb->setObjProperty(id,10,h->getOwner());
  3638. }
  3639. const std::string & CGOnceVisitable::getHoverText() const
  3640. {
  3641. hoverName = VLC->generaltexth->names[ID] + " ";
  3642. hoverName += (hasVisited(cb->getCurrentPlayer())
  3643. ? (VLC->generaltexth->allTexts[352]) //visited
  3644. : ( VLC->generaltexth->allTexts[353])); //not visited
  3645. return hoverName;
  3646. }
  3647. void CGOnceVisitable::initObj()
  3648. {
  3649. switch(ID)
  3650. {
  3651. case 22: //Corpse
  3652. {
  3653. blockVisit = true;
  3654. int hlp = ran()%100;
  3655. if(hlp < 20)
  3656. {
  3657. artOrRes = 1;
  3658. bonusType = cb->getRandomArt (CArtifact::ART_TREASURE | CArtifact::ART_MINOR | CArtifact::ART_MAJOR);
  3659. }
  3660. else
  3661. {
  3662. artOrRes = 0;
  3663. }
  3664. }
  3665. break;
  3666. case 39: //Lean To
  3667. {
  3668. artOrRes = 2;
  3669. bonusType = ran()%6; //any basic resource without gold
  3670. bonusVal = ran()%4 + 1;
  3671. }
  3672. break;
  3673. case 108://Warrior's Tomb
  3674. {
  3675. artOrRes = 1;
  3676. int hlp = ran()%100;
  3677. if(hlp < 30)
  3678. bonusType = cb->getRandomArt (CArtifact::ART_TREASURE);
  3679. else if(hlp < 80)
  3680. bonusType = cb->getRandomArt (CArtifact::ART_MINOR);
  3681. else if(hlp < 95)
  3682. bonusType = cb->getRandomArt (CArtifact::ART_MAJOR);
  3683. else
  3684. bonusType = cb->getRandomArt (CArtifact::ART_RELIC);
  3685. }
  3686. break;
  3687. case 105://Wagon
  3688. {
  3689. int hlp = ran()%100;
  3690. if(hlp < 10)
  3691. {
  3692. artOrRes = 0; // nothing... :(
  3693. }
  3694. else if(hlp < 50) //minor or treasure art
  3695. {
  3696. artOrRes = 1;
  3697. bonusType = cb->getRandomArt (CArtifact::ART_TREASURE | CArtifact::ART_MINOR);
  3698. }
  3699. else //2 - 5 of non-gold resource
  3700. {
  3701. artOrRes = 2;
  3702. bonusType = ran()%6;
  3703. bonusVal = ran()%4 + 2;
  3704. }
  3705. }
  3706. break;
  3707. }
  3708. }
  3709. void CGOnceVisitable::searchTomb(const CGHeroInstance *h, ui32 accept) const
  3710. {
  3711. if(accept)
  3712. {
  3713. InfoWindow iw;
  3714. iw.player = h->getOwner();
  3715. iw.components.push_back(Component(Component::MORALE,0,-3,0));
  3716. if(players.size()) //we've been already visited, player found nothing
  3717. {
  3718. iw.text.addTxt(MetaString::ADVOB_TXT,163);
  3719. }
  3720. else //first visit - give artifact
  3721. {
  3722. iw.text.addTxt(MetaString::ADVOB_TXT,162);
  3723. iw.components.push_back(Component(Component::ARTIFACT,bonusType,0,0));
  3724. iw.text.addReplacement(MetaString::ART_NAMES, bonusType);
  3725. cb->giveHeroArtifact(bonusType,h->id,-2);
  3726. }
  3727. if(!h->getBonus(HeroBonus::OBJECT,ID)) //we don't have modifier from this object yet
  3728. {
  3729. //ruin morale
  3730. GiveBonus gb;
  3731. gb.hid = h->id;
  3732. gb.bonus = HeroBonus(HeroBonus::ONE_BATTLE,HeroBonus::MORALE,HeroBonus::OBJECT,-3,id,"");
  3733. gb.bdescr.addTxt(MetaString::ARRAY_TXT,104); //Warrior Tomb Visited -3
  3734. cb->giveHeroBonus(&gb);
  3735. }
  3736. cb->showInfoDialog(&iw);
  3737. cb->setObjProperty(id,10,h->getOwner());
  3738. }
  3739. }
  3740. void CBank::initObj()
  3741. {
  3742. switch (ID) //find apriopriate key
  3743. {
  3744. case 16: //bank
  3745. index = subID; break;
  3746. case 24: //derelict ship
  3747. index = 8; break;
  3748. case 25: //utopia
  3749. index = 10; break;
  3750. case 84: //crypt
  3751. index = 9; break;
  3752. case 85: //shipwreck
  3753. index = 7; break;
  3754. }
  3755. bc = NULL;
  3756. daycounter = 0;
  3757. multiplier = 1;
  3758. }
  3759. const std::string & CBank::getHoverText() const
  3760. {
  3761. hoverName = VLC->objh->creBanksNames[index];
  3762. if (bc == NULL)
  3763. hoverName += " " + VLC->generaltexth->allTexts[352];
  3764. else
  3765. hoverName += " " + VLC->generaltexth->allTexts[353];
  3766. return hoverName;
  3767. }
  3768. void CBank::reset(ui16 var1) //prevents desync
  3769. {
  3770. ui8 chance = 0;
  3771. for (ui8 i = 0; i < VLC->objh->banksInfo[index].size(); i++)
  3772. {
  3773. if (var1 < (chance += VLC->objh->banksInfo[index][i]->chance))
  3774. {
  3775. bc = VLC->objh->banksInfo[index][i];
  3776. break;
  3777. }
  3778. }
  3779. artifacts.clear();
  3780. }
  3781. void CBank::initialize() const
  3782. {
  3783. cb->setObjProperty (id, 14, ran()); //synchronous reset
  3784. for (ui8 i = 0; i <= 3; i++)
  3785. {
  3786. for (ui8 n = 0; n < bc->artifacts[i]; n++) //new function using proper randomization algorithm
  3787. {
  3788. switch (i)
  3789. {
  3790. case 0:
  3791. cb->setObjProperty(id, 18, cb->getRandomArt (CArtifact::ART_TREASURE));
  3792. break;
  3793. case 1:
  3794. cb->setObjProperty(id, 18, cb->getRandomArt (CArtifact::ART_MINOR));
  3795. break;
  3796. case 2:
  3797. cb->setObjProperty(id, 18, cb->getRandomArt (CArtifact::ART_MAJOR));
  3798. break;
  3799. case 3:
  3800. cb->setObjProperty(id, 18, cb->getRandomArt (CArtifact::ART_RELIC));
  3801. break;
  3802. }
  3803. }
  3804. }
  3805. }
  3806. void CBank::setPropertyDer (ui8 what, ui32 val)
  3807. /// random values are passed as arguments and processed identically on all clients
  3808. {
  3809. switch (what)
  3810. {
  3811. case 11: //daycounter
  3812. if (val == 0)
  3813. daycounter = 0;
  3814. else
  3815. daycounter++;
  3816. break;
  3817. case 12: //multiplier, in percent
  3818. multiplier = ((float)val)/100;
  3819. break;
  3820. case 13: //bank preset
  3821. bc = VLC->objh->banksInfo[index][val];
  3822. break;
  3823. case 14:
  3824. reset (val%100);
  3825. break;
  3826. case 15:
  3827. bc = NULL;
  3828. break;
  3829. case 16:
  3830. artifacts.clear();
  3831. break;
  3832. case 17: //set ArmedInstance army
  3833. {
  3834. int upgraded = 0;
  3835. if (val%100 < bc->upgradeChance) //once again anti-desync
  3836. upgraded = 1;
  3837. switch (bc->guards.size())
  3838. {
  3839. case 1:
  3840. for (int i = 0; i <= 4; i++)
  3841. army.setCreature (i, bc->guards[0].first + upgraded, bc->guards[0].second / 5 );
  3842. break;
  3843. case 4:
  3844. {
  3845. std::vector< std::pair <ui16, ui32> >::const_iterator it;
  3846. for (it = bc->guards.begin(); it != bc->guards.end(); it++)
  3847. {
  3848. int n = army.slots.size(); //debug
  3849. army.setCreature (n, it->first, it->second);
  3850. }
  3851. }
  3852. break;
  3853. default:
  3854. tlog2 << "Error: Unexpected army data: " << bc->guards.size() <<" items found";
  3855. return;
  3856. }
  3857. }
  3858. break;
  3859. case 18: //add Artifact
  3860. artifacts.push_back (val);
  3861. break;
  3862. }
  3863. }
  3864. void CBank::newTurn() const
  3865. {
  3866. if (bc == NULL)
  3867. {
  3868. if (cb->getDate(0) == 1)
  3869. initialize(); //initialize on first day
  3870. else if (daycounter >= 28 && (subID < 13 || subID > 16)) //no reset for Emissaries
  3871. {
  3872. initialize();
  3873. cb->setObjProperty (id, 11, 0); //daycounter 0
  3874. if (ID == 24 && cb->getDate(0) > 1)
  3875. cb->setObjProperty (id, 16, 0); //derelict ships are usable only once
  3876. }
  3877. else
  3878. cb->setObjProperty (id, 11, 1); //daycounter++
  3879. }
  3880. }
  3881. void CBank::onHeroVisit (const CGHeroInstance * h) const
  3882. {
  3883. if (bc != NULL)
  3884. {
  3885. int banktext = 0;
  3886. switch (ID)
  3887. {
  3888. case 16: //generic bank
  3889. banktext = 32;
  3890. break;
  3891. case 24:
  3892. banktext = 41;
  3893. break;
  3894. case 25: //utopia
  3895. banktext = 47;
  3896. break;
  3897. case 84: //crypt
  3898. banktext = 119;
  3899. break;
  3900. case 85: //shipwreck
  3901. banktext = 122;
  3902. break;
  3903. }
  3904. BlockingDialog bd (true, false);
  3905. bd.player = h->getOwner();
  3906. bd.soundID = soundBase::DANGER;
  3907. bd.text << VLC->generaltexth->advobtxt[banktext];
  3908. if (ID == 16)
  3909. bd.text.addReplacement (VLC->objh->creBanksNames[index]);
  3910. cb->showBlockingDialog (&bd, boost::bind (&CBank::fightGuards, this, h, _1));
  3911. }
  3912. else
  3913. {
  3914. InfoWindow iw;
  3915. iw.soundID = soundBase::GRAVEYARD;
  3916. iw.player = h->getOwner();
  3917. //if (ID == 16 || ID == 24 || ID == 25 || ID == 84)
  3918. iw.text << VLC->generaltexth->advobtxt[33];
  3919. iw.text.addReplacement (VLC->objh->creBanksNames[index]);
  3920. cb->showInfoDialog(&iw);
  3921. }
  3922. }
  3923. void CBank::fightGuards (const CGHeroInstance * h, ui32 accept) const
  3924. {
  3925. if (accept)
  3926. {
  3927. cb->setObjProperty (id, 17, ran()); //get army
  3928. cb->startBattleI (h, this, boost::bind (&CBank::endBattle, this, h, _1), true);
  3929. }
  3930. }
  3931. void CBank::endBattle (const CGHeroInstance *h, const BattleResult *result) const
  3932. {
  3933. if (result->winner == 0)
  3934. {
  3935. int textID = -1;
  3936. InfoWindow iw;
  3937. iw.player = h->getOwner();
  3938. MetaString loot;
  3939. switch (ID)
  3940. {
  3941. case 16: case 25:
  3942. textID = 34;
  3943. break;
  3944. case 24: //derelict ship
  3945. if (bc->resources.size() != 0)
  3946. textID = 43;
  3947. else
  3948. {
  3949. iw.components.push_back (Component (Component::MORALE, 0 , -2, 0));
  3950. GiveBonus gbonus;
  3951. gbonus.hid = h->id;
  3952. gbonus.bonus.duration = HeroBonus::ONE_BATTLE;
  3953. gbonus.bonus.source = HeroBonus::OBJECT;
  3954. gbonus.bonus.id = ID;
  3955. gbonus.bdescr << "\n" << VLC->generaltexth->arraytxt[ID];
  3956. gbonus.bonus.type = HeroBonus::MORALE;
  3957. gbonus.bonus.val = -2;
  3958. cb->giveHeroBonus(&gbonus);
  3959. textID = 42;
  3960. iw.components.push_back (Component (Component::MORALE, 0 , -2, 0));
  3961. }
  3962. break;
  3963. case 84: //Crypt
  3964. if (bc->resources.size() != 0)
  3965. textID = 121;
  3966. else
  3967. {
  3968. iw.components.push_back (Component (Component::MORALE, 0 , -1, 0));
  3969. GiveBonus gbonus;
  3970. gbonus.hid = h->id;
  3971. gbonus.bonus.duration = HeroBonus::ONE_BATTLE;
  3972. gbonus.bonus.source = HeroBonus::OBJECT;
  3973. gbonus.bonus.id = ID;
  3974. gbonus.bdescr << "\n" << VLC->generaltexth->arraytxt[ID];
  3975. gbonus.bonus.type = HeroBonus::MORALE;
  3976. gbonus.bonus.val = -1;
  3977. cb->giveHeroBonus(&gbonus);
  3978. textID = 120;
  3979. iw.components.push_back (Component (Component::MORALE, 0 , -1, 0));
  3980. }
  3981. break;
  3982. case 85: //shipwreck
  3983. if (bc->resources.size() != 0)
  3984. textID = 124;
  3985. else
  3986. {
  3987. textID = 123;
  3988. }
  3989. break;
  3990. }
  3991. //grant resources
  3992. for (int it = 0; it < bc->resources.size(); it++)
  3993. {
  3994. if (bc->resources[it] != 0)
  3995. {
  3996. iw.components.push_back (Component (Component::RESOURCE, it, bc->resources[it], 0));
  3997. loot << "%d %s";
  3998. loot.addReplacement (iw.components.back().val);
  3999. loot.addReplacement (MetaString::RES_NAMES, iw.components.back().subtype);
  4000. cb->giveResource (h->getOwner(), it, bc->resources[it]);
  4001. }
  4002. }
  4003. //grant artifacts
  4004. for (std::vector<ui32>::const_iterator it = artifacts.begin(); it != artifacts.end(); it++)
  4005. {
  4006. iw.components.push_back (Component (Component::ARTIFACT, *it, 0, 0));
  4007. loot << "%s";
  4008. loot.addReplacement (MetaString::ART_NAMES, *it);
  4009. cb->giveHeroArtifact (*it, h->id ,-2);
  4010. }
  4011. //display loot
  4012. if (!loot.message.empty())
  4013. {
  4014. if (textID == 34)
  4015. {
  4016. iw.text.addTxt(MetaString::ADVOB_TXT, 34);//Heaving defeated %s, you discover %s
  4017. iw.text.addReplacement (MetaString::CRE_PL_NAMES, result->casualties[1].begin()->first);
  4018. iw.text.addReplacement (loot.buildList());
  4019. }
  4020. else
  4021. iw.text.addTxt (MetaString::ADVOB_TXT, textID);
  4022. cb->showInfoDialog(&iw);
  4023. }
  4024. loot.clear();
  4025. iw.components.clear();
  4026. iw.text.clear();
  4027. //grant creatures
  4028. CCreatureSet ourArmy;
  4029. for (std::vector< std::pair <ui16, ui32> >::const_iterator it = bc->creatures.begin(); it != bc->creatures.end(); it++)
  4030. {
  4031. int slot = ourArmy.getSlotFor (it->first);
  4032. ourArmy.slots[slot].first = it->first;
  4033. ourArmy.slots[slot].second += it->second;
  4034. }
  4035. for (std::map<si32,std::pair<ui32,si32> >::const_iterator i = ourArmy.slots.begin(); i != ourArmy.slots.end(); i++)
  4036. {
  4037. iw.components.push_back (Component(Component::CREATURE, i->second.first, i->second.second, 0));
  4038. loot << "%s";
  4039. if (i->second.second == 1)
  4040. loot.addReplacement (MetaString::CRE_SING_NAMES, i->second.first);
  4041. else
  4042. loot.addReplacement (MetaString::CRE_PL_NAMES, i->second.first);
  4043. }
  4044. if (ourArmy.slots.size())
  4045. {
  4046. if (ourArmy.slots.size() == 1 && ourArmy.slots.begin()->second.second == 1)
  4047. iw.text.addTxt (MetaString::ADVOB_TXT, 185);
  4048. else
  4049. iw.text.addTxt (MetaString::ADVOB_TXT, 186);
  4050. iw.text.addReplacement (loot.buildList());
  4051. iw.text.addReplacement (h->name);
  4052. cb->showInfoDialog(&iw);
  4053. cb->giveCreatures (id, h, ourArmy);
  4054. }
  4055. cb->setObjProperty (id, 15, 0); //bc = NULL
  4056. }
  4057. else //in case of defeat
  4058. initialize();
  4059. }
  4060. void CGPyramid::initObj()
  4061. {
  4062. //would be nice to do that only once
  4063. if (!pyramidConfig.guards.size())
  4064. {
  4065. pyramidConfig.level = 1;
  4066. pyramidConfig.chance = 100;
  4067. pyramidConfig.upgradeChance = 0;
  4068. for (int i = 0; i < 2; ++i)
  4069. {
  4070. pyramidConfig.guards.push_back (std::pair <ui16, ui32>(116, 20));
  4071. pyramidConfig.guards.push_back (std::pair <ui16, ui32>(117, 10));
  4072. }
  4073. pyramidConfig.combatValue; //how hard are guards of this level
  4074. pyramidConfig.value; //overall value of given things
  4075. pyramidConfig.rewardDifficulty; //proportion of reward value to difficulty of guards; how profitable is this creature Bank config
  4076. pyramidConfig.easiest; //?!?
  4077. }
  4078. bc = &pyramidConfig;
  4079. std::vector<ui16> available;
  4080. cb->getAllowedSpells (available, 5);
  4081. spell = (available[rand()%available.size()]);
  4082. }
  4083. const std::string & CGPyramid::getHoverText() const
  4084. {
  4085. hoverName = VLC->generaltexth->names[ID];
  4086. if (bc == NULL)
  4087. hoverName += " " + VLC->generaltexth->allTexts[352];
  4088. else
  4089. hoverName += " " + VLC->generaltexth->allTexts[353];
  4090. return hoverName;
  4091. }
  4092. void CGPyramid::onHeroVisit (const CGHeroInstance * h) const
  4093. {
  4094. if (bc)
  4095. {
  4096. BlockingDialog bd (true, false);
  4097. bd.player = h->getOwner();
  4098. bd.soundID = soundBase::DANGER;
  4099. bd.text << VLC->generaltexth->advobtxt[105];
  4100. cb->showBlockingDialog (&bd, boost::bind (&CBank::fightGuards, this, h, _1));
  4101. }
  4102. else
  4103. {
  4104. InfoWindow iw;
  4105. iw.player = h->getOwner();
  4106. iw.text << VLC->generaltexth->advobtxt[107];
  4107. iw.components.push_back (Component (Component::LUCK, 0 , -2, 0));
  4108. GiveBonus gb;
  4109. gb.bonus = HeroBonus(HeroBonus::ONE_BATTLE,HeroBonus::LUCK,HeroBonus::OBJECT,-2,id,VLC->generaltexth->arraytxt[70]);
  4110. gb.hid = h->id;
  4111. cb->giveHeroBonus(&gb);
  4112. cb->showInfoDialog(&iw);
  4113. }
  4114. }
  4115. void CGPyramid::endBattle (const CGHeroInstance *h, const BattleResult *result) const
  4116. {
  4117. if (result->winner == 0)
  4118. {
  4119. InfoWindow iw;
  4120. iw.player = h->getOwner();
  4121. iw.text.addTxt (MetaString::ADVOB_TXT, 106);
  4122. iw.text.addTxt (MetaString::SPELL_NAME, spell);
  4123. if (!h->getArt(17))
  4124. iw.text.addTxt (MetaString::ADVOB_TXT, 109); //no spellbook
  4125. else if (h->getSecSkillLevel(7) < 3)
  4126. iw.text.addTxt (MetaString::ADVOB_TXT, 108); //no expert Wisdom
  4127. else
  4128. {
  4129. std::set<ui32> spells;
  4130. spells.insert (spell);
  4131. cb->changeSpells (h->id, true, spells);
  4132. iw.components.push_back(Component (Component::SPELL, spell, 0, 0));
  4133. }
  4134. cb->showInfoDialog(&iw);
  4135. cb->setObjProperty (id, 15, 0);
  4136. }
  4137. }
  4138. void CGKeys::setPropertyDer (ui8 what, ui32 val) //101-108 - enable key for player 1-8
  4139. {
  4140. if (what >= 101 && what <= (100 + PLAYER_LIMIT))
  4141. playerKeyMap.find(what-101)->second.insert(val);
  4142. }
  4143. bool CGKeys::wasMyColorVisited (int player) const
  4144. {
  4145. if (vstd::contains(playerKeyMap[player], subID)) //creates set if it's not there
  4146. return true;
  4147. else
  4148. return false;
  4149. }
  4150. const std::string & CGKeymasterTent::getHoverText() const
  4151. {
  4152. hoverName = VLC->generaltexth->names[ID];
  4153. if (wasMyColorVisited (cb->getCurrentPlayer()) )//TODO: use local player, not current
  4154. hoverName += "\n" + VLC->generaltexth->allTexts[352];
  4155. else
  4156. hoverName += "\n" + VLC->generaltexth->allTexts[353];
  4157. return hoverName;
  4158. }
  4159. void CGKeymasterTent::onHeroVisit( const CGHeroInstance * h ) const
  4160. {
  4161. InfoWindow iw;
  4162. iw.soundID = soundBase::CAVEHEAD;
  4163. iw.player = h->getOwner();
  4164. if (!wasMyColorVisited (h->getOwner()) )
  4165. {
  4166. cb->setObjProperty(id, h->tempOwner+101, subID);
  4167. iw.text << std::pair<ui8,ui32>(11,19);
  4168. }
  4169. else
  4170. iw.text << std::pair<ui8,ui32>(11,20);
  4171. cb->showInfoDialog(&iw);
  4172. }
  4173. void CGBorderGuard::initObj()
  4174. {
  4175. blockVisit = true;
  4176. }
  4177. const std::string & CGBorderGuard::getHoverText() const
  4178. {
  4179. hoverName = VLC->generaltexth->names[ID];
  4180. if (wasMyColorVisited (cb->getCurrentPlayer()) )//TODO: use local player, not current
  4181. hoverName += "\n" + VLC->generaltexth->allTexts[352];
  4182. else
  4183. hoverName += "\n" + VLC->generaltexth->allTexts[353];
  4184. return hoverName;
  4185. }
  4186. void CGBorderGuard::onHeroVisit( const CGHeroInstance * h ) const
  4187. {
  4188. if (wasMyColorVisited (h->getOwner()) )
  4189. {
  4190. BlockingDialog bd (true, false);
  4191. bd.player = h->getOwner();
  4192. bd.soundID = soundBase::QUEST;
  4193. bd.text.addTxt (MetaString::ADVOB_TXT, 17);
  4194. cb->showBlockingDialog (&bd, boost::bind (&CGBorderGuard::openGate, this, h, _1));
  4195. }
  4196. else
  4197. {
  4198. InfoWindow iw;
  4199. iw.player = h->getOwner();
  4200. iw.soundID = soundBase::CAVEHEAD;
  4201. iw.text << std::pair<ui8,ui32>(11,18);
  4202. cb->showInfoDialog (&iw);
  4203. }
  4204. }
  4205. void CGBorderGuard::openGate(const CGHeroInstance *h, ui32 accept) const
  4206. {
  4207. if (accept)
  4208. cb->removeObject(id);
  4209. }
  4210. void CGBorderGate::onHeroVisit( const CGHeroInstance * h ) const //TODO: passability
  4211. {
  4212. if (!wasMyColorVisited (h->getOwner()) )
  4213. {
  4214. InfoWindow iw;
  4215. iw.player = h->getOwner();
  4216. iw.text << std::pair<ui8,ui32>(11,18);
  4217. cb->showInfoDialog(&iw);
  4218. }
  4219. }
  4220. ui8 CGBorderGate::getPassableness() const
  4221. {
  4222. ui8 ret = 0;
  4223. for (int i = 0; i < PLAYER_LIMIT; i++)
  4224. ret |= wasMyColorVisited(i)<<i;
  4225. return ret;
  4226. }
  4227. void CGMagi::initObj()
  4228. {
  4229. if (ID == 27)
  4230. {
  4231. blockVisit = true;
  4232. eyelist[subID].push_back(id);
  4233. }
  4234. }
  4235. void CGMagi::onHeroVisit(const CGHeroInstance * h) const
  4236. {
  4237. if (ID == 37)
  4238. {
  4239. InfoWindow iw;
  4240. CenterView cv;
  4241. FoWChange fw;
  4242. cv.player = iw.player = fw.player = h->tempOwner;
  4243. iw.soundID = soundBase::LIGHTHOUSE;
  4244. iw.player = h->tempOwner;
  4245. iw.text.addTxt (MetaString::ADVOB_TXT, 61);
  4246. cb->showInfoDialog(&iw);
  4247. fw.mode = 1;
  4248. std::vector<si32>::iterator it;
  4249. for (it = eyelist[subID].begin(); it < eyelist[subID].end(); it++)
  4250. {
  4251. const CGObjectInstance *eye = cb->getObj(*it);
  4252. cb->getTilesInRange (fw.tiles, eye->pos, 5, h->tempOwner, 1);
  4253. cb->sendAndApply(&fw);
  4254. cv.pos = eye->pos;
  4255. cv.focusTime = 2000;
  4256. cb->sendAndApply(&cv);
  4257. }
  4258. cv.pos = h->getPosition(false);
  4259. cb->sendAndApply(&cv);
  4260. }
  4261. else if (ID == 27)
  4262. {
  4263. InfoWindow iw;
  4264. iw.player = h->tempOwner;
  4265. iw.text.addTxt (MetaString::ADVOB_TXT, 48);
  4266. cb->showInfoDialog(&iw);
  4267. }
  4268. }
  4269. void CGBoat::initObj()
  4270. {
  4271. hero = NULL;
  4272. }
  4273. void CGSirens::initObj()
  4274. {
  4275. blockVisit = true;
  4276. }
  4277. const std::string & CGSirens::getHoverText() const
  4278. {
  4279. getNameVis(hoverName);
  4280. return hoverName;
  4281. }
  4282. void CGSirens::onHeroVisit( const CGHeroInstance * h ) const
  4283. {
  4284. int message;
  4285. InfoWindow iw;
  4286. iw.soundID = soundBase::DANGER;
  4287. iw.player = h->tempOwner;
  4288. if(h->getBonus(HeroBonus::OBJECT,ID)) //has already visited Sirens
  4289. {
  4290. iw.text.addTxt(11,133);
  4291. }
  4292. else
  4293. {
  4294. giveDummyBonus(h->id, HeroBonus::ONE_BATTLE);
  4295. int xp = 0;
  4296. SetGarrisons sg;
  4297. sg.garrs[h->id] = h->army;
  4298. for (std::map<si32,std::pair<ui32,si32> >::const_iterator i = h->army.slots.begin(); i != h->army.slots.end(); i++)
  4299. {
  4300. int drown = (int)(i->second.second * 0.3);
  4301. if(drown)
  4302. {
  4303. sg.garrs[h->id].slots[i->first].second -= drown;
  4304. xp += drown * VLC->creh->creatures[i->second.first].hitPoints;
  4305. }
  4306. }
  4307. if(xp)
  4308. {
  4309. iw.text.addTxt(11,132);
  4310. iw.text.addReplacement(xp);
  4311. cb->sendAndApply(&sg);
  4312. }
  4313. else
  4314. {
  4315. iw.text.addTxt(11,134);
  4316. }
  4317. }
  4318. cb->showInfoDialog(&iw);
  4319. }
  4320. void IShipyard::getBoatCost( std::vector<si32> &cost ) const
  4321. {
  4322. cost.resize(RESOURCE_QUANTITY);
  4323. cost[0] = 10;
  4324. cost[6] = 1000;
  4325. }
  4326. //bool IShipyard::validLocation() const
  4327. //{
  4328. // std::vector<int3> offsets;
  4329. // getOutOffsets(offsets);
  4330. //
  4331. // TerrainTile *tile;
  4332. // for(int i = 0; i < offsets.size(); i++)
  4333. // if((tile = IObjectInterface::cb->getTile(o->pos + offsets[i])) && tile->tertype == TerrainTile::water) //tile is in the map and is water
  4334. // return true;
  4335. // return false;
  4336. //}
  4337. int3 IShipyard::bestLocation() const
  4338. {
  4339. std::vector<int3> offsets;
  4340. getOutOffsets(offsets);
  4341. TerrainTile *tile;
  4342. for(int i = 0; i < offsets.size(); i++)
  4343. if((tile = IObjectInterface::cb->getTile(o->pos + offsets[i])) && tile->tertype == TerrainTile::water) //tile is in the map and is water
  4344. return o->pos + offsets[i];
  4345. return int3(-1,-1,-1);
  4346. }
  4347. IShipyard::IShipyard(const CGObjectInstance *O)
  4348. : o(O)
  4349. {
  4350. }
  4351. int IShipyard::state() const
  4352. {
  4353. int3 tile = bestLocation();
  4354. TerrainTile *t = IObjectInterface::cb->getTile(tile);
  4355. if(!t)
  4356. return 3; //no water
  4357. else if(!t->blockingObjects.size())
  4358. return 0; //OK
  4359. else if(t->blockingObjects.front()->ID == 8)
  4360. return 1; //blocked with boat
  4361. else
  4362. return 2; //blocked
  4363. }
  4364. IShipyard * IShipyard::castFrom( CGObjectInstance *obj )
  4365. {
  4366. if(obj->ID == TOWNI_TYPE)
  4367. {
  4368. return static_cast<CGTownInstance*>(obj);
  4369. }
  4370. else if(obj->ID == 87)
  4371. {
  4372. return static_cast<CGShipyard*>(obj);
  4373. }
  4374. else
  4375. {
  4376. tlog1 << "Cannot cast to IShipyard object with ID " << obj->ID << std::endl;
  4377. return NULL;
  4378. }
  4379. }
  4380. const IShipyard * IShipyard::castFrom( const CGObjectInstance *obj )
  4381. {
  4382. return castFrom(const_cast<CGObjectInstance*>(obj));
  4383. }
  4384. CGShipyard::CGShipyard()
  4385. :IShipyard(this)
  4386. {
  4387. }
  4388. void CGShipyard::getOutOffsets( std::vector<int3> &offsets ) const
  4389. {
  4390. offsets += int3(1,0,0), int3(-3,0,0), int3(1,1,0), int3(-3,1,0), int3(1,-1,0), int3(-3,-1,0),
  4391. int3(-2,-1,0), int3(0,-1,0), int3(-1,-1,0), int3(-2,1,0), int3(0,1,0), int3(-1,1,0);
  4392. }
  4393. void CGShipyard::onHeroVisit( const CGHeroInstance * h ) const
  4394. {
  4395. if(tempOwner != h->tempOwner)
  4396. cb->setOwner(id, h->tempOwner);
  4397. int s = state();
  4398. if(s)
  4399. {
  4400. InfoWindow iw;
  4401. iw.player = tempOwner;
  4402. switch(s)
  4403. {
  4404. case 1:
  4405. iw.text.addTxt(MetaString::GENERAL_TXT, 51);
  4406. break;
  4407. case 2:
  4408. iw.text.addTxt(MetaString::ADVOB_TXT, 189);
  4409. break;
  4410. case 3:
  4411. tlog1 << "Shipyard without water!!! " << pos << "\t" << id << std::endl;
  4412. return;
  4413. }
  4414. cb->showInfoDialog(&iw);
  4415. }
  4416. else
  4417. {
  4418. OpenWindow ow;
  4419. ow.id1 = id;
  4420. ow.id2 = h->id;
  4421. ow.window = OpenWindow::SHIPYARD_WINDOW;
  4422. cb->sendAndApply(&ow);
  4423. }
  4424. }
  4425. void CCartographer::onHeroVisit( const CGHeroInstance * h ) const
  4426. {
  4427. if (!hasVisited (h->getOwner()) ) //if hero has not visited yet this cartographer
  4428. {
  4429. if (cb->getResource(h->tempOwner, 6) >= 1000) //if he can afford a map
  4430. {
  4431. //ask if he wants to buy one
  4432. int text;
  4433. switch (subID)
  4434. {
  4435. case 0:
  4436. text = 25;
  4437. break;
  4438. case 1:
  4439. text = 26;
  4440. break;
  4441. case 2:
  4442. text = 27;
  4443. break;
  4444. default:
  4445. tlog2 << "Unrecognized subtype of cartographer" << std::endl;
  4446. }
  4447. BlockingDialog bd (true, false);
  4448. bd.player = h->getOwner();
  4449. bd.soundID = soundBase::LIGHTHOUSE;
  4450. bd.text.addTxt (MetaString::ADVOB_TXT, text);
  4451. cb->showBlockingDialog (&bd, boost::bind (&CCartographer::buyMap, this, h, _1));
  4452. }
  4453. else //if he cannot afford
  4454. {
  4455. InfoWindow iw;
  4456. iw.player = h->getOwner();
  4457. iw.soundID = soundBase::CAVEHEAD;
  4458. iw.text << std::pair<ui8,ui32>(11,28);
  4459. cb->showInfoDialog (&iw);
  4460. }
  4461. }
  4462. else //if he already visited carographer
  4463. {
  4464. InfoWindow iw;
  4465. iw.player = h->getOwner();
  4466. iw.soundID = soundBase::CAVEHEAD;
  4467. iw.text << std::pair<ui8,ui32>(11,24);
  4468. cb->showInfoDialog (&iw);
  4469. }
  4470. }
  4471. void CCartographer::buyMap (const CGHeroInstance *h, ui32 accept) const
  4472. {
  4473. if (accept) //if hero wants to buy map
  4474. {
  4475. cb->giveResource (h->tempOwner, 6, -1000);
  4476. FoWChange fw;
  4477. fw.player = h->tempOwner;
  4478. //subIDs of different types of cartographers:
  4479. //water = 0; land = 1; underground = 2;
  4480. cb->getAllTiles (fw.tiles, h->tempOwner, subID - 1, !subID + 1); //reveal appropriate tiles
  4481. cb->sendAndApply (&fw);
  4482. cb->setObjProperty (id, 10, h->tempOwner);
  4483. }
  4484. }
  4485. void CShop::newTurn() const
  4486. {
  4487. switch (ID)
  4488. {
  4489. case 7: //ArtMerchant aka. Black Market
  4490. if (cb->getDate(0)%28 == 1)
  4491. {
  4492. cb->setObjProperty (id, 13, 0);
  4493. cb->setObjProperty (id, 14, rand());
  4494. }
  4495. break;
  4496. case 78: //Refugee Camp
  4497. case 95: //Tavern -- global hero pool?
  4498. if (cb->getDate(0)%7 == 1)
  4499. cb->setObjProperty (id, 14, rand());
  4500. break;
  4501. }
  4502. }
  4503. void CShop::setPropertyDer (ui8 what, ui32 val)
  4504. {
  4505. switch (what)
  4506. {
  4507. case 13: //sweep
  4508. available.clear();
  4509. chosen.clear();
  4510. bought.clear();
  4511. break;
  4512. case 14: //reset - get new items
  4513. reset(val);
  4514. break;
  4515. }
  4516. }
  4517. void CGArtMerchant::reset(ui32 val)
  4518. {
  4519. std::vector<CArtifact*>::iterator index;
  4520. for (ui8 i = 0; i < 4; ++i) //each tier
  4521. {
  4522. int count = 0;
  4523. std::vector<CArtifact*> arts; //to avoid addition of different tiers
  4524. switch (i)
  4525. {
  4526. case 0:
  4527. cb->getAllowed (arts, CArtifact::ART_TREASURE);
  4528. count = 2;
  4529. break;
  4530. case 1:
  4531. cb->getAllowed (arts, CArtifact::ART_MINOR);
  4532. count = 2;
  4533. break;
  4534. case 2:
  4535. cb->getAllowed (arts, CArtifact::ART_MAJOR);
  4536. count = 1;
  4537. break;
  4538. case 3:
  4539. cb->getAllowed (arts, CArtifact::ART_RELIC);
  4540. count = 1;
  4541. break;
  4542. }
  4543. for (ui8 n = 0; n < count; n++)
  4544. {
  4545. index = arts.begin() + val % arts.size();
  4546. available [available.size()] = new Component (Component::ARTIFACT, (*index)->id, 0, 0);
  4547. arts.erase(index);
  4548. val *= (id + n * i); //randomize
  4549. }
  4550. }
  4551. }
  4552. void CGRefugeeCamp::reset(ui32 val)
  4553. {
  4554. /*int creid = creh->creatures[val%creh->creatures.size()].idNumber;
  4555. VLC->creh->creatures[creatures[creid].second[0]].growth;
  4556. available[0] = new Component (Component::CREATURE, creid, 0, 0);
  4557. */
  4558. }