From ddc69f89ae5403cda822aca396a19ad59ad7d141 Mon Sep 17 00:00:00 2001 From: Sebastian Jura <22455534+CrosRoad95@users.noreply.github.com> Date: Sat, 19 Oct 2024 16:53:21 +0200 Subject: [PATCH 1/2] Tweak vehicle damage logic, add more test and test commands --- SlipeServer.Console/Logic/ServerTestLogic.cs | 6 --- .../Resources/TestResource/test.lua | 34 ++++++++++++ SlipeServer.Example/ServerExampleLogic.cs | 53 +++++++++++++++++++ .../ProjectileScriptDefinitions.cs | 16 ++++++ .../Unit/Elements/Vehicles/VehicleTests.cs | 25 +++++++++ SlipeServer.Server/Elements/Vehicle.cs | 35 ++++++++---- 6 files changed, 153 insertions(+), 16 deletions(-) create mode 100644 SlipeServer.Scripting/Definitions/ProjectileScriptDefinitions.cs diff --git a/SlipeServer.Console/Logic/ServerTestLogic.cs b/SlipeServer.Console/Logic/ServerTestLogic.cs index 478d7e83..c1256397 100644 --- a/SlipeServer.Console/Logic/ServerTestLogic.cs +++ b/SlipeServer.Console/Logic/ServerTestLogic.cs @@ -1015,12 +1015,6 @@ void Player_Disconnected(Player sender, PlayerQuitEventArgs e) this.logger.LogInformation("Starting Slipe Lua test resource for {playerName} took {milliseconds}ms", args.Player.Name, stopwatch.ElapsedMilliseconds); }; - this.commandService.AddCommand("fixmyveh").Triggered += (source, args) => - { - args.Player.Vehicle?.Fix(); - this.chatBox.OutputTo(args.Player, "Vehicle fixed"); - }; - this.commandService.AddCommand("blowup").Triggered += (source, args) => { args.Player.Vehicle?.BlowUp(); diff --git a/SlipeServer.Console/Resources/TestResource/test.lua b/SlipeServer.Console/Resources/TestResource/test.lua index 512d13cd..b4963921 100644 --- a/SlipeServer.Console/Resources/TestResource/test.lua +++ b/SlipeServer.Console/Resources/TestResource/test.lua @@ -58,4 +58,38 @@ setFPSLimit(60) addCommandHandler("triggerLatent", function(command, ...) outputChatBox("Sending latent event...") triggerLatentServerEvent("exampleLatentTrigger",5000,false,root,string.rep("a", 10000)) +end) + +addCommandHandler("myvehprintdamageclient", function() + local vehicle = getPedOccupiedVehicle(localPlayer) + if not vehicle then + outputChatBox("You are not in a vehicle.") + return + end + + outputChatBox("List of damaged vehicle parts:") + + outputChatBox("Doors:") + for i = 0, 5 do + local state = getVehicleDoorState(vehicle, i) + if state ~= 0 then + outputChatBox(" " .. i .. " - " .. state) + end + end + + outputChatBox("Panels:") + for i = 0, 6 do + local state = getVehiclePanelState(vehicle, i) + if state ~= 0 then + outputChatBox(" " .. i .. " - " .. state) + end + end + + outputChatBox("Wheels:") + local states = {getVehicleWheelStates(vehicle, i)} + for i = 1, 4 do + if states[i] ~= 0 then + outputChatBox(" " .. i .. " - " .. states[i]) + end + end end) \ No newline at end of file diff --git a/SlipeServer.Example/ServerExampleLogic.cs b/SlipeServer.Example/ServerExampleLogic.cs index fa73f447..b331ffed 100644 --- a/SlipeServer.Example/ServerExampleLogic.cs +++ b/SlipeServer.Example/ServerExampleLogic.cs @@ -17,6 +17,59 @@ public ServerExampleLogic(CommandService commandService, ChatBox chatBox) { this.chatBox.OutputTo(player, "Hello world"); }); + + AddVehiclesCommands(); + } + + private void AddVehiclesCommands() + { + AddCommand("myvehprintdamage", player => + { + var vehicle = player.Vehicle!; + + this.chatBox.OutputTo(player, "List of damaged vehicles parts:"); + this.chatBox.OutputTo(player, "Doors:"); + foreach (var item in Enum.GetValues()) + { + var state = vehicle.GetDoorState(item); + if(state != Packets.Enums.VehicleDoorState.ShutIntact) + this.chatBox.OutputTo(player, $" {item} - {state}"); + } + + this.chatBox.OutputTo(player, "Panels:"); + foreach (var item in Enum.GetValues()) + { + var state = vehicle.GetPanelState(item); + if(state != Packets.Enums.VehiclePanelState.Undamaged) + this.chatBox.OutputTo(player, $" {item} - {state}"); + } + + this.chatBox.OutputTo(player, "Wheels:"); + foreach (var item in Enum.GetValues()) + { + var state = vehicle.GetWheelState(item); + if(state != Packets.Enums.VehicleWheelState.Inflated) + this.chatBox.OutputTo(player, $" {item} - {state}"); + } + }); + + AddCommand("damagemyveh", player => + { + var vehicle = player.Vehicle!; + + vehicle.SetDoorState(Packets.Enums.VehicleDoor.Hood, Packets.Enums.VehicleDoorState.Missing); + vehicle.SetPanelState(Packets.Enums.VehiclePanel.FrontBumper, Packets.Enums.VehiclePanelState.Damaged3); + vehicle.SetWheelState(Packets.Enums.VehicleWheel.FrontLeft, Packets.Enums.VehicleWheelState.FallenOff); + this.chatBox.OutputTo(player, "Vehicle damaged!"); + }); + + AddCommand("fixmyveh", player => + { + var vehicle = player.Vehicle!; + + vehicle.Fix(); + this.chatBox.OutputTo(player, "Vehicle fixed"); + }); } private void AddCommand(string command, Action callback) diff --git a/SlipeServer.Scripting/Definitions/ProjectileScriptDefinitions.cs b/SlipeServer.Scripting/Definitions/ProjectileScriptDefinitions.cs new file mode 100644 index 00000000..87aacc2b --- /dev/null +++ b/SlipeServer.Scripting/Definitions/ProjectileScriptDefinitions.cs @@ -0,0 +1,16 @@ +using SlipeServer.Server.Elements; + +namespace SlipeServer.Scripting.Definitions; + +public class ProjectileScriptDefinitions +{ + public ProjectileScriptDefinitions() + { + } + + [ScriptFunctionDefinition("detonateSatchels")] + public void DetonateSatchels(Player player) + { + + } +} diff --git a/SlipeServer.Server.Tests/Unit/Elements/Vehicles/VehicleTests.cs b/SlipeServer.Server.Tests/Unit/Elements/Vehicles/VehicleTests.cs index 42354e42..64ca053c 100644 --- a/SlipeServer.Server.Tests/Unit/Elements/Vehicles/VehicleTests.cs +++ b/SlipeServer.Server.Tests/Unit/Elements/Vehicles/VehicleTests.cs @@ -1,6 +1,8 @@ using FluentAssertions; +using FluentAssertions.Execution; using SlipeServer.Server.Elements; using SlipeServer.Server.Enums; +using System.Linq; using System.Numerics; using Xunit; @@ -22,6 +24,8 @@ public void VehicleRespawn_ShouldResetDefaults() vehicle.Respawn(); + using var _ = new AssertionScope(); + vehicle.Position.Should().Be(Vector3.Zero); vehicle.Rotation.Should().Be(Vector3.Zero); vehicle.IsLandingGearDown.Should().Be(true); @@ -29,4 +33,25 @@ public void VehicleRespawn_ShouldResetDefaults() vehicle.BlownState.Should().Be(VehicleBlownState.Intact); vehicle.Velocity.Should().Be(Vector3.Zero); } + + [Fact] + public void VehicleDamageEvents() + { + using var _ = new AssertionScope(); + + var vehicle = new Vehicle(VehicleModel.Alpha, Vector3.Zero); + using var monitor = vehicle.Monitor(); + + vehicle.Fix(); + monitor.OccurredEvents.Select(x => x.EventName).Should().BeEquivalentTo(["Fixed"]); // Fixing already fixed, not damaged vehicle should raise only "fixed" event. + + vehicle.Health = 100; + vehicle.SetDoorState(Packets.Enums.VehicleDoor.Hood, Packets.Enums.VehicleDoorState.Missing); + vehicle.SetWheelState(Packets.Enums.VehicleWheel.FrontLeft, Packets.Enums.VehicleWheelState.FallenOff); + vehicle.SetPanelState(Packets.Enums.VehiclePanel.FrontBumper, Packets.Enums.VehiclePanelState.Damaged3); + + monitor.Clear(); + vehicle.Fix(); + monitor.OccurredEvents.Select(x => x.EventName).Should().BeEquivalentTo(["HealthChanged", "DoorStateChanged", "WheelStateChanged", "PanelStateChanged", "Fixed"]); + } } diff --git a/SlipeServer.Server/Elements/Vehicle.cs b/SlipeServer.Server/Elements/Vehicle.cs index d0624e8e..92b4189a 100644 --- a/SlipeServer.Server/Elements/Vehicle.cs +++ b/SlipeServer.Server/Elements/Vehicle.cs @@ -614,32 +614,47 @@ public void Fix() public void SetDoorState(VehicleDoor door, VehicleDoorState state, bool spawnFlyingComponent = false) { - this.doorStates[(int)door] = (byte)state; - this.DoorStateChanged?.Invoke(this, new VehicleDoorStateChangedArgs(this, door, state, spawnFlyingComponent)); + if (this.doorStates[(int)door] != (byte)state) + { + this.doorStates[(int)door] = (byte)state; + this.DoorStateChanged?.Invoke(this, new VehicleDoorStateChangedArgs(this, door, state, spawnFlyingComponent)); + } } public void SetWheelState(VehicleWheel wheel, VehicleWheelState state) { - this.wheelStates[(int)wheel] = (byte)state; - this.WheelStateChanged?.Invoke(this, new VehicleWheelStateChangedArgs(this, wheel, state)); + if (this.wheelStates[(int)wheel] != (byte)state) + { + this.wheelStates[(int)wheel] = (byte)state; + this.WheelStateChanged?.Invoke(this, new VehicleWheelStateChangedArgs(this, wheel, state)); + } } public void SetPanelState(VehiclePanel panel, VehiclePanelState state) { - this.panelStates[(int)panel] = (byte)state; - this.PanelStateChanged?.Invoke(this, new VehiclePanelStateChangedArgs(this, panel, state)); + if (this.panelStates[(int)panel] != (byte)state) + { + this.panelStates[(int)panel] = (byte)state; + this.PanelStateChanged?.Invoke(this, new VehiclePanelStateChangedArgs(this, panel, state)); + } } public void SetLightState(VehicleLight light, VehicleLightState state) { - this.lightStates[(int)light] = (byte)state; - this.LightStateChanged?.Invoke(this, new VehicleLightStateChangedArgs(this, light, state)); + if (this.lightStates[(int)light] != (byte)state) + { + this.lightStates[(int)light] = (byte)state; + this.LightStateChanged?.Invoke(this, new VehicleLightStateChangedArgs(this, light, state)); + } } public void SetDoorOpenRatio(VehicleDoor door, float ratio, uint time = 0) { - this.doorRatios[(int)door] = ratio; - this.DoorOpenRatioChanged?.Invoke(this, new VehicleDoorOpenRatioChangedArgs(this, door, ratio, time)); + if (this.doorRatios[(int)door] != ratio) + { + this.doorRatios[(int)door] = ratio; + this.DoorOpenRatioChanged?.Invoke(this, new VehicleDoorOpenRatioChangedArgs(this, door, ratio, time)); + } } public void TriggerPushed(Player player) From 25fdce8da2ae937b8f08eb78bc1845b2096b84dc Mon Sep 17 00:00:00 2001 From: Sebastian Jura <22455534+CrosRoad95@users.noreply.github.com> Date: Sat, 19 Oct 2024 16:54:44 +0200 Subject: [PATCH 2/2] Addendum --- .../Definitions/ProjectileScriptDefinitions.cs | 16 ---------------- 1 file changed, 16 deletions(-) delete mode 100644 SlipeServer.Scripting/Definitions/ProjectileScriptDefinitions.cs diff --git a/SlipeServer.Scripting/Definitions/ProjectileScriptDefinitions.cs b/SlipeServer.Scripting/Definitions/ProjectileScriptDefinitions.cs deleted file mode 100644 index 87aacc2b..00000000 --- a/SlipeServer.Scripting/Definitions/ProjectileScriptDefinitions.cs +++ /dev/null @@ -1,16 +0,0 @@ -using SlipeServer.Server.Elements; - -namespace SlipeServer.Scripting.Definitions; - -public class ProjectileScriptDefinitions -{ - public ProjectileScriptDefinitions() - { - } - - [ScriptFunctionDefinition("detonateSatchels")] - public void DetonateSatchels(Player player) - { - - } -}