Skip to content

Commit

Permalink
version 1.5
Browse files Browse the repository at this point in the history
-Fixed a log error.
-Fixed model error on console.
-ResourcePrecacher is not needed.
-Upgraded cssharp version to atleast 179.
-Set ParachuteModelEnabled to true by default.
-Code optimizations.
  • Loading branch information
Franc1sco committed Apr 1, 2024
1 parent 08fe1b7 commit ec23615
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 57 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
obj
.vs
.vs
src/bin
*.sln
13 changes: 3 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,12 @@
Parachute function when you keep pressed E on the air.

### Requirements
* [CounterStrikeSharp](https://github.com/roflmuffin/CounterStrikeSharp/) (version 139 or higher)
* [PrecaheResource](https://github.com/KillStr3aK/ResourcePrecacher/) for parachute model.
* [CounterStrikeSharp](https://github.com/roflmuffin/CounterStrikeSharp/) (version 179 or higher)

### Installation

Drag and drop from [releases](https://github.com/Franc1sco/CS2-Parachute/releases) to game/csgo/addons/counterstrikesharp/plugins

If you want to use ParachuteModel you need to precache the parachute model "models/props_survival/parachute/chute.vmdl" using [PrecaheResource](https://github.com/KillStr3aK/ResourcePrecacher/) and set ParachuteModelEnabled to true on the json config.

### Configuration

Configure the file parachute.json generated on addons/counterstrikesharp/configs/plugins/Parachute
Expand All @@ -23,7 +20,7 @@ Configure the file parachute.json generated on addons/counterstrikesharp/configs
"FallSpeed": 100,
"AccessFlag": "@css/vip",
"TeleportTicks": 300,
"ParachuteModelEnabled": false,
"ParachuteModelEnabled": true,
"ConfigVersion": 1
}
```
Expand All @@ -32,9 +29,5 @@ Configure the file parachute.json generated on addons/counterstrikesharp/configs
* Linear - 0: disables linear fallspeed - 1: enables it
* FallSpeed - speed of the fall when you use the parachute
* TeleportTicks - 300: ticks until perform a teleport (for prevent console spam).
* ParachuteModelEnabled - false: Add a parachute model while using it.
* ParachuteModelEnabled - true: Add a parachute model while using it.
* AccessFlag - access required for can use parachuse, leave blank "" for public access.

### TODO

Take ideas from https://forums.alliedmods.net/showthread.php?p=580269 like add option for buy a parachute.
116 changes: 71 additions & 45 deletions src/Parachute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Core.Attributes;
using CounterStrikeSharp.API.Modules.Admin;
using CounterStrikeSharp.API.Modules.Entities;
using CounterStrikeSharp.API.Modules.Entities.Constants;
using System.Text.Json.Serialization;

Expand All @@ -16,24 +15,24 @@ public class ConfigGen : BasePluginConfig
[JsonPropertyName("FallSpeed")] public float FallSpeed { get; set; } = 100;
[JsonPropertyName("AccessFlag")] public string AccessFlag { get; set; } = "";
[JsonPropertyName("TeleportTicks")] public int TeleportTicks { get; set; } = 300;
[JsonPropertyName("ParachuteModelEnabled")] public bool ParachuteModelEnabled { get; set; } = false;
[JsonPropertyName("ParachuteModelEnabled")] public bool ParachuteModelEnabled { get; set; } = true;
[JsonPropertyName("ParachuteModel")] public string ParachuteModel { get; set; } = "models/props_survival/parachute/chute.vmdl";
}

[MinimumApiVersion(139)]
[MinimumApiVersion(179)]
public class Parachute : BasePlugin, IPluginConfig<ConfigGen>
{
public override string ModuleName => "CS2 Parachute";
public override string ModuleAuthor => "Franc1sco Franug";
public override string ModuleVersion => "1.4";
public override string ModuleVersion => "1.5";


public ConfigGen Config { get; set; } = null!;
public void OnConfigParsed(ConfigGen config) { Config = config; }

private readonly Dictionary<int?, bool> bUsingPara = new();
private readonly Dictionary<int?, int> gParaTicks = new();
private readonly Dictionary<int?, CBaseEntity?> gParaModel = new();
private readonly Dictionary<int, bool> bUsingPara = new();
private readonly Dictionary<int, int> gParaTicks = new();
private readonly Dictionary<int, CBaseEntity?> gParaModel = new();

public override void Load(bool hotReload)
{
Expand All @@ -47,17 +46,18 @@ public override void Load(bool hotReload)
{
Utilities.GetPlayers().ForEach(player =>
{
bUsingPara.Add(player.UserId, false);
gParaTicks.Add(player.UserId, 0);
gParaModel.Add(player.UserId, null);
bUsingPara.Add((int)player.Index, false);
gParaTicks.Add((int)player.Index, 0);
gParaModel.Add((int)player.Index, null);
});
}

if (Config.ParachuteModelEnabled)
{
RegisterListener<Listeners.OnMapStart>(map =>
RegisterListener<Listeners.OnServerPrecacheResources>((manifest) =>
{
Server.PrecacheModel(Config.ParachuteModel);
manifest.AddResource(Config.ParachuteModel);

});
}

Expand All @@ -72,9 +72,9 @@ public override void Load(bool hotReload)
}
else
{
bUsingPara.Add(player.UserId, false);
gParaTicks.Add(player.UserId, 0);
gParaModel.Add(player.UserId, null);
bUsingPara.Add((int)player.Index, false);
gParaTicks.Add((int)player.Index, 0);
gParaModel.Add((int)player.Index, null);
return HookResult.Continue;
}
});
Expand All @@ -90,17 +90,17 @@ public override void Load(bool hotReload)
}
else
{
if (bUsingPara.ContainsKey(player.UserId))
if (bUsingPara.ContainsKey((int)player.Index))
{
bUsingPara.Remove(player.UserId);
bUsingPara.Remove((int)player.Index);
}
if (gParaTicks.ContainsKey(player.UserId))
if (gParaTicks.ContainsKey((int)player.Index))
{
gParaTicks.Remove(player.UserId);
gParaTicks.Remove((int)player.Index);
}
if (gParaModel.ContainsKey(player.UserId))
if (gParaModel.ContainsKey((int)player.Index))
{
gParaModel.Remove(player.UserId);
gParaModel.Remove((int)player.Index);
}
return HookResult.Continue;
}
Expand All @@ -120,14 +120,14 @@ public override void Load(bool hotReload)
&& (Config.AccessFlag == "" || AdminManager.PlayerHasPermissions(player, Config.AccessFlag)))
{
var buttons = player.Buttons;
if ((buttons & PlayerButtons.Use) != 0 && !player.PlayerPawn.Value.OnGroundLastTick)
if ((buttons & PlayerButtons.Use) != 0 && player.PlayerPawn?.Value?.OnGroundLastTick == false)
{
StartPara(player);

}
else if (bUsingPara[player.UserId])
else if (bUsingPara[(int)player.Index])
{
bUsingPara[player.UserId] = false;
bUsingPara[(int)player.Index] = false;
StopPara(player);
}
}
Expand All @@ -138,9 +138,9 @@ public override void Load(bool hotReload)
{
var player = @event.Userid;

if (bUsingPara[player.UserId])
if (bUsingPara.ContainsKey((int)player.Index) && bUsingPara[(int)player.Index])
{
bUsingPara[player.UserId] = false;
bUsingPara[(int)player.Index] = false;
StopPara(player);
}
return HookResult.Continue;
Expand All @@ -151,45 +151,52 @@ public override void Load(bool hotReload)
private void StopPara(CCSPlayerController player)
{
player.GravityScale = 1.0f;
gParaTicks[player.UserId] = 0;
if (gParaModel[player.UserId] != null && gParaModel[player.UserId].IsValid)
gParaTicks[(int)player.Index] = 0;
if (gParaModel[(int)player.Index] != null && gParaModel[(int)player.Index]!.IsValid)
{
gParaModel[player.UserId].Remove();
gParaModel[player.UserId] = null;
gParaModel[(int)player.Index]?.Remove();
gParaModel[(int)player.Index] = null;
}
}

private void StartPara(CCSPlayerController player)
{
if (!bUsingPara[player.UserId])
if (!bUsingPara[(int)player.Index])
{
bUsingPara[player.UserId] = true;
bUsingPara[(int)player.Index] = true;
player.GravityScale = 0.1f;
if (Config.ParachuteModelEnabled)
{
var entity = Utilities.CreateEntityByName<CBaseProp>("prop_dynamic_override");
var entity = Utilities.CreateEntityByName<CDynamicProp>("prop_dynamic_override");
if (entity != null && entity.IsValid)
{
entity.SetModel(Config.ParachuteModel);
entity.MoveType = MoveType_t.MOVETYPE_NOCLIP;
entity.Collision.CollisionGroup = (byte)CollisionGroup.COLLISION_GROUP_NONE;
entity.Collision.CollisionAttribute.CollisionGroup = (byte)CollisionGroup.COLLISION_GROUP_NONE;
// entity.RenderMode = RenderMode_t.kRenderNormal; // shadows test - dont work
entity.DispatchSpawn();

gParaModel[player.UserId] = entity;
entity.SetModel(Config.ParachuteModel);
//entity.ShadowStrength = 1.0f; // shadows test - dont work
//entity.ShapeType = 0; // shadows test - dont work
//entity.AcceptInput("EnableShadow"); // shadows test - dont work
//entity.AcceptInput("EnableReceivingFlashlight"); // shadows test - dont work

// CBaseEntity_SetParent(entity, player); // need fix
gParaModel[(int)player.Index] = entity;
}
}
}

var fallspeed = Config.FallSpeed * (-1.0f);
var isFallSpeed = false;
var velocity = player.PlayerPawn.Value.AbsVelocity;
if (velocity.Z >= fallspeed)
var velocity = player.PlayerPawn.Value?.AbsVelocity;
if (velocity?.Z >= fallspeed)
{
isFallSpeed = true;
}

if (velocity.Z < 0.0f)
if (velocity?.Z < 0.0f)
{
if (isFallSpeed && Config.Linear || Config.DecreaseVec == 0.0)
{
Expand All @@ -201,22 +208,41 @@ private void StartPara(CCSPlayerController player)
velocity.Z = velocity.Z + Config.DecreaseVec;
}

var position = player.PlayerPawn.Value.AbsOrigin!;
var angle = player.PlayerPawn.Value.AbsRotation!;
var position = player.PlayerPawn.Value?.AbsOrigin!;
var angle = player.PlayerPawn.Value?.AbsRotation!;

if (gParaTicks[player.UserId] > Config.TeleportTicks)
if (gParaTicks[(int)player.Index] > Config.TeleportTicks)
{
player.Teleport(position, angle, velocity);
gParaTicks[player.UserId] = 0;
gParaTicks[(int)player.Index] = 0;
}

if (gParaModel[player.UserId] != null && gParaModel[player.UserId].IsValid)
if (gParaModel[(int)player.Index] != null && gParaModel[(int)player.Index]!.IsValid)
{
gParaModel[player.UserId].Teleport(position, angle, velocity);
gParaModel[(int)player.Index]?.Teleport(position, angle, velocity);
}

++gParaTicks[player.UserId];
++gParaTicks[(int)player.Index];
}
}

/* // dont work not sure why
public static string setParentFuncWindowsSig = @"\x4D\x8B\xD9\x48\x85\xD2\x74\x2A";
public static string setParentFuncLinuxSig = @"\x48\x85\xF6\x74\x2A\x48\x8B\x47\x10\xF6\x40\x31\x02\x75\x2A\x48\x8B\x46\x10\xF6\x40\x31\x02\x75\x2A\xB8\x2A\x2A\x2A\x2A";
private static MemoryFunctionVoid<CBaseEntity, CBaseEntity, CUtlStringToken?, matrix3x4_t?> CBaseEntity_SetParentFunc
= new(RuntimeInformation.IsOSPlatform(OSPlatform.Linux) ? setParentFuncLinuxSig : setParentFuncWindowsSig);
public static void CBaseEntity_SetParent(CBaseEntity childrenEntity, CBaseEntity parentEntity)
{
if (!childrenEntity.IsValid || !parentEntity.IsValid) return;
var origin = parentEntity.AbsOrigin;
var angle = parentEntity.AbsRotation!;
CBaseEntity_SetParentFunc.Invoke(childrenEntity, parentEntity, null, null);
// If not teleported, the childrenEntity will not follow the parentEntity correctly.
childrenEntity.Teleport(origin, angle, new Vector(IntPtr.Zero));
Console.WriteLine("CBaseEntity_SetParent() done!");
}*/
}

2 changes: 1 addition & 1 deletion src/Parachute.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="CounterStrikeSharp.API" Version="1.0.139" />
<PackageReference Include="CounterStrikeSharp.API" Version="1.0.179" />
</ItemGroup>
</Project>

0 comments on commit ec23615

Please sign in to comment.