From 416a7040642081f36001065ae1dd9dfdda125c5c Mon Sep 17 00:00:00 2001 From: magicono43 Date: Wed, 11 Sep 2024 22:39:18 -0400 Subject: [PATCH 1/3] Add More Substantial Override Paths For Physical Combat, 9/11/2024 Add More Substantial Override Paths For Physical Combat. This idea of these changes is to allow for much more of the vanilla behavior to be circumvented if a modder chooses to do their own thing, in this instance hopefully with as little impact on other parts of the code and execution as possible, 9/11/2024. --- Assets/Scripts/Game/EnemyAttack.cs | 12 +++++++++ Assets/Scripts/Game/Formulas/FormulaHelper.cs | 27 +++++++++++++++++++ Assets/Scripts/Game/WeaponManager.cs | 9 +++++++ 3 files changed, 48 insertions(+) diff --git a/Assets/Scripts/Game/EnemyAttack.cs b/Assets/Scripts/Game/EnemyAttack.cs index ea0c2d8e4f..1838f66874 100644 --- a/Assets/Scripts/Game/EnemyAttack.cs +++ b/Assets/Scripts/Game/EnemyAttack.cs @@ -246,6 +246,12 @@ private int ApplyDamageToPlayer(Items.DaggerfallUnityItem weapon) { const int doYouSurrenderToGuardsTextID = 15; + if (GameManager.Instance.WeaponManager.moddedPhysicalAttackBehaviorEnabled) + { + if (FormulaHelper.AlterMonsterVsPlayerPhysicalAttackAction(weapon, entityBehaviour)) + return 0; + } + EnemyEntity entity = entityBehaviour.Entity as EnemyEntity; PlayerEntity playerEntity = GameManager.Instance.PlayerEntity; @@ -302,6 +308,12 @@ private int ApplyDamageToPlayer(Items.DaggerfallUnityItem weapon) private int ApplyDamageToNonPlayer(Items.DaggerfallUnityItem weapon, Vector3 direction, bool bowAttack = false) { + if (GameManager.Instance.WeaponManager.moddedPhysicalAttackBehaviorEnabled) + { + if (FormulaHelper.AlterMonsterVsMonsterPhysicalAttackAction(weapon, entityBehaviour, senses, direction, bowAttack)) + return 0; + } + if (senses.Target == null) return 0; // TODO: Merge with hit code in WeaponManager to eliminate duplicate code diff --git a/Assets/Scripts/Game/Formulas/FormulaHelper.cs b/Assets/Scripts/Game/Formulas/FormulaHelper.cs index 3e891cb9ec..518fcd777d 100644 --- a/Assets/Scripts/Game/Formulas/FormulaHelper.cs +++ b/Assets/Scripts/Game/Formulas/FormulaHelper.cs @@ -1074,6 +1074,33 @@ public static int AdjustWeaponAttackDamage(DaggerfallEntity attacker, Daggerfall return damage; } + public static bool AlterPlayerVsMonsterPhysicalAttackAction(DaggerfallUnityItem weapon, bool arrowHit, bool arrowSummoned, Transform hitTransform, Vector3 impactPosition, Vector3 direction) + { + Func del; + if (TryGetOverride("AlterPlayerVsMonsterPhysicalAttackAction", out del)) + return del(weapon, arrowHit, arrowSummoned, hitTransform, impactPosition, direction); + + return false; + } + + public static bool AlterMonsterVsPlayerPhysicalAttackAction(DaggerfallUnityItem weapon, DaggerfallEntityBehaviour entityBehaviour) + { + Func del; + if (TryGetOverride("AlterMonsterVsPlayerPhysicalAttackAction", out del)) + return del(weapon, entityBehaviour); + + return false; + } + + public static bool AlterMonsterVsMonsterPhysicalAttackAction(DaggerfallUnityItem weapon, DaggerfallEntityBehaviour entityBehaviour, EnemySenses senses, Vector3 direction, bool bowAttack) + { + Func del; + if (TryGetOverride("AlterMonsterVsMonsterPhysicalAttackAction", out del)) + return del(weapon, entityBehaviour, senses, direction, bowAttack); + + return false; + } + /// /// Allocate any equipment damage from a strike, and reduce item condition. /// diff --git a/Assets/Scripts/Game/WeaponManager.cs b/Assets/Scripts/Game/WeaponManager.cs index f26f276ad6..46905d712f 100644 --- a/Assets/Scripts/Game/WeaponManager.cs +++ b/Assets/Scripts/Game/WeaponManager.cs @@ -55,6 +55,9 @@ public class WeaponManager : MonoBehaviour public float ChanceToBeParried = 0.1f; // Example: Chance for player hit to be parried public DaggerfallMissile ArrowMissilePrefab; + // Allows a mod to specify whether to skip over certain vanilla behavior related to physical attack logic, so it can do its own behavior + public bool moddedPhysicalAttackBehaviorEnabled = false; + //float weaponSensitivity = 1.0f; // Sensitivity of weapon swings to mouse movements private Gesture _gesture; private int _longestDim; // Longest screen dimension, used to compare gestures for attack @@ -486,6 +489,12 @@ public bool WeaponEnvDamage(DaggerfallUnityItem strikingWeapon, RaycastHit hit) // Returns true if hit an enemy entity public bool WeaponDamage(DaggerfallUnityItem strikingWeapon, bool arrowHit, bool arrowSummoned, Transform hitTransform, Vector3 impactPosition, Vector3 direction) { + if (moddedPhysicalAttackBehaviorEnabled) + { + if (FormulaHelper.AlterPlayerVsMonsterPhysicalAttackAction(strikingWeapon, arrowHit, arrowSummoned, hitTransform, impactPosition, direction)) + return false; + } + DaggerfallEntityBehaviour entityBehaviour = hitTransform.GetComponent(); var entityMobileUnit = hitTransform.GetComponentInChildren(); EnemyMotor enemyMotor = hitTransform.GetComponent(); From ac0bb14ec5c1e8f121b8189e3340647477e87e6b Mon Sep 17 00:00:00 2001 From: magicono43 Date: Fri, 3 Jan 2025 00:58:00 -0500 Subject: [PATCH 2/3] Revert "Add More Substantial Override Paths For Physical Combat, 9/11/2024" This reverts commit 416a7040642081f36001065ae1dd9dfdda125c5c. --- Assets/Scripts/Game/EnemyAttack.cs | 12 --------- Assets/Scripts/Game/Formulas/FormulaHelper.cs | 27 ------------------- Assets/Scripts/Game/WeaponManager.cs | 9 ------- 3 files changed, 48 deletions(-) diff --git a/Assets/Scripts/Game/EnemyAttack.cs b/Assets/Scripts/Game/EnemyAttack.cs index 1838f66874..ea0c2d8e4f 100644 --- a/Assets/Scripts/Game/EnemyAttack.cs +++ b/Assets/Scripts/Game/EnemyAttack.cs @@ -246,12 +246,6 @@ private int ApplyDamageToPlayer(Items.DaggerfallUnityItem weapon) { const int doYouSurrenderToGuardsTextID = 15; - if (GameManager.Instance.WeaponManager.moddedPhysicalAttackBehaviorEnabled) - { - if (FormulaHelper.AlterMonsterVsPlayerPhysicalAttackAction(weapon, entityBehaviour)) - return 0; - } - EnemyEntity entity = entityBehaviour.Entity as EnemyEntity; PlayerEntity playerEntity = GameManager.Instance.PlayerEntity; @@ -308,12 +302,6 @@ private int ApplyDamageToPlayer(Items.DaggerfallUnityItem weapon) private int ApplyDamageToNonPlayer(Items.DaggerfallUnityItem weapon, Vector3 direction, bool bowAttack = false) { - if (GameManager.Instance.WeaponManager.moddedPhysicalAttackBehaviorEnabled) - { - if (FormulaHelper.AlterMonsterVsMonsterPhysicalAttackAction(weapon, entityBehaviour, senses, direction, bowAttack)) - return 0; - } - if (senses.Target == null) return 0; // TODO: Merge with hit code in WeaponManager to eliminate duplicate code diff --git a/Assets/Scripts/Game/Formulas/FormulaHelper.cs b/Assets/Scripts/Game/Formulas/FormulaHelper.cs index 518fcd777d..3e891cb9ec 100644 --- a/Assets/Scripts/Game/Formulas/FormulaHelper.cs +++ b/Assets/Scripts/Game/Formulas/FormulaHelper.cs @@ -1074,33 +1074,6 @@ public static int AdjustWeaponAttackDamage(DaggerfallEntity attacker, Daggerfall return damage; } - public static bool AlterPlayerVsMonsterPhysicalAttackAction(DaggerfallUnityItem weapon, bool arrowHit, bool arrowSummoned, Transform hitTransform, Vector3 impactPosition, Vector3 direction) - { - Func del; - if (TryGetOverride("AlterPlayerVsMonsterPhysicalAttackAction", out del)) - return del(weapon, arrowHit, arrowSummoned, hitTransform, impactPosition, direction); - - return false; - } - - public static bool AlterMonsterVsPlayerPhysicalAttackAction(DaggerfallUnityItem weapon, DaggerfallEntityBehaviour entityBehaviour) - { - Func del; - if (TryGetOverride("AlterMonsterVsPlayerPhysicalAttackAction", out del)) - return del(weapon, entityBehaviour); - - return false; - } - - public static bool AlterMonsterVsMonsterPhysicalAttackAction(DaggerfallUnityItem weapon, DaggerfallEntityBehaviour entityBehaviour, EnemySenses senses, Vector3 direction, bool bowAttack) - { - Func del; - if (TryGetOverride("AlterMonsterVsMonsterPhysicalAttackAction", out del)) - return del(weapon, entityBehaviour, senses, direction, bowAttack); - - return false; - } - /// /// Allocate any equipment damage from a strike, and reduce item condition. /// diff --git a/Assets/Scripts/Game/WeaponManager.cs b/Assets/Scripts/Game/WeaponManager.cs index 46905d712f..f26f276ad6 100644 --- a/Assets/Scripts/Game/WeaponManager.cs +++ b/Assets/Scripts/Game/WeaponManager.cs @@ -55,9 +55,6 @@ public class WeaponManager : MonoBehaviour public float ChanceToBeParried = 0.1f; // Example: Chance for player hit to be parried public DaggerfallMissile ArrowMissilePrefab; - // Allows a mod to specify whether to skip over certain vanilla behavior related to physical attack logic, so it can do its own behavior - public bool moddedPhysicalAttackBehaviorEnabled = false; - //float weaponSensitivity = 1.0f; // Sensitivity of weapon swings to mouse movements private Gesture _gesture; private int _longestDim; // Longest screen dimension, used to compare gestures for attack @@ -489,12 +486,6 @@ public bool WeaponEnvDamage(DaggerfallUnityItem strikingWeapon, RaycastHit hit) // Returns true if hit an enemy entity public bool WeaponDamage(DaggerfallUnityItem strikingWeapon, bool arrowHit, bool arrowSummoned, Transform hitTransform, Vector3 impactPosition, Vector3 direction) { - if (moddedPhysicalAttackBehaviorEnabled) - { - if (FormulaHelper.AlterPlayerVsMonsterPhysicalAttackAction(strikingWeapon, arrowHit, arrowSummoned, hitTransform, impactPosition, direction)) - return false; - } - DaggerfallEntityBehaviour entityBehaviour = hitTransform.GetComponent(); var entityMobileUnit = hitTransform.GetComponentInChildren(); EnemyMotor enemyMotor = hitTransform.GetComponent(); From eabd86a943739d58a0a513ed1773ab0dc35cc81a Mon Sep 17 00:00:00 2001 From: magicono43 Date: Fri, 3 Jan 2025 01:38:10 -0500 Subject: [PATCH 3/3] Revise Original Commit To Use Delegates Instead, 1/3/2025 Revise Original Commit To Use Delegates Instead. Took examples mainly from Dunny's commits from here: https://github.com/Interkarma/daggerfall-unity/pull/2563 1/3/2025. --- Assets/Scripts/Game/EnemyAttack.cs | 13 +++++++++++-- Assets/Scripts/Game/WeaponManager.cs | 7 ++++++- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/Assets/Scripts/Game/EnemyAttack.cs b/Assets/Scripts/Game/EnemyAttack.cs index ea0c2d8e4f..c55bc26170 100644 --- a/Assets/Scripts/Game/EnemyAttack.cs +++ b/Assets/Scripts/Game/EnemyAttack.cs @@ -38,6 +38,12 @@ public class EnemyAttack : MonoBehaviour MobileUnit mobile; DaggerfallEntityBehaviour entityBehaviour; int damage = 0; + + public delegate int ApplyDamageToPlayerCallback(Items.DaggerfallUnityItem weapon); + public delegate int ApplyDamageToNonPlayerCallback(Items.DaggerfallUnityItem weapon, Vector3 direction, bool bowAttack = false); + + public ApplyDamageToPlayerCallback ApplyDamageToPlayer { get; set; } + public ApplyDamageToNonPlayerCallback ApplyDamageToNonPlayer { get; set; } void Start() { @@ -46,6 +52,9 @@ void Start() sounds = GetComponent(); mobile = GetComponent().MobileUnit; entityBehaviour = GetComponent(); + + ApplyDamageToPlayer = DefaultApplyDamageToPlayer; + ApplyDamageToNonPlayer = DefaultApplyDamageToNonPlayer; } void FixedUpdate() @@ -242,7 +251,7 @@ private void ShootBow() } } - private int ApplyDamageToPlayer(Items.DaggerfallUnityItem weapon) + private int DefaultApplyDamageToPlayer(Items.DaggerfallUnityItem weapon) { const int doYouSurrenderToGuardsTextID = 15; @@ -300,7 +309,7 @@ private int ApplyDamageToPlayer(Items.DaggerfallUnityItem weapon) return damage; } - private int ApplyDamageToNonPlayer(Items.DaggerfallUnityItem weapon, Vector3 direction, bool bowAttack = false) + private int DefaultApplyDamageToNonPlayer(Items.DaggerfallUnityItem weapon, Vector3 direction, bool bowAttack = false) { if (senses.Target == null) return 0; diff --git a/Assets/Scripts/Game/WeaponManager.cs b/Assets/Scripts/Game/WeaponManager.cs index f26f276ad6..f48431e9bb 100644 --- a/Assets/Scripts/Game/WeaponManager.cs +++ b/Assets/Scripts/Game/WeaponManager.cs @@ -79,6 +79,9 @@ public class WeaponManager : MonoBehaviour public float EquipCountdownRightHand; public float EquipCountdownLeftHand; + + public delegate bool WeaponDamageCallback(DaggerfallUnityItem strikingWeapon, bool arrowHit, bool arrowSummoned, Transform hitTransform, Vector3 impactPosition, Vector3 direction); + public WeaponDamageCallback WeaponDamage { get; set; } #region Properties @@ -200,6 +203,8 @@ void Start() _gesture = new Gesture(); _longestDim = Math.Max(Screen.width, Screen.height); SetMelee(ScreenWeapon); + + WeaponDamage = DefaultWeaponDamage; } void Update() @@ -484,7 +489,7 @@ public bool WeaponEnvDamage(DaggerfallUnityItem strikingWeapon, RaycastHit hit) } // Returns true if hit an enemy entity - public bool WeaponDamage(DaggerfallUnityItem strikingWeapon, bool arrowHit, bool arrowSummoned, Transform hitTransform, Vector3 impactPosition, Vector3 direction) + public bool DefaultWeaponDamage(DaggerfallUnityItem strikingWeapon, bool arrowHit, bool arrowSummoned, Transform hitTransform, Vector3 impactPosition, Vector3 direction) { DaggerfallEntityBehaviour entityBehaviour = hitTransform.GetComponent(); var entityMobileUnit = hitTransform.GetComponentInChildren();