map.cpp 51 KB

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