CObjectHandler.cpp 146 KB

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