map.cpp 50 KB

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