CObjectHandler.cpp 169 KB

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