CObjectHandler.cpp 97 KB

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