CObjectHandler.cpp 97 KB

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