CAmbarCendamo.cpp 99 KB

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