Rewriting the logic_lineto entity code to output an integer

Grab your favourite IDE and tinker with the innards of game engines

Rewriting the logic_lineto entity code to output an integer

Postby coohandluke on Sat Jan 01, 2011 11:27 pm

I'm trying to create a new entity using the existing logic_lineto entity so that it outputs each coordinate as an integer, instead of outputing a vector so that I can measure the change in distance on any axis between two points. All I really need is the Z coordinate, but it would be nice to be able to have each coordinate as a possible output. I don't really know anything about c++ or source code, at least enough to make such an entity, so hopefull someone here can help.

Here is the existing logic_lineto code found in logicentities.cpp including the includes for reference:

Code: Select all
#include "cbase.h"
#include "entityinput.h"
#include "entityoutput.h"
#include "eventqueue.h"
#include "mathlib.h"
#include "globalstate.h"
#include "ndebugoverlay.h"
#include "saverestore_utlvector.h"
#include "vstdlib/random.h"
#include "gameinterface.h"

.................

//-----------------------------------------------------------------------------
// Purpose: Computes a line between two entities
//-----------------------------------------------------------------------------
class CLogicLineToEntity : public CLogicalEntity
{
public:
   DECLARE_CLASS( CLogicLineToEntity, CLogicalEntity );

   void Activate(void);
   void Spawn( void );
   void Think( void );

   // outputs
   COutputVector m_Line;

   DECLARE_DATADESC();

private:
   string_t m_SourceName;
   EHANDLE   m_StartEntity;
   EHANDLE m_EndEntity;
};

LINK_ENTITY_TO_CLASS( logic_lineto, CLogicLineToEntity );


BEGIN_DATADESC( CLogicLineToEntity )

   // Keys
   // target is handled in the base class, stored in field m_target
   DEFINE_KEYFIELD( m_SourceName, FIELD_STRING, "source" ),
    DEFINE_FIELD( m_StartEntity, FIELD_EHANDLE ),
   DEFINE_FIELD( m_EndEntity, FIELD_EHANDLE ),

   // Outputs
   DEFINE_OUTPUT( m_Line, "Line" ),

END_DATADESC()



//-----------------------------------------------------------------------------
// Find the entities
//-----------------------------------------------------------------------------
void CLogicLineToEntity::Activate(void)
{
   BaseClass::Activate();

   if (m_target != NULL_STRING)
   {
      m_EndEntity = gEntList.FindEntityByName( NULL, m_target );

      //
      // If we were given a bad measure target, just measure sound where we are.
      //
      if ((m_EndEntity == NULL) || (m_EndEntity->edict() == NULL))
      {
         Warning( "logic_lineto - Target not found or target with no origin!\n");
         m_EndEntity = this;
      }
   }
   else
   {
      m_EndEntity = this;
   }

   if (m_SourceName != NULL_STRING)
   {
      m_StartEntity = gEntList.FindEntityByName( NULL, m_SourceName );

      //
      // If we were given a bad measure target, just measure sound where we are.
      //
      if ((m_StartEntity == NULL) || (m_StartEntity->edict() == NULL))
      {
         Warning( "logic_lineto - Source not found or source with no origin!\n");
         m_StartEntity = this;
      }
   }
   else
   {
      m_StartEntity = this;
   }
}


//-----------------------------------------------------------------------------
// Find the entities
//-----------------------------------------------------------------------------
void CLogicLineToEntity::Spawn(void)
{
   SetNextThink( gpGlobals->curtime + 0.01f );
}


//-----------------------------------------------------------------------------
// Find the entities
//-----------------------------------------------------------------------------
void CLogicLineToEntity::Think(void)
{
   CBaseEntity* pDest = m_EndEntity.Get();
   CBaseEntity* pSrc = m_StartEntity.Get();
   if (!pDest || !pSrc || !pDest->edict() || !pSrc->edict())
   {
      // Can sleep for a long time, no more lines.
      m_Line.Set( vec3_origin, this, this );
      SetNextThink( gpGlobals->curtime + 10 );
      return;
   }

   Vector delta;
   VectorSubtract( pDest->GetAbsOrigin(), pSrc->GetAbsOrigin(), delta );
   m_Line.Set(delta, this, this);

   SetNextThink( gpGlobals->curtime + 0.01f );
}


Any hints or suggestions would be helpful.

~coohandluke
coohandluke
Dumpling
Dumpling
 
Joined: Sat Jan 01, 2011 10:42 pm

Re: Rewriting the logic_lineto entity code to output an inte

Postby zombie@computer on Sun Jan 02, 2011 10:00 am

