CObjectHandler.cpp 111 KB

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