CAmbarCendamo.cpp 93 KB

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