CObjectHandler.cpp 124 KB

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