doom3-bfg/neo/framework/DeclAF.cpp

2166 lines
46 KiB
C++
Raw Normal View History

2012-11-26 18:58:24 +00:00
/*
===========================================================================
Doom 3 BFG Edition GPL Source Code
Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company.
2012-11-26 18:58:24 +00:00
This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code").
2012-11-26 18:58:24 +00:00
Doom 3 BFG Edition Source Code is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Doom 3 BFG Edition Source Code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Doom 3 BFG Edition Source Code. If not, see <http://www.gnu.org/licenses/>.
In addition, the Doom 3 BFG Edition Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 BFG Edition Source Code. If not, please request a copy in writing from id Software at the address below.
If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
===========================================================================
*/
#include "../idlib/precompiled.h"
#pragma hdrstop
/*
===============================================================================
idDeclAF
===============================================================================
*/
/*
================
idAFVector::idAFVector
================
*/
idAFVector::idAFVector()
{
2012-11-26 18:58:24 +00:00
type = VEC_COORDS;
vec.Zero();
negate = false;
}
/*
================
idAFVector::Parse
================
*/
bool idAFVector::Parse( idLexer& src )
{
2012-11-26 18:58:24 +00:00
idToken token;
if( !src.ReadToken( &token ) )
{
2012-11-26 18:58:24 +00:00
return false;
}
if( token == "-" )
{
2012-11-26 18:58:24 +00:00
negate = true;
if( !src.ReadToken( &token ) )
{
2012-11-26 18:58:24 +00:00
return false;
}
}
else
{
2012-11-26 18:58:24 +00:00
negate = false;
}
if( token == "(" )
{
2012-11-26 18:58:24 +00:00
type = idAFVector::VEC_COORDS;
vec.x = src.ParseFloat();
src.ExpectTokenString( "," );
vec.y = src.ParseFloat();
src.ExpectTokenString( "," );
vec.z = src.ParseFloat();
src.ExpectTokenString( ")" );
}
else if( token == "joint" )
{
2012-11-26 18:58:24 +00:00
type = idAFVector::VEC_JOINT;
src.ExpectTokenString( "(" );
src.ReadToken( &token );
joint1 = token;
src.ExpectTokenString( ")" );
}
else if( token == "bonecenter" )
{
2012-11-26 18:58:24 +00:00
type = idAFVector::VEC_BONECENTER;
src.ExpectTokenString( "(" );
src.ReadToken( &token );
joint1 = token;
src.ExpectTokenString( "," );
src.ReadToken( &token );
joint2 = token;
src.ExpectTokenString( ")" );
}
else if( token == "bonedir" )
{
2012-11-26 18:58:24 +00:00
type = idAFVector::VEC_BONEDIR;
src.ExpectTokenString( "(" );
src.ReadToken( &token );
joint1 = token;
src.ExpectTokenString( "," );
src.ReadToken( &token );
joint2 = token;
src.ExpectTokenString( ")" );
}
else
{
2012-11-26 18:58:24 +00:00
src.Error( "unknown token %s in vector", token.c_str() );
return false;
}
2012-11-26 18:58:24 +00:00
return true;
}
/*
================
idAFVector::Finish
================
*/
bool idAFVector::Finish( const char* fileName, const getJointTransform_t GetJointTransform, const idJointMat* frame, void* model ) const
{
2012-11-26 18:58:24 +00:00
idMat3 axis;
idVec3 start, end;
switch( type )
{
case idAFVector::VEC_COORDS:
{
2012-11-26 18:58:24 +00:00
break;
}
case idAFVector::VEC_JOINT:
{
if( !GetJointTransform( model, frame, joint1, vec, axis ) )
{
2012-11-26 18:58:24 +00:00
common->Warning( "invalid joint %s in joint() in '%s'", joint1.c_str(), fileName );
vec.Zero();
}
break;
}
case idAFVector::VEC_BONECENTER:
{
if( !GetJointTransform( model, frame, joint1, start, axis ) )
{
2012-11-26 18:58:24 +00:00
common->Warning( "invalid joint %s in bonecenter() in '%s'", joint1.c_str(), fileName );
start.Zero();
}
if( !GetJointTransform( model, frame, joint2, end, axis ) )
{
2012-11-26 18:58:24 +00:00
common->Warning( "invalid joint %s in bonecenter() in '%s'", joint2.c_str(), fileName );
end.Zero();
}
vec = ( start + end ) * 0.5f;
break;
}
case idAFVector::VEC_BONEDIR:
{
if( !GetJointTransform( model, frame, joint1, start, axis ) )
{
2012-11-26 18:58:24 +00:00
common->Warning( "invalid joint %s in bonedir() in '%s'", joint1.c_str(), fileName );
start.Zero();
}
if( !GetJointTransform( model, frame, joint2, end, axis ) )
{
2012-11-26 18:58:24 +00:00
common->Warning( "invalid joint %s in bonedir() in '%s'", joint2.c_str(), fileName );
end.Zero();
}
vec = ( end - start );
break;
}
default:
{
2012-11-26 18:58:24 +00:00
vec.Zero();
break;
}
}
if( negate )
{
2012-11-26 18:58:24 +00:00
vec = -vec;
}
2012-11-26 18:58:24 +00:00
return true;
}
/*
================
idAFVector::Write
================
*/
bool idAFVector::Write( idFile* f ) const
{
2012-11-26 18:58:24 +00:00
if( negate )
{
2012-11-26 18:58:24 +00:00
f->WriteFloatString( "-" );
}
switch( type )
{
case idAFVector::VEC_COORDS:
{
2012-11-26 18:58:24 +00:00
f->WriteFloatString( "( %f, %f, %f )", vec.x, vec.y, vec.z );
break;
}
case idAFVector::VEC_JOINT:
{
2012-11-26 18:58:24 +00:00
f->WriteFloatString( "joint( \"%s\" )", joint1.c_str() );
break;
}
case idAFVector::VEC_BONECENTER:
{
2012-11-26 18:58:24 +00:00
f->WriteFloatString( "bonecenter( \"%s\", \"%s\" )", joint1.c_str(), joint2.c_str() );
break;
}
case idAFVector::VEC_BONEDIR:
{
2012-11-26 18:58:24 +00:00
f->WriteFloatString( "bonedir( \"%s\", \"%s\" )", joint1.c_str(), joint2.c_str() );
break;
}
default:
{
2012-11-26 18:58:24 +00:00
break;
}
}
return true;
}
/*
================
idAFVector::ToString
================
*/
const char* idAFVector::ToString( idStr& str, const int precision )
{
2012-11-26 18:58:24 +00:00
switch( type )
{
case idAFVector::VEC_COORDS:
{
2012-11-26 18:58:24 +00:00
char format[128];
sprintf( format, "( %%.%df, %%.%df, %%.%df )", precision, precision, precision );
sprintf( str, format, vec.x, vec.y, vec.z );
break;
}
case idAFVector::VEC_JOINT:
{
2012-11-26 18:58:24 +00:00
sprintf( str, "joint( \"%s\" )", joint1.c_str() );
break;
}
case idAFVector::VEC_BONECENTER:
{
2012-11-26 18:58:24 +00:00
sprintf( str, "bonecenter( \"%s\", \"%s\" )", joint1.c_str(), joint2.c_str() );
break;
}
case idAFVector::VEC_BONEDIR:
{
2012-11-26 18:58:24 +00:00
sprintf( str, "bonedir( \"%s\", \"%s\" )", joint1.c_str(), joint2.c_str() );
break;
}
default:
{
2012-11-26 18:58:24 +00:00
break;
}
}
if( negate )
{
2012-11-26 18:58:24 +00:00
str = "-" + str;
}
return str.c_str();
}
/*
================
idDeclAF_Body::SetDefault
================
*/
void idDeclAF_Body::SetDefault( const idDeclAF* file )
{
2012-11-26 18:58:24 +00:00
name = "noname";
modelType = TRM_BOX;
v1.type = idAFVector::VEC_COORDS;
v1.ToVec3().x = v1.ToVec3().y = v1.ToVec3().z = -10.0f;
v2.type = idAFVector::VEC_COORDS;
v2.ToVec3().x = v2.ToVec3().y = v2.ToVec3().z = 10.0f;
numSides = 3;
origin.ToVec3().Zero();
angles.Zero();
density = 0.2f;
inertiaScale.Identity();
linearFriction = file->defaultLinearFriction;
angularFriction = file->defaultAngularFriction;
contactFriction = file->defaultContactFriction;
contents = file->contents;
clipMask = file->clipMask;
selfCollision = file->selfCollision;
frictionDirection.ToVec3().Zero();
contactMotorDirection.ToVec3().Zero();
jointName = "origin";
jointMod = DECLAF_JOINTMOD_AXIS;
containedJoints = "*origin";
}
/*
================
idDeclAF_Constraint::SetDefault
================
*/
void idDeclAF_Constraint::SetDefault( const idDeclAF* file )
{
2012-11-26 18:58:24 +00:00
name = "noname";
type = DECLAF_CONSTRAINT_UNIVERSALJOINT;
if( file->bodies.Num() )
{
2012-11-26 18:58:24 +00:00
body1 = file->bodies[0]->name;
}
else
{
2012-11-26 18:58:24 +00:00
body1 = "world";
}
body2 = "world";
friction = file->defaultConstraintFriction;
anchor.ToVec3().Zero();
anchor2.ToVec3().Zero();
axis.ToVec3().Set( 1.0f, 0.0f, 0.0f );
shaft[0].ToVec3().Set( 0.0f, 0.0f, -1.0f );
shaft[1].ToVec3().Set( 0.0f, 0.0f, 1.0f );
limit = idDeclAF_Constraint::LIMIT_NONE;
limitAngles[0] =
limitAngles[1] =
limitAngles[2] = 0.0f;
2012-11-26 18:58:24 +00:00
limitAxis.ToVec3().Set( 0.0f, 0.0f, -1.0f );
}
/*
================
idDeclAF::WriteBody
================
*/
bool idDeclAF::WriteBody( idFile* f, const idDeclAF_Body& body ) const
{
2012-11-26 18:58:24 +00:00
idStr str;
2012-11-26 18:58:24 +00:00
f->WriteFloatString( "\nbody \"%s\" {\n", body.name.c_str() );
f->WriteFloatString( "\tjoint \"%s\"\n", body.jointName.c_str() );
f->WriteFloatString( "\tmod %s\n", JointModToString( body.jointMod ) );
switch( body.modelType )
{
case TRM_BOX:
{
f->WriteFloatString( "\tmodel box( " );
2012-11-26 18:58:24 +00:00
body.v1.Write( f );
f->WriteFloatString( ", " );
body.v2.Write( f );
f->WriteFloatString( " )\n" );
break;
}
case TRM_OCTAHEDRON:
{
f->WriteFloatString( "\tmodel octahedron( " );
2012-11-26 18:58:24 +00:00
body.v1.Write( f );
f->WriteFloatString( ", " );
body.v2.Write( f );
f->WriteFloatString( " )\n" );
break;
}
case TRM_DODECAHEDRON:
{
f->WriteFloatString( "\tmodel dodecahedron( " );
2012-11-26 18:58:24 +00:00
body.v1.Write( f );
f->WriteFloatString( ", " );
body.v2.Write( f );
f->WriteFloatString( " )\n" );
break;
}
case TRM_CYLINDER:
{
f->WriteFloatString( "\tmodel cylinder( " );
2012-11-26 18:58:24 +00:00
body.v1.Write( f );
f->WriteFloatString( ", " );
body.v2.Write( f );
f->WriteFloatString( ", %d )\n", body.numSides );
break;
}
case TRM_CONE:
{
f->WriteFloatString( "\tmodel cone( " );
2012-11-26 18:58:24 +00:00
body.v1.Write( f );
f->WriteFloatString( ", " );
body.v2.Write( f );
f->WriteFloatString( ", %d )\n", body.numSides );
break;
}
case TRM_BONE:
{
f->WriteFloatString( "\tmodel bone( " );
2012-11-26 18:58:24 +00:00
body.v1.Write( f );
f->WriteFloatString( ", " );
body.v2.Write( f );
f->WriteFloatString( ", %f )\n", body.width );
break;
}
default:
assert( 0 );
break;
}
f->WriteFloatString( "\torigin " );
body.origin.Write( f );
f->WriteFloatString( "\n" );
if( body.angles != ang_zero )
{
2012-11-26 18:58:24 +00:00
f->WriteFloatString( "\tangles ( %f, %f, %f )\n", body.angles.pitch, body.angles.yaw, body.angles.roll );
}
f->WriteFloatString( "\tdensity %f\n", body.density );
if( body.inertiaScale != mat3_identity )
{
const idMat3& ic = body.inertiaScale;
2012-11-26 18:58:24 +00:00
f->WriteFloatString( "\tinertiaScale (%f %f %f %f %f %f %f %f %f)\n",
ic[0][0], ic[0][1], ic[0][2],
ic[1][0], ic[1][1], ic[1][2],
ic[2][0], ic[2][1], ic[2][2] );
2012-11-26 18:58:24 +00:00
}
if( body.linearFriction != -1 )
{
2012-11-26 18:58:24 +00:00
f->WriteFloatString( "\tfriction %f, %f, %f\n", body.linearFriction, body.angularFriction, body.contactFriction );
}
f->WriteFloatString( "\tcontents %s\n", ContentsToString( body.contents, str ) );
f->WriteFloatString( "\tclipMask %s\n", ContentsToString( body.clipMask, str ) );
f->WriteFloatString( "\tselfCollision %d\n", body.selfCollision );
if( body.frictionDirection.ToVec3() != vec3_origin )
{
2012-11-26 18:58:24 +00:00
f->WriteFloatString( "\tfrictionDirection " );
body.frictionDirection.Write( f );
f->WriteFloatString( "\n" );
}
if( body.contactMotorDirection.ToVec3() != vec3_origin )
{
2012-11-26 18:58:24 +00:00
f->WriteFloatString( "\tcontactMotorDirection " );
body.contactMotorDirection.Write( f );
f->WriteFloatString( "\n" );
}
f->WriteFloatString( "\tcontainedJoints \"%s\"\n", body.containedJoints.c_str() );
f->WriteFloatString( "}\n" );
return true;
}
/*
================
idDeclAF::WriteFixed
================
*/
bool idDeclAF::WriteFixed( idFile* f, const idDeclAF_Constraint& c ) const
{
2012-11-26 18:58:24 +00:00
f->WriteFloatString( "\nfixed \"%s\" {\n", c.name.c_str() );
f->WriteFloatString( "\tbody1 \"%s\"\n", c.body1.c_str() );
f->WriteFloatString( "\tbody2 \"%s\"\n", c.body2.c_str() );
f->WriteFloatString( "}\n" );
return true;
}
/*
================
idDeclAF::WriteBallAndSocketJoint
================
*/
bool idDeclAF::WriteBallAndSocketJoint( idFile* f, const idDeclAF_Constraint& c ) const
{
2012-11-26 18:58:24 +00:00
f->WriteFloatString( "\nballAndSocketJoint \"%s\" {\n", c.name.c_str() );
f->WriteFloatString( "\tbody1 \"%s\"\n", c.body1.c_str() );
f->WriteFloatString( "\tbody2 \"%s\"\n", c.body2.c_str() );
f->WriteFloatString( "\tanchor " );
c.anchor.Write( f );
f->WriteFloatString( "\n" );
f->WriteFloatString( "\tfriction %f\n", c.friction );
if( c.limit == idDeclAF_Constraint::LIMIT_CONE )
{
2012-11-26 18:58:24 +00:00
f->WriteFloatString( "\tconeLimit " );
c.limitAxis.Write( f );
f->WriteFloatString( ", %f, ", c.limitAngles[0] );
c.shaft[0].Write( f );
f->WriteFloatString( "\n" );
}
else if( c.limit == idDeclAF_Constraint::LIMIT_PYRAMID )
{
2012-11-26 18:58:24 +00:00
f->WriteFloatString( "\tpyramidLimit " );
c.limitAxis.Write( f );
f->WriteFloatString( ", %f, %f, %f, ", c.limitAngles[0], c.limitAngles[1], c.limitAngles[2] );
c.shaft[0].Write( f );
f->WriteFloatString( "\n" );
}
f->WriteFloatString( "}\n" );
return true;
}
/*
================
idDeclAF::WriteUniversalJoint
================
*/
bool idDeclAF::WriteUniversalJoint( idFile* f, const idDeclAF_Constraint& c ) const
{
2012-11-26 18:58:24 +00:00
f->WriteFloatString( "\nuniversalJoint \"%s\" {\n", c.name.c_str() );
f->WriteFloatString( "\tbody1 \"%s\"\n", c.body1.c_str() );
f->WriteFloatString( "\tbody2 \"%s\"\n", c.body2.c_str() );
f->WriteFloatString( "\tanchor " );
c.anchor.Write( f );
f->WriteFloatString( "\n" );
f->WriteFloatString( "\tshafts " );
c.shaft[0].Write( f );
f->WriteFloatString( ", " );
c.shaft[1].Write( f );
f->WriteFloatString( "\n" );
f->WriteFloatString( "\tfriction %f\n", c.friction );
if( c.limit == idDeclAF_Constraint::LIMIT_CONE )
{
2012-11-26 18:58:24 +00:00
f->WriteFloatString( "\tconeLimit " );
c.limitAxis.Write( f );
f->WriteFloatString( ", %f\n", c.limitAngles[0] );
}
else if( c.limit == idDeclAF_Constraint::LIMIT_PYRAMID )
{
2012-11-26 18:58:24 +00:00
f->WriteFloatString( "\tpyramidLimit " );
c.limitAxis.Write( f );
f->WriteFloatString( ", %f, %f, %f\n", c.limitAngles[0], c.limitAngles[1], c.limitAngles[2] );
}
f->WriteFloatString( "}\n" );
return true;
}
/*
================
idDeclAF::WriteHinge
================
*/
bool idDeclAF::WriteHinge( idFile* f, const idDeclAF_Constraint& c ) const
{
2012-11-26 18:58:24 +00:00
f->WriteFloatString( "\nhinge \"%s\" {\n", c.name.c_str() );
f->WriteFloatString( "\tbody1 \"%s\"\n", c.body1.c_str() );
f->WriteFloatString( "\tbody2 \"%s\"\n", c.body2.c_str() );
f->WriteFloatString( "\tanchor " );
c.anchor.Write( f );
f->WriteFloatString( "\n" );
f->WriteFloatString( "\taxis " );
c.axis.Write( f );
f->WriteFloatString( "\n" );
f->WriteFloatString( "\tfriction %f\n", c.friction );
if( c.limit == idDeclAF_Constraint::LIMIT_CONE )
{
2012-11-26 18:58:24 +00:00
f->WriteFloatString( "\tlimit " );
f->WriteFloatString( "%f, %f, %f", c.limitAngles[0], c.limitAngles[1], c.limitAngles[2] );
f->WriteFloatString( "\n" );
}
f->WriteFloatString( "}\n" );
return true;
}
/*
================
idDeclAF::WriteSlider
================
*/
bool idDeclAF::WriteSlider( idFile* f, const idDeclAF_Constraint& c ) const
{
2012-11-26 18:58:24 +00:00
f->WriteFloatString( "\nslider \"%s\" {\n", c.name.c_str() );
f->WriteFloatString( "\tbody1 \"%s\"\n", c.body1.c_str() );
f->WriteFloatString( "\tbody2 \"%s\"\n", c.body2.c_str() );
f->WriteFloatString( "\taxis " );
c.axis.Write( f );
f->WriteFloatString( "\n" );
f->WriteFloatString( "\tfriction %f\n", c.friction );
f->WriteFloatString( "}\n" );
return true;
}
/*
================
idDeclAF::WriteSpring
================
*/
bool idDeclAF::WriteSpring( idFile* f, const idDeclAF_Constraint& c ) const
{
2012-11-26 18:58:24 +00:00
f->WriteFloatString( "\nspring \"%s\" {\n", c.name.c_str() );
f->WriteFloatString( "\tbody1 \"%s\"\n", c.body1.c_str() );
f->WriteFloatString( "\tbody2 \"%s\"\n", c.body2.c_str() );
f->WriteFloatString( "\tanchor1 " );
c.anchor.Write( f );
f->WriteFloatString( "\n" );
f->WriteFloatString( "\tanchor2 " );
c.anchor2.Write( f );
f->WriteFloatString( "\n" );
f->WriteFloatString( "\tfriction %f\n", c.friction );
f->WriteFloatString( "\tstretch %f\n", c.stretch );
f->WriteFloatString( "\tcompress %f\n", c.compress );
f->WriteFloatString( "\tdamping %f\n", c.damping );
f->WriteFloatString( "\trestLength %f\n", c.restLength );
f->WriteFloatString( "\tminLength %f\n", c.minLength );
f->WriteFloatString( "\tmaxLength %f\n", c.maxLength );
f->WriteFloatString( "}\n" );
return true;
}
/*
================
idDeclAF::WriteConstraint
================
*/
bool idDeclAF::WriteConstraint( idFile* f, const idDeclAF_Constraint& c ) const
{
switch( c.type )
{
2012-11-26 18:58:24 +00:00
case DECLAF_CONSTRAINT_FIXED:
return WriteFixed( f, c );
case DECLAF_CONSTRAINT_BALLANDSOCKETJOINT:
return WriteBallAndSocketJoint( f, c );
case DECLAF_CONSTRAINT_UNIVERSALJOINT:
return WriteUniversalJoint( f, c );
case DECLAF_CONSTRAINT_HINGE:
return WriteHinge( f, c );
case DECLAF_CONSTRAINT_SLIDER:
return WriteSlider( f, c );
case DECLAF_CONSTRAINT_SPRING:
return WriteSpring( f, c );
default:
break;
}
return false;
}
/*
================
idDeclAF::WriteSettings
================
*/
bool idDeclAF::WriteSettings( idFile* f ) const
{
2012-11-26 18:58:24 +00:00
idStr str;
2012-11-26 18:58:24 +00:00
f->WriteFloatString( "\nsettings {\n" );
f->WriteFloatString( "\tmodel \"%s\"\n", model.c_str() );
f->WriteFloatString( "\tskin \"%s\"\n", skin.c_str() );
f->WriteFloatString( "\tfriction %f, %f, %f, %f\n", defaultLinearFriction, defaultAngularFriction, defaultContactFriction, defaultConstraintFriction );
f->WriteFloatString( "\tsuspendSpeed %f, %f, %f, %f\n", suspendVelocity[0], suspendVelocity[1], suspendAcceleration[0], suspendAcceleration[1] );
f->WriteFloatString( "\tnoMoveTime %f\n", noMoveTime );
f->WriteFloatString( "\tnoMoveTranslation %f\n", noMoveTranslation );
f->WriteFloatString( "\tnoMoveRotation %f\n", noMoveRotation );
f->WriteFloatString( "\tminMoveTime %f\n", minMoveTime );
f->WriteFloatString( "\tmaxMoveTime %f\n", maxMoveTime );
f->WriteFloatString( "\ttotalMass %f\n", totalMass );
f->WriteFloatString( "\tcontents %s\n", ContentsToString( contents, str ) );
f->WriteFloatString( "\tclipMask %s\n", ContentsToString( clipMask, str ) );
f->WriteFloatString( "\tselfCollision %d\n", selfCollision );
f->WriteFloatString( "}\n" );
return true;
}
/*
================
idDeclAF::RebuildTextSource
================
*/
bool idDeclAF::RebuildTextSource()
{
2012-11-26 18:58:24 +00:00
int i;
idFile_Memory f;
f.WriteFloatString( "\n\n/*\n"
2012-11-26 18:58:24 +00:00
"\tGenerated by the Articulated Figure Editor.\n"
"\tDo not edit directly but launch the game and type 'editAFs' on the console.\n"
"*/\n" );
2012-11-26 18:58:24 +00:00
f.WriteFloatString( "\narticulatedFigure %s {\n", GetName() );
if( !WriteSettings( &f ) )
{
2012-11-26 18:58:24 +00:00
return false;
}
for( i = 0; i < bodies.Num(); i++ )
{
if( !WriteBody( &f, *bodies[i] ) )
{
2012-11-26 18:58:24 +00:00
return false;
}
}
for( i = 0; i < constraints.Num(); i++ )
{
if( !WriteConstraint( &f, *constraints[i] ) )
{
2012-11-26 18:58:24 +00:00
return false;
}
}
2012-11-26 18:58:24 +00:00
f.WriteFloatString( "\n}" );
2012-11-26 18:58:24 +00:00
SetText( f.GetDataPtr() );
2012-11-26 18:58:24 +00:00
return true;
}
/*
================
idDeclAF::Save
================
*/
bool idDeclAF::Save()
{
2012-11-26 18:58:24 +00:00
RebuildTextSource();
ReplaceSourceFileText();
modified = false;
return true;
}
/*
================
idDeclAF::ContentsFromString
================
*/
int idDeclAF::ContentsFromString( const char* str )
{
2012-11-26 18:58:24 +00:00
int c;
idToken token;
idLexer src( str, idStr::Length( str ), "idDeclAF::ContentsFromString" );
2012-11-26 18:58:24 +00:00
c = 0;
while( src.ReadToken( &token ) )
{
if( token.Icmp( "none" ) == 0 )
{
2012-11-26 18:58:24 +00:00
c = 0;
}
else if( token.Icmp( "solid" ) == 0 )
{
2012-11-26 18:58:24 +00:00
c |= CONTENTS_SOLID;
}
else if( token.Icmp( "body" ) == 0 )
{
2012-11-26 18:58:24 +00:00
c |= CONTENTS_BODY;
}
else if( token.Icmp( "corpse" ) == 0 )
{
2012-11-26 18:58:24 +00:00
c |= CONTENTS_CORPSE;
}
else if( token.Icmp( "playerclip" ) == 0 )
{
2012-11-26 18:58:24 +00:00
c |= CONTENTS_PLAYERCLIP;
}
else if( token.Icmp( "monsterclip" ) == 0 )
{
2012-11-26 18:58:24 +00:00
c |= CONTENTS_MONSTERCLIP;
}
else if( token == "," )
{
2012-11-26 18:58:24 +00:00
continue;
}
else
{
2012-11-26 18:58:24 +00:00
return c;
}
}
return c;
}
/*
================
idDeclAF::ContentsToString
================
*/
const char* idDeclAF::ContentsToString( const int contents, idStr& str )
{
2012-11-26 18:58:24 +00:00
str = "";
if( contents & CONTENTS_SOLID )
{
if( str.Length() ) str += ", ";
2012-11-26 18:58:24 +00:00
str += "solid";
}
if( contents & CONTENTS_BODY )
{
if( str.Length() ) str += ", ";
2012-11-26 18:58:24 +00:00
str += "body";
}
if( contents & CONTENTS_CORPSE )
{
if( str.Length() ) str += ", ";
2012-11-26 18:58:24 +00:00
str += "corpse";
}
if( contents & CONTENTS_PLAYERCLIP )
{
if( str.Length() ) str += ", ";
2012-11-26 18:58:24 +00:00
str += "playerclip";
}
if( contents & CONTENTS_MONSTERCLIP )
{
if( str.Length() ) str += ", ";
2012-11-26 18:58:24 +00:00
str += "monsterclip";
}
if( str[0] == '\0' )
{
2012-11-26 18:58:24 +00:00
str = "none";
}
return str.c_str();
}
/*
================
idDeclAF::JointModFromString
================
*/
declAFJointMod_t idDeclAF::JointModFromString( const char* str )
{
if( idStr::Icmp( str, "orientation" ) == 0 )
{
2012-11-26 18:58:24 +00:00
return DECLAF_JOINTMOD_AXIS;
}
if( idStr::Icmp( str, "position" ) == 0 )
{
2012-11-26 18:58:24 +00:00
return DECLAF_JOINTMOD_ORIGIN;
}
if( idStr::Icmp( str, "both" ) == 0 )
{
2012-11-26 18:58:24 +00:00
return DECLAF_JOINTMOD_BOTH;
}
return DECLAF_JOINTMOD_AXIS;
}
/*
================
idDeclAF::JointModToString
================
*/
const char* idDeclAF::JointModToString( declAFJointMod_t jointMod )
{
switch( jointMod )
{
case DECLAF_JOINTMOD_AXIS:
{
2012-11-26 18:58:24 +00:00
return "orientation";
}
case DECLAF_JOINTMOD_ORIGIN:
{
2012-11-26 18:58:24 +00:00
return "position";
}
case DECLAF_JOINTMOD_BOTH:
{
2012-11-26 18:58:24 +00:00
return "both";
}
}
return "orientation";
}
/*
=================
idDeclAF::Size
=================
*/
size_t idDeclAF::Size() const
{
2012-11-26 18:58:24 +00:00
return sizeof( idDeclAF );
}
/*
================
idDeclAF::ParseContents
================
*/
bool idDeclAF::ParseContents( idLexer& src, int& c ) const
{
2012-11-26 18:58:24 +00:00
idToken token;
idStr str;
while( src.ReadToken( &token ) )
{
2012-11-26 18:58:24 +00:00
str += token;
if( !src.CheckTokenString( "," ) )
{
2012-11-26 18:58:24 +00:00
break;
}
str += ",";
}
c = ContentsFromString( str );
return true;
}
/*
================
idDeclAF::ParseBody
================
*/
bool idDeclAF::ParseBody( idLexer& src )
{
2012-11-26 18:58:24 +00:00
bool hasJoint = false;
idToken token;
idAFVector angles;
idDeclAF_Body* body = new( TAG_DECL ) idDeclAF_Body;
2012-11-26 18:58:24 +00:00
bodies.Alloc() = body;
2012-11-26 18:58:24 +00:00
body->SetDefault( this );
if( !src.ExpectTokenType( TT_STRING, 0, &token ) ||
!src.ExpectTokenString( "{" ) )
{
2012-11-26 18:58:24 +00:00
return false;
}
2012-11-26 18:58:24 +00:00
body->name = token;
if( !body->name.Icmp( "origin" ) || !body->name.Icmp( "world" ) )
{
2012-11-26 18:58:24 +00:00
src.Error( "a body may not be named \"origin\" or \"world\"" );
return false;
}
while( src.ReadToken( &token ) )
{
if( !token.Icmp( "model" ) )
{
if( !src.ExpectTokenType( TT_NAME, 0, &token ) )
{
2012-11-26 18:58:24 +00:00
return false;
}
if( !token.Icmp( "box" ) )
{
2012-11-26 18:58:24 +00:00
body->modelType = TRM_BOX;
if( !src.ExpectTokenString( "(" ) ||
!body->v1.Parse( src ) ||
!src.ExpectTokenString( "," ) ||
!body->v2.Parse( src ) ||
!src.ExpectTokenString( ")" ) )
{
2012-11-26 18:58:24 +00:00
return false;
}
}
else if( !token.Icmp( "octahedron" ) )
{
2012-11-26 18:58:24 +00:00
body->modelType = TRM_OCTAHEDRON;
if( !src.ExpectTokenString( "(" ) ||
!body->v1.Parse( src ) ||
!src.ExpectTokenString( "," ) ||
!body->v2.Parse( src ) ||
!src.ExpectTokenString( ")" ) )
{
2012-11-26 18:58:24 +00:00
return false;
}
}
else if( !token.Icmp( "dodecahedron" ) )
{
2012-11-26 18:58:24 +00:00
body->modelType = TRM_DODECAHEDRON;
if( !src.ExpectTokenString( "(" ) ||
!body->v1.Parse( src ) ||
!src.ExpectTokenString( "," ) ||
!body->v2.Parse( src ) ||
!src.ExpectTokenString( ")" ) )
{
2012-11-26 18:58:24 +00:00
return false;
}
}
else if( !token.Icmp( "cylinder" ) )
{
2012-11-26 18:58:24 +00:00
body->modelType = TRM_CYLINDER;
if( !src.ExpectTokenString( "(" ) ||
!body->v1.Parse( src ) ||
!src.ExpectTokenString( "," ) ||
!body->v2.Parse( src ) ||
!src.ExpectTokenString( "," ) )
{
2012-11-26 18:58:24 +00:00
return false;
}
body->numSides = src.ParseInt();
if( !src.ExpectTokenString( ")" ) )
{
2012-11-26 18:58:24 +00:00
return false;
}
}
else if( !token.Icmp( "cone" ) )
{
2012-11-26 18:58:24 +00:00
body->modelType = TRM_CONE;
if( !src.ExpectTokenString( "(" ) ||
!body->v1.Parse( src ) ||
!src.ExpectTokenString( "," ) ||
!body->v2.Parse( src ) ||
!src.ExpectTokenString( "," ) )
{
2012-11-26 18:58:24 +00:00
return false;
}
body->numSides = src.ParseInt();
if( !src.ExpectTokenString( ")" ) )
{
2012-11-26 18:58:24 +00:00
return false;
}
}
else if( !token.Icmp( "bone" ) )
{
2012-11-26 18:58:24 +00:00
body->modelType = TRM_BONE;
if( !src.ExpectTokenString( "(" ) ||
!body->v1.Parse( src ) ||
!src.ExpectTokenString( "," ) ||
!body->v2.Parse( src ) ||
!src.ExpectTokenString( "," ) )
{
2012-11-26 18:58:24 +00:00
return false;
}
body->width = src.ParseFloat();
if( !src.ExpectTokenString( ")" ) )
{
2012-11-26 18:58:24 +00:00
return false;
}
}
else if( !token.Icmp( "custom" ) )
{
2012-11-26 18:58:24 +00:00
src.Error( "custom models not yet implemented" );
return false;
}
else
{
2012-11-26 18:58:24 +00:00
src.Error( "unkown model type %s", token.c_str() );
return false;
}
}
else if( !token.Icmp( "origin" ) )
{
if( !body->origin.Parse( src ) )
{
2012-11-26 18:58:24 +00:00
return false;
}
}
else if( !token.Icmp( "angles" ) )
{
if( !angles.Parse( src ) )
{
2012-11-26 18:58:24 +00:00
return false;
}
body->angles = idAngles( angles.ToVec3().x, angles.ToVec3().y, angles.ToVec3().z );
}
else if( !token.Icmp( "joint" ) )
{
if( !src.ExpectTokenType( TT_STRING, 0, &token ) )
{
2012-11-26 18:58:24 +00:00
return false;
}
body->jointName = token;
hasJoint = true;
}
else if( !token.Icmp( "mod" ) )
{
if( !src.ExpectAnyToken( &token ) )
{
2012-11-26 18:58:24 +00:00
return false;
}
body->jointMod = JointModFromString( token.c_str() );
}
else if( !token.Icmp( "density" ) )
{
2012-11-26 18:58:24 +00:00
body->density = src.ParseFloat();
}
else if( !token.Icmp( "inertiaScale" ) )
{
2012-11-26 18:58:24 +00:00
src.Parse1DMatrix( 9, body->inertiaScale[0].ToFloatPtr() );
}
else if( !token.Icmp( "friction" ) )
{
2012-11-26 18:58:24 +00:00
body->linearFriction = src.ParseFloat();
src.ExpectTokenString( "," );
body->angularFriction = src.ParseFloat();
src.ExpectTokenString( "," );
body->contactFriction = src.ParseFloat();
}
else if( !token.Icmp( "contents" ) )
{
2012-11-26 18:58:24 +00:00
ParseContents( src, body->contents );
}
else if( !token.Icmp( "clipMask" ) )
{
2012-11-26 18:58:24 +00:00
ParseContents( src, body->clipMask );
body->clipMask &= ~CONTENTS_CORPSE; // never allow collisions against corpses
}
else if( !token.Icmp( "selfCollision" ) )
{
2012-11-26 18:58:24 +00:00
body->selfCollision = src.ParseBool();
}
else if( !token.Icmp( "containedjoints" ) )
{
if( !src.ExpectTokenType( TT_STRING, 0, &token ) )
{
2012-11-26 18:58:24 +00:00
return false;
}
body->containedJoints = token;
}
else if( !token.Icmp( "frictionDirection" ) )
{
if( !body->frictionDirection.Parse( src ) )
{
2012-11-26 18:58:24 +00:00
return false;
}
}
else if( !token.Icmp( "contactMotorDirection" ) )
{
if( !body->contactMotorDirection.Parse( src ) )
{
2012-11-26 18:58:24 +00:00
return false;
}
}
else if( token == "}" )
{
2012-11-26 18:58:24 +00:00
break;
}
else
{
2012-11-26 18:58:24 +00:00
src.Error( "unknown token %s in body", token.c_str() );
return false;
}
}
if( body->modelType == TRM_INVALID )
{
2012-11-26 18:58:24 +00:00
src.Error( "no model set for body" );
return false;
}
if( !hasJoint )
{
2012-11-26 18:58:24 +00:00
src.Error( "no joint set for body" );
return false;
}
2012-11-26 18:58:24 +00:00
body->clipMask |= CONTENTS_MOVEABLECLIP;
2012-11-26 18:58:24 +00:00
return true;
}
/*
================
idDeclAF::ParseFixed
================
*/
bool idDeclAF::ParseFixed( idLexer& src )
{
2012-11-26 18:58:24 +00:00
idToken token;
idDeclAF_Constraint* constraint = new( TAG_DECL ) idDeclAF_Constraint;
2012-11-26 18:58:24 +00:00
constraint->SetDefault( this );
constraints.Alloc() = constraint;
if( !src.ExpectTokenType( TT_STRING, 0, &token ) ||
!src.ExpectTokenString( "{" ) )
{
2012-11-26 18:58:24 +00:00
return false;
}
2012-11-26 18:58:24 +00:00
constraint->type = DECLAF_CONSTRAINT_FIXED;
constraint->name = token;
while( src.ReadToken( &token ) )
{
if( !token.Icmp( "body1" ) )
{
2012-11-26 18:58:24 +00:00
src.ExpectTokenType( TT_STRING, 0, &token );
constraint->body1 = token;
}
else if( !token.Icmp( "body2" ) )
{
2012-11-26 18:58:24 +00:00
src.ExpectTokenType( TT_STRING, 0, &token );
constraint->body2 = token;
}
else if( token == "}" )
{
2012-11-26 18:58:24 +00:00
break;
}
else
{
2012-11-26 18:58:24 +00:00
src.Error( "unknown token %s in ball and socket joint", token.c_str() );
return false;
}
}
2012-11-26 18:58:24 +00:00
return true;
}
/*
================
idDeclAF::ParseBallAndSocketJoint
================
*/
bool idDeclAF::ParseBallAndSocketJoint( idLexer& src )
{
2012-11-26 18:58:24 +00:00
idToken token;
idDeclAF_Constraint* constraint = new( TAG_DECL ) idDeclAF_Constraint;
2012-11-26 18:58:24 +00:00
constraint->SetDefault( this );
constraints.Alloc() = constraint;
if( !src.ExpectTokenType( TT_STRING, 0, &token ) ||
!src.ExpectTokenString( "{" ) )
{
2012-11-26 18:58:24 +00:00
return false;
}
2012-11-26 18:58:24 +00:00
constraint->type = DECLAF_CONSTRAINT_BALLANDSOCKETJOINT;
constraint->limit = idDeclAF_Constraint::LIMIT_NONE;
constraint->name = token;
constraint->friction = 0.5f;
constraint->anchor.ToVec3().Zero();
constraint->shaft[0].ToVec3().Zero();
while( src.ReadToken( &token ) )
{
if( !token.Icmp( "body1" ) )
{
2012-11-26 18:58:24 +00:00
src.ExpectTokenType( TT_STRING, 0, &token );
constraint->body1 = token;
}
else if( !token.Icmp( "body2" ) )
{
2012-11-26 18:58:24 +00:00
src.ExpectTokenType( TT_STRING, 0, &token );
constraint->body2 = token;
}
else if( !token.Icmp( "anchor" ) )
{
if( !constraint->anchor.Parse( src ) )
{
2012-11-26 18:58:24 +00:00
return false;
}
}
else if( !token.Icmp( "conelimit" ) )
{
if( !constraint->limitAxis.Parse( src ) ||
!src.ExpectTokenString( "," ) )
{
return false;
2012-11-26 18:58:24 +00:00
}
constraint->limitAngles[0] = src.ParseFloat();
if( !src.ExpectTokenString( "," ) ||
!constraint->shaft[0].Parse( src ) )
{
2012-11-26 18:58:24 +00:00
return false;
}
constraint->limit = idDeclAF_Constraint::LIMIT_CONE;
}
else if( !token.Icmp( "pyramidlimit" ) )
{
if( !constraint->limitAxis.Parse( src ) ||
!src.ExpectTokenString( "," ) )
{
return false;
2012-11-26 18:58:24 +00:00
}
constraint->limitAngles[0] = src.ParseFloat();
if( !src.ExpectTokenString( "," ) )
{
2012-11-26 18:58:24 +00:00
return false;
}
constraint->limitAngles[1] = src.ParseFloat();
if( !src.ExpectTokenString( "," ) )
{
2012-11-26 18:58:24 +00:00
return false;
}
constraint->limitAngles[2] = src.ParseFloat();
if( !src.ExpectTokenString( "," ) ||
!constraint->shaft[0].Parse( src ) )
{
2012-11-26 18:58:24 +00:00
return false;
}
constraint->limit = idDeclAF_Constraint::LIMIT_PYRAMID;
}
else if( !token.Icmp( "friction" ) )
{
2012-11-26 18:58:24 +00:00
constraint->friction = src.ParseFloat();
}
else if( token == "}" )
{
2012-11-26 18:58:24 +00:00
break;
}
else
{
2012-11-26 18:58:24 +00:00
src.Error( "unknown token %s in ball and socket joint", token.c_str() );
return false;
}
}
2012-11-26 18:58:24 +00:00
return true;
}
/*
================
idDeclAF::ParseUniversalJoint
================
*/
bool idDeclAF::ParseUniversalJoint( idLexer& src )
{
2012-11-26 18:58:24 +00:00
idToken token;
idDeclAF_Constraint* constraint = new( TAG_DECL ) idDeclAF_Constraint;
2012-11-26 18:58:24 +00:00
constraint->SetDefault( this );
constraints.Alloc() = constraint;
if( !src.ExpectTokenType( TT_STRING, 0, &token ) ||
!src.ExpectTokenString( "{" ) )
{
2012-11-26 18:58:24 +00:00
return false;
}
2012-11-26 18:58:24 +00:00
constraint->type = DECLAF_CONSTRAINT_UNIVERSALJOINT;
constraint->limit = idDeclAF_Constraint::LIMIT_NONE;
constraint->name = token;
constraint->friction = 0.5f;
constraint->anchor.ToVec3().Zero();
constraint->shaft[0].ToVec3().Zero();
constraint->shaft[1].ToVec3().Zero();
while( src.ReadToken( &token ) )
{
if( !token.Icmp( "body1" ) )
{
2012-11-26 18:58:24 +00:00
src.ExpectTokenType( TT_STRING, 0, &token );
constraint->body1 = token;
}
else if( !token.Icmp( "body2" ) )
{
2012-11-26 18:58:24 +00:00
src.ExpectTokenType( TT_STRING, 0, &token );
constraint->body2 = token;
}
else if( !token.Icmp( "anchor" ) )
{
if( !constraint->anchor.Parse( src ) )
{
2012-11-26 18:58:24 +00:00
return false;
}
}
else if( !token.Icmp( "shafts" ) )
{
if( !constraint->shaft[0].Parse( src ) ||
2012-11-26 18:58:24 +00:00
!src.ExpectTokenString( "," ) ||
!constraint->shaft[1].Parse( src ) )
{
2012-11-26 18:58:24 +00:00
return false;
}
}
else if( !token.Icmp( "conelimit" ) )
{
if( !constraint->limitAxis.Parse( src ) ||
!src.ExpectTokenString( "," ) )
{
return false;
2012-11-26 18:58:24 +00:00
}
constraint->limitAngles[0] = src.ParseFloat();
constraint->limit = idDeclAF_Constraint::LIMIT_CONE;
}
else if( !token.Icmp( "pyramidlimit" ) )
{
if( !constraint->limitAxis.Parse( src ) ||
!src.ExpectTokenString( "," ) )
{
return false;
2012-11-26 18:58:24 +00:00
}
constraint->limitAngles[0] = src.ParseFloat();
if( !src.ExpectTokenString( "," ) )
{
2012-11-26 18:58:24 +00:00
return false;
}
constraint->limitAngles[1] = src.ParseFloat();
if( !src.ExpectTokenString( "," ) )
{
2012-11-26 18:58:24 +00:00
return false;
}
constraint->limitAngles[2] = src.ParseFloat();
constraint->limit = idDeclAF_Constraint::LIMIT_PYRAMID;
}
else if( !token.Icmp( "friction" ) )
{
2012-11-26 18:58:24 +00:00
constraint->friction = src.ParseFloat();
}
else if( token == "}" )
{
2012-11-26 18:58:24 +00:00
break;
}
else
{
2012-11-26 18:58:24 +00:00
src.Error( "unknown token %s in universal joint", token.c_str() );
return false;
}
}
2012-11-26 18:58:24 +00:00
return true;
}
/*
================
idDeclAF::ParseHinge
================
*/
bool idDeclAF::ParseHinge( idLexer& src )
{
2012-11-26 18:58:24 +00:00
idToken token;
idDeclAF_Constraint* constraint = new( TAG_DECL ) idDeclAF_Constraint;
2012-11-26 18:58:24 +00:00
constraint->SetDefault( this );
constraints.Alloc() = constraint;
if( !src.ExpectTokenType( TT_STRING, 0, &token ) ||
!src.ExpectTokenString( "{" ) )
{
2012-11-26 18:58:24 +00:00
return false;
}
2012-11-26 18:58:24 +00:00
constraint->type = DECLAF_CONSTRAINT_HINGE;
constraint->limit = idDeclAF_Constraint::LIMIT_NONE;
constraint->name = token;
constraint->friction = 0.5f;
constraint->anchor.ToVec3().Zero();
constraint->axis.ToVec3().Zero();
while( src.ReadToken( &token ) )
{
if( !token.Icmp( "body1" ) )
{
2012-11-26 18:58:24 +00:00
src.ExpectTokenType( TT_STRING, 0, &token );
constraint->body1 = token;
}
else if( !token.Icmp( "body2" ) )
{
2012-11-26 18:58:24 +00:00
src.ExpectTokenType( TT_STRING, 0, &token );
constraint->body2 = token;
}
else if( !token.Icmp( "anchor" ) )
{
if( !constraint->anchor.Parse( src ) )
{
2012-11-26 18:58:24 +00:00
return false;
}
}
else if( !token.Icmp( "axis" ) )
{
if( !constraint->axis.Parse( src ) )
{
2012-11-26 18:58:24 +00:00
return false;
}
}
else if( !token.Icmp( "limit" ) )
{
2012-11-26 18:58:24 +00:00
constraint->limitAngles[0] = src.ParseFloat();
if( !src.ExpectTokenString( "," ) )
{
2012-11-26 18:58:24 +00:00
return false;
}
constraint->limitAngles[1] = src.ParseFloat();
if( !src.ExpectTokenString( "," ) )
{
2012-11-26 18:58:24 +00:00
return false;
}
constraint->limitAngles[2] = src.ParseFloat();
constraint->limit = idDeclAF_Constraint::LIMIT_CONE;
}
else if( !token.Icmp( "friction" ) )
{
2012-11-26 18:58:24 +00:00
constraint->friction = src.ParseFloat();
}
else if( token == "}" )
{
2012-11-26 18:58:24 +00:00
break;
}
else
{
2012-11-26 18:58:24 +00:00
src.Error( "unknown token %s in hinge", token.c_str() );
return false;
}
}
2012-11-26 18:58:24 +00:00
return true;
}
/*
================
idDeclAF::ParseSlider
================
*/
bool idDeclAF::ParseSlider( idLexer& src )
{
2012-11-26 18:58:24 +00:00
idToken token;
idDeclAF_Constraint* constraint = new( TAG_DECL ) idDeclAF_Constraint;
2012-11-26 18:58:24 +00:00
constraint->SetDefault( this );
constraints.Alloc() = constraint;
if( !src.ExpectTokenType( TT_STRING, 0, &token ) ||
!src.ExpectTokenString( "{" ) )
{
2012-11-26 18:58:24 +00:00
return false;
}
2012-11-26 18:58:24 +00:00
constraint->type = DECLAF_CONSTRAINT_SLIDER;
constraint->limit = idDeclAF_Constraint::LIMIT_NONE;
constraint->name = token;
constraint->friction = 0.5f;
while( src.ReadToken( &token ) )
{
if( !token.Icmp( "body1" ) )
{
2012-11-26 18:58:24 +00:00
src.ExpectTokenType( TT_STRING, 0, &token );
constraint->body1 = token;
}
else if( !token.Icmp( "body2" ) )
{
2012-11-26 18:58:24 +00:00
src.ExpectTokenType( TT_STRING, 0, &token );
constraint->body2 = token;
}
else if( !token.Icmp( "axis" ) )
{
if( !constraint->axis.Parse( src ) )
{
2012-11-26 18:58:24 +00:00
return false;
}
}
else if( !token.Icmp( "friction" ) )
{
2012-11-26 18:58:24 +00:00
constraint->friction = src.ParseFloat();
}
else if( token == "}" )
{
2012-11-26 18:58:24 +00:00
break;
}
else
{
2012-11-26 18:58:24 +00:00
src.Error( "unknown token %s in slider", token.c_str() );
return false;
}
}
2012-11-26 18:58:24 +00:00
return true;
}
/*
================
idDeclAF::ParseSpring
================
*/
bool idDeclAF::ParseSpring( idLexer& src )
{
2012-11-26 18:58:24 +00:00
idToken token;
idDeclAF_Constraint* constraint = new( TAG_DECL ) idDeclAF_Constraint;
2012-11-26 18:58:24 +00:00
constraint->SetDefault( this );
constraints.Alloc() = constraint;
if( !src.ExpectTokenType( TT_STRING, 0, &token ) ||
!src.ExpectTokenString( "{" ) )
{
2012-11-26 18:58:24 +00:00
return false;
}
2012-11-26 18:58:24 +00:00
constraint->type = DECLAF_CONSTRAINT_SPRING;
constraint->limit = idDeclAF_Constraint::LIMIT_NONE;
constraint->name = token;
constraint->friction = 0.5f;
while( src.ReadToken( &token ) )
{
if( !token.Icmp( "body1" ) )
{
2012-11-26 18:58:24 +00:00
src.ExpectTokenType( TT_STRING, 0, &token );
constraint->body1 = token;
}
else if( !token.Icmp( "body2" ) )
{
2012-11-26 18:58:24 +00:00
src.ExpectTokenType( TT_STRING, 0, &token );
constraint->body2 = token;
}
else if( !token.Icmp( "anchor1" ) )
{
if( !constraint->anchor.Parse( src ) )
{
2012-11-26 18:58:24 +00:00
return false;
}
}
else if( !token.Icmp( "anchor2" ) )
{
if( !constraint->anchor2.Parse( src ) )
{
2012-11-26 18:58:24 +00:00
return false;
}
}
else if( !token.Icmp( "friction" ) )
{
2012-11-26 18:58:24 +00:00
constraint->friction = src.ParseFloat();
}
else if( !token.Icmp( "stretch" ) )
{
2012-11-26 18:58:24 +00:00
constraint->stretch = src.ParseFloat();
}
else if( !token.Icmp( "compress" ) )
{
2012-11-26 18:58:24 +00:00
constraint->compress = src.ParseFloat();
}
else if( !token.Icmp( "damping" ) )
{
2012-11-26 18:58:24 +00:00
constraint->damping = src.ParseFloat();
}
else if( !token.Icmp( "restLength" ) )
{
2012-11-26 18:58:24 +00:00
constraint->restLength = src.ParseFloat();
}
else if( !token.Icmp( "minLength" ) )
{
2012-11-26 18:58:24 +00:00
constraint->minLength = src.ParseFloat();
}
else if( !token.Icmp( "maxLength" ) )
{
2012-11-26 18:58:24 +00:00
constraint->maxLength = src.ParseFloat();
}
else if( token == "}" )
{
2012-11-26 18:58:24 +00:00
break;
}
else
{
2012-11-26 18:58:24 +00:00
src.Error( "unknown token %s in spring", token.c_str() );
return false;
}
}
2012-11-26 18:58:24 +00:00
return true;
}
/*
================
idDeclAF::ParseSettings
================
*/
bool idDeclAF::ParseSettings( idLexer& src )
{
2012-11-26 18:58:24 +00:00
idToken token;
if( !src.ExpectTokenString( "{" ) )
{
2012-11-26 18:58:24 +00:00
return false;
}
while( src.ReadToken( &token ) )
{
if( !token.Icmp( "mesh" ) )
{
if( !src.ExpectTokenType( TT_STRING, 0, &token ) )
{
2012-11-26 18:58:24 +00:00
return false;
}
}
else if( !token.Icmp( "anim" ) )
{
if( !src.ExpectTokenType( TT_STRING, 0, &token ) )
{
2012-11-26 18:58:24 +00:00
return false;
}
}
else if( !token.Icmp( "model" ) )
{
if( !src.ExpectTokenType( TT_STRING, 0, &token ) )
{
2012-11-26 18:58:24 +00:00
return false;
}
model = token;
}
else if( !token.Icmp( "skin" ) )
{
if( !src.ExpectTokenType( TT_STRING, 0, &token ) )
{
2012-11-26 18:58:24 +00:00
return false;
}
skin = token;
}
else if( !token.Icmp( "friction" ) )
{
2012-11-26 18:58:24 +00:00
defaultLinearFriction = src.ParseFloat();
if( !src.ExpectTokenString( "," ) )
{
2012-11-26 18:58:24 +00:00
return false;
}
defaultAngularFriction = src.ParseFloat();
if( !src.ExpectTokenString( "," ) )
{
2012-11-26 18:58:24 +00:00
return false;
}
defaultContactFriction = src.ParseFloat();
if( src.CheckTokenString( "," ) )
{
2012-11-26 18:58:24 +00:00
defaultConstraintFriction = src.ParseFloat();
}
}
else if( !token.Icmp( "totalMass" ) )
{
2012-11-26 18:58:24 +00:00
totalMass = src.ParseFloat();
}
else if( !token.Icmp( "suspendSpeed" ) )
{
2012-11-26 18:58:24 +00:00
suspendVelocity[0] = src.ParseFloat();
if( !src.ExpectTokenString( "," ) )
{
2012-11-26 18:58:24 +00:00
return false;
}
suspendVelocity[1] = src.ParseFloat();
if( !src.ExpectTokenString( "," ) )
{
2012-11-26 18:58:24 +00:00
return false;
}
suspendAcceleration[0] = src.ParseFloat();
if( !src.ExpectTokenString( "," ) )
{
2012-11-26 18:58:24 +00:00
return false;
}
suspendAcceleration[1] = src.ParseFloat();
}
else if( !token.Icmp( "noMoveTime" ) )
{
2012-11-26 18:58:24 +00:00
noMoveTime = src.ParseFloat();
}
else if( !token.Icmp( "noMoveTranslation" ) )
{
2012-11-26 18:58:24 +00:00
noMoveTranslation = src.ParseFloat();
}
else if( !token.Icmp( "noMoveRotation" ) )
{
2012-11-26 18:58:24 +00:00
noMoveRotation = src.ParseFloat();
}
else if( !token.Icmp( "minMoveTime" ) )
{
2012-11-26 18:58:24 +00:00
minMoveTime = src.ParseFloat();
}
else if( !token.Icmp( "maxMoveTime" ) )
{
2012-11-26 18:58:24 +00:00
maxMoveTime = src.ParseFloat();
}
else if( !token.Icmp( "contents" ) )
{
2012-11-26 18:58:24 +00:00
ParseContents( src, contents );
}
else if( !token.Icmp( "clipMask" ) )
{
2012-11-26 18:58:24 +00:00
ParseContents( src, clipMask );
clipMask &= ~CONTENTS_CORPSE; // never allow collisions against corpses
}
else if( !token.Icmp( "selfCollision" ) )
{
2012-11-26 18:58:24 +00:00
selfCollision = src.ParseBool();
}
else if( token == "}" )
{
2012-11-26 18:58:24 +00:00
break;
}
else
{
2012-11-26 18:58:24 +00:00
src.Error( "unknown token %s in settings", token.c_str() );
return false;
}
}
2012-11-26 18:58:24 +00:00
return true;
}
/*
================
idDeclAF::Parse
================
*/
bool idDeclAF::Parse( const char* text, const int textLength, bool allowBinaryVersion )
{
2012-11-26 18:58:24 +00:00
int i, j;
idLexer src;
idToken token;
2012-11-26 18:58:24 +00:00
src.LoadMemory( text, textLength, GetFileName(), GetLineNum() );
src.SetFlags( DECL_LEXER_FLAGS );
src.SkipUntilString( "{" );
while( src.ReadToken( &token ) )
{
if( !token.Icmp( "settings" ) )
{
if( !ParseSettings( src ) )
{
2012-11-26 18:58:24 +00:00
return false;
}
}
else if( !token.Icmp( "body" ) )
{
if( !ParseBody( src ) )
{
2012-11-26 18:58:24 +00:00
return false;
}
}
else if( !token.Icmp( "fixed" ) )
{
if( !ParseFixed( src ) )
{
2012-11-26 18:58:24 +00:00
return false;
}
}
else if( !token.Icmp( "ballAndSocketJoint" ) )
{
if( !ParseBallAndSocketJoint( src ) )
{
2012-11-26 18:58:24 +00:00
return false;
}
}
else if( !token.Icmp( "universalJoint" ) )
{
if( !ParseUniversalJoint( src ) )
{
2012-11-26 18:58:24 +00:00
return false;
}
}
else if( !token.Icmp( "hinge" ) )
{
if( !ParseHinge( src ) )
{
2012-11-26 18:58:24 +00:00
return false;
}
}
else if( !token.Icmp( "slider" ) )
{
if( !ParseSlider( src ) )
{
2012-11-26 18:58:24 +00:00
return false;
}
}
else if( !token.Icmp( "spring" ) )
{
if( !ParseSpring( src ) )
{
2012-11-26 18:58:24 +00:00
return false;
}
}
else if( token == "}" )
{
2012-11-26 18:58:24 +00:00
break;
}
else
{
2012-11-26 18:58:24 +00:00
src.Error( "unknown keyword %s", token.c_str() );
return false;
}
}
for( i = 0; i < bodies.Num(); i++ )
{
2012-11-26 18:58:24 +00:00
// check for multiple bodies with the same name
for( j = i + 1; j < bodies.Num(); j++ )
{
if( bodies[i]->name == bodies[j]->name )
{
2012-11-26 18:58:24 +00:00
src.Error( "two bodies with the same name \"%s\"", bodies[i]->name.c_str() );
}
}
}
for( i = 0; i < constraints.Num(); i++ )
{
2012-11-26 18:58:24 +00:00
// check for multiple constraints with the same name
for( j = i + 1; j < constraints.Num(); j++ )
{
if( constraints[i]->name == constraints[j]->name )
{
2012-11-26 18:58:24 +00:00
src.Error( "two constraints with the same name \"%s\"", constraints[i]->name.c_str() );
}
}
// check if there are two valid bodies set
if( constraints[i]->body1 == "" )
{
2012-11-26 18:58:24 +00:00
src.Error( "no valid body1 specified for constraint '%s'", constraints[i]->name.c_str() );
}
if( constraints[i]->body2 == "" )
{
2012-11-26 18:58:24 +00:00
src.Error( "no valid body2 specified for constraint '%s'", constraints[i]->name.c_str() );
}
}
2012-11-26 18:58:24 +00:00
// make sure the body which modifies the origin comes first
for( i = 0; i < bodies.Num(); i++ )
{
if( bodies[i]->jointName == "origin" )
{
if( i != 0 )
{
idDeclAF_Body* b = bodies[0];
2012-11-26 18:58:24 +00:00
bodies[0] = bodies[i];
bodies[i] = b;
}
break;
}
}
2012-11-26 18:58:24 +00:00
return true;
}
/*
================
idDeclAF::DefaultDefinition
================
*/
const char* idDeclAF::DefaultDefinition() const
{
2012-11-26 18:58:24 +00:00
return
"{\n"
"\t" "settings {\n"
"\t\t" "model \"\"\n"
"\t\t" "skin \"\"\n"
"\t\t" "friction 0.01, 0.01, 0.8, 0.5\n"
"\t\t" "suspendSpeed 20, 30, 40, 60\n"
"\t\t" "noMoveTime 1\n"
"\t\t" "noMoveTranslation 10\n"
"\t\t" "noMoveRotation 10\n"
"\t\t" "minMoveTime -1\n"
"\t\t" "maxMoveTime -1\n"
"\t\t" "totalMass -1\n"
"\t\t" "contents corpse\n"
"\t\t" "clipMask solid, corpse\n"
"\t\t" "selfCollision 1\n"
"\t" "}\n"
"\t" "body \"body\" {\n"
"\t\t" "joint \"origin\"\n"
"\t\t" "mod orientation\n"
"\t\t" "model box( ( -10, -10, -10 ), ( 10, 10, 10 ) )\n"
"\t\t" "origin ( 0, 0, 0 )\n"
"\t\t" "density 0.2\n"
"\t\t" "friction 0.01, 0.01, 0.8\n"
"\t\t" "contents corpse\n"
"\t\t" "clipMask solid, corpse\n"
"\t\t" "selfCollision 1\n"
"\t\t" "containedJoints \"*origin\"\n"
"\t" "}\n"
2012-11-26 18:58:24 +00:00
"}\n";
}
/*
================
idDeclAF::FreeData
================
*/
void idDeclAF::FreeData()
{
2012-11-26 18:58:24 +00:00
modified = false;
defaultLinearFriction = 0.01f;
defaultAngularFriction = 0.01f;
defaultContactFriction = 0.8f;
defaultConstraintFriction = 0.5f;
totalMass = -1;
suspendVelocity.Set( 20.0f, 30.0f );
suspendAcceleration.Set( 40.0f, 60.0f );
noMoveTime = 1.0f;
noMoveTranslation = 10.0f;
noMoveRotation = 10.0f;
minMoveTime = -1.0f;
maxMoveTime = -1.0f;
selfCollision = true;
contents = CONTENTS_CORPSE;
clipMask = CONTENTS_SOLID;
bodies.DeleteContents( true );
constraints.DeleteContents( true );
}
/*
================
idDeclAF::Finish
================
*/
void idDeclAF::Finish( const getJointTransform_t GetJointTransform, const idJointMat* frame, void* model ) const
{
2012-11-26 18:58:24 +00:00
int i;
const char* name = GetName();
for( i = 0; i < bodies.Num(); i++ )
{
idDeclAF_Body* body = bodies[i];
2012-11-26 18:58:24 +00:00
body->v1.Finish( name, GetJointTransform, frame, model );
body->v2.Finish( name, GetJointTransform, frame, model );
body->origin.Finish( name, GetJointTransform, frame, model );
body->frictionDirection.Finish( name, GetJointTransform, frame, model );
body->contactMotorDirection.Finish( name, GetJointTransform, frame, model );
}
for( i = 0; i < constraints.Num(); i++ )
{
idDeclAF_Constraint* constraint = constraints[i];
2012-11-26 18:58:24 +00:00
constraint->anchor.Finish( name, GetJointTransform, frame, model );
constraint->anchor2.Finish( name, GetJointTransform, frame, model );
constraint->shaft[0].Finish( name, GetJointTransform, frame, model );
constraint->shaft[1].Finish( name, GetJointTransform, frame, model );
constraint->axis.Finish( name, GetJointTransform, frame, model );
constraint->limitAxis.Finish( name, GetJointTransform, frame, model );
}
}
/*
================
idDeclAF::NewBody
================
*/
void idDeclAF::NewBody( const char* name )
{
idDeclAF_Body* body;
body = new( TAG_DECL ) idDeclAF_Body();
2012-11-26 18:58:24 +00:00
body->SetDefault( this );
body->name = name;
bodies.Append( body );
}
/*
================
idDeclAF::RenameBody
rename the body with the given name and rename
all constraint body references
================
*/
void idDeclAF::RenameBody( const char* oldName, const char* newName )
{
2012-11-26 18:58:24 +00:00
int i;
for( i = 0; i < bodies.Num(); i++ )
{
if( bodies[i]->name.Icmp( oldName ) == 0 )
{
2012-11-26 18:58:24 +00:00
bodies[i]->name = newName;
break;
}
}
for( i = 0; i < constraints.Num(); i++ )
{
if( constraints[i]->body1.Icmp( oldName ) == 0 )
{
2012-11-26 18:58:24 +00:00
constraints[i]->body1 = newName;
}
else if( constraints[i]->body2.Icmp( oldName ) == 0 )
{
2012-11-26 18:58:24 +00:00
constraints[i]->body2 = newName;
}
}
}
/*
================
idDeclAF::DeleteBody
delete the body with the given name and delete
all constraints that reference the body
================
*/
void idDeclAF::DeleteBody( const char* name )
{
2012-11-26 18:58:24 +00:00
int i;
for( i = 0; i < bodies.Num(); i++ )
{
if( bodies[i]->name.Icmp( name ) == 0 )
{
2012-11-26 18:58:24 +00:00
delete bodies[i];
bodies.RemoveIndex( i );
break;
}
}
for( i = 0; i < constraints.Num(); i++ )
{
if( constraints[i]->body1.Icmp( name ) == 0 ||
constraints[i]->body2.Icmp( name ) == 0 )
{
2012-11-26 18:58:24 +00:00
delete constraints[i];
constraints.RemoveIndex( i );
i--;
}
}
}
/*
================
idDeclAF::NewConstraint
================
*/
void idDeclAF::NewConstraint( const char* name )
{
idDeclAF_Constraint* constraint;
constraint = new( TAG_DECL ) idDeclAF_Constraint;
2012-11-26 18:58:24 +00:00
constraint->SetDefault( this );
constraint->name = name;
constraints.Append( constraint );
}
/*
================
idDeclAF::RenameConstraint
================
*/
void idDeclAF::RenameConstraint( const char* oldName, const char* newName )
{
2012-11-26 18:58:24 +00:00
int i;
for( i = 0; i < constraints.Num(); i++ )
{
if( constraints[i]->name.Icmp( oldName ) == 0 )
{
2012-11-26 18:58:24 +00:00
constraints[i]->name = newName;
return;
}
}
}
/*
================
idDeclAF::DeleteConstraint
================
*/
void idDeclAF::DeleteConstraint( const char* name )
{
2012-11-26 18:58:24 +00:00
int i;
for( i = 0; i < constraints.Num(); i++ )
{
if( constraints[i]->name.Icmp( name ) == 0 )
{
2012-11-26 18:58:24 +00:00
delete constraints[i];
constraints.RemoveIndex( i );
return;
}
}
}
/*
================
idDeclAF::idDeclAF
================
*/
idDeclAF::idDeclAF()
{
2012-11-26 18:58:24 +00:00
FreeData();
}
/*
================
idDeclAF::~idDeclAF
================
*/
idDeclAF::~idDeclAF()
{
2012-11-26 18:58:24 +00:00
bodies.DeleteContents( true );
constraints.DeleteContents( true );
}