map.cpp 50 KB

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