Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Response System #143

Merged
merged 3 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions gamedata/srccoop.games.txt
Original file line number Diff line number Diff line change
Expand Up @@ -737,6 +737,23 @@
}
}
}
"CAI_ExpresserHost_NPC_DoModifyOrAppendCriteria"
{
"signature" "CAI_ExpresserHost_NPC_DoModifyOrAppendCriteria"
"callconv" "cdecl"
"return" "void"
"arguments"
{
"pSpeaker"
{
"type" "cbaseentity"
}
"pCriteriaSet"
{
"type" "int"
}
}
}
}
}

Expand Down Expand Up @@ -879,6 +896,16 @@
"windows" "\x55\x8B\xEC\x83\xEC\x5C\x56\x57\x8B\xF9\x2A\x2A\x2A\x2A\x2A\x8B\xF0\x85\xF6\x2A\x2A\x2A\x2A\x2A\x2A"
"linux" "@_ZN11CWeapon_Tau8FireBeamERKfb"
}
"CAI_ExpresserHost_NPC_DoModifyOrAppendCriteria"
{
"windows" "\x55\x8B\xEC\x83\xEC\x0C\x53\x56\x8B\x75\x08\x57\xFF\xB6\xA0\x09\x00\x00\x2A\x2A\x2A\x2A\x2A\x8B\x7D\x0C\x83\xC4"
"linux" "@_Z46CAI_ExpresserHost_NPC_DoModifyOrAppendCriteriaP11CAI_BaseNPCR14AI_CriteriaSet"
}
"AI_CriteriaSet::AppendCriteria"
{
"windows" "\x55\x8B\xEC\x83\xEC\x50\xA1\x2A\x2A\x2A\x2A\x33\xC5\x89\x45\x2A\x53\x8B\x5D\x2A\xB8\xFF\xFF\x00\x00"
"linux" "@_ZN14AI_CriteriaSet14AppendCriteriaEPKcS1_f"
}
}

// ____ __ __ _____ __ __ ______ __ __ _____ _______ _____ _ _ ______ _____
Expand Down Expand Up @@ -1309,6 +1336,16 @@
"windows" "\x55\x8B\xEC\x83\xEC\x1C\x56\x8B\xF1\x80\xBE\x88\x03\x00\x00\x01"
"linux" "@_ZN20CAI_ScriptConditions15EvaluationThinkEv"
}
"CAI_ExpresserHost_NPC_DoModifyOrAppendCriteria"
{
"windows" "\x55\x8B\xEC\x83\xEC\x0C\x53\x56\x8B\x75\x08\x57\xFF\xB6\xA0\x09\x00\x00\x2A\x2A\x2A\x2A\x2A\x8B\x7D\x0C\x83\xC4"
"linux" "@_Z46CAI_ExpresserHost_NPC_DoModifyOrAppendCriteriaP11CAI_BaseNPCR14AI_CriteriaSet"
}
"AI_CriteriaSet::AppendCriteria"
{
"windows" "\x55\x8B\xEC\x83\xEC\x48\x53\x56\x8B\x75\x08\xBB\xFF\xFF\x00\x00\x57\x8B\xF9\x66\x89\x5D"
"linux" "@_ZN14AI_CriteriaSet14AppendCriteriaEPKcS1_f"
}
}

