map.cpp 52 KB

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