Skip to content

Commit

Permalink
* Merge #322
Browse files Browse the repository at this point in the history
* HS-1364: Fix old bug with logging out giving an error message (login/logout/login works now)

* HS-1334: implement absorb shield mechanic
  • Loading branch information
baughj committed May 5, 2024
1 parent 0792fbd commit 6a97dac
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 93 deletions.
2 changes: 1 addition & 1 deletion hybrasyl/Hybrasyl.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Humanizer.Core" Version="2.14.1" />
<PackageReference Include="Hybrasyl.Xml" Version="0.9.3.32" />
<PackageReference Include="Hybrasyl.Xml" Version="0.9.3.33" />
<PackageReference Include="MoonSharp" Version="2.0.0" />
<PackageReference Include="MSBuildGitHash" Version="2.0.2">
<PrivateAssets>all</PrivateAssets>
Expand Down
2 changes: 2 additions & 0 deletions hybrasyl/NumberCruncher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,7 @@ public static StatInfo CalculateStatBonus(Creature source, StatModifiers bonus)
BonusManaSteal = _evalFormula(bonus.BonusManaSteal, null, source),
BonusInboundDamageToMp = _evalFormula(bonus.BonusInboundDamageToMp, null, source),
BonusExtraFaith = _evalFormula(bonus.BonusExtraFaith, null, source),
Shield = _evalFormula(bonus.Shield, null, source)
};
return modifiers;
}
Expand Down Expand Up @@ -480,6 +481,7 @@ public static long Modify(double val, double intensity)
BonusExtraFaith = Modify(_evalFormula(effect.BonusExtraFaith, castable, target, source), intensity),
BonusLifeSteal = Modify(_evalFormula(effect.BonusLifeSteal, castable, target, source), intensity),
BonusManaSteal = Modify(_evalFormula(effect.BonusManaSteal, castable, target, source), intensity),
Shield = Modify(_evalFormula(effect.Shield, castable, target, source), intensity)
};

modifiers.ElementalModifiers.Apply(effect.ElementalModifiers);
Expand Down
19 changes: 18 additions & 1 deletion hybrasyl/Objects/CombatEvents.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Hybrasyl.Enums;
using Hybrasyl.Xml.Objects;
using MoonSharp.Interpreter;
using MoonSharp.Interpreter.Interop;

namespace Hybrasyl.Objects;

Expand Down Expand Up @@ -103,10 +104,26 @@ public record DamageEvent : StatChangeEvent
// Was the damage actually applied or was the target immune?
public bool Applied { get; set; } = true;

// How much of the damage was shielded, if any?
public uint Shielded { get; set; } = 0;