// _ _ _ ___ __ __ _____ __ __ ______ __ __ _____ _______ _____ _ _ ______ _____
Expand Down
4 changes: 2 additions & 2 deletions scripting/include/srccoop.inc
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
#define ENTPATCH_PLAYER_LOADSAVED
#define ENTPATCH_LOGIC_AUTOSAVE_SURVIVAL_RESPAWN
#define ENTPATCH_GAME_END
#define ENTPATCH_FIND_NAMED_ENTITY
#define ENTPATCH_NPC_DIALOGUE
#define ENTPATCH_GOALENTITY_RESOLVENAMES
#define ENTPATCH_GOAL_LEAD
#define ENTPATCH_SETPLAYERAVOIDSTATE
Expand Down Expand Up @@ -82,7 +82,7 @@
#define ENTPATCH_PLAYER_LOADSAVED
#define ENTPATCH_LOGIC_AUTOSAVE_SURVIVAL_RESPAWN
#define ENTPATCH_GAME_END
#define ENTPATCH_FIND_NAMED_ENTITY
#define ENTPATCH_NPC_DIALOGUE
#define ENTPATCH_REMOVE_BONE_FOLLOWERS
#define ENTPATCH_WEAPON_MODELS
#define ENTPATCH_PLAYER_ALLY
Expand Down
47 changes: 47 additions & 0 deletions scripting/include/srccoop/classdef.inc
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ Handle g_pGetPlayerState;
Handle g_pSelectWeightedSequence;
Handle g_pSendViewModelMatchingSequence;
Handle g_pGetGlobalVars;
Handle g_pAppendCriteria;

int g_iCSoundSize = -1;
int g_iCPlayerState_frags = -1;
Expand Down Expand Up @@ -342,6 +343,18 @@ stock void InitClassdef(GameData pGameConfig)
PrepSDKCall_SetReturnInfo(SDKType_PlainOldData, SDKPass_Plain);
if (!(g_pGetGlobalVars = EndPrepSDKCall())) SetFailState("Could not prep SDK call %s", szGetGlobalVars);
}

char szAppendCriteria[] = "AI_CriteriaSet::AppendCriteria";
StartPrepSDKCall(SDKCall_Raw);
if (!PrepSDKCall_SetFromConf(pGameConfig, SDKConf_Signature, szAppendCriteria))
LogMessage("Could not obtain gamedata offset %s", szAppendCriteria);
else
{
PrepSDKCall_AddParameter(SDKType_String, SDKPass_Pointer);
PrepSDKCall_AddParameter(SDKType_String, SDKPass_Pointer);
PrepSDKCall_AddParameter(SDKType_Float, SDKPass_Plain);
if (!(g_pAppendCriteria = EndPrepSDKCall())) SetFailState("Could not prep SDK call %s", szAppendCriteria);
}

#if defined SRCCOOP_BLACKMESA
InitClassdefBM(pGameConfig);
Expand Down Expand Up @@ -1193,6 +1206,23 @@ methodmap CSceneEntity < CPointEntity
// Could also be a "scripted_scene`.
return CSceneEntity(CreateEntityByName("logic_choreographed_scene"));
}

public float GetCurrentTime()
{
return GetEntPropFloat(this.GetEntIndex(), Prop_Data, "m_flCurrentTime");
}
public void SetCurrentTime(const float flCurrentTime)
{
SetEntPropFloat(this.GetEntIndex(), Prop_Data, "m_flCurrentTime", flCurrentTime);
}
public float GetForceClientTime()
{
return GetEntPropFloat(this.GetEntIndex(), Prop_Data, "m_flForceClientTime");
}
public void SetForceClientTime(const float flForceClientTime)
{
SetEntPropFloat(this.GetEntIndex(), Prop_Data, "m_flForceClientTime", flForceClientTime);
}
}

methodmap CSoundEnt < CPointEntity
Expand Down Expand Up @@ -3174,4 +3204,21 @@ methodmap CGlobalVarsBase
methodmap CGlobalVars < CGlobalVarsBase
{

}

methodmap AI_CriteriaSet
{
public AI_CriteriaSet(const Address pAddress)
{
return view_as<AI_CriteriaSet>(pAddress);
}

public Address GetAddress()
{
return view_as<Address>(this);
}
public void AppendCriteria(const char[] szCriteria, const char[] szValue, const float flWeight = 1.0)
{
SDKCall(g_pAppendCriteria, this.GetAddress(), szCriteria, szValue, flWeight);
}
}
35 changes: 35 additions & 0 deletions scripting/include/srccoop/entitypatch.inc
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,41 @@ public MRESReturn Hook_FindNamedEntity(int _this, Handle hReturn, Handle hParams
return MRES_Ignored;
}

