Skip to content

Commit

Permalink
Server owned damage
Browse files Browse the repository at this point in the history
  • Loading branch information
Lacyway committed May 4, 2024
1 parent b0b336e commit c9f937b
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 19 deletions.
23 changes: 12 additions & 11 deletions Fika.Core/Coop/Players/CoopPlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public class CoopPlayer : LocalPlayer
public bool hasGround = false;
public Transform RaycastCameraTransform;
public int NetId;
public bool IsObservedAI = false;

public static async Task<LocalPlayer> Create(
int playerId,
Expand Down Expand Up @@ -199,11 +200,6 @@ public override void ApplyDamageInfo(DamageInfo damageInfo, EBodyPart bodyPartTy
{
if (IsYourPlayer)
{
if (!FikaPlugin.Instance.FriendlyFire && damageInfo.Player is not null && damageInfo.Player.iPlayer is ObservedCoopPlayer observedCoopPlayer && !observedCoopPlayer.IsObservedAI)
{
return;
}

if (colliderType == EBodyPartColliderType.HeadCommon)
{
damageInfo.Damage *= FikaPlugin.HeadDamageMultiplier.Value;
Expand All @@ -220,15 +216,12 @@ public override void ApplyDamageInfo(DamageInfo damageInfo, EBodyPart bodyPartTy

public override GClass1676 ApplyShot(DamageInfo damageInfo, EBodyPart bodyPartType, EBodyPartColliderType colliderType, EArmorPlateCollider armorPlateCollider, GStruct390 shotId)
{
if (IsYourPlayer)
if (damageInfo.Player != null & damageInfo.Player.iPlayer is CoopBot)
{
if (!FikaPlugin.Instance.FriendlyFire && damageInfo.Player is not null && damageInfo.Player.iPlayer is ObservedCoopPlayer observedCoopPlayer && !observedCoopPlayer.IsObservedAI)
{
return null;
}
return base.ApplyShot(damageInfo, bodyPartType, colliderType, armorPlateCollider, shotId);
}

return base.ApplyShot(damageInfo, bodyPartType, colliderType, armorPlateCollider, shotId);
return null;
}

public override void Proceed(bool withNetwork, Callback<GInterface125> callback, bool scheduled = true)
Expand Down Expand Up @@ -1267,6 +1260,7 @@ public virtual void HandleDamagePacket(DamagePacket packet)
DamageType = packet.DamageInfo.DamageType,
BodyPartColliderType = packet.DamageInfo.ColliderType,
HitPoint = packet.DamageInfo.Point,
HitNormal = packet.DamageInfo.HitNormal,
Direction = packet.DamageInfo.Direction,
PenetrationPower = packet.DamageInfo.PenetrationPower,
BlockedBy = packet.DamageInfo.BlockedBy,
Expand All @@ -1281,6 +1275,13 @@ public virtual void HandleDamagePacket(DamagePacket packet)
if (player != null)
{
damageInfo.Player = player;
if (IsYourPlayer)
{
if (!FikaPlugin.Instance.FriendlyFire && damageInfo.Player.iPlayer is ObservedCoopPlayer observedCoopPlayer && !observedCoopPlayer.IsObservedAI)
{
return;
}
}
}

if (Singleton<GameWorld>.Instance.GetAlivePlayerByProfileID(packet.DamageInfo.ProfileId).HandsController.Item is Weapon weapon)
Expand Down
115 changes: 107 additions & 8 deletions Fika.Core/Coop/Players/ObservedCoopPlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ public GClass2417 NetworkHealthController
{
get => HealthController as GClass2417;
}
public bool IsObservedAI = false;
private readonly GClass2156 ObservedVaultingParameters = new();
public override bool CanBeSnapped => false;
public override EPointOfView PointOfView { get => EPointOfView.ThirdPerson; }
Expand Down Expand Up @@ -324,13 +323,40 @@ public override void ApplyDamageInfo(DamageInfo damageInfo, EBodyPart bodyPartTy
ManageAggressor(damageInfo, bodyPartType, colliderType);*/
}

/*public override void ShotReactions(DamageInfo shot, EBodyPart bodyPart)
{
Vector3 normalized = shot.Direction.normalized;
if (PointOfView == EPointOfView.ThirdPerson)
{
turnOffFbbikAt = Time.time + 0.6f;
_fbbik.solver.Quick = false;
BodyPartCollider bodyPartCollider;
if ((bodyPartCollider = shot.HittedBallisticCollider as BodyPartCollider) != null)
{
HitReaction.Hit(bodyPartCollider.BodyPartColliderType, bodyPartCollider.BodyPartType, normalized, shot.HitPoint, false);
}
}
if (shot.Weapon is KnifeClass knifeClass)
{
KnifeComponent itemComponent = knifeClass.GetItemComponent<KnifeComponent>();
Vector3 normalized2 = (shot.Player.iPlayer.Transform.position - Transform.position).normalized;
Vector3 vector = Vector3.Cross(normalized2, Vector3.up);
float y = normalized.y;
float num = Vector3.Dot(vector, normalized);
float num2 = 1f - Mathf.Abs(Vector3.Dot(normalized2, normalized));
num2 = ((bodyPart == EBodyPart.Head) ? num2 : Mathf.Sqrt(num2));
Rotation += new Vector2(-num, -y).normalized * itemComponent.Template.AppliedTrunkRotation.Random(false) * num2;
ProceduralWeaponAnimation.ForceReact.AddForce(new Vector3(-y, num, 0f).normalized, num2, 1f, itemComponent.Template.AppliedHeadRotation.Random(false));
}
}*/

public override GClass1676 ApplyShot(DamageInfo damageInfo, EBodyPart bodyPartType, EBodyPartColliderType colliderType, EArmorPlateCollider armorPlateCollider, GStruct390 shotId)
{
if (!IsObservedAI)
/*if (!IsObservedAI)
{
ShotReactions(damageInfo, bodyPartType);
return null;
}
}*/

if (damageInfo.Player != null)
{
Expand All @@ -340,7 +366,10 @@ public override GClass1676 ApplyShot(DamageInfo damageInfo, EBodyPart bodyPartTy
LastDamageInfo = damageInfo;
LastDamageType = damageInfo.DamageType;

if (damageInfo.Player.iPlayer.IsYourPlayer)
// There should never be other instances than CoopPlayer or its derived types
CoopPlayer player = (CoopPlayer)damageInfo.Player.iPlayer;

if (player.IsYourPlayer)
{
if (HealthController != null && !HealthController.IsAlive)
{
Expand Down Expand Up @@ -385,6 +414,66 @@ public override GClass1676 ApplyShot(DamageInfo damageInfo, EBodyPart bodyPartTy
Absorbed = 0f,
Direction = damageInfo.Direction,
Point = damageInfo.HitPoint,
HitNormal = damageInfo.HitNormal,
PenetrationPower = damageInfo.PenetrationPower,
BlockedBy = damageInfo.BlockedBy,
DeflectedBy = damageInfo.DeflectedBy,
SourceId = damageInfo.SourceId,
ProfileId = damageInfo.Player.iPlayer.ProfileId
}
});

// Run this to get weapon skill
ManageAggressor(damageInfo, bodyPartType, colliderType);

return hitInfo;
}
else if (player.IsAI)
{
if (HealthController != null && !HealthController.IsAlive)
{
return null;
}

bool flag = !string.IsNullOrEmpty(damageInfo.DeflectedBy);
float damage = damageInfo.Damage;
List<ArmorComponent> list = ProceedDamageThroughArmor(ref damageInfo, colliderType, armorPlateCollider, true);
MaterialType materialType = (flag ? MaterialType.HelmetRicochet : ((list == null || list.Count < 1) ? MaterialType.Body : list[0].Material));
GClass1676 hitInfo = new()
{
PoV = PointOfView,
Penetrated = (string.IsNullOrEmpty(damageInfo.BlockedBy) || string.IsNullOrEmpty(damageInfo.DeflectedBy)),
Material = materialType
};
float num = damage - damageInfo.Damage;
if (num > 0)
{
damageInfo.DidArmorDamage = num;
}
damageInfo.DidBodyDamage = damageInfo.Damage;
//ApplyDamageInfo(damageInfo, bodyPartType, colliderType, 0f);
ShotReactions(damageInfo, bodyPartType);
ReceiveDamage(damageInfo.Damage, bodyPartType, damageInfo.DamageType, num, hitInfo.Material);

if (damageInfo.HittedBallisticCollider != null)
{
BodyPartCollider bodyPartCollider = (BodyPartCollider)damageInfo.HittedBallisticCollider;
colliderType = bodyPartCollider.BodyPartColliderType;
}

PacketSender?.HealthPackets?.Enqueue(new()
{
DamageInfo = new()
{
Damage = damage,
DamageType = damageInfo.DamageType,
BodyPartType = bodyPartType,
ColliderType = colliderType,
ArmorPlateCollider = armorPlateCollider,
Absorbed = 0f,
Direction = damageInfo.Direction,
Point = damageInfo.HitPoint,
HitNormal = damageInfo.HitNormal,
PenetrationPower = damageInfo.PenetrationPower,
BlockedBy = damageInfo.BlockedBy,
DeflectedBy = damageInfo.DeflectedBy,
Expand Down Expand Up @@ -720,17 +809,26 @@ public override void SetInventory(EquipmentClass equipmentClass)
Inventory.Equipment = equipmentClass;

BindableState<Item> itemInHands = (BindableState<Item>)Traverse.Create(this).Field("_itemInHands").GetValue();
bool shouldSet = false;
if (HandsController != null && HandsController.Item != null)
{
shouldSet = true;
Item item = FindItem(HandsController.Item.Id);
if (item != null)
{
itemInHands.Value = item;
}
}
PlayerBody.Init(PlayerBody.BodyCustomization, Inventory.Equipment, shouldSet ? itemInHands : null, LayerMask.NameToLayer("Player"), Side);

EquipmentSlot[] equipmentSlots = Traverse.Create<PlayerBody>().Field<EquipmentSlot[]>("SlotNames").Value;
foreach (EquipmentSlot equipmentSlot in equipmentSlots)
{
Transform slotBone = PlayerBody.GetSlotBone(equipmentSlot);
Transform alternativeHolsterBone = PlayerBody.GetAlternativeHolsterBone(equipmentSlot);
PlayerBody.GClass1860 gclass = new(PlayerBody, Inventory.Equipment.GetSlot(equipmentSlot), slotBone, equipmentSlot, Inventory.Equipment.GetSlot(EquipmentSlot.Backpack), alternativeHolsterBone);
PlayerBody.GClass1860 gclass2 = PlayerBody.SlotViews.AddOrReplace(equipmentSlot, gclass);
gclass2?.Dispose();
}

//PlayerBody.Init(PlayerBody.BodyCustomization, Inventory.Equipment, shouldSet ? itemInHands : null, LayerMask.NameToLayer("Player"), Side);
}

public override void DoObservedVault(VaultPacket packet)
Expand Down Expand Up @@ -791,10 +889,11 @@ protected override async void Start()
{
IsObservedAI = true;
}

PacketSender = gameObject.AddComponent<ObservedPacketSender>();

if (IsObservedAI)
{
PacketSender = gameObject.AddComponent<ObservedPacketSender>();
GenericPacket genericPacket = new(EPackageType.LoadBot)
{
NetId = NetId,
Expand Down

0 comments on commit c9f937b

Please sign in to comment.