CObjectHandler.cpp 123 KB

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