map.cpp 52 KB

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