CAmbarCendamo.cpp 54 KB

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