map.cpp 51 KB

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