CAmbarCendamo.cpp 101 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483
  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. else
  847. {
  848. spec->artFeet = NULL;
  849. spec->artHead = NULL;
  850. spec->artLHand = NULL;
  851. spec->artLRing = NULL;
  852. spec->artMach1 = NULL;
  853. spec->artMach2 = NULL;
  854. spec->artMach3 = NULL;
  855. spec->artMach4 = NULL;
  856. spec->artMisc1 = NULL;
  857. spec->artMisc2 = NULL;
  858. spec->artMisc3 = NULL;
  859. spec->artMisc4 = NULL;
  860. spec->artMisc5 = NULL;
  861. spec->artNeck = NULL;
  862. spec->artRhand = NULL;
  863. spec->artRRing = NULL;
  864. spec->artShoulders = NULL;
  865. spec->artSpellBook = NULL;
  866. spec->artTorso = NULL;
  867. }
  868. spec->guardRange = readNormalNr(i, 1); ++i;
  869. if(spec->guardRange == 0xff)
  870. spec->isGuarding = false;
  871. else
  872. spec->isGuarding = true;
  873. if(map.version>RoE)
  874. {
  875. bool hasBiography = bufor[i]; ++i; //true if hero has nonstandard (mapmaker defined) biography
  876. if(hasBiography)
  877. {
  878. int length = readNormalNr(i); i+=4;
  879. int iStart = i;
  880. i+=length;
  881. for(int bb=0; bb<length; ++bb)
  882. {
  883. spec->biography+=bufor[iStart+bb];
  884. }
  885. }
  886. spec->sex = !(bufor[i]); ++i;
  887. }
  888. //spells
  889. if(map.version>AB)
  890. {
  891. bool areSpells = bufor[i]; ++i;
  892. if(areSpells) //TODO: sprawdziæ //seems to be ok - tow
  893. {
  894. int ist = i;
  895. for(i; i<ist+9; ++i)
  896. {
  897. unsigned char c = bufor[i];
  898. for(int yy=0; yy<8; ++yy)
  899. {
  900. if((i-ist)*8+yy < CGameInfo::mainObj->spellh->spells.size())
  901. {
  902. if(c == (c|((unsigned char)intPow(2, yy))))
  903. spec->spells.push_back(&(CGameInfo::mainObj->spellh->spells[(i-ist)*8+yy]));
  904. }
  905. }
  906. }
  907. }
  908. }
  909. else if(map.version==AB) //we can read one spell
  910. {
  911. unsigned char buff = bufor[i]; ++i;
  912. if(buff!=254)
  913. {
  914. spec->spells.push_back(&(CGameInfo::mainObj->spellh->spells[buff]));
  915. }
  916. }
  917. //spells loaded
  918. if(map.version>AB)
  919. {
  920. spec->defaultMainStats = bufor[i]; ++i;
  921. if(spec->defaultMainStats)
  922. {
  923. spec->attack = bufor[i]; ++i;
  924. spec->defence = bufor[i]; ++i;
  925. spec->power = bufor[i]; ++i;
  926. spec->knowledge = bufor[i]; ++i;
  927. }
  928. else
  929. {
  930. spec->attack = -1;
  931. spec->defence = -1;
  932. spec->power = -1;
  933. spec->knowledge = -1;
  934. }
  935. }
  936. i+=16;
  937. nobj->info = spec;
  938. //////creating CHeroInstance
  939. CGHeroInstance * nhi = new CGHeroInstance;
  940. (*(static_cast<CGObjectInstance*>(nhi))) = *nobj;
  941. delete nobj;
  942. nobj=nhi;
  943. spec->myInstance = nhi;
  944. //nobj->isHero = true;
  945. (static_cast<CGHeroInstance*>(nobj))->moveDir = 4;
  946. nhi->isStanding = true;
  947. nhi->exp = spec->experience;
  948. nhi->level = CGI->heroh->level(nhi->exp);
  949. nhi->primSkills.resize(PRIMARY_SKILLS);
  950. nhi->primSkills[0] = spec->attack;
  951. nhi->primSkills[1] = spec->defence;
  952. nhi->primSkills[2] = spec->power;
  953. nhi->primSkills[3] = spec->knowledge;
  954. nhi->mana = spec->knowledge * 10;
  955. nhi->movement = -1;
  956. nhi->name = spec->name;
  957. nhi->setOwner(spec->player);
  958. nhi->pos = nobj->pos;
  959. nhi->type = spec->type;
  960. nhi->army = spec->garrison;
  961. nhi->portrait = -1; // TODO: przypisywac portret
  962. nhi->artFeet = spec->artFeet;
  963. nhi->artHead = spec->artHead;
  964. nhi->artifacts = spec->artifacts;
  965. nhi->artLHand = spec->artLHand;
  966. nhi->artLRing = spec->artLRing;
  967. nhi->artMach1 = spec->artMach1;
  968. nhi->artMach2 = spec->artMach2;
  969. nhi->artMach3 = spec->artMach3;
  970. nhi->artMach4 = spec->artMach4 = new CArtifact(CGI->arth->artifacts[3]);
  971. nhi->artMisc1 = spec->artMisc1;
  972. nhi->artMisc2 = spec->artMisc2;
  973. nhi->artMisc3 = spec->artMisc3;
  974. nhi->artMisc4 = spec->artMisc4;
  975. nhi->artMisc5 = spec->artMisc5;
  976. nhi->artNeck = spec->artNeck;
  977. nhi->artRhand = spec->artRhand;
  978. nhi->artRRing = spec->artRRing;
  979. nhi->artShoulders = spec->artShoulders;
  980. nhi->artSpellBook = spec->artSpellBook;
  981. nhi->artTorso = spec->artTorso;
  982. for(int qq=0; qq<spec->abilities.size(); ++qq)
  983. {
  984. nhi->secSkills.push_back(std::make_pair(spec->abilities[qq]->idNumber, spec->abilityLevels[qq]-1));
  985. }
  986. CGI->heroh->heroInstances.push_back(nhi);
  987. break;
  988. }
  989. case CREATURES_DEF:
  990. {
  991. CCreatureObjInfo * spec = new CCreatureObjInfo;
  992. if(map.version>RoE)
  993. {
  994. spec->bytes[0] = bufor[i]; ++i;
  995. spec->bytes[1] = bufor[i]; ++i;
  996. spec->bytes[2] = bufor[i]; ++i;
  997. spec->bytes[3] = bufor[i]; ++i;
  998. }
  999. spec->number = readNormalNr(i, 2); i+=2;
  1000. spec->character = bufor[i]; ++i;
  1001. bool isMesTre = bufor[i]; ++i; //true if there is message or treasury
  1002. if(isMesTre)
  1003. {
  1004. int messLength = readNormalNr(i); i+=4;
  1005. if(messLength>0)
  1006. {
  1007. for(int tt=0; tt<messLength; ++tt)
  1008. {
  1009. spec->message += bufor[i]; ++i;
  1010. }
  1011. }
  1012. spec->wood = readNormalNr(i); i+=4;
  1013. spec->mercury = readNormalNr(i); i+=4;
  1014. spec->ore = readNormalNr(i); i+=4;
  1015. spec->sulfur = readNormalNr(i); i+=4;
  1016. spec->crytal = readNormalNr(i); i+=4;
  1017. spec->gems = readNormalNr(i); i+=4;
  1018. spec->gold = readNormalNr(i); i+=4;
  1019. int artID = readNormalNr(i, (map.version == RoE ? 1 : 2)); i+=(map.version == RoE ? 1 : 2);
  1020. if(map.version==RoE)
  1021. {
  1022. if(artID!=0xff)
  1023. spec->gainedArtifact = &(CGameInfo::mainObj->arth->artifacts[artID]);
  1024. else
  1025. spec->gainedArtifact = NULL;
  1026. }
  1027. else
  1028. {
  1029. if(artID!=0xffff)
  1030. spec->gainedArtifact = &(CGameInfo::mainObj->arth->artifacts[artID]);
  1031. else
  1032. spec->gainedArtifact = NULL;
  1033. }
  1034. }
  1035. spec->neverFlees = bufor[i]; ++i;
  1036. spec->notGrowingTeam = bufor[i]; ++i;
  1037. i+=2;
  1038. nobj->info = spec;
  1039. break;
  1040. }
  1041. case EDefType::SIGN_DEF:
  1042. {
  1043. CSignObjInfo * spec = new CSignObjInfo;
  1044. int length = readNormalNr(i); i+=4;
  1045. for(int rr=0; rr<length; ++rr)
  1046. {
  1047. spec->message += bufor[i]; ++i;
  1048. }
  1049. i+=4;
  1050. nobj->info = spec;
  1051. break;
  1052. }
  1053. case EDefType::SEERHUT_DEF:
  1054. {
  1055. CSeerHutObjInfo * spec = new CSeerHutObjInfo;
  1056. if(map.version>RoE)
  1057. {
  1058. spec->missionType = bufor[i]; ++i;
  1059. switch(spec->missionType)
  1060. {
  1061. case 0:
  1062. i+=3;
  1063. continue;
  1064. case 1:
  1065. {
  1066. spec->m1level = readNormalNr(i); i+=4;
  1067. int limit = readNormalNr(i); i+=4;
  1068. if(limit == ((int)0xffffffff))
  1069. {
  1070. spec->isDayLimit = false;
  1071. spec->lastDay = -1;
  1072. }
  1073. else
  1074. {
  1075. spec->isDayLimit = true;
  1076. spec->lastDay = limit;
  1077. }
  1078. break;
  1079. }
  1080. case 2:
  1081. {
  1082. spec->m2attack = bufor[i]; ++i;
  1083. spec->m2defence = bufor[i]; ++i;
  1084. spec->m2power = bufor[i]; ++i;
  1085. spec->m2knowledge = bufor[i]; ++i;
  1086. int limit = readNormalNr(i); i+=4;
  1087. if(limit == ((int)0xffffffff))
  1088. {
  1089. spec->isDayLimit = false;
  1090. spec->lastDay = -1;
  1091. }
  1092. else
  1093. {
  1094. spec->isDayLimit = true;
  1095. spec->lastDay = limit;
  1096. }
  1097. break;
  1098. }
  1099. case 3:
  1100. {
  1101. spec->m3bytes[0] = bufor[i]; ++i;
  1102. spec->m3bytes[1] = bufor[i]; ++i;
  1103. spec->m3bytes[2] = bufor[i]; ++i;
  1104. spec->m3bytes[3] = bufor[i]; ++i;
  1105. int limit = readNormalNr(i); i+=4;
  1106. if(limit == ((int)0xffffffff))
  1107. {
  1108. spec->isDayLimit = false;
  1109. spec->lastDay = -1;
  1110. }
  1111. else
  1112. {
  1113. spec->isDayLimit = true;
  1114. spec->lastDay = limit;
  1115. }
  1116. break;
  1117. }
  1118. case 4:
  1119. {
  1120. spec->m4bytes[0] = bufor[i]; ++i;
  1121. spec->m4bytes[1] = bufor[i]; ++i;
  1122. spec->m4bytes[2] = bufor[i]; ++i;
  1123. spec->m4bytes[3] = bufor[i]; ++i;
  1124. int limit = readNormalNr(i); i+=4;
  1125. if(limit == ((int)0xffffffff))
  1126. {
  1127. spec->isDayLimit = false;
  1128. spec->lastDay = -1;
  1129. }
  1130. else
  1131. {
  1132. spec->isDayLimit = true;
  1133. spec->lastDay = limit;
  1134. }
  1135. break;
  1136. }
  1137. case 5:
  1138. {
  1139. int artNumber = bufor[i]; ++i;
  1140. for(int yy=0; yy<artNumber; ++yy)
  1141. {
  1142. int artid = readNormalNr(i, 2); i+=2;
  1143. spec->m5arts.push_back(&(CGameInfo::mainObj->arth->artifacts[artid]));
  1144. }
  1145. int limit = readNormalNr(i); i+=4;
  1146. if(limit == ((int)0xffffffff))
  1147. {
  1148. spec->isDayLimit = false;
  1149. spec->lastDay = -1;
  1150. }
  1151. else
  1152. {
  1153. spec->isDayLimit = true;
  1154. spec->lastDay = limit;
  1155. }
  1156. break;
  1157. }
  1158. case 6:
  1159. {
  1160. int typeNumber = bufor[i]; ++i;
  1161. for(int hh=0; hh<typeNumber; ++hh)
  1162. {
  1163. int creType = readNormalNr(i, 2); i+=2;
  1164. int creNumb = readNormalNr(i, 2); i+=2;
  1165. spec->m6cre.push_back(&(CGameInfo::mainObj->creh->creatures[creType]));
  1166. spec->m6number.push_back(creNumb);
  1167. }
  1168. int limit = readNormalNr(i); i+=4;
  1169. if(limit == ((int)0xffffffff))
  1170. {
  1171. spec->isDayLimit = false;
  1172. spec->lastDay = -1;
  1173. }
  1174. else
  1175. {
  1176. spec->isDayLimit = true;
  1177. spec->lastDay = limit;
  1178. }
  1179. break;
  1180. }
  1181. case 7:
  1182. {
  1183. spec->m7wood = readNormalNr(i); i+=4;
  1184. spec->m7mercury = readNormalNr(i); i+=4;
  1185. spec->m7ore = readNormalNr(i); i+=4;
  1186. spec->m7sulfur = readNormalNr(i); i+=4;
  1187. spec->m7crystal = readNormalNr(i); i+=4;
  1188. spec->m7gems = readNormalNr(i); i+=4;
  1189. spec->m7gold = readNormalNr(i); i+=4;
  1190. int limit = readNormalNr(i); i+=4;
  1191. if(limit == ((int)0xffffffff))
  1192. {
  1193. spec->isDayLimit = false;
  1194. spec->lastDay = -1;
  1195. }
  1196. else
  1197. {
  1198. spec->isDayLimit = true;
  1199. spec->lastDay = limit;
  1200. }
  1201. break;
  1202. }
  1203. case 8:
  1204. {
  1205. int heroType = bufor[i]; ++i;
  1206. spec->m8hero = CGameInfo::mainObj->heroh->heroes[heroType];
  1207. int limit = readNormalNr(i); i+=4;
  1208. if(limit == ((int)0xffffffff))
  1209. {
  1210. spec->isDayLimit = false;
  1211. spec->lastDay = -1;
  1212. }
  1213. else
  1214. {
  1215. spec->isDayLimit = true;
  1216. spec->lastDay = limit;
  1217. }
  1218. break;
  1219. }
  1220. case 9:
  1221. {
  1222. spec->m9player = bufor[i]; ++i;
  1223. int limit = readNormalNr(i); i+=4;
  1224. if(limit == ((int)0xffffffff))
  1225. {
  1226. spec->isDayLimit = false;
  1227. spec->lastDay = -1;
  1228. }
  1229. else
  1230. {
  1231. spec->isDayLimit = true;
  1232. spec->lastDay = limit;
  1233. }
  1234. break;
  1235. }
  1236. }//internal switch end (seer huts)
  1237. int len1 = readNormalNr(i); i+=4;
  1238. for(int ee=0; ee<len1; ++ee)
  1239. {
  1240. spec->firstVisitText += bufor[i]; ++i;
  1241. }
  1242. int len2 = readNormalNr(i); i+=4;
  1243. for(int ee=0; ee<len2; ++ee)
  1244. {
  1245. spec->nextVisitText += bufor[i]; ++i;
  1246. }
  1247. int len3 = readNormalNr(i); i+=4;
  1248. for(int ee=0; ee<len3; ++ee)
  1249. {
  1250. spec->completedText += bufor[i]; ++i;
  1251. }
  1252. }
  1253. else //RoE
  1254. {
  1255. int artID = bufor[i]; ++i;
  1256. if(artID!=255) //not none quest
  1257. {
  1258. spec->m5arts.push_back(&(CGameInfo::mainObj->arth->artifacts[artID]));
  1259. spec->missionType = 5;
  1260. }
  1261. else
  1262. {
  1263. spec->missionType = 255;
  1264. }
  1265. }
  1266. if(spec->missionType!=255)
  1267. {
  1268. unsigned char rewardType = bufor[i]; ++i;
  1269. spec->rewardType = rewardType;
  1270. switch(rewardType)
  1271. {
  1272. case 1:
  1273. {
  1274. spec->r1exp = readNormalNr(i); i+=4;
  1275. break;
  1276. }
  1277. case 2:
  1278. {
  1279. spec->r2mana = readNormalNr(i); i+=4;
  1280. break;
  1281. }
  1282. case 3:
  1283. {
  1284. spec->r3morale = bufor[i]; ++i;
  1285. break;
  1286. }
  1287. case 4:
  1288. {
  1289. spec->r4luck = bufor[i]; ++i;
  1290. break;
  1291. }
  1292. case 5:
  1293. {
  1294. spec->r5type = bufor[i]; ++i;
  1295. spec->r5amount = readNormalNr(i, 3); i+=3;
  1296. i+=1;
  1297. break;
  1298. }
  1299. case 6:
  1300. {
  1301. spec->r6type = bufor[i]; ++i;
  1302. spec->r6amount = bufor[i]; ++i;
  1303. break;
  1304. }
  1305. case 7:
  1306. {
  1307. int abid = bufor[i]; ++i;
  1308. spec->r7ability = CGameInfo::mainObj->abilh->abilities[abid];
  1309. spec->r7level = bufor[i]; ++i;
  1310. break;
  1311. }
  1312. case 8:
  1313. {
  1314. int artid = readNormalNr(i, (map.version == RoE ? 1 : 2)); i+=(map.version == RoE ? 1 : 2);
  1315. spec->r8art = &(CGameInfo::mainObj->arth->artifacts[artid]);
  1316. break;
  1317. }
  1318. case 9:
  1319. {
  1320. int spellid = bufor[i]; ++i;
  1321. spec->r9spell = &(CGameInfo::mainObj->spellh->spells[spellid]);
  1322. break;
  1323. }
  1324. case 10:
  1325. {
  1326. int creid = readNormalNr(i, 2); i+=2;
  1327. spec->r10creature = &(CGameInfo::mainObj->creh->creatures[creid]);
  1328. spec->r10amount = readNormalNr(i, 2); i+=2;
  1329. break;
  1330. }
  1331. }// end of internal switch
  1332. i+=2;
  1333. }
  1334. else //missionType==255
  1335. {
  1336. i+=3;
  1337. }
  1338. nobj->info = spec;
  1339. break;
  1340. }
  1341. case EDefType::WITCHHUT_DEF:
  1342. {
  1343. CWitchHutObjInfo * spec = new CWitchHutObjInfo;
  1344. if(map.version>RoE) //in reo we cannot specify it - all are allowed (I hope)
  1345. {
  1346. ist=i; //starting i for loop
  1347. for(i; i<ist+4; ++i)
  1348. {
  1349. unsigned char c = bufor[i];
  1350. for(int yy=0; yy<8; ++yy)
  1351. {
  1352. if((i-ist)*8+yy < CGameInfo::mainObj->abilh->abilities.size())
  1353. {
  1354. if(c == (c|((unsigned char)intPow(2, yy))))
  1355. spec->allowedAbilities.push_back(CGameInfo::mainObj->abilh->abilities[(i-ist)*8+yy]);
  1356. }
  1357. }
  1358. }
  1359. }
  1360. else //(RoE map)
  1361. {
  1362. for(int gg=0; gg<CGameInfo::mainObj->abilh->abilities.size(); ++gg)
  1363. {
  1364. spec->allowedAbilities.push_back(CGameInfo::mainObj->abilh->abilities[gg]);
  1365. }
  1366. }
  1367. nobj->info = spec;
  1368. break;
  1369. }
  1370. case EDefType::SCHOLAR_DEF:
  1371. {
  1372. CScholarObjInfo * spec = new CScholarObjInfo;
  1373. spec->bonusType = bufor[i]; ++i;
  1374. switch(spec->bonusType)
  1375. {
  1376. case 0xff:
  1377. ++i;
  1378. break;
  1379. case 0:
  1380. spec->r0type = bufor[i]; ++i;
  1381. break;
  1382. case 1:
  1383. spec->r1 = CGameInfo::mainObj->abilh->abilities[bufor[i]]; ++i;
  1384. break;
  1385. case 2:
  1386. spec->r2 = &(CGameInfo::mainObj->spellh->spells[bufor[i]]); ++i;
  1387. break;
  1388. }
  1389. i+=6;
  1390. nobj->info = spec;
  1391. break;
  1392. }
  1393. case EDefType::GARRISON_DEF:
  1394. {
  1395. CGarrisonObjInfo * spec = new CGarrisonObjInfo;
  1396. spec->player = bufor[i]; ++i;
  1397. i+=3;
  1398. spec->units = readCreatureSet(i); i+= (map.version==RoE ? 21 : 28);
  1399. if(map.version > RoE)
  1400. {
  1401. spec->movableUnits = bufor[i]; ++i;
  1402. }
  1403. else
  1404. spec->movableUnits = true;
  1405. i+=8;
  1406. nobj->setOwner(spec->player);
  1407. nobj->info = spec;
  1408. break;
  1409. }
  1410. case EDefType::ARTIFACT_DEF:
  1411. {
  1412. CArtifactObjInfo * spec = new CArtifactObjInfo;
  1413. bool areSettings = bufor[i]; ++i;
  1414. if(areSettings)
  1415. {
  1416. int messLength = readNormalNr(i, 4); i+=4;
  1417. for(int hh=0; hh<messLength; ++hh)
  1418. {
  1419. spec->message += bufor[i]; ++i;
  1420. }
  1421. bool areGuards = bufor[i]; ++i;
  1422. if(areGuards)
  1423. {
  1424. spec->areGuards = true;
  1425. spec->guards = readCreatureSet(i); i+= (map.version == RoE ? 21 : 28) ;
  1426. }
  1427. else
  1428. spec->areGuards = false;
  1429. i+=4;
  1430. }
  1431. nobj->info = spec;
  1432. break;
  1433. }
  1434. case EDefType::RESOURCE_DEF:
  1435. {
  1436. CResourceObjInfo * spec = new CResourceObjInfo;
  1437. bool isMessGuard = bufor[i]; ++i;
  1438. if(isMessGuard)
  1439. {
  1440. int messLength = readNormalNr(i); i+=4;
  1441. for(int mm=0; mm<messLength; ++mm)
  1442. {
  1443. spec->message+=bufor[i]; ++i;
  1444. }
  1445. spec->areGuards = bufor[i]; ++i;
  1446. if(spec->areGuards)
  1447. {
  1448. spec->guards = readCreatureSet(i); i+= (map.version == RoE ? 21 : 28);
  1449. }
  1450. i+=4;
  1451. }
  1452. else
  1453. {
  1454. spec->areGuards = false;
  1455. }
  1456. spec->amount = readNormalNr(i); i+=4;
  1457. i+=4;
  1458. nobj->info = spec;
  1459. break;
  1460. }
  1461. case EDefType::TOWN_DEF:
  1462. {
  1463. CCastleObjInfo * spec = new CCastleObjInfo;
  1464. if(map.version!=RoE)
  1465. {
  1466. spec->bytes[0] = bufor[i]; ++i;
  1467. spec->bytes[1] = bufor[i]; ++i;
  1468. spec->bytes[2] = bufor[i]; ++i;
  1469. spec->bytes[3] = bufor[i]; ++i;
  1470. }
  1471. else
  1472. {
  1473. spec->bytes[0] = 0;
  1474. spec->bytes[1] = 0;
  1475. spec->bytes[2] = 0;
  1476. spec->bytes[3] = 0;
  1477. }
  1478. spec->player = bufor[i]; ++i;
  1479. bool hasName = bufor[i]; ++i;
  1480. if(hasName)
  1481. {
  1482. int len = readNormalNr(i); i+=4;
  1483. for(int gg=0; gg<len; ++gg)
  1484. {
  1485. spec->name += bufor[i]; ++i;
  1486. }
  1487. }
  1488. bool stGarr = bufor[i]; ++i; //true if garrison isn't empty
  1489. if(stGarr)
  1490. {
  1491. spec->garrison = readCreatureSet(i); i+=( map.version > RoE ? 28 : 21 );
  1492. }
  1493. spec->garrison.formation = bufor[i]; ++i;
  1494. spec->unusualBuildins = bufor[i]; ++i;
  1495. if(spec->unusualBuildins)
  1496. {
  1497. for(int ff=0; ff<12; ++ff)
  1498. {
  1499. spec->buildingSettings[ff] = bufor[i]; ++i;
  1500. }
  1501. }
  1502. else
  1503. {
  1504. spec->hasFort = bufor[i]; ++i;
  1505. }
  1506. int ist = i;
  1507. if(map.version>RoE)
  1508. {
  1509. for(i; i<ist+9; ++i)
  1510. {
  1511. unsigned char c = bufor[i];
  1512. for(int yy=0; yy<8; ++yy)
  1513. {
  1514. if((i-ist)*8+yy < CGameInfo::mainObj->spellh->spells.size())
  1515. {
  1516. if(c == (c|((unsigned char)intPow(2, yy))))
  1517. spec->obligatorySpells.push_back(&(CGameInfo::mainObj->spellh->spells[(i-ist)*8+yy]));
  1518. }
  1519. }
  1520. }
  1521. }
  1522. ist = i;
  1523. for(i; i<ist+9; ++i)
  1524. {
  1525. unsigned char c = bufor[i];
  1526. for(int yy=0; yy<8; ++yy)
  1527. {
  1528. if((i-ist)*8+yy < CGameInfo::mainObj->spellh->spells.size())
  1529. {
  1530. if(c != (c|((unsigned char)intPow(2, yy))))
  1531. spec->possibleSpells.push_back(&(CGameInfo::mainObj->spellh->spells[(i-ist)*8+yy]));
  1532. }
  1533. }
  1534. }
  1535. /////// reading castle events //////////////////////////////////
  1536. int numberOfEvent = readNormalNr(i); i+=4;
  1537. for(int gh = 0; gh<numberOfEvent; ++gh)
  1538. {
  1539. CCastleEvent nce;
  1540. int nameLen = readNormalNr(i); i+=4;
  1541. for(int ll=0; ll<nameLen; ++ll)
  1542. {
  1543. nce.name += bufor[i]; ++i;
  1544. }
  1545. int messLen = readNormalNr(i); i+=4;
  1546. for(int ll=0; ll<messLen; ++ll)
  1547. {
  1548. nce.message += bufor[i]; ++i;
  1549. }
  1550. nce.wood = readNormalNr(i); i+=4;
  1551. nce.mercury = readNormalNr(i); i+=4;
  1552. nce.ore = readNormalNr(i); i+=4;
  1553. nce.sulfur = readNormalNr(i); i+=4;
  1554. nce.crystal = readNormalNr(i); i+=4;
  1555. nce.gems = readNormalNr(i); i+=4;
  1556. nce.gold = readNormalNr(i); i+=4;
  1557. nce.players = bufor[i]; ++i;
  1558. if(map.version > AB)
  1559. {
  1560. nce.forHuman = bufor[i]; ++i;
  1561. }
  1562. else
  1563. nce.forHuman = true;
  1564. nce.forComputer = bufor[i]; ++i;
  1565. nce.firstShow = readNormalNr(i, 2); i+=2;
  1566. nce.forEvery = bufor[i]; ++i;
  1567. i+=17;
  1568. for(int kk=0; kk<6; ++kk)
  1569. {
  1570. nce.bytes[kk] = bufor[i]; ++i;
  1571. }
  1572. for(int vv=0; vv<7; ++vv)
  1573. {
  1574. nce.gen[vv] = readNormalNr(i, 2); i+=2;
  1575. }
  1576. i+=4;
  1577. spec->events.push_back(nce);
  1578. }
  1579. spec->x = nobj->pos.x;
  1580. spec->y = nobj->pos.y;
  1581. spec->z = nobj->pos.z;
  1582. /////// castle events have been read ///////////////////////////
  1583. if(map.version > AB)
  1584. {
  1585. spec->alignment = bufor[i]; ++i;
  1586. }
  1587. else
  1588. spec->alignment = 0xff;
  1589. i+=3;
  1590. nobj->info = spec;
  1591. //////////// rewriting info to CTownInstance class /////////////////////
  1592. CGTownInstance * nt = new CGTownInstance();
  1593. (*(static_cast<CGObjectInstance*>(nt))) = *nobj;
  1594. delete nobj;
  1595. nobj = nt;
  1596. if(spec->unusualBuildins)
  1597. {
  1598. nt->builtBuildings.insert(10);
  1599. for(int ir = 0; ir < 6; ir++)
  1600. {
  1601. for(int bs=0;bs<8;bs++)
  1602. {
  1603. if(ir==0)
  1604. {
  1605. if (bs<3)
  1606. {
  1607. if(spec->buildingSettings[ir] & (1<<bs))
  1608. {
  1609. nt->builtBuildings.insert(11+bs);
  1610. }
  1611. }
  1612. else if (bs<6)
  1613. {
  1614. if(spec->buildingSettings[ir] & (1<<bs))
  1615. {
  1616. nt->builtBuildings.insert(7+bs-3);
  1617. }
  1618. }
  1619. else if(bs==6)
  1620. {
  1621. if(spec->buildingSettings[ir] & (1<<bs))
  1622. {
  1623. nt->builtBuildings.insert(5);
  1624. }
  1625. }
  1626. else// if(bs==7)
  1627. {
  1628. if(spec->buildingSettings[ir] & (1<<bs))
  1629. {
  1630. nt->builtBuildings.insert(16);
  1631. }
  1632. }
  1633. } //if(ir==0)
  1634. else if(ir==1)
  1635. {
  1636. if(bs<2)
  1637. {
  1638. if(spec->buildingSettings[ir] & (1<<bs))
  1639. {
  1640. nt->builtBuildings.insert(14+bs);
  1641. }
  1642. }
  1643. else if (bs==2)
  1644. {
  1645. if(spec->buildingSettings[ir] & (1<<bs))
  1646. {
  1647. std::cout<<"Hej, sprawdz co to za budynek w miescie " <<nt<<std::endl;
  1648. }
  1649. }//bs==3 - not known what it is, 4 in 2. byte
  1650. else
  1651. {
  1652. if(spec->buildingSettings[ir] & (1<<bs))
  1653. {
  1654. nt->builtBuildings.insert(0+bs-3);
  1655. }
  1656. }
  1657. }//else if(ir==1)
  1658. else if(ir==2)
  1659. {
  1660. if(bs==0)
  1661. {
  1662. if(spec->buildingSettings[ir] & (1<<bs))
  1663. {
  1664. nt->builtBuildings.insert(6); //stocznia
  1665. }
  1666. }
  1667. else if(bs==1)
  1668. {
  1669. if(spec->buildingSettings[ir] & (1<<bs))
  1670. {
  1671. nt->builtBuildings.insert(26); //grail
  1672. }
  1673. }
  1674. else if(bs==2)
  1675. {
  1676. if(spec->buildingSettings[ir] & (1<<bs))
  1677. {
  1678. nt->builtBuildings.insert(17); //latarnia
  1679. }
  1680. }
  1681. else if(bs==3)
  1682. {
  1683. if(spec->buildingSettings[ir] & (1<<bs))
  1684. {
  1685. nt->builtBuildings.insert(22); //bractwo miecza
  1686. }
  1687. }
  1688. else if(bs==4)
  1689. {
  1690. if(spec->buildingSettings[ir] & (1<<bs))
  1691. {
  1692. nt->builtBuildings.insert(21); //stables
  1693. }
  1694. }
  1695. else if(bs==5)
  1696. {
  1697. if(spec->buildingSettings[ir] & (1<<bs))
  1698. {
  1699. std::cout<<"Hej, sprawdz co to za budynek2 w miescie " <<nt<<std::endl;
  1700. }
  1701. }
  1702. else if(bs==6)
  1703. {
  1704. if(spec->buildingSettings[ir] & (1<<bs))
  1705. {
  1706. nt->builtBuildings.insert(30); //gen1
  1707. }
  1708. }
  1709. else if(bs==7)
  1710. {
  1711. if(spec->buildingSettings[ir] & (1<<bs))
  1712. {
  1713. nt->builtBuildings.insert(37); //gen1+
  1714. }
  1715. }
  1716. }//else if(ir==2)
  1717. else if (ir==3)
  1718. {
  1719. if(bs==0)
  1720. {
  1721. if(spec->buildingSettings[ir] & (1<<bs))
  1722. {
  1723. std::cout<<"Hej, sprawdz co to za budynek3 w miescie " <<nt<<std::endl;
  1724. }
  1725. continue;
  1726. }
  1727. else if(bs<3)
  1728. {
  1729. if(bs==1)
  1730. {
  1731. if(spec->buildingSettings[ir] & (1<<bs))
  1732. {
  1733. nt->builtBuildings.insert(31); //gen2
  1734. }
  1735. }
  1736. else
  1737. {
  1738. if(spec->buildingSettings[ir] & (1<<bs))
  1739. {
  1740. nt->builtBuildings.insert(38); //gen2+
  1741. }
  1742. }
  1743. }
  1744. else if (bs==3)
  1745. {
  1746. if(spec->buildingSettings[ir] & (1<<bs))
  1747. {
  1748. std::cout<<"Hej, sprawdz co to za budynek4 w miescie " <<nt<<std::endl;
  1749. }
  1750. continue;
  1751. }
  1752. else
  1753. {
  1754. if(bs%2) //nieulepszone
  1755. {
  1756. if(spec->buildingSettings[ir] & (1<<bs))
  1757. {
  1758. nt->builtBuildings.insert((int)(39+(bs/2)-2));
  1759. }
  1760. }
  1761. else
  1762. {
  1763. if(spec->buildingSettings[ir] & (1<<bs))
  1764. {
  1765. nt->builtBuildings.insert(32+(bs/2)-2);
  1766. }
  1767. }
  1768. }
  1769. }//else if (ir==3)
  1770. else if (ir==4 && bs==0)
  1771. {
  1772. if(spec->buildingSettings[ir] & 1<<0)
  1773. nt->builtBuildings.insert(40);
  1774. if(spec->buildingSettings[ir] & 1<<2)
  1775. nt->builtBuildings.insert(34);
  1776. if(spec->buildingSettings[ir] & 1<<3)
  1777. nt->builtBuildings.insert(41);
  1778. if(spec->buildingSettings[ir] & 1<<5)
  1779. nt->builtBuildings.insert(35);
  1780. if(spec->buildingSettings[ir] & 1<<6)
  1781. nt->builtBuildings.insert(42);
  1782. if(spec->buildingSettings[ir] & 1<<7)
  1783. nt->builtBuildings.insert(36);
  1784. }//else if (ir==4)
  1785. else if (ir==5)
  1786. {
  1787. if(bs==0)
  1788. {
  1789. if(spec->buildingSettings[ir] & (1<<bs))
  1790. {
  1791. nt->builtBuildings.insert(43); //gen7+
  1792. }
  1793. }
  1794. }//else if (ir==5)
  1795. }
  1796. }
  1797. //testowe zczytywanie h3mowych ID
  1798. for(int byte=0;byte<6;byte++)
  1799. {
  1800. for(int bit=0;bit<8;bit++)
  1801. {
  1802. if(spec->buildingSettings[byte] & (1<<bit))
  1803. {
  1804. nt->h3mbuildings.insert(byte*8+bit);
  1805. }
  1806. }
  1807. }
  1808. }
  1809. else
  1810. {
  1811. if(spec->hasFort)
  1812. {
  1813. nt->builtBuildings.insert(7);
  1814. }
  1815. }
  1816. nt->setOwner(spec->player);
  1817. nt->town = &CGI->townh->towns[nt->defInfo->subid];
  1818. nt->builded = 0;
  1819. nt->destroyed = 0;
  1820. nt->name = spec->name;
  1821. nt->garrison = spec->garrison;
  1822. nt->garrisonHero = NULL;// spec->garnisonHero is not readed - TODO: readit
  1823. nt->pos = int3(spec->x, spec->y, spec->z);
  1824. nt->possibleSpells = spec->possibleSpells;
  1825. nt->obligatorySpells = spec->obligatorySpells;
  1826. nt->availableSpells = std::vector<CSpell*>();
  1827. nt->creatureIncome = std::vector<int>();
  1828. nt->creaturesLeft = std::vector<int>();
  1829. CGI->townh->townInstances.push_back(nt);
  1830. break;
  1831. }
  1832. case EDefType::PLAYERONLY_DEF:
  1833. {
  1834. CPlayerOnlyObjInfo * spec = new CPlayerOnlyObjInfo;
  1835. spec->player = bufor[i]; ++i;
  1836. i+=3;
  1837. nobj->setOwner(spec->player);
  1838. nobj->info = spec;
  1839. break;
  1840. }
  1841. case EDefType::SHRINE_DEF:
  1842. {
  1843. CShrineObjInfo * spec = new CShrineObjInfo;
  1844. spec->spell = bufor[i]; i+=4;
  1845. nobj->info = spec;
  1846. break;
  1847. }
  1848. case EDefType::SPELLSCROLL_DEF:
  1849. {
  1850. CSpellScrollObjinfo * spec = new CSpellScrollObjinfo;
  1851. bool messg = bufor[i]; ++i;
  1852. if(messg)
  1853. {
  1854. int mLength = readNormalNr(i); i+=4;
  1855. for(int vv=0; vv<mLength; ++vv)
  1856. {
  1857. spec->message += bufor[i]; ++i;
  1858. }
  1859. spec->areGuarders = bufor[i]; ++i;
  1860. if(spec->areGuarders)
  1861. {
  1862. spec->guarders = readCreatureSet(i); i+=( map.version == RoE ? 21 : 28 );
  1863. }
  1864. i+=4;
  1865. }
  1866. spec->spell = &(CGameInfo::mainObj->spellh->spells[bufor[i]]); ++i;
  1867. i+=3;
  1868. nobj->info = spec;
  1869. break;
  1870. }
  1871. case EDefType::PANDORA_DEF:
  1872. {
  1873. CPandorasBoxObjInfo * spec = new CPandorasBoxObjInfo;
  1874. bool messg = bufor[i]; ++i;
  1875. if(messg)
  1876. {
  1877. int mLength = readNormalNr(i); i+=4;
  1878. for(int vv=0; vv<mLength; ++vv)
  1879. {
  1880. spec->message += bufor[i]; ++i;
  1881. }
  1882. spec->areGuarders = bufor[i]; ++i;
  1883. if(spec->areGuarders)
  1884. {
  1885. spec->guarders = readCreatureSet(i); i+= (map.version == RoE ? 21 : 28);
  1886. }
  1887. i+=4;
  1888. }
  1889. ////// copied form event handling (seems to be similar)
  1890. spec->gainedExp = readNormalNr(i, 4); i+=4;
  1891. spec->manaDiff = readNormalNr(i, 4); i+=4;
  1892. spec->moraleDiff = readNormalNr(i, 1, true); ++i;
  1893. spec->luckDiff = readNormalNr(i, 1, true); ++i;
  1894. spec->wood = readNormalNr(i); i+=4;
  1895. spec->mercury = readNormalNr(i); i+=4;
  1896. spec->ore = readNormalNr(i); i+=4;
  1897. spec->sulfur = readNormalNr(i); i+=4;
  1898. spec->crystal = readNormalNr(i); i+=4;
  1899. spec->gems = readNormalNr(i); i+=4;
  1900. spec->gold = readNormalNr(i); i+=4;
  1901. spec->attack = readNormalNr(i, 1); ++i;
  1902. spec->defence = readNormalNr(i, 1); ++i;
  1903. spec->power = readNormalNr(i, 1); ++i;
  1904. spec->knowledge = readNormalNr(i, 1); ++i;
  1905. int gabn; //number of gained abilities
  1906. gabn = readNormalNr(i, 1); ++i;
  1907. for(int oo = 0; oo<gabn; ++oo)
  1908. {
  1909. spec->abilities.push_back((CGameInfo::mainObj->abilh)->abilities[readNormalNr(i, 1)]); ++i;
  1910. spec->abilityLevels.push_back(readNormalNr(i, 1)); ++i;
  1911. }
  1912. int gart = readNormalNr(i, 1); ++i; //number of gained artifacts
  1913. for(int oo = 0; oo<gart; ++oo)
  1914. {
  1915. spec->artifacts.push_back(&(CGameInfo::mainObj->arth->artifacts[readNormalNr(i, 2)])); i+=2;
  1916. }
  1917. int gspel = readNormalNr(i, 1); ++i; //number of gained spells
  1918. for(int oo = 0; oo<gspel; ++oo)
  1919. {
  1920. spec->spells.push_back(&(CGameInfo::mainObj->spellh->spells[readNormalNr(i, 1)])); ++i;
  1921. }
  1922. int gcre = readNormalNr(i, 1); ++i; //number of gained creatures
  1923. spec->creatures = readCreatureSet(i, gcre); i+=4*gcre;
  1924. i+=8;
  1925. nobj->info = spec;
  1926. ///////end of copied fragment
  1927. break;
  1928. }
  1929. case EDefType::GRAIL_DEF:
  1930. {
  1931. CGrailObjInfo * spec = new CGrailObjInfo;
  1932. spec->radius = readNormalNr(i); i+=4;
  1933. nobj->info = spec;
  1934. break;
  1935. }
  1936. case EDefType::CREGEN_DEF:
  1937. {
  1938. CCreGenObjInfo * spec = new CCreGenObjInfo;
  1939. spec->player = bufor[i]; ++i;
  1940. i+=3;
  1941. for(int ggg=0; ggg<4; ++ggg)
  1942. {
  1943. spec->bytes[ggg] = bufor[i]; ++i;
  1944. }
  1945. if((spec->bytes[0] == '\0') && (spec->bytes[1] == '\0') && (spec->bytes[2] == '\0') && (spec->bytes[3] == '\0'))
  1946. {
  1947. spec->asCastle = false;
  1948. spec->castles[0] = bufor[i]; ++i;
  1949. spec->castles[1] = bufor[i]; ++i;
  1950. }
  1951. else
  1952. {
  1953. spec->asCastle = true;
  1954. }
  1955. nobj->setOwner(spec->player);
  1956. nobj->info = spec;
  1957. break;
  1958. }
  1959. case EDefType::CREGEN2_DEF:
  1960. {
  1961. CCreGen2ObjInfo * spec = new CCreGen2ObjInfo;
  1962. spec->player = bufor[i]; ++i;
  1963. i+=3;
  1964. for(int ggg=0; ggg<4; ++ggg)
  1965. {
  1966. spec->bytes[ggg] = bufor[i]; ++i;
  1967. }
  1968. if((spec->bytes[0] == '\0') && (spec->bytes[1] == '\0') && (spec->bytes[2] == '\0') && (spec->bytes[3] == '\0'))
  1969. {
  1970. spec->asCastle = false;
  1971. spec->castles[0] = bufor[i]; ++i;
  1972. spec->castles[1] = bufor[i]; ++i;
  1973. }
  1974. else
  1975. {
  1976. spec->asCastle = true;
  1977. }
  1978. spec->minLevel = bufor[i]; ++i;
  1979. spec->maxLevel = bufor[i]; ++i;
  1980. if(spec->maxLevel>7)
  1981. spec->maxLevel = 7;
  1982. if(spec->minLevel<1)
  1983. spec->minLevel = 1;
  1984. nobj->setOwner(spec->player);
  1985. nobj->info = spec;
  1986. break;
  1987. }
  1988. case EDefType::CREGEN3_DEF:
  1989. {
  1990. CCreGen3ObjInfo * spec = new CCreGen3ObjInfo;
  1991. spec->player = bufor[i]; ++i;
  1992. i+=3;
  1993. spec->minLevel = bufor[i]; ++i;
  1994. spec->maxLevel = bufor[i]; ++i;
  1995. if(spec->maxLevel>7)
  1996. spec->maxLevel = 7;
  1997. if(spec->minLevel<1)
  1998. spec->minLevel = 1;
  1999. nobj->setOwner(spec->player);
  2000. nobj->info = spec;
  2001. break;
  2002. }
  2003. case EDefType::BORDERGUARD_DEF:
  2004. {
  2005. CBorderGuardObjInfo * spec = new CBorderGuardObjInfo;
  2006. spec->missionType = bufor[i]; ++i;
  2007. switch(spec->missionType)
  2008. {
  2009. case 1:
  2010. {
  2011. spec->m1level = readNormalNr(i); i+=4;
  2012. int limit = readNormalNr(i); i+=4;
  2013. if(limit == ((int)0xffffffff))
  2014. {
  2015. spec->isDayLimit = false;
  2016. spec->lastDay = -1;
  2017. }
  2018. else
  2019. {
  2020. spec->isDayLimit = true;
  2021. spec->lastDay = limit;
  2022. }
  2023. break;
  2024. }
  2025. case 2:
  2026. {
  2027. spec->m2attack = bufor[i]; ++i;
  2028. spec->m2defence = bufor[i]; ++i;
  2029. spec->m2power = bufor[i]; ++i;
  2030. spec->m2knowledge = bufor[i]; ++i;
  2031. int limit = readNormalNr(i); i+=4;
  2032. if(limit == ((int)0xffffffff))
  2033. {
  2034. spec->isDayLimit = false;
  2035. spec->lastDay = -1;
  2036. }
  2037. else
  2038. {
  2039. spec->isDayLimit = true;
  2040. spec->lastDay = limit;
  2041. }
  2042. break;
  2043. }
  2044. case 3:
  2045. {
  2046. spec->m3bytes[0] = bufor[i]; ++i;
  2047. spec->m3bytes[1] = bufor[i]; ++i;
  2048. spec->m3bytes[2] = bufor[i]; ++i;
  2049. spec->m3bytes[3] = bufor[i]; ++i;
  2050. int limit = readNormalNr(i); i+=4;
  2051. if(limit == ((int)0xffffffff))
  2052. {
  2053. spec->isDayLimit = false;
  2054. spec->lastDay = -1;
  2055. }
  2056. else
  2057. {
  2058. spec->isDayLimit = true;
  2059. spec->lastDay = limit;
  2060. }
  2061. break;
  2062. }
  2063. case 4:
  2064. {
  2065. spec->m4bytes[0] = bufor[i]; ++i;
  2066. spec->m4bytes[1] = bufor[i]; ++i;
  2067. spec->m4bytes[2] = bufor[i]; ++i;
  2068. spec->m4bytes[3] = bufor[i]; ++i;
  2069. int limit = readNormalNr(i); i+=4;
  2070. if(limit == ((int)0xffffffff))
  2071. {
  2072. spec->isDayLimit = false;
  2073. spec->lastDay = -1;
  2074. }
  2075. else
  2076. {
  2077. spec->isDayLimit = true;
  2078. spec->lastDay = limit;
  2079. }
  2080. break;
  2081. }
  2082. case 5:
  2083. {
  2084. int artNumber = bufor[i]; ++i;
  2085. for(int yy=0; yy<artNumber; ++yy)
  2086. {
  2087. int artid = readNormalNr(i, 2); i+=2;
  2088. spec->m5arts.push_back(&(CGameInfo::mainObj->arth->artifacts[artid]));
  2089. }
  2090. int limit = readNormalNr(i); i+=4;
  2091. if(limit == ((int)0xffffffff))
  2092. {
  2093. spec->isDayLimit = false;
  2094. spec->lastDay = -1;
  2095. }
  2096. else
  2097. {
  2098. spec->isDayLimit = true;
  2099. spec->lastDay = limit;
  2100. }
  2101. break;
  2102. }
  2103. case 6:
  2104. {
  2105. int typeNumber = bufor[i]; ++i;
  2106. for(int hh=0; hh<typeNumber; ++hh)
  2107. {
  2108. int creType = readNormalNr(i, 2); i+=2;
  2109. int creNumb = readNormalNr(i, 2); i+=2;
  2110. spec->m6cre.push_back(&(CGameInfo::mainObj->creh->creatures[creType]));
  2111. spec->m6number.push_back(creNumb);
  2112. }
  2113. int limit = readNormalNr(i); i+=4;
  2114. if(limit == ((int)0xffffffff))
  2115. {
  2116. spec->isDayLimit = false;
  2117. spec->lastDay = -1;
  2118. }
  2119. else
  2120. {
  2121. spec->isDayLimit = true;
  2122. spec->lastDay = limit;
  2123. }
  2124. break;
  2125. }
  2126. case 7:
  2127. {
  2128. spec->m7wood = readNormalNr(i); i+=4;
  2129. spec->m7mercury = readNormalNr(i); i+=4;
  2130. spec->m7ore = readNormalNr(i); i+=4;
  2131. spec->m7sulfur = readNormalNr(i); i+=4;
  2132. spec->m7crystal = readNormalNr(i); i+=4;
  2133. spec->m7gems = readNormalNr(i); i+=4;
  2134. spec->m7gold = readNormalNr(i); i+=4;
  2135. int limit = readNormalNr(i); i+=4;
  2136. if(limit == ((int)0xffffffff))
  2137. {
  2138. spec->isDayLimit = false;
  2139. spec->lastDay = -1;
  2140. }
  2141. else
  2142. {
  2143. spec->isDayLimit = true;
  2144. spec->lastDay = limit;
  2145. }
  2146. break;
  2147. }
  2148. case 8:
  2149. {
  2150. int heroType = bufor[i]; ++i;
  2151. spec->m8hero = CGameInfo::mainObj->heroh->heroes[heroType];
  2152. int limit = readNormalNr(i); i+=4;
  2153. if(limit == ((int)0xffffffff))
  2154. {
  2155. spec->isDayLimit = false;
  2156. spec->lastDay = -1;
  2157. }
  2158. else
  2159. {
  2160. spec->isDayLimit = true;
  2161. spec->lastDay = limit;
  2162. }
  2163. break;
  2164. }
  2165. case 9:
  2166. {
  2167. spec->m9player = bufor[i]; ++i;
  2168. int limit = readNormalNr(i); i+=4;
  2169. if(limit == ((int)0xffffffff))
  2170. {
  2171. spec->isDayLimit = false;
  2172. spec->lastDay = -1;
  2173. }
  2174. else
  2175. {
  2176. spec->isDayLimit = true;
  2177. spec->lastDay = limit;
  2178. }
  2179. break;
  2180. }
  2181. }//internal switch end (seer huts)
  2182. int len1 = readNormalNr(i); i+=4;
  2183. for(int ee=0; ee<len1; ++ee)
  2184. {
  2185. spec->firstVisitText += bufor[i]; ++i;
  2186. }
  2187. int len2 = readNormalNr(i); i+=4;
  2188. for(int ee=0; ee<len2; ++ee)
  2189. {
  2190. spec->nextVisitText += bufor[i]; ++i;
  2191. }
  2192. int len3 = readNormalNr(i); i+=4;
  2193. for(int ee=0; ee<len3; ++ee)
  2194. {
  2195. spec->completedText += bufor[i]; ++i;
  2196. }
  2197. nobj->info = spec;
  2198. break;
  2199. }
  2200. } //end of main switch
  2201. CGameInfo::mainObj->objh->objInstances.push_back(nobj);
  2202. //TODO - dokoñczyæ, du¿o do zrobienia - trzeba patrzeæ, co def niesie
  2203. }//*/ //end of loading objects; commented to make application work until it will be finished
  2204. ////objects loaded
  2205. //processMap(defsToUnpack);
  2206. std::vector<CDefHandler *> dhandlers = CGameInfo::mainObj->spriteh->extractManyFiles(defsToUnpack);
  2207. for (int i=0;i<dhandlers.size();i++)
  2208. map.defy[i]->handler=dhandlers[i];
  2209. for(int vv=0; vv<map.defy.size(); ++vv)
  2210. {
  2211. if(map.defy[vv]->handler->alphaTransformed)
  2212. continue;
  2213. for(int yy=0; yy<map.defy[vv]->handler->ourImages.size(); ++yy)
  2214. {
  2215. map.defy[vv]->handler->ourImages[yy].bitmap = CSDL_Ext::alphaTransform(map.defy[vv]->handler->ourImages[yy].bitmap);
  2216. SDL_Surface * bufs = CSDL_Ext::secondAlphaTransform(map.defy[vv]->handler->ourImages[yy].bitmap, alphaTransSurf);
  2217. SDL_FreeSurface(map.defy[vv]->handler->ourImages[yy].bitmap);
  2218. map.defy[vv]->handler->ourImages[yy].bitmap = bufs;
  2219. map.defy[vv]->handler->alphaTransformed = true;
  2220. }
  2221. }
  2222. SDL_FreeSurface(alphaTransSurf);
  2223. //assigning defobjinfos
  2224. for(int ww=0; ww<CGI->objh->objInstances.size(); ++ww)
  2225. {
  2226. for(int h=0; h<CGI->dobjinfo->objs.size(); ++h)
  2227. {
  2228. if(CGI->dobjinfo->objs[h].defName==CGI->objh->objInstances[ww]->defInfo->name)
  2229. {
  2230. CGI->objh->objInstances[ww]->defObjInfoNumber = h;
  2231. break;
  2232. }
  2233. }
  2234. }
  2235. for(int ww=0; ww<CGI->objh->objInstances.size(); ++ww)
  2236. {
  2237. if (CGI->objh->objInstances[ww]->defObjInfoNumber==-1)
  2238. std::cout<<CGI->objh->objInstances[ww]->ID<<"\t" << CGI->objh->objInstances[ww]->subID<<"\t"<<CGI->objh->objInstances[ww]->defInfo->name<<std::endl;
  2239. }
  2240. //assigned
  2241. //loading events
  2242. int numberOfEvents = readNormalNr(i); i+=4;
  2243. for(int yyoo=0; yyoo<numberOfEvents; ++yyoo)
  2244. {
  2245. CMapEvent ne;
  2246. ne.name = std::string();
  2247. ne.message = std::string();
  2248. int nameLen = readNormalNr(i); i+=4;
  2249. for(int qq=0; qq<nameLen; ++qq)
  2250. {
  2251. ne.name += bufor[i]; ++i;
  2252. }
  2253. int messLen = readNormalNr(i); i+=4;
  2254. for(int qq=0; qq<messLen; ++qq)
  2255. {
  2256. ne.message +=bufor[i]; ++i;
  2257. }
  2258. ne.wood = readNormalNr(i); i+=4;
  2259. ne.mercury = readNormalNr(i); i+=4;
  2260. ne.ore = readNormalNr(i); i+=4;
  2261. ne.sulfur = readNormalNr(i); i+=4;
  2262. ne.crystal = readNormalNr(i); i+=4;
  2263. ne.gems = readNormalNr(i); i+=4;
  2264. ne.gold = readNormalNr(i); i+=4;
  2265. ne.players = bufor[i]; ++i;
  2266. if(map.version>AB)
  2267. {
  2268. ne.humanAffected = bufor[i]; ++i;
  2269. }
  2270. else
  2271. ne.humanAffected = true;
  2272. ne.computerAffected = bufor[i]; ++i;
  2273. ne.firstOccurence = bufor[i]; ++i;
  2274. ne.nextOccurence = bufor[i]; ++i;
  2275. i+=18;
  2276. map.events.push_back(ne);
  2277. }
  2278. }
  2279. int CAmbarCendamo::readNormalNr (int pos, int bytCon, bool cyclic)
  2280. {
  2281. int ret=0;
  2282. int amp=1;
  2283. for (int i=0; i<bytCon; i++)
  2284. {
  2285. ret+=bufor[pos+i]*amp;
  2286. amp*=256;
  2287. }
  2288. if(cyclic && bytCon<4 && ret>=amp/2)
  2289. {
  2290. ret = ret-amp;
  2291. }
  2292. return ret;
  2293. }
  2294. void CAmbarCendamo::loadDefs()
  2295. {
  2296. std::set<int> loadedTypes;
  2297. for (int i=0; i<map.width; i++)
  2298. {
  2299. for (int j=0; j<map.width; j++)
  2300. {
  2301. if (loadedTypes.find(map.terrain[i][j].tertype)==loadedTypes.end())
  2302. {
  2303. CDefHandler *sdh = CGI->spriteh->giveDef(CSemiDefHandler::nameFromType(map.terrain[i][j].tertype).c_str());
  2304. loadedTypes.insert(map.terrain[i][j].tertype);
  2305. defs.push_back(sdh);
  2306. }
  2307. if (map.twoLevel && loadedTypes.find(map.undergroungTerrain[i][j].tertype)==loadedTypes.end())
  2308. {
  2309. CDefHandler *sdh = CGI->spriteh->giveDef(CSemiDefHandler::nameFromType(map.undergroungTerrain[i][j].tertype).c_str());
  2310. loadedTypes.insert(map.undergroungTerrain[i][j].tertype);
  2311. defs.push_back(sdh);
  2312. }
  2313. }
  2314. }
  2315. }
  2316. EDefType CAmbarCendamo::getDefType(CGDefInfo * a)
  2317. {
  2318. switch(a->id)
  2319. {
  2320. case 5: case 65: case 66: case 67: case 68: case 69:
  2321. return EDefType::ARTIFACT_DEF; //handled
  2322. case 6:
  2323. return EDefType::PANDORA_DEF; //hanled
  2324. case 26:
  2325. return EDefType::EVENTOBJ_DEF; //handled
  2326. case 33:
  2327. return EDefType::GARRISON_DEF; //handled
  2328. case 34: case 70: case 62: //70 - random hero //62 - prison
  2329. return EDefType::HERO_DEF; //handled
  2330. case 36:
  2331. return EDefType::GRAIL_DEF; //hanled
  2332. case 53: case 17: case 18: case 19: case 20: case 42: case 87: case 220://cases 17 - 20 and 42 - tests
  2333. return EDefType::PLAYERONLY_DEF; //handled
  2334. case 54: case 71: case 72: case 73: case 74: case 75: case 162: case 163: case 164:
  2335. return EDefType::CREATURES_DEF; //handled
  2336. case 59:
  2337. return EDefType::SIGN_DEF; //handled
  2338. case 77:
  2339. return EDefType::TOWN_DEF; //can be problematic, but handled
  2340. case 79: case 76:
  2341. return EDefType::RESOURCE_DEF; //handled
  2342. case 81:
  2343. return EDefType::SCHOLAR_DEF; //handled
  2344. case 83:
  2345. return EDefType::SEERHUT_DEF; //handled
  2346. case 91:
  2347. return EDefType::SIGN_DEF; //handled
  2348. case 88: case 89: case 90:
  2349. return SHRINE_DEF; //handled
  2350. case 93:
  2351. return SPELLSCROLL_DEF; //handled
  2352. case 98:
  2353. return EDefType::TOWN_DEF; //handled
  2354. case 113:
  2355. return EDefType::WITCHHUT_DEF; //handled
  2356. case 215:
  2357. return EDefType::BORDERGUARD_DEF; //handled by analogy to seer huts ;]
  2358. case 216:
  2359. return EDefType::CREGEN2_DEF; //handled
  2360. case 217:
  2361. return EDefType::CREGEN_DEF; //handled
  2362. case 218:
  2363. return EDefType::CREGEN3_DEF; //handled
  2364. case 219:
  2365. return EDefType::GARRISON_DEF; //handled
  2366. default:
  2367. return EDefType::TERRAINOBJ_DEF; // nothing to be handled
  2368. }
  2369. }
  2370. CCreatureSet CAmbarCendamo::readCreatureSet(int pos, int number)
  2371. {
  2372. if(map.version>RoE)
  2373. {
  2374. CCreatureSet ret;
  2375. std::pair<CCreature *, int> ins;
  2376. if(number>0 && readNormalNr(pos, 2)!=0xffff)
  2377. {
  2378. int rettt = readNormalNr(pos, 2);
  2379. if(rettt>32768)
  2380. rettt = 65536-rettt+CGameInfo::mainObj->creh->creatures.size()-16;
  2381. ins.first = &(CGameInfo::mainObj->creh->creatures[rettt]);
  2382. ins.second = readNormalNr(pos+2, 2);
  2383. std::pair<int,std::pair<CCreature *, int> > tt(0,ins);
  2384. ret.slots.insert(tt);
  2385. }
  2386. if(number>1 && readNormalNr(pos+4, 2)!=0xffff)
  2387. {
  2388. int rettt = readNormalNr(pos+4, 2);
  2389. if(rettt>32768)
  2390. rettt = 65536-rettt+CGameInfo::mainObj->creh->creatures.size()-16;
  2391. ins.first = &(CGameInfo::mainObj->creh->creatures[rettt]);
  2392. ins.second = readNormalNr(pos+6, 2);
  2393. std::pair<int,std::pair<CCreature *, int> > tt(1,ins);
  2394. ret.slots.insert(tt);
  2395. }
  2396. if(number>2 && readNormalNr(pos+8, 2)!=0xffff)
  2397. {
  2398. int rettt = readNormalNr(pos+8, 2);
  2399. if(rettt>32768)
  2400. rettt = 65536-rettt+CGameInfo::mainObj->creh->creatures.size()-16;
  2401. ins.first = &(CGameInfo::mainObj->creh->creatures[rettt]);
  2402. ins.second = readNormalNr(pos+10, 2);
  2403. std::pair<int,std::pair<CCreature *, int> > tt(2,ins);
  2404. ret.slots.insert(tt);
  2405. }
  2406. if(number>3 && readNormalNr(pos+12, 2)!=0xffff)
  2407. {
  2408. int rettt = readNormalNr(pos+12, 2);
  2409. if(rettt>32768)
  2410. rettt = 65536-rettt+CGameInfo::mainObj->creh->creatures.size()-16;
  2411. ins.first = &(CGameInfo::mainObj->creh->creatures[rettt]);
  2412. ins.second = readNormalNr(pos+14, 2);
  2413. std::pair<int,std::pair<CCreature *, int> > tt(3,ins);
  2414. ret.slots.insert(tt);
  2415. }
  2416. if(number>4 && readNormalNr(pos+16, 2)!=0xffff)
  2417. {
  2418. int rettt = readNormalNr(pos+16, 2);
  2419. if(rettt>32768)
  2420. rettt = 65536-rettt+CGameInfo::mainObj->creh->creatures.size()-16;
  2421. ins.first = &(CGameInfo::mainObj->creh->creatures[rettt]);
  2422. ins.second = readNormalNr(pos+18, 2);
  2423. std::pair<int,std::pair<CCreature *, int> > tt(4,ins);
  2424. ret.slots.insert(tt);
  2425. }
  2426. if(number>5 && readNormalNr(pos+20, 2)!=0xffff)
  2427. {
  2428. int rettt = readNormalNr(pos+20, 2);
  2429. if(rettt>32768)
  2430. rettt = 65536-rettt+CGameInfo::mainObj->creh->creatures.size()-16;
  2431. ins.first = &(CGameInfo::mainObj->creh->creatures[rettt]);
  2432. ins.second = readNormalNr(pos+22, 2);
  2433. std::pair<int,std::pair<CCreature *, int> > tt(5,ins);
  2434. ret.slots.insert(tt);
  2435. }
  2436. if(number>6 && readNormalNr(pos+24, 2)!=0xffff)
  2437. {
  2438. int rettt = readNormalNr(pos+24, 2);
  2439. if(rettt>32768)
  2440. rettt = 65536-rettt+CGameInfo::mainObj->creh->creatures.size()-16;
  2441. ins.first = &(CGameInfo::mainObj->creh->creatures[rettt]);
  2442. ins.second = readNormalNr(pos+26, 2);
  2443. std::pair<int,std::pair<CCreature *, int> > tt(6,ins);
  2444. ret.slots.insert(tt);
  2445. }
  2446. return ret;
  2447. }
  2448. else
  2449. {
  2450. CCreatureSet ret;
  2451. std::pair<CCreature *, int> ins;
  2452. if(number>0 && readNormalNr(pos, 1)!=0xff)
  2453. {
  2454. int rettt = readNormalNr(pos, 1);
  2455. if(rettt>220)
  2456. rettt = 256-rettt+CGameInfo::mainObj->creh->creatures.size()-16;
  2457. ins.first = &(CGameInfo::mainObj->creh->creatures[rettt]);
  2458. ins.second = readNormalNr(pos+1, 2);
  2459. std::pair<int,std::pair<CCreature *, int> > tt(0,ins);
  2460. ret.slots.insert(tt);
  2461. }
  2462. if(number>1 && readNormalNr(pos+3, 1)!=0xff)
  2463. {
  2464. int rettt = readNormalNr(pos+3, 1);
  2465. if(rettt>220)
  2466. rettt = 256-rettt+CGameInfo::mainObj->creh->creatures.size()-16;
  2467. ins.first = &(CGameInfo::mainObj->creh->creatures[rettt]);
  2468. ins.second = readNormalNr(pos+4, 2);
  2469. std::pair<int,std::pair<CCreature *, int> > tt(1,ins);
  2470. ret.slots.insert(tt);
  2471. }
  2472. if(number>2 && readNormalNr(pos+6, 1)!=0xff)
  2473. {
  2474. int rettt = readNormalNr(pos+6, 1);
  2475. if(rettt>220)
  2476. rettt = 256-rettt+CGameInfo::mainObj->creh->creatures.size()-16;
  2477. ins.first = &(CGameInfo::mainObj->creh->creatures[rettt]);
  2478. ins.second = readNormalNr(pos+7, 2);
  2479. std::pair<int,std::pair<CCreature *, int> > tt(2,ins);
  2480. ret.slots.insert(tt);
  2481. }
  2482. if(number>3 && readNormalNr(pos+9, 1)!=0xff)
  2483. {
  2484. int rettt = readNormalNr(pos+9, 1);
  2485. if(rettt>220)
  2486. rettt = 256-rettt+CGameInfo::mainObj->creh->creatures.size()-16;
  2487. ins.first = &(CGameInfo::mainObj->creh->creatures[rettt]);
  2488. ins.second = readNormalNr(pos+10, 2);
  2489. std::pair<int,std::pair<CCreature *, int> > tt(3,ins);
  2490. ret.slots.insert(tt);
  2491. }
  2492. if(number>4 && readNormalNr(pos+12, 1)!=0xff)
  2493. {
  2494. int rettt = readNormalNr(pos+12, 1);
  2495. if(rettt>220)
  2496. rettt = 256-rettt+CGameInfo::mainObj->creh->creatures.size()-16;
  2497. ins.first = &(CGameInfo::mainObj->creh->creatures[rettt]);
  2498. ins.second = readNormalNr(pos+13, 2);
  2499. std::pair<int,std::pair<CCreature *, int> > tt(4,ins);
  2500. ret.slots.insert(tt);
  2501. }
  2502. if(number>5 && readNormalNr(pos+15, 1)!=0xff)
  2503. {
  2504. int rettt = readNormalNr(pos+15, 1);
  2505. if(rettt>220)
  2506. rettt = 256-rettt+CGameInfo::mainObj->creh->creatures.size()-16;
  2507. ins.first = &(CGameInfo::mainObj->creh->creatures[rettt]);
  2508. ins.second = readNormalNr(pos+16, 2);
  2509. std::pair<int,std::pair<CCreature *, int> > tt(5,ins);
  2510. ret.slots.insert(tt);
  2511. }
  2512. if(number>6 && readNormalNr(pos+18, 1)!=0xff)
  2513. {
  2514. int rettt = readNormalNr(pos+18, 1);
  2515. if(rettt>220)
  2516. rettt = 256-rettt+CGameInfo::mainObj->creh->creatures.size()-16;
  2517. ins.first = &(CGameInfo::mainObj->creh->creatures[rettt]);
  2518. ins.second = readNormalNr(pos+19, 2);
  2519. std::pair<int,std::pair<CCreature *, int> > tt(6,ins);
  2520. ret.slots.insert(tt);
  2521. }
  2522. return ret;
  2523. }
  2524. }
  2525. void CAmbarCendamo::processMap(std::vector<std::string> & defsToUnpack)
  2526. {
  2527. resDefNames.push_back("AVTRNDM0.DEF");
  2528. resDefNames.push_back("AVTWOOD0.DEF");
  2529. resDefNames.push_back("AVTMERC0.DEF");
  2530. resDefNames.push_back("AVTORE0.DEF");
  2531. resDefNames.push_back("AVTSULF0.DEF");
  2532. resDefNames.push_back("AVTCRYS0.DEF");
  2533. resDefNames.push_back("AVTGEMS0.DEF");
  2534. resDefNames.push_back("AVTGOLD0.DEF");
  2535. resDefNames.push_back("ZMITHR.DEF");
  2536. std::vector<CGDefInfo*> resDefNumbers;
  2537. for(int hh=0; hh<resDefNames.size(); ++hh)
  2538. {
  2539. resDefNumbers.push_back(NULL);
  2540. for(int k=0; k<map.defy.size(); ++k)
  2541. {
  2542. std::string buf = map.defy[k]->name;
  2543. std::transform(buf.begin(), buf.end(), buf.begin(), (int(*)(int))toupper);
  2544. if(resDefNames[hh] == buf)
  2545. {
  2546. resDefNumbers[resDefNumbers.size()-1] = map.defy[k];
  2547. break;
  2548. }
  2549. }
  2550. }
  2551. std::vector<std::string> creDefNames;
  2552. for(int dd=0; dd<140; ++dd) //we do not use here WoG units
  2553. {
  2554. creDefNames.push_back(CGI->dobjinfo->objs[dd+1184].defName);
  2555. }
  2556. std::vector<CGDefInfo*> creDefNumbers;
  2557. for(int ee=0; ee<creDefNames.size(); ++ee)
  2558. {
  2559. creDefNumbers.push_back(NULL);
  2560. }
  2561. std::vector<std::string> artDefNames;
  2562. std::vector<CGDefInfo* > artDefNumbers;
  2563. for(int bb=0; bb<162; ++bb)
  2564. {
  2565. if(CGI->arth->artifacts[CGI->dobjinfo->objs[bb+213].subtype].aClass!=EartClass::SartClass)
  2566. artDefNames.push_back(CGI->dobjinfo->objs[bb+213].defName);
  2567. }
  2568. for(int ee=0; ee<artDefNames.size(); ++ee)
  2569. {
  2570. artDefNumbers.push_back(NULL);
  2571. }
  2572. std::vector<std::string> art1DefNames;
  2573. std::vector<CGDefInfo*> art1DefNumbers;
  2574. for(int bb=0; bb<162; ++bb)
  2575. {
  2576. if(CGI->arth->artifacts[CGI->dobjinfo->objs[bb+213].subtype].aClass==EartClass::TartClass)
  2577. art1DefNames.push_back(CGI->dobjinfo->objs[bb+213].defName);
  2578. }
  2579. for(int ee=0; ee<art1DefNames.size(); ++ee)
  2580. {
  2581. art1DefNumbers.push_back(NULL);
  2582. }
  2583. std::vector<std::string> art2DefNames;
  2584. std::vector<CGDefInfo*> art2DefNumbers;
  2585. for(int bb=0; bb<162; ++bb)
  2586. {
  2587. if(CGI->arth->artifacts[CGI->dobjinfo->objs[bb+213].subtype].aClass==EartClass::NartClass)
  2588. art2DefNames.push_back(CGI->dobjinfo->objs[bb+213].defName);
  2589. }
  2590. for(int ee=0; ee<art2DefNames.size(); ++ee)
  2591. {
  2592. art2DefNumbers.push_back(NULL);
  2593. }
  2594. std::vector<std::string> art3DefNames;
  2595. std::vector<CGDefInfo*> art3DefNumbers;
  2596. for(int bb=0; bb<162; ++bb)
  2597. {
  2598. if(CGI->arth->artifacts[CGI->dobjinfo->objs[bb+213].subtype].aClass==EartClass::JartClass)
  2599. art3DefNames.push_back(CGI->dobjinfo->objs[bb+213].defName);
  2600. }
  2601. for(int ee=0; ee<art3DefNames.size(); ++ee)
  2602. {
  2603. art3DefNumbers.push_back(NULL);
  2604. }
  2605. std::vector<std::string> art4DefNames;
  2606. std::vector<CGDefInfo*> art4DefNumbers;
  2607. for(int bb=0; bb<162; ++bb)
  2608. {
  2609. if(CGI->arth->artifacts[CGI->dobjinfo->objs[bb+213].subtype].aClass==EartClass::RartClass)
  2610. art4DefNames.push_back(CGI->dobjinfo->objs[bb+213].defName);
  2611. }
  2612. for(int ee=0; ee<art4DefNames.size(); ++ee)
  2613. {
  2614. art4DefNumbers.push_back(NULL);
  2615. }
  2616. std::vector<std::string> town0DefNames; //without fort
  2617. std::vector<CGDefInfo*> town0DefNumbers;
  2618. std::vector<std::string> town1DefNames; //with fort
  2619. std::vector<CGDefInfo*> town1DefNumbers;
  2620. for(int dd=0; dd<F_NUMBER; ++dd)
  2621. {
  2622. town1DefNames.push_back(CGI->dobjinfo->objs[dd+385].defName);
  2623. town1DefNumbers.push_back(NULL);
  2624. }
  2625. std::vector< std::vector<std::string> > creGenNames;
  2626. std::vector< std::vector<CGDefInfo*> > creGenNumbers;
  2627. creGenNames.resize(F_NUMBER);
  2628. creGenNumbers.resize(F_NUMBER);
  2629. for(int ff=0; ff<F_NUMBER-1; ++ff)
  2630. {
  2631. for(int dd=0; dd<7; ++dd)
  2632. {
  2633. creGenNames[ff].push_back(CGI->dobjinfo->objs[395+7*ff+dd].defName);
  2634. creGenNumbers[ff].push_back(NULL);
  2635. }
  2636. }
  2637. for(int dd=0; dd<7; ++dd)
  2638. {
  2639. creGenNumbers[8].push_back(NULL);
  2640. }
  2641. creGenNames[F_NUMBER-1].push_back(CGI->dobjinfo->objs[457].defName);
  2642. creGenNames[F_NUMBER-1].push_back(CGI->dobjinfo->objs[452].defName);
  2643. creGenNames[F_NUMBER-1].push_back(CGI->dobjinfo->objs[455].defName);
  2644. creGenNames[F_NUMBER-1].push_back(CGI->dobjinfo->objs[454].defName);
  2645. creGenNames[F_NUMBER-1].push_back(CGI->dobjinfo->objs[453].defName);
  2646. creGenNames[F_NUMBER-1].push_back(CGI->dobjinfo->objs[458].defName);
  2647. creGenNames[F_NUMBER-1].push_back(CGI->dobjinfo->objs[459].defName);
  2648. for(int b=0; b<CGI->scenarioOps.playerInfos.size(); ++b) // choosing random player alignment
  2649. {
  2650. if(CGI->scenarioOps.playerInfos[b].castle==-1)
  2651. CGI->scenarioOps.playerInfos[b].castle = rand()%F_NUMBER;
  2652. }
  2653. //variables initialized
  2654. for(int j=0; j<CGI->objh->objInstances.size(); ++j)
  2655. {
  2656. CGDefInfo * curDef = CGI->objh->objInstances[j]->defInfo;
  2657. switch(getDefType(curDef))
  2658. {
  2659. case EDefType::RESOURCE_DEF:
  2660. {
  2661. if(curDef->id==76) //resource to specify
  2662. {
  2663. CGDefInfo * nxt = curDef;
  2664. nxt->id = 79;
  2665. nxt->subid = rand()%7;
  2666. if(resDefNumbers[nxt->subid+1]!=NULL)
  2667. {
  2668. CGI->objh->objInstances[j]->defInfo = resDefNumbers[nxt->subid+1];
  2669. continue;
  2670. }
  2671. nxt->name = resDefNames[nxt->subid+1];
  2672. std::vector<DefObjInfo>::iterator pit = std::find(CGameInfo::mainObj->dobjinfo->objs.begin(), CGameInfo::mainObj->dobjinfo->objs.end(),
  2673. nxt->name);
  2674. if(pit == CGameInfo::mainObj->dobjinfo->objs.end())
  2675. {
  2676. nxt->isOnDefList = false;
  2677. }
  2678. else
  2679. {
  2680. nxt->printPriority = pit->priority;
  2681. nxt->isOnDefList = true;
  2682. }
  2683. map.defy.push_back(nxt); // add this def to the vector
  2684. defsToUnpack.push_back(nxt->name);
  2685. CGI->objh->objInstances[j]->defInfo = nxt;
  2686. if(resDefNumbers[nxt->subid+1]==NULL)
  2687. {
  2688. resDefNumbers[nxt->subid+1] = nxt;
  2689. }
  2690. }
  2691. break;
  2692. }
  2693. case EDefType::CREATURES_DEF:
  2694. {
  2695. if(curDef->id==72) //random monster lvl 1
  2696. {
  2697. CGDefInfo * nxt = curDef;
  2698. nxt->id = 54;
  2699. nxt->subid = 14*(rand()%9)+rand()%2;
  2700. if(creDefNumbers[nxt->subid]!=NULL)
  2701. {
  2702. CGI->objh->objInstances[j]->defInfo = creDefNumbers[nxt->subid];
  2703. continue;
  2704. }
  2705. nxt->name = creDefNames[nxt->subid];
  2706. std::vector<DefObjInfo>::iterator pit = std::find(CGameInfo::mainObj->dobjinfo->objs.begin(), CGameInfo::mainObj->dobjinfo->objs.end(),
  2707. nxt->name);
  2708. if(pit == CGameInfo::mainObj->dobjinfo->objs.end())
  2709. {
  2710. nxt->isOnDefList = false;
  2711. }
  2712. else
  2713. {
  2714. nxt->printPriority = pit->priority;
  2715. nxt->isOnDefList = true;
  2716. }
  2717. map.defy.push_back(nxt); // add this def to the vector
  2718. defsToUnpack.push_back(nxt->name);
  2719. CGI->objh->objInstances[j]->defInfo = nxt;
  2720. if(creDefNumbers[nxt->subid]==NULL)
  2721. {
  2722. creDefNumbers[nxt->subid] = nxt;
  2723. }
  2724. }
  2725. if(curDef->id==73) //random monster lvl 2
  2726. {
  2727. CGDefInfo * nxt = curDef;
  2728. nxt->id = 54;
  2729. nxt->subid = 14*(rand()%9)+rand()%2+2;
  2730. if(creDefNumbers[nxt->subid]!=NULL)
  2731. {
  2732. CGI->objh->objInstances[j]->defInfo = creDefNumbers[nxt->subid];
  2733. continue;
  2734. }
  2735. nxt->name = creDefNames[nxt->subid];
  2736. std::vector<DefObjInfo>::iterator pit = std::find(CGameInfo::mainObj->dobjinfo->objs.begin(), CGameInfo::mainObj->dobjinfo->objs.end(),
  2737. nxt->name);
  2738. if(pit == CGameInfo::mainObj->dobjinfo->objs.end())
  2739. {
  2740. nxt->isOnDefList = false;
  2741. }
  2742. else
  2743. {
  2744. nxt->printPriority = pit->priority;
  2745. nxt->isOnDefList = true;
  2746. }
  2747. map.defy.push_back(nxt); // add this def to the vector
  2748. defsToUnpack.push_back(nxt->name);
  2749. CGI->objh->objInstances[j]->defInfo = nxt;
  2750. if(creDefNumbers[nxt->subid]==NULL)
  2751. {
  2752. creDefNumbers[nxt->subid] = nxt;
  2753. }
  2754. }
  2755. if(curDef->id==74) //random monster lvl 3
  2756. {
  2757. CGDefInfo *nxt = curDef;
  2758. nxt->id = 54;
  2759. nxt->subid = 14*(rand()%9)+rand()%2+4;
  2760. if(creDefNumbers[nxt->subid]!=NULL)
  2761. {
  2762. CGI->objh->objInstances[j]->defInfo = creDefNumbers[nxt->subid];
  2763. continue;
  2764. }
  2765. nxt->name = creDefNames[nxt->subid];
  2766. std::vector<DefObjInfo>::iterator pit = std::find(CGameInfo::mainObj->dobjinfo->objs.begin(), CGameInfo::mainObj->dobjinfo->objs.end(),
  2767. nxt->name);
  2768. if(pit == CGameInfo::mainObj->dobjinfo->objs.end())
  2769. {
  2770. nxt->isOnDefList = false;
  2771. }
  2772. else
  2773. {
  2774. nxt->printPriority = pit->priority;
  2775. nxt->isOnDefList = true;
  2776. }
  2777. map.defy.push_back(nxt); // add this def to the vector
  2778. defsToUnpack.push_back(nxt->name);
  2779. CGI->objh->objInstances[j]->defInfo = nxt;
  2780. if(creDefNumbers[nxt->subid]==NULL)
  2781. {
  2782. creDefNumbers[nxt->subid] = nxt;
  2783. }
  2784. }
  2785. if(curDef->id==75) //random monster lvl 4
  2786. {
  2787. CGDefInfo * nxt = curDef;
  2788. nxt->id = 54;
  2789. nxt->subid = 14*(rand()%9)+rand()%2+6;
  2790. if(creDefNumbers[nxt->subid]!=NULL)
  2791. {
  2792. CGI->objh->objInstances[j]->defInfo = creDefNumbers[nxt->subid];
  2793. continue;
  2794. }
  2795. nxt->name = creDefNames[nxt->subid];
  2796. std::vector<DefObjInfo>::iterator pit = std::find(CGameInfo::mainObj->dobjinfo->objs.begin(), CGameInfo::mainObj->dobjinfo->objs.end(),
  2797. nxt->name);
  2798. if(pit == CGameInfo::mainObj->dobjinfo->objs.end())
  2799. {
  2800. nxt->isOnDefList = false;
  2801. }
  2802. else
  2803. {
  2804. nxt->printPriority = pit->priority;
  2805. nxt->isOnDefList = true;
  2806. }
  2807. map.defy.push_back(nxt); // add this def to the vector
  2808. defsToUnpack.push_back(nxt->name);
  2809. CGI->objh->objInstances[j]->defInfo = nxt;
  2810. if(creDefNumbers[nxt->subid]==NULL)
  2811. {
  2812. creDefNumbers[nxt->subid] = nxt;
  2813. }
  2814. }
  2815. if(curDef->id==162) //random monster lvl 5
  2816. {
  2817. CGDefInfo * nxt = curDef;
  2818. nxt->id = 54;
  2819. nxt->subid = 14*(rand()%9)+rand()%2+8;
  2820. if(creDefNumbers[nxt->subid]!=NULL)
  2821. {
  2822. CGI->objh->objInstances[j]->defInfo = creDefNumbers[nxt->subid];
  2823. continue;
  2824. }
  2825. nxt->name = creDefNames[nxt->subid];
  2826. std::vector<DefObjInfo>::iterator pit = std::find(CGameInfo::mainObj->dobjinfo->objs.begin(), CGameInfo::mainObj->dobjinfo->objs.end(),
  2827. nxt->name);
  2828. if(pit == CGameInfo::mainObj->dobjinfo->objs.end())
  2829. {
  2830. nxt->isOnDefList = false;
  2831. }
  2832. else
  2833. {
  2834. nxt->printPriority = pit->priority;
  2835. nxt->isOnDefList = true;
  2836. }
  2837. map.defy.push_back(nxt); // add this def to the vector
  2838. defsToUnpack.push_back(nxt->name);
  2839. CGI->objh->objInstances[j]->defInfo = nxt;
  2840. if(creDefNumbers[nxt->subid]==NULL)
  2841. {
  2842. creDefNumbers[nxt->subid] = nxt;
  2843. }
  2844. }
  2845. if(curDef->id==163) //random monster lvl 6
  2846. {
  2847. CGDefInfo* nxt = curDef;
  2848. nxt->id = 54;
  2849. nxt->subid = 14*(rand()%9)+rand()%2+10;
  2850. if(creDefNumbers[nxt->subid]!=NULL)
  2851. {
  2852. CGI->objh->objInstances[j]->defInfo = creDefNumbers[nxt->subid];
  2853. continue;
  2854. }
  2855. nxt->name = creDefNames[nxt->subid];
  2856. std::vector<DefObjInfo>::iterator pit = std::find(CGameInfo::mainObj->dobjinfo->objs.begin(), CGameInfo::mainObj->dobjinfo->objs.end(),
  2857. nxt->name);
  2858. if(pit == CGameInfo::mainObj->dobjinfo->objs.end())
  2859. {
  2860. nxt->isOnDefList = false;
  2861. }
  2862. else
  2863. {
  2864. nxt->printPriority = pit->priority;
  2865. nxt->isOnDefList = true;
  2866. }
  2867. map.defy.push_back(nxt); // add this def to the vector
  2868. defsToUnpack.push_back(nxt->name);
  2869. CGI->objh->objInstances[j]->defInfo = nxt;
  2870. if(creDefNumbers[nxt->subid]==NULL)
  2871. {
  2872. creDefNumbers[nxt->subid] = nxt;
  2873. }
  2874. }
  2875. if(curDef->id==164) //random monster lvl 7
  2876. {
  2877. CGDefInfo * nxt = curDef;
  2878. nxt->id = 54;
  2879. nxt->subid = 14*(rand()%9)+rand()%2+12;
  2880. if(creDefNumbers[nxt->subid]!=NULL)
  2881. {
  2882. CGI->objh->objInstances[j]->defInfo = creDefNumbers[nxt->subid];
  2883. continue;
  2884. }
  2885. nxt->name = creDefNames[nxt->subid];
  2886. std::vector<DefObjInfo>::iterator pit = std::find(CGameInfo::mainObj->dobjinfo->objs.begin(), CGameInfo::mainObj->dobjinfo->objs.end(),
  2887. nxt->name);
  2888. if(pit == CGameInfo::mainObj->dobjinfo->objs.end())
  2889. {
  2890. nxt->isOnDefList = false;
  2891. }
  2892. else
  2893. {
  2894. nxt->printPriority = pit->priority;
  2895. nxt->isOnDefList = true;
  2896. }
  2897. map.defy.push_back(nxt); // add this def to the vector
  2898. defsToUnpack.push_back(nxt->name);
  2899. CGI->objh->objInstances[j]->defInfo = nxt;
  2900. if(creDefNumbers[nxt->subid]==NULL)
  2901. {
  2902. creDefNumbers[nxt->subid] = nxt;
  2903. }
  2904. }
  2905. if(curDef->id==71) //random monster (any level)
  2906. {
  2907. CGDefInfo * nxt = curDef;
  2908. nxt->id = 54;
  2909. nxt->subid = rand()%126;
  2910. if(creDefNumbers[nxt->subid]!=NULL)
  2911. {
  2912. CGI->objh->objInstances[j]->defInfo = creDefNumbers[nxt->subid];
  2913. continue;
  2914. }
  2915. nxt->name = creDefNames[nxt->subid];
  2916. std::vector<DefObjInfo>::iterator pit = std::find(CGameInfo::mainObj->dobjinfo->objs.begin(), CGameInfo::mainObj->dobjinfo->objs.end(),
  2917. nxt->name);
  2918. if(pit == CGameInfo::mainObj->dobjinfo->objs.end())
  2919. {
  2920. nxt->isOnDefList = false;
  2921. }
  2922. else
  2923. {
  2924. nxt->printPriority = pit->priority;
  2925. nxt->isOnDefList = true;
  2926. }
  2927. map.defy.push_back(nxt); // add this def to the vector
  2928. defsToUnpack.push_back(nxt->name);
  2929. CGI->objh->objInstances[j]->defInfo = nxt;
  2930. if(creDefNumbers[nxt->subid]==NULL)
  2931. {
  2932. creDefNumbers[nxt->subid] = nxt;
  2933. }
  2934. }
  2935. break;
  2936. } //end of case
  2937. case EDefType::ARTIFACT_DEF:
  2938. {
  2939. if(curDef->id==65) //random atrifact (any class)
  2940. {
  2941. CGDefInfo *nxt = curDef;
  2942. nxt->id = 5;
  2943. nxt->subid = rand()%artDefNames.size();
  2944. if(artDefNumbers[nxt->subid]!=NULL)
  2945. {
  2946. CGI->objh->objInstances[j]->defInfo = artDefNumbers[nxt->subid];
  2947. continue;
  2948. }
  2949. nxt->name = artDefNames[nxt->subid];
  2950. std::vector<DefObjInfo>::iterator pit = std::find(CGameInfo::mainObj->dobjinfo->objs.begin(), CGameInfo::mainObj->dobjinfo->objs.end(),
  2951. nxt->name);
  2952. if(pit == CGameInfo::mainObj->dobjinfo->objs.end())
  2953. {
  2954. nxt->isOnDefList = false;
  2955. }
  2956. else
  2957. {
  2958. nxt->printPriority = pit->priority;
  2959. nxt->isOnDefList = true;
  2960. }
  2961. map.defy.push_back(nxt); // add this def to the vector
  2962. defsToUnpack.push_back(nxt->name);
  2963. CGI->objh->objInstances[j]->defInfo = nxt;
  2964. if(artDefNumbers[nxt->subid]==NULL)
  2965. {
  2966. artDefNumbers[nxt->subid] = nxt;
  2967. }
  2968. }
  2969. if(curDef->id==66) //random atrifact (treasure)
  2970. {
  2971. CGDefInfo* nxt = curDef;
  2972. nxt->id = 5;
  2973. nxt->subid = rand()%art1DefNames.size();
  2974. if(art1DefNumbers[nxt->subid]!=NULL)
  2975. {
  2976. CGI->objh->objInstances[j]->defInfo = art1DefNumbers[nxt->subid];
  2977. continue;
  2978. }
  2979. nxt->name = art1DefNames[nxt->subid];
  2980. std::vector<DefObjInfo>::iterator pit = std::find(CGameInfo::mainObj->dobjinfo->objs.begin(), CGameInfo::mainObj->dobjinfo->objs.end(),
  2981. nxt->name);
  2982. if(pit == CGameInfo::mainObj->dobjinfo->objs.end())
  2983. {
  2984. nxt->isOnDefList = false;
  2985. }
  2986. else
  2987. {
  2988. nxt->printPriority = pit->priority;
  2989. nxt->isOnDefList = true;
  2990. }
  2991. map.defy.push_back(nxt); // add this def to the vector
  2992. defsToUnpack.push_back(nxt->name);
  2993. CGI->objh->objInstances[j]->defInfo = nxt;
  2994. if(art1DefNumbers[nxt->subid]==NULL)
  2995. {
  2996. art1DefNumbers[nxt->subid] = nxt;
  2997. }
  2998. }
  2999. if(curDef->id==67) //random atrifact (minor)
  3000. {
  3001. CGDefInfo *nxt = curDef;
  3002. nxt->id = 5;
  3003. nxt->subid = rand()%art2DefNames.size();
  3004. if(art2DefNumbers[nxt->subid]!=NULL)
  3005. {
  3006. CGI->objh->objInstances[j]->defInfo = art2DefNumbers[nxt->subid];
  3007. continue;
  3008. }
  3009. nxt->name = art2DefNames[nxt->subid];
  3010. std::vector<DefObjInfo>::iterator pit = std::find(CGameInfo::mainObj->dobjinfo->objs.begin(), CGameInfo::mainObj->dobjinfo->objs.end(),
  3011. nxt->name);
  3012. if(pit == CGameInfo::mainObj->dobjinfo->objs.end())
  3013. {
  3014. nxt->isOnDefList = false;
  3015. }
  3016. else
  3017. {
  3018. nxt->printPriority = pit->priority;
  3019. nxt->isOnDefList = true;
  3020. }
  3021. map.defy.push_back(nxt); // add this def to the vector
  3022. defsToUnpack.push_back(nxt->name);
  3023. CGI->objh->objInstances[j]->defInfo = nxt;
  3024. if(art2DefNumbers[nxt->subid]==NULL)
  3025. {
  3026. art2DefNumbers[nxt->subid] = nxt;
  3027. }
  3028. }
  3029. if(curDef->id==68) //random atrifact (major)
  3030. {
  3031. CGDefInfo* nxt = curDef;
  3032. nxt->id = 5;
  3033. nxt->subid = rand()%art3DefNames.size();
  3034. if(art3DefNumbers[nxt->subid]!=NULL)
  3035. {
  3036. CGI->objh->objInstances[j]->defInfo = art3DefNumbers[nxt->subid];
  3037. continue;
  3038. }
  3039. nxt->name = art3DefNames[nxt->subid];
  3040. std::vector<DefObjInfo>::iterator pit = std::find(CGameInfo::mainObj->dobjinfo->objs.begin(), CGameInfo::mainObj->dobjinfo->objs.end(),
  3041. nxt->name);
  3042. if(pit == CGameInfo::mainObj->dobjinfo->objs.end())
  3043. {
  3044. nxt->isOnDefList = false;
  3045. }
  3046. else
  3047. {
  3048. nxt->printPriority = pit->priority;
  3049. nxt->isOnDefList = true;
  3050. }
  3051. map.defy.push_back(nxt); // add this def to the vector
  3052. defsToUnpack.push_back(nxt->name);
  3053. CGI->objh->objInstances[j]->defInfo = nxt;
  3054. if(art3DefNumbers[nxt->subid]==NULL)
  3055. {
  3056. art3DefNumbers[nxt->subid] =nxt;
  3057. }
  3058. }
  3059. if(curDef->id==69) //random atrifact (relic)
  3060. {
  3061. CGDefInfo *nxt = curDef;
  3062. nxt->id = 5;
  3063. nxt->subid = rand()%art4DefNames.size();
  3064. if(art4DefNumbers[nxt->subid]!=NULL)
  3065. {
  3066. CGI->objh->objInstances[j]->defInfo = art4DefNumbers[nxt->subid];
  3067. continue;
  3068. }
  3069. nxt->name = art4DefNames[nxt->subid];
  3070. std::vector<DefObjInfo>::iterator pit = std::find(CGameInfo::mainObj->dobjinfo->objs.begin(), CGameInfo::mainObj->dobjinfo->objs.end(),
  3071. nxt->name);
  3072. if(pit == CGameInfo::mainObj->dobjinfo->objs.end())
  3073. {
  3074. nxt->isOnDefList = false;
  3075. }
  3076. else
  3077. {
  3078. nxt->printPriority = pit->priority;
  3079. nxt->isOnDefList = true;
  3080. }
  3081. map.defy.push_back(nxt); // add this def to the vector
  3082. defsToUnpack.push_back(nxt->name);
  3083. CGI->objh->objInstances[j]->defInfo = nxt;
  3084. if(art4DefNumbers[nxt->subid]==NULL)
  3085. {
  3086. art4DefNumbers[nxt->subid] = nxt;
  3087. }
  3088. }
  3089. break;
  3090. }
  3091. case EDefType::TOWN_DEF:
  3092. {
  3093. if(curDef->id==77) //random town
  3094. {
  3095. CGDefInfo* nxt = curDef;
  3096. nxt->id = 98;
  3097. if(((CCastleObjInfo*)CGI->objh->objInstances[j]->info)->player==0xff)
  3098. {
  3099. nxt->subid = rand()%town1DefNames.size();
  3100. }
  3101. else
  3102. {
  3103. if(CGI->scenarioOps.getIthPlayersSettings(((CCastleObjInfo*)CGI->objh->objInstances[j]->info)->player).castle>-1)
  3104. {
  3105. nxt->subid = CGI->scenarioOps.getIthPlayersSettings(((CCastleObjInfo*)CGI->objh->objInstances[j]->info)->player).castle;
  3106. }
  3107. else
  3108. {
  3109. nxt->subid = rand()%town1DefNames.size();
  3110. }
  3111. }
  3112. if(town1DefNumbers[nxt->subid]!=NULL)
  3113. {
  3114. CGI->objh->objInstances[j]->defInfo = town1DefNumbers[nxt->subid];
  3115. continue;
  3116. }
  3117. nxt->name = town1DefNames[nxt->subid];
  3118. std::vector<DefObjInfo>::iterator pit = std::find(CGameInfo::mainObj->dobjinfo->objs.begin(), CGameInfo::mainObj->dobjinfo->objs.end(),
  3119. nxt->name);
  3120. if(pit == CGameInfo::mainObj->dobjinfo->objs.end())
  3121. {
  3122. nxt->isOnDefList = false;
  3123. }
  3124. else
  3125. {
  3126. nxt->printPriority = pit->priority;
  3127. nxt->isOnDefList = true;
  3128. }
  3129. map.defy.push_back(nxt); // add this def to the vector
  3130. defsToUnpack.push_back(nxt->name);
  3131. CGI->objh->objInstances[j]->defInfo = nxt;
  3132. if(town1DefNumbers[nxt->subid]==NULL)
  3133. {
  3134. town1DefNumbers[nxt->subid] = nxt;
  3135. }
  3136. for (int ij=0;ij<CGI->townh->townInstances.size();ij++) // wyharatac gdy bedzie dziedziczenie
  3137. {
  3138. if (CGI->townh->townInstances[ij]->pos==CGI->objh->objInstances[j]->pos)
  3139. {
  3140. CGI->townh->townInstances[ij]->town = &CGI->townh->towns[nxt->subid];
  3141. break;
  3142. }
  3143. }
  3144. }
  3145. //((CCastleObjInfo*)CGI->objh->objInstances[j].info)
  3146. break;
  3147. }
  3148. case EDefType::HERO_DEF:
  3149. {
  3150. std::vector<DefObjInfo>::iterator pit = std::find(CGameInfo::mainObj->dobjinfo->objs.begin(), CGameInfo::mainObj->dobjinfo->objs.end(),
  3151. CGI->objh->objInstances[j]->defInfo->name);
  3152. CGI->objh->objInstances[j]->defInfo->printPriority = 0;
  3153. if(pit == CGameInfo::mainObj->dobjinfo->objs.end())
  3154. {
  3155. CGI->objh->objInstances[j]->defInfo->isOnDefList = false;
  3156. }
  3157. else
  3158. {
  3159. CGI->objh->objInstances[j]->defInfo->isOnDefList = true;
  3160. }
  3161. break;
  3162. }
  3163. } //end of main switch
  3164. } //end of main loop
  3165. for(int j=0; j<CGI->objh->objInstances.size(); ++j) //for creature dwellings on map (they are town type dependent)
  3166. {
  3167. CGDefInfo * curDef = CGI->objh->objInstances[j]->defInfo;
  3168. switch(getDefType(curDef))
  3169. {
  3170. case EDefType::CREGEN_DEF:
  3171. {
  3172. if(((CCreGenObjInfo*)CGI->objh->objInstances[j]->info)->asCastle)
  3173. {
  3174. CGDefInfo *nxt = curDef;
  3175. nxt->id = 17;
  3176. for(int vv=0; vv<CGI->objh->objInstances.size(); ++vv)
  3177. {
  3178. if(getDefType(CGI->objh->objInstances[vv]->defInfo)==EDefType::TOWN_DEF)
  3179. {
  3180. if(
  3181. ((CCastleObjInfo*)CGI->objh->objInstances[vv]->info)->bytes[0]==((CCreGenObjInfo*)CGI->objh->objInstances[j]->info)->bytes[0]
  3182. && ((CCastleObjInfo*)CGI->objh->objInstances[vv]->info)->bytes[1]==((CCreGenObjInfo*)CGI->objh->objInstances[j]->info)->bytes[1]
  3183. && ((CCastleObjInfo*)CGI->objh->objInstances[vv]->info)->bytes[2]==((CCreGenObjInfo*)CGI->objh->objInstances[j]->info)->bytes[2]
  3184. && ((CCastleObjInfo*)CGI->objh->objInstances[vv]->info)->bytes[3]==((CCreGenObjInfo*)CGI->objh->objInstances[j]->info)->bytes[3])
  3185. {
  3186. for(int mm=0; mm<town1DefNames.size(); ++mm)
  3187. {
  3188. std::transform(town1DefNames[mm].begin(), town1DefNames[mm].end(), town1DefNames[mm].begin(), (int(*)(int))toupper);
  3189. std::string hlp = CGI->objh->objInstances[vv]->defInfo->name;
  3190. std::transform(hlp.begin(), hlp.end(), hlp.begin(), (int(*)(int))toupper);
  3191. if(town1DefNames[mm]==hlp)
  3192. {
  3193. nxt->subid = mm;
  3194. }
  3195. }
  3196. }
  3197. }
  3198. }
  3199. int lvl = atoi(CGI->objh->objInstances[j]->defInfo->name.substr(7, 8).c_str())-1;
  3200. nxt->name = creGenNames[nxt->subid][lvl];
  3201. if(creGenNumbers[nxt->subid][lvl]!=NULL)
  3202. {
  3203. CGI->objh->objInstances[j]->defInfo = creGenNumbers[nxt->subid][lvl];
  3204. continue;
  3205. }
  3206. std::vector<DefObjInfo>::iterator pit = std::find(CGameInfo::mainObj->dobjinfo->objs.begin(), CGameInfo::mainObj->dobjinfo->objs.end(),
  3207. nxt->name);
  3208. if(pit == CGameInfo::mainObj->dobjinfo->objs.end())
  3209. {
  3210. nxt->isOnDefList = false;
  3211. }
  3212. else
  3213. {
  3214. nxt->printPriority = pit->priority;
  3215. nxt->isOnDefList = true;
  3216. }
  3217. map.defy.push_back(nxt); // add this def to the vector
  3218. defsToUnpack.push_back(nxt->name);
  3219. CGI->objh->objInstances[j]->defInfo = nxt;
  3220. if(creGenNumbers[nxt->subid][lvl]==NULL)
  3221. {
  3222. creGenNumbers[nxt->subid][lvl] = nxt;
  3223. }
  3224. }
  3225. else //if not as castle
  3226. {
  3227. CGDefInfo * nxt = curDef;
  3228. nxt->id = 17;
  3229. std::vector<int> possibleTowns;
  3230. for(int bb=0; bb<8; ++bb)
  3231. {
  3232. if(((CCreGenObjInfo*)CGI->objh->objInstances[j]->info)->castles[0] & (1<<bb))
  3233. {
  3234. possibleTowns.push_back(bb);
  3235. }
  3236. }
  3237. if(((CCreGenObjInfo*)CGI->objh->objInstances[j]->info)->castles[1])
  3238. possibleTowns.push_back(8);
  3239. nxt->subid = possibleTowns[rand()%possibleTowns.size()];
  3240. int lvl = atoi(CGI->objh->objInstances[j]->defInfo->name.substr(7, 8).c_str())-1;
  3241. nxt->name = creGenNames[nxt->subid][lvl];
  3242. if(creGenNumbers[nxt->subid][lvl]!=NULL)
  3243. {
  3244. CGI->objh->objInstances[j]->defInfo = creGenNumbers[nxt->subid][lvl];
  3245. continue;
  3246. }
  3247. std::vector<DefObjInfo>::iterator pit = std::find(CGameInfo::mainObj->dobjinfo->objs.begin(), CGameInfo::mainObj->dobjinfo->objs.end(),
  3248. nxt->name);
  3249. if(pit == CGameInfo::mainObj->dobjinfo->objs.end())
  3250. {
  3251. nxt->isOnDefList = false;
  3252. }
  3253. else
  3254. {
  3255. nxt->printPriority = pit->priority;
  3256. nxt->isOnDefList = true;
  3257. }
  3258. map.defy.push_back(nxt); // add this def to the vector
  3259. defsToUnpack.push_back(nxt->name);
  3260. CGI->objh->objInstances[j]->defInfo = nxt;
  3261. if(creGenNumbers[nxt->subid][lvl]==NULL)
  3262. {
  3263. creGenNumbers[nxt->subid][lvl] = nxt;
  3264. }
  3265. }
  3266. break;
  3267. }
  3268. case EDefType::CREGEN2_DEF:
  3269. {
  3270. if(((CCreGenObjInfo*)CGI->objh->objInstances[j]->info)->asCastle)
  3271. {
  3272. CGDefInfo * nxt = curDef;
  3273. nxt->id = 17;
  3274. for(int vv=0; vv<CGI->objh->objInstances.size(); ++vv)
  3275. {
  3276. if(getDefType(CGI->objh->objInstances[vv]->defInfo)==EDefType::TOWN_DEF)
  3277. {
  3278. if(
  3279. ((CCastleObjInfo*)CGI->objh->objInstances[vv]->info)->bytes[0]==((CCreGen2ObjInfo*)CGI->objh->objInstances[j]->info)->bytes[0]
  3280. && ((CCastleObjInfo*)CGI->objh->objInstances[vv]->info)->bytes[1]==((CCreGen2ObjInfo*)CGI->objh->objInstances[j]->info)->bytes[1]
  3281. && ((CCastleObjInfo*)CGI->objh->objInstances[vv]->info)->bytes[2]==((CCreGen2ObjInfo*)CGI->objh->objInstances[j]->info)->bytes[2]
  3282. && ((CCastleObjInfo*)CGI->objh->objInstances[vv]->info)->bytes[3]==((CCreGen2ObjInfo*)CGI->objh->objInstances[j]->info)->bytes[3])
  3283. {
  3284. for(int mm=0; mm<town1DefNames.size(); ++mm)
  3285. {
  3286. std::transform(town1DefNames[mm].begin(), town1DefNames[mm].end(), town1DefNames[mm].begin(), (int(*)(int))toupper);
  3287. std::string hlp = CGI->objh->objInstances[vv]->defInfo->name;
  3288. std::transform(hlp.begin(), hlp.end(), hlp.begin(), (int(*)(int))toupper);
  3289. if(town1DefNames[mm]==hlp)
  3290. {
  3291. nxt->subid = mm;
  3292. }
  3293. }
  3294. }
  3295. }
  3296. }
  3297. int lvl;
  3298. if((((CCreGen2ObjInfo*)CGI->objh->objInstances[j]->info)->maxLevel - ((CCreGen2ObjInfo*)CGI->objh->objInstances[j]->info)->minLevel)!=0)
  3299. lvl = rand()%(((CCreGen2ObjInfo*)CGI->objh->objInstances[j]->info)->maxLevel - ((CCreGen2ObjInfo*)CGI->objh->objInstances[j]->info)->minLevel) + ((CCreGen2ObjInfo*)CGI->objh->objInstances[j]->info)->minLevel;
  3300. else lvl = 0;
  3301. nxt->name = creGenNames[nxt->subid][lvl];
  3302. if(creGenNumbers[nxt->subid][lvl]!=NULL)
  3303. {
  3304. CGI->objh->objInstances[j]->defInfo = creGenNumbers[nxt->subid][lvl];
  3305. continue;
  3306. }
  3307. std::vector<DefObjInfo>::iterator pit = std::find(CGameInfo::mainObj->dobjinfo->objs.begin(), CGameInfo::mainObj->dobjinfo->objs.end(),
  3308. nxt->name);
  3309. if(pit == CGameInfo::mainObj->dobjinfo->objs.end())
  3310. {
  3311. nxt->isOnDefList = false;
  3312. }
  3313. else
  3314. {
  3315. nxt->printPriority = pit->priority;
  3316. nxt->isOnDefList = true;
  3317. }
  3318. map.defy.push_back(nxt); // add this def to the vector
  3319. defsToUnpack.push_back(nxt->name);
  3320. CGI->objh->objInstances[j]->defInfo = nxt;
  3321. if(creGenNumbers[nxt->subid][lvl]==NULL)
  3322. {
  3323. creGenNumbers[nxt->subid][lvl] = nxt;
  3324. }
  3325. }
  3326. else //if not as castle
  3327. {
  3328. CGDefInfo * nxt = curDef;
  3329. nxt->id = 17;
  3330. std::vector<int> possibleTowns;
  3331. for(int bb=0; bb<8; ++bb)
  3332. {
  3333. if(((CCreGen2ObjInfo*)CGI->objh->objInstances[j]->info)->castles[0] & (1<<bb))
  3334. {
  3335. possibleTowns.push_back(bb);
  3336. }
  3337. }
  3338. if(((CCreGen2ObjInfo*)CGI->objh->objInstances[j]->info)->castles[1])
  3339. possibleTowns.push_back(8);
  3340. nxt->subid = possibleTowns[rand()%possibleTowns.size()];
  3341. int lvl = rand()%(((CCreGen2ObjInfo*)CGI->objh->objInstances[j]->info)->maxLevel - ((CCreGen2ObjInfo*)CGI->objh->objInstances[j]->info)->minLevel) + ((CCreGen2ObjInfo*)CGI->objh->objInstances[j]->info)->minLevel;
  3342. nxt->name = creGenNames[nxt->subid][lvl];
  3343. if(creGenNumbers[nxt->subid][lvl]!=NULL)
  3344. {
  3345. CGI->objh->objInstances[j]->defInfo = creGenNumbers[nxt->subid][lvl];
  3346. continue;
  3347. }
  3348. std::vector<DefObjInfo>::iterator pit = std::find(CGameInfo::mainObj->dobjinfo->objs.begin(), CGameInfo::mainObj->dobjinfo->objs.end(),
  3349. nxt->name);
  3350. if(pit == CGameInfo::mainObj->dobjinfo->objs.end())
  3351. {
  3352. nxt->isOnDefList = false;
  3353. }
  3354. else
  3355. {
  3356. nxt->printPriority = pit->priority;
  3357. nxt->isOnDefList = true;
  3358. }
  3359. map.defy.push_back(nxt); // add this def to the vector
  3360. defsToUnpack.push_back(nxt->name);
  3361. CGI->objh->objInstances[j]->defInfo = nxt;
  3362. if(creGenNumbers[nxt->subid][lvl]==NULL)
  3363. {
  3364. creGenNumbers[nxt->subid][lvl] = nxt;
  3365. }
  3366. }
  3367. }
  3368. case EDefType::CREGEN3_DEF:
  3369. {
  3370. CGDefInfo * nxt = curDef;
  3371. nxt->id = 17;
  3372. nxt->subid = atoi(CGI->objh->objInstances[j]->defInfo->name.substr(7, 8).c_str());
  3373. int lvl = -1;
  3374. CCreGen3ObjInfo * ct = (CCreGen3ObjInfo*)CGI->objh->objInstances[j]->info;
  3375. if(ct->maxLevel>7)
  3376. ct->maxLevel = 7;
  3377. if(ct->minLevel<1)
  3378. ct->minLevel = 1;
  3379. if((((CCreGen3ObjInfo*)CGI->objh->objInstances[j]->info)->maxLevel - ((CCreGen3ObjInfo*)CGI->objh->objInstances[j]->info)->minLevel)!=0)
  3380. lvl = rand()%(((CCreGen3ObjInfo*)CGI->objh->objInstances[j]->info)->maxLevel - ((CCreGen3ObjInfo*)CGI->objh->objInstances[j]->info)->minLevel) + ((CCreGen3ObjInfo*)CGI->objh->objInstances[j]->info)->minLevel;
  3381. else
  3382. lvl = ((CCreGen3ObjInfo*)CGI->objh->objInstances[j]->info)->maxLevel;
  3383. nxt->name = creGenNames[nxt->subid][lvl];
  3384. if(creGenNumbers[nxt->subid][lvl]!=NULL)
  3385. {
  3386. CGI->objh->objInstances[j]->defInfo = creGenNumbers[nxt->subid][lvl];
  3387. continue;
  3388. }
  3389. std::vector<DefObjInfo>::iterator pit = std::find(CGameInfo::mainObj->dobjinfo->objs.begin(), CGameInfo::mainObj->dobjinfo->objs.end(),
  3390. nxt->name);
  3391. if(pit == CGameInfo::mainObj->dobjinfo->objs.end())
  3392. {
  3393. nxt->isOnDefList = false;
  3394. }
  3395. else
  3396. {
  3397. nxt->printPriority = pit->priority;
  3398. nxt->isOnDefList = true;
  3399. }
  3400. map.defy.push_back(nxt); // add this def to the vector
  3401. defsToUnpack.push_back(nxt->name);
  3402. CGI->objh->objInstances[j]->defInfo = nxt;
  3403. if(creGenNumbers[nxt->subid][lvl]==NULL)
  3404. {
  3405. creGenNumbers[nxt->subid][lvl] = nxt;
  3406. }
  3407. break;
  3408. }
  3409. }//end of main switch
  3410. } //end of sencond for loop
  3411. }