CObjectHandler.cpp 128 KB

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