map.cpp 52 KB

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