CObjectHandler.cpp 190 KB

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