2
0

CObjectHandler.cpp 87 KB

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