map.cpp 51 KB

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