Skip to content

Commit

Permalink
Add support for custom vehicle soundscripts (#20)
Browse files Browse the repository at this point in the history
* Allow loading a custom soundscript for each vehicle

* Precache script sound entries

* Properly mark LoadSoundscript as removed

* Add script sound to downloads table

* Revert "Add script sound to downloads table"

This reverts commit a44574c.

* Bump version

* Fix double word in error

* Update comment
  • Loading branch information
Mikusch authored Jul 8, 2021
1 parent 3161d38 commit 18ed1f0
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 91 deletions.
21 changes: 11 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,17 @@ the [Vehicle Scripts for Source](https://steamcommunity.com/sharedfiles/filedeta
{
"0"
{
"id" "example_vehicle"
"name" "#Vehicle_ExampleVehicle"
"model" "models/vehicles/example_vehicle.mdl"
"script" "scripts/vehicles/example_vehicle.txt"
"type" "car_wheels"
"skins" "0,1,2"
"key_hint" "#Hint_VehicleKeys_Car"
"lock_speed" "10.0"
"is_passenger_visible" "1"
"horn_sound" "sounds/vehicles/example_horn.wav"
"id" "example_vehicle"
"name" "#Vehicle_ExampleVehicle"
"model" "models/vehicles/example_vehicle.mdl"
"script" "scripts/vehicles/example_vehicle.txt"
"type" "car_wheels"
"soundscript" "scripts/example_soundscript.txt"
"skins" "0,1,2"
"key_hint" "#Hint_VehicleKeys_Car"
"lock_speed" "10.0"
"is_passenger_visible" "1"
"horn_sound" "sounds/vehicles/example_horn.wav"
"downloads"
{
"0" "models/vehicles/example_vehicle.dx80.vtx"
Expand Down
47 changes: 25 additions & 22 deletions addons/sourcemod/configs/vehicles/vehicles.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
// <script> - Vehicle script path
// <type> - Vehicle type
// - Possible values: car_wheels, car_raycast, jetski_raycast, airboat_raycast
// [soundscript] - Custom soundscript to load for this vehicle, requires the LoadSoundscript extension (def. "")
// [skins] - Vehicle model skins, separated by commas (def. "0")
// [key_hint] - Key hint displayed to the player entering the vehicle (defined in vehicles.phrases.txt) (def. "")
// [lock_speed] - Vehicle must be going slower than this for player to enter or exit, in in/sec (def. "10.0")
Expand All @@ -21,39 +22,41 @@
{
"0"
{
"id" "hl2_jeep"
"name" "#Vehicle_HL2_Jeep"
"model" "models/buggy.mdl"
"script" "scripts/vehicles/jeep_test.txt"
"type" "car_wheels"
"key_hint" "#Hint_VehicleKeys_Car"
"id" "hl2_jeep"
"name" "#Vehicle_HL2_Jeep"
"model" "models/buggy.mdl"
"script" "scripts/vehicles/jeep_test.txt"
"type" "car_wheels"
"soundscript" "scripts/game_sounds_vehicles.txt"
"key_hint" "#Hint_VehicleKeys_Car"
}

"1"
{
"id" "hl2_airboat"
"name" "#Vehicle_HL2_Airboat"
"model" "models/airboat.mdl"
"script" "scripts/vehicles/airboat.txt"
"type" "airboat_raycast"
"key_hint" "#Hint_VehicleKeys_Airboat"
"id" "hl2_airboat"
"name" "#Vehicle_HL2_Airboat"
"model" "models/airboat.mdl"
"script" "scripts/vehicles/airboat.txt"
"type" "airboat_raycast"
"key_hint" "#Hint_VehicleKeys_Airboat"
}

//
// Example: (do not put // in front of real lines, as // means 'comment')
//
// "0"
// {
// "id" "example_vehicle"
// "name" "#Vehicle_ExampleVehicle"
// "model" "models/vehicles/example_vehicle.mdl"
// "script" "scripts/vehicles/example_vehicle.txt"
// "type" "car_wheels"
// "skins" "0,1,2"
// "key_hint" "#Hint_VehicleKeys_Car"
// "lock_speed" "10.0"
// "is_passenger_visible" "1"
// "horn_sound" "sounds/vehicles/example_horn.wav"
// "id" "example_vehicle"
// "name" "#Vehicle_ExampleVehicle"
// "model" "models/vehicles/example_vehicle.mdl"
// "script" "scripts/vehicles/example_vehicle.txt"
// "type" "car_wheels"
// "soundscript" "scripts/example_soundscript.txt"
// "skins" "0,1,2"
// "key_hint" "#Hint_VehicleKeys_Car"
// "lock_speed" "10.0"
// "is_passenger_visible" "1"
// "horn_sound" "sounds/vehicles/example_horn.wav"
// "downloads"
// {
// "0" "models/vehicles/example_vehicle.dx80.vtx"
Expand Down
152 changes: 93 additions & 59 deletions addons/sourcemod/scripting/vehicles.sp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
#pragma semicolon 1
#pragma newdecls required

#define PLUGIN_VERSION "2.0.1"
#define PLUGIN_VERSION "2.1.0"
#define PLUGIN_AUTHOR "Mikusch"
#define PLUGIN_URL "https://github.com/Mikusch/source-vehicles"

Expand All @@ -49,18 +49,55 @@ enum VehicleType
VEHICLE_TYPE_AIRBOAT_RAYCAST = (1 << 3)
}

bool g_LoadSoundscript;

ConVar vehicle_config_path;
ConVar vehicle_physics_damage_modifier;
ConVar vehicle_passenger_damage_modifier;
ConVar vehicle_enable_entry_exit_anims;
ConVar vehicle_enable_horns;

GlobalForward g_ForwardOnVehicleSpawned;
GlobalForward g_ForwardOnVehicleDestroyed;

DynamicHook g_DHookSetPassenger;
DynamicHook g_DHookIsPassengerVisible;
DynamicHook g_DHookHandlePassengerEntry;
DynamicHook g_DHookGetExitAnimToUse;
DynamicHook g_DHookGetInVehicle;
DynamicHook g_DHookLeaveVehicle;

Handle g_SDKCallVehicleSetupMove;
Handle g_SDKCallCanEnterVehicle;
Handle g_SDKCallGetAttachmentLocal;
Handle g_SDKCallGetVehicleEnt;
Handle g_SDKCallHandlePassengerEntry;
Handle g_SDKCallHandlePassengerExit;
Handle g_SDKCallHandleEntryExitFinish;
Handle g_SDKCallStudioFrameAdvance;
Handle g_SDKCallGetInVehicle;

ArrayList g_AllVehicles;
ArrayList g_VehicleProperties;

char g_OldAllowPlayerUse[8];
char g_OldTurboPhysics[8];

bool g_ClientIsUsingHorn[MAXPLAYERS + 1];

enum struct VehicleConfig
{
char id[256]; /**< Unique identifier of the vehicle */
char name[256]; /**< Display name of the vehicle */
char model[PLATFORM_MAX_PATH]; /**< Vehicle model */
char script[PLATFORM_MAX_PATH]; /**< Vehicle script path */
VehicleType type; /**< The type of vehicle */
ArrayList skins; /**< Model skins */
char key_hint[256]; /**< Vehicle key hint */
float lock_speed; /**< Vehicle lock speed */
bool is_passenger_visible; /**< Whether the passenger is visible */
char horn_sound[PLATFORM_MAX_PATH]; /**< Custom horn sound */
char id[256]; /**< Unique identifier of the vehicle */
char name[256]; /**< Display name of the vehicle */
char model[PLATFORM_MAX_PATH]; /**< Vehicle model */
char script[PLATFORM_MAX_PATH]; /**< Vehicle script path */
VehicleType type; /**< The type of vehicle */
char soundscript[PLATFORM_MAX_PATH]; /**< Custom soundscript */
ArrayList skins; /**< Model skins */
char key_hint[256]; /**< Vehicle key hint */
float lock_speed; /**< Vehicle lock speed */
bool is_passenger_visible; /**< Whether the passenger is visible */
char horn_sound[PLATFORM_MAX_PATH]; /**< Custom horn sound */

void ReadConfig(KeyValues kv)
{
Expand All @@ -82,6 +119,30 @@ enum struct VehicleConfig
else if (type[0] != '\0')
LogError("Invalid vehicle type '%s'", type);

kv.GetString("soundscript", this.soundscript, PLATFORM_MAX_PATH, this.soundscript);
if (this.soundscript[0] != '\0')
{
if (g_LoadSoundscript)
{
#if defined _loadsoundscript_included
SoundScript soundscript = LoadSoundScript(this.soundscript);
for (int i = 0; i < soundscript.Count; i++)
{
SoundEntry entry = soundscript.GetSound(i);
char soundname[256];
entry.GetName(soundname, sizeof(soundname));
PrecacheScriptSound(soundname);
}
#else
LogMessage("Failed to load vehicle soundscript '%s' because the plugin was compiled without the LoadSoundscript include", this.soundscript);
#endif
}
else
{
LogMessage("Failed to load vehicle soundscript '%s' because the LoadSoundscript extension could not be found", this.soundscript);
}
}

this.skins = new ArrayList();

char skins[128];
Expand Down Expand Up @@ -147,40 +208,6 @@ enum struct VehicleProperties
}
}

ConVar vehicle_config_path;
ConVar vehicle_physics_damage_modifier;
ConVar vehicle_passenger_damage_modifier;
ConVar vehicle_enable_entry_exit_anims;
ConVar vehicle_enable_horns;

GlobalForward g_ForwardOnVehicleSpawned;
GlobalForward g_ForwardOnVehicleDestroyed;

DynamicHook g_DHookSetPassenger;
DynamicHook g_DHookIsPassengerVisible;
DynamicHook g_DHookHandlePassengerEntry;
DynamicHook g_DHookGetExitAnimToUse;
DynamicHook g_DHookGetInVehicle;
DynamicHook g_DHookLeaveVehicle;

Handle g_SDKCallVehicleSetupMove;
Handle g_SDKCallCanEnterVehicle;
Handle g_SDKCallGetAttachmentLocal;
Handle g_SDKCallGetVehicleEnt;
Handle g_SDKCallHandlePassengerEntry;
Handle g_SDKCallHandlePassengerExit;
Handle g_SDKCallHandleEntryExitFinish;
Handle g_SDKCallStudioFrameAdvance;
Handle g_SDKCallGetInVehicle;

ArrayList g_AllVehicles;
ArrayList g_VehicleProperties;

char g_OldAllowPlayerUse[8];
char g_OldTurboPhysics[8];

bool g_ClientIsUsingHorn[MAXPLAYERS + 1];

methodmap Vehicle
{
public Vehicle(int entity)
Expand Down Expand Up @@ -245,20 +272,6 @@ public void OnPluginStart()
LoadTranslations("common.phrases");
LoadTranslations("vehicles.phrases");

//Load common vehicle sounds
if (LibraryExists("LoadSoundscript"))
{
#if defined _loadsoundscript_included
LoadSoundScript("scripts/game_sounds_vehicles.txt");
#else
LogMessage("LoadSoundscript extension was found but plugin was compiled without support for it, vehicles won't have sounds");
#endif
}
else
{
LogMessage("LoadSoundScript extension could not be found, vehicles won't have sounds");
}

//Create plugin convars
vehicle_config_path = CreateConVar("vehicle_config_path", "configs/vehicles/vehicles.cfg", "Path to vehicle configuration file, relative to the SourceMod folder");
vehicle_config_path.AddChangeHook(ConVarChanged_RefreshVehicleConfig);
Expand Down Expand Up @@ -334,6 +347,27 @@ public APLRes AskPluginLoad2(Handle myself, bool late, char[] error, int err_max
MarkNativeAsOptional("LoadSoundScript");
}

public void OnAllPluginsLoaded()
{
g_LoadSoundscript = LibraryExists("LoadSoundscript");
}

public void OnLibraryAdded(const char[] name)
{
if (StrEqual(name, "LoadSoundscript"))
{
g_LoadSoundscript = true;
}
}

public void OnLibraryRemoved(const char[] name)
{
if (StrEqual(name, "LoadSoundscript"))
{
g_LoadSoundscript = false;
}
}

public void OnMapStart()
{
SetupConVar("tf_allow_player_use", g_OldAllowPlayerUse, sizeof(g_OldAllowPlayerUse), "1");
Expand Down

0 comments on commit 18ed1f0

Please sign in to comment.