CAmbarCendamo.cpp 93 KB

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