Skip to content

Commit

Permalink
Improve entity glow (#30)
Browse files Browse the repository at this point in the history
* Improve entity glow

* What's your problem green

* Split up the callbacks

* Use a lighter shade

* Fix callback name

* Update callback names
  • Loading branch information
Mikusch authored Feb 24, 2025
1 parent be25e8a commit ef4869b
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 93 deletions.
19 changes: 6 additions & 13 deletions addons/sourcemod/gamedata/mitm.txt
Original file line number Diff line number Diff line change
Expand Up @@ -366,10 +366,10 @@
"linux" "18"
"windows" "17"
}
"CBaseEntity::SetModel"
"CBaseEntity::UpdateTransmitState"
{
"linux" "27"
"windows" "26"
"linux" "22"
"windows" "21"
}
"CBaseEntity::IsCombatItem"
{
Expand Down Expand Up @@ -1236,19 +1236,12 @@
}
}
}
"CBaseEntity::SetModel"
"CBaseEntity::UpdateTransmitState"
{
"offset" "CBaseEntity::SetModel"
"offset" "CBaseEntity::UpdateTransmitState"
"hooktype" "entity"
"return" "void"
"return" "int"
"this" "entity"
"arguments"
{
"szModelName"
{
"type" "charptr"
}
}
}
"CBaseEntity::FVisible"
{
Expand Down
2 changes: 2 additions & 0 deletions addons/sourcemod/scripting/mitm/data.sp
Original file line number Diff line number Diff line change
Expand Up @@ -1949,6 +1949,8 @@ methodmap CTFPlayer < CBaseCombatCharacter
// MannInTheMachinePlayer
this.m_annotationTimer = null;
this.m_delayedNoticeList.Clear();

Entity(this.index).CreateGlowEntity( { 153, 255, 153, 255 }, SDKHook_PlayerGlow_SetTransmit);
}
}

Expand Down
38 changes: 11 additions & 27 deletions addons/sourcemod/scripting/mitm/dhooks.sp
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
#pragma semicolon 1
#pragma newdecls required

static DynamicHook g_hDHook_CBaseEntity_SetModel;
static DynamicHook g_hDHook_CBaseEntity_FVisible;
static DynamicHook g_hDHook_CBaseObject_IsPlacementPosValid;
static DynamicHook g_hDHook_CBaseEntity_UpdateTransmitState;
static DynamicHook g_hDHook_CBaseObject_CanBeUpgraded;
static DynamicHook g_hDHook_CItem_ComeToRest;
static DynamicHook g_hDHook_CBaseEntity_ShouldTransmit;
Expand Down Expand Up @@ -82,9 +82,9 @@ void DHooks_Init()
PSM_AddDynamicDetourFromConf("DoTeleporterOverride", DHookCallback_DoTeleporterOverride_Pre);
PSM_AddDynamicDetourFromConf("OnBotTeleported", DHookCallback_OnBotTeleported_Pre);