//------------------------------------------------------
// NPCs that use the response system https://developer.valvesoftware.com/wiki/Response_System
// The original function calls `AI_GetSinglePlayer()` which will end up returning null.
//------------------------------------------------------
public MRESReturn Hook_ExpresserHost_DoModifyOrAppendCriteriaPost(Handle hParams)
{
CBaseEntity pSpeaker = CBaseEntity(DHookGetParam(hParams, 1));
CBasePlayer pPlayer = GetNearestPlayerPreferAlive(pSpeaker, true);
if (pPlayer != NULL_CBASEENTITY)
{
AI_CriteriaSet pCriteria = AI_CriteriaSet(DHookGetParam(hParams, 2));

float vec3SpeakerPosition[3];
pSpeaker.GetAbsOrigin(vec3SpeakerPosition);

float vec3PlayerPosition[3];
pPlayer.GetAbsOrigin(vec3PlayerPosition);

int iLength = RoundFloat(GetVectorDistance(vec3SpeakerPosition, vec3PlayerPosition));

// This is rounded from a float to string to save stack space for string formatting.
char szLength[16];
IntToString(iLength, szLength, sizeof(szLength));
pCriteria.AppendCriteria("distancetoplayer", szLength);

// Original SDK code checks if the player is looking at the speaker.
// This is not used as the closest player that is alive may not be looking at the speaker while other players are.
if (/*pPlayer.FInViewConeEntity(pSpeaker) &&*/ pPlayer.FVisibleEntity(pSpeaker))
{
pCriteria.AppendCriteria("seenbyplayer", "1");
}
}
return MRES_Ignored;
}

//------------------------------------------------------
// CChangelevel - trigger_changelevel
// Hook to handle mapchanges
Expand Down
3 changes: 2 additions & 1 deletion scripting/include/srccoop/globals.inc
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,10 @@ DynamicHook hkIsPlayerAlly;
DynamicHook hkProtoSniperSelectSchedule;
#endif

#if defined ENTPATCH_FIND_NAMED_ENTITY
#if defined ENTPATCH_NPC_DIALOGUE
DynamicHook hkFindNamedEntity;
DynamicHook hkFindNamedEntityClosest;
DynamicDetour hkExpresserHostDoModifyOrAppendCriteria;
#endif

#if defined SRCCOOP_BLACKMESA
Expand Down
5 changes: 3 additions & 2 deletions scripting/srccoop.sp
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,10 @@ void LoadGameData()
LoadDHookVirtual(pGameConfig, hkIsPlayerAlly, "CAI_BaseNPC::IsPlayerAlly");
#endif

#if defined ENTPATCH_FIND_NAMED_ENTITY
#if defined ENTPATCH_NPC_DIALOGUE
LoadDHookVirtual(pGameConfig, hkFindNamedEntity, "CSceneEntity::FindNamedEntity");
LoadDHookVirtual(pGameConfig, hkFindNamedEntityClosest, "CSceneEntity::FindNamedEntityClosest");
LoadDHookDetour(pGameConfig, hkExpresserHostDoModifyOrAppendCriteria, "CAI_ExpresserHost_NPC_DoModifyOrAppendCriteria", _, Hook_ExpresserHost_DoModifyOrAppendCriteriaPost);
#endif

#if defined ENTPATCH_SNIPER
Expand Down Expand Up @@ -822,7 +823,7 @@ public void OnEntityCreated(int iEntIndex, const char[] szClassname)
}
#endif

#if defined ENTPATCH_FIND_NAMED_ENTITY
#if defined ENTPATCH_NPC_DIALOGUE
if ((strcmp(szClassname, "instanced_scripted_scene", false) == 0) ||
(strcmp(szClassname, "logic_choreographed_scene", false) == 0) ||
(strcmp(szClassname, "scripted_scene", false) == 0))
Expand Down