CAmbarCendamo.cpp 93 KB

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