map.cpp 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139
  1. #define VCMI_DLL
  2. #include "../stdafx.h"
  3. #include "map.h"
  4. #include "CObjectHandler.h"
  5. #include "CDefObjInfoHandler.h"
  6. #include "VCMI_Lib.h"
  7. #include <zlib.h>
  8. #include <boost/crc.hpp>
  9. #include "CLodHandler.h"
  10. #include "CArtHandler.h"
  11. #include "CCreatureHandler.h"
  12. #include <boost/bind.hpp>
  13. #include <assert.h>
  14. #include "CSpellHandler.h"
  15. #include <boost/foreach.hpp>
  16. /*
  17. * map.cpp, part of VCMI engine
  18. *
  19. * Authors: listed in file AUTHORS in main folder
  20. *
  21. * License: GNU General Public License v2.0 or later
  22. * Full text of license available in license.txt file, in main folder
  23. *
  24. */
  25. static std::set<si32> convertBuildings(const std::set<si32> h3m, int castleID)
  26. {
  27. std::map<int,int> mapa;
  28. std::set<si32> ret;
  29. std::ifstream b5(DATA_DIR "/config/buildings5.txt");
  30. while(!b5.eof())
  31. {
  32. int h3, VCMI, town;
  33. b5 >> town >> h3 >> VCMI;
  34. if ( town == castleID || town == -1 )
  35. mapa[h3]=VCMI;
  36. }
  37. for(std::set<si32>::const_iterator i=h3m.begin();i!=h3m.end();i++)
  38. {
  39. if(mapa[*i]>=0)
  40. ret.insert(mapa[*i]);
  41. else if(mapa[*i] >= (-CREATURES_PER_TOWN)) // horde buildings
  42. {
  43. int level = (mapa[*i]);
  44. ret.insert(level-30);//(-30)..(-36) - horde buildings (for game loading only), don't see other way to handle hordes in random towns
  45. }
  46. else
  47. {
  48. tlog3<<"Conversion warning: unknown building "<<*i<<" in castle "<<castleID<<std::endl;
  49. }
  50. }
  51. ret.insert(10); //village hall is always present
  52. ret.insert(-1); //houses near v.hall / eyecandies
  53. ret.insert(-2); //terrain eyecandy, if -1 is used
  54. if(ret.find(11)!=ret.end())
  55. ret.insert(27);
  56. if(ret.find(12)!=ret.end())
  57. ret.insert(28);
  58. if(ret.find(13)!=ret.end())
  59. ret.insert(29);
  60. return ret;
  61. }
  62. static unsigned int intPow(unsigned int a, unsigned int b)
  63. {
  64. unsigned int ret=1;
  65. for(unsigned int i=0; i<b; ++i)
  66. ret*=a;
  67. return ret;
  68. }
  69. static unsigned char reverse(unsigned char arg)
  70. {
  71. unsigned char ret = 0;
  72. for (int i=0; i<8;i++)
  73. {
  74. if(((arg)&(1<<i))>>i)
  75. {
  76. ret |= (128>>i);
  77. }
  78. }
  79. return ret;
  80. }
  81. void readCreatureSet(CCreatureSet *out, const unsigned char * bufor, int &i, int number, bool version) //version==true for >RoE maps
  82. {
  83. const int bytesPerCre = version ? 4 : 3,
  84. idBytes = version ? 2 : 1,
  85. maxID = version ? 0xffff : 0xff;
  86. for(int ir=0;ir < number; ir++)
  87. {
  88. int creID = readNormalNr(bufor,i+ir*bytesPerCre, idBytes);
  89. int count = readNormalNr(bufor,i+ir*bytesPerCre+idBytes, 2);
  90. if(creID == maxID) //empty slot
  91. continue;
  92. CStackInstance *hlp = new CStackInstance();
  93. hlp->count = count;
  94. if(creID > maxID - 0xf)
  95. {
  96. creID = maxID + 1 - creID + VLC->creh->creatures.size();//this will happen when random object has random army
  97. hlp->idRand = creID;
  98. }
  99. else
  100. hlp->setType(creID);
  101. out->putStack(ir, hlp);
  102. }
  103. i+=number*bytesPerCre;
  104. out->validTypes(true);
  105. }
  106. CMapHeader::CMapHeader(const unsigned char *map)
  107. {
  108. int i=0;
  109. initFromMemory(map,i);
  110. }
  111. CMapHeader::CMapHeader()
  112. {
  113. areAnyPLayers = difficulty = levelLimit = howManyTeams = 0;
  114. height = width = twoLevel = -1;
  115. }
  116. void CMapHeader::initFromMemory( const unsigned char *bufor, int &i )
  117. {
  118. version = (Eformat)(readNormalNr(bufor,i)); i+=4; //map version
  119. if(version != RoE && version != AB && version != SoD && version != WoG)
  120. {
  121. throw std::string("Invalid map format!");
  122. }
  123. areAnyPLayers = readChar(bufor,i); //invalid on some maps
  124. height = width = (readNormalNr(bufor,i)); i+=4; // dimensions of map
  125. twoLevel = readChar(bufor,i); //if there is underground
  126. int pom;
  127. name = readString(bufor,i);
  128. description= readString(bufor,i);
  129. difficulty = readChar(bufor,i); // reading map difficulty
  130. if(version != RoE)
  131. levelLimit = readChar(bufor,i); // hero level limit
  132. else
  133. levelLimit = 0;
  134. loadPlayerInfo(pom, bufor, i);
  135. loadViCLossConditions(bufor, i);
  136. howManyTeams=bufor[i++]; //read number of teams
  137. if(howManyTeams>0) //read team numbers
  138. {
  139. for(int rr=0; rr<8; ++rr)
  140. {
  141. players[rr].team = bufor[i++];
  142. }
  143. }
  144. else//no alliances
  145. {
  146. for(int i=0;i<PLAYER_LIMIT;i++)
  147. if(players[i].canComputerPlay || players[i].canHumanPlay)
  148. players[i].team = howManyTeams++;
  149. }
  150. pom = i;
  151. allowedHeroes.resize(HEROES_QUANTITY,false);
  152. for(; i<pom+ (version == RoE ? 16 : 20) ; ++i)
  153. {
  154. unsigned char c = bufor[i];
  155. for(int yy=0; yy<8; ++yy)
  156. if((i-pom)*8+yy < HEROES_QUANTITY)
  157. if(c == (c|((unsigned char)intPow(2, yy))))
  158. allowedHeroes[(i-pom)*8+yy] = true;
  159. }
  160. if(version>RoE) //probably reserved for further heroes
  161. {
  162. int placeholdersQty = readNormalNr(bufor, i); i+=4;
  163. for(int p = 0; p < placeholdersQty; p++)
  164. placeholdedHeroes.push_back(bufor[i++]);
  165. }
  166. }
  167. void CMapHeader::loadPlayerInfo( int &pom, const unsigned char * bufor, int &i )
  168. {
  169. players.resize(8);
  170. for (pom=0;pom<8;pom++)
  171. {
  172. players[pom].canHumanPlay = bufor[i++];
  173. players[pom].canComputerPlay = bufor[i++];
  174. if ((!(players[pom].canHumanPlay || players[pom].canComputerPlay))) //if nobody can play with this player
  175. {
  176. switch(version)
  177. {
  178. case SoD: case WoG:
  179. i+=13;
  180. break;
  181. case AB:
  182. i+=12;
  183. break;
  184. case RoE:
  185. i+=6;
  186. break;
  187. }
  188. continue;
  189. }
  190. players[pom].AITactic = bufor[i++];
  191. if(version == SoD || version == WoG)
  192. players[pom].p7= bufor[i++];
  193. else
  194. players[pom].p7= -1;
  195. //factions this player can choose
  196. players[pom].allowedFactions = 0;
  197. players[pom].allowedFactions += bufor[i++];
  198. if(version != RoE)
  199. players[pom].allowedFactions += (bufor[i++])*256;
  200. players[pom].isFactionRandom = bufor[i++];
  201. players[pom].hasMainTown = bufor[i++];
  202. if (players[pom].hasMainTown)
  203. {
  204. if(version != RoE)
  205. {
  206. players[pom].generateHeroAtMainTown = bufor[i++];
  207. players[pom].generateHero = bufor[i++];
  208. }
  209. else
  210. {
  211. players[pom].generateHeroAtMainTown = true;
  212. players[pom].generateHero = false;
  213. }
  214. players[pom].posOfMainTown.x = bufor[i++];
  215. players[pom].posOfMainTown.y = bufor[i++];
  216. players[pom].posOfMainTown.z = bufor[i++];
  217. }
  218. players[pom].p8= bufor[i++];
  219. players[pom].p9= bufor[i++];
  220. if(players[pom].p9!=0xff)
  221. {
  222. players[pom].mainHeroPortrait = bufor[i++];
  223. players[pom].mainHeroName = readString(bufor,i);
  224. }
  225. if(version != RoE)
  226. {
  227. players[pom].powerPlacehodlers = bufor[i++];//unknown byte
  228. int heroCount = bufor[i++];
  229. i+=3;
  230. for (int pp=0;pp<heroCount;pp++)
  231. {
  232. SheroName vv;
  233. vv.heroID=bufor[i++];
  234. int hnl = bufor[i++];
  235. i+=3;
  236. for (int zz=0;zz<hnl;zz++)
  237. {
  238. vv.heroName+=bufor[i++];
  239. }
  240. players[pom].heroesNames.push_back(vv);
  241. }
  242. }
  243. }
  244. }
  245. void CMapHeader::loadViCLossConditions( const unsigned char * bufor, int &i)
  246. {
  247. victoryCondition.obj = NULL;
  248. victoryCondition.condition = (EvictoryConditions)bufor[i++];
  249. if (victoryCondition.condition != winStandard) //specific victory conditions
  250. {
  251. int nr;
  252. switch (victoryCondition.condition) //read victory conditions
  253. {
  254. case artifact:
  255. {
  256. victoryCondition.ID = bufor[i+2];
  257. nr=(version==RoE ? 1 : 2);
  258. break;
  259. }
  260. case gatherTroop:
  261. {
  262. // int temp1 = bufor[i+2];
  263. // int temp2 = bufor[i+3];
  264. victoryCondition.ID = bufor[i+2];
  265. victoryCondition.count = readNormalNr(bufor, i+(version==RoE ? 3 : 4));
  266. nr=(version==RoE ? 5 : 6);
  267. break;
  268. }
  269. case gatherResource:
  270. {
  271. victoryCondition.ID = bufor[i+2];
  272. victoryCondition.count = readNormalNr(bufor, i+3);
  273. nr = 5;
  274. break;
  275. }
  276. case buildCity:
  277. {
  278. victoryCondition.pos.x = bufor[i+2];
  279. victoryCondition.pos.y = bufor[i+3];
  280. victoryCondition.pos.z = bufor[i+4];
  281. victoryCondition.count = bufor[i+5];
  282. victoryCondition.ID = bufor[i+6];
  283. nr = 5;
  284. break;
  285. }
  286. case buildGrail:
  287. {
  288. if (bufor[i+4]>2)
  289. victoryCondition.pos = int3(-1,-1,-1);
  290. else
  291. {
  292. victoryCondition.pos.x = bufor[i+2];
  293. victoryCondition.pos.y = bufor[i+3];
  294. victoryCondition.pos.z = bufor[i+4];
  295. }
  296. nr = 3;
  297. break;
  298. }
  299. case beatHero:
  300. case captureCity:
  301. case beatMonster:
  302. {
  303. victoryCondition.pos.x = bufor[i+2];
  304. victoryCondition.pos.y = bufor[i+3];
  305. victoryCondition.pos.z = bufor[i+4];
  306. nr = 3;
  307. break;
  308. }
  309. case takeDwellings:
  310. case takeMines:
  311. {
  312. nr = 0;
  313. break;
  314. }
  315. case transportItem:
  316. {
  317. victoryCondition.ID = bufor[i+2];
  318. victoryCondition.pos.x = bufor[i+3];
  319. victoryCondition.pos.y = bufor[i+4];
  320. victoryCondition.pos.z = bufor[i+5];
  321. nr = 4;
  322. break;
  323. }
  324. }
  325. victoryCondition.allowNormalVictory = bufor[i++];
  326. victoryCondition.appliesToAI = bufor[i++];
  327. i+=nr;
  328. }
  329. lossCondition.typeOfLossCon = (ElossCon)bufor[i++];
  330. switch (lossCondition.typeOfLossCon) //read loss conditions
  331. {
  332. case lossCastle:
  333. case lossHero:
  334. {
  335. lossCondition.pos.x=bufor[i++];
  336. lossCondition.pos.y=bufor[i++];
  337. lossCondition.pos.z=bufor[i++];
  338. break;
  339. }
  340. case timeExpires:
  341. {
  342. lossCondition.timeLimit = readNormalNr(bufor,i++,2);
  343. i++;
  344. break;
  345. }
  346. }
  347. }
  348. CMapHeader::~CMapHeader()
  349. {
  350. }
  351. void Mapa::initFromBytes(const unsigned char * bufor)
  352. {
  353. int i=0;
  354. initFromMemory(bufor,i);
  355. timeHandler th;
  356. th.getDif();
  357. readHeader(bufor, i);
  358. tlog0<<"\tReading header: "<<th.getDif()<<std::endl;
  359. if (victoryCondition.condition == artifact || victoryCondition.condition == transportItem)
  360. { //messy, but needed
  361. allowedArtifact[victoryCondition.ID] = false;
  362. }
  363. readRumors(bufor, i);
  364. tlog0<<"\tReading rumors: "<<th.getDif()<<std::endl;
  365. readPredefinedHeroes(bufor, i);
  366. tlog0<<"\tReading predefined heroes: "<<th.getDif()<<std::endl;
  367. readTerrain(bufor, i);
  368. tlog0<<"\tReading terrain: "<<th.getDif()<<std::endl;
  369. readDefInfo(bufor, i);
  370. tlog0<<"\tReading defs info: "<<th.getDif()<<std::endl;
  371. readObjects(bufor, i);
  372. tlog0<<"\tReading objects: "<<th.getDif()<<std::endl;
  373. readEvents(bufor, i);
  374. tlog0<<"\tReading events: "<<th.getDif()<<std::endl;
  375. //map readed, bufor no longer needed
  376. for(unsigned int f=0; f<objects.size(); ++f) //calculationg blocked / visitable positions
  377. {
  378. if(!objects[f]->defInfo)
  379. continue;
  380. addBlockVisTiles(objects[f]);
  381. }
  382. tlog0<<"\tCalculating blocked/visitable tiles: "<<th.getDif()<<std::endl;
  383. checkForObjectives();
  384. tlog0 << "\tMap initialization done!" << std::endl;
  385. }
  386. void Mapa::removeBlockVisTiles(CGObjectInstance * obj, bool total)
  387. {
  388. for(int fx=0; fx<8; ++fx)
  389. {
  390. for(int fy=0; fy<6; ++fy)
  391. {
  392. int xVal = obj->pos.x + fx - 7;
  393. int yVal = obj->pos.y + fy - 5;
  394. int zVal = obj->pos.z;
  395. if(xVal>=0 && xVal<width && yVal>=0 && yVal<height)
  396. {
  397. TerrainTile & curt = terrain[xVal][yVal][zVal];
  398. if(total || ((obj->defInfo->visitMap[fy] >> (7 - fx)) & 1))
  399. {
  400. curt.visitableObjects -= obj;
  401. curt.visitable = curt.visitableObjects.size();
  402. }
  403. if(total || !((obj->defInfo->blockMap[fy] >> (7 - fx)) & 1))
  404. {
  405. curt.blockingObjects -= obj;
  406. curt.blocked = curt.blockingObjects.size();
  407. }
  408. }
  409. }
  410. }
  411. }
  412. void Mapa::addBlockVisTiles(CGObjectInstance * obj)
  413. {
  414. for(int fx=0; fx<8; ++fx)
  415. {
  416. for(int fy=0; fy<6; ++fy)
  417. {
  418. int xVal = obj->pos.x + fx - 7;
  419. int yVal = obj->pos.y + fy - 5;
  420. int zVal = obj->pos.z;
  421. if(xVal>=0 && xVal<width && yVal>=0 && yVal<height)
  422. {
  423. TerrainTile & curt = terrain[xVal][yVal][zVal];
  424. if(((obj->defInfo->visitMap[fy] >> (7 - fx)) & 1))
  425. {
  426. curt.visitableObjects.push_back(obj);
  427. curt.visitable = true;
  428. }
  429. if(!((obj->defInfo->blockMap[fy] >> (7 - fx)) & 1))
  430. {
  431. curt.blockingObjects.push_back(obj);
  432. curt.blocked = true;
  433. }
  434. }
  435. }
  436. }
  437. }
  438. Mapa::Mapa(std::string filename)
  439. :grailPos(-1, -1, -1), grailRadious(0)
  440. {
  441. int mapsize = 0;
  442. tlog0<<"Opening map file: "<<filename<<"\t "<<std::flush;
  443. //load file and decompress
  444. unsigned char * initTable = CLodHandler::getUnpackedFile(filename, &mapsize);
  445. tlog0<<"done."<<std::endl;
  446. // Compute checksum
  447. boost::crc_32_type result;
  448. result.process_bytes(initTable, mapsize);
  449. checksum = result.checksum();
  450. tlog0 << "\tOur map checksum: "<<result.checksum() << std::endl;
  451. initFromBytes(initTable);
  452. delete [] initTable;
  453. }
  454. Mapa::Mapa()
  455. {
  456. terrain = NULL;
  457. }
  458. Mapa::~Mapa()
  459. {
  460. //for(int i=0; i < defy.size(); i++)
  461. // if(defy[i]->serial < 0) //def not present in the main vector in defobjinfo
  462. // delete defy[i];
  463. if(terrain)
  464. {
  465. for (int ii=0;ii<width;ii++)
  466. {
  467. for(int jj=0;jj<height;jj++)
  468. delete [] terrain[ii][jj];
  469. delete [] terrain[ii];
  470. }
  471. delete [] terrain;
  472. }
  473. for(std::list<ConstTransitivePtr<CMapEvent> >::iterator i = events.begin(); i != events.end(); i++)
  474. i->dellNull();
  475. }
  476. CGHeroInstance * Mapa::getHero(int ID, int mode)
  477. {
  478. if (mode != 0)
  479. #ifndef __GNUC__
  480. throw new std::exception("gs->getHero: This mode is not supported!");
  481. #else
  482. throw new std::exception();
  483. #endif
  484. for(unsigned int i=0; i<heroes.size();i++)
  485. if(heroes[i]->subID == ID)
  486. return heroes[i];
  487. return NULL;
  488. }
  489. int Mapa::loadSeerHut( const unsigned char * bufor, int i, CGObjectInstance *& nobj )
  490. {
  491. CGSeerHut *hut = new CGSeerHut();
  492. nobj = hut;
  493. if(version>RoE)
  494. {
  495. loadQuest(hut,bufor,i);
  496. }
  497. else //RoE
  498. {
  499. int artID = bufor[i]; ++i;
  500. if(artID!=255) //not none quest
  501. {
  502. hut->m5arts.push_back(artID);
  503. hut->missionType = 5;
  504. }
  505. else
  506. {
  507. hut->missionType = 0; //no mission
  508. }
  509. hut->isCustom = false;
  510. }
  511. if(hut->missionType)
  512. {
  513. unsigned char rewardType = bufor[i]; ++i;
  514. hut->rewardType = rewardType;
  515. switch(rewardType)
  516. {
  517. case 1:
  518. {
  519. hut->rVal = readNormalNr(bufor,i); i+=4;
  520. break;
  521. }
  522. case 2:
  523. {
  524. hut->rVal = readNormalNr(bufor,i); i+=4;
  525. break;
  526. }
  527. case 3:
  528. {
  529. hut->rVal = bufor[i]; ++i;
  530. break;
  531. }
  532. case 4:
  533. {
  534. hut->rVal = bufor[i]; ++i;
  535. break;
  536. }
  537. case 5:
  538. {
  539. hut->rID = bufor[i]; ++i;
  540. hut->rVal = readNormalNr(bufor,i, 3); i+=3;
  541. i+=1;
  542. break;
  543. }
  544. case 6:
  545. {
  546. hut->rID = bufor[i]; ++i;
  547. hut->rVal = bufor[i]; ++i;
  548. break;
  549. }
  550. case 7:
  551. {
  552. hut->rID = bufor[i]; ++i;
  553. hut->rVal = bufor[i]; ++i;
  554. break;
  555. }
  556. case 8:
  557. {
  558. hut->rID = readNormalNr(bufor,i, (version == RoE ? 1 : 2)); i+=(version == RoE ? 1 : 2);
  559. break;
  560. }
  561. case 9:
  562. {
  563. hut->rID = bufor[i]; ++i;
  564. break;
  565. }
  566. case 10:
  567. {
  568. if(version>RoE)
  569. {
  570. hut->rID = readNormalNr(bufor,i, 2); i+=2;
  571. hut->rVal = readNormalNr(bufor,i, 2); i+=2;
  572. }
  573. else
  574. {
  575. hut->rID = bufor[i]; ++i;
  576. hut->rVal = readNormalNr(bufor,i, 2); i+=2;
  577. }
  578. break;
  579. }
  580. }// end of internal switch
  581. i+=2;
  582. }
  583. else //missionType==255
  584. {
  585. i+=3;
  586. }
  587. return i;
  588. }
  589. void Mapa::loadTown( CGObjectInstance * &nobj, const unsigned char * bufor, int &i, int subid)
  590. {
  591. CGTownInstance * nt = new CGTownInstance();
  592. //(*(static_cast<CGObjectInstance*>(nt))) = *nobj;
  593. //delete nobj;
  594. nobj = nt;
  595. nt->identifier = 0;
  596. if(version>RoE)
  597. {
  598. nt->identifier = readNormalNr(bufor,i); i+=4;
  599. }
  600. nt->tempOwner = bufor[i]; ++i;
  601. if(readChar(bufor,i)) //has name
  602. nt->name = readString(bufor,i);
  603. if(readChar(bufor,i))//true if garrison isn't empty
  604. readCreatureSet(nt, bufor, i, 7, version > RoE);
  605. nt->formation = bufor[i]; ++i;
  606. if(readChar(bufor,i)) //custom buildings info
  607. {
  608. //built buildings
  609. for(int byte=0;byte<6;byte++)
  610. {
  611. for(int bit=0;bit<8;bit++)
  612. if(bufor[i] & (1<<bit))
  613. nt->builtBuildings.insert(byte*8+bit);
  614. i++;
  615. }
  616. //forbidden buildings
  617. for(int byte=6;byte<12;byte++)
  618. {
  619. for(int bit=0;bit<8;bit++)
  620. if(bufor[i] & (1<<bit))
  621. nt->forbiddenBuildings.insert((byte-6)*8+bit);
  622. i++;
  623. }
  624. nt->builtBuildings = convertBuildings(nt->builtBuildings,subid);
  625. nt->forbiddenBuildings = convertBuildings(nt->forbiddenBuildings,subid);
  626. }
  627. else //standard buildings
  628. {
  629. if(readChar(bufor,i)) //has fort
  630. nt->builtBuildings.insert(7);
  631. nt->builtBuildings.insert(-50); //means that set of standard building should be included
  632. }
  633. int ist = i;
  634. if(version>RoE)
  635. {
  636. for(; i<ist+9; ++i)
  637. {
  638. unsigned char c = bufor[i];
  639. for(int yy=0; yy<8; ++yy)
  640. {
  641. if((i-ist)*8+yy < SPELLS_QUANTITY)
  642. {
  643. if(c == (c|((unsigned char)intPow(2, yy))))
  644. nt->obligatorySpells.push_back((i-ist)*8+yy);
  645. }
  646. }
  647. }
  648. }
  649. ist = i;
  650. for(; i<ist+9; ++i)
  651. {
  652. unsigned char c = bufor[i];
  653. for(int yy=0; yy<8; ++yy)
  654. {
  655. if((i-ist)*8+yy < SPELLS_QUANTITY)
  656. {
  657. if(c != (c|((unsigned char)intPow(2, yy))))
  658. nt->possibleSpells.push_back((i-ist)*8+yy);
  659. }
  660. }
  661. }
  662. /////// reading castle events //////////////////////////////////
  663. int numberOfEvent = readNormalNr(bufor,i); i+=4;
  664. for(int gh = 0; gh<numberOfEvent; ++gh)
  665. {
  666. CCastleEvent *nce = new CCastleEvent();
  667. nce->town = nt;
  668. nce->name = readString(bufor,i);
  669. nce->message = readString(bufor,i);
  670. nce->resources.resize(RESOURCE_QUANTITY);
  671. for(int x=0; x < 7; x++)
  672. {
  673. nce->resources[x] = readNormalNr(bufor,i);
  674. i+=4;
  675. }
  676. nce->players = bufor[i]; ++i;
  677. if(version > AB)
  678. {
  679. nce->humanAffected = bufor[i]; ++i;
  680. }
  681. else
  682. nce->humanAffected = true;
  683. nce->computerAffected = bufor[i]; ++i;
  684. nce->firstOccurence = readNormalNr(bufor,i, 2); i+=2;
  685. nce->nextOccurence = bufor[i]; ++i;
  686. i+=17;
  687. //new buildings
  688. for(int byte=0;byte<6;byte++)
  689. {
  690. for(int bit=0;bit<8;bit++)
  691. if(bufor[i] & (1<<bit))
  692. nce->buildings.insert(byte*8+bit);
  693. i++;
  694. }
  695. nce->buildings = convertBuildings(nce->buildings,subid);
  696. nce->creatures.resize(7);
  697. for(int vv=0; vv<7; ++vv)
  698. {
  699. nce->creatures[vv] = readNormalNr(bufor,i, 2);i+=2;
  700. }
  701. i+=4;
  702. nt->events.push_back(nce);
  703. }//castle events have been read
  704. if(version > AB)
  705. {
  706. nt->alignment = bufor[i]; ++i;
  707. }
  708. else
  709. nt->alignment = 0xff;
  710. i+=3;
  711. nt->builded = 0;
  712. nt->destroyed = 0;
  713. nt->garrisonHero = NULL;
  714. }
  715. CGObjectInstance * Mapa::loadHero(const unsigned char * bufor, int &i, int idToBeGiven)
  716. {
  717. CGHeroInstance * nhi = new CGHeroInstance();
  718. int identifier = 0;
  719. if(version > RoE)
  720. {
  721. identifier = readNormalNr(bufor,i, 4); i+=4;
  722. questIdentifierToId[identifier] = idToBeGiven;
  723. }
  724. ui8 owner = bufor[i++];
  725. nhi->subID = readNormalNr(bufor,i, 1); ++i;
  726. for(unsigned int j=0; j<predefinedHeroes.size(); j++)
  727. {
  728. if(predefinedHeroes[j]->subID == nhi->subID)
  729. {
  730. tlog0 << "Hero " << nhi->subID << " will be taken from the predefined heroes list.\n";
  731. delete nhi;
  732. nhi = predefinedHeroes[j];
  733. break;
  734. }
  735. }
  736. nhi->setOwner(owner);
  737. nhi->portrait = nhi->subID;
  738. for(unsigned int j=0; j<disposedHeroes.size(); j++)
  739. {
  740. if(disposedHeroes[j].ID == nhi->subID)
  741. {
  742. nhi->name = disposedHeroes[j].name;
  743. nhi->portrait = disposedHeroes[j].portrait;
  744. break;
  745. }
  746. }
  747. if(readChar(bufor,i))//true if hero has nonstandard name
  748. nhi->name = readString(bufor,i);
  749. if(version>AB)
  750. {
  751. if(readChar(bufor,i))//true if hero's experience is greater than 0
  752. { nhi->exp = readNormalNr(bufor,i); i+=4; }
  753. else
  754. nhi->exp = 0xffffffff;
  755. }
  756. else
  757. {
  758. nhi->exp = readNormalNr(bufor,i); i+=4;
  759. if(!nhi->exp) //0 means "not set" in <=AB maps
  760. nhi->exp = 0xffffffff;
  761. }
  762. bool portrait=bufor[i]; ++i;
  763. if (portrait)
  764. nhi->portrait = bufor[i++];
  765. if(readChar(bufor,i))//true if hero has specified abilities
  766. {
  767. int howMany = readNormalNr(bufor,i); i+=4;
  768. nhi->secSkills.resize(howMany);
  769. for(int yy=0; yy<howMany; ++yy)
  770. {
  771. nhi->secSkills[yy].first = readNormalNr(bufor,i, 1); ++i;
  772. nhi->secSkills[yy].second = readNormalNr(bufor,i, 1); ++i;
  773. }
  774. }
  775. if(readChar(bufor,i))//true if hero has nonstandard garrison
  776. readCreatureSet(nhi, bufor, i, 7, version > RoE);
  777. nhi->formation =bufor[i]; ++i; //formation
  778. loadArtifactsOfHero(bufor, i, nhi);
  779. nhi->patrol.patrolRadious = readNormalNr(bufor,i, 1); ++i;
  780. if(nhi->patrol.patrolRadious == 0xff)
  781. nhi->patrol.patrolling = false;
  782. else
  783. nhi->patrol.patrolling = true;
  784. if(version>RoE)
  785. {
  786. if(readChar(bufor,i))//true if hero has nonstandard (mapmaker defined) biography
  787. nhi->biography = readString(bufor,i);
  788. nhi->sex = bufor[i]; ++i;
  789. if (nhi->sex != 0xFF)//remove trash
  790. nhi->sex &=1;
  791. }
  792. else
  793. nhi->sex = 0xFF;
  794. //spells
  795. if(version>AB)
  796. {
  797. bool areSpells = bufor[i]; ++i;
  798. if(areSpells) //TODO: sprawdziæ //seems to be ok - tow
  799. {
  800. nhi->spells.insert(0xffffffff); //placeholder "preset spells"
  801. int ist = i;
  802. for(; i<ist+9; ++i)
  803. {
  804. unsigned char c = bufor[i];
  805. for(int yy=0; yy<8; ++yy)
  806. {
  807. if((i-ist)*8+yy < SPELLS_QUANTITY)
  808. {
  809. if(c == (c|((unsigned char)intPow(2, yy))))
  810. nhi->spells.insert((i-ist)*8+yy);
  811. }
  812. }
  813. }
  814. }
  815. }
  816. else if(version==AB) //we can read one spell
  817. {
  818. unsigned char buff = bufor[i]; ++i;
  819. if(buff != 254)
  820. {
  821. nhi->spells.insert(0xffffffff); //placeholder "preset spells"
  822. if(buff < 254) //255 means no spells
  823. nhi->spells.insert(buff);
  824. }
  825. }
  826. //spells loaded
  827. if(version>AB)
  828. {
  829. if(readChar(bufor,i))//customPrimSkills
  830. {
  831. for(int xx=0;xx<4;xx++)
  832. nhi->pushPrimSkill(xx, bufor[i++]);
  833. }
  834. }
  835. i+=16;
  836. return nhi;
  837. }
  838. void Mapa::readRumors( const unsigned char * bufor, int &i)
  839. {
  840. int rumNr = readNormalNr(bufor,i,4);i+=4;
  841. for (int it=0;it<rumNr;it++)
  842. {
  843. Rumor ourRumor;
  844. int nameL = readNormalNr(bufor,i,4);i+=4; //read length of name of rumor
  845. for (int zz=0; zz<nameL; zz++)
  846. ourRumor.name+=bufor[i++];
  847. nameL = readNormalNr(bufor,i,4);i+=4; //read length of rumor
  848. for (int zz=0; zz<nameL; zz++)
  849. ourRumor.text+=bufor[i++];
  850. rumors.push_back(ourRumor); //add to our list
  851. }
  852. }
  853. void Mapa::readHeader( const unsigned char * bufor, int &i)
  854. {
  855. //reading allowed heroes (20 bytes)
  856. int ist = i;
  857. ist=i; //starting i for loop
  858. unsigned char disp = 0;
  859. if(version>=SoD)
  860. {
  861. disp = bufor[i++];
  862. disposedHeroes.resize(disp);
  863. for(int g=0; g<disp; ++g)
  864. {
  865. disposedHeroes[g].ID = bufor[i++];
  866. disposedHeroes[g].portrait = bufor[i++];
  867. int lenbuf = readNormalNr(bufor,i); i+=4;
  868. for (int zz=0; zz<lenbuf; zz++)
  869. disposedHeroes[g].name+=bufor[i++];
  870. disposedHeroes[g].players = bufor[i++];
  871. }
  872. }
  873. i+=31; //omitting NULLS
  874. allowedArtifact.resize(ARTIFACTS_QUANTITY);
  875. for (unsigned int x=0; x<allowedArtifact.size(); x++)
  876. allowedArtifact[x] = true;
  877. //reading allowed artifacts: 17 or 18 bytes
  878. if (version!=RoE)
  879. {
  880. ist=i; //starting i for loop
  881. for (; i<ist+(version==AB ? 17 : 18); ++i)
  882. {
  883. unsigned char c = bufor[i];
  884. for (int yy=0; yy<8; ++yy)
  885. {
  886. if ((i-ist)*8+yy < ARTIFACTS_QUANTITY)
  887. {
  888. if (c == (c|((unsigned char)intPow(2, yy))))
  889. allowedArtifact[(i-ist)*8+yy] = false;
  890. }
  891. }
  892. }//allowed artifacts have been read
  893. }
  894. allowedSpell.resize(SPELLS_QUANTITY);
  895. for(unsigned int x=0;x<allowedSpell.size();x++)
  896. allowedSpell[x] = true;
  897. allowedAbilities.resize(SKILL_QUANTITY);
  898. for(unsigned int x=0;x<allowedAbilities.size();x++)
  899. allowedAbilities[x] = true;
  900. if(version>=SoD)
  901. {
  902. //reading allowed spells (9 bytes)
  903. ist=i; //starting i for loop
  904. for(; i<ist+9; ++i)
  905. {
  906. unsigned char c = bufor[i];
  907. for(int yy=0; yy<8; ++yy)
  908. if((i-ist)*8+yy < SPELLS_QUANTITY)
  909. if(c == (c|((unsigned char)intPow(2, yy))))
  910. allowedSpell[(i-ist)*8+yy] = false;
  911. }
  912. //allowed hero's abilities (4 bytes)
  913. ist=i; //starting i for loop
  914. for(; i<ist+4; ++i)
  915. {
  916. unsigned char c = bufor[i];
  917. for(int yy=0; yy<8; ++yy)
  918. {
  919. if((i-ist)*8+yy < SKILL_QUANTITY)
  920. {
  921. if(c == (c|((unsigned char)intPow(2, yy))))
  922. allowedAbilities[(i-ist)*8+yy] = false;
  923. }
  924. }
  925. }
  926. }
  927. }
  928. void Mapa::readPredefinedHeroes( const unsigned char * bufor, int &i)
  929. {
  930. switch(version)
  931. {
  932. case WoG: case SoD:
  933. {
  934. for(int z=0;z<HEROES_QUANTITY;z++) //disposed heroes
  935. {
  936. int custom = bufor[i++];
  937. if(!custom)
  938. continue;
  939. CGHeroInstance * cgh = new CGHeroInstance;
  940. cgh->ID = HEROI_TYPE;
  941. cgh->subID = z;
  942. if(readChar(bufor,i))//true if hore's experience is greater than 0
  943. { cgh->exp = readNormalNr(bufor,i); i+=4; }
  944. else
  945. cgh->exp = 0;
  946. if(readChar(bufor,i))//true if hero has specified abilities
  947. {
  948. int howMany = readNormalNr(bufor,i); i+=4;
  949. cgh->secSkills.resize(howMany);
  950. for(int yy=0; yy<howMany; ++yy)
  951. {
  952. cgh->secSkills[yy].first = readNormalNr(bufor,i, 1); ++i;
  953. cgh->secSkills[yy].second = readNormalNr(bufor,i, 1); ++i;
  954. }
  955. }
  956. loadArtifactsOfHero(bufor, i, cgh);
  957. if(readChar(bufor,i))//customBio
  958. cgh->biography = readString(bufor,i);
  959. int sex = bufor[i++]; // 0xFF is default, 00 male, 01 female //FIXME:unused?
  960. if(readChar(bufor,i))//are spells
  961. {
  962. int ist = i;
  963. for(; i<ist+9; ++i)
  964. {
  965. unsigned char c = bufor[i];
  966. for(int yy=0; yy<8; ++yy)
  967. {
  968. if((i-ist)*8+yy < SPELLS_QUANTITY)
  969. {
  970. if(c == (c|((unsigned char)intPow(2, yy))))
  971. cgh->spells.insert((i-ist)*8+yy);
  972. }
  973. }
  974. }
  975. }
  976. if(readChar(bufor,i))//customPrimSkills
  977. {
  978. for(int xx=0;xx<4;xx++)
  979. cgh->pushPrimSkill(xx, bufor[i++]);
  980. }
  981. predefinedHeroes.push_back(cgh);
  982. }
  983. break;
  984. }
  985. case RoE:
  986. i+=0;
  987. break;
  988. }
  989. }
  990. void Mapa::readTerrain( const unsigned char * bufor, int &i)
  991. {
  992. terrain = new TerrainTile**[width]; // allocate memory
  993. for (int ii=0;ii<width;ii++)
  994. {
  995. terrain[ii] = new TerrainTile*[height]; // allocate memory
  996. for(int jj=0;jj<height;jj++)
  997. terrain[ii][jj] = new TerrainTile[twoLevel+1];
  998. }
  999. for (int c=0; c<width; c++) // reading terrain
  1000. {
  1001. for (int z=0; z<height; z++)
  1002. {
  1003. terrain[z][c][0].tertype = static_cast<TerrainTile::EterrainType>(bufor[i++]);
  1004. terrain[z][c][0].terview = bufor[i++];
  1005. terrain[z][c][0].nuine = static_cast<TerrainTile::Eriver>(bufor[i++]);
  1006. terrain[z][c][0].rivDir = bufor[i++];
  1007. terrain[z][c][0].malle = static_cast<TerrainTile::Eroad>(bufor[i++]);
  1008. terrain[z][c][0].roadDir = bufor[i++];
  1009. terrain[z][c][0].siodmyTajemniczyBajt = bufor[i++];
  1010. terrain[z][c][0].blocked = 0;
  1011. terrain[z][c][0].visitable = 0;
  1012. }
  1013. }
  1014. if (twoLevel) // read underground terrain
  1015. {
  1016. for (int c=0; c<width; c++) // reading terrain
  1017. {
  1018. for (int z=0; z<height; z++)
  1019. {
  1020. terrain[z][c][1].tertype = static_cast<TerrainTile::EterrainType>(bufor[i++]);
  1021. terrain[z][c][1].terview = bufor[i++];
  1022. terrain[z][c][1].nuine = static_cast<TerrainTile::Eriver>(bufor[i++]);
  1023. terrain[z][c][1].rivDir = bufor[i++];
  1024. terrain[z][c][1].malle = static_cast<TerrainTile::Eroad>(bufor[i++]);
  1025. terrain[z][c][1].roadDir = bufor[i++];
  1026. terrain[z][c][1].siodmyTajemniczyBajt = bufor[i++];
  1027. terrain[z][c][1].blocked = 0;
  1028. terrain[z][c][1].visitable = 0;
  1029. }
  1030. }
  1031. }
  1032. }
  1033. void Mapa::readDefInfo( const unsigned char * bufor, int &i)
  1034. {
  1035. int defAmount = readNormalNr(bufor,i); i+=4;
  1036. defy.reserve(defAmount+8);
  1037. for (int idd = 0 ; idd<defAmount; idd++) // reading defs
  1038. {
  1039. CGDefInfo * vinya = new CGDefInfo(); // info about new def
  1040. //reading name
  1041. int nameLength = readNormalNr(bufor,i,4);i+=4;
  1042. vinya->name.reserve(nameLength);
  1043. for (int cd=0;cd<nameLength;cd++)
  1044. {
  1045. vinya->name += bufor[i++];
  1046. }
  1047. std::transform(vinya->name.begin(),vinya->name.end(),vinya->name.begin(),(int(*)(int))toupper);
  1048. unsigned char bytes[12];
  1049. for (int v=0; v<12; v++) // read info
  1050. {
  1051. bytes[v] = bufor[i++];
  1052. }
  1053. vinya->terrainAllowed = readNormalNr(bufor,i,2);i+=2;
  1054. vinya->terrainMenu = readNormalNr(bufor,i,2);i+=2;
  1055. vinya->id = readNormalNr(bufor,i,4);i+=4;
  1056. vinya->subid = readNormalNr(bufor,i,4);i+=4;
  1057. vinya->type = bufor[i++];
  1058. vinya->printPriority = bufor[i++];
  1059. for (int zi=0; zi<6; zi++)
  1060. {
  1061. vinya->blockMap[zi] = reverse(bytes[zi]);
  1062. }
  1063. for (int zi=0; zi<6; zi++)
  1064. {
  1065. vinya->visitMap[zi] = reverse(bytes[6+zi]);
  1066. }
  1067. i+=16;
  1068. if(vinya->id!=HEROI_TYPE && vinya->id!=70)
  1069. {
  1070. CGDefInfo *h = VLC->dobjinfo->gobjs[vinya->id][vinya->subid];
  1071. if(!h)
  1072. {
  1073. //remove fake entry
  1074. VLC->dobjinfo->gobjs[vinya->id].erase(vinya->subid);
  1075. if(VLC->dobjinfo->gobjs[vinya->id].size())
  1076. VLC->dobjinfo->gobjs.erase(vinya->id);
  1077. tlog2<<"\t\tWarning: no defobjinfo entry for object ID="<<vinya->id<<" subID=" << vinya->subid<<std::endl;
  1078. }
  1079. else
  1080. {
  1081. vinya->visitDir = VLC->dobjinfo->gobjs[vinya->id][vinya->subid]->visitDir;
  1082. }
  1083. }
  1084. else
  1085. {
  1086. vinya->visitDir = 0xff;
  1087. }
  1088. if(vinya->id == EVENTI_TYPE)
  1089. std::memset(vinya->blockMap,255,6);
  1090. //calculating coverageMap
  1091. vinya->fetchInfoFromMSK();
  1092. defy.push_back(vinya); // add this def to the vector
  1093. }
  1094. //add holes - they always can appear
  1095. for (int i = 0; i < 8 ; i++)
  1096. {
  1097. defy.push_back(VLC->dobjinfo->gobjs[124][i]);
  1098. }
  1099. }
  1100. class _HERO_SORTER
  1101. {
  1102. public:
  1103. bool operator()(const ConstTransitivePtr<CGHeroInstance> & a, const ConstTransitivePtr<CGHeroInstance> & b)
  1104. {
  1105. return a->subID < b->subID;
  1106. }
  1107. };
  1108. void Mapa::readObjects( const unsigned char * bufor, int &i)
  1109. {
  1110. int howManyObjs = readNormalNr(bufor,i, 4); i+=4;
  1111. for(int ww=0; ww<howManyObjs; ++ww) //comment this line to turn loading objects off
  1112. {
  1113. CGObjectInstance * nobj = 0;
  1114. int3 pos;
  1115. pos.x = bufor[i++];
  1116. pos.y = bufor[i++];
  1117. pos.z = bufor[i++];
  1118. int defnum = readNormalNr(bufor,i, 4); i+=4;
  1119. int idToBeGiven = objects.size();
  1120. CGDefInfo * defInfo = defy[defnum];
  1121. i+=5;
  1122. switch(defInfo->id)
  1123. {
  1124. case EVENTI_TYPE: //for event objects
  1125. {
  1126. CGEvent *evnt = new CGEvent();
  1127. nobj = evnt;
  1128. bool guardMess = bufor[i]; ++i;
  1129. if(guardMess)
  1130. {
  1131. int messLong = readNormalNr(bufor,i, 4); i+=4;
  1132. if(messLong>0)
  1133. {
  1134. for(int yy=0; yy<messLong; ++yy)
  1135. {
  1136. evnt->message +=bufor[i+yy];
  1137. }
  1138. i+=messLong;
  1139. }
  1140. if(bufor[i++])
  1141. {
  1142. readCreatureSet(evnt, bufor, i, 7, version > RoE);
  1143. }
  1144. i+=4;
  1145. }
  1146. evnt->gainedExp = readNormalNr(bufor,i, 4); i+=4;
  1147. evnt->manaDiff = readNormalNr(bufor,i, 4); i+=4;
  1148. evnt->moraleDiff = readNormalNr(bufor,i, 1, true); ++i;
  1149. evnt->luckDiff = readNormalNr(bufor,i, 1, true); ++i;
  1150. evnt->resources.resize(RESOURCE_QUANTITY);
  1151. for(int x=0; x<7; x++)
  1152. {
  1153. evnt->resources[x] = readNormalNr(bufor,i);
  1154. i+=4;
  1155. }
  1156. evnt->primskills.resize(PRIMARY_SKILLS);
  1157. for(int x=0; x<4; x++)
  1158. {
  1159. evnt->primskills[x] = readNormalNr(bufor,i, 1);
  1160. i++;
  1161. }
  1162. int gabn; //number of gained abilities
  1163. gabn = readNormalNr(bufor,i, 1); ++i;
  1164. for(int oo = 0; oo<gabn; ++oo)
  1165. {
  1166. evnt->abilities.push_back(readNormalNr(bufor,i, 1)); ++i;
  1167. evnt->abilityLevels.push_back(readNormalNr(bufor,i, 1)); ++i;
  1168. }
  1169. int gart = readNormalNr(bufor,i, 1); ++i; //number of gained artifacts
  1170. for(int oo = 0; oo<gart; ++oo)
  1171. {
  1172. evnt->artifacts.push_back(readNormalNr(bufor,i, (version == RoE ? 1 : 2))); i+=(version == RoE ? 1 : 2);
  1173. }
  1174. int gspel = readNormalNr(bufor,i, 1); ++i; //number of gained spells
  1175. for(int oo = 0; oo<gspel; ++oo)
  1176. {
  1177. evnt->spells.push_back(readNormalNr(bufor,i, 1)); ++i;
  1178. }
  1179. int gcre = readNormalNr(bufor,i, 1); ++i; //number of gained creatures
  1180. readCreatureSet(&evnt->creatures, bufor,i,gcre,(version>RoE));
  1181. i+=8;
  1182. evnt->availableFor = readNormalNr(bufor,i, 1); ++i;
  1183. evnt->computerActivate = readNormalNr(bufor,i, 1); ++i;
  1184. evnt->removeAfterVisit = readNormalNr(bufor,i, 1); ++i;
  1185. evnt->humanActivate = true;
  1186. i+=4;
  1187. break;
  1188. }
  1189. case 34: case 70: case 62: //34 - hero; 70 - random hero; 62 - prison
  1190. {
  1191. nobj = loadHero(bufor, i, idToBeGiven);
  1192. break;
  1193. }
  1194. case 4: //Arena
  1195. case 51: //Mercenary Camp
  1196. case 23: //Marletto Tower
  1197. case 61: // Star Axis
  1198. case 32: // Garden of Revelation
  1199. case 100: //Learning Stone
  1200. case 102: //Tree of Knowledge
  1201. case 41: //Library of Enlightenment
  1202. case 47: //School of Magic
  1203. case 107: //School of War
  1204. {
  1205. nobj = new CGVisitableOPH();
  1206. break;
  1207. }
  1208. case 55: //mystical garden
  1209. case 112://windmill
  1210. case 109://water wheel
  1211. {
  1212. nobj = new CGVisitableOPW();
  1213. break;
  1214. }
  1215. case 43: //teleport
  1216. case 44: //teleport
  1217. case 45: //teleport
  1218. case 103://subterranean gate
  1219. case 111://Whirlpool
  1220. {
  1221. nobj = new CGTeleport();
  1222. break;
  1223. }
  1224. case 12: //campfire
  1225. case 29: //Flotsam
  1226. case 82: //Sea Chest
  1227. case 86: //Shipwreck Survivor
  1228. case 101://treasure chest
  1229. {
  1230. nobj = new CGPickable();
  1231. break;
  1232. }
  1233. case 54: //Monster
  1234. case 71: case 72: case 73: case 74: case 75: // Random Monster 1 - 4
  1235. case 162: case 163: case 164: // Random Monster 5 - 7
  1236. {
  1237. CGCreature *cre = new CGCreature();
  1238. nobj = cre;
  1239. if(version>RoE)
  1240. {
  1241. cre->identifier = readNormalNr(bufor,i); i+=4;
  1242. questIdentifierToId[cre->identifier] = idToBeGiven;
  1243. //monsters[cre->identifier] = cre;
  1244. }
  1245. CStackInstance *hlp = new CStackInstance();
  1246. hlp->count = readNormalNr(bufor,i, 2); i+=2;
  1247. //type will be set during initialization
  1248. cre->putStack(0, hlp);
  1249. cre->character = bufor[i]; ++i;
  1250. bool isMesTre = bufor[i]; ++i; //true if there is message or treasury
  1251. if(isMesTre)
  1252. {
  1253. cre->message = readString(bufor,i);
  1254. cre->resources.resize(RESOURCE_QUANTITY);
  1255. for(int j=0; j<7; j++)
  1256. {
  1257. cre->resources[j] = readNormalNr(bufor,i); i+=4;
  1258. }
  1259. int artID = readNormalNr(bufor,i, (version == RoE ? 1 : 2)); i+=(version == RoE ? 1 : 2);
  1260. if(version==RoE)
  1261. {
  1262. if(artID!=0xff)
  1263. cre->gainedArtifact = artID;
  1264. else
  1265. cre->gainedArtifact = -1;
  1266. }
  1267. else
  1268. {
  1269. if(artID!=0xffff)
  1270. cre->gainedArtifact = artID;
  1271. else
  1272. cre->gainedArtifact = -1;
  1273. }
  1274. }
  1275. cre->neverFlees = bufor[i]; ++i;
  1276. cre->notGrowingTeam = bufor[i]; ++i;
  1277. i+=2;;
  1278. break;
  1279. }
  1280. case 59: case 91: //ocean bottle and sign
  1281. {
  1282. CGSignBottle *sb = new CGSignBottle();
  1283. nobj = sb;
  1284. sb->message = readString(bufor,i);
  1285. i+=4;
  1286. break;
  1287. }
  1288. case 83: //seer's hut
  1289. {
  1290. i = loadSeerHut(bufor, i, nobj);
  1291. break;
  1292. }
  1293. case 113: //witch hut
  1294. {
  1295. CGWitchHut *wh = new CGWitchHut();
  1296. nobj = wh;
  1297. if(version>RoE) //in reo we cannot specify it - all are allowed (I hope)
  1298. {
  1299. int ist=i; //starting i for loop
  1300. for(; i<ist+4; ++i)
  1301. {
  1302. unsigned char c = bufor[i];
  1303. for(int yy=0; yy<8; ++yy)
  1304. {
  1305. if((i-ist)*8+yy < SKILL_QUANTITY)
  1306. {
  1307. if(c == (c|((unsigned char)intPow(2, yy))))
  1308. wh->allowedAbilities.push_back((i-ist)*8+yy);
  1309. }
  1310. }
  1311. }
  1312. }
  1313. else //(RoE map)
  1314. {
  1315. for(int gg=0; gg<SKILL_QUANTITY; ++gg)
  1316. {
  1317. wh->allowedAbilities.push_back(gg);
  1318. }
  1319. }
  1320. break;
  1321. }
  1322. case 81: //scholar
  1323. {
  1324. CGScholar *sch = new CGScholar();
  1325. nobj = sch;
  1326. sch->bonusType = bufor[i++];
  1327. sch->bonusID = bufor[i++];
  1328. i+=6;
  1329. break;
  1330. }
  1331. case 33: case 219: //garrison
  1332. {
  1333. CGGarrison *gar = new CGGarrison();
  1334. nobj = gar;
  1335. nobj->setOwner(bufor[i++]);
  1336. i+=3;
  1337. readCreatureSet(gar, bufor, i, 7, version > RoE);
  1338. if(version > RoE)
  1339. {
  1340. gar->removableUnits = bufor[i]; ++i;
  1341. }
  1342. else
  1343. gar->removableUnits = true;
  1344. i+=8;
  1345. break;
  1346. }
  1347. case 5: //artifact
  1348. case 65: case 66: case 67: case 68: case 69: //random artifact
  1349. case 93: //spell scroll
  1350. {
  1351. int artID = -1;
  1352. int spellID = -1;
  1353. CGArtifact *art = new CGArtifact();
  1354. nobj = art;
  1355. bool areSettings = bufor[i++];
  1356. if(areSettings)
  1357. {
  1358. art->message = readString(bufor,i);
  1359. bool areGuards = bufor[i++];
  1360. if(areGuards)
  1361. {
  1362. readCreatureSet(art, bufor, i, 7, version > RoE);
  1363. }
  1364. i+=4;
  1365. }
  1366. if(defInfo->id==93)
  1367. {
  1368. spellID = readNormalNr(bufor,i); i+=4;
  1369. artID = 1;
  1370. }
  1371. else if(defInfo->id == 5) //specific artifact
  1372. {
  1373. artID = defInfo->subid;
  1374. }
  1375. art->storedArtifact = createArt(artID, spellID);
  1376. break;
  1377. }
  1378. case 76: case 79: //random resource; resource
  1379. {
  1380. CGResource *res = new CGResource();
  1381. nobj = res;
  1382. bool isMessGuard = bufor[i]; ++i;
  1383. if(isMessGuard)
  1384. {
  1385. res->message = readString(bufor,i);
  1386. if(bufor[i++])
  1387. {
  1388. readCreatureSet(res, bufor, i, 7, version > RoE);
  1389. }
  1390. i+=4;
  1391. }
  1392. res->amount = readNormalNr(bufor,i); i+=4;
  1393. if (defInfo->subid == 6) // Gold is multiplied by 100.
  1394. res->amount *= 100;
  1395. i+=4;
  1396. break;
  1397. }
  1398. case 77: case 98: //random town; town
  1399. {
  1400. loadTown(nobj, bufor, i, defInfo->subid);
  1401. break;
  1402. }
  1403. case 53:
  1404. case 220://mine (?)
  1405. {
  1406. nobj = new CGMine();
  1407. nobj->setOwner(bufor[i++]);
  1408. i+=3;
  1409. break;
  1410. }
  1411. case 17: case 18: case 19: case 20: //dwellings
  1412. {
  1413. nobj = new CGDwelling();
  1414. nobj->setOwner(bufor[i++]);
  1415. i+=3;
  1416. break;
  1417. }
  1418. case 78: //Refugee Camp
  1419. case 106: //War Machine Factory
  1420. {
  1421. nobj = new CGDwelling();
  1422. break;
  1423. }
  1424. case 88: case 89: case 90: //spell shrine
  1425. {
  1426. CGShrine * shr = new CGShrine();
  1427. nobj = shr;
  1428. shr->spell = bufor[i]; i+=4;
  1429. break;
  1430. }
  1431. case 6: //pandora's box
  1432. {
  1433. CGPandoraBox *box = new CGPandoraBox();
  1434. nobj = box;
  1435. bool messg = bufor[i]; ++i;
  1436. if(messg)
  1437. {
  1438. box->message = readString(bufor,i);
  1439. if(bufor[i++])
  1440. {
  1441. readCreatureSet(box, bufor, i, 7, version > RoE);
  1442. }
  1443. i+=4;
  1444. }
  1445. box->gainedExp = readNormalNr(bufor,i, 4); i+=4;
  1446. box->manaDiff = readNormalNr(bufor,i, 4); i+=4;
  1447. box->moraleDiff = readNormalNr(bufor,i, 1, true); ++i;
  1448. box->luckDiff = readNormalNr(bufor,i, 1, true); ++i;
  1449. box->resources.resize(RESOURCE_QUANTITY);
  1450. for(int x=0; x<7; x++)
  1451. {
  1452. box->resources[x] = readNormalNr(bufor,i);
  1453. i+=4;
  1454. }
  1455. box->primskills.resize(PRIMARY_SKILLS);
  1456. for(int x=0; x<4; x++)
  1457. {
  1458. box->primskills[x] = readNormalNr(bufor,i, 1);
  1459. i++;
  1460. }
  1461. int gabn; //number of gained abilities
  1462. gabn = readNormalNr(bufor,i, 1); ++i;
  1463. for(int oo = 0; oo<gabn; ++oo)
  1464. {
  1465. box->abilities.push_back(readNormalNr(bufor,i, 1)); ++i;
  1466. box->abilityLevels.push_back(readNormalNr(bufor,i, 1)); ++i;
  1467. }
  1468. int gart = readNormalNr(bufor,i, 1); ++i; //number of gained artifacts
  1469. for(int oo = 0; oo<gart; ++oo)
  1470. {
  1471. if(version > RoE)
  1472. {
  1473. box->artifacts.push_back(readNormalNr(bufor,i, 2)); i+=2;
  1474. }
  1475. else
  1476. {
  1477. box->artifacts.push_back(readNormalNr(bufor,i, 1)); i+=1;
  1478. }
  1479. }
  1480. int gspel = readNormalNr(bufor,i, 1); ++i; //number of gained spells
  1481. for(int oo = 0; oo<gspel; ++oo)
  1482. {
  1483. box->spells.push_back(readNormalNr(bufor,i, 1)); ++i;
  1484. }
  1485. int gcre = readNormalNr(bufor,i, 1); ++i; //number of gained creatures
  1486. readCreatureSet(&box->creatures, bufor,i,gcre,(version>RoE));
  1487. i+=8;
  1488. break;
  1489. }
  1490. case 36: //grail
  1491. {
  1492. grailPos = pos;
  1493. grailRadious = readNormalNr(bufor,i); i+=4;
  1494. continue;
  1495. }
  1496. case 217:
  1497. {
  1498. nobj = new CGDwelling();
  1499. CCreGenObjInfo * spec = new CCreGenObjInfo;
  1500. spec->player = readNormalNr(bufor,i); i+=4;
  1501. spec->identifier = readNormalNr(bufor,i); i+=4;
  1502. if(!spec->identifier)
  1503. {
  1504. spec->asCastle = false;
  1505. spec->castles[0] = bufor[i]; ++i;
  1506. spec->castles[1] = bufor[i]; ++i;
  1507. }
  1508. else
  1509. {
  1510. spec->asCastle = true;
  1511. }
  1512. nobj->setOwner(spec->player);
  1513. static_cast<CGDwelling*>(nobj)->info = spec;
  1514. break;
  1515. }
  1516. case 216:
  1517. {
  1518. nobj = new CGDwelling();
  1519. CCreGen2ObjInfo * spec = new CCreGen2ObjInfo;
  1520. spec->player = readNormalNr(bufor,i); i+=4;
  1521. spec->identifier = readNormalNr(bufor,i); i+=4;
  1522. if(!spec->identifier)
  1523. {
  1524. spec->asCastle = false;
  1525. spec->castles[0] = bufor[i]; ++i;
  1526. spec->castles[1] = bufor[i]; ++i;
  1527. }
  1528. else
  1529. {
  1530. spec->asCastle = true;
  1531. }
  1532. spec->minLevel = bufor[i]; ++i;
  1533. spec->maxLevel = bufor[i]; ++i;
  1534. nobj->setOwner(spec->player);
  1535. static_cast<CGDwelling*>(nobj)->info = spec;
  1536. break;
  1537. }
  1538. case 218:
  1539. {
  1540. nobj = new CGDwelling();
  1541. CCreGen3ObjInfo * spec = new CCreGen3ObjInfo;
  1542. spec->player = bufor[i]; ++i;
  1543. i+=3;
  1544. spec->minLevel = bufor[i]; ++i;
  1545. spec->maxLevel = bufor[i]; ++i;
  1546. if(spec->maxLevel>7)
  1547. spec->maxLevel = 7;
  1548. if(spec->minLevel<1)
  1549. spec->minLevel = 1;
  1550. nobj->setOwner(spec->player);
  1551. static_cast<CGDwelling*>(nobj)->info = spec;
  1552. break;
  1553. }
  1554. case 215:
  1555. {
  1556. CGQuestGuard *guard = new CGQuestGuard();
  1557. nobj = guard;
  1558. loadQuest(guard, bufor, i);
  1559. break;
  1560. }
  1561. case 28: //faerie ring
  1562. case 14: //Swan pond
  1563. case 38: //idol of fortune
  1564. case 30: //Fountain of Fortune
  1565. case 64: //Rally Flag
  1566. case 56: //oasis
  1567. case 96: //temple
  1568. case 110://Watering Hole
  1569. case 31: //Fountain of Youth
  1570. case 11: //Buoy
  1571. case 52: //Mermaid
  1572. case 94: //Stables
  1573. {
  1574. nobj = new CGBonusingObject();
  1575. break;
  1576. }
  1577. case 49: //Magic Well
  1578. {
  1579. nobj = new CGMagicWell();
  1580. break;
  1581. }
  1582. case 15: //Cover of darkness
  1583. case 58: //Redwood Observatory
  1584. case 60: //Pillar of Fire
  1585. {
  1586. nobj = new CGObservatory();
  1587. break;
  1588. }
  1589. case 22: //Corpse
  1590. case 39: //Lean To
  1591. case 105://Wagon
  1592. case 108://Warrior's Tomb
  1593. {
  1594. nobj = new CGOnceVisitable();
  1595. break;
  1596. }
  1597. case 8: //Boat
  1598. {
  1599. nobj = new CGBoat();
  1600. break;
  1601. }
  1602. case 92: //Sirens
  1603. {
  1604. nobj = new CGSirens();
  1605. break;
  1606. }
  1607. case 87: //Shipyard
  1608. {
  1609. nobj = new CGShipyard();
  1610. nobj->setOwner(readNormalNr(bufor,i)); i+=4;
  1611. break;
  1612. }
  1613. case 214: //hero placeholder
  1614. {
  1615. CGHeroPlaceholder *hp = new CGHeroPlaceholder();;
  1616. nobj = hp;
  1617. int a = bufor[i++]; //unkown byte, seems to be always 0 (if not - scream!)
  1618. tlog2 << "Unhandled Hero Placeholder detected\n";
  1619. int htid = bufor[i++]; //hero type id
  1620. nobj->subID = htid;
  1621. if(htid == 0xff)
  1622. hp->power = bufor[i++];
  1623. else
  1624. hp->power = 0;
  1625. break;
  1626. }
  1627. case 10: //Keymaster
  1628. {
  1629. nobj = new CGKeymasterTent();
  1630. break;
  1631. }
  1632. case 9: //Border Guard
  1633. {
  1634. nobj = new CGBorderGuard();
  1635. break;
  1636. }
  1637. case 212: //Border Gate
  1638. {
  1639. nobj = new CGBorderGate();
  1640. break;
  1641. }
  1642. case 27: case 37: //Eye and Hut of Magi
  1643. {
  1644. nobj = new CGMagi();
  1645. break;
  1646. }
  1647. case 16: case 24: case 25: case 84: case 85: //treasure bank
  1648. {
  1649. nobj = new CBank();
  1650. break;
  1651. }
  1652. case 63: //Pyramid
  1653. {
  1654. nobj = new CGPyramid();
  1655. break;
  1656. }
  1657. case 13: //Cartographer
  1658. {
  1659. nobj = new CCartographer();
  1660. break;
  1661. }
  1662. case 48: //Magic Spring
  1663. {
  1664. nobj = new CGMagicSpring();
  1665. break;
  1666. }
  1667. case 97: //den of thieves
  1668. {
  1669. nobj = new CGDenOfthieves();
  1670. break;
  1671. }
  1672. case 57: //Obelisk
  1673. {
  1674. nobj = new CGObelisk();
  1675. break;
  1676. }
  1677. case 42: //Lighthouse
  1678. {
  1679. nobj = new CGLighthouse();
  1680. nobj->tempOwner = readNormalNr(bufor,i); i+=4;
  1681. break;
  1682. }
  1683. case 2: //Altar of Sacrifice
  1684. case 99: //Trading Post
  1685. case 213: //Freelancer's Guild
  1686. case 221: //Trading Post (snow)
  1687. {
  1688. nobj = new CGMarket();
  1689. break;
  1690. }
  1691. case 104: //University
  1692. {
  1693. nobj = new CGUniversity();
  1694. break;
  1695. }
  1696. case 7: //Black Market
  1697. {
  1698. nobj = new CGBlackMarket();
  1699. break;
  1700. }
  1701. default: //any other object
  1702. {
  1703. nobj = new CGObjectInstance();
  1704. break;
  1705. }
  1706. } //end of main switch
  1707. nobj->pos = pos;
  1708. nobj->ID = defInfo->id;
  1709. nobj->id = idToBeGiven;
  1710. if(nobj->ID != HEROI_TYPE && nobj->ID != 214 && nobj->ID != 62)
  1711. nobj->subID = defInfo->subid;
  1712. nobj->defInfo = defInfo;
  1713. assert(idToBeGiven == objects.size());
  1714. objects.push_back(nobj);
  1715. if(nobj->ID==TOWNI_TYPE)
  1716. towns.push_back(static_cast<CGTownInstance*>(nobj));
  1717. if(nobj->ID==HEROI_TYPE)
  1718. heroes.push_back(static_cast<CGHeroInstance*>(nobj));
  1719. }
  1720. std::sort(heroes.begin(), heroes.end(), _HERO_SORTER());
  1721. }
  1722. void Mapa::readEvents( const unsigned char * bufor, int &i )
  1723. {
  1724. int numberOfEvents = readNormalNr(bufor,i); i+=4;
  1725. for(int yyoo=0; yyoo<numberOfEvents; ++yyoo)
  1726. {
  1727. CMapEvent *ne = new CMapEvent();
  1728. ne->name = std::string();
  1729. ne->message = std::string();
  1730. int nameLen = readNormalNr(bufor,i); i+=4;
  1731. for(int qq=0; qq<nameLen; ++qq)
  1732. {
  1733. ne->name += bufor[i]; ++i;
  1734. }
  1735. int messLen = readNormalNr(bufor,i); i+=4;
  1736. for(int qq=0; qq<messLen; ++qq)
  1737. {
  1738. ne->message +=bufor[i]; ++i;
  1739. }
  1740. ne->resources.resize(RESOURCE_QUANTITY);
  1741. for(int k=0; k < 7; k++)
  1742. {
  1743. ne->resources[k] = readNormalNr(bufor,i); i+=4;
  1744. }
  1745. ne->players = bufor[i]; ++i;
  1746. if(version>AB)
  1747. {
  1748. ne->humanAffected = bufor[i]; ++i;
  1749. }
  1750. else
  1751. ne->humanAffected = true;
  1752. ne->computerAffected = bufor[i]; ++i;
  1753. ne->firstOccurence = readNormalNr(bufor,i, 2); i+=2;
  1754. ne->nextOccurence = bufor[i]; ++i;
  1755. char unknown[17];
  1756. memcpy(unknown, bufor+i, 17);
  1757. i+=17;
  1758. events.push_back(ne);
  1759. }
  1760. }
  1761. bool Mapa::isInTheMap(const int3 &pos) const
  1762. {
  1763. if(pos.x<0 || pos.y<0 || pos.z<0 || pos.x >= width || pos.y >= height || pos.z > twoLevel)
  1764. return false;
  1765. else return true;
  1766. }
  1767. void Mapa::loadQuest(CQuest * guard, const unsigned char * bufor, int & i)
  1768. {
  1769. guard->missionType = bufor[i]; ++i;
  1770. //int len1, len2, len3;
  1771. switch(guard->missionType)
  1772. {
  1773. case 0:
  1774. return;
  1775. case 2:
  1776. {
  1777. guard->m2stats.resize(4);
  1778. for(int x=0; x<4; x++)
  1779. {
  1780. guard->m2stats[x] = bufor[i++];
  1781. }
  1782. }
  1783. break;
  1784. case 1:
  1785. case 3:
  1786. case 4:
  1787. {
  1788. guard->m13489val = readNormalNr(bufor,i); i+=4;
  1789. break;
  1790. }
  1791. case 5:
  1792. {
  1793. int artNumber = bufor[i]; ++i;
  1794. for(int yy=0; yy<artNumber; ++yy)
  1795. {
  1796. int artid = readNormalNr(bufor,i, 2); i+=2;
  1797. guard->m5arts.push_back(artid);
  1798. allowedArtifact[artid] = false; //these are unavaliable for random generation
  1799. }
  1800. break;
  1801. }
  1802. case 6:
  1803. {
  1804. int typeNumber = bufor[i]; ++i;
  1805. guard->m6creatures.resize(typeNumber);
  1806. for(int hh=0; hh<typeNumber; ++hh)
  1807. {
  1808. guard->m6creatures[hh].type = VLC->creh->creatures[readNormalNr(bufor,i, 2)]; i+=2;
  1809. guard->m6creatures[hh].count = readNormalNr(bufor,i, 2); i+=2;
  1810. }
  1811. break;
  1812. }
  1813. case 7:
  1814. {
  1815. guard->m7resources.resize(7);
  1816. for(int x=0; x<7; x++)
  1817. {
  1818. guard->m7resources[x] = readNormalNr(bufor,i);
  1819. i+=4;
  1820. }
  1821. break;
  1822. }
  1823. case 8:
  1824. case 9:
  1825. {
  1826. guard->m13489val = bufor[i]; ++i;
  1827. break;
  1828. }
  1829. }
  1830. int limit = readNormalNr(bufor,i); i+=4;
  1831. if(limit == ((int)0xffffffff))
  1832. {
  1833. guard->lastDay = -1;
  1834. }
  1835. else
  1836. {
  1837. guard->lastDay = limit;
  1838. }
  1839. guard->firstVisitText = readString(bufor,i);
  1840. guard->nextVisitText = readString(bufor,i);
  1841. guard->completedText = readString(bufor,i);
  1842. if (guard->firstVisitText.size() && guard->nextVisitText.size() && guard->completedText.size())
  1843. guard->isCustom = true;
  1844. else
  1845. guard->isCustom = false; //randomize all if any text is missing
  1846. }
  1847. TerrainTile & Mapa::getTile( const int3 & tile )
  1848. {
  1849. return terrain[tile.x][tile.y][tile.z];
  1850. }
  1851. const TerrainTile & Mapa::getTile( const int3 & tile ) const
  1852. {
  1853. return terrain[tile.x][tile.y][tile.z];
  1854. }
  1855. bool Mapa::isWaterTile(const int3 &pos) const
  1856. {
  1857. return isInTheMap(pos) && getTile(pos).tertype == TerrainTile::water;
  1858. }
  1859. void Mapa::checkForObjectives()
  1860. {
  1861. if(isInTheMap(victoryCondition.pos))
  1862. {
  1863. const std::vector <CGObjectInstance*> &objs = getTile(victoryCondition.pos).visitableObjects;
  1864. assert(objs.size());
  1865. victoryCondition.obj = objs.front();
  1866. }
  1867. if(isInTheMap(lossCondition.pos))
  1868. {
  1869. const std::vector <CGObjectInstance*> &objs = getTile(lossCondition.pos).visitableObjects;
  1870. assert(objs.size());
  1871. lossCondition.obj = objs.front();
  1872. }
  1873. }
  1874. void Mapa::addNewArtifactInstance( CArtifactInstance *art )
  1875. {
  1876. art->id = artInstances.size();
  1877. artInstances.push_back(art);
  1878. }
  1879. bool Mapa::loadArtifactToSlot(CGHeroInstance *h, int slot, const unsigned char * bufor, int &i)
  1880. {
  1881. const int artmask = version == RoE ? 0xff : 0xffff;
  1882. const int artidlen = version == RoE ? 1 : 2;
  1883. int aid = readNormalNr(bufor,i, artidlen); i+=artidlen;
  1884. bool isArt = aid != artmask;
  1885. if(isArt)
  1886. {
  1887. if(vstd::contains(VLC->arth->bigArtifacts, aid) && slot >= Arts::BACKPACK_START)
  1888. tlog3 << "Warning: A big artifact (war machine) in hero's backpack, ignoring...\n";
  1889. else
  1890. h->putArtifact(slot, createArt(aid));
  1891. }
  1892. return isArt;
  1893. }
  1894. void Mapa::loadArtifactsOfHero(const unsigned char * bufor, int & i, CGHeroInstance * nhi)
  1895. {
  1896. bool artSet = bufor[i]; ++i; //true if artifact set is not default (hero has some artifacts)
  1897. if(artSet)
  1898. {
  1899. for(int pom=0;pom<16;pom++)
  1900. loadArtifactToSlot(nhi, pom, bufor, i);
  1901. //misc5 art //17
  1902. if(version >= SoD)
  1903. {
  1904. if(!loadArtifactToSlot(nhi, Arts::MACH4, bufor, i))
  1905. nhi->putArtifact(Arts::MACH4, createArt(Arts::ID_CATAPULT)); //catapult by default
  1906. }
  1907. loadArtifactToSlot(nhi, Arts::SPELLBOOK, bufor, i);
  1908. //19 //???what is that? gap in file or what? - it's probably fifth slot..
  1909. if(version > RoE)
  1910. loadArtifactToSlot(nhi, Arts::MISC5, bufor, i);
  1911. else
  1912. i+=1;
  1913. //bag artifacts //20
  1914. int amount = readNormalNr(bufor,i, 2); i+=2; //number of artifacts in hero's bag
  1915. for(int ss = 0; ss < amount; ++ss)
  1916. loadArtifactToSlot(nhi, Arts::BACKPACK_START + nhi->artifactsInBackpack.size(), bufor, i);
  1917. } //artifacts
  1918. }
  1919. CArtifactInstance * Mapa::createArt(int aid, int spellID /*= -1*/)
  1920. {
  1921. CArtifactInstance *a = NULL;
  1922. if(aid >= 0)
  1923. {
  1924. if(spellID < 0)
  1925. a = CArtifactInstance::createNewArtifactInstance(aid);
  1926. else
  1927. a = CArtifactInstance::createScroll(VLC->spellh->spells[spellID]);
  1928. }
  1929. else
  1930. a = new CArtifactInstance();
  1931. addNewArtifactInstance(a);
  1932. if(a->artType && a->artType->constituents) //TODO make it nicer
  1933. {
  1934. CCombinedArtifactInstance *comb = dynamic_cast<CCombinedArtifactInstance*>(a);
  1935. BOOST_FOREACH(CCombinedArtifactInstance::ConstituentInfo &ci, comb->constituentsInfo)
  1936. {
  1937. addNewArtifactInstance(ci.art);
  1938. }
  1939. }
  1940. return a;
  1941. }
  1942. void Mapa::eraseArtifactInstance(CArtifactInstance *art)
  1943. {
  1944. assert(artInstances[art->id] == art);
  1945. artInstances[art->id].dellNull();
  1946. }
  1947. LossCondition::LossCondition()
  1948. {
  1949. obj = NULL;
  1950. timeLimit = -1;
  1951. pos = int3(-1,-1,-1);
  1952. }
  1953. CVictoryCondition::CVictoryCondition()
  1954. {
  1955. pos = int3(-1,-1,-1);
  1956. obj = NULL;
  1957. ID = allowNormalVictory = appliesToAI = count = 0;
  1958. }
  1959. bool TerrainTile::entrableTerrain(const TerrainTile *from /*= NULL*/) const
  1960. {
  1961. return entrableTerrain(from ? from->tertype != water : true, from ? from->tertype == water : true);
  1962. }
  1963. bool TerrainTile::entrableTerrain(bool allowLand, bool allowSea) const
  1964. {
  1965. return tertype != rock
  1966. && ((allowSea && tertype == water) || (allowLand && tertype != water));
  1967. }
  1968. bool TerrainTile::isClear(const TerrainTile *from /*= NULL*/) const
  1969. {
  1970. return entrableTerrain(from) && !blocked;
  1971. }