CObjectHandler.cpp 148 KB

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