CObjectHandler.cpp 162 KB

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