split out RecursiveHullCheck and related code into collision.c (shared by client...
[xonotic/darkplaces.git] / r_explosion.c
1 /*
2 Copyright (C) 1996-1997 Id Software, Inc.
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
13 See the GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18
19 */
20
21 #include "quakedef.h"
22 #include "cl_collision.h"
23
24 #define MAX_EXPLOSIONS 64
25 #define EXPLOSIONGRID 8
26 #define EXPLOSIONVERTS ((EXPLOSIONGRID+1)*(EXPLOSIONGRID+1))
27 #define EXPLOSIONTRIS (EXPLOSIONGRID*EXPLOSIONGRID*2)
28 #define EXPLOSIONSTARTVELOCITY (256.0f)
29 //#define EXPLOSIONSTARTVELOCITY (384.0f)
30 //#define EXPLOSIONRANDOMVELOCITY (32.0f)
31 #define EXPLOSIONFADESTART (1.5f)
32 //#define EXPLOSIONFADERATE (4.5f)
33 #define EXPLOSIONFADERATE (3.0f)
34 /*
35 #define MAX_EXPLOSIONGAS (MAX_EXPLOSIONS * EXPLOSIONGAS)
36 #define EXPLOSIONGAS 8
37 #define EXPLOSIONGASSTARTRADIUS (15.0f)
38 #define EXPLOSIONGASSTARTVELOCITY (50.0f)
39 #define GASDENSITY_SCALER (32768.0f / EXPLOSIONGAS)
40 #define GASFADERATE (GASDENSITY_SCALER * EXPLOSIONGAS * 2)
41
42 typedef struct explosiongas_s
43 {
44         float pressure;
45         vec3_t origin;
46         vec3_t velocity;
47 }
48 explosiongas_t;
49
50 explosiongas_t explosiongas[MAX_EXPLOSIONGAS];
51 */
52
53 float explosiontexcoords[EXPLOSIONVERTS][2];
54 int explosiontris[EXPLOSIONTRIS][3];
55 int explosionnoiseindex[EXPLOSIONVERTS];
56 vec3_t explosionpoint[EXPLOSIONVERTS];
57 vec3_t explosionspherevertvel[EXPLOSIONVERTS];
58
59 typedef struct explosion_s
60 {
61         float starttime;
62         float time;
63         float alpha;
64         vec3_t origin;
65         vec3_t vert[EXPLOSIONVERTS];
66         vec3_t vertvel[EXPLOSIONVERTS];
67 }
68 explosion_t;
69
70 explosion_t explosion[MAX_EXPLOSIONS];
71
72 rtexture_t      *explosiontexture;
73 rtexture_t      *explosiontexturefog;
74
75 rtexturepool_t  *explosiontexturepool;
76
77 cvar_t r_explosionclip = {CVAR_SAVE, "r_explosionclip", "1"};
78 cvar_t r_drawexplosions = {0, "r_drawexplosions", "1"};
79
80 void r_explosion_start(void)
81 {
82         int x, y;
83         qbyte noise1[128][128], noise2[128][128], noise3[128][128], data[128][128][4];
84         explosiontexturepool = R_AllocTexturePool();
85         fractalnoise(&noise1[0][0], 128, 32);
86         fractalnoise(&noise2[0][0], 128, 4);
87         fractalnoise(&noise3[0][0], 128, 4);
88         for (y = 0;y < 128;y++)
89         {
90                 for (x = 0;x < 128;x++)
91                 {
92                         int j, r, g, b, a;
93                         j = (noise1[y][x] * noise2[y][x]) * 3 / 256 - 128;
94                         r = (j * 512) / 256;
95                         g = (j * 256) / 256;
96                         b = (j * 128) / 256;
97                         a = noise3[y][x] * 3 - 128;
98                         data[y][x][0] = bound(0, r, 255);
99                         data[y][x][1] = bound(0, g, 255);
100                         data[y][x][2] = bound(0, b, 255);
101                         data[y][x][3] = bound(0, a, 255);
102                 }
103         }
104         explosiontexture = R_LoadTexture (explosiontexturepool, "explosiontexture", 128, 128, &data[0][0][0], TEXTYPE_RGBA, TEXF_MIPMAP | TEXF_ALPHA | TEXF_PRECACHE);
105         for (y = 0;y < 128;y++)
106                 for (x = 0;x < 128;x++)
107                         data[y][x][0] = data[y][x][1] = data[y][x][2] = 255;
108         explosiontexturefog = R_LoadTexture (explosiontexturepool, "explosiontexturefog", 128, 128, &data[0][0][0], TEXTYPE_RGBA, TEXF_MIPMAP | TEXF_ALPHA | TEXF_PRECACHE);
109         // note that explosions survive the restart
110 }
111
112 void r_explosion_shutdown(void)
113 {
114         R_FreeTexturePool(&explosiontexturepool);
115 }
116
117 void r_explosion_newmap(void)
118 {
119         memset(explosion, 0, sizeof(explosion));
120 //      memset(explosiongas, 0, sizeof(explosiongas));
121 }
122
123 int R_ExplosionVert(int column, int row)
124 {
125         int i;
126         float a, b, c;
127         i = row * (EXPLOSIONGRID + 1) + column;
128         a = row * M_PI * 2 / EXPLOSIONGRID;
129         b = column * M_PI * 2 / EXPLOSIONGRID;
130         c = cos(b);
131         explosionpoint[i][0] = cos(a) * c;
132         explosionpoint[i][1] = sin(a) * c;
133         explosionpoint[i][2] = -sin(b);
134         explosionspherevertvel[i][0] = explosionpoint[i][0] * EXPLOSIONSTARTVELOCITY;
135         explosionspherevertvel[i][1] = explosionpoint[i][1] * EXPLOSIONSTARTVELOCITY;
136         explosionspherevertvel[i][2] = explosionpoint[i][2] * EXPLOSIONSTARTVELOCITY;
137         explosiontexcoords[i][0] = (float) column / (float) EXPLOSIONGRID;
138         explosiontexcoords[i][1] = (float) row / (float) EXPLOSIONGRID;
139         // top and bottom rows are all one position...
140         if (row == 0 || row == EXPLOSIONGRID)
141                 column = 0;
142         explosionnoiseindex[i] = (row % EXPLOSIONGRID) * EXPLOSIONGRID + (column % EXPLOSIONGRID);
143         return i;
144 }
145
146 void R_Explosion_Init(void)
147 {
148         int i, x, y;
149         i = 0;
150         for (y = 0;y < EXPLOSIONGRID;y++)
151         {
152                 for (x = 0;x < EXPLOSIONGRID;x++)
153                 {
154                         explosiontris[i][0] = R_ExplosionVert(x    , y    );
155                         explosiontris[i][1] = R_ExplosionVert(x + 1, y    );
156                         explosiontris[i][2] = R_ExplosionVert(x    , y + 1);
157                         i++;
158                         explosiontris[i][0] = R_ExplosionVert(x + 1, y    );
159                         explosiontris[i][1] = R_ExplosionVert(x + 1, y + 1);
160                         explosiontris[i][2] = R_ExplosionVert(x    , y + 1);
161                         i++;
162                 }
163         }
164
165         Cvar_RegisterVariable(&r_explosionclip);
166         Cvar_RegisterVariable(&r_drawexplosions);
167
168         R_RegisterModule("R_Explosions", r_explosion_start, r_explosion_shutdown, r_explosion_newmap);
169 }
170
171 void R_NewExplosion(vec3_t org)
172 {
173         int i, j;
174         float dist;
175         qbyte noise[EXPLOSIONGRID*EXPLOSIONGRID];
176         fractalnoisequick(noise, EXPLOSIONGRID, 4); // adjust noise grid size according to explosion
177         for (i = 0;i < MAX_EXPLOSIONS;i++)
178         {
179                 if (explosion[i].alpha <= 0.01f)
180                 {
181                         explosion[i].starttime = cl.time;
182                         explosion[i].time = explosion[i].starttime - 0.1;
183                         explosion[i].alpha = EXPLOSIONFADESTART;
184                         VectorCopy(org, explosion[i].origin);
185                         for (j = 0;j < EXPLOSIONVERTS;j++)
186                         {
187                                 // calculate start
188                                 VectorCopy(explosion[i].origin, explosion[i].vert[j]);
189                                 // calculate velocity
190                                 dist = noise[explosionnoiseindex[j]] * (1.0f / 255.0f) + 0.5;
191                                 VectorScale(explosionspherevertvel[j], dist, explosion[i].vertvel[j]);
192                                 //explosion[i].vertvel[j][0] = explosionspherevertvel[j][0] * dist; + (((float) noise[0][explosionnoiseindex[j]] - 128.0f) * (EXPLOSIONRANDOMVELOCITY / 128.0f));
193                                 //explosion[i].vertvel[j][1] = explosionspherevertvel[j][1] * dist; + (((float) noise[1][explosionnoiseindex[j]] - 128.0f) * (EXPLOSIONRANDOMVELOCITY / 128.0f));
194                                 //explosion[i].vertvel[j][2] = explosionspherevertvel[j][2] * dist; + (((float) noise[2][explosionnoiseindex[j]] - 128.0f) * (EXPLOSIONRANDOMVELOCITY / 128.0f));
195                         }
196                         break;
197                 }
198         }
199
200         /*
201         i = 0;
202         j = EXPLOSIONGAS;
203         while (i < MAX_EXPLOSIONGAS && j > 0)
204         {
205                 while (explosiongas[i].pressure > 0)
206                 {
207                         i++;
208                         if (i >= MAX_EXPLOSIONGAS)
209                                 return;
210                 }
211                 VectorRandom(v);
212                 VectorMA(org, EXPLOSIONGASSTARTRADIUS, v, v);
213                 TraceLine(org, v, explosiongas[i].origin, NULL, 0, true);
214                 VectorRandom(v);
215                 VectorScale(v, EXPLOSIONGASSTARTVELOCITY, explosiongas[i].velocity);
216                 explosiongas[i].pressure = j * GASDENSITY_SCALER;
217                 j--;
218         }
219         */
220 }
221
222 void R_DrawExplosion(explosion_t *e)
223 {
224         int i;
225         float c[EXPLOSIONVERTS][4], diff[3], centerdir[3], /*fog, */ifog, alpha, dist/*, centerdist, size, scale*/;
226         rmeshinfo_t m;
227         memset(&m, 0, sizeof(m));
228         m.transparent = true;
229         m.blendfunc1 = GL_SRC_ALPHA;
230         m.blendfunc2 = GL_ONE; //_MINUS_SRC_ALPHA;
231         m.numtriangles = EXPLOSIONTRIS;
232         m.index = &explosiontris[0][0];
233         m.numverts = EXPLOSIONVERTS;
234         m.vertex = &e->vert[0][0];
235         m.vertexstep = sizeof(float[3]);
236         alpha = e->alpha;
237         //if (alpha > 1)
238         //      alpha = 1;
239         m.cr = 1;
240         m.cg = 1;
241         m.cb = 1;
242         m.ca = 1; //alpha;
243         m.color = &c[0][0];
244         m.colorstep = sizeof(float[4]);
245         VectorSubtract(r_origin, e->origin, centerdir);
246         VectorNormalizeFast(centerdir);
247         /*
248         centerdist = DotProduct(e->origin, centerdir);
249         size = 0;
250         for (i = 0;i < EXPLOSIONVERTS;i++)
251         {
252                 dist = DotProduct(e->vert[i], centerdir) - centerdist;
253                 if (size < dist)
254                         size = dist;
255         }
256         scale = 4.0f / size;
257         */
258         if (fogenabled)
259         {
260                 for (i = 0;i < EXPLOSIONVERTS;i++)
261                 {
262                         //dist = (DotProduct(e->vert[i], centerdir) - centerdist) * scale - 2.0f;
263                         VectorSubtract(e->vert[i], e->origin, diff);
264                         VectorNormalizeFast(diff);
265                         dist = (DotProduct(diff, centerdir) * 6.0f - 4.0f) * alpha;
266                         if (dist > 0)
267                         {
268                                 // use inverse fog alpha
269                                 VectorSubtract(e->vert[i], r_origin, diff);
270                                 ifog = 1 - exp(fogdensity/DotProduct(diff,diff));
271                                 dist = dist * ifog;
272                                 if (dist < 0)
273                                         dist = 0;
274                         }
275                         else
276                                 dist = 0;
277                         c[i][0] = c[i][1] = c[i][2] = dist;
278                         c[i][3] = 1;
279                 }
280         }
281         else
282         {
283                 for (i = 0;i < EXPLOSIONVERTS;i++)
284                 {
285                         //dist = (DotProduct(e->vert[i], centerdir) - centerdist) * scale - 2.0f;
286                         VectorSubtract(e->vert[i], e->origin, diff);
287                         VectorNormalizeFast(diff);
288                         dist = (DotProduct(diff, centerdir) * 6.0f - 4.0f) * alpha;
289                         if (dist < 0)
290                                 dist = 0;
291                         c[i][0] = c[i][1] = c[i][2] = dist;
292                         c[i][3] = 1;
293                 }
294         }
295         /*
296         if (fogenabled)
297         {
298                 m.color = &c[0][0];
299                 m.colorstep = sizeof(float[4]);
300                 for (i = 0;i < EXPLOSIONVERTS;i++)
301                 {
302                         // use inverse fog alpha as color
303                         VectorSubtract(e->vert[i], r_origin, diff);
304                         ifog = 1 - exp(fogdensity/DotProduct(diff,diff));
305                         if (ifog < 0)
306                                 ifog = 0;
307                         c[i][0] = ifog;
308                         c[i][1] = ifog;
309                         c[i][2] = ifog;
310                         c[i][3] = alpha;
311                 }
312         }
313         */
314         m.tex[0] = R_GetTexture(explosiontexture);
315         m.texcoords[0] = &explosiontexcoords[0][0];
316         m.texcoordstep[0] = sizeof(float[2]);
317
318         R_Mesh_Draw(&m);
319
320         /*
321         if (fogenabled)
322         {
323                 m.blendfunc1 = GL_SRC_ALPHA;
324                 m.blendfunc2 = GL_ONE;
325                 for (i = 0;i < EXPLOSIONVERTS;i++)
326                 {
327                         VectorSubtract(e->vert[i], r_origin, diff);
328                         fog = exp(fogdensity/DotProduct(diff,diff));
329                         c[i][0] = fogcolor[0];
330                         c[i][1] = fogcolor[1];
331                         c[i][2] = fogcolor[2];
332                         c[i][3] = alpha * fog;
333                 }
334                 //m.color = &c[0][0];
335                 //m.colorstep = sizeof(float[4]);
336                 m.tex[0] = R_GetTexture(explosiontexturefog);
337                 R_Mesh_Draw(&m);
338         }
339         */
340 }
341
342 void R_MoveExplosion(explosion_t *e/*, explosiongas_t **list, explosiongas_t **listend, */)
343 {
344         int i;
345         float dot, frictionscale, end[3], impact[3], normal[3], frametime;
346         /*
347         vec3_t diff;
348         vec_t dist;
349         explosiongas_t **l;
350         */
351         frametime = cl.time - e->time;
352         e->time = cl.time;
353         e->alpha = EXPLOSIONFADESTART - (cl.time - e->starttime) * EXPLOSIONFADERATE;
354         if (e->alpha <= 0.01f)
355         {
356                 e->alpha = -1;
357                 return;
358         }
359         frictionscale = 1 - frametime;
360         frictionscale = bound(0, frictionscale, 1);
361         for (i = 0;i < EXPLOSIONVERTS;i++)
362         {
363                 if (e->vertvel[i][0] || e->vertvel[i][1] || e->vertvel[i][2])
364                 {
365                         //e->vertvel[i][2] += sv_gravity.value * frametime * -0.25f;
366                         VectorScale(e->vertvel[i], frictionscale, e->vertvel[i]);
367                         VectorMA(e->vert[i], frametime, e->vertvel[i], end);
368                         if (r_explosionclip.integer)
369                         {
370                                 if (CL_TraceLine(e->vert[i], end, impact, normal, 0, true) < 1)
371                                 {
372                                         // clip velocity against the wall
373                                         dot = DotProduct(e->vertvel[i], normal) * -1.125f;
374                                         VectorMA(e->vertvel[i], dot, normal, e->vertvel[i]);
375                                 }
376                                 VectorCopy(impact, e->vert[i]);
377                         }
378                         else
379                                 VectorCopy(end, e->vert[i]);
380                 }
381                 /*
382                 for (l = list;l < listend;l++)
383                 {
384                         VectorSubtract(e->vert[i], (*l)->origin, diff);
385                         dist = DotProduct(diff, diff);
386                         if (dist < 4096 && dist >= 1)
387                         {
388                                 dist = (*l)->pressure * frametime / dist;
389                                 VectorMA(e->vertvel[i], dist, diff, e->vertvel[i]);
390                         }
391                 }
392                 */
393         }
394         for (i = 0;i < EXPLOSIONGRID;i++)
395                 VectorCopy(e->vert[i * (EXPLOSIONGRID + 1)], e->vert[i * (EXPLOSIONGRID + 1) + EXPLOSIONGRID]);
396         memcpy(e->vert[EXPLOSIONGRID * (EXPLOSIONGRID + 1)], e->vert[0], sizeof(float[3]) * (EXPLOSIONGRID + 1));
397 }
398
399 /*
400 void R_MoveExplosionGas(explosiongas_t *e, explosiongas_t **list, explosiongas_t **listend, float frametime)
401 {
402         vec3_t end, diff;
403         vec_t dist, frictionscale;
404         explosiongas_t **l;
405         frictionscale = 1 - frametime;
406         frictionscale = bound(0, frictionscale, 1);
407         if (e->velocity[0] || e->velocity[1] || e->velocity[2])
408         {
409                 end[0] = e->origin[0] + frametime * e->velocity[0];
410                 end[1] = e->origin[1] + frametime * e->velocity[1];
411                 end[2] = e->origin[2] + frametime * e->velocity[2];
412                 if (r_explosionclip.integer)
413                 {
414                         float f, dot;
415                         vec3_t impact, normal;
416                         f = TraceLine(e->origin, end, impact, normal, 0, true);
417                         VectorCopy(impact, e->origin);
418                         if (f < 1)
419                         {
420                                 // clip velocity against the wall
421                                 dot = DotProduct(e->velocity, normal) * -1.3f;
422                                 e->velocity[0] += normal[0] * dot;
423                                 e->velocity[1] += normal[1] * dot;
424                                 e->velocity[2] += normal[2] * dot;
425                         }
426                 }
427                 else
428                 {
429                         VectorCopy(end, e->origin);
430                 }
431                 e->velocity[2] += sv_gravity.value * frametime;
432                 VectorScale(e->velocity, frictionscale, e->velocity);
433         }
434         for (l = list;l < listend;l++)
435         {
436                 if (*l != e)
437                 {
438                         VectorSubtract(e->origin, (*l)->origin, diff);
439                         dist = DotProduct(diff, diff);
440                         if (dist < 4096 && dist >= 1)
441                         {
442                                 dist = (*l)->pressure * frametime / dist;
443                                 VectorMA(e->velocity, dist, diff, e->velocity);
444                         }
445                 }
446         }
447 }
448 */
449
450 void R_MoveExplosions(void)
451 {
452         int i;
453         float frametime;
454 //      explosiongas_t *gaslist[MAX_EXPLOSIONGAS], **l, **end;
455         frametime = cl.time - cl.oldtime;
456         /*
457         l = &gaslist[0];
458         for (i = 0;i < MAX_EXPLOSIONGAS;i++)
459         {
460                 if (explosiongas[i].pressure > 0)
461                 {
462                         explosiongas[i].pressure -= frametime * GASFADERATE;
463                         if (explosiongas[i].pressure > 0)
464                                 *l++ = &explosiongas[i];
465                 }
466         }
467         end = l;
468         for (l = gaslist;l < end;l++)
469                 R_MoveExplosionGas(*l, gaslist, end, frametime);
470         */
471
472         for (i = 0;i < MAX_EXPLOSIONS;i++)
473                 if (explosion[i].alpha > 0.01f)
474                         R_MoveExplosion(&explosion[i]/*, gaslist, end, */);
475 }
476
477 void R_DrawExplosions(void)
478 {
479         int i;
480         if (!r_drawexplosions.integer)
481                 return;
482         for (i = 0;i < MAX_EXPLOSIONS;i++)
483                 if (explosion[i].alpha > 0.01f)
484                         R_DrawExplosion(&explosion[i]);
485 }