Skip to content

Commit

Permalink
server: restructurized source code from triggers.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
SNMetamorph committed Mar 9, 2024
1 parent cdf996b commit 0a36908
Show file tree
Hide file tree
Showing 86 changed files with 6,166 additions and 4,749 deletions.
118 changes: 118 additions & 0 deletions server/entities/conveyor_setspeed.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
/***
*
* 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.
*
****/

#include "conveyor_setspeed.h"

LINK_ENTITY_TO_CLASS( conveyor_setspeed, CConveyorSetSpeed );

// Global Savedata for train speed modifier
BEGIN_DATADESC( CConveyorSetSpeed )
DEFINE_KEYFIELD( m_flTime, FIELD_FLOAT, "time" ),
DEFINE_FUNCTION( UpdateSpeed ),
END_DATADESC()

// Sets toucher's friction to m_frictionFraction (1.0 = normal friction)
void CConveyorSetSpeed :: KeyValue( KeyValueData *pkvd )
{
if( FStrEq( pkvd->szKeyName, "time" ))
{
m_flTime = Q_atof( pkvd->szValue );
pkvd->fHandled = TRUE;
}
else
CBaseEntity::KeyValue( pkvd );
}

void CConveyorSetSpeed :: Spawn( void )
{
m_iState = STATE_OFF;
m_hActivator = NULL;
pev->speed = bound( -10000.0f, pev->speed, 10000.0f );
}

BOOL CConveyorSetSpeed :: EvaluateSpeed( BOOL bStartup )
{
CBaseEntity *pEntity = NULL;
int affected_conveyors = 0;
int finished_conveyors = 0;

while(( pEntity = UTIL_FindEntityByTargetname( pEntity, STRING( pev->target ), m_hActivator )) != NULL )
{
if( !FClassnameIs( pEntity->pev, "func_transporter" ) )
continue;

if( bStartup )
{
pEntity->pev->frags = pev->speed - pEntity->pev->speed;
pEntity->pev->dmg_inflictor = edict();
continue;
}

if( m_flTime > 0 && pEntity->pev->dmg_inflictor != edict( ))
continue; // owned by another set speed

if( fabs( pev->speed - pEntity->pev->speed ) <= 1.0f || m_flTime <= 0 )
{
if( FBitSet( pev->spawnflags, SF_CONVSPEED_DEBUG ) && pev->speed != pEntity->pev->speed )
ALERT( at_console, "conveyor_setspeed: %s target speed %g, curspeed %g\n", GetTargetname(), pev->speed, pEntity->pev->speed );
pEntity->Use( this, this, USE_SET, pev->speed );
pEntity->pev->dmg_inflictor = NULL; // done
finished_conveyors++;
}
else
{
float flInterval = pEntity->pev->frags / ( m_flTime / 0.1 ); // nextthink 0.1
pEntity->Use( this, this, USE_SET, pEntity->pev->speed + flInterval );
if( FBitSet( pev->spawnflags, SF_CONVSPEED_DEBUG ))
ALERT( at_console, "conveyor_setspeed: %s target speed %g, curspeed %g\n", GetTargetname(), pev->speed, pEntity->pev->speed );
}
affected_conveyors++;
}

return (affected_conveyors != finished_conveyors);
}

void CConveyorSetSpeed :: Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value )
{
if( m_iState == STATE_ON )
return; // entity in work

m_hActivator = pActivator;

if( m_flTime > 0 )
{
EvaluateSpeed( TRUE );
SetThink( &CConveyorSetSpeed :: UpdateSpeed );
m_iState = STATE_ON;
SetNextThink( 0.1 );
}
else
{
EvaluateSpeed( FALSE );
}
}

void CConveyorSetSpeed :: UpdateSpeed( void )
{
if( !EvaluateSpeed( FALSE ))
{
if( FBitSet( pev->spawnflags, SF_CONVSPEED_DEBUG ))
ALERT( at_console, "conveyor_setspeed: %s is done\n", GetTargetname( ));
m_iState = STATE_OFF;
return; // finished
}

SetNextThink( 0.1 );
}
38 changes: 38 additions & 0 deletions server/entities/conveyor_setspeed.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/***
*
* 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.
*
****/

#pragma once
#include "extdll.h"
#include "util.h"
#include "cbase.h"

#define SF_CONVSPEED_DEBUG BIT( 0 )

class CConveyorSetSpeed : public CBaseDelay
{
DECLARE_CLASS( CConveyorSetSpeed, CBaseDelay );
public:
void Spawn( void );
void KeyValue( KeyValueData *pkvd );
BOOL EvaluateSpeed( BOOL bStartup );
void UpdateSpeed( void );
virtual int ObjectCaps( void ) { return CBaseEntity :: ObjectCaps() & ~FCAP_ACROSS_TRANSITION; }
virtual void Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value );
virtual STATE GetState( void ) { return m_iState; }

DECLARE_DATADESC();

float m_flTime;
};
Loading

0 comments on commit 0a36908

Please sign in to comment.