map.cpp 52 KB

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