map.cpp 52 KB

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