CAmbarCendamo.cpp 104 KB

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