map.cpp 46 KB

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