CObjectHandler.cpp 186 KB

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