CObjectHandler.cpp 170 KB

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