Bring QW's unlimited beams/explosions into NQ.

This commit is contained in:
Bill Currie 2010-11-23 10:48:10 +09:00
parent abc17c1d91
commit 525e20e3e2
3 changed files with 225 additions and 183 deletions

View file

@ -155,6 +155,7 @@ CL_ClearState (void)
r_force_fullscreen = 0; r_force_fullscreen = 0;
CL_Init_Entity (&cl.viewent); CL_Init_Entity (&cl.viewent);
r_view_model = &cl.viewent;
SZ_Clear (&cls.message); SZ_Clear (&cls.message);

View file

@ -50,47 +50,60 @@ static __attribute__ ((used)) const char rcsid[] =
#include "compat.h" #include "compat.h"
#include "r_dynamic.h" #include "r_dynamic.h"
#define MAX_BEAMS 8 typedef struct tent_s {
#define MAX_BEAM_ENTS 20 struct tent_s *next;
entity_t ent;
} tent_t;
#define TEMP_BATCH 64
static tent_t *temp_entities = 0;
typedef struct { typedef struct {
int entity; int entity;
struct model_s *model; struct model_s *model;
float endtime; float endtime;
vec3_t start, end; vec3_t start, end;
entity_t ent_list[MAX_BEAM_ENTS]; tent_t *tents;
int ent_count; int seed;
int seed;
} beam_t; } beam_t;
beam_t cl_beams[MAX_BEAMS];
#define MAX_EXPLOSIONS 8
typedef struct { typedef struct {
float start; float start;
entity_t ent; tent_t *tent;
} explosion_t; } explosion_t;
explosion_t cl_explosions[MAX_EXPLOSIONS]; typedef struct tent_obj_s {
struct tent_obj_s *next;
union {
beam_t beam;
explosion_t ex;
} to;
} tent_obj_t;
sfx_t *cl_sfx_wizhit; static tent_obj_t *tent_objects;
sfx_t *cl_sfx_knighthit; static tent_obj_t *cl_beams;
sfx_t *cl_sfx_tink1; static tent_obj_t *cl_explosions;
sfx_t *cl_sfx_ric1;
sfx_t *cl_sfx_ric2;
sfx_t *cl_sfx_ric3;
sfx_t *cl_sfx_r_exp3;
model_t *cl_mod_beam; static sfx_t *cl_sfx_wizhit;
model_t *cl_mod_bolt; static sfx_t *cl_sfx_knighthit;
model_t *cl_mod_bolt2; static sfx_t *cl_sfx_tink1;
model_t *cl_mod_bolt3; static sfx_t *cl_sfx_ric1;
model_t *cl_spr_explod; static sfx_t *cl_sfx_ric2;
static sfx_t *cl_sfx_ric3;
static sfx_t *cl_sfx_r_exp3;
void static model_t *cl_mod_beam;
CL_TEnts_Init (void) static model_t *cl_mod_bolt;
static model_t *cl_mod_bolt2;
static model_t *cl_mod_bolt3;
static model_t *cl_spr_explod;
static void
CL_TEnts_Precache (int phase)
{ {
if (!phase)
return;
cl_sfx_wizhit = S_PrecacheSound ("wizard/hit.wav"); cl_sfx_wizhit = S_PrecacheSound ("wizard/hit.wav");
cl_sfx_knighthit = S_PrecacheSound ("hknight/hit.wav"); cl_sfx_knighthit = S_PrecacheSound ("hknight/hit.wav");
cl_sfx_tink1 = S_PrecacheSound ("weapons/tink1.wav"); cl_sfx_tink1 = S_PrecacheSound ("weapons/tink1.wav");
@ -108,6 +121,13 @@ CL_TEnts_Init (void)
cl_mod_beam = cl_mod_bolt; cl_mod_beam = cl_mod_bolt;
} }
void
CL_TEnts_Init (void)
{
QFS_GamedirCallback (CL_TEnts_Precache);
CL_TEnts_Precache (1);
}
void void
CL_Init_Entity (entity_t *ent) CL_Init_Entity (entity_t *ent)
{ {
@ -120,81 +140,106 @@ CL_Init_Entity (entity_t *ent)
ent->pose1 = ent->pose2 = -1; ent->pose1 = ent->pose2 = -1;
} }
static tent_t *
new_temp_entity (void)
{
tent_t *tent;
if (!temp_entities) {
int i;
temp_entities = malloc (TEMP_BATCH * sizeof (tent_t));
for (i = 0; i < TEMP_BATCH - 1; i++) {
temp_entities[i].next = &temp_entities[i + 1];
}
temp_entities[i].next = 0;
}
tent = temp_entities;
temp_entities = tent->next;
tent->next = 0;
CL_Init_Entity (&tent->ent);
return tent;
}
static void
free_temp_entities (tent_t *tents)
{
tent_t **t = &tents;
while (*t)
t = &(*t)->next;
*t = temp_entities;
temp_entities = tents;
}
static tent_obj_t *
new_tent_object (void)
{
tent_obj_t *tobj;
if (!tent_objects) {
int i;
tent_objects = malloc (TEMP_BATCH * sizeof (tent_t));
for (i = 0; i < TEMP_BATCH - 1; i++)
tent_objects[i].next = &tent_objects[i + 1];
tent_objects[i].next = 0;
}
tobj = tent_objects;
tent_objects = tobj->next;
tobj->next = 0;
return tobj;
}
static void
free_tent_objects (tent_obj_t *tobjs)
{
tent_obj_t **t = &tobjs;
while (*t)
t = &(*t)->next;
*t = tent_objects;
tent_objects = tobjs;
}
void void
CL_ClearTEnts (void) CL_ClearTEnts (void)
{ {
int i; tent_t *t;
tent_obj_t *to;
memset (&cl_beams, 0, sizeof (cl_beams)); for (to = cl_beams; to; to = to->next) {
memset (&cl_explosions, 0, sizeof (cl_explosions)); for (t = to->to.beam.tents; t; t = t->next)
for (i = 0; i < MAX_BEAMS; i++) { t->ent.efrag = 0;
int j; free_temp_entities (to->to.beam.tents);
for (j = 0; j < MAX_BEAM_ENTS; j++) {
CL_Init_Entity (&cl_beams[i].ent_list[j]);
}
} }
for (i = 0; i < MAX_EXPLOSIONS; i++) { free_tent_objects (cl_beams);
CL_Init_Entity (&cl_explosions[i].ent); cl_beams = 0;
for (to = cl_explosions; to; to = to->next) {
for (t = to->to.ex.tent; t; t = t->next)
t->ent.efrag = 0;
free_temp_entities (to->to.ex.tent);
} }
r_view_model = &cl.viewent;
}
static explosion_t *
CL_AllocExplosion (void)
{
float time;
int index, i;
for (i = 0; i < MAX_EXPLOSIONS; i++)
if (!cl_explosions[i].ent.model)
return &cl_explosions[i];
// find the oldest explosion
time = cl.time;
index = 0;
for (i = 0; i < MAX_EXPLOSIONS; i++)
if (cl_explosions[i].start < time) {
time = cl_explosions[i].start;
index = i;
}
return &cl_explosions[index];
}
static beam_t *
beam_alloc (int ent)
{
int i;
beam_t *b;
// override any beam with the same entity
for (i = 0, b = cl_beams; i < MAX_BEAMS; i++, b++)
if (b->entity == ent)
return b;
// find a free beam
for (i = 0, b = cl_beams; i < MAX_BEAMS; i++, b++)
if (!b->model || b->endtime < cl.time)
return b;
Sys_Printf ("beam list overflow!\n");
return 0;
} }
static inline void static inline void
beam_clear (beam_t *b) beam_clear (beam_t *b)
{ {
if (b->ent_count) { if (b->tents) {
entity_t *e = b->ent_list + b->ent_count; tent_t *t;
while (e != b->ent_list) for (t = b->tents; t; t = t->next) {
R_RemoveEfrags (e-- - 1); R_RemoveEfrags (&t->ent);
b->ent_count = 0; t->ent.efrag = 0;
}
free_temp_entities (b->tents);
b->tents = 0;
} }
} }
static inline void static inline void
beam_setup (beam_t *b) beam_setup (beam_t *b)
{ {
entity_t *ent; tent_t *tent;
float forward, pitch, yaw, d; float forward, pitch, yaw, d;
int ent_count; int ent_count;
vec3_t dist, org; vec3_t dist, org;
@ -224,24 +269,25 @@ beam_setup (beam_t *b)
d = VectorNormalize (dist); d = VectorNormalize (dist);
VectorScale (dist, 30, dist); VectorScale (dist, 30, dist);
ent_count = ceil (d / 30); ent_count = ceil (d / 30);
ent_count = min (ent_count, MAX_BEAM_ENTS);
b->ent_count = ent_count;
d = 0; d = 0;
while (ent_count--) { while (ent_count--) {
ent = &b->ent_list[ent_count]; tent = new_temp_entity ();
VectorMultAdd (org, d, dist, ent->origin); tent->next = b->tents;
b->tents = tent;
VectorMultAdd (org, d, dist, tent->ent.origin);
d += 1.0; d += 1.0;
ent->model = b->model; tent->ent.model = b->model;
ent->angles[0] = pitch; tent->ent.angles[0] = pitch;
ent->angles[1] = yaw; tent->ent.angles[1] = yaw;
if (!ent->efrag) R_AddEfrags (&tent->ent);
R_AddEfrags (ent);
} }
} }
static void static void
CL_ParseBeam (model_t *m) CL_ParseBeam (model_t *m)
{ {
tent_obj_t *to;
beam_t *b; beam_t *b;
int ent; int ent;
vec3_t start, end; vec3_t start, end;
@ -251,30 +297,35 @@ CL_ParseBeam (model_t *m)
MSG_ReadCoordV (net_message, start); MSG_ReadCoordV (net_message, start);
MSG_ReadCoordV (net_message, end); MSG_ReadCoordV (net_message, end);
if ((b = beam_alloc (ent))) { to = new_tent_object ();
beam_clear (b); to->next = cl_beams;
b->entity = ent; cl_beams = to;
b->model = m; b = &to->to.beam;
b->endtime = cl.time + 0.2; b->tents = 0;
b->seed = rand ();
VectorCopy (end, b->end); beam_clear (b);
if (b->entity != cl.viewentity) { b->entity = ent;
// this will be done in CL_UpdateBeams b->model = m;
VectorCopy (start, b->start); b->endtime = cl.time + 0.2;
beam_setup (b); b->seed = rand ();
} VectorCopy (end, b->end);
if (b->entity != cl.viewentity) {
// this will be done in CL_UpdateBeams
VectorCopy (start, b->start);
beam_setup (b);
} }
} }
void void
CL_ParseTEnt (void) CL_ParseTEnt (void)
{ {
byte type; byte type;
dlight_t *dl; dlight_t *dl;
int colorStart, colorLength; tent_obj_t *to;
explosion_t *ex; explosion_t *ex;
vec3_t col, pos; int colorStart, colorLength;
sfx_t * spike_sound[] = { vec3_t col, pos;
sfx_t *spike_sound[] = {
cl_sfx_ric3, cl_sfx_ric3, cl_sfx_ric2, cl_sfx_ric1, cl_sfx_ric3, cl_sfx_ric3, cl_sfx_ric2, cl_sfx_ric1,
}; };
@ -346,18 +397,24 @@ CL_ParseTEnt (void)
S_StartSound (-1, 0, cl_sfx_r_exp3, pos, 1, 1); S_StartSound (-1, 0, cl_sfx_r_exp3, pos, 1, 1);
// sprite // sprite
ex = CL_AllocExplosion (); to = new_tent_object ();
VectorCopy (pos, ex->ent.origin); to->next = cl_explosions;
cl_explosions = to;
ex = &to->to.ex;
ex->tent = new_temp_entity ();
VectorCopy (pos, ex->tent->ent.origin);
ex->start = cl.time; ex->start = cl.time;
//FIXME need better model management //FIXME need better model management
if (!cl_spr_explod->cache.data) if (!cl_spr_explod->cache.data)
cl_spr_explod = Mod_ForName ("progs/s_explod.spr", true); cl_spr_explod = Mod_ForName ("progs/s_explod.spr", true);
ex->ent.model = cl_spr_explod; ex->tent->ent.model = cl_spr_explod;
break; break;
case TE_TAREXPLOSION: // tarbaby explosion case TE_TAREXPLOSION: // tarbaby explosion
MSG_ReadCoordV (net_message, pos); MSG_ReadCoordV (net_message, pos);
R_BlobExplosion (pos); R_BlobExplosion (pos);
S_StartSound (-1, 0, cl_sfx_r_exp3, pos, 1, 1); S_StartSound (-1, 0, cl_sfx_r_exp3, pos, 1, 1);
break; break;
@ -439,24 +496,33 @@ CL_ParseTEnt (void)
} }
} }
#define BEAM_SEED_INTERVAL 72
#define BEAM_SEED_PRIME 3191
static void static void
CL_UpdateBeams (void) CL_UpdateBeams (void)
{ {
tent_obj_t **to;
beam_t *b; beam_t *b;
entity_t **ent; unsigned seed;
float forward, pitch, yaw, d; tent_t *t;
int i;
vec3_t dist, org;
// update lightning // update lightning
for (i = 0, b = cl_beams; i < MAX_BEAMS; i++, b++) { for (to = &cl_beams; *to; ) {
b = &(*to)->to.beam;
if (!b->endtime) if (!b->endtime)
continue; continue;
if (!b->model || b->endtime < cl.time) { if (!b->model || b->endtime < cl.time) {
tent_obj_t *_to;
b->endtime = 0; b->endtime = 0;
beam_clear (b); beam_clear (b);
_to = *to;
*to = _to->next;
_to->next = tent_objects;
tent_objects = _to;
continue; continue;
} }
to = &(*to)->next;
// if coming from the player, update the start position // if coming from the player, update the start position
if (b->entity == cl.viewentity) { if (b->entity == cl.viewentity) {
@ -465,43 +531,13 @@ CL_UpdateBeams (void)
beam_setup (b); beam_setup (b);
} }
// calculate pitch and yaw seed = b->seed + ((int) (cl.time * BEAM_SEED_INTERVAL) %
VectorSubtract (b->end, b->start, dist); BEAM_SEED_INTERVAL);
if (dist[1] == 0 && dist[0] == 0) {
yaw = 0;
if (dist[2] > 0)
pitch = 90;
else
pitch = 270;
} else {
yaw = (int) (atan2 (dist[1], dist[0]) * 180 / M_PI);
if (yaw < 0)
yaw += 360;
forward = sqrt (dist[0] * dist[0] + dist[1] * dist[1]);
pitch = (int) (atan2 (dist[2], forward) * 180 / M_PI);
if (pitch < 0)
pitch += 360;
}
// add new entities for the lightning // add new entities for the lightning
VectorCopy (b->start, org); for (t = b->tents; t; t = t->next) {
d = VectorNormalize (dist); seed = seed * BEAM_SEED_PRIME;
b->ent_count = 0; t->ent.angles[2] = seed % 360;
while (d > 0 && b->ent_count < MAX_BEAM_ENTS) {
ent = R_NewEntity ();
if (!ent)
return;
*ent = &b->ent_list[b->ent_count++];
VectorCopy (org, (*ent)->origin);
(*ent)->model = b->model;
(*ent)->angles[0] = pitch;
(*ent)->angles[1] = yaw;
(*ent)->angles[2] = rand () % 360;
VectorMultAdd(org, 30, dist, org);
d -= 30;
} }
} }
} }
@ -509,24 +545,31 @@ CL_UpdateBeams (void)
static void static void
CL_UpdateExplosions (void) CL_UpdateExplosions (void)
{ {
entity_t **ent; int f;
tent_obj_t **to;
explosion_t *ex; explosion_t *ex;
int f, i; entity_t *ent;
for (i = 0, ex = cl_explosions; i < MAX_EXPLOSIONS; i++, ex++) { for (to = &cl_explosions; *to; ) {
if (!ex->ent.model) ex = &(*to)->to.ex;
continue; ent = &ex->tent->ent;
f = 10 * (cl.time - ex->start); f = 10 * (cl.time - ex->start);
if (f >= ex->ent.model->numframes) { if (f >= ent->model->numframes) {
ex->ent.model = NULL; tent_obj_t *_to;
R_RemoveEfrags (ent);
ent->efrag = 0;
free_temp_entities (ex->tent);
_to = *to;
*to = _to->next;
_to->next = tent_objects;
tent_objects = _to;
continue; continue;
} }
to = &(*to)->next;
ent = R_NewEntity (); ent->frame = f;
if (!ent) if (!ent->efrag)
return; R_AddEfrags (ent);
ex->ent.frame = f;
*ent = &ex->ent;
} }
} }

