Resources.cpp 52 KB

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