Skip to content

Commit

Permalink
Merge pull request #134 from fortressforever/lua/misc
Browse files Browse the repository at this point in the history
Add various Lua improvements that aid in moving Collection to Lua
  • Loading branch information
squeek502 committed Mar 27, 2015
2 parents 8e4f158 + b2f2470 commit 1973ed1
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 2 deletions.
12 changes: 12 additions & 0 deletions dlls/ff/ff_lualib_base.cpp
Expand Up @@ -7,8 +7,10 @@
// includes
#include "cbase.h"
#include "ff_lualib.h"
#include "EntityList.h"
#include "ff_item_flag.h"
#include "ff_triggerclip.h"
#include "ff_projectile_base.h"
#include "ff_team.h"

#include "triggers.h"
Expand Down Expand Up @@ -50,6 +52,11 @@ void CFFLuaLib::InitBase(lua_State* L)

module(L)
[
class_<CGlobalEntityList>("EntityList")
.def("FirstEntity", &CGlobalEntityList::FirstEnt)
.def("NextEntity", &CGlobalEntityList::NextEnt)
.def("NumEntities", &CGlobalEntityList::NumberOfEntities),

// CBaseEntity
class_<CBaseEntity>("BaseEntity")
.def(tostring(self))
Expand Down Expand Up @@ -87,6 +94,9 @@ void CFFLuaLib::InitBase(lua_State* L)
.def("GetFriction", &CBaseEntity::GetFriction)
.def("SetFriction", &CBaseEntity::SetFriction),

// CFFProjectileBase
class_<CFFProjectileBase, CBaseEntity>("Projectile"),

// CFFInfoScript
class_<CFFInfoScript, CBaseEntity>("InfoScript")
.def("Drop", (void(CFFInfoScript::*)(float, float))&CFFInfoScript::Drop)
Expand Down Expand Up @@ -132,4 +142,6 @@ void CFFLuaLib::InitBase(lua_State* L)
class_<CFFTriggerClip>("TriggerClip")
.def("SetClipFlags", &CFFTriggerClip::LUA_SetClipFlags)
];

(globals(L))["GlobalEntityList"] = &gEntList;
};
72 changes: 72 additions & 0 deletions dlls/ff/ff_lualib_globals.cpp
Expand Up @@ -170,6 +170,12 @@ namespace FFLib
return IsOfClass( pEntity, CLASS_MANCANNON );
}

// is entity a buildable
bool IsBuildable( CBaseEntity *pEntity )
{
return IsDispenser(pEntity) || IsSentrygun(pEntity) || IsDetpack(pEntity) || IsJumpPad(pEntity);
}

// is the entity a grenade
bool IsGrenade(CBaseEntity* pEntity)
{
Expand All @@ -187,6 +193,18 @@ namespace FFLib
*/
}

// is the entity a projectile
bool IsProjectile(CBaseEntity* pEntity)
{
return dynamic_cast< CFFProjectileBase * >( pEntity ) != NULL;
}

// is entity an infoscript
bool IsInfoScript( CBaseEntity *pEntity )
{
return IsOfClass( pEntity, CLASS_INFOSCRIPT );
}

// is the entity a miniturret
bool IsTurret( CBaseEntity *pEntity )
{
Expand Down Expand Up @@ -566,6 +584,32 @@ namespace FFLib
return luatblEntities;
}

luabind::adl::object GetEntitiesInSphere(const Vector& vecOrigin, float flRadius, bool bIgnoreWalls)
{
luabind::adl::object luatblEntities = luabind::newtable(_scriptman.GetLuaState());

int iTableKey = 1;
CBaseEntity *pEntity = NULL;
for( CEntitySphereQuery sphere( vecOrigin, flRadius ); ( pEntity = sphere.GetCurrentEntity() ) != NULL; sphere.NextEntity() )
{
if( !pEntity )
continue;

if (!bIgnoreWalls)
{
trace_t tr;
UTIL_TraceLine( vecOrigin, pEntity->GetAbsOrigin(), MASK_SOLID, NULL, COLLISION_GROUP_NONE, &tr );

if( FF_TraceHitWorld( &tr ) )
continue;
}

luatblEntities[iTableKey++] = luabind::adl::object(_scriptman.GetLuaState(), pEntity);
}

return luatblEntities;
}

CFFPlayer* GetPlayer(CBaseEntity *pEntity)
{
// ToFFPlayer checks for NULL & IsPlayer()
Expand Down Expand Up @@ -676,6 +720,17 @@ namespace FFLib
return dynamic_cast< CFFGrenadeBase * >( pEntity );
}

CFFProjectileBase *CastToProjectile( CBaseEntity *pEntity )
{
if( !pEntity )
return NULL;

if( !IsProjectile( pEntity ) )
return NULL;

return dynamic_cast< CFFProjectileBase * >( pEntity );
}

