map.cpp 52 KB

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