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