CObjectHandler.cpp 172 KB

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