CObjectHandler.cpp 147 KB

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