CObjectHandler.cpp 108 KB

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