Code: Select all
    #include "cbase.h"
    #include "entityinput.h"
    #include "entityoutput.h"
    #include "eventqueue.h"
    #include "mathlib.h"
    #include "globalstate.h"
    #include "ndebugoverlay.h"
    #include "saverestore_utlvector.h"
    #include "vstdlib/random.h"
    #include "gameinterface.h"


    //-----------------------------------------------------------------------------
    // Purpose: Computes a line between two entities
    //-----------------------------------------------------------------------------
    class CLogicLineToEntity : public CLogicalEntity
    {
    public:
       DECLARE_CLASS( CLogicLineToEntity, CLogicalEntity );

       void Activate(void);
       void Spawn( void );
       void Think( void );

       // outputs
       COutputVector m_Line;
      COutputInt    m_iZDist;

       DECLARE_DATADESC();

    private:
       string_t m_SourceName;
       EHANDLE   m_StartEntity;
       EHANDLE m_EndEntity;
    };

    LINK_ENTITY_TO_CLASS( logic_lineto, CLogicLineToEntity );


    BEGIN_DATADESC( CLogicLineToEntity )

       // Keys
       // target is handled in the base class, stored in field m_target
       DEFINE_KEYFIELD( m_SourceName, FIELD_STRING, "source" ),
       DEFINE_FIELD( m_StartEntity, FIELD_EHANDLE ),
       DEFINE_FIELD( m_EndEntity, FIELD_EHANDLE ),

       // Outputs
       DEFINE_OUTPUT( m_Line, "Line" ),
      DEFINE_OUTPUT( m_iZDist, "Zdist" ),

    END_DATADESC()



    //-----------------------------------------------------------------------------
    // Find the entities
    //-----------------------------------------------------------------------------
    void CLogicLineToEntity::Activate(void)
    {
       BaseClass::Activate();

       if (m_target != NULL_STRING)
       {
          m_EndEntity = gEntList.FindEntityByName( NULL, m_target );

          //
          // If we were given a bad measure target, just measure sound where we are.
          //
          if ((m_EndEntity == NULL) || (m_EndEntity->edict() == NULL))
          {
             Warning( "logic_lineto - Target not found or target with no origin!\n");
             m_EndEntity = this;
          }
       }
       else
       {
          m_EndEntity = this;
       }

       if (m_SourceName != NULL_STRING)
       {
          m_StartEntity = gEntList.FindEntityByName( NULL, m_SourceName );

          //
          // If we were given a bad measure target, just measure sound where we are.
          //
          if ((m_StartEntity == NULL) || (m_StartEntity->edict() == NULL))
          {
             Warning( "logic_lineto - Source not found or source with no origin!\n");
             m_StartEntity = this;
          }
       }
       else
       {
          m_StartEntity = this;
       }
    }


    //-----------------------------------------------------------------------------
    // Find the entities
    //-----------------------------------------------------------------------------
    void CLogicLineToEntity::Spawn(void)
    {
       SetNextThink( gpGlobals->curtime + 0.01f );
    }


    //-----------------------------------------------------------------------------
    // Find the entities
    //-----------------------------------------------------------------------------
    void CLogicLineToEntity::Think(void)
    {
       CBaseEntity* pDest = m_EndEntity.Get();
       CBaseEntity* pSrc = m_StartEntity.Get();
       if (!pDest || !pSrc || !pDest->edict() || !pSrc->edict())
       {
          // Can sleep for a long time, no more lines.
          m_Line.Set( vec3_origin, this, this );
        m_iZDist.Set(int(vec3_origin.z), this, this);
          SetNextThink( gpGlobals->curtime + 10 );
          return;
       }

       Vector delta;
       VectorSubtract( pDest->GetAbsOrigin(), pSrc->GetAbsOrigin(), delta );
       m_Line.Set(delta, this, this);
      m_iZDist.Set(int(delta.z), this, this);
       SetNextThink( gpGlobals->curtime + 0.01f );
    }
should do it, though its terribly inefficient code.
When you are up to your neck in shit, keep your head up high
zombie@computer
Forum Goer Elite™
Forum Goer Elite™
 
Joined: Fri Dec 31, 2004 5:58 pm
Location: Lent, Netherlands

Re: Rewriting the logic_lineto entity code to output an inte

Postby coohandluke on Tue Jan 04, 2011 2:57 am

Thanks! I'll check it out, I'll let you know how it goes.

EDIT: Works like a charm!
coohandluke
Dumpling
Dumpling
 
Joined: Sat Jan 01, 2011 10:42 pm

Return to Programming

Who is online

Users browsing this forum: No registered users