map.cpp 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118
  1. #define VCMI_DLL
  2. #include "../stdafx.h"
  3. #include "map.h"
  4. #include "../hch/CObjectHandler.h"
  5. #include "../hch/CDefObjInfoHandler.h"
  6. #include "VCMI_Lib.h"
  7. #include <zlib.h>
  8. #include <boost/crc.hpp>
  9. #include "../hch/CLodHandler.h"
  10. #include "../hch/CArtHandler.h"
  11. #include "../hch/CCreatureHandler.h"
  12. #include <boost/bind.hpp>
  13. #include <assert.h>
  14. /*
  15. * map.cpp, part of VCMI engine
  16. *
  17. * Authors: listed in file AUTHORS in main folder
  18. *
  19. * License: GNU General Public License v2.0 or later
  20. * Full text of license available in license.txt file, in main folder
  21. *
  22. */
  23. static std::set<si32> convertBuildings(const std::set<si32> h3m, int castleID)
  24. {
  25. std::map<int,int> mapa;
  26. std::set<si32> ret;
  27. std::ifstream b5(DATA_DIR "/config/buildings5.txt");
  28. while(!b5.eof())
  29. {
  30. int h3, VCMI, town;
  31. b5 >> town >> h3 >> VCMI;
  32. if ( town == castleID || town == -1 )
  33. mapa[h3]=VCMI;
  34. }
  35. for(std::set<si32>::const_iterator i=h3m.begin();i!=h3m.end();i++)
  36. {
  37. if(mapa[*i]>=0)
  38. ret.insert(mapa[*i]);
  39. else if(mapa[*i] >= (-CREATURES_PER_TOWN)) // horde buildings
  40. {
  41. int level = (mapa[*i]);
  42. ret.insert(level-30);//(-30)..(-36) - horde buildings (for game loading only), don't see other way to handle hordes in random towns
  43. }
  44. else
  45. {
  46. tlog3<<"Conversion warning: unknown building "<<*i<<" in castle "<<castleID<<std::endl;
  47. }
  48. }
  49. ret.insert(10); //village hall is always present
  50. ret.insert(-1); //houses near v.hall / eyecandies
  51. ret.insert(-2); //terrain eyecandy, if -1 is used
  52. if(ret.find(11)!=ret.end())
  53. ret.insert(27);
  54. if(ret.find(12)!=ret.end())
  55. ret.insert(28);
  56. if(ret.find(13)!=ret.end())
  57. ret.insert(29);
  58. return ret;
  59. }
  60. static unsigned int intPow(unsigned int a, unsigned int b)
  61. {
  62. unsigned int ret=1;
  63. for(unsigned int i=0; i<b; ++i)
  64. ret*=a;
  65. return ret;
  66. }
  67. static unsigned char reverse(unsigned char arg)
  68. {
  69. unsigned char ret = 0;
  70. for (int i=0; i<8;i++)
  71. {
  72. if(((arg)&(1<<i))>>i)
  73. {
  74. ret |= (128>>i);
  75. }
  76. }
  77. return ret;
  78. }
  79. static CCreatureSet readCreatureSet(const unsigned char * bufor, int &i, int number, bool version) //version==true for >RoE maps
  80. {
  81. const int bytesPerCre = version ? 4 : 3,
  82. idBytes = version ? 2 : 1,
  83. maxID = version ? 0xffff : 0xff;
  84. CCreatureSet ret;
  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;
  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. ret.slots[ir] = hlp;
  101. }
  102. i+=number*bytesPerCre;
  103. ret.validTypes(true);
  104. return ret;
  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; 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 = false;
  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<CMapEvent*>::iterator i = events.begin(); i != events.end(); i++)
  474. delete *i;
  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. nt->setArmy(readCreatureSet(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; 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; 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. void Mapa::loadHero( CGObjectInstance * &nobj, const unsigned char * bufor, int &i )
  716. {
  717. CGHeroInstance * nhi = new CGHeroInstance;
  718. nobj=nhi;
  719. int identifier = 0;
  720. if(version>RoE)
  721. {
  722. identifier = readNormalNr(bufor,i, 4); i+=4;
  723. }
  724. ui8 owner = bufor[i++];
  725. nobj->subID = readNormalNr(bufor,i, 1); ++i;
  726. for(unsigned int j=0; j<predefinedHeroes.size(); j++)
  727. {
  728. if(predefinedHeroes[j]->subID == nobj->subID)
  729. {
  730. *nhi = *predefinedHeroes[j];
  731. break;
  732. }
  733. }
  734. nobj->setOwner(owner);
  735. //(*(static_cast<CGObjectInstance*>(nhi))) = *nobj;
  736. //delete nobj;
  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. heroesToBeat[identifier] = nhi;
  748. if(readChar(bufor,i))//true if hero has nonstandard name
  749. nhi->name = readString(bufor,i);
  750. if(version>AB)
  751. {
  752. if(readChar(bufor,i))//true if hero's experience is greater than 0
  753. { nhi->exp = readNormalNr(bufor,i); i+=4; }
  754. else
  755. nhi->exp = 0xffffffff;
  756. }
  757. else
  758. {
  759. nhi->exp = readNormalNr(bufor,i); i+=4;
  760. if(!nhi->exp) //0 means "not set" in <=AB maps
  761. nhi->exp = 0xffffffff;
  762. }
  763. bool portrait=bufor[i]; ++i;
  764. if (portrait)
  765. nhi->portrait = bufor[i++];
  766. if(readChar(bufor,i))//true if hero has specified abilities
  767. {
  768. int howMany = readNormalNr(bufor,i); i+=4;
  769. nhi->secSkills.resize(howMany);
  770. for(int yy=0; yy<howMany; ++yy)
  771. {
  772. nhi->secSkills[yy].first = readNormalNr(bufor,i, 1); ++i;
  773. nhi->secSkills[yy].second = readNormalNr(bufor,i, 1); ++i;
  774. }
  775. }
  776. if(readChar(bufor,i))//true if hero has nonstandard garrison
  777. nhi->setArmy(readCreatureSet(bufor, i, 7, version > RoE));
  778. nhi->formation =bufor[i]; ++i; //formation
  779. bool artSet = bufor[i]; ++i; //true if artifact set is not default (hero has some artifacts)
  780. int artmask = version == RoE ? 0xff : 0xffff;
  781. int artidlen = version == RoE ? 1 : 2;
  782. if(artSet)
  783. {
  784. for(int pom=0;pom<16;pom++)
  785. {
  786. int id = readNormalNr(bufor,i, artidlen); i+=artidlen;
  787. if(id != artmask)
  788. VLC->arth->equipArtifact(nhi->artifWorn, pom, id);
  789. }
  790. //misc5 art //17
  791. if(version>=SoD)
  792. {
  793. int id = readNormalNr(bufor,i, artidlen); i+=artidlen;
  794. if(id!=artmask)
  795. VLC->arth->equipArtifact(nhi->artifWorn, 16, id);
  796. else
  797. VLC->arth->equipArtifact(nhi->artifWorn, 16, 3); //catapult by default
  798. }
  799. //spellbook
  800. int id = readNormalNr(bufor,i, artidlen); i+=artidlen;
  801. if(id!=artmask)
  802. VLC->arth->equipArtifact(nhi->artifWorn, 17, id);
  803. //19 //???what is that? gap in file or what? - it's probably fifth slot..
  804. if(version>RoE)
  805. {
  806. id = readNormalNr(bufor,i, artidlen); i+=artidlen;
  807. if(id!=artmask)
  808. VLC->arth->equipArtifact(nhi->artifWorn, 18, id);
  809. }
  810. else
  811. i+=1;
  812. //bag artifacts //20
  813. int amount = readNormalNr(bufor,i, 2); i+=2; //number of artifacts in hero's bag
  814. if(amount > 0)
  815. {
  816. for(int ss = 0; ss < amount; ++ss)
  817. {
  818. id = readNormalNr(bufor,i, artidlen); i+=artidlen;
  819. if(id != artmask)
  820. nhi->giveArtifact(id);
  821. }
  822. }
  823. } //artifacts
  824. nhi->patrol.patrolRadious = readNormalNr(bufor,i, 1); ++i;
  825. if(nhi->patrol.patrolRadious == 0xff)
  826. nhi->patrol.patrolling = false;
  827. else
  828. nhi->patrol.patrolling = true;
  829. if(version>RoE)
  830. {
  831. if(readChar(bufor,i))//true if hero has nonstandard (mapmaker defined) biography
  832. nhi->biography = readString(bufor,i);
  833. nhi->sex = !(bufor[i]); ++i;
  834. }
  835. //spells
  836. if(version>AB)
  837. {
  838. bool areSpells = bufor[i]; ++i;
  839. if(areSpells) //TODO: sprawdziæ //seems to be ok - tow
  840. {
  841. nhi->spells.insert(0xffffffff); //placeholder "preset spells"
  842. int ist = i;
  843. for(i; i<ist+9; ++i)
  844. {
  845. unsigned char c = bufor[i];
  846. for(int yy=0; yy<8; ++yy)
  847. {
  848. if((i-ist)*8+yy < SPELLS_QUANTITY)
  849. {
  850. if(c == (c|((unsigned char)intPow(2, yy))))
  851. nhi->spells.insert((i-ist)*8+yy);
  852. }
  853. }
  854. }
  855. }
  856. }
  857. else if(version==AB) //we can read one spell
  858. {
  859. unsigned char buff = bufor[i]; ++i;
  860. if(buff != 254)
  861. {
  862. nhi->spells.insert(0xffffffff); //placeholder "preset spells"
  863. if(buff < 254) //255 means no spells
  864. nhi->spells.insert(buff);
  865. }
  866. }
  867. //spells loaded
  868. if(version>AB)
  869. {
  870. if(readChar(bufor,i))//customPrimSkills
  871. {
  872. for(int xx=0;xx<4;xx++)
  873. nhi->pushPrimSkill(xx, bufor[i++]);
  874. }
  875. }
  876. i+=16;
  877. nhi->moveDir = 4;
  878. nhi->isStanding = true;
  879. nhi->level = -1;
  880. nhi->mana = -1;
  881. nhi->movement = -1;
  882. }
  883. void Mapa::readRumors( const unsigned char * bufor, int &i)
  884. {
  885. int rumNr = readNormalNr(bufor,i,4);i+=4;
  886. for (int it=0;it<rumNr;it++)
  887. {
  888. Rumor ourRumor;
  889. int nameL = readNormalNr(bufor,i,4);i+=4; //read length of name of rumor
  890. for (int zz=0; zz<nameL; zz++)
  891. ourRumor.name+=bufor[i++];
  892. nameL = readNormalNr(bufor,i,4);i+=4; //read length of rumor
  893. for (int zz=0; zz<nameL; zz++)
  894. ourRumor.text+=bufor[i++];
  895. rumors.push_back(ourRumor); //add to our list
  896. }
  897. }
  898. void Mapa::readHeader( const unsigned char * bufor, int &i)
  899. {
  900. //reading allowed heroes (20 bytes)
  901. int ist = i;
  902. ist=i; //starting i for loop
  903. unsigned char disp = 0;
  904. if(version>=SoD)
  905. {
  906. disp = bufor[i++];
  907. disposedHeroes.resize(disp);
  908. for(int g=0; g<disp; ++g)
  909. {
  910. disposedHeroes[g].ID = bufor[i++];
  911. disposedHeroes[g].portrait = bufor[i++];
  912. int lenbuf = readNormalNr(bufor,i); i+=4;
  913. for (int zz=0; zz<lenbuf; zz++)
  914. disposedHeroes[g].name+=bufor[i++];
  915. disposedHeroes[g].players = bufor[i++];
  916. }
  917. }
  918. i+=31; //omitting NULLS
  919. allowedArtifact.resize(ARTIFACTS_QUANTITY);
  920. for (unsigned int x=0; x<allowedArtifact.size(); x++)
  921. allowedArtifact[x] = true;
  922. //reading allowed artifacts: 17 or 18 bytes
  923. if (version!=RoE)
  924. {
  925. ist=i; //starting i for loop
  926. for (i; i<ist+(version==AB ? 17 : 18); ++i)
  927. {
  928. unsigned char c = bufor[i];
  929. for (int yy=0; yy<8; ++yy)
  930. {
  931. if ((i-ist)*8+yy < ARTIFACTS_QUANTITY)
  932. {
  933. if (c == (c|((unsigned char)intPow(2, yy))))
  934. allowedArtifact[(i-ist)*8+yy] = false;
  935. }
  936. }
  937. }//allowed artifacts have been read
  938. }
  939. allowedSpell.resize(SPELLS_QUANTITY);
  940. for(unsigned int x=0;x<allowedSpell.size();x++)
  941. allowedSpell[x] = true;
  942. allowedAbilities.resize(SKILL_QUANTITY);
  943. for(unsigned int x=0;x<allowedAbilities.size();x++)
  944. allowedAbilities[x] = true;
  945. if(version>=SoD)
  946. {
  947. //reading allowed spells (9 bytes)
  948. ist=i; //starting i for loop
  949. for(i; i<ist+9; ++i)
  950. {
  951. unsigned char c = bufor[i];
  952. for(int yy=0; yy<8; ++yy)
  953. if((i-ist)*8+yy < SPELLS_QUANTITY)
  954. if(c == (c|((unsigned char)intPow(2, yy))))
  955. allowedSpell[(i-ist)*8+yy] = false;
  956. }
  957. //allowed hero's abilities (4 bytes)
  958. ist=i; //starting i for loop
  959. for(i; i<ist+4; ++i)
  960. {
  961. unsigned char c = bufor[i];
  962. for(int yy=0; yy<8; ++yy)
  963. {
  964. if((i-ist)*8+yy < SKILL_QUANTITY)
  965. {
  966. if(c == (c|((unsigned char)intPow(2, yy))))
  967. allowedAbilities[(i-ist)*8+yy] = false;
  968. }
  969. }
  970. }
  971. }
  972. }
  973. void Mapa::readPredefinedHeroes( const unsigned char * bufor, int &i)
  974. {
  975. switch(version)
  976. {
  977. case WoG: case SoD:
  978. {
  979. for(int z=0;z<HEROES_QUANTITY;z++) //disposed heroes
  980. {
  981. int custom = bufor[i++];
  982. if(!custom)
  983. continue;
  984. CGHeroInstance * cgh = new CGHeroInstance;
  985. cgh->ID = HEROI_TYPE;
  986. cgh->subID = z;
  987. if(readChar(bufor,i))//true if hore's experience is greater than 0
  988. { cgh->exp = readNormalNr(bufor,i); i+=4; }
  989. else
  990. cgh->exp = 0;
  991. if(readChar(bufor,i))//true if hero has specified abilities
  992. {
  993. int howMany = readNormalNr(bufor,i); i+=4;
  994. cgh->secSkills.resize(howMany);
  995. for(int yy=0; yy<howMany; ++yy)
  996. {
  997. cgh->secSkills[yy].first = readNormalNr(bufor,i, 1); ++i;
  998. cgh->secSkills[yy].second = readNormalNr(bufor,i, 1); ++i;
  999. }
  1000. }
  1001. bool artSet = bufor[i]; ++i; //true if artifact set is not default (hero has some artifacts)
  1002. int artmask = version == RoE ? 0xff : 0xffff;
  1003. int artidlen = version == RoE ? 1 : 2;
  1004. if(artSet)
  1005. {
  1006. for(int pom=0;pom<16;pom++)
  1007. {
  1008. int id = readNormalNr(bufor,i, artidlen); i+=artidlen;
  1009. if(id!=artmask)
  1010. VLC->arth->equipArtifact(cgh->artifWorn, pom, id);
  1011. }
  1012. //misc5 art //17
  1013. if(version>=SoD)
  1014. {
  1015. i+=2;
  1016. //int id = readNormalNr(bufor,i, artidlen); i+=artidlen;
  1017. //if(id!=artmask)
  1018. // spec->artifWorn[16] = id;
  1019. }
  1020. //spellbook
  1021. int id = readNormalNr(bufor,i, artidlen); i+=artidlen;
  1022. if(id!=artmask)
  1023. VLC->arth->equipArtifact(cgh->artifWorn, 17, id);
  1024. //19 //???what is that? gap in file or what? - it's probably fifth slot..
  1025. if(version>RoE)
  1026. {
  1027. id = readNormalNr(bufor,i, artidlen); i+=artidlen;
  1028. if(id!=artmask)
  1029. VLC->arth->equipArtifact(cgh->artifWorn, 18, id);
  1030. }
  1031. else
  1032. i+=1;
  1033. //bag artifacts //20
  1034. int amount = readNormalNr(bufor,i, 2); i+=2; //number of artifacts in hero's bag
  1035. if(amount>0)
  1036. {
  1037. for(int ss=0; ss<amount; ++ss)
  1038. {
  1039. id = readNormalNr(bufor,i, artidlen); i+=artidlen;
  1040. if(id!=artmask)
  1041. cgh->giveArtifact(id);
  1042. }
  1043. }
  1044. } //artifacts
  1045. if(readChar(bufor,i))//customBio
  1046. cgh->biography = readString(bufor,i);
  1047. int sex = bufor[i++]; // 0xFF is default, 00 male, 01 female
  1048. if(readChar(bufor,i))//are spells
  1049. {
  1050. int ist = i;
  1051. for(i; i<ist+9; ++i)
  1052. {
  1053. unsigned char c = bufor[i];
  1054. for(int yy=0; yy<8; ++yy)
  1055. {
  1056. if((i-ist)*8+yy < SPELLS_QUANTITY)
  1057. {
  1058. if(c == (c|((unsigned char)intPow(2, yy))))
  1059. cgh->spells.insert((i-ist)*8+yy);
  1060. }
  1061. }
  1062. }
  1063. }
  1064. if(readChar(bufor,i))//customPrimSkills
  1065. {
  1066. for(int xx=0;xx<4;xx++)
  1067. cgh->pushPrimSkill(xx, bufor[i++]);
  1068. }
  1069. predefinedHeroes.push_back(cgh);
  1070. }
  1071. break;
  1072. }
  1073. case RoE:
  1074. i+=0;
  1075. break;
  1076. }
  1077. }
  1078. void Mapa::readTerrain( const unsigned char * bufor, int &i)
  1079. {
  1080. terrain = new TerrainTile**[width]; // allocate memory
  1081. for (int ii=0;ii<width;ii++)
  1082. {
  1083. terrain[ii] = new TerrainTile*[height]; // allocate memory
  1084. for(int jj=0;jj<height;jj++)
  1085. terrain[ii][jj] = new TerrainTile[twoLevel+1];
  1086. }
  1087. for (int c=0; c<width; c++) // reading terrain
  1088. {
  1089. for (int z=0; z<height; z++)
  1090. {
  1091. terrain[z][c][0].tertype = static_cast<TerrainTile::EterrainType>(bufor[i++]);
  1092. terrain[z][c][0].terview = bufor[i++];
  1093. terrain[z][c][0].nuine = static_cast<TerrainTile::Eriver>(bufor[i++]);
  1094. terrain[z][c][0].rivDir = bufor[i++];
  1095. terrain[z][c][0].malle = static_cast<TerrainTile::Eroad>(bufor[i++]);
  1096. terrain[z][c][0].roadDir = bufor[i++];
  1097. terrain[z][c][0].siodmyTajemniczyBajt = bufor[i++];
  1098. terrain[z][c][0].blocked = 0;
  1099. terrain[z][c][0].visitable = 0;
  1100. }
  1101. }
  1102. if (twoLevel) // read underground terrain
  1103. {
  1104. for (int c=0; c<width; c++) // reading terrain
  1105. {
  1106. for (int z=0; z<height; z++)
  1107. {
  1108. terrain[z][c][1].tertype = static_cast<TerrainTile::EterrainType>(bufor[i++]);
  1109. terrain[z][c][1].terview = bufor[i++];
  1110. terrain[z][c][1].nuine = static_cast<TerrainTile::Eriver>(bufor[i++]);
  1111. terrain[z][c][1].rivDir = bufor[i++];
  1112. terrain[z][c][1].malle = static_cast<TerrainTile::Eroad>(bufor[i++]);
  1113. terrain[z][c][1].roadDir = bufor[i++];
  1114. terrain[z][c][1].siodmyTajemniczyBajt = bufor[i++];
  1115. terrain[z][c][1].blocked = 0;
  1116. terrain[z][c][1].visitable = 0;
  1117. }
  1118. }
  1119. }
  1120. }
  1121. void Mapa::readDefInfo( const unsigned char * bufor, int &i)
  1122. {
  1123. int defAmount = readNormalNr(bufor,i); i+=4;
  1124. defy.reserve(defAmount+8);
  1125. for (int idd = 0 ; idd<defAmount; idd++) // reading defs
  1126. {
  1127. CGDefInfo * vinya = new CGDefInfo(); // info about new def
  1128. //reading name
  1129. int nameLength = readNormalNr(bufor,i,4);i+=4;
  1130. vinya->name.reserve(nameLength);
  1131. for (int cd=0;cd<nameLength;cd++)
  1132. {
  1133. vinya->name += bufor[i++];
  1134. }
  1135. std::transform(vinya->name.begin(),vinya->name.end(),vinya->name.begin(),(int(*)(int))toupper);
  1136. unsigned char bytes[12];
  1137. for (int v=0; v<12; v++) // read info
  1138. {
  1139. bytes[v] = bufor[i++];
  1140. }
  1141. vinya->terrainAllowed = readNormalNr(bufor,i,2);i+=2;
  1142. vinya->terrainMenu = readNormalNr(bufor,i,2);i+=2;
  1143. vinya->id = readNormalNr(bufor,i,4);i+=4;
  1144. vinya->subid = readNormalNr(bufor,i,4);i+=4;
  1145. vinya->type = bufor[i++];
  1146. vinya->printPriority = bufor[i++];
  1147. for (int zi=0; zi<6; zi++)
  1148. {
  1149. vinya->blockMap[zi] = reverse(bytes[zi]);
  1150. }
  1151. for (int zi=0; zi<6; zi++)
  1152. {
  1153. vinya->visitMap[zi] = reverse(bytes[6+zi]);
  1154. }
  1155. i+=16;
  1156. if(vinya->id!=HEROI_TYPE && vinya->id!=70)
  1157. {
  1158. CGDefInfo *h = VLC->dobjinfo->gobjs[vinya->id][vinya->subid];
  1159. if(!h)
  1160. {
  1161. //remove fake entry
  1162. VLC->dobjinfo->gobjs[vinya->id].erase(vinya->subid);
  1163. if(VLC->dobjinfo->gobjs[vinya->id].size())
  1164. VLC->dobjinfo->gobjs.erase(vinya->id);
  1165. tlog2<<"\t\tWarning: no defobjinfo entry for object ID="<<vinya->id<<" subID=" << vinya->subid<<std::endl;
  1166. }
  1167. else
  1168. {
  1169. vinya->visitDir = VLC->dobjinfo->gobjs[vinya->id][vinya->subid]->visitDir;
  1170. }
  1171. }
  1172. else
  1173. {
  1174. vinya->visitDir = 0xff;
  1175. }
  1176. if(vinya->id == EVENTI_TYPE)
  1177. std::memset(vinya->blockMap,255,6);
  1178. //calculating coverageMap
  1179. vinya->fetchInfoFromMSK();
  1180. defy.push_back(vinya); // add this def to the vector
  1181. }
  1182. //add holes - they always can appear
  1183. for (int i = 0; i < 8 ; i++)
  1184. {
  1185. defy.push_back(VLC->dobjinfo->gobjs[124][i]);
  1186. }
  1187. }
  1188. void Mapa::readObjects( const unsigned char * bufor, int &i)
  1189. {
  1190. int howManyObjs = readNormalNr(bufor,i, 4); i+=4;
  1191. for(int ww=0; ww<howManyObjs; ++ww) //comment this line to turn loading objects off
  1192. {
  1193. CGObjectInstance * nobj = 0;
  1194. int3 pos;
  1195. pos.x = bufor[i++];
  1196. pos.y = bufor[i++];
  1197. pos.z = bufor[i++];
  1198. int defnum = readNormalNr(bufor,i, 4); i+=4;
  1199. CGDefInfo * defInfo = defy[defnum];
  1200. i+=5;
  1201. switch(defInfo->id)
  1202. {
  1203. case EVENTI_TYPE: //for event objects
  1204. {
  1205. CGEvent *evnt = new CGEvent();
  1206. nobj = evnt;
  1207. bool guardMess = bufor[i]; ++i;
  1208. if(guardMess)
  1209. {
  1210. int messLong = readNormalNr(bufor,i, 4); i+=4;
  1211. if(messLong>0)
  1212. {
  1213. for(int yy=0; yy<messLong; ++yy)
  1214. {
  1215. evnt->message +=bufor[i+yy];
  1216. }
  1217. i+=messLong;
  1218. }
  1219. if(bufor[i++])
  1220. {
  1221. evnt->setArmy(readCreatureSet(bufor, i, 7, version > RoE));
  1222. }
  1223. i+=4;
  1224. }
  1225. evnt->gainedExp = readNormalNr(bufor,i, 4); i+=4;
  1226. evnt->manaDiff = readNormalNr(bufor,i, 4); i+=4;
  1227. evnt->moraleDiff = readNormalNr(bufor,i, 1, true); ++i;
  1228. evnt->luckDiff = readNormalNr(bufor,i, 1, true); ++i;
  1229. evnt->resources.resize(RESOURCE_QUANTITY);
  1230. for(int x=0; x<7; x++)
  1231. {
  1232. evnt->resources[x] = readNormalNr(bufor,i);
  1233. i+=4;
  1234. }
  1235. evnt->primskills.resize(PRIMARY_SKILLS);
  1236. for(int x=0; x<4; x++)
  1237. {
  1238. evnt->primskills[x] = readNormalNr(bufor,i, 1);
  1239. i++;
  1240. }
  1241. int gabn; //number of gained abilities
  1242. gabn = readNormalNr(bufor,i, 1); ++i;
  1243. for(int oo = 0; oo<gabn; ++oo)
  1244. {
  1245. evnt->abilities.push_back(readNormalNr(bufor,i, 1)); ++i;
  1246. evnt->abilityLevels.push_back(readNormalNr(bufor,i, 1)); ++i;
  1247. }
  1248. int gart = readNormalNr(bufor,i, 1); ++i; //number of gained artifacts
  1249. for(int oo = 0; oo<gart; ++oo)
  1250. {
  1251. evnt->artifacts.push_back(readNormalNr(bufor,i, (version == RoE ? 1 : 2))); i+=(version == RoE ? 1 : 2);
  1252. }
  1253. int gspel = readNormalNr(bufor,i, 1); ++i; //number of gained spells
  1254. for(int oo = 0; oo<gspel; ++oo)
  1255. {
  1256. evnt->spells.push_back(readNormalNr(bufor,i, 1)); ++i;
  1257. }
  1258. int gcre = readNormalNr(bufor,i, 1); ++i; //number of gained creatures
  1259. evnt->creatures = readCreatureSet(bufor,i,gcre,(version>RoE));
  1260. i+=8;
  1261. evnt->availableFor = readNormalNr(bufor,i, 1); ++i;
  1262. evnt->computerActivate = readNormalNr(bufor,i, 1); ++i;
  1263. evnt->removeAfterVisit = readNormalNr(bufor,i, 1); ++i;
  1264. evnt->humanActivate = true;
  1265. i+=4;
  1266. break;
  1267. }
  1268. case 34: case 70: case 62: //34 - hero; 70 - random hero; 62 - prison
  1269. {
  1270. loadHero(nobj, bufor, i);
  1271. break;
  1272. }
  1273. case 4: //Arena
  1274. case 51: //Mercenary Camp
  1275. case 23: //Marletto Tower
  1276. case 61: // Star Axis
  1277. case 32: // Garden of Revelation
  1278. case 100: //Learning Stone
  1279. case 102: //Tree of Knowledge
  1280. case 41: //Library of Enlightenment
  1281. case 47: //School of Magic
  1282. case 107: //School of War
  1283. {
  1284. nobj = new CGVisitableOPH();
  1285. break;
  1286. }
  1287. case 55: //mystical garden
  1288. case 112://windmill
  1289. case 109://water wheel
  1290. {
  1291. nobj = new CGVisitableOPW();
  1292. break;
  1293. }
  1294. case 43: //teleport
  1295. case 44: //teleport
  1296. case 45: //teleport
  1297. case 103://subterranean gate
  1298. case 111://Whirlpool
  1299. {
  1300. nobj = new CGTeleport();
  1301. break;
  1302. }
  1303. case 12: //campfire
  1304. case 29: //Flotsam
  1305. case 82: //Sea Chest
  1306. case 86: //Shipwreck Survivor
  1307. case 101://treasure chest
  1308. {
  1309. nobj = new CGPickable();
  1310. break;
  1311. }
  1312. case 54: //Monster
  1313. case 71: case 72: case 73: case 74: case 75: // Random Monster 1 - 4
  1314. case 162: case 163: case 164: // Random Monster 5 - 7
  1315. {
  1316. CGCreature *cre = new CGCreature();
  1317. nobj = cre;
  1318. if(version>RoE)
  1319. {
  1320. cre->identifier = readNormalNr(bufor,i); i+=4;
  1321. monsters[cre->identifier] = cre;
  1322. }
  1323. CStackInstance hlp;
  1324. hlp.count = readNormalNr(bufor,i, 2); i+=2;
  1325. //type will be set during initialization
  1326. cre->slots[0] = hlp;
  1327. cre->character = bufor[i]; ++i;
  1328. bool isMesTre = bufor[i]; ++i; //true if there is message or treasury
  1329. if(isMesTre)
  1330. {
  1331. cre->message = readString(bufor,i);
  1332. cre->resources.resize(RESOURCE_QUANTITY);
  1333. for(int j=0; j<7; j++)
  1334. {
  1335. cre->resources[j] = readNormalNr(bufor,i); i+=4;
  1336. }
  1337. int artID = readNormalNr(bufor,i, (version == RoE ? 1 : 2)); i+=(version == RoE ? 1 : 2);
  1338. if(version==RoE)
  1339. {
  1340. if(artID!=0xff)
  1341. cre->gainedArtifact = artID;
  1342. else
  1343. cre->gainedArtifact = -1;
  1344. }
  1345. else
  1346. {
  1347. if(artID!=0xffff)
  1348. cre->gainedArtifact = artID;
  1349. else
  1350. cre->gainedArtifact = -1;
  1351. }
  1352. }
  1353. cre->neverFlees = bufor[i]; ++i;
  1354. cre->notGrowingTeam = bufor[i]; ++i;
  1355. i+=2;;
  1356. break;
  1357. }
  1358. case 59: case 91: //ocean bottle and sign
  1359. {
  1360. CGSignBottle *sb = new CGSignBottle();
  1361. nobj = sb;
  1362. sb->message = readString(bufor,i);
  1363. i+=4;
  1364. break;
  1365. }
  1366. case 83: //seer's hut
  1367. {
  1368. i = loadSeerHut(bufor, i, nobj);
  1369. break;
  1370. }
  1371. case 113: //witch hut
  1372. {
  1373. CGWitchHut *wh = new CGWitchHut();
  1374. nobj = wh;
  1375. if(version>RoE) //in reo we cannot specify it - all are allowed (I hope)
  1376. {
  1377. int ist=i; //starting i for loop
  1378. for(i; i<ist+4; ++i)
  1379. {
  1380. unsigned char c = bufor[i];
  1381. for(int yy=0; yy<8; ++yy)
  1382. {
  1383. if((i-ist)*8+yy < SKILL_QUANTITY)
  1384. {
  1385. if(c == (c|((unsigned char)intPow(2, yy))))
  1386. wh->allowedAbilities.push_back((i-ist)*8+yy);
  1387. }
  1388. }
  1389. }
  1390. }
  1391. else //(RoE map)
  1392. {
  1393. for(int gg=0; gg<SKILL_QUANTITY; ++gg)
  1394. {
  1395. wh->allowedAbilities.push_back(gg);
  1396. }
  1397. }
  1398. break;
  1399. }
  1400. case 81: //scholar
  1401. {
  1402. CGScholar *sch = new CGScholar();
  1403. nobj = sch;
  1404. sch->bonusType = bufor[i++];
  1405. sch->bonusID = bufor[i++];
  1406. i+=6;
  1407. break;
  1408. }
  1409. case 33: case 219: //garrison
  1410. {
  1411. CGGarrison *gar = new CGGarrison();
  1412. nobj = gar;
  1413. nobj->setOwner(bufor[i++]);
  1414. i+=3;
  1415. gar->setArmy(readCreatureSet(bufor, i, 7, version > RoE));
  1416. if(version > RoE)
  1417. {
  1418. gar->removableUnits = bufor[i]; ++i;
  1419. }
  1420. else
  1421. gar->removableUnits = true;
  1422. i+=8;
  1423. break;
  1424. }
  1425. case 5: //artifact
  1426. case 65: case 66: case 67: case 68: case 69: //random artifact
  1427. case 93: //spell scroll
  1428. {
  1429. CGArtifact *art = new CGArtifact();
  1430. nobj = art;
  1431. bool areSettings = bufor[i]; ++i;
  1432. if(areSettings)
  1433. {
  1434. art->message = readString(bufor,i);
  1435. bool areGuards = bufor[i]; ++i;
  1436. if(areGuards)
  1437. {
  1438. art->setArmy(readCreatureSet(bufor, i, 7, version > RoE));
  1439. }
  1440. i+=4;
  1441. }
  1442. if(defInfo->id==93)
  1443. {
  1444. art->spell = readNormalNr(bufor,i);
  1445. i+=4;
  1446. }
  1447. break;
  1448. }
  1449. case 76: case 79: //random resource; resource
  1450. {
  1451. CGResource *res = new CGResource();
  1452. nobj = res;
  1453. bool isMessGuard = bufor[i]; ++i;
  1454. if(isMessGuard)
  1455. {
  1456. res->message = readString(bufor,i);
  1457. if(bufor[i++])
  1458. {
  1459. res->setArmy(readCreatureSet(bufor, i, 7, version > RoE));
  1460. }
  1461. i+=4;
  1462. }
  1463. res->amount = readNormalNr(bufor,i); i+=4;
  1464. if (defInfo->subid == 6) // Gold is multiplied by 100.
  1465. res->amount *= 100;
  1466. i+=4;
  1467. break;
  1468. }
  1469. case 77: case 98: //random town; town
  1470. {
  1471. loadTown(nobj, bufor, i, defInfo->subid);
  1472. break;
  1473. }
  1474. case 53:
  1475. case 220://mine (?)
  1476. {
  1477. nobj = new CGMine();
  1478. nobj->setOwner(bufor[i++]);
  1479. i+=3;
  1480. break;
  1481. }
  1482. case 17: case 18: case 19: case 20: //dwellings
  1483. {
  1484. nobj = new CGDwelling();
  1485. nobj->setOwner(bufor[i++]);
  1486. i+=3;
  1487. break;
  1488. }
  1489. case 78: //Refugee Camp
  1490. case 106: //War Machine Factory
  1491. {
  1492. nobj = new CGDwelling();
  1493. break;
  1494. }
  1495. case 88: case 89: case 90: //spell shrine
  1496. {
  1497. CGShrine * shr = new CGShrine();
  1498. nobj = shr;
  1499. shr->spell = bufor[i]; i+=4;
  1500. break;
  1501. }
  1502. case 6: //pandora's box
  1503. {
  1504. CGPandoraBox *box = new CGPandoraBox();
  1505. nobj = box;
  1506. bool messg = bufor[i]; ++i;
  1507. if(messg)
  1508. {
  1509. box->message = readString(bufor,i);
  1510. if(bufor[i++])
  1511. {
  1512. box->setArmy(readCreatureSet(bufor, i, 7, version > RoE));
  1513. }
  1514. i+=4;
  1515. }
  1516. box->gainedExp = readNormalNr(bufor,i, 4); i+=4;
  1517. box->manaDiff = readNormalNr(bufor,i, 4); i+=4;
  1518. box->moraleDiff = readNormalNr(bufor,i, 1, true); ++i;
  1519. box->luckDiff = readNormalNr(bufor,i, 1, true); ++i;
  1520. box->resources.resize(RESOURCE_QUANTITY);
  1521. for(int x=0; x<7; x++)
  1522. {
  1523. box->resources[x] = readNormalNr(bufor,i);
  1524. i+=4;
  1525. }
  1526. box->primskills.resize(PRIMARY_SKILLS);
  1527. for(int x=0; x<4; x++)
  1528. {
  1529. box->primskills[x] = readNormalNr(bufor,i, 1);
  1530. i++;
  1531. }
  1532. int gabn; //number of gained abilities
  1533. gabn = readNormalNr(bufor,i, 1); ++i;
  1534. for(int oo = 0; oo<gabn; ++oo)
  1535. {
  1536. box->abilities.push_back(readNormalNr(bufor,i, 1)); ++i;
  1537. box->abilityLevels.push_back(readNormalNr(bufor,i, 1)); ++i;
  1538. }
  1539. int gart = readNormalNr(bufor,i, 1); ++i; //number of gained artifacts
  1540. for(int oo = 0; oo<gart; ++oo)
  1541. {
  1542. if(version > RoE)
  1543. {
  1544. box->artifacts.push_back(readNormalNr(bufor,i, 2)); i+=2;
  1545. }
  1546. else
  1547. {
  1548. box->artifacts.push_back(readNormalNr(bufor,i, 1)); i+=1;
  1549. }
  1550. }
  1551. int gspel = readNormalNr(bufor,i, 1); ++i; //number of gained spells
  1552. for(int oo = 0; oo<gspel; ++oo)
  1553. {
  1554. box->spells.push_back(readNormalNr(bufor,i, 1)); ++i;
  1555. }
  1556. int gcre = readNormalNr(bufor,i, 1); ++i; //number of gained creatures
  1557. box->creatures = readCreatureSet(bufor,i,gcre,(version>RoE));
  1558. i+=8;
  1559. break;
  1560. }
  1561. case 36: //grail
  1562. {
  1563. grailPos = pos;
  1564. grailRadious = readNormalNr(bufor,i); i+=4;
  1565. continue;
  1566. }
  1567. case 217:
  1568. {
  1569. nobj = new CGDwelling();
  1570. CCreGenObjInfo * spec = new CCreGenObjInfo;
  1571. spec->player = readNormalNr(bufor,i); i+=4;
  1572. spec->identifier = readNormalNr(bufor,i); i+=4;
  1573. if(!spec->identifier)
  1574. {
  1575. spec->asCastle = false;
  1576. spec->castles[0] = bufor[i]; ++i;
  1577. spec->castles[1] = bufor[i]; ++i;
  1578. }
  1579. else
  1580. {
  1581. spec->asCastle = true;
  1582. }
  1583. nobj->setOwner(spec->player);
  1584. static_cast<CGDwelling*>(nobj)->info = spec;
  1585. break;
  1586. }
  1587. case 216:
  1588. {
  1589. nobj = new CGDwelling();
  1590. CCreGen2ObjInfo * spec = new CCreGen2ObjInfo;
  1591. spec->player = readNormalNr(bufor,i); i+=4;
  1592. spec->identifier = readNormalNr(bufor,i); i+=4;
  1593. if(!spec->identifier)
  1594. {
  1595. spec->asCastle = false;
  1596. spec->castles[0] = bufor[i]; ++i;
  1597. spec->castles[1] = bufor[i]; ++i;
  1598. }
  1599. else
  1600. {
  1601. spec->asCastle = true;
  1602. }
  1603. spec->minLevel = bufor[i]; ++i;
  1604. spec->maxLevel = bufor[i]; ++i;
  1605. nobj->setOwner(spec->player);
  1606. static_cast<CGDwelling*>(nobj)->info = spec;
  1607. break;
  1608. }
  1609. case 218:
  1610. {
  1611. nobj = new CGDwelling();
  1612. CCreGen3ObjInfo * spec = new CCreGen3ObjInfo;
  1613. spec->player = bufor[i]; ++i;
  1614. i+=3;
  1615. spec->minLevel = bufor[i]; ++i;
  1616. spec->maxLevel = bufor[i]; ++i;
  1617. if(spec->maxLevel>7)
  1618. spec->maxLevel = 7;
  1619. if(spec->minLevel<1)
  1620. spec->minLevel = 1;
  1621. nobj->setOwner(spec->player);
  1622. static_cast<CGDwelling*>(nobj)->info = spec;
  1623. break;
  1624. }
  1625. case 215:
  1626. {
  1627. CGQuestGuard *guard = new CGQuestGuard();
  1628. nobj = guard;
  1629. loadQuest(guard, bufor, i);
  1630. break;
  1631. }
  1632. case 28: //faerie ring
  1633. case 14: //Swan pond
  1634. case 38: //idol of fortune
  1635. case 30: //Fountain of Fortune
  1636. case 64: //Rally Flag
  1637. case 56: //oasis
  1638. case 96: //temple
  1639. case 110://Watering Hole
  1640. case 31: //Fountain of Youth
  1641. case 11: //Buoy
  1642. case 52: //Mermaid
  1643. case 94: //Stables
  1644. {
  1645. nobj = new CGBonusingObject();
  1646. break;
  1647. }
  1648. case 49: //Magic Well
  1649. {
  1650. nobj = new CGMagicWell();
  1651. break;
  1652. }
  1653. case 15: //Cover of darkness
  1654. case 58: //Redwood Observatory
  1655. case 60: //Pillar of Fire
  1656. {
  1657. nobj = new CGObservatory();
  1658. break;
  1659. }
  1660. case 22: //Corpse
  1661. case 39: //Lean To
  1662. case 105://Wagon
  1663. case 108://Warrior's Tomb
  1664. {
  1665. nobj = new CGOnceVisitable();
  1666. break;
  1667. }
  1668. case 8: //Boat
  1669. {
  1670. nobj = new CGBoat();
  1671. break;
  1672. }
  1673. case 92: //Sirens
  1674. {
  1675. nobj = new CGSirens();
  1676. break;
  1677. }
  1678. case 87: //Shipyard
  1679. {
  1680. nobj = new CGShipyard();
  1681. nobj->setOwner(readNormalNr(bufor,i)); i+=4;
  1682. break;
  1683. }
  1684. case 214: //hero placeholder
  1685. {
  1686. CGHeroPlaceholder *hp = new CGHeroPlaceholder();;
  1687. nobj = hp;
  1688. int a = bufor[i++]; //unkown byte, seems to be always 0 (if not - scream!)
  1689. assert(!a);
  1690. int htid = bufor[i++]; //hero type id
  1691. nobj->subID = htid;
  1692. if(htid == 0xff)
  1693. hp->power = bufor[i++];
  1694. else
  1695. hp->power = 0;
  1696. break;
  1697. }
  1698. case 10: //Keymaster
  1699. {
  1700. nobj = new CGKeymasterTent();
  1701. break;
  1702. }
  1703. case 9: //Border Guard
  1704. {
  1705. nobj = new CGBorderGuard();
  1706. break;
  1707. }
  1708. case 212: //Border Gate
  1709. {
  1710. nobj = new CGBorderGate();
  1711. break;
  1712. }
  1713. case 27: case 37: //Eye and Hut of Magi
  1714. {
  1715. nobj = new CGMagi();
  1716. break;
  1717. }
  1718. case 16: case 24: case 25: case 84: case 85: //treasure bank
  1719. {
  1720. nobj = new CBank();
  1721. break;
  1722. }
  1723. case 63: //Pyramid
  1724. {
  1725. nobj = new CGPyramid();
  1726. break;
  1727. }
  1728. case 13: //Cartographer
  1729. {
  1730. nobj = new CCartographer();
  1731. break;
  1732. }
  1733. case 48: //Magic Spring
  1734. {
  1735. nobj = new CGMagicSpring();
  1736. break;
  1737. }
  1738. case 97: //den of thieves
  1739. {
  1740. nobj = new CGDenOfthieves();
  1741. break;
  1742. }
  1743. case 57: //Obelisk
  1744. {
  1745. nobj = new CGObelisk();
  1746. break;
  1747. }
  1748. case 42: //Lighthouse
  1749. {
  1750. nobj = new CGLighthouse();
  1751. nobj->tempOwner = readNormalNr(bufor,i); i+=4;
  1752. break;
  1753. }
  1754. case 2: //Altar of Sacrifice
  1755. case 99: //Trading Post
  1756. case 213: //Freelancer's Guild
  1757. case 221: //Trading Post (snow)
  1758. {
  1759. nobj = new CGMarket();
  1760. break;
  1761. }
  1762. case 104: //University
  1763. {
  1764. nobj = new CGUniversity();
  1765. break;
  1766. }
  1767. case 7: //Black Market
  1768. {
  1769. nobj = new CGBlackMarket();
  1770. break;
  1771. }
  1772. default: //any other object
  1773. {
  1774. nobj = new CGObjectInstance();
  1775. break;
  1776. }
  1777. } //end of main switch
  1778. nobj->pos = pos;
  1779. nobj->ID = defInfo->id;
  1780. nobj->id = objects.size();
  1781. if(nobj->ID != HEROI_TYPE && nobj->ID != 214 && nobj->ID != 62)
  1782. nobj->subID = defInfo->subid;
  1783. nobj->defInfo = defInfo;
  1784. objects.push_back(nobj);
  1785. if(nobj->ID==TOWNI_TYPE)
  1786. towns.push_back(static_cast<CGTownInstance*>(nobj));
  1787. if(nobj->ID==HEROI_TYPE)
  1788. heroes.push_back(static_cast<CGHeroInstance*>(nobj));
  1789. }
  1790. std::sort(heroes.begin(), heroes.end(), boost::bind(&CGHeroInstance::subID, _1) < boost::bind(&CGHeroInstance::subID, _2));
  1791. }
  1792. void Mapa::readEvents( const unsigned char * bufor, int &i )
  1793. {
  1794. int numberOfEvents = readNormalNr(bufor,i); i+=4;
  1795. for(int yyoo=0; yyoo<numberOfEvents; ++yyoo)
  1796. {
  1797. CMapEvent *ne = new CMapEvent();
  1798. ne->name = std::string();
  1799. ne->message = std::string();
  1800. int nameLen = readNormalNr(bufor,i); i+=4;
  1801. for(int qq=0; qq<nameLen; ++qq)
  1802. {
  1803. ne->name += bufor[i]; ++i;
  1804. }
  1805. int messLen = readNormalNr(bufor,i); i+=4;
  1806. for(int qq=0; qq<messLen; ++qq)
  1807. {
  1808. ne->message +=bufor[i]; ++i;
  1809. }
  1810. ne->resources.resize(RESOURCE_QUANTITY);
  1811. for(int k=0; k < 7; k++)
  1812. {
  1813. ne->resources[k] = readNormalNr(bufor,i); i+=4;
  1814. }
  1815. ne->players = bufor[i]; ++i;
  1816. if(version>AB)
  1817. {
  1818. ne->humanAffected = bufor[i]; ++i;
  1819. }
  1820. else
  1821. ne->humanAffected = true;
  1822. ne->computerAffected = bufor[i]; ++i;
  1823. ne->firstOccurence = readNormalNr(bufor,i, 2); i+=2;
  1824. ne->nextOccurence = bufor[i]; ++i;
  1825. char unknown[17];
  1826. memcpy(unknown, bufor+i, 17);
  1827. i+=17;
  1828. events.push_back(ne);
  1829. }
  1830. }
  1831. bool Mapa::isInTheMap(const int3 &pos) const
  1832. {
  1833. if(pos.x<0 || pos.y<0 || pos.z<0 || pos.x >= width || pos.y >= height || pos.z > twoLevel)
  1834. return false;
  1835. else return true;
  1836. }
  1837. void Mapa::loadQuest(CQuest * guard, const unsigned char * bufor, int & i)
  1838. {
  1839. guard->missionType = bufor[i]; ++i;
  1840. //int len1, len2, len3;
  1841. switch(guard->missionType)
  1842. {
  1843. case 0:
  1844. return;
  1845. case 2:
  1846. {
  1847. guard->m2stats.resize(4);
  1848. for(int x=0; x<4; x++)
  1849. {
  1850. guard->m2stats[x] = bufor[i++];
  1851. }
  1852. }
  1853. break;
  1854. case 1:
  1855. case 3:
  1856. case 4:
  1857. {
  1858. guard->m13489val = readNormalNr(bufor,i); i+=4;
  1859. break;
  1860. }
  1861. case 5:
  1862. {
  1863. int artNumber = bufor[i]; ++i;
  1864. for(int yy=0; yy<artNumber; ++yy)
  1865. {
  1866. int artid = readNormalNr(bufor,i, 2); i+=2;
  1867. guard->m5arts.push_back(artid);
  1868. allowedArtifact[artid] = false; //these are unavaliable for random generation
  1869. }
  1870. break;
  1871. }
  1872. case 6:
  1873. {
  1874. int typeNumber = bufor[i]; ++i;
  1875. for(int hh=0; hh<typeNumber; ++hh)
  1876. {
  1877. ui32 creType = readNormalNr(bufor,i, 2); i+=2;
  1878. ui32 creNumb = readNormalNr(bufor,i, 2); i+=2;
  1879. guard->m6creatures[hh] = CStackInstance(creType,creNumb);
  1880. }
  1881. break;
  1882. }
  1883. case 7:
  1884. {
  1885. guard->m7resources.resize(7);
  1886. for(int x=0; x<7; x++)
  1887. {
  1888. guard->m7resources[x] = readNormalNr(bufor,i);
  1889. i+=4;
  1890. }
  1891. break;
  1892. }
  1893. case 8:
  1894. case 9:
  1895. {
  1896. guard->m13489val = bufor[i]; ++i;
  1897. break;
  1898. }
  1899. }
  1900. int limit = readNormalNr(bufor,i); i+=4;
  1901. if(limit == ((int)0xffffffff))
  1902. {
  1903. guard->lastDay = -1;
  1904. }
  1905. else
  1906. {
  1907. guard->lastDay = limit;
  1908. }
  1909. guard->firstVisitText = readString(bufor,i);
  1910. guard->nextVisitText = readString(bufor,i);
  1911. guard->completedText = readString(bufor,i);
  1912. if (guard->firstVisitText.size() && guard->nextVisitText.size() && guard->completedText.size())
  1913. guard->isCustom = true;
  1914. else
  1915. guard->isCustom = false; //randomize all if any text is missing
  1916. }
  1917. TerrainTile & Mapa::getTile( const int3 & tile )
  1918. {
  1919. return terrain[tile.x][tile.y][tile.z];
  1920. }
  1921. const TerrainTile & Mapa::getTile( const int3 & tile ) const
  1922. {
  1923. return terrain[tile.x][tile.y][tile.z];
  1924. }
  1925. bool Mapa::isWaterTile(const int3 &pos) const
  1926. {
  1927. return isInTheMap(pos) && getTile(pos).tertype == TerrainTile::water;
  1928. }
  1929. void Mapa::checkForObjectives()
  1930. {
  1931. if(isInTheMap(victoryCondition.pos))
  1932. {
  1933. const std::vector <CGObjectInstance*> &objs = getTile(victoryCondition.pos).visitableObjects;
  1934. assert(objs.size());
  1935. victoryCondition.obj = objs.front();
  1936. }
  1937. if(isInTheMap(lossCondition.pos))
  1938. {
  1939. const std::vector <CGObjectInstance*> &objs = getTile(lossCondition.pos).visitableObjects;
  1940. assert(objs.size());
  1941. lossCondition.obj = objs.front();
  1942. }
  1943. }
  1944. LossCondition::LossCondition()
  1945. {
  1946. obj = NULL;
  1947. timeLimit = -1;
  1948. pos = int3(-1,-1,-1);
  1949. }
  1950. CVictoryCondition::CVictoryCondition()
  1951. {
  1952. pos = int3(-1,-1,-1);
  1953. obj = NULL;
  1954. ID = allowNormalVictory = appliesToAI = count = 0;
  1955. }
  1956. bool TerrainTile::entrableTerrain(const TerrainTile *from /*= NULL*/) const
  1957. {
  1958. return entrableTerrain(from ? from->tertype != water : true, from ? from->tertype == water : true);
  1959. }
  1960. bool TerrainTile::entrableTerrain(bool allowLand, bool allowSea) const
  1961. {
  1962. return tertype != rock
  1963. && (allowSea && tertype == water || allowLand && tertype != water);
  1964. }
  1965. bool TerrainTile::isClear(const TerrainTile *from /*= NULL*/) const
  1966. {
  1967. return entrableTerrain(from) && !blocked;
  1968. }