map.cpp 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293
  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. std::set<int> convertBuildings(const std::set<int> h3m, int castleID)
  8. {
  9. std::map<int,int> mapa;
  10. std::set<int> ret;
  11. std::ifstream b5("config/buildings5.txt");
  12. while(!b5.eof())
  13. {
  14. int a, b;
  15. b5 >> a >> b;
  16. if(castleID==8 && b==17) //magic university ID 17 (h3m) => 21 (vcmi)
  17. b=21;
  18. mapa[a]=b;
  19. }
  20. for(std::set<int>::const_iterator i=h3m.begin();i!=h3m.end();i++)
  21. {
  22. if(mapa[*i]>=0)
  23. ret.insert(mapa[*i]);
  24. else if(mapa[*i] >= (-CREATURES_PER_TOWN)) // horde buildings
  25. {
  26. int level = (-mapa[*i]);
  27. if(h3m.find(20+(level*3)) != h3m.end()) //upgraded creature horde building
  28. {
  29. if(((castleID==1) || (castleID==3)) && ((level==3) || (level==5)))
  30. ret.insert(25);
  31. else
  32. ret.insert(19);
  33. }
  34. else
  35. {
  36. if(((castleID==1) || (castleID==3)) && ((level==3) || (level==5)))
  37. ret.insert(24);
  38. else
  39. ret.insert(18);
  40. }
  41. }
  42. else
  43. {
  44. std::cout<<"Conversion warning: unknown building "<<*i<<" in castle "<<castleID<<std::endl;
  45. }
  46. }
  47. ret.insert(10); //village hall is always present
  48. ret.insert(-1); //houses near v.hall / eyecandies
  49. ret.insert(-2); //terrain eyecandy, if -1 is used
  50. if(ret.find(11)!=ret.end())
  51. ret.insert(27);
  52. if(ret.find(12)!=ret.end())
  53. ret.insert(28);
  54. if(ret.find(13)!=ret.end())
  55. ret.insert(29);
  56. return ret;
  57. }
  58. unsigned int intPow(unsigned int a, unsigned int b)
  59. {
  60. unsigned int ret=1;
  61. for(int i=0; i<b; ++i)
  62. ret*=a;
  63. return ret;
  64. }
  65. unsigned char reverse(unsigned char arg)
  66. {
  67. unsigned char ret = 0;
  68. for (int i=0; i<8;i++)
  69. {
  70. if(((arg)&(1<<i))>>i)
  71. {
  72. ret |= (128>>i);
  73. }
  74. }
  75. return ret;
  76. }
  77. EDefType getDefType(CGDefInfo * a)
  78. {
  79. switch(a->id)
  80. {
  81. case 5: case 65: case 66: case 67: case 68: case 69:
  82. return EDefType::ARTIFACT_DEF; //handled
  83. case 6:
  84. return EDefType::PANDORA_DEF; //hanled
  85. case 26:
  86. return EDefType::EVENTOBJ_DEF; //handled
  87. case 33:
  88. return EDefType::GARRISON_DEF; //handled
  89. case 34: case 70: case 62: //70 - random hero //62 - prison
  90. return EDefType::HERO_DEF; //handled
  91. case 36:
  92. return EDefType::GRAIL_DEF; //hanled
  93. case 53: case 17: case 18: case 19: case 20: case 42: case 87: case 220://cases 17 - 20 and 42 - tests
  94. return EDefType::PLAYERONLY_DEF; //handled
  95. case 54: case 71: case 72: case 73: case 74: case 75: case 162: case 163: case 164:
  96. return EDefType::CREATURES_DEF; //handled
  97. case 59:
  98. return EDefType::SIGN_DEF; //handled
  99. case 77:
  100. return EDefType::TOWN_DEF; //can be problematic, but handled
  101. case 79: case 76:
  102. return EDefType::RESOURCE_DEF; //handled
  103. case 81:
  104. return EDefType::SCHOLAR_DEF; //handled
  105. case 83:
  106. return EDefType::SEERHUT_DEF; //handled
  107. case 91:
  108. return EDefType::SIGN_DEF; //handled
  109. case 88: case 89: case 90:
  110. return SHRINE_DEF; //handled
  111. case 93:
  112. return SPELLSCROLL_DEF; //handled
  113. case 98:
  114. return EDefType::TOWN_DEF; //handled
  115. case 113:
  116. return EDefType::WITCHHUT_DEF; //handled
  117. case 214:
  118. return EDefType::HEROPLACEHOLDER_DEF; //partially handled
  119. case 215:
  120. return EDefType::BORDERGUARD_DEF; //handled by analogy to seer huts ;]
  121. case 216:
  122. return EDefType::CREGEN2_DEF; //handled
  123. case 217:
  124. return EDefType::CREGEN_DEF; //handled
  125. case 218:
  126. return EDefType::CREGEN3_DEF; //handled
  127. case 219:
  128. return EDefType::GARRISON_DEF; //handled
  129. default:
  130. return EDefType::TERRAINOBJ_DEF; // nothing to be handled
  131. }
  132. }
  133. int readNormalNr (unsigned char * bufor, int pos, int bytCon = 4, bool cyclic = false)
  134. {
  135. int ret=0;
  136. int amp=1;
  137. for (int ir=0; ir<bytCon; ir++)
  138. {
  139. ret+=bufor[pos+ir]*amp;
  140. amp*=256;
  141. }
  142. if(cyclic && bytCon<4 && ret>=amp/2)
  143. {
  144. ret = ret-amp;
  145. }
  146. return ret;
  147. }
  148. char readChar(unsigned char * bufor, int &i)
  149. {
  150. return bufor[i++];
  151. }
  152. std::string readString(unsigned char * bufor, int &i)
  153. {
  154. int len = readNormalNr(bufor,i); i+=4;
  155. std::string ret; ret.reserve(len);
  156. for(int gg=0; gg<len; ++gg)
  157. {
  158. ret += bufor[i++];
  159. }
  160. return ret;
  161. }
  162. CCreatureSet readCreatureSet(unsigned char * bufor, int &i, int number, bool version) //version==true for >RoE maps
  163. {
  164. if(version)
  165. {
  166. CCreatureSet ret;
  167. std::pair<CCreature *, int> ins;
  168. for(int ir=0;ir<number;ir++)
  169. {
  170. int rettt = readNormalNr(bufor,i+ir*4, 2);
  171. if(rettt==0xffff) continue;
  172. if(rettt>32768)
  173. rettt = 65536-rettt+VLC->creh->creatures.size()-16;
  174. ins.first = &(VLC->creh->creatures[rettt]);
  175. ins.second = readNormalNr(bufor,i+ir*4+2, 2);
  176. std::pair<int,std::pair<CCreature *, int> > tt(ir,ins);
  177. ret.slots.insert(tt);
  178. }
  179. i+=number*4;
  180. return ret;
  181. }
  182. else
  183. {
  184. CCreatureSet ret;
  185. std::pair<CCreature *, int> ins;
  186. for(int ir=0;ir<number;ir++)
  187. {
  188. int rettt = readNormalNr(bufor,i+ir*3, 1);
  189. if(rettt==0xff) continue;
  190. if(rettt>220)
  191. rettt = 256-rettt+VLC->creh->creatures.size()-16;
  192. ins.first = &(VLC->creh->creatures[rettt]);
  193. ins.second = readNormalNr(bufor,i+ir*3+1, 2);
  194. std::pair<int,std::pair<CCreature *, int> > tt(ir,ins);
  195. ret.slots.insert(tt);
  196. }
  197. i+=number*3;
  198. return ret;
  199. }
  200. }
  201. CMapHeader::CMapHeader(unsigned char *map)
  202. {
  203. this->version = (Eformat)map[0]; //wersja mapy
  204. this->areAnyPLayers = map[4]; //seems to be invalid
  205. this->height = this->width = map[5]; // wymiary mapy
  206. this->twoLevel = map[9]; //czy sa lochy
  207. int length = map[10]; //name length
  208. int i=14, pom;
  209. while (i-14<length) //read name
  210. this->name+=map[i++];
  211. length = map[i] + map[i+1]*256; //description length
  212. i+=4;
  213. for (pom=0;pom<length;pom++)
  214. this->description+=map[i++];
  215. this->difficulty = map[i++]; // reading map difficulty
  216. if(version!=Eformat::RoE)
  217. {
  218. this->levelLimit = map[i++]; // hero level limit
  219. }
  220. else
  221. {
  222. levelLimit = 0;
  223. }
  224. for (pom=0;pom<8;pom++)
  225. {
  226. this->players[pom].canHumanPlay = map[i++];
  227. this->players[pom].canComputerPlay = map[i++];
  228. if ((!(this->players[pom].canHumanPlay || this->players[pom].canComputerPlay)))
  229. {
  230. switch(version)
  231. {
  232. case Eformat::SoD: case Eformat::WoG:
  233. i+=13;
  234. break;
  235. case Eformat::AB:
  236. i+=12;
  237. break;
  238. case Eformat::RoE:
  239. i+=6;
  240. break;
  241. }
  242. continue;
  243. }
  244. this->players[pom].AITactic = map[i++];
  245. if(version == Eformat::SoD || version == Eformat::WoG)
  246. i++;
  247. this->players[pom].allowedFactions = 0;
  248. this->players[pom].allowedFactions += map[i++];
  249. if(version != Eformat::RoE)
  250. this->players[pom].allowedFactions += (map[i++])*256;
  251. this->players[pom].isFactionRandom = map[i++];
  252. this->players[pom].hasMainTown = map[i++];
  253. if (this->players[pom].hasMainTown)
  254. {
  255. if(version != Eformat::RoE)
  256. {
  257. this->players[pom].generateHeroAtMainTown = map[i++];
  258. this->players[pom].generateHero = map[i++];
  259. }
  260. this->players[pom].posOfMainTown.x = map[i++];
  261. this->players[pom].posOfMainTown.y = map[i++];
  262. this->players[pom].posOfMainTown.z = map[i++];
  263. }
  264. players[pom].p8= map[i++];
  265. players[pom].p9= map[i++];
  266. if(players[pom].p9!=0xff)
  267. {
  268. players[pom].mainHeroPortrait = map[i++];
  269. int nameLength = map[i++];
  270. i+=3;
  271. for (int pp=0;pp<nameLength;pp++)
  272. players[pom].mainHeroName+=map[i++];
  273. }
  274. if(version!=Eformat::RoE)
  275. {
  276. i++; ////heroes placeholders //domostwa
  277. int heroCount = map[i++];
  278. i+=3;
  279. for (int pp=0;pp<heroCount;pp++)
  280. {
  281. SheroName vv;
  282. vv.heroID=map[i++];
  283. int hnl = map[i++];
  284. i+=3;
  285. for (int zz=0;zz<hnl;zz++)
  286. {
  287. vv.heroName+=map[i++];
  288. }
  289. this->players[pom].heroesNames.push_back(vv);
  290. }
  291. }
  292. }
  293. this->victoryCondition = (EvictoryConditions)map[i++];
  294. if (this->victoryCondition != winStandard) //specific victory conditions
  295. {
  296. int nr;
  297. switch (this->victoryCondition) //read victory conditions
  298. {
  299. case artifact:
  300. {
  301. this->vicConDetails = new VicCon0();
  302. ((VicCon0*)this->vicConDetails)->ArtifactID = map[i+2];
  303. nr=(version==RoE ? 1 : 2);
  304. break;
  305. }
  306. case gatherTroop:
  307. {
  308. this->vicConDetails = new VicCon1();
  309. int temp1 = map[i+2];
  310. int temp2 = map[i+3];
  311. ((VicCon1*)this->vicConDetails)->monsterID = map[i+2];
  312. ((VicCon1*)this->vicConDetails)->neededQuantity=readNormalNr(map, i+(version==RoE ? 3 : 4));
  313. nr=(version==RoE ? 5 : 6);
  314. break;
  315. }
  316. case gatherResource:
  317. {
  318. this->vicConDetails = new VicCon2();
  319. ((VicCon2*)this->vicConDetails)->resourceID = map[i+2];
  320. ((VicCon2*)this->vicConDetails)->neededQuantity=readNormalNr(map, i+3);
  321. nr=5;
  322. break;
  323. }
  324. case buildCity:
  325. {
  326. this->vicConDetails = new VicCon3();
  327. ((VicCon3*)this->vicConDetails)->posOfCity.x = map[i+2];
  328. ((VicCon3*)this->vicConDetails)->posOfCity.y = map[i+3];
  329. ((VicCon3*)this->vicConDetails)->posOfCity.z = map[i+4];
  330. ((VicCon3*)this->vicConDetails)->councilNeededLevel = map[i+5];
  331. ((VicCon3*)this->vicConDetails)->fortNeededLevel = map[i+6];
  332. nr=5;
  333. break;
  334. }
  335. case buildGrail:
  336. {
  337. this->vicConDetails = new VicCon4();
  338. if (map[i+4]>2)
  339. ((VicCon4*)this->vicConDetails)->anyLocation = true;
  340. else
  341. {
  342. ((VicCon4*)this->vicConDetails)->whereBuildGrail.x = map[i+2];
  343. ((VicCon4*)this->vicConDetails)->whereBuildGrail.y = map[i+3];
  344. ((VicCon4*)this->vicConDetails)->whereBuildGrail.z = map[i+4];
  345. }
  346. nr=3;
  347. break;
  348. }
  349. case beatHero:
  350. {
  351. this->vicConDetails = new VicCon5();
  352. ((VicCon5*)this->vicConDetails)->locationOfHero.x = map[i+2];
  353. ((VicCon5*)this->vicConDetails)->locationOfHero.y = map[i+3];
  354. ((VicCon5*)this->vicConDetails)->locationOfHero.z = map[i+4];
  355. nr=3;
  356. break;
  357. }
  358. case captureCity:
  359. {
  360. this->vicConDetails = new VicCon6();
  361. ((VicCon6*)this->vicConDetails)->locationOfTown.x = map[i+2];
  362. ((VicCon6*)this->vicConDetails)->locationOfTown.y = map[i+3];
  363. ((VicCon6*)this->vicConDetails)->locationOfTown.z = map[i+4];
  364. nr=3;
  365. break;
  366. }
  367. case beatMonster:
  368. {
  369. this->vicConDetails = new VicCon7();
  370. ((VicCon7*)this->vicConDetails)->locationOfMonster.x = map[i+2];
  371. ((VicCon7*)this->vicConDetails)->locationOfMonster.y = map[i+3];
  372. ((VicCon7*)this->vicConDetails)->locationOfMonster.z = map[i+4];
  373. nr=3;
  374. break;
  375. }
  376. case takeDwellings:
  377. {
  378. this->vicConDetails = new CspecificVictoryConidtions();
  379. nr=3;
  380. break;
  381. }
  382. case takeMines:
  383. {
  384. this->vicConDetails = new CspecificVictoryConidtions();
  385. nr=3;
  386. break;
  387. }
  388. case transportItem:
  389. {
  390. this->vicConDetails = new VicCona();
  391. ((VicCona*)this->vicConDetails)->artifactID = map[i+2];
  392. ((VicCona*)this->vicConDetails)->destinationPlace.x = map[i+3];
  393. ((VicCona*)this->vicConDetails)->destinationPlace.y = map[i+4];
  394. ((VicCona*)this->vicConDetails)->destinationPlace.z = map[i+5];
  395. nr=4;
  396. break;
  397. }
  398. }
  399. this->vicConDetails->allowNormalVictory = map[i++];
  400. this->vicConDetails->appliesToAI = map[i++];
  401. i+=nr;
  402. }
  403. this->lossCondition.typeOfLossCon = (ElossCon)map[i++];
  404. switch (this->lossCondition.typeOfLossCon) //read loss conditions
  405. {
  406. case lossCastle:
  407. {
  408. this->lossCondition.castlePos.x=map[i++];
  409. this->lossCondition.castlePos.y=map[i++];
  410. this->lossCondition.castlePos.z=map[i++];
  411. }
  412. case lossHero:
  413. {
  414. this->lossCondition.heroPos.x=map[i++];
  415. this->lossCondition.heroPos.y=map[i++];
  416. this->lossCondition.heroPos.z=map[i++];
  417. }
  418. case timeExpires:
  419. {
  420. this->lossCondition.timeLimit = readNormalNr(map, i++,2);
  421. i++;
  422. }
  423. }
  424. this->howManyTeams=map[i++]; //read number of teams
  425. if(this->howManyTeams>0) //read team numbers
  426. {
  427. for(int rr=0; rr<8; ++rr)
  428. {
  429. this->players[rr].team=map[i++];
  430. }
  431. }
  432. }
  433. Mapa::Mapa(unsigned char * bufor)
  434. {
  435. THC timeHandler th;
  436. th.getDif();
  437. int i=0;
  438. version = (Eformat)(readNormalNr(bufor,i)); i+=4; //map version
  439. areAnyPLayers = readChar(bufor,i); //invalid on some maps
  440. height = width = (readNormalNr(bufor,i)); i+=4; // wymiary mapy
  441. twoLevel = readChar(bufor,i); //czy sa lochy
  442. terrain = new TerrainTile*[width]; // allocate memory
  443. for (int ii=0;ii<width;ii++)
  444. terrain[ii] = new TerrainTile[height]; // allocate memory
  445. if (twoLevel)
  446. {
  447. undergroungTerrain = new TerrainTile*[width]; // allocate memory
  448. for (int ii=0;ii<width;ii++)
  449. undergroungTerrain[ii] = new TerrainTile[height]; // allocate memory
  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].tertype = (EterrainType)(bufor[i++]);
  890. terrain[z][c].terview = bufor[i++];
  891. terrain[z][c].nuine = (Eriver)bufor[i++];
  892. terrain[z][c].rivDir = bufor[i++];
  893. terrain[z][c].malle = (Eroad)bufor[i++];
  894. terrain[z][c].roadDir = bufor[i++];
  895. terrain[z][c].siodmyTajemniczyBajt = bufor[i++];
  896. }
  897. }
  898. if (twoLevel) // read underground terrain
  899. {
  900. for (int c=0; c<width; c++) // reading terrain
  901. {
  902. for (int z=0; z<height; z++)
  903. {
  904. undergroungTerrain[z][c].tertype = (EterrainType)(bufor[i++]);
  905. undergroungTerrain[z][c].terview = bufor[i++];
  906. undergroungTerrain[z][c].nuine = (Eriver)bufor[i++];
  907. undergroungTerrain[z][c].rivDir = bufor[i++];
  908. undergroungTerrain[z][c].malle = (Eroad)bufor[i++];
  909. undergroungTerrain[z][c].roadDir = bufor[i++];
  910. undergroungTerrain[z][c].siodmyTajemniczyBajt = bufor[i++];
  911. }
  912. }
  913. }
  914. THC std::cout<<"\tReading terrain: "<<th.getDif()<<std::endl;
  915. //////READING DEF INFO///////
  916. int defAmount = readNormalNr(bufor,i); i+=4;
  917. defy.reserve(defAmount);
  918. for (int idd = 0 ; idd<defAmount; idd++) // reading defs
  919. {
  920. CGDefInfo * vinya = new CGDefInfo(); // info about new def
  921. //reading name
  922. int nameLength = readNormalNr(bufor,i,4);i+=4;
  923. vinya->name.reserve(nameLength);
  924. for (int cd=0;cd<nameLength;cd++)
  925. {
  926. vinya->name += bufor[i++];
  927. }
  928. std::transform(vinya->name.begin(),vinya->name.end(),vinya->name.begin(),(int(*)(int))toupper);
  929. unsigned char bytes[12];
  930. for (int v=0; v<12; v++) // read info
  931. {
  932. bytes[v] = bufor[i++];
  933. }
  934. vinya->terrainAllowed = readNormalNr(bufor,i,2);i+=2;
  935. vinya->terrainMenu = readNormalNr(bufor,i,2);i+=2;
  936. vinya->id = readNormalNr(bufor,i,4);i+=4;
  937. vinya->subid = readNormalNr(bufor,i,4);i+=4;
  938. vinya->type = bufor[i++];
  939. vinya->printPriority = bufor[i++];
  940. for (int zi=0; zi<6; zi++)
  941. {
  942. vinya->blockMap[zi] = reverse(bytes[zi]);
  943. }
  944. for (int zi=0; zi<6; zi++)
  945. {
  946. vinya->visitMap[zi] = reverse(bytes[6+zi]);
  947. }
  948. i+=16;
  949. defy.push_back(vinya); // add this def to the vector
  950. }
  951. THC std::cout<<"\tReading defs info: "<<th.getDif()<<std::endl;
  952. ////loading objects
  953. int howManyObjs = readNormalNr(bufor,i, 4); i+=4;
  954. for(int ww=0; ww<howManyObjs; ++ww) //comment this line to turn loading objects off
  955. {
  956. //std::cout << "object nr "<<ww<<"\ti= "<<i<<std::endl;
  957. CGObjectInstance * nobj = new CGObjectInstance(); //we will read this object
  958. nobj->id = objects.size();
  959. nobj->pos.x = bufor[i++];
  960. nobj->pos.y = bufor[i++];
  961. nobj->pos.z = bufor[i++];
  962. int tempd = readNormalNr(bufor,i, 4); i+=4;
  963. nobj->defInfo = defy[tempd];
  964. nobj->ID = nobj->defInfo->id;
  965. nobj->subID = nobj->defInfo->subid;
  966. //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";
  967. i+=5;
  968. unsigned char buff [30];
  969. for(int ccc=0; ccc<30; ++ccc)
  970. {
  971. buff[ccc] = bufor[i+ccc];
  972. }
  973. int j = nobj->defInfo->id;
  974. int p = 99;
  975. switch(getDefType(nobj->defInfo))
  976. {
  977. case EDefType::EVENTOBJ_DEF: //for event - objects
  978. {
  979. CEventObjInfo * spec = new CEventObjInfo;
  980. bool guardMess;
  981. guardMess = bufor[i]; ++i;
  982. if(guardMess)
  983. {
  984. int messLong = readNormalNr(bufor,i, 4); i+=4;
  985. if(messLong>0)
  986. {
  987. spec->isMessage = true;
  988. for(int yy=0; yy<messLong; ++yy)
  989. {
  990. spec->message +=bufor[i+yy];
  991. }
  992. i+=messLong;
  993. }
  994. spec->areGuarders = bufor[i]; ++i;
  995. if(spec->areGuarders)
  996. {
  997. spec->guarders = readCreatureSet(bufor,i,7,(version>RoE));
  998. }
  999. i+=4;
  1000. }
  1001. else
  1002. {
  1003. spec->isMessage = false;
  1004. spec->areGuarders = false;
  1005. spec->message = std::string("");
  1006. }
  1007. spec->gainedExp = readNormalNr(bufor,i, 4); i+=4;
  1008. spec->manaDiff = readNormalNr(bufor,i, 4); i+=4;
  1009. spec->moraleDiff = readNormalNr(bufor,i, 1, true); ++i;
  1010. spec->luckDiff = readNormalNr(bufor,i, 1, true); ++i;
  1011. spec->wood = readNormalNr(bufor,i); i+=4;
  1012. spec->mercury = readNormalNr(bufor,i); i+=4;
  1013. spec->ore = readNormalNr(bufor,i); i+=4;
  1014. spec->sulfur = readNormalNr(bufor,i); i+=4;
  1015. spec->crystal = readNormalNr(bufor,i); i+=4;
  1016. spec->gems = readNormalNr(bufor,i); i+=4;
  1017. spec->gold = readNormalNr(bufor,i); i+=4;
  1018. spec->attack = readNormalNr(bufor,i, 1); ++i;
  1019. spec->defence = readNormalNr(bufor,i, 1); ++i;
  1020. spec->power = readNormalNr(bufor,i, 1); ++i;
  1021. spec->knowledge = readNormalNr(bufor,i, 1); ++i;
  1022. int gabn; //number of gained abilities
  1023. gabn = readNormalNr(bufor,i, 1); ++i;
  1024. for(int oo = 0; oo<gabn; ++oo)
  1025. {
  1026. spec->abilities.push_back(readNormalNr(bufor,i, 1)); ++i;
  1027. spec->abilityLevels.push_back(readNormalNr(bufor,i, 1)); ++i;
  1028. }
  1029. int gart = readNormalNr(bufor,i, 1); ++i; //number of gained artifacts
  1030. for(int oo = 0; oo<gart; ++oo)
  1031. {
  1032. spec->artifacts.push_back(readNormalNr(bufor,i, (version == RoE ? 1 : 2))); i+=(version == RoE ? 1 : 2);
  1033. }
  1034. int gspel = readNormalNr(bufor,i, 1); ++i; //number of gained spells
  1035. for(int oo = 0; oo<gspel; ++oo)
  1036. {
  1037. spec->spells.push_back(readNormalNr(bufor,i, 1)); ++i;
  1038. }
  1039. int gcre = readNormalNr(bufor,i, 1); ++i; //number of gained creatures
  1040. spec->creatures = readCreatureSet(bufor,i,gcre,(version>RoE));
  1041. if(version>RoE)
  1042. i+=gcre;
  1043. i+=8;
  1044. spec->availableFor = readNormalNr(bufor,i, 1); ++i;
  1045. spec->computerActivate = readNormalNr(bufor,i, 1); ++i;
  1046. spec->humanActivate = readNormalNr(bufor,i, 1); ++i;
  1047. i+=4;
  1048. nobj->info = spec;
  1049. break;
  1050. }
  1051. case EDefType::HERO_DEF:
  1052. {
  1053. CGHeroInstance * nhi = new CGHeroInstance;
  1054. (*(static_cast<CGObjectInstance*>(nhi))) = *nobj;
  1055. delete nobj;
  1056. nobj=nhi;
  1057. if(version>RoE)
  1058. {
  1059. nhi->identifier = readNormalNr(bufor,i, 4); i+=4;
  1060. }
  1061. nhi->setOwner(bufor[i]); ++i;
  1062. nhi->subID = readNormalNr(bufor,i, 1); ++i;
  1063. if(readChar(bufor,i))//true if hero has nonstandard name
  1064. nhi->name = readString(bufor,i);
  1065. if(version>AB)
  1066. {
  1067. if(readChar(bufor,i))//true if hore's experience is greater than 0
  1068. { nhi->exp = readNormalNr(bufor,i); i+=4; }
  1069. else
  1070. nhi->exp = -1;
  1071. }
  1072. else
  1073. { nhi->exp = readNormalNr(bufor,i); i+=4; }
  1074. bool portrait=bufor[i]; ++i;
  1075. if (portrait)
  1076. i++; //TODO read portrait nr, save, open
  1077. if(readChar(bufor,i))//true if hero has specified abilities
  1078. {
  1079. int howMany = readNormalNr(bufor,i); i+=4;
  1080. nhi->secSkills.resize(howMany);
  1081. for(int yy=0; yy<howMany; ++yy)
  1082. {
  1083. nhi->secSkills[yy].first = readNormalNr(bufor,i, 1); ++i;
  1084. nhi->secSkills[yy].second = readNormalNr(bufor,i, 1); ++i;
  1085. }
  1086. }
  1087. if(readChar(bufor,i))//true if hero has nonstandard garrison
  1088. nhi->army = readCreatureSet(bufor,i,7,(version>RoE));
  1089. nhi->army.formation =bufor[i]; ++i; //formation
  1090. bool artSet = bufor[i]; ++i; //true if artifact set is not default (hero has some artifacts)
  1091. int artmask = version == RoE ? 0xff : 0xffff;
  1092. int artidlen = version == RoE ? 1 : 2;
  1093. if(artSet)
  1094. {
  1095. for(int pom=0;pom<16;pom++)
  1096. {
  1097. int id = readNormalNr(bufor,i, artidlen); i+=artidlen;
  1098. if(id!=artmask)
  1099. nhi->artifWorn[pom] = id;
  1100. }
  1101. //misc5 art //17
  1102. if(version>=SoD)
  1103. {
  1104. int id = readNormalNr(bufor,i, artidlen); i+=artidlen;
  1105. if(id!=artmask)
  1106. nhi->artifWorn[16] = id;
  1107. }
  1108. //spellbook
  1109. int id = readNormalNr(bufor,i, artidlen); i+=artidlen;
  1110. if(id!=artmask)
  1111. nhi->artifWorn[17] = id;
  1112. //19 //???what is that? gap in file or what? - it's probably fifth slot..
  1113. if(version>RoE)
  1114. {
  1115. id = readNormalNr(bufor,i, artidlen); i+=artidlen;
  1116. if(id!=artmask)
  1117. nhi->artifWorn[18] = id;
  1118. }
  1119. else
  1120. i+=1;
  1121. //bag artifacts //20
  1122. int amount = readNormalNr(bufor,i, 2); i+=2; //number of artifacts in hero's bag
  1123. if(amount>0)
  1124. {
  1125. for(int ss=0; ss<amount; ++ss)
  1126. {
  1127. id = readNormalNr(bufor,i, artidlen); i+=artidlen;
  1128. if(id!=artmask)
  1129. nhi->artifacts.push_back(id);
  1130. }
  1131. }
  1132. } //artifacts
  1133. nhi->patrol.patrolRadious = readNormalNr(bufor,i, 1); ++i;
  1134. if(nhi->patrol.patrolRadious == 0xff)
  1135. nhi->patrol.patrolling = false;
  1136. else
  1137. nhi->patrol.patrolling = true;
  1138. if(version>RoE)
  1139. {
  1140. if(readChar(bufor,i))//true if hero has nonstandard (mapmaker defined) biography
  1141. nhi->biography = readString(bufor,i);
  1142. nhi->sex = !(bufor[i]); ++i;
  1143. }
  1144. //spells
  1145. if(version>AB)
  1146. {
  1147. bool areSpells = bufor[i]; ++i;
  1148. if(areSpells) //TODO: sprawdziæ //seems to be ok - tow
  1149. {
  1150. int ist = i;
  1151. for(i; i<ist+9; ++i)
  1152. {
  1153. unsigned char c = bufor[i];
  1154. for(int yy=0; yy<8; ++yy)
  1155. {
  1156. if((i-ist)*8+yy < SPELLS_QUANTITY)
  1157. {
  1158. if(c == (c|((unsigned char)intPow(2, yy))))
  1159. nhi->spells.insert((i-ist)*8+yy);
  1160. }
  1161. }
  1162. }
  1163. }
  1164. }
  1165. else if(version==AB) //we can read one spell
  1166. {
  1167. unsigned char buff = bufor[i]; ++i;
  1168. if(buff!=254)
  1169. {
  1170. nhi->spells.insert(buff);
  1171. }
  1172. }
  1173. //spells loaded
  1174. if(version>AB)
  1175. {
  1176. if(readChar(bufor,i))//customPrimSkills
  1177. {
  1178. nhi->primSkills.resize(4);
  1179. for(int xx=0;xx<4;xx++)
  1180. nhi->primSkills[xx] = bufor[i++];
  1181. }
  1182. }
  1183. i+=16;
  1184. nhi->moveDir = 4;
  1185. nhi->isStanding = true;
  1186. nhi->level = -1;
  1187. nhi->mana = -1;
  1188. nhi->movement = -1;
  1189. if(nhi->ID==34)
  1190. heroes.push_back(nhi);
  1191. //else
  1192. // CGI->objh->objInstances.push_back(nhi);
  1193. break;
  1194. }
  1195. case CREATURES_DEF:
  1196. {
  1197. CCreatureObjInfo * spec = new CCreatureObjInfo;
  1198. if(version>RoE)
  1199. {
  1200. spec->bytes[0] = bufor[i]; ++i;
  1201. spec->bytes[1] = bufor[i]; ++i;
  1202. spec->bytes[2] = bufor[i]; ++i;
  1203. spec->bytes[3] = bufor[i]; ++i;
  1204. }
  1205. spec->number = readNormalNr(bufor,i, 2); i+=2;
  1206. spec->character = bufor[i]; ++i;
  1207. bool isMesTre = bufor[i]; ++i; //true if there is message or treasury
  1208. if(isMesTre)
  1209. {
  1210. int messLength = readNormalNr(bufor,i); i+=4;
  1211. if(messLength>0)
  1212. {
  1213. for(int tt=0; tt<messLength; ++tt)
  1214. {
  1215. spec->message += bufor[i]; ++i;
  1216. }
  1217. }
  1218. spec->wood = readNormalNr(bufor,i); i+=4;
  1219. spec->mercury = readNormalNr(bufor,i); i+=4;
  1220. spec->ore = readNormalNr(bufor,i); i+=4;
  1221. spec->sulfur = readNormalNr(bufor,i); i+=4;
  1222. spec->crytal = readNormalNr(bufor,i); i+=4;
  1223. spec->gems = readNormalNr(bufor,i); i+=4;
  1224. spec->gold = readNormalNr(bufor,i); i+=4;
  1225. int artID = readNormalNr(bufor,i, (version == RoE ? 1 : 2)); i+=(version == RoE ? 1 : 2);
  1226. if(version==RoE)
  1227. {
  1228. if(artID!=0xff)
  1229. spec->gainedArtifact = artID;
  1230. else
  1231. spec->gainedArtifact = -1;
  1232. }
  1233. else
  1234. {
  1235. if(artID!=0xffff)
  1236. spec->gainedArtifact = artID;
  1237. else
  1238. spec->gainedArtifact = -1;
  1239. }
  1240. }
  1241. spec->neverFlees = bufor[i]; ++i;
  1242. spec->notGrowingTeam = bufor[i]; ++i;
  1243. i+=2;
  1244. nobj->info = spec;
  1245. break;
  1246. }
  1247. case EDefType::SIGN_DEF:
  1248. {
  1249. CSignObjInfo * spec = new CSignObjInfo;
  1250. int length = readNormalNr(bufor,i); i+=4;
  1251. for(int rr=0; rr<length; ++rr)
  1252. {
  1253. spec->message += bufor[i]; ++i;
  1254. }
  1255. i+=4;
  1256. nobj->info = spec;
  1257. break;
  1258. }
  1259. case EDefType::SEERHUT_DEF:
  1260. {
  1261. CSeerHutObjInfo * spec = new CSeerHutObjInfo;
  1262. if(version>RoE)
  1263. {
  1264. spec->missionType = bufor[i]; ++i;
  1265. switch(spec->missionType)
  1266. {
  1267. case 0:
  1268. i+=3;
  1269. continue;
  1270. case 1:
  1271. {
  1272. spec->m1level = readNormalNr(bufor,i); i+=4;
  1273. int limit = readNormalNr(bufor,i); i+=4;
  1274. if(limit == ((int)0xffffffff))
  1275. {
  1276. spec->isDayLimit = false;
  1277. spec->lastDay = -1;
  1278. }
  1279. else
  1280. {
  1281. spec->isDayLimit = true;
  1282. spec->lastDay = limit;
  1283. }
  1284. break;
  1285. }
  1286. case 2:
  1287. {
  1288. spec->m2attack = bufor[i]; ++i;
  1289. spec->m2defence = bufor[i]; ++i;
  1290. spec->m2power = bufor[i]; ++i;
  1291. spec->m2knowledge = bufor[i]; ++i;
  1292. int limit = readNormalNr(bufor,i); i+=4;
  1293. if(limit == ((int)0xffffffff))
  1294. {
  1295. spec->isDayLimit = false;
  1296. spec->lastDay = -1;
  1297. }
  1298. else
  1299. {
  1300. spec->isDayLimit = true;
  1301. spec->lastDay = limit;
  1302. }
  1303. break;
  1304. }
  1305. case 3:
  1306. {
  1307. spec->m3bytes[0] = bufor[i]; ++i;
  1308. spec->m3bytes[1] = bufor[i]; ++i;
  1309. spec->m3bytes[2] = bufor[i]; ++i;
  1310. spec->m3bytes[3] = bufor[i]; ++i;
  1311. int limit = readNormalNr(bufor,i); i+=4;
  1312. if(limit == ((int)0xffffffff))
  1313. {
  1314. spec->isDayLimit = false;
  1315. spec->lastDay = -1;
  1316. }
  1317. else
  1318. {
  1319. spec->isDayLimit = true;
  1320. spec->lastDay = limit;
  1321. }
  1322. break;
  1323. }
  1324. case 4:
  1325. {
  1326. spec->m4bytes[0] = bufor[i]; ++i;
  1327. spec->m4bytes[1] = bufor[i]; ++i;
  1328. spec->m4bytes[2] = bufor[i]; ++i;
  1329. spec->m4bytes[3] = bufor[i]; ++i;
  1330. int limit = readNormalNr(bufor,i); i+=4;
  1331. if(limit == ((int)0xffffffff))
  1332. {
  1333. spec->isDayLimit = false;
  1334. spec->lastDay = -1;
  1335. }
  1336. else
  1337. {
  1338. spec->isDayLimit = true;
  1339. spec->lastDay = limit;
  1340. }
  1341. break;
  1342. }
  1343. case 5:
  1344. {
  1345. int artNumber = bufor[i]; ++i;
  1346. for(int yy=0; yy<artNumber; ++yy)
  1347. {
  1348. int artid = readNormalNr(bufor,i, 2); i+=2;
  1349. spec->m5arts.push_back(artid);
  1350. }
  1351. int limit = readNormalNr(bufor,i); i+=4;
  1352. if(limit == ((int)0xffffffff))
  1353. {
  1354. spec->isDayLimit = false;
  1355. spec->lastDay = -1;
  1356. }
  1357. else
  1358. {
  1359. spec->isDayLimit = true;
  1360. spec->lastDay = limit;
  1361. }
  1362. break;
  1363. }
  1364. case 6:
  1365. {
  1366. int typeNumber = bufor[i]; ++i;
  1367. for(int hh=0; hh<typeNumber; ++hh)
  1368. {
  1369. int creType = readNormalNr(bufor,i, 2); i+=2;
  1370. int creNumb = readNormalNr(bufor,i, 2); i+=2;
  1371. spec->m6cre.push_back(&(VLC->creh->creatures[creType]));
  1372. spec->m6number.push_back(creNumb);
  1373. }
  1374. int limit = readNormalNr(bufor,i); i+=4;
  1375. if(limit == ((int)0xffffffff))
  1376. {
  1377. spec->isDayLimit = false;
  1378. spec->lastDay = -1;
  1379. }
  1380. else
  1381. {
  1382. spec->isDayLimit = true;
  1383. spec->lastDay = limit;
  1384. }
  1385. break;
  1386. }
  1387. case 7:
  1388. {
  1389. spec->m7wood = readNormalNr(bufor,i); i+=4;
  1390. spec->m7mercury = readNormalNr(bufor,i); i+=4;
  1391. spec->m7ore = readNormalNr(bufor,i); i+=4;
  1392. spec->m7sulfur = readNormalNr(bufor,i); i+=4;
  1393. spec->m7crystal = readNormalNr(bufor,i); i+=4;
  1394. spec->m7gems = readNormalNr(bufor,i); i+=4;
  1395. spec->m7gold = readNormalNr(bufor,i); i+=4;
  1396. int limit = readNormalNr(bufor,i); i+=4;
  1397. if(limit == ((int)0xffffffff))
  1398. {
  1399. spec->isDayLimit = false;
  1400. spec->lastDay = -1;
  1401. }
  1402. else
  1403. {
  1404. spec->isDayLimit = true;
  1405. spec->lastDay = limit;
  1406. }
  1407. break;
  1408. }
  1409. case 8:
  1410. {
  1411. int heroType = bufor[i]; ++i;
  1412. spec->m8hero = heroType;
  1413. int limit = readNormalNr(bufor,i); i+=4;
  1414. if(limit == ((int)0xffffffff))
  1415. {
  1416. spec->isDayLimit = false;
  1417. spec->lastDay = -1;
  1418. }
  1419. else
  1420. {
  1421. spec->isDayLimit = true;
  1422. spec->lastDay = limit;
  1423. }
  1424. break;
  1425. }
  1426. case 9:
  1427. {
  1428. spec->m9player = bufor[i]; ++i;
  1429. int limit = readNormalNr(bufor,i); i+=4;
  1430. if(limit == ((int)0xffffffff))
  1431. {
  1432. spec->isDayLimit = false;
  1433. spec->lastDay = -1;
  1434. }
  1435. else
  1436. {
  1437. spec->isDayLimit = true;
  1438. spec->lastDay = limit;
  1439. }
  1440. break;
  1441. }
  1442. }//internal switch end (seer huts)
  1443. int len1 = readNormalNr(bufor,i); i+=4;
  1444. for(int ee=0; ee<len1; ++ee)
  1445. {
  1446. spec->firstVisitText += bufor[i]; ++i;
  1447. }
  1448. int len2 = readNormalNr(bufor,i); i+=4;
  1449. for(int ee=0; ee<len2; ++ee)
  1450. {
  1451. spec->nextVisitText += bufor[i]; ++i;
  1452. }
  1453. int len3 = readNormalNr(bufor,i); i+=4;
  1454. for(int ee=0; ee<len3; ++ee)
  1455. {
  1456. spec->completedText += bufor[i]; ++i;
  1457. }
  1458. }
  1459. else //RoE
  1460. {
  1461. int artID = bufor[i]; ++i;
  1462. if(artID!=255) //not none quest
  1463. {
  1464. spec->m5arts.push_back(artID);
  1465. spec->missionType = 5;
  1466. }
  1467. else
  1468. {
  1469. spec->missionType = 255;
  1470. }
  1471. }
  1472. if(spec->missionType!=255)
  1473. {
  1474. unsigned char rewardType = bufor[i]; ++i;
  1475. spec->rewardType = rewardType;
  1476. switch(rewardType)
  1477. {
  1478. case 1:
  1479. {
  1480. spec->r1exp = readNormalNr(bufor,i); i+=4;
  1481. break;
  1482. }
  1483. case 2:
  1484. {
  1485. spec->r2mana = readNormalNr(bufor,i); i+=4;
  1486. break;
  1487. }
  1488. case 3:
  1489. {
  1490. spec->r3morale = bufor[i]; ++i;
  1491. break;
  1492. }
  1493. case 4:
  1494. {
  1495. spec->r4luck = bufor[i]; ++i;
  1496. break;
  1497. }
  1498. case 5:
  1499. {
  1500. spec->r5type = bufor[i]; ++i;
  1501. spec->r5amount = readNormalNr(bufor,i, 3); i+=3;
  1502. i+=1;
  1503. break;
  1504. }
  1505. case 6:
  1506. {
  1507. spec->r6type = bufor[i]; ++i;
  1508. spec->r6amount = bufor[i]; ++i;
  1509. break;
  1510. }
  1511. case 7:
  1512. {
  1513. spec->r7ability = bufor[i]; ++i;
  1514. spec->r7level = bufor[i]; ++i;
  1515. break;
  1516. }
  1517. case 8:
  1518. {
  1519. spec->r8art = readNormalNr(bufor,i, (version == RoE ? 1 : 2)); i+=(version == RoE ? 1 : 2);
  1520. break;
  1521. }
  1522. case 9:
  1523. {
  1524. spec->r9spell = bufor[i]; ++i;
  1525. break;
  1526. }
  1527. case 10:
  1528. {
  1529. if(version>RoE)
  1530. {
  1531. spec->r10creature = readNormalNr(bufor,i, 2); i+=2;
  1532. spec->r10amount = readNormalNr(bufor,i, 2); i+=2;
  1533. }
  1534. else
  1535. {
  1536. spec->r10creature = bufor[i]; ++i;
  1537. spec->r10amount = readNormalNr(bufor,i, 2); i+=2;
  1538. }
  1539. break;
  1540. }
  1541. }// end of internal switch
  1542. i+=2;
  1543. }
  1544. else //missionType==255
  1545. {
  1546. i+=3;
  1547. }
  1548. nobj->info = spec;
  1549. break;
  1550. }
  1551. case EDefType::WITCHHUT_DEF:
  1552. {
  1553. CWitchHutObjInfo * spec = new CWitchHutObjInfo;
  1554. if(version>RoE) //in reo we cannot specify it - all are allowed (I hope)
  1555. {
  1556. ist=i; //starting i for loop
  1557. for(i; i<ist+4; ++i)
  1558. {
  1559. unsigned char c = bufor[i];
  1560. for(int yy=0; yy<8; ++yy)
  1561. {
  1562. if((i-ist)*8+yy < SKILL_QUANTITY)
  1563. {
  1564. if(c == (c|((unsigned char)intPow(2, yy))))
  1565. spec->allowedAbilities.push_back((i-ist)*8+yy);
  1566. }
  1567. }
  1568. }
  1569. }
  1570. else //(RoE map)
  1571. {
  1572. for(int gg=0; gg<SKILL_QUANTITY; ++gg)
  1573. {
  1574. spec->allowedAbilities.push_back(gg);
  1575. }
  1576. }
  1577. nobj->info = spec;
  1578. break;
  1579. }
  1580. case EDefType::SCHOLAR_DEF:
  1581. {
  1582. CScholarObjInfo * spec = new CScholarObjInfo;
  1583. spec->bonusType = bufor[i]; ++i;
  1584. switch(spec->bonusType)
  1585. {
  1586. case 0xff:
  1587. ++i;
  1588. break;
  1589. case 0:
  1590. spec->r0type = bufor[i]; ++i;
  1591. break;
  1592. case 1:
  1593. spec->r1 = bufor[i]; ++i;
  1594. break;
  1595. case 2:
  1596. spec->r2 = bufor[i]; ++i;
  1597. break;
  1598. }
  1599. i+=6;
  1600. nobj->info = spec;
  1601. break;
  1602. }
  1603. case EDefType::GARRISON_DEF:
  1604. {
  1605. CGarrisonObjInfo * spec = new CGarrisonObjInfo;
  1606. spec->player = bufor[i]; ++i;
  1607. i+=3;
  1608. spec->units = readCreatureSet(bufor,i,7,(version>RoE));
  1609. if(version > RoE)
  1610. {
  1611. spec->movableUnits = bufor[i]; ++i;
  1612. }
  1613. else
  1614. spec->movableUnits = true;
  1615. i+=8;
  1616. nobj->setOwner(spec->player);
  1617. nobj->info = spec;
  1618. break;
  1619. }
  1620. case EDefType::ARTIFACT_DEF:
  1621. {
  1622. CArtifactObjInfo * spec = new CArtifactObjInfo;
  1623. bool areSettings = bufor[i]; ++i;
  1624. if(areSettings)
  1625. {
  1626. int messLength = readNormalNr(bufor,i, 4); i+=4;
  1627. for(int hh=0; hh<messLength; ++hh)
  1628. {
  1629. spec->message += bufor[i]; ++i;
  1630. }
  1631. bool areGuards = bufor[i]; ++i;
  1632. if(areGuards)
  1633. {
  1634. spec->areGuards = true;
  1635. spec->guards = readCreatureSet(bufor,i,7,(version>RoE));
  1636. }
  1637. else
  1638. spec->areGuards = false;
  1639. i+=4;
  1640. }
  1641. nobj->info = spec;
  1642. break;
  1643. }
  1644. case EDefType::RESOURCE_DEF:
  1645. {
  1646. CResourceObjInfo * spec = new CResourceObjInfo;
  1647. bool isMessGuard = bufor[i]; ++i;
  1648. if(isMessGuard)
  1649. {
  1650. int messLength = readNormalNr(bufor,i); i+=4;
  1651. for(int mm=0; mm<messLength; ++mm)
  1652. {
  1653. spec->message+=bufor[i]; ++i;
  1654. }
  1655. spec->areGuards = bufor[i]; ++i;
  1656. if(spec->areGuards)
  1657. {
  1658. spec->guards = readCreatureSet(bufor,i,7,(version>RoE));
  1659. }
  1660. i+=4;
  1661. }
  1662. else
  1663. {
  1664. spec->areGuards = false;
  1665. }
  1666. spec->amount = readNormalNr(bufor,i); i+=4;
  1667. i+=4;
  1668. nobj->info = spec;
  1669. break;
  1670. }
  1671. case EDefType::TOWN_DEF:
  1672. {
  1673. CGTownInstance * nt = new CGTownInstance();
  1674. (*(static_cast<CGObjectInstance*>(nt))) = *nobj;
  1675. delete nobj;
  1676. nobj = nt;
  1677. nt->identifier = 0;
  1678. if(version>RoE)
  1679. {
  1680. readNormalNr(bufor,i); i+=4;
  1681. }
  1682. nt->tempOwner = bufor[i]; ++i;
  1683. if(readChar(bufor,i)) //has name
  1684. nt->name = readString(bufor,i);
  1685. if(readChar(bufor,i))//true if garrison isn't empty
  1686. nt->army = readCreatureSet(bufor,i,7,(version>RoE));
  1687. nt->army.formation = bufor[i]; ++i;
  1688. if(readChar(bufor,i)) //unusualBuildings
  1689. {
  1690. //built buildings
  1691. for(int byte=0;byte<6;byte++)
  1692. {
  1693. for(int bit=0;bit<8;bit++)
  1694. if(bufor[i] & (1<<bit))
  1695. nt->builtBuildings.insert(byte*8+bit);
  1696. i++;
  1697. }
  1698. //forbidden buildings
  1699. for(int byte=6;byte<12;byte++)
  1700. {
  1701. for(int bit=0;bit<8;bit++)
  1702. if(bufor[i] & (1<<bit))
  1703. nt->forbiddenBuildings.insert(byte*8+bit);
  1704. i++;
  1705. }
  1706. nt->builtBuildings = convertBuildings(nt->builtBuildings,nt->subID);
  1707. nt->forbiddenBuildings = convertBuildings(nt->forbiddenBuildings,nt->subID);
  1708. }
  1709. else //standard buildings
  1710. {
  1711. if(readChar(bufor,i)) //has fort
  1712. nt->builtBuildings.insert(7);
  1713. nt->builtBuildings.insert(-50); //means that set of standard building should be included
  1714. }
  1715. int ist = i;
  1716. if(version>RoE)
  1717. {
  1718. for(i; i<ist+9; ++i)
  1719. {
  1720. unsigned char c = bufor[i];
  1721. for(int yy=0; yy<8; ++yy)
  1722. {
  1723. if((i-ist)*8+yy < SPELLS_QUANTITY)
  1724. {
  1725. if(c == (c|((unsigned char)intPow(2, yy))))
  1726. nt->obligatorySpells.push_back((i-ist)*8+yy);
  1727. }
  1728. }
  1729. }
  1730. }
  1731. ist = i;
  1732. for(i; i<ist+9; ++i)
  1733. {
  1734. unsigned char c = bufor[i];
  1735. for(int yy=0; yy<8; ++yy)
  1736. {
  1737. if((i-ist)*8+yy < SPELLS_QUANTITY)
  1738. {
  1739. if(c != (c|((unsigned char)intPow(2, yy))))
  1740. nt->possibleSpells.push_back((i-ist)*8+yy);
  1741. }
  1742. }
  1743. }
  1744. /////// reading castle events //////////////////////////////////
  1745. int numberOfEvent = readNormalNr(bufor,i); i+=4;
  1746. for(int gh = 0; gh<numberOfEvent; ++gh)
  1747. {
  1748. CCastleEvent nce;
  1749. int nameLen = readNormalNr(bufor,i); i+=4;
  1750. for(int ll=0; ll<nameLen; ++ll)
  1751. {
  1752. nce.name += bufor[i]; ++i;
  1753. }
  1754. int messLen = readNormalNr(bufor,i); i+=4;
  1755. for(int ll=0; ll<messLen; ++ll)
  1756. {
  1757. nce.message += bufor[i]; ++i;
  1758. }
  1759. nce.wood = readNormalNr(bufor,i); i+=4;
  1760. nce.mercury = readNormalNr(bufor,i); i+=4;
  1761. nce.ore = readNormalNr(bufor,i); i+=4;
  1762. nce.sulfur = readNormalNr(bufor,i); i+=4;
  1763. nce.crystal = readNormalNr(bufor,i); i+=4;
  1764. nce.gems = readNormalNr(bufor,i); i+=4;
  1765. nce.gold = readNormalNr(bufor,i); i+=4;
  1766. nce.players = bufor[i]; ++i;
  1767. if(version > AB)
  1768. {
  1769. nce.forHuman = bufor[i]; ++i;
  1770. }
  1771. else
  1772. nce.forHuman = true;
  1773. nce.forComputer = bufor[i]; ++i;
  1774. nce.firstShow = readNormalNr(bufor,i, 2); i+=2;
  1775. nce.forEvery = bufor[i]; ++i;
  1776. i+=17;
  1777. for(int kk=0; kk<6; ++kk)
  1778. {
  1779. nce.bytes[kk] = bufor[i]; ++i;
  1780. }
  1781. for(int vv=0; vv<7; ++vv)
  1782. {
  1783. nce.gen[vv] = readNormalNr(bufor,i, 2); i+=2;
  1784. }
  1785. i+=4;
  1786. nt->events.insert(nce);
  1787. }//castle events have been read
  1788. if(version > AB)
  1789. {
  1790. nt->alignment = bufor[i]; ++i;
  1791. }
  1792. else
  1793. nt->alignment = 0xff;
  1794. i+=3;
  1795. nt->builded = 0;
  1796. nt->destroyed = 0;
  1797. nt->garrisonHero = NULL;
  1798. towns.push_back(nt);
  1799. break;
  1800. }
  1801. case EDefType::PLAYERONLY_DEF:
  1802. {
  1803. CPlayerOnlyObjInfo * spec = new CPlayerOnlyObjInfo;
  1804. spec->player = bufor[i]; ++i;
  1805. i+=3;
  1806. nobj->setOwner(spec->player);
  1807. nobj->info = spec;
  1808. break;
  1809. }
  1810. case EDefType::SHRINE_DEF:
  1811. {
  1812. CShrineObjInfo * spec = new CShrineObjInfo;
  1813. spec->spell = bufor[i]; i+=4;
  1814. nobj->info = spec;
  1815. break;
  1816. }
  1817. case EDefType::SPELLSCROLL_DEF:
  1818. {
  1819. CSpellScrollObjinfo * spec = new CSpellScrollObjinfo;
  1820. bool messg = bufor[i]; ++i;
  1821. if(messg)
  1822. {
  1823. int mLength = readNormalNr(bufor,i); i+=4;
  1824. for(int vv=0; vv<mLength; ++vv)
  1825. {
  1826. spec->message += bufor[i]; ++i;
  1827. }
  1828. spec->areGuarders = bufor[i]; ++i;
  1829. if(spec->areGuarders)
  1830. {
  1831. spec->guarders = readCreatureSet(bufor,i,7,(version>RoE));
  1832. }
  1833. i+=4;
  1834. }
  1835. spec->spell = bufor[i]; ++i;
  1836. i+=3;
  1837. nobj->info = spec;
  1838. break;
  1839. }
  1840. case EDefType::PANDORA_DEF:
  1841. {
  1842. CPandorasBoxObjInfo * spec = new CPandorasBoxObjInfo;
  1843. bool messg = bufor[i]; ++i;
  1844. if(messg)
  1845. {
  1846. int mLength = readNormalNr(bufor,i); i+=4;
  1847. for(int vv=0; vv<mLength; ++vv)
  1848. {
  1849. spec->message += bufor[i]; ++i;
  1850. }
  1851. spec->areGuarders = bufor[i]; ++i;
  1852. if(spec->areGuarders)
  1853. {
  1854. spec->guarders = readCreatureSet(bufor,i,7,(version>RoE));
  1855. }
  1856. i+=4;
  1857. }
  1858. ////// copied form event handling (seems to be similar)
  1859. spec->gainedExp = readNormalNr(bufor,i, 4); i+=4;
  1860. spec->manaDiff = readNormalNr(bufor,i, 4); i+=4;
  1861. spec->moraleDiff = readNormalNr(bufor,i, 1, true); ++i;
  1862. spec->luckDiff = readNormalNr(bufor,i, 1, true); ++i;
  1863. spec->wood = readNormalNr(bufor,i); i+=4;
  1864. spec->mercury = readNormalNr(bufor,i); i+=4;
  1865. spec->ore = readNormalNr(bufor,i); i+=4;
  1866. spec->sulfur = readNormalNr(bufor,i); i+=4;
  1867. spec->crystal = readNormalNr(bufor,i); i+=4;
  1868. spec->gems = readNormalNr(bufor,i); i+=4;
  1869. spec->gold = readNormalNr(bufor,i); i+=4;
  1870. spec->attack = readNormalNr(bufor,i, 1); ++i;
  1871. spec->defence = readNormalNr(bufor,i, 1); ++i;
  1872. spec->power = readNormalNr(bufor,i, 1); ++i;
  1873. spec->knowledge = readNormalNr(bufor,i, 1); ++i;
  1874. int gabn; //number of gained abilities
  1875. gabn = readNormalNr(bufor,i, 1); ++i;
  1876. for(int oo = 0; oo<gabn; ++oo)
  1877. {
  1878. spec->abilities.push_back(readNormalNr(bufor,i, 1)); ++i;
  1879. spec->abilityLevels.push_back(readNormalNr(bufor,i, 1)); ++i;
  1880. }
  1881. int gart = readNormalNr(bufor,i, 1); ++i; //number of gained artifacts
  1882. for(int oo = 0; oo<gart; ++oo)
  1883. {
  1884. if(version > RoE)
  1885. {
  1886. spec->artifacts.push_back(readNormalNr(bufor,i, 2)); i+=2;
  1887. }
  1888. else
  1889. {
  1890. spec->artifacts.push_back(readNormalNr(bufor,i, 1)); i+=1;
  1891. }
  1892. }
  1893. int gspel = readNormalNr(bufor,i, 1); ++i; //number of gained spells
  1894. for(int oo = 0; oo<gspel; ++oo)
  1895. {
  1896. spec->spells.push_back(readNormalNr(bufor,i, 1)); ++i;
  1897. }
  1898. int gcre = readNormalNr(bufor,i, 1); ++i; //number of gained creatures
  1899. spec->creatures = readCreatureSet(bufor,i,gcre,(version>RoE));
  1900. if(version > RoE)
  1901. i+=gcre;
  1902. i+=8;
  1903. nobj->info = spec;
  1904. ///////end of copied fragment
  1905. break;
  1906. }
  1907. case EDefType::GRAIL_DEF:
  1908. {
  1909. CGrailObjInfo * spec = new CGrailObjInfo;
  1910. spec->radius = readNormalNr(bufor,i); i+=4;
  1911. nobj->info = spec;
  1912. break;
  1913. }
  1914. case EDefType::CREGEN_DEF:
  1915. {
  1916. CCreGenObjInfo * spec = new CCreGenObjInfo;
  1917. spec->player = readNormalNr(bufor,i); i+=4;
  1918. spec->identifier = readNormalNr(bufor,i); i+=4;
  1919. if(!spec->identifier)
  1920. {
  1921. spec->asCastle = false;
  1922. spec->castles[0] = bufor[i]; ++i;
  1923. spec->castles[1] = bufor[i]; ++i;
  1924. }
  1925. else
  1926. {
  1927. spec->asCastle = true;
  1928. }
  1929. nobj->setOwner(spec->player);
  1930. nobj->info = spec;
  1931. break;
  1932. }
  1933. case EDefType::CREGEN2_DEF:
  1934. {
  1935. CCreGen2ObjInfo * spec = new CCreGen2ObjInfo;
  1936. spec->player = readNormalNr(bufor,i); i+=4;
  1937. spec->identifier = readNormalNr(bufor,i); i+=4;
  1938. if(!spec->identifier)
  1939. {
  1940. spec->asCastle = false;
  1941. spec->castles[0] = bufor[i]; ++i;
  1942. spec->castles[1] = bufor[i]; ++i;
  1943. }
  1944. else
  1945. {
  1946. spec->asCastle = true;
  1947. }
  1948. spec->minLevel = bufor[i]; ++i;
  1949. spec->maxLevel = bufor[i]; ++i;
  1950. //if(spec->maxLevel>7)
  1951. // spec->maxLevel = 7;
  1952. //if(spec->minLevel<1)
  1953. // spec->minLevel = 1;
  1954. nobj->setOwner(spec->player);
  1955. nobj->info = spec;
  1956. break;
  1957. }
  1958. case EDefType::CREGEN3_DEF:
  1959. {
  1960. CCreGen3ObjInfo * spec = new CCreGen3ObjInfo;
  1961. spec->player = bufor[i]; ++i;
  1962. i+=3;
  1963. spec->minLevel = bufor[i]; ++i;
  1964. spec->maxLevel = bufor[i]; ++i;
  1965. if(spec->maxLevel>7)
  1966. spec->maxLevel = 7;
  1967. if(spec->minLevel<1)
  1968. spec->minLevel = 1;
  1969. nobj->setOwner(spec->player);
  1970. nobj->info = spec;
  1971. break;
  1972. }
  1973. case EDefType::BORDERGUARD_DEF:
  1974. {
  1975. CBorderGuardObjInfo * spec = new CBorderGuardObjInfo;
  1976. spec->missionType = bufor[i]; ++i;
  1977. switch(spec->missionType)
  1978. {
  1979. case 0:
  1980. {
  1981. goto borderguardend;
  1982. break;
  1983. }
  1984. case 1:
  1985. {
  1986. spec->m1level = readNormalNr(bufor,i); i+=4;
  1987. int limit = readNormalNr(bufor,i); i+=4;
  1988. if(limit == ((int)0xffffffff))
  1989. {
  1990. spec->isDayLimit = false;
  1991. spec->lastDay = -1;
  1992. }
  1993. else
  1994. {
  1995. spec->isDayLimit = true;
  1996. spec->lastDay = limit;
  1997. }
  1998. break;
  1999. }
  2000. case 2:
  2001. {
  2002. spec->m2attack = bufor[i]; ++i;
  2003. spec->m2defence = bufor[i]; ++i;
  2004. spec->m2power = bufor[i]; ++i;
  2005. spec->m2knowledge = bufor[i]; ++i;
  2006. int limit = readNormalNr(bufor,i); i+=4;
  2007. if(limit == ((int)0xffffffff))
  2008. {
  2009. spec->isDayLimit = false;
  2010. spec->lastDay = -1;
  2011. }
  2012. else
  2013. {
  2014. spec->isDayLimit = true;
  2015. spec->lastDay = limit;
  2016. }
  2017. break;
  2018. }
  2019. case 3:
  2020. {
  2021. spec->m3bytes[0] = bufor[i]; ++i;
  2022. spec->m3bytes[1] = bufor[i]; ++i;
  2023. spec->m3bytes[2] = bufor[i]; ++i;
  2024. spec->m3bytes[3] = bufor[i]; ++i;
  2025. int limit = readNormalNr(bufor,i); i+=4;
  2026. if(limit == ((int)0xffffffff))
  2027. {
  2028. spec->isDayLimit = false;
  2029. spec->lastDay = -1;
  2030. }
  2031. else
  2032. {
  2033. spec->isDayLimit = true;
  2034. spec->lastDay = limit;
  2035. }
  2036. break;
  2037. }
  2038. case 4:
  2039. {
  2040. spec->m4bytes[0] = bufor[i]; ++i;
  2041. spec->m4bytes[1] = bufor[i]; ++i;
  2042. spec->m4bytes[2] = bufor[i]; ++i;
  2043. spec->m4bytes[3] = bufor[i]; ++i;
  2044. int limit = readNormalNr(bufor,i); i+=4;
  2045. if(limit == ((int)0xffffffff))
  2046. {
  2047. spec->isDayLimit = false;
  2048. spec->lastDay = -1;
  2049. }
  2050. else
  2051. {
  2052. spec->isDayLimit = true;
  2053. spec->lastDay = limit;
  2054. }
  2055. break;
  2056. }
  2057. case 5:
  2058. {
  2059. int artNumber = bufor[i]; ++i;
  2060. for(int yy=0; yy<artNumber; ++yy)
  2061. {
  2062. spec->m5arts.push_back(readNormalNr(bufor,i, 2)); i+=2;
  2063. }
  2064. int limit = readNormalNr(bufor,i); i+=4;
  2065. if(limit == ((int)0xffffffff))
  2066. {
  2067. spec->isDayLimit = false;
  2068. spec->lastDay = -1;
  2069. }
  2070. else
  2071. {
  2072. spec->isDayLimit = true;
  2073. spec->lastDay = limit;
  2074. }
  2075. break;
  2076. }
  2077. case 6:
  2078. {
  2079. int typeNumber = bufor[i]; ++i;
  2080. for(int hh=0; hh<typeNumber; ++hh)
  2081. {
  2082. int creType = readNormalNr(bufor,i, 2); i+=2;
  2083. int creNumb = readNormalNr(bufor,i, 2); i+=2;
  2084. spec->m6cre.push_back(&(VLC->creh->creatures[creType]));
  2085. spec->m6number.push_back(creNumb);
  2086. }
  2087. int limit = readNormalNr(bufor,i); i+=4;
  2088. if(limit == ((int)0xffffffff))
  2089. {
  2090. spec->isDayLimit = false;
  2091. spec->lastDay = -1;
  2092. }
  2093. else
  2094. {
  2095. spec->isDayLimit = true;
  2096. spec->lastDay = limit;
  2097. }
  2098. break;
  2099. }
  2100. case 7:
  2101. {
  2102. spec->m7wood = readNormalNr(bufor,i); i+=4;
  2103. spec->m7mercury = readNormalNr(bufor,i); i+=4;
  2104. spec->m7ore = readNormalNr(bufor,i); i+=4;
  2105. spec->m7sulfur = readNormalNr(bufor,i); i+=4;
  2106. spec->m7crystal = readNormalNr(bufor,i); i+=4;
  2107. spec->m7gems = readNormalNr(bufor,i); i+=4;
  2108. spec->m7gold = readNormalNr(bufor,i); i+=4;
  2109. int limit = readNormalNr(bufor,i); i+=4;
  2110. if(limit == ((int)0xffffffff))
  2111. {
  2112. spec->isDayLimit = false;
  2113. spec->lastDay = -1;
  2114. }
  2115. else
  2116. {
  2117. spec->isDayLimit = true;
  2118. spec->lastDay = limit;
  2119. }
  2120. break;
  2121. }
  2122. case 8:
  2123. {
  2124. int heroType = bufor[i]; ++i;
  2125. spec->m8hero = heroType;
  2126. int limit = readNormalNr(bufor,i); i+=4;
  2127. if(limit == ((int)0xffffffff))
  2128. {
  2129. spec->isDayLimit = false;
  2130. spec->lastDay = -1;
  2131. }
  2132. else
  2133. {
  2134. spec->isDayLimit = true;
  2135. spec->lastDay = limit;
  2136. }
  2137. break;
  2138. }
  2139. case 9:
  2140. {
  2141. spec->m9player = bufor[i]; ++i;
  2142. int limit = readNormalNr(bufor,i); i+=4;
  2143. if(limit == ((int)0xffffffff))
  2144. {
  2145. spec->isDayLimit = false;
  2146. spec->lastDay = -1;
  2147. }
  2148. else
  2149. {
  2150. spec->isDayLimit = true;
  2151. spec->lastDay = limit;
  2152. }
  2153. break;
  2154. }
  2155. }//internal switch end (seer huts)
  2156. int len1 = readNormalNr(bufor,i); i+=4;
  2157. for(int ee=0; ee<len1; ++ee)
  2158. {
  2159. spec->firstVisitText += bufor[i]; ++i;
  2160. }
  2161. int len2 = readNormalNr(bufor,i); i+=4;
  2162. for(int ee=0; ee<len2; ++ee)
  2163. {
  2164. spec->nextVisitText += bufor[i]; ++i;
  2165. }
  2166. int len3 = readNormalNr(bufor,i); i+=4;
  2167. for(int ee=0; ee<len3; ++ee)
  2168. {
  2169. spec->completedText += bufor[i]; ++i;
  2170. }
  2171. nobj->info = spec;
  2172. borderguardend:
  2173. break;
  2174. }
  2175. case EDefType::HEROPLACEHOLDER_DEF:
  2176. {
  2177. i+=3; //TODO: handle it more properly
  2178. break;
  2179. }
  2180. } //end of main switch
  2181. objects.push_back(nobj);
  2182. }//end of loading objects
  2183. THC std::cout<<"\tReading objects: "<<th.getDif()<<std::endl;
  2184. //loading events
  2185. int numberOfEvents = readNormalNr(bufor,i); i+=4;
  2186. for(int yyoo=0; yyoo<numberOfEvents; ++yyoo)
  2187. {
  2188. CMapEvent ne;
  2189. ne.name = std::string();
  2190. ne.message = std::string();
  2191. int nameLen = readNormalNr(bufor,i); i+=4;
  2192. for(int qq=0; qq<nameLen; ++qq)
  2193. {
  2194. ne.name += bufor[i]; ++i;
  2195. }
  2196. int messLen = readNormalNr(bufor,i); i+=4;
  2197. for(int qq=0; qq<messLen; ++qq)
  2198. {
  2199. ne.message +=bufor[i]; ++i;
  2200. }
  2201. ne.wood = readNormalNr(bufor,i); i+=4;
  2202. ne.mercury = readNormalNr(bufor,i); i+=4;
  2203. ne.ore = readNormalNr(bufor,i); i+=4;
  2204. ne.sulfur = readNormalNr(bufor,i); i+=4;
  2205. ne.crystal = readNormalNr(bufor,i); i+=4;
  2206. ne.gems = readNormalNr(bufor,i); i+=4;
  2207. ne.gold = readNormalNr(bufor,i); i+=4;
  2208. ne.players = bufor[i]; ++i;
  2209. if(version>AB)
  2210. {
  2211. ne.humanAffected = bufor[i]; ++i;
  2212. }
  2213. else
  2214. ne.humanAffected = true;
  2215. ne.computerAffected = bufor[i]; ++i;
  2216. ne.firstOccurence = bufor[i]; ++i;
  2217. ne.nextOccurence = bufor[i]; ++i;
  2218. i+=18;
  2219. events.push_back(ne);
  2220. }
  2221. //map readed, bufor no longer needed
  2222. delete[] bufor; bufor=NULL;
  2223. }