CAmbarCendamo.cpp 85 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877
  1. #include "stdafx.h"
  2. #include "CAmbarCendamo.h"
  3. #include "CSemiDefHandler.h"
  4. #include "CGameInfo.h"
  5. #include "CObjectHandler.h"
  6. #include "CCastleHandler.h"
  7. #include "SDL_Extensions.h"
  8. #include "boost\filesystem.hpp"
  9. #include <set>
  10. #include <iomanip>
  11. #include <sstream>
  12. unsigned int intPow(unsigned int a, unsigned int b)
  13. {
  14. unsigned int ret=1;
  15. for(int i=0; i<b; ++i)
  16. ret*=a;
  17. return ret;
  18. }
  19. CAmbarCendamo::CAmbarCendamo (unsigned char * map)
  20. {
  21. bufor=map;
  22. }
  23. CAmbarCendamo::CAmbarCendamo (const char * tie)
  24. {
  25. is = new std::ifstream();
  26. is -> open(tie,std::ios::binary);
  27. is->seekg(0,std::ios::end); // na koniec
  28. andame = is->tellg(); // read length
  29. is->seekg(0,std::ios::beg); // wracamy na poczatek
  30. bufor = new unsigned char[andame]; // allocate memory
  31. is->read((char*)bufor, andame); // read map file to buffer
  32. is->close();
  33. delete is;
  34. }
  35. CAmbarCendamo::~CAmbarCendamo ()
  36. {// free memory
  37. for (int ii=0;ii<map.width;ii++)
  38. delete map.terrain[ii] ;
  39. delete map.terrain;
  40. delete bufor;
  41. }
  42. void CAmbarCendamo::teceDef()
  43. {
  44. for (int i=0; i<map.defy.size(); i++)
  45. {
  46. std::ofstream * of = new std::ofstream(map.defy[i].name.c_str());
  47. for (int j=0;j<46;j++)
  48. {
  49. (*of) << map.defy[i].bytes[j]<<std::endl;
  50. }
  51. of->close();
  52. delete of;
  53. }
  54. }
  55. void CAmbarCendamo::deh3m()
  56. {
  57. THC timeHandler th;
  58. map.version = (Eformat)bufor[0]; //wersja mapy
  59. map.areAnyPLayers = bufor[4]; //invalid on some maps
  60. map.height = map.width = bufor[5]; // wymiary mapy
  61. map.twoLevel = bufor[9]; //czy sa lochy
  62. map.terrain = new TerrainTile*[map.width]; // allocate memory
  63. for (int ii=0;ii<map.width;ii++)
  64. map.terrain[ii] = new TerrainTile[map.height]; // allocate memory
  65. if (map.twoLevel)
  66. {
  67. map.undergroungTerrain = new TerrainTile*[map.width]; // allocate memory
  68. for (int ii=0;ii<map.width;ii++)
  69. map.undergroungTerrain[ii] = new TerrainTile[map.height]; // allocate memory
  70. }
  71. int length = bufor[10]; //name length
  72. int i=14, pom;
  73. while (i-14<length) //read name
  74. map.name+=bufor[i++];
  75. length = bufor[i] + bufor[i+1]*256; //description length
  76. i+=4;
  77. for (pom=0;pom<length;pom++)
  78. map.description+=bufor[i++];
  79. map.difficulty = bufor[i++]; // reading map difficulty
  80. if(map.version != Eformat::RoE)
  81. {
  82. map.levelLimit = bufor[i++]; // hero level limit
  83. }
  84. else
  85. {
  86. map.levelLimit = 0;
  87. }
  88. for (pom=0;pom<8;pom++)
  89. {
  90. map.players[pom].canHumanPlay = bufor[i++];
  91. map.players[pom].canComputerPlay = bufor[i++];
  92. if ((!(map.players[pom].canHumanPlay || map.players[pom].canComputerPlay)))
  93. {
  94. switch(map.version)
  95. {
  96. case Eformat::SoD: case Eformat::WoG:
  97. i+=13;
  98. break;
  99. case Eformat::AB:
  100. i+=12;
  101. break;
  102. case Eformat::RoE:
  103. i+=6;
  104. break;
  105. }
  106. continue;
  107. }
  108. map.players[pom].AITactic = bufor[i++];
  109. if(map.version == Eformat::SoD || map.version == Eformat::WoG)
  110. i++;
  111. map.players[pom].allowedFactions = 0;
  112. map.players[pom].allowedFactions += bufor[i++];
  113. if(map.version != Eformat::RoE)
  114. map.players[pom].allowedFactions += (bufor[i++])*256;
  115. map.players[pom].isFactionRandom = bufor[i++];
  116. map.players[pom].hasMainTown = bufor[i++];
  117. if (map.players[pom].hasMainTown)
  118. {
  119. if(map.version != Eformat::RoE)
  120. {
  121. map.players[pom].generateHeroAtMainTown = bufor[i++];
  122. map.players[pom].generateHero = bufor[i++];
  123. }
  124. map.players[pom].posOfMainTown.x = bufor[i++];
  125. map.players[pom].posOfMainTown.y = bufor[i++];
  126. map.players[pom].posOfMainTown.z = bufor[i++];
  127. }
  128. i++; //unknown
  129. if(bufor[i++]!=0xff)
  130. {
  131. map.players[pom].mainHeroPortrait = bufor[i++];
  132. int nameLength = bufor[i++];
  133. i+=3;
  134. for (int pp=0;pp<nameLength;pp++)
  135. map.players[pom].mainHeroName+=bufor[i++];
  136. }
  137. //i++; //unknown byte
  138. if(map.version != Eformat::RoE)
  139. {
  140. i++; ////unknown byte
  141. int heroCount = bufor[i++];
  142. i+=3;
  143. for (int pp=0;pp<heroCount;pp++)
  144. {
  145. SheroName vv;
  146. vv.heroID=bufor[i++];
  147. int hnl = bufor[i++];
  148. i+=3;
  149. for (int zz=0;zz<hnl;zz++)
  150. {
  151. vv.heroName+=bufor[i++];
  152. }
  153. map.players[pom].heroesNames.push_back(vv);
  154. }
  155. }
  156. }
  157. map.victoryCondition = (EvictoryConditions)bufor[i++];
  158. if (map.victoryCondition != winStandard) //specific victory conditions
  159. {
  160. int nr;
  161. switch (map.victoryCondition) //read victory conditions
  162. {
  163. case artifact:
  164. {
  165. map.vicConDetails = new VicCon0();
  166. ((VicCon0*)map.vicConDetails)->ArtifactID = bufor[i+2];
  167. nr=2;
  168. break;
  169. }
  170. case gatherTroop:
  171. {
  172. map.vicConDetails = new VicCon1();
  173. int temp1 = bufor[i+2];
  174. int temp2 = bufor[i+3];
  175. ((VicCon1*)map.vicConDetails)->monsterID = bufor[i+2];
  176. ((VicCon1*)map.vicConDetails)->neededQuantity=readNormalNr(i+4);
  177. nr=6;
  178. break;
  179. }
  180. case gatherResource:
  181. {
  182. map.vicConDetails = new VicCon2();
  183. ((VicCon2*)map.vicConDetails)->resourceID = bufor[i+2];
  184. ((VicCon2*)map.vicConDetails)->neededQuantity=readNormalNr(i+3);
  185. nr=5;
  186. break;
  187. }
  188. case buildCity:
  189. {
  190. map.vicConDetails = new VicCon3();
  191. ((VicCon3*)map.vicConDetails)->posOfCity.x = bufor[i+2];
  192. ((VicCon3*)map.vicConDetails)->posOfCity.y = bufor[i+3];
  193. ((VicCon3*)map.vicConDetails)->posOfCity.z = bufor[i+4];
  194. ((VicCon3*)map.vicConDetails)->councilNeededLevel = bufor[i+5];
  195. ((VicCon3*)map.vicConDetails)->fortNeededLevel = bufor[i+6];
  196. nr=5;
  197. break;
  198. }
  199. case buildGrail:
  200. {
  201. map.vicConDetails = new VicCon4();
  202. if (bufor[i+4]>2)
  203. ((VicCon4*)map.vicConDetails)->anyLocation = true;
  204. else
  205. {
  206. ((VicCon4*)map.vicConDetails)->whereBuildGrail.x = bufor[i+2];
  207. ((VicCon4*)map.vicConDetails)->whereBuildGrail.y = bufor[i+3];
  208. ((VicCon4*)map.vicConDetails)->whereBuildGrail.z = bufor[i+4];
  209. }
  210. nr=3;
  211. break;
  212. }
  213. case beatHero:
  214. {
  215. map.vicConDetails = new VicCon5();
  216. ((VicCon5*)map.vicConDetails)->locationOfHero.x = bufor[i+2];
  217. ((VicCon5*)map.vicConDetails)->locationOfHero.y = bufor[i+3];
  218. ((VicCon5*)map.vicConDetails)->locationOfHero.z = bufor[i+4];
  219. nr=3;
  220. break;
  221. }
  222. case captureCity:
  223. {
  224. map.vicConDetails = new VicCon6();
  225. ((VicCon6*)map.vicConDetails)->locationOfTown.x = bufor[i+2];
  226. ((VicCon6*)map.vicConDetails)->locationOfTown.y = bufor[i+3];
  227. ((VicCon6*)map.vicConDetails)->locationOfTown.z = bufor[i+4];
  228. nr=3;
  229. break;
  230. }
  231. case beatMonster:
  232. {
  233. map.vicConDetails = new VicCon7();
  234. ((VicCon7*)map.vicConDetails)->locationOfMonster.x = bufor[i+2];
  235. ((VicCon7*)map.vicConDetails)->locationOfMonster.y = bufor[i+3];
  236. ((VicCon7*)map.vicConDetails)->locationOfMonster.z = bufor[i+4];
  237. nr=3;
  238. break;
  239. }
  240. case takeDwellings:
  241. {
  242. map.vicConDetails = new CspecificVictoryConidtions();
  243. nr=0;
  244. break;
  245. }
  246. case takeMines:
  247. {
  248. map.vicConDetails = new CspecificVictoryConidtions();
  249. nr=0;
  250. break;
  251. }
  252. case transportItem:
  253. {
  254. map.vicConDetails = new VicCona();
  255. ((VicCona*)map.vicConDetails)->artifactID = bufor[i+2];
  256. ((VicCona*)map.vicConDetails)->destinationPlace.x = bufor[i+3];
  257. ((VicCona*)map.vicConDetails)->destinationPlace.y = bufor[i+4];
  258. ((VicCona*)map.vicConDetails)->destinationPlace.z = bufor[i+5];
  259. nr=3;
  260. break;
  261. }
  262. }
  263. map.vicConDetails->allowNormalVictory = bufor[i++];
  264. map.vicConDetails->appliesToAI = bufor[i++];
  265. i+=nr;
  266. }
  267. map.lossCondition.typeOfLossCon = (ElossCon)bufor[i++];
  268. switch (map.lossCondition.typeOfLossCon) //read loss conditions
  269. {
  270. case lossCastle:
  271. {
  272. map.lossCondition.castlePos.x=bufor[i++];
  273. map.lossCondition.castlePos.y=bufor[i++];
  274. map.lossCondition.castlePos.z=bufor[i++];
  275. break;
  276. }
  277. case lossHero:
  278. {
  279. map.lossCondition.heroPos.x=bufor[i++];
  280. map.lossCondition.heroPos.y=bufor[i++];
  281. map.lossCondition.heroPos.z=bufor[i++];
  282. break;
  283. }
  284. case timeExpires:
  285. {
  286. map.lossCondition.timeLimit = readNormalNr(i++,2);
  287. i++;
  288. break;
  289. }
  290. }
  291. map.howManyTeams=bufor[i++]; //read number of teams
  292. if(map.howManyTeams>0) //read team numbers
  293. {
  294. for(int rr=0; rr<8; ++rr)
  295. {
  296. map.players[rr].team=bufor[i++];
  297. }
  298. }
  299. //reading allowed heroes (20 bytes)
  300. int ist;
  301. ist=i; //starting i for loop
  302. for(i; i<ist+ (map.version == Eformat::RoE ? 16 : 20) ; ++i)
  303. {
  304. unsigned char c = bufor[i];
  305. for(int yy=0; yy<8; ++yy)
  306. {
  307. if((i-ist)*8+yy < CGameInfo::mainObj->heroh->heroes.size())
  308. {
  309. if(c == (c|((unsigned char)intPow(2, yy))))
  310. CGameInfo::mainObj->heroh->heroes[(i-ist)*8+yy]->isAllowed = true;
  311. else
  312. CGameInfo::mainObj->heroh->heroes[(i-ist)*8+yy]->isAllowed = false;
  313. }
  314. }
  315. }
  316. //allowed heroes have been read
  317. switch(map.version)
  318. {
  319. case SoD: case WoG:
  320. i+=36;
  321. break;
  322. case AB:
  323. i+=35;
  324. break;
  325. case RoE:
  326. i+=31;
  327. break;
  328. }
  329. //reading allowed artifacts //18 bytes
  330. if(map.version!=RoE)
  331. {
  332. ist=i; //starting i for loop
  333. for(i; i<ist+18; ++i)
  334. {
  335. unsigned char c = bufor[i];
  336. for(int yy=0; yy<8; ++yy)
  337. {
  338. if((i-ist)*8+yy < CGameInfo::mainObj->arth->artifacts.size())
  339. {
  340. if(c != (c|((unsigned char)intPow(2, yy))))
  341. CGameInfo::mainObj->arth->artifacts[(i-ist)*8+yy].isAllowed = true;
  342. else
  343. CGameInfo::mainObj->arth->artifacts[(i-ist)*8+yy].isAllowed = false;
  344. }
  345. }
  346. }//allowed artifacts have been read
  347. }
  348. //reading allowed spells (9 bytes)
  349. if(map.version>=SoD)
  350. {
  351. ist=i; //starting i for loop
  352. for(i; i<ist+9; ++i)
  353. {
  354. unsigned char c = bufor[i];
  355. for(int yy=0; yy<8; ++yy)
  356. {
  357. if((i-ist)*8+yy < CGameInfo::mainObj->spellh->spells.size())
  358. {
  359. if(c != (c|((unsigned char)intPow(2, yy))))
  360. CGameInfo::mainObj->spellh->spells[(i-ist)*8+yy].isAllowed = true;
  361. else
  362. CGameInfo::mainObj->spellh->spells[(i-ist)*8+yy].isAllowed = false;
  363. }
  364. }
  365. }
  366. //allowed spells have been read
  367. //allowed hero's abilities (4 bytes)
  368. ist=i; //starting i for loop
  369. for(i; i<ist+4; ++i)
  370. {
  371. unsigned char c = bufor[i];
  372. for(int yy=0; yy<8; ++yy)
  373. {
  374. if((i-ist)*8+yy < CGameInfo::mainObj->abilh->abilities.size())
  375. {
  376. if(c != (c|((unsigned char)intPow(2, yy))))
  377. CGameInfo::mainObj->abilh->abilities[(i-ist)*8+yy]->isAllowed = true;
  378. else
  379. CGameInfo::mainObj->abilh->abilities[(i-ist)*8+yy]->isAllowed = false;
  380. }
  381. }
  382. }
  383. }
  384. //allowed hero's abilities have been read
  385. THC std::cout<<"Reading header: "<<th.getDif()<<std::endl;
  386. int rumNr = readNormalNr(i,4);i+=4;
  387. for (int it=0;it<rumNr;it++)
  388. {
  389. Rumor ourRumor;
  390. int nameL = readNormalNr(i,4);i+=4; //read length of name of rumor
  391. for (int zz=0; zz<nameL; zz++)
  392. ourRumor.name+=bufor[i++];
  393. nameL = readNormalNr(i,4);i+=4; //read length of rumor
  394. for (int zz=0; zz<nameL; zz++)
  395. ourRumor.text+=bufor[i++];
  396. map.rumors.push_back(ourRumor); //add to our list
  397. }
  398. THC std::cout<<"Reading rumors: "<<th.getDif()<<std::endl;
  399. switch(map.version)
  400. {
  401. case WoG: case SoD: case AB:
  402. i+=156;
  403. break;
  404. case RoE:
  405. i+=0;
  406. break;
  407. }
  408. for (int c=0; c<map.width; c++) // reading terrain
  409. {
  410. for (int z=0; z<map.height; z++)
  411. {
  412. map.terrain[z][c].tertype = (EterrainType)(bufor[i++]);
  413. map.terrain[z][c].terview = bufor[i++];
  414. map.terrain[z][c].nuine = (Eriver)bufor[i++];
  415. map.terrain[z][c].rivDir = bufor[i++];
  416. map.terrain[z][c].malle = (Eroad)bufor[i++];
  417. map.terrain[z][c].roadDir = bufor[i++];
  418. map.terrain[z][c].siodmyTajemniczyBajt = bufor[i++];
  419. }
  420. }
  421. if (map.twoLevel) // read underground terrain
  422. {
  423. for (int c=0; c<map.width; c++) // reading terrain
  424. {
  425. for (int z=0; z<map.height; z++)
  426. {
  427. map.undergroungTerrain[z][c].tertype = (EterrainType)(bufor[i++]);
  428. map.undergroungTerrain[z][c].terview = bufor[i++];
  429. map.undergroungTerrain[z][c].nuine = (Eriver)bufor[i++];
  430. map.undergroungTerrain[z][c].rivDir = bufor[i++];
  431. map.undergroungTerrain[z][c].malle = (Eroad)bufor[i++];
  432. map.undergroungTerrain[z][c].roadDir = bufor[i++];
  433. map.undergroungTerrain[z][c].siodmyTajemniczyBajt = bufor[i++];
  434. }
  435. }
  436. }
  437. THC std::cout<<"Reading terrain: "<<th.getDif()<<std::endl;
  438. int defAmount = bufor[i]; // liczba defow
  439. defAmount = readNormalNr(i);
  440. i+=4;
  441. #if SDL_BYTEORDER == SDL_BIG_ENDIAN
  442. int srmask = 0xff000000;
  443. int sgmask = 0x00ff0000;
  444. int sbmask = 0x0000ff00;
  445. int samask = 0x000000ff;
  446. #else
  447. int srmask = 0x000000ff;
  448. int sgmask = 0x0000ff00;
  449. int sbmask = 0x00ff0000;
  450. int samask = 0xff000000;
  451. #endif
  452. SDL_Surface * alphaTransSurf = SDL_CreateRGBSurface(SDL_SWSURFACE, 12, 12, 32, srmask, sgmask, sbmask, samask);
  453. std::vector<std::string> defsToUnpack;
  454. for (int idd = 0 ; idd<defAmount; idd++) // reading defs
  455. {
  456. int nameLength = readNormalNr(i,4);i+=4;
  457. DefInfo vinya; // info about new def
  458. for (int cd=0;cd<nameLength;cd++)
  459. {
  460. vinya.name += bufor[i++];
  461. }
  462. for (int v=0; v<42; v++) // read info
  463. {
  464. vinya.bytes[v] = bufor[i++];
  465. }
  466. std::vector<DefObjInfo>::iterator pit = std::find(CGameInfo::mainObj->dobjinfo->objs.begin(), CGameInfo::mainObj->dobjinfo->objs.end(),
  467. vinya.name);
  468. if(pit == CGameInfo::mainObj->dobjinfo->objs.end())
  469. {
  470. vinya.isOnDefList = false;
  471. }
  472. else
  473. {
  474. vinya.printPriority = pit->priority;
  475. vinya.isOnDefList = true;
  476. }
  477. map.defy.push_back(vinya); // add this def to the vector
  478. defsToUnpack.push_back(vinya.name);
  479. //testing - only fragment//////////////////////////////////////////////////////////////
  480. /*map.defy[idd].handler = new CDefHandler();
  481. CGameInfo::mainObj->lodh->extractFile(std::string("newH3sprite.lod"), map.defy[idd].name);
  482. map.defy[idd].handler->openDef( std::string("newH3sprite\\")+map.defy[idd].name);
  483. for(int ff=0; ff<map.defy[idd].handler->ourImages.size(); ++ff) //adding shadows and transparency
  484. {
  485. map.defy[idd].handler->ourImages[ff].bitmap = CSDL_Ext::alphaTransform(map.defy[idd].handler->ourImages[ff].bitmap);
  486. SDL_Surface * bufs = CSDL_Ext::secondAlphaTransform(map.defy[idd].handler->ourImages[ff].bitmap, alphaTransSurf);
  487. SDL_FreeSurface(map.defy[idd].handler->ourImages[ff].bitmap);
  488. map.defy[idd].handler->ourImages[ff].bitmap = bufs;
  489. }
  490. boost::filesystem::remove(boost::filesystem::path(std::string("newH3sprite\\")+map.defy[idd].name));*/
  491. //system((std::string("DEL newH3sprite\\")+map.defy[idd].name).c_str());
  492. //end fo testing - only fragment///////////////////////////////////////////////////////
  493. //teceDef();
  494. }
  495. THC std::cout<<"Reading defs: "<<th.getDif()<<std::endl;
  496. ////loading objects
  497. int howManyObjs = readNormalNr(i, 4); i+=4;
  498. for(int ww=0; ww<howManyObjs; ++ww) //comment this line to turn loading objects off
  499. {
  500. //std::cout << "object nr "<<ww<<"\ti= "<<i<<std::endl;
  501. CObjectInstance * nobj = new CObjectInstance; //we will read this object
  502. nobj->id = CGameInfo::mainObj->objh->objInstances.size();
  503. nobj->pos.x = bufor[i++];
  504. nobj->pos.y = bufor[i++];
  505. nobj->pos.z = bufor[i++];
  506. nobj->defNumber = readNormalNr(i, 4); i+=4;
  507. nobj->defObjInfoNumber = -1;
  508. //if (((nobj.x==0)&&(nobj.y==0)) || nobj.x>map.width || nobj.y>map.height || nobj.z>1 || nobj.defNumber>map.defy.size())
  509. // std::cout << "Alarm!!! Obiekt "<<ww<<" jest kopniety (lub wystaje poza mape)\n";
  510. i+=5;
  511. unsigned char buff [30];
  512. for(int ccc=0; ccc<30; ++ccc)
  513. {
  514. buff[ccc] = bufor[i+ccc];
  515. }
  516. EDefType uu = getDefType(map.defy[nobj->defNumber]);
  517. int j = map.defy[nobj->defNumber].bytes[16];
  518. int p = 99;
  519. switch(getDefType(map.defy[nobj->defNumber]))
  520. {
  521. case EDefType::EVENTOBJ_DEF: //for event - objects
  522. {
  523. CEventObjInfo * spec = new CEventObjInfo;
  524. bool guardMess;
  525. guardMess = bufor[i]; ++i;
  526. if(guardMess)
  527. {
  528. int messLong = readNormalNr(i, 4); i+=4;
  529. if(messLong>0)
  530. {
  531. spec->isMessage = true;
  532. for(int yy=0; yy<messLong; ++yy)
  533. {
  534. spec->message +=bufor[i+yy];
  535. }
  536. i+=messLong;
  537. }
  538. spec->areGuarders = bufor[i]; ++i;
  539. if(spec->areGuarders)
  540. {
  541. spec->guarders = readCreatureSet(i); i+=28;
  542. }
  543. i+=4;
  544. }
  545. else
  546. {
  547. spec->isMessage = false;
  548. spec->areGuarders = false;
  549. spec->message = std::string("");
  550. }
  551. spec->gainedExp = readNormalNr(i, 4); i+=4;
  552. spec->manaDiff = readNormalNr(i, 4); i+=4;
  553. spec->moraleDiff = readNormalNr(i, 1, true); ++i;
  554. spec->luckDiff = readNormalNr(i, 1, true); ++i;
  555. spec->wood = readNormalNr(i); i+=4;
  556. spec->mercury = readNormalNr(i); i+=4;
  557. spec->ore = readNormalNr(i); i+=4;
  558. spec->sulfur = readNormalNr(i); i+=4;
  559. spec->crystal = readNormalNr(i); i+=4;
  560. spec->gems = readNormalNr(i); i+=4;
  561. spec->gold = readNormalNr(i); i+=4;
  562. spec->attack = readNormalNr(i, 1); ++i;
  563. spec->defence = readNormalNr(i, 1); ++i;
  564. spec->power = readNormalNr(i, 1); ++i;
  565. spec->knowledge = readNormalNr(i, 1); ++i;
  566. int gabn; //number of gained abilities
  567. gabn = readNormalNr(i, 1); ++i;
  568. for(int oo = 0; oo<gabn; ++oo)
  569. {
  570. spec->abilities.push_back((CGameInfo::mainObj->abilh)->abilities[readNormalNr(i, 1)]); ++i;
  571. spec->abilityLevels.push_back(readNormalNr(i, 1)); ++i;
  572. }
  573. int gart = readNormalNr(i, 1); ++i; //number of gained artifacts
  574. for(int oo = 0; oo<gart; ++oo)
  575. {
  576. spec->artifacts.push_back(&(CGameInfo::mainObj->arth->artifacts[readNormalNr(i, 2)])); i+=2;
  577. }
  578. int gspel = readNormalNr(i, 1); ++i; //number of gained spells
  579. for(int oo = 0; oo<gspel; ++oo)
  580. {
  581. spec->spells.push_back(&(CGameInfo::mainObj->spellh->spells[readNormalNr(i, 1)])); ++i;
  582. }
  583. int gcre = readNormalNr(i, 1); ++i; //number of gained creatures
  584. spec->creatures = readCreatureSet(i, gcre); i+=4*gcre;
  585. i+=8;
  586. spec->availableFor = readNormalNr(i, 1); ++i;
  587. spec->computerActivate = readNormalNr(i, 1); ++i;
  588. spec->humanActivate = readNormalNr(i, 1); ++i;
  589. i+=4;
  590. nobj->info = spec;
  591. break;
  592. }
  593. case EDefType::HERO_DEF:
  594. {
  595. CHeroObjInfo * spec = new CHeroObjInfo;
  596. spec->bytes[0] = bufor[i]; ++i;
  597. spec->bytes[1] = bufor[i]; ++i;
  598. spec->bytes[2] = bufor[i]; ++i;
  599. spec->bytes[3] = bufor[i]; ++i;
  600. spec->player = bufor[i]; ++i;
  601. int typeBuf = readNormalNr(i, 1); ++i;
  602. if(typeBuf==0xff)
  603. spec->type = NULL;
  604. else
  605. spec->type = CGameInfo::mainObj->heroh->heroes[typeBuf];
  606. bool isName = bufor[i]; ++i; //true if hero has nonstandard name
  607. if(isName)
  608. {
  609. int length = readNormalNr(i, 4); i+=4;
  610. for(int gg=0; gg<length; ++gg)
  611. {
  612. spec->name+=bufor[i]; ++i;
  613. }
  614. }
  615. else
  616. spec->name = std::string("");
  617. bool isExp = bufor[i]; ++i; //true if hore's experience is greater than 0
  618. if(isExp)
  619. {
  620. spec->experience = readNormalNr(i); i+=4;
  621. }
  622. else spec->experience = 0;
  623. bool portrait=bufor[i]; ++i;
  624. if (portrait)
  625. i++; //TODO read portrait nr, save, open
  626. bool nonstandardAbilities = bufor[i]; ++i; //true if hero has specified abilities
  627. if(nonstandardAbilities)
  628. {
  629. int howMany = readNormalNr(i); i+=4;
  630. for(int yy=0; yy<howMany; ++yy)
  631. {
  632. spec->abilities.push_back(CGameInfo::mainObj->abilh->abilities[readNormalNr(i, 1)]); ++i;
  633. spec->abilityLevels.push_back(readNormalNr(i, 1)); ++i;
  634. }
  635. }
  636. bool standGarrison = bufor[i]; ++i; //true if hero has nonstandard garrison
  637. spec->standardGarrison = standGarrison;
  638. if(standGarrison)
  639. {
  640. spec->garrison = readCreatureSet(i); i+=28; //4 bytes per slot
  641. }
  642. bool form = bufor[i]; ++i; //formation
  643. spec->garrison.formation = form;
  644. bool artSet = bufor[i]; ++i; //true if artifact set is not default (hero has some artifacts)
  645. if(artSet)
  646. {
  647. //head art //1
  648. int id = readNormalNr(i, 2); i+=2;
  649. if(id!=0xffff)
  650. spec->artHead = &(CGameInfo::mainObj->arth->artifacts[id]);
  651. else
  652. spec->artHead = NULL;
  653. //shoulders art //2
  654. id = readNormalNr(i, 2); i+=2;
  655. if(id!=0xffff)
  656. spec->artShoulders = &(CGameInfo::mainObj->arth->artifacts[id]);
  657. else
  658. spec->artShoulders = NULL;
  659. //neck art //3
  660. id = readNormalNr(i, 2); i+=2;
  661. if(id!=0xffff)
  662. spec->artNeck = &(CGameInfo::mainObj->arth->artifacts[id]);
  663. else
  664. spec->artNeck = NULL;
  665. //right hand art //4
  666. id = readNormalNr(i, 2); i+=2;
  667. if(id!=0xffff)
  668. spec->artRhand = &(CGameInfo::mainObj->arth->artifacts[id]);
  669. else
  670. spec->artRhand = NULL;
  671. //left hand art //5
  672. id = readNormalNr(i, 2); i+=2;
  673. if(id!=0xffff)
  674. spec->artLHand = &(CGameInfo::mainObj->arth->artifacts[id]);
  675. else
  676. spec->artLHand = NULL;
  677. //torso art //6
  678. id = readNormalNr(i, 2); i+=2;
  679. if(id!=0xffff)
  680. spec->artTorso = &(CGameInfo::mainObj->arth->artifacts[id]);
  681. else
  682. spec->artTorso = NULL;
  683. //right hand ring //7
  684. id = readNormalNr(i, 2); i+=2;
  685. if(id!=0xffff)
  686. spec->artRRing = &(CGameInfo::mainObj->arth->artifacts[id]);
  687. else
  688. spec->artRRing = NULL;
  689. //left hand ring //8
  690. id = readNormalNr(i, 2); i+=2;
  691. if(id!=0xffff)
  692. spec->artLRing = &(CGameInfo::mainObj->arth->artifacts[id]);
  693. else
  694. spec->artLRing = NULL;
  695. //feet art //9
  696. id = readNormalNr(i, 2); i+=2;
  697. if(id!=0xffff)
  698. spec->artFeet = &(CGameInfo::mainObj->arth->artifacts[id]);
  699. else
  700. spec->artFeet = NULL;
  701. //misc1 art //10
  702. id = readNormalNr(i, 2); i+=2;
  703. if(id!=0xffff)
  704. spec->artMisc1 = &(CGameInfo::mainObj->arth->artifacts[id]);
  705. else
  706. spec->artMisc1 = NULL;
  707. //misc2 art //11
  708. id = readNormalNr(i, 2); i+=2;
  709. if(id!=0xffff)
  710. spec->artMisc2 = &(CGameInfo::mainObj->arth->artifacts[id]);
  711. else
  712. spec->artMisc2 = NULL;
  713. //misc3 art //12
  714. id = readNormalNr(i, 2); i+=2;
  715. if(id!=0xffff)
  716. spec->artMisc3 = &(CGameInfo::mainObj->arth->artifacts[id]);
  717. else
  718. spec->artMisc3 = NULL;
  719. //misc4 art //13
  720. id = readNormalNr(i, 2); i+=2;
  721. if(id!=0xffff)
  722. spec->artMisc4 = &(CGameInfo::mainObj->arth->artifacts[id]);
  723. else
  724. spec->artMisc4 = NULL;
  725. //machine1 art //14
  726. id = readNormalNr(i, 2); i+=2;
  727. if(id!=0xffff)
  728. spec->artMach1 = &(CGameInfo::mainObj->arth->artifacts[id]);
  729. else
  730. spec->artMach1 = NULL;
  731. //machine2 art //15
  732. id = readNormalNr(i, 2); i+=2;
  733. if(id!=0xffff)
  734. spec->artMach2 = &(CGameInfo::mainObj->arth->artifacts[id]);
  735. else
  736. spec->artMach2 = NULL;
  737. //machine3 art //16
  738. id = readNormalNr(i, 2); i+=2;
  739. if(id!=0xffff)
  740. spec->artMach3 = &(CGameInfo::mainObj->arth->artifacts[id]);
  741. else
  742. spec->artMach3 = NULL;
  743. //misc5 art //17
  744. id = readNormalNr(i, 2); i+=2;
  745. if(id!=0xffff)
  746. spec->artMisc5 = &(CGameInfo::mainObj->arth->artifacts[id]);
  747. else
  748. spec->artMisc5 = NULL;
  749. //spellbook
  750. id = readNormalNr(i, 2); i+=2;
  751. if(id!=0xffff)
  752. spec->artSpellBook = &(CGameInfo::mainObj->arth->artifacts[id]);
  753. else
  754. spec->artSpellBook = NULL;
  755. //19 //???what is that? gap in file or what?
  756. i+=2;
  757. //bag artifacts //20
  758. int amount = readNormalNr(i, 2); i+=2; //number of artifacts in hero's bag
  759. if(amount>0)
  760. {
  761. for(int ss=0; ss<amount; ++ss)
  762. {
  763. id = readNormalNr(i, 2); i+=2;
  764. if(id!=0xffff)
  765. spec->artifacts.push_back(&(CGameInfo::mainObj->arth->artifacts[id]));
  766. else
  767. spec->artifacts.push_back(NULL);
  768. }
  769. }
  770. } //artifacts
  771. spec->guardRange = readNormalNr(i, 1); ++i;
  772. if(spec->guardRange == 0xff)
  773. spec->isGuarding = false;
  774. else
  775. spec->isGuarding = true;
  776. bool hasBiography = bufor[i]; ++i; //true if hero has nonstandard (mapmaker defined) biography
  777. if(hasBiography)
  778. {
  779. int length = readNormalNr(i); i+=4;
  780. int iStart = i;
  781. i+=length;
  782. for(int bb=0; bb<length; ++bb)
  783. {
  784. spec->biography+=bufor[iStart+bb];
  785. }
  786. }
  787. spec->sex = !(bufor[i]); ++i;
  788. //spells
  789. bool areSpells = bufor[i]; ++i;
  790. if(areSpells) //TODO: sprawdziæ //seems to be ok - tow
  791. {
  792. int ist = i;
  793. for(i; i<ist+9; ++i)
  794. {
  795. unsigned char c = bufor[i];
  796. for(int yy=0; yy<8; ++yy)
  797. {
  798. if((i-ist)*8+yy < CGameInfo::mainObj->spellh->spells.size())
  799. {
  800. if(c == (c|((unsigned char)intPow(2, yy))))
  801. spec->spells.push_back(&(CGameInfo::mainObj->spellh->spells[(i-ist)*8+yy]));
  802. }
  803. }
  804. }
  805. }
  806. //spells loaded
  807. spec->defaultMianStats = bufor[i]; ++i;
  808. if(spec->defaultMianStats)
  809. {
  810. spec->attack = bufor[i]; ++i;
  811. spec->defence = bufor[i]; ++i;
  812. spec->power = bufor[i]; ++i;
  813. spec->knowledge = bufor[i]; ++i;
  814. }
  815. i+=16;
  816. nobj->info = spec;
  817. //////creating CHeroInstance
  818. CHeroInstance * nhi = new CHeroInstance;
  819. nhi->exp = spec->experience;
  820. nhi->level = CGI->heroh->level(nhi->exp);
  821. nhi->mana = spec->knowledge * 10;
  822. nhi->movement = -1;
  823. nhi->name = spec->name;
  824. nhi->owner = spec->player;
  825. nhi->pos = nobj->pos;
  826. nhi->type = spec->type;
  827. nhi->army = spec->garrison;
  828. CGI->heroh->heroInstances.push_back(nhi);
  829. break;
  830. }
  831. case CREATURES_DEF:
  832. {
  833. CCreatureObjInfo * spec = new CCreatureObjInfo;
  834. spec->bytes[0] = bufor[i]; ++i;
  835. spec->bytes[1] = bufor[i]; ++i;
  836. spec->bytes[2] = bufor[i]; ++i;
  837. spec->bytes[3] = bufor[i]; ++i;
  838. spec->number = readNormalNr(i, 2); i+=2;
  839. spec->character = bufor[i]; ++i;
  840. bool isMesTre = bufor[i]; ++i; //true if there is message or treasury
  841. if(isMesTre)
  842. {
  843. int messLength = readNormalNr(i); i+=4;
  844. if(messLength>0)
  845. {
  846. for(int tt=0; tt<messLength; ++tt)
  847. {
  848. spec->message += bufor[i]; ++i;
  849. }
  850. }
  851. spec->wood = readNormalNr(i); i+=4;
  852. spec->mercury = readNormalNr(i); i+=4;
  853. spec->ore = readNormalNr(i); i+=4;
  854. spec->sulfur = readNormalNr(i); i+=4;
  855. spec->crytal = readNormalNr(i); i+=4;
  856. spec->gems = readNormalNr(i); i+=4;
  857. spec->gold = readNormalNr(i); i+=4;
  858. int artID = readNormalNr(i, 2); i+=2;
  859. if(artID!=0xffff)
  860. spec->gainedArtifact = &(CGameInfo::mainObj->arth->artifacts[artID]);
  861. else
  862. spec->gainedArtifact = NULL;
  863. }
  864. spec->neverFlees = bufor[i]; ++i;
  865. spec->notGrowingTeam = bufor[i]; ++i;
  866. i+=2;
  867. nobj->info = spec;
  868. break;
  869. }
  870. case EDefType::SIGN_DEF:
  871. {
  872. CSignObjInfo * spec = new CSignObjInfo;
  873. int length = readNormalNr(i); i+=4;
  874. for(int rr=0; rr<length; ++rr)
  875. {
  876. spec->message += bufor[i]; ++i;
  877. }
  878. i+=4;
  879. nobj->info = spec;
  880. break;
  881. }
  882. case EDefType::SEERHUT_DEF:
  883. {
  884. CSeerHutObjInfo * spec = new CSeerHutObjInfo;
  885. spec->missionType = bufor[i]; ++i;
  886. switch(spec->missionType)
  887. {
  888. case 1:
  889. {
  890. spec->m1level = readNormalNr(i); i+=4;
  891. int limit = readNormalNr(i); i+=4;
  892. if(limit == ((int)0xffffffff))
  893. {
  894. spec->isDayLimit = false;
  895. spec->lastDay = -1;
  896. }
  897. else
  898. {
  899. spec->isDayLimit = true;
  900. spec->lastDay = limit;
  901. }
  902. break;
  903. }
  904. case 2:
  905. {
  906. spec->m2attack = bufor[i]; ++i;
  907. spec->m2defence = bufor[i]; ++i;
  908. spec->m2power = bufor[i]; ++i;
  909. spec->m2knowledge = bufor[i]; ++i;
  910. int limit = readNormalNr(i); i+=4;
  911. if(limit == ((int)0xffffffff))
  912. {
  913. spec->isDayLimit = false;
  914. spec->lastDay = -1;
  915. }
  916. else
  917. {
  918. spec->isDayLimit = true;
  919. spec->lastDay = limit;
  920. }
  921. break;
  922. }
  923. case 3:
  924. {
  925. spec->m3bytes[0] = bufor[i]; ++i;
  926. spec->m3bytes[1] = bufor[i]; ++i;
  927. spec->m3bytes[2] = bufor[i]; ++i;
  928. spec->m3bytes[3] = bufor[i]; ++i;
  929. int limit = readNormalNr(i); i+=4;
  930. if(limit == ((int)0xffffffff))
  931. {
  932. spec->isDayLimit = false;
  933. spec->lastDay = -1;
  934. }
  935. else
  936. {
  937. spec->isDayLimit = true;
  938. spec->lastDay = limit;
  939. }
  940. break;
  941. }
  942. case 4:
  943. {
  944. spec->m4bytes[0] = bufor[i]; ++i;
  945. spec->m4bytes[1] = bufor[i]; ++i;
  946. spec->m4bytes[2] = bufor[i]; ++i;
  947. spec->m4bytes[3] = bufor[i]; ++i;
  948. int limit = readNormalNr(i); i+=4;
  949. if(limit == ((int)0xffffffff))
  950. {
  951. spec->isDayLimit = false;
  952. spec->lastDay = -1;
  953. }
  954. else
  955. {
  956. spec->isDayLimit = true;
  957. spec->lastDay = limit;
  958. }
  959. break;
  960. }
  961. case 5:
  962. {
  963. int artNumber = bufor[i]; ++i;
  964. for(int yy=0; yy<artNumber; ++yy)
  965. {
  966. int artid = readNormalNr(i, 2); i+=2;
  967. spec->m5arts.push_back(&(CGameInfo::mainObj->arth->artifacts[artid]));
  968. }
  969. int limit = readNormalNr(i); i+=4;
  970. if(limit == ((int)0xffffffff))
  971. {
  972. spec->isDayLimit = false;
  973. spec->lastDay = -1;
  974. }
  975. else
  976. {
  977. spec->isDayLimit = true;
  978. spec->lastDay = limit;
  979. }
  980. break;
  981. }
  982. case 6:
  983. {
  984. int typeNumber = bufor[i]; ++i;
  985. for(int hh=0; hh<typeNumber; ++hh)
  986. {
  987. int creType = readNormalNr(i, 2); i+=2;
  988. int creNumb = readNormalNr(i, 2); i+=2;
  989. spec->m6cre.push_back(&(CGameInfo::mainObj->creh->creatures[creType]));
  990. spec->m6number.push_back(creNumb);
  991. }
  992. int limit = readNormalNr(i); i+=4;
  993. if(limit == ((int)0xffffffff))
  994. {
  995. spec->isDayLimit = false;
  996. spec->lastDay = -1;
  997. }
  998. else
  999. {
  1000. spec->isDayLimit = true;
  1001. spec->lastDay = limit;
  1002. }
  1003. break;
  1004. }
  1005. case 7:
  1006. {
  1007. spec->m7wood = readNormalNr(i); i+=4;
  1008. spec->m7mercury = readNormalNr(i); i+=4;
  1009. spec->m7ore = readNormalNr(i); i+=4;
  1010. spec->m7sulfur = readNormalNr(i); i+=4;
  1011. spec->m7crystal = readNormalNr(i); i+=4;
  1012. spec->m7gems = readNormalNr(i); i+=4;
  1013. spec->m7gold = readNormalNr(i); i+=4;
  1014. int limit = readNormalNr(i); i+=4;
  1015. if(limit == ((int)0xffffffff))
  1016. {
  1017. spec->isDayLimit = false;
  1018. spec->lastDay = -1;
  1019. }
  1020. else
  1021. {
  1022. spec->isDayLimit = true;
  1023. spec->lastDay = limit;
  1024. }
  1025. break;
  1026. }
  1027. case 8:
  1028. {
  1029. int heroType = bufor[i]; ++i;
  1030. spec->m8hero = CGameInfo::mainObj->heroh->heroes[heroType];
  1031. int limit = readNormalNr(i); i+=4;
  1032. if(limit == ((int)0xffffffff))
  1033. {
  1034. spec->isDayLimit = false;
  1035. spec->lastDay = -1;
  1036. }
  1037. else
  1038. {
  1039. spec->isDayLimit = true;
  1040. spec->lastDay = limit;
  1041. }
  1042. break;
  1043. }
  1044. case 9:
  1045. {
  1046. spec->m9player = bufor[i]; ++i;
  1047. int limit = readNormalNr(i); i+=4;
  1048. if(limit == ((int)0xffffffff))
  1049. {
  1050. spec->isDayLimit = false;
  1051. spec->lastDay = -1;
  1052. }
  1053. else
  1054. {
  1055. spec->isDayLimit = true;
  1056. spec->lastDay = limit;
  1057. }
  1058. break;
  1059. }
  1060. }//internal switch end (seer huts)
  1061. int len1 = readNormalNr(i); i+=4;
  1062. for(int ee=0; ee<len1; ++ee)
  1063. {
  1064. spec->firstVisitText += bufor[i]; ++i;
  1065. }
  1066. int len2 = readNormalNr(i); i+=4;
  1067. for(int ee=0; ee<len2; ++ee)
  1068. {
  1069. spec->nextVisitText += bufor[i]; ++i;
  1070. }
  1071. int len3 = readNormalNr(i); i+=4;
  1072. for(int ee=0; ee<len3; ++ee)
  1073. {
  1074. spec->completedText += bufor[i]; ++i;
  1075. }
  1076. unsigned char rewardType = bufor[i]; ++i;
  1077. spec->rewardType = rewardType;
  1078. switch(rewardType)
  1079. {
  1080. case 1:
  1081. {
  1082. spec->r1exp = readNormalNr(i); i+=4;
  1083. break;
  1084. }
  1085. case 2:
  1086. {
  1087. spec->r2mana = readNormalNr(i); i+=4;
  1088. break;
  1089. }
  1090. case 3:
  1091. {
  1092. spec->r3morale = bufor[i]; ++i;
  1093. break;
  1094. }
  1095. case 4:
  1096. {
  1097. spec->r4luck = bufor[i]; ++i;
  1098. break;
  1099. }
  1100. case 5:
  1101. {
  1102. spec->r5type = bufor[i]; ++i;
  1103. spec->r5amount = readNormalNr(i, 3); i+=3;
  1104. i+=1;
  1105. break;
  1106. }
  1107. case 6:
  1108. {
  1109. spec->r6type = bufor[i]; ++i;
  1110. spec->r6amount = bufor[i]; ++i;
  1111. break;
  1112. }
  1113. case 7:
  1114. {
  1115. int abid = bufor[i]; ++i;
  1116. spec->r7ability = CGameInfo::mainObj->abilh->abilities[abid];
  1117. spec->r7level = bufor[i]; ++i;
  1118. break;
  1119. }
  1120. case 8:
  1121. {
  1122. int artid = readNormalNr(i, 2); i+=2;
  1123. spec->r8art = &(CGameInfo::mainObj->arth->artifacts[artid]);
  1124. break;
  1125. }
  1126. case 9:
  1127. {
  1128. int spellid = bufor[i]; ++i;
  1129. spec->r9spell = &(CGameInfo::mainObj->spellh->spells[spellid]);
  1130. break;
  1131. }
  1132. case 10:
  1133. {
  1134. int creid = readNormalNr(i, 2); i+=2;
  1135. spec->r10creature = &(CGameInfo::mainObj->creh->creatures[creid]);
  1136. spec->r10amount = readNormalNr(i, 2); i+=2;
  1137. break;
  1138. }
  1139. }// end of internal switch
  1140. i+=2;
  1141. nobj->info = spec;
  1142. break;
  1143. }
  1144. case EDefType::WITCHHUT_DEF:
  1145. {
  1146. CWitchHutObjInfo * spec = new CWitchHutObjInfo;
  1147. ist=i; //starting i for loop
  1148. for(i; i<ist+4; ++i)
  1149. {
  1150. unsigned char c = bufor[i];
  1151. for(int yy=0; yy<8; ++yy)
  1152. {
  1153. if((i-ist)*8+yy < CGameInfo::mainObj->abilh->abilities.size())
  1154. {
  1155. if(c == (c|((unsigned char)intPow(2, yy))))
  1156. spec->allowedAbilities.push_back(CGameInfo::mainObj->abilh->abilities[(i-ist)*8+yy]);
  1157. }
  1158. }
  1159. }
  1160. nobj->info = spec;
  1161. break;
  1162. }
  1163. case EDefType::SCHOLAR_DEF:
  1164. {
  1165. CScholarObjInfo * spec = new CScholarObjInfo;
  1166. spec->bonusType = bufor[i]; ++i;
  1167. switch(spec->bonusType)
  1168. {
  1169. case 0xff:
  1170. ++i;
  1171. break;
  1172. case 0:
  1173. spec->r0type = bufor[i]; ++i;
  1174. break;
  1175. case 1:
  1176. spec->r1 = CGameInfo::mainObj->abilh->abilities[bufor[i]]; ++i;
  1177. break;
  1178. case 2:
  1179. spec->r2 = &(CGameInfo::mainObj->spellh->spells[bufor[i]]); ++i;
  1180. break;
  1181. }
  1182. i+=6;
  1183. nobj->info = spec;
  1184. break;
  1185. }
  1186. case EDefType::GARRISON_DEF:
  1187. {
  1188. CGarrisonObjInfo * spec = new CGarrisonObjInfo;
  1189. spec->player = bufor[i]; ++i;
  1190. i+=3;
  1191. spec->units = readCreatureSet(i); i+=28;
  1192. spec->movableUnits = bufor[i]; ++i;
  1193. i+=8;
  1194. nobj->info = spec;
  1195. break;
  1196. }
  1197. case EDefType::ARTIFACT_DEF:
  1198. {
  1199. CArtifactObjInfo * spec = new CArtifactObjInfo;
  1200. bool areSettings = bufor[i]; ++i;
  1201. if(areSettings)
  1202. {
  1203. int messLength = readNormalNr(i, 4); i+=4;
  1204. for(int hh=0; hh<messLength; ++hh)
  1205. {
  1206. spec->message += bufor[i]; ++i;
  1207. }
  1208. bool areGuards = bufor[i]; ++i;
  1209. if(areGuards)
  1210. {
  1211. spec->areGuards = true;
  1212. spec->guards = readCreatureSet(i); i+=28;
  1213. }
  1214. else
  1215. spec->areGuards = false;
  1216. i+=4;
  1217. }
  1218. nobj->info = spec;
  1219. break;
  1220. }
  1221. case EDefType::RESOURCE_DEF:
  1222. {
  1223. CResourceObjInfo * spec = new CResourceObjInfo;
  1224. bool isMessGuard = bufor[i]; ++i;
  1225. if(isMessGuard)
  1226. {
  1227. int messLength = readNormalNr(i); i+=4;
  1228. for(int mm=0; mm<messLength; ++mm)
  1229. {
  1230. spec->message+=bufor[i]; ++i;
  1231. }
  1232. spec->areGuards = bufor[i]; ++i;
  1233. if(spec->areGuards)
  1234. {
  1235. spec->guards = readCreatureSet(i); i+=28;
  1236. }
  1237. i+=4;
  1238. }
  1239. else
  1240. {
  1241. spec->areGuards = false;
  1242. }
  1243. spec->amount = readNormalNr(i); i+=4;
  1244. i+=4;
  1245. nobj->info = spec;
  1246. break;
  1247. }
  1248. case EDefType::TOWN_DEF:
  1249. {
  1250. CCastleObjInfo * spec = new CCastleObjInfo;
  1251. spec->bytes[0] = bufor[i]; ++i;
  1252. spec->bytes[1] = bufor[i]; ++i;
  1253. spec->bytes[2] = bufor[i]; ++i;
  1254. spec->bytes[3] = bufor[i]; ++i;
  1255. spec->player = bufor[i]; ++i;
  1256. bool hasName = bufor[i]; ++i;
  1257. if(hasName)
  1258. {
  1259. int len = readNormalNr(i); i+=4;
  1260. for(int gg=0; gg<len; ++gg)
  1261. {
  1262. spec->name += bufor[i]; ++i;
  1263. }
  1264. }
  1265. bool stGarr = bufor[i]; ++i; //true if garrison isn't empty
  1266. if(stGarr)
  1267. {
  1268. spec->garrison = readCreatureSet(i); i+=28;
  1269. }
  1270. spec->garrison.formation = bufor[i]; ++i;
  1271. spec->unusualBuildins = bufor[i]; ++i;
  1272. if(spec->unusualBuildins)
  1273. {
  1274. for(int ff=0; ff<12; ++ff)
  1275. {
  1276. spec->buildingSettings[ff] = bufor[i]; ++i;
  1277. }
  1278. }
  1279. else
  1280. {
  1281. spec->hasFort = bufor[i]; ++i;
  1282. }
  1283. int ist = i;
  1284. for(i; i<ist+9; ++i)
  1285. {
  1286. unsigned char c = bufor[i];
  1287. for(int yy=0; yy<8; ++yy)
  1288. {
  1289. if((i-ist)*8+yy < CGameInfo::mainObj->spellh->spells.size())
  1290. {
  1291. if(c == (c|((unsigned char)intPow(2, yy))))
  1292. spec->obligatorySpells.push_back(&(CGameInfo::mainObj->spellh->spells[(i-ist)*8+yy]));
  1293. }
  1294. }
  1295. }
  1296. ist = i;
  1297. for(i; i<ist+9; ++i)
  1298. {
  1299. unsigned char c = bufor[i];
  1300. for(int yy=0; yy<8; ++yy)
  1301. {
  1302. if((i-ist)*8+yy < CGameInfo::mainObj->spellh->spells.size())
  1303. {
  1304. if(c != (c|((unsigned char)intPow(2, yy))))
  1305. spec->possibleSpells.push_back(&(CGameInfo::mainObj->spellh->spells[(i-ist)*8+yy]));
  1306. }
  1307. }
  1308. }
  1309. /////// reading castle events //////////////////////////////////
  1310. int numberOfEvent = readNormalNr(i); i+=4;
  1311. for(int gh = 0; gh<numberOfEvent; ++gh)
  1312. {
  1313. CCastleEvent nce;
  1314. int nameLen = readNormalNr(i); i+=4;
  1315. for(int ll=0; ll<nameLen; ++ll)
  1316. {
  1317. nce.name += bufor[i]; ++i;
  1318. }
  1319. int messLen = readNormalNr(i); i+=4;
  1320. for(int ll=0; ll<messLen; ++ll)
  1321. {
  1322. nce.message += bufor[i]; ++i;
  1323. }
  1324. nce.wood = readNormalNr(i); i+=4;
  1325. nce.mercury = readNormalNr(i); i+=4;
  1326. nce.ore = readNormalNr(i); i+=4;
  1327. nce.sulfur = readNormalNr(i); i+=4;
  1328. nce.crystal = readNormalNr(i); i+=4;
  1329. nce.gems = readNormalNr(i); i+=4;
  1330. nce.gold = readNormalNr(i); i+=4;
  1331. nce.players = bufor[i]; ++i;
  1332. nce.forHuman = bufor[i]; ++i;
  1333. nce.forComputer = bufor[i]; ++i;
  1334. nce.firstShow = readNormalNr(i, 2); i+=2;
  1335. nce.forEvery = bufor[i]; ++i;
  1336. i+=17;
  1337. for(int kk=0; kk<6; ++kk)
  1338. {
  1339. nce.bytes[kk] = bufor[i]; ++i;
  1340. }
  1341. for(int vv=0; vv<7; ++vv)
  1342. {
  1343. nce.gen[vv] = readNormalNr(i, 2); i+=2;
  1344. }
  1345. i+=4;
  1346. spec->events.push_back(nce);
  1347. }
  1348. /////// castle events have been read ///////////////////////////
  1349. spec->alignment = bufor[i]; ++i;
  1350. i+=3;
  1351. nobj->info = spec;
  1352. break;
  1353. }
  1354. case EDefType::PLAYERONLY_DEF:
  1355. {
  1356. CPlayerOnlyObjInfo * spec = new CPlayerOnlyObjInfo;
  1357. spec->player = bufor[i]; ++i;
  1358. i+=3;
  1359. nobj->info = spec;
  1360. break;
  1361. }
  1362. case EDefType::SHRINE_DEF:
  1363. {
  1364. CShrineObjInfo * spec = new CShrineObjInfo;
  1365. spec->spell = bufor[i]; i+=4;
  1366. nobj->info = spec;
  1367. break;
  1368. }
  1369. case EDefType::SPELLSCROLL_DEF:
  1370. {
  1371. CSpellScrollObjinfo * spec = new CSpellScrollObjinfo;
  1372. bool messg = bufor[i]; ++i;
  1373. if(messg)
  1374. {
  1375. int mLength = readNormalNr(i); i+=4;
  1376. for(int vv=0; vv<mLength; ++vv)
  1377. {
  1378. spec->message += bufor[i]; ++i;
  1379. }
  1380. spec->areGuarders = bufor[i]; ++i;
  1381. if(spec->areGuarders)
  1382. {
  1383. spec->guarders = readCreatureSet(i); i+=28;
  1384. }
  1385. i+=4;
  1386. }
  1387. spec->spell = &(CGameInfo::mainObj->spellh->spells[bufor[i]]); ++i;
  1388. i+=3;
  1389. nobj->info = spec;
  1390. break;
  1391. }
  1392. case EDefType::PANDORA_DEF:
  1393. {
  1394. CPandorasBoxObjInfo * spec = new CPandorasBoxObjInfo;
  1395. bool messg = bufor[i]; ++i;
  1396. if(messg)
  1397. {
  1398. int mLength = readNormalNr(i); i+=4;
  1399. for(int vv=0; vv<mLength; ++vv)
  1400. {
  1401. spec->message += bufor[i]; ++i;
  1402. }
  1403. spec->areGuarders = bufor[i]; ++i;
  1404. if(spec->areGuarders)
  1405. {
  1406. spec->guarders = readCreatureSet(i); i+=28;
  1407. }
  1408. i+=4;
  1409. }
  1410. ////// copied form event handling (seems to be similar)
  1411. spec->gainedExp = readNormalNr(i, 4); i+=4;
  1412. spec->manaDiff = readNormalNr(i, 4); i+=4;
  1413. spec->moraleDiff = readNormalNr(i, 1, true); ++i;
  1414. spec->luckDiff = readNormalNr(i, 1, true); ++i;
  1415. spec->wood = readNormalNr(i); i+=4;
  1416. spec->mercury = readNormalNr(i); i+=4;
  1417. spec->ore = readNormalNr(i); i+=4;
  1418. spec->sulfur = readNormalNr(i); i+=4;
  1419. spec->crystal = readNormalNr(i); i+=4;
  1420. spec->gems = readNormalNr(i); i+=4;
  1421. spec->gold = readNormalNr(i); i+=4;
  1422. spec->attack = readNormalNr(i, 1); ++i;
  1423. spec->defence = readNormalNr(i, 1); ++i;
  1424. spec->power = readNormalNr(i, 1); ++i;
  1425. spec->knowledge = readNormalNr(i, 1); ++i;
  1426. int gabn; //number of gained abilities
  1427. gabn = readNormalNr(i, 1); ++i;
  1428. for(int oo = 0; oo<gabn; ++oo)
  1429. {
  1430. spec->abilities.push_back((CGameInfo::mainObj->abilh)->abilities[readNormalNr(i, 1)]); ++i;
  1431. spec->abilityLevels.push_back(readNormalNr(i, 1)); ++i;
  1432. }
  1433. int gart = readNormalNr(i, 1); ++i; //number of gained artifacts
  1434. for(int oo = 0; oo<gart; ++oo)
  1435. {
  1436. spec->artifacts.push_back(&(CGameInfo::mainObj->arth->artifacts[readNormalNr(i, 2)])); i+=2;
  1437. }
  1438. int gspel = readNormalNr(i, 1); ++i; //number of gained spells
  1439. for(int oo = 0; oo<gspel; ++oo)
  1440. {
  1441. spec->spells.push_back(&(CGameInfo::mainObj->spellh->spells[readNormalNr(i, 1)])); ++i;
  1442. }
  1443. int gcre = readNormalNr(i, 1); ++i; //number of gained creatures
  1444. spec->creatures = readCreatureSet(i, gcre); i+=4*gcre;
  1445. i+=8;
  1446. nobj->info = spec;
  1447. ///////end of copied fragment
  1448. break;
  1449. }
  1450. case EDefType::GRAIL_DEF:
  1451. {
  1452. CGrailObjInfo * spec = new CGrailObjInfo;
  1453. spec->radius = readNormalNr(i); i+=4;
  1454. nobj->info = spec;
  1455. break;
  1456. }
  1457. case EDefType::CREGEN_DEF:
  1458. {
  1459. CCreGenObjInfo * spec = new CCreGenObjInfo;
  1460. spec->player = bufor[i]; ++i;
  1461. i+=3;
  1462. for(int ggg=0; ggg<4; ++ggg)
  1463. {
  1464. spec->bytes[ggg] = bufor[i]; ++i;
  1465. }
  1466. if((spec->bytes[0] == '\0') && (spec->bytes[1] == '\0') && (spec->bytes[2] == '\0') && (spec->bytes[3] == '\0'))
  1467. {
  1468. spec->asCastle = false;
  1469. spec->castles[0] = bufor[i]; ++i;
  1470. spec->castles[1] = bufor[i]; ++i;
  1471. }
  1472. else
  1473. {
  1474. spec->asCastle = true;
  1475. }
  1476. nobj->info = spec;
  1477. break;
  1478. }
  1479. case EDefType::CREGEN2_DEF:
  1480. {
  1481. CCreGen2ObjInfo * spec = new CCreGen2ObjInfo;
  1482. spec->player = bufor[i]; ++i;
  1483. i+=3;
  1484. for(int ggg=0; ggg<4; ++ggg)
  1485. {
  1486. spec->bytes[ggg] = bufor[i]; ++i;
  1487. }
  1488. if((spec->bytes[0] == '\0') && (spec->bytes[1] == '\0') && (spec->bytes[2] == '\0') && (spec->bytes[3] == '\0'))
  1489. {
  1490. spec->asCastle = false;
  1491. spec->castles[0] = bufor[i]; ++i;
  1492. spec->castles[1] = bufor[i]; ++i;
  1493. }
  1494. else
  1495. {
  1496. spec->asCastle = true;
  1497. }
  1498. spec->minLevel = bufor[i]; ++i;
  1499. spec->maxLevel = bufor[i]; ++i;
  1500. if(spec->maxLevel>7)
  1501. spec->maxLevel = 7;
  1502. if(spec->minLevel<1)
  1503. spec->minLevel = 1;
  1504. nobj->info = spec;
  1505. break;
  1506. }
  1507. case EDefType::CREGEN3_DEF:
  1508. {
  1509. CCreGen3ObjInfo * spec = new CCreGen3ObjInfo;
  1510. spec->player = bufor[i]; ++i;
  1511. i+=3;
  1512. spec->minLevel = bufor[i]; ++i;
  1513. spec->maxLevel = bufor[i]; ++i;
  1514. if(spec->maxLevel>7)
  1515. spec->maxLevel = 7;
  1516. if(spec->minLevel<1)
  1517. spec->minLevel = 1;
  1518. nobj->info = spec;
  1519. break;
  1520. }
  1521. case EDefType::BORDERGUARD_DEF:
  1522. {
  1523. CBorderGuardObjInfo * spec = new CBorderGuardObjInfo;
  1524. spec->missionType = bufor[i]; ++i;
  1525. switch(spec->missionType)
  1526. {
  1527. case 1:
  1528. {
  1529. spec->m1level = readNormalNr(i); i+=4;
  1530. int limit = readNormalNr(i); i+=4;
  1531. if(limit == ((int)0xffffffff))
  1532. {
  1533. spec->isDayLimit = false;
  1534. spec->lastDay = -1;
  1535. }
  1536. else
  1537. {
  1538. spec->isDayLimit = true;
  1539. spec->lastDay = limit;
  1540. }
  1541. break;
  1542. }
  1543. case 2:
  1544. {
  1545. spec->m2attack = bufor[i]; ++i;
  1546. spec->m2defence = bufor[i]; ++i;
  1547. spec->m2power = bufor[i]; ++i;
  1548. spec->m2knowledge = bufor[i]; ++i;
  1549. int limit = readNormalNr(i); i+=4;
  1550. if(limit == ((int)0xffffffff))
  1551. {
  1552. spec->isDayLimit = false;
  1553. spec->lastDay = -1;
  1554. }
  1555. else
  1556. {
  1557. spec->isDayLimit = true;
  1558. spec->lastDay = limit;
  1559. }
  1560. break;
  1561. }
  1562. case 3:
  1563. {
  1564. spec->m3bytes[0] = bufor[i]; ++i;
  1565. spec->m3bytes[1] = bufor[i]; ++i;
  1566. spec->m3bytes[2] = bufor[i]; ++i;
  1567. spec->m3bytes[3] = bufor[i]; ++i;
  1568. int limit = readNormalNr(i); i+=4;
  1569. if(limit == ((int)0xffffffff))
  1570. {
  1571. spec->isDayLimit = false;
  1572. spec->lastDay = -1;
  1573. }
  1574. else
  1575. {
  1576. spec->isDayLimit = true;
  1577. spec->lastDay = limit;
  1578. }
  1579. break;
  1580. }
  1581. case 4:
  1582. {
  1583. spec->m4bytes[0] = bufor[i]; ++i;
  1584. spec->m4bytes[1] = bufor[i]; ++i;
  1585. spec->m4bytes[2] = bufor[i]; ++i;
  1586. spec->m4bytes[3] = bufor[i]; ++i;
  1587. int limit = readNormalNr(i); i+=4;
  1588. if(limit == ((int)0xffffffff))
  1589. {
  1590. spec->isDayLimit = false;
  1591. spec->lastDay = -1;
  1592. }
  1593. else
  1594. {
  1595. spec->isDayLimit = true;
  1596. spec->lastDay = limit;
  1597. }
  1598. break;
  1599. }
  1600. case 5:
  1601. {
  1602. int artNumber = bufor[i]; ++i;
  1603. for(int yy=0; yy<artNumber; ++yy)
  1604. {
  1605. int artid = readNormalNr(i, 2); i+=2;
  1606. spec->m5arts.push_back(&(CGameInfo::mainObj->arth->artifacts[artid]));
  1607. }
  1608. int limit = readNormalNr(i); i+=4;
  1609. if(limit == ((int)0xffffffff))
  1610. {
  1611. spec->isDayLimit = false;
  1612. spec->lastDay = -1;
  1613. }
  1614. else
  1615. {
  1616. spec->isDayLimit = true;
  1617. spec->lastDay = limit;
  1618. }
  1619. break;
  1620. }
  1621. case 6:
  1622. {
  1623. int typeNumber = bufor[i]; ++i;
  1624. for(int hh=0; hh<typeNumber; ++hh)
  1625. {
  1626. int creType = readNormalNr(i, 2); i+=2;
  1627. int creNumb = readNormalNr(i, 2); i+=2;
  1628. spec->m6cre.push_back(&(CGameInfo::mainObj->creh->creatures[creType]));
  1629. spec->m6number.push_back(creNumb);
  1630. }
  1631. int limit = readNormalNr(i); i+=4;
  1632. if(limit == ((int)0xffffffff))
  1633. {
  1634. spec->isDayLimit = false;
  1635. spec->lastDay = -1;
  1636. }
  1637. else
  1638. {
  1639. spec->isDayLimit = true;
  1640. spec->lastDay = limit;
  1641. }
  1642. break;
  1643. }
  1644. case 7:
  1645. {
  1646. spec->m7wood = readNormalNr(i); i+=4;
  1647. spec->m7mercury = readNormalNr(i); i+=4;
  1648. spec->m7ore = readNormalNr(i); i+=4;
  1649. spec->m7sulfur = readNormalNr(i); i+=4;
  1650. spec->m7crystal = readNormalNr(i); i+=4;
  1651. spec->m7gems = readNormalNr(i); i+=4;
  1652. spec->m7gold = readNormalNr(i); i+=4;
  1653. int limit = readNormalNr(i); i+=4;
  1654. if(limit == ((int)0xffffffff))
  1655. {
  1656. spec->isDayLimit = false;
  1657. spec->lastDay = -1;
  1658. }
  1659. else
  1660. {
  1661. spec->isDayLimit = true;
  1662. spec->lastDay = limit;
  1663. }
  1664. break;
  1665. }
  1666. case 8:
  1667. {
  1668. int heroType = bufor[i]; ++i;
  1669. spec->m8hero = CGameInfo::mainObj->heroh->heroes[heroType];
  1670. int limit = readNormalNr(i); i+=4;
  1671. if(limit == ((int)0xffffffff))
  1672. {
  1673. spec->isDayLimit = false;
  1674. spec->lastDay = -1;
  1675. }
  1676. else
  1677. {
  1678. spec->isDayLimit = true;
  1679. spec->lastDay = limit;
  1680. }
  1681. break;
  1682. }
  1683. case 9:
  1684. {
  1685. spec->m9player = bufor[i]; ++i;
  1686. int limit = readNormalNr(i); i+=4;
  1687. if(limit == ((int)0xffffffff))
  1688. {
  1689. spec->isDayLimit = false;
  1690. spec->lastDay = -1;
  1691. }
  1692. else
  1693. {
  1694. spec->isDayLimit = true;
  1695. spec->lastDay = limit;
  1696. }
  1697. break;
  1698. }
  1699. }//internal switch end (seer huts)
  1700. int len1 = readNormalNr(i); i+=4;
  1701. for(int ee=0; ee<len1; ++ee)
  1702. {
  1703. spec->firstVisitText += bufor[i]; ++i;
  1704. }
  1705. int len2 = readNormalNr(i); i+=4;
  1706. for(int ee=0; ee<len2; ++ee)
  1707. {
  1708. spec->nextVisitText += bufor[i]; ++i;
  1709. }
  1710. int len3 = readNormalNr(i); i+=4;
  1711. for(int ee=0; ee<len3; ++ee)
  1712. {
  1713. spec->completedText += bufor[i]; ++i;
  1714. }
  1715. nobj->info = spec;
  1716. break;
  1717. }
  1718. } //end of main switch
  1719. CGameInfo::mainObj->objh->objInstances.push_back(nobj);
  1720. //TODO - dokoñczyæ, du¿o do zrobienia - trzeba patrzeæ, co def niesie
  1721. }//*/ //end of loading objects; commented to make application work until it will be finished
  1722. ////objects loaded
  1723. processMap(defsToUnpack);
  1724. std::vector<CDefHandler *> dhandlers = CGameInfo::mainObj->spriteh->extractManyFiles(defsToUnpack);
  1725. for (int i=0;i<dhandlers.size();i++)
  1726. map.defy[i].handler=dhandlers[i];
  1727. for(int vv=0; vv<map.defy.size(); ++vv)
  1728. {
  1729. if(map.defy[vv].handler->alphaTransformed)
  1730. continue;
  1731. for(int yy=0; yy<map.defy[vv].handler->ourImages.size(); ++yy)
  1732. {
  1733. map.defy[vv].handler->ourImages[yy].bitmap = CSDL_Ext::alphaTransform(map.defy[vv].handler->ourImages[yy].bitmap);
  1734. SDL_Surface * bufs = CSDL_Ext::secondAlphaTransform(map.defy[vv].handler->ourImages[yy].bitmap, alphaTransSurf);
  1735. SDL_FreeSurface(map.defy[vv].handler->ourImages[yy].bitmap);
  1736. map.defy[vv].handler->ourImages[yy].bitmap = bufs;
  1737. map.defy[vv].handler->alphaTransformed = true;
  1738. }
  1739. }
  1740. SDL_FreeSurface(alphaTransSurf);
  1741. //assigning defobjinfos
  1742. for(int ww=0; ww<CGI->objh->objInstances.size(); ++ww)
  1743. {
  1744. for(int h=0; h<CGI->dobjinfo->objs.size(); ++h)
  1745. {
  1746. if(CGI->dobjinfo->objs[h].defName==CGI->ac->map.defy[CGI->objh->objInstances[ww]->defNumber].name)
  1747. {
  1748. CGI->objh->objInstances[ww]->defObjInfoNumber = h;
  1749. break;
  1750. }
  1751. }
  1752. }
  1753. //assigned
  1754. //loading events
  1755. int numberOfEvents = readNormalNr(i); i+=4;
  1756. for(int yyoo=0; yyoo<numberOfEvents; ++yyoo)
  1757. {
  1758. CMapEvent ne;
  1759. ne.name = std::string();
  1760. ne.message = std::string();
  1761. int nameLen = readNormalNr(i); i+=4;
  1762. for(int qq=0; qq<nameLen; ++qq)
  1763. {
  1764. ne.name += bufor[i]; ++i;
  1765. }
  1766. int messLen = readNormalNr(i); i+=4;
  1767. for(int qq=0; qq<messLen; ++qq)
  1768. {
  1769. ne.message +=bufor[i]; ++i;
  1770. }
  1771. ne.wood = readNormalNr(i); i+=4;
  1772. ne.mercury = readNormalNr(i); i+=4;
  1773. ne.ore = readNormalNr(i); i+=4;
  1774. ne.sulfur = readNormalNr(i); i+=4;
  1775. ne.crystal = readNormalNr(i); i+=4;
  1776. ne.gems = readNormalNr(i); i+=4;
  1777. ne.gold = readNormalNr(i); i+=4;
  1778. ne.players = bufor[i]; ++i;
  1779. ne.humanAffected = bufor[i]; ++i;
  1780. ne.computerAffected = bufor[i]; ++i;
  1781. ne.firstOccurence = bufor[i]; ++i;
  1782. ne.nextOccurence = bufor[i]; ++i;
  1783. i+=18;
  1784. map.events.push_back(ne);
  1785. }
  1786. }
  1787. int CAmbarCendamo::readNormalNr (int pos, int bytCon, bool cyclic)
  1788. {
  1789. int ret=0;
  1790. int amp=1;
  1791. for (int i=0; i<bytCon; i++)
  1792. {
  1793. ret+=bufor[pos+i]*amp;
  1794. amp*=256;
  1795. }
  1796. if(cyclic && bytCon<4 && ret>=amp/2)
  1797. {
  1798. ret = ret-amp;
  1799. }
  1800. return ret;
  1801. }
  1802. void CAmbarCendamo::loadDefs()
  1803. {
  1804. std::set<int> loadedTypes;
  1805. for (int i=0; i<map.width; i++)
  1806. {
  1807. for (int j=0; j<map.width; j++)
  1808. {
  1809. if (loadedTypes.find(map.terrain[i][j].tertype)==loadedTypes.end())
  1810. {
  1811. CDefHandler *sdh = CGI->spriteh->giveDef(CSemiDefHandler::nameFromType(map.terrain[i][j].tertype).c_str());
  1812. loadedTypes.insert(map.terrain[i][j].tertype);
  1813. defs.push_back(sdh);
  1814. }
  1815. if (map.twoLevel && loadedTypes.find(map.undergroungTerrain[i][j].tertype)==loadedTypes.end())
  1816. {
  1817. CDefHandler *sdh = CGI->spriteh->giveDef(CSemiDefHandler::nameFromType(map.undergroungTerrain[i][j].tertype).c_str());
  1818. loadedTypes.insert(map.undergroungTerrain[i][j].tertype);
  1819. defs.push_back(sdh);
  1820. }
  1821. }
  1822. }
  1823. }
  1824. EDefType CAmbarCendamo::getDefType(DefInfo &a)
  1825. {
  1826. switch(a.bytes[16])
  1827. {
  1828. case 5: case 65: case 66: case 67: case 68: case 69:
  1829. return EDefType::ARTIFACT_DEF; //handled
  1830. case 6:
  1831. return EDefType::PANDORA_DEF; //hanled
  1832. case 26:
  1833. return EDefType::EVENTOBJ_DEF; //handled
  1834. case 33:
  1835. return EDefType::GARRISON_DEF; //handled
  1836. case 34: case 70: case 62: //70 - random hero //62 - prison
  1837. return EDefType::HERO_DEF; //handled
  1838. case 36:
  1839. return EDefType::GRAIL_DEF; //hanled
  1840. case 53: case 17: case 18: case 19: case 20: case 42: case 87: case 220://cases 17 - 20 and 42 - tests
  1841. return EDefType::PLAYERONLY_DEF; //handled
  1842. case 54: case 71: case 72: case 73: case 74: case 75: case 162: case 163: case 164:
  1843. return EDefType::CREATURES_DEF; //handled
  1844. case 59:
  1845. return EDefType::SIGN_DEF; //handled
  1846. case 77:
  1847. return EDefType::TOWN_DEF; //can be problematic, but handled
  1848. case 79: case 76:
  1849. return EDefType::RESOURCE_DEF; //handled
  1850. case 81:
  1851. return EDefType::SCHOLAR_DEF; //handled
  1852. case 83:
  1853. return EDefType::SEERHUT_DEF; //handled
  1854. case 91:
  1855. return EDefType::SIGN_DEF; //handled
  1856. case 88: case 89: case 90:
  1857. return SHRINE_DEF; //handled
  1858. case 93:
  1859. return SPELLSCROLL_DEF; //handled
  1860. case 98:
  1861. return EDefType::TOWN_DEF; //handled
  1862. case 113:
  1863. return EDefType::WITCHHUT_DEF; //handled
  1864. case 215:
  1865. return EDefType::BORDERGUARD_DEF; //handled by analogy to seer huts ;]
  1866. case 216:
  1867. return EDefType::CREGEN2_DEF; //handled
  1868. case 217:
  1869. return EDefType::CREGEN_DEF; //handled
  1870. case 218:
  1871. return EDefType::CREGEN3_DEF; //handled
  1872. case 219:
  1873. return EDefType::GARRISON_DEF; //handled
  1874. default:
  1875. return EDefType::TERRAINOBJ_DEF; // nothing to be handled
  1876. }
  1877. }
  1878. CCreatureSet CAmbarCendamo::readCreatureSet(int pos, int number)
  1879. {
  1880. CCreatureSet ret;
  1881. std::pair<CCreature *, int> ins;
  1882. if(number>0 && readNormalNr(pos, 2)!=0xffff)
  1883. {
  1884. int rettt = readNormalNr(pos, 2);
  1885. if(rettt>32768)
  1886. rettt = 65536-rettt+CGameInfo::mainObj->creh->creatures.size()-16;
  1887. ins.first = &(CGameInfo::mainObj->creh->creatures[rettt]);
  1888. ins.second = readNormalNr(pos+2, 2);
  1889. std::pair<int,std::pair<CCreature *, int> > tt(0,ins);
  1890. ret.slots.insert(tt);
  1891. }
  1892. if(number>1 && readNormalNr(pos+4, 2)!=0xffff)
  1893. {
  1894. int rettt = readNormalNr(pos+4, 2);
  1895. if(rettt>32768)
  1896. rettt = 65536-rettt+CGameInfo::mainObj->creh->creatures.size()-16;
  1897. ins.first = &(CGameInfo::mainObj->creh->creatures[rettt]);
  1898. ins.second = readNormalNr(pos+6, 2);
  1899. std::pair<int,std::pair<CCreature *, int> > tt(1,ins);
  1900. ret.slots.insert(tt);
  1901. }
  1902. if(number>2 && readNormalNr(pos+8, 2)!=0xffff)
  1903. {
  1904. int rettt = readNormalNr(pos+8, 2);
  1905. if(rettt>32768)
  1906. rettt = 65536-rettt+CGameInfo::mainObj->creh->creatures.size()-16;
  1907. ins.first = &(CGameInfo::mainObj->creh->creatures[rettt]);
  1908. ins.second = readNormalNr(pos+10, 2);
  1909. std::pair<int,std::pair<CCreature *, int> > tt(2,ins);
  1910. ret.slots.insert(tt);
  1911. }
  1912. if(number>3 && readNormalNr(pos+12, 2)!=0xffff)
  1913. {
  1914. int rettt = readNormalNr(pos+12, 2);
  1915. if(rettt>32768)
  1916. rettt = 65536-rettt+CGameInfo::mainObj->creh->creatures.size()-16;
  1917. ins.first = &(CGameInfo::mainObj->creh->creatures[rettt]);
  1918. ins.second = readNormalNr(pos+14, 2);
  1919. std::pair<int,std::pair<CCreature *, int> > tt(3,ins);
  1920. ret.slots.insert(tt);
  1921. }
  1922. if(number>4 && readNormalNr(pos+16, 2)!=0xffff)
  1923. {
  1924. int rettt = readNormalNr(pos+16, 2);
  1925. if(rettt>32768)
  1926. rettt = 65536-rettt+CGameInfo::mainObj->creh->creatures.size()-16;
  1927. ins.first = &(CGameInfo::mainObj->creh->creatures[rettt]);
  1928. ins.second = readNormalNr(pos+18, 2);
  1929. std::pair<int,std::pair<CCreature *, int> > tt(4,ins);
  1930. ret.slots.insert(tt);
  1931. }
  1932. if(number>5 && readNormalNr(pos+20, 2)!=0xffff)
  1933. {
  1934. int rettt = readNormalNr(pos+20, 2);
  1935. if(rettt>32768)
  1936. rettt = 65536-rettt+CGameInfo::mainObj->creh->creatures.size()-16;
  1937. ins.first = &(CGameInfo::mainObj->creh->creatures[rettt]);
  1938. ins.second = readNormalNr(pos+22, 2);
  1939. std::pair<int,std::pair<CCreature *, int> > tt(5,ins);
  1940. ret.slots.insert(tt);
  1941. }
  1942. if(number>6 && readNormalNr(pos+24, 2)!=0xffff)
  1943. {
  1944. int rettt = readNormalNr(pos+24, 2);
  1945. if(rettt>32768)
  1946. rettt = 65536-rettt+CGameInfo::mainObj->creh->creatures.size()-16;
  1947. ins.first = &(CGameInfo::mainObj->creh->creatures[rettt]);
  1948. ins.second = readNormalNr(pos+26, 2);
  1949. std::pair<int,std::pair<CCreature *, int> > tt(6,ins);
  1950. ret.slots.insert(tt);
  1951. }
  1952. return ret;
  1953. }
  1954. void CAmbarCendamo::processMap(std::vector<std::string> & defsToUnpack)
  1955. {
  1956. resDefNames.push_back("AVTRNDM0.DEF");
  1957. resDefNames.push_back("AVTWOOD0.DEF");
  1958. resDefNames.push_back("AVTMERC0.DEF");
  1959. resDefNames.push_back("AVTORE0.DEF");
  1960. resDefNames.push_back("AVTSULF0.DEF");
  1961. resDefNames.push_back("AVTCRYS0.DEF");
  1962. resDefNames.push_back("AVTGEMS0.DEF");
  1963. resDefNames.push_back("AVTGOLD0.DEF");
  1964. resDefNames.push_back("ZMITHR.DEF");
  1965. std::vector<int> resDefNumbers;
  1966. for(int hh=0; hh<resDefNames.size(); ++hh)
  1967. {
  1968. resDefNumbers.push_back(-1);
  1969. for(int k=0; k<map.defy.size(); ++k)
  1970. {
  1971. std::string buf = map.defy[k].name;
  1972. std::transform(buf.begin(), buf.end(), buf.begin(), (int(*)(int))toupper);
  1973. if(resDefNames[hh] == buf)
  1974. {
  1975. resDefNumbers[resDefNumbers.size()-1] = k;
  1976. break;
  1977. }
  1978. }
  1979. }
  1980. std::vector<std::string> creDefNames;
  1981. for(int dd=0; dd<140; ++dd) //we do not use here WoG units
  1982. {
  1983. creDefNames.push_back(CGI->dobjinfo->objs[dd+1184].defName);
  1984. }
  1985. std::vector<int> creDefNumbers;
  1986. for(int ee=0; ee<creDefNames.size(); ++ee)
  1987. {
  1988. creDefNumbers.push_back(-1);
  1989. }
  1990. std::vector<std::string> artDefNames;
  1991. std::vector<int> artDefNumbers;
  1992. for(int bb=0; bb<162; ++bb)
  1993. {
  1994. if(CGI->arth->artifacts[CGI->dobjinfo->objs[bb+213].subtype].aClass!=EartClass::SartClass)
  1995. artDefNames.push_back(CGI->dobjinfo->objs[bb+213].defName);
  1996. }
  1997. for(int ee=0; ee<artDefNames.size(); ++ee)
  1998. {
  1999. artDefNumbers.push_back(-1);
  2000. }
  2001. std::vector<std::string> art1DefNames;
  2002. std::vector<int> art1DefNumbers;
  2003. for(int bb=0; bb<162; ++bb)
  2004. {
  2005. if(CGI->arth->artifacts[CGI->dobjinfo->objs[bb+213].subtype].aClass==EartClass::TartClass)
  2006. art1DefNames.push_back(CGI->dobjinfo->objs[bb+213].defName);
  2007. }
  2008. for(int ee=0; ee<art1DefNames.size(); ++ee)
  2009. {
  2010. art1DefNumbers.push_back(-1);
  2011. }
  2012. std::vector<std::string> art2DefNames;
  2013. std::vector<int> art2DefNumbers;
  2014. for(int bb=0; bb<162; ++bb)
  2015. {
  2016. if(CGI->arth->artifacts[CGI->dobjinfo->objs[bb+213].subtype].aClass==EartClass::NartClass)
  2017. art2DefNames.push_back(CGI->dobjinfo->objs[bb+213].defName);
  2018. }
  2019. for(int ee=0; ee<art2DefNames.size(); ++ee)
  2020. {
  2021. art2DefNumbers.push_back(-1);
  2022. }
  2023. std::vector<std::string> art3DefNames;
  2024. std::vector<int> art3DefNumbers;
  2025. for(int bb=0; bb<162; ++bb)
  2026. {
  2027. if(CGI->arth->artifacts[CGI->dobjinfo->objs[bb+213].subtype].aClass==EartClass::JartClass)
  2028. art3DefNames.push_back(CGI->dobjinfo->objs[bb+213].defName);
  2029. }
  2030. for(int ee=0; ee<art3DefNames.size(); ++ee)
  2031. {
  2032. art3DefNumbers.push_back(-1);
  2033. }
  2034. std::vector<std::string> art4DefNames;
  2035. std::vector<int> art4DefNumbers;
  2036. for(int bb=0; bb<162; ++bb)
  2037. {
  2038. if(CGI->arth->artifacts[CGI->dobjinfo->objs[bb+213].subtype].aClass==EartClass::RartClass)
  2039. art4DefNames.push_back(CGI->dobjinfo->objs[bb+213].defName);
  2040. }
  2041. for(int ee=0; ee<art4DefNames.size(); ++ee)
  2042. {
  2043. art4DefNumbers.push_back(-1);
  2044. }
  2045. std::vector<std::string> town0DefNames; //without fort
  2046. std::vector<int> town0DefNumbers;
  2047. std::vector<std::string> town1DefNames; //with fort
  2048. std::vector<int> town1DefNumbers;
  2049. for(int dd=0; dd<F_NUMBER; ++dd)
  2050. {
  2051. town1DefNames.push_back(CGI->dobjinfo->objs[dd+385].defName);
  2052. town1DefNumbers.push_back(-1);
  2053. }
  2054. std::vector< std::vector<std::string> > creGenNames;
  2055. std::vector< std::vector<int> > creGenNumbers;
  2056. creGenNames.resize(F_NUMBER);
  2057. creGenNumbers.resize(F_NUMBER);
  2058. for(int ff=0; ff<F_NUMBER-1; ++ff)
  2059. {
  2060. for(int dd=0; dd<7; ++dd)
  2061. {
  2062. creGenNames[ff].push_back(CGI->dobjinfo->objs[395+7*ff+dd].defName);
  2063. creGenNumbers[ff].push_back(-1);
  2064. }
  2065. }
  2066. for(int dd=0; dd<7; ++dd)
  2067. {
  2068. creGenNumbers[8].push_back(-1);
  2069. }
  2070. creGenNames[F_NUMBER-1].push_back(CGI->dobjinfo->objs[457].defName);
  2071. creGenNames[F_NUMBER-1].push_back(CGI->dobjinfo->objs[452].defName);
  2072. creGenNames[F_NUMBER-1].push_back(CGI->dobjinfo->objs[455].defName);
  2073. creGenNames[F_NUMBER-1].push_back(CGI->dobjinfo->objs[454].defName);
  2074. creGenNames[F_NUMBER-1].push_back(CGI->dobjinfo->objs[453].defName);
  2075. creGenNames[F_NUMBER-1].push_back(CGI->dobjinfo->objs[458].defName);
  2076. creGenNames[F_NUMBER-1].push_back(CGI->dobjinfo->objs[459].defName);
  2077. for(int b=0; b<CGI->scenarioOps.playerInfos.size(); ++b)
  2078. {
  2079. if(CGI->scenarioOps.playerInfos[b].castle==-1)
  2080. CGI->scenarioOps.playerInfos[b].castle = rand()%F_NUMBER;
  2081. }
  2082. //variables initialized
  2083. for(int j=0; j<CGI->objh->objInstances.size(); ++j)
  2084. {
  2085. DefInfo curDef = map.defy[CGI->objh->objInstances[j]->defNumber];
  2086. switch(getDefType(curDef))
  2087. {
  2088. case EDefType::RESOURCE_DEF:
  2089. {
  2090. if(curDef.bytes[16]==76) //resource to specify
  2091. {
  2092. DefInfo nxt = curDef;
  2093. nxt.bytes[16] = 79;
  2094. nxt.bytes[20] = rand()%7;
  2095. if(resDefNumbers[nxt.bytes[20]+1]!=-1)
  2096. {
  2097. CGI->objh->objInstances[j]->defNumber = resDefNumbers[nxt.bytes[20]+1];
  2098. continue;
  2099. }
  2100. nxt.name = resDefNames[nxt.bytes[20]+1];
  2101. std::vector<DefObjInfo>::iterator pit = std::find(CGameInfo::mainObj->dobjinfo->objs.begin(), CGameInfo::mainObj->dobjinfo->objs.end(),
  2102. nxt.name);
  2103. if(pit == CGameInfo::mainObj->dobjinfo->objs.end())
  2104. {
  2105. nxt.isOnDefList = false;
  2106. }
  2107. else
  2108. {
  2109. nxt.printPriority = pit->priority;
  2110. nxt.isOnDefList = true;
  2111. }
  2112. map.defy.push_back(nxt); // add this def to the vector
  2113. defsToUnpack.push_back(nxt.name);
  2114. CGI->objh->objInstances[j]->defNumber = map.defy.size()-1;
  2115. if(resDefNumbers[nxt.bytes[20]+1]==-1)
  2116. {
  2117. resDefNumbers[nxt.bytes[20]+1] = map.defy.size()-1;
  2118. }
  2119. }
  2120. break;
  2121. }
  2122. case EDefType::CREATURES_DEF:
  2123. {
  2124. if(curDef.bytes[16]==72) //random monster lvl 1
  2125. {
  2126. DefInfo nxt = curDef;
  2127. nxt.bytes[16] = 54;
  2128. nxt.bytes[20] = 14*(rand()%9)+rand()%2;
  2129. if(creDefNumbers[nxt.bytes[20]]!=-1)
  2130. {
  2131. CGI->objh->objInstances[j]->defNumber = creDefNumbers[nxt.bytes[20]];
  2132. continue;
  2133. }
  2134. nxt.name = creDefNames[nxt.bytes[20]];
  2135. std::vector<DefObjInfo>::iterator pit = std::find(CGameInfo::mainObj->dobjinfo->objs.begin(), CGameInfo::mainObj->dobjinfo->objs.end(),
  2136. nxt.name);
  2137. if(pit == CGameInfo::mainObj->dobjinfo->objs.end())
  2138. {
  2139. nxt.isOnDefList = false;
  2140. }
  2141. else
  2142. {
  2143. nxt.printPriority = pit->priority;
  2144. nxt.isOnDefList = true;
  2145. }
  2146. map.defy.push_back(nxt); // add this def to the vector
  2147. defsToUnpack.push_back(nxt.name);
  2148. CGI->objh->objInstances[j]->defNumber = map.defy.size()-1;
  2149. if(creDefNumbers[nxt.bytes[20]]==-1)
  2150. {
  2151. creDefNumbers[nxt.bytes[20]] = map.defy.size()-1;
  2152. }
  2153. }
  2154. if(curDef.bytes[16]==73) //random monster lvl 2
  2155. {
  2156. DefInfo nxt = curDef;
  2157. nxt.bytes[16] = 54;
  2158. nxt.bytes[20] = 14*(rand()%9)+rand()%2+2;
  2159. if(creDefNumbers[nxt.bytes[20]]!=-1)
  2160. {
  2161. CGI->objh->objInstances[j]->defNumber = creDefNumbers[nxt.bytes[20]];
  2162. continue;
  2163. }
  2164. nxt.name = creDefNames[nxt.bytes[20]];
  2165. std::vector<DefObjInfo>::iterator pit = std::find(CGameInfo::mainObj->dobjinfo->objs.begin(), CGameInfo::mainObj->dobjinfo->objs.end(),
  2166. nxt.name);
  2167. if(pit == CGameInfo::mainObj->dobjinfo->objs.end())
  2168. {
  2169. nxt.isOnDefList = false;
  2170. }
  2171. else
  2172. {
  2173. nxt.printPriority = pit->priority;
  2174. nxt.isOnDefList = true;
  2175. }
  2176. map.defy.push_back(nxt); // add this def to the vector
  2177. defsToUnpack.push_back(nxt.name);
  2178. CGI->objh->objInstances[j]->defNumber = map.defy.size()-1;
  2179. if(creDefNumbers[nxt.bytes[20]]==-1)
  2180. {
  2181. creDefNumbers[nxt.bytes[20]] = map.defy.size()-1;
  2182. }
  2183. }
  2184. if(curDef.bytes[16]==74) //random monster lvl 3
  2185. {
  2186. DefInfo nxt = curDef;
  2187. nxt.bytes[16] = 54;
  2188. nxt.bytes[20] = 14*(rand()%9)+rand()%2+4;
  2189. if(creDefNumbers[nxt.bytes[20]]!=-1)
  2190. {
  2191. CGI->objh->objInstances[j]->defNumber = creDefNumbers[nxt.bytes[20]];
  2192. continue;
  2193. }
  2194. nxt.name = creDefNames[nxt.bytes[20]];
  2195. std::vector<DefObjInfo>::iterator pit = std::find(CGameInfo::mainObj->dobjinfo->objs.begin(), CGameInfo::mainObj->dobjinfo->objs.end(),
  2196. nxt.name);
  2197. if(pit == CGameInfo::mainObj->dobjinfo->objs.end())
  2198. {
  2199. nxt.isOnDefList = false;
  2200. }
  2201. else
  2202. {
  2203. nxt.printPriority = pit->priority;
  2204. nxt.isOnDefList = true;
  2205. }
  2206. map.defy.push_back(nxt); // add this def to the vector
  2207. defsToUnpack.push_back(nxt.name);
  2208. CGI->objh->objInstances[j]->defNumber = map.defy.size()-1;
  2209. if(creDefNumbers[nxt.bytes[20]]==-1)
  2210. {
  2211. creDefNumbers[nxt.bytes[20]] = map.defy.size()-1;
  2212. }
  2213. }
  2214. if(curDef.bytes[16]==75) //random monster lvl 4
  2215. {
  2216. DefInfo nxt = curDef;
  2217. nxt.bytes[16] = 54;
  2218. nxt.bytes[20] = 14*(rand()%9)+rand()%2+6;
  2219. if(creDefNumbers[nxt.bytes[20]]!=-1)
  2220. {
  2221. CGI->objh->objInstances[j]->defNumber = creDefNumbers[nxt.bytes[20]];
  2222. continue;
  2223. }
  2224. nxt.name = creDefNames[nxt.bytes[20]];
  2225. std::vector<DefObjInfo>::iterator pit = std::find(CGameInfo::mainObj->dobjinfo->objs.begin(), CGameInfo::mainObj->dobjinfo->objs.end(),
  2226. nxt.name);
  2227. if(pit == CGameInfo::mainObj->dobjinfo->objs.end())
  2228. {
  2229. nxt.isOnDefList = false;
  2230. }
  2231. else
  2232. {
  2233. nxt.printPriority = pit->priority;
  2234. nxt.isOnDefList = true;
  2235. }
  2236. map.defy.push_back(nxt); // add this def to the vector
  2237. defsToUnpack.push_back(nxt.name);
  2238. CGI->objh->objInstances[j]->defNumber = map.defy.size()-1;
  2239. if(creDefNumbers[nxt.bytes[20]]==-1)
  2240. {
  2241. creDefNumbers[nxt.bytes[20]] = map.defy.size()-1;
  2242. }
  2243. }
  2244. if(curDef.bytes[16]==162) //random monster lvl 5
  2245. {
  2246. DefInfo nxt = curDef;
  2247. nxt.bytes[16] = 54;
  2248. nxt.bytes[20] = 14*(rand()%9)+rand()%2+8;
  2249. if(creDefNumbers[nxt.bytes[20]]!=-1)
  2250. {
  2251. CGI->objh->objInstances[j]->defNumber = creDefNumbers[nxt.bytes[20]];
  2252. continue;
  2253. }
  2254. nxt.name = creDefNames[nxt.bytes[20]];
  2255. std::vector<DefObjInfo>::iterator pit = std::find(CGameInfo::mainObj->dobjinfo->objs.begin(), CGameInfo::mainObj->dobjinfo->objs.end(),
  2256. nxt.name);
  2257. if(pit == CGameInfo::mainObj->dobjinfo->objs.end())
  2258. {
  2259. nxt.isOnDefList = false;
  2260. }
  2261. else
  2262. {
  2263. nxt.printPriority = pit->priority;
  2264. nxt.isOnDefList = true;
  2265. }
  2266. map.defy.push_back(nxt); // add this def to the vector
  2267. defsToUnpack.push_back(nxt.name);
  2268. CGI->objh->objInstances[j]->defNumber = map.defy.size()-1;
  2269. if(creDefNumbers[nxt.bytes[20]]==-1)
  2270. {
  2271. creDefNumbers[nxt.bytes[20]] = map.defy.size()-1;
  2272. }
  2273. }
  2274. if(curDef.bytes[16]==163) //random monster lvl 6
  2275. {
  2276. DefInfo nxt = curDef;
  2277. nxt.bytes[16] = 54;
  2278. nxt.bytes[20] = 14*(rand()%9)+rand()%2+10;
  2279. if(creDefNumbers[nxt.bytes[20]]!=-1)
  2280. {
  2281. CGI->objh->objInstances[j]->defNumber = creDefNumbers[nxt.bytes[20]];
  2282. continue;
  2283. }
  2284. nxt.name = creDefNames[nxt.bytes[20]];
  2285. std::vector<DefObjInfo>::iterator pit = std::find(CGameInfo::mainObj->dobjinfo->objs.begin(), CGameInfo::mainObj->dobjinfo->objs.end(),
  2286. nxt.name);
  2287. if(pit == CGameInfo::mainObj->dobjinfo->objs.end())
  2288. {
  2289. nxt.isOnDefList = false;
  2290. }
  2291. else
  2292. {
  2293. nxt.printPriority = pit->priority;
  2294. nxt.isOnDefList = true;
  2295. }
  2296. map.defy.push_back(nxt); // add this def to the vector
  2297. defsToUnpack.push_back(nxt.name);
  2298. CGI->objh->objInstances[j]->defNumber = map.defy.size()-1;
  2299. if(creDefNumbers[nxt.bytes[20]]==-1)
  2300. {
  2301. creDefNumbers[nxt.bytes[20]] = map.defy.size()-1;
  2302. }
  2303. }
  2304. if(curDef.bytes[16]==164) //random monster lvl 7
  2305. {
  2306. DefInfo nxt = curDef;
  2307. nxt.bytes[16] = 54;
  2308. nxt.bytes[20] = 14*(rand()%9)+rand()%2+12;
  2309. if(creDefNumbers[nxt.bytes[20]]!=-1)
  2310. {
  2311. CGI->objh->objInstances[j]->defNumber = creDefNumbers[nxt.bytes[20]];
  2312. continue;
  2313. }
  2314. nxt.name = creDefNames[nxt.bytes[20]];
  2315. std::vector<DefObjInfo>::iterator pit = std::find(CGameInfo::mainObj->dobjinfo->objs.begin(), CGameInfo::mainObj->dobjinfo->objs.end(),
  2316. nxt.name);
  2317. if(pit == CGameInfo::mainObj->dobjinfo->objs.end())
  2318. {
  2319. nxt.isOnDefList = false;
  2320. }
  2321. else
  2322. {
  2323. nxt.printPriority = pit->priority;
  2324. nxt.isOnDefList = true;
  2325. }
  2326. map.defy.push_back(nxt); // add this def to the vector
  2327. defsToUnpack.push_back(nxt.name);
  2328. CGI->objh->objInstances[j]->defNumber = map.defy.size()-1;
  2329. if(creDefNumbers[nxt.bytes[20]]==-1)
  2330. {
  2331. creDefNumbers[nxt.bytes[20]] = map.defy.size()-1;
  2332. }
  2333. }
  2334. if(curDef.bytes[16]==71) //random monster (any level)
  2335. {
  2336. DefInfo nxt = curDef;
  2337. nxt.bytes[16] = 54;
  2338. nxt.bytes[20] = rand()%126;
  2339. if(creDefNumbers[nxt.bytes[20]]!=-1)
  2340. {
  2341. CGI->objh->objInstances[j]->defNumber = creDefNumbers[nxt.bytes[20]];
  2342. continue;
  2343. }
  2344. nxt.name = creDefNames[nxt.bytes[20]];
  2345. std::vector<DefObjInfo>::iterator pit = std::find(CGameInfo::mainObj->dobjinfo->objs.begin(), CGameInfo::mainObj->dobjinfo->objs.end(),
  2346. nxt.name);
  2347. if(pit == CGameInfo::mainObj->dobjinfo->objs.end())
  2348. {
  2349. nxt.isOnDefList = false;
  2350. }
  2351. else
  2352. {
  2353. nxt.printPriority = pit->priority;
  2354. nxt.isOnDefList = true;
  2355. }
  2356. map.defy.push_back(nxt); // add this def to the vector
  2357. defsToUnpack.push_back(nxt.name);
  2358. CGI->objh->objInstances[j]->defNumber = map.defy.size()-1;
  2359. if(creDefNumbers[nxt.bytes[20]]==-1)
  2360. {
  2361. creDefNumbers[nxt.bytes[20]] = map.defy.size()-1;
  2362. }
  2363. }
  2364. break;
  2365. } //end of case
  2366. case EDefType::ARTIFACT_DEF:
  2367. {
  2368. if(curDef.bytes[16]==65) //random atrifact (any class)
  2369. {
  2370. DefInfo nxt = curDef;
  2371. nxt.bytes[16] = 5;
  2372. nxt.bytes[20] = rand()%artDefNames.size();
  2373. if(artDefNumbers[nxt.bytes[20]]!=-1)
  2374. {
  2375. CGI->objh->objInstances[j]->defNumber = artDefNumbers[nxt.bytes[20]];
  2376. continue;
  2377. }
  2378. nxt.name = artDefNames[nxt.bytes[20]];
  2379. std::vector<DefObjInfo>::iterator pit = std::find(CGameInfo::mainObj->dobjinfo->objs.begin(), CGameInfo::mainObj->dobjinfo->objs.end(),
  2380. nxt.name);
  2381. if(pit == CGameInfo::mainObj->dobjinfo->objs.end())
  2382. {
  2383. nxt.isOnDefList = false;
  2384. }
  2385. else
  2386. {
  2387. nxt.printPriority = pit->priority;
  2388. nxt.isOnDefList = true;
  2389. }
  2390. map.defy.push_back(nxt); // add this def to the vector
  2391. defsToUnpack.push_back(nxt.name);
  2392. CGI->objh->objInstances[j]->defNumber = map.defy.size()-1;
  2393. if(artDefNumbers[nxt.bytes[20]]==-1)
  2394. {
  2395. artDefNumbers[nxt.bytes[20]] = map.defy.size()-1;
  2396. }
  2397. }
  2398. if(curDef.bytes[16]==66) //random atrifact (treasure)
  2399. {
  2400. DefInfo nxt = curDef;
  2401. nxt.bytes[16] = 5;
  2402. nxt.bytes[20] = rand()%art1DefNames.size();
  2403. if(art1DefNumbers[nxt.bytes[20]]!=-1)
  2404. {
  2405. CGI->objh->objInstances[j]->defNumber = art1DefNumbers[nxt.bytes[20]];
  2406. continue;
  2407. }
  2408. nxt.name = art1DefNames[nxt.bytes[20]];
  2409. std::vector<DefObjInfo>::iterator pit = std::find(CGameInfo::mainObj->dobjinfo->objs.begin(), CGameInfo::mainObj->dobjinfo->objs.end(),
  2410. nxt.name);
  2411. if(pit == CGameInfo::mainObj->dobjinfo->objs.end())
  2412. {
  2413. nxt.isOnDefList = false;
  2414. }
  2415. else
  2416. {
  2417. nxt.printPriority = pit->priority;
  2418. nxt.isOnDefList = true;
  2419. }
  2420. map.defy.push_back(nxt); // add this def to the vector
  2421. defsToUnpack.push_back(nxt.name);
  2422. CGI->objh->objInstances[j]->defNumber = map.defy.size()-1;
  2423. if(art1DefNumbers[nxt.bytes[20]]==-1)
  2424. {
  2425. art1DefNumbers[nxt.bytes[20]] = map.defy.size()-1;
  2426. }
  2427. }
  2428. if(curDef.bytes[16]==67) //random atrifact (minor)
  2429. {
  2430. DefInfo nxt = curDef;
  2431. nxt.bytes[16] = 5;
  2432. nxt.bytes[20] = rand()%art2DefNames.size();
  2433. if(art2DefNumbers[nxt.bytes[20]]!=-1)
  2434. {
  2435. CGI->objh->objInstances[j]->defNumber = art2DefNumbers[nxt.bytes[20]];
  2436. continue;
  2437. }
  2438. nxt.name = art2DefNames[nxt.bytes[20]];
  2439. std::vector<DefObjInfo>::iterator pit = std::find(CGameInfo::mainObj->dobjinfo->objs.begin(), CGameInfo::mainObj->dobjinfo->objs.end(),
  2440. nxt.name);
  2441. if(pit == CGameInfo::mainObj->dobjinfo->objs.end())
  2442. {
  2443. nxt.isOnDefList = false;
  2444. }
  2445. else
  2446. {
  2447. nxt.printPriority = pit->priority;
  2448. nxt.isOnDefList = true;
  2449. }
  2450. map.defy.push_back(nxt); // add this def to the vector
  2451. defsToUnpack.push_back(nxt.name);
  2452. CGI->objh->objInstances[j]->defNumber = map.defy.size()-1;
  2453. if(art2DefNumbers[nxt.bytes[20]]==-1)
  2454. {
  2455. art2DefNumbers[nxt.bytes[20]] = map.defy.size()-1;
  2456. }
  2457. }
  2458. if(curDef.bytes[16]==68) //random atrifact (major)
  2459. {
  2460. DefInfo nxt = curDef;
  2461. nxt.bytes[16] = 5;
  2462. nxt.bytes[20] = rand()%art3DefNames.size();
  2463. if(art3DefNumbers[nxt.bytes[20]]!=-1)
  2464. {
  2465. CGI->objh->objInstances[j]->defNumber = art3DefNumbers[nxt.bytes[20]];
  2466. continue;
  2467. }
  2468. nxt.name = art3DefNames[nxt.bytes[20]];
  2469. std::vector<DefObjInfo>::iterator pit = std::find(CGameInfo::mainObj->dobjinfo->objs.begin(), CGameInfo::mainObj->dobjinfo->objs.end(),
  2470. nxt.name);
  2471. if(pit == CGameInfo::mainObj->dobjinfo->objs.end())
  2472. {
  2473. nxt.isOnDefList = false;
  2474. }
  2475. else
  2476. {
  2477. nxt.printPriority = pit->priority;
  2478. nxt.isOnDefList = true;
  2479. }
  2480. map.defy.push_back(nxt); // add this def to the vector
  2481. defsToUnpack.push_back(nxt.name);
  2482. CGI->objh->objInstances[j]->defNumber = map.defy.size()-1;
  2483. if(art3DefNumbers[nxt.bytes[20]]==-1)
  2484. {
  2485. art3DefNumbers[nxt.bytes[20]] = map.defy.size()-1;
  2486. }
  2487. }
  2488. if(curDef.bytes[16]==69) //random atrifact (relic)
  2489. {
  2490. DefInfo nxt = curDef;
  2491. nxt.bytes[16] = 5;
  2492. nxt.bytes[20] = rand()%art4DefNames.size();
  2493. if(art4DefNumbers[nxt.bytes[20]]!=-1)
  2494. {
  2495. CGI->objh->objInstances[j]->defNumber = art4DefNumbers[nxt.bytes[20]];
  2496. continue;
  2497. }
  2498. nxt.name = art4DefNames[nxt.bytes[20]];
  2499. std::vector<DefObjInfo>::iterator pit = std::find(CGameInfo::mainObj->dobjinfo->objs.begin(), CGameInfo::mainObj->dobjinfo->objs.end(),
  2500. nxt.name);
  2501. if(pit == CGameInfo::mainObj->dobjinfo->objs.end())
  2502. {
  2503. nxt.isOnDefList = false;
  2504. }
  2505. else
  2506. {
  2507. nxt.printPriority = pit->priority;
  2508. nxt.isOnDefList = true;
  2509. }
  2510. map.defy.push_back(nxt); // add this def to the vector
  2511. defsToUnpack.push_back(nxt.name);
  2512. CGI->objh->objInstances[j]->defNumber = map.defy.size()-1;
  2513. if(art4DefNumbers[nxt.bytes[20]]==-1)
  2514. {
  2515. art4DefNumbers[nxt.bytes[20]] = map.defy.size()-1;
  2516. }
  2517. }
  2518. break;
  2519. }
  2520. case EDefType::TOWN_DEF:
  2521. {
  2522. if(curDef.bytes[16]==77) //random town
  2523. {
  2524. DefInfo nxt = curDef;
  2525. nxt.bytes[16] = 98;
  2526. if(((CCastleObjInfo*)CGI->objh->objInstances[j]->info)->player==0xff)
  2527. {
  2528. nxt.bytes[20] = rand()%town1DefNames.size();
  2529. }
  2530. else
  2531. {
  2532. if(CGI->scenarioOps.getIthPlayersSettings(((CCastleObjInfo*)CGI->objh->objInstances[j]->info)->player).castle>-1)
  2533. {
  2534. nxt.bytes[20] = CGI->scenarioOps.getIthPlayersSettings(((CCastleObjInfo*)CGI->objh->objInstances[j]->info)->player).castle;
  2535. }
  2536. else
  2537. {
  2538. nxt.bytes[20] = rand()%town1DefNames.size();
  2539. }
  2540. }
  2541. if(town1DefNumbers[nxt.bytes[20]]!=-1)
  2542. {
  2543. CGI->objh->objInstances[j]->defNumber = town1DefNumbers[nxt.bytes[20]];
  2544. continue;
  2545. }
  2546. nxt.name = town1DefNames[nxt.bytes[20]];
  2547. std::vector<DefObjInfo>::iterator pit = std::find(CGameInfo::mainObj->dobjinfo->objs.begin(), CGameInfo::mainObj->dobjinfo->objs.end(),
  2548. nxt.name);
  2549. if(pit == CGameInfo::mainObj->dobjinfo->objs.end())
  2550. {
  2551. nxt.isOnDefList = false;
  2552. }
  2553. else
  2554. {
  2555. nxt.printPriority = pit->priority;
  2556. nxt.isOnDefList = true;
  2557. }
  2558. map.defy.push_back(nxt); // add this def to the vector
  2559. defsToUnpack.push_back(nxt.name);
  2560. CGI->objh->objInstances[j]->defNumber = map.defy.size()-1;
  2561. if(town1DefNumbers[nxt.bytes[20]]==-1)
  2562. {
  2563. town1DefNumbers[nxt.bytes[20]] = map.defy.size()-1;
  2564. }
  2565. }
  2566. //((CCastleObjInfo*)CGI->objh->objInstances[j].info)
  2567. break;
  2568. }
  2569. } //end of main switch
  2570. } //end of main loop
  2571. for(int j=0; j<CGI->objh->objInstances.size(); ++j) //for creature dwellings on map (they are town type dependent)
  2572. {
  2573. DefInfo curDef = map.defy[CGI->objh->objInstances[j]->defNumber];
  2574. switch(getDefType(curDef))
  2575. {
  2576. case EDefType::CREGEN_DEF:
  2577. {
  2578. if(((CCreGenObjInfo*)CGI->objh->objInstances[j]->info)->asCastle)
  2579. {
  2580. DefInfo nxt = curDef;
  2581. nxt.bytes[16] = 17;
  2582. for(int vv=0; vv<CGI->objh->objInstances.size(); ++vv)
  2583. {
  2584. if(getDefType(map.defy[CGI->objh->objInstances[vv]->defNumber])==EDefType::TOWN_DEF)
  2585. {
  2586. if(
  2587. ((CCastleObjInfo*)CGI->objh->objInstances[vv]->info)->bytes[0]==((CCreGenObjInfo*)CGI->objh->objInstances[j]->info)->bytes[0]
  2588. && ((CCastleObjInfo*)CGI->objh->objInstances[vv]->info)->bytes[1]==((CCreGenObjInfo*)CGI->objh->objInstances[j]->info)->bytes[1]
  2589. && ((CCastleObjInfo*)CGI->objh->objInstances[vv]->info)->bytes[2]==((CCreGenObjInfo*)CGI->objh->objInstances[j]->info)->bytes[2]
  2590. && ((CCastleObjInfo*)CGI->objh->objInstances[vv]->info)->bytes[3]==((CCreGenObjInfo*)CGI->objh->objInstances[j]->info)->bytes[3])
  2591. {
  2592. for(int mm=0; mm<town1DefNames.size(); ++mm)
  2593. {
  2594. std::transform(town1DefNames[mm].begin(), town1DefNames[mm].end(), town1DefNames[mm].begin(), (int(*)(int))toupper);
  2595. std::string hlp = map.defy[CGI->objh->objInstances[vv]->defNumber].name;
  2596. std::transform(hlp.begin(), hlp.end(), hlp.begin(), (int(*)(int))toupper);
  2597. if(town1DefNames[mm]==hlp)
  2598. {
  2599. nxt.bytes[20] = mm;
  2600. }
  2601. }
  2602. }
  2603. }
  2604. }
  2605. int lvl = atoi(map.defy[CGI->objh->objInstances[j]->defNumber].name.substr(7, 8).c_str())-1;
  2606. nxt.name = creGenNames[nxt.bytes[20]][lvl];
  2607. if(creGenNumbers[nxt.bytes[20]][lvl]!=-1)
  2608. {
  2609. CGI->objh->objInstances[j]->defNumber = creGenNumbers[nxt.bytes[20]][lvl];
  2610. continue;
  2611. }
  2612. std::vector<DefObjInfo>::iterator pit = std::find(CGameInfo::mainObj->dobjinfo->objs.begin(), CGameInfo::mainObj->dobjinfo->objs.end(),
  2613. nxt.name);
  2614. if(pit == CGameInfo::mainObj->dobjinfo->objs.end())
  2615. {
  2616. nxt.isOnDefList = false;
  2617. }
  2618. else
  2619. {
  2620. nxt.printPriority = pit->priority;
  2621. nxt.isOnDefList = true;
  2622. }
  2623. map.defy.push_back(nxt); // add this def to the vector
  2624. defsToUnpack.push_back(nxt.name);
  2625. CGI->objh->objInstances[j]->defNumber = map.defy.size()-1;
  2626. if(creGenNumbers[nxt.bytes[20]][lvl]==-1)
  2627. {
  2628. creGenNumbers[nxt.bytes[20]][lvl] = map.defy.size()-1;
  2629. }
  2630. }
  2631. else //if not as castle
  2632. {
  2633. DefInfo nxt = curDef;
  2634. nxt.bytes[16] = 17;
  2635. std::vector<int> possibleTowns;
  2636. for(int bb=0; bb<8; ++bb)
  2637. {
  2638. if(((CCreGenObjInfo*)CGI->objh->objInstances[j]->info)->castles[0] & (1<<bb))
  2639. {
  2640. possibleTowns.push_back(bb);
  2641. }
  2642. }
  2643. if(((CCreGenObjInfo*)CGI->objh->objInstances[j]->info)->castles[1])
  2644. possibleTowns.push_back(8);
  2645. nxt.bytes[20] = possibleTowns[rand()%possibleTowns.size()];
  2646. int lvl = atoi(map.defy[CGI->objh->objInstances[j]->defNumber].name.substr(7, 8).c_str())-1;
  2647. nxt.name = creGenNames[nxt.bytes[20]][lvl];
  2648. if(creGenNumbers[nxt.bytes[20]][lvl]!=-1)
  2649. {
  2650. CGI->objh->objInstances[j]->defNumber = creGenNumbers[nxt.bytes[20]][lvl];
  2651. continue;
  2652. }
  2653. std::vector<DefObjInfo>::iterator pit = std::find(CGameInfo::mainObj->dobjinfo->objs.begin(), CGameInfo::mainObj->dobjinfo->objs.end(),
  2654. nxt.name);
  2655. if(pit == CGameInfo::mainObj->dobjinfo->objs.end())
  2656. {
  2657. nxt.isOnDefList = false;
  2658. }
  2659. else
  2660. {
  2661. nxt.printPriority = pit->priority;
  2662. nxt.isOnDefList = true;
  2663. }
  2664. map.defy.push_back(nxt); // add this def to the vector
  2665. defsToUnpack.push_back(nxt.name);
  2666. CGI->objh->objInstances[j]->defNumber = map.defy.size()-1;
  2667. if(creGenNumbers[nxt.bytes[20]][lvl]==-1)
  2668. {
  2669. creGenNumbers[nxt.bytes[20]][lvl] = map.defy.size()-1;
  2670. }
  2671. }
  2672. break;
  2673. }
  2674. case EDefType::CREGEN2_DEF:
  2675. {
  2676. if(((CCreGenObjInfo*)CGI->objh->objInstances[j]->info)->asCastle)
  2677. {
  2678. DefInfo nxt = curDef;
  2679. nxt.bytes[16] = 17;
  2680. for(int vv=0; vv<CGI->objh->objInstances.size(); ++vv)
  2681. {
  2682. if(getDefType(map.defy[CGI->objh->objInstances[vv]->defNumber])==EDefType::TOWN_DEF)
  2683. {
  2684. if(
  2685. ((CCastleObjInfo*)CGI->objh->objInstances[vv]->info)->bytes[0]==((CCreGen2ObjInfo*)CGI->objh->objInstances[j]->info)->bytes[0]
  2686. && ((CCastleObjInfo*)CGI->objh->objInstances[vv]->info)->bytes[1]==((CCreGen2ObjInfo*)CGI->objh->objInstances[j]->info)->bytes[1]
  2687. && ((CCastleObjInfo*)CGI->objh->objInstances[vv]->info)->bytes[2]==((CCreGen2ObjInfo*)CGI->objh->objInstances[j]->info)->bytes[2]
  2688. && ((CCastleObjInfo*)CGI->objh->objInstances[vv]->info)->bytes[3]==((CCreGen2ObjInfo*)CGI->objh->objInstances[j]->info)->bytes[3])
  2689. {
  2690. for(int mm=0; mm<town1DefNames.size(); ++mm)
  2691. {
  2692. std::transform(town1DefNames[mm].begin(), town1DefNames[mm].end(), town1DefNames[mm].begin(), (int(*)(int))toupper);
  2693. std::string hlp = map.defy[CGI->objh->objInstances[vv]->defNumber].name;
  2694. std::transform(hlp.begin(), hlp.end(), hlp.begin(), (int(*)(int))toupper);
  2695. if(town1DefNames[mm]==hlp)
  2696. {
  2697. nxt.bytes[20] = mm;
  2698. }
  2699. }
  2700. }
  2701. }
  2702. }
  2703. int lvl = rand()%(((CCreGen2ObjInfo*)CGI->objh->objInstances[j]->info)->maxLevel - ((CCreGen2ObjInfo*)CGI->objh->objInstances[j]->info)->minLevel) + ((CCreGen2ObjInfo*)CGI->objh->objInstances[j]->info)->minLevel;
  2704. nxt.name = creGenNames[nxt.bytes[20]][lvl];
  2705. if(creGenNumbers[nxt.bytes[20]][lvl]!=-1)
  2706. {
  2707. CGI->objh->objInstances[j]->defNumber = creGenNumbers[nxt.bytes[20]][lvl];
  2708. continue;
  2709. }
  2710. std::vector<DefObjInfo>::iterator pit = std::find(CGameInfo::mainObj->dobjinfo->objs.begin(), CGameInfo::mainObj->dobjinfo->objs.end(),
  2711. nxt.name);
  2712. if(pit == CGameInfo::mainObj->dobjinfo->objs.end())
  2713. {
  2714. nxt.isOnDefList = false;
  2715. }
  2716. else
  2717. {
  2718. nxt.printPriority = pit->priority;
  2719. nxt.isOnDefList = true;
  2720. }
  2721. map.defy.push_back(nxt); // add this def to the vector
  2722. defsToUnpack.push_back(nxt.name);
  2723. CGI->objh->objInstances[j]->defNumber = map.defy.size()-1;
  2724. if(creGenNumbers[nxt.bytes[20]][lvl]==-1)
  2725. {
  2726. creGenNumbers[nxt.bytes[20]][lvl] = map.defy.size()-1;
  2727. }
  2728. }
  2729. else //if not as castle
  2730. {
  2731. DefInfo nxt = curDef;
  2732. nxt.bytes[16] = 17;
  2733. std::vector<int> possibleTowns;
  2734. for(int bb=0; bb<8; ++bb)
  2735. {
  2736. if(((CCreGen2ObjInfo*)CGI->objh->objInstances[j]->info)->castles[0] & (1<<bb))
  2737. {
  2738. possibleTowns.push_back(bb);
  2739. }
  2740. }
  2741. if(((CCreGen2ObjInfo*)CGI->objh->objInstances[j]->info)->castles[1])
  2742. possibleTowns.push_back(8);
  2743. nxt.bytes[20] = possibleTowns[rand()%possibleTowns.size()];
  2744. int lvl = rand()%(((CCreGen2ObjInfo*)CGI->objh->objInstances[j]->info)->maxLevel - ((CCreGen2ObjInfo*)CGI->objh->objInstances[j]->info)->minLevel) + ((CCreGen2ObjInfo*)CGI->objh->objInstances[j]->info)->minLevel;
  2745. nxt.name = creGenNames[nxt.bytes[20]][lvl];
  2746. if(creGenNumbers[nxt.bytes[20]][lvl]!=-1)
  2747. {
  2748. CGI->objh->objInstances[j]->defNumber = creGenNumbers[nxt.bytes[20]][lvl];
  2749. continue;
  2750. }
  2751. std::vector<DefObjInfo>::iterator pit = std::find(CGameInfo::mainObj->dobjinfo->objs.begin(), CGameInfo::mainObj->dobjinfo->objs.end(),
  2752. nxt.name);
  2753. if(pit == CGameInfo::mainObj->dobjinfo->objs.end())
  2754. {
  2755. nxt.isOnDefList = false;
  2756. }
  2757. else
  2758. {
  2759. nxt.printPriority = pit->priority;
  2760. nxt.isOnDefList = true;
  2761. }
  2762. map.defy.push_back(nxt); // add this def to the vector
  2763. defsToUnpack.push_back(nxt.name);
  2764. CGI->objh->objInstances[j]->defNumber = map.defy.size()-1;
  2765. if(creGenNumbers[nxt.bytes[20]][lvl]==-1)
  2766. {
  2767. creGenNumbers[nxt.bytes[20]][lvl] = map.defy.size()-1;
  2768. }
  2769. }
  2770. }
  2771. case EDefType::CREGEN3_DEF:
  2772. {
  2773. DefInfo nxt = curDef;
  2774. nxt.bytes[16] = 17;
  2775. nxt.bytes[20] = atoi(map.defy[CGI->objh->objInstances[j]->defNumber].name.substr(7, 8).c_str());
  2776. int lvl = -1;
  2777. CCreGen3ObjInfo * ct = (CCreGen3ObjInfo*)CGI->objh->objInstances[j]->info;
  2778. if(ct->maxLevel>7)
  2779. ct->maxLevel = 7;
  2780. if(ct->minLevel<1)
  2781. ct->minLevel = 1;
  2782. if((((CCreGen3ObjInfo*)CGI->objh->objInstances[j]->info)->maxLevel - ((CCreGen3ObjInfo*)CGI->objh->objInstances[j]->info)->minLevel)!=0)
  2783. lvl = rand()%(((CCreGen3ObjInfo*)CGI->objh->objInstances[j]->info)->maxLevel - ((CCreGen3ObjInfo*)CGI->objh->objInstances[j]->info)->minLevel) + ((CCreGen3ObjInfo*)CGI->objh->objInstances[j]->info)->minLevel;
  2784. else
  2785. lvl = ((CCreGen3ObjInfo*)CGI->objh->objInstances[j]->info)->maxLevel;
  2786. nxt.name = creGenNames[nxt.bytes[20]][lvl];
  2787. if(creGenNumbers[nxt.bytes[20]][lvl]!=-1)
  2788. {
  2789. CGI->objh->objInstances[j]->defNumber = creGenNumbers[nxt.bytes[20]][lvl];
  2790. continue;
  2791. }
  2792. std::vector<DefObjInfo>::iterator pit = std::find(CGameInfo::mainObj->dobjinfo->objs.begin(), CGameInfo::mainObj->dobjinfo->objs.end(),
  2793. nxt.name);
  2794. if(pit == CGameInfo::mainObj->dobjinfo->objs.end())
  2795. {
  2796. nxt.isOnDefList = false;
  2797. }
  2798. else
  2799. {
  2800. nxt.printPriority = pit->priority;
  2801. nxt.isOnDefList = true;
  2802. }
  2803. map.defy.push_back(nxt); // add this def to the vector
  2804. defsToUnpack.push_back(nxt.name);
  2805. CGI->objh->objInstances[j]->defNumber = map.defy.size()-1;
  2806. if(creGenNumbers[nxt.bytes[20]][lvl]==-1)
  2807. {
  2808. creGenNumbers[nxt.bytes[20]][lvl] = map.defy.size()-1;
  2809. }
  2810. break;
  2811. }
  2812. }//end of main switch
  2813. } //end of sencond for loop
  2814. }