CObjectHandler.cpp 146 KB

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