CObjectHandler.cpp 123 KB

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