CAmbarCendamo.cpp 104 KB

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