map.cpp 52 KB

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