Formatted document

This commit is contained in:
smallmodel 2023-10-24 20:36:53 +02:00
parent 4f9831faa4
commit 088b22073c
No known key found for this signature in database
GPG key ID: 9F2D623CEDF08512

View file

@ -30,13 +30,7 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#include "g_phys.h" #include "g_phys.h"
#include "scriptexception.h" #include "scriptexception.h"
void BSpline::Set void BSpline::Set(Vector *control_points_, int num_control_points_, splinetype_t type)
(
Vector *control_points_,
int num_control_points_,
splinetype_t type
)
{ {
int i; int i;
@ -44,34 +38,29 @@ void BSpline::Set
has_orientation = false; has_orientation = false;
if ( control_points ) if (control_points) {
{
delete[] control_points; delete[] control_points;
control_points = NULL; control_points = NULL;
} }
num_control_points = num_control_points_; num_control_points = num_control_points_;
if ( num_control_points ) if (num_control_points) {
{
control_points = new BSplineControlPoint[num_control_points]; control_points = new BSplineControlPoint[num_control_points];
assert(control_points); assert(control_points);
for( i = 0; i < num_control_points; i++ ) for (i = 0; i < num_control_points; i++) {
{
control_points[i].Set(control_points_[i]); control_points[i].Set(control_points_[i]);
} }
} }
} }
void BSpline::Set void BSpline::Set(
(
Vector *control_points_, Vector *control_points_,
Vector *control_orients_, Vector *control_orients_,
float *control_speeds_, float *control_speeds_,
int num_control_points_, int num_control_points_,
splinetype_t type splinetype_t type
) )
{ {
int i; int i;
@ -79,33 +68,25 @@ void BSpline::Set
has_orientation = true; has_orientation = true;
if ( control_points ) if (control_points) {
{
delete[] control_points; delete[] control_points;
control_points = NULL; control_points = NULL;
} }
num_control_points = num_control_points_; num_control_points = num_control_points_;
if ( num_control_points ) if (num_control_points) {
{
control_points = new BSplineControlPoint[num_control_points]; control_points = new BSplineControlPoint[num_control_points];
assert(control_points); assert(control_points);
for( i = 0; i < num_control_points; i++ ) for (i = 0; i < num_control_points; i++) {
{
control_points[i].Set(control_points_[i], control_orients_[i], control_speeds_[i]); control_points[i].Set(control_points_[i], control_orients_[i], control_speeds_[i]);
} }
} }
} }
void BSpline::Clear void BSpline::Clear(void)
(
void
)
{
if( control_points )
{ {
if (control_points) {
delete[] control_points; delete[] control_points;
control_points = NULL; control_points = NULL;
} }
@ -113,13 +94,7 @@ void BSpline::Clear
has_orientation = false; has_orientation = false;
} }
inline float BSpline::EvalNormal inline float BSpline::EvalNormal(float u, Vector& pos, Vector& orient)
(
float u,
Vector& pos,
Vector& orient
)
{ {
int segment_id; int segment_id;
float B[4]; float B[4];
@ -131,12 +106,10 @@ inline float BSpline::EvalNormal
float speed; float speed;
segment_id = (int)u; segment_id = (int)u;
if ( segment_id < 0 ) if (segment_id < 0) {
{
segment_id = 0; segment_id = 0;
} }
if ( segment_id > num_control_points - 4 ) if (segment_id > num_control_points - 4) {
{
segment_id = num_control_points - 4; segment_id = num_control_points - 4;
} }
u -= (float)segment_id; u -= (float)segment_id;
@ -150,29 +123,19 @@ inline float BSpline::EvalNormal
B[2] = (-3.0f * u_3 + 3.0f * u_2 + 3.0f * u + 1) * (1.0f / 6.0f); B[2] = (-3.0f * u_3 + 3.0f * u_2 + 3.0f * u + 1) * (1.0f / 6.0f);
B[3] = u_3 * (1.0f / 6.0f); B[3] = u_3 * (1.0f / 6.0f);
pos = pos = *control_points[0 + segment_id].GetPosition() * B[0] + *control_points[1 + segment_id].GetPosition() * B[1]
*control_points[ 0 + segment_id ].GetPosition() * B[ 0 ] + + *control_points[2 + segment_id].GetPosition() * B[2] + *control_points[3 + segment_id].GetPosition() * B[3];
*control_points[ 1 + segment_id ].GetPosition() * B[ 1 ] +
*control_points[ 2 + segment_id ].GetPosition() * B[ 2 ] +
*control_points[ 3 + segment_id ].GetPosition() * B[ 3 ];
ang = ang = *control_points[0 + segment_id].GetOrientation() * B[0]
*control_points[ 0 + segment_id ].GetOrientation() * B[ 0 ] + + *control_points[1 + segment_id].GetOrientation() * B[1]
*control_points[ 1 + segment_id ].GetOrientation() * B[ 1 ] + + *control_points[2 + segment_id].GetOrientation() * B[2]
*control_points[ 2 + segment_id ].GetOrientation() * B[ 2 ] + + *control_points[3 + segment_id].GetOrientation() * B[3];
*control_points[ 3 + segment_id ].GetOrientation() * B[ 3 ];
roll = roll = *control_points[0 + segment_id].GetRoll() * B[0] + *control_points[1 + segment_id].GetRoll() * B[1]
*control_points[ 0 + segment_id ].GetRoll() * B[ 0 ] + + *control_points[2 + segment_id].GetRoll() * B[2] + *control_points[3 + segment_id].GetRoll() * B[3];
*control_points[ 1 + segment_id ].GetRoll() * B[ 1 ] +
*control_points[ 2 + segment_id ].GetRoll() * B[ 2 ] +
*control_points[ 3 + segment_id ].GetRoll() * B[ 3 ];
speed = speed = *control_points[0 + segment_id].GetSpeed() * B[0] + *control_points[1 + segment_id].GetSpeed() * B[1]
*control_points[ 0 + segment_id ].GetSpeed() * B[ 0 ] + + *control_points[2 + segment_id].GetSpeed() * B[2] + *control_points[3 + segment_id].GetSpeed() * B[3];
*control_points[ 1 + segment_id ].GetSpeed() * B[ 1 ] +
*control_points[ 2 + segment_id ].GetSpeed() * B[ 2 ] +
*control_points[ 3 + segment_id ].GetSpeed() * B[ 3 ];
orient = ang.toAngles(); orient = ang.toAngles();
orient[ROLL] = roll; orient[ROLL] = roll;
@ -180,13 +143,7 @@ inline float BSpline::EvalNormal
return speed; return speed;
} }
inline float BSpline::EvalLoop inline float BSpline::EvalLoop(float t, Vector& pos, Vector& orient)
(
float t,
Vector& pos,
Vector& orient
)
{ {
Vector retval; Vector retval;
Vector ang; Vector ang;
@ -206,8 +163,7 @@ inline float BSpline::EvalLoop
u = t - floor(t); u = t - floor(t);
segment_id %= num_control_points; segment_id %= num_control_points;
if ( segment_id < 0 ) if (segment_id < 0) {
{
segment_id += num_control_points; segment_id += num_control_points;
} }
@ -223,10 +179,8 @@ inline float BSpline::EvalLoop
speed = 0; speed = 0;
roll = 0; roll = 0;
for( i = 0, j = segment_id; i < 4; i++, j++ ) for (i = 0, j = segment_id; i < 4; i++, j++) {
{ if (j >= num_control_points) {
if ( j >= num_control_points )
{
j -= (num_control_points - loop_control_point); j -= (num_control_points - loop_control_point);
} }
@ -239,8 +193,7 @@ inline float BSpline::EvalLoop
pos = retval; pos = retval;
next_id = segment_id + 1; next_id = segment_id + 1;
if ( next_id >= num_control_points ) if (next_id >= num_control_points) {
{
next_id -= (num_control_points - loop_control_point); next_id -= (num_control_points - loop_control_point);
} }
orient = ang.toAngles(); orient = ang.toAngles();
@ -249,13 +202,7 @@ inline float BSpline::EvalLoop
return speed; return speed;
} }
inline float BSpline::EvalClamp inline float BSpline::EvalClamp(float t, Vector& pos, Vector& orient)
(
float t,
Vector& pos,
Vector& orient
)
{ {
Vector retval; Vector retval;
Vector ang; Vector ang;
@ -285,15 +232,11 @@ inline float BSpline::EvalClamp
speed = 0; speed = 0;
roll = 0; roll = 0;
for( i = 0; i < 4; i++, segment_id++ ) for (i = 0; i < 4; i++, segment_id++) {
{
j = segment_id; j = segment_id;
if ( j < 0 ) if (j < 0) {
{
j = 0; j = 0;
} } else if (j >= num_control_points) {
else if ( j >= num_control_points )
{
j = num_control_points - 1; j = num_control_points - 1;
} }
@ -306,20 +249,16 @@ inline float BSpline::EvalClamp
pos = retval; pos = retval;
next_id = segment_id + 1; next_id = segment_id + 1;
if ( segment_id < 0 ) if (segment_id < 0) {
{
segment_id = 0; segment_id = 0;
} }
if ( segment_id >= num_control_points ) if (segment_id >= num_control_points) {
{
segment_id = num_control_points - 1; segment_id = num_control_points - 1;
} }
if ( next_id < 0 ) if (next_id < 0) {
{
next_id = 0; next_id = 0;
} }
if ( next_id >= num_control_points ) if (next_id >= num_control_points) {
{
next_id = num_control_points - 1; next_id = num_control_points - 1;
} }
orient = ang.toAngles(); orient = ang.toAngles();
@ -328,18 +267,12 @@ inline float BSpline::EvalClamp
return speed; return speed;
} }
Vector BSpline::Eval(float u)
Vector BSpline::Eval
(
float u
)
{ {
Vector pos; Vector pos;
Vector orient; Vector orient;
switch( curvetype ) switch (curvetype) {
{
default: default:
case SPLINE_NORMAL: case SPLINE_NORMAL:
EvalNormal(u, pos, orient); EvalNormal(u, pos, orient);
@ -350,12 +283,9 @@ Vector BSpline::Eval
break; break;
case SPLINE_LOOP: case SPLINE_LOOP:
if ( u < 0 ) if (u < 0) {
{
EvalClamp(u, pos, orient); EvalClamp(u, pos, orient);
} } else {
else
{
EvalLoop(u, pos, orient); EvalLoop(u, pos, orient);
} }
break; break;
@ -363,16 +293,9 @@ Vector BSpline::Eval
return pos; return pos;
} }
float BSpline::Eval float BSpline::Eval(float u, Vector& pos, Vector& orient)
(
float u,
Vector &pos,
Vector &orient
)
{
switch( curvetype )
{ {
switch (curvetype) {
default: default:
case SPLINE_NORMAL: case SPLINE_NORMAL:
return EvalNormal(u, pos, orient); return EvalNormal(u, pos, orient);
@ -383,64 +306,45 @@ float BSpline::Eval
break; break;
case SPLINE_LOOP: case SPLINE_LOOP:
if ( u < 0 ) if (u < 0) {
{
return EvalClamp(u, pos, orient); return EvalClamp(u, pos, orient);
} } else {
else
{
return EvalLoop(u, pos, orient); return EvalLoop(u, pos, orient);
} }
break; break;
} }
} }
void BSpline::DrawControlSegments void BSpline::DrawControlSegments(void)
(
void
)
{ {
int i; int i;
G_BeginLine(); G_BeginLine();
for( i = 0; i < num_control_points; i++ ) for (i = 0; i < num_control_points; i++) {
{
G_Vertex(*control_points[i].GetPosition()); G_Vertex(*control_points[i].GetPosition());
} }
G_EndLine(); G_EndLine();
} }
void BSpline::DrawCurve void BSpline::DrawCurve(int num_subdivisions)
(
int num_subdivisions
)
{ {
float u; float u;
float du; float du;
if ( !num_control_points ) if (!num_control_points) {
{
return; return;
} }
du = 1.0f / (float)num_subdivisions; du = 1.0f / (float)num_subdivisions;
G_BeginLine(); G_BeginLine();
for( u = -2.0f; u <= ( float )num_control_points; u += du ) for (u = -2.0f; u <= (float)num_control_points; u += du) {
{
G_Vertex((Vector)Eval(u)); G_Vertex((Vector)Eval(u));
} }
G_EndLine(); G_EndLine();
} }
void BSpline::DrawCurve void BSpline::DrawCurve(Vector offset, int num_subdivisions)
(
Vector offset,
int num_subdivisions
)
{ {
float u; float u;
float du; float du;
@ -448,18 +352,13 @@ void BSpline::DrawCurve
du = 1.0f / (float)num_subdivisions; du = 1.0f / (float)num_subdivisions;
G_BeginLine(); G_BeginLine();
for( u = -2.0f; u <= ( float )num_control_points; u += du ) for (u = -2.0f; u <= (float)num_control_points; u += du) {
{
G_Vertex(offset + (Vector)Eval(u)); G_Vertex(offset + (Vector)Eval(u));
} }
G_EndLine(); G_EndLine();
} }
void BSpline::AppendControlPoint void BSpline::AppendControlPoint(const Vector& new_control_point)
(
const Vector& new_control_point
)
{ {
BSplineControlPoint *old_control_points; BSplineControlPoint *old_control_points;
int i; int i;
@ -470,10 +369,8 @@ void BSpline::AppendControlPoint
control_points = new BSplineControlPoint[num_control_points]; control_points = new BSplineControlPoint[num_control_points];
assert(control_points); assert(control_points);
if ( old_control_points ) if (old_control_points) {
{ for (i = 0; i < num_control_points - 1; i++) {
for( i = 0; i < num_control_points - 1; i++ )
{
control_points[i] = old_control_points[i]; control_points[i] = old_control_points[i];
} }
delete[] old_control_points; delete[] old_control_points;
@ -482,12 +379,7 @@ void BSpline::AppendControlPoint
control_points[num_control_points - 1].Set(new_control_point); control_points[num_control_points - 1].Set(new_control_point);
} }
void BSpline::AppendControlPoint void BSpline::AppendControlPoint(const Vector& new_control_point, const float& speed)
(
const Vector& new_control_point,
const float& speed
)
{ {
BSplineControlPoint *old_control_points; BSplineControlPoint *old_control_points;
int i; int i;
@ -498,10 +390,8 @@ void BSpline::AppendControlPoint
control_points = new BSplineControlPoint[num_control_points]; control_points = new BSplineControlPoint[num_control_points];
assert(control_points); assert(control_points);
if ( old_control_points ) if (old_control_points) {
{ for (i = 0; i < num_control_points - 1; i++) {
for( i = 0; i < num_control_points - 1; i++ )
{
control_points[i] = old_control_points[i]; control_points[i] = old_control_points[i];
} }
delete[] old_control_points; delete[] old_control_points;
@ -510,13 +400,9 @@ void BSpline::AppendControlPoint
control_points[num_control_points - 1].Set(new_control_point, speed); control_points[num_control_points - 1].Set(new_control_point, speed);
} }
void BSpline::AppendControlPoint void BSpline::AppendControlPoint(
( const Vector& new_control_point, const Vector& new_control_orient, const float& new_control_speed
const Vector& new_control_point,
const Vector& new_control_orient,
const float& new_control_speed
) )
{ {
BSplineControlPoint *old_control_points; BSplineControlPoint *old_control_points;
int i; int i;
@ -529,10 +415,8 @@ void BSpline::AppendControlPoint
control_points = new BSplineControlPoint[num_control_points]; control_points = new BSplineControlPoint[num_control_points];
assert(control_points); assert(control_points);
if ( old_control_points ) if (old_control_points) {
{ for (i = 0; i < num_control_points - 1; i++) {
for( i = 0; i < num_control_points - 1; i++ )
{
control_points[i] = old_control_points[i]; control_points[i] = old_control_points[i];
} }
delete[] old_control_points; delete[] old_control_points;
@ -541,29 +425,19 @@ void BSpline::AppendControlPoint
control_points[num_control_points - 1].Set(new_control_point, new_control_orient, new_control_speed); control_points[num_control_points - 1].Set(new_control_point, new_control_orient, new_control_speed);
} }
void BSpline::SetLoopPoint void BSpline::SetLoopPoint(const Vector& pos)
(
const Vector& pos
)
{ {
int i; int i;
for( i = 0; i < num_control_points; i++ ) for (i = 0; i < num_control_points; i++) {
{ if (pos == *control_points[i].GetPosition()) {
if ( pos == *control_points[ i ].GetPosition() )
{
loop_control_point = i; loop_control_point = i;
break; break;
} }
} }
} }
int BSpline::PickControlPoint int BSpline::PickControlPoint(const Vector& window_point, float pick_size)
(
const Vector& window_point,
float pick_size
)
{ {
int i; int i;
float closest_dist_2; float closest_dist_2;
@ -573,23 +447,18 @@ int BSpline::PickControlPoint
closest_index = -1; closest_index = -1;
closest_dist_2 = 1000000.0f; closest_dist_2 = 1000000.0f;
for( i = 0; i < num_control_points; i++ ) for (i = 0; i < num_control_points; i++) {
{
delta = window_point - *control_points[i].GetPosition(); delta = window_point - *control_points[i].GetPosition();
dist_2 = delta * delta; dist_2 = delta * delta;
if ( dist_2 < closest_dist_2 ) if (dist_2 < closest_dist_2) {
{
closest_dist_2 = dist_2; closest_dist_2 = dist_2;
closest_index = i; closest_index = i;
} }
} }
if ( pick_size * pick_size >= closest_dist_2 ) if (pick_size * pick_size >= closest_dist_2) {
{
return closest_index; return closest_index;
} } else {
else
{
return -1; return -1;
} }
} }
@ -660,8 +529,7 @@ Event EV_SplinePath_SetFadeTime
EV_NORMAL EV_NORMAL
); );
CLASS_DECLARATION( Entity, SplinePath, "info_splinepath" ) CLASS_DECLARATION(Entity, SplinePath, "info_splinepath") {
{
{&EV_SplinePath_Create, &SplinePath::CreatePath }, {&EV_SplinePath_Create, &SplinePath::CreatePath },
{&EV_SplinePath_Loop, &SplinePath::SetLoop }, {&EV_SplinePath_Loop, &SplinePath::SetLoop },
{&EV_SplinePath_Speed, &SplinePath::SetSpeed }, {&EV_SplinePath_Speed, &SplinePath::SetSpeed },
@ -678,7 +546,6 @@ SplinePath::SplinePath()
AddWaitTill(STRING_REACH); AddWaitTill(STRING_REACH);
owner = this; owner = this;
next = NULL; next = NULL;
loop = NULL; loop = NULL;
@ -692,8 +559,7 @@ SplinePath::SplinePath()
setSolidType(SOLID_NOT); setSolidType(SOLID_NOT);
hideModel(); hideModel();
if( !LoadingSavegame ) if (!LoadingSavegame) {
{
PostEvent(EV_SplinePath_Create, FRAMETIME); PostEvent(EV_SplinePath_Create, FRAMETIME);
} }
} }
@ -701,12 +567,9 @@ SplinePath::SplinePath()
SplinePath::~SplinePath() SplinePath::~SplinePath()
{ {
// disconnect from the chain // disconnect from the chain
if ( owner != this ) if (owner != this) {
{
owner->SetNext(next); owner->SetNext(next);
} } else if (next) {
else if ( next )
{
next->SetPrev(NULL); next->SetPrev(NULL);
next = NULL; next = NULL;
} }
@ -715,249 +578,153 @@ SplinePath::~SplinePath()
assert(next == NULL); assert(next == NULL);
} }
void SplinePath::SetLoop void SplinePath::SetLoop(Event *ev)
(
Event *ev
)
{ {
loop_name = ev->GetString(1); loop_name = ev->GetString(1);
} }
void SplinePath::SetSpeed void SplinePath::SetSpeed(Event *ev)
(
Event *ev
)
{ {
speed = ev->GetFloat(1); speed = ev->GetFloat(1);
} }
void SplinePath::SetTriggerTarget void SplinePath::SetTriggerTarget(Event *ev)
(
Event *ev
)
{ {
SetTriggerTarget(ev->GetString(1)); SetTriggerTarget(ev->GetString(1));
} }
void SplinePath::CreatePath void SplinePath::CreatePath(Event *ev)
(
Event *ev
)
{ {
const char *target; const char *target;
Entity *ent; Entity *ent;
// Make the path from the targetlist. // Make the path from the targetlist.
target = Target(); target = Target();
if ( target[ 0 ] ) if (target[0]) {
{
ent = (Entity *)G_FindTarget(NULL, target); ent = (Entity *)G_FindTarget(NULL, target);
if ( ent ) if (ent) {
{
next = (SplinePath *)ent; next = (SplinePath *)ent;
next->owner = this; next->owner = this;
} } else {
else
{
ScriptError("SplinePath::CreatePath: target %s not found\n", target); ScriptError("SplinePath::CreatePath: target %s not found\n", target);
} }
} }
if ( loop_name.length() ) if (loop_name.length()) {
{
ent = (Entity *)G_FindTarget(NULL, loop_name.c_str()); ent = (Entity *)G_FindTarget(NULL, loop_name.c_str());
if ( ent ) if (ent) {
{
loop = (SplinePath *)ent; loop = (SplinePath *)ent;
} }
} }
} }
SplinePath *SplinePath::GetNext SplinePath *SplinePath::GetNext(void)
(
void
)
{ {
return next; return next;
} }
SplinePath *SplinePath::GetPrev SplinePath *SplinePath::GetPrev(void)
(
void
)
{
if ( owner == this )
{ {
if (owner == this) {
return NULL; return NULL;
} }
return owner; return owner;
} }
void SplinePath::SetNext void SplinePath::SetNext(SplinePath *node)
(
SplinePath *node
)
{
if ( next )
{ {
if (next) {
// remove ourselves from the chain // remove ourselves from the chain
next->owner = next; next->owner = next;
} }
next = node; next = node;
if ( next ) if (next) {
{
// disconnect next from it's previous node // disconnect next from it's previous node
if ( next->owner != next ) if (next->owner != next) {
{
next->owner->next = NULL; next->owner->next = NULL;
} }
next->owner = this; next->owner = this;
} }
} }
void SplinePath::SetPrev void SplinePath::SetPrev(SplinePath *node)
(
SplinePath *node
)
{
if ( owner != this )
{ {
if (owner != this) {
owner->next = NULL; owner->next = NULL;
} }
if ( node && ( node != this ) ) if (node && (node != this)) {
{
// safely remove the node from its chain // safely remove the node from its chain
if ( node->next ) if (node->next) {
{
node->next->owner = node->next; node->next->owner = node->next;
} }
node->next = this; node->next = this;
owner = node; owner = node;
} } else {
else
{
owner = this; owner = this;
} }
} }
SplinePath *SplinePath::GetLoop SplinePath *SplinePath::GetLoop(void)
(
void
)
{ {
return loop; return loop;
} }
void SplinePath::SetWatch void SplinePath::SetWatch(const char *name)
(
const char *name
)
{
if ( watchEnt != name )
{ {
if (watchEnt != name) {
watchEnt = name; watchEnt = name;
if ( watchEnt.length() ) if (watchEnt.length()) {
{
doWatch = true; doWatch = true;
} } else {
else
{
doWatch = false; doWatch = false;
} }
} }
} }
void SplinePath::SetWatch void SplinePath::SetWatch(Event *ev)
(
Event *ev
)
{ {
SetWatch(ev->GetString(1)); SetWatch(ev->GetString(1));
} }
void SplinePath::NoWatch(void)
void SplinePath::NoWatch
(
void
)
{ {
doWatch = true; doWatch = true;
watchEnt = "none"; watchEnt = "none";
} }
str SplinePath::GetWatch str SplinePath::GetWatch(void)
(
void
)
{ {
return watchEnt; return watchEnt;
} }
void SplinePath::SetFov void SplinePath::SetFov(float newFov)
(
float newFov
)
{ {
fov = newFov; fov = newFov;
} }
void SplinePath::SetFov void SplinePath::SetFov(Event *ev)
(
Event *ev
)
{ {
fov = ev->GetFloat(1); fov = ev->GetFloat(1);
} }
float SplinePath::GetFov float SplinePath::GetFov(void)
(
void
)
{ {
return fov; return fov;
} }
void SplinePath::SetFadeTime void SplinePath::SetFadeTime(float newFadeTime)
(
float newFadeTime
)
{ {
fadeTime = newFadeTime; fadeTime = newFadeTime;
} }
void SplinePath::SetFadeTime void SplinePath::SetFadeTime(Event *ev)
(
Event *ev
)
{ {
fadeTime = ev->GetFloat(1); fadeTime = ev->GetFloat(1);
} }
float SplinePath::GetFadeTime float SplinePath::GetFadeTime(void)
(
void
)
{ {
return fadeTime; return fadeTime;
} }