CObjectHandler.cpp 151 KB

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