CObjectHandler.cpp 101 KB

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