Skip to content

Commit

Permalink
Merge pull request #708 from starfi5h/pr-combatFix
Browse files Browse the repository at this point in the history
Combat balance change and bugfix
  • Loading branch information
starfi5h authored Sep 9, 2024
2 parents f0ad997 + 900049f commit 0af58c9
Show file tree
Hide file tree
Showing 18 changed files with 348 additions and 19 deletions.
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
## Changelog

0.9.10:
- @fakeboboliu: Support to connect server with WSS by specifying wss:// in the connect Uri
- @starfi5h: Sync Logistics Control Panel (I) entry list
- @starfi5h: Esc in multiplayer menu can now return to the upper-level
- @starfi5h: (Balance) When player killed, Drop half of item in inventory and increase CD from 1.5s to 5s
- @starfi5h: (Balance) Increase base alert range to player from 90 to 200
- @starfi5h: Space hive threat will now increase correctly when client attack relays by player's fleet
- @starfi5h: DF base/hive will now launch attack at player on remote empty planet too
- @starfi5h: Fix client player can't see the death animation of other clients
- @starfi5h: Fix DF base sometimes can't be destroyed in client
- @starfi5h: (Headless server) Stop relay landing when there are 7 or more working shield generators

0.9.9:
- @starfi5h: Fix multiplayer tab in the option window for DSP v0.10.30.23430
- @starfi5h: Separate error close button (x) and copy button
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ protected override void ProcessPacket(CombatStatDamagePacket packet, NebulaConne
{
if (IsHost)
{
Multiplayer.Session.Server.SendPacketExclude(packet, conn);
if (packet.TargetAstroId > 1000000)
{
Multiplayer.Session.Server.SendPacketExclude(packet, conn);
}
}

SkillTarget target;
Expand Down Expand Up @@ -48,7 +51,11 @@ protected override void ProcessPacket(CombatStatDamagePacket packet, NebulaConne

using (Multiplayer.Session.Combat.IsIncomingRequest.On())
{
GameMain.spaceSector.skillSystem.DamageObject(packet.Damage, packet.Slice, ref target, ref caster);
var skillSystem = GameMain.spaceSector.skillSystem;
var tmp = skillSystem.playerAlive;
skillSystem.playerAlive = true;
skillSystem.DamageObject(packet.Damage, packet.Slice, ref target, ref caster);
skillSystem.playerAlive = tmp;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ public class MechaAliveEventProcessor : PacketProcessor<MechaAliveEventPacket>
{
protected override void ProcessPacket(MechaAliveEventPacket packet, NebulaConnection conn)
{
if (IsHost)
{
Multiplayer.Session.Server.SendPacketExclude(packet, conn);
}

using (Multiplayer.Session.World.GetRemotePlayersModels(out var remotePlayersModels))
{
if (!remotePlayersModels.TryGetValue(packet.PlayerId, out var playerModel)) return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ protected override void ProcessPacket(StationUIInitialSyncRequest packet, Nebula

if (stationComponent == null)
{
Log.Error(
Log.Warn(
$"StationUIInitialSyncRequestProcessor: Unable to find requested station on planet {packet.PlanetId} with id {packet.StationId} and gid {packet.StationGId}");
return;
}
Expand Down
3 changes: 2 additions & 1 deletion NebulaPatcher/Patches/Dynamic/DFGBaseComponent_Patch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,8 @@ public static bool UpdateHatred_Prefix(DFGBaseComponent __instance, long gameTic
for (var pid = 0; pid < players.Length; pid++)
{
if (players[pid].planetId != planetId) continue;
if (((Vector3)enemyPool[__instance.enemyId].pos - players[pid].position).sqrMagnitude < 8100.0)
// Balance: Increase base player alert range from 90 to 200
if (((Vector3)enemyPool[__instance.enemyId].pos - players[pid].position).sqrMagnitude < 40000.0)
{
__instance.UnderAttack(players[pid].position, 50f, 120);
}
Expand Down
25 changes: 25 additions & 0 deletions NebulaPatcher/Patches/Dynamic/EnemyDFGroundSystem_Patch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,31 @@ public static bool ExecuteDeferredUnitFormation_Prefix(EnemyDFGroundSystem __ins
return true;
}

[HarmonyPrefix]
[HarmonyPatch(nameof(EnemyDFGroundSystem.CanEraseBase))]
public static bool CanEraseBase_Prefix(DFGBaseComponent _base, ref bool __result)
{
if (!Multiplayer.IsActive) return true;

if (_base == null || _base.id == 0)
{
__result = true;
return false;
}
// Skip __instance.builders.buffer[_base.builderId].sp check as it may have different value
var pbuilders = _base.pbuilders;
for (var i = 2; i < pbuilders.Length; i++)
{
if (pbuilders[i].instId > 0)
{
__result = false;
return false;
}
}
__result = true;
return false;
}

[HarmonyPrefix]
[HarmonyPatch(nameof(EnemyDFGroundSystem.NotifyEnemyKilled))]
public static bool NotifyEnemyKilled_Prefix()
Expand Down
4 changes: 0 additions & 4 deletions NebulaPatcher/Patches/Dynamic/PlanetData_Patch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,8 @@ public static bool UpdateDirtyMesh_Prefix(PlanetData __instance, int dirtyIdx, r
{
return true;
}
Log.Warn(__instance == GameMain.localPlanet
? "Local"
: "Remote" + $" PlanetData.UpdateDirtyMesh: meshes[{dirtyIdx}] is null");
__result = false;
return false;

}

[HarmonyPrefix]
Expand Down
2 changes: 1 addition & 1 deletion NebulaPatcher/Patches/Dynamic/PlayerAction_Death_Patch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ internal class PlayerAction_Death_Patch
[HarmonyPatch(nameof(PlayerAction_Death.Respawn))]
public static void Respawn_Prefix(PlayerAction_Death __instance, int _respawnMode)
{
if (!Multiplayer.IsActive) return;
if (!Multiplayer.IsActive || __instance.player != GameMain.mainPlayer) return;

if (!__instance.player.isAlive && !__instance.respawning)
{
Expand Down
17 changes: 17 additions & 0 deletions NebulaPatcher/Patches/Dynamic/Player_Patch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ public static bool Kill_Prefix(Player __instance)
{
Multiplayer.Session.Network.SendPacket(new MechaAliveEventPacket(
Multiplayer.Session.LocalPlayer.Id, MechaAliveEventPacket.EStatus.Kill));
ThrowItemsInInventory(__instance);
}
return true;
}
Expand All @@ -179,5 +180,21 @@ public static bool PrepareRedeploy_Prefix(Player __instance)
return false;
}

private static void ThrowItemsInInventory(Player player)
{
// Balance: Drop half of item in inventory when player killed
const float DROP_RATE = 0.5f;
for (var i = 0; i < player.package.size; i++)
{
var itemId = 0;
var itemCount = (int)(player.package.grids[i].count * DROP_RATE);
player.package.TakeItemFromGrid(i, ref itemId, ref itemCount, out var itemInc);
if (itemId > 0 && itemCount > 0)
{
player.ThrowTrash(itemId, itemCount, itemInc, 0, 0);
}
}
}

#endregion
}
31 changes: 24 additions & 7 deletions NebulaPatcher/Patches/Dynamic/SkillSystem_Patch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ public static void CollectPlayerStates_Postfix(SkillSystem __instance)
// Set those flags to false so AddSpaceEnemyHatred can add threat correctly for client's skill in host
__instance.playerIsSailing = false;
__instance.playerIsWarping = false;
// Set this flag to true so AddSpaceEnemyHatred can add threat correctly from craft/skill of other players even if host is dead (dedicated server)
__instance.playerAlive = true;
}

[HarmonyPostfix]
Expand Down Expand Up @@ -91,14 +93,29 @@ public static bool MechaEnergyShieldResist_Prefix(SkillSystem __instance, ref bo
[HarmonyPatch(nameof(SkillSystem.DamageObject))]
public static void DamageObject_Prefix(int damage, int slice, ref SkillTarget target, ref SkillTarget caster)
{
if (caster.type != ETargetType.Craft || target.type != ETargetType.Enemy
|| target.astroId <= 1000000 // Only sync for space target
if (!(caster.type == ETargetType.Craft || caster.type == ETargetType.Player)
|| target.type != ETargetType.Enemy
|| !Multiplayer.IsActive || Multiplayer.Session.Combat.IsIncomingRequest.Value) return;

var packet = new CombatStatDamagePacket(damage, slice, in target, in caster);
// Change the caster to player as craft (space fleet) is not sync yet
packet.CasterType = (short)ETargetType.Player;
packet.CasterId = Multiplayer.Session.LocalPlayer.Id;
Multiplayer.Session.Network.SendPacket(packet);
if (target.astroId > 1000000) // Sync for space enemy
{
var packet = new CombatStatDamagePacket(damage, slice, in target, in caster)
{
// Change the caster to player as craft (space fleet) is not sync yet
CasterType = (short)ETargetType.Player,
CasterId = Multiplayer.Session.LocalPlayer.Id
};
Multiplayer.Session.Network.SendPacket(packet);
}
else if (target.astroId == GameMain.localPlanet?.id) // Sync for local planet
{
var packet = new CombatStatDamagePacket(damage, slice, in target, in caster)
{
// Change the caster to player as craft (space fleet) is not sync yet
CasterType = (short)ETargetType.Player,
CasterId = Multiplayer.Session.LocalPlayer.Id
};
Multiplayer.Session.Network.SendPacketToLocalPlanet(packet);
}
}
}
25 changes: 25 additions & 0 deletions NebulaPatcher/Patches/Dynamic/UIMainMenu_Patch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,31 @@ public static void OnUpdateLogButtonClick_Postfix()
OnMultiplayerBackButtonClick();
}

[HarmonyPrefix]
[HarmonyPatch(nameof(UIMainMenu.UpdateDemoScene))]
[HarmonyPatch(nameof(UIMainMenu._OnUpdate))]
public static void OnEscSwitch()
{
if (!VFInput.escape) return;

// Go back to the upper level when hitting esc
if (multiplayerMenu.gameObject.activeInHierarchy)
{
OnJoinGameBackButtonClick();
VFInput.UseEscape();
}
else if (UIRoot.instance.loadGameWindow.active)
{
UIRoot.instance.loadGameWindow.OnCancelClick(0);
VFInput.UseEscape();
}
else if (multiplayerSubMenu.gameObject.activeInHierarchy)
{
OnMultiplayerBackButtonClick();
VFInput.UseEscape();
}
}

// Main Menu
private static void AddMultiplayerButton()
{
Expand Down
8 changes: 8 additions & 0 deletions NebulaPatcher/Patches/Misc/Dedicated_Server_Patches.cs
Original file line number Diff line number Diff line change
Expand Up @@ -209,4 +209,12 @@ public static bool RecalculatePhysicsShape_Prefix(PlanetATField __instance)

return false;
}

[HarmonyPostfix]
[HarmonyPatch(typeof(PlanetATField), nameof(PlanetATField.TestRelayCondition))]
public static void StopLanding(PlanetATField __instance, ref bool __result)
{
// Balance: Stop relay landing when there are 7 or more working shield generators
__result &= !(__instance.energy > 0 && __instance.generatorCount >= 7);
}
}
86 changes: 86 additions & 0 deletions NebulaPatcher/Patches/Transpilers/DFGBaseComponent_Transpiler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#region

using System.Collections.Generic;
using System.Reflection.Emit;
using HarmonyLib;
using NebulaModel.Logger;
using NebulaWorld;

#endregion

namespace NebulaPatcher.Patches.Transpilers;

[HarmonyPatch(typeof(DFGBaseComponent))]
internal class DFGBaseComponent_Transpiler
{
[HarmonyTranspiler]
[HarmonyPatch(nameof(DFGBaseComponent.UpdateFactoryThreat))]
public static IEnumerable<CodeInstruction> UpdateFactoryThreat_Transpiler(IEnumerable<CodeInstruction> instructions)
{
try
{
/* Launch assault for player who on remote planet and has no power buildings
from:
if (num24 == 0.0 && this.groundSystem.local_player_grounded_alive)
{
num24 = 10.0;
ref Vector3 ptr2 = ref this.groundSystem.local_player_pos;
vector = new Vector3(ptr2.x - num, ptr2.y - num2, ptr2.z - num3);
num18 = num16;
num19 = num17;
}
to:
>> if (num24 == 0.0 && LaunchCondition(this))
{
...
}
*/

var codeMatcher = new CodeMatcher(instructions)
.End()
.MatchBack(true,
new CodeMatch(OpCodes.Ldloc_S),
new CodeMatch(OpCodes.Ldc_R8),
new CodeMatch(OpCodes.Bne_Un),
new CodeMatch(OpCodes.Ldarg_0),
new CodeMatch(OpCodes.Ldfld, AccessTools.Field(typeof(DFGBaseComponent), nameof(DFGBaseComponent.groundSystem))),
new CodeMatch(OpCodes.Ldfld, AccessTools.Field(typeof(EnemyDFGroundSystem), nameof(EnemyDFGroundSystem.local_player_grounded_alive))),
new CodeMatch(OpCodes.Brfalse));

if (codeMatcher.IsInvalid)
{
Log.Warn("DFGBaseComponent.UpdateFactoryThreat: Can't find target");
return codeMatcher.InstructionEnumeration();
}
codeMatcher.Advance(-2)
.RemoveInstruction()
.SetAndAdvance(OpCodes.Call, AccessTools.Method(typeof(DFGBaseComponent_Transpiler), nameof(LaunchCondition)));

return codeMatcher.InstructionEnumeration();
}
catch (System.Exception e)
{
Log.Warn("Transpiler DFGBaseComponent.UpdateFactoryThreat failed.");
Log.Warn(e);
return instructions;
}
}

static bool LaunchCondition(DFGBaseComponent @this)
{
if (!Multiplayer.IsActive || @this.groundSystem.local_player_alive == true) return @this.groundSystem.local_player_alive;

var planetId = @this.groundSystem.planet.id;
var players = Multiplayer.Session.Combat.Players;
for (var i = 0; i < players.Length; i++)
{
if (players[i].isAlive && players[i].planetId == planetId)
{
@this.groundSystem.local_player_pos = players[i].position;
Log.Info($"Base attack LaunchCondition: player[{i}] planeId{planetId}");
return true;
}
}
return false;
}
}
Loading

0 comments on commit 0af58c9

Please sign in to comment.