map.cpp 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147
  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. int artID = bufor[i]; ++i;
  554. if(artID!=255) //not none quest
  555. {
  556. hut->m5arts.push_back(artID);
  557. hut->missionType = 5;
  558. }
  559. else
  560. {
  561. hut->missionType = 255;
  562. }
  563. }
  564. if(hut->missionType!=255)
  565. {
  566. unsigned char rewardType = bufor[i]; ++i;
  567. hut->rewardType = rewardType;
  568. switch(rewardType)
  569. {
  570. case 1:
  571. {
  572. hut->rVal = readNormalNr(bufor,i); i+=4;
  573. break;
  574. }
  575. case 2:
  576. {
  577. hut->rVal = readNormalNr(bufor,i); i+=4;
  578. break;
  579. }
  580. case 3:
  581. {
  582. hut->rVal = bufor[i]; ++i;
  583. break;
  584. }
  585. case 4:
  586. {
  587. hut->rVal = bufor[i]; ++i;
  588. break;
  589. }
  590. case 5:
  591. {
  592. hut->rID = bufor[i]; ++i;
  593. hut->rVal = readNormalNr(bufor,i, 3); i+=3;
  594. i+=1;
  595. break;
  596. }
  597. case 6:
  598. {
  599. hut->rID = bufor[i]; ++i;
  600. hut->rVal = bufor[i]; ++i;
  601. break;
  602. }
  603. case 7:
  604. {
  605. hut->rID = bufor[i]; ++i;
  606. hut->rVal = bufor[i]; ++i;
  607. break;
  608. }
  609. case 8:
  610. {
  611. hut->rID = readNormalNr(bufor,i, (version == RoE ? 1 : 2)); i+=(version == RoE ? 1 : 2);
  612. break;
  613. }
  614. case 9:
  615. {
  616. hut->rID = bufor[i]; ++i;
  617. break;
  618. }
  619. case 10:
  620. {
  621. if(version>RoE)
  622. {
  623. hut->rID = readNormalNr(bufor,i, 2); i+=2;
  624. hut->rVal = readNormalNr(bufor,i, 2); i+=2;
  625. }
  626. else
  627. {
  628. hut->rID = bufor[i]; ++i;
  629. hut->rVal = readNormalNr(bufor,i, 2); i+=2;
  630. }
  631. break;
  632. }
  633. }// end of internal switch
  634. i+=2;
  635. }
  636. else //missionType==255
  637. {
  638. i+=3;
  639. }
  640. return i;
  641. }
  642. void Mapa::loadTown( CGObjectInstance * &nobj, const unsigned char * bufor, int &i, int subid)
  643. {
  644. CGTownInstance * nt = new CGTownInstance();
  645. //(*(static_cast<CGObjectInstance*>(nt))) = *nobj;
  646. //delete nobj;
  647. nobj = nt;
  648. nt->identifier = 0;
  649. if(version>RoE)
  650. {
  651. nt->identifier = readNormalNr(bufor,i); i+=4;
  652. }
  653. nt->tempOwner = bufor[i]; ++i;
  654. if(readChar(bufor,i)) //has name
  655. nt->name = readString(bufor,i);
  656. if(readChar(bufor,i))//true if garrison isn't empty
  657. nt->setArmy(readCreatureSet(bufor, i, 7, version > RoE));
  658. nt->formation = bufor[i]; ++i;
  659. if(readChar(bufor,i)) //custom buildings info
  660. {
  661. //built buildings
  662. for(int byte=0;byte<6;byte++)
  663. {
  664. for(int bit=0;bit<8;bit++)
  665. if(bufor[i] & (1<<bit))
  666. nt->builtBuildings.insert(byte*8+bit);
  667. i++;
  668. }
  669. //forbidden buildings
  670. for(int byte=6;byte<12;byte++)
  671. {
  672. for(int bit=0;bit<8;bit++)
  673. if(bufor[i] & (1<<bit))
  674. nt->forbiddenBuildings.insert((byte-6)*8+bit);
  675. i++;
  676. }
  677. nt->builtBuildings = convertBuildings(nt->builtBuildings,subid);
  678. nt->forbiddenBuildings = convertBuildings(nt->forbiddenBuildings,subid);
  679. }
  680. else //standard buildings
  681. {
  682. if(readChar(bufor,i)) //has fort
  683. nt->builtBuildings.insert(7);
  684. nt->builtBuildings.insert(-50); //means that set of standard building should be included
  685. }
  686. int ist = i;
  687. if(version>RoE)
  688. {
  689. for(i; i<ist+9; ++i)
  690. {
  691. unsigned char c = bufor[i];
  692. for(int yy=0; yy<8; ++yy)
  693. {
  694. if((i-ist)*8+yy < SPELLS_QUANTITY)
  695. {
  696. if(c == (c|((unsigned char)intPow(2, yy))))
  697. nt->obligatorySpells.push_back((i-ist)*8+yy);
  698. }
  699. }
  700. }
  701. }
  702. ist = i;
  703. for(i; i<ist+9; ++i)
  704. {
  705. unsigned char c = bufor[i];
  706. for(int yy=0; yy<8; ++yy)
  707. {
  708. if((i-ist)*8+yy < SPELLS_QUANTITY)
  709. {
  710. if(c != (c|((unsigned char)intPow(2, yy))))
  711. nt->possibleSpells.push_back((i-ist)*8+yy);
  712. }
  713. }
  714. }
  715. /////// reading castle events //////////////////////////////////
  716. int numberOfEvent = readNormalNr(bufor,i); i+=4;
  717. for(int gh = 0; gh<numberOfEvent; ++gh)
  718. {
  719. CCastleEvent nce;
  720. nce.name = readString(bufor,i);
  721. nce.message = readString(bufor,i);
  722. nce.resources.resize(RESOURCE_QUANTITY);
  723. for(int x=0; x < 7; x++)
  724. {
  725. nce.resources[x] = readNormalNr(bufor,i);
  726. i+=4;
  727. }
  728. nce.players = bufor[i]; ++i;
  729. if(version > AB)
  730. {
  731. nce.forHuman = bufor[i]; ++i;
  732. }
  733. else
  734. nce.forHuman = true;
  735. nce.forComputer = bufor[i]; ++i;
  736. nce.firstShow = readNormalNr(bufor,i, 2); i+=2;
  737. nce.forEvery = bufor[i]; ++i;
  738. i+=17;
  739. for(int kk=0; kk<6; ++kk)
  740. {
  741. nce.bytes[kk] = bufor[i]; ++i;
  742. }
  743. for(int vv=0; vv<7; ++vv)
  744. {
  745. nce.gen[vv] = readNormalNr(bufor,i, 2); i+=2;
  746. }
  747. i+=4;
  748. nt->events.insert(nce);
  749. }//castle events have been read
  750. if(version > AB)
  751. {
  752. nt->alignment = bufor[i]; ++i;
  753. }
  754. else
  755. nt->alignment = 0xff;
  756. i+=3;
  757. nt->builded = 0;
  758. nt->destroyed = 0;
  759. nt->garrisonHero = NULL;
  760. }
  761. void Mapa::loadHero( CGObjectInstance * &nobj, const unsigned char * bufor, int &i )
  762. {
  763. CGHeroInstance * nhi = new CGHeroInstance;
  764. nobj=nhi;
  765. int identifier = 0;
  766. if(version>RoE)
  767. {
  768. identifier = readNormalNr(bufor,i, 4); i+=4;
  769. }
  770. ui8 owner = bufor[i++];
  771. nobj->subID = readNormalNr(bufor,i, 1); ++i;
  772. for(unsigned int j=0; j<predefinedHeroes.size(); j++)
  773. {
  774. if(predefinedHeroes[j]->subID == nobj->subID)
  775. {
  776. *nhi = *predefinedHeroes[j];
  777. break;
  778. }
  779. }
  780. nobj->setOwner(owner);
  781. //(*(static_cast<CGObjectInstance*>(nhi))) = *nobj;
  782. //delete nobj;
  783. nhi->portrait = nhi->subID;
  784. for(unsigned int j=0; j<disposedHeroes.size(); j++)
  785. {
  786. if(disposedHeroes[j].ID == nhi->subID)
  787. {
  788. nhi->name = disposedHeroes[j].name;
  789. nhi->portrait = disposedHeroes[j].portrait;
  790. break;
  791. }
  792. }
  793. heroesToBeat[identifier] = nhi;
  794. if(readChar(bufor,i))//true if hero has nonstandard name
  795. nhi->name = readString(bufor,i);
  796. if(version>AB)
  797. {
  798. if(readChar(bufor,i))//true if hero's experience is greater than 0
  799. { nhi->exp = readNormalNr(bufor,i); i+=4; }
  800. else
  801. nhi->exp = 0xffffffff;
  802. }
  803. else
  804. {
  805. nhi->exp = readNormalNr(bufor,i); i+=4;
  806. if(!nhi->exp) //0 means "not set" in <=AB maps
  807. nhi->exp = 0xffffffff;
  808. }
  809. bool portrait=bufor[i]; ++i;
  810. if (portrait)
  811. nhi->portrait = bufor[i++];
  812. if(readChar(bufor,i))//true if hero has specified abilities
  813. {
  814. int howMany = readNormalNr(bufor,i); i+=4;
  815. nhi->secSkills.resize(howMany);
  816. for(int yy=0; yy<howMany; ++yy)
  817. {
  818. nhi->secSkills[yy].first = readNormalNr(bufor,i, 1); ++i;
  819. nhi->secSkills[yy].second = readNormalNr(bufor,i, 1); ++i;
  820. }
  821. }
  822. if(readChar(bufor,i))//true if hero has nonstandard garrison
  823. nhi->setArmy(readCreatureSet(bufor, i, 7, version > RoE));
  824. nhi->formation =bufor[i]; ++i; //formation
  825. bool artSet = bufor[i]; ++i; //true if artifact set is not default (hero has some artifacts)
  826. int artmask = version == RoE ? 0xff : 0xffff;
  827. int artidlen = version == RoE ? 1 : 2;
  828. if(artSet)
  829. {
  830. for(int pom=0;pom<16;pom++)
  831. {
  832. int id = readNormalNr(bufor,i, artidlen); i+=artidlen;
  833. if(id!=artmask)
  834. VLC->arth->equipArtifact(nhi->artifWorn, pom, id, &nhi->bonuses);
  835. }
  836. //misc5 art //17
  837. if(version>=SoD)
  838. {
  839. int id = readNormalNr(bufor,i, artidlen); i+=artidlen;
  840. if(id!=artmask)
  841. VLC->arth->equipArtifact(nhi->artifWorn, 16, id, &nhi->bonuses);
  842. else
  843. VLC->arth->equipArtifact(nhi->artifWorn, 16, 3, &nhi->bonuses); //catapult by default
  844. }
  845. //spellbook
  846. int id = readNormalNr(bufor,i, artidlen); i+=artidlen;
  847. if(id!=artmask)
  848. VLC->arth->equipArtifact(nhi->artifWorn, 17, id, &nhi->bonuses);
  849. //19 //???what is that? gap in file or what? - it's probably fifth slot..
  850. if(version>RoE)
  851. {
  852. id = readNormalNr(bufor,i, artidlen); i+=artidlen;
  853. if(id!=artmask)
  854. VLC->arth->equipArtifact(nhi->artifWorn, 18, id, &nhi->bonuses);
  855. }
  856. else
  857. i+=1;
  858. //bag artifacts //20
  859. int amount = readNormalNr(bufor,i, 2); i+=2; //number of artifacts in hero's bag
  860. if(amount>0)
  861. {
  862. for(int ss=0; ss<amount; ++ss)
  863. {
  864. id = readNormalNr(bufor,i, artidlen); i+=artidlen;
  865. if(id!=artmask)
  866. nhi->giveArtifact(id);
  867. }
  868. }
  869. } //artifacts
  870. nhi->patrol.patrolRadious = readNormalNr(bufor,i, 1); ++i;
  871. if(nhi->patrol.patrolRadious == 0xff)
  872. nhi->patrol.patrolling = false;
  873. else
  874. nhi->patrol.patrolling = true;
  875. if(version>RoE)
  876. {
  877. if(readChar(bufor,i))//true if hero has nonstandard (mapmaker defined) biography
  878. nhi->biography = readString(bufor,i);
  879. nhi->sex = !(bufor[i]); ++i;
  880. }
  881. //spells
  882. if(version>AB)
  883. {
  884. bool areSpells = bufor[i]; ++i;
  885. if(areSpells) //TODO: sprawdziæ //seems to be ok - tow
  886. {
  887. nhi->spells.insert(0xffffffff); //placeholder "preset spells"
  888. int ist = i;
  889. for(i; i<ist+9; ++i)
  890. {
  891. unsigned char c = bufor[i];
  892. for(int yy=0; yy<8; ++yy)
  893. {
  894. if((i-ist)*8+yy < SPELLS_QUANTITY)
  895. {
  896. if(c == (c|((unsigned char)intPow(2, yy))))
  897. nhi->spells.insert((i-ist)*8+yy);
  898. }
  899. }
  900. }
  901. }
  902. }
  903. else if(version==AB) //we can read one spell
  904. {
  905. unsigned char buff = bufor[i]; ++i;
  906. if(buff != 254)
  907. {
  908. nhi->spells.insert(0xffffffff); //placeholder "preset spells"
  909. if(buff < 254) //255 means no spells
  910. nhi->spells.insert(buff);
  911. }
  912. }
  913. //spells loaded
  914. if(version>AB)
  915. {
  916. if(readChar(bufor,i))//customPrimSkills
  917. {
  918. for(int xx=0;xx<4;xx++)
  919. nhi->pushPrimSkill(xx, bufor[i++]);
  920. }
  921. }
  922. i+=16;
  923. nhi->moveDir = 4;
  924. nhi->isStanding = true;
  925. nhi->level = -1;
  926. nhi->mana = -1;
  927. nhi->movement = -1;
  928. }
  929. void Mapa::readRumors( const unsigned char * bufor, int &i)
  930. {
  931. int rumNr = readNormalNr(bufor,i,4);i+=4;
  932. for (int it=0;it<rumNr;it++)
  933. {
  934. Rumor ourRumor;
  935. int nameL = readNormalNr(bufor,i,4);i+=4; //read length of name of rumor
  936. for (int zz=0; zz<nameL; zz++)
  937. ourRumor.name+=bufor[i++];
  938. nameL = readNormalNr(bufor,i,4);i+=4; //read length of rumor
  939. for (int zz=0; zz<nameL; zz++)
  940. ourRumor.text+=bufor[i++];
  941. rumors.push_back(ourRumor); //add to our list
  942. }
  943. }
  944. void Mapa::readHeader( const unsigned char * bufor, int &i)
  945. {
  946. //reading allowed heroes (20 bytes)
  947. int ist = i;
  948. ist=i; //starting i for loop
  949. unsigned char disp = 0;
  950. if(version>=SoD)
  951. {
  952. disp = bufor[i++];
  953. disposedHeroes.resize(disp);
  954. for(int g=0; g<disp; ++g)
  955. {
  956. disposedHeroes[g].ID = bufor[i++];
  957. disposedHeroes[g].portrait = bufor[i++];
  958. int lenbuf = readNormalNr(bufor,i); i+=4;
  959. for (int zz=0; zz<lenbuf; zz++)
  960. disposedHeroes[g].name+=bufor[i++];
  961. disposedHeroes[g].players = bufor[i++];
  962. }
  963. }
  964. i+=31; //omitting NULLS
  965. allowedArtifact.resize(ARTIFACTS_QUANTITY);
  966. for(unsigned int x=0;x<allowedArtifact.size();x++)
  967. allowedArtifact[x] = true;
  968. //reading allowed artifacts: 17 or 18 bytes
  969. if(version!=RoE)
  970. {
  971. ist=i; //starting i for loop
  972. for(i; i<ist+(version==AB ? 17 : 18); ++i)
  973. {
  974. unsigned char c = bufor[i];
  975. for(int yy=0; yy<8; ++yy)
  976. {
  977. if((i-ist)*8+yy < ARTIFACTS_QUANTITY)
  978. {
  979. if(c == (c|((unsigned char)intPow(2, yy))))
  980. allowedArtifact[(i-ist)*8+yy] = false;
  981. }
  982. }
  983. }//allowed artifacts have been read
  984. }
  985. allowedSpell.resize(SPELLS_QUANTITY);
  986. for(unsigned int x=0;x<allowedSpell.size();x++)
  987. allowedSpell[x] = true;
  988. allowedAbilities.resize(SKILL_QUANTITY);
  989. for(unsigned int x=0;x<allowedAbilities.size();x++)
  990. allowedAbilities[x] = true;
  991. if(version>=SoD)
  992. {
  993. //reading allowed spells (9 bytes)
  994. ist=i; //starting i for loop
  995. for(i; i<ist+9; ++i)
  996. {
  997. unsigned char c = bufor[i];
  998. for(int yy=0; yy<8; ++yy)
  999. if((i-ist)*8+yy < SPELLS_QUANTITY)
  1000. if(c == (c|((unsigned char)intPow(2, yy))))
  1001. allowedSpell[(i-ist)*8+yy] = false;
  1002. }
  1003. //allowed hero's abilities (4 bytes)
  1004. ist=i; //starting i for loop
  1005. for(i; i<ist+4; ++i)
  1006. {
  1007. unsigned char c = bufor[i];
  1008. for(int yy=0; yy<8; ++yy)
  1009. {
  1010. if((i-ist)*8+yy < SKILL_QUANTITY)
  1011. {
  1012. if(c == (c|((unsigned char)intPow(2, yy))))
  1013. allowedAbilities[(i-ist)*8+yy] = false;
  1014. }
  1015. }
  1016. }
  1017. }
  1018. }
  1019. void Mapa::readPredefinedHeroes( const unsigned char * bufor, int &i)
  1020. {
  1021. switch(version)
  1022. {
  1023. case WoG: case SoD:
  1024. {
  1025. for(int z=0;z<HEROES_QUANTITY;z++) //disposed heroes
  1026. {
  1027. int custom = bufor[i++];
  1028. if(!custom)
  1029. continue;
  1030. CGHeroInstance * cgh = new CGHeroInstance;
  1031. cgh->ID = HEROI_TYPE;
  1032. cgh->subID = z;
  1033. if(readChar(bufor,i))//true if hore's experience is greater than 0
  1034. { cgh->exp = readNormalNr(bufor,i); i+=4; }
  1035. else
  1036. cgh->exp = 0;
  1037. if(readChar(bufor,i))//true if hero has specified abilities
  1038. {
  1039. int howMany = readNormalNr(bufor,i); i+=4;
  1040. cgh->secSkills.resize(howMany);
  1041. for(int yy=0; yy<howMany; ++yy)
  1042. {
  1043. cgh->secSkills[yy].first = readNormalNr(bufor,i, 1); ++i;
  1044. cgh->secSkills[yy].second = readNormalNr(bufor,i, 1); ++i;
  1045. }
  1046. }
  1047. bool artSet = bufor[i]; ++i; //true if artifact set is not default (hero has some artifacts)
  1048. int artmask = version == RoE ? 0xff : 0xffff;
  1049. int artidlen = version == RoE ? 1 : 2;
  1050. if(artSet)
  1051. {
  1052. for(int pom=0;pom<16;pom++)
  1053. {
  1054. int id = readNormalNr(bufor,i, artidlen); i+=artidlen;
  1055. if(id!=artmask)
  1056. VLC->arth->equipArtifact(cgh->artifWorn, pom, id, &cgh->bonuses);
  1057. }
  1058. //misc5 art //17
  1059. if(version>=SoD)
  1060. {
  1061. i+=2;
  1062. //int id = readNormalNr(bufor,i, artidlen); i+=artidlen;
  1063. //if(id!=artmask)
  1064. // spec->artifWorn[16] = id;
  1065. }
  1066. //spellbook
  1067. int id = readNormalNr(bufor,i, artidlen); i+=artidlen;
  1068. if(id!=artmask)
  1069. VLC->arth->equipArtifact(cgh->artifWorn, 17, id, &cgh->bonuses);
  1070. //19 //???what is that? gap in file or what? - it's probably fifth slot..
  1071. if(version>RoE)
  1072. {
  1073. id = readNormalNr(bufor,i, artidlen); i+=artidlen;
  1074. if(id!=artmask)
  1075. VLC->arth->equipArtifact(cgh->artifWorn, 18, id, &cgh->bonuses);
  1076. }
  1077. else
  1078. i+=1;
  1079. //bag artifacts //20
  1080. int amount = readNormalNr(bufor,i, 2); i+=2; //number of artifacts in hero's bag
  1081. if(amount>0)
  1082. {
  1083. for(int ss=0; ss<amount; ++ss)
  1084. {
  1085. id = readNormalNr(bufor,i, artidlen); i+=artidlen;
  1086. if(id!=artmask)
  1087. cgh->giveArtifact(id);
  1088. }
  1089. }
  1090. } //artifacts
  1091. if(readChar(bufor,i))//customBio
  1092. cgh->biography = readString(bufor,i);
  1093. int sex = bufor[i++]; // 0xFF is default, 00 male, 01 female
  1094. if(readChar(bufor,i))//are spells
  1095. {
  1096. int ist = i;
  1097. for(i; i<ist+9; ++i)
  1098. {
  1099. unsigned char c = bufor[i];
  1100. for(int yy=0; yy<8; ++yy)
  1101. {
  1102. if((i-ist)*8+yy < SPELLS_QUANTITY)
  1103. {
  1104. if(c == (c|((unsigned char)intPow(2, yy))))
  1105. cgh->spells.insert((i-ist)*8+yy);
  1106. }
  1107. }
  1108. }
  1109. }
  1110. if(readChar(bufor,i))//customPrimSkills
  1111. {
  1112. for(int xx=0;xx<4;xx++)
  1113. cgh->pushPrimSkill(xx, bufor[i++]);
  1114. }
  1115. predefinedHeroes.push_back(cgh);
  1116. }
  1117. break;
  1118. }
  1119. case RoE:
  1120. i+=0;
  1121. break;
  1122. }
  1123. }
  1124. void Mapa::readTerrain( const unsigned char * bufor, int &i)
  1125. {
  1126. terrain = new TerrainTile**[width]; // allocate memory
  1127. for (int ii=0;ii<width;ii++)
  1128. {
  1129. terrain[ii] = new TerrainTile*[height]; // allocate memory
  1130. for(int jj=0;jj<height;jj++)
  1131. terrain[ii][jj] = new TerrainTile[twoLevel+1];
  1132. }
  1133. for (int c=0; c<width; c++) // reading terrain
  1134. {
  1135. for (int z=0; z<height; z++)
  1136. {
  1137. terrain[z][c][0].tertype = static_cast<TerrainTile::EterrainType>(bufor[i++]);
  1138. terrain[z][c][0].terview = bufor[i++];
  1139. terrain[z][c][0].nuine = static_cast<TerrainTile::Eriver>(bufor[i++]);
  1140. terrain[z][c][0].rivDir = bufor[i++];
  1141. terrain[z][c][0].malle = static_cast<TerrainTile::Eroad>(bufor[i++]);
  1142. terrain[z][c][0].roadDir = bufor[i++];
  1143. terrain[z][c][0].siodmyTajemniczyBajt = bufor[i++];
  1144. terrain[z][c][0].blocked = 0;
  1145. terrain[z][c][0].visitable = 0;
  1146. }
  1147. }
  1148. if (twoLevel) // read underground terrain
  1149. {
  1150. for (int c=0; c<width; c++) // reading terrain
  1151. {
  1152. for (int z=0; z<height; z++)
  1153. {
  1154. terrain[z][c][1].tertype = static_cast<TerrainTile::EterrainType>(bufor[i++]);
  1155. terrain[z][c][1].terview = bufor[i++];
  1156. terrain[z][c][1].nuine = static_cast<TerrainTile::Eriver>(bufor[i++]);
  1157. terrain[z][c][1].rivDir = bufor[i++];
  1158. terrain[z][c][1].malle = static_cast<TerrainTile::Eroad>(bufor[i++]);
  1159. terrain[z][c][1].roadDir = bufor[i++];
  1160. terrain[z][c][1].siodmyTajemniczyBajt = bufor[i++];
  1161. terrain[z][c][1].blocked = 0;
  1162. terrain[z][c][1].visitable = 0;
  1163. }
  1164. }
  1165. }
  1166. }
  1167. void Mapa::readDefInfo( const unsigned char * bufor, int &i)
  1168. {
  1169. int defAmount = readNormalNr(bufor,i); i+=4;
  1170. defy.reserve(defAmount+8);
  1171. for (int idd = 0 ; idd<defAmount; idd++) // reading defs
  1172. {
  1173. CGDefInfo * vinya = new CGDefInfo(); // info about new def
  1174. //reading name
  1175. int nameLength = readNormalNr(bufor,i,4);i+=4;
  1176. vinya->name.reserve(nameLength);
  1177. for (int cd=0;cd<nameLength;cd++)
  1178. {
  1179. vinya->name += bufor[i++];
  1180. }
  1181. std::transform(vinya->name.begin(),vinya->name.end(),vinya->name.begin(),(int(*)(int))toupper);
  1182. unsigned char bytes[12];
  1183. for (int v=0; v<12; v++) // read info
  1184. {
  1185. bytes[v] = bufor[i++];
  1186. }
  1187. vinya->terrainAllowed = readNormalNr(bufor,i,2);i+=2;
  1188. vinya->terrainMenu = readNormalNr(bufor,i,2);i+=2;
  1189. vinya->id = readNormalNr(bufor,i,4);i+=4;
  1190. vinya->subid = readNormalNr(bufor,i,4);i+=4;
  1191. vinya->type = bufor[i++];
  1192. vinya->printPriority = bufor[i++];
  1193. for (int zi=0; zi<6; zi++)
  1194. {
  1195. vinya->blockMap[zi] = reverse(bytes[zi]);
  1196. }
  1197. for (int zi=0; zi<6; zi++)
  1198. {
  1199. vinya->visitMap[zi] = reverse(bytes[6+zi]);
  1200. }
  1201. i+=16;
  1202. if(vinya->id!=HEROI_TYPE && vinya->id!=70)
  1203. {
  1204. CGDefInfo *h = VLC->dobjinfo->gobjs[vinya->id][vinya->subid];
  1205. if(!h)
  1206. {
  1207. //remove fake entry
  1208. VLC->dobjinfo->gobjs[vinya->id].erase(vinya->subid);
  1209. if(VLC->dobjinfo->gobjs[vinya->id].size())
  1210. VLC->dobjinfo->gobjs.erase(vinya->id);
  1211. tlog2<<"\t\tWarning: no defobjinfo entry for object ID="<<vinya->id<<" subID=" << vinya->subid<<std::endl;
  1212. }
  1213. else
  1214. {
  1215. vinya->visitDir = VLC->dobjinfo->gobjs[vinya->id][vinya->subid]->visitDir;
  1216. }
  1217. }
  1218. else
  1219. {
  1220. vinya->visitDir = 0xff;
  1221. }
  1222. if(vinya->id == EVENTI_TYPE)
  1223. std::memset(vinya->blockMap,255,6);
  1224. //calculating coverageMap
  1225. std::string nameCopy = vinya->name;
  1226. std::string msk = spriteh->getTextFile(nameCopy.replace( nameCopy.size()-4, 4, ".MSK" ));
  1227. for(int i=0; i<6; ++i)
  1228. {
  1229. vinya->coverageMap[i] = msk[i+2];
  1230. vinya->shadowCoverage[i] = msk[i+8];
  1231. }
  1232. defy.push_back(vinya); // add this def to the vector
  1233. }
  1234. //add holes - they always can appear
  1235. for (int i = 0; i < 8 ; i++)
  1236. {
  1237. defy.push_back(VLC->dobjinfo->gobjs[124][i]);
  1238. }
  1239. }
  1240. void Mapa::readObjects( const unsigned char * bufor, int &i)
  1241. {
  1242. int howManyObjs = readNormalNr(bufor,i, 4); i+=4;
  1243. for(int ww=0; ww<howManyObjs; ++ww) //comment this line to turn loading objects off
  1244. {
  1245. CGObjectInstance * nobj = 0;
  1246. int3 pos;
  1247. pos.x = bufor[i++];
  1248. pos.y = bufor[i++];
  1249. pos.z = bufor[i++];
  1250. int defnum = readNormalNr(bufor,i, 4); i+=4;
  1251. CGDefInfo * defInfo = defy[defnum];
  1252. i+=5;
  1253. switch(defInfo->id)
  1254. {
  1255. case EVENTI_TYPE: //for event objects
  1256. {
  1257. CGEvent *evnt = new CGEvent();
  1258. nobj = evnt;
  1259. bool guardMess = bufor[i]; ++i;
  1260. if(guardMess)
  1261. {
  1262. int messLong = readNormalNr(bufor,i, 4); i+=4;
  1263. if(messLong>0)
  1264. {
  1265. for(int yy=0; yy<messLong; ++yy)
  1266. {
  1267. evnt->message +=bufor[i+yy];
  1268. }
  1269. i+=messLong;
  1270. }
  1271. if(bufor[i++])
  1272. {
  1273. evnt->setArmy(readCreatureSet(bufor, i, 7, version > RoE));
  1274. }
  1275. i+=4;
  1276. }
  1277. evnt->gainedExp = readNormalNr(bufor,i, 4); i+=4;
  1278. evnt->manaDiff = readNormalNr(bufor,i, 4); i+=4;
  1279. evnt->moraleDiff = readNormalNr(bufor,i, 1, true); ++i;
  1280. evnt->luckDiff = readNormalNr(bufor,i, 1, true); ++i;
  1281. evnt->resources.resize(RESOURCE_QUANTITY);
  1282. for(int x=0; x<7; x++)
  1283. {
  1284. evnt->resources[x] = readNormalNr(bufor,i);
  1285. i+=4;
  1286. }
  1287. evnt->primskills.resize(PRIMARY_SKILLS);
  1288. for(int x=0; x<4; x++)
  1289. {
  1290. evnt->primskills[x] = readNormalNr(bufor,i, 1);
  1291. i++;
  1292. }
  1293. int gabn; //number of gained abilities
  1294. gabn = readNormalNr(bufor,i, 1); ++i;
  1295. for(int oo = 0; oo<gabn; ++oo)
  1296. {
  1297. evnt->abilities.push_back(readNormalNr(bufor,i, 1)); ++i;
  1298. evnt->abilityLevels.push_back(readNormalNr(bufor,i, 1)); ++i;
  1299. }
  1300. int gart = readNormalNr(bufor,i, 1); ++i; //number of gained artifacts
  1301. for(int oo = 0; oo<gart; ++oo)
  1302. {
  1303. evnt->artifacts.push_back(readNormalNr(bufor,i, (version == RoE ? 1 : 2))); i+=(version == RoE ? 1 : 2);
  1304. }
  1305. int gspel = readNormalNr(bufor,i, 1); ++i; //number of gained spells
  1306. for(int oo = 0; oo<gspel; ++oo)
  1307. {
  1308. evnt->spells.push_back(readNormalNr(bufor,i, 1)); ++i;
  1309. }
  1310. int gcre = readNormalNr(bufor,i, 1); ++i; //number of gained creatures
  1311. evnt->creatures = readCreatureSet(bufor,i,gcre,(version>RoE));
  1312. i+=8;
  1313. evnt->availableFor = readNormalNr(bufor,i, 1); ++i;
  1314. evnt->computerActivate = readNormalNr(bufor,i, 1); ++i;
  1315. evnt->removeAfterVisit = readNormalNr(bufor,i, 1); ++i;
  1316. evnt->humanActivate = true;
  1317. i+=4;
  1318. break;
  1319. }
  1320. case 34: case 70: case 62: //34 - hero; 70 - random hero; 62 - prison
  1321. {
  1322. loadHero(nobj, bufor, i);
  1323. break;
  1324. }
  1325. case 4: //Arena
  1326. case 51: //Mercenary Camp
  1327. case 23: //Marletto Tower
  1328. case 61: // Star Axis
  1329. case 32: // Garden of Revelation
  1330. case 100: //Learning Stone
  1331. case 102: //Tree of Knowledge
  1332. case 41: //Library of Enlightenment
  1333. case 47: //School of Magic
  1334. case 107: //School of War
  1335. {
  1336. nobj = new CGVisitableOPH();
  1337. break;
  1338. }
  1339. case 55: //mystical garden
  1340. case 112://windmill
  1341. case 109://water wheel
  1342. {
  1343. nobj = new CGVisitableOPW();
  1344. break;
  1345. }
  1346. case 43: //teleport
  1347. case 44: //teleport
  1348. case 45: //teleport
  1349. case 103://subterranean gate
  1350. {
  1351. nobj = new CGTeleport();
  1352. break;
  1353. }
  1354. case 12: //campfire
  1355. case 29: //Flotsam
  1356. case 82: //Sea Chest
  1357. case 86: //Shipwreck Survivor
  1358. case 101://treasure chest
  1359. {
  1360. nobj = new CGPickable();
  1361. break;
  1362. }
  1363. case 54: //Monster
  1364. case 71: case 72: case 73: case 74: case 75: // Random Monster 1 - 4
  1365. case 162: case 163: case 164: // Random Monster 5 - 7
  1366. {
  1367. CGCreature *cre = new CGCreature();
  1368. nobj = cre;
  1369. if(version>RoE)
  1370. {
  1371. cre->identifier = readNormalNr(bufor,i); i+=4;
  1372. monsters[cre->identifier] = cre;
  1373. }
  1374. CStackInstance hlp;
  1375. hlp.count = readNormalNr(bufor,i, 2); i+=2;
  1376. //type will be set during initialization
  1377. cre->slots[0] = hlp;
  1378. cre->character = bufor[i]; ++i;
  1379. bool isMesTre = bufor[i]; ++i; //true if there is message or treasury
  1380. if(isMesTre)
  1381. {
  1382. cre->message = readString(bufor,i);
  1383. cre->resources.resize(RESOURCE_QUANTITY);
  1384. for(int j=0; j<7; j++)
  1385. {
  1386. cre->resources[j] = readNormalNr(bufor,i); i+=4;
  1387. }
  1388. int artID = readNormalNr(bufor,i, (version == RoE ? 1 : 2)); i+=(version == RoE ? 1 : 2);
  1389. if(version==RoE)
  1390. {
  1391. if(artID!=0xff)
  1392. cre->gainedArtifact = artID;
  1393. else
  1394. cre->gainedArtifact = -1;
  1395. }
  1396. else
  1397. {
  1398. if(artID!=0xffff)
  1399. cre->gainedArtifact = artID;
  1400. else
  1401. cre->gainedArtifact = -1;
  1402. }
  1403. }
  1404. cre->neverFlees = bufor[i]; ++i;
  1405. cre->notGrowingTeam = bufor[i]; ++i;
  1406. i+=2;;
  1407. break;
  1408. }
  1409. case 59: case 91: //ocean bottle and sign
  1410. {
  1411. CGSignBottle *sb = new CGSignBottle();
  1412. nobj = sb;
  1413. sb->message = readString(bufor,i);
  1414. i+=4;
  1415. break;
  1416. }
  1417. case 83: //seer's hut
  1418. {
  1419. i = loadSeerHut(bufor, i, nobj);
  1420. break;
  1421. }
  1422. case 113: //witch hut
  1423. {
  1424. CGWitchHut *wh = new CGWitchHut();
  1425. nobj = wh;
  1426. if(version>RoE) //in reo we cannot specify it - all are allowed (I hope)
  1427. {
  1428. int ist=i; //starting i for loop
  1429. for(i; i<ist+4; ++i)
  1430. {
  1431. unsigned char c = bufor[i];
  1432. for(int yy=0; yy<8; ++yy)
  1433. {
  1434. if((i-ist)*8+yy < SKILL_QUANTITY)
  1435. {
  1436. if(c == (c|((unsigned char)intPow(2, yy))))
  1437. wh->allowedAbilities.push_back((i-ist)*8+yy);
  1438. }
  1439. }
  1440. }
  1441. }
  1442. else //(RoE map)
  1443. {
  1444. for(int gg=0; gg<SKILL_QUANTITY; ++gg)
  1445. {
  1446. wh->allowedAbilities.push_back(gg);
  1447. }
  1448. }
  1449. break;
  1450. }
  1451. case 81: //scholar
  1452. {
  1453. CGScholar *sch = new CGScholar();
  1454. nobj = sch;
  1455. sch->bonusType = bufor[i++];
  1456. sch->bonusID = bufor[i++];
  1457. i+=6;
  1458. break;
  1459. }
  1460. case 33: case 219: //garrison
  1461. {
  1462. CGGarrison *gar = new CGGarrison();
  1463. nobj = gar;
  1464. nobj->setOwner(bufor[i++]);
  1465. i+=3;
  1466. gar->setArmy(readCreatureSet(bufor, i, 7, version > RoE));
  1467. if(version > RoE)
  1468. {
  1469. gar->removableUnits = bufor[i]; ++i;
  1470. }
  1471. else
  1472. gar->removableUnits = true;
  1473. i+=8;
  1474. break;
  1475. }
  1476. case 5: //artifact
  1477. case 65: case 66: case 67: case 68: case 69: //random artifact
  1478. case 93: //spell scroll
  1479. {
  1480. CGArtifact *art = new CGArtifact();
  1481. nobj = art;
  1482. bool areSettings = bufor[i]; ++i;
  1483. if(areSettings)
  1484. {
  1485. art->message = readString(bufor,i);
  1486. bool areGuards = bufor[i]; ++i;
  1487. if(areGuards)
  1488. {
  1489. art->setArmy(readCreatureSet(bufor, i, 7, version > RoE));
  1490. }
  1491. i+=4;
  1492. }
  1493. if(defInfo->id==93)
  1494. {
  1495. art->spell = readNormalNr(bufor,i);
  1496. i+=4;
  1497. }
  1498. break;
  1499. }
  1500. case 76: case 79: //random resource; resource
  1501. {
  1502. CGResource *res = new CGResource();
  1503. nobj = res;
  1504. bool isMessGuard = bufor[i]; ++i;
  1505. if(isMessGuard)
  1506. {
  1507. res->message = readString(bufor,i);
  1508. if(bufor[i++])
  1509. {
  1510. res->setArmy(readCreatureSet(bufor, i, 7, version > RoE));
  1511. }
  1512. i+=4;
  1513. }
  1514. res->amount = readNormalNr(bufor,i); i+=4;
  1515. if (defInfo->subid == 6) // Gold is multiplied by 100.
  1516. res->amount *= 100;
  1517. i+=4;
  1518. break;
  1519. }
  1520. case 77: case 98: //random town; town
  1521. {
  1522. loadTown(nobj, bufor, i, defInfo->subid);
  1523. break;
  1524. }
  1525. case 53:
  1526. {
  1527. nobj = new CGMine();
  1528. nobj->setOwner(bufor[i++]);
  1529. i+=3;
  1530. break;
  1531. }
  1532. case 17: case 18: case 19: case 20: //dwellings
  1533. {
  1534. nobj = new CGDwelling();
  1535. nobj->setOwner(bufor[i++]);
  1536. i+=3;
  1537. break;
  1538. }
  1539. case 220://mine (?)
  1540. {
  1541. nobj = new CGObjectInstance();
  1542. nobj->setOwner(bufor[i++]);
  1543. i+=3;
  1544. break;
  1545. }
  1546. case 88: case 89: case 90: //spell shrine
  1547. {
  1548. CGShrine * shr = new CGShrine();
  1549. nobj = shr;
  1550. shr->spell = bufor[i]; i+=4;
  1551. break;
  1552. }
  1553. case 6: //pandora's box
  1554. {
  1555. CGPandoraBox *box = new CGPandoraBox();
  1556. nobj = box;
  1557. bool messg = bufor[i]; ++i;
  1558. if(messg)
  1559. {
  1560. box->message = readString(bufor,i);
  1561. if(bufor[i++])
  1562. {
  1563. box->setArmy(readCreatureSet(bufor, i, 7, version > RoE));
  1564. }
  1565. i+=4;
  1566. }
  1567. box->gainedExp = readNormalNr(bufor,i, 4); i+=4;
  1568. box->manaDiff = readNormalNr(bufor,i, 4); i+=4;
  1569. box->moraleDiff = readNormalNr(bufor,i, 1, true); ++i;
  1570. box->luckDiff = readNormalNr(bufor,i, 1, true); ++i;
  1571. box->resources.resize(RESOURCE_QUANTITY);
  1572. for(int x=0; x<7; x++)
  1573. {
  1574. box->resources[x] = readNormalNr(bufor,i);
  1575. i+=4;
  1576. }
  1577. box->primskills.resize(PRIMARY_SKILLS);
  1578. for(int x=0; x<4; x++)
  1579. {
  1580. box->primskills[x] = readNormalNr(bufor,i, 1);
  1581. i++;
  1582. }
  1583. int gabn; //number of gained abilities
  1584. gabn = readNormalNr(bufor,i, 1); ++i;
  1585. for(int oo = 0; oo<gabn; ++oo)
  1586. {
  1587. box->abilities.push_back(readNormalNr(bufor,i, 1)); ++i;
  1588. box->abilityLevels.push_back(readNormalNr(bufor,i, 1)); ++i;
  1589. }
  1590. int gart = readNormalNr(bufor,i, 1); ++i; //number of gained artifacts
  1591. for(int oo = 0; oo<gart; ++oo)
  1592. {
  1593. if(version > RoE)
  1594. {
  1595. box->artifacts.push_back(readNormalNr(bufor,i, 2)); i+=2;
  1596. }
  1597. else
  1598. {
  1599. box->artifacts.push_back(readNormalNr(bufor,i, 1)); i+=1;
  1600. }
  1601. }
  1602. int gspel = readNormalNr(bufor,i, 1); ++i; //number of gained spells
  1603. for(int oo = 0; oo<gspel; ++oo)
  1604. {
  1605. box->spells.push_back(readNormalNr(bufor,i, 1)); ++i;
  1606. }
  1607. int gcre = readNormalNr(bufor,i, 1); ++i; //number of gained creatures
  1608. box->creatures = readCreatureSet(bufor,i,gcre,(version>RoE));
  1609. i+=8;
  1610. break;
  1611. }
  1612. case 36: //grail
  1613. {
  1614. grailPos = pos;
  1615. grailRadious = readNormalNr(bufor,i); i+=4;
  1616. continue;
  1617. }
  1618. case 217:
  1619. {
  1620. nobj = new CGDwelling();
  1621. CCreGenObjInfo * spec = new CCreGenObjInfo;
  1622. spec->player = readNormalNr(bufor,i); i+=4;
  1623. spec->identifier = readNormalNr(bufor,i); i+=4;
  1624. if(!spec->identifier)
  1625. {
  1626. spec->asCastle = false;
  1627. spec->castles[0] = bufor[i]; ++i;
  1628. spec->castles[1] = bufor[i]; ++i;
  1629. }
  1630. else
  1631. {
  1632. spec->asCastle = true;
  1633. }
  1634. nobj->setOwner(spec->player);
  1635. static_cast<CGDwelling*>(nobj)->info = spec;
  1636. break;
  1637. }
  1638. case 216:
  1639. {
  1640. nobj = new CGDwelling();
  1641. CCreGen2ObjInfo * spec = new CCreGen2ObjInfo;
  1642. spec->player = readNormalNr(bufor,i); i+=4;
  1643. spec->identifier = readNormalNr(bufor,i); i+=4;
  1644. if(!spec->identifier)
  1645. {
  1646. spec->asCastle = false;
  1647. spec->castles[0] = bufor[i]; ++i;
  1648. spec->castles[1] = bufor[i]; ++i;
  1649. }
  1650. else
  1651. {
  1652. spec->asCastle = true;
  1653. }
  1654. spec->minLevel = bufor[i]; ++i;
  1655. spec->maxLevel = bufor[i]; ++i;
  1656. nobj->setOwner(spec->player);
  1657. static_cast<CGDwelling*>(nobj)->info = spec;
  1658. break;
  1659. }
  1660. case 218:
  1661. {
  1662. nobj = new CGDwelling();
  1663. CCreGen3ObjInfo * spec = new CCreGen3ObjInfo;
  1664. spec->player = bufor[i]; ++i;
  1665. i+=3;
  1666. spec->minLevel = bufor[i]; ++i;
  1667. spec->maxLevel = bufor[i]; ++i;
  1668. if(spec->maxLevel>7)
  1669. spec->maxLevel = 7;
  1670. if(spec->minLevel<1)
  1671. spec->minLevel = 1;
  1672. nobj->setOwner(spec->player);
  1673. static_cast<CGDwelling*>(nobj)->info = spec;
  1674. break;
  1675. }
  1676. case 215:
  1677. {
  1678. CGQuestGuard *guard = new CGQuestGuard();
  1679. nobj = guard;
  1680. loadQuest(guard, bufor, i);
  1681. break;
  1682. }
  1683. case 28: //faerie ring
  1684. case 14: //Swan pond
  1685. case 38: //idol of fortune
  1686. case 30: //Fountain of Fortune
  1687. case 64: //Rally Flag
  1688. case 56: //oasis
  1689. case 96: //temple
  1690. case 110://Watering Hole
  1691. case 31: //Fountain of Youth
  1692. case 11: //Buoy
  1693. case 52: //Mermaid
  1694. case 94: //Stables
  1695. {
  1696. nobj = new CGBonusingObject();
  1697. break;
  1698. }
  1699. case 49: //Magic Well
  1700. {
  1701. nobj = new CGMagicWell();
  1702. break;
  1703. }
  1704. case 58: //Redwood Observatory
  1705. case 60: //Pillar of Fire
  1706. {
  1707. nobj = new CGObservatory();
  1708. break;
  1709. }
  1710. case 22: //Corpse
  1711. case 39: //Lean To
  1712. case 105://Wagon
  1713. case 108://Warrior's Tomb
  1714. {
  1715. nobj = new CGOnceVisitable();
  1716. break;
  1717. }
  1718. case 8: //Boat
  1719. {
  1720. nobj = new CGBoat();
  1721. break;
  1722. }
  1723. case 92: //Sirens
  1724. {
  1725. nobj = new CGSirens();
  1726. break;
  1727. }
  1728. case 87: //Shipyard
  1729. {
  1730. nobj = new CGShipyard();
  1731. nobj->setOwner(readNormalNr(bufor,i)); i+=4;
  1732. break;
  1733. }
  1734. case 214: //hero placeholder
  1735. {
  1736. CGHeroPlaceholder *hp = new CGHeroPlaceholder();;
  1737. nobj = hp;
  1738. int a = bufor[i++]; //unkown byte, seems to be always 0 (if not - scream!)
  1739. assert(!a);
  1740. int htid = bufor[i++]; //hero type id
  1741. nobj->subID = htid;
  1742. if(htid == 0xff)
  1743. hp->power = bufor[i++];
  1744. else
  1745. hp->power = 0;
  1746. break;
  1747. }
  1748. case 10: //Keymaster
  1749. {
  1750. nobj = new CGKeymasterTent();
  1751. break;
  1752. }
  1753. case 9: //Border Guard
  1754. {
  1755. nobj = new CGBorderGuard();
  1756. break;
  1757. }
  1758. case 212: //Border Gate
  1759. {
  1760. nobj = new CGBorderGate();
  1761. break;
  1762. }
  1763. case 27: case 37: //Eye and Hut of Magi
  1764. {
  1765. nobj = new CGMagi();
  1766. break;
  1767. }
  1768. case 16: case 24: case 25: case 84: case 85: //treasure bank
  1769. {
  1770. nobj = new CBank();
  1771. break;
  1772. }
  1773. case 63: //Pyramid
  1774. {
  1775. nobj = new CGPyramid();
  1776. break;
  1777. }
  1778. case 13: //Cartographer
  1779. {
  1780. nobj = new CCartographer();
  1781. break;
  1782. }
  1783. case 48: //Magic Spring
  1784. {
  1785. nobj = new CGMagicSpring();
  1786. break;
  1787. }
  1788. case 97: //den of thieves
  1789. {
  1790. nobj = new CGDenOfthieves();
  1791. break;
  1792. }
  1793. case 57: //Obelisk
  1794. {
  1795. nobj = new CGObelisk();
  1796. break;
  1797. }
  1798. case 42: //Lighthouse
  1799. {
  1800. nobj = new CGLighthouse();
  1801. nobj->tempOwner = readNormalNr(bufor,i); i+=4;
  1802. break;
  1803. }
  1804. default: //any other object
  1805. {
  1806. nobj = new CGObjectInstance();
  1807. break;
  1808. }
  1809. } //end of main switch
  1810. nobj->pos = pos;
  1811. nobj->ID = defInfo->id;
  1812. nobj->id = objects.size();
  1813. if(nobj->ID != HEROI_TYPE && nobj->ID != 214 && nobj->ID != 62)
  1814. nobj->subID = defInfo->subid;
  1815. nobj->defInfo = defInfo;
  1816. objects.push_back(nobj);
  1817. if(nobj->ID==TOWNI_TYPE)
  1818. towns.push_back(static_cast<CGTownInstance*>(nobj));
  1819. if(nobj->ID==HEROI_TYPE)
  1820. heroes.push_back(static_cast<CGHeroInstance*>(nobj));
  1821. }
  1822. std::sort(heroes.begin(), heroes.end(), boost::bind(&CGHeroInstance::subID, _1) < boost::bind(&CGHeroInstance::subID, _2));
  1823. }
  1824. void Mapa::readEvents( const unsigned char * bufor, int &i )
  1825. {
  1826. int numberOfEvents = readNormalNr(bufor,i); i+=4;
  1827. for(int yyoo=0; yyoo<numberOfEvents; ++yyoo)
  1828. {
  1829. CMapEvent *ne = new CMapEvent();
  1830. ne->name = std::string();
  1831. ne->message = std::string();
  1832. int nameLen = readNormalNr(bufor,i); i+=4;
  1833. for(int qq=0; qq<nameLen; ++qq)
  1834. {
  1835. ne->name += bufor[i]; ++i;
  1836. }
  1837. int messLen = readNormalNr(bufor,i); i+=4;
  1838. for(int qq=0; qq<messLen; ++qq)
  1839. {
  1840. ne->message +=bufor[i]; ++i;
  1841. }
  1842. ne->resources.resize(RESOURCE_QUANTITY);
  1843. for(int k=0; k < 7; k++)
  1844. {
  1845. ne->resources[k] = readNormalNr(bufor,i); i+=4;
  1846. }
  1847. ne->players = bufor[i]; ++i;
  1848. if(version>AB)
  1849. {
  1850. ne->humanAffected = bufor[i]; ++i;
  1851. }
  1852. else
  1853. ne->humanAffected = true;
  1854. ne->computerAffected = bufor[i]; ++i;
  1855. ne->firstOccurence = readNormalNr(bufor,i, 2); i+=2;
  1856. ne->nextOccurence = bufor[i]; ++i;
  1857. char unknown[17];
  1858. memcpy(unknown, bufor+i, 17);
  1859. i+=17;
  1860. events.push_back(ne);
  1861. }
  1862. }
  1863. bool Mapa::isInTheMap(const int3 &pos) const
  1864. {
  1865. if(pos.x<0 || pos.y<0 || pos.z<0 || pos.x >= width || pos.y >= height || pos.z > twoLevel)
  1866. return false;
  1867. else return true;
  1868. }
  1869. void Mapa::loadQuest(CQuest * guard, const unsigned char * bufor, int & i)
  1870. {
  1871. guard->missionType = bufor[i]; ++i;
  1872. //int len1, len2, len3;
  1873. switch(guard->missionType)
  1874. {
  1875. case 0:
  1876. return;
  1877. case 2:
  1878. {
  1879. guard->m2stats.resize(4);
  1880. for(int x=0; x<4; x++)
  1881. {
  1882. guard->m2stats[x] = bufor[i++];
  1883. }
  1884. }
  1885. break;
  1886. case 1:
  1887. case 3:
  1888. case 4:
  1889. {
  1890. guard->m13489val = readNormalNr(bufor,i); i+=4;
  1891. break;
  1892. }
  1893. case 5:
  1894. {
  1895. int artNumber = bufor[i]; ++i;
  1896. for(int yy=0; yy<artNumber; ++yy)
  1897. {
  1898. guard->m5arts.push_back(readNormalNr(bufor,i, 2)); i+=2;
  1899. }
  1900. break;
  1901. }
  1902. case 6:
  1903. {
  1904. int typeNumber = bufor[i]; ++i;
  1905. for(int hh=0; hh<typeNumber; ++hh)
  1906. {
  1907. ui32 creType = readNormalNr(bufor,i, 2); i+=2;
  1908. ui32 creNumb = readNormalNr(bufor,i, 2); i+=2;
  1909. guard->m6creatures[hh] = CStackInstance(creType,creNumb);
  1910. }
  1911. break;
  1912. }
  1913. case 7:
  1914. {
  1915. guard->m7resources.resize(7);
  1916. for(int x=0; x<7; x++)
  1917. {
  1918. guard->m7resources[x] = readNormalNr(bufor,i);
  1919. i+=4;
  1920. }
  1921. break;
  1922. }
  1923. case 8:
  1924. case 9:
  1925. {
  1926. guard->m13489val = bufor[i]; ++i;
  1927. break;
  1928. }
  1929. }
  1930. int limit = readNormalNr(bufor,i); i+=4;
  1931. if(limit == ((int)0xffffffff))
  1932. {
  1933. guard->lastDay = -1;
  1934. }
  1935. else
  1936. {
  1937. guard->lastDay = limit;
  1938. }
  1939. guard->firstVisitText = readString(bufor,i);
  1940. guard->nextVisitText = readString(bufor,i);
  1941. guard->completedText = readString(bufor,i);
  1942. if (guard->firstVisitText.size() && guard->nextVisitText.size() && guard->completedText.size())
  1943. guard->isCustom = false; //randomize all if any text is missing
  1944. else
  1945. guard->isCustom = true;
  1946. }
  1947. TerrainTile & Mapa::getTile( int3 tile )
  1948. {
  1949. return terrain[tile.x][tile.y][tile.z];
  1950. }
  1951. const TerrainTile & Mapa::getTile( int3 tile ) const
  1952. {
  1953. return terrain[tile.x][tile.y][tile.z];
  1954. }
  1955. bool Mapa::isWaterTile(const int3 &pos) const
  1956. {
  1957. return isInTheMap(pos) && getTile(pos).tertype == TerrainTile::water;
  1958. }
  1959. void Mapa::checkForObjectives()
  1960. {
  1961. if(isInTheMap(victoryCondition.pos))
  1962. {
  1963. const std::vector <CGObjectInstance*> &objs = getTile(victoryCondition.pos).visitableObjects;
  1964. assert(objs.size());
  1965. victoryCondition.obj = objs.front();
  1966. }
  1967. if(isInTheMap(lossCondition.pos))
  1968. {
  1969. const std::vector <CGObjectInstance*> &objs = getTile(lossCondition.pos).visitableObjects;
  1970. assert(objs.size());
  1971. lossCondition.obj = objs.front();
  1972. }
  1973. }
  1974. LossCondition::LossCondition()
  1975. {
  1976. obj = NULL;
  1977. timeLimit = -1;
  1978. pos = int3(-1,-1,-1);
  1979. }
  1980. CVictoryCondition::CVictoryCondition()
  1981. {
  1982. pos = int3(-1,-1,-1);
  1983. obj = NULL;
  1984. ID = allowNormalVictory = appliesToAI = count = 0;
  1985. }
  1986. bool TerrainTile::entrableTerrain(const TerrainTile *from /*= NULL*/) const
  1987. {
  1988. return entrableTerrain(from ? from->tertype != water : true, from ? from->tertype == water : true);
  1989. }
  1990. bool TerrainTile::entrableTerrain(bool allowLand, bool allowSea) const
  1991. {
  1992. return tertype != rock
  1993. && (allowSea && tertype == water || allowLand && tertype != water);
  1994. }
  1995. bool TerrainTile::isClear(const TerrainTile *from /*= NULL*/) const
  1996. {
  1997. return entrableTerrain(from) && !blocked;
  1998. }