CObjectHandler.cpp 174 KB

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