CAmbarCendamo.cpp 85 KB

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