CBeam* CastToBeam(CBaseEntity* pEntity)
{
if( !pEntity )
Expand Down Expand Up @@ -764,6 +819,17 @@ namespace FFLib
return dynamic_cast< CFFManCannon * >( pEntity );
}

CFFBuildableObject *CastToBuildable( CBaseEntity *pEntity )
{
if( !pEntity )
return NULL;

if( !IsBuildable( pEntity ) )
return NULL;

return dynamic_cast< CFFBuildableObject * >( pEntity );
}

bool AreTeamsAllied(CTeam* pTeam1, CTeam* pTeam2)
{
if(NULL == pTeam1 || NULL == pTeam2)
Expand Down Expand Up @@ -3041,6 +3107,8 @@ void CFFLuaLib::InitGlobals(lua_State* L)
def("CastToSentrygun", &FFLib::CastToSentrygun),
def("CastToDetpack", &FFLib::CastToDetpack),
def("CastToJumpPad", &FFLib::CastToJumpPad),
def("CastToBuildable", &FFLib::CastToBuildable),
def("CastToProjectile", &FFLib::CastToProjectile),
def("ConsoleToAll", &FFLib::ConsoleToAll),
def("DeleteSchedule", &FFLib::DeleteSchedule),
def("DropToFloor", &FFLib::DropToFloor),
Expand All @@ -3049,6 +3117,7 @@ void CFFLuaLib::InitGlobals(lua_State* L)
def("GetEntity", &FFLib::GetEntity),
def("GetEntityByName", &FFLib::GetEntityByName),
def("GetEntitiesByName", &FFLib::GetEntitiesByName),
def("GetEntitiesInSphere", &FFLib::GetEntitiesInSphere),
def("GetInfoScriptById", &FFLib::GetInfoScriptById),
def("GetInfoScriptByName", &FFLib::GetInfoScriptByName),
def("GetGrenade", &FFLib::GetGrenade),
Expand All @@ -3073,8 +3142,11 @@ void CFFLuaLib::InitGlobals(lua_State* L)
def("IsSentrygun", &FFLib::IsSentrygun),
def("IsDetpack", &FFLib::IsDetpack),
def("IsJumpPad", &FFLib::IsJumpPad),
def("IsBuildable", &FFLib::IsBuildable),
def("IsGrenade", &FFLib::IsGrenade),
def("IsTurret", &FFLib::IsTurret),
def("IsProjectile", &FFLib::IsProjectile),
def("IsInfoScript", &FFLib::IsInfoScript),
//def("KillAndRespawnAllPlayers", &FFLib::KillAndRespawnAllPlayers),
def("NumPlayers", &FF_NumPlayers),
def("GetPlayers", &FFLib::GetPlayers),
Expand Down
13 changes: 11 additions & 2 deletions dlls/ff/ff_lualib_util.cpp
Expand Up @@ -82,6 +82,7 @@ enum CollectionFilter
CF_BUILDABLE_DISPENSER,
CF_BUILDABLE_SENTRYGUN,
CF_BUILDABLE_DETPACK,
CF_BUILDABLE_JUMPPAD,

CF_TRACE_BLOCK_WALLS,

Expand Down Expand Up @@ -410,6 +411,12 @@ bool PassesCollectionFilter( CBaseEntity *pEntity, bool *pbFlags )
return false;
}

if( pbFlags[ CF_BUILDABLE_JUMPPAD ] )
{
if( pEntity->Classify() != CLASS_MANCANNON )
return false;
}

return true;
}

Expand Down Expand Up @@ -990,7 +997,8 @@ void CFFLuaLib::InitUtil(lua_State* L)

value("kProjectiles", CF_PROJECTILES),
value("kGrenades", CF_GRENADES),
value("kInfoScipts", CF_INFOSCRIPTS),
value("kInfoScipts", CF_INFOSCRIPTS), // typo; kept for backwards compatibility
value("kInfoScripts", CF_INFOSCRIPTS),

value("kInfoScript_Carried", CF_INFOSCRIPT_CARRIED),
value("kInfoScript_Dropped", CF_INFOSCRIPT_DROPPED),
Expand All @@ -1004,7 +1012,8 @@ void CFFLuaLib::InitUtil(lua_State* L)
value("kBuildables", CF_BUILDABLES),
value("kDispenser", CF_BUILDABLE_DISPENSER),
value("kSentrygun", CF_BUILDABLE_SENTRYGUN),
value("kDetpack", CF_BUILDABLE_DETPACK)
value("kDetpack", CF_BUILDABLE_DETPACK),
value("kJumpPad", CF_BUILDABLE_JUMPPAD)
]
];
};

0 comments on commit 1973ed1

Please sign in to comment.