CObjectHandler.cpp 122 KB

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