public override string ToString()
{
var str =
$"Damage: {SourceName}: {SourceCastableName} on {TargetName} ({Element}, {Type})\n [{(Applied ? "Applied" : "Immune")}] {Amount} dmg, ER {ElementalResisted}, EA {ElementalAugmented} MR {MagicResisted}, AC {ArmorReduction} Mod {ModifierDmg}";
$"Damage: {SourceName}: {SourceCastableName} on {TargetName} ({Element}, {Type})\n [{(Applied ? "Applied" : "Immune")}] {Amount} dmg";

if (ElementalResisted > 0)
str += $" EResist {ElementalResisted}";
if (ElementalAugmented > 0)
str += $" EAug {ElementalAugmented}";
if (MagicResisted > 0)
str += $" MR {MagicResisted}";
if (ArmorReduction > 0)
str += $" AC {ArmorReduction}";
if (ModifierDmg > 0)
str += $" Mod {ModifierDmg}";
if (Shielded >0)
str += $" Shield {Shielded}";
if (Crit || MagicCrit)
str = $"CRIT {str}";
return $"[combat] {str}";
Expand Down
16 changes: 16 additions & 0 deletions hybrasyl/Objects/Creature.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1124,6 +1124,22 @@ public virtual void RegenerateMp(double mp, Creature regenerator = null)
// Negative damage (possible with augments and resistances)
if (damage < 0) damage = 1;

// lastly, handle shield

if (Stats.Shield > 0)
{
if (Stats.Shield >= damage)
{
damage = 0;
Stats.Shield -= damage;
}
else
{
damage -= Stats.Shield;
Stats.Shield = 0;
}
}

// Now, normalize damage for uint (max hp)
var normalized = (uint)damage;

Expand Down
2 changes: 0 additions & 2 deletions hybrasyl/Objects/Monster.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using Hybrasyl.ChatCommands;
using Serilog;

namespace Hybrasyl.Objects;

Expand Down
8 changes: 8 additions & 0 deletions hybrasyl/Objects/StatInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1403,6 +1403,14 @@ public double BonusInboundDamageToMp

[FormulaVariable] public double InboundDamageToMp => BaseInboundDamageToMp + BonusInboundDamageToMp;

[FormulaVariable]
public double Shield
{
get => _shield;
set => _shield = value < 0 ? 0 : value;
}

private double _shield { get; set; }

public ElementType BaseOffensiveElement
{
Expand Down
5 changes: 2 additions & 3 deletions hybrasyl/Objects/User.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4957,11 +4957,10 @@ public void SendWorldMessage(string sender, string message)
Enqueue(x0A);
}

public void SendRedirectAndLogoff(World world, Login login, string name, int transmitDelay = 1200)
public void SendRedirect(World world, Login login, string name, bool logoff = true, int transmitDelay = 1200)
{
GlobalConnectionManifest.DeregisterClient(Client);
Client.Redirect(
new Redirect(Client, world, Game.Login, name, Client.EncryptionSeed, Client.EncryptionKey), true,
new Redirect(Client, world, Game.Login, name, Client.EncryptionSeed, Client.EncryptionKey), logoff,
transmitDelay);
}

Expand Down
107 changes: 26 additions & 81 deletions hybrasyl/Packet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -296,8 +296,8 @@ protected Packet()

public abstract EncryptMethod EncryptMethod { get; }

public abstract bool ShouldEncrypt { get; }
public abstract bool UseDefaultKey { get; }
public bool ShouldEncrypt => EncryptMethod != EncryptMethod.None;
public bool UseDefaultKey => EncryptMethod == EncryptMethod.Normal;

public string Hash()
{
Expand Down Expand Up @@ -419,86 +419,37 @@ public ClientPacket(byte[] buffer)
}
}

public override bool ShouldEncrypt => Opcode != 0x00 && Opcode != 0x10;

public override bool UseDefaultKey => Opcode == 0x02 || Opcode == 0x03 || Opcode == 0x04 || Opcode == 0x0B ||
Opcode == 0x26
|| Opcode == 0x2D || Opcode == 0x3A || Opcode == 0x42 || Opcode == 0x43 ||
Opcode == 0x4B
|| Opcode == 0x57 || Opcode == 0x62 || Opcode == 0x68 || Opcode == 0x71 ||
Opcode == 0x73
|| Opcode == 0x7B;


