forked from mapbase-source/source-sdk-2013
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request mapbase-source#140 from moofemp/feature/logic-subs…
…tring Add logic_substring
- Loading branch information
Showing
1 changed file
with
110 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
//====================== By Holly Liberatore / MoofEMP ======================// | ||
// | ||
// Purpose: Takes a string parameter and returns a substring defined by keyvalues | ||
// | ||
//===========================================================================// | ||
|
||
#include "cbase.h" | ||
|
||
#define SF_SUBSTRING_START_DISABLED (1 << 0) | ||
|
||
class CLogicSubstring : public CLogicalEntity | ||
{ | ||
public: | ||
DECLARE_CLASS( CLogicSubstring, CLogicalEntity ); | ||
DECLARE_DATADESC(); | ||
|
||
CLogicSubstring( void ) { } | ||
|
||
void InputDisable( inputdata_t &inputData ); | ||
void InputEnable( inputdata_t &inputData ); | ||
void InputInValue( inputdata_t &inputData ); | ||
void InputSetLength( inputdata_t &inputData ); | ||
void InputSetStartPos( inputdata_t &inputData ); | ||
|
||
void Spawn(void); | ||
|
||
private: | ||
int m_nLength; | ||
int m_nStartPos; | ||
|
||
bool m_bEnabled; | ||
|
||
COutputString m_OutValue; | ||
}; | ||
|
||
LINK_ENTITY_TO_CLASS( logic_substring, CLogicSubstring ); | ||
|
||
BEGIN_DATADESC( CLogicSubstring ) | ||
|
||
DEFINE_FIELD( m_bEnabled, FIELD_BOOLEAN ), | ||
|
||
DEFINE_KEYFIELD(m_nLength, FIELD_INTEGER, "length" ), | ||
DEFINE_KEYFIELD(m_nStartPos, FIELD_INTEGER, "startPos" ), | ||
|
||
DEFINE_INPUTFUNC( FIELD_VOID, "Disable", InputDisable ), | ||
DEFINE_INPUTFUNC( FIELD_VOID, "Enable", InputEnable ), | ||
DEFINE_INPUTFUNC( FIELD_STRING, "InValue", InputInValue ), | ||
DEFINE_INPUTFUNC( FIELD_INTEGER, "SetLength", InputSetLength ), | ||
DEFINE_INPUTFUNC( FIELD_INTEGER, "SetStartPos", InputSetStartPos ), | ||
|
||
DEFINE_OUTPUT( m_OutValue, "OutValue" ), | ||
|
||
END_DATADESC() | ||
|
||
//----------------------------------------------------------------------------- | ||
// Purpose: Disable or enable the entity (disabling prevents any input functions from running) | ||
//----------------------------------------------------------------------------- | ||
void CLogicSubstring::InputDisable( inputdata_t &inputData ) { m_bEnabled = false; } | ||
void CLogicSubstring::InputEnable ( inputdata_t &inputData ) { m_bEnabled = true ; } | ||
|
||
//----------------------------------------------------------------------------- | ||
// Purpose: Trim substring from input | ||
// Output: Substring | ||
//----------------------------------------------------------------------------- | ||
void CLogicSubstring::InputInValue( inputdata_t &inputData ) | ||
{ | ||
if( !m_bEnabled ) return; | ||
|
||
int inputLength = Q_strlen(inputData.value.String()); | ||
int startPosCheck = m_nStartPos < 0 ? inputLength + m_nStartPos : m_nStartPos; | ||
if( startPosCheck < 0 ) | ||
{ | ||
startPosCheck = 0; | ||
} | ||
int lengthCheck = (m_nLength < 0 || m_nLength > inputLength - startPosCheck ? inputLength - startPosCheck : m_nLength) + 1; | ||
if( lengthCheck < 1 || startPosCheck > inputLength ) | ||
{ | ||
m_OutValue.Set( MAKE_STRING(""), inputData.pActivator, this ); | ||
return; | ||
} | ||
char* strOutValue = (char*)malloc( lengthCheck ); | ||
Q_strncpy( strOutValue, inputData.value.String() + startPosCheck, lengthCheck ); | ||
m_OutValue.Set( AllocPooledString(strOutValue), inputData.pActivator, this ); | ||
free(strOutValue); | ||
} | ||
|
||
//----------------------------------------------------------------------------- | ||
// Purpose: Setter methods for keyvalues | ||
//----------------------------------------------------------------------------- | ||
void CLogicSubstring::InputSetLength( inputdata_t &inputData ) | ||
{ | ||
if( !m_bEnabled ) return; | ||
|
||
m_nLength = inputData.value.Int(); | ||
} | ||
|
||
void CLogicSubstring::InputSetStartPos( inputdata_t &inputData ) | ||
{ | ||
if( !m_bEnabled ) return; | ||
|
||
m_nStartPos = inputData.value.Int(); | ||
} | ||
|
||
//----------------------------------------------------------------------------- | ||
// Purpose: Respond to spawnflags when entity spawns | ||
//----------------------------------------------------------------------------- | ||
void CLogicSubstring::Spawn( void ) | ||
{ | ||
m_bEnabled = !HasSpawnFlags( SF_SUBSTRING_START_DISABLED ); | ||
} |