map.cpp 52 KB

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