map.cpp 52 KB

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