CAmbarCendamo.cpp 94 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206
  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. if(artID!=255) //not none quest
  1210. {
  1211. spec->m5arts.push_back(&(CGameInfo::mainObj->arth->artifacts[artID]));
  1212. spec->missionType = 5;
  1213. }
  1214. else
  1215. {
  1216. spec->missionType = 255;
  1217. }
  1218. }
  1219. if(spec->missionType!=255)
  1220. {
  1221. unsigned char rewardType = bufor[i]; ++i;
  1222. spec->rewardType = rewardType;
  1223. switch(rewardType)
  1224. {
  1225. case 1:
  1226. {
  1227. spec->r1exp = readNormalNr(i); i+=4;
  1228. break;
  1229. }
  1230. case 2:
  1231. {
  1232. spec->r2mana = readNormalNr(i); i+=4;
  1233. break;
  1234. }
  1235. case 3:
  1236. {
  1237. spec->r3morale = bufor[i]; ++i;
  1238. break;
  1239. }
  1240. case 4:
  1241. {
  1242. spec->r4luck = bufor[i]; ++i;
  1243. break;
  1244. }
  1245. case 5:
  1246. {
  1247. spec->r5type = bufor[i]; ++i;
  1248. spec->r5amount = readNormalNr(i, 3); i+=3;
  1249. i+=1;
  1250. break;
  1251. }
  1252. case 6:
  1253. {
  1254. spec->r6type = bufor[i]; ++i;
  1255. spec->r6amount = bufor[i]; ++i;
  1256. break;
  1257. }
  1258. case 7:
  1259. {
  1260. int abid = bufor[i]; ++i;
  1261. spec->r7ability = CGameInfo::mainObj->abilh->abilities[abid];
  1262. spec->r7level = bufor[i]; ++i;
  1263. break;
  1264. }
  1265. case 8:
  1266. {
  1267. int artid = readNormalNr(i, (map.version == RoE ? 1 : 2)); i+=(map.version == RoE ? 1 : 2);
  1268. spec->r8art = &(CGameInfo::mainObj->arth->artifacts[artid]);
  1269. break;
  1270. }
  1271. case 9:
  1272. {
  1273. int spellid = bufor[i]; ++i;
  1274. spec->r9spell = &(CGameInfo::mainObj->spellh->spells[spellid]);
  1275. break;
  1276. }
  1277. case 10:
  1278. {
  1279. int creid = readNormalNr(i, 2); i+=2;
  1280. spec->r10creature = &(CGameInfo::mainObj->creh->creatures[creid]);
  1281. spec->r10amount = readNormalNr(i, 2); i+=2;
  1282. break;
  1283. }
  1284. }// end of internal switch
  1285. i+=2;
  1286. }
  1287. else //missionType==255
  1288. {
  1289. i+=3;
  1290. }
  1291. nobj->info = spec;
  1292. break;
  1293. }
  1294. case EDefType::WITCHHUT_DEF:
  1295. {
  1296. CWitchHutObjInfo * spec = new CWitchHutObjInfo;
  1297. if(map.version>RoE) //in reo we cannot specify it - all are allowed (I hope)
  1298. {
  1299. ist=i; //starting i for loop
  1300. for(i; i<ist+4; ++i)
  1301. {
  1302. unsigned char c = bufor[i];
  1303. for(int yy=0; yy<8; ++yy)
  1304. {
  1305. if((i-ist)*8+yy < CGameInfo::mainObj->abilh->abilities.size())
  1306. {
  1307. if(c == (c|((unsigned char)intPow(2, yy))))
  1308. spec->allowedAbilities.push_back(CGameInfo::mainObj->abilh->abilities[(i-ist)*8+yy]);
  1309. }
  1310. }
  1311. }
  1312. }
  1313. else //(RoE map)
  1314. {
  1315. for(int gg=0; gg<CGameInfo::mainObj->abilh->abilities.size(); ++gg)
  1316. {
  1317. spec->allowedAbilities.push_back(CGameInfo::mainObj->abilh->abilities[gg]);
  1318. }
  1319. }
  1320. nobj->info = spec;
  1321. break;
  1322. }
  1323. case EDefType::SCHOLAR_DEF:
  1324. {
  1325. CScholarObjInfo * spec = new CScholarObjInfo;
  1326. spec->bonusType = bufor[i]; ++i;
  1327. switch(spec->bonusType)
  1328. {
  1329. case 0xff:
  1330. ++i;
  1331. break;
  1332. case 0:
  1333. spec->r0type = bufor[i]; ++i;
  1334. break;
  1335. case 1:
  1336. spec->r1 = CGameInfo::mainObj->abilh->abilities[bufor[i]]; ++i;
  1337. break;
  1338. case 2:
  1339. spec->r2 = &(CGameInfo::mainObj->spellh->spells[bufor[i]]); ++i;
  1340. break;
  1341. }
  1342. i+=6;
  1343. nobj->info = spec;
  1344. break;
  1345. }
  1346. case EDefType::GARRISON_DEF:
  1347. {
  1348. CGarrisonObjInfo * spec = new CGarrisonObjInfo;
  1349. spec->player = bufor[i]; ++i;
  1350. i+=3;
  1351. spec->units = readCreatureSet(i); i+= (map.version==RoE ? 21 : 28);
  1352. if(map.version > RoE)
  1353. {
  1354. spec->movableUnits = bufor[i]; ++i;
  1355. }
  1356. else
  1357. spec->movableUnits = true;
  1358. i+=8;
  1359. nobj->setOwner(spec->player);
  1360. nobj->info = spec;
  1361. break;
  1362. }
  1363. case EDefType::ARTIFACT_DEF:
  1364. {
  1365. CArtifactObjInfo * spec = new CArtifactObjInfo;
  1366. bool areSettings = bufor[i]; ++i;
  1367. if(areSettings)
  1368. {
  1369. int messLength = readNormalNr(i, 4); i+=4;
  1370. for(int hh=0; hh<messLength; ++hh)
  1371. {
  1372. spec->message += bufor[i]; ++i;
  1373. }
  1374. bool areGuards = bufor[i]; ++i;
  1375. if(areGuards)
  1376. {
  1377. spec->areGuards = true;
  1378. spec->guards = readCreatureSet(i); i+= (map.version == RoE ? 21 : 28) ;
  1379. }
  1380. else
  1381. spec->areGuards = false;
  1382. i+=4;
  1383. }
  1384. nobj->info = spec;
  1385. break;
  1386. }
  1387. case EDefType::RESOURCE_DEF:
  1388. {
  1389. CResourceObjInfo * spec = new CResourceObjInfo;
  1390. bool isMessGuard = bufor[i]; ++i;
  1391. if(isMessGuard)
  1392. {
  1393. int messLength = readNormalNr(i); i+=4;
  1394. for(int mm=0; mm<messLength; ++mm)
  1395. {
  1396. spec->message+=bufor[i]; ++i;
  1397. }
  1398. spec->areGuards = bufor[i]; ++i;
  1399. if(spec->areGuards)
  1400. {
  1401. spec->guards = readCreatureSet(i); i+= (map.version == RoE ? 21 : 28);
  1402. }
  1403. i+=4;
  1404. }
  1405. else
  1406. {
  1407. spec->areGuards = false;
  1408. }
  1409. spec->amount = readNormalNr(i); i+=4;
  1410. i+=4;
  1411. nobj->info = spec;
  1412. break;
  1413. }
  1414. case EDefType::TOWN_DEF:
  1415. {
  1416. CCastleObjInfo * spec = new CCastleObjInfo;
  1417. if(map.version!=RoE)
  1418. {
  1419. spec->bytes[0] = bufor[i]; ++i;
  1420. spec->bytes[1] = bufor[i]; ++i;
  1421. spec->bytes[2] = bufor[i]; ++i;
  1422. spec->bytes[3] = bufor[i]; ++i;
  1423. }
  1424. else
  1425. {
  1426. spec->bytes[0] = 0;
  1427. spec->bytes[1] = 0;
  1428. spec->bytes[2] = 0;
  1429. spec->bytes[3] = 0;
  1430. }
  1431. spec->player = bufor[i]; ++i;
  1432. bool hasName = bufor[i]; ++i;
  1433. if(hasName)
  1434. {
  1435. int len = readNormalNr(i); i+=4;
  1436. for(int gg=0; gg<len; ++gg)
  1437. {
  1438. spec->name += bufor[i]; ++i;
  1439. }
  1440. }
  1441. bool stGarr = bufor[i]; ++i; //true if garrison isn't empty
  1442. if(stGarr)
  1443. {
  1444. spec->garrison = readCreatureSet(i); i+=( map.version > RoE ? 28 : 21 );
  1445. }
  1446. spec->garrison.formation = bufor[i]; ++i;
  1447. spec->unusualBuildins = bufor[i]; ++i;
  1448. if(spec->unusualBuildins)
  1449. {
  1450. for(int ff=0; ff<12; ++ff)
  1451. {
  1452. spec->buildingSettings[ff] = bufor[i]; ++i;
  1453. }
  1454. }
  1455. else
  1456. {
  1457. spec->hasFort = bufor[i]; ++i;
  1458. }
  1459. int ist = i;
  1460. if(map.version>RoE)
  1461. {
  1462. for(i; i<ist+9; ++i)
  1463. {
  1464. unsigned char c = bufor[i];
  1465. for(int yy=0; yy<8; ++yy)
  1466. {
  1467. if((i-ist)*8+yy < CGameInfo::mainObj->spellh->spells.size())
  1468. {
  1469. if(c == (c|((unsigned char)intPow(2, yy))))
  1470. spec->obligatorySpells.push_back(&(CGameInfo::mainObj->spellh->spells[(i-ist)*8+yy]));
  1471. }
  1472. }
  1473. }
  1474. }
  1475. ist = i;
  1476. for(i; i<ist+9; ++i)
  1477. {
  1478. unsigned char c = bufor[i];
  1479. for(int yy=0; yy<8; ++yy)
  1480. {
  1481. if((i-ist)*8+yy < CGameInfo::mainObj->spellh->spells.size())
  1482. {
  1483. if(c != (c|((unsigned char)intPow(2, yy))))
  1484. spec->possibleSpells.push_back(&(CGameInfo::mainObj->spellh->spells[(i-ist)*8+yy]));
  1485. }
  1486. }
  1487. }
  1488. /////// reading castle events //////////////////////////////////
  1489. int numberOfEvent = readNormalNr(i); i+=4;
  1490. for(int gh = 0; gh<numberOfEvent; ++gh)
  1491. {
  1492. CCastleEvent nce;
  1493. int nameLen = readNormalNr(i); i+=4;
  1494. for(int ll=0; ll<nameLen; ++ll)
  1495. {
  1496. nce.name += bufor[i]; ++i;
  1497. }
  1498. int messLen = readNormalNr(i); i+=4;
  1499. for(int ll=0; ll<messLen; ++ll)
  1500. {
  1501. nce.message += bufor[i]; ++i;
  1502. }
  1503. nce.wood = readNormalNr(i); i+=4;
  1504. nce.mercury = readNormalNr(i); i+=4;
  1505. nce.ore = readNormalNr(i); i+=4;
  1506. nce.sulfur = readNormalNr(i); i+=4;
  1507. nce.crystal = readNormalNr(i); i+=4;
  1508. nce.gems = readNormalNr(i); i+=4;
  1509. nce.gold = readNormalNr(i); i+=4;
  1510. nce.players = bufor[i]; ++i;
  1511. if(map.version > AB)
  1512. {
  1513. nce.forHuman = bufor[i]; ++i;
  1514. }
  1515. else
  1516. nce.forHuman = true;
  1517. nce.forComputer = bufor[i]; ++i;
  1518. nce.firstShow = readNormalNr(i, 2); i+=2;
  1519. nce.forEvery = bufor[i]; ++i;
  1520. i+=17;
  1521. for(int kk=0; kk<6; ++kk)
  1522. {
  1523. nce.bytes[kk] = bufor[i]; ++i;
  1524. }
  1525. for(int vv=0; vv<7; ++vv)
  1526. {
  1527. nce.gen[vv] = readNormalNr(i, 2); i+=2;
  1528. }
  1529. i+=4;
  1530. spec->events.push_back(nce);
  1531. }
  1532. spec->x = nobj->pos.x;
  1533. spec->y = nobj->pos.y;
  1534. spec->z = nobj->pos.z;
  1535. /////// castle events have been read ///////////////////////////
  1536. if(map.version > AB)
  1537. {
  1538. spec->alignment = bufor[i]; ++i;
  1539. }
  1540. else
  1541. spec->alignment = 0xff;
  1542. i+=3;
  1543. nobj->info = spec;
  1544. //////////// rewriting info to CTownInstance class /////////////////////
  1545. CGTownInstance * nt = new CGTownInstance();
  1546. (*(static_cast<CGObjectInstance*>(nt))) = *nobj;
  1547. delete nobj;
  1548. nobj = nt;
  1549. nt->setOwner(spec->player);
  1550. nt->town = &CGI->townh->towns[nt->defInfo->subid];
  1551. nt->builded = 0;
  1552. nt->destroyed = 0;
  1553. nt->name = spec->name;
  1554. nt->garrison = spec->garrison;
  1555. nt->garrisonHero = NULL;// spec->garnisonHero is not readed - TODO: readit
  1556. nt->pos = int3(spec->x, spec->y, spec->z);
  1557. nt->possibleSpells = spec->possibleSpells;
  1558. nt->obligatorySpells = spec->obligatorySpells;
  1559. nt->availableSpells = std::vector<CSpell*>();
  1560. nt->creatureIncome = std::vector<int>();
  1561. nt->creaturesLeft = std::vector<int>();
  1562. CGI->townh->townInstances.push_back(nt);
  1563. break;
  1564. }
  1565. case EDefType::PLAYERONLY_DEF:
  1566. {
  1567. CPlayerOnlyObjInfo * spec = new CPlayerOnlyObjInfo;
  1568. spec->player = bufor[i]; ++i;
  1569. i+=3;
  1570. nobj->setOwner(spec->player);
  1571. nobj->info = spec;
  1572. break;
  1573. }
  1574. case EDefType::SHRINE_DEF:
  1575. {
  1576. CShrineObjInfo * spec = new CShrineObjInfo;
  1577. spec->spell = bufor[i]; i+=4;
  1578. nobj->info = spec;
  1579. break;
  1580. }
  1581. case EDefType::SPELLSCROLL_DEF:
  1582. {
  1583. CSpellScrollObjinfo * spec = new CSpellScrollObjinfo;
  1584. bool messg = bufor[i]; ++i;
  1585. if(messg)
  1586. {
  1587. int mLength = readNormalNr(i); i+=4;
  1588. for(int vv=0; vv<mLength; ++vv)
  1589. {
  1590. spec->message += bufor[i]; ++i;
  1591. }
  1592. spec->areGuarders = bufor[i]; ++i;
  1593. if(spec->areGuarders)
  1594. {
  1595. spec->guarders = readCreatureSet(i); i+=( map.version == RoE ? 21 : 28 );
  1596. }
  1597. i+=4;
  1598. }
  1599. spec->spell = &(CGameInfo::mainObj->spellh->spells[bufor[i]]); ++i;
  1600. i+=3;
  1601. nobj->info = spec;
  1602. break;
  1603. }
  1604. case EDefType::PANDORA_DEF:
  1605. {
  1606. CPandorasBoxObjInfo * spec = new CPandorasBoxObjInfo;
  1607. bool messg = bufor[i]; ++i;
  1608. if(messg)
  1609. {
  1610. int mLength = readNormalNr(i); i+=4;
  1611. for(int vv=0; vv<mLength; ++vv)
  1612. {
  1613. spec->message += bufor[i]; ++i;
  1614. }
  1615. spec->areGuarders = bufor[i]; ++i;
  1616. if(spec->areGuarders)
  1617. {
  1618. spec->guarders = readCreatureSet(i); i+= (map.version == RoE ? 21 : 28);
  1619. }
  1620. i+=4;
  1621. }
  1622. ////// copied form event handling (seems to be similar)
  1623. spec->gainedExp = readNormalNr(i, 4); i+=4;
  1624. spec->manaDiff = readNormalNr(i, 4); i+=4;
  1625. spec->moraleDiff = readNormalNr(i, 1, true); ++i;
  1626. spec->luckDiff = readNormalNr(i, 1, true); ++i;
  1627. spec->wood = readNormalNr(i); i+=4;
  1628. spec->mercury = readNormalNr(i); i+=4;
  1629. spec->ore = readNormalNr(i); i+=4;
  1630. spec->sulfur = readNormalNr(i); i+=4;
  1631. spec->crystal = readNormalNr(i); i+=4;
  1632. spec->gems = readNormalNr(i); i+=4;
  1633. spec->gold = readNormalNr(i); i+=4;
  1634. spec->attack = readNormalNr(i, 1); ++i;
  1635. spec->defence = readNormalNr(i, 1); ++i;
  1636. spec->power = readNormalNr(i, 1); ++i;
  1637. spec->knowledge = readNormalNr(i, 1); ++i;
  1638. int gabn; //number of gained abilities
  1639. gabn = readNormalNr(i, 1); ++i;
  1640. for(int oo = 0; oo<gabn; ++oo)
  1641. {
  1642. spec->abilities.push_back((CGameInfo::mainObj->abilh)->abilities[readNormalNr(i, 1)]); ++i;
  1643. spec->abilityLevels.push_back(readNormalNr(i, 1)); ++i;
  1644. }
  1645. int gart = readNormalNr(i, 1); ++i; //number of gained artifacts
  1646. for(int oo = 0; oo<gart; ++oo)
  1647. {
  1648. spec->artifacts.push_back(&(CGameInfo::mainObj->arth->artifacts[readNormalNr(i, 2)])); i+=2;
  1649. }
  1650. int gspel = readNormalNr(i, 1); ++i; //number of gained spells
  1651. for(int oo = 0; oo<gspel; ++oo)
  1652. {
  1653. spec->spells.push_back(&(CGameInfo::mainObj->spellh->spells[readNormalNr(i, 1)])); ++i;
  1654. }
  1655. int gcre = readNormalNr(i, 1); ++i; //number of gained creatures
  1656. spec->creatures = readCreatureSet(i, gcre); i+=4*gcre;
  1657. i+=8;
  1658. nobj->info = spec;
  1659. ///////end of copied fragment
  1660. break;
  1661. }
  1662. case EDefType::GRAIL_DEF:
  1663. {
  1664. CGrailObjInfo * spec = new CGrailObjInfo;
  1665. spec->radius = readNormalNr(i); i+=4;
  1666. nobj->info = spec;
  1667. break;
  1668. }
  1669. case EDefType::CREGEN_DEF:
  1670. {
  1671. CCreGenObjInfo * spec = new CCreGenObjInfo;
  1672. spec->player = bufor[i]; ++i;
  1673. i+=3;
  1674. for(int ggg=0; ggg<4; ++ggg)
  1675. {
  1676. spec->bytes[ggg] = bufor[i]; ++i;
  1677. }
  1678. if((spec->bytes[0] == '\0') && (spec->bytes[1] == '\0') && (spec->bytes[2] == '\0') && (spec->bytes[3] == '\0'))
  1679. {
  1680. spec->asCastle = false;
  1681. spec->castles[0] = bufor[i]; ++i;
  1682. spec->castles[1] = bufor[i]; ++i;
  1683. }
  1684. else
  1685. {
  1686. spec->asCastle = true;
  1687. }
  1688. nobj->setOwner(spec->player);
  1689. nobj->info = spec;
  1690. break;
  1691. }
  1692. case EDefType::CREGEN2_DEF:
  1693. {
  1694. CCreGen2ObjInfo * spec = new CCreGen2ObjInfo;
  1695. spec->player = bufor[i]; ++i;
  1696. i+=3;
  1697. for(int ggg=0; ggg<4; ++ggg)
  1698. {
  1699. spec->bytes[ggg] = bufor[i]; ++i;
  1700. }
  1701. if((spec->bytes[0] == '\0') && (spec->bytes[1] == '\0') && (spec->bytes[2] == '\0') && (spec->bytes[3] == '\0'))
  1702. {
  1703. spec->asCastle = false;
  1704. spec->castles[0] = bufor[i]; ++i;
  1705. spec->castles[1] = bufor[i]; ++i;
  1706. }
  1707. else
  1708. {
  1709. spec->asCastle = true;
  1710. }
  1711. spec->minLevel = bufor[i]; ++i;
  1712. spec->maxLevel = bufor[i]; ++i;
  1713. if(spec->maxLevel>7)
  1714. spec->maxLevel = 7;
  1715. if(spec->minLevel<1)
  1716. spec->minLevel = 1;
  1717. nobj->setOwner(spec->player);
  1718. nobj->info = spec;
  1719. break;
  1720. }
  1721. case EDefType::CREGEN3_DEF:
  1722. {
  1723. CCreGen3ObjInfo * spec = new CCreGen3ObjInfo;
  1724. spec->player = bufor[i]; ++i;
  1725. i+=3;
  1726. spec->minLevel = bufor[i]; ++i;
  1727. spec->maxLevel = bufor[i]; ++i;
  1728. if(spec->maxLevel>7)
  1729. spec->maxLevel = 7;
  1730. if(spec->minLevel<1)
  1731. spec->minLevel = 1;
  1732. nobj->setOwner(spec->player);
  1733. nobj->info = spec;
  1734. break;
  1735. }
  1736. case EDefType::BORDERGUARD_DEF:
  1737. {
  1738. CBorderGuardObjInfo * spec = new CBorderGuardObjInfo;
  1739. spec->missionType = bufor[i]; ++i;
  1740. switch(spec->missionType)
  1741. {
  1742. case 1:
  1743. {
  1744. spec->m1level = readNormalNr(i); i+=4;
  1745. int limit = readNormalNr(i); i+=4;
  1746. if(limit == ((int)0xffffffff))
  1747. {
  1748. spec->isDayLimit = false;
  1749. spec->lastDay = -1;
  1750. }
  1751. else
  1752. {
  1753. spec->isDayLimit = true;
  1754. spec->lastDay = limit;
  1755. }
  1756. break;
  1757. }
  1758. case 2:
  1759. {
  1760. spec->m2attack = bufor[i]; ++i;
  1761. spec->m2defence = bufor[i]; ++i;
  1762. spec->m2power = bufor[i]; ++i;
  1763. spec->m2knowledge = bufor[i]; ++i;
  1764. int limit = readNormalNr(i); i+=4;
  1765. if(limit == ((int)0xffffffff))
  1766. {
  1767. spec->isDayLimit = false;
  1768. spec->lastDay = -1;
  1769. }
  1770. else
  1771. {
  1772. spec->isDayLimit = true;
  1773. spec->lastDay = limit;
  1774. }
  1775. break;
  1776. }
  1777. case 3:
  1778. {
  1779. spec->m3bytes[0] = bufor[i]; ++i;
  1780. spec->m3bytes[1] = bufor[i]; ++i;
  1781. spec->m3bytes[2] = bufor[i]; ++i;
  1782. spec->m3bytes[3] = bufor[i]; ++i;
  1783. int limit = readNormalNr(i); i+=4;
  1784. if(limit == ((int)0xffffffff))
  1785. {
  1786. spec->isDayLimit = false;
  1787. spec->lastDay = -1;
  1788. }
  1789. else
  1790. {
  1791. spec->isDayLimit = true;
  1792. spec->lastDay = limit;
  1793. }
  1794. break;
  1795. }
  1796. case 4:
  1797. {
  1798. spec->m4bytes[0] = bufor[i]; ++i;
  1799. spec->m4bytes[1] = bufor[i]; ++i;
  1800. spec->m4bytes[2] = bufor[i]; ++i;
  1801. spec->m4bytes[3] = bufor[i]; ++i;
  1802. int limit = readNormalNr(i); i+=4;
  1803. if(limit == ((int)0xffffffff))
  1804. {
  1805. spec->isDayLimit = false;
  1806. spec->lastDay = -1;
  1807. }
  1808. else
  1809. {
  1810. spec->isDayLimit = true;
  1811. spec->lastDay = limit;
  1812. }
  1813. break;
  1814. }
  1815. case 5:
  1816. {
  1817. int artNumber = bufor[i]; ++i;
  1818. for(int yy=0; yy<artNumber; ++yy)
  1819. {
  1820. int artid = readNormalNr(i, 2); i+=2;
  1821. spec->m5arts.push_back(&(CGameInfo::mainObj->arth->artifacts[artid]));
  1822. }
  1823. int limit = readNormalNr(i); i+=4;
  1824. if(limit == ((int)0xffffffff))
  1825. {
  1826. spec->isDayLimit = false;
  1827. spec->lastDay = -1;
  1828. }
  1829. else
  1830. {
  1831. spec->isDayLimit = true;
  1832. spec->lastDay = limit;
  1833. }
  1834. break;
  1835. }
  1836. case 6:
  1837. {
  1838. int typeNumber = bufor[i]; ++i;
  1839. for(int hh=0; hh<typeNumber; ++hh)
  1840. {
  1841. int creType = readNormalNr(i, 2); i+=2;
  1842. int creNumb = readNormalNr(i, 2); i+=2;
  1843. spec->m6cre.push_back(&(CGameInfo::mainObj->creh->creatures[creType]));
  1844. spec->m6number.push_back(creNumb);
  1845. }
  1846. int limit = readNormalNr(i); i+=4;
  1847. if(limit == ((int)0xffffffff))
  1848. {
  1849. spec->isDayLimit = false;
  1850. spec->lastDay = -1;
  1851. }
  1852. else
  1853. {
  1854. spec->isDayLimit = true;
  1855. spec->lastDay = limit;
  1856. }
  1857. break;
  1858. }
  1859. case 7:
  1860. {
  1861. spec->m7wood = readNormalNr(i); i+=4;
  1862. spec->m7mercury = readNormalNr(i); i+=4;
  1863. spec->m7ore = readNormalNr(i); i+=4;
  1864. spec->m7sulfur = readNormalNr(i); i+=4;
  1865. spec->m7crystal = readNormalNr(i); i+=4;
  1866. spec->m7gems = readNormalNr(i); i+=4;
  1867. spec->m7gold = readNormalNr(i); i+=4;
  1868. int limit = readNormalNr(i); i+=4;
  1869. if(limit == ((int)0xffffffff))
  1870. {
  1871. spec->isDayLimit = false;
  1872. spec->lastDay = -1;
  1873. }
  1874. else
  1875. {
  1876. spec->isDayLimit = true;
  1877. spec->lastDay = limit;
  1878. }
  1879. break;
  1880. }
  1881. case 8:
  1882. {
  1883. int heroType = bufor[i]; ++i;
  1884. spec->m8hero = CGameInfo::mainObj->heroh->heroes[heroType];
  1885. int limit = readNormalNr(i); i+=4;
  1886. if(limit == ((int)0xffffffff))
  1887. {
  1888. spec->isDayLimit = false;
  1889. spec->lastDay = -1;
  1890. }
  1891. else
  1892. {
  1893. spec->isDayLimit = true;
  1894. spec->lastDay = limit;
  1895. }
  1896. break;
  1897. }
  1898. case 9:
  1899. {
  1900. spec->m9player = bufor[i]; ++i;
  1901. int limit = readNormalNr(i); i+=4;
  1902. if(limit == ((int)0xffffffff))
  1903. {
  1904. spec->isDayLimit = false;
  1905. spec->lastDay = -1;
  1906. }
  1907. else
  1908. {
  1909. spec->isDayLimit = true;
  1910. spec->lastDay = limit;
  1911. }
  1912. break;
  1913. }
  1914. }//internal switch end (seer huts)
  1915. int len1 = readNormalNr(i); i+=4;
  1916. for(int ee=0; ee<len1; ++ee)
  1917. {
  1918. spec->firstVisitText += bufor[i]; ++i;
  1919. }
  1920. int len2 = readNormalNr(i); i+=4;
  1921. for(int ee=0; ee<len2; ++ee)
  1922. {
  1923. spec->nextVisitText += bufor[i]; ++i;
  1924. }
  1925. int len3 = readNormalNr(i); i+=4;
  1926. for(int ee=0; ee<len3; ++ee)
  1927. {
  1928. spec->completedText += bufor[i]; ++i;
  1929. }
  1930. nobj->info = spec;
  1931. break;
  1932. }
  1933. } //end of main switch
  1934. CGameInfo::mainObj->objh->objInstances.push_back(nobj);
  1935. //TODO - dokoñczyæ, du¿o do zrobienia - trzeba patrzeæ, co def niesie
  1936. }//*/ //end of loading objects; commented to make application work until it will be finished
  1937. ////objects loaded
  1938. //processMap(defsToUnpack);
  1939. std::vector<CDefHandler *> dhandlers = CGameInfo::mainObj->spriteh->extractManyFiles(defsToUnpack);
  1940. for (int i=0;i<dhandlers.size();i++)
  1941. map.defy[i]->handler=dhandlers[i];
  1942. for(int vv=0; vv<map.defy.size(); ++vv)
  1943. {
  1944. if(map.defy[vv]->handler->alphaTransformed)
  1945. continue;
  1946. for(int yy=0; yy<map.defy[vv]->handler->ourImages.size(); ++yy)
  1947. {
  1948. map.defy[vv]->handler->ourImages[yy].bitmap = CSDL_Ext::alphaTransform(map.defy[vv]->handler->ourImages[yy].bitmap);
  1949. SDL_Surface * bufs = CSDL_Ext::secondAlphaTransform(map.defy[vv]->handler->ourImages[yy].bitmap, alphaTransSurf);
  1950. SDL_FreeSurface(map.defy[vv]->handler->ourImages[yy].bitmap);
  1951. map.defy[vv]->handler->ourImages[yy].bitmap = bufs;
  1952. map.defy[vv]->handler->alphaTransformed = true;
  1953. }
  1954. }
  1955. SDL_FreeSurface(alphaTransSurf);
  1956. //assigning defobjinfos
  1957. for(int ww=0; ww<CGI->objh->objInstances.size(); ++ww)
  1958. {
  1959. for(int h=0; h<CGI->dobjinfo->objs.size(); ++h)
  1960. {
  1961. if(CGI->dobjinfo->objs[h].defName==CGI->objh->objInstances[ww]->defInfo->name)
  1962. {
  1963. CGI->objh->objInstances[ww]->defObjInfoNumber = h;
  1964. break;
  1965. }
  1966. }
  1967. }
  1968. //assigned
  1969. //loading events
  1970. int numberOfEvents = readNormalNr(i); i+=4;
  1971. for(int yyoo=0; yyoo<numberOfEvents; ++yyoo)
  1972. {
  1973. CMapEvent ne;
  1974. ne.name = std::string();
  1975. ne.message = std::string();
  1976. int nameLen = readNormalNr(i); i+=4;
  1977. for(int qq=0; qq<nameLen; ++qq)
  1978. {
  1979. ne.name += bufor[i]; ++i;
  1980. }
  1981. int messLen = readNormalNr(i); i+=4;
  1982. for(int qq=0; qq<messLen; ++qq)
  1983. {
  1984. ne.message +=bufor[i]; ++i;
  1985. }
  1986. ne.wood = readNormalNr(i); i+=4;
  1987. ne.mercury = readNormalNr(i); i+=4;
  1988. ne.ore = readNormalNr(i); i+=4;
  1989. ne.sulfur = readNormalNr(i); i+=4;
  1990. ne.crystal = readNormalNr(i); i+=4;
  1991. ne.gems = readNormalNr(i); i+=4;
  1992. ne.gold = readNormalNr(i); i+=4;
  1993. ne.players = bufor[i]; ++i;
  1994. if(map.version>AB)
  1995. {
  1996. ne.humanAffected = bufor[i]; ++i;
  1997. }
  1998. else
  1999. ne.humanAffected = true;
  2000. ne.computerAffected = bufor[i]; ++i;
  2001. ne.firstOccurence = bufor[i]; ++i;
  2002. ne.nextOccurence = bufor[i]; ++i;
  2003. i+=18;
  2004. map.events.push_back(ne);
  2005. }
  2006. }
  2007. int CAmbarCendamo::readNormalNr (int pos, int bytCon, bool cyclic)
  2008. {
  2009. int ret=0;
  2010. int amp=1;
  2011. for (int i=0; i<bytCon; i++)
  2012. {
  2013. ret+=bufor[pos+i]*amp;
  2014. amp*=256;
  2015. }
  2016. if(cyclic && bytCon<4 && ret>=amp/2)
  2017. {
  2018. ret = ret-amp;
  2019. }
  2020. return ret;
  2021. }
  2022. void CAmbarCendamo::loadDefs()
  2023. {
  2024. std::set<int> loadedTypes;
  2025. for (int i=0; i<map.width; i++)
  2026. {
  2027. for (int j=0; j<map.width; j++)
  2028. {
  2029. if (loadedTypes.find(map.terrain[i][j].tertype)==loadedTypes.end())
  2030. {
  2031. CDefHandler *sdh = CGI->spriteh->giveDef(CSemiDefHandler::nameFromType(map.terrain[i][j].tertype).c_str());
  2032. loadedTypes.insert(map.terrain[i][j].tertype);
  2033. defs.push_back(sdh);
  2034. }
  2035. if (map.twoLevel && loadedTypes.find(map.undergroungTerrain[i][j].tertype)==loadedTypes.end())
  2036. {
  2037. CDefHandler *sdh = CGI->spriteh->giveDef(CSemiDefHandler::nameFromType(map.undergroungTerrain[i][j].tertype).c_str());
  2038. loadedTypes.insert(map.undergroungTerrain[i][j].tertype);
  2039. defs.push_back(sdh);
  2040. }
  2041. }
  2042. }
  2043. }
  2044. EDefType CAmbarCendamo::getDefType(CGDefInfo * a)
  2045. {
  2046. switch(a->id)
  2047. {
  2048. case 5: case 65: case 66: case 67: case 68: case 69:
  2049. return EDefType::ARTIFACT_DEF; //handled
  2050. case 6:
  2051. return EDefType::PANDORA_DEF; //hanled
  2052. case 26:
  2053. return EDefType::EVENTOBJ_DEF; //handled
  2054. case 33:
  2055. return EDefType::GARRISON_DEF; //handled
  2056. case 34: case 70: case 62: //70 - random hero //62 - prison
  2057. return EDefType::HERO_DEF; //handled
  2058. case 36:
  2059. return EDefType::GRAIL_DEF; //hanled
  2060. case 53: case 17: case 18: case 19: case 20: case 42: case 87: case 220://cases 17 - 20 and 42 - tests
  2061. return EDefType::PLAYERONLY_DEF; //handled
  2062. case 54: case 71: case 72: case 73: case 74: case 75: case 162: case 163: case 164:
  2063. return EDefType::CREATURES_DEF; //handled
  2064. case 59:
  2065. return EDefType::SIGN_DEF; //handled
  2066. case 77:
  2067. return EDefType::TOWN_DEF; //can be problematic, but handled
  2068. case 79: case 76:
  2069. return EDefType::RESOURCE_DEF; //handled
  2070. case 81:
  2071. return EDefType::SCHOLAR_DEF; //handled
  2072. case 83:
  2073. return EDefType::SEERHUT_DEF; //handled
  2074. case 91:
  2075. return EDefType::SIGN_DEF; //handled
  2076. case 88: case 89: case 90:
  2077. return SHRINE_DEF; //handled
  2078. case 93:
  2079. return SPELLSCROLL_DEF; //handled
  2080. case 98:
  2081. return EDefType::TOWN_DEF; //handled
  2082. case 113:
  2083. return EDefType::WITCHHUT_DEF; //handled
  2084. case 215:
  2085. return EDefType::BORDERGUARD_DEF; //handled by analogy to seer huts ;]
  2086. case 216:
  2087. return EDefType::CREGEN2_DEF; //handled
  2088. case 217:
  2089. return EDefType::CREGEN_DEF; //handled
  2090. case 218:
  2091. return EDefType::CREGEN3_DEF; //handled
  2092. case 219:
  2093. return EDefType::GARRISON_DEF; //handled
  2094. default:
  2095. return EDefType::TERRAINOBJ_DEF; // nothing to be handled
  2096. }
  2097. }
  2098. CCreatureSet CAmbarCendamo::readCreatureSet(int pos, int number)
  2099. {
  2100. if(map.version>RoE)
  2101. {
  2102. CCreatureSet ret;
  2103. std::pair<CCreature *, int> ins;
  2104. if(number>0 && readNormalNr(pos, 2)!=0xffff)
  2105. {
  2106. int rettt = readNormalNr(pos, 2);
  2107. if(rettt>32768)
  2108. rettt = 65536-rettt+CGameInfo::mainObj->creh->creatures.size()-16;
  2109. ins.first = &(CGameInfo::mainObj->creh->creatures[rettt]);
  2110. ins.second = readNormalNr(pos+2, 2);
  2111. std::pair<int,std::pair<CCreature *, int> > tt(0,ins);
  2112. ret.slots.insert(tt);
  2113. }
  2114. if(number>1 && readNormalNr(pos+4, 2)!=0xffff)
  2115. {
  2116. int rettt = readNormalNr(pos+4, 2);
  2117. if(rettt>32768)
  2118. rettt = 65536-rettt+CGameInfo::mainObj->creh->creatures.size()-16;
  2119. ins.first = &(CGameInfo::mainObj->creh->creatures[rettt]);
  2120. ins.second = readNormalNr(pos+6, 2);
  2121. std::pair<int,std::pair<CCreature *, int> > tt(1,ins);
  2122. ret.slots.insert(tt);
  2123. }
  2124. if(number>2 && readNormalNr(pos+8, 2)!=0xffff)
  2125. {
  2126. int rettt = readNormalNr(pos+8, 2);
  2127. if(rettt>32768)
  2128. rettt = 65536-rettt+CGameInfo::mainObj->creh->creatures.size()-16;
  2129. ins.first = &(CGameInfo::mainObj->creh->creatures[rettt]);
  2130. ins.second = readNormalNr(pos+10, 2);
  2131. std::pair<int,std::pair<CCreature *, int> > tt(2,ins);
  2132. ret.slots.insert(tt);
  2133. }
  2134. if(number>3 && readNormalNr(pos+12, 2)!=0xffff)
  2135. {
  2136. int rettt = readNormalNr(pos+12, 2);
  2137. if(rettt>32768)
  2138. rettt = 65536-rettt+CGameInfo::mainObj->creh->creatures.size()-16;
  2139. ins.first = &(CGameInfo::mainObj->creh->creatures[rettt]);
  2140. ins.second = readNormalNr(pos+14, 2);
  2141. std::pair<int,std::pair<CCreature *, int> > tt(3,ins);
  2142. ret.slots.insert(tt);
  2143. }
  2144. if(number>4 && readNormalNr(pos+16, 2)!=0xffff)
  2145. {
  2146. int rettt = readNormalNr(pos+16, 2);
  2147. if(rettt>32768)
  2148. rettt = 65536-rettt+CGameInfo::mainObj->creh->creatures.size()-16;
  2149. ins.first = &(CGameInfo::mainObj->creh->creatures[rettt]);
  2150. ins.second = readNormalNr(pos+18, 2);
  2151. std::pair<int,std::pair<CCreature *, int> > tt(4,ins);
  2152. ret.slots.insert(tt);
  2153. }
  2154. if(number>5 && readNormalNr(pos+20, 2)!=0xffff)
  2155. {
  2156. int rettt = readNormalNr(pos+20, 2);
  2157. if(rettt>32768)
  2158. rettt = 65536-rettt+CGameInfo::mainObj->creh->creatures.size()-16;
  2159. ins.first = &(CGameInfo::mainObj->creh->creatures[rettt]);
  2160. ins.second = readNormalNr(pos+22, 2);
  2161. std::pair<int,std::pair<CCreature *, int> > tt(5,ins);
  2162. ret.slots.insert(tt);
  2163. }
  2164. if(number>6 && readNormalNr(pos+24, 2)!=0xffff)
  2165. {
  2166. int rettt = readNormalNr(pos+24, 2);
  2167. if(rettt>32768)
  2168. rettt = 65536-rettt+CGameInfo::mainObj->creh->creatures.size()-16;
  2169. ins.first = &(CGameInfo::mainObj->creh->creatures[rettt]);
  2170. ins.second = readNormalNr(pos+26, 2);
  2171. std::pair<int,std::pair<CCreature *, int> > tt(6,ins);
  2172. ret.slots.insert(tt);
  2173. }
  2174. return ret;
  2175. }
  2176. else
  2177. {
  2178. CCreatureSet ret;
  2179. std::pair<CCreature *, int> ins;
  2180. if(number>0 && readNormalNr(pos, 1)!=0xff)
  2181. {
  2182. int rettt = readNormalNr(pos, 1);
  2183. if(rettt>220)
  2184. rettt = 256-rettt+CGameInfo::mainObj->creh->creatures.size()-16;
  2185. ins.first = &(CGameInfo::mainObj->creh->creatures[rettt]);
  2186. ins.second = readNormalNr(pos+1, 2);
  2187. std::pair<int,std::pair<CCreature *, int> > tt(0,ins);
  2188. ret.slots.insert(tt);
  2189. }
  2190. if(number>1 && readNormalNr(pos+3, 1)!=0xff)
  2191. {
  2192. int rettt = readNormalNr(pos+3, 1);
  2193. if(rettt>220)
  2194. rettt = 256-rettt+CGameInfo::mainObj->creh->creatures.size()-16;
  2195. ins.first = &(CGameInfo::mainObj->creh->creatures[rettt]);
  2196. ins.second = readNormalNr(pos+4, 2);
  2197. std::pair<int,std::pair<CCreature *, int> > tt(1,ins);
  2198. ret.slots.insert(tt);
  2199. }
  2200. if(number>2 && readNormalNr(pos+6, 1)!=0xff)
  2201. {
  2202. int rettt = readNormalNr(pos+6, 1);
  2203. if(rettt>220)
  2204. rettt = 256-rettt+CGameInfo::mainObj->creh->creatures.size()-16;
  2205. ins.first = &(CGameInfo::mainObj->creh->creatures[rettt]);
  2206. ins.second = readNormalNr(pos+7, 2);
  2207. std::pair<int,std::pair<CCreature *, int> > tt(2,ins);
  2208. ret.slots.insert(tt);
  2209. }
  2210. if(number>3 && readNormalNr(pos+9, 1)!=0xff)
  2211. {
  2212. int rettt = readNormalNr(pos+9, 1);
  2213. if(rettt>220)
  2214. rettt = 256-rettt+CGameInfo::mainObj->creh->creatures.size()-16;
  2215. ins.first = &(CGameInfo::mainObj->creh->creatures[rettt]);
  2216. ins.second = readNormalNr(pos+10, 2);
  2217. std::pair<int,std::pair<CCreature *, int> > tt(3,ins);
  2218. ret.slots.insert(tt);
  2219. }
  2220. if(number>4 && readNormalNr(pos+12, 1)!=0xff)
  2221. {
  2222. int rettt = readNormalNr(pos+12, 1);
  2223. if(rettt>220)
  2224. rettt = 256-rettt+CGameInfo::mainObj->creh->creatures.size()-16;
  2225. ins.first = &(CGameInfo::mainObj->creh->creatures[rettt]);
  2226. ins.second = readNormalNr(pos+13, 2);
  2227. std::pair<int,std::pair<CCreature *, int> > tt(4,ins);
  2228. ret.slots.insert(tt);
  2229. }
  2230. if(number>5 && readNormalNr(pos+15, 1)!=0xff)
  2231. {
  2232. int rettt = readNormalNr(pos+15, 1);
  2233. if(rettt>220)
  2234. rettt = 256-rettt+CGameInfo::mainObj->creh->creatures.size()-16;
  2235. ins.first = &(CGameInfo::mainObj->creh->creatures[rettt]);
  2236. ins.second = readNormalNr(pos+16, 2);
  2237. std::pair<int,std::pair<CCreature *, int> > tt(5,ins);
  2238. ret.slots.insert(tt);
  2239. }
  2240. if(number>6 && readNormalNr(pos+18, 1)!=0xff)
  2241. {
  2242. int rettt = readNormalNr(pos+18, 1);
  2243. if(rettt>220)
  2244. rettt = 256-rettt+CGameInfo::mainObj->creh->creatures.size()-16;
  2245. ins.first = &(CGameInfo::mainObj->creh->creatures[rettt]);
  2246. ins.second = readNormalNr(pos+19, 2);
  2247. std::pair<int,std::pair<CCreature *, int> > tt(6,ins);
  2248. ret.slots.insert(tt);
  2249. }
  2250. return ret;
  2251. }
  2252. }
  2253. void CAmbarCendamo::processMap(std::vector<std::string> & defsToUnpack)
  2254. {
  2255. resDefNames.push_back("AVTRNDM0.DEF");
  2256. resDefNames.push_back("AVTWOOD0.DEF");
  2257. resDefNames.push_back("AVTMERC0.DEF");
  2258. resDefNames.push_back("AVTORE0.DEF");
  2259. resDefNames.push_back("AVTSULF0.DEF");
  2260. resDefNames.push_back("AVTCRYS0.DEF");
  2261. resDefNames.push_back("AVTGEMS0.DEF");
  2262. resDefNames.push_back("AVTGOLD0.DEF");
  2263. resDefNames.push_back("ZMITHR.DEF");
  2264. std::vector<CGDefInfo*> resDefNumbers;
  2265. for(int hh=0; hh<resDefNames.size(); ++hh)
  2266. {
  2267. resDefNumbers.push_back(NULL);
  2268. for(int k=0; k<map.defy.size(); ++k)
  2269. {
  2270. std::string buf = map.defy[k]->name;
  2271. std::transform(buf.begin(), buf.end(), buf.begin(), (int(*)(int))toupper);
  2272. if(resDefNames[hh] == buf)
  2273. {
  2274. resDefNumbers[resDefNumbers.size()-1] = map.defy[k];
  2275. break;
  2276. }
  2277. }
  2278. }
  2279. std::vector<std::string> creDefNames;
  2280. for(int dd=0; dd<140; ++dd) //we do not use here WoG units
  2281. {
  2282. creDefNames.push_back(CGI->dobjinfo->objs[dd+1184].defName);
  2283. }
  2284. std::vector<CGDefInfo*> creDefNumbers;
  2285. for(int ee=0; ee<creDefNames.size(); ++ee)
  2286. {
  2287. creDefNumbers.push_back(NULL);
  2288. }
  2289. std::vector<std::string> artDefNames;
  2290. std::vector<CGDefInfo* > artDefNumbers;
  2291. for(int bb=0; bb<162; ++bb)
  2292. {
  2293. if(CGI->arth->artifacts[CGI->dobjinfo->objs[bb+213].subtype].aClass!=EartClass::SartClass)
  2294. artDefNames.push_back(CGI->dobjinfo->objs[bb+213].defName);
  2295. }
  2296. for(int ee=0; ee<artDefNames.size(); ++ee)
  2297. {
  2298. artDefNumbers.push_back(NULL);
  2299. }
  2300. std::vector<std::string> art1DefNames;
  2301. std::vector<CGDefInfo*> art1DefNumbers;
  2302. for(int bb=0; bb<162; ++bb)
  2303. {
  2304. if(CGI->arth->artifacts[CGI->dobjinfo->objs[bb+213].subtype].aClass==EartClass::TartClass)
  2305. art1DefNames.push_back(CGI->dobjinfo->objs[bb+213].defName);
  2306. }
  2307. for(int ee=0; ee<art1DefNames.size(); ++ee)
  2308. {
  2309. art1DefNumbers.push_back(NULL);
  2310. }
  2311. std::vector<std::string> art2DefNames;
  2312. std::vector<CGDefInfo*> art2DefNumbers;
  2313. for(int bb=0; bb<162; ++bb)
  2314. {
  2315. if(CGI->arth->artifacts[CGI->dobjinfo->objs[bb+213].subtype].aClass==EartClass::NartClass)
  2316. art2DefNames.push_back(CGI->dobjinfo->objs[bb+213].defName);
  2317. }
  2318. for(int ee=0; ee<art2DefNames.size(); ++ee)
  2319. {
  2320. art2DefNumbers.push_back(NULL);
  2321. }
  2322. std::vector<std::string> art3DefNames;
  2323. std::vector<CGDefInfo*> art3DefNumbers;
  2324. for(int bb=0; bb<162; ++bb)
  2325. {
  2326. if(CGI->arth->artifacts[CGI->dobjinfo->objs[bb+213].subtype].aClass==EartClass::JartClass)
  2327. art3DefNames.push_back(CGI->dobjinfo->objs[bb+213].defName);
  2328. }
  2329. for(int ee=0; ee<art3DefNames.size(); ++ee)
  2330. {
  2331. art3DefNumbers.push_back(NULL);
  2332. }
  2333. std::vector<std::string> art4DefNames;
  2334. std::vector<CGDefInfo*> art4DefNumbers;
  2335. for(int bb=0; bb<162; ++bb)
  2336. {
  2337. if(CGI->arth->artifacts[CGI->dobjinfo->objs[bb+213].subtype].aClass==EartClass::RartClass)
  2338. art4DefNames.push_back(CGI->dobjinfo->objs[bb+213].defName);
  2339. }
  2340. for(int ee=0; ee<art4DefNames.size(); ++ee)
  2341. {
  2342. art4DefNumbers.push_back(NULL);
  2343. }
  2344. std::vector<std::string> town0DefNames; //without fort
  2345. std::vector<CGDefInfo*> town0DefNumbers;
  2346. std::vector<std::string> town1DefNames; //with fort
  2347. std::vector<CGDefInfo*> town1DefNumbers;
  2348. for(int dd=0; dd<F_NUMBER; ++dd)
  2349. {
  2350. town1DefNames.push_back(CGI->dobjinfo->objs[dd+385].defName);
  2351. town1DefNumbers.push_back(NULL);
  2352. }
  2353. std::vector< std::vector<std::string> > creGenNames;
  2354. std::vector< std::vector<CGDefInfo*> > creGenNumbers;
  2355. creGenNames.resize(F_NUMBER);
  2356. creGenNumbers.resize(F_NUMBER);
  2357. for(int ff=0; ff<F_NUMBER-1; ++ff)
  2358. {
  2359. for(int dd=0; dd<7; ++dd)
  2360. {
  2361. creGenNames[ff].push_back(CGI->dobjinfo->objs[395+7*ff+dd].defName);
  2362. creGenNumbers[ff].push_back(NULL);
  2363. }
  2364. }
  2365. for(int dd=0; dd<7; ++dd)
  2366. {
  2367. creGenNumbers[8].push_back(NULL);
  2368. }
  2369. creGenNames[F_NUMBER-1].push_back(CGI->dobjinfo->objs[457].defName);
  2370. creGenNames[F_NUMBER-1].push_back(CGI->dobjinfo->objs[452].defName);
  2371. creGenNames[F_NUMBER-1].push_back(CGI->dobjinfo->objs[455].defName);
  2372. creGenNames[F_NUMBER-1].push_back(CGI->dobjinfo->objs[454].defName);
  2373. creGenNames[F_NUMBER-1].push_back(CGI->dobjinfo->objs[453].defName);
  2374. creGenNames[F_NUMBER-1].push_back(CGI->dobjinfo->objs[458].defName);
  2375. creGenNames[F_NUMBER-1].push_back(CGI->dobjinfo->objs[459].defName);
  2376. for(int b=0; b<CGI->scenarioOps.playerInfos.size(); ++b) // choosing random player alignment
  2377. {
  2378. if(CGI->scenarioOps.playerInfos[b].castle==-1)
  2379. CGI->scenarioOps.playerInfos[b].castle = rand()%F_NUMBER;
  2380. }
  2381. //variables initialized
  2382. for(int j=0; j<CGI->objh->objInstances.size(); ++j)
  2383. {
  2384. CGDefInfo * curDef = CGI->objh->objInstances[j]->defInfo;
  2385. switch(getDefType(curDef))
  2386. {
  2387. case EDefType::RESOURCE_DEF:
  2388. {
  2389. if(curDef->id==76) //resource to specify
  2390. {
  2391. CGDefInfo * nxt = curDef;
  2392. nxt->id = 79;
  2393. nxt->subid = rand()%7;
  2394. if(resDefNumbers[nxt->subid+1]!=NULL)
  2395. {
  2396. CGI->objh->objInstances[j]->defInfo = resDefNumbers[nxt->subid+1];
  2397. continue;
  2398. }
  2399. nxt->name = resDefNames[nxt->subid+1];
  2400. std::vector<DefObjInfo>::iterator pit = std::find(CGameInfo::mainObj->dobjinfo->objs.begin(), CGameInfo::mainObj->dobjinfo->objs.end(),
  2401. nxt->name);
  2402. if(pit == CGameInfo::mainObj->dobjinfo->objs.end())
  2403. {
  2404. nxt->isOnDefList = false;
  2405. }
  2406. else
  2407. {
  2408. nxt->printPriority = pit->priority;
  2409. nxt->isOnDefList = true;
  2410. }
  2411. map.defy.push_back(nxt); // add this def to the vector
  2412. defsToUnpack.push_back(nxt->name);
  2413. CGI->objh->objInstances[j]->defInfo = nxt;
  2414. if(resDefNumbers[nxt->subid+1]==NULL)
  2415. {
  2416. resDefNumbers[nxt->subid+1] = nxt;
  2417. }
  2418. }
  2419. break;
  2420. }
  2421. case EDefType::CREATURES_DEF:
  2422. {
  2423. if(curDef->id==72) //random monster lvl 1
  2424. {
  2425. CGDefInfo * nxt = curDef;
  2426. nxt->id = 54;
  2427. nxt->subid = 14*(rand()%9)+rand()%2;
  2428. if(creDefNumbers[nxt->subid]!=NULL)
  2429. {
  2430. CGI->objh->objInstances[j]->defInfo = creDefNumbers[nxt->subid];
  2431. continue;
  2432. }
  2433. nxt->name = creDefNames[nxt->subid];
  2434. std::vector<DefObjInfo>::iterator pit = std::find(CGameInfo::mainObj->dobjinfo->objs.begin(), CGameInfo::mainObj->dobjinfo->objs.end(),
  2435. nxt->name);
  2436. if(pit == CGameInfo::mainObj->dobjinfo->objs.end())
  2437. {
  2438. nxt->isOnDefList = false;
  2439. }
  2440. else
  2441. {
  2442. nxt->printPriority = pit->priority;
  2443. nxt->isOnDefList = true;
  2444. }
  2445. map.defy.push_back(nxt); // add this def to the vector
  2446. defsToUnpack.push_back(nxt->name);
  2447. CGI->objh->objInstances[j]->defInfo = nxt;
  2448. if(creDefNumbers[nxt->subid]==NULL)
  2449. {
  2450. creDefNumbers[nxt->subid] = nxt;
  2451. }
  2452. }
  2453. if(curDef->id==73) //random monster lvl 2
  2454. {
  2455. CGDefInfo * nxt = curDef;
  2456. nxt->id = 54;
  2457. nxt->subid = 14*(rand()%9)+rand()%2+2;
  2458. if(creDefNumbers[nxt->subid]!=NULL)
  2459. {
  2460. CGI->objh->objInstances[j]->defInfo = creDefNumbers[nxt->subid];
  2461. continue;
  2462. }
  2463. nxt->name = creDefNames[nxt->subid];
  2464. std::vector<DefObjInfo>::iterator pit = std::find(CGameInfo::mainObj->dobjinfo->objs.begin(), CGameInfo::mainObj->dobjinfo->objs.end(),
  2465. nxt->name);
  2466. if(pit == CGameInfo::mainObj->dobjinfo->objs.end())
  2467. {
  2468. nxt->isOnDefList = false;
  2469. }
  2470. else
  2471. {
  2472. nxt->printPriority = pit->priority;
  2473. nxt->isOnDefList = true;
  2474. }
  2475. map.defy.push_back(nxt); // add this def to the vector
  2476. defsToUnpack.push_back(nxt->name);
  2477. CGI->objh->objInstances[j]->defInfo = nxt;
  2478. if(creDefNumbers[nxt->subid]==NULL)
  2479. {
  2480. creDefNumbers[nxt->subid] = nxt;
  2481. }
  2482. }
  2483. if(curDef->id==74) //random monster lvl 3
  2484. {
  2485. CGDefInfo *nxt = curDef;
  2486. nxt->id = 54;
  2487. nxt->subid = 14*(rand()%9)+rand()%2+4;
  2488. if(creDefNumbers[nxt->subid]!=NULL)
  2489. {
  2490. CGI->objh->objInstances[j]->defInfo = creDefNumbers[nxt->subid];
  2491. continue;
  2492. }
  2493. nxt->name = creDefNames[nxt->subid];
  2494. std::vector<DefObjInfo>::iterator pit = std::find(CGameInfo::mainObj->dobjinfo->objs.begin(), CGameInfo::mainObj->dobjinfo->objs.end(),
  2495. nxt->name);
  2496. if(pit == CGameInfo::mainObj->dobjinfo->objs.end())
  2497. {
  2498. nxt->isOnDefList = false;
  2499. }
  2500. else
  2501. {
  2502. nxt->printPriority = pit->priority;
  2503. nxt->isOnDefList = true;
  2504. }
  2505. map.defy.push_back(nxt); // add this def to the vector
  2506. defsToUnpack.push_back(nxt->name);
  2507. CGI->objh->objInstances[j]->defInfo = nxt;
  2508. if(creDefNumbers[nxt->subid]==NULL)
  2509. {
  2510. creDefNumbers[nxt->subid] = nxt;
  2511. }
  2512. }
  2513. if(curDef->id==75) //random monster lvl 4
  2514. {
  2515. CGDefInfo * nxt = curDef;
  2516. nxt->id = 54;
  2517. nxt->subid = 14*(rand()%9)+rand()%2+6;
  2518. if(creDefNumbers[nxt->subid]!=NULL)
  2519. {
  2520. CGI->objh->objInstances[j]->defInfo = creDefNumbers[nxt->subid];
  2521. continue;
  2522. }
  2523. nxt->name = creDefNames[nxt->subid];
  2524. std::vector<DefObjInfo>::iterator pit = std::find(CGameInfo::mainObj->dobjinfo->objs.begin(), CGameInfo::mainObj->dobjinfo->objs.end(),
  2525. nxt->name);
  2526. if(pit == CGameInfo::mainObj->dobjinfo->objs.end())
  2527. {
  2528. nxt->isOnDefList = false;
  2529. }
  2530. else
  2531. {
  2532. nxt->printPriority = pit->priority;
  2533. nxt->isOnDefList = true;
  2534. }
  2535. map.defy.push_back(nxt); // add this def to the vector
  2536. defsToUnpack.push_back(nxt->name);
  2537. CGI->objh->objInstances[j]->defInfo = nxt;
  2538. if(creDefNumbers[nxt->subid]==NULL)
  2539. {
  2540. creDefNumbers[nxt->subid] = nxt;
  2541. }
  2542. }
  2543. if(curDef->id==162) //random monster lvl 5
  2544. {
  2545. CGDefInfo * nxt = curDef;
  2546. nxt->id = 54;
  2547. nxt->subid = 14*(rand()%9)+rand()%2+8;
  2548. if(creDefNumbers[nxt->subid]!=NULL)
  2549. {
  2550. CGI->objh->objInstances[j]->defInfo = creDefNumbers[nxt->subid];
  2551. continue;
  2552. }
  2553. nxt->name = creDefNames[nxt->subid];
  2554. std::vector<DefObjInfo>::iterator pit = std::find(CGameInfo::mainObj->dobjinfo->objs.begin(), CGameInfo::mainObj->dobjinfo->objs.end(),
  2555. nxt->name);
  2556. if(pit == CGameInfo::mainObj->dobjinfo->objs.end())
  2557. {
  2558. nxt->isOnDefList = false;
  2559. }
  2560. else
  2561. {
  2562. nxt->printPriority = pit->priority;
  2563. nxt->isOnDefList = true;
  2564. }
  2565. map.defy.push_back(nxt); // add this def to the vector
  2566. defsToUnpack.push_back(nxt->name);
  2567. CGI->objh->objInstances[j]->defInfo = nxt;
  2568. if(creDefNumbers[nxt->subid]==NULL)
  2569. {
  2570. creDefNumbers[nxt->subid] = nxt;
  2571. }
  2572. }
  2573. if(curDef->id==163) //random monster lvl 6
  2574. {
  2575. CGDefInfo* nxt = curDef;
  2576. nxt->id = 54;
  2577. nxt->subid = 14*(rand()%9)+rand()%2+10;
  2578. if(creDefNumbers[nxt->subid]!=NULL)
  2579. {
  2580. CGI->objh->objInstances[j]->defInfo = creDefNumbers[nxt->subid];
  2581. continue;
  2582. }
  2583. nxt->name = creDefNames[nxt->subid];
  2584. std::vector<DefObjInfo>::iterator pit = std::find(CGameInfo::mainObj->dobjinfo->objs.begin(), CGameInfo::mainObj->dobjinfo->objs.end(),
  2585. nxt->name);
  2586. if(pit == CGameInfo::mainObj->dobjinfo->objs.end())
  2587. {
  2588. nxt->isOnDefList = false;
  2589. }
  2590. else
  2591. {
  2592. nxt->printPriority = pit->priority;
  2593. nxt->isOnDefList = true;
  2594. }
  2595. map.defy.push_back(nxt); // add this def to the vector
  2596. defsToUnpack.push_back(nxt->name);
  2597. CGI->objh->objInstances[j]->defInfo = nxt;
  2598. if(creDefNumbers[nxt->subid]==NULL)
  2599. {
  2600. creDefNumbers[nxt->subid] = nxt;
  2601. }
  2602. }
  2603. if(curDef->id==164) //random monster lvl 7
  2604. {
  2605. CGDefInfo * nxt = curDef;
  2606. nxt->id = 54;
  2607. nxt->subid = 14*(rand()%9)+rand()%2+12;
  2608. if(creDefNumbers[nxt->subid]!=NULL)
  2609. {
  2610. CGI->objh->objInstances[j]->defInfo = creDefNumbers[nxt->subid];
  2611. continue;
  2612. }
  2613. nxt->name = creDefNames[nxt->subid];
  2614. std::vector<DefObjInfo>::iterator pit = std::find(CGameInfo::mainObj->dobjinfo->objs.begin(), CGameInfo::mainObj->dobjinfo->objs.end(),
  2615. nxt->name);
  2616. if(pit == CGameInfo::mainObj->dobjinfo->objs.end())
  2617. {
  2618. nxt->isOnDefList = false;
  2619. }
  2620. else
  2621. {
  2622. nxt->printPriority = pit->priority;
  2623. nxt->isOnDefList = true;
  2624. }
  2625. map.defy.push_back(nxt); // add this def to the vector
  2626. defsToUnpack.push_back(nxt->name);
  2627. CGI->objh->objInstances[j]->defInfo = nxt;
  2628. if(creDefNumbers[nxt->subid]==NULL)
  2629. {
  2630. creDefNumbers[nxt->subid] = nxt;
  2631. }
  2632. }
  2633. if(curDef->id==71) //random monster (any level)
  2634. {
  2635. CGDefInfo * nxt = curDef;
  2636. nxt->id = 54;
  2637. nxt->subid = rand()%126;
  2638. if(creDefNumbers[nxt->subid]!=NULL)
  2639. {
  2640. CGI->objh->objInstances[j]->defInfo = creDefNumbers[nxt->subid];
  2641. continue;
  2642. }
  2643. nxt->name = creDefNames[nxt->subid];
  2644. std::vector<DefObjInfo>::iterator pit = std::find(CGameInfo::mainObj->dobjinfo->objs.begin(), CGameInfo::mainObj->dobjinfo->objs.end(),
  2645. nxt->name);
  2646. if(pit == CGameInfo::mainObj->dobjinfo->objs.end())
  2647. {
  2648. nxt->isOnDefList = false;
  2649. }
  2650. else
  2651. {
  2652. nxt->printPriority = pit->priority;
  2653. nxt->isOnDefList = true;
  2654. }
  2655. map.defy.push_back(nxt); // add this def to the vector
  2656. defsToUnpack.push_back(nxt->name);
  2657. CGI->objh->objInstances[j]->defInfo = nxt;
  2658. if(creDefNumbers[nxt->subid]==NULL)
  2659. {
  2660. creDefNumbers[nxt->subid] = nxt;
  2661. }
  2662. }
  2663. break;
  2664. } //end of case
  2665. case EDefType::ARTIFACT_DEF:
  2666. {
  2667. if(curDef->id==65) //random atrifact (any class)
  2668. {
  2669. CGDefInfo *nxt = curDef;
  2670. nxt->id = 5;
  2671. nxt->subid = rand()%artDefNames.size();
  2672. if(artDefNumbers[nxt->subid]!=NULL)
  2673. {
  2674. CGI->objh->objInstances[j]->defInfo = artDefNumbers[nxt->subid];
  2675. continue;
  2676. }
  2677. nxt->name = artDefNames[nxt->subid];
  2678. std::vector<DefObjInfo>::iterator pit = std::find(CGameInfo::mainObj->dobjinfo->objs.begin(), CGameInfo::mainObj->dobjinfo->objs.end(),
  2679. nxt->name);
  2680. if(pit == CGameInfo::mainObj->dobjinfo->objs.end())
  2681. {
  2682. nxt->isOnDefList = false;
  2683. }
  2684. else
  2685. {
  2686. nxt->printPriority = pit->priority;
  2687. nxt->isOnDefList = true;
  2688. }
  2689. map.defy.push_back(nxt); // add this def to the vector
  2690. defsToUnpack.push_back(nxt->name);
  2691. CGI->objh->objInstances[j]->defInfo = nxt;
  2692. if(artDefNumbers[nxt->subid]==NULL)
  2693. {
  2694. artDefNumbers[nxt->subid] = nxt;
  2695. }
  2696. }
  2697. if(curDef->id==66) //random atrifact (treasure)
  2698. {
  2699. CGDefInfo* nxt = curDef;
  2700. nxt->id = 5;
  2701. nxt->subid = rand()%art1DefNames.size();
  2702. if(art1DefNumbers[nxt->subid]!=NULL)
  2703. {
  2704. CGI->objh->objInstances[j]->defInfo = art1DefNumbers[nxt->subid];
  2705. continue;
  2706. }
  2707. nxt->name = art1DefNames[nxt->subid];
  2708. std::vector<DefObjInfo>::iterator pit = std::find(CGameInfo::mainObj->dobjinfo->objs.begin(), CGameInfo::mainObj->dobjinfo->objs.end(),
  2709. nxt->name);
  2710. if(pit == CGameInfo::mainObj->dobjinfo->objs.end())
  2711. {
  2712. nxt->isOnDefList = false;
  2713. }
  2714. else
  2715. {
  2716. nxt->printPriority = pit->priority;
  2717. nxt->isOnDefList = true;
  2718. }
  2719. map.defy.push_back(nxt); // add this def to the vector
  2720. defsToUnpack.push_back(nxt->name);
  2721. CGI->objh->objInstances[j]->defInfo = nxt;
  2722. if(art1DefNumbers[nxt->subid]==NULL)
  2723. {
  2724. art1DefNumbers[nxt->subid] = nxt;
  2725. }
  2726. }
  2727. if(curDef->id==67) //random atrifact (minor)
  2728. {
  2729. CGDefInfo *nxt = curDef;
  2730. nxt->id = 5;
  2731. nxt->subid = rand()%art2DefNames.size();
  2732. if(art2DefNumbers[nxt->subid]!=NULL)
  2733. {
  2734. CGI->objh->objInstances[j]->defInfo = art2DefNumbers[nxt->subid];
  2735. continue;
  2736. }
  2737. nxt->name = art2DefNames[nxt->subid];
  2738. std::vector<DefObjInfo>::iterator pit = std::find(CGameInfo::mainObj->dobjinfo->objs.begin(), CGameInfo::mainObj->dobjinfo->objs.end(),
  2739. nxt->name);
  2740. if(pit == CGameInfo::mainObj->dobjinfo->objs.end())
  2741. {
  2742. nxt->isOnDefList = false;
  2743. }
  2744. else
  2745. {
  2746. nxt->printPriority = pit->priority;
  2747. nxt->isOnDefList = true;
  2748. }
  2749. map.defy.push_back(nxt); // add this def to the vector
  2750. defsToUnpack.push_back(nxt->name);
  2751. CGI->objh->objInstances[j]->defInfo = nxt;
  2752. if(art2DefNumbers[nxt->subid]==NULL)
  2753. {
  2754. art2DefNumbers[nxt->subid] = nxt;
  2755. }
  2756. }
  2757. if(curDef->id==68) //random atrifact (major)
  2758. {
  2759. CGDefInfo* nxt = curDef;
  2760. nxt->id = 5;
  2761. nxt->subid = rand()%art3DefNames.size();
  2762. if(art3DefNumbers[nxt->subid]!=NULL)
  2763. {
  2764. CGI->objh->objInstances[j]->defInfo = art3DefNumbers[nxt->subid];
  2765. continue;
  2766. }
  2767. nxt->name = art3DefNames[nxt->subid];
  2768. std::vector<DefObjInfo>::iterator pit = std::find(CGameInfo::mainObj->dobjinfo->objs.begin(), CGameInfo::mainObj->dobjinfo->objs.end(),
  2769. nxt->name);
  2770. if(pit == CGameInfo::mainObj->dobjinfo->objs.end())
  2771. {
  2772. nxt->isOnDefList = false;
  2773. }
  2774. else
  2775. {
  2776. nxt->printPriority = pit->priority;
  2777. nxt->isOnDefList = true;
  2778. }
  2779. map.defy.push_back(nxt); // add this def to the vector
  2780. defsToUnpack.push_back(nxt->name);
  2781. CGI->objh->objInstances[j]->defInfo = nxt;
  2782. if(art3DefNumbers[nxt->subid]==NULL)
  2783. {
  2784. art3DefNumbers[nxt->subid] =nxt;
  2785. }
  2786. }
  2787. if(curDef->id==69) //random atrifact (relic)
  2788. {
  2789. CGDefInfo *nxt = curDef;
  2790. nxt->id = 5;
  2791. nxt->subid = rand()%art4DefNames.size();
  2792. if(art4DefNumbers[nxt->subid]!=NULL)
  2793. {
  2794. CGI->objh->objInstances[j]->defInfo = art4DefNumbers[nxt->subid];
  2795. continue;
  2796. }
  2797. nxt->name = art4DefNames[nxt->subid];
  2798. std::vector<DefObjInfo>::iterator pit = std::find(CGameInfo::mainObj->dobjinfo->objs.begin(), CGameInfo::mainObj->dobjinfo->objs.end(),
  2799. nxt->name);
  2800. if(pit == CGameInfo::mainObj->dobjinfo->objs.end())
  2801. {
  2802. nxt->isOnDefList = false;
  2803. }
  2804. else
  2805. {
  2806. nxt->printPriority = pit->priority;
  2807. nxt->isOnDefList = true;
  2808. }
  2809. map.defy.push_back(nxt); // add this def to the vector
  2810. defsToUnpack.push_back(nxt->name);
  2811. CGI->objh->objInstances[j]->defInfo = nxt;
  2812. if(art4DefNumbers[nxt->subid]==NULL)
  2813. {
  2814. art4DefNumbers[nxt->subid] = nxt;
  2815. }
  2816. }
  2817. break;
  2818. }
  2819. case EDefType::TOWN_DEF:
  2820. {
  2821. if(curDef->id==77) //random town
  2822. {
  2823. CGDefInfo* nxt = curDef;
  2824. nxt->id = 98;
  2825. if(((CCastleObjInfo*)CGI->objh->objInstances[j]->info)->player==0xff)
  2826. {
  2827. nxt->subid = rand()%town1DefNames.size();
  2828. }
  2829. else
  2830. {
  2831. if(CGI->scenarioOps.getIthPlayersSettings(((CCastleObjInfo*)CGI->objh->objInstances[j]->info)->player).castle>-1)
  2832. {
  2833. nxt->subid = CGI->scenarioOps.getIthPlayersSettings(((CCastleObjInfo*)CGI->objh->objInstances[j]->info)->player).castle;
  2834. }
  2835. else
  2836. {
  2837. nxt->subid = rand()%town1DefNames.size();
  2838. }
  2839. }
  2840. if(town1DefNumbers[nxt->subid]!=NULL)
  2841. {
  2842. CGI->objh->objInstances[j]->defInfo = town1DefNumbers[nxt->subid];
  2843. continue;
  2844. }
  2845. nxt->name = town1DefNames[nxt->subid];
  2846. std::vector<DefObjInfo>::iterator pit = std::find(CGameInfo::mainObj->dobjinfo->objs.begin(), CGameInfo::mainObj->dobjinfo->objs.end(),
  2847. nxt->name);
  2848. if(pit == CGameInfo::mainObj->dobjinfo->objs.end())
  2849. {
  2850. nxt->isOnDefList = false;
  2851. }
  2852. else
  2853. {
  2854. nxt->printPriority = pit->priority;
  2855. nxt->isOnDefList = true;
  2856. }
  2857. map.defy.push_back(nxt); // add this def to the vector
  2858. defsToUnpack.push_back(nxt->name);
  2859. CGI->objh->objInstances[j]->defInfo = nxt;
  2860. if(town1DefNumbers[nxt->subid]==NULL)
  2861. {
  2862. town1DefNumbers[nxt->subid] = nxt;
  2863. }
  2864. for (int ij=0;ij<CGI->townh->townInstances.size();ij++) // wyharatac gdy bedzie dziedziczenie
  2865. {
  2866. if (CGI->townh->townInstances[ij]->pos==CGI->objh->objInstances[j]->pos)
  2867. {
  2868. CGI->townh->townInstances[ij]->town = &CGI->townh->towns[nxt->subid];
  2869. break;
  2870. }
  2871. }
  2872. }
  2873. //((CCastleObjInfo*)CGI->objh->objInstances[j].info)
  2874. break;
  2875. }
  2876. case EDefType::HERO_DEF:
  2877. {
  2878. std::vector<DefObjInfo>::iterator pit = std::find(CGameInfo::mainObj->dobjinfo->objs.begin(), CGameInfo::mainObj->dobjinfo->objs.end(),
  2879. CGI->objh->objInstances[j]->defInfo->name);
  2880. CGI->objh->objInstances[j]->defInfo->printPriority = 0;
  2881. if(pit == CGameInfo::mainObj->dobjinfo->objs.end())
  2882. {
  2883. CGI->objh->objInstances[j]->defInfo->isOnDefList = false;
  2884. }
  2885. else
  2886. {
  2887. CGI->objh->objInstances[j]->defInfo->isOnDefList = true;
  2888. }
  2889. break;
  2890. }
  2891. } //end of main switch
  2892. } //end of main loop
  2893. for(int j=0; j<CGI->objh->objInstances.size(); ++j) //for creature dwellings on map (they are town type dependent)
  2894. {
  2895. CGDefInfo * curDef = CGI->objh->objInstances[j]->defInfo;
  2896. switch(getDefType(curDef))
  2897. {
  2898. case EDefType::CREGEN_DEF:
  2899. {
  2900. if(((CCreGenObjInfo*)CGI->objh->objInstances[j]->info)->asCastle)
  2901. {
  2902. CGDefInfo *nxt = curDef;
  2903. nxt->id = 17;
  2904. for(int vv=0; vv<CGI->objh->objInstances.size(); ++vv)
  2905. {
  2906. if(getDefType(CGI->objh->objInstances[vv]->defInfo)==EDefType::TOWN_DEF)
  2907. {
  2908. if(
  2909. ((CCastleObjInfo*)CGI->objh->objInstances[vv]->info)->bytes[0]==((CCreGenObjInfo*)CGI->objh->objInstances[j]->info)->bytes[0]
  2910. && ((CCastleObjInfo*)CGI->objh->objInstances[vv]->info)->bytes[1]==((CCreGenObjInfo*)CGI->objh->objInstances[j]->info)->bytes[1]
  2911. && ((CCastleObjInfo*)CGI->objh->objInstances[vv]->info)->bytes[2]==((CCreGenObjInfo*)CGI->objh->objInstances[j]->info)->bytes[2]
  2912. && ((CCastleObjInfo*)CGI->objh->objInstances[vv]->info)->bytes[3]==((CCreGenObjInfo*)CGI->objh->objInstances[j]->info)->bytes[3])
  2913. {
  2914. for(int mm=0; mm<town1DefNames.size(); ++mm)
  2915. {
  2916. std::transform(town1DefNames[mm].begin(), town1DefNames[mm].end(), town1DefNames[mm].begin(), (int(*)(int))toupper);
  2917. std::string hlp = CGI->objh->objInstances[vv]->defInfo->name;
  2918. std::transform(hlp.begin(), hlp.end(), hlp.begin(), (int(*)(int))toupper);
  2919. if(town1DefNames[mm]==hlp)
  2920. {
  2921. nxt->subid = mm;
  2922. }
  2923. }
  2924. }
  2925. }
  2926. }
  2927. int lvl = atoi(CGI->objh->objInstances[j]->defInfo->name.substr(7, 8).c_str())-1;
  2928. nxt->name = creGenNames[nxt->subid][lvl];
  2929. if(creGenNumbers[nxt->subid][lvl]!=NULL)
  2930. {
  2931. CGI->objh->objInstances[j]->defInfo = creGenNumbers[nxt->subid][lvl];
  2932. continue;
  2933. }
  2934. std::vector<DefObjInfo>::iterator pit = std::find(CGameInfo::mainObj->dobjinfo->objs.begin(), CGameInfo::mainObj->dobjinfo->objs.end(),
  2935. nxt->name);
  2936. if(pit == CGameInfo::mainObj->dobjinfo->objs.end())
  2937. {
  2938. nxt->isOnDefList = false;
  2939. }
  2940. else
  2941. {
  2942. nxt->printPriority = pit->priority;
  2943. nxt->isOnDefList = true;
  2944. }
  2945. map.defy.push_back(nxt); // add this def to the vector
  2946. defsToUnpack.push_back(nxt->name);
  2947. CGI->objh->objInstances[j]->defInfo = nxt;
  2948. if(creGenNumbers[nxt->subid][lvl]==NULL)
  2949. {
  2950. creGenNumbers[nxt->subid][lvl] = nxt;
  2951. }
  2952. }
  2953. else //if not as castle
  2954. {
  2955. CGDefInfo * nxt = curDef;
  2956. nxt->id = 17;
  2957. std::vector<int> possibleTowns;
  2958. for(int bb=0; bb<8; ++bb)
  2959. {
  2960. if(((CCreGenObjInfo*)CGI->objh->objInstances[j]->info)->castles[0] & (1<<bb))
  2961. {
  2962. possibleTowns.push_back(bb);
  2963. }
  2964. }
  2965. if(((CCreGenObjInfo*)CGI->objh->objInstances[j]->info)->castles[1])
  2966. possibleTowns.push_back(8);
  2967. nxt->subid = possibleTowns[rand()%possibleTowns.size()];
  2968. int lvl = atoi(CGI->objh->objInstances[j]->defInfo->name.substr(7, 8).c_str())-1;
  2969. nxt->name = creGenNames[nxt->subid][lvl];
  2970. if(creGenNumbers[nxt->subid][lvl]!=NULL)
  2971. {
  2972. CGI->objh->objInstances[j]->defInfo = creGenNumbers[nxt->subid][lvl];
  2973. continue;
  2974. }
  2975. std::vector<DefObjInfo>::iterator pit = std::find(CGameInfo::mainObj->dobjinfo->objs.begin(), CGameInfo::mainObj->dobjinfo->objs.end(),
  2976. nxt->name);
  2977. if(pit == CGameInfo::mainObj->dobjinfo->objs.end())
  2978. {
  2979. nxt->isOnDefList = false;
  2980. }
  2981. else
  2982. {
  2983. nxt->printPriority = pit->priority;
  2984. nxt->isOnDefList = true;
  2985. }
  2986. map.defy.push_back(nxt); // add this def to the vector
  2987. defsToUnpack.push_back(nxt->name);
  2988. CGI->objh->objInstances[j]->defInfo = nxt;
  2989. if(creGenNumbers[nxt->subid][lvl]==NULL)
  2990. {
  2991. creGenNumbers[nxt->subid][lvl] = nxt;
  2992. }
  2993. }
  2994. break;
  2995. }
  2996. case EDefType::CREGEN2_DEF:
  2997. {
  2998. if(((CCreGenObjInfo*)CGI->objh->objInstances[j]->info)->asCastle)
  2999. {
  3000. CGDefInfo * nxt = curDef;
  3001. nxt->id = 17;
  3002. for(int vv=0; vv<CGI->objh->objInstances.size(); ++vv)
  3003. {
  3004. if(getDefType(CGI->objh->objInstances[vv]->defInfo)==EDefType::TOWN_DEF)
  3005. {
  3006. if(
  3007. ((CCastleObjInfo*)CGI->objh->objInstances[vv]->info)->bytes[0]==((CCreGen2ObjInfo*)CGI->objh->objInstances[j]->info)->bytes[0]
  3008. && ((CCastleObjInfo*)CGI->objh->objInstances[vv]->info)->bytes[1]==((CCreGen2ObjInfo*)CGI->objh->objInstances[j]->info)->bytes[1]
  3009. && ((CCastleObjInfo*)CGI->objh->objInstances[vv]->info)->bytes[2]==((CCreGen2ObjInfo*)CGI->objh->objInstances[j]->info)->bytes[2]
  3010. && ((CCastleObjInfo*)CGI->objh->objInstances[vv]->info)->bytes[3]==((CCreGen2ObjInfo*)CGI->objh->objInstances[j]->info)->bytes[3])
  3011. {
  3012. for(int mm=0; mm<town1DefNames.size(); ++mm)
  3013. {
  3014. std::transform(town1DefNames[mm].begin(), town1DefNames[mm].end(), town1DefNames[mm].begin(), (int(*)(int))toupper);
  3015. std::string hlp = CGI->objh->objInstances[vv]->defInfo->name;
  3016. std::transform(hlp.begin(), hlp.end(), hlp.begin(), (int(*)(int))toupper);
  3017. if(town1DefNames[mm]==hlp)
  3018. {
  3019. nxt->subid = mm;
  3020. }
  3021. }
  3022. }
  3023. }
  3024. }
  3025. int lvl;
  3026. if((((CCreGen2ObjInfo*)CGI->objh->objInstances[j]->info)->maxLevel - ((CCreGen2ObjInfo*)CGI->objh->objInstances[j]->info)->minLevel)!=0)
  3027. lvl = rand()%(((CCreGen2ObjInfo*)CGI->objh->objInstances[j]->info)->maxLevel - ((CCreGen2ObjInfo*)CGI->objh->objInstances[j]->info)->minLevel) + ((CCreGen2ObjInfo*)CGI->objh->objInstances[j]->info)->minLevel;
  3028. else lvl = 0;
  3029. nxt->name = creGenNames[nxt->subid][lvl];
  3030. if(creGenNumbers[nxt->subid][lvl]!=NULL)
  3031. {
  3032. CGI->objh->objInstances[j]->defInfo = creGenNumbers[nxt->subid][lvl];
  3033. continue;
  3034. }
  3035. std::vector<DefObjInfo>::iterator pit = std::find(CGameInfo::mainObj->dobjinfo->objs.begin(), CGameInfo::mainObj->dobjinfo->objs.end(),
  3036. nxt->name);
  3037. if(pit == CGameInfo::mainObj->dobjinfo->objs.end())
  3038. {
  3039. nxt->isOnDefList = false;
  3040. }
  3041. else
  3042. {
  3043. nxt->printPriority = pit->priority;
  3044. nxt->isOnDefList = true;
  3045. }
  3046. map.defy.push_back(nxt); // add this def to the vector
  3047. defsToUnpack.push_back(nxt->name);
  3048. CGI->objh->objInstances[j]->defInfo = nxt;
  3049. if(creGenNumbers[nxt->subid][lvl]==NULL)
  3050. {
  3051. creGenNumbers[nxt->subid][lvl] = nxt;
  3052. }
  3053. }
  3054. else //if not as castle
  3055. {
  3056. CGDefInfo * nxt = curDef;
  3057. nxt->id = 17;
  3058. std::vector<int> possibleTowns;
  3059. for(int bb=0; bb<8; ++bb)
  3060. {
  3061. if(((CCreGen2ObjInfo*)CGI->objh->objInstances[j]->info)->castles[0] & (1<<bb))
  3062. {
  3063. possibleTowns.push_back(bb);
  3064. }
  3065. }
  3066. if(((CCreGen2ObjInfo*)CGI->objh->objInstances[j]->info)->castles[1])
  3067. possibleTowns.push_back(8);
  3068. nxt->subid = possibleTowns[rand()%possibleTowns.size()];
  3069. int lvl = rand()%(((CCreGen2ObjInfo*)CGI->objh->objInstances[j]->info)->maxLevel - ((CCreGen2ObjInfo*)CGI->objh->objInstances[j]->info)->minLevel) + ((CCreGen2ObjInfo*)CGI->objh->objInstances[j]->info)->minLevel;
  3070. nxt->name = creGenNames[nxt->subid][lvl];
  3071. if(creGenNumbers[nxt->subid][lvl]!=NULL)
  3072. {
  3073. CGI->objh->objInstances[j]->defInfo = creGenNumbers[nxt->subid][lvl];
  3074. continue;
  3075. }
  3076. std::vector<DefObjInfo>::iterator pit = std::find(CGameInfo::mainObj->dobjinfo->objs.begin(), CGameInfo::mainObj->dobjinfo->objs.end(),
  3077. nxt->name);
  3078. if(pit == CGameInfo::mainObj->dobjinfo->objs.end())
  3079. {
  3080. nxt->isOnDefList = false;
  3081. }
  3082. else
  3083. {
  3084. nxt->printPriority = pit->priority;
  3085. nxt->isOnDefList = true;
  3086. }
  3087. map.defy.push_back(nxt); // add this def to the vector
  3088. defsToUnpack.push_back(nxt->name);
  3089. CGI->objh->objInstances[j]->defInfo = nxt;
  3090. if(creGenNumbers[nxt->subid][lvl]==NULL)
  3091. {
  3092. creGenNumbers[nxt->subid][lvl] = nxt;
  3093. }
  3094. }
  3095. }
  3096. case EDefType::CREGEN3_DEF:
  3097. {
  3098. CGDefInfo * nxt = curDef;
  3099. nxt->id = 17;
  3100. nxt->subid = atoi(CGI->objh->objInstances[j]->defInfo->name.substr(7, 8).c_str());
  3101. int lvl = -1;
  3102. CCreGen3ObjInfo * ct = (CCreGen3ObjInfo*)CGI->objh->objInstances[j]->info;
  3103. if(ct->maxLevel>7)
  3104. ct->maxLevel = 7;
  3105. if(ct->minLevel<1)
  3106. ct->minLevel = 1;
  3107. if((((CCreGen3ObjInfo*)CGI->objh->objInstances[j]->info)->maxLevel - ((CCreGen3ObjInfo*)CGI->objh->objInstances[j]->info)->minLevel)!=0)
  3108. lvl = rand()%(((CCreGen3ObjInfo*)CGI->objh->objInstances[j]->info)->maxLevel - ((CCreGen3ObjInfo*)CGI->objh->objInstances[j]->info)->minLevel) + ((CCreGen3ObjInfo*)CGI->objh->objInstances[j]->info)->minLevel;
  3109. else
  3110. lvl = ((CCreGen3ObjInfo*)CGI->objh->objInstances[j]->info)->maxLevel;
  3111. nxt->name = creGenNames[nxt->subid][lvl];
  3112. if(creGenNumbers[nxt->subid][lvl]!=NULL)
  3113. {
  3114. CGI->objh->objInstances[j]->defInfo = creGenNumbers[nxt->subid][lvl];
  3115. continue;
  3116. }
  3117. std::vector<DefObjInfo>::iterator pit = std::find(CGameInfo::mainObj->dobjinfo->objs.begin(), CGameInfo::mainObj->dobjinfo->objs.end(),
  3118. nxt->name);
  3119. if(pit == CGameInfo::mainObj->dobjinfo->objs.end())
  3120. {
  3121. nxt->isOnDefList = false;
  3122. }
  3123. else
  3124. {
  3125. nxt->printPriority = pit->priority;
  3126. nxt->isOnDefList = true;
  3127. }
  3128. map.defy.push_back(nxt); // add this def to the vector
  3129. defsToUnpack.push_back(nxt->name);
  3130. CGI->objh->objInstances[j]->defInfo = nxt;
  3131. if(creGenNumbers[nxt->subid][lvl]==NULL)
  3132. {
  3133. creGenNumbers[nxt->subid][lvl] = nxt;
  3134. }
  3135. break;
  3136. }
  3137. }//end of main switch
  3138. } //end of sencond for loop
  3139. }