map.cpp 50 KB

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