Resources.cpp 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988
  1. #include "Hunt.h"
  2. #include "stdio.h"
  3. HANDLE hfile;
  4. DWORD l;
  5. void GenerateModelMipMaps(TModel *mptr);
  6. void GenerateAlphaFlags(TModel *mptr);
  7. LPVOID _HeapAlloc(HANDLE hHeap,
  8. DWORD dwFlags,
  9. DWORD dwBytes)
  10. {
  11. LPVOID res = HeapAlloc(hHeap,
  12. dwFlags | HEAP_ZERO_MEMORY,
  13. dwBytes);
  14. if (!res)
  15. DoHalt("Heap allocation error!");
  16. HeapAllocated+=dwBytes;
  17. return res;
  18. }
  19. BOOL _HeapFree(HANDLE hHeap,
  20. DWORD dwFlags,
  21. LPVOID lpMem)
  22. {
  23. if (!lpMem) return FALSE;
  24. HeapReleased+=
  25. HeapSize(hHeap, HEAP_NO_SERIALIZE, lpMem);
  26. BOOL res = HeapFree(hHeap,
  27. dwFlags,
  28. lpMem);
  29. if (!res)
  30. DoHalt("Heap free error!");
  31. return res;
  32. }
  33. void AddMessage(LPSTR mt)
  34. {
  35. MessageList.timeleft = timeGetTime() + 2 * 1000;
  36. lstrcpy(MessageList.mtext, mt);
  37. }
  38. void PlaceHunter()
  39. {
  40. if (LockLanding) return;
  41. if (TrophyMode) {
  42. PlayerX = 76*256+128;
  43. PlayerZ = 70*256+128;
  44. PlayerY = GetLandQH(PlayerX, PlayerZ);
  45. return;
  46. }
  47. int p = (timeGetTime() % LandingList.PCount);
  48. PlayerX = (float)LandingList.list[p].x * 256+128;
  49. PlayerZ = (float)LandingList.list[p].y * 256+128;
  50. PlayerY = GetLandQH(PlayerX, PlayerZ);
  51. }
  52. int DitherHi(int C)
  53. {
  54. int d = C & 255;
  55. C = C / 256;
  56. if (rand() * 255 / RAND_MAX < d) C++;
  57. if (C>31) C=31;
  58. return C;
  59. }
  60. void CreateWaterTab()
  61. {
  62. for (int c=0; c<0x8000; c++) {
  63. int R = (c >> 10);
  64. int G = (c >> 5) & 31;
  65. int B = c & 31;
  66. R = 1+(R * 8 ) / 28; if (R>31) R=31;
  67. G = 2+(G * 18) / 28; if (G>31) G=31;
  68. B = 3+(B * 22) / 28; if (B>31) B=31;
  69. FadeTab[64][c] = HiColor(R, G, B);
  70. }
  71. }
  72. void CreateFadeTab()
  73. {
  74. #ifdef _soft
  75. for (int l=0; l<64; l++)
  76. for (int c=0; c<0x8000; c++) {
  77. int R = (c >> 10);
  78. int G = (c >> 5) & 31;
  79. int B = c & 31;
  80. R = (int)((float)R * (l) / 60.f + (float)rand() *0.2f / RAND_MAX); if (R>31) R=31;
  81. G = (int)((float)G * (l) / 60.f + (float)rand() *0.2f / RAND_MAX); if (G>31) G=31;
  82. B = (int)((float)B * (l) / 60.f + (float)rand() *0.2f / RAND_MAX); if (B>31) B=31;
  83. FadeTab[l][c] = HiColor(R, G, B);
  84. }
  85. CreateWaterTab();
  86. #endif
  87. }
  88. void CreateDivTable()
  89. {
  90. DivTbl[0] = 0x7fffffff;
  91. DivTbl[1] = 0x7fffffff;
  92. DivTbl[2] = 0x7fffffff;
  93. for( int i = 3; i < 10240; i++ )
  94. DivTbl[i] = (int) ((float)0x100000000 / i);
  95. for (int y=0; y<32; y++)
  96. for (int x=0; x<32; x++)
  97. RandomMap[y][x] = rand() * 1024 / RAND_MAX;
  98. }
  99. void CreateVideoDIB()
  100. {
  101. hdcMain=GetDC(hwndMain);
  102. hdcCMain = CreateCompatibleDC(hdcMain);
  103. SelectObject(hdcMain, fnt_Midd);
  104. SelectObject(hdcCMain, fnt_Midd);
  105. BITMAPINFOHEADER bmih;
  106. bmih.biSize = sizeof( BITMAPINFOHEADER );
  107. bmih.biWidth =1024;
  108. bmih.biHeight = -768;
  109. bmih.biPlanes = 1;
  110. bmih.biBitCount = 16;
  111. bmih.biCompression = BI_RGB;
  112. bmih.biSizeImage = 0;
  113. bmih.biXPelsPerMeter = 400;
  114. bmih.biYPelsPerMeter = 400;
  115. bmih.biClrUsed = 0;
  116. bmih.biClrImportant = 0;
  117. BITMAPINFO binfo;
  118. binfo.bmiHeader = bmih;
  119. hbmpVideoBuf =
  120. CreateDIBSection(hdcMain, &binfo, DIB_RGB_COLORS, &lpVideoBuf, NULL, 0);
  121. }
  122. int GetObjectH(int x, int y, int R)
  123. {
  124. x = (x<<8) + 128;
  125. y = (y<<8) + 128;
  126. float hr,h;
  127. hr =GetLandH((float)x, (float)y);
  128. h = GetLandH( (float)x+R, (float)y); if (h < hr) hr = h;
  129. h = GetLandH( (float)x-R, (float)y); if (h < hr) hr = h;
  130. h = GetLandH( (float)x, (float)y+R); if (h < hr) hr = h;
  131. h = GetLandH( (float)x, (float)y-R); if (h < hr) hr = h;
  132. hr += 15;
  133. return (int) (hr / ctHScale);
  134. }
  135. int GetObjectHWater(int x, int y)
  136. {
  137. if (FMap[y][x] & fmReverse)
  138. return (int)(HMap[y][x+1]+HMap[y+1][x]) / 2 + 48;
  139. else
  140. return (int)(HMap[y][x]+HMap[y+1][x+1]) / 2 + 48;
  141. }
  142. void CreateTMap()
  143. {
  144. int x,y;
  145. LandingList.PCount = 0;
  146. int ScMaps = 0;
  147. int SL = (100*(OptBrightness + 128))>>8;
  148. for (y=0; y<ctMapSize; y++)
  149. for (x=0; x<ctMapSize; x++) {
  150. if (TMap1[y][x]==0xFFFF) TMap1[y][x] = 1;
  151. if (TMap2[y][x]==0xFFFF) TMap2[y][x] = 1;
  152. if (Textures[TMap1[y][x]]->mR > SL &&
  153. Textures[TMap1[y][x]]->mG > SL &&
  154. Textures[TMap1[y][x]]->mB > SL) ScMaps++;
  155. }
  156. SNOW = (ScMaps > 200000);
  157. /*
  158. for (y=1; y<ctMapSize-1; y++)
  159. for (x=1; x<ctMapSize-1; x++)
  160. if (!(FMap[y][x] & fmWater) ) {
  161. if (FMap[y ][x+1] & fmWater) { FMap[y][x]|= fmWater2; WMap[y][x] = WMap[y ][x+1];}
  162. if (FMap[y+1][x ] & fmWater) { FMap[y][x]|= fmWater2; WMap[y][x] = WMap[y+1][x ];}
  163. if (FMap[y ][x-1] & fmWater) { FMap[y][x]|= fmWater2; WMap[y][x] = WMap[y ][x-1];}
  164. if (FMap[y-1][x ] & fmWater) { FMap[y][x]|= fmWater2; WMap[y][x] = WMap[y-1][x ];}
  165. if (FMap[y][x] & fmWater2)
  166. if (HMap[y][x] > WaterList[WMap[y][x]].wlevel) HMap[y][x]=WaterList[WMap[y][x]].wlevel;
  167. }
  168. for (y=1; y<ctMapSize-1; y++)
  169. for (x=1; x<ctMapSize-1; x++)
  170. if (FMap[y][x] & fmWater2) {
  171. FMap[y][x]-=fmWater2;
  172. FMap[y][x]+=fmWater;
  173. }
  174. */
  175. for (y=1; y<ctMapSize-1; y++)
  176. for (x=1; x<ctMapSize-1; x++)
  177. if (!(FMap[y][x] & fmWater) ) {
  178. if (FMap[y ][x+1] & fmWater) { FMap[y][x]|= fmWater2; WMap[y][x] = WMap[y ][x+1];}
  179. if (FMap[y+1][x ] & fmWater) { FMap[y][x]|= fmWater2; WMap[y][x] = WMap[y+1][x ];}
  180. if (FMap[y ][x-1] & fmWater) { FMap[y][x]|= fmWater2; WMap[y][x] = WMap[y ][x-1];}
  181. if (FMap[y-1][x ] & fmWater) { FMap[y][x]|= fmWater2; WMap[y][x] = WMap[y-1][x ];}
  182. BOOL l = TRUE;
  183. #ifdef _soft
  184. if (FMap[y][x] & fmWater2) {
  185. l = FALSE;
  186. if (HMap[y][x] > WaterList[WMap[y][x]].wlevel) HMap[y][x]=WaterList[WMap[y][x]].wlevel;
  187. HMap[y][x]=WaterList[WMap[y][x]].wlevel;
  188. }
  189. #endif
  190. if (FMap[y-1][x-1] & fmWater) { FMap[y][x]|= fmWater2; WMap[y][x] = WMap[y-1][x-1];}
  191. if (FMap[y-1][x+1] & fmWater) { FMap[y][x]|= fmWater2; WMap[y][x] = WMap[y-1][x+1];}
  192. if (FMap[y+1][x-1] & fmWater) { FMap[y][x]|= fmWater2; WMap[y][x] = WMap[y+1][x-1];}
  193. if (FMap[y+1][x+1] & fmWater) { FMap[y][x]|= fmWater2; WMap[y][x] = WMap[y+1][x+1];}
  194. if (l)
  195. if (FMap[y][x] & fmWater2)
  196. if (HMap[y][x] == WaterList[WMap[y][x]].wlevel) HMap[y][x]+=1;
  197. //if (FMap[y][x] & fmWater2)
  198. }
  199. #ifdef _soft
  200. for (y=0; y<1024; y++)
  201. for (x=0; x<1024; x++ ) {
  202. if( abs( HMap[y][x]-HMap[y+1][x+1] ) > abs( HMap[y+1][x]-HMap[y][x+1] ) )
  203. FMap[y][x] |= fmReverse;
  204. else
  205. FMap[y][x] &= ~fmReverse;
  206. }
  207. #endif
  208. for (y=0; y<ctMapSize; y++)
  209. for (x=0; x<ctMapSize; x++) {
  210. if (!(FMap[y][x] & fmWaterA))
  211. WMap[y][x]=255;
  212. #ifdef _soft
  213. if (MObjects[OMap[y][x]].info.flags & ofNOSOFT2)
  214. if ( (x+y) & 1 )
  215. OMap[y][x]=255;
  216. if (MObjects[OMap[y][x]].info.flags & ofNOSOFT)
  217. OMap[y][x]=255;
  218. #endif
  219. if (OMap[y][x]==254) {
  220. LandingList.list[LandingList.PCount].x = x;
  221. LandingList.list[LandingList.PCount].y = y;
  222. LandingList.PCount++;
  223. OMap[y][x]=255;
  224. }
  225. int ob = OMap[y][x];
  226. if (ob == 255) { HMapO[y][x] = 0; continue; }
  227. //HMapO[y][x] = GetObjectH(x,y);
  228. if (MObjects[ob].info.flags & ofPLACEGROUND) HMapO[y][x] = GetObjectH(x,y, MObjects[ob].info.GrRad);
  229. //if (MObjects[ob].info.flags & ofPLACEWATER) HMapO[y][x] = GetObjectHWater(x,y);
  230. }
  231. if (!LandingList.PCount) {
  232. LandingList.list[LandingList.PCount].x = 256;
  233. LandingList.list[LandingList.PCount].y = 256;
  234. LandingList.PCount=1;
  235. }
  236. if (TrophyMode) {
  237. LandingList.PCount = 0;
  238. for (x=0; x<6; x++) {
  239. LandingList.list[LandingList.PCount].x = 69 + x*3;
  240. LandingList.list[LandingList.PCount].y = 66;
  241. LandingList.PCount++;
  242. }
  243. for (y=0; y<6; y++) {
  244. LandingList.list[LandingList.PCount].x = 87;
  245. LandingList.list[LandingList.PCount].y = 69 + y*3;
  246. LandingList.PCount++;
  247. }
  248. for (x=0; x<6; x++) {
  249. LandingList.list[LandingList.PCount].x = 84 - x*3;
  250. LandingList.list[LandingList.PCount].y = 87;
  251. LandingList.PCount++;
  252. }
  253. for (y=0; y<6; y++) {
  254. LandingList.list[LandingList.PCount].x = 66;
  255. LandingList.list[LandingList.PCount].y = 84 - y*3;
  256. LandingList.PCount++;
  257. }
  258. }
  259. }
  260. void CreateMipMap(WORD* src, WORD* dst, int Ls, int Ld)
  261. {
  262. int scale = Ls / Ld;
  263. int R[64][64], G[64][64], B[64][64];
  264. FillMemory(R, sizeof(R), 0);
  265. FillMemory(G, sizeof(R), 0);
  266. FillMemory(B, sizeof(R), 0);
  267. for (int y=0; y<Ls; y++)
  268. for (int x=0; x<Ls; x++) {
  269. WORD C = *(src + x + y*Ls);
  270. B[ y/scale ][ x/scale ]+= (C>> 0) & 31;
  271. G[ y/scale ][ x/scale ]+= (C>> 5) & 31;
  272. R[ y/scale ][ x/scale ]+= (C>>10) & 31;
  273. }
  274. scale*=scale;
  275. for (y=0; y<Ld; y++)
  276. for (int x=0; x<Ld; x++) {
  277. R[y][x]/=scale;
  278. G[y][x]/=scale;
  279. B[y][x]/=scale;
  280. *(dst + x + y*Ld) = (R[y][x]<<10) + (G[y][x]<<5) + B[y][x];
  281. }
  282. }
  283. int CalcImageDifference(WORD* A, WORD* B, int L)
  284. {
  285. int r = 0; L*=L;
  286. for (int l=0; l<L; l++) {
  287. WORD C1 = *(A + l);
  288. WORD C2 = *(B + l);
  289. int R1 = (C1>>10) & 31;
  290. int G1 = (C1>> 5) & 31;
  291. int B1 = (C1>> 0) & 31;
  292. int R2 = (C2>>10) & 31;
  293. int G2 = (C2>> 5) & 31;
  294. int B2 = (C2>> 0) & 31;
  295. r+=(R1-R2)*(R1-R2) +
  296. (G1-G2)*(G1-G2) +
  297. (B1-B2)*(B1-B2);
  298. }
  299. return r;
  300. }
  301. void RotateImage(WORD* src, WORD* dst, int L)
  302. {
  303. for (int y=0; y<L; y++)
  304. for (int x=0; x<L; x++)
  305. *(dst + x*L + (L-1-y) ) = *(src + x + y*L);
  306. }
  307. void BrightenTexture(WORD* A, int L)
  308. {
  309. int factor=OptBrightness + 128;
  310. //if (factor > 256) factor = (factor-256)*3/2 + 256;
  311. for (int c=0; c<L; c++) {
  312. WORD w = *(A + c);
  313. int B = (w>> 0) & 31;
  314. int G = (w>> 5) & 31;
  315. int R = (w>>10) & 31;
  316. B = (B * factor) >> 8; if (B > 31) B = 31;
  317. G = (G * factor) >> 8; if (G > 31) G = 31;
  318. R = (R * factor) >> 8; if (R > 31) R = 31;
  319. if (OptDayNight==2) { B=G>>3; R=G>>3; }
  320. *(A + c) = (B) + (G<<5) + (R<<10);
  321. }
  322. }
  323. void GenerateMipMap(WORD* A, WORD* D, int L)
  324. {
  325. for (int y=0; y<L; y++)
  326. for (int x=0; x<L; x++) {
  327. int C1 = *(A + x*2 + (y*2+0)*2*L);
  328. int C2 = *(A + x*2+1 + (y*2+0)*2*L);
  329. int C3 = *(A + x*2 + (y*2+1)*2*L);
  330. int C4 = *(A + x*2+1 + (y*2+1)*2*L);
  331. //C4 = C1;
  332. /*
  333. if (L==64)
  334. C3=((C3 & 0x7bde) + (C1 & 0x7bde))>>1;
  335. */
  336. int B = ( ((C1>>0) & 31) + ((C2>>0) & 31) + ((C3>>0) & 31) + ((C4>>0) & 31) +2 ) >> 2;
  337. int G = ( ((C1>>5) & 31) + ((C2>>5) & 31) + ((C3>>5) & 31) + ((C4>>5) & 31) +2 ) >> 2;
  338. int R = ( ((C1>>10) & 31) + ((C2>>10) & 31) + ((C3>>10) & 31) + ((C4>>10) & 31) +2 ) >> 2;
  339. *(D + x + y * L) = HiColor(R,G,B);
  340. }
  341. }
  342. int CalcColorSum(WORD* A, int L)
  343. {
  344. int R = 0, G = 0, B = 0;
  345. for (int x=0; x<L; x++) {
  346. B+= (*(A+x) >> 0) & 31;
  347. G+= (*(A+x) >> 5) & 31;
  348. R+= (*(A+x) >>10) & 31;
  349. }
  350. return HiColor(R/L, G/L, B/L);
  351. }
  352. void GenerateShadedMipMap(WORD* src, WORD* dst, int L)
  353. {
  354. for (int x=0; x<16*16; x++) {
  355. int B = (*(src+x) >> 0) & 31;
  356. int G = (*(src+x) >> 5) & 31;
  357. int R = (*(src+x) >>10) & 31;
  358. R=DitherHi(SkyR*L/8 + R*(256-L)+6);
  359. G=DitherHi(SkyG*L/8 + G*(256-L)+6);
  360. B=DitherHi(SkyB*L/8 + B*(256-L)+6);
  361. *(dst + x) = HiColor(R,G,B);
  362. }
  363. }
  364. void GenerateShadedSkyMipMap(WORD* src, WORD* dst, int L)
  365. {
  366. for (int x=0; x<128*128; x++) {
  367. int B = (*(src+x) >> 0) & 31;
  368. int G = (*(src+x) >> 5) & 31;
  369. int R = (*(src+x) >>10) & 31;
  370. R=DitherHi(SkyR*L/8 + R*(256-L)+6);
  371. G=DitherHi(SkyG*L/8 + G*(256-L)+6);
  372. B=DitherHi(SkyB*L/8 + B*(256-L)+6);
  373. *(dst + x) = HiColor(R,G,B);
  374. }
  375. }
  376. void DATASHIFT(WORD* d, int cnt)
  377. {
  378. cnt>>=1;
  379. /*
  380. for (int l=0; l<cnt; l++)
  381. *(d+l)=(*(d+l)) & 0x3e0;
  382. */
  383. if (HARD3D) return;
  384. for (l=0; l<cnt; l++)
  385. *(d+l)*=2;
  386. }
  387. void ApplyAlphaFlags(WORD* tptr, int cnt)
  388. {
  389. #ifdef _soft
  390. #else
  391. for (int w=0; w<cnt; w++)
  392. *(tptr+w)|=0x8000;
  393. #endif
  394. }
  395. void CalcMidColor(WORD* tptr, int l, int &mr, int &mg, int &mb)
  396. {
  397. for (int w=0; w<l; w++) {
  398. WORD c = *(tptr + w);
  399. mb+=((c>> 0) & 31)*8;
  400. mg+=((c>> 5) & 31)*8;
  401. mr+=((c>>10) & 31)*8;
  402. }
  403. mr/=l; mg/=l; mb/=l;
  404. }
  405. void LoadTexture(TEXTURE* &T)
  406. {
  407. T = (TEXTURE*) _HeapAlloc(Heap, 0, sizeof(TEXTURE));
  408. DWORD L;
  409. ReadFile(hfile, T->DataA, 128*128*2, &L, NULL);
  410. for (int y=0; y<128; y++)
  411. for (int x=0; x<128; x++)
  412. if (!T->DataA[y*128+x]) T->DataA[y*128+x]=1;
  413. BrightenTexture(T->DataA, 128*128);
  414. CalcMidColor(T->DataA, 128*128, T->mR, T->mG, T->mB);
  415. GenerateMipMap(T->DataA, T->DataB, 64);
  416. GenerateMipMap(T->DataB, T->DataC, 32);
  417. GenerateMipMap(T->DataC, T->DataD, 16);
  418. memcpy(T->SDataC[0], T->DataC, 32*32*2);
  419. memcpy(T->SDataC[1], T->DataC, 32*32*2);
  420. DATASHIFT((unsigned short *)T, sizeof(TEXTURE));
  421. for (int w=0; w<32*32; w++)
  422. T->SDataC[1][w] = FadeTab[48][T->SDataC[1][w]>>1];
  423. ApplyAlphaFlags(T->DataA, 128*128);
  424. ApplyAlphaFlags(T->DataB, 64*64);
  425. ApplyAlphaFlags(T->DataC, 32*32);
  426. }
  427. void LoadSky()
  428. {
  429. SetFilePointer(hfile, 256*512*OptDayNight, NULL, FILE_CURRENT);
  430. ReadFile(hfile, SkyPic, 256*256*2, &l, NULL);
  431. SetFilePointer(hfile, 256*512*(2-OptDayNight), NULL, FILE_CURRENT);
  432. BrightenTexture(SkyPic, 256*256);
  433. for (int y=0; y<128; y++)
  434. for (int x=0; x<128; x++)
  435. SkyFade[0][y*128+x] = SkyPic[y*2*256 + x*2];
  436. for (int l=1; l<8; l++)
  437. GenerateShadedSkyMipMap(SkyFade[0], SkyFade[l], l*32-16);
  438. GenerateShadedSkyMipMap(SkyFade[0], SkyFade[8], 250);
  439. ApplyAlphaFlags(SkyPic, 256*256);
  440. //DATASHIFT(SkyPic, 256*256*2);
  441. }
  442. void LoadSkyMap()
  443. {
  444. ReadFile(hfile, SkyMap, 128*128, &l, NULL);
  445. }
  446. void fp_conv(LPVOID d)
  447. {
  448. int i;
  449. float f;
  450. memcpy(&i, d, 4);
  451. #ifdef _d3d
  452. f = ((float)i) / 256.f;
  453. #else
  454. f = ((float)i);
  455. #endif
  456. memcpy(d, &f, 4);
  457. }
  458. void CorrectModel(TModel *mptr)
  459. {
  460. TFace tface[1024];
  461. for (int f=0; f<mptr->FCount; f++) {
  462. if (!(mptr->gFace[f].Flags & sfDoubleSide))
  463. mptr->gFace[f].Flags |= sfNeedVC;
  464. #ifdef _soft
  465. mptr->gFace[f].tax = (mptr->gFace[f].tax<<16) + 0x8000;
  466. mptr->gFace[f].tay = (mptr->gFace[f].tay<<16) + 0x8000;
  467. mptr->gFace[f].tbx = (mptr->gFace[f].tbx<<16) + 0x8000;
  468. mptr->gFace[f].tby = (mptr->gFace[f].tby<<16) + 0x8000;
  469. mptr->gFace[f].tcx = (mptr->gFace[f].tcx<<16) + 0x8000;
  470. mptr->gFace[f].tcy = (mptr->gFace[f].tcy<<16) + 0x8000;
  471. #else
  472. fp_conv(&mptr->gFace[f].tax);
  473. fp_conv(&mptr->gFace[f].tay);
  474. fp_conv(&mptr->gFace[f].tbx);
  475. fp_conv(&mptr->gFace[f].tby);
  476. fp_conv(&mptr->gFace[f].tcx);
  477. fp_conv(&mptr->gFace[f].tcy);
  478. #endif
  479. }
  480. int fp = 0;
  481. for (f=0; f<mptr->FCount; f++)
  482. if ( (mptr->gFace[f].Flags & (sfOpacity | sfTransparent))==0)
  483. {
  484. tface[fp] = mptr->gFace[f];
  485. fp++;
  486. }
  487. for (f=0; f<mptr->FCount; f++)
  488. if ( (mptr->gFace[f].Flags & sfOpacity)!=0)
  489. {
  490. tface[fp] = mptr->gFace[f];
  491. fp++;
  492. }
  493. for (f=0; f<mptr->FCount; f++)
  494. if ( (mptr->gFace[f].Flags & sfTransparent)!=0)
  495. {
  496. tface[fp] = mptr->gFace[f];
  497. fp++;
  498. }
  499. memcpy( mptr->gFace, tface, sizeof(tface) );
  500. }
  501. void LoadModel(TModel* &mptr)
  502. {
  503. mptr = (TModel*) _HeapAlloc(Heap, 0, sizeof(TModel));
  504. ReadFile( hfile, &mptr->VCount, 4, &l, NULL );
  505. ReadFile( hfile, &mptr->FCount, 4, &l, NULL );
  506. ReadFile( hfile, &OCount, 4, &l, NULL );
  507. ReadFile( hfile, &mptr->TextureSize, 4, &l, NULL );
  508. ReadFile( hfile, mptr->gFace, mptr->FCount<<6, &l, NULL );
  509. ReadFile( hfile, mptr->gVertex, mptr->VCount<<4, &l, NULL );
  510. ReadFile( hfile, gObj, OCount*48, &l, NULL );
  511. if (HARD3D) CalcLights(mptr);
  512. int ts = mptr->TextureSize;
  513. if (HARD3D) mptr->TextureHeight = 256;
  514. else mptr->TextureHeight = mptr->TextureSize>>9;
  515. mptr->TextureSize = mptr->TextureHeight*512;
  516. mptr->lpTexture = (WORD*) _HeapAlloc(Heap, 0, mptr->TextureSize);
  517. ReadFile(hfile, mptr->lpTexture, ts, &l, NULL);
  518. BrightenTexture(mptr->lpTexture, ts/2);
  519. for (int v=0; v<mptr->VCount; v++) {
  520. mptr->gVertex[v].x*=2.f;
  521. mptr->gVertex[v].y*=2.f;
  522. mptr->gVertex[v].z*=-2.f;
  523. }
  524. CorrectModel(mptr);
  525. DATASHIFT(mptr->lpTexture, mptr->TextureSize);
  526. }
  527. void LoadAnimation(TVTL &vtl)
  528. {
  529. int vc;
  530. DWORD l;
  531. ReadFile( hfile, &vc, 4, &l, NULL );
  532. ReadFile( hfile, &vc, 4, &l, NULL );
  533. ReadFile( hfile, &vtl.aniKPS, 4, &l, NULL );
  534. ReadFile( hfile, &vtl.FramesCount, 4, &l, NULL );
  535. vtl.FramesCount++;
  536. vtl.AniTime = (vtl.FramesCount * 1000) / vtl.aniKPS;
  537. vtl.aniData = (short int*)
  538. _HeapAlloc(Heap, 0, (vc*vtl.FramesCount*6) );
  539. ReadFile( hfile, vtl.aniData, (vc*vtl.FramesCount*6), &l, NULL);
  540. }
  541. void LoadModelEx(TModel* &mptr, char* FName)
  542. {
  543. hfile = CreateFile(FName,
  544. GENERIC_READ, FILE_SHARE_READ,
  545. NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
  546. if (hfile==INVALID_HANDLE_VALUE) {
  547. char sz[512];
  548. wsprintf( sz, "Error opening file\n%s.", FName );
  549. DoHalt(sz);
  550. }
  551. mptr = (TModel*) _HeapAlloc(Heap, 0, sizeof(TModel));
  552. ReadFile( hfile, &mptr->VCount, 4, &l, NULL );
  553. ReadFile( hfile, &mptr->FCount, 4, &l, NULL );
  554. ReadFile( hfile, &OCount, 4, &l, NULL );
  555. ReadFile( hfile, &mptr->TextureSize, 4, &l, NULL );
  556. ReadFile( hfile, mptr->gFace, mptr->FCount<<6, &l, NULL );
  557. ReadFile( hfile, mptr->gVertex, mptr->VCount<<4, &l, NULL );
  558. ReadFile( hfile, gObj, OCount*48, &l, NULL );
  559. int ts = mptr->TextureSize;
  560. if (HARD3D) mptr->TextureHeight = 256;
  561. else mptr->TextureHeight = mptr->TextureSize>>9;
  562. mptr->TextureSize = mptr->TextureHeight*512;
  563. mptr->lpTexture = (WORD*) _HeapAlloc(Heap, 0, mptr->TextureSize);
  564. ReadFile(hfile, mptr->lpTexture, ts, &l, NULL);
  565. BrightenTexture(mptr->lpTexture, ts/2);
  566. for (int v=0; v<mptr->VCount; v++) {
  567. mptr->gVertex[v].x*=2.f;
  568. mptr->gVertex[v].y*=2.f;
  569. mptr->gVertex[v].z*=-2.f;
  570. }
  571. CorrectModel(mptr);
  572. DATASHIFT(mptr->lpTexture, mptr->TextureSize);
  573. GenerateModelMipMaps(mptr);
  574. GenerateAlphaFlags(mptr);
  575. }
  576. void LoadWav(char* FName, TSFX &sfx)
  577. {
  578. DWORD l;
  579. HANDLE hfile = CreateFile(FName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
  580. if( hfile==INVALID_HANDLE_VALUE ) {
  581. char sz[512];
  582. wsprintf( sz, "Error opening file\n%s.", FName );
  583. DoHalt(sz);
  584. }
  585. _HeapFree(Heap, 0, (void*)sfx.lpData);
  586. sfx.lpData = NULL;
  587. SetFilePointer( hfile, 36, NULL, FILE_BEGIN );
  588. char c[5]; c[4] = 0;
  589. for ( ; ; ) {
  590. ReadFile( hfile, c, 1, &l, NULL );
  591. if( c[0] == 'd' ) {
  592. ReadFile( hfile, &c[1], 3, &l, NULL );
  593. if( !lstrcmp( c, "data" ) ) break;
  594. else SetFilePointer( hfile, -3, NULL, FILE_CURRENT );
  595. }
  596. }
  597. ReadFile( hfile, &sfx.length, 4, &l, NULL );
  598. sfx.lpData = (short int*)
  599. _HeapAlloc( Heap, 0, sfx.length );
  600. ReadFile( hfile, sfx.lpData, sfx.length, &l, NULL );
  601. CloseHandle(hfile);
  602. }
  603. WORD conv_565(WORD c)
  604. {
  605. return (c & 31) + ( (c & 0xFFE0) << 1 );
  606. }
  607. int conv_xGx(int c) {
  608. if (OptDayNight!=2) return c;
  609. DWORD a = c;
  610. int r = ((c>> 0) & 0xFF);
  611. int g = ((c>> 8) & 0xFF);
  612. int b = ((c>>16) & 0xFF);
  613. c = max(r,g);
  614. c = max(c,b);
  615. return (c<<8) + (a & 0xFF000000);
  616. }
  617. void conv_pic(TPicture &pic)
  618. {
  619. if (!HARD3D) return;
  620. for (int y=0; y<pic.H; y++)
  621. for (int x=0; x<pic.W; x++)
  622. *(pic.lpImage + x + y*pic.W) = conv_565(*(pic.lpImage + x + y*pic.W));
  623. }
  624. void LoadPicture(TPicture &pic, LPSTR pname)
  625. {
  626. int C;
  627. byte fRGB[800][3];
  628. BITMAPFILEHEADER bmpFH;
  629. BITMAPINFOHEADER bmpIH;
  630. DWORD l;
  631. HANDLE hfile;
  632. hfile = CreateFile(pname, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );
  633. if( hfile==INVALID_HANDLE_VALUE ) {
  634. char sz[512];
  635. wsprintf( sz, "Error opening file\n%s.", pname );
  636. DoHalt(sz);
  637. }
  638. ReadFile( hfile, &bmpFH, sizeof( BITMAPFILEHEADER ), &l, NULL );
  639. ReadFile( hfile, &bmpIH, sizeof( BITMAPINFOHEADER ), &l, NULL );
  640. _HeapFree(Heap, 0, (void*)pic.lpImage);
  641. pic.lpImage = NULL;
  642. pic.W = bmpIH.biWidth;
  643. pic.H = bmpIH.biHeight;
  644. pic.lpImage = (WORD*) _HeapAlloc(Heap, 0, pic.W * pic.H * 2);
  645. for (int y=0; y<pic.H; y++) {
  646. ReadFile( hfile, fRGB, 3*pic.W, &l, NULL );
  647. for (int x=0; x<pic.W; x++) {
  648. C = ((int)fRGB[x][2]/8<<10) + ((int)fRGB[x][1]/8<< 5) + ((int)fRGB[x][0]/8) ;
  649. *(pic.lpImage + (pic.H-y-1)*pic.W+x) = C;
  650. }
  651. }
  652. CloseHandle( hfile );
  653. }
  654. void LoadPictureTGA(TPicture &pic, LPSTR pname)
  655. {
  656. DWORD l;
  657. WORD w,h;
  658. HANDLE hfile;
  659. hfile = CreateFile(pname, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );
  660. if( hfile==INVALID_HANDLE_VALUE ) {
  661. char sz[512];
  662. wsprintf( sz, "Error opening file\n%s.", pname );
  663. DoHalt(sz);
  664. }
  665. SetFilePointer(hfile, 12, 0, FILE_BEGIN);
  666. ReadFile( hfile, &w, 2, &l, NULL );
  667. ReadFile( hfile, &h, 2, &l, NULL );
  668. SetFilePointer(hfile, 18, 0, FILE_BEGIN);
  669. _HeapFree(Heap, 0, (void*)pic.lpImage);
  670. pic.lpImage = NULL;
  671. pic.W = w;
  672. pic.H = h;
  673. pic.lpImage = (WORD*) _HeapAlloc(Heap, 0, pic.W * pic.H * 2);
  674. for (int y=0; y<pic.H; y++)
  675. ReadFile( hfile, (void*)(pic.lpImage + (pic.H-y-1)*pic.W), 2*pic.W, &l, NULL );
  676. CloseHandle( hfile );
  677. }
  678. void CreateMipMapMT(WORD* dst, WORD* src, int H)
  679. {
  680. for (int y=0; y<H; y++)
  681. for (int x=0; x<127; x++) {
  682. int C1 = *(src + (x*2+0) + (y*2+0)*256);
  683. int C2 = *(src + (x*2+1) + (y*2+0)*256);
  684. int C3 = *(src + (x*2+0) + (y*2+1)*256);
  685. int C4 = *(src + (x*2+1) + (y*2+1)*256);
  686. if (!HARD3D) { C1>>=1; C2>>=1; C3>>=1; C4>>=1; }
  687. /*if (C1 == 0 && C2!=0) C1 = C2;
  688. if (C1 == 0 && C3!=0) C1 = C3;
  689. if (C1 == 0 && C4!=0) C1 = C4;*/
  690. if (C1 == 0) { *(dst + x + y*128) = 0; continue; }
  691. //C4 = C1;
  692. if (!C2) C2=C1;
  693. if (!C3) C3=C1;
  694. if (!C4) C4=C1;
  695. int B = ( ((C1>> 0) & 31) + ((C2 >>0) & 31) + ((C3 >>0) & 31) + ((C4 >>0) & 31) +2 ) >> 2;
  696. int G = ( ((C1>> 5) & 31) + ((C2 >>5) & 31) + ((C3 >>5) & 31) + ((C4 >>5) & 31) +2 ) >> 2;
  697. int R = ( ((C1>>10) & 31) + ((C2>>10) & 31) + ((C3>>10) & 31) + ((C4>>10) & 31) +2 ) >> 2;
  698. if (!HARD3D) *(dst + x + y * 128) = HiColor(R,G,B)*2;
  699. else *(dst + x + y * 128) = HiColor(R,G,B);
  700. }
  701. }
  702. void CreateMipMapMT2(WORD* dst, WORD* src, int H)
  703. {
  704. for (int y=0; y<H; y++)
  705. for (int x=0; x<63; x++) {
  706. int C1 = *(src + (x*2+0) + (y*2+0)*128);
  707. int C2 = *(src + (x*2+1) + (y*2+0)*128);
  708. int C3 = *(src + (x*2+0) + (y*2+1)*128);
  709. int C4 = *(src + (x*2+1) + (y*2+1)*128);
  710. if (!HARD3D) { C1>>=1; C2>>=1; C3>>=1; C4>>=1; }
  711. if (C1 == 0) { *(dst + x + y*64) = 0; continue; }
  712. //C2 = C1;
  713. if (!C2) C2=C1;
  714. if (!C3) C3=C1;
  715. if (!C4) C4=C1;
  716. int B = ( ((C1>> 0) & 31) + ((C2 >>0) & 31) + ((C3 >>0) & 31) + ((C4 >>0) & 31) +2 ) >> 2;
  717. int G = ( ((C1>> 5) & 31) + ((C2 >>5) & 31) + ((C3 >>5) & 31) + ((C4 >>5) & 31) +2 ) >> 2;
  718. int R = ( ((C1>>10) & 31) + ((C2>>10) & 31) + ((C3>>10) & 31) + ((C4>>10) & 31) +2 ) >> 2;
  719. if (!HARD3D) *(dst + x + y * 64) = HiColor(R,G,B)*2;
  720. else *(dst + x + y * 64) = HiColor(R,G,B);
  721. }
  722. }
  723. void GetObjectCaracteristics(TModel* mptr, int& ylo, int& yhi)
  724. {
  725. ylo = 10241024;
  726. yhi =-10241024;
  727. for (int v=0; v<mptr->VCount; v++) {
  728. if (mptr->gVertex[v].y < ylo) ylo = (int)mptr->gVertex[v].y;
  729. if (mptr->gVertex[v].y > yhi) yhi = (int)mptr->gVertex[v].y;
  730. }
  731. if (yhi<ylo) yhi=ylo+1;
  732. }
  733. void GenerateAlphaFlags(TModel *mptr)
  734. {
  735. #ifdef _soft
  736. #else
  737. int w;
  738. BOOL Opacity = FALSE;
  739. WORD* tptr = mptr->lpTexture;
  740. for (w=0; w<mptr->FCount; w++)
  741. if ((mptr->gFace[w].Flags & sfOpacity)>0) Opacity = TRUE;
  742. if (Opacity) {
  743. for (w=0; w<256*256; w++)
  744. if ( *(tptr+w)>0 ) *(tptr+w)=(*(tptr+w)) + 0x8000; }
  745. else
  746. for (w=0; w<256*256; w++)
  747. *(tptr+w)=(*(tptr+w)) + 0x8000;
  748. tptr = mptr->lpTexture2;
  749. if (tptr==NULL) return;
  750. if (Opacity) {
  751. for (w=0; w<128*128; w++)
  752. if ( (*(tptr+w))>0 ) *(tptr+w)=(*(tptr+w)) + 0x8000; }
  753. else
  754. for (w=0; w<128*128; w++)
  755. *(tptr+w)=(*(tptr+w)) + 0x8000;
  756. tptr = mptr->lpTexture3;
  757. if (tptr==NULL) return;
  758. if (Opacity) {
  759. for (w=0; w<64*64; w++)
  760. if ( (*(tptr+w))>0 ) *(tptr+w)=(*(tptr+w)) + 0x8000; }
  761. else
  762. for (w=0; w<64*64; w++)
  763. *(tptr+w)=(*(tptr+w)) + 0x8000;
  764. #endif
  765. }
  766. void GenerateModelMipMaps(TModel *mptr)
  767. {
  768. int th = (mptr->TextureHeight) / 2;
  769. mptr->lpTexture2 =
  770. (WORD*) _HeapAlloc(Heap, HEAP_ZERO_MEMORY , (1+th)*128*2);
  771. CreateMipMapMT(mptr->lpTexture2, mptr->lpTexture, th);
  772. th = (mptr->TextureHeight) / 4;
  773. mptr->lpTexture3 =
  774. (WORD*) _HeapAlloc(Heap, HEAP_ZERO_MEMORY , (1+th)*64*2);
  775. CreateMipMapMT2(mptr->lpTexture3, mptr->lpTexture2, th);
  776. }
  777. void GenerateMapImage()
  778. {
  779. int YShift = 23;
  780. int XShift = 11;
  781. int lsw = MapPic.W;
  782. for (int y=0; y<256; y++)
  783. for (int x=0; x<256; x++) {
  784. int t;
  785. WORD c;
  786. if (FMap[y<<2][x<<2] & fmWater) {
  787. t = WaterList[WMap[y<<2][x<<2]].tindex;
  788. c= Textures[t]->DataD[(y & 15)*16+(x&15)];
  789. } else {
  790. t = TMap1[y<<2][x<<2];
  791. c= Textures[t]->DataC[(y & 31)*32+(x&31)];
  792. }
  793. if (!HARD3D) c=c>>1; else c=conv_565(c);
  794. *((WORD*)MapPic.lpImage + (y+YShift)*lsw + x + XShift) = c;
  795. }
  796. }
  797. void ReleaseResources()
  798. {
  799. HeapReleased=0;
  800. for (int t=0; t<1024; t++)
  801. if (Textures[t]) {
  802. _HeapFree(Heap, 0, (void*)Textures[t]);
  803. Textures[t] = NULL;
  804. } else break;
  805. for (int m=0; m<255; m++) {
  806. TModel *mptr = MObjects[m].model;
  807. if (mptr) {
  808. _HeapFree(Heap,0,MObjects[m].bmpmodel.lpTexture);
  809. MObjects[m].bmpmodel.lpTexture = NULL;
  810. if (MObjects[m].vtl.FramesCount>0) {
  811. _HeapFree(Heap, 0, MObjects[m].vtl.aniData);
  812. MObjects[m].vtl.aniData = NULL;
  813. }
  814. _HeapFree(Heap,0,mptr->lpTexture); mptr->lpTexture = NULL;
  815. _HeapFree(Heap,0,mptr->lpTexture2); mptr->lpTexture2 = NULL;
  816. _HeapFree(Heap,0,mptr->lpTexture3); mptr->lpTexture3 = NULL;
  817. _HeapFree(Heap,0,MObjects[m].model);
  818. MObjects[m].model = NULL;
  819. MObjects[m].vtl.FramesCount = 0;
  820. } else break;
  821. }
  822. for (int a=0; a<255; a++) {
  823. if (!Ambient[a].sfx.lpData) break;
  824. _HeapFree(Heap, 0, Ambient[a].sfx.lpData);
  825. Ambient[a].sfx.lpData = NULL;
  826. }
  827. for (int r=0; r<255; r++) {
  828. if (!RandSound[r].lpData) break;
  829. _HeapFree(Heap, 0, RandSound[r].lpData);
  830. RandSound[r].lpData = NULL;
  831. RandSound[r].length = 0;
  832. }
  833. }
  834. void LoadBMPModel(TObject &obj)
  835. {
  836. obj.bmpmodel.lpTexture = (WORD*) _HeapAlloc(Heap, 0, 128 * 128 * 2);
  837. //WORD * lpT = (WORD*) _HeapAlloc(Heap, 0, 256 * 256 * 2);
  838. //ReadFile(hfile, lpT, 256*256*2, &l, NULL);
  839. //DATASHIFT(obj.bmpmodel.lpTexture, 128*128*2);
  840. //BrightenTexture(lpT, 256*256);
  841. ReadFile(hfile, obj.bmpmodel.lpTexture, 128*128*2, &l, NULL);
  842. BrightenTexture(obj.bmpmodel.lpTexture, 128*128);
  843. DATASHIFT(obj.bmpmodel.lpTexture, 128*128*2);
  844. //CreateMipMapMT(obj.bmpmodel.lpTexture, lpT, 128);
  845. //_HeapFree(Heap, 0, lpT);
  846. if (HARD3D)
  847. for (int x=0; x<128; x++)
  848. for (int y=0; y<128; y++)
  849. if ( *(obj.bmpmodel.lpTexture + x + y*128) )
  850. *(obj.bmpmodel.lpTexture + x + y*128) |= 0x8000;
  851. float mxx = obj.model->gVertex[0].x+0.5f;
  852. float mnx = obj.model->gVertex[0].x-0.5f;
  853. float mxy = obj.model->gVertex[0].x+0.5f;
  854. float mny = obj.model->gVertex[0].y-0.5f;
  855. for (int v=0; v<obj.model->VCount; v++) {
  856. float x = obj.model->gVertex[v].x;
  857. float y = obj.model->gVertex[v].y;
  858. if (x > mxx) mxx=x;
  859. if (x < mnx) mnx=x;
  860. if (y > mxy) mxy=y;
  861. if (y < mny) mny=y;
  862. }
  863. obj.bmpmodel.gVertex[0].x = mnx;
  864. obj.bmpmodel.gVertex[0].y = mxy;
  865. obj.bmpmodel.gVertex[0].z = 0;
  866. obj.bmpmodel.gVertex[1].x = mxx;
  867. obj.bmpmodel.gVertex[1].y = mxy;
  868. obj.bmpmodel.gVertex[1].z = 0;
  869. obj.bmpmodel.gVertex[2].x = mxx;
  870. obj.bmpmodel.gVertex[2].y = mny;
  871. obj.bmpmodel.gVertex[2].z = 0;
  872. obj.bmpmodel.gVertex[3].x = mnx;
  873. obj.bmpmodel.gVertex[3].y = mny;
  874. obj.bmpmodel.gVertex[3].z = 0;
  875. }
  876. void LoadResources()
  877. {
  878. int FadeRGB[3][3];
  879. int TransRGB[3][3];
  880. int tc,mc;
  881. char MapName[128],RscName[128];
  882. HeapAllocated=0;
  883. if (strstr(ProjectName, "trophy")) {
  884. TrophyMode = TRUE;
  885. ctViewR = 60;
  886. ctViewR1 = 48;
  887. }
  888. wsprintf(MapName,"%s%s", ProjectName, ".map");
  889. wsprintf(RscName,"%s%s", ProjectName, ".rsc");
  890. ReleaseResources();
  891. hfile = CreateFile(RscName,
  892. GENERIC_READ, FILE_SHARE_READ,
  893. NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
  894. if (hfile==INVALID_HANDLE_VALUE) {
  895. char sz[512];
  896. wsprintf( sz, "Error opening resource file\n%s.", RscName );
  897. DoHalt(sz);
  898. return; }
  899. ReadFile(hfile, &tc, 4, &l, NULL);
  900. ReadFile(hfile, &mc, 4, &l, NULL);
  901. ReadFile(hfile, FadeRGB, 4*3*3, &l, NULL);
  902. ReadFile(hfile, TransRGB, 4*3*3, &l, NULL);
  903. SkyR = FadeRGB[OptDayNight][0];
  904. SkyG = FadeRGB[OptDayNight][1];
  905. SkyB = FadeRGB[OptDayNight][2];
  906. SkyTR = TransRGB[OptDayNight][0];
  907. SkyTG = TransRGB[OptDayNight][1];
  908. SkyTB = TransRGB[OptDayNight][2];
  909. if (OptDayNight==2) {
  910. SkyR = 0; SkyB = 0; SkyTR = 0; SkyTB = 0;
  911. }
  912. SkyTR = min(255,SkyTR * (OptBrightness + 128) / 256);
  913. SkyTG = min(255,SkyTG * (OptBrightness + 128) / 256);
  914. SkyTB = min(255,SkyTB * (OptBrightness + 128) / 256);
  915. SkyR = min(255,SkyR * (OptBrightness + 128) / 256);
  916. SkyG = min(255,SkyG * (OptBrightness + 128) / 256);
  917. SkyB = min(255,SkyB * (OptBrightness + 128) / 256);
  918. PrintLog("Loading textures:");
  919. for (int tt=0; tt<tc; tt++)
  920. LoadTexture(Textures[tt]);
  921. PrintLog(" Done.\n");
  922. PrintLog("Loading models:");
  923. PrintLoad("Loading models...");
  924. for (int mm=0; mm<mc; mm++) {
  925. ReadFile(hfile, &MObjects[mm].info, 64, &l, NULL);
  926. MObjects[mm].info.Radius*=2;
  927. MObjects[mm].info.YLo*=2;
  928. MObjects[mm].info.YHi*=2;
  929. MObjects[mm].info.linelenght = (MObjects[mm].info.linelenght / 128) * 128;
  930. LoadModel(MObjects[mm].model);
  931. LoadBMPModel(MObjects[mm]);
  932. if (MObjects[mm].info.flags & ofNOLIGHT)
  933. FillMemory(MObjects[mm].model->VLight, 4*1024*4, 0);
  934. if (MObjects[mm].info.flags & ofANIMATED)
  935. LoadAnimation(MObjects[mm].vtl);
  936. MObjects[mm].info.BoundR = 0;
  937. for (int v=0; v<MObjects[mm].model->VCount; v++) {
  938. float r = (float)sqrt(MObjects[mm].model->gVertex[v].x * MObjects[mm].model->gVertex[v].x +
  939. MObjects[mm].model->gVertex[v].z * MObjects[mm].model->gVertex[v].z );
  940. if (r>MObjects[mm].info.BoundR) MObjects[mm].info.BoundR=r;
  941. }
  942. if (MObjects[mm].info.flags & ofBOUND)
  943. CalcBoundBox(MObjects[mm].model, MObjects[mm].bound);
  944. GenerateModelMipMaps(MObjects[mm].model);
  945. GenerateAlphaFlags(MObjects[mm].model);
  946. }
  947. PrintLog(" Done.\n");
  948. PrintLoad("Finishing with .res...");
  949. PrintLog("Finishing with .res:");
  950. LoadSky();
  951. LoadSkyMap();
  952. int FgCount;
  953. ReadFile(hfile, &FgCount, 4, &l, NULL);
  954. ReadFile(hfile, &FogsList[1], FgCount * sizeof(TFogEntity), &l, NULL);
  955. for (int f=0; f<=FgCount; f++) {
  956. int fb = (FogsList[f].fogRGB >> 00) & 0xFF;
  957. int fg = (FogsList[f].fogRGB >> 8) & 0xFF;
  958. int fr = (FogsList[f].fogRGB >> 16) & 0xFF;
  959. #ifdef _d3d
  960. FogsList[f].fogRGB = (fr) + (fg<<8) + (fb<<16);
  961. #endif
  962. if (OptDayNight==2) FogsList[f].fogRGB&=0x00FF00;
  963. }
  964. int RdCount, AmbCount, WtrCount;
  965. ReadFile(hfile, &RdCount, 4, &l, NULL);
  966. for (int r=0; r<RdCount; r++) {
  967. ReadFile(hfile, &RandSound[r].length, 4, &l, NULL);
  968. RandSound[r].lpData = (short int*) _HeapAlloc(Heap,0,RandSound[r].length);
  969. ReadFile(hfile, RandSound[r].lpData, RandSound[r].length, &l, NULL);
  970. }
  971. ReadFile(hfile, &AmbCount, 4, &l, NULL);
  972. for (int a=0; a<AmbCount; a++) {
  973. ReadFile(hfile, &Ambient[a].sfx.length, 4, &l, NULL);
  974. Ambient[a].sfx.lpData = (short int*) _HeapAlloc(Heap,0,Ambient[a].sfx.length);
  975. ReadFile(hfile, Ambient[a].sfx.lpData, Ambient[a].sfx.length, &l, NULL);
  976. ReadFile(hfile, Ambient[a].rdata, sizeof(Ambient[a].rdata), &l, NULL);
  977. ReadFile(hfile, &Ambient[a].RSFXCount, 4, &l, NULL);
  978. ReadFile(hfile, &Ambient[a].AVolume, 4, &l, NULL);
  979. if (Ambient[a].RSFXCount)
  980. Ambient[a].RndTime = (Ambient[a].rdata[0].RFreq / 2 + rRand(Ambient[a].rdata[0].RFreq)) * 1000;
  981. int F = Ambient[a].rdata[0].RFreq;
  982. int E = Ambient[a].rdata[0].REnvir;
  983. /////////////////
  984. //wsprintf(logt,"Env=%d Flag=%d Freq=%d\n", E, Ambient[a].rdata[0].Flags, F);
  985. //PrintLog(logt);
  986. if (OptDayNight==2)
  987. for (int r=0; r<Ambient[a].RSFXCount; r++)
  988. if (Ambient[a].rdata[r].Flags)
  989. {
  990. if (r!=15) memcpy(&Ambient[a].rdata[r], &Ambient[a].rdata[r+1], (15-r)*sizeof(TRD));
  991. Ambient[a].RSFXCount--;
  992. r--;
  993. }
  994. Ambient[a].rdata[0].RFreq = F;
  995. Ambient[a].rdata[0].REnvir = E;
  996. }
  997. ReadFile(hfile, &WtrCount, 4, &l, NULL);
  998. ReadFile(hfile, WaterList, 16*WtrCount, &l, NULL);
  999. WaterList[255].wlevel = 0;
  1000. for (int w=0; w<WtrCount; w++) {
  1001. #ifdef _3dfx
  1002. WaterList[w].fogRGB = (Textures[WaterList[w].tindex]->mR) +
  1003. (Textures[WaterList[w].tindex]->mG<<8) +
  1004. (Textures[WaterList[w].tindex]->mB<<16);
  1005. #else
  1006. WaterList[w].fogRGB = (Textures[WaterList[w].tindex]->mB) +
  1007. (Textures[WaterList[w].tindex]->mG<<8) +
  1008. (Textures[WaterList[w].tindex]->mR<<16);
  1009. #endif
  1010. }
  1011. CloseHandle(hfile);
  1012. PrintLog(" Done.\n");
  1013. //================ Load MAPs file ==================//
  1014. PrintLoad("Loading .map...");
  1015. PrintLog("Loading .map:");
  1016. hfile = CreateFile(MapName,
  1017. GENERIC_READ, FILE_SHARE_READ,
  1018. NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
  1019. if (hfile==INVALID_HANDLE_VALUE)
  1020. DoHalt("Error opening map file.");
  1021. ReadFile(hfile, HMap, 1024*1024, &l, NULL);
  1022. ReadFile(hfile, TMap1, 1024*1024*2, &l, NULL);
  1023. ReadFile(hfile, TMap2, 1024*1024*2, &l, NULL);
  1024. ReadFile(hfile, OMap, 1024*1024, &l, NULL);
  1025. ReadFile(hfile, FMap, 1024*1024*2, &l, NULL);
  1026. SetFilePointer(hfile, 1024*1024*OptDayNight, NULL, FILE_CURRENT);
  1027. ReadFile(hfile, LMap, 1024*1024, &l, NULL);
  1028. SetFilePointer(hfile, 1024*1024*(2-OptDayNight), NULL, FILE_CURRENT);
  1029. ReadFile(hfile, WMap , 1024*1024, &l, NULL);
  1030. ReadFile(hfile, HMapO, 1024*1024, &l, NULL);
  1031. ReadFile(hfile, FogsMap, 512*512, &l, NULL);
  1032. ReadFile(hfile, AmbMap, 512*512, &l, NULL);
  1033. if (FogsList[1].YBegin>1.f)
  1034. for (int x=0; x<510; x++)
  1035. for (int y=0; y<510; y++)
  1036. if (!FogsMap[y][x])
  1037. if (HMap[y*2+0][x*2+0]<FogsList[1].YBegin || HMap[y*2+0][x*2+1]<FogsList[1].YBegin || HMap[y*2+0][x*2+2] < FogsList[1].YBegin ||
  1038. HMap[y*2+1][x*2+0]<FogsList[1].YBegin || HMap[y*2+1][x*2+1]<FogsList[1].YBegin || HMap[y*2+1][x*2+2] < FogsList[1].YBegin ||
  1039. HMap[y*2+2][x*2+0]<FogsList[1].YBegin || HMap[y*2+2][x*2+1]<FogsList[1].YBegin || HMap[y*2+2][x*2+2] < FogsList[1].YBegin)
  1040. FogsMap[y][x] = 1;
  1041. CloseHandle(hfile);
  1042. PrintLog(" Done.\n");
  1043. //======= Post load rendering ==============//
  1044. PrintLoad("Prepearing maps...");
  1045. CreateTMap();
  1046. RenderLightMap();
  1047. LoadPictureTGA(MapPic, "HUNTDAT\\MENU\\mapframe.tga");
  1048. conv_pic(MapPic);
  1049. GenerateMapImage();
  1050. if (TrophyMode) LoadPictureTGA(TrophyPic, "HUNTDAT\\MENU\\trophy.tga");
  1051. else LoadPictureTGA(TrophyPic, "HUNTDAT\\MENU\\trophy_g.tga");
  1052. conv_pic(TrophyPic);
  1053. // ReInitGame();
  1054. }
  1055. void LoadCharacters()
  1056. {
  1057. BOOL pres[64];
  1058. FillMemory(pres, sizeof(pres), 0);
  1059. pres[0]=TRUE;
  1060. for (int c=0; c<ChCount; c++) {
  1061. pres[Characters[c].CType] = TRUE;
  1062. }
  1063. for (c=0; c<TotalC; c++) if (pres[c]) {
  1064. if (!ChInfo[c].mptr) {
  1065. wsprintf(logt, "HUNTDAT\\%s", DinoInfo[c].FName);
  1066. LoadCharacterInfo(ChInfo[c], logt);
  1067. PrintLog("Loading: "); PrintLog(logt); PrintLog("\n");
  1068. }
  1069. }
  1070. for (c=10; c<20; c++)
  1071. if (TargetDino & (1<<c))
  1072. if (!DinoInfo[AI_to_CIndex[c]].CallIcon.lpImage) {
  1073. wsprintf(logt, "HUNTDAT\\MENU\\PICS\\call%d.tga", c-9);
  1074. LoadPictureTGA(DinoInfo[AI_to_CIndex[c]].CallIcon, logt);
  1075. conv_pic(DinoInfo[AI_to_CIndex[c]].CallIcon);
  1076. }
  1077. for (c=0; c<TotalW; c++)
  1078. if (WeaponPres & (1<<c)) {
  1079. if (!Weapon.chinfo[c].mptr) {
  1080. wsprintf(logt, "HUNTDAT\\WEAPONS\\%s", WeapInfo[c].FName);
  1081. LoadCharacterInfo(Weapon.chinfo[c], logt);
  1082. PrintLog("Loading: "); PrintLog(logt); PrintLog("\n");
  1083. }
  1084. if (!Weapon.BulletPic[c].lpImage) {
  1085. wsprintf(logt, "HUNTDAT\\WEAPONS\\%s", WeapInfo[c].BFName);
  1086. LoadPictureTGA(Weapon.BulletPic[c], logt);
  1087. conv_pic(Weapon.BulletPic[c]);
  1088. PrintLog("Loading: "); PrintLog(logt); PrintLog("\n");
  1089. }
  1090. }
  1091. for (c=10; c<20; c++)
  1092. if (TargetDino & (1<<c))
  1093. if (!fxCall[c-10][0].lpData) {
  1094. wsprintf(logt,"HUNTDAT\\SOUNDFX\\CALLS\\call%d_a.wav", (c-9));
  1095. LoadWav(logt, fxCall[c-10][0]);
  1096. wsprintf(logt,"HUNTDAT\\SOUNDFX\\CALLS\\call%d_b.wav", (c-9));
  1097. LoadWav(logt, fxCall[c-10][1]);
  1098. wsprintf(logt,"HUNTDAT\\SOUNDFX\\CALLS\\call%d_c.wav", (c-9));
  1099. LoadWav(logt, fxCall[c-10][2]);
  1100. }
  1101. }
  1102. void ReInitGame()
  1103. {
  1104. PrintLog("ReInitGame();\n");
  1105. PlaceHunter();
  1106. if (TrophyMode) PlaceTrophy();
  1107. else PlaceCharacters();
  1108. LoadCharacters();
  1109. LockLanding = FALSE;
  1110. Wind.alpha = rRand(1024) * 2.f * pi / 1024.f;
  1111. Wind.speed = 10;
  1112. MyHealth = MAX_HEALTH;
  1113. TargetWeapon = -1;
  1114. for (int w=0; w<TotalW; w++)
  1115. if ( WeaponPres & (1<<w) ) {
  1116. ShotsLeft[w] = WeapInfo[w].Shots;
  1117. if (DoubleAmmo) AmmoMag[w] = 1;
  1118. if (TargetWeapon==-1) TargetWeapon=w;
  1119. }
  1120. CurrentWeapon = TargetWeapon;
  1121. Weapon.state = 0;
  1122. Weapon.FTime = 0;
  1123. PlayerAlpha = 0;
  1124. PlayerBeta = 0;
  1125. WCCount = 0;
  1126. SnCount = 0;
  1127. ElCount = 0;
  1128. BloodTrail.Count = 0;
  1129. BINMODE = FALSE;
  1130. OPTICMODE = FALSE;
  1131. EXITMODE = FALSE;
  1132. PAUSE = FALSE;
  1133. Ship.pos.x = PlayerX;
  1134. Ship.pos.z = PlayerZ;
  1135. Ship.pos.y = GetLandUpH(Ship.pos.x, Ship.pos.z) + 2048;
  1136. Ship.State = -1;
  1137. Ship.tgpos.x = Ship.pos.x;
  1138. Ship.tgpos.z = Ship.pos.z + 60*256;
  1139. Ship.cindex = -1;
  1140. Ship.tgpos.y = GetLandUpH(Ship.tgpos.x, Ship.tgpos.z) + 2048;
  1141. ShipTask.tcount = 0;
  1142. if (!TrophyMode) {
  1143. TrophyRoom.Last.smade = 0;
  1144. TrophyRoom.Last.success = 0;
  1145. TrophyRoom.Last.path = 0;
  1146. TrophyRoom.Last.time = 0;
  1147. }
  1148. DemoPoint.DemoTime = 0;
  1149. RestartMode = FALSE;
  1150. TrophyTime=0;
  1151. answtime = 0;
  1152. ExitTime = 0;
  1153. }
  1154. void ReleaseCharacterInfo(TCharacterInfo &chinfo)
  1155. {
  1156. if (!chinfo.mptr) return;
  1157. _HeapFree(Heap, 0, chinfo.mptr);
  1158. chinfo.mptr = NULL;
  1159. for (int c = 0; c<64; c++) {
  1160. if (!chinfo.Animation[c].aniData) break;
  1161. _HeapFree(Heap, 0, chinfo.Animation[c].aniData);
  1162. chinfo.Animation[c].aniData = NULL;
  1163. }
  1164. for (c = 0; c<64; c++) {
  1165. if (!chinfo.SoundFX[c].lpData) break;
  1166. _HeapFree(Heap, 0, chinfo.SoundFX[c].lpData);
  1167. chinfo.SoundFX[c].lpData = NULL;
  1168. }
  1169. chinfo.AniCount = 0;
  1170. chinfo.SfxCount = 0;
  1171. }
  1172. void LoadCharacterInfo(TCharacterInfo &chinfo, char* FName)
  1173. {
  1174. ReleaseCharacterInfo(chinfo);
  1175. HANDLE hfile = CreateFile(FName,
  1176. GENERIC_READ, FILE_SHARE_READ,
  1177. NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
  1178. if (hfile==INVALID_HANDLE_VALUE) {
  1179. char sz[512];
  1180. wsprintf( sz, "Error opening character file:\n%s.", FName );
  1181. DoHalt(sz);
  1182. }
  1183. ReadFile(hfile, chinfo.ModelName, 32, &l, NULL);
  1184. ReadFile(hfile, &chinfo.AniCount, 4, &l, NULL);
  1185. ReadFile(hfile, &chinfo.SfxCount, 4, &l, NULL);
  1186. //============= read model =================//
  1187. chinfo.mptr = (TModel*) _HeapAlloc(Heap, 0, sizeof(TModel));
  1188. ReadFile( hfile, &chinfo.mptr->VCount, 4, &l, NULL );
  1189. ReadFile( hfile, &chinfo.mptr->FCount, 4, &l, NULL );
  1190. ReadFile( hfile, &chinfo.mptr->TextureSize, 4, &l, NULL );
  1191. ReadFile( hfile, chinfo.mptr->gFace, chinfo.mptr->FCount<<6, &l, NULL );
  1192. ReadFile( hfile, chinfo.mptr->gVertex, chinfo.mptr->VCount<<4, &l, NULL );
  1193. int ts = chinfo.mptr->TextureSize;
  1194. if (HARD3D) chinfo.mptr->TextureHeight = 256;
  1195. else chinfo.mptr->TextureHeight = chinfo.mptr->TextureSize>>9;
  1196. chinfo.mptr->TextureSize = chinfo.mptr->TextureHeight*512;
  1197. chinfo.mptr->lpTexture = (WORD*) _HeapAlloc(Heap, 0, chinfo.mptr->TextureSize);
  1198. ReadFile(hfile, chinfo.mptr->lpTexture, ts, &l, NULL);
  1199. BrightenTexture(chinfo.mptr->lpTexture, ts/2);
  1200. DATASHIFT(chinfo.mptr->lpTexture, chinfo.mptr->TextureSize);
  1201. GenerateModelMipMaps(chinfo.mptr);
  1202. GenerateAlphaFlags(chinfo.mptr);
  1203. //CalcLights(chinfo.mptr);
  1204. //ApplyAlphaFlags(chinfo.mptr->lpTexture, 256*256);
  1205. //ApplyAlphaFlags(chinfo.mptr->lpTexture2, 128*128);
  1206. //============= read animations =============//
  1207. for (int a=0; a<chinfo.AniCount; a++) {
  1208. ReadFile(hfile, chinfo.Animation[a].aniName, 32, &l, NULL);
  1209. ReadFile(hfile, &chinfo.Animation[a].aniKPS, 4, &l, NULL);
  1210. ReadFile(hfile, &chinfo.Animation[a].FramesCount, 4, &l, NULL);
  1211. chinfo.Animation[a].AniTime = (chinfo.Animation[a].FramesCount * 1000) / chinfo.Animation[a].aniKPS;
  1212. chinfo.Animation[a].aniData = (short int*)
  1213. _HeapAlloc(Heap, 0, (chinfo.mptr->VCount*chinfo.Animation[a].FramesCount*6) );
  1214. ReadFile(hfile, chinfo.Animation[a].aniData, (chinfo.mptr->VCount*chinfo.Animation[a].FramesCount*6), &l, NULL);
  1215. }
  1216. //============= read sound fx ==============//
  1217. BYTE tmp[32];
  1218. for (int s=0; s<chinfo.SfxCount; s++) {
  1219. ReadFile(hfile, tmp, 32, &l, NULL);
  1220. ReadFile(hfile, &chinfo.SoundFX[s].length, 4, &l, NULL);
  1221. chinfo.SoundFX[s].lpData = (short int*) _HeapAlloc(Heap, 0, chinfo.SoundFX[s].length);
  1222. ReadFile(hfile, chinfo.SoundFX[s].lpData, chinfo.SoundFX[s].length, &l, NULL);
  1223. }
  1224. for (int v=0; v<chinfo.mptr->VCount; v++) {
  1225. chinfo.mptr->gVertex[v].x*=2.f;
  1226. chinfo.mptr->gVertex[v].y*=2.f;
  1227. chinfo.mptr->gVertex[v].z*=-2.f;
  1228. }
  1229. CorrectModel(chinfo.mptr);
  1230. ReadFile(hfile, chinfo.Anifx, 64*4, &l, NULL);
  1231. if (l!=256)
  1232. for (l=0; l<64; l++) chinfo.Anifx[l] = -1;
  1233. CloseHandle(hfile);
  1234. }
  1235. //================ light map ========================//
  1236. void FillVector(int x, int y, Vector3d& v)
  1237. {
  1238. v.x = (float)x*256;
  1239. v.z = (float)y*256;
  1240. v.y = (float)((int)HMap[y][x])*ctHScale;
  1241. }
  1242. BOOL TraceVector(Vector3d v, Vector3d lv)
  1243. {
  1244. v.y+=4;
  1245. NormVector(lv,64);
  1246. for (int l=0; l<32; l++) {
  1247. v.x-=lv.x; v.y-=lv.y/6; v.z-=lv.z;
  1248. if (v.y>255 * ctHScale) return TRUE;
  1249. if (GetLandH(v.x, v.z) > v.y) return FALSE;
  1250. }
  1251. return TRUE;
  1252. }
  1253. void AddShadow(int x, int y, int d)
  1254. {
  1255. if (x<0 || y<0 || x>1023 || y>1023) return;
  1256. int l = LMap[y][x];
  1257. l-=d;
  1258. if (l<32) l=32;
  1259. LMap[y][x]=l;
  1260. }
  1261. void RenderShadowCircle(int x, int y, int R, int D)
  1262. {
  1263. int cx = x / 256;
  1264. int cy = y / 256;
  1265. int cr = 1 + R / 256;
  1266. for (int yy=-cr; yy<=cr; yy++)
  1267. for (int xx=-cr; xx<=cr; xx++) {
  1268. int tx = (cx+xx)*256;
  1269. int ty = (cy+yy)*256;
  1270. int r = (int)sqrt( (tx-x)*(tx-x) + (ty-y)*(ty-y) );
  1271. if (r>R) continue;
  1272. AddShadow(cx+xx, cy+yy, D * (R-r) / R);
  1273. }
  1274. }
  1275. void RenderLightMap()
  1276. {
  1277. Vector3d lv;
  1278. int x,y;
  1279. lv.x = - 412;
  1280. lv.z = - 412;
  1281. lv.y = - 1024;
  1282. NormVector(lv, 1.0f);
  1283. for (y=1; y<ctMapSize-1; y++)
  1284. for (x=1; x<ctMapSize-1; x++) {
  1285. int ob = OMap[y][x];
  1286. if (ob == 255) continue;
  1287. int l = MObjects[ob].info.linelenght / 128;
  1288. int s = 1;
  1289. if (OptDayNight==2) s=-1;
  1290. if (OptDayNight!=1) l = MObjects[ob].info.linelenght / 70;
  1291. if (l>0) RenderShadowCircle(x*256+128,y*256+128, 256, MObjects[ob].info.lintensity * 2);
  1292. for (int i=1; i<l; i++)
  1293. AddShadow(x+i*s, y+i*s, MObjects[ob].info.lintensity);
  1294. l = MObjects[ob].info.linelenght * 2;
  1295. RenderShadowCircle(x*256+128+l*s,y*256+128+l*s,
  1296. MObjects[ob].info.circlerad*2,
  1297. MObjects[ob].info.cintensity*4);
  1298. }
  1299. }
  1300. void SaveScreenShot()
  1301. {
  1302. HANDLE hf; /* file handle */
  1303. BITMAPFILEHEADER hdr; /* bitmap file-header */
  1304. BITMAPINFOHEADER bmi; /* bitmap info-header */
  1305. DWORD dwTmp;
  1306. if (WinW>1024) return;
  1307. //MessageBeep(0xFFFFFFFF);
  1308. CopyHARDToDIB();
  1309. bmi.biSize = sizeof(BITMAPINFOHEADER);
  1310. bmi.biWidth = WinW;
  1311. bmi.biHeight = WinH;
  1312. bmi.biPlanes = 1;
  1313. bmi.biBitCount = 24;
  1314. bmi.biCompression = BI_RGB;
  1315. bmi.biSizeImage = WinW*WinH*3;
  1316. bmi.biClrImportant = 0;
  1317. bmi.biClrUsed = 0;
  1318. hdr.bfType = 0x4d42;
  1319. hdr.bfSize = (DWORD) (sizeof(BITMAPFILEHEADER) +
  1320. bmi.biSize + bmi.biSizeImage);
  1321. hdr.bfReserved1 = 0;
  1322. hdr.bfReserved2 = 0;
  1323. hdr.bfOffBits = (DWORD) sizeof(BITMAPFILEHEADER) +
  1324. bmi.biSize;
  1325. char t[12];
  1326. wsprintf(t,"HUNT%004d.BMP",++_shotcounter);
  1327. hf = CreateFile(t,
  1328. GENERIC_READ | GENERIC_WRITE,
  1329. (DWORD) 0,
  1330. (LPSECURITY_ATTRIBUTES) NULL,
  1331. CREATE_ALWAYS,
  1332. FILE_ATTRIBUTE_NORMAL,
  1333. (HANDLE) NULL);
  1334. WriteFile(hf, (LPVOID) &hdr, sizeof(BITMAPFILEHEADER), (LPDWORD) &dwTmp, (LPOVERLAPPED) NULL);
  1335. WriteFile(hf, &bmi, sizeof(BITMAPINFOHEADER), (LPDWORD) &dwTmp, (LPOVERLAPPED) NULL);
  1336. byte fRGB[1024][3];
  1337. for (int y=0; y<WinH; y++) {
  1338. for (int x=0; x<WinW; x++) {
  1339. WORD C = *((WORD*)lpVideoBuf + (WinEY-y)*1024+x);
  1340. fRGB[x][0] = (C & 31)<<3;
  1341. if (HARD3D) {
  1342. fRGB[x][1] = ((C>> 5) & 63)<<2;
  1343. fRGB[x][2] = ((C>>11) & 31)<<3;
  1344. } else {
  1345. fRGB[x][1] = ((C>> 5) & 31)<<3;
  1346. fRGB[x][2] = ((C>>10) & 31)<<3;
  1347. }
  1348. }
  1349. WriteFile( hf, fRGB, 3*WinW, &dwTmp, NULL );
  1350. }
  1351. CloseHandle(hf);
  1352. //MessageBeep(0xFFFFFFFF);
  1353. }
  1354. //===============================================================================================
  1355. void ReadWeapons(FILE *stream)
  1356. {
  1357. TotalW = 0;
  1358. char line[256], *value;
  1359. while (fgets( line, 255, stream))
  1360. {
  1361. if (strstr(line, "}")) break;
  1362. if (strstr(line, "{"))
  1363. while (fgets( line, 255, stream)) {
  1364. if (strstr(line, "}")) { TotalW++; break; }
  1365. value = strstr(line, "=");
  1366. if (!value) DoHalt("Script loading error");
  1367. value++;
  1368. if (strstr(line, "power")) WeapInfo[TotalW].Power = (float)atof(value);
  1369. if (strstr(line, "prec")) WeapInfo[TotalW].Prec = (float)atof(value);
  1370. if (strstr(line, "loud")) WeapInfo[TotalW].Loud = (float)atof(value);
  1371. if (strstr(line, "rate")) WeapInfo[TotalW].Rate = (float)atof(value);
  1372. if (strstr(line, "shots")) WeapInfo[TotalW].Shots = atoi(value);
  1373. if (strstr(line, "reload")) WeapInfo[TotalW].Reload= atoi(value);
  1374. if (strstr(line, "trace")) WeapInfo[TotalW].TraceC= atoi(value)-1;
  1375. if (strstr(line, "optic")) WeapInfo[TotalW].Optic = atoi(value);
  1376. if (strstr(line, "fall")) WeapInfo[TotalW].Fall = atoi(value);
  1377. //if (strstr(line, "price")) WeapInfo[TotalW].Price = atoi(value);
  1378. if (strstr(line, "name")) {
  1379. value = strstr(line, "'"); if (!value) DoHalt("Script loading error");
  1380. value[strlen(value)-2] = 0;
  1381. strcpy(WeapInfo[TotalW].Name, &value[1]); }
  1382. if (strstr(line, "file")) {
  1383. value = strstr(line, "'"); if (!value) DoHalt("Script loading error");
  1384. value[strlen(value)-2] = 0;
  1385. strcpy(WeapInfo[TotalW].FName, &value[1]);}
  1386. if (strstr(line, "pic")) {
  1387. value = strstr(line, "'"); if (!value) DoHalt("Script loading error");
  1388. value[strlen(value)-2] = 0;
  1389. strcpy(WeapInfo[TotalW].BFName, &value[1]);}
  1390. }
  1391. }
  1392. }
  1393. void ReadCharacters(FILE *stream)
  1394. {
  1395. TotalC = 0;
  1396. char line[256], *value;
  1397. while (fgets( line, 255, stream))
  1398. {
  1399. if (strstr(line, "}")) break;
  1400. if (strstr(line, "{"))
  1401. while (fgets( line, 255, stream)) {
  1402. if (strstr(line, "}")) {
  1403. AI_to_CIndex[DinoInfo[TotalC].AI] = TotalC;
  1404. TotalC++;
  1405. break;
  1406. }
  1407. value = strstr(line, "=");
  1408. if (!value)
  1409. DoHalt("Script loading error");
  1410. value++;
  1411. if (strstr(line, "mass" )) DinoInfo[TotalC].Mass = (float)atof(value);
  1412. if (strstr(line, "length" )) DinoInfo[TotalC].Length = (float)atof(value);
  1413. if (strstr(line, "radius" )) DinoInfo[TotalC].Radius = (float)atof(value);
  1414. if (strstr(line, "health" )) DinoInfo[TotalC].Health0 = atoi(value);
  1415. if (strstr(line, "basescore")) DinoInfo[TotalC].BaseScore = atoi(value);
  1416. if (strstr(line, "ai" )) DinoInfo[TotalC].AI = atoi(value);
  1417. if (strstr(line, "smell" )) DinoInfo[TotalC].SmellK = (float)atof(value);
  1418. if (strstr(line, "hear" )) DinoInfo[TotalC].HearK = (float)atof(value);
  1419. if (strstr(line, "look" )) DinoInfo[TotalC].LookK = (float)atof(value);
  1420. if (strstr(line, "shipdelta")) DinoInfo[TotalC].ShDelta = (float)atof(value);
  1421. if (strstr(line, "scale0" )) DinoInfo[TotalC].Scale0 = atoi(value);
  1422. if (strstr(line, "scaleA" )) DinoInfo[TotalC].ScaleA = atoi(value);
  1423. if (strstr(line, "danger" )) DinoInfo[TotalC].DangerCall= TRUE;
  1424. if (strstr(line, "name")) {
  1425. value = strstr(line, "'"); if (!value) DoHalt("Script loading error");
  1426. value[strlen(value)-2] = 0;
  1427. strcpy(DinoInfo[TotalC].Name, &value[1]); }
  1428. if (strstr(line, "file")) {
  1429. value = strstr(line, "'"); if (!value) DoHalt("Script loading error");
  1430. value[strlen(value)-2] = 0;
  1431. strcpy(DinoInfo[TotalC].FName, &value[1]);}
  1432. if (strstr(line, "pic")) {
  1433. value = strstr(line, "'"); if (!value) DoHalt("Script loading error");
  1434. value[strlen(value)-2] = 0;
  1435. strcpy(DinoInfo[TotalC].PName, &value[1]);}
  1436. }
  1437. }
  1438. }
  1439. void LoadResourcesScript()
  1440. {
  1441. FILE *stream;
  1442. char line[256];
  1443. stream = fopen("HUNTDAT\\_res.txt", "r");
  1444. if (!stream) DoHalt("Can't open resources file _res.txt");
  1445. while (fgets( line, 255, stream)) {
  1446. if (line[0] == '.') break;
  1447. if (strstr(line, "weapons") ) ReadWeapons(stream);
  1448. if (strstr(line, "characters") ) ReadCharacters(stream);
  1449. }
  1450. fclose (stream);
  1451. }
  1452. //===============================================================================================
  1453. void CreateLog()
  1454. {
  1455. hlog = CreateFile("render.log",
  1456. GENERIC_WRITE,
  1457. FILE_SHARE_READ, NULL,
  1458. CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
  1459. #ifdef _d3d
  1460. PrintLog("Carnivores Ice Age D3D video driver.");
  1461. #endif
  1462. #ifdef _3dfx
  1463. PrintLog("Carnivores Ice Age 3DFX video driver.");
  1464. #endif
  1465. #ifdef _soft
  1466. PrintLog("Carnivores Ice Age Soft video driver.");
  1467. #endif
  1468. PrintLog(" Build v2.04. Sep.24 1999.\n");
  1469. }
  1470. void PrintLog(LPSTR l)
  1471. {
  1472. DWORD w;
  1473. if (l[strlen(l)-1]==0x0A) {
  1474. BYTE b = 0x0D;
  1475. WriteFile(hlog, l, strlen(l)-1, &w, NULL);
  1476. WriteFile(hlog, &b, 1, &w, NULL);
  1477. b = 0x0A;
  1478. WriteFile(hlog, &b, 1, &w, NULL);
  1479. } else
  1480. WriteFile(hlog, l, strlen(l), &w, NULL);
  1481. }
  1482. void CloseLog()
  1483. {
  1484. CloseHandle(hlog);
  1485. }