map.cpp 50 KB

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