map.cpp 62 KB

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