public override EncryptMethod EncryptMethod
public override EncryptMethod EncryptMethod
{
get
{
var opcode = Opcode;
if (opcode <= 0x43)
switch (Opcode)
{
if (opcode <= 0x10)
{
switch (opcode)
{
case 0x00:
break;
case 0x01:
return EncryptMethod.MD5Key;
case 0x02:
case 0x03:
case 0x04:
return EncryptMethod.Normal;
default:
if (opcode == 0x0A) return EncryptMethod.Normal;
if (opcode != 0x10) return EncryptMethod.MD5Key;
break;
}
}
else if (opcode <= 0x2D)
{
if (opcode != 0x26 && opcode != 0x2D) return EncryptMethod.MD5Key;
case 0x00:
case 0x10:
case 0x48:
return EncryptMethod.None;
case 0x02:
case 0x03:
case 0x04:
case 0x0B:
case 0x26:
case 0x2D:
case 0x3A:
case 0x42:
case 0x43:
case 0x4B:
case 0x57:
case 0x62:
case 0x68:
case 0x71:
case 0x73:
case 0x7B:
return EncryptMethod.Normal;
}
else
{
if (opcode == 0x3A) return EncryptMethod.Normal;
switch (opcode)
{
case 0x42:
case 0x43:
return EncryptMethod.Normal;
default:
return EncryptMethod.MD5Key;
}
}
}
else if (opcode <= 0x57)
{
if (opcode == 0x48) return EncryptMethod.None;
if (opcode != 0x4B && opcode != 0x57) return EncryptMethod.MD5Key;
return EncryptMethod.Normal;
default:
return EncryptMethod.MD5Key;
}
else if (opcode <= 0x68)
{
if (opcode != 0x62 && opcode != 0x68) return EncryptMethod.MD5Key;
return EncryptMethod.Normal;
}
else
{
switch (opcode)
{
case 0x71:
case 0x73:
return EncryptMethod.Normal;
case 0x72:
return EncryptMethod.MD5Key;
default:
return opcode != 0x7B ? EncryptMethod.MD5Key : EncryptMethod.Normal;
}
}

return EncryptMethod.None;
}
}

Expand Down Expand Up @@ -697,12 +648,6 @@ public ServerPacket(byte[] buffer)
}
}

public override bool ShouldEncrypt => Opcode != 0x00 && Opcode != 0x03 && Opcode != 0x7E; //&& Opcode != 0x0D;

public override bool UseDefaultKey => Opcode == 0x01 || Opcode == 0x02 || Opcode == 0x0A || Opcode == 0x56 ||
Opcode == 0x60
|| Opcode == 0x62 || Opcode == 0x66; //(|| Opcode == 0x6F;)

public override EncryptMethod EncryptMethod
{
get
Expand Down
10 changes: 5 additions & 5 deletions hybrasyl/World.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2035,12 +2035,11 @@ private void PacketHandler_0x0B_ClientExit(object obj, ClientPacket packet)
user.Map.Remove(user);
if (user.Grouped) user.Group.Remove(user);
Remove(user);
user.SendRedirectAndLogoff(this, Game.Login, user.Name);
user.SendRedirect(this, Game.Login, user.Name, false);
user.AuthInfo.CurrentState = UserState.Disconnected;
user.Save(true);
DeleteUser(user.Name);


// Remove any active async dialog sessions
// TODO: async fix
//foreach (var dialog in ActiveAsyncDialogs.Keys.Where(key => key.Item1 == user.Id || key.Item2 == user.Id))
Expand All @@ -2049,7 +2048,8 @@ private void PacketHandler_0x0B_ClientExit(object obj, ClientPacket packet)
// request.End();
//}

GameLog.InfoFormat("{1} leaving world", user.Name);
GameLog.InfoFormat($"{user.Name} leaving world");

}
}

Expand Down Expand Up @@ -3603,7 +3603,7 @@ private void PacketHandler_0x45_ByteHeartbeat(object obj, ClientPacket packet)
if (!user.IsHeartbeatValid(byteA, byteB))
{
GameLog.WarningFormat("{0}: byte heartbeat not valid, disconnecting", user.Name);
user.SendRedirectAndLogoff(Game.World, Game.Login, user.Name);
user.SendRedirect(Game.World, Game.Login, user.Name);
}
else
{
Expand Down Expand Up @@ -3793,7 +3793,7 @@ private void PacketHandler_0x75_TickHeartbeat(object obj, ClientPacket packet)
if (!user.IsHeartbeatValid(serverTick, clientTick))
{
GameLog.InfoFormat("{0}: tick heartbeat not valid, disconnecting", user.Name);
user.SendRedirectAndLogoff(Game.World, Game.Login, user.Name);
user.SendRedirect(Game.World, Game.Login, user.Name);
}
else
{
Expand Down

0 comments on commit 6a97dac

Please sign in to comment.