CObjectHandler.cpp 120 KB

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