Skip to content

Commit

Permalink
Merge pull request #556 from CrosRoad95/improvments/vehicle-damage
Browse files Browse the repository at this point in the history
Tweak vehicle damage logic, add more test and  test commands
  • Loading branch information
NanoBob authored Nov 1, 2024
2 parents 2e1c5a1 + 25fdce8 commit d0f27ef
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 16 deletions.
6 changes: 0 additions & 6 deletions SlipeServer.Console/Logic/ServerTestLogic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
34 changes: 34 additions & 0 deletions SlipeServer.Console/Resources/TestResource/test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
53 changes: 53 additions & 0 deletions SlipeServer.Example/ServerExampleLogic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Packets.Enums.VehicleDoor>())
{
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<Packets.Enums.VehiclePanel>())
{
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<Packets.Enums.VehicleWheel>())
{
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<Player> callback)
Expand Down
25 changes: 25 additions & 0 deletions SlipeServer.Server.Tests/Unit/Elements/Vehicles/VehicleTests.cs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -22,11 +24,34 @@ 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);
vehicle.Health.Should().Be(1000);
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"]);
}
}
35 changes: 25 additions & 10 deletions SlipeServer.Server/Elements/Vehicle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit d0f27ef

Please sign in to comment.