View file

@ -62,8 +62,6 @@ typedef struct tent_s {
#define TEMP_BATCH 64 #define TEMP_BATCH 64
static tent_t *temp_entities = 0; static tent_t *temp_entities = 0;
#define MAX_BEAMS 8
typedef struct { typedef struct {
int entity; int entity;
struct model_s *model; struct model_s *model;
@ -91,19 +89,19 @@ static tent_obj_t *tent_objects;
static tent_obj_t *cl_beams; static tent_obj_t *cl_beams;
static tent_obj_t *cl_explosions; static tent_obj_t *cl_explosions;
static sfx_t *cl_sfx_wizhit; static sfx_t *cl_sfx_wizhit;
static sfx_t *cl_sfx_knighthit; static sfx_t *cl_sfx_knighthit;
static sfx_t *cl_sfx_tink1; static sfx_t *cl_sfx_tink1;
static sfx_t *cl_sfx_ric1; static sfx_t *cl_sfx_ric1;
static sfx_t *cl_sfx_ric2; static sfx_t *cl_sfx_ric2;
static sfx_t *cl_sfx_ric3; static sfx_t *cl_sfx_ric3;
static sfx_t *cl_sfx_r_exp3; static sfx_t *cl_sfx_r_exp3;
static model_t *cl_mod_beam; static model_t *cl_mod_beam;
static model_t *cl_mod_bolt; static model_t *cl_mod_bolt;
static model_t *cl_mod_bolt2; static model_t *cl_mod_bolt2;
static model_t *cl_mod_bolt3; static model_t *cl_mod_bolt3;
static model_t *cl_spr_explod; static model_t *cl_spr_explod;
static void static void
CL_TEnts_Precache (int phase) CL_TEnts_Precache (int phase)
@ -325,14 +323,14 @@ CL_ParseBeam (model_t *m)
void void
CL_ParseTEnt (void) CL_ParseTEnt (void)
{ {
byte type; byte type;
dlight_t *dl; dlight_t *dl;
tent_obj_t *to; tent_obj_t *to;
explosion_t *ex; explosion_t *ex;
int colorStart, colorLength; int colorStart, colorLength;
int cnt = -1; int cnt = -1;
vec3_t pos; vec3_t pos;
sfx_t * spike_sound[] = { sfx_t *spike_sound[] = {
cl_sfx_ric3, cl_sfx_ric3, cl_sfx_ric2, cl_sfx_ric1, cl_sfx_ric3, cl_sfx_ric3, cl_sfx_ric2, cl_sfx_ric1,
}; };
@ -556,13 +554,13 @@ CL_UpdateBeams (void)
static void static void
CL_UpdateExplosions (void) CL_UpdateExplosions (void)
{ {
int f; int f;
tent_obj_t **to; tent_obj_t **to;
explosion_t *ex; explosion_t *ex;
entity_t *ent;
for (to = &cl_explosions; *to; ) { for (to = &cl_explosions; *to; ) {
ex = &(*to)->to.ex; ex = &(*to)->to.ex;
entity_t *ent;
ent = &ex->tent->ent; ent = &ex->tent->ent;
f = 10 * (cl.time - ex->start); f = 10 * (cl.time - ex->start);
if (f >= ent->model->numframes) { if (f >= ent->model->numframes) {