CObjectHandler.cpp 114 KB

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