CObjectHandler.cpp 181 KB

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