CObjectHandler.cpp 114 KB

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