CObjectHandler.cpp 149 KB

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