CObjectHandler.cpp 103 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193
  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.first;
  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.second = 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.second = 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 = std::max(type->heroClass->terrCosts[dest.tertype],type->heroClass->terrCosts[from.tertype]); //take cost of worse of two tiles
  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. factions.insert(VLC->creh->creatures[i->second.first].faction);
  783. if(i->second.first == 13)
  784. archangelInArmy = true;
  785. }
  786. if(factions.size() == 1)
  787. ret.push_back(std::pair<int,std::string>(1,VLC->generaltexth->arraytxt[115])); //All troops of one alignment +1
  788. else
  789. {
  790. if(VLC->generaltexth->arraytxt[114].length() <= 100)
  791. {
  792. char buf[150];
  793. std::sprintf(buf,VLC->generaltexth->arraytxt[114].c_str(),factions.size(),2-factions.size());
  794. ret.push_back(std::pair<int,std::string>(2-factions.size(),buf)); //Troops of %d alignments %d
  795. }
  796. else
  797. {
  798. ret.push_back(std::pair<int,std::string>(2-factions.size(),"")); //Troops of %d alignments %d
  799. }
  800. }
  801. if(vstd::contains(factions,4))
  802. ret.push_back(std::pair<int,std::string>(-1,VLC->generaltexth->arraytxt[116])); //Undead in group -1
  803. if(archangelInArmy)
  804. {
  805. char buf[100];
  806. sprintf(buf,VLC->generaltexth->arraytxt[117].c_str(),VLC->creh->creatures[13].namePl.c_str());
  807. ret.push_back(std::pair<int,std::string>(1,buf)); //%s in group +1
  808. }
  809. }
  810. return ret;
  811. }
  812. int CGHeroInstance::getCurrentLuck( int stack/*=-1*/, bool town/*=false*/ ) const
  813. {
  814. int ret = 0;
  815. std::vector<std::pair<int,std::string> > mods = getCurrentLuckModifiers(stack,town);
  816. for(int i=0; i < mods.size(); i++)
  817. ret += mods[i].first;
  818. if(ret > 3)
  819. return 3;
  820. if(ret < -3)
  821. return -3;
  822. return ret;
  823. }
  824. std::vector<std::pair<int,std::string> > CGHeroInstance::getCurrentLuckModifiers( int stack/*=-1*/, bool town/*=false*/ ) const
  825. {
  826. std::vector<std::pair<int,std::string> > ret;
  827. //various morale bonuses (from buildings, artifacts, etc)
  828. for(std::list<HeroBonus>::const_iterator i=bonuses.begin(); i != bonuses.end(); i++)
  829. if(i->type == HeroBonus::LUCK || i->type == HeroBonus::MORALE_AND_LUCK)
  830. ret.push_back(std::make_pair(i->val, i->description));
  831. //luck skill
  832. if(getSecSkillLevel(9))
  833. ret.push_back(std::make_pair(getSecSkillLevel(9),VLC->generaltexth->arraytxt[73+getSecSkillLevel(9)]));
  834. return ret;
  835. }
  836. const HeroBonus * CGHeroInstance::getBonus( int from, int id ) const
  837. {
  838. for (std::list<HeroBonus>::const_iterator i=bonuses.begin(); i!=bonuses.end(); i++)
  839. if(i->source == from && i->id == id)
  840. return &*i;
  841. return NULL;
  842. }
  843. void CGHeroInstance::setPropertyDer( ui8 what, ui32 val )
  844. {
  845. if(what == 3)
  846. army.slots[0].second = val;
  847. }
  848. double CGHeroInstance::getHeroStrength() const
  849. {
  850. return sqrt((1.0 + 0.05*getPrimSkillLevel(0)) * (1.0 + 0.05*getPrimSkillLevel(1)));
  851. }
  852. int CGHeroInstance::getTotalStrength() const
  853. {
  854. double ret = getHeroStrength() * getArmyStrength();
  855. return (int) ret;
  856. }
  857. ui8 CGHeroInstance::getSpellSchoolLevel(const CSpell * spell) const
  858. {
  859. ui8 skill = 0; //skill level
  860. if(spell->fire)
  861. skill = std::max(skill,getSecSkillLevel(14));
  862. if(spell->air)
  863. skill = std::max(skill,getSecSkillLevel(15));
  864. if(spell->water)
  865. skill = std::max(skill,getSecSkillLevel(16));
  866. if(spell->earth)
  867. skill = std::max(skill,getSecSkillLevel(17));
  868. //bonuses (eg. from special terrains)
  869. skill = std::max<ui8>(skill, valOfBonuses(HeroBonus::MAGIC_SCHOOL_SKILL, 0)); //any school bonus
  870. if(spell->fire)
  871. skill = std::max<ui8>(skill, valOfBonuses(HeroBonus::MAGIC_SCHOOL_SKILL, 1));
  872. if(spell->air)
  873. skill = std::max<ui8>(skill, valOfBonuses(HeroBonus::MAGIC_SCHOOL_SKILL, 2));
  874. if(spell->water)
  875. skill = std::max<ui8>(skill, valOfBonuses(HeroBonus::MAGIC_SCHOOL_SKILL, 4));
  876. if(spell->earth)
  877. skill = std::max<ui8>(skill, valOfBonuses(HeroBonus::MAGIC_SCHOOL_SKILL, 8));
  878. return skill;
  879. }
  880. bool CGHeroInstance::canCastThisSpell(const CSpell * spell) const
  881. {
  882. if(!getArt(17)) //if hero has no spellbook
  883. return false;
  884. if(vstd::contains(spells, spell->id) //hero does not have this spell in spellbook
  885. || (spell->air && hasBonusOfType(HeroBonus::AIR_SPELLS)) // this is air spell and hero can cast all air spells
  886. || (spell->fire && hasBonusOfType(HeroBonus::FIRE_SPELLS)) // this is fire spell and hero can cast all fire spells
  887. || (spell->water && hasBonusOfType(HeroBonus::WATER_SPELLS)) // this is water spell and hero can cast all water spells
  888. || (spell->earth && hasBonusOfType(HeroBonus::EARTH_SPELLS)) // this is earth spell and hero can cast all earth spells
  889. )
  890. return true;
  891. for(std::list<HeroBonus>::const_iterator it = bonuses.begin(); it != bonuses.end(); ++it)
  892. {
  893. if(it->type == HeroBonus::SPELL && it->subtype == spell->id)
  894. {
  895. return true;
  896. }
  897. if(it->type == HeroBonus::SPELLS_OF_LEVEL && it->subtype == spell->level)
  898. {
  899. return true;
  900. }
  901. }
  902. return false;
  903. }
  904. int3 CGHeroInstance::getSightCenter() const
  905. {
  906. return getPosition(false);
  907. }
  908. int CGHeroInstance::getSightRadious() const
  909. {
  910. return 5 + getSecSkillLevel(3) + valOfBonuses(HeroBonus::SIGHT_RADIOUS); //default + scouting
  911. }
  912. si32 CGHeroInstance::manaRegain() const
  913. {
  914. return 1 + getSecSkillLevel(8) + valOfBonuses(HeroBonus::MANA_REGENERATION); //1 + Mysticism level
  915. }
  916. int CGHeroInstance::valOfBonuses( HeroBonus::BonusType type, int subtype /*= -1*/ ) const
  917. {
  918. int ret = 0;
  919. if(subtype == -1)
  920. {
  921. for(std::list<HeroBonus>::const_iterator i=bonuses.begin(); i != bonuses.end(); i++)
  922. if(i->type == type)
  923. ret += i->val;
  924. }
  925. else
  926. {
  927. for(std::list<HeroBonus>::const_iterator i=bonuses.begin(); i != bonuses.end(); i++)
  928. if(i->type == type && i->subtype == subtype)
  929. ret += i->val;
  930. }
  931. return ret;
  932. }
  933. bool CGHeroInstance::hasBonusOfType(HeroBonus::BonusType type, int subtype /*= -1*/) const
  934. {
  935. if(subtype == -1) //any subtype
  936. {
  937. for(std::list<HeroBonus>::const_iterator i=bonuses.begin(); i != bonuses.end(); i++)
  938. if(i->type == type)
  939. return true;
  940. }
  941. else //given subtype
  942. {
  943. for(std::list<HeroBonus>::const_iterator i=bonuses.begin(); i != bonuses.end(); i++)
  944. if(i->type == type && i->subtype == subtype)
  945. return true;
  946. }
  947. return false;
  948. }
  949. si32 CGHeroInstance::getArtPos(int aid) const
  950. {
  951. for(std::map<ui16,ui32>::const_iterator i = artifWorn.begin(); i != artifWorn.end(); i++)
  952. if(i->second == aid)
  953. return i->first;
  954. return -1;
  955. }
  956. void CGHeroInstance::recreateArtBonuses()
  957. {
  958. //clear all bonuses from artifacts (if present) and give them again
  959. std::remove_if(bonuses.begin(), bonuses.end(), boost::bind(HeroBonus::IsFrom,_1,HeroBonus::ARTIFACT,0xffffff));
  960. for (std::map<ui16,ui32>::iterator ari = artifWorn.begin(); ari != artifWorn.end(); ari++)
  961. {
  962. CArtifact &art = VLC->arth->artifacts[ari->second];
  963. for(std::list<HeroBonus>::iterator i = art.bonuses.begin(); i != art.bonuses.end(); i++)
  964. bonuses.push_back(*i);
  965. }
  966. }
  967. void CGDwelling::initObj()
  968. {
  969. switch(ID)
  970. {
  971. case 17:
  972. {
  973. int crid = VLC->objh->cregens[subID];
  974. CCreature *crs = &VLC->creh->creatures[crid];
  975. creatures.resize(1);
  976. creatures[0].second.push_back(crid);
  977. hoverName = VLC->generaltexth->creGens[subID];
  978. if(crs->level > 4)
  979. {
  980. army.slots[0].first = crs->idNumber;
  981. army.slots[0].second = (8 - crs->level) * 3;
  982. }
  983. }
  984. break;
  985. case 20:
  986. creatures.resize(4);
  987. if(subID == 1) //Golem Factory
  988. {
  989. creatures[0].second.push_back(32); //Stone Golem
  990. creatures[1].second.push_back(33); //Iron Golem
  991. creatures[2].second.push_back(116); //Gold Golem
  992. creatures[3].second.push_back(117); //Diamond Golem
  993. //guards
  994. army.slots[0].first = 116;
  995. army.slots[0].second = 9;
  996. army.slots[1].first = 117;
  997. army.slots[1].second = 6;
  998. }
  999. else if(subID == 0) // Elemental Conflux
  1000. {
  1001. creatures[0].second.push_back(112); //Air Elemental
  1002. creatures[1].second.push_back(114); //Fire Elemental
  1003. creatures[2].second.push_back(113); //Earth Elemental
  1004. creatures[3].second.push_back(115); //Water Elemental
  1005. //guards
  1006. army.slots[0].first = 113;
  1007. army.slots[0].second = 12;
  1008. }
  1009. else
  1010. {
  1011. assert(0);
  1012. }
  1013. hoverName = VLC->generaltexth->creGens4[subID];
  1014. break;
  1015. default:
  1016. assert(0);
  1017. break;
  1018. }
  1019. }
  1020. void CGDwelling::onHeroVisit( const CGHeroInstance * h ) const
  1021. {
  1022. if(h->tempOwner != tempOwner && army) //object is guarded
  1023. {
  1024. BlockingDialog bd;
  1025. bd.player = h->tempOwner;
  1026. bd.flags = BlockingDialog::ALLOW_CANCEL;
  1027. 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?
  1028. bd.text.addReplacement(ID == 17 ? MetaString::CREGENS : MetaString::CREGENS4, subID);
  1029. bd.text.addReplacement(MetaString::ARRAY_TXT, 176 + CCreature::getQuantityID(army.slots.begin()->second.second)*3);
  1030. bd.text.addReplacement(MetaString::CRE_PL_NAMES, army.slots.begin()->second.first);
  1031. cb->showBlockingDialog(&bd, boost::bind(&CGDwelling::wantsFight, this, h, _1));
  1032. return;
  1033. }
  1034. if(h->tempOwner != tempOwner)
  1035. cb->setOwner(id, h->tempOwner);
  1036. BlockingDialog bd;
  1037. bd.player = h->tempOwner;
  1038. bd.flags = BlockingDialog::ALLOW_CANCEL;
  1039. bd.text.addTxt(MetaString::ADVOB_TXT, ID == 17 ? 35 : 36); //{%s} Would you like to recruit %s?
  1040. bd.text.addReplacement(ID == 17 ? MetaString::CREGENS : MetaString::CREGENS4, subID);
  1041. for(size_t i = 0; i < creatures.size(); i++)
  1042. bd.text.addReplacement(MetaString::CRE_PL_NAMES, creatures[i].second[0]);
  1043. cb->showBlockingDialog(&bd, boost::bind(&CGDwelling::heroAcceptsCreatures, this, h, _1));
  1044. }
  1045. void CGDwelling::newTurn() const
  1046. {
  1047. if(cb->getDate(1) != 1) //not first day of week
  1048. return;
  1049. //town growths are handled separately
  1050. if(ID == TOWNI_TYPE)
  1051. return;
  1052. bool change = false;
  1053. SetAvailableCreatures sac;
  1054. sac.creatures = creatures;
  1055. sac.tid = id;
  1056. for (size_t i = 0; i < creatures.size(); i++)
  1057. {
  1058. if(creatures[i].second.size())
  1059. {
  1060. if(false /*accumulate creatures*/)
  1061. sac.creatures[i].first += VLC->creh->creatures[creatures[i].second[0]].growth;
  1062. else
  1063. sac.creatures[i].first = VLC->creh->creatures[creatures[i].second[0]].growth;
  1064. change = true;
  1065. }
  1066. }
  1067. if(change)
  1068. cb->sendAndApply(&sac);
  1069. }
  1070. void CGDwelling::heroAcceptsCreatures( const CGHeroInstance *h, ui32 answer ) const
  1071. {
  1072. if(!answer)
  1073. return;
  1074. int crid = creatures[0].second[0];
  1075. CCreature *crs = &VLC->creh->creatures[crid];
  1076. if(crs->level == 1) //first level - creatures are for free
  1077. {
  1078. if(creatures[0].first) //there are available creatures
  1079. {
  1080. int slot = h->army.getSlotFor(crid);
  1081. if(slot < 0) //no available slot
  1082. {
  1083. InfoWindow iw;
  1084. iw.player = h->tempOwner;
  1085. iw.text.addTxt(MetaString::GENERAL_TXT, 425);//The %s would join your hero, but there aren't enough provisions to support them.
  1086. iw.text.addReplacement(MetaString::CRE_PL_NAMES, crid);
  1087. cb->showInfoDialog(&iw);
  1088. }
  1089. else //give creatures
  1090. {
  1091. SetAvailableCreatures sac;
  1092. sac.tid = id;
  1093. sac.creatures = creatures;
  1094. sac.creatures[0].first = 0;
  1095. SetGarrisons sg;
  1096. sg.garrs[h->id] = h->army;
  1097. sg.garrs[h->id].slots[slot].first = crid;
  1098. sg.garrs[h->id].slots[slot].second += creatures[0].first;
  1099. InfoWindow iw;
  1100. iw.player = h->tempOwner;
  1101. iw.text.addTxt(MetaString::GENERAL_TXT, 423); //%d %s join your army.
  1102. iw.text.addReplacement(creatures[0].first);
  1103. iw.text.addReplacement(MetaString::CRE_PL_NAMES, crid);
  1104. cb->showInfoDialog(&iw);
  1105. cb->sendAndApply(&sac);
  1106. cb->sendAndApply(&sg);
  1107. }
  1108. }
  1109. else //there no creatures
  1110. {
  1111. InfoWindow iw;
  1112. iw.text.addTxt(MetaString::GENERAL_TXT, 422); //There are no %s here to recruit.
  1113. iw.text.addReplacement(MetaString::CRE_PL_NAMES, crid);
  1114. iw.player = h->tempOwner;
  1115. cb->sendAndApply(&iw);
  1116. }
  1117. }
  1118. else
  1119. {
  1120. OpenWindow ow;
  1121. ow.id1 = id;
  1122. ow.id2 = h->id;
  1123. ow.window = ID == 17
  1124. ? OpenWindow::RECRUITMENT_FIRST
  1125. : OpenWindow::RECRUITMENT_ALL;
  1126. cb->sendAndApply(&ow);
  1127. }
  1128. }
  1129. void CGDwelling::wantsFight( const CGHeroInstance *h, ui32 answer ) const
  1130. {
  1131. if(answer)
  1132. cb->startBattleI(h, this, false, boost::bind(&CGDwelling::fightOver, this, h, _1));
  1133. }
  1134. void CGDwelling::fightOver(const CGHeroInstance *h, BattleResult *result) const
  1135. {
  1136. if (result->winner == 0)
  1137. {
  1138. onHeroVisit(h);
  1139. }
  1140. }
  1141. int CGTownInstance::getSightRadious() const //returns sight distance
  1142. {
  1143. if (subID == 2 && (builtBuildings.find(21))!=builtBuildings.end()) //town has lookout tower
  1144. return 20;
  1145. return 5;
  1146. }
  1147. int CGTownInstance::fortLevel() const //0 - none, 1 - fort, 2 - citadel, 3 - castle
  1148. {
  1149. if((builtBuildings.find(9))!=builtBuildings.end())
  1150. return 3;
  1151. if((builtBuildings.find(8))!=builtBuildings.end())
  1152. return 2;
  1153. if((builtBuildings.find(7))!=builtBuildings.end())
  1154. return 1;
  1155. return 0;
  1156. }
  1157. int CGTownInstance::hallLevel() const // -1 - none, 0 - village, 1 - town, 2 - city, 3 - capitol
  1158. {
  1159. if ((builtBuildings.find(13))!=builtBuildings.end())
  1160. return 3;
  1161. if ((builtBuildings.find(12))!=builtBuildings.end())
  1162. return 2;
  1163. if ((builtBuildings.find(11))!=builtBuildings.end())
  1164. return 1;
  1165. if ((builtBuildings.find(10))!=builtBuildings.end())
  1166. return 0;
  1167. return -1;
  1168. }
  1169. int CGTownInstance::mageGuildLevel() const
  1170. {
  1171. if ((builtBuildings.find(4))!=builtBuildings.end())
  1172. return 5;
  1173. if ((builtBuildings.find(3))!=builtBuildings.end())
  1174. return 4;
  1175. if ((builtBuildings.find(2))!=builtBuildings.end())
  1176. return 3;
  1177. if ((builtBuildings.find(1))!=builtBuildings.end())
  1178. return 2;
  1179. if ((builtBuildings.find(0))!=builtBuildings.end())
  1180. return 1;
  1181. return 0;
  1182. }
  1183. bool CGTownInstance::creatureDwelling(const int & level, bool upgraded) const
  1184. {
  1185. return builtBuildings.find(30+level+upgraded*7)!=builtBuildings.end();
  1186. }
  1187. int CGTownInstance::getHordeLevel(const int & HID) const//HID - 0 or 1; returns creature level or -1 if that horde structure is not present
  1188. {
  1189. return town->hordeLvl[HID];
  1190. }
  1191. int CGTownInstance::creatureGrowth(const int & level) const
  1192. {
  1193. int ret = VLC->creh->creatures[town->basicCreatures[level]].growth;
  1194. switch(fortLevel())
  1195. {
  1196. case 3:
  1197. ret*=2;break;
  1198. case 2:
  1199. ret*=(1.5); break;
  1200. }
  1201. if(builtBuildings.find(26)!=builtBuildings.end()) //grail
  1202. ret+=VLC->creh->creatures[town->basicCreatures[level]].growth;
  1203. if(getHordeLevel(0)==level)
  1204. if((builtBuildings.find(18)!=builtBuildings.end()) || (builtBuildings.find(19)!=builtBuildings.end()))
  1205. ret+=VLC->creh->creatures[town->basicCreatures[level]].hordeGrowth;
  1206. if(getHordeLevel(1)==level)
  1207. if((builtBuildings.find(24)!=builtBuildings.end()) || (builtBuildings.find(25)!=builtBuildings.end()))
  1208. ret+=VLC->creh->creatures[town->basicCreatures[level]].hordeGrowth;
  1209. //support for legs of legion etc.
  1210. if(garrisonHero)
  1211. ret += garrisonHero->valOfBonuses(HeroBonus::CREATURE_GROWTH, level);
  1212. if(visitingHero)
  1213. ret += visitingHero->valOfBonuses(HeroBonus::CREATURE_GROWTH, level);
  1214. return ret;
  1215. }
  1216. int CGTownInstance::dailyIncome() const
  1217. {
  1218. int ret = 0;
  1219. if ((builtBuildings.find(26))!=builtBuildings.end())
  1220. ret+=5000;
  1221. if ((builtBuildings.find(13))!=builtBuildings.end())
  1222. ret+=4000;
  1223. else if ((builtBuildings.find(12))!=builtBuildings.end())
  1224. ret+=2000;
  1225. else if ((builtBuildings.find(11))!=builtBuildings.end())
  1226. ret+=1000;
  1227. else if ((builtBuildings.find(10))!=builtBuildings.end())
  1228. ret+=500;
  1229. return ret;
  1230. }
  1231. bool CGTownInstance::hasFort() const
  1232. {
  1233. return (builtBuildings.find(7))!=builtBuildings.end();
  1234. }
  1235. bool CGTownInstance::hasCapitol() const
  1236. {
  1237. return (builtBuildings.find(13))!=builtBuildings.end();
  1238. }
  1239. CGTownInstance::CGTownInstance()
  1240. :IShipyard(this)
  1241. {
  1242. builded=-1;
  1243. destroyed=-1;
  1244. garrisonHero=NULL;
  1245. town=NULL;
  1246. visitingHero = NULL;
  1247. }
  1248. CGTownInstance::~CGTownInstance()
  1249. {}
  1250. int CGTownInstance::spellsAtLevel(int level, bool checkGuild) const
  1251. {
  1252. if(checkGuild && mageGuildLevel() < level)
  1253. return 0;
  1254. int ret = 6 - level; //how many spells are available at this level
  1255. if(subID == 2 && vstd::contains(builtBuildings,22)) //magic library in Tower
  1256. ret++;
  1257. return ret;
  1258. }
  1259. bool CGTownInstance::needsLastStack() const
  1260. {
  1261. if(garrisonHero)
  1262. return true;
  1263. else return false;
  1264. }
  1265. void CGTownInstance::onHeroVisit(const CGHeroInstance * h) const
  1266. {
  1267. if(getOwner() != h->getOwner())
  1268. {
  1269. return;
  1270. }
  1271. cb->heroVisitCastle(id,h->id);
  1272. }
  1273. void CGTownInstance::onHeroLeave(const CGHeroInstance * h) const
  1274. {
  1275. cb->stopHeroVisitCastle(id,h->id);
  1276. }
  1277. void CGTownInstance::initObj()
  1278. {
  1279. hoverName = name + ", " + town->Name();
  1280. creatures.resize(CREATURES_PER_TOWN);
  1281. for (int i = 0; i < CREATURES_PER_TOWN; i++)
  1282. {
  1283. if(creatureDwelling(i,false))
  1284. creatures[i].second.push_back(town->basicCreatures[i]);
  1285. if(creatureDwelling(i,true))
  1286. creatures[i].second.push_back(town->upgradedCreatures[i]);
  1287. }
  1288. }
  1289. int3 CGTownInstance::getSightCenter() const
  1290. {
  1291. return pos - int3(2,0,0);
  1292. }
  1293. void CGTownInstance::getOutOffsets( std::vector<int3> &offsets ) const
  1294. {
  1295. offsets += int3(-1,3,0), int3(-3,3,0);
  1296. }
  1297. void CGVisitableOPH::onHeroVisit( const CGHeroInstance * h ) const
  1298. {
  1299. if(visitors.find(h->id)==visitors.end())
  1300. {
  1301. onNAHeroVisit(h->id, false);
  1302. switch(ID)
  1303. {
  1304. case 102: //tree
  1305. case 4: //arena
  1306. case 41://library
  1307. case 47: //School of Magic
  1308. case 107://School of War
  1309. break;
  1310. default:
  1311. cb->setObjProperty(id,4,h->id); //add to the visitors
  1312. break;
  1313. }
  1314. }
  1315. else
  1316. {
  1317. onNAHeroVisit(h->id, true);
  1318. }
  1319. }
  1320. void CGVisitableOPH::initObj()
  1321. {
  1322. if(ID==102)
  1323. ttype = ran()%3;
  1324. else
  1325. ttype = -1;
  1326. }
  1327. void CGVisitableOPH::treeSelected( int heroID, int resType, int resVal, ui64 expVal, ui32 result ) const
  1328. {
  1329. if(result) //player agreed to give res for exp
  1330. {
  1331. cb->giveResource(cb->getOwner(heroID),resType,-resVal); //take resource
  1332. cb->changePrimSkill(heroID,4,expVal); //give exp
  1333. cb->setObjProperty(id,4,heroID); //add to the visitors
  1334. }
  1335. }
  1336. void CGVisitableOPH::onNAHeroVisit(int heroID, bool alreadyVisited) const
  1337. {
  1338. int id=0, subid=0, ot=0, val=1, sound = 0;
  1339. switch(ID)
  1340. {
  1341. case 4: //arena
  1342. sound = soundBase::NOMAD;
  1343. ot = 0;
  1344. break;
  1345. case 51: //mercenary camp
  1346. sound = soundBase::NOMAD;
  1347. subid=0;
  1348. ot=80;
  1349. break;
  1350. case 23: //marletto tower
  1351. sound = soundBase::NOMAD;
  1352. subid=1;
  1353. ot=39;
  1354. break;
  1355. case 61:
  1356. sound = soundBase::gazebo;
  1357. subid=2;
  1358. ot=100;
  1359. break;
  1360. case 32:
  1361. sound = soundBase::GETPROTECTION;
  1362. subid=3;
  1363. ot=59;
  1364. break;
  1365. case 100:
  1366. sound = soundBase::gazebo;
  1367. id=5;
  1368. ot=143;
  1369. val=1000;
  1370. break;
  1371. case 102:
  1372. sound = soundBase::gazebo;
  1373. id = 5;
  1374. subid = 1;
  1375. ot = 146;
  1376. val = 1;
  1377. break;
  1378. case 41:
  1379. sound = soundBase::gazebo;
  1380. ot = 66;
  1381. break;
  1382. case 47: //School of Magic
  1383. sound = soundBase::faerie;
  1384. ot = 71;
  1385. break;
  1386. case 107://School of War
  1387. sound = soundBase::MILITARY;
  1388. ot = 158;
  1389. break;
  1390. }
  1391. if (!alreadyVisited)
  1392. {
  1393. switch (ID)
  1394. {
  1395. case 4: //arena
  1396. {
  1397. BlockingDialog sd(false,true);
  1398. sd.soundID = sound;
  1399. sd.text << std::pair<ui8,ui32>(11,ot);
  1400. sd.components.push_back(Component(0,0,2,0));
  1401. sd.components.push_back(Component(0,1,2,0));
  1402. sd.player = cb->getOwner(heroID);
  1403. cb->showBlockingDialog(&sd,boost::bind(&CGVisitableOPH::arenaSelected,this,heroID,_1));
  1404. return;
  1405. }
  1406. case 51:
  1407. case 23:
  1408. case 61:
  1409. case 32:
  1410. {
  1411. cb->changePrimSkill(heroID,subid,val);
  1412. InfoWindow iw;
  1413. iw.soundID = sound;
  1414. iw.components.push_back(Component(0,subid,val,0));
  1415. iw.text << std::pair<ui8,ui32>(11,ot);
  1416. iw.player = cb->getOwner(heroID);
  1417. cb->showInfoDialog(&iw);
  1418. break;
  1419. }
  1420. case 100: //give exp
  1421. {
  1422. InfoWindow iw;
  1423. iw.soundID = sound;
  1424. iw.components.push_back(Component(id,subid,val,0));
  1425. iw.player = cb->getOwner(heroID);
  1426. iw.text << std::pair<ui8,ui32>(11,ot);
  1427. iw.soundID = soundBase::gazebo;
  1428. cb->showInfoDialog(&iw);
  1429. cb->changePrimSkill(heroID,4,val);
  1430. break;
  1431. }
  1432. case 102://tree
  1433. {
  1434. const CGHeroInstance *h = cb->getHero(heroID);
  1435. val = VLC->heroh->reqExp(h->level+val) - VLC->heroh->reqExp(h->level);
  1436. if(!ttype)
  1437. {
  1438. cb->setObjProperty(this->id,4,heroID); //add to the visitors
  1439. InfoWindow iw;
  1440. iw.soundID = sound;
  1441. iw.components.push_back(Component(id,subid,1,0));
  1442. iw.player = cb->getOwner(heroID);
  1443. iw.text << std::pair<ui8,ui32>(11,148);
  1444. cb->showInfoDialog(&iw);
  1445. cb->changePrimSkill(heroID,4,val);
  1446. break;
  1447. }
  1448. else
  1449. {
  1450. int res, resval;
  1451. if(ttype==1)
  1452. {
  1453. res = 6;
  1454. resval = 2000;
  1455. ot = 149;
  1456. }
  1457. else
  1458. {
  1459. res = 5;
  1460. resval = 10;
  1461. ot = 151;
  1462. }
  1463. if(cb->getResource(h->tempOwner,res) < resval) //not enough resources
  1464. {
  1465. ot++;
  1466. InfoWindow iw;
  1467. iw.soundID = sound;
  1468. iw.player = h->tempOwner;
  1469. iw.text << std::pair<ui8,ui32>(11,ot);
  1470. cb->showInfoDialog(&iw);
  1471. return;
  1472. }
  1473. BlockingDialog sd(true,false);
  1474. sd.soundID = sound;
  1475. sd.player = cb->getOwner(heroID);
  1476. sd.text << std::pair<ui8,ui32>(11,ot);
  1477. sd.components.push_back(Component(id,subid,val,0));
  1478. cb->showBlockingDialog(&sd,boost::bind(&CGVisitableOPH::treeSelected,this,heroID,res,resval,val,_1));
  1479. }
  1480. break;
  1481. }
  1482. case 41://library of enlightenment
  1483. {
  1484. const CGHeroInstance *h = cb->getHero(heroID);
  1485. if(h->level < 10 - 2*h->getSecSkillLevel(4)) //not enough level
  1486. {
  1487. InfoWindow iw;
  1488. iw.soundID = sound;
  1489. iw.player = cb->getOwner(heroID);
  1490. iw.text << std::pair<ui8,ui32>(11,68);
  1491. cb->showInfoDialog(&iw);
  1492. }
  1493. else
  1494. {
  1495. cb->setObjProperty(this->id,4,heroID); //add to the visitors
  1496. cb->changePrimSkill(heroID,0,2);
  1497. cb->changePrimSkill(heroID,1,2);
  1498. cb->changePrimSkill(heroID,2,2);
  1499. cb->changePrimSkill(heroID,3,2);
  1500. InfoWindow iw;
  1501. iw.soundID = sound;
  1502. iw.player = cb->getOwner(heroID);
  1503. iw.text << std::pair<ui8,ui32>(11,66);
  1504. cb->showInfoDialog(&iw);
  1505. }
  1506. break;
  1507. }
  1508. case 47: //School of Magic
  1509. case 107://School of War
  1510. {
  1511. int skill = (ID==47 ? 2 : 0);
  1512. if(cb->getResource(cb->getOwner(heroID),6) < 1000) //not enough resources
  1513. {
  1514. InfoWindow iw;
  1515. iw.soundID = sound;
  1516. iw.player = cb->getOwner(heroID);
  1517. iw.text << std::pair<ui8,ui32>(MetaString::ADVOB_TXT,ot+2);
  1518. cb->showInfoDialog(&iw);
  1519. }
  1520. else
  1521. {
  1522. BlockingDialog sd(true,true);
  1523. sd.soundID = sound;
  1524. sd.player = cb->getOwner(heroID);
  1525. sd.text << std::pair<ui8,ui32>(11,ot);
  1526. sd.components.push_back(Component(Component::PRIM_SKILL, skill, +1, 0));
  1527. sd.components.push_back(Component(Component::PRIM_SKILL, skill+1, +1, 0));
  1528. cb->showBlockingDialog(&sd,boost::bind(&CGVisitableOPH::schoolSelected,this,heroID,_1));
  1529. }
  1530. }
  1531. break;
  1532. }
  1533. }
  1534. else
  1535. {
  1536. ot++;
  1537. InfoWindow iw;
  1538. iw.soundID = sound;
  1539. iw.player = cb->getOwner(heroID);
  1540. iw.text << std::pair<ui8,ui32>(11,ot);
  1541. cb->showInfoDialog(&iw);
  1542. }
  1543. }
  1544. const std::string & CGVisitableOPH::getHoverText() const
  1545. {
  1546. int pom = -1;
  1547. switch(ID)
  1548. {
  1549. case 4:
  1550. pom = -1;
  1551. break;
  1552. case 51:
  1553. pom = 8;
  1554. break;
  1555. case 23:
  1556. pom = 7;
  1557. break;
  1558. case 61:
  1559. pom = 11;
  1560. break;
  1561. case 32:
  1562. pom = 4;
  1563. break;
  1564. case 100:
  1565. pom = 5;
  1566. break;
  1567. case 102:
  1568. pom = 18;
  1569. break;
  1570. case 41:
  1571. break;
  1572. case 47: //School of Magic
  1573. pom = 9;
  1574. break;
  1575. case 107://School of War
  1576. pom = 10;
  1577. break;
  1578. default:
  1579. throw std::string("Wrong CGVisitableOPH object ID!\n");
  1580. }
  1581. hoverName = VLC->generaltexth->names[ID];
  1582. if(pom >= 0)
  1583. hoverName += (" " + VLC->generaltexth->xtrainfo[pom]);
  1584. const CGHeroInstance *h = cb->getSelectedHero(cb->getCurrentPlayer());
  1585. if(h)
  1586. {
  1587. hoverName += ' ';
  1588. hoverName += (vstd::contains(visitors,h->id))
  1589. ? (VLC->generaltexth->allTexts[352]) //visited
  1590. : ( VLC->generaltexth->allTexts[353]); //not visited
  1591. }
  1592. return hoverName;
  1593. }
  1594. void CGVisitableOPH::arenaSelected( int heroID, int primSkill ) const
  1595. {
  1596. cb->setObjProperty(id,4,heroID); //add to the visitors
  1597. cb->changePrimSkill(heroID,primSkill-1,2);
  1598. }
  1599. void CGVisitableOPH::setPropertyDer( ui8 what, ui32 val )
  1600. {
  1601. if(what == 4)
  1602. visitors.insert(val);
  1603. }
  1604. void CGVisitableOPH::schoolSelected(int heroID, ui32 which) const
  1605. {
  1606. if(!which) //player refused to pay
  1607. return;
  1608. int base = (ID == 47 ? 2 : 0);
  1609. cb->setObjProperty(id,4,heroID); //add to the visitors
  1610. cb->giveResource(cb->getOwner(heroID),6,-1000); //take 1000 gold
  1611. cb->changePrimSkill(heroID, base + which-1, +1); //give appropriate skill
  1612. }
  1613. bool CArmedInstance::needsLastStack() const
  1614. {
  1615. return false;
  1616. }
  1617. int CArmedInstance::getArmyStrength() const
  1618. {
  1619. int ret = 0;
  1620. for(std::map<si32,std::pair<ui32,si32> >::const_iterator i=army.slots.begin(); i!=army.slots.end(); i++)
  1621. ret += VLC->creh->creatures[i->second.first].AIValue * i->second.second;
  1622. return ret;
  1623. }
  1624. /*const std::string & CGCreature::getHoverText() const
  1625. {
  1626. hoverName = VLC->generaltexth->names[ID];
  1627. hoverName += "\n Power rating: ";
  1628. float ratio = ((float)getArmyStrength() / cb->getSelectedHero(cb->getCurrentPlayer())->getTotalStrength());
  1629. if (ratio < 0.1) hoverName += "Effortless";
  1630. else if (ratio < 0.3) hoverName += "Very Weak";
  1631. else if (ratio < 0.6) hoverName += "Weak";
  1632. else if (ratio < 0.9) hoverName += "A bit weaker";
  1633. else if (ratio < 1.1) hoverName += "Equal";
  1634. else if (ratio < 1.3) hoverName += "A bit stronger";
  1635. else if (ratio < 1.8) hoverName += "Strong";
  1636. else if (ratio < 2.5) hoverName += "Very Strong";
  1637. else if (ratio < 4) hoverName += "Challenging";
  1638. else if (ratio < 8) hoverName += "Overpowering";
  1639. else if (ratio < 20) hoverName += "Deadly";
  1640. else hoverName += "Impossible";
  1641. return hoverName;
  1642. }*/
  1643. void CGCreature::onHeroVisit( const CGHeroInstance * h ) const
  1644. {
  1645. int action = takenAction(h);
  1646. switch( action ) //decide what we do...
  1647. {
  1648. case -2: //fight
  1649. fight(h);
  1650. break;
  1651. case -1: //flee
  1652. {
  1653. flee(h);
  1654. break;
  1655. }
  1656. case 0: //join for free
  1657. {
  1658. BlockingDialog ynd(true,false);
  1659. ynd.player = h->tempOwner;
  1660. ynd.text << std::pair<ui8,ui32>(MetaString::ADVOB_TXT, 86);
  1661. ynd.text.addReplacement(MetaString::CRE_PL_NAMES, subID);
  1662. cb->showBlockingDialog(&ynd,boost::bind(&CGCreature::joinDecision,this,h,0,_1));
  1663. break;
  1664. }
  1665. default: //join for gold
  1666. {
  1667. assert(action > 0);
  1668. //ask if player agrees to pay gold
  1669. BlockingDialog ynd(true,false);
  1670. ynd.player = h->tempOwner;
  1671. std::string tmp = VLC->generaltexth->advobtxt[90];
  1672. boost::algorithm::replace_first(tmp,"%d",boost::lexical_cast<std::string>(army.slots.find(0)->second.second));
  1673. boost::algorithm::replace_first(tmp,"%d",boost::lexical_cast<std::string>(action));
  1674. boost::algorithm::replace_first(tmp,"%s",VLC->creh->creatures[subID].namePl);
  1675. ynd.text << tmp;
  1676. cb->showBlockingDialog(&ynd,boost::bind(&CGCreature::joinDecision,this,h,action,_1));
  1677. break;
  1678. }
  1679. }
  1680. }
  1681. void CGCreature::endBattle( BattleResult *result ) const
  1682. {
  1683. if(result->winner==0)
  1684. {
  1685. cb->removeObject(id);
  1686. }
  1687. else
  1688. {
  1689. //int killedAmount=0;
  1690. //for(std::set<std::pair<ui32,si32> >::iterator i=result->casualties[1].begin(); i!=result->casualties[1].end(); i++)
  1691. // if(i->first == subID)
  1692. // killedAmount += i->second;
  1693. //cb->setAmount(id, army.slots.find(0)->second.second - killedAmount);
  1694. MetaString ms;
  1695. int pom = CCreature::getQuantityID(army.slots.find(0)->second.second);
  1696. pom = 174 + 3*pom + 1;
  1697. ms << std::pair<ui8,ui32>(6,pom) << " " << std::pair<ui8,ui32>(7,subID);
  1698. cb->setHoverName(id,&ms);
  1699. }
  1700. }
  1701. void CGCreature::initObj()
  1702. {
  1703. blockVisit = true;
  1704. switch(character)
  1705. {
  1706. case 0:
  1707. character = 0;
  1708. break;
  1709. case 1:
  1710. character = 1 + ran()%7;
  1711. break;
  1712. case 2:
  1713. character = 1 + ran()%10;
  1714. break;
  1715. case 3:
  1716. character = 4 + ran()%7;
  1717. break;
  1718. case 4:
  1719. character = 10;
  1720. break;
  1721. }
  1722. army.slots[0].first = subID;
  1723. si32 &amount = army.slots[0].second;
  1724. CCreature &c = VLC->creh->creatures[subID];
  1725. if(!amount)
  1726. if(c.ammMax == c.ammMin)
  1727. amount = c.ammMax;
  1728. else
  1729. amount = c.ammMin + (ran() % (c.ammMax - c.ammMin));
  1730. MetaString ms;
  1731. int pom = CCreature::getQuantityID(army.slots.find(0)->second.second);
  1732. pom = 174 + 3*pom + 1;
  1733. ms << std::pair<ui8,ui32>(6,pom) << " " << std::pair<ui8,ui32>(7,subID);
  1734. ms.toString(hoverName);
  1735. }
  1736. int CGCreature::takenAction(const CGHeroInstance *h, bool allowJoin) const
  1737. {
  1738. double hlp = h->getTotalStrength() / getArmyStrength();
  1739. if(!character) //compliant creatures will always join
  1740. return 0;
  1741. else if(allowJoin)//test for joining
  1742. {
  1743. int factor;
  1744. if(hlp >= 7)
  1745. factor = 11;
  1746. else if(hlp >= 1)
  1747. factor = (int)(2*(hlp-1));
  1748. else if(hlp >= 0.5)
  1749. factor = -1;
  1750. else if(hlp >= 0.333)
  1751. factor = -2;
  1752. else
  1753. factor = -3;
  1754. int sympathy = 0;
  1755. std::set<ui32> myKindCres; //what creatures are the same kind as we
  1756. myKindCres.insert(subID); //we
  1757. myKindCres.insert(VLC->creh->creatures[subID].upgrades.begin(),VLC->creh->creatures[subID].upgrades.end()); //our upgrades
  1758. for(std::vector<CCreature>::iterator i=VLC->creh->creatures.begin(); i!=VLC->creh->creatures.end(); i++)
  1759. if(vstd::contains(i->upgrades,id)) //it's our base creatures
  1760. myKindCres.insert(i->idNumber);
  1761. int count = 0, //how many creatures of our kind has hero
  1762. totalCount = 0;
  1763. for (std::map<si32,std::pair<ui32,si32> >::const_iterator i = h->army.slots.begin(); i != h->army.slots.end(); i++)
  1764. {
  1765. if(vstd::contains(myKindCres,i->second.first))
  1766. count += i->second.second;
  1767. totalCount += i->second.second;
  1768. }
  1769. if(count*2 > totalCount)
  1770. sympathy++;
  1771. if(count)
  1772. sympathy++;
  1773. int charisma = factor + h->getSecSkillLevel(4) + sympathy;
  1774. if(charisma >= character) //creatures might join...
  1775. {
  1776. if(h->getSecSkillLevel(4) + sympathy + 1 >= character)
  1777. return 0; //join for free
  1778. else if(h->getSecSkillLevel(4) * 2 + sympathy + 1 >= character)
  1779. return VLC->creh->creatures[subID].cost[6] * army.slots.find(0)->second.second; //join for gold
  1780. }
  1781. }
  1782. //we are still here - creatures not joined heroes, test for fleeing
  1783. //TODO: it's provisional formula, should be replaced with original one (or something closer to it)
  1784. //TODO: should be deterministic (will be needed for Vision spell)
  1785. int hlp2 = (int) (hlp - 2)*1000;
  1786. if(!neverFlees
  1787. && hlp2 >= 0
  1788. && rand()%2000 < hlp2
  1789. )
  1790. return -1; //flee
  1791. else
  1792. return -2; //fight
  1793. }
  1794. void CGCreature::fleeDecision(const CGHeroInstance *h, ui32 pursue) const
  1795. {
  1796. if(pursue)
  1797. {
  1798. fight(h);
  1799. }
  1800. else
  1801. {
  1802. cb->removeObject(id);
  1803. }
  1804. }
  1805. void CGCreature::joinDecision(const CGHeroInstance *h, int cost, ui32 accept) const
  1806. {
  1807. if(!accept)
  1808. {
  1809. if(takenAction(h,false) == -1) //they flee
  1810. {
  1811. flee(h);
  1812. }
  1813. else //they fight
  1814. {
  1815. InfoWindow iw;
  1816. iw.player = h->tempOwner;
  1817. iw.text << std::pair<ui8,ui32>(11,87); //Insulted by your refusal of their offer, the monsters attack!
  1818. cb->showInfoDialog(&iw);
  1819. fight(h);
  1820. }
  1821. }
  1822. else //accepted
  1823. {
  1824. if (cb->getResource(h->tempOwner,6) < cost) //player don't have enough gold!
  1825. {
  1826. InfoWindow iw;
  1827. iw.player = h->tempOwner;
  1828. iw.text << std::pair<ui8,ui32>(1,29); //You don't have enough gold
  1829. cb->showInfoDialog(&iw);
  1830. //act as if player refused
  1831. joinDecision(h,cost,false);
  1832. return;
  1833. }
  1834. //take gold
  1835. if(cost)
  1836. cb->giveResource(h->tempOwner,6,-cost);
  1837. int slot = h->army.getSlotFor(subID);
  1838. if(slot >= 0) //there is place
  1839. {
  1840. //add creatures
  1841. SetGarrisons sg;
  1842. sg.garrs[h->id] = h->army;
  1843. if(vstd::contains(h->army.slots,slot)) //add to already present stack
  1844. {
  1845. sg.garrs[h->id].slots[slot].second += army.slots.find(0)->second.second;
  1846. }
  1847. else //add as a new stack
  1848. {
  1849. sg.garrs[h->id].slots[slot] = army.slots.find(0)->second;
  1850. }
  1851. cb->sendAndApply(&sg);
  1852. cb->removeObject(id);
  1853. }
  1854. else
  1855. {
  1856. cb->showGarrisonDialog(id,h->id,boost::bind(&IGameCallback::removeObject,cb,id)); //show garrison window and remove ourselves from map when player ends
  1857. }
  1858. }
  1859. }
  1860. void CGCreature::fight( const CGHeroInstance *h ) const
  1861. {
  1862. cb->startBattleI(h, this, false, boost::bind(&CGCreature::endBattle,this,_1));
  1863. }
  1864. void CGCreature::flee( const CGHeroInstance * h ) const
  1865. {
  1866. BlockingDialog ynd(true,false);
  1867. ynd.player = h->tempOwner;
  1868. ynd.text << std::pair<ui8,ui32>(11,91);
  1869. ynd.text.addReplacement(MetaString::CRE_PL_NAMES, subID);
  1870. cb->showBlockingDialog(&ynd,boost::bind(&CGCreature::fleeDecision,this,h,_1));
  1871. }
  1872. void CGMine::onHeroVisit( const CGHeroInstance * h ) const
  1873. {
  1874. if(subID == 7) //TODO: support for abandoned mine
  1875. return;
  1876. if(h->tempOwner == tempOwner) //we're visiting our mine
  1877. {
  1878. cb->showGarrisonDialog(id,h->id,0);
  1879. return;
  1880. }
  1881. //TODO: check if mine is guarded
  1882. cb->setOwner(id,h->tempOwner); //not ours? flag it!
  1883. MetaString ms;
  1884. ms << std::pair<ui8,ui32>(9,subID) << " (" << std::pair<ui8,ui32>(6,23+h->tempOwner) << ")";
  1885. cb->setHoverName(id,&ms);
  1886. int vv=1; //amount of resource per turn
  1887. if (subID==0 || subID==2)
  1888. vv++;
  1889. else if (subID==6)
  1890. vv = 1000;
  1891. InfoWindow iw;
  1892. iw.soundID = soundBase::FLAGMINE;
  1893. iw.text << std::pair<ui8,ui32>(10,subID);
  1894. iw.player = h->tempOwner;
  1895. iw.components.push_back(Component(2,subID,vv,-1));
  1896. cb->showInfoDialog(&iw);
  1897. }
  1898. void CGMine::newTurn() const
  1899. {
  1900. if(cb->getDate() == 1)
  1901. return;
  1902. if (tempOwner == NEUTRAL_PLAYER)
  1903. return;
  1904. int vv = 1;
  1905. if (subID==0 || subID==2)
  1906. vv++;
  1907. else if (subID==6)
  1908. vv = 1000;
  1909. cb->giveResource(tempOwner,subID,vv);
  1910. }
  1911. void CGMine::initObj()
  1912. {
  1913. MetaString ms;
  1914. ms << std::pair<ui8,ui32>(9,subID);
  1915. if(tempOwner >= PLAYER_LIMIT)
  1916. tempOwner = NEUTRAL_PLAYER;
  1917. else
  1918. ms << " (" << std::pair<ui8,ui32>(6,23+tempOwner) << ")";
  1919. ms.toString(hoverName);
  1920. }
  1921. void CGResource::initObj()
  1922. {
  1923. blockVisit = true;
  1924. hoverName = VLC->generaltexth->restypes[subID];
  1925. if(!amount)
  1926. {
  1927. switch(subID)
  1928. {
  1929. case 6:
  1930. amount = 500 + (rand()%6)*100;
  1931. break;
  1932. case 0: case 2:
  1933. amount = 6 + (rand()%5);
  1934. break;
  1935. default:
  1936. amount = 3 + (rand()%3);
  1937. break;
  1938. }
  1939. }
  1940. }
  1941. void CGResource::onHeroVisit( const CGHeroInstance * h ) const
  1942. {
  1943. if(army.slots.size())
  1944. {
  1945. if(message.size())
  1946. {
  1947. BlockingDialog ynd(true,false);
  1948. ynd.player = h->getOwner();
  1949. ynd.text << message;
  1950. cb->showBlockingDialog(&ynd,boost::bind(&CGResource::fightForRes,this,_1,h));
  1951. }
  1952. else
  1953. {
  1954. fightForRes(1,h);
  1955. }
  1956. }
  1957. else
  1958. {
  1959. if(message.length())
  1960. {
  1961. InfoWindow iw;
  1962. iw.player = h->tempOwner;
  1963. iw.text << message;
  1964. cb->showInfoDialog(&iw);
  1965. }
  1966. collectRes(h->getOwner());
  1967. }
  1968. }
  1969. void CGResource::collectRes( int player ) const
  1970. {
  1971. cb->giveResource(player,subID,amount);
  1972. ShowInInfobox sii;
  1973. sii.player = player;
  1974. sii.c = Component(2,subID,amount,0);
  1975. sii.text << std::pair<ui8,ui32>(11,113);
  1976. sii.text.addReplacement(MetaString::RES_NAMES, subID);
  1977. cb->showCompInfo(&sii);
  1978. cb->removeObject(id);
  1979. }
  1980. void CGResource::fightForRes(ui32 agreed, const CGHeroInstance *h) const
  1981. {
  1982. if(agreed)
  1983. cb->startBattleI(h, this, false, boost::bind(&CGResource::endBattle,this,_1,h));
  1984. }
  1985. void CGResource::endBattle( BattleResult *result, const CGHeroInstance *h ) const
  1986. {
  1987. if(result->winner == 0) //attacker won
  1988. collectRes(h->getOwner());
  1989. }
  1990. void CGVisitableOPW::newTurn() const
  1991. {
  1992. if (cb->getDate(1)==1) //first day of week
  1993. {
  1994. cb->setObjProperty(id,5,false);
  1995. MetaString ms; //set text to "not visited"
  1996. ms << std::pair<ui8,ui32>(3,ID) << " " << std::pair<ui8,ui32>(1,353);
  1997. cb->setHoverName(id,&ms);
  1998. }
  1999. }
  2000. void CGVisitableOPW::onHeroVisit( const CGHeroInstance * h ) const
  2001. {
  2002. int mid, sound = 0;
  2003. switch (ID)
  2004. {
  2005. case 55: //mystical garden
  2006. sound = soundBase::experience;
  2007. mid = 92;
  2008. break;
  2009. case 112://windmill
  2010. sound = soundBase::GENIE;
  2011. mid = 170;
  2012. break;
  2013. case 109://waterwheel
  2014. sound = soundBase::GENIE;
  2015. mid = 164;
  2016. break;
  2017. }
  2018. if (visited)
  2019. {
  2020. if (ID!=112)
  2021. mid++;
  2022. else
  2023. mid--;
  2024. InfoWindow iw;
  2025. iw.soundID = sound;
  2026. iw.player = h->tempOwner;
  2027. iw.text << std::pair<ui8,ui32>(11,mid);
  2028. cb->showInfoDialog(&iw);
  2029. }
  2030. else
  2031. {
  2032. int type, sub, val;
  2033. type = 2;
  2034. switch (ID)
  2035. {
  2036. case 55:
  2037. if (rand()%2)
  2038. {
  2039. sub = 5;
  2040. val = 5;
  2041. }
  2042. else
  2043. {
  2044. sub = 6;
  2045. val = 500;
  2046. }
  2047. break;
  2048. case 112:
  2049. mid = 170;
  2050. sub = (rand() % 5) + 1;
  2051. val = (rand() % 4) + 3;
  2052. break;
  2053. case 109:
  2054. mid = 164;
  2055. sub = 6;
  2056. if(cb->getDate(0)<8)
  2057. val = 500;
  2058. else
  2059. val = 1000;
  2060. }
  2061. cb->giveResource(h->tempOwner,sub,val);
  2062. InfoWindow iw;
  2063. iw.soundID = sound;
  2064. iw.player = h->tempOwner;
  2065. iw.components.push_back(Component(type,sub,val,0));
  2066. iw.text << std::pair<ui8,ui32>(11,mid);
  2067. cb->showInfoDialog(&iw);
  2068. cb->setObjProperty(id,5,true);
  2069. MetaString ms; //set text to "visited"
  2070. ms << std::pair<ui8,ui32>(3,ID) << " " << std::pair<ui8,ui32>(1,352);
  2071. cb->setHoverName(id,&ms);
  2072. }
  2073. }
  2074. void CGVisitableOPW::setPropertyDer( ui8 what, ui32 val )
  2075. {
  2076. if(what == 5)
  2077. visited = val;
  2078. }
  2079. void CGTeleport::onHeroVisit( const CGHeroInstance * h ) const
  2080. {
  2081. int destinationid=-1;
  2082. switch(ID)
  2083. {
  2084. case 43: //one way - find corresponding exit monolith
  2085. if(vstd::contains(objs,44) && vstd::contains(objs[44],subID) && objs[44][subID].size())
  2086. destinationid = objs[44][subID][rand()%objs[44][subID].size()];
  2087. else
  2088. tlog2 << "Cannot find corresponding exit monolith for "<< id << std::endl;
  2089. break;
  2090. case 45: //two way monolith - pick any other one
  2091. if(vstd::contains(objs,45) && vstd::contains(objs[45],subID) && objs[45][subID].size()>1)
  2092. while ((destinationid = objs[45][subID][rand()%objs[45][subID].size()])==id);
  2093. else
  2094. tlog2 << "Cannot find corresponding exit monolith for "<< id << std::endl;
  2095. break;
  2096. case 103: //find nearest subterranean gate on the other level
  2097. {
  2098. std::pair<int,double> best(-1,150000); //pair<id,dist>
  2099. for(int i=0; i<objs[103][0].size(); i++)
  2100. {
  2101. if(cb->getObj(objs[103][0][i])->pos.z == pos.z) continue; //gates on our level are not interesting
  2102. double hlp = cb->getObj(objs[103][0][i])->pos.dist2d(pos);
  2103. if(hlp<best.second)
  2104. {
  2105. best.first = objs[103][0][i];
  2106. best.second = hlp;
  2107. }
  2108. }
  2109. if(best.first<0)
  2110. return;
  2111. else
  2112. destinationid = best.first;
  2113. break;
  2114. }
  2115. }
  2116. if(destinationid < 0)
  2117. {
  2118. tlog2 << "Cannot find exit... :( \n";
  2119. return;
  2120. }
  2121. cb->moveHero(h->id,CGHeroInstance::convertPosition(cb->getObj(destinationid)->pos,true) - getVisitableOffset(),
  2122. true);
  2123. }
  2124. void CGTeleport::initObj()
  2125. {
  2126. objs[ID][subID].push_back(id);
  2127. }
  2128. void CGArtifact::initObj()
  2129. {
  2130. blockVisit = true;
  2131. if(ID == 5)
  2132. hoverName = VLC->arth->artifacts[subID].Name();
  2133. }
  2134. void CGArtifact::onHeroVisit( const CGHeroInstance * h ) const
  2135. {
  2136. if(!army.slots.size())
  2137. {
  2138. if(ID == 5)
  2139. {
  2140. InfoWindow iw;
  2141. iw.soundID = soundBase::treasure;
  2142. iw.player = h->tempOwner;
  2143. iw.components.push_back(Component(4,subID,0,0));
  2144. if(message.length())
  2145. iw.text << message;
  2146. else
  2147. iw.text << std::pair<ui8,ui32>(12,subID);
  2148. cb->showInfoDialog(&iw);
  2149. }
  2150. pick(h);
  2151. }
  2152. else
  2153. {
  2154. if(message.size())
  2155. {
  2156. BlockingDialog ynd(true,false);
  2157. ynd.player = h->getOwner();
  2158. ynd.text << message;
  2159. cb->showBlockingDialog(&ynd,boost::bind(&CGArtifact::fightForArt,this,_1,h));
  2160. }
  2161. else
  2162. {
  2163. fightForArt(0,h);
  2164. }
  2165. }
  2166. }
  2167. void CGArtifact::pick(const CGHeroInstance * h) const
  2168. {
  2169. if(ID == 5) //Artifact
  2170. {
  2171. cb->giveHeroArtifact(subID,h->id,-2);
  2172. }
  2173. else if(ID == 93) // Spell scroll
  2174. {
  2175. //TODO: support for the spell scroll
  2176. }
  2177. cb->removeObject(id);
  2178. }
  2179. void CGArtifact::fightForArt( ui32 agreed, const CGHeroInstance *h ) const
  2180. {
  2181. if(agreed)
  2182. cb->startBattleI(h, this, false, boost::bind(&CGArtifact::endBattle,this,_1,h));
  2183. }
  2184. void CGArtifact::endBattle( BattleResult *result, const CGHeroInstance *h ) const
  2185. {
  2186. if(result->winner == 0) //attacker won
  2187. pick(h);
  2188. }
  2189. void CGPickable::initObj()
  2190. {
  2191. blockVisit = true;
  2192. switch(ID)
  2193. {
  2194. case 12: //campfire
  2195. val2 = (ran()%3) + 4; //4 - 6
  2196. val1 = val2 * 100;
  2197. type = ran()%6; //given resource
  2198. break;
  2199. case 29: //floatsam
  2200. switch(type = ran()%4)
  2201. {
  2202. case 0:
  2203. val1 = val2 = 0;
  2204. break;
  2205. case 1:
  2206. val1 = 5;
  2207. val2 = 0;
  2208. break;
  2209. case 2:
  2210. val1 = 5;
  2211. val2 = 200;
  2212. break;
  2213. case 3:
  2214. val1 = 10;
  2215. val2 = 500;
  2216. break;
  2217. }
  2218. break;
  2219. case 82: //sea chest
  2220. {
  2221. int hlp = ran()%100;
  2222. if(hlp < 20)
  2223. {
  2224. val1 = 0;
  2225. type = 0;
  2226. }
  2227. else if(hlp < 90)
  2228. {
  2229. val1 = 1500;
  2230. type = 2;
  2231. }
  2232. else
  2233. {
  2234. val1 = 1000;
  2235. val2 = VLC->arth->treasures[ran()%VLC->arth->treasures.size()]->id;
  2236. type = 1;
  2237. }
  2238. }
  2239. break;
  2240. case 86: //Shipwreck Survivor
  2241. {
  2242. int hlp = ran()%100;
  2243. if(hlp < 55)
  2244. val1 = VLC->arth->treasures[ran()%VLC->arth->treasures.size()]->id;
  2245. else if(hlp < 75)
  2246. val1 = VLC->arth->minors[ran()%VLC->arth->minors.size()]->id;
  2247. else if(hlp < 95)
  2248. val1 = VLC->arth->majors[ran()%VLC->arth->majors.size()]->id;
  2249. else
  2250. val1 = VLC->arth->relics[ran()%VLC->arth->relics.size()]->id;
  2251. }
  2252. break;
  2253. case 101: //treasure chest
  2254. {
  2255. int hlp = ran()%100;
  2256. if(hlp >= 95)
  2257. {
  2258. type = 1;
  2259. val1 = VLC->arth->treasures[ran()%VLC->arth->treasures.size()]->id;
  2260. return;
  2261. }
  2262. else if (hlp >= 65)
  2263. {
  2264. val1 = 2000;
  2265. }
  2266. else if(hlp >= 33)
  2267. {
  2268. val1 = 1500;
  2269. }
  2270. else
  2271. {
  2272. val1 = 1000;
  2273. }
  2274. val2 = val1 - 500;
  2275. type = 0;
  2276. break;
  2277. }
  2278. }
  2279. }
  2280. void CGPickable::onHeroVisit( const CGHeroInstance * h ) const
  2281. {
  2282. switch(ID)
  2283. {
  2284. case 12: //campfire
  2285. {
  2286. cb->giveResource(h->tempOwner,type,val2); //non-gold resource
  2287. cb->giveResource(h->tempOwner,6,val1);//gold
  2288. InfoWindow iw;
  2289. iw.soundID = soundBase::experience;
  2290. iw.player = h->tempOwner;
  2291. iw.components.push_back(Component(2,6,val1,0));
  2292. iw.components.push_back(Component(2,type,val2,0));
  2293. iw.text << std::pair<ui8,ui32>(11,23);
  2294. cb->showInfoDialog(&iw);
  2295. break;
  2296. }
  2297. case 29: //flotsam
  2298. {
  2299. cb->giveResource(h->tempOwner,0,val1); //wood
  2300. cb->giveResource(h->tempOwner,6,val2);//gold
  2301. InfoWindow iw;
  2302. iw.soundID = soundBase::GENIE;
  2303. iw.player = h->tempOwner;
  2304. if(val1)
  2305. iw.components.push_back(Component(2,0,val1,0));
  2306. if(val2)
  2307. iw.components.push_back(Component(2,6,val2,0));
  2308. iw.text.addTxt(MetaString::ADVOB_TXT, 51+type);
  2309. cb->showInfoDialog(&iw);
  2310. break;
  2311. }
  2312. case 82: //Sea Chest
  2313. {
  2314. InfoWindow iw;
  2315. iw.soundID = soundBase::chest;
  2316. iw.player = h->tempOwner;
  2317. iw.text.addTxt(MetaString::ADVOB_TXT, 116 + type);
  2318. if(val1) //there is gold
  2319. {
  2320. iw.components.push_back(Component(2,6,val1,0));
  2321. cb->giveResource(h->tempOwner,6,val1);
  2322. }
  2323. if(type == 1) //art
  2324. {
  2325. //TODO: what if no space in backpack?
  2326. iw.components.push_back(Component(4, val2, 1, 0));
  2327. iw.text.addReplacement(MetaString::ART_NAMES, val2);
  2328. cb->giveHeroArtifact(val2,h->id,-2);
  2329. }
  2330. cb->showInfoDialog(&iw);
  2331. break;
  2332. }
  2333. case 86: //Shipwreck Survivor
  2334. {
  2335. //TODO: what if no space in backpack?
  2336. InfoWindow iw;
  2337. iw.soundID = soundBase::experience;
  2338. iw.player = h->tempOwner;
  2339. iw.components.push_back(Component(4,val1,1,0));
  2340. iw.text.addTxt(MetaString::ADVOB_TXT, 125);
  2341. iw.text.addReplacement(MetaString::ART_NAMES, val1);
  2342. cb->giveHeroArtifact(val1,h->id,-2);
  2343. cb->showInfoDialog(&iw);
  2344. break;
  2345. }
  2346. case 101: //treasure chest
  2347. {
  2348. if (subID) //not OH3 treasure chest
  2349. {
  2350. tlog2 << "Not supported WoG treasure chest!\n";
  2351. return;
  2352. }
  2353. if(type) //there is an artifact
  2354. {
  2355. cb->giveHeroArtifact(val1,h->id,-2);
  2356. InfoWindow iw;
  2357. iw.soundID = soundBase::treasure;
  2358. iw.player = h->tempOwner;
  2359. iw.components.push_back(Component(4,val1,1,0));
  2360. iw.text << std::pair<ui8,ui32>(11,145);
  2361. iw.text.addReplacement(MetaString::ART_NAMES, val1);
  2362. cb->showInfoDialog(&iw);
  2363. break;
  2364. }
  2365. else
  2366. {
  2367. BlockingDialog sd(false,true);
  2368. sd.player = h->tempOwner;
  2369. sd.text << std::pair<ui8,ui32>(11,146);
  2370. sd.components.push_back(Component(2,6,val1,0));
  2371. sd.components.push_back(Component(5,0,val2,0));
  2372. sd.soundID = soundBase::chest;
  2373. boost::function<void(ui32)> fun = boost::bind(&CGPickable::chosen,this,_1,h->id);
  2374. cb->showBlockingDialog(&sd,fun);
  2375. return;
  2376. }
  2377. }
  2378. }
  2379. cb->removeObject(id);
  2380. }
  2381. void CGPickable::chosen( int which, int heroID ) const
  2382. {
  2383. switch(which)
  2384. {
  2385. case 1: //player pick gold
  2386. cb->giveResource(cb->getOwner(heroID),6,val1);
  2387. break;
  2388. case 2: //player pick exp
  2389. cb->changePrimSkill(heroID, 4, val2);
  2390. break;
  2391. default:
  2392. throw std::string("Unhandled treasure choice");
  2393. }
  2394. cb->removeObject(id);
  2395. }
  2396. void CGWitchHut::initObj()
  2397. {
  2398. ability = allowedAbilities[ran()%allowedAbilities.size()];
  2399. }
  2400. void CGWitchHut::onHeroVisit( const CGHeroInstance * h ) const
  2401. {
  2402. InfoWindow iw;
  2403. iw.soundID = soundBase::gazebo;
  2404. iw.player = h->getOwner();
  2405. if(!hasVisited(h->tempOwner))
  2406. cb->setObjProperty(id,10,h->tempOwner);
  2407. if(h->getSecSkillLevel(ability)) //you alredy know this skill
  2408. {
  2409. iw.text << std::pair<ui8,ui32>(11,172);
  2410. iw.text.addReplacement(MetaString::SEC_SKILL_NAME, ability);
  2411. }
  2412. else if(h->secSkills.size() >= SKILL_PER_HERO) //already all skills slots used
  2413. {
  2414. iw.text << std::pair<ui8,ui32>(11,173);
  2415. iw.text.addReplacement(MetaString::SEC_SKILL_NAME, ability);
  2416. }
  2417. else //give sec skill
  2418. {
  2419. iw.components.push_back(Component(1, ability, 1, 0));
  2420. iw.text << std::pair<ui8,ui32>(11,171);
  2421. iw.text.addReplacement(MetaString::SEC_SKILL_NAME, ability);
  2422. cb->changeSecSkill(h->id,ability,1,true);
  2423. }
  2424. cb->showInfoDialog(&iw);
  2425. }
  2426. const std::string & CGWitchHut::getHoverText() const
  2427. {
  2428. hoverName = VLC->generaltexth->names[ID];
  2429. if(hasVisited(cb->getCurrentPlayer())) //TODO: use local player, not current
  2430. {
  2431. hoverName += "\n" + VLC->generaltexth->allTexts[356]; // + (learn %s)
  2432. boost::algorithm::replace_first(hoverName,"%s",VLC->generaltexth->skillName[ability]);
  2433. const CGHeroInstance *h = cb->getSelectedHero(cb->getCurrentPlayer());
  2434. if(h && h->getSecSkillLevel(ability)) //hero knows that ability
  2435. hoverName += "\n\n" + VLC->generaltexth->allTexts[357]; // (Already learned)
  2436. }
  2437. return hoverName;
  2438. }
  2439. void CGBonusingObject::onHeroVisit( const CGHeroInstance * h ) const
  2440. {
  2441. bool visited = h->getBonus(HeroBonus::OBJECT,ID);
  2442. int messageID, bonusType, bonusVal;
  2443. int bonusMove = 0, sound = -1;
  2444. InfoWindow iw;
  2445. iw.player = h->tempOwner;
  2446. GiveBonus gbonus;
  2447. gbonus.hid = h->id;
  2448. gbonus.bonus.duration = HeroBonus::ONE_BATTLE;
  2449. gbonus.bonus.source = HeroBonus::OBJECT;
  2450. gbonus.bonus.id = ID;
  2451. switch(ID)
  2452. {
  2453. case 11: //buoy
  2454. messageID = 21;
  2455. sound = soundBase::MORALE;
  2456. gbonus.bonus.type = HeroBonus::MORALE;
  2457. gbonus.bonus.val = +1;
  2458. gbonus.bdescr << std::pair<ui8,ui32>(6,94);
  2459. break;
  2460. case 14: //swan pond
  2461. messageID = 29;
  2462. sound = soundBase::LUCK;
  2463. gbonus.bonus.type = HeroBonus::LUCK;
  2464. gbonus.bonus.val = 2;
  2465. gbonus.bdescr << std::pair<ui8,ui32>(6,67);
  2466. bonusMove = -h->movement;
  2467. break;
  2468. case 28: //Faerie Ring
  2469. messageID = 49;
  2470. sound = soundBase::LUCK;
  2471. gbonus.bonus.type = HeroBonus::LUCK;
  2472. gbonus.bonus.val = 1;
  2473. gbonus.bdescr << std::pair<ui8,ui32>(6,71);
  2474. break;
  2475. case 30: //fountain of fortune
  2476. messageID = 55;
  2477. sound = soundBase::LUCK;
  2478. gbonus.bonus.type = HeroBonus::LUCK;
  2479. gbonus.bonus.val = rand()%5 - 1;
  2480. gbonus.bdescr << std::pair<ui8,ui32>(6,69);
  2481. gbonus.bdescr.addReplacement((gbonus.bonus.val<0 ? "-" : "+") + boost::lexical_cast<std::string>(gbonus.bonus.val));
  2482. break;
  2483. case 38: //idol of fortune
  2484. messageID = 62;
  2485. iw.soundID = soundBase::experience;
  2486. if(cb->getDate(1) == 7) //7th day of week
  2487. gbonus.bonus.type = HeroBonus::MORALE_AND_LUCK;
  2488. else
  2489. gbonus.bonus.type = (cb->getDate(1)%2) ? HeroBonus::LUCK : HeroBonus::MORALE;
  2490. gbonus.bonus.val = 1;
  2491. gbonus.bdescr << std::pair<ui8,ui32>(6,68);
  2492. break;
  2493. case 52: //Mermaid
  2494. messageID = 83;
  2495. sound = soundBase::LUCK;
  2496. gbonus.bonus.type = HeroBonus::LUCK;
  2497. gbonus.bonus.val = 1;
  2498. gbonus.bdescr << std::pair<ui8,ui32>(6,72);
  2499. break;
  2500. case 64: //Rally Flag
  2501. sound = soundBase::MORALE;
  2502. messageID = 111;
  2503. gbonus.bonus.type = HeroBonus::MORALE_AND_LUCK;
  2504. gbonus.bonus.val = 1;
  2505. gbonus.bdescr << std::pair<ui8,ui32>(6,102);
  2506. bonusMove = 400;
  2507. break;
  2508. case 56: //oasis
  2509. messageID = 95;
  2510. gbonus.bonus.type = HeroBonus::MORALE;
  2511. gbonus.bonus.val = 1;
  2512. gbonus.bdescr << std::pair<ui8,ui32>(6,95);
  2513. bonusMove = 800;
  2514. break;
  2515. case 96: //temple
  2516. messageID = 140;
  2517. iw.soundID = soundBase::temple;
  2518. gbonus.bonus.type = HeroBonus::MORALE;
  2519. if(cb->getDate(1)==7) //sunday
  2520. {
  2521. gbonus.bonus.val = 2;
  2522. gbonus.bdescr << std::pair<ui8,ui32>(6,97);
  2523. }
  2524. else
  2525. {
  2526. gbonus.bonus.val = 1;
  2527. gbonus.bdescr << std::pair<ui8,ui32>(6,96);
  2528. }
  2529. break;
  2530. case 110://Watering Hole
  2531. sound = soundBase::MORALE;
  2532. messageID = 166;
  2533. gbonus.bonus.type = HeroBonus::MORALE;
  2534. gbonus.bonus.val = 1;
  2535. gbonus.bdescr << std::pair<ui8,ui32>(6,100);
  2536. bonusMove = 400;
  2537. break;
  2538. case 31: //Fountain of Youth
  2539. sound = soundBase::MORALE;
  2540. messageID = 57;
  2541. gbonus.bonus.type = HeroBonus::MORALE;
  2542. gbonus.bonus.val = 1;
  2543. gbonus.bdescr << std::pair<ui8,ui32>(6,103);
  2544. bonusMove = 400;
  2545. break;
  2546. case 94: //Stables TODO: upgrade Cavaliers
  2547. sound = soundBase::horse20;
  2548. std::set<ui32> slots;
  2549. for (std::map<si32,std::pair<ui32,si32> >::const_iterator i = h->army.slots.begin(); i != h->army.slots.end(); ++i)
  2550. {
  2551. if(i->second.first == 10)
  2552. slots.insert(i->first);
  2553. }
  2554. if (!slots.empty())
  2555. {
  2556. for (std::set<ui32>::const_iterator i = slots.begin(); i != slots.end(); i++)
  2557. {
  2558. UpgradeCreature uc(*i, id, 11);
  2559. //uc.applyGh (&gh);
  2560. }
  2561. }
  2562. messageID = 137;
  2563. gbonus.bonus.type = HeroBonus::LAND_MOVEMENT;
  2564. gbonus.bonus.val = 600;
  2565. gbonus.bdescr << std::pair<ui8,ui32>(6, 100);
  2566. break;
  2567. }
  2568. if(visited)
  2569. {
  2570. if(ID==64 || ID==96 || ID==56 || ID==52 || ID==94)
  2571. messageID--;
  2572. else
  2573. messageID++;
  2574. }
  2575. else
  2576. {
  2577. if(gbonus.bonus.type == HeroBonus::MORALE || gbonus.bonus.type == HeroBonus::MORALE_AND_LUCK)
  2578. iw.components.push_back(Component(8,0,gbonus.bonus.val,0));
  2579. if(gbonus.bonus.type == HeroBonus::LUCK || gbonus.bonus.type == HeroBonus::MORALE_AND_LUCK)
  2580. iw.components.push_back(Component(9,0,gbonus.bonus.val,0));
  2581. cb->giveHeroBonus(&gbonus);
  2582. if(bonusMove) //swan pond - take all move points
  2583. {
  2584. SetMovePoints smp;
  2585. smp.hid = h->id;
  2586. smp.val = h->movement + bonusMove;
  2587. cb->setMovePoints(&smp);
  2588. }
  2589. }
  2590. iw.soundID = sound;
  2591. iw.text << std::pair<ui8,ui32>(11,messageID);
  2592. cb->showInfoDialog(&iw);
  2593. }
  2594. const std::string & CGBonusingObject::getHoverText() const
  2595. {
  2596. const CGHeroInstance *h = cb->getSelectedHero(cb->getCurrentPlayer());
  2597. hoverName = VLC->generaltexth->names[ID];
  2598. if(h)
  2599. {
  2600. if(!h->getBonus(HeroBonus::OBJECT,ID))
  2601. hoverName += " " + VLC->generaltexth->allTexts[353]; //not visited
  2602. else
  2603. hoverName += " " + VLC->generaltexth->allTexts[352]; //visited
  2604. }
  2605. return hoverName;
  2606. }
  2607. void CGBonusingObject::initObj()
  2608. {
  2609. if(ID == 11 || ID == 52) //Buoy / Mermaid
  2610. {
  2611. blockVisit = true;
  2612. }
  2613. }
  2614. void CGMagicWell::onHeroVisit( const CGHeroInstance * h ) const
  2615. {
  2616. int message;
  2617. InfoWindow iw;
  2618. iw.soundID = soundBase::faerie;
  2619. iw.player = h->tempOwner;
  2620. if(h->getBonus(HeroBonus::OBJECT,ID)) //has already visited Well today
  2621. {
  2622. message = 78;
  2623. }
  2624. else if(h->mana < h->manaLimit())
  2625. {
  2626. giveDummyBonus(h->id);
  2627. cb->setManaPoints(h->id,h->manaLimit());
  2628. message = 77;
  2629. }
  2630. else
  2631. {
  2632. message = 79;
  2633. }
  2634. iw.text << std::pair<ui8,ui32>(11,message); //"A second drink at the well in one day will not help you."
  2635. cb->showInfoDialog(&iw);
  2636. }
  2637. const std::string & CGMagicWell::getHoverText() const
  2638. {
  2639. getNameVis(hoverName);
  2640. return hoverName;
  2641. }
  2642. void CGPandoraBox::initObj()
  2643. {
  2644. blockVisit = true;
  2645. }
  2646. void CGPandoraBox::onHeroVisit(const CGHeroInstance * h) const
  2647. {
  2648. BlockingDialog bd (true, false);
  2649. bd.player = h->getOwner();
  2650. bd.soundID = soundBase::QUEST;
  2651. bd.text.addTxt (MetaString::ADVOB_TXT, 14);
  2652. cb->showBlockingDialog (&bd, boost::bind (&CGPandoraBox::open, this, h, _1));
  2653. }
  2654. void CGPandoraBox::open( const CGHeroInstance * h, ui32 accept ) const
  2655. {
  2656. if (accept)
  2657. {
  2658. if (army) //if pandora's box is protected by army
  2659. {
  2660. InfoWindow iw;
  2661. iw.player = h->tempOwner;
  2662. iw.text.addTxt(MetaString::ADVOB_TXT, 16);
  2663. cb->showInfoDialog(&iw);
  2664. cb->startBattleI(h, this, false, boost::bind(&CGPandoraBox::endBattle, this, h, _1)); //grants things after battle
  2665. }
  2666. else if (message.size() == 0 && resources.size() == 0
  2667. && primskills.size() == 0 && abilities.size() == 0
  2668. && abilityLevels.size() == 0 && artifacts.size() == 0
  2669. && spells.size() == 0 && creatures == 0
  2670. && gainedExp == 0 && manaDiff == 0 && moraleDiff == 0 && luckDiff == 0) //if it gives nothing without battle
  2671. {
  2672. InfoWindow iw;
  2673. iw.player = h->tempOwner;
  2674. iw.text.addTxt(MetaString::ADVOB_TXT, 15);
  2675. cb->showInfoDialog(&iw);
  2676. }
  2677. else //if it gives something without battle
  2678. {
  2679. giveContents (h, false);
  2680. }
  2681. }
  2682. }
  2683. void CGPandoraBox::endBattle( const CGHeroInstance *h, BattleResult *result ) const
  2684. {
  2685. if(result->winner)
  2686. return;
  2687. giveContents(h,true);
  2688. }
  2689. void CGPandoraBox::giveContents( const CGHeroInstance *h, bool afterBattle ) const
  2690. {
  2691. InfoWindow iw;
  2692. iw.player = h->getOwner();
  2693. bool changesPrimSkill = false;
  2694. for (int i = 0; i < primskills.size(); i++)
  2695. {
  2696. if(primskills[i])
  2697. {
  2698. changesPrimSkill = true;
  2699. break;
  2700. }
  2701. }
  2702. if(gainedExp || changesPrimSkill || abilities.size())
  2703. {
  2704. getText(iw,afterBattle,175,h);
  2705. if(gainedExp)
  2706. iw.components.push_back(Component(Component::EXPERIENCE,0,gainedExp,0));
  2707. for(int i=0; i<primskills.size(); i++)
  2708. if(primskills[i])
  2709. iw.components.push_back(Component(Component::PRIM_SKILL,i,primskills[i],0));
  2710. for(int i=0; i<abilities.size(); i++)
  2711. iw.components.push_back(Component(Component::SEC_SKILL,abilities[i],abilityLevels[i],0));
  2712. cb->showInfoDialog(&iw);
  2713. //give exp
  2714. if(gainedExp)
  2715. cb->changePrimSkill(h->id,4,gainedExp,false);
  2716. //give prim skills
  2717. for(int i=0; i<primskills.size(); i++)
  2718. if(primskills[i])
  2719. cb->changePrimSkill(h->id,i,primskills[i],false);
  2720. //give sec skills
  2721. for(int i=0; i<abilities.size(); i++)
  2722. {
  2723. int curLev = h->getSecSkillLevel(abilities[i]);
  2724. if(curLev && curLev < abilityLevels[i]
  2725. || h->secSkills.size() < SKILL_PER_HERO )
  2726. {
  2727. cb->changeSecSkill(h->id,abilities[i],abilityLevels[i],true);
  2728. }
  2729. }
  2730. }
  2731. if(spells.size())
  2732. {
  2733. std::set<ui32> spellsToGive;
  2734. iw.components.clear();
  2735. for(int i=0; i<spells.size(); i++)
  2736. {
  2737. iw.components.push_back(Component(Component::SPELL,spells[i],0,0));
  2738. spellsToGive.insert(spells[i]);
  2739. }
  2740. if(spellsToGive.size())
  2741. {
  2742. cb->changeSpells(h->id,true,spellsToGive);
  2743. cb->showInfoDialog(&iw);
  2744. }
  2745. }
  2746. if(manaDiff)
  2747. {
  2748. getText(iw,afterBattle,manaDiff,176,177,h);
  2749. iw.components.push_back(Component(Component::PRIM_SKILL,5,manaDiff,0));
  2750. cb->showInfoDialog(&iw);
  2751. cb->setManaPoints(h->id, h->mana + manaDiff);
  2752. }
  2753. if(moraleDiff)
  2754. {
  2755. getText(iw,afterBattle,moraleDiff,178,179,h);
  2756. iw.components.push_back(Component(Component::MORALE,0,moraleDiff,0));
  2757. cb->showInfoDialog(&iw);
  2758. GiveBonus gb;
  2759. gb.bonus = HeroBonus(HeroBonus::ONE_BATTLE,HeroBonus::MORALE,HeroBonus::OBJECT,moraleDiff,id,"");
  2760. gb.hid = h->id;
  2761. cb->giveHeroBonus(&gb);
  2762. }
  2763. if(luckDiff)
  2764. {
  2765. getText(iw,afterBattle,luckDiff,180,181,h);
  2766. iw.components.push_back(Component(Component::LUCK,0,luckDiff,0));
  2767. cb->showInfoDialog(&iw);
  2768. GiveBonus gb;
  2769. gb.bonus = HeroBonus(HeroBonus::ONE_BATTLE,HeroBonus::LUCK,HeroBonus::OBJECT,luckDiff,id,"");
  2770. gb.hid = h->id;
  2771. cb->giveHeroBonus(&gb);
  2772. }
  2773. iw.components.clear();
  2774. iw.text.clear();
  2775. for(int i=0; i<resources.size(); i++)
  2776. {
  2777. if(resources[i] < 0)
  2778. iw.components.push_back(Component(Component::RESOURCE,i,resources[i],0));
  2779. }
  2780. if(iw.components.size())
  2781. {
  2782. getText(iw,afterBattle,182,h);
  2783. cb->showInfoDialog(&iw);
  2784. }
  2785. iw.components.clear();
  2786. iw.text.clear();
  2787. for(int i=0; i<resources.size(); i++)
  2788. {
  2789. if(resources[i] > 0)
  2790. iw.components.push_back(Component(Component::RESOURCE,i,resources[i],0));
  2791. }
  2792. if(iw.components.size())
  2793. {
  2794. getText(iw,afterBattle,183,h);
  2795. cb->showInfoDialog(&iw);
  2796. }
  2797. iw.components.clear();
  2798. getText(iw,afterBattle,183,h);
  2799. for(int i=0; i<artifacts.size(); i++)
  2800. {
  2801. iw.components.push_back(Component(Component::ARTIFACT,artifacts[i],0,0));
  2802. if(iw.components.size() >= 14)
  2803. {
  2804. cb->showInfoDialog(&iw);
  2805. iw.components.clear();
  2806. }
  2807. }
  2808. if(iw.components.size())
  2809. {
  2810. cb->showInfoDialog(&iw);
  2811. }
  2812. for(int i=0; i<resources.size(); i++)
  2813. if(resources[i])
  2814. cb->giveResource(h->getOwner(),i,resources[i]);
  2815. for(int i=0; i<artifacts.size(); i++)
  2816. cb->giveHeroArtifact(artifacts[i],h->id,-2);
  2817. //show dialog with given creatures
  2818. iw.components.clear();
  2819. iw.text.clear();
  2820. for(std::map<si32,std::pair<ui32,si32> >::const_iterator i = creatures.slots.begin(); i != creatures.slots.end(); i++)
  2821. {
  2822. iw.components.push_back(Component(Component::CREATURE,i->second.first,i->second.second,0));
  2823. }
  2824. if(iw.components.size())
  2825. {
  2826. if(afterBattle)
  2827. {
  2828. if(iw.components.front().val == 1)
  2829. {
  2830. iw.text.addTxt(MetaString::ADVOB_TXT,185);//A %s joins %s's army.
  2831. iw.text.addReplacement(MetaString::CRE_SING_NAMES, iw.components.front().subtype);
  2832. }
  2833. else
  2834. {
  2835. iw.text.addTxt(MetaString::ADVOB_TXT,186);//%s join %s's army.
  2836. iw.text.addReplacement(MetaString::CRE_PL_NAMES, iw.components.front().subtype);
  2837. }
  2838. iw.text.addReplacement(h->name);
  2839. }
  2840. else
  2841. {
  2842. iw.text << message;
  2843. afterBattle = true;
  2844. }
  2845. cb->showInfoDialog(&iw);
  2846. }
  2847. //check if creatures can be moved to hero army
  2848. CCreatureSet heroArmy = h->army;
  2849. CCreatureSet ourArmy = creatures;
  2850. while(ourArmy)
  2851. {
  2852. int slot = heroArmy.getSlotFor(ourArmy.slots.begin()->second.first);
  2853. if(slot < 0)
  2854. break;
  2855. heroArmy.slots[slot].first = ourArmy.slots.begin()->second.first;
  2856. heroArmy.slots[slot].second += ourArmy.slots.begin()->second.second;
  2857. ourArmy.slots.erase(ourArmy.slots.begin());
  2858. }
  2859. if(!ourArmy) //all creatures can be moved to hero army - do that
  2860. {
  2861. SetGarrisons sg;
  2862. sg.garrs[h->id] = heroArmy;
  2863. cb->sendAndApply(&sg);
  2864. }
  2865. else //show garrison window and let player pick creatures
  2866. {
  2867. SetGarrisons sg;
  2868. sg.garrs[id] = creatures;
  2869. cb->sendAndApply(&sg);
  2870. cb->showGarrisonDialog(id,h->id,boost::bind(&IGameCallback::removeObject,cb,id));
  2871. }
  2872. if(!afterBattle && message.size())
  2873. {
  2874. iw.text << message;
  2875. cb->showInfoDialog(&iw);
  2876. }
  2877. cb->removeObject(id);
  2878. }
  2879. void CGPandoraBox::getText( InfoWindow &iw, bool &afterBattle, int text, const CGHeroInstance * h ) const
  2880. {
  2881. if(afterBattle || !message.size())
  2882. {
  2883. iw.text.addTxt(MetaString::ADVOB_TXT,text);//%s has lost treasure.
  2884. iw.text.addReplacement(h->name);
  2885. }
  2886. else
  2887. {
  2888. iw.text << message;
  2889. afterBattle = true;
  2890. }
  2891. }
  2892. void CGPandoraBox::getText( InfoWindow &iw, bool &afterBattle, int val, int negative, int positive, const CGHeroInstance * h ) const
  2893. {
  2894. iw.components.clear();
  2895. iw.text.clear();
  2896. if(afterBattle || !message.size())
  2897. {
  2898. iw.text.addTxt(MetaString::ADVOB_TXT,val < 0 ? negative : positive); //%s's luck takes a turn for the worse / %s's luck increases
  2899. iw.text.addReplacement(h->name);
  2900. }
  2901. else
  2902. {
  2903. iw.text << message;
  2904. afterBattle = true;
  2905. }
  2906. }
  2907. void CGEvent::onHeroVisit( const CGHeroInstance * h ) const
  2908. {
  2909. if(!(availableFor & (1 << h->tempOwner)))
  2910. return;
  2911. if(cb->getPlayerSettings(h->tempOwner)->human)
  2912. {
  2913. if(humanActivate)
  2914. activated(h);
  2915. }
  2916. else if(computerActivate)
  2917. activated(h);
  2918. }
  2919. void CGEvent::activated( const CGHeroInstance * h ) const
  2920. {
  2921. if(army)
  2922. {
  2923. InfoWindow iw;
  2924. iw.player = h->tempOwner;
  2925. if(message.size())
  2926. iw.text << message;
  2927. else
  2928. iw.text.addTxt(MetaString::ADVOB_TXT, 16);
  2929. cb->showInfoDialog(&iw);
  2930. cb->startBattleI(h, this, false, boost::bind(&CGEvent::endBattle,this,h,_1));
  2931. }
  2932. else
  2933. {
  2934. giveContents(h,false);
  2935. }
  2936. }
  2937. void CGObservatory::onHeroVisit( const CGHeroInstance * h ) const
  2938. {
  2939. InfoWindow iw;
  2940. iw.soundID = soundBase::LIGHTHOUSE;
  2941. iw.player = h->tempOwner;
  2942. iw.text.addTxt(MetaString::ADVOB_TXT,98 + (ID==60));
  2943. cb->showInfoDialog(&iw);
  2944. FoWChange fw;
  2945. fw.player = h->tempOwner;
  2946. fw.mode = 1;
  2947. cb->getTilesInRange(fw.tiles,pos,20,h->tempOwner,1);
  2948. cb->sendAndApply(&fw);
  2949. }
  2950. void CGShrine::onHeroVisit( const CGHeroInstance * h ) const
  2951. {
  2952. if(spell == 255)
  2953. {
  2954. tlog1 << "Not initialized shrine visited!\n";
  2955. return;
  2956. }
  2957. if(!hasVisited(h->tempOwner))
  2958. cb->setObjProperty(id,10,h->tempOwner);
  2959. InfoWindow iw;
  2960. iw.soundID = soundBase::temple;
  2961. iw.player = h->getOwner();
  2962. iw.text.addTxt(MetaString::ADVOB_TXT,127 + ID - 88);
  2963. iw.text.addTxt(MetaString::SPELL_NAME,spell);
  2964. iw.text << ".";
  2965. if(!h->getArt(17)) //no spellbook
  2966. {
  2967. iw.text.addTxt(MetaString::ADVOB_TXT,131);
  2968. }
  2969. else if(ID == 90 && !h->getSecSkillLevel(7)) //it's third level spell and hero doesn't have wisdom
  2970. {
  2971. iw.text.addTxt(MetaString::ADVOB_TXT,130);
  2972. }
  2973. else if(vstd::contains(h->spells,spell))//hero already knows the spell
  2974. {
  2975. iw.text.addTxt(MetaString::ADVOB_TXT,174);
  2976. }
  2977. else //give spell
  2978. {
  2979. std::set<ui32> spells;
  2980. spells.insert(spell);
  2981. cb->changeSpells(h->id,true,spells);
  2982. iw.components.push_back(Component(Component::SPELL,spell,0,0));
  2983. }
  2984. cb->showInfoDialog(&iw);
  2985. }
  2986. void CGShrine::initObj()
  2987. {
  2988. if(spell == 255) //spell not set
  2989. {
  2990. int level = ID-87;
  2991. std::vector<ui32> possibilities;
  2992. //add all allowed spells of wanted level
  2993. for(int i=0; i<SPELLS_QUANTITY; i++)
  2994. {
  2995. if(VLC->spellh->spells[i].level == level
  2996. && cb->isAllowed(0,VLC->spellh->spells[i].id))
  2997. {
  2998. possibilities.push_back(VLC->spellh->spells[i].id);
  2999. }
  3000. }
  3001. if(!possibilities.size())
  3002. {
  3003. tlog1 << "Error: cannot init shrine, no allowed spells!\n";
  3004. return;
  3005. }
  3006. spell = possibilities[ran() % possibilities.size()];
  3007. }
  3008. }
  3009. const std::string & CGShrine::getHoverText() const
  3010. {
  3011. hoverName = VLC->generaltexth->names[ID];
  3012. if(hasVisited(cb->getCurrentPlayer())) //TODO: use local player, not current
  3013. {
  3014. hoverName += "\n" + VLC->generaltexth->allTexts[355]; // + (learn %s)
  3015. boost::algorithm::replace_first(hoverName,"%s",VLC->spellh->spells[spell].name);
  3016. const CGHeroInstance *h = cb->getSelectedHero(cb->getCurrentPlayer());
  3017. if(h && vstd::contains(h->spells,spell)) //hero knows that ability
  3018. hoverName += "\n\n" + VLC->generaltexth->allTexts[354]; // (Already learned)
  3019. }
  3020. return hoverName;
  3021. }
  3022. void CGSignBottle::initObj()
  3023. {
  3024. //if no text is set than we pick random from the predefined ones
  3025. if(!message.size())
  3026. message = VLC->generaltexth->randsign[ran()%VLC->generaltexth->randsign.size()];
  3027. if(ID == 59)
  3028. {
  3029. blockVisit = true;
  3030. }
  3031. }
  3032. void CGSignBottle::onHeroVisit( const CGHeroInstance * h ) const
  3033. {
  3034. InfoWindow iw;
  3035. iw.soundID = soundBase::STORE;
  3036. iw.player = h->getOwner();
  3037. iw.text << message;
  3038. cb->showInfoDialog(&iw);
  3039. if(ID == 59)
  3040. cb->removeObject(id);
  3041. }
  3042. void CGScholar::giveAnyBonus( const CGHeroInstance * h ) const
  3043. {
  3044. }
  3045. void CGScholar::onHeroVisit( const CGHeroInstance * h ) const
  3046. {
  3047. int type = bonusType;
  3048. int bid = bonusID;
  3049. //check if the bonus if applicable, if not - give primary skill (always possible)
  3050. int ssl = h->getSecSkillLevel(bid); //current sec skill level, used if bonusType == 1
  3051. if((type == 1
  3052. && ((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)
  3053. || (type == 2 && (!h->getArt(17) || vstd::contains(h->spells,bid)))) //hero doesn't have a spellbook or already knows the spell
  3054. {
  3055. type = 0;
  3056. bid = ran() % PRIMARY_SKILLS;
  3057. }
  3058. InfoWindow iw;
  3059. iw.soundID = soundBase::gazebo;
  3060. iw.player = h->getOwner();
  3061. iw.text.addTxt(MetaString::ADVOB_TXT,115);
  3062. switch (type)
  3063. {
  3064. case 0:
  3065. cb->changePrimSkill(h->id,bid,+1);
  3066. iw.components.push_back(Component(Component::PRIM_SKILL,bid,+1,0));
  3067. break;
  3068. case 1:
  3069. cb->changeSecSkill(h->id,bid,+1);
  3070. iw.components.push_back(Component(Component::SEC_SKILL,bid,ssl+1,0));
  3071. break;
  3072. case 2:
  3073. {
  3074. std::set<ui32> hlp;
  3075. hlp.insert(bid);
  3076. cb->changeSpells(h->id,true,hlp);
  3077. iw.components.push_back(Component(Component::SPELL,bid,0,0));
  3078. }
  3079. break;
  3080. default:
  3081. tlog1 << "Error: wrong bonustype (" << (int)type << ") for Scholar!\n";
  3082. return;
  3083. }
  3084. cb->showInfoDialog(&iw);
  3085. cb->removeObject(id);
  3086. }
  3087. void CGScholar::initObj()
  3088. {
  3089. blockVisit = true;
  3090. if(bonusType == 255)
  3091. {
  3092. bonusType = ran()%3;
  3093. switch(bonusType)
  3094. {
  3095. case 0:
  3096. bonusID = ran() % PRIMARY_SKILLS;
  3097. break;
  3098. case 1:
  3099. bonusID = ran() % SKILL_QUANTITY;
  3100. break;
  3101. case 2:
  3102. bonusID = ran() % SPELLS_QUANTITY;
  3103. break;
  3104. }
  3105. }
  3106. }
  3107. void CGOnceVisitable::onHeroVisit( const CGHeroInstance * h ) const
  3108. {
  3109. int sound = 0;
  3110. int txtid = -1;
  3111. switch(ID)
  3112. {
  3113. case 22: //Corpse
  3114. txtid = 37;
  3115. sound = soundBase::MYSTERY;
  3116. break;
  3117. case 39: //Lean To
  3118. sound = soundBase::GENIE;
  3119. txtid = 64;
  3120. break;
  3121. case 105://Wagon
  3122. sound = soundBase::GENIE;
  3123. txtid = 154;
  3124. break;
  3125. case 108:
  3126. break;
  3127. default:
  3128. tlog1 << "Error: Unknown object (" << ID <<") treated as CGOnceVisitable!\n";
  3129. return;
  3130. }
  3131. if(ID == 108)//Warrior's Tomb
  3132. {
  3133. //ask if player wants to search the Tomb
  3134. BlockingDialog bd(true, false);
  3135. sound = soundBase::GRAVEYARD;
  3136. bd.player = h->getOwner();
  3137. bd.text.addTxt(MetaString::ADVOB_TXT,161);
  3138. cb->showBlockingDialog(&bd,boost::bind(&CGOnceVisitable::searchTomb,this,h,_1));
  3139. return;
  3140. }
  3141. InfoWindow iw;
  3142. iw.soundID = sound;
  3143. iw.player = h->getOwner();
  3144. if(players.size()) //we have been already visited...
  3145. {
  3146. txtid++;
  3147. if(ID == 105) //wagon has extra text (for finding art) we need to omit
  3148. txtid++;
  3149. iw.text.addTxt(MetaString::ADVOB_TXT, txtid);
  3150. }
  3151. else //first visit - give bonus!
  3152. {
  3153. switch(artOrRes)
  3154. {
  3155. case 0:
  3156. txtid++;
  3157. break;
  3158. case 1: //art
  3159. iw.components.push_back(Component(Component::ARTIFACT,bonusType,0,0));
  3160. cb->giveHeroArtifact(bonusType,h->id,-2);
  3161. break;
  3162. case 2: //res
  3163. iw.components.push_back(Component(Component::RESOURCE,bonusType,bonusVal,0));
  3164. cb->giveResource(h->getOwner(),bonusType,bonusVal);
  3165. break;
  3166. }
  3167. iw.text.addTxt(MetaString::ADVOB_TXT, txtid);
  3168. if(ID == 105 && artOrRes == 1)
  3169. {
  3170. iw.text.localStrings.back().second++;
  3171. iw.text.addReplacement(MetaString::ART_NAMES, bonusType);
  3172. }
  3173. }
  3174. cb->showInfoDialog(&iw);
  3175. cb->setObjProperty(id,10,h->getOwner());
  3176. }
  3177. const std::string & CGOnceVisitable::getHoverText() const
  3178. {
  3179. hoverName = VLC->generaltexth->names[ID] + " ";
  3180. hoverName += (hasVisited(cb->getCurrentPlayer())
  3181. ? (VLC->generaltexth->allTexts[352]) //visited
  3182. : ( VLC->generaltexth->allTexts[353])); //not visited
  3183. return hoverName;
  3184. }
  3185. void CGOnceVisitable::initObj()
  3186. {
  3187. switch(ID)
  3188. {
  3189. case 22: //Corpse
  3190. {
  3191. blockVisit = true;
  3192. int hlp = ran()%100;
  3193. if(hlp < 20)
  3194. {
  3195. artOrRes = 1;
  3196. std::vector<CArtifact*> arts;
  3197. cb->getAllowed(arts, CArtifact::ART_TREASURE | CArtifact::ART_MINOR | CArtifact::ART_MAJOR);
  3198. bonusType = arts[ran() % arts.size()]->id;
  3199. }
  3200. else
  3201. {
  3202. artOrRes = 0;
  3203. }
  3204. }
  3205. break;
  3206. case 39: //Lean To
  3207. {
  3208. artOrRes = 2;
  3209. bonusType = ran()%6; //any basic resource without gold
  3210. bonusVal = ran()%4 + 1;
  3211. break;
  3212. }
  3213. case 108://Warrior's Tomb
  3214. {
  3215. artOrRes = 1;
  3216. std::vector<CArtifact*> arts;
  3217. int hlp = ran()%100;
  3218. if(hlp < 30)
  3219. cb->getAllowed(arts,CArtifact::ART_TREASURE);
  3220. else if(hlp < 80)
  3221. cb->getAllowed(arts,CArtifact::ART_MINOR);
  3222. else if(hlp < 95)
  3223. cb->getAllowed(arts,CArtifact::ART_MAJOR);
  3224. else
  3225. cb->getAllowed(arts,CArtifact::ART_RELIC);
  3226. bonusType = arts[ran() % arts.size()]->id;
  3227. }
  3228. break;
  3229. case 105://Wagon
  3230. {
  3231. int hlp = ran()%100;
  3232. if(hlp < 10)
  3233. {
  3234. artOrRes = 0; // nothing... :(
  3235. }
  3236. else if(hlp < 50) //minor or treasure art
  3237. {
  3238. artOrRes = 1;
  3239. std::vector<CArtifact*> arts;
  3240. cb->getAllowed(arts, CArtifact::ART_TREASURE | CArtifact::ART_MINOR);
  3241. bonusType = arts[ran() % arts.size()]->id;
  3242. }
  3243. else //2 - 5 of non-gold resource
  3244. {
  3245. artOrRes = 2;
  3246. bonusType = ran()%6;
  3247. bonusVal = ran()%4 + 2;
  3248. }
  3249. break;
  3250. }
  3251. }
  3252. }
  3253. void CGOnceVisitable::searchTomb(const CGHeroInstance *h, ui32 accept) const
  3254. {
  3255. if(accept)
  3256. {
  3257. InfoWindow iw;
  3258. iw.player = h->getOwner();
  3259. iw.components.push_back(Component(Component::MORALE,0,-3,0));
  3260. if(players.size()) //we've been already visited, player found nothing
  3261. {
  3262. iw.text.addTxt(MetaString::ADVOB_TXT,163);
  3263. }
  3264. else //first visit - give artifact
  3265. {
  3266. iw.text.addTxt(MetaString::ADVOB_TXT,162);
  3267. iw.components.push_back(Component(Component::ARTIFACT,bonusType,0,0));
  3268. iw.text.addReplacement(MetaString::ART_NAMES, bonusType);
  3269. cb->giveHeroArtifact(bonusType,h->id,-2);
  3270. }
  3271. if(!h->getBonus(HeroBonus::OBJECT,ID)) //we don't have modifier from this object yet
  3272. {
  3273. //ruin morale
  3274. GiveBonus gb;
  3275. gb.hid = h->id;
  3276. gb.bonus = HeroBonus(HeroBonus::ONE_BATTLE,HeroBonus::MORALE,HeroBonus::OBJECT,-3,id,"");
  3277. gb.bdescr.addTxt(MetaString::ARRAY_TXT,104); //Warrior Tomb Visited -3
  3278. cb->giveHeroBonus(&gb);
  3279. }
  3280. }
  3281. }
  3282. void CBank::initObj()
  3283. {
  3284. index = 0;
  3285. switch (ID) //find apriopriate key
  3286. {
  3287. case 16: //bank
  3288. index = subID; break;
  3289. case 24: //derelict ship
  3290. index = 8;
  3291. case 25: //utopia
  3292. index = 10; break;
  3293. case 84: //crypt
  3294. index = 9; break;
  3295. case 85: //shipwreck
  3296. index = 7; break;
  3297. }
  3298. bc = NULL;
  3299. daycounter = 0;
  3300. multiplier = 1;
  3301. }
  3302. void CBank::reset()
  3303. {
  3304. int val1 = ran()%100;
  3305. int chance = 0;
  3306. for (ui8 i = 1; i <= VLC->objh->banksInfo[index].size(); i++)
  3307. {
  3308. if (val1 < (chance += VLC->objh->banksInfo[index][i].chance))
  3309. cb->setObjProperty (id, 13, i);
  3310. }
  3311. artifacts.clear();
  3312. for (ui8 i = 1; i <= 4; i++)
  3313. {
  3314. for (ui8 j = 1; j <= bc->artifacts[i]; j++)
  3315. cb->setObjProperty (id, 18, i);
  3316. }
  3317. }
  3318. void CBank::setPropertyDer (ui8 what, ui32 val)
  3319. {
  3320. switch (what)
  3321. {
  3322. case 11: //daycounter
  3323. daycounter++;
  3324. break;
  3325. case 12: //multiplier
  3326. multiplier = ((float)val)/100;
  3327. break;
  3328. case 13: //bank preset
  3329. bc = &VLC->objh->banksInfo[index][val];
  3330. break;
  3331. case 14:
  3332. reset();
  3333. break;
  3334. case 15:
  3335. bc = NULL;
  3336. break;
  3337. case 18: //Artifacts
  3338. {
  3339. std::vector<CArtifact*> arts;
  3340. switch (val)
  3341. {
  3342. case 1:
  3343. cb->getAllowed (arts, CArtifact::ART_TREASURE);
  3344. break;
  3345. case 2:
  3346. cb->getAllowed (arts, CArtifact::ART_MINOR);
  3347. break;
  3348. case 3:
  3349. cb->getAllowed (arts, CArtifact::ART_MAJOR);
  3350. break;
  3351. case 4:
  3352. cb->getAllowed (arts, CArtifact::ART_RELIC);
  3353. break;
  3354. }
  3355. artifacts.push_back (arts[ran() % arts.size()]->id);
  3356. }
  3357. break;
  3358. }
  3359. }
  3360. void CBank::newTurn()
  3361. {
  3362. if (bc == NULL)
  3363. {
  3364. if (daycounter >= 28 || cb->getDate(0) == 1)
  3365. {
  3366. reset();
  3367. daycounter = 0;
  3368. if (ID == 24 && cb->getDate(0) > 1)
  3369. {
  3370. artifacts.clear(); //derelict ships are usable only once
  3371. }
  3372. }
  3373. else
  3374. daycounter++;
  3375. }
  3376. }
  3377. void CBank::onHeroVisit (const CGHeroInstance * h) const
  3378. {
  3379. if (bc != NULL)
  3380. {
  3381. int banktext = 0;
  3382. switch (ID)
  3383. {
  3384. case 16: //generic bank
  3385. banktext = 32;
  3386. break;
  3387. case 24:
  3388. banktext = 41;
  3389. break;
  3390. case 25: //utopia
  3391. banktext = 47;
  3392. break;
  3393. case 84: //crypt
  3394. banktext = 119;
  3395. break;
  3396. case 85: //shipwreck
  3397. banktext = 122;
  3398. break;
  3399. }
  3400. BlockingDialog bd (true, false);
  3401. bd.player = h->getOwner();
  3402. bd.soundID = soundBase::DANGER;
  3403. std::string desc = VLC->generaltexth->allTexts[banktext];
  3404. boost::algorithm::replace_first (desc, "%s", VLC->generaltexth->names[ID]);
  3405. bd.text << desc;
  3406. cb->showBlockingDialog (&bd, boost::bind (&CBank::fightGuards, this, h, _1));
  3407. }
  3408. else
  3409. {
  3410. InfoWindow iw;
  3411. if (ID == 85)
  3412. iw.components.push_back (Component (Component::MORALE, 0 , -2, 0));
  3413. iw.soundID = soundBase::GRAVEYARD;
  3414. iw.player = h->getOwner();
  3415. iw.text.addTxt (MetaString::ADVOB_TXT, 33);
  3416. cb->showInfoDialog(&iw);
  3417. }
  3418. }
  3419. void CBank::fightGuards (const CGHeroInstance * h, ui32 accept) const
  3420. {
  3421. if (accept)
  3422. {
  3423. int upgraded = 0;
  3424. if (ran()%100 < bc->upgradeChance) upgraded = 1;
  3425. CCreatureSet ourArmy;
  3426. switch (bc->guards.size())
  3427. {
  3428. case 1:
  3429. for (int i = 1; i <= 5; i++)
  3430. ourArmy.setCreature (i, bc->guards[1].first + upgraded, bc->guards[1].second / 5 );
  3431. break;
  3432. case 4:
  3433. {
  3434. std::vector< std::pair <ui16, ui32> >::const_iterator it;
  3435. for (it = bc->guards.begin(); it != bc->guards.end(); it++)
  3436. ourArmy.setCreature (ourArmy.slots.size() + 1, it->first, it->second );
  3437. }
  3438. break;
  3439. default:
  3440. tlog1 << "Error: Unexpected army data: " << bc->guards.size() <<" items found";
  3441. return;
  3442. }
  3443. cb->startBattleI (h, this, true, boost::bind (&CBank::endBattle, this, h, _1));
  3444. }
  3445. }
  3446. void CBank::endBattle (const CGHeroInstance *h, const BattleResult *result) const
  3447. {
  3448. if (result->winner == 0)
  3449. {
  3450. int textID = -1;
  3451. InfoWindow iw;
  3452. switch (ID)
  3453. {
  3454. case 16: //generic bank
  3455. textID = 34;
  3456. break;
  3457. case 24: //derelict ship
  3458. if (bc->resources.size() != 0)
  3459. textID = 43;
  3460. else
  3461. {
  3462. textID = 42;
  3463. iw.components.push_back (Component (Component::MORALE, 0 , -2, 0));
  3464. }
  3465. break;
  3466. case 25: //utopia
  3467. textID = 47;
  3468. break;
  3469. case 84: //crypt
  3470. textID = 121;
  3471. break;
  3472. case 85: //shipwreck
  3473. if (bc->resources.size() != 0)
  3474. textID = 124;
  3475. else
  3476. {
  3477. textID = 123;
  3478. }
  3479. break;
  3480. }
  3481. iw.text.addTxt (MetaString::ADVOB_TXT, textID);
  3482. iw.player = cb->getCurrentPlayer();
  3483. //grant resources
  3484. for (int it = 0; it < bc->resources.size(); ++it)
  3485. {
  3486. iw.components.push_back (Component (Component::RESOURCE, it, bc->resources[it], 0));
  3487. cb->giveResource (cb->getCurrentPlayer(), it, bc->resources[it]);
  3488. }
  3489. //grant artifacts
  3490. for (std::vector<si32>::const_iterator it = artifacts.begin(); it != artifacts.end(); it++)
  3491. {
  3492. iw.components.push_back (Component (Component::ARTIFACT, *it, 0, 0));
  3493. iw.text.addReplacement (MetaString::ART_NAMES, *it);
  3494. cb->giveHeroArtifact (*it, cb->getSelectedHero() ,-2);
  3495. }
  3496. //grant creatures
  3497. CCreatureSet ourArmy;
  3498. for (std::vector< std::pair <ui16, ui32> >::const_iterator it = bc->creatures.begin(); it != bc->creatures.end(); it++)
  3499. {
  3500. int slot = ourArmy.getSlotFor (it->second);
  3501. ourArmy.slots[slot] = *it; //assuming we're not going to add multiple stacks of same creature
  3502. }
  3503. cb->giveCreatures (id, cb->getHero (cb->getSelectedHero()), &ourArmy);
  3504. cb->setObjProperty (id, 15, 0); //bc = NULL
  3505. }
  3506. else
  3507. cb->setObjProperty (id, 14, 0); //reset
  3508. }
  3509. void CGKeys::setPropertyDer (ui8 what, ui32 val) //101-108 - enable key for player 1-8
  3510. {
  3511. if (what >= 101 && what <= (100 + PLAYER_LIMIT))
  3512. playerKeyMap.find(what-101)->second.insert(val);
  3513. }
  3514. bool CGKeys::wasMyColorVisited (int player) const
  3515. {
  3516. if (vstd::contains(playerKeyMap[player], subID)) //creates set if it's not there
  3517. return true;
  3518. else
  3519. return false;
  3520. }
  3521. const std::string & CGKeymasterTent::getHoverText() const
  3522. {
  3523. hoverName = VLC->generaltexth->names[ID];
  3524. if (wasMyColorVisited (cb->getCurrentPlayer()) )//TODO: use local player, not current
  3525. hoverName += "\n" + VLC->generaltexth->allTexts[352];
  3526. else
  3527. hoverName += "\n" + VLC->generaltexth->allTexts[353];
  3528. return hoverName;
  3529. }
  3530. void CGKeymasterTent::onHeroVisit( const CGHeroInstance * h ) const
  3531. {
  3532. InfoWindow iw;
  3533. iw.soundID = soundBase::CAVEHEAD;
  3534. iw.player = h->getOwner();
  3535. if (!wasMyColorVisited (h->getOwner()) )
  3536. {
  3537. cb->setObjProperty(id, h->tempOwner+101, subID);
  3538. iw.text << std::pair<ui8,ui32>(11,19);
  3539. }
  3540. else
  3541. iw.text << std::pair<ui8,ui32>(11,20);
  3542. cb->showInfoDialog(&iw);
  3543. }
  3544. void CGBorderGuard::initObj()
  3545. {
  3546. blockVisit = true;
  3547. }
  3548. const std::string & CGBorderGuard::getHoverText() const
  3549. {
  3550. hoverName = VLC->generaltexth->names[ID];
  3551. if (wasMyColorVisited (cb->getCurrentPlayer()) )//TODO: use local player, not current
  3552. hoverName += "\n" + VLC->generaltexth->allTexts[352];
  3553. else
  3554. hoverName += "\n" + VLC->generaltexth->allTexts[353];
  3555. return hoverName;
  3556. }
  3557. void CGBorderGuard::onHeroVisit( const CGHeroInstance * h ) const
  3558. {
  3559. if (wasMyColorVisited (h->getOwner()) )
  3560. {
  3561. BlockingDialog bd (true, false);
  3562. bd.player = h->getOwner();
  3563. bd.soundID = soundBase::QUEST;
  3564. bd.text.addTxt (MetaString::ADVOB_TXT, 17);
  3565. cb->showBlockingDialog (&bd, boost::bind (&CGBorderGuard::openGate, this, h, _1));
  3566. }
  3567. else
  3568. {
  3569. InfoWindow iw;
  3570. iw.player = h->getOwner();
  3571. iw.soundID = soundBase::CAVEHEAD;
  3572. iw.text << std::pair<ui8,ui32>(11,18);
  3573. cb->showInfoDialog (&iw);
  3574. }
  3575. }
  3576. void CGBorderGuard::openGate(const CGHeroInstance *h, ui32 accept) const
  3577. {
  3578. if (accept)
  3579. cb->removeObject(id);
  3580. }
  3581. void CGBorderGate::onHeroVisit( const CGHeroInstance * h ) const //TODO: passability
  3582. {
  3583. InfoWindow iw;
  3584. iw.player = h->getOwner();
  3585. if (!wasMyColorVisited (h->getOwner()) )
  3586. {
  3587. iw.text << std::pair<ui8,ui32>(11,18);
  3588. cb->showInfoDialog(&iw);
  3589. }
  3590. }
  3591. void CGMagi::initObj()
  3592. {
  3593. if (ID == 27)
  3594. {
  3595. blockVisit = true;
  3596. eyelist[subID].push_back(id);
  3597. }
  3598. }
  3599. void CGMagi::onHeroVisit(const CGHeroInstance * h) const
  3600. {
  3601. if (ID == 37)
  3602. {
  3603. InfoWindow iw;
  3604. CenterView cv;
  3605. FoWChange fw;
  3606. cv.player = iw.player = fw.player = h->tempOwner;
  3607. iw.soundID = soundBase::LIGHTHOUSE;
  3608. iw.player = h->tempOwner;
  3609. iw.text.addTxt (MetaString::ADVOB_TXT, 61);
  3610. cb->showInfoDialog(&iw);
  3611. fw.mode = 1;
  3612. std::vector<si32>::iterator it;
  3613. for (it = eyelist[subID].begin(); it < eyelist[subID].end(); it++)
  3614. {
  3615. const CGObjectInstance *eye = cb->getObj(*it);
  3616. cb->getTilesInRange (fw.tiles, eye->pos, 5, h->tempOwner, 1);
  3617. cb->sendAndApply(&fw);
  3618. cv.pos = eye->pos;
  3619. cv.focusTime = 2000;
  3620. cb->sendAndApply(&cv);
  3621. }
  3622. cv.pos = h->getPosition(false);
  3623. cb->sendAndApply(&cv);
  3624. }
  3625. else if (ID == 27)
  3626. {
  3627. InfoWindow iw;
  3628. iw.player = h->tempOwner;
  3629. iw.text.addTxt (MetaString::ADVOB_TXT, 48);
  3630. cb->showInfoDialog(&iw);
  3631. }
  3632. }
  3633. void CGBoat::initObj()
  3634. {
  3635. hero = NULL;
  3636. }
  3637. void CGSirens::initObj()
  3638. {
  3639. blockVisit = true;
  3640. }
  3641. const std::string & CGSirens::getHoverText() const
  3642. {
  3643. getNameVis(hoverName);
  3644. return hoverName;
  3645. }
  3646. void CGSirens::onHeroVisit( const CGHeroInstance * h ) const
  3647. {
  3648. int message;
  3649. InfoWindow iw;
  3650. iw.soundID = soundBase::DANGER;
  3651. iw.player = h->tempOwner;
  3652. if(h->getBonus(HeroBonus::OBJECT,ID)) //has already visited Sirens
  3653. {
  3654. iw.text.addTxt(11,133);
  3655. }
  3656. else
  3657. {
  3658. giveDummyBonus(h->id, HeroBonus::ONE_BATTLE);
  3659. int xp = 0;
  3660. SetGarrisons sg;
  3661. sg.garrs[h->id] = h->army;
  3662. for (std::map<si32,std::pair<ui32,si32> >::const_iterator i = h->army.slots.begin(); i != h->army.slots.end(); i++)
  3663. {
  3664. int drown = (int)(i->second.second * 0.3);
  3665. if(drown)
  3666. {
  3667. sg.garrs[h->id].slots[i->first].second -= drown;
  3668. xp += drown * VLC->creh->creatures[i->second.first].hitPoints;
  3669. }
  3670. }
  3671. if(xp)
  3672. {
  3673. iw.text.addTxt(11,132);
  3674. iw.text.addReplacement(xp);
  3675. cb->sendAndApply(&sg);
  3676. }
  3677. else
  3678. {
  3679. iw.text.addTxt(11,134);
  3680. }
  3681. }
  3682. cb->showInfoDialog(&iw);
  3683. }
  3684. void IShipyard::getBoatCost( std::vector<si32> &cost ) const
  3685. {
  3686. cost.resize(RESOURCE_QUANTITY);
  3687. cost[0] = 10;
  3688. cost[6] = 1000;
  3689. }
  3690. //bool IShipyard::validLocation() const
  3691. //{
  3692. // std::vector<int3> offsets;
  3693. // getOutOffsets(offsets);
  3694. //
  3695. // TerrainTile *tile;
  3696. // for(int i = 0; i < offsets.size(); i++)
  3697. // if((tile = IObjectInterface::cb->getTile(o->pos + offsets[i])) && tile->tertype == TerrainTile::water) //tile is in the map and is water
  3698. // return true;
  3699. // return false;
  3700. //}
  3701. int3 IShipyard::bestLocation() const
  3702. {
  3703. std::vector<int3> offsets;
  3704. getOutOffsets(offsets);
  3705. TerrainTile *tile;
  3706. for(int i = 0; i < offsets.size(); i++)
  3707. if((tile = IObjectInterface::cb->getTile(o->pos + offsets[i])) && tile->tertype == TerrainTile::water) //tile is in the map and is water
  3708. return o->pos + offsets[i];
  3709. return int3(-1,-1,-1);
  3710. }
  3711. IShipyard::IShipyard(const CGObjectInstance *O)
  3712. : o(O)
  3713. {
  3714. }
  3715. int IShipyard::state() const
  3716. {
  3717. int3 tile = bestLocation();
  3718. TerrainTile *t = IObjectInterface::cb->getTile(tile);
  3719. if(!t)
  3720. return 3; //no water
  3721. else if(!t->blockingObjects.size())
  3722. return 0; //OK
  3723. else if(t->blockingObjects.front()->ID == 8)
  3724. return 1; //blocked with boat
  3725. else
  3726. return 2; //blocked
  3727. }
  3728. IShipyard * IShipyard::castFrom( CGObjectInstance *obj )
  3729. {
  3730. if(obj->ID == TOWNI_TYPE)
  3731. {
  3732. return static_cast<CGTownInstance*>(obj);
  3733. }
  3734. else if(obj->ID == 87)
  3735. {
  3736. return static_cast<CGShipyard*>(obj);
  3737. }
  3738. else
  3739. {
  3740. tlog1 << "Cannot cast to IShipyard object with ID " << obj->ID << std::endl;
  3741. return NULL;
  3742. }
  3743. }
  3744. const IShipyard * IShipyard::castFrom( const CGObjectInstance *obj )
  3745. {
  3746. return castFrom(const_cast<CGObjectInstance*>(obj));
  3747. }
  3748. CGShipyard::CGShipyard()
  3749. :IShipyard(this)
  3750. {
  3751. }
  3752. void CGShipyard::getOutOffsets( std::vector<int3> &offsets ) const
  3753. {
  3754. 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),
  3755. int3(-2,-1,0), int3(0,-1,0), int3(-1,-1,0), int3(-2,1,0), int3(0,1,0), int3(-1,1,0);
  3756. }
  3757. void CGShipyard::onHeroVisit( const CGHeroInstance * h ) const
  3758. {
  3759. if(tempOwner != h->tempOwner)
  3760. cb->setOwner(id, h->tempOwner);
  3761. int s = state();
  3762. if(s)
  3763. {
  3764. InfoWindow iw;
  3765. iw.player = tempOwner;
  3766. switch(s)
  3767. {
  3768. case 1:
  3769. iw.text.addTxt(MetaString::GENERAL_TXT, 51);
  3770. break;
  3771. case 2:
  3772. iw.text.addTxt(MetaString::ADVOB_TXT, 189);
  3773. break;
  3774. case 3:
  3775. tlog1 << "Shipyard without water!!! " << pos << "\t" << id << std::endl;
  3776. return;
  3777. }
  3778. cb->showInfoDialog(&iw);
  3779. }
  3780. else
  3781. {
  3782. OpenWindow ow;
  3783. ow.id1 = id;
  3784. ow.id2 = h->id;
  3785. ow.window = OpenWindow::SHIPYARD_WINDOW;
  3786. cb->sendAndApply(&ow);
  3787. }
  3788. }
  3789. void CCartographer::onHeroVisit( const CGHeroInstance * h ) const
  3790. {
  3791. if (!hasVisited (h->getOwner()) ) //if hero has not visited yet this cartographer
  3792. {
  3793. if (cb->getResource(h->tempOwner, 6) >= 1000) //if he can afford a map
  3794. {
  3795. //ask if he wants to buy one
  3796. int text;
  3797. if (cb->getTile(pos)->tertype == 8) //water
  3798. text = 25;
  3799. else
  3800. {
  3801. if (pos.z == 0)
  3802. text = 26;
  3803. else
  3804. text = 27;
  3805. }
  3806. BlockingDialog bd (true, false);
  3807. bd.player = h->getOwner();
  3808. bd.soundID = soundBase::LIGHTHOUSE;
  3809. bd.text.addTxt (MetaString::ADVOB_TXT, text);
  3810. cb->showBlockingDialog (&bd, boost::bind (&CCartographer::buyMap, this, h, _1));
  3811. }
  3812. else //if he cannot afford
  3813. {
  3814. InfoWindow iw;
  3815. iw.player = h->getOwner();
  3816. iw.soundID = soundBase::CAVEHEAD;
  3817. iw.text << std::pair<ui8,ui32>(11,28);
  3818. cb->showInfoDialog (&iw);
  3819. }
  3820. }
  3821. else //if he already visited carographer
  3822. {
  3823. InfoWindow iw;
  3824. iw.player = h->getOwner();
  3825. iw.soundID = soundBase::CAVEHEAD;
  3826. iw.text << std::pair<ui8,ui32>(11,24);
  3827. cb->showInfoDialog (&iw);
  3828. }
  3829. }
  3830. void CCartographer::buyMap (const CGHeroInstance *h, ui32 accept) const
  3831. {
  3832. if (accept) //if hero wants to buy map
  3833. {
  3834. cb->giveResource (h->tempOwner, 6, -1000);
  3835. FoWChange fw;
  3836. fw.player = h->tempOwner;
  3837. int surface = 0;
  3838. if (cb->getTile(pos)->tertype == 8) //water
  3839. surface = 2;
  3840. else
  3841. surface = 1;
  3842. //reveal apropriate tiles
  3843. cb->getAllTiles (fw.tiles, h->tempOwner, pos.z, surface);
  3844. cb->sendAndApply (&fw);
  3845. cb->setObjProperty (id, 10, h->tempOwner);
  3846. }
  3847. }