CObjectHandler.cpp 120 KB

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