CObjectHandler.cpp 117 KB

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