map.cpp 51 KB

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