map.cpp 54 KB

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