map.cpp 51 KB

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