CObjectHandler.cpp 131 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156
  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. bool CGHeroInstance::hasArt( ui32 aid ) const
  1109. {
  1110. if(vstd::contains(artifacts, aid))
  1111. return true;
  1112. for(std::map<ui16,ui32>::const_iterator i = artifWorn.begin(); i != artifWorn.end(); i++)
  1113. if(i->second == aid)
  1114. return true;
  1115. return false;
  1116. }
  1117. void CGDwelling::initObj()
  1118. {
  1119. switch(ID)
  1120. {
  1121. case 17:
  1122. {
  1123. int crid = VLC->objh->cregens[subID];
  1124. CCreature *crs = &VLC->creh->creatures[crid];
  1125. creatures.resize(1);
  1126. creatures[0].second.push_back(crid);
  1127. hoverName = VLC->generaltexth->creGens[subID];
  1128. if(crs->level > 4)
  1129. {
  1130. army.slots[0].first = crs->idNumber;
  1131. army.slots[0].second = (8 - crs->level) * 3;
  1132. }
  1133. if (getOwner() != 255)
  1134. cb->gameState()->players[getOwner()].dwellings.push_back (this);
  1135. }
  1136. break;
  1137. case 20:
  1138. creatures.resize(4);
  1139. if(subID == 1) //Golem Factory
  1140. {
  1141. creatures[0].second.push_back(32); //Stone Golem
  1142. creatures[1].second.push_back(33); //Iron Golem
  1143. creatures[2].second.push_back(116); //Gold Golem
  1144. creatures[3].second.push_back(117); //Diamond Golem
  1145. //guards
  1146. army.slots[0].first = 116;
  1147. army.slots[0].second = 9;
  1148. army.slots[1].first = 117;
  1149. army.slots[1].second = 6;
  1150. }
  1151. else if(subID == 0) // Elemental Conflux
  1152. {
  1153. creatures[0].second.push_back(112); //Air Elemental
  1154. creatures[1].second.push_back(114); //Fire Elemental
  1155. creatures[2].second.push_back(113); //Earth Elemental
  1156. creatures[3].second.push_back(115); //Water Elemental
  1157. //guards
  1158. army.slots[0].first = 113;
  1159. army.slots[0].second = 12;
  1160. }
  1161. else
  1162. {
  1163. assert(0);
  1164. }
  1165. hoverName = VLC->generaltexth->creGens4[subID];
  1166. break;
  1167. default:
  1168. assert(0);
  1169. break;
  1170. }
  1171. }
  1172. void CGDwelling::setProperty(ui8 what, ui32 val)
  1173. {
  1174. switch (what)
  1175. {
  1176. case 1: //change owner
  1177. if (ID == 17) //single generators
  1178. {
  1179. if (tempOwner != NEUTRAL_PLAYER)
  1180. {
  1181. std::vector<CGDwelling *>* dwellings = &cb->gameState()->players[tempOwner].dwellings;
  1182. dwellings->erase (std::find(dwellings->begin(), dwellings->end(), this));
  1183. }
  1184. if (val != NEUTRAL_PLAYER) //can new owner be neutral?
  1185. cb->gameState()->players[val].dwellings.push_back (this);
  1186. }
  1187. break;
  1188. }
  1189. CGObjectInstance::setProperty(what,val);
  1190. }
  1191. void CGDwelling::onHeroVisit( const CGHeroInstance * h ) const
  1192. {
  1193. if(h->tempOwner != tempOwner && army.slots.size() > 0) //object is guarded
  1194. {
  1195. BlockingDialog bd;
  1196. bd.player = h->tempOwner;
  1197. bd.flags = BlockingDialog::ALLOW_CANCEL;
  1198. 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?
  1199. bd.text.addReplacement(ID == 17 ? MetaString::CREGENS : MetaString::CREGENS4, subID);
  1200. bd.text.addReplacement(MetaString::ARRAY_TXT, 176 + CCreature::getQuantityID(army.slots.begin()->second.second)*3);
  1201. bd.text.addReplacement(MetaString::CRE_PL_NAMES, army.slots.begin()->second.first);
  1202. cb->showBlockingDialog(&bd, boost::bind(&CGDwelling::wantsFight, this, h, _1));
  1203. return;
  1204. }
  1205. if(h->tempOwner != tempOwner)
  1206. {
  1207. cb->setOwner(id, h->tempOwner);
  1208. }
  1209. BlockingDialog bd;
  1210. bd.player = h->tempOwner;
  1211. bd.flags = BlockingDialog::ALLOW_CANCEL;
  1212. bd.text.addTxt(MetaString::ADVOB_TXT, ID == 17 ? 35 : 36); //{%s} Would you like to recruit %s?
  1213. bd.text.addReplacement(ID == 17 ? MetaString::CREGENS : MetaString::CREGENS4, subID);
  1214. for(size_t i = 0; i < creatures.size(); i++)
  1215. bd.text.addReplacement(MetaString::CRE_PL_NAMES, creatures[i].second[0]);
  1216. cb->showBlockingDialog(&bd, boost::bind(&CGDwelling::heroAcceptsCreatures, this, h, _1));
  1217. }
  1218. void CGDwelling::newTurn() const
  1219. {
  1220. if(cb->getDate(1) != 1) //not first day of week
  1221. return;
  1222. //town growths are handled separately
  1223. if(ID == TOWNI_TYPE)
  1224. return;
  1225. bool change = false;
  1226. SetAvailableCreatures sac;
  1227. sac.creatures = creatures;
  1228. sac.tid = id;
  1229. for (size_t i = 0; i < creatures.size(); i++)
  1230. {
  1231. if(creatures[i].second.size())
  1232. {
  1233. if(false /*accumulate creatures*/)
  1234. sac.creatures[i].first += VLC->creh->creatures[creatures[i].second[0]].growth;
  1235. else
  1236. sac.creatures[i].first = VLC->creh->creatures[creatures[i].second[0]].growth;
  1237. change = true;
  1238. }
  1239. }
  1240. if(change)
  1241. cb->sendAndApply(&sac);
  1242. }
  1243. void CGDwelling::heroAcceptsCreatures( const CGHeroInstance *h, ui32 answer ) const
  1244. {
  1245. if(!answer)
  1246. return;
  1247. int crid = creatures[0].second[0];
  1248. CCreature *crs = &VLC->creh->creatures[crid];
  1249. if(crs->level == 1) //first level - creatures are for free
  1250. {
  1251. if(creatures[0].first) //there are available creatures
  1252. {
  1253. int slot = h->army.getSlotFor(crid);
  1254. if(slot < 0) //no available slot
  1255. {
  1256. InfoWindow iw;
  1257. iw.player = h->tempOwner;
  1258. iw.text.addTxt(MetaString::GENERAL_TXT, 425);//The %s would join your hero, but there aren't enough provisions to support them.
  1259. iw.text.addReplacement(MetaString::CRE_PL_NAMES, crid);
  1260. cb->showInfoDialog(&iw);
  1261. }
  1262. else //give creatures
  1263. {
  1264. SetAvailableCreatures sac;
  1265. sac.tid = id;
  1266. sac.creatures = creatures;
  1267. sac.creatures[0].first = 0;
  1268. SetGarrisons sg;
  1269. sg.garrs[h->id] = h->army;
  1270. sg.garrs[h->id].slots[slot].first = crid;
  1271. sg.garrs[h->id].slots[slot].second += creatures[0].first;
  1272. InfoWindow iw;
  1273. iw.player = h->tempOwner;
  1274. iw.text.addTxt(MetaString::GENERAL_TXT, 423); //%d %s join your army.
  1275. iw.text.addReplacement(creatures[0].first);
  1276. iw.text.addReplacement(MetaString::CRE_PL_NAMES, crid);
  1277. cb->showInfoDialog(&iw);
  1278. cb->sendAndApply(&sac);
  1279. cb->sendAndApply(&sg);
  1280. }
  1281. }
  1282. else //there no creatures
  1283. {
  1284. InfoWindow iw;
  1285. iw.text.addTxt(MetaString::GENERAL_TXT, 422); //There are no %s here to recruit.
  1286. iw.text.addReplacement(MetaString::CRE_PL_NAMES, crid);
  1287. iw.player = h->tempOwner;
  1288. cb->sendAndApply(&iw);
  1289. }
  1290. }
  1291. else
  1292. {
  1293. OpenWindow ow;
  1294. ow.id1 = id;
  1295. ow.id2 = h->id;
  1296. ow.window = ID == 17
  1297. ? OpenWindow::RECRUITMENT_FIRST
  1298. : OpenWindow::RECRUITMENT_ALL;
  1299. cb->sendAndApply(&ow);
  1300. }
  1301. }
  1302. void CGDwelling::wantsFight( const CGHeroInstance *h, ui32 answer ) const
  1303. {
  1304. if(answer)
  1305. cb->startBattleI(h, this, boost::bind(&CGDwelling::fightOver, this, h, _1));
  1306. }
  1307. void CGDwelling::fightOver(const CGHeroInstance *h, BattleResult *result) const
  1308. {
  1309. if (result->winner == 0)
  1310. {
  1311. onHeroVisit(h);
  1312. }
  1313. }
  1314. int CGTownInstance::getSightRadious() const //returns sight distance
  1315. {
  1316. if (subID == 2) //tower
  1317. {
  1318. if ((builtBuildings.find(17)) != builtBuildings.end()) //skyship
  1319. return -1; //entire map
  1320. else if ((builtBuildings.find(21)) != builtBuildings.end()) //lookout tower
  1321. return 20;
  1322. }
  1323. return 5;
  1324. }
  1325. void CGTownInstance::setPropertyDer(ui8 what, ui32 val)
  1326. {
  1327. ///this is freakin' overcomplicated solution
  1328. switch (what)
  1329. {
  1330. case 11: //add visitor of town building
  1331. bonusingBuildings[val]->setProperty (4, visitingHero->id);
  1332. break;
  1333. case 12:
  1334. bonusingBuildings[val]->setProperty (12, 0);
  1335. break;
  1336. case 13: //add garrisoned hero to visitors
  1337. bonusingBuildings[val]->setProperty (4, garrisonHero->id);
  1338. break;
  1339. case 14:
  1340. bonusValue.first = val;
  1341. break;
  1342. case 15:
  1343. bonusValue.second = val;
  1344. break;
  1345. }
  1346. }
  1347. int CGTownInstance::fortLevel() const //0 - none, 1 - fort, 2 - citadel, 3 - castle
  1348. {
  1349. if((builtBuildings.find(9))!=builtBuildings.end())
  1350. return 3;
  1351. if((builtBuildings.find(8))!=builtBuildings.end())
  1352. return 2;
  1353. if((builtBuildings.find(7))!=builtBuildings.end())
  1354. return 1;
  1355. return 0;
  1356. }
  1357. int CGTownInstance::hallLevel() const // -1 - none, 0 - village, 1 - town, 2 - city, 3 - capitol
  1358. {
  1359. if ((builtBuildings.find(13))!=builtBuildings.end())
  1360. return 3;
  1361. if ((builtBuildings.find(12))!=builtBuildings.end())
  1362. return 2;
  1363. if ((builtBuildings.find(11))!=builtBuildings.end())
  1364. return 1;
  1365. if ((builtBuildings.find(10))!=builtBuildings.end())
  1366. return 0;
  1367. return -1;
  1368. }
  1369. int CGTownInstance::mageGuildLevel() const
  1370. {
  1371. if ((builtBuildings.find(4))!=builtBuildings.end())
  1372. return 5;
  1373. if ((builtBuildings.find(3))!=builtBuildings.end())
  1374. return 4;
  1375. if ((builtBuildings.find(2))!=builtBuildings.end())
  1376. return 3;
  1377. if ((builtBuildings.find(1))!=builtBuildings.end())
  1378. return 2;
  1379. if ((builtBuildings.find(0))!=builtBuildings.end())
  1380. return 1;
  1381. return 0;
  1382. }
  1383. bool CGTownInstance::creatureDwelling(const int & level, bool upgraded) const
  1384. {
  1385. return builtBuildings.find(30+level+upgraded*7)!=builtBuildings.end();
  1386. }
  1387. int CGTownInstance::getHordeLevel(const int & HID) const//HID - 0 or 1; returns creature level or -1 if that horde structure is not present
  1388. {
  1389. return town->hordeLvl[HID];
  1390. }
  1391. int CGTownInstance::creatureGrowth(const int & level) const
  1392. {
  1393. int ret = VLC->creh->creatures[town->basicCreatures[level]].growth;
  1394. switch(fortLevel())
  1395. {
  1396. case 3:
  1397. ret*=2;break;
  1398. case 2:
  1399. ret*=(1.5); break;
  1400. }
  1401. if(getHordeLevel(0)==level)
  1402. if((builtBuildings.find(18)!=builtBuildings.end()) || (builtBuildings.find(19)!=builtBuildings.end()))
  1403. ret+=VLC->creh->creatures[town->basicCreatures[level]].hordeGrowth;
  1404. if(getHordeLevel(1)==level)
  1405. if((builtBuildings.find(24)!=builtBuildings.end()) || (builtBuildings.find(25)!=builtBuildings.end()))
  1406. ret+=VLC->creh->creatures[town->basicCreatures[level]].hordeGrowth;
  1407. //support for legs of legion etc.
  1408. if(garrisonHero)
  1409. ret += garrisonHero->valOfBonuses(HeroBonus::CREATURE_GROWTH, level);
  1410. if(visitingHero)
  1411. ret += visitingHero->valOfBonuses(HeroBonus::CREATURE_GROWTH, level);
  1412. for (std::vector<CGDwelling*>::const_iterator it = cb->gameState()->players[tempOwner].dwellings.begin(); it != cb->gameState()->players[tempOwner].dwellings.end(); ++it)
  1413. { //+1 for each dwelling
  1414. if (VLC->creh->creatures[town->basicCreatures[level]].idNumber == (*it)->creatures[0].second[0])
  1415. ++ret;
  1416. }
  1417. if(builtBuildings.find(26)!=builtBuildings.end()) //grail - +50% to ALL growth
  1418. ret*=1.5;
  1419. return ret;//check CCastleInterface.cpp->CCastleInterface::CCreaInfo::clickRight if this one will be modified
  1420. }
  1421. int CGTownInstance::dailyIncome() const
  1422. {
  1423. int ret = 0;
  1424. if ((builtBuildings.find(26))!=builtBuildings.end())
  1425. ret+=5000;
  1426. if ((builtBuildings.find(13))!=builtBuildings.end())
  1427. ret+=4000;
  1428. else if ((builtBuildings.find(12))!=builtBuildings.end())
  1429. ret+=2000;
  1430. else if ((builtBuildings.find(11))!=builtBuildings.end())
  1431. ret+=1000;
  1432. else if ((builtBuildings.find(10))!=builtBuildings.end())
  1433. ret+=500;
  1434. return ret;
  1435. }
  1436. bool CGTownInstance::hasFort() const
  1437. {
  1438. return (builtBuildings.find(7))!=builtBuildings.end();
  1439. }
  1440. bool CGTownInstance::hasCapitol() const
  1441. {
  1442. return (builtBuildings.find(13))!=builtBuildings.end();
  1443. }
  1444. CGTownInstance::CGTownInstance()
  1445. :IShipyard(this)
  1446. {
  1447. builded=-1;
  1448. destroyed=-1;
  1449. garrisonHero=NULL;
  1450. town=NULL;
  1451. visitingHero = NULL;
  1452. }
  1453. CGTownInstance::~CGTownInstance()
  1454. {
  1455. for (std::vector<CGTownBuilding*>::const_iterator i = bonusingBuildings.begin(); i != bonusingBuildings.end(); i++)
  1456. delete *i;
  1457. }
  1458. int CGTownInstance::spellsAtLevel(int level, bool checkGuild) const
  1459. {
  1460. if(checkGuild && mageGuildLevel() < level)
  1461. return 0;
  1462. int ret = 6 - level; //how many spells are available at this level
  1463. if(subID == 2 && vstd::contains(builtBuildings,22)) //magic library in Tower
  1464. ret++;
  1465. return ret;
  1466. }
  1467. bool CGTownInstance::needsLastStack() const
  1468. {
  1469. if(garrisonHero)
  1470. return true;
  1471. else return false;
  1472. }
  1473. void CGTownInstance::onHeroVisit(const CGHeroInstance * h) const
  1474. {
  1475. if(getOwner() != h->getOwner())
  1476. {
  1477. //TODO ally check
  1478. if(army.slots.size() > 0 || visitingHero)
  1479. {
  1480. const CGHeroInstance *defendingHero = NULL;
  1481. if(visitingHero)
  1482. defendingHero = visitingHero;
  1483. else if(garrisonHero)
  1484. defendingHero = garrisonHero;
  1485. const CArmedInstance *defendingArmy = this;
  1486. if(defendingHero)
  1487. defendingArmy = defendingHero;
  1488. bool outsideTown = (defendingHero == visitingHero && garrisonHero);
  1489. cb->startBattleI(h, defendingArmy, getSightCenter(), h, defendingHero, false, boost::bind(&CGTownInstance::fightOver, this, h, _1), (outsideTown ? NULL : this));
  1490. }
  1491. else
  1492. {
  1493. cb->setOwner(id, h->tempOwner);
  1494. removeCapitols (h->getOwner(), true);
  1495. cb->heroVisitCastle(id, h->id);
  1496. }
  1497. }
  1498. else
  1499. cb->heroVisitCastle(id, h->id);
  1500. }
  1501. void CGTownInstance::onHeroLeave(const CGHeroInstance * h) const
  1502. {
  1503. cb->stopHeroVisitCastle(id,h->id);
  1504. }
  1505. void CGTownInstance::initObj()
  1506. {
  1507. blockVisit = true;
  1508. hoverName = name + ", " + town->Name();
  1509. creatures.resize(CREATURES_PER_TOWN);
  1510. for (int i = 0; i < CREATURES_PER_TOWN; i++)
  1511. {
  1512. if(creatureDwelling(i,false))
  1513. creatures[i].second.push_back(town->basicCreatures[i]);
  1514. if(creatureDwelling(i,true))
  1515. creatures[i].second.push_back(town->upgradedCreatures[i]);
  1516. }
  1517. switch (subID)
  1518. { //add new visitable objects
  1519. case 0:
  1520. bonusingBuildings.push_back (new COPWBonus(21, this)); //Stables
  1521. break;
  1522. case 2: case 3: case 5: case 6:
  1523. bonusingBuildings.push_back (new CTownBonus(23, this));
  1524. if (subID == 5)
  1525. bonusingBuildings.push_back (new COPWBonus(21, this)); //Vortex
  1526. break;
  1527. case 7:
  1528. bonusingBuildings.push_back (new CTownBonus(17, this));
  1529. break;
  1530. }
  1531. if (getOwner() != 255)
  1532. removeCapitols (getOwner(), false); // destroy other capitols
  1533. }
  1534. void CGTownInstance::newTurn() const
  1535. {
  1536. if (cb->getDate(1) == 1) //reset on new week
  1537. {
  1538. if (vstd::contains(builtBuildings,17) && subID == 1 && cb->getDate(0) && (tempOwner < PLAYER_LIMIT) )//give resources for Rampart, Mystic Pond
  1539. {
  1540. int resID = rand()%4+2;//bonus to random rare resource
  1541. resID = (resID==2)?1:resID;
  1542. int resVal = rand()%4+1;//with size 1..4
  1543. cb->giveResource(tempOwner, resID, resVal);
  1544. cb->setObjProperty (id, 14, resID);
  1545. cb->setObjProperty (id, 15, resVal);
  1546. }
  1547. if ( subID == 5 )
  1548. for (std::vector<CGTownBuilding*>::const_iterator i = bonusingBuildings.begin(); i!=bonusingBuildings.end(); i++)
  1549. {
  1550. if ((*i)->ID == 21)
  1551. cb->setObjProperty (id, 12, (*i)->id); //reset visitors for Mana Vortex
  1552. }
  1553. }
  1554. }
  1555. int3 CGTownInstance::getSightCenter() const
  1556. {
  1557. return pos - int3(2,0,0);
  1558. }
  1559. void CGTownInstance::getOutOffsets( std::vector<int3> &offsets ) const
  1560. {
  1561. offsets += int3(-1,2,0), int3(-3,2,0);
  1562. }
  1563. void CGTownInstance::fightOver( const CGHeroInstance *h, BattleResult *result ) const
  1564. {
  1565. if(result->winner == 0)
  1566. {
  1567. removeCapitols (h->getOwner(), true);
  1568. cb->setOwner (id, h->tempOwner); //give control after checkout is done
  1569. }
  1570. }
  1571. void CGTownInstance::removeCapitols (ui8 owner, bool me) const
  1572. {
  1573. if (hasCapitol()) // search for older capitol
  1574. {
  1575. PlayerState* state = cb->gameState()->getPlayer (owner); //get all towns owned by player
  1576. for (std::vector<CGTownInstance*>::const_iterator i = state->towns.begin(); i < state->towns.end(); ++i)
  1577. {
  1578. if (*i != this && (*i)->hasCapitol())
  1579. {
  1580. if (me)
  1581. {
  1582. RazeStructures rs;
  1583. rs.tid = id;
  1584. rs.bid.insert(13);
  1585. rs.destroyed = destroyed;
  1586. cb->sendAndApply(&rs);
  1587. return;
  1588. }
  1589. else
  1590. (*i)->builtBuildings.erase(13); //destroy all other capitols at the beginning of game
  1591. }
  1592. }
  1593. }
  1594. }
  1595. ui8 CGTownInstance::getPassableness() const
  1596. {
  1597. return army ? 1<<tempOwner : ALL_PLAYERS; //if there is garrison, castle be entered only by owner //TODO: allies
  1598. }
  1599. void CGVisitableOPH::onHeroVisit( const CGHeroInstance * h ) const
  1600. {
  1601. if(visitors.find(h->id)==visitors.end())
  1602. {
  1603. onNAHeroVisit(h->id, false);
  1604. switch(ID)
  1605. {
  1606. case 102: //tree
  1607. case 4: //arena
  1608. case 41://library
  1609. case 47: //School of Magic
  1610. case 107://School of War
  1611. break;
  1612. default:
  1613. cb->setObjProperty(id,4,h->id); //add to the visitors
  1614. break;
  1615. }
  1616. }
  1617. else
  1618. {
  1619. onNAHeroVisit(h->id, true);
  1620. }
  1621. }
  1622. void CGVisitableOPH::initObj()
  1623. {
  1624. if(ID==102)
  1625. ttype = ran()%3;
  1626. else
  1627. ttype = -1;
  1628. }
  1629. void CGVisitableOPH::treeSelected( int heroID, int resType, int resVal, ui64 expVal, ui32 result ) const
  1630. {
  1631. if(result) //player agreed to give res for exp
  1632. {
  1633. cb->giveResource(cb->getOwner(heroID),resType,-resVal); //take resource
  1634. cb->changePrimSkill(heroID,4,expVal); //give exp
  1635. cb->setObjProperty(id,4,heroID); //add to the visitors
  1636. }
  1637. }
  1638. void CGVisitableOPH::onNAHeroVisit(int heroID, bool alreadyVisited) const
  1639. {
  1640. int id=0, subid=0, ot=0, val=1, sound = 0;
  1641. switch(ID)
  1642. {
  1643. case 4: //arena
  1644. sound = soundBase::NOMAD;
  1645. ot = 0;
  1646. break;
  1647. case 51: //mercenary camp
  1648. sound = soundBase::NOMAD;
  1649. subid=0;
  1650. ot=80;
  1651. break;
  1652. case 23: //marletto tower
  1653. sound = soundBase::NOMAD;
  1654. subid=1;
  1655. ot=39;
  1656. break;
  1657. case 61:
  1658. sound = soundBase::gazebo;
  1659. subid=2;
  1660. ot=100;
  1661. break;
  1662. case 32:
  1663. sound = soundBase::GETPROTECTION;
  1664. subid=3;
  1665. ot=59;
  1666. break;
  1667. case 100:
  1668. sound = soundBase::gazebo;
  1669. id=5;
  1670. ot=143;
  1671. val=1000;
  1672. break;
  1673. case 102:
  1674. sound = soundBase::gazebo;
  1675. id = 5;
  1676. subid = 1;
  1677. ot = 146;
  1678. val = 1;
  1679. break;
  1680. case 41:
  1681. sound = soundBase::gazebo;
  1682. ot = 66;
  1683. break;
  1684. case 47: //School of Magic
  1685. sound = soundBase::faerie;
  1686. ot = 71;
  1687. break;
  1688. case 107://School of War
  1689. sound = soundBase::MILITARY;
  1690. ot = 158;
  1691. break;
  1692. }
  1693. if (!alreadyVisited)
  1694. {
  1695. switch (ID)
  1696. {
  1697. case 4: //arena
  1698. {
  1699. BlockingDialog sd(false,true);
  1700. sd.soundID = sound;
  1701. sd.text << std::pair<ui8,ui32>(11,ot);
  1702. sd.components.push_back(Component(0,0,2,0));
  1703. sd.components.push_back(Component(0,1,2,0));
  1704. sd.player = cb->getOwner(heroID);
  1705. cb->showBlockingDialog(&sd,boost::bind(&CGVisitableOPH::arenaSelected,this,heroID,_1));
  1706. return;
  1707. }
  1708. case 51:
  1709. case 23:
  1710. case 61:
  1711. case 32:
  1712. {
  1713. cb->changePrimSkill(heroID,subid,val);
  1714. InfoWindow iw;
  1715. iw.soundID = sound;
  1716. iw.components.push_back(Component(0,subid,val,0));
  1717. iw.text << std::pair<ui8,ui32>(11,ot);
  1718. iw.player = cb->getOwner(heroID);
  1719. cb->showInfoDialog(&iw);
  1720. break;
  1721. }
  1722. case 100: //give exp
  1723. {
  1724. InfoWindow iw;
  1725. iw.soundID = sound;
  1726. iw.components.push_back(Component(id,subid,val,0));
  1727. iw.player = cb->getOwner(heroID);
  1728. iw.text << std::pair<ui8,ui32>(11,ot);
  1729. iw.soundID = soundBase::gazebo;
  1730. cb->showInfoDialog(&iw);
  1731. cb->changePrimSkill(heroID,4,val);
  1732. break;
  1733. }
  1734. case 102://tree
  1735. {
  1736. const CGHeroInstance *h = cb->getHero(heroID);
  1737. val = VLC->heroh->reqExp(h->level+val) - VLC->heroh->reqExp(h->level);
  1738. if(!ttype)
  1739. {
  1740. cb->setObjProperty(this->id,4,heroID); //add to the visitors
  1741. InfoWindow iw;
  1742. iw.soundID = sound;
  1743. iw.components.push_back(Component(id,subid,1,0));
  1744. iw.player = cb->getOwner(heroID);
  1745. iw.text << std::pair<ui8,ui32>(11,148);
  1746. cb->showInfoDialog(&iw);
  1747. cb->changePrimSkill(heroID,4,val);
  1748. break;
  1749. }
  1750. else
  1751. {
  1752. int res, resval;
  1753. if(ttype==1)
  1754. {
  1755. res = 6;
  1756. resval = 2000;
  1757. ot = 149;
  1758. }
  1759. else
  1760. {
  1761. res = 5;
  1762. resval = 10;
  1763. ot = 151;
  1764. }
  1765. if(cb->getResource(h->tempOwner,res) < resval) //not enough resources
  1766. {
  1767. ot++;
  1768. InfoWindow iw;
  1769. iw.soundID = sound;
  1770. iw.player = h->tempOwner;
  1771. iw.text << std::pair<ui8,ui32>(11,ot);
  1772. cb->showInfoDialog(&iw);
  1773. return;
  1774. }
  1775. BlockingDialog sd (true, false);
  1776. sd.soundID = sound;
  1777. sd.player = cb->getOwner(heroID);
  1778. sd.text << std::pair<ui8,ui32>(11,ot);
  1779. sd.components.push_back (Component (Component::RESOURCE, res, resval, 0));
  1780. cb->showBlockingDialog(&sd,boost::bind(&CGVisitableOPH::treeSelected,this,heroID,res,resval,val,_1));
  1781. }
  1782. break;
  1783. }
  1784. case 41://library of enlightenment
  1785. {
  1786. const CGHeroInstance *h = cb->getHero(heroID);
  1787. if(h->level < 10 - 2*h->getSecSkillLevel(4)) //not enough level
  1788. {
  1789. InfoWindow iw;
  1790. iw.soundID = sound;
  1791. iw.player = cb->getOwner(heroID);
  1792. iw.text << std::pair<ui8,ui32>(11,68);
  1793. cb->showInfoDialog(&iw);
  1794. }
  1795. else
  1796. {
  1797. cb->setObjProperty(this->id,4,heroID); //add to the visitors
  1798. cb->changePrimSkill(heroID,0,2);
  1799. cb->changePrimSkill(heroID,1,2);
  1800. cb->changePrimSkill(heroID,2,2);
  1801. cb->changePrimSkill(heroID,3,2);
  1802. InfoWindow iw;
  1803. iw.soundID = sound;
  1804. iw.player = cb->getOwner(heroID);
  1805. iw.text << std::pair<ui8,ui32>(11,66);
  1806. cb->showInfoDialog(&iw);
  1807. }
  1808. break;
  1809. }
  1810. case 47: //School of Magic
  1811. case 107://School of War
  1812. {
  1813. int skill = (ID==47 ? 2 : 0);
  1814. if(cb->getResource(cb->getOwner(heroID),6) < 1000) //not enough resources
  1815. {
  1816. InfoWindow iw;
  1817. iw.soundID = sound;
  1818. iw.player = cb->getOwner(heroID);
  1819. iw.text << std::pair<ui8,ui32>(MetaString::ADVOB_TXT,ot+2);
  1820. cb->showInfoDialog(&iw);
  1821. }
  1822. else
  1823. {
  1824. BlockingDialog sd(true,true);
  1825. sd.soundID = sound;
  1826. sd.player = cb->getOwner(heroID);
  1827. sd.text << std::pair<ui8,ui32>(11,ot);
  1828. sd.components.push_back(Component(Component::PRIM_SKILL, skill, +1, 0));
  1829. sd.components.push_back(Component(Component::PRIM_SKILL, skill+1, +1, 0));
  1830. cb->showBlockingDialog(&sd,boost::bind(&CGVisitableOPH::schoolSelected,this,heroID,_1));
  1831. }
  1832. }
  1833. break;
  1834. }
  1835. }
  1836. else
  1837. {
  1838. ot++;
  1839. InfoWindow iw;
  1840. iw.soundID = sound;
  1841. iw.player = cb->getOwner(heroID);
  1842. iw.text << std::pair<ui8,ui32>(11,ot);
  1843. cb->showInfoDialog(&iw);
  1844. }
  1845. }
  1846. const std::string & CGVisitableOPH::getHoverText() const
  1847. {
  1848. int pom = -1;
  1849. switch(ID)
  1850. {
  1851. case 4:
  1852. pom = -1;
  1853. break;
  1854. case 51:
  1855. pom = 8;
  1856. break;
  1857. case 23:
  1858. pom = 7;
  1859. break;
  1860. case 61:
  1861. pom = 11;
  1862. break;
  1863. case 32:
  1864. pom = 4;
  1865. break;
  1866. case 100:
  1867. pom = 5;
  1868. break;
  1869. case 102:
  1870. pom = 18;
  1871. break;
  1872. case 41:
  1873. break;
  1874. case 47: //School of Magic
  1875. pom = 9;
  1876. break;
  1877. case 107://School of War
  1878. pom = 10;
  1879. break;
  1880. default:
  1881. throw std::string("Wrong CGVisitableOPH object ID!\n");
  1882. }
  1883. hoverName = VLC->generaltexth->names[ID];
  1884. if(pom >= 0)
  1885. hoverName += (" " + VLC->generaltexth->xtrainfo[pom]);
  1886. const CGHeroInstance *h = cb->getSelectedHero(cb->getCurrentPlayer());
  1887. if(h)
  1888. {
  1889. hoverName += ' ';
  1890. hoverName += (vstd::contains(visitors,h->id))
  1891. ? (VLC->generaltexth->allTexts[352]) //visited
  1892. : ( VLC->generaltexth->allTexts[353]); //not visited
  1893. }
  1894. return hoverName;
  1895. }
  1896. void CGVisitableOPH::arenaSelected( int heroID, int primSkill ) const
  1897. {
  1898. cb->setObjProperty(id,4,heroID); //add to the visitors
  1899. cb->changePrimSkill(heroID,primSkill-1,2);
  1900. }
  1901. void CGVisitableOPH::setPropertyDer( ui8 what, ui32 val )
  1902. {
  1903. if(what == 4)
  1904. visitors.insert(val);
  1905. }
  1906. void CGVisitableOPH::schoolSelected(int heroID, ui32 which) const
  1907. {
  1908. if(!which) //player refused to pay
  1909. return;
  1910. int base = (ID == 47 ? 2 : 0);
  1911. cb->setObjProperty(id,4,heroID); //add to the visitors
  1912. cb->giveResource(cb->getOwner(heroID),6,-1000); //take 1000 gold
  1913. cb->changePrimSkill(heroID, base + which-1, +1); //give appropriate skill
  1914. }
  1915. COPWBonus::COPWBonus (int index, CGTownInstance *TOWN)
  1916. {
  1917. ID = index;
  1918. town = TOWN;
  1919. id = town->bonusingBuildings.size();
  1920. }
  1921. void COPWBonus::setProperty(ui8 what, ui32 val)
  1922. {
  1923. switch (what)
  1924. {
  1925. case 4:
  1926. visitors.insert(val);
  1927. break;
  1928. case 12:
  1929. visitors.clear();
  1930. break;
  1931. }
  1932. }
  1933. void COPWBonus::onHeroVisit (const CGHeroInstance * h) const
  1934. {
  1935. int heroID = h->id;
  1936. if (town->builtBuildings.find(ID) != town->builtBuildings.end())
  1937. {
  1938. InfoWindow iw;
  1939. iw.player = h->tempOwner;
  1940. switch (town->subID)
  1941. {
  1942. case 0: //Stables
  1943. if (!h->getBonus(HeroBonus::OBJECT, 94)) //no advMap Stables
  1944. {
  1945. GiveBonus gb;
  1946. gb.bonus = HeroBonus(HeroBonus::ONE_WEEK, HeroBonus::LAND_MOVEMENT, HeroBonus::OBJECT, 600, 94, VLC->generaltexth->arraytxt[100]);
  1947. gb.hid = heroID;
  1948. cb->giveHeroBonus(&gb);
  1949. iw.text << VLC->generaltexth->allTexts[580];
  1950. cb->showInfoDialog(&iw);
  1951. }
  1952. break;
  1953. case 5: //Mana Vortex
  1954. if (visitors.empty() && h->mana <= h->manaLimit())
  1955. {
  1956. cb->setManaPoints (heroID, 2 * h->manaLimit());
  1957. cb->setObjProperty (id, 5, true);
  1958. iw.text << VLC->generaltexth->allTexts[579];
  1959. cb->showInfoDialog(&iw);
  1960. cb->setObjProperty (town->id, 11, id); //add to visitors
  1961. }
  1962. break;
  1963. }
  1964. }
  1965. }
  1966. CTownBonus::CTownBonus (int index, CGTownInstance *TOWN)
  1967. {
  1968. ID = index;
  1969. town = TOWN;
  1970. id = town->bonusingBuildings.size();
  1971. }
  1972. void CTownBonus::setProperty (ui8 what, ui32 val)
  1973. {
  1974. if(what == 4)
  1975. visitors.insert(val);
  1976. }
  1977. void CTownBonus::onHeroVisit (const CGHeroInstance * h) const
  1978. {
  1979. int heroID = h->id;
  1980. if ((town->builtBuildings.find(ID) != town->builtBuildings.end()) && (visitors.find(heroID) == visitors.end()))
  1981. {
  1982. InfoWindow iw;
  1983. int what, val, mid;
  1984. switch (ID)
  1985. {
  1986. case 23:
  1987. switch(town->subID)
  1988. {
  1989. case 2: //wall
  1990. what = 3;
  1991. val = 1;
  1992. mid = 581;
  1993. iw.components.push_back (Component(Component::PRIM_SKILL, 3, 1, 0));
  1994. break;
  1995. case 3: //order of fire
  1996. what = 2;
  1997. val = 1;
  1998. mid = 582;
  1999. iw.components.push_back (Component(Component::PRIM_SKILL, 2, 1, 0));
  2000. break;
  2001. case 6://hall of valhalla
  2002. what = 0;
  2003. val = 1;
  2004. mid = 584;
  2005. iw.components.push_back (Component(Component::PRIM_SKILL, 0, 1, 0));
  2006. break;
  2007. case 5://academy of battle scholars
  2008. what = 4;
  2009. val = 1000;
  2010. mid = 583;
  2011. iw.components.push_back (Component(Component::EXPERIENCE, 0, 1000, 0));
  2012. break;
  2013. }
  2014. break;
  2015. case 17:
  2016. switch(town->subID)
  2017. {
  2018. case 7: //cage of warlords
  2019. what = 1;
  2020. val = 1;
  2021. mid = 585;
  2022. iw.components.push_back (Component(Component::PRIM_SKILL, 1, 1, 0));
  2023. break;
  2024. }
  2025. break;
  2026. }
  2027. iw.player = cb->getOwner(heroID);
  2028. iw.text << VLC->generaltexth->allTexts[mid];
  2029. cb->showInfoDialog(&iw);
  2030. cb->changePrimSkill (heroID, what, val);
  2031. if (town->visitingHero == h)
  2032. cb->setObjProperty (town->id, 11, id); //add to visitors
  2033. else
  2034. cb->setObjProperty (town->id, 13, id); //then it must be garrisoned hero
  2035. }
  2036. }
  2037. bool CArmedInstance::needsLastStack() const
  2038. {
  2039. return false;
  2040. }
  2041. int CArmedInstance::getArmyStrength() const
  2042. {
  2043. int ret = 0;
  2044. for(std::map<si32,std::pair<ui32,si32> >::const_iterator i=army.slots.begin(); i!=army.slots.end(); i++)
  2045. ret += VLC->creh->creatures[i->second.first].AIValue * i->second.second;
  2046. return ret;
  2047. }
  2048. ui64 CArmedInstance::getPower (TSlot slot) const
  2049. {
  2050. return VLC->creh->creatures[army.getCreature(slot)].AIValue * army.getAmount(slot);
  2051. }
  2052. std::string CArmedInstance::getRoughAmount (TSlot slot) const
  2053. {
  2054. return VLC->generaltexth->arraytxt[174 + 3*CCreature::getQuantityID(army.getAmount(slot))];
  2055. }
  2056. /*const std::string & CGCreature::getHoverText() const
  2057. {
  2058. hoverName = VLC->generaltexth->names[ID];
  2059. hoverName += "\n Power rating: ";
  2060. float ratio = ((float)getArmyStrength() / cb->getSelectedHero(cb->getCurrentPlayer())->getTotalStrength());
  2061. if (ratio < 0.1) hoverName += "Effortless";
  2062. else if (ratio < 0.3) hoverName += "Very Weak";
  2063. else if (ratio < 0.6) hoverName += "Weak";
  2064. else if (ratio < 0.9) hoverName += "A bit weaker";
  2065. else if (ratio < 1.1) hoverName += "Equal";
  2066. else if (ratio < 1.3) hoverName += "A bit stronger";
  2067. else if (ratio < 1.8) hoverName += "Strong";
  2068. else if (ratio < 2.5) hoverName += "Very Strong";
  2069. else if (ratio < 4) hoverName += "Challenging";
  2070. else if (ratio < 8) hoverName += "Overpowering";
  2071. else if (ratio < 20) hoverName += "Deadly";
  2072. else hoverName += "Impossible";
  2073. return hoverName;
  2074. }*/
  2075. void CGCreature::onHeroVisit( const CGHeroInstance * h ) const
  2076. {
  2077. int action = takenAction(h);
  2078. switch( action ) //decide what we do...
  2079. {
  2080. case -2: //fight
  2081. fight(h);
  2082. break;
  2083. case -1: //flee
  2084. {
  2085. flee(h);
  2086. break;
  2087. }
  2088. case 0: //join for free
  2089. {
  2090. BlockingDialog ynd(true,false);
  2091. ynd.player = h->tempOwner;
  2092. ynd.text << std::pair<ui8,ui32>(MetaString::ADVOB_TXT, 86);
  2093. ynd.text.addReplacement(MetaString::CRE_PL_NAMES, subID);
  2094. cb->showBlockingDialog(&ynd,boost::bind(&CGCreature::joinDecision,this,h,0,_1));
  2095. break;
  2096. }
  2097. default: //join for gold
  2098. {
  2099. assert(action > 0);
  2100. //ask if player agrees to pay gold
  2101. BlockingDialog ynd(true,false);
  2102. ynd.player = h->tempOwner;
  2103. std::string tmp = VLC->generaltexth->advobtxt[90];
  2104. boost::algorithm::replace_first(tmp,"%d",boost::lexical_cast<std::string>(army.slots.find(0)->second.second));
  2105. boost::algorithm::replace_first(tmp,"%d",boost::lexical_cast<std::string>(action));
  2106. boost::algorithm::replace_first(tmp,"%s",VLC->creh->creatures[subID].namePl);
  2107. ynd.text << tmp;
  2108. cb->showBlockingDialog(&ynd,boost::bind(&CGCreature::joinDecision,this,h,action,_1));
  2109. break;
  2110. }
  2111. }
  2112. }
  2113. void CGCreature::endBattle( BattleResult *result ) const
  2114. {
  2115. if(result->winner==0)
  2116. {
  2117. cb->removeObject(id);
  2118. }
  2119. else
  2120. {
  2121. //int killedAmount=0;
  2122. //for(std::set<std::pair<ui32,si32> >::iterator i=result->casualties[1].begin(); i!=result->casualties[1].end(); i++)
  2123. // if(i->first == subID)
  2124. // killedAmount += i->second;
  2125. //cb->setAmount(id, army.slots.find(0)->second.second - killedAmount);
  2126. MetaString ms;
  2127. int pom = CCreature::getQuantityID(army.slots.find(0)->second.second);
  2128. pom = 174 + 3*pom + 1;
  2129. ms << std::pair<ui8,ui32>(6,pom) << " " << std::pair<ui8,ui32>(7,subID);
  2130. cb->setHoverName(id,&ms);
  2131. }
  2132. }
  2133. void CGCreature::initObj()
  2134. {
  2135. blockVisit = true;
  2136. switch(character)
  2137. {
  2138. case 0:
  2139. character = 0;
  2140. break;
  2141. case 1:
  2142. character = 1 + ran()%7;
  2143. break;
  2144. case 2:
  2145. character = 1 + ran()%10;
  2146. break;
  2147. case 3:
  2148. character = 4 + ran()%7;
  2149. break;
  2150. case 4:
  2151. character = 10;
  2152. break;
  2153. }
  2154. army.slots[0].first = subID;
  2155. si32 &amount = army.slots[0].second;
  2156. CCreature &c = VLC->creh->creatures[subID];
  2157. if(!amount)
  2158. if(c.ammMax == c.ammMin)
  2159. amount = c.ammMax;
  2160. else
  2161. amount = c.ammMin + (ran() % (c.ammMax - c.ammMin));
  2162. MetaString ms;
  2163. int pom = CCreature::getQuantityID(army.slots.find(0)->second.second);
  2164. pom = 174 + 3*pom + 1;
  2165. ms << std::pair<ui8,ui32>(6,pom) << " " << std::pair<ui8,ui32>(7,subID);
  2166. ms.toString(hoverName);
  2167. }
  2168. int CGCreature::takenAction(const CGHeroInstance *h, bool allowJoin) const
  2169. {
  2170. double hlp = h->getTotalStrength() / getArmyStrength();
  2171. if(!character) //compliant creatures will always join
  2172. return 0;
  2173. else if(allowJoin)//test for joining
  2174. {
  2175. int factor;
  2176. if(hlp >= 7)
  2177. factor = 11;
  2178. else if(hlp >= 1)
  2179. factor = (int)(2*(hlp-1));
  2180. else if(hlp >= 0.5)
  2181. factor = -1;
  2182. else if(hlp >= 0.333)
  2183. factor = -2;
  2184. else
  2185. factor = -3;
  2186. int sympathy = 0;
  2187. std::set<ui32> myKindCres; //what creatures are the same kind as we
  2188. myKindCres.insert(subID); //we
  2189. myKindCres.insert(VLC->creh->creatures[subID].upgrades.begin(),VLC->creh->creatures[subID].upgrades.end()); //our upgrades
  2190. for(std::vector<CCreature>::iterator i=VLC->creh->creatures.begin(); i!=VLC->creh->creatures.end(); i++)
  2191. if(vstd::contains(i->upgrades, (ui32) id)) //it's our base creatures
  2192. myKindCres.insert(i->idNumber);
  2193. int count = 0, //how many creatures of our kind has hero
  2194. totalCount = 0;
  2195. for (std::map<si32,std::pair<ui32,si32> >::const_iterator i = h->army.slots.begin(); i != h->army.slots.end(); i++)
  2196. {
  2197. if(vstd::contains(myKindCres,i->second.first))
  2198. count += i->second.second;
  2199. totalCount += i->second.second;
  2200. }
  2201. if(count*2 > totalCount)
  2202. sympathy++;
  2203. if(count)
  2204. sympathy++;
  2205. int charisma = factor + h->getSecSkillLevel(4) + sympathy;
  2206. if(charisma >= character) //creatures might join...
  2207. {
  2208. if(h->getSecSkillLevel(4) + sympathy + 1 >= character)
  2209. return 0; //join for free
  2210. else if(h->getSecSkillLevel(4) * 2 + sympathy + 1 >= character)
  2211. return VLC->creh->creatures[subID].cost[6] * army.slots.find(0)->second.second; //join for gold
  2212. }
  2213. }
  2214. //we are still here - creatures not joined heroes, test for fleeing
  2215. //TODO: it's provisional formula, should be replaced with original one (or something closer to it)
  2216. //TODO: should be deterministic (will be needed for Vision spell)
  2217. int hlp2 = (int) (hlp - 2)*1000;
  2218. if(!neverFlees
  2219. && hlp2 >= 0
  2220. && rand()%2000 < hlp2
  2221. )
  2222. return -1; //flee
  2223. else
  2224. return -2; //fight
  2225. }
  2226. void CGCreature::fleeDecision(const CGHeroInstance *h, ui32 pursue) const
  2227. {
  2228. if(pursue)
  2229. {
  2230. fight(h);
  2231. }
  2232. else
  2233. {
  2234. cb->removeObject(id);
  2235. }
  2236. }
  2237. void CGCreature::joinDecision(const CGHeroInstance *h, int cost, ui32 accept) const
  2238. {
  2239. if(!accept)
  2240. {
  2241. if(takenAction(h,false) == -1) //they flee
  2242. {
  2243. flee(h);
  2244. }
  2245. else //they fight
  2246. {
  2247. InfoWindow iw;
  2248. iw.player = h->tempOwner;
  2249. iw.text << std::pair<ui8,ui32>(11,87); //Insulted by your refusal of their offer, the monsters attack!
  2250. cb->showInfoDialog(&iw);
  2251. fight(h);
  2252. }
  2253. }
  2254. else //accepted
  2255. {
  2256. if (cb->getResource(h->tempOwner,6) < cost) //player don't have enough gold!
  2257. {
  2258. InfoWindow iw;
  2259. iw.player = h->tempOwner;
  2260. iw.text << std::pair<ui8,ui32>(1,29); //You don't have enough gold
  2261. cb->showInfoDialog(&iw);
  2262. //act as if player refused
  2263. joinDecision(h,cost,false);
  2264. return;
  2265. }
  2266. //take gold
  2267. if(cost)
  2268. cb->giveResource(h->tempOwner,6,-cost);
  2269. int slot = h->army.getSlotFor(subID);
  2270. if(slot >= 0) //there is place
  2271. {
  2272. //add creatures
  2273. SetGarrisons sg;
  2274. sg.garrs[h->id] = h->army;
  2275. if(vstd::contains(h->army.slots,slot)) //add to already present stack
  2276. {
  2277. sg.garrs[h->id].slots[slot].second += army.slots.find(0)->second.second;
  2278. }
  2279. else //add as a new stack
  2280. {
  2281. sg.garrs[h->id].slots[slot] = army.slots.find(0)->second;
  2282. }
  2283. cb->sendAndApply(&sg);
  2284. cb->removeObject(id);
  2285. }
  2286. else
  2287. {
  2288. cb->showGarrisonDialog(id,h->id,true,boost::bind(&IGameCallback::removeObject,cb,id)); //show garrison window and remove ourselves from map when player ends
  2289. }
  2290. }
  2291. }
  2292. void CGCreature::fight( const CGHeroInstance *h ) const
  2293. {
  2294. cb->startBattleI(h, this, boost::bind(&CGCreature::endBattle,this,_1));
  2295. }
  2296. void CGCreature::flee( const CGHeroInstance * h ) const
  2297. {
  2298. BlockingDialog ynd(true,false);
  2299. ynd.player = h->tempOwner;
  2300. ynd.text << std::pair<ui8,ui32>(11,91);
  2301. ynd.text.addReplacement(MetaString::CRE_PL_NAMES, subID);
  2302. cb->showBlockingDialog(&ynd,boost::bind(&CGCreature::fleeDecision,this,h,_1));
  2303. }
  2304. void CGMine::onHeroVisit( const CGHeroInstance * h ) const
  2305. {
  2306. if(subID == 7) //TODO: support for abandoned mine
  2307. return;
  2308. if(h->tempOwner == tempOwner) //we're visiting our mine
  2309. {
  2310. cb->showGarrisonDialog(id,h->id,true,0);
  2311. return;
  2312. }
  2313. //TODO: check if mine is guarded
  2314. cb->setOwner(id,h->tempOwner); //not ours? flag it!
  2315. MetaString ms;
  2316. ms << std::pair<ui8,ui32>(9,subID) << " (" << std::pair<ui8,ui32>(6,23+h->tempOwner) << ")";
  2317. cb->setHoverName(id,&ms);
  2318. int vv=1; //amount of resource per turn
  2319. if (subID==0 || subID==2)
  2320. vv++;
  2321. else if (subID==6)
  2322. vv = 1000;
  2323. InfoWindow iw;
  2324. iw.soundID = soundBase::FLAGMINE;
  2325. iw.text << std::pair<ui8,ui32>(10,subID);
  2326. iw.player = h->tempOwner;
  2327. iw.components.push_back(Component(2,subID,vv,-1));
  2328. cb->showInfoDialog(&iw);
  2329. }
  2330. void CGMine::newTurn() const
  2331. {
  2332. if(cb->getDate() == 1)
  2333. return;
  2334. if (tempOwner == NEUTRAL_PLAYER)
  2335. return;
  2336. int vv = 1;
  2337. if (subID==0 || subID==2)
  2338. vv++;
  2339. else if (subID==6)
  2340. vv = 1000;
  2341. cb->giveResource(tempOwner,subID,vv);
  2342. }
  2343. void CGMine::initObj()
  2344. {
  2345. MetaString ms;
  2346. ms << std::pair<ui8,ui32>(9,subID);
  2347. if(tempOwner >= PLAYER_LIMIT)
  2348. tempOwner = NEUTRAL_PLAYER;
  2349. else
  2350. ms << " (" << std::pair<ui8,ui32>(6,23+tempOwner) << ")";
  2351. ms.toString(hoverName);
  2352. }
  2353. void CGResource::initObj()
  2354. {
  2355. blockVisit = true;
  2356. hoverName = VLC->generaltexth->restypes[subID];
  2357. if(!amount)
  2358. {
  2359. switch(subID)
  2360. {
  2361. case 6:
  2362. amount = 500 + (rand()%6)*100;
  2363. break;
  2364. case 0: case 2:
  2365. amount = 6 + (rand()%5);
  2366. break;
  2367. default:
  2368. amount = 3 + (rand()%3);
  2369. break;
  2370. }
  2371. }
  2372. }
  2373. void CGResource::onHeroVisit( const CGHeroInstance * h ) const
  2374. {
  2375. if(army.slots.size())
  2376. {
  2377. if(message.size())
  2378. {
  2379. BlockingDialog ynd(true,false);
  2380. ynd.player = h->getOwner();
  2381. ynd.text << message;
  2382. cb->showBlockingDialog(&ynd,boost::bind(&CGResource::fightForRes,this,_1,h));
  2383. }
  2384. else
  2385. {
  2386. fightForRes(1,h);
  2387. }
  2388. }
  2389. else
  2390. {
  2391. if(message.length())
  2392. {
  2393. InfoWindow iw;
  2394. iw.player = h->tempOwner;
  2395. iw.text << message;
  2396. cb->showInfoDialog(&iw);
  2397. }
  2398. collectRes(h->getOwner());
  2399. }
  2400. }
  2401. void CGResource::collectRes( int player ) const
  2402. {
  2403. cb->giveResource(player,subID,amount);
  2404. ShowInInfobox sii;
  2405. sii.player = player;
  2406. sii.c = Component(2,subID,amount,0);
  2407. sii.text << std::pair<ui8,ui32>(11,113);
  2408. sii.text.addReplacement(MetaString::RES_NAMES, subID);
  2409. cb->showCompInfo(&sii);
  2410. cb->removeObject(id);
  2411. }
  2412. void CGResource::fightForRes(ui32 agreed, const CGHeroInstance *h) const
  2413. {
  2414. if(agreed)
  2415. cb->startBattleI(h, this, boost::bind(&CGResource::endBattle,this,_1,h));
  2416. }
  2417. void CGResource::endBattle( BattleResult *result, const CGHeroInstance *h ) const
  2418. {
  2419. if(result->winner == 0) //attacker won
  2420. collectRes(h->getOwner());
  2421. }
  2422. void CGVisitableOPW::newTurn() const
  2423. {
  2424. if (cb->getDate(1)==1) //first day of week
  2425. {
  2426. cb->setObjProperty(id,5,false);
  2427. MetaString ms; //set text to "not visited"
  2428. ms << std::pair<ui8,ui32>(3,ID) << " " << std::pair<ui8,ui32>(1,353);
  2429. cb->setHoverName(id,&ms);
  2430. }
  2431. }
  2432. void CGVisitableOPW::onHeroVisit( const CGHeroInstance * h ) const
  2433. {
  2434. int mid, sound = 0;
  2435. switch (ID)
  2436. {
  2437. case 55: //mystical garden
  2438. sound = soundBase::experience;
  2439. mid = 92;
  2440. break;
  2441. case 112://windmill
  2442. sound = soundBase::GENIE;
  2443. mid = 170;
  2444. break;
  2445. case 109://waterwheel
  2446. sound = soundBase::GENIE;
  2447. mid = 164;
  2448. break;
  2449. }
  2450. if (visited)
  2451. {
  2452. if (ID!=112)
  2453. mid++;
  2454. else
  2455. mid--;
  2456. InfoWindow iw;
  2457. iw.soundID = sound;
  2458. iw.player = h->tempOwner;
  2459. iw.text << std::pair<ui8,ui32>(11,mid);
  2460. cb->showInfoDialog(&iw);
  2461. }
  2462. else
  2463. {
  2464. int type, sub, val;
  2465. type = 2;
  2466. switch (ID)
  2467. {
  2468. case 55:
  2469. if (rand()%2)
  2470. {
  2471. sub = 5;
  2472. val = 5;
  2473. }
  2474. else
  2475. {
  2476. sub = 6;
  2477. val = 500;
  2478. }
  2479. break;
  2480. case 112:
  2481. mid = 170;
  2482. sub = (rand() % 5) + 1;
  2483. val = (rand() % 4) + 3;
  2484. break;
  2485. case 109:
  2486. mid = 164;
  2487. sub = 6;
  2488. if(cb->getDate(0)<8)
  2489. val = 500;
  2490. else
  2491. val = 1000;
  2492. }
  2493. cb->giveResource(h->tempOwner,sub,val);
  2494. InfoWindow iw;
  2495. iw.soundID = sound;
  2496. iw.player = h->tempOwner;
  2497. iw.components.push_back(Component(type,sub,val,0));
  2498. iw.text << std::pair<ui8,ui32>(11,mid);
  2499. cb->showInfoDialog(&iw);
  2500. cb->setObjProperty(id,5,true);
  2501. MetaString ms; //set text to "visited"
  2502. ms << std::pair<ui8,ui32>(3,ID) << " " << std::pair<ui8,ui32>(1,352);
  2503. cb->setHoverName(id,&ms);
  2504. }
  2505. }
  2506. void CGVisitableOPW::setPropertyDer( ui8 what, ui32 val )
  2507. {
  2508. if(what == 5)
  2509. visited = val;
  2510. }
  2511. void CGTeleport::onHeroVisit( const CGHeroInstance * h ) const
  2512. {
  2513. int destinationid=-1;
  2514. switch(ID)
  2515. {
  2516. case 43: //one way - find corresponding exit monolith
  2517. if(vstd::contains(objs,44) && vstd::contains(objs[44],subID) && objs[44][subID].size())
  2518. destinationid = objs[44][subID][rand()%objs[44][subID].size()];
  2519. else
  2520. tlog2 << "Cannot find corresponding exit monolith for "<< id << std::endl;
  2521. break;
  2522. case 45: //two way monolith - pick any other one
  2523. if(vstd::contains(objs,45) && vstd::contains(objs[45],subID) && objs[45][subID].size()>1)
  2524. while ((destinationid = objs[45][subID][rand()%objs[45][subID].size()])==id);
  2525. else
  2526. tlog2 << "Cannot find corresponding exit monolith for "<< id << std::endl;
  2527. break;
  2528. case 103: //find nearest subterranean gate on the other level
  2529. {
  2530. for(int i=0; i < gates.size(); i++)
  2531. {
  2532. if(gates[i].first == id)
  2533. {
  2534. destinationid = gates[i].second;
  2535. break;
  2536. }
  2537. else if(gates[i].second == id)
  2538. {
  2539. destinationid = gates[i].first;
  2540. break;
  2541. }
  2542. }
  2543. break;
  2544. }
  2545. }
  2546. if(destinationid < 0)
  2547. {
  2548. tlog2 << "Cannot find exit... (obj at " << pos << ") :( \n";
  2549. return;
  2550. }
  2551. cb->moveHero(h->id,CGHeroInstance::convertPosition(cb->getObj(destinationid)->pos,true) - getVisitableOffset(),
  2552. true);
  2553. }
  2554. void CGTeleport::initObj()
  2555. {
  2556. int si = subID;
  2557. if(ID == 103) //ignore subterranean gates subid
  2558. si = 0;
  2559. objs[ID][si].push_back(id);
  2560. }
  2561. void CGTeleport::postInit() //matches subterranean gates into pairs
  2562. {
  2563. //split on underground and surface gates
  2564. std::vector<const CGObjectInstance *> gatesSplit[2]; //surface and underground gates
  2565. for(size_t i = 0; i < objs[103][0].size(); i++)
  2566. {
  2567. const CGObjectInstance *hlp = cb->getObj(objs[103][0][i]);
  2568. gatesSplit[hlp->pos.z].push_back(hlp);
  2569. }
  2570. //sort by position
  2571. std::sort(gatesSplit[0].begin(), gatesSplit[0].end(), boost::bind(&CGObjectInstance::pos, _1) < boost::bind(&CGObjectInstance::pos, _2));
  2572. for(size_t i = 0; i < gatesSplit[0].size(); i++)
  2573. {
  2574. const CGObjectInstance *cur = gatesSplit[0][i];
  2575. //find nearest underground exit
  2576. std::pair<int,double> best(-1,150000); //pair<pos_in_vector, distance>
  2577. for(int j = 0; j < gatesSplit[1].size(); j++)
  2578. {
  2579. const CGObjectInstance *checked = gatesSplit[1][j];
  2580. if(!checked)
  2581. continue;
  2582. double hlp = checked->pos.dist2d(cur->pos);
  2583. if(hlp < best.second)
  2584. {
  2585. best.first = j;
  2586. best.second = hlp;
  2587. }
  2588. }
  2589. gates.push_back(std::pair<int, int>(cur->id, gatesSplit[1][best.first]->id));
  2590. gatesSplit[1][best.first] = NULL;
  2591. }
  2592. objs.erase(103);
  2593. }
  2594. void CGArtifact::initObj()
  2595. {
  2596. blockVisit = true;
  2597. if(ID == 5)
  2598. hoverName = VLC->arth->artifacts[subID].Name();
  2599. }
  2600. void CGArtifact::onHeroVisit( const CGHeroInstance * h ) const
  2601. {
  2602. if(!army.slots.size())
  2603. {
  2604. if(ID == 5)
  2605. {
  2606. InfoWindow iw;
  2607. iw.soundID = soundBase::treasure;
  2608. iw.player = h->tempOwner;
  2609. iw.components.push_back(Component(4,subID,0,0));
  2610. if(message.length())
  2611. iw.text << message;
  2612. else
  2613. iw.text << std::pair<ui8,ui32>(12,subID);
  2614. cb->showInfoDialog(&iw);
  2615. }
  2616. pick(h);
  2617. }
  2618. else
  2619. {
  2620. if(message.size())
  2621. {
  2622. BlockingDialog ynd(true,false);
  2623. ynd.player = h->getOwner();
  2624. ynd.text << message;
  2625. cb->showBlockingDialog(&ynd,boost::bind(&CGArtifact::fightForArt,this,_1,h));
  2626. }
  2627. else
  2628. {
  2629. fightForArt(0,h);
  2630. }
  2631. }
  2632. }
  2633. void CGArtifact::pick(const CGHeroInstance * h) const
  2634. {
  2635. if(ID == 5) //Artifact
  2636. {
  2637. cb->giveHeroArtifact(subID,h->id,-2);
  2638. }
  2639. else if(ID == 93) // Spell scroll
  2640. {
  2641. //TODO: support for the spell scroll
  2642. }
  2643. cb->removeObject(id);
  2644. }
  2645. void CGArtifact::fightForArt( ui32 agreed, const CGHeroInstance *h ) const
  2646. {
  2647. if(agreed)
  2648. cb->startBattleI(h, this, boost::bind(&CGArtifact::endBattle,this,_1,h));
  2649. }
  2650. void CGArtifact::endBattle( BattleResult *result, const CGHeroInstance *h ) const
  2651. {
  2652. if(result->winner == 0) //attacker won
  2653. pick(h);
  2654. }
  2655. void CGPickable::initObj()
  2656. {
  2657. blockVisit = true;
  2658. switch(ID)
  2659. {
  2660. case 12: //campfire
  2661. val2 = (ran()%3) + 4; //4 - 6
  2662. val1 = val2 * 100;
  2663. type = ran()%6; //given resource
  2664. break;
  2665. case 29: //floatsam
  2666. switch(type = ran()%4)
  2667. {
  2668. case 0:
  2669. val1 = val2 = 0;
  2670. break;
  2671. case 1:
  2672. val1 = 5;
  2673. val2 = 0;
  2674. break;
  2675. case 2:
  2676. val1 = 5;
  2677. val2 = 200;
  2678. break;
  2679. case 3:
  2680. val1 = 10;
  2681. val2 = 500;
  2682. break;
  2683. }
  2684. break;
  2685. case 82: //sea chest
  2686. {
  2687. int hlp = ran()%100;
  2688. if(hlp < 20)
  2689. {
  2690. val1 = 0;
  2691. type = 0;
  2692. }
  2693. else if(hlp < 90)
  2694. {
  2695. val1 = 1500;
  2696. type = 2;
  2697. }
  2698. else
  2699. {
  2700. val1 = 1000;
  2701. val2 = cb->getRandomArt (CArtifact::ART_TREASURE);
  2702. type = 1;
  2703. }
  2704. }
  2705. break;
  2706. case 86: //Shipwreck Survivor
  2707. {
  2708. int hlp = ran()%100;
  2709. if(hlp < 55)
  2710. val1 = cb->getRandomArt (CArtifact::ART_TREASURE);
  2711. else if(hlp < 75)
  2712. val1 = cb->getRandomArt (CArtifact::ART_MINOR);
  2713. else if(hlp < 95)
  2714. val1 = cb->getRandomArt (CArtifact::ART_MAJOR);
  2715. else
  2716. val1 = cb->getRandomArt (CArtifact::ART_RELIC);
  2717. }
  2718. break;
  2719. case 101: //treasure chest
  2720. {
  2721. int hlp = ran()%100;
  2722. if(hlp >= 95)
  2723. {
  2724. type = 1;
  2725. val1 = cb->getRandomArt (CArtifact::ART_TREASURE);
  2726. return;
  2727. }
  2728. else if (hlp >= 65)
  2729. {
  2730. val1 = 2000;
  2731. }
  2732. else if(hlp >= 33)
  2733. {
  2734. val1 = 1500;
  2735. }
  2736. else
  2737. {
  2738. val1 = 1000;
  2739. }
  2740. val2 = val1 - 500;
  2741. type = 0;
  2742. break;
  2743. }
  2744. }
  2745. }
  2746. void CGPickable::onHeroVisit( const CGHeroInstance * h ) const
  2747. {
  2748. switch(ID)
  2749. {
  2750. case 12: //campfire
  2751. {
  2752. cb->giveResource(h->tempOwner,type,val2); //non-gold resource
  2753. cb->giveResource(h->tempOwner,6,val1);//gold
  2754. InfoWindow iw;
  2755. iw.soundID = soundBase::experience;
  2756. iw.player = h->tempOwner;
  2757. iw.components.push_back(Component(2,6,val1,0));
  2758. iw.components.push_back(Component(2,type,val2,0));
  2759. iw.text << std::pair<ui8,ui32>(11,23);
  2760. cb->showInfoDialog(&iw);
  2761. break;
  2762. }
  2763. case 29: //flotsam
  2764. {
  2765. cb->giveResource(h->tempOwner,0,val1); //wood
  2766. cb->giveResource(h->tempOwner,6,val2);//gold
  2767. InfoWindow iw;
  2768. iw.soundID = soundBase::GENIE;
  2769. iw.player = h->tempOwner;
  2770. if(val1)
  2771. iw.components.push_back(Component(2,0,val1,0));
  2772. if(val2)
  2773. iw.components.push_back(Component(2,6,val2,0));
  2774. iw.text.addTxt(MetaString::ADVOB_TXT, 51+type);
  2775. cb->showInfoDialog(&iw);
  2776. break;
  2777. }
  2778. case 82: //Sea Chest
  2779. {
  2780. InfoWindow iw;
  2781. iw.soundID = soundBase::chest;
  2782. iw.player = h->tempOwner;
  2783. iw.text.addTxt(MetaString::ADVOB_TXT, 116 + type);
  2784. if(val1) //there is gold
  2785. {
  2786. iw.components.push_back(Component(2,6,val1,0));
  2787. cb->giveResource(h->tempOwner,6,val1);
  2788. }
  2789. if(type == 1) //art
  2790. {
  2791. //TODO: what if no space in backpack?
  2792. iw.components.push_back(Component(4, val2, 1, 0));
  2793. iw.text.addReplacement(MetaString::ART_NAMES, val2);
  2794. cb->giveHeroArtifact(val2,h->id,-2);
  2795. }
  2796. cb->showInfoDialog(&iw);
  2797. break;
  2798. }
  2799. case 86: //Shipwreck Survivor
  2800. {
  2801. //TODO: what if no space in backpack?
  2802. InfoWindow iw;
  2803. iw.soundID = soundBase::experience;
  2804. iw.player = h->tempOwner;
  2805. iw.components.push_back(Component(4,val1,1,0));
  2806. iw.text.addTxt(MetaString::ADVOB_TXT, 125);
  2807. iw.text.addReplacement(MetaString::ART_NAMES, val1);
  2808. cb->giveHeroArtifact(val1,h->id,-2);
  2809. cb->showInfoDialog(&iw);
  2810. break;
  2811. }
  2812. case 101: //treasure chest
  2813. {
  2814. if (subID) //not OH3 treasure chest
  2815. {
  2816. tlog2 << "Not supported WoG treasure chest!\n";
  2817. return;
  2818. }
  2819. if(type) //there is an artifact
  2820. {
  2821. cb->giveHeroArtifact(val1,h->id,-2);
  2822. InfoWindow iw;
  2823. iw.soundID = soundBase::treasure;
  2824. iw.player = h->tempOwner;
  2825. iw.components.push_back(Component(4,val1,1,0));
  2826. iw.text << std::pair<ui8,ui32>(11,145);
  2827. iw.text.addReplacement(MetaString::ART_NAMES, val1);
  2828. cb->showInfoDialog(&iw);
  2829. break;
  2830. }
  2831. else
  2832. {
  2833. BlockingDialog sd(false,true);
  2834. sd.player = h->tempOwner;
  2835. sd.text << std::pair<ui8,ui32>(11,146);
  2836. sd.components.push_back(Component(2,6,val1,0));
  2837. sd.components.push_back(Component(5,0,val2,0));
  2838. sd.soundID = soundBase::chest;
  2839. boost::function<void(ui32)> fun = boost::bind(&CGPickable::chosen,this,_1,h->id);
  2840. cb->showBlockingDialog(&sd,fun);
  2841. return;
  2842. }
  2843. }
  2844. }
  2845. cb->removeObject(id);
  2846. }
  2847. void CGPickable::chosen( int which, int heroID ) const
  2848. {
  2849. switch(which)
  2850. {
  2851. case 1: //player pick gold
  2852. cb->giveResource(cb->getOwner(heroID),6,val1);
  2853. break;
  2854. case 2: //player pick exp
  2855. cb->changePrimSkill(heroID, 4, val2);
  2856. break;
  2857. default:
  2858. throw std::string("Unhandled treasure choice");
  2859. }
  2860. cb->removeObject(id);
  2861. }
  2862. bool CQuest::checkQuest (const CGHeroInstance * h) const
  2863. {
  2864. switch (missionType)
  2865. {
  2866. case MISSION_NONE:
  2867. return true;
  2868. break;
  2869. case MISSION_LEVEL:
  2870. if (m13489val <= h->level)
  2871. return true;
  2872. return false;
  2873. break;
  2874. case MISSION_PRIMARY_STAT:
  2875. for (int i = 0; i < 4; ++i)
  2876. {
  2877. if (m2stats[i] < h->primSkills[i])
  2878. return false;
  2879. }
  2880. return true;
  2881. break;
  2882. case MISSION_KILL_HERO:
  2883. if (h->cb->getHero (m13489val)) //first we must check if the hero was ever alive
  2884. return false; //if the pointer is not NULL
  2885. return true;
  2886. break;
  2887. case MISSION_KILL_CREATURE:
  2888. if (h->cb->getObj (m13489val))
  2889. return false; //if the pointer is not NULL
  2890. return true;
  2891. break;
  2892. case MISSION_ART:
  2893. for (int i = 0; i < m5arts.size(); ++i)
  2894. {
  2895. if (vstd::contains(h->artifacts, m5arts[i]))
  2896. continue;
  2897. if (vstd::contains(h->artifWorn, m5arts[i]))
  2898. continue;
  2899. return false; //if the artifact was not found
  2900. }
  2901. return true;
  2902. break;
  2903. case MISSION_ARMY:
  2904. {
  2905. TSlots::const_iterator it, cre;
  2906. ui32 count;
  2907. for (cre = m6creatures.begin(); cre != m6creatures.end(); ++cre)
  2908. {
  2909. for (count = 0, it = h->army.slots.begin(); it != h->army.slots.end(); ++it)
  2910. {
  2911. if (it->second.first == cre->second.first)
  2912. count += it->second.second;
  2913. }
  2914. if (count < cre->second.second) //not enough creatures of this kind
  2915. return false;
  2916. }
  2917. }
  2918. return true;
  2919. break;
  2920. case MISSION_RESOURCES:
  2921. for (int i = 0; i < 7; ++i) //including Mithril ?
  2922. { //Quest has no direct access to callback
  2923. if (h->cb->getResource (h->tempOwner, i) < m7resources[i])
  2924. return false;
  2925. }
  2926. return true;
  2927. break;
  2928. case MISSION_HERO:
  2929. if (m13489val == h->ID)
  2930. return true;
  2931. return false;
  2932. break;
  2933. case MISSION_PLAYER:
  2934. if (m13489val == h->getOwner())
  2935. return true;
  2936. return false;
  2937. break;
  2938. default:
  2939. return false;
  2940. }
  2941. }
  2942. void CGSeerHut::initObj()
  2943. {
  2944. seerName = VLC->generaltexth->seerNames[ran()%VLC->generaltexth->seerNames.size()];
  2945. textOption = ran()%3;
  2946. progress = 0;
  2947. firstVisitText = VLC->generaltexth->quests[missionType][0][textOption];
  2948. nextVisitText = VLC->generaltexth->quests[missionType][1][textOption];
  2949. completedText = VLC->generaltexth->quests[missionType][2][textOption];
  2950. }
  2951. const std::string & CGSeerHut::getHoverText() const
  2952. {
  2953. hoverName = VLC->generaltexth->allTexts[347];
  2954. boost::algorithm::replace_first(hoverName,"%s", seerName);
  2955. return hoverName;
  2956. }
  2957. void CGSeerHut::setPropertyDer (ui8 what, ui32 val)
  2958. {
  2959. switch (what)
  2960. {
  2961. case 10:
  2962. progress = val;
  2963. break;
  2964. case 11:
  2965. missionType = CQuest::MISSION_NONE;
  2966. break;
  2967. }
  2968. }
  2969. void CGSeerHut::onHeroVisit( const CGHeroInstance * h ) const
  2970. {
  2971. InfoWindow iw;
  2972. iw.player = h->getOwner();
  2973. if (missionType)
  2974. {
  2975. if (!progress)
  2976. {
  2977. iw.text << firstVisitText;
  2978. cb->setObjProperty (id,10,1);
  2979. }
  2980. else
  2981. if (!checkQuest(h))
  2982. iw.text << nextVisitText;
  2983. else
  2984. {
  2985. //iw.text << completedText;
  2986. completeQuest (h);
  2987. return;
  2988. }
  2989. }
  2990. else
  2991. {
  2992. iw.text << VLC->generaltexth->seerEmpty[textOption];
  2993. iw.text.addReplacement(seerName);
  2994. }
  2995. cb->showInfoDialog(&iw);
  2996. }
  2997. void CGSeerHut::completeQuest (const CGHeroInstance * h) const
  2998. {
  2999. cb->setObjProperty (id,11,0); //no more mission avaliable
  3000. }
  3001. void CGQuestGuard::initObj()
  3002. {
  3003. progress = 0;
  3004. textOption = ran()%3 + 3; //3-5
  3005. firstVisitText = VLC->generaltexth->quests[missionType][0][textOption];
  3006. nextVisitText = VLC->generaltexth->quests[missionType][1][textOption];
  3007. completedText = VLC->generaltexth->quests[missionType][2][textOption];
  3008. }
  3009. const std::string & CGQuestGuard::getHoverText() const
  3010. {
  3011. hoverName = VLC->generaltexth->names[ID];
  3012. if (progress){}//what does it seek for?
  3013. return hoverName;
  3014. }
  3015. void CGQuestGuard::onHeroVisit( const CGHeroInstance * h ) const
  3016. {}
  3017. void CGQuestGuard::completeQuest (const CGHeroInstance * h) const
  3018. {
  3019. BlockingDialog bd (true, false);
  3020. bd.player = h->getOwner();
  3021. bd.soundID = soundBase::QUEST;
  3022. bd.text << completedText;
  3023. cb->showBlockingDialog (&bd, boost::bind (&CGQuestGuard::openGate, this, h, _1));
  3024. }
  3025. void CGQuestGuard::openGate(const CGHeroInstance *h, ui32 accept) const
  3026. {
  3027. if (accept)
  3028. cb->removeObject(id);
  3029. }
  3030. void CGWitchHut::initObj()
  3031. {
  3032. ability = allowedAbilities[ran()%allowedAbilities.size()];
  3033. }
  3034. void CGWitchHut::onHeroVisit( const CGHeroInstance * h ) const
  3035. {
  3036. InfoWindow iw;
  3037. iw.soundID = soundBase::gazebo;
  3038. iw.player = h->getOwner();
  3039. if(!hasVisited(h->tempOwner))
  3040. cb->setObjProperty(id,10,h->tempOwner);
  3041. if(h->getSecSkillLevel(ability)) //you alredy know this skill
  3042. {
  3043. iw.text << std::pair<ui8,ui32>(11,172);
  3044. iw.text.addReplacement(MetaString::SEC_SKILL_NAME, ability);
  3045. }
  3046. else if(h->secSkills.size() >= SKILL_PER_HERO) //already all skills slots used
  3047. {
  3048. iw.text << std::pair<ui8,ui32>(11,173);
  3049. iw.text.addReplacement(MetaString::SEC_SKILL_NAME, ability);
  3050. }
  3051. else //give sec skill
  3052. {
  3053. iw.components.push_back(Component(1, ability, 1, 0));
  3054. iw.text << std::pair<ui8,ui32>(11,171);
  3055. iw.text.addReplacement(MetaString::SEC_SKILL_NAME, ability);
  3056. cb->changeSecSkill(h->id,ability,1,true);
  3057. }
  3058. cb->showInfoDialog(&iw);
  3059. }
  3060. const std::string & CGWitchHut::getHoverText() const
  3061. {
  3062. hoverName = VLC->generaltexth->names[ID];
  3063. if(hasVisited(cb->getCurrentPlayer())) //TODO: use local player, not current
  3064. {
  3065. hoverName += "\n" + VLC->generaltexth->allTexts[356]; // + (learn %s)
  3066. boost::algorithm::replace_first(hoverName,"%s",VLC->generaltexth->skillName[ability]);
  3067. const CGHeroInstance *h = cb->getSelectedHero(cb->getCurrentPlayer());
  3068. if(h && h->getSecSkillLevel(ability)) //hero knows that ability
  3069. hoverName += "\n\n" + VLC->generaltexth->allTexts[357]; // (Already learned)
  3070. }
  3071. return hoverName;
  3072. }
  3073. void CGBonusingObject::onHeroVisit( const CGHeroInstance * h ) const
  3074. {
  3075. bool visited = h->getBonus(HeroBonus::OBJECT,ID);
  3076. int messageID, bonusType, bonusVal;
  3077. int bonusMove = 0, sound = -1;
  3078. InfoWindow iw;
  3079. iw.player = h->tempOwner;
  3080. GiveBonus gbonus;
  3081. gbonus.hid = h->id;
  3082. gbonus.bonus.duration = HeroBonus::ONE_BATTLE;
  3083. gbonus.bonus.source = HeroBonus::OBJECT;
  3084. gbonus.bonus.id = ID;
  3085. switch(ID)
  3086. {
  3087. case 11: //buoy
  3088. messageID = 21;
  3089. sound = soundBase::MORALE;
  3090. gbonus.bonus.type = HeroBonus::MORALE;
  3091. gbonus.bonus.val = +1;
  3092. gbonus.bdescr << std::pair<ui8,ui32>(6,94);
  3093. break;
  3094. case 14: //swan pond
  3095. messageID = 29;
  3096. sound = soundBase::LUCK;
  3097. gbonus.bonus.type = HeroBonus::LUCK;
  3098. gbonus.bonus.val = 2;
  3099. gbonus.bdescr << std::pair<ui8,ui32>(6,67);
  3100. bonusMove = -h->movement;
  3101. break;
  3102. case 28: //Faerie Ring
  3103. messageID = 49;
  3104. sound = soundBase::LUCK;
  3105. gbonus.bonus.type = HeroBonus::LUCK;
  3106. gbonus.bonus.val = 1;
  3107. gbonus.bdescr << std::pair<ui8,ui32>(6,71);
  3108. break;
  3109. case 30: //fountain of fortune
  3110. messageID = 55;
  3111. sound = soundBase::LUCK;
  3112. gbonus.bonus.type = HeroBonus::LUCK;
  3113. gbonus.bonus.val = rand()%5 - 1;
  3114. gbonus.bdescr << std::pair<ui8,ui32>(6,69);
  3115. gbonus.bdescr.addReplacement((gbonus.bonus.val<0 ? "-" : "+") + boost::lexical_cast<std::string>(gbonus.bonus.val));
  3116. break;
  3117. case 38: //idol of fortune
  3118. messageID = 62;
  3119. sound = soundBase::experience;
  3120. if(cb->getDate(1) == 7) //7th day of week
  3121. gbonus.bonus.type = HeroBonus::MORALE_AND_LUCK;
  3122. else
  3123. gbonus.bonus.type = (cb->getDate(1)%2) ? HeroBonus::LUCK : HeroBonus::MORALE;
  3124. gbonus.bonus.val = 1;
  3125. gbonus.bdescr << std::pair<ui8,ui32>(6,68);
  3126. break;
  3127. case 52: //Mermaid
  3128. messageID = 83;
  3129. sound = soundBase::LUCK;
  3130. gbonus.bonus.type = HeroBonus::LUCK;
  3131. gbonus.bonus.val = 1;
  3132. gbonus.bdescr << std::pair<ui8,ui32>(6,72);
  3133. break;
  3134. case 64: //Rally Flag
  3135. sound = soundBase::MORALE;
  3136. messageID = 111;
  3137. gbonus.bonus.type = HeroBonus::MORALE_AND_LUCK;
  3138. gbonus.bonus.val = 1;
  3139. gbonus.bdescr << std::pair<ui8,ui32>(6,102);
  3140. bonusMove = 400;
  3141. break;
  3142. case 56: //oasis
  3143. messageID = 95;
  3144. gbonus.bonus.type = HeroBonus::MORALE;
  3145. gbonus.bonus.val = 1;
  3146. gbonus.bdescr << std::pair<ui8,ui32>(6,95);
  3147. bonusMove = 800;
  3148. break;
  3149. case 96: //temple
  3150. messageID = 140;
  3151. iw.soundID = soundBase::temple;
  3152. gbonus.bonus.type = HeroBonus::MORALE;
  3153. if(cb->getDate(1)==7) //sunday
  3154. {
  3155. gbonus.bonus.val = 2;
  3156. gbonus.bdescr << std::pair<ui8,ui32>(6,97);
  3157. }
  3158. else
  3159. {
  3160. gbonus.bonus.val = 1;
  3161. gbonus.bdescr << std::pair<ui8,ui32>(6,96);
  3162. }
  3163. break;
  3164. case 110://Watering Hole
  3165. sound = soundBase::MORALE;
  3166. messageID = 166;
  3167. gbonus.bonus.type = HeroBonus::MORALE;
  3168. gbonus.bonus.val = 1;
  3169. gbonus.bdescr << std::pair<ui8,ui32>(6,100);
  3170. bonusMove = 400;
  3171. break;
  3172. case 31: //Fountain of Youth
  3173. sound = soundBase::MORALE;
  3174. messageID = 57;
  3175. gbonus.bonus.type = HeroBonus::MORALE;
  3176. gbonus.bonus.val = 1;
  3177. gbonus.bdescr << std::pair<ui8,ui32>(6,103);
  3178. bonusMove = 400;
  3179. break;
  3180. case 94: //Stables TODO: upgrade Cavaliers
  3181. sound = soundBase::horse20;
  3182. std::set<ui32> slots;
  3183. for (std::map<si32,std::pair<ui32,si32> >::const_iterator i = h->army.slots.begin(); i != h->army.slots.end(); ++i)
  3184. {
  3185. if(i->second.first == 10)
  3186. slots.insert(i->first);
  3187. }
  3188. if (!slots.empty())
  3189. {
  3190. for (std::set<ui32>::const_iterator i = slots.begin(); i != slots.end(); i++)
  3191. {
  3192. UpgradeCreature uc(*i, id, 11);
  3193. //uc.applyGh (&gh);
  3194. }
  3195. }
  3196. messageID = 137;
  3197. gbonus.bonus.type = HeroBonus::LAND_MOVEMENT;
  3198. gbonus.bonus.val = 600;
  3199. gbonus.bdescr << std::pair<ui8,ui32>(6, 100);
  3200. break;
  3201. }
  3202. if(visited)
  3203. {
  3204. if(ID==64 || ID==96 || ID==56 || ID==52 || ID==94)
  3205. messageID--;
  3206. else
  3207. messageID++;
  3208. }
  3209. else
  3210. {
  3211. if(gbonus.bonus.type == HeroBonus::MORALE || gbonus.bonus.type == HeroBonus::MORALE_AND_LUCK)
  3212. iw.components.push_back(Component(8,0,gbonus.bonus.val,0));
  3213. if(gbonus.bonus.type == HeroBonus::LUCK || gbonus.bonus.type == HeroBonus::MORALE_AND_LUCK)
  3214. iw.components.push_back(Component(9,0,gbonus.bonus.val,0));
  3215. cb->giveHeroBonus(&gbonus);
  3216. if(bonusMove) //swan pond - take all move points
  3217. {
  3218. SetMovePoints smp;
  3219. smp.hid = h->id;
  3220. smp.val = h->movement + bonusMove;
  3221. cb->setMovePoints(&smp);
  3222. }
  3223. }
  3224. iw.soundID = sound;
  3225. iw.text << std::pair<ui8,ui32>(11,messageID);
  3226. cb->showInfoDialog(&iw);
  3227. }
  3228. const std::string & CGBonusingObject::getHoverText() const
  3229. {
  3230. const CGHeroInstance *h = cb->getSelectedHero(cb->getCurrentPlayer());
  3231. hoverName = VLC->generaltexth->names[ID];
  3232. if(h)
  3233. {
  3234. if(!h->getBonus(HeroBonus::OBJECT,ID))
  3235. hoverName += " " + VLC->generaltexth->allTexts[353]; //not visited
  3236. else
  3237. hoverName += " " + VLC->generaltexth->allTexts[352]; //visited
  3238. }
  3239. return hoverName;
  3240. }
  3241. void CGBonusingObject::initObj()
  3242. {
  3243. if(ID == 11 || ID == 52) //Buoy / Mermaid
  3244. {
  3245. blockVisit = true;
  3246. }
  3247. }
  3248. void CGMagicSpring::onHeroVisit(const CGHeroInstance * h) const
  3249. {
  3250. int messageID;
  3251. InfoWindow iw;
  3252. iw.player = h->tempOwner;
  3253. iw.soundID = soundBase::GENIE;
  3254. if (!visited)
  3255. {
  3256. if (h->mana > h->manaLimit())
  3257. messageID = 76;
  3258. else
  3259. {
  3260. messageID = 74;
  3261. cb->setManaPoints (h->id, 2 * h->manaLimit());
  3262. cb->setObjProperty (id, 5, true);
  3263. }
  3264. }
  3265. else
  3266. messageID = 75;
  3267. iw.text << std::pair<ui8,ui32>(11,messageID);
  3268. cb->showInfoDialog(&iw);
  3269. }
  3270. const std::string & CGMagicSpring::getHoverText() const
  3271. {
  3272. hoverName = VLC->generaltexth->names[ID];
  3273. if(!visited)
  3274. hoverName += " " + VLC->generaltexth->allTexts[353]; //not visited
  3275. else
  3276. hoverName += " " + VLC->generaltexth->allTexts[352]; //visited
  3277. return hoverName;
  3278. }
  3279. void CGMagicWell::onHeroVisit( const CGHeroInstance * h ) const
  3280. {
  3281. int message;
  3282. InfoWindow iw;
  3283. iw.soundID = soundBase::faerie;
  3284. iw.player = h->tempOwner;
  3285. if(h->getBonus(HeroBonus::OBJECT,ID)) //has already visited Well today
  3286. {
  3287. message = 78;
  3288. }
  3289. else if(h->mana < h->manaLimit())
  3290. {
  3291. giveDummyBonus(h->id);
  3292. cb->setManaPoints(h->id,h->manaLimit());
  3293. message = 77;
  3294. }
  3295. else
  3296. {
  3297. message = 79;
  3298. }
  3299. iw.text << std::pair<ui8,ui32>(11,message); //"A second drink at the well in one day will not help you."
  3300. cb->showInfoDialog(&iw);
  3301. }
  3302. const std::string & CGMagicWell::getHoverText() const
  3303. {
  3304. getNameVis(hoverName);
  3305. return hoverName;
  3306. }
  3307. void CGPandoraBox::initObj()
  3308. {
  3309. blockVisit = (ID==6); //block only if it's really pandora's box (events also derive from that class)
  3310. }
  3311. void CGPandoraBox::onHeroVisit(const CGHeroInstance * h) const
  3312. {
  3313. BlockingDialog bd (true, false);
  3314. bd.player = h->getOwner();
  3315. bd.soundID = soundBase::QUEST;
  3316. bd.text.addTxt (MetaString::ADVOB_TXT, 14);
  3317. cb->showBlockingDialog (&bd, boost::bind (&CGPandoraBox::open, this, h, _1));
  3318. }
  3319. void CGPandoraBox::open( const CGHeroInstance * h, ui32 accept ) const
  3320. {
  3321. if (accept)
  3322. {
  3323. if (army.slots.size() > 0) //if pandora's box is protected by army
  3324. {
  3325. InfoWindow iw;
  3326. iw.player = h->tempOwner;
  3327. iw.text.addTxt(MetaString::ADVOB_TXT, 16);
  3328. cb->showInfoDialog(&iw);
  3329. cb->startBattleI(h, this, boost::bind(&CGPandoraBox::endBattle, this, h, _1)); //grants things after battle
  3330. }
  3331. else if (message.size() == 0 && resources.size() == 0
  3332. && primskills.size() == 0 && abilities.size() == 0
  3333. && abilityLevels.size() == 0 && artifacts.size() == 0
  3334. && spells.size() == 0 && creatures.slots.size() > 0
  3335. && gainedExp == 0 && manaDiff == 0 && moraleDiff == 0 && luckDiff == 0) //if it gives nothing without battle
  3336. {
  3337. InfoWindow iw;
  3338. iw.player = h->tempOwner;
  3339. iw.text.addTxt(MetaString::ADVOB_TXT, 15);
  3340. cb->showInfoDialog(&iw);
  3341. }
  3342. else //if it gives something without battle
  3343. {
  3344. giveContents (h, false);
  3345. }
  3346. }
  3347. }
  3348. void CGPandoraBox::endBattle( const CGHeroInstance *h, BattleResult *result ) const
  3349. {
  3350. if(result->winner)
  3351. return;
  3352. giveContents(h,true);
  3353. }
  3354. void CGPandoraBox::giveContents( const CGHeroInstance *h, bool afterBattle ) const
  3355. {
  3356. InfoWindow iw;
  3357. iw.player = h->getOwner();
  3358. bool changesPrimSkill = false;
  3359. for (int i = 0; i < primskills.size(); i++)
  3360. {
  3361. if(primskills[i])
  3362. {
  3363. changesPrimSkill = true;
  3364. break;
  3365. }
  3366. }
  3367. if(gainedExp || changesPrimSkill || abilities.size())
  3368. {
  3369. getText(iw,afterBattle,175,h);
  3370. if(gainedExp)
  3371. iw.components.push_back(Component(Component::EXPERIENCE,0,gainedExp,0));
  3372. for(int i=0; i<primskills.size(); i++)
  3373. if(primskills[i])
  3374. iw.components.push_back(Component(Component::PRIM_SKILL,i,primskills[i],0));
  3375. for(int i=0; i<abilities.size(); i++)
  3376. iw.components.push_back(Component(Component::SEC_SKILL,abilities[i],abilityLevels[i],0));
  3377. cb->showInfoDialog(&iw);
  3378. //give exp
  3379. if(gainedExp)
  3380. cb->changePrimSkill(h->id,4,gainedExp,false);
  3381. //give prim skills
  3382. for(int i=0; i<primskills.size(); i++)
  3383. if(primskills[i])
  3384. cb->changePrimSkill(h->id,i,primskills[i],false);
  3385. //give sec skills
  3386. for(int i=0; i<abilities.size(); i++)
  3387. {
  3388. int curLev = h->getSecSkillLevel(abilities[i]);
  3389. if(curLev && curLev < abilityLevels[i]
  3390. || h->secSkills.size() < SKILL_PER_HERO )
  3391. {
  3392. cb->changeSecSkill(h->id,abilities[i],abilityLevels[i],true);
  3393. }
  3394. }
  3395. }
  3396. if(spells.size())
  3397. {
  3398. std::set<ui32> spellsToGive;
  3399. iw.components.clear();
  3400. for(int i=0; i<spells.size(); i++)
  3401. {
  3402. iw.components.push_back(Component(Component::SPELL,spells[i],0,0));
  3403. spellsToGive.insert(spells[i]);
  3404. }
  3405. if(spellsToGive.size())
  3406. {
  3407. cb->changeSpells(h->id,true,spellsToGive);
  3408. cb->showInfoDialog(&iw);
  3409. }
  3410. }
  3411. if(manaDiff)
  3412. {
  3413. getText(iw,afterBattle,manaDiff,176,177,h);
  3414. iw.components.push_back(Component(Component::PRIM_SKILL,5,manaDiff,0));
  3415. cb->showInfoDialog(&iw);
  3416. cb->setManaPoints(h->id, h->mana + manaDiff);
  3417. }
  3418. if(moraleDiff)
  3419. {
  3420. getText(iw,afterBattle,moraleDiff,178,179,h);
  3421. iw.components.push_back(Component(Component::MORALE,0,moraleDiff,0));
  3422. cb->showInfoDialog(&iw);
  3423. GiveBonus gb;
  3424. gb.bonus = HeroBonus(HeroBonus::ONE_BATTLE,HeroBonus::MORALE,HeroBonus::OBJECT,moraleDiff,id,"");
  3425. gb.hid = h->id;
  3426. cb->giveHeroBonus(&gb);
  3427. }
  3428. if(luckDiff)
  3429. {
  3430. getText(iw,afterBattle,luckDiff,180,181,h);
  3431. iw.components.push_back(Component(Component::LUCK,0,luckDiff,0));
  3432. cb->showInfoDialog(&iw);
  3433. GiveBonus gb;
  3434. gb.bonus = HeroBonus(HeroBonus::ONE_BATTLE,HeroBonus::LUCK,HeroBonus::OBJECT,luckDiff,id,"");
  3435. gb.hid = h->id;
  3436. cb->giveHeroBonus(&gb);
  3437. }
  3438. iw.components.clear();
  3439. iw.text.clear();
  3440. for(int i=0; i<resources.size(); i++)
  3441. {
  3442. if(resources[i] < 0)
  3443. iw.components.push_back(Component(Component::RESOURCE,i,resources[i],0));
  3444. }
  3445. if(iw.components.size())
  3446. {
  3447. getText(iw,afterBattle,182,h);
  3448. cb->showInfoDialog(&iw);
  3449. }
  3450. iw.components.clear();
  3451. iw.text.clear();
  3452. for(int i=0; i<resources.size(); i++)
  3453. {
  3454. if(resources[i] > 0)
  3455. iw.components.push_back(Component(Component::RESOURCE,i,resources[i],0));
  3456. }
  3457. if(iw.components.size())
  3458. {
  3459. getText(iw,afterBattle,183,h);
  3460. cb->showInfoDialog(&iw);
  3461. }
  3462. iw.components.clear();
  3463. // getText(iw,afterBattle,183,h);
  3464. for(int i=0; i<artifacts.size(); i++)
  3465. {
  3466. iw.components.push_back(Component(Component::ARTIFACT,artifacts[i],0,0));
  3467. if(iw.components.size() >= 14)
  3468. {
  3469. cb->showInfoDialog(&iw);
  3470. iw.components.clear();
  3471. }
  3472. }
  3473. if(iw.components.size())
  3474. {
  3475. cb->showInfoDialog(&iw);
  3476. }
  3477. for(int i=0; i<resources.size(); i++)
  3478. if(resources[i])
  3479. cb->giveResource(h->getOwner(),i,resources[i]);
  3480. for(int i=0; i<artifacts.size(); i++)
  3481. cb->giveHeroArtifact(artifacts[i],h->id,-2);
  3482. iw.components.clear();
  3483. iw.text.clear();
  3484. if (creatures.slots.size())
  3485. { //this part is taken straight from creature bank
  3486. MetaString loot;
  3487. CCreatureSet ourArmy = creatures;
  3488. for (std::map<si32,std::pair<ui32,si32> >::const_iterator i = ourArmy.slots.begin(); i != ourArmy.slots.end(); i++)
  3489. { //build list of joined creatures
  3490. iw.components.push_back (Component(Component::CREATURE, i->second.first, i->second.second, 0));
  3491. loot << "%s";
  3492. if (i->second.second == 1)
  3493. loot.addReplacement (MetaString::CRE_SING_NAMES, i->second.first);
  3494. else
  3495. loot.addReplacement (MetaString::CRE_PL_NAMES, i->second.first);
  3496. }
  3497. if (ourArmy.slots.size() == 1 && ourArmy.slots.begin()->second.second == 1)
  3498. iw.text.addTxt (MetaString::ADVOB_TXT, 185);
  3499. else
  3500. iw.text.addTxt (MetaString::ADVOB_TXT, 186);
  3501. iw.text.addReplacement (loot.buildList());
  3502. iw.text.addReplacement (h->name);
  3503. cb->showInfoDialog(&iw);
  3504. cb->giveCreatures (id, h, ourArmy);
  3505. }
  3506. if(!afterBattle && message.size())
  3507. {
  3508. iw.text << message;
  3509. cb->showInfoDialog(&iw);
  3510. }
  3511. cb->removeObject(id);
  3512. }
  3513. void CGPandoraBox::getText( InfoWindow &iw, bool &afterBattle, int text, const CGHeroInstance * h ) const
  3514. {
  3515. if(afterBattle || !message.size())
  3516. {
  3517. iw.text.addTxt(MetaString::ADVOB_TXT,text);//%s has lost treasure.
  3518. iw.text.addReplacement(h->name);
  3519. }
  3520. else
  3521. {
  3522. iw.text << message;
  3523. afterBattle = true;
  3524. }
  3525. }
  3526. void CGPandoraBox::getText( InfoWindow &iw, bool &afterBattle, int val, int negative, int positive, const CGHeroInstance * h ) const
  3527. {
  3528. iw.components.clear();
  3529. iw.text.clear();
  3530. if(afterBattle || !message.size())
  3531. {
  3532. iw.text.addTxt(MetaString::ADVOB_TXT,val < 0 ? negative : positive); //%s's luck takes a turn for the worse / %s's luck increases
  3533. iw.text.addReplacement(h->name);
  3534. }
  3535. else
  3536. {
  3537. iw.text << message;
  3538. afterBattle = true;
  3539. }
  3540. }
  3541. void CGEvent::onHeroVisit( const CGHeroInstance * h ) const
  3542. {
  3543. if(!(availableFor & (1 << h->tempOwner)))
  3544. return;
  3545. if(cb->getPlayerSettings(h->tempOwner)->human)
  3546. {
  3547. if(humanActivate)
  3548. activated(h);
  3549. }
  3550. else if(computerActivate)
  3551. activated(h);
  3552. }
  3553. void CGEvent::activated( const CGHeroInstance * h ) const
  3554. {
  3555. if(army.slots.size() > 0)
  3556. {
  3557. InfoWindow iw;
  3558. iw.player = h->tempOwner;
  3559. if(message.size())
  3560. iw.text << message;
  3561. else
  3562. iw.text.addTxt(MetaString::ADVOB_TXT, 16);
  3563. cb->showInfoDialog(&iw);
  3564. cb->startBattleI(h, this, boost::bind(&CGEvent::endBattle,this,h,_1));
  3565. }
  3566. else
  3567. {
  3568. giveContents(h,false);
  3569. }
  3570. }
  3571. void CGObservatory::onHeroVisit( const CGHeroInstance * h ) const
  3572. {
  3573. InfoWindow iw;
  3574. iw.soundID = soundBase::LIGHTHOUSE;
  3575. iw.player = h->tempOwner;
  3576. iw.text.addTxt(MetaString::ADVOB_TXT,98 + (ID==60));
  3577. cb->showInfoDialog(&iw);
  3578. FoWChange fw;
  3579. fw.player = h->tempOwner;
  3580. fw.mode = 1;
  3581. cb->getTilesInRange(fw.tiles,pos,20,h->tempOwner,1);
  3582. cb->sendAndApply(&fw);
  3583. }
  3584. void CGShrine::onHeroVisit( const CGHeroInstance * h ) const
  3585. {
  3586. if(spell == 255)
  3587. {
  3588. tlog1 << "Not initialized shrine visited!\n";
  3589. return;
  3590. }
  3591. if(!hasVisited(h->tempOwner))
  3592. cb->setObjProperty(id,10,h->tempOwner);
  3593. InfoWindow iw;
  3594. iw.soundID = soundBase::temple;
  3595. iw.player = h->getOwner();
  3596. iw.text.addTxt(MetaString::ADVOB_TXT,127 + ID - 88);
  3597. iw.text.addTxt(MetaString::SPELL_NAME,spell);
  3598. iw.text << ".";
  3599. if(!h->getArt(17)) //no spellbook
  3600. {
  3601. iw.text.addTxt(MetaString::ADVOB_TXT,131);
  3602. }
  3603. else if(ID == 90 && !h->getSecSkillLevel(7)) //it's third level spell and hero doesn't have wisdom
  3604. {
  3605. iw.text.addTxt(MetaString::ADVOB_TXT,130);
  3606. }
  3607. else if(vstd::contains(h->spells,spell))//hero already knows the spell
  3608. {
  3609. iw.text.addTxt(MetaString::ADVOB_TXT,174);
  3610. }
  3611. else //give spell
  3612. {
  3613. std::set<ui32> spells;
  3614. spells.insert(spell);
  3615. cb->changeSpells(h->id,true,spells);
  3616. iw.components.push_back(Component(Component::SPELL,spell,0,0));
  3617. }
  3618. cb->showInfoDialog(&iw);
  3619. }
  3620. void CGShrine::initObj()
  3621. {
  3622. if(spell == 255) //spell not set
  3623. {
  3624. int level = ID-87;
  3625. std::vector<ui16> possibilities;
  3626. cb->getAllowedSpells (possibilities, level);
  3627. if(!possibilities.size())
  3628. {
  3629. tlog1 << "Error: cannot init shrine, no allowed spells!\n";
  3630. return;
  3631. }
  3632. spell = possibilities[ran() % possibilities.size()];
  3633. }
  3634. }
  3635. const std::string & CGShrine::getHoverText() const
  3636. {
  3637. hoverName = VLC->generaltexth->names[ID];
  3638. if(hasVisited(cb->getCurrentPlayer())) //TODO: use local player, not current
  3639. {
  3640. hoverName += "\n" + VLC->generaltexth->allTexts[355]; // + (learn %s)
  3641. boost::algorithm::replace_first(hoverName,"%s",VLC->spellh->spells[spell].name);
  3642. const CGHeroInstance *h = cb->getSelectedHero(cb->getCurrentPlayer());
  3643. if(h && vstd::contains(h->spells,spell)) //hero knows that ability
  3644. hoverName += "\n\n" + VLC->generaltexth->allTexts[354]; // (Already learned)
  3645. }
  3646. return hoverName;
  3647. }
  3648. void CGSignBottle::initObj()
  3649. {
  3650. //if no text is set than we pick random from the predefined ones
  3651. if(!message.size())
  3652. message = VLC->generaltexth->randsign[ran()%VLC->generaltexth->randsign.size()];
  3653. if(ID == 59)
  3654. {
  3655. blockVisit = true;
  3656. }
  3657. }
  3658. void CGSignBottle::onHeroVisit( const CGHeroInstance * h ) const
  3659. {
  3660. InfoWindow iw;
  3661. iw.soundID = soundBase::STORE;
  3662. iw.player = h->getOwner();
  3663. iw.text << message;
  3664. cb->showInfoDialog(&iw);
  3665. if(ID == 59)
  3666. cb->removeObject(id);
  3667. }
  3668. void CGScholar::giveAnyBonus( const CGHeroInstance * h ) const
  3669. {
  3670. }
  3671. void CGScholar::onHeroVisit( const CGHeroInstance * h ) const
  3672. {
  3673. int type = bonusType;
  3674. int bid = bonusID;
  3675. //check if the bonus if applicable, if not - give primary skill (always possible)
  3676. int ssl = h->getSecSkillLevel(bid); //current sec skill level, used if bonusType == 1
  3677. if((type == 1
  3678. && ((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)
  3679. || (type == 2 && (!h->getArt(17) || vstd::contains(h->spells, (ui32) bid)))) //hero doesn't have a spellbook or already knows the spell
  3680. {
  3681. type = 0;
  3682. bid = ran() % PRIMARY_SKILLS;
  3683. }
  3684. InfoWindow iw;
  3685. iw.soundID = soundBase::gazebo;
  3686. iw.player = h->getOwner();
  3687. iw.text.addTxt(MetaString::ADVOB_TXT,115);
  3688. switch (type)
  3689. {
  3690. case 0:
  3691. cb->changePrimSkill(h->id,bid,+1);
  3692. iw.components.push_back(Component(Component::PRIM_SKILL,bid,+1,0));
  3693. break;
  3694. case 1:
  3695. cb->changeSecSkill(h->id,bid,+1);
  3696. iw.components.push_back(Component(Component::SEC_SKILL,bid,ssl+1,0));
  3697. break;
  3698. case 2:
  3699. {
  3700. std::set<ui32> hlp;
  3701. hlp.insert(bid);
  3702. cb->changeSpells(h->id,true,hlp);
  3703. iw.components.push_back(Component(Component::SPELL,bid,0,0));
  3704. }
  3705. break;
  3706. default:
  3707. tlog1 << "Error: wrong bonustype (" << (int)type << ") for Scholar!\n";
  3708. return;
  3709. }
  3710. cb->showInfoDialog(&iw);
  3711. cb->removeObject(id);
  3712. }
  3713. void CGScholar::initObj()
  3714. {
  3715. blockVisit = true;
  3716. if(bonusType == 255)
  3717. {
  3718. bonusType = ran()%3;
  3719. switch(bonusType)
  3720. {
  3721. case 0:
  3722. bonusID = ran() % PRIMARY_SKILLS;
  3723. break;
  3724. case 1:
  3725. bonusID = ran() % SKILL_QUANTITY;
  3726. break;
  3727. case 2:
  3728. std::vector<ui16> possibilities;
  3729. for (int i = 1; i < 6; ++i)
  3730. cb->getAllowedSpells (possibilities, i);
  3731. bonusID = possibilities[ran() % possibilities.size()];
  3732. break;
  3733. }
  3734. }
  3735. }
  3736. void CGGarrison::onHeroVisit (const CGHeroInstance *h) const
  3737. {
  3738. if (h->tempOwner != tempOwner && army.slots.size() > 0) {
  3739. //TODO: Find a way to apply magic garrison effects in battle.
  3740. cb->startBattleI(h, this, boost::bind(&CGGarrison::fightOver, this, h, _1));
  3741. return;
  3742. }
  3743. //New owner.
  3744. if (h->tempOwner != tempOwner)
  3745. cb->setOwner(id, h->tempOwner);
  3746. cb->showGarrisonDialog(id, h->id, removableUnits, 0);
  3747. }
  3748. void CGGarrison::fightOver (const CGHeroInstance *h, BattleResult *result) const
  3749. {
  3750. if (result->winner == 0)
  3751. onHeroVisit(h);
  3752. }
  3753. ui8 CGGarrison::getPassableness() const
  3754. {
  3755. return 1<<tempOwner;
  3756. }
  3757. void CGOnceVisitable::onHeroVisit( const CGHeroInstance * h ) const
  3758. {
  3759. int sound = 0;
  3760. int txtid = -1;
  3761. switch(ID)
  3762. {
  3763. case 22: //Corpse
  3764. txtid = 37;
  3765. sound = soundBase::MYSTERY;
  3766. break;
  3767. case 39: //Lean To
  3768. sound = soundBase::GENIE;
  3769. txtid = 64;
  3770. break;
  3771. case 105://Wagon
  3772. sound = soundBase::GENIE;
  3773. txtid = 154;
  3774. break;
  3775. case 108:
  3776. break;
  3777. default:
  3778. tlog1 << "Error: Unknown object (" << ID <<") treated as CGOnceVisitable!\n";
  3779. return;
  3780. }
  3781. if(ID == 108)//Warrior's Tomb
  3782. {
  3783. //ask if player wants to search the Tomb
  3784. BlockingDialog bd(true, false);
  3785. sound = soundBase::GRAVEYARD;
  3786. bd.player = h->getOwner();
  3787. bd.text.addTxt(MetaString::ADVOB_TXT,161);
  3788. cb->showBlockingDialog(&bd,boost::bind(&CGOnceVisitable::searchTomb,this,h,_1));
  3789. return;
  3790. }
  3791. InfoWindow iw;
  3792. iw.soundID = sound;
  3793. iw.player = h->getOwner();
  3794. if(players.size()) //we have been already visited...
  3795. {
  3796. txtid++;
  3797. if(ID == 105) //wagon has extra text (for finding art) we need to omit
  3798. txtid++;
  3799. iw.text.addTxt(MetaString::ADVOB_TXT, txtid);
  3800. }
  3801. else //first visit - give bonus!
  3802. {
  3803. switch(artOrRes)
  3804. {
  3805. case 0: // first visit but empty
  3806. if (ID == 22) //Corpse
  3807. ++txtid;
  3808. else
  3809. txtid+=2;
  3810. iw.text.addTxt(MetaString::ADVOB_TXT, txtid);
  3811. break;
  3812. case 1: //art
  3813. iw.components.push_back(Component(Component::ARTIFACT,bonusType,0,0));
  3814. cb->giveHeroArtifact(bonusType,h->id,-2);
  3815. iw.text.addTxt(MetaString::ADVOB_TXT, txtid);
  3816. if (ID == 22) //Corpse
  3817. {
  3818. iw.text << "%s";
  3819. iw.text.addReplacement (MetaString::ART_NAMES, bonusType);
  3820. }
  3821. break;
  3822. case 2: //res
  3823. iw.text.addTxt(MetaString::ADVOB_TXT, txtid);
  3824. iw.components.push_back (Component(Component::RESOURCE, bonusType, bonusVal, 0));
  3825. cb->giveResource(h->getOwner(), bonusType, bonusVal);
  3826. break;
  3827. }
  3828. if(ID == 105 && artOrRes == 1)
  3829. {
  3830. iw.text.localStrings.back().second++;
  3831. iw.text.addReplacement(MetaString::ART_NAMES, bonusType);
  3832. }
  3833. }
  3834. cb->showInfoDialog(&iw);
  3835. cb->setObjProperty(id,10,h->getOwner());
  3836. }
  3837. const std::string & CGOnceVisitable::getHoverText() const
  3838. {
  3839. hoverName = VLC->generaltexth->names[ID] + " ";
  3840. hoverName += (hasVisited(cb->getCurrentPlayer())
  3841. ? (VLC->generaltexth->allTexts[352]) //visited
  3842. : ( VLC->generaltexth->allTexts[353])); //not visited
  3843. return hoverName;
  3844. }
  3845. void CGOnceVisitable::initObj()
  3846. {
  3847. switch(ID)
  3848. {
  3849. case 22: //Corpse
  3850. {
  3851. blockVisit = true;
  3852. int hlp = ran()%100;
  3853. if(hlp < 20)
  3854. {
  3855. artOrRes = 1;
  3856. bonusType = cb->getRandomArt (CArtifact::ART_TREASURE | CArtifact::ART_MINOR | CArtifact::ART_MAJOR);
  3857. }
  3858. else
  3859. {
  3860. artOrRes = 0;
  3861. }
  3862. }
  3863. break;
  3864. case 39: //Lean To
  3865. {
  3866. artOrRes = 2;
  3867. bonusType = ran()%6; //any basic resource without gold
  3868. bonusVal = ran()%4 + 1;
  3869. }
  3870. break;
  3871. case 108://Warrior's Tomb
  3872. {
  3873. artOrRes = 1;
  3874. int hlp = ran()%100;
  3875. if(hlp < 30)
  3876. bonusType = cb->getRandomArt (CArtifact::ART_TREASURE);
  3877. else if(hlp < 80)
  3878. bonusType = cb->getRandomArt (CArtifact::ART_MINOR);
  3879. else if(hlp < 95)
  3880. bonusType = cb->getRandomArt (CArtifact::ART_MAJOR);
  3881. else
  3882. bonusType = cb->getRandomArt (CArtifact::ART_RELIC);
  3883. }
  3884. break;
  3885. case 105://Wagon
  3886. {
  3887. int hlp = ran()%100;
  3888. if(hlp < 10)
  3889. {
  3890. artOrRes = 0; // nothing... :(
  3891. }
  3892. else if(hlp < 50) //minor or treasure art
  3893. {
  3894. artOrRes = 1;
  3895. bonusType = cb->getRandomArt (CArtifact::ART_TREASURE | CArtifact::ART_MINOR);
  3896. }
  3897. else //2 - 5 of non-gold resource
  3898. {
  3899. artOrRes = 2;
  3900. bonusType = ran()%6;
  3901. bonusVal = ran()%4 + 2;
  3902. }
  3903. }
  3904. break;
  3905. }
  3906. }
  3907. void CGOnceVisitable::searchTomb(const CGHeroInstance *h, ui32 accept) const
  3908. {
  3909. if(accept)
  3910. {
  3911. InfoWindow iw;
  3912. iw.player = h->getOwner();
  3913. iw.components.push_back(Component(Component::MORALE,0,-3,0));
  3914. if(players.size()) //we've been already visited, player found nothing
  3915. {
  3916. iw.text.addTxt(MetaString::ADVOB_TXT,163);
  3917. }
  3918. else //first visit - give artifact
  3919. {
  3920. iw.text.addTxt(MetaString::ADVOB_TXT,162);
  3921. iw.components.push_back(Component(Component::ARTIFACT,bonusType,0,0));
  3922. iw.text.addReplacement(MetaString::ART_NAMES, bonusType);
  3923. cb->giveHeroArtifact(bonusType,h->id,-2);
  3924. }
  3925. if(!h->getBonus(HeroBonus::OBJECT,ID)) //we don't have modifier from this object yet
  3926. {
  3927. //ruin morale
  3928. GiveBonus gb;
  3929. gb.hid = h->id;
  3930. gb.bonus = HeroBonus(HeroBonus::ONE_BATTLE,HeroBonus::MORALE,HeroBonus::OBJECT,-3,id,"");
  3931. gb.bdescr.addTxt(MetaString::ARRAY_TXT,104); //Warrior Tomb Visited -3
  3932. cb->giveHeroBonus(&gb);
  3933. }
  3934. cb->showInfoDialog(&iw);
  3935. cb->setObjProperty(id,10,h->getOwner());
  3936. }
  3937. }
  3938. void CBank::initObj()
  3939. {
  3940. switch (ID) //find apriopriate key
  3941. {
  3942. case 16: //bank
  3943. index = subID; break;
  3944. case 24: //derelict ship
  3945. index = 8; break;
  3946. case 25: //utopia
  3947. index = 10; break;
  3948. case 84: //crypt
  3949. index = 9; break;
  3950. case 85: //shipwreck
  3951. index = 7; break;
  3952. }
  3953. bc = NULL;
  3954. daycounter = 0;
  3955. multiplier = 1;
  3956. }
  3957. const std::string & CBank::getHoverText() const
  3958. {
  3959. hoverName = VLC->objh->creBanksNames[index];
  3960. if (bc == NULL)
  3961. hoverName += " " + VLC->generaltexth->allTexts[352];
  3962. else
  3963. hoverName += " " + VLC->generaltexth->allTexts[353];
  3964. return hoverName;
  3965. }
  3966. void CBank::reset(ui16 var1) //prevents desync
  3967. {
  3968. ui8 chance = 0;
  3969. for (ui8 i = 0; i < VLC->objh->banksInfo[index].size(); i++)
  3970. {
  3971. if (var1 < (chance += VLC->objh->banksInfo[index][i]->chance))
  3972. {
  3973. bc = VLC->objh->banksInfo[index][i];
  3974. break;
  3975. }
  3976. }
  3977. artifacts.clear();
  3978. }
  3979. void CBank::initialize() const
  3980. {
  3981. cb->setObjProperty (id, 14, ran()); //synchronous reset
  3982. for (ui8 i = 0; i <= 3; i++)
  3983. {
  3984. for (ui8 n = 0; n < bc->artifacts[i]; n++) //new function using proper randomization algorithm
  3985. {
  3986. switch (i)
  3987. {
  3988. case 0:
  3989. cb->setObjProperty(id, 18, cb->getRandomArt (CArtifact::ART_TREASURE));
  3990. break;
  3991. case 1:
  3992. cb->setObjProperty(id, 18, cb->getRandomArt (CArtifact::ART_MINOR));
  3993. break;
  3994. case 2:
  3995. cb->setObjProperty(id, 18, cb->getRandomArt (CArtifact::ART_MAJOR));
  3996. break;
  3997. case 3:
  3998. cb->setObjProperty(id, 18, cb->getRandomArt (CArtifact::ART_RELIC));
  3999. break;
  4000. }
  4001. }
  4002. }
  4003. }
  4004. void CBank::setPropertyDer (ui8 what, ui32 val)
  4005. /// random values are passed as arguments and processed identically on all clients
  4006. {
  4007. switch (what)
  4008. {
  4009. case 11: //daycounter
  4010. if (val == 0)
  4011. daycounter = 0;
  4012. else
  4013. daycounter++;
  4014. break;
  4015. case 12: //multiplier, in percent
  4016. multiplier = ((float)val)/100;
  4017. break;
  4018. case 13: //bank preset
  4019. bc = VLC->objh->banksInfo[index][val];
  4020. break;
  4021. case 14:
  4022. reset (val%100);
  4023. break;
  4024. case 15:
  4025. bc = NULL;
  4026. break;
  4027. case 16:
  4028. artifacts.clear();
  4029. break;
  4030. case 17: //set ArmedInstance army
  4031. {
  4032. int upgraded = 0;
  4033. if (val%100 < bc->upgradeChance) //once again anti-desync
  4034. upgraded = 1;
  4035. switch (bc->guards.size())
  4036. {
  4037. case 1:
  4038. for (int i = 0; i <= 4; i++)
  4039. army.setCreature (i, bc->guards[0].first + upgraded, bc->guards[0].second / 5 );
  4040. break;
  4041. case 4:
  4042. {
  4043. std::vector< std::pair <ui16, ui32> >::const_iterator it;
  4044. for (it = bc->guards.begin(); it != bc->guards.end(); it++)
  4045. {
  4046. int n = army.slots.size(); //debug
  4047. army.setCreature (n, it->first, it->second);
  4048. }
  4049. }
  4050. break;
  4051. default:
  4052. tlog2 << "Error: Unexpected army data: " << bc->guards.size() <<" items found";
  4053. return;
  4054. }
  4055. }
  4056. break;
  4057. case 18: //add Artifact
  4058. artifacts.push_back (val);
  4059. break;
  4060. }
  4061. }
  4062. void CBank::newTurn() const
  4063. {
  4064. if (bc == NULL)
  4065. {
  4066. if (cb->getDate(0) == 1)
  4067. initialize(); //initialize on first day
  4068. else if (daycounter >= 28 && (subID < 13 || subID > 16)) //no reset for Emissaries
  4069. {
  4070. initialize();
  4071. cb->setObjProperty (id, 11, 0); //daycounter 0
  4072. if (ID == 24 && cb->getDate(0) > 1)
  4073. cb->setObjProperty (id, 16, 0); //derelict ships are usable only once
  4074. }
  4075. else
  4076. cb->setObjProperty (id, 11, 1); //daycounter++
  4077. }
  4078. }
  4079. void CBank::onHeroVisit (const CGHeroInstance * h) const
  4080. {
  4081. if (bc != NULL)
  4082. {
  4083. int banktext = 0;
  4084. switch (ID)
  4085. {
  4086. case 16: //generic bank
  4087. banktext = 32;
  4088. break;
  4089. case 24:
  4090. banktext = 41;
  4091. break;
  4092. case 25: //utopia
  4093. banktext = 47;
  4094. break;
  4095. case 84: //crypt
  4096. banktext = 119;
  4097. break;
  4098. case 85: //shipwreck
  4099. banktext = 122;
  4100. break;
  4101. }
  4102. BlockingDialog bd (true, false);
  4103. bd.player = h->getOwner();
  4104. bd.soundID = soundBase::DANGER;
  4105. bd.text << VLC->generaltexth->advobtxt[banktext];
  4106. if (ID == 16)
  4107. bd.text.addReplacement (VLC->objh->creBanksNames[index]);
  4108. cb->showBlockingDialog (&bd, boost::bind (&CBank::fightGuards, this, h, _1));
  4109. }
  4110. else
  4111. {
  4112. InfoWindow iw;
  4113. iw.soundID = soundBase::GRAVEYARD;
  4114. iw.player = h->getOwner();
  4115. //if (ID == 16 || ID == 24 || ID == 25 || ID == 84)
  4116. iw.text << VLC->generaltexth->advobtxt[33];
  4117. iw.text.addReplacement (VLC->objh->creBanksNames[index]);
  4118. cb->showInfoDialog(&iw);
  4119. }
  4120. }
  4121. void CBank::fightGuards (const CGHeroInstance * h, ui32 accept) const
  4122. {
  4123. if (accept)
  4124. {
  4125. cb->setObjProperty (id, 17, ran()); //get army
  4126. cb->startBattleI (h, this, boost::bind (&CBank::endBattle, this, h, _1), true);
  4127. }
  4128. }
  4129. void CBank::endBattle (const CGHeroInstance *h, const BattleResult *result) const
  4130. {
  4131. if (result->winner == 0)
  4132. {
  4133. int textID = -1;
  4134. InfoWindow iw;
  4135. iw.player = h->getOwner();
  4136. MetaString loot;
  4137. switch (ID)
  4138. {
  4139. case 16: case 25:
  4140. textID = 34;
  4141. break;
  4142. case 24: //derelict ship
  4143. if (bc->resources.size() != 0)
  4144. textID = 43;
  4145. else
  4146. {
  4147. iw.components.push_back (Component (Component::MORALE, 0 , -2, 0));
  4148. GiveBonus gbonus;
  4149. gbonus.hid = h->id;
  4150. gbonus.bonus.duration = HeroBonus::ONE_BATTLE;
  4151. gbonus.bonus.source = HeroBonus::OBJECT;
  4152. gbonus.bonus.id = ID;
  4153. gbonus.bdescr << "\n" << VLC->generaltexth->arraytxt[ID];
  4154. gbonus.bonus.type = HeroBonus::MORALE;
  4155. gbonus.bonus.val = -2;
  4156. cb->giveHeroBonus(&gbonus);
  4157. textID = 42;
  4158. iw.components.push_back (Component (Component::MORALE, 0 , -2, 0));
  4159. }
  4160. break;
  4161. case 84: //Crypt
  4162. if (bc->resources.size() != 0)
  4163. textID = 121;
  4164. else
  4165. {
  4166. iw.components.push_back (Component (Component::MORALE, 0 , -1, 0));
  4167. GiveBonus gbonus;
  4168. gbonus.hid = h->id;
  4169. gbonus.bonus.duration = HeroBonus::ONE_BATTLE;
  4170. gbonus.bonus.source = HeroBonus::OBJECT;
  4171. gbonus.bonus.id = ID;
  4172. gbonus.bdescr << "\n" << VLC->generaltexth->arraytxt[ID];
  4173. gbonus.bonus.type = HeroBonus::MORALE;
  4174. gbonus.bonus.val = -1;
  4175. cb->giveHeroBonus(&gbonus);
  4176. textID = 120;
  4177. iw.components.push_back (Component (Component::MORALE, 0 , -1, 0));
  4178. }
  4179. break;
  4180. case 85: //shipwreck
  4181. if (bc->resources.size() != 0)
  4182. textID = 124;
  4183. else
  4184. {
  4185. textID = 123;
  4186. }
  4187. break;
  4188. }
  4189. //grant resources
  4190. for (int it = 0; it < bc->resources.size(); it++)
  4191. {
  4192. if (bc->resources[it] != 0)
  4193. {
  4194. iw.components.push_back (Component (Component::RESOURCE, it, bc->resources[it], 0));
  4195. loot << "%d %s";
  4196. loot.addReplacement (iw.components.back().val);
  4197. loot.addReplacement (MetaString::RES_NAMES, iw.components.back().subtype);
  4198. cb->giveResource (h->getOwner(), it, bc->resources[it]);
  4199. }
  4200. }
  4201. //grant artifacts
  4202. for (std::vector<ui32>::const_iterator it = artifacts.begin(); it != artifacts.end(); it++)
  4203. {
  4204. iw.components.push_back (Component (Component::ARTIFACT, *it, 0, 0));
  4205. loot << "%s";
  4206. loot.addReplacement (MetaString::ART_NAMES, *it);
  4207. cb->giveHeroArtifact (*it, h->id ,-2);
  4208. }
  4209. //display loot
  4210. if (!loot.message.empty())
  4211. {
  4212. if (textID == 34)
  4213. {
  4214. iw.text.addTxt(MetaString::ADVOB_TXT, 34);//Heaving defeated %s, you discover %s
  4215. iw.text.addReplacement (MetaString::CRE_PL_NAMES, result->casualties[1].begin()->first);
  4216. iw.text.addReplacement (loot.buildList());
  4217. }
  4218. else
  4219. iw.text.addTxt (MetaString::ADVOB_TXT, textID);
  4220. cb->showInfoDialog(&iw);
  4221. }
  4222. loot.clear();
  4223. iw.components.clear();
  4224. iw.text.clear();
  4225. //grant creatures
  4226. CCreatureSet ourArmy;
  4227. for (std::vector< std::pair <ui16, ui32> >::const_iterator it = bc->creatures.begin(); it != bc->creatures.end(); it++)
  4228. {
  4229. int slot = ourArmy.getSlotFor (it->first);
  4230. ourArmy.slots[slot].first = it->first;
  4231. ourArmy.slots[slot].second += it->second;
  4232. }
  4233. for (std::map<si32,std::pair<ui32,si32> >::const_iterator i = ourArmy.slots.begin(); i != ourArmy.slots.end(); i++)
  4234. {
  4235. iw.components.push_back (Component(Component::CREATURE, i->second.first, i->second.second, 0));
  4236. loot << "%s";
  4237. if (i->second.second == 1)
  4238. loot.addReplacement (MetaString::CRE_SING_NAMES, i->second.first);
  4239. else
  4240. loot.addReplacement (MetaString::CRE_PL_NAMES, i->second.first);
  4241. }
  4242. if (ourArmy.slots.size())
  4243. {
  4244. if (ourArmy.slots.size() == 1 && ourArmy.slots.begin()->second.second == 1)
  4245. iw.text.addTxt (MetaString::ADVOB_TXT, 185);
  4246. else
  4247. iw.text.addTxt (MetaString::ADVOB_TXT, 186);
  4248. iw.text.addReplacement (loot.buildList());
  4249. iw.text.addReplacement (h->name);
  4250. cb->showInfoDialog(&iw);
  4251. cb->giveCreatures (id, h, ourArmy);
  4252. }
  4253. cb->setObjProperty (id, 15, 0); //bc = NULL
  4254. }
  4255. else //in case of defeat
  4256. initialize();
  4257. }
  4258. void CGPyramid::initObj()
  4259. {
  4260. //would be nice to do that only once
  4261. if (!pyramidConfig.guards.size())
  4262. {
  4263. pyramidConfig.level = 1;
  4264. pyramidConfig.chance = 100;
  4265. pyramidConfig.upgradeChance = 0;
  4266. for (int i = 0; i < 2; ++i)
  4267. {
  4268. pyramidConfig.guards.push_back (std::pair <ui16, ui32>(116, 20));
  4269. pyramidConfig.guards.push_back (std::pair <ui16, ui32>(117, 10));
  4270. }
  4271. pyramidConfig.combatValue; //how hard are guards of this level
  4272. pyramidConfig.value; //overall value of given things
  4273. pyramidConfig.rewardDifficulty; //proportion of reward value to difficulty of guards; how profitable is this creature Bank config
  4274. pyramidConfig.easiest; //?!?
  4275. }
  4276. bc = &pyramidConfig;
  4277. std::vector<ui16> available;
  4278. cb->getAllowedSpells (available, 5);
  4279. spell = (available[rand()%available.size()]);
  4280. }
  4281. const std::string & CGPyramid::getHoverText() const
  4282. {
  4283. hoverName = VLC->generaltexth->names[ID];
  4284. if (bc == NULL)
  4285. hoverName += " " + VLC->generaltexth->allTexts[352];
  4286. else
  4287. hoverName += " " + VLC->generaltexth->allTexts[353];
  4288. return hoverName;
  4289. }
  4290. void CGPyramid::onHeroVisit (const CGHeroInstance * h) const
  4291. {
  4292. if (bc)
  4293. {
  4294. BlockingDialog bd (true, false);
  4295. bd.player = h->getOwner();
  4296. bd.soundID = soundBase::DANGER;
  4297. bd.text << VLC->generaltexth->advobtxt[105];
  4298. cb->showBlockingDialog (&bd, boost::bind (&CBank::fightGuards, this, h, _1));
  4299. }
  4300. else
  4301. {
  4302. InfoWindow iw;
  4303. iw.player = h->getOwner();
  4304. iw.text << VLC->generaltexth->advobtxt[107];
  4305. iw.components.push_back (Component (Component::LUCK, 0 , -2, 0));
  4306. GiveBonus gb;
  4307. gb.bonus = HeroBonus(HeroBonus::ONE_BATTLE,HeroBonus::LUCK,HeroBonus::OBJECT,-2,id,VLC->generaltexth->arraytxt[70]);
  4308. gb.hid = h->id;
  4309. cb->giveHeroBonus(&gb);
  4310. cb->showInfoDialog(&iw);
  4311. }
  4312. }
  4313. void CGPyramid::endBattle (const CGHeroInstance *h, const BattleResult *result) const
  4314. {
  4315. if (result->winner == 0)
  4316. {
  4317. InfoWindow iw;
  4318. iw.player = h->getOwner();
  4319. iw.text.addTxt (MetaString::ADVOB_TXT, 106);
  4320. iw.text.addTxt (MetaString::SPELL_NAME, spell);
  4321. if (!h->getArt(17))
  4322. iw.text.addTxt (MetaString::ADVOB_TXT, 109); //no spellbook
  4323. else if (h->getSecSkillLevel(7) < 3)
  4324. iw.text.addTxt (MetaString::ADVOB_TXT, 108); //no expert Wisdom
  4325. else
  4326. {
  4327. std::set<ui32> spells;
  4328. spells.insert (spell);
  4329. cb->changeSpells (h->id, true, spells);
  4330. iw.components.push_back(Component (Component::SPELL, spell, 0, 0));
  4331. }
  4332. cb->showInfoDialog(&iw);
  4333. cb->setObjProperty (id, 15, 0);
  4334. }
  4335. }
  4336. void CGKeys::setPropertyDer (ui8 what, ui32 val) //101-108 - enable key for player 1-8
  4337. {
  4338. if (what >= 101 && what <= (100 + PLAYER_LIMIT))
  4339. playerKeyMap.find(what-101)->second.insert(val);
  4340. }
  4341. bool CGKeys::wasMyColorVisited (int player) const
  4342. {
  4343. if (vstd::contains(playerKeyMap[player], subID)) //creates set if it's not there
  4344. return true;
  4345. else
  4346. return false;
  4347. }
  4348. const std::string & CGKeymasterTent::getHoverText() const
  4349. {
  4350. hoverName = VLC->generaltexth->names[ID];
  4351. if (wasMyColorVisited (cb->getCurrentPlayer()) )//TODO: use local player, not current
  4352. hoverName += "\n" + VLC->generaltexth->allTexts[352];
  4353. else
  4354. hoverName += "\n" + VLC->generaltexth->allTexts[353];
  4355. return hoverName;
  4356. }
  4357. void CGKeymasterTent::onHeroVisit( const CGHeroInstance * h ) const
  4358. {
  4359. InfoWindow iw;
  4360. iw.soundID = soundBase::CAVEHEAD;
  4361. iw.player = h->getOwner();
  4362. if (!wasMyColorVisited (h->getOwner()) )
  4363. {
  4364. cb->setObjProperty(id, h->tempOwner+101, subID);
  4365. iw.text << std::pair<ui8,ui32>(11,19);
  4366. }
  4367. else
  4368. iw.text << std::pair<ui8,ui32>(11,20);
  4369. cb->showInfoDialog(&iw);
  4370. }
  4371. void CGBorderGuard::initObj()
  4372. {
  4373. blockVisit = true;
  4374. }
  4375. const std::string & CGBorderGuard::getHoverText() const
  4376. {
  4377. hoverName = VLC->generaltexth->names[ID];
  4378. if (wasMyColorVisited (cb->getCurrentPlayer()) )//TODO: use local player, not current
  4379. hoverName += "\n" + VLC->generaltexth->allTexts[352];
  4380. else
  4381. hoverName += "\n" + VLC->generaltexth->allTexts[353];
  4382. return hoverName;
  4383. }
  4384. void CGBorderGuard::onHeroVisit( const CGHeroInstance * h ) const
  4385. {
  4386. if (wasMyColorVisited (h->getOwner()) )
  4387. {
  4388. BlockingDialog bd (true, false);
  4389. bd.player = h->getOwner();
  4390. bd.soundID = soundBase::QUEST;
  4391. bd.text.addTxt (MetaString::ADVOB_TXT, 17);
  4392. cb->showBlockingDialog (&bd, boost::bind (&CGBorderGuard::openGate, this, h, _1));
  4393. }
  4394. else
  4395. {
  4396. InfoWindow iw;
  4397. iw.player = h->getOwner();
  4398. iw.soundID = soundBase::CAVEHEAD;
  4399. iw.text << std::pair<ui8,ui32>(11,18);
  4400. cb->showInfoDialog (&iw);
  4401. }
  4402. }
  4403. void CGBorderGuard::openGate(const CGHeroInstance *h, ui32 accept) const
  4404. {
  4405. if (accept)
  4406. cb->removeObject(id);
  4407. }
  4408. void CGBorderGate::onHeroVisit( const CGHeroInstance * h ) const //TODO: passability
  4409. {
  4410. if (!wasMyColorVisited (h->getOwner()) )
  4411. {
  4412. InfoWindow iw;
  4413. iw.player = h->getOwner();
  4414. iw.text << std::pair<ui8,ui32>(11,18);
  4415. cb->showInfoDialog(&iw);
  4416. }
  4417. }
  4418. ui8 CGBorderGate::getPassableness() const
  4419. {
  4420. ui8 ret = 0;
  4421. for (int i = 0; i < PLAYER_LIMIT; i++)
  4422. ret |= wasMyColorVisited(i)<<i;
  4423. return ret;
  4424. }
  4425. void CGMagi::initObj()
  4426. {
  4427. if (ID == 27)
  4428. {
  4429. blockVisit = true;
  4430. eyelist[subID].push_back(id);
  4431. }
  4432. }
  4433. void CGMagi::onHeroVisit(const CGHeroInstance * h) const
  4434. {
  4435. if (ID == 37)
  4436. {
  4437. InfoWindow iw;
  4438. CenterView cv;
  4439. FoWChange fw;
  4440. cv.player = iw.player = fw.player = h->tempOwner;
  4441. iw.soundID = soundBase::LIGHTHOUSE;
  4442. iw.player = h->tempOwner;
  4443. iw.text.addTxt (MetaString::ADVOB_TXT, 61);
  4444. cb->showInfoDialog(&iw);
  4445. fw.mode = 1;
  4446. std::vector<si32>::iterator it;
  4447. for (it = eyelist[subID].begin(); it < eyelist[subID].end(); it++)
  4448. {
  4449. const CGObjectInstance *eye = cb->getObj(*it);
  4450. cb->getTilesInRange (fw.tiles, eye->pos, 5, h->tempOwner, 1);
  4451. cb->sendAndApply(&fw);
  4452. cv.pos = eye->pos;
  4453. cv.focusTime = 2000;
  4454. cb->sendAndApply(&cv);
  4455. }
  4456. cv.pos = h->getPosition(false);
  4457. cb->sendAndApply(&cv);
  4458. }
  4459. else if (ID == 27)
  4460. {
  4461. InfoWindow iw;
  4462. iw.player = h->tempOwner;
  4463. iw.text.addTxt (MetaString::ADVOB_TXT, 48);
  4464. cb->showInfoDialog(&iw);
  4465. }
  4466. }
  4467. void CGBoat::initObj()
  4468. {
  4469. hero = NULL;
  4470. }
  4471. void CGSirens::initObj()
  4472. {
  4473. blockVisit = true;
  4474. }
  4475. const std::string & CGSirens::getHoverText() const
  4476. {
  4477. getNameVis(hoverName);
  4478. return hoverName;
  4479. }
  4480. void CGSirens::onHeroVisit( const CGHeroInstance * h ) const
  4481. {
  4482. int message;
  4483. InfoWindow iw;
  4484. iw.soundID = soundBase::DANGER;
  4485. iw.player = h->tempOwner;
  4486. if(h->getBonus(HeroBonus::OBJECT,ID)) //has already visited Sirens
  4487. {
  4488. iw.text.addTxt(11,133);
  4489. }
  4490. else
  4491. {
  4492. giveDummyBonus(h->id, HeroBonus::ONE_BATTLE);
  4493. int xp = 0;
  4494. SetGarrisons sg;
  4495. sg.garrs[h->id] = h->army;
  4496. for (std::map<si32,std::pair<ui32,si32> >::const_iterator i = h->army.slots.begin(); i != h->army.slots.end(); i++)
  4497. {
  4498. int drown = (int)(i->second.second * 0.3);
  4499. if(drown)
  4500. {
  4501. sg.garrs[h->id].slots[i->first].second -= drown;
  4502. xp += drown * VLC->creh->creatures[i->second.first].hitPoints;
  4503. }
  4504. }
  4505. if(xp)
  4506. {
  4507. iw.text.addTxt(11,132);
  4508. iw.text.addReplacement(xp);
  4509. cb->sendAndApply(&sg);
  4510. }
  4511. else
  4512. {
  4513. iw.text.addTxt(11,134);
  4514. }
  4515. }
  4516. cb->showInfoDialog(&iw);
  4517. }
  4518. void IShipyard::getBoatCost( std::vector<si32> &cost ) const
  4519. {
  4520. cost.resize(RESOURCE_QUANTITY);
  4521. cost[0] = 10;
  4522. cost[6] = 1000;
  4523. }
  4524. //bool IShipyard::validLocation() const
  4525. //{
  4526. // std::vector<int3> offsets;
  4527. // getOutOffsets(offsets);
  4528. //
  4529. // TerrainTile *tile;
  4530. // for(int i = 0; i < offsets.size(); i++)
  4531. // if((tile = IObjectInterface::cb->getTile(o->pos + offsets[i])) && tile->tertype == TerrainTile::water) //tile is in the map and is water
  4532. // return true;
  4533. // return false;
  4534. //}
  4535. int3 IShipyard::bestLocation() const
  4536. {
  4537. std::vector<int3> offsets;
  4538. getOutOffsets(offsets);
  4539. TerrainTile *tile;
  4540. for(int i = 0; i < offsets.size(); i++)
  4541. if((tile = IObjectInterface::cb->getTile(o->pos + offsets[i])) && tile->tertype == TerrainTile::water) //tile is in the map and is water
  4542. return o->pos + offsets[i];
  4543. return int3(-1,-1,-1);
  4544. }
  4545. IShipyard::IShipyard(const CGObjectInstance *O)
  4546. : o(O)
  4547. {
  4548. }
  4549. int IShipyard::state() const
  4550. {
  4551. int3 tile = bestLocation();
  4552. TerrainTile *t = IObjectInterface::cb->getTile(tile);
  4553. if(!t)
  4554. return 3; //no water
  4555. else if(!t->blockingObjects.size())
  4556. return 0; //OK
  4557. else if(t->blockingObjects.front()->ID == 8)
  4558. return 1; //blocked with boat
  4559. else
  4560. return 2; //blocked
  4561. }
  4562. IShipyard * IShipyard::castFrom( CGObjectInstance *obj )
  4563. {
  4564. if(obj->ID == TOWNI_TYPE)
  4565. {
  4566. return static_cast<CGTownInstance*>(obj);
  4567. }
  4568. else if(obj->ID == 87)
  4569. {
  4570. return static_cast<CGShipyard*>(obj);
  4571. }
  4572. else
  4573. {
  4574. tlog1 << "Cannot cast to IShipyard object with ID " << obj->ID << std::endl;
  4575. return NULL;
  4576. }
  4577. }
  4578. const IShipyard * IShipyard::castFrom( const CGObjectInstance *obj )
  4579. {
  4580. return castFrom(const_cast<CGObjectInstance*>(obj));
  4581. }
  4582. CGShipyard::CGShipyard()
  4583. :IShipyard(this)
  4584. {
  4585. }
  4586. void CGShipyard::getOutOffsets( std::vector<int3> &offsets ) const
  4587. {
  4588. 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),
  4589. int3(-2,-1,0), int3(0,-1,0), int3(-1,-1,0), int3(-2,1,0), int3(0,1,0), int3(-1,1,0);
  4590. }
  4591. void CGShipyard::onHeroVisit( const CGHeroInstance * h ) const
  4592. {
  4593. if(tempOwner != h->tempOwner)
  4594. cb->setOwner(id, h->tempOwner);
  4595. int s = state();
  4596. if(s)
  4597. {
  4598. InfoWindow iw;
  4599. iw.player = tempOwner;
  4600. switch(s)
  4601. {
  4602. case 1:
  4603. iw.text.addTxt(MetaString::GENERAL_TXT, 51);
  4604. break;
  4605. case 2:
  4606. iw.text.addTxt(MetaString::ADVOB_TXT, 189);
  4607. break;
  4608. case 3:
  4609. tlog1 << "Shipyard without water!!! " << pos << "\t" << id << std::endl;
  4610. return;
  4611. }
  4612. cb->showInfoDialog(&iw);
  4613. }
  4614. else
  4615. {
  4616. OpenWindow ow;
  4617. ow.id1 = id;
  4618. ow.id2 = h->id;
  4619. ow.window = OpenWindow::SHIPYARD_WINDOW;
  4620. cb->sendAndApply(&ow);
  4621. }
  4622. }
  4623. void CCartographer::onHeroVisit( const CGHeroInstance * h ) const
  4624. {
  4625. if (!hasVisited (h->getOwner()) ) //if hero has not visited yet this cartographer
  4626. {
  4627. if (cb->getResource(h->tempOwner, 6) >= 1000) //if he can afford a map
  4628. {
  4629. //ask if he wants to buy one
  4630. int text;
  4631. switch (subID)
  4632. {
  4633. case 0:
  4634. text = 25;
  4635. break;
  4636. case 1:
  4637. text = 26;
  4638. break;
  4639. case 2:
  4640. text = 27;
  4641. break;
  4642. default:
  4643. tlog2 << "Unrecognized subtype of cartographer" << std::endl;
  4644. }
  4645. BlockingDialog bd (true, false);
  4646. bd.player = h->getOwner();
  4647. bd.soundID = soundBase::LIGHTHOUSE;
  4648. bd.text.addTxt (MetaString::ADVOB_TXT, text);
  4649. cb->showBlockingDialog (&bd, boost::bind (&CCartographer::buyMap, this, h, _1));
  4650. }
  4651. else //if he cannot afford
  4652. {
  4653. InfoWindow iw;
  4654. iw.player = h->getOwner();
  4655. iw.soundID = soundBase::CAVEHEAD;
  4656. iw.text << std::pair<ui8,ui32>(11,28);
  4657. cb->showInfoDialog (&iw);
  4658. }
  4659. }
  4660. else //if he already visited carographer
  4661. {
  4662. InfoWindow iw;
  4663. iw.player = h->getOwner();
  4664. iw.soundID = soundBase::CAVEHEAD;
  4665. iw.text << std::pair<ui8,ui32>(11,24);
  4666. cb->showInfoDialog (&iw);
  4667. }
  4668. }
  4669. void CCartographer::buyMap (const CGHeroInstance *h, ui32 accept) const
  4670. {
  4671. if (accept) //if hero wants to buy map
  4672. {
  4673. cb->giveResource (h->tempOwner, 6, -1000);
  4674. FoWChange fw;
  4675. fw.player = h->tempOwner;
  4676. //subIDs of different types of cartographers:
  4677. //water = 0; land = 1; underground = 2;
  4678. cb->getAllTiles (fw.tiles, h->tempOwner, subID - 1, !subID + 1); //reveal appropriate tiles
  4679. cb->sendAndApply (&fw);
  4680. cb->setObjProperty (id, 10, h->tempOwner);
  4681. }
  4682. }
  4683. void CShop::newTurn() const
  4684. {
  4685. switch (ID)
  4686. {
  4687. case 7: //ArtMerchant aka. Black Market
  4688. if (cb->getDate(0)%28 == 1)
  4689. {
  4690. cb->setObjProperty (id, 13, 0);
  4691. cb->setObjProperty (id, 14, rand());
  4692. }
  4693. break;
  4694. case 78: //Refugee Camp
  4695. case 95: //Tavern -- global hero pool?
  4696. if (cb->getDate(0)%7 == 1)
  4697. cb->setObjProperty (id, 14, rand());
  4698. break;
  4699. }
  4700. }
  4701. void CShop::setPropertyDer (ui8 what, ui32 val)
  4702. {
  4703. switch (what)
  4704. {
  4705. case 13: //sweep
  4706. available.clear();
  4707. chosen.clear();
  4708. bought.clear();
  4709. break;
  4710. case 14: //reset - get new items
  4711. reset(val);
  4712. break;
  4713. }
  4714. }
  4715. void CGArtMerchant::reset(ui32 val)
  4716. {
  4717. std::vector<CArtifact*>::iterator index;
  4718. for (ui8 i = 0; i < 4; ++i) //each tier
  4719. {
  4720. int count = 0;
  4721. std::vector<CArtifact*> arts; //to avoid addition of different tiers
  4722. switch (i)
  4723. {
  4724. case 0:
  4725. cb->getAllowed (arts, CArtifact::ART_TREASURE);
  4726. count = 2;
  4727. break;
  4728. case 1:
  4729. cb->getAllowed (arts, CArtifact::ART_MINOR);
  4730. count = 2;
  4731. break;
  4732. case 2:
  4733. cb->getAllowed (arts, CArtifact::ART_MAJOR);
  4734. count = 1;
  4735. break;
  4736. case 3:
  4737. cb->getAllowed (arts, CArtifact::ART_RELIC);
  4738. count = 1;
  4739. break;
  4740. }
  4741. for (ui8 n = 0; n < count; n++)
  4742. {
  4743. index = arts.begin() + val % arts.size();
  4744. available [available.size()] = new Component (Component::ARTIFACT, (*index)->id, 0, 0);
  4745. arts.erase(index);
  4746. val *= (id + n * i); //randomize
  4747. }
  4748. }
  4749. }
  4750. void CGRefugeeCamp::reset(ui32 val)
  4751. {
  4752. /*int creid = creh->creatures[val%creh->creatures.size()].idNumber;
  4753. VLC->creh->creatures[creatures[creid].second[0]].growth;
  4754. available[0] = new Component (Component::CREATURE, creid, 0, 0);
  4755. */
  4756. }