CObjectHandler.cpp 203 KB

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