Portable Half-Life SDK. GoldSource and Xash3D. Crossplatform.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

560 lines
14 KiB

7 years ago
/***
*
* Copyright (c) 1996-2002, Valve LLC. All rights reserved.
*
* This product contains software technology licensed from Id
* Software, Inc. ("Id Technology"). Id Technology (c) 1996 Id Software, Inc.
* All Rights Reserved.
*
* Use, distribution, and modification of this source code and/or resulting
* object code is restricted to non-commercial enhancements to products from
* Valve LLC. All other use, distribution, or modification is prohibited
* without written permission from Valve LLC.
*
****/
8 years ago
7 years ago
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef int BOOL;
8 years ago
#define TRUE 1
7 years ago
#define FALSE 0
// hack into header files that we can ship
typedef int qboolean;
typedef unsigned char byte;
9 years ago
#include "mathlib.h"
7 years ago
#include "const.h"
#include "progdefs.h"
#include "edict.h"
#include "eiface.h"
#include "studio.h"
#ifndef ACTIVITY_H
#include "activity.h"
#endif
#include "activitymap.h"
#ifndef ANIMATION_H
#include "animation.h"
#endif
#ifndef SCRIPTEVENT_H
#include "scriptevent.h"
#endif
#ifndef ENGINECALLBACK_H
#include "enginecallback.h"
#endif
extern globalvars_t *gpGlobals;
#pragma warning( disable : 4244 )
int ExtractBbox( void *pmodel, int sequence, float *mins, float *maxs )
{
studiohdr_t *pstudiohdr;
8 years ago
7 years ago
pstudiohdr = (studiohdr_t *)pmodel;
8 years ago
if( !pstudiohdr )
7 years ago
return 0;
8 years ago
mstudioseqdesc_t *pseqdesc;
7 years ago
8 years ago
pseqdesc = (mstudioseqdesc_t *)( (byte *)pstudiohdr + pstudiohdr->seqindex );
7 years ago
8 years ago
mins[0] = pseqdesc[sequence].bbmin[0];
mins[1] = pseqdesc[sequence].bbmin[1];
mins[2] = pseqdesc[sequence].bbmin[2];
9 years ago
8 years ago
maxs[0] = pseqdesc[sequence].bbmax[0];
maxs[1] = pseqdesc[sequence].bbmax[1];
maxs[2] = pseqdesc[sequence].bbmax[2];
7 years ago
return 1;
}
int LookupActivity( void *pmodel, entvars_t *pev, int activity )
{
studiohdr_t *pstudiohdr;
8 years ago
7 years ago
pstudiohdr = (studiohdr_t *)pmodel;
8 years ago
if( !pstudiohdr )
7 years ago
return 0;
8 years ago
mstudioseqdesc_t *pseqdesc;
7 years ago
8 years ago
pseqdesc = (mstudioseqdesc_t *)( (byte *)pstudiohdr + pstudiohdr->seqindex );
7 years ago
int weighttotal = 0;
int seq = ACTIVITY_NOT_AVAILABLE;
8 years ago
for( int i = 0; i < pstudiohdr->numseq; i++ )
7 years ago
{
8 years ago
if( pseqdesc[i].activity == activity )
7 years ago
{
weighttotal += pseqdesc[i].actweight;
8 years ago
if( !weighttotal || RANDOM_LONG( 0, weighttotal - 1 ) < pseqdesc[i].actweight )
7 years ago
seq = i;
}
}
return seq;
}
int LookupActivityHeaviest( void *pmodel, entvars_t *pev, int activity )
{
studiohdr_t *pstudiohdr;
8 years ago
7 years ago
pstudiohdr = (studiohdr_t *)pmodel;
8 years ago
if( !pstudiohdr )
7 years ago
return 0;
8 years ago
mstudioseqdesc_t *pseqdesc;
7 years ago
8 years ago
pseqdesc = (mstudioseqdesc_t *)( (byte *)pstudiohdr + pstudiohdr->seqindex );
7 years ago
int weight = 0;
int seq = ACTIVITY_NOT_AVAILABLE;
8 years ago
for( int i = 0; i < pstudiohdr->numseq; i++ )
7 years ago
{
8 years ago
if( pseqdesc[i].activity == activity )
7 years ago
{
8 years ago
if( pseqdesc[i].actweight > weight )
7 years ago
{
weight = pseqdesc[i].actweight;
seq = i;
}
}
}
return seq;
}
8 years ago
void GetEyePosition( void *pmodel, float *vecEyePosition )
7 years ago
{
studiohdr_t *pstudiohdr;
8 years ago
7 years ago
pstudiohdr = (studiohdr_t *)pmodel;
8 years ago
if( !pstudiohdr )
7 years ago
{
8 years ago
ALERT( at_console, "GetEyePosition() Can't get pstudiohdr ptr!\n" );
7 years ago
return;
}
8 years ago
VectorCopy( pstudiohdr->eyeposition, vecEyePosition );
7 years ago
}
int LookupSequence( void *pmodel, const char *label )
{
studiohdr_t *pstudiohdr;
8 years ago
7 years ago
pstudiohdr = (studiohdr_t *)pmodel;
8 years ago
if( !pstudiohdr )
7 years ago
return 0;
8 years ago
mstudioseqdesc_t *pseqdesc;
7 years ago
8 years ago
pseqdesc = (mstudioseqdesc_t *)( (byte *)pstudiohdr + pstudiohdr->seqindex );
7 years ago
8 years ago
for( int i = 0; i < pstudiohdr->numseq; i++ )
7 years ago
{
8 years ago
if( stricmp( pseqdesc[i].label, label ) == 0 )
7 years ago
return i;
}
return -1;
}
int IsSoundEvent( int eventNumber )
{
8 years ago
if( eventNumber == SCRIPT_EVENT_SOUND || eventNumber == SCRIPT_EVENT_SOUND_VOICE )
7 years ago
return 1;
return 0;
}
void SequencePrecache( void *pmodel, const char *pSequenceName )
{
int index = LookupSequence( pmodel, pSequenceName );
8 years ago
if( index >= 0 )
7 years ago
{
studiohdr_t *pstudiohdr;
8 years ago
7 years ago
pstudiohdr = (studiohdr_t *)pmodel;
8 years ago
if( !pstudiohdr || index >= pstudiohdr->numseq )
7 years ago
return;
8 years ago
mstudioseqdesc_t *pseqdesc;
mstudioevent_t *pevent;
7 years ago
8 years ago
pseqdesc = (mstudioseqdesc_t *)( (byte *)pstudiohdr + pstudiohdr->seqindex ) + index;
pevent = (mstudioevent_t *)( (byte *)pstudiohdr + pseqdesc->eventindex );
7 years ago
8 years ago
for( int i = 0; i < pseqdesc->numevents; i++ )
7 years ago
{
// Don't send client-side events to the server AI
8 years ago
if( pevent[i].event >= EVENT_CLIENT )
7 years ago
continue;
// UNDONE: Add a callback to check to see if a sound is precached yet and don't allocate a copy
// of it's name if it is.
8 years ago
if( IsSoundEvent( pevent[i].event ) )
7 years ago
{
if( pevent[i].options[0] == '\0' )
7 years ago
{
ALERT( at_error, "Bad sound event %d in sequence %s :: %s (sound is \"%s\")\n", pevent[i].event, pstudiohdr->name, pSequenceName, pevent[i].options );
}
PRECACHE_SOUND( gpGlobals->pStringBase + ALLOC_STRING( pevent[i].options ) );
7 years ago
}
}
}
}
void GetSequenceInfo( void *pmodel, entvars_t *pev, float *pflFrameRate, float *pflGroundSpeed )
{
studiohdr_t *pstudiohdr;
8 years ago
7 years ago
pstudiohdr = (studiohdr_t *)pmodel;
8 years ago
if( !pstudiohdr )
7 years ago
return;
8 years ago
mstudioseqdesc_t *pseqdesc;
7 years ago
8 years ago
if( pev->sequence >= pstudiohdr->numseq )
7 years ago
{
*pflFrameRate = 0.0;
*pflGroundSpeed = 0.0;
return;
}
8 years ago
pseqdesc = (mstudioseqdesc_t *)( (byte *)pstudiohdr + pstudiohdr->seqindex ) + (int)pev->sequence;
7 years ago
8 years ago
if( pseqdesc->numframes > 1 )
7 years ago
{
8 years ago
*pflFrameRate = 256 * pseqdesc->fps / ( pseqdesc->numframes - 1 );
*pflGroundSpeed = sqrt( pseqdesc->linearmovement[0] * pseqdesc->linearmovement[0] + pseqdesc->linearmovement[1] * pseqdesc->linearmovement[1] + pseqdesc->linearmovement[2] * pseqdesc->linearmovement[2] );
*pflGroundSpeed = *pflGroundSpeed * pseqdesc->fps / ( pseqdesc->numframes - 1 );
7 years ago
}
else
{
*pflFrameRate = 256.0;
*pflGroundSpeed = 0.0;
}
}
int GetSequenceFlags( void *pmodel, entvars_t *pev )
{
studiohdr_t *pstudiohdr;
8 years ago
7 years ago
pstudiohdr = (studiohdr_t *)pmodel;
8 years ago
if( !pstudiohdr || pev->sequence >= pstudiohdr->numseq )
7 years ago
return 0;
8 years ago
mstudioseqdesc_t *pseqdesc;
pseqdesc = (mstudioseqdesc_t *)( (byte *)pstudiohdr + pstudiohdr->seqindex ) + (int)pev->sequence;
7 years ago
return pseqdesc->flags;
}
int GetAnimationEvent( void *pmodel, entvars_t *pev, MonsterEvent_t *pMonsterEvent, float flStart, float flEnd, int index )
{
studiohdr_t *pstudiohdr;
8 years ago
7 years ago
pstudiohdr = (studiohdr_t *)pmodel;
8 years ago
if( !pstudiohdr || pev->sequence >= pstudiohdr->numseq || !pMonsterEvent )
7 years ago
return 0;
8 years ago
mstudioseqdesc_t *pseqdesc;
mstudioevent_t *pevent;
7 years ago
8 years ago
pseqdesc = (mstudioseqdesc_t *)( (byte *)pstudiohdr + pstudiohdr->seqindex ) + (int)pev->sequence;
pevent = (mstudioevent_t *)( (byte *)pstudiohdr + pseqdesc->eventindex );
7 years ago
8 years ago
if( pseqdesc->numevents == 0 || index > pseqdesc->numevents )
7 years ago
return 0;
8 years ago
if( pseqdesc->numframes > 1 )
7 years ago
{
8 years ago
flStart *= ( pseqdesc->numframes - 1 ) / 256.0;
7 years ago
flEnd *= (pseqdesc->numframes - 1) / 256.0;
}
else
{
flStart = 0;
flEnd = 1.0;
}
8 years ago
for( ; index < pseqdesc->numevents; index++ )
7 years ago
{
// Don't send client-side events to the server AI
8 years ago
if( pevent[index].event >= EVENT_CLIENT )
7 years ago
continue;
8 years ago
if( ( pevent[index].frame >= flStart && pevent[index].frame < flEnd ) ||
( ( pseqdesc->flags & STUDIO_LOOPING ) && flEnd >= pseqdesc->numframes - 1 && pevent[index].frame < flEnd - pseqdesc->numframes + 1 ) )
7 years ago
{
pMonsterEvent->event = pevent[index].event;
pMonsterEvent->options = pevent[index].options;
return index + 1;
}
}
return 0;
}
float SetController( void *pmodel, entvars_t *pev, int iController, float flValue )
{
studiohdr_t *pstudiohdr;
int i;
8 years ago
7 years ago
pstudiohdr = (studiohdr_t *)pmodel;
8 years ago
if( !pstudiohdr )
7 years ago
return flValue;
8 years ago
mstudiobonecontroller_t *pbonecontroller = (mstudiobonecontroller_t *)( (byte *)pstudiohdr + pstudiohdr->bonecontrollerindex );
7 years ago
// find first controller that matches the index
8 years ago
for( i = 0; i < pstudiohdr->numbonecontrollers; i++, pbonecontroller++ )
7 years ago
{
8 years ago
if( pbonecontroller->index == iController )
7 years ago
break;
}
8 years ago
if( i >= pstudiohdr->numbonecontrollers )
7 years ago
return flValue;
// wrap 0..360 if it's a rotational controller
8 years ago
if( pbonecontroller->type & ( STUDIO_XR | STUDIO_YR | STUDIO_ZR ) )
7 years ago
{
// ugly hack, invert value if end < start
8 years ago
if( pbonecontroller->end < pbonecontroller->start )
7 years ago
flValue = -flValue;
// does the controller not wrap?
8 years ago
if( pbonecontroller->start + 359.0 >= pbonecontroller->end )
7 years ago
{
8 years ago
if( flValue > ( ( pbonecontroller->start + pbonecontroller->end ) / 2.0 ) + 180 )
7 years ago
flValue = flValue - 360;
8 years ago
if( flValue < ( ( pbonecontroller->start + pbonecontroller->end) / 2.0 ) - 180 )
7 years ago
flValue = flValue + 360;
}
else
{
8 years ago
if( flValue > 360 )
flValue = flValue - (int)( flValue / 360.0 ) * 360.0;
else if( flValue < 0 )
flValue = flValue + (int)( ( flValue / -360.0 ) + 1 ) * 360.0;
7 years ago
}
}
int setting = (int)( 255 * ( flValue - pbonecontroller->start ) / ( pbonecontroller->end - pbonecontroller->start ) );
7 years ago
8 years ago
if( setting < 0 )
setting = 0;
if( setting > 255 )
setting = 255;
7 years ago
pev->controller[iController] = setting;
8 years ago
return setting * ( 1.0 / 255.0 ) * (pbonecontroller->end - pbonecontroller->start ) + pbonecontroller->start;
7 years ago
}
float SetBlending( void *pmodel, entvars_t *pev, int iBlender, float flValue )
{
studiohdr_t *pstudiohdr;
8 years ago
7 years ago
pstudiohdr = (studiohdr_t *)pmodel;
8 years ago
if( !pstudiohdr )
7 years ago
return flValue;
8 years ago
mstudioseqdesc_t *pseqdesc;
7 years ago
8 years ago
pseqdesc = (mstudioseqdesc_t *)( (byte *)pstudiohdr + pstudiohdr->seqindex ) + (int)pev->sequence;
7 years ago
8 years ago
if( pseqdesc->blendtype[iBlender] == 0 )
7 years ago
return flValue;
8 years ago
if( pseqdesc->blendtype[iBlender] & ( STUDIO_XR | STUDIO_YR | STUDIO_ZR ) )
7 years ago
{
// ugly hack, invert value if end < start
8 years ago
if( pseqdesc->blendend[iBlender] < pseqdesc->blendstart[iBlender] )
7 years ago
flValue = -flValue;
// does the controller not wrap?
8 years ago
if( pseqdesc->blendstart[iBlender] + 359.0 >= pseqdesc->blendend[iBlender] )
7 years ago
{
8 years ago
if( flValue > ( ( pseqdesc->blendstart[iBlender] + pseqdesc->blendend[iBlender] ) / 2.0 ) + 180 )
7 years ago
flValue = flValue - 360;
8 years ago
if( flValue < ( ( pseqdesc->blendstart[iBlender] + pseqdesc->blendend[iBlender] ) / 2.0 ) - 180 )
7 years ago
flValue = flValue + 360;
}
}
int setting = (int)( 255 * ( flValue - pseqdesc->blendstart[iBlender] ) / ( pseqdesc->blendend[iBlender] - pseqdesc->blendstart[iBlender] ) );
7 years ago
8 years ago
if( setting < 0 )
setting = 0;
if(setting > 255)
setting = 255;
7 years ago
pev->blending[iBlender] = setting;
8 years ago
return setting * ( 1.0 / 255.0 ) * ( pseqdesc->blendend[iBlender] - pseqdesc->blendstart[iBlender] ) + pseqdesc->blendstart[iBlender];
7 years ago
}
int FindTransition( void *pmodel, int iEndingAnim, int iGoalAnim, int *piDir )
{
studiohdr_t *pstudiohdr;
8 years ago
7 years ago
pstudiohdr = (studiohdr_t *)pmodel;
8 years ago
if( !pstudiohdr )
7 years ago
return iGoalAnim;
8 years ago
mstudioseqdesc_t *pseqdesc;
pseqdesc = (mstudioseqdesc_t *)( (byte *)pstudiohdr + pstudiohdr->seqindex );
7 years ago
// bail if we're going to or from a node 0
8 years ago
if( pseqdesc[iEndingAnim].entrynode == 0 || pseqdesc[iGoalAnim].entrynode == 0 )
7 years ago
{
return iGoalAnim;
}
8 years ago
int iEndNode;
7 years ago
// ALERT( at_console, "from %d to %d: ", pEndNode->iEndNode, pGoalNode->iStartNode );
8 years ago
if( *piDir > 0 )
7 years ago
{
iEndNode = pseqdesc[iEndingAnim].exitnode;
}
else
{
iEndNode = pseqdesc[iEndingAnim].entrynode;
}
8 years ago
if( iEndNode == pseqdesc[iGoalAnim].entrynode )
7 years ago
{
*piDir = 1;
return iGoalAnim;
}
8 years ago
byte *pTransition = ( (byte *)pstudiohdr + pstudiohdr->transitionindex );
7 years ago
8 years ago
int iInternNode = pTransition[( iEndNode - 1 ) * pstudiohdr->numtransitions + ( pseqdesc[iGoalAnim].entrynode - 1 )];
7 years ago
8 years ago
if( iInternNode == 0 )
7 years ago
return iGoalAnim;
int i;
// look for someone going
8 years ago
for( i = 0; i < pstudiohdr->numseq; i++ )
7 years ago
{
8 years ago
if( pseqdesc[i].entrynode == iEndNode && pseqdesc[i].exitnode == iInternNode )
7 years ago
{
*piDir = 1;
return i;
}
8 years ago
if( pseqdesc[i].nodeflags )
7 years ago
{
8 years ago
if( pseqdesc[i].exitnode == iEndNode && pseqdesc[i].entrynode == iInternNode )
7 years ago
{
*piDir = -1;
return i;
}
}
}
9 years ago
ALERT( at_console, "error in transition graph" );
7 years ago
return iGoalAnim;
}
void SetBodygroup( void *pmodel, entvars_t *pev, int iGroup, int iValue )
{
studiohdr_t *pstudiohdr;
8 years ago
7 years ago
pstudiohdr = (studiohdr_t *)pmodel;
8 years ago
if( !pstudiohdr )
7 years ago
return;
8 years ago
if( iGroup > pstudiohdr->numbodyparts )
7 years ago
return;
8 years ago
mstudiobodyparts_t *pbodypart = (mstudiobodyparts_t *)( (byte *)pstudiohdr + pstudiohdr->bodypartindex ) + iGroup;
7 years ago
8 years ago
if( iValue >= pbodypart->nummodels )
7 years ago
return;
8 years ago
int iCurrent = ( pev->body / pbodypart->base ) % pbodypart->nummodels;
7 years ago
8 years ago
pev->body = ( pev->body - ( iCurrent * pbodypart->base ) + ( iValue * pbodypart->base ) );
7 years ago
}
int GetBodygroup( void *pmodel, entvars_t *pev, int iGroup )
{
studiohdr_t *pstudiohdr;
8 years ago
7 years ago
pstudiohdr = (studiohdr_t *)pmodel;
8 years ago
if( !pstudiohdr )
7 years ago
return 0;
8 years ago
if( iGroup > pstudiohdr->numbodyparts )
7 years ago
return 0;
8 years ago
mstudiobodyparts_t *pbodypart = (mstudiobodyparts_t *)( (byte *)pstudiohdr + pstudiohdr->bodypartindex ) + iGroup;
7 years ago
8 years ago
if( pbodypart->nummodels <= 1 )
7 years ago
return 0;
8 years ago
int iCurrent = ( pev->body / pbodypart->base ) % pbodypart->nummodels;
7 years ago
return iCurrent;
}
//LRC
int GetBoneCount( void *pmodel )
{
studiohdr_t *pstudiohdr;
pstudiohdr = (studiohdr_t *)pmodel;
if (!pstudiohdr)
{
ALERT(at_error, "Bad header in SetBones!\n");
return 0;
}
return pstudiohdr->numbones;
}
#ifndef min
#define min(a,b) (((a) < (b)) ? (a) : (b))
#endif
//LRC
void SetBones( void *pmodel, float (*data)[3], int datasize)
{
studiohdr_t *pstudiohdr;
pstudiohdr = (studiohdr_t *)pmodel;
if (!pstudiohdr)
{
ALERT(at_error, "Bad header in SetBones!\n");
return;
}
mstudiobone_t *pbone = (mstudiobone_t *)((byte *)pstudiohdr + pstudiohdr->boneindex);
// ALERT(at_console, "List begins:\n");
int j;
int limit = min(pstudiohdr->numbones, datasize);
// go through the bones
for (int i = 0; i < limit; i++, pbone++)
{
// ALERT(at_console, " %s\n", pbone->name);
for (j = 0; j < 3; j++)
pbone->value[j] = data[i][j];
}
// ALERT(at_console, "List ends.\n");
}