2
0

map.cpp 51 KB

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