map.cpp 51 KB

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