map.cpp 52 KB

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