2
0

map.cpp 52 KB

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