CObjectHandler.cpp 104 KB

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