CObjectHandler.cpp 147 KB

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