map.cpp 52 KB

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