map.cpp 51 KB

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