CAmbarCendamo.cpp 84 KB

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