g_hDHook_CBaseEntity_SetModel = PSM_AddDynamicHookFromConf("CBaseEntity::SetModel");
g_hDHook_CBaseEntity_FVisible = PSM_AddDynamicHookFromConf("CBaseEntity::FVisible");
g_hDHook_CBaseObject_IsPlacementPosValid = PSM_AddDynamicHookFromConf("CBaseObject::IsPlacementPosValid");
g_hDHook_CBaseEntity_UpdateTransmitState = PSM_AddDynamicHookFromConf("CBaseEntity::UpdateTransmitState");
g_hDHook_CBaseObject_CanBeUpgraded = PSM_AddDynamicHookFromConf("CBaseObject::CanBeUpgraded");
g_hDHook_CItem_ComeToRest = PSM_AddDynamicHookFromConf("CItem::ComeToRest");
g_hDHook_CBaseEntity_ShouldTransmit = PSM_AddDynamicHookFromConf("CBaseEntity::ShouldTransmit");
Expand Down Expand Up @@ -142,9 +142,6 @@ void DHooks_OnEntityCreated(int entity, const char[] classname)
{
if (IsEntityClient(entity))
{
if (g_hDHook_CBaseEntity_SetModel)
PSM_DHookEntity(g_hDHook_CBaseEntity_SetModel, Hook_Post, entity, DHookCallback_CBaseEntity_SetModel_Post);

if (g_hDHook_CBaseEntity_ShouldTransmit)
PSM_DHookEntity(g_hDHook_CBaseEntity_ShouldTransmit, Hook_Pre, entity, DHookCallback_CTFPlayer_ShouldTransmit_Pre);

Expand Down Expand Up @@ -183,10 +180,10 @@ void DHooks_OnEntityCreated(int entity, const char[] classname)
if (g_hDHook_CItem_ComeToRest)
PSM_DHookEntity(g_hDHook_CItem_ComeToRest, Hook_Pre, entity, DHookCallback_CCurrencyPack_ComeToRest_Pre);
}
else if (StrEqual(classname, "obj_sentrygun"))
else if (StrEqual(classname, "tf_glow"))
{
if (g_hDHook_CBaseEntity_SetModel)
PSM_DHookEntity(g_hDHook_CBaseEntity_SetModel, Hook_Post, entity, DHookCallback_CBaseEntity_SetModel_Post);
if (g_hDHook_CBaseEntity_UpdateTransmitState)
PSM_DHookEntity(g_hDHook_CBaseEntity_UpdateTransmitState, Hook_Pre, entity, DHookCallback_CTFGlow_UpdateTransmitState_Pre);
}
else if (HasEntProp(entity, Prop_Data, "CBaseCombatWeaponDefaultTouch"))
{
Expand Down Expand Up @@ -1657,25 +1654,6 @@ static MRESReturn DHookCallback_CTFGameRules_RespawnPlayers_Pre(DHookParam param
return bTeam && team == TFTeam_Invaders ? MRES_Supercede : MRES_Ignored;
}

static MRESReturn DHookCallback_CBaseEntity_SetModel_Post(int entity, DHookParam params)
{
char szModelName[PLATFORM_MAX_PATH];
params.GetString(1, szModelName, sizeof(szModelName));

int hGlowEntity = Entity(entity).GetGlowEntity();
if (IsValidEntity(hGlowEntity))
{
// we already have a glow, update it with the new model
SetEntityModel(hGlowEntity, szModelName);
}
else
{
// no existing glow entity, create one
Entity(entity).SetGlowEntity(CreateEntityGlow(entity));
}

return MRES_Ignored;
}

static MRESReturn DHookCallback_CBaseCombatWeapon_FVisible_Pre(int weapon, DHookReturn ret, DHookParam params)
{
Expand Down Expand Up @@ -1772,6 +1750,12 @@ static MRESReturn DHookCallback_CCurrencyPack_ComeToRest_Pre(int item)
return MRES_Ignored;
}

static MRESReturn DHookCallback_CTFGlow_UpdateTransmitState_Pre(int glow, DHookReturn ret)
{
ret.Value = FL_EDICT_ALWAYS;
return MRES_Supercede;
}

static MRESReturn DHookCallback_CTFBot_ScriptAddBotAttribute_Pre(int bot, DHookParam params)
{
if (IsFakeClient(bot))
Expand Down
13 changes: 13 additions & 0 deletions addons/sourcemod/scripting/mitm/entity.sp
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,19 @@ methodmap Entity
return this.m_hGlowEntity;
}

public void CreateGlowEntity(const int color[4], SDKHookCB callback)
{
int hGlowEntity = this.GetGlowEntity();
if (!IsValidEntity(hGlowEntity))
{
hGlowEntity = CreateGlowEntity(view_as<int>(this), color, callback);
if (IsValidEntity(hGlowEntity))
{
this.SetGlowEntity(hGlowEntity);
}
}
}

public void Destroy()
{
int index = this.m_listIndex;
Expand Down
2 changes: 2 additions & 0 deletions addons/sourcemod/scripting/mitm/events.sp
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ static void EventHook_PlayerBuiltObject(Event event, const char[] name, bool don
TFObjectType type = view_as<TFObjectType>(event.GetInt("object"));
int index = event.GetInt("index");

Entity(index).CreateGlowEntity( { 255, 255, 255, 255 }, SDKHook_ObjectGlow_SetTransmit);

if (TF2_GetClientTeam(builder) == TFTeam_Invaders)
{
float origin[3];
Expand Down
48 changes: 29 additions & 19 deletions addons/sourcemod/scripting/mitm/sdkhooks.sp
Original file line number Diff line number Diff line change
Expand Up @@ -208,43 +208,53 @@ static void SDKHook_CTFPlayerResource_ThinkPost(int manager)
}
}

Action SDKHookCB_EntityGlow_SetTransmit(int entity, int client)
Action SDKHook_PlayerGlow_SetTransmit(int entity, int client)
{
int hEffectEntity = GetEntPropEnt(entity, Prop_Data, "m_hEffectEntity");
Action action = Plugin_Handled;

if (!IsValidEntity(hEffectEntity))
return Plugin_Handled;
if (CTFPlayer(client).IsInASquad())
{
CTFBotSquad squad = CTFPlayer(client).GetSquad();
int hEffectEntity = GetEntPropEnt(entity, Prop_Data, "m_hMoveParent");

if (hEffectEntity != client && (squad.IsLeader(hEffectEntity) || squad.IsLeader(client) && squad.IsMember(hEffectEntity)))
{
// show the glow of our squad leader or our squad members
action = Plugin_Continue;
}
}

CBaseEntity(entity).RefreshNetwork(client, action == Plugin_Continue ? true : false);
return action;
}

Action SDKHook_ObjectGlow_SetTransmit(int entity, int client)
{
Action action = Plugin_Handled;

int hMissionTarget = CTFPlayer(client).GetMissionTarget();
if (IsValidEntity(hMissionTarget) && IsBaseObject(hMissionTarget))
{
// target sentry - only outline if not carried
int hEffectEntity = GetEntPropEnt(entity, Prop_Data, "m_hMoveParent");

// mission target - only outline if not carried
if (hEffectEntity == hMissionTarget)
{
if (!GetEntProp(hMissionTarget, Prop_Send, "m_bCarried"))
{
return Plugin_Continue;
action = Plugin_Continue;
}
}
// player - only outline if carrying target sentry
// player - only outline if carrying mission target
else if (hEffectEntity == GetEntPropEnt(hMissionTarget, Prop_Send, "m_hBuilder"))
{
if (GetEntProp(hMissionTarget, Prop_Send, "m_bCarried"))
{
return Plugin_Continue;
action = Plugin_Continue;
}
}
}

if (CTFPlayer(client).IsInASquad())
{
CTFBotSquad squad = CTFPlayer(client).GetSquad();
if (hEffectEntity != client && (squad.IsLeader(hEffectEntity) || squad.IsLeader(client) && squad.IsMember(hEffectEntity)))
{
// show the glow of our squad leader or our squad members
return Plugin_Continue;
}
}

return Plugin_Handled;
CBaseEntity(entity).RefreshNetwork(client, action == Plugin_Continue ? true : false);
return action;
}
49 changes: 15 additions & 34 deletions addons/sourcemod/scripting/mitm/util.sp
Original file line number Diff line number Diff line change
Expand Up @@ -353,47 +353,28 @@ CTFPlayer FindNextInvader(bool bIsMiniBoss)
return priorityPlayer;
}

int CreateEntityGlow(int entity)
int CreateGlowEntity(int parent, const int color[4], SDKHookCB callback)
{
int glow = CreateEntityByName("tf_taunt_prop");
int glow = CreateEntityByName("tf_glow");
if (glow != -1)
{
char iszModel[PLATFORM_MAX_PATH];
float origin[3], angles[3];
GetEntPropVector(parent, Prop_Data, "m_vecAbsOrigin", origin);
GetEntPropVector(parent, Prop_Data, "m_angAbsRotation", angles);

if (HasEntProp(entity, Prop_Send, "m_iszCustomModel"))
{
GetEntPropString(entity, Prop_Send, "m_iszCustomModel", iszModel, sizeof(iszModel));
}
DispatchKeyValueVector(glow, "origin", origin);
DispatchKeyValueVector(glow, "angles", angles);

if (!iszModel[0])
{
GetEntPropString(entity, Prop_Data, "m_ModelName", iszModel, sizeof(iszModel));
}
SetEntPropEnt(glow, Prop_Send, "m_hTarget", parent);

SetEntityModel(glow, iszModel);
SetVariantString("!activator");
AcceptEntityInput(glow, "SetParent", parent);

if (DispatchSpawn(glow))
{
SetEntPropEnt(glow, Prop_Data, "m_hEffectEntity", entity);
SetEntProp(glow, Prop_Send, "m_bGlowEnabled", true);

SetEntityRenderMode(glow, RENDER_TRANSCOLOR);
SetEntityRenderColor(glow, 0, 0, 0, 0);

int fEffects = GetEntProp(glow, Prop_Send, "m_fEffects");
SetEntProp(glow, Prop_Send, "m_fEffects", fEffects | EF_BONEMERGE | EF_NOSHADOW | EF_NORECEIVESHADOW);

SetVariantString("!activator");
AcceptEntityInput(glow, "SetParent", entity);

SDKHook(glow, SDKHook_SetTransmit, SDKHookCB_EntityGlow_SetTransmit);

return EntIndexToEntRef(glow);
}
else
{
RemoveEntity(glow);
}
SetVariantColor(color);
AcceptEntityInput(glow, "SetGlowColor");

PSM_SDKHook(glow, SDKHook_SetTransmit, callback);
return EntIndexToEntRef(glow);
}

return INVALID_ENT_REFERENCE;
Expand Down

0 comments on commit ef4869b

Please sign in to comment.