Initial work on scripts

This commit is contained in:
Deukhoofd 2024-07-27 16:53:06 +02:00
parent e17fe767bc
commit 9186d0efcc
4 changed files with 60 additions and 4 deletions

View File

@ -1,6 +1,36 @@
using PkmnLib.Dynamic.Libraries;
using PkmnLib.Dynamic.Models;
using PkmnLib.Static.Utils;
namespace PkmnLib.Dynamic.ScriptHandling;
public abstract class Script
{
private bool _markedForDeletion;
private int _suppressCount;
public abstract string Name { get; }
public bool MarkForDeletion() => _markedForDeletion = true;
public bool IsMarkedForDeletion() => _markedForDeletion;
public int SuppressCount() => _suppressCount;
public bool IsSuppressed() => _suppressCount > 0;
public void Suppress() => _suppressCount++;
public void Unsuppress() => _suppressCount--;
public virtual void Stack()
{
}
public virtual void OnRemove()
{
}
public virtual void OnInitialize(IDynamicLibrary library, IReadOnlyDictionary<StringKey, object> parameters)
{
}
public virtual void ChangeStabModifier(IExecutingMove executingMove, IPokemon target, byte hitNumber,
ref float modifier)
{
}
}

View File

@ -1,4 +1,5 @@
using System.Collections;
using System.Diagnostics.CodeAnalysis;
namespace PkmnLib.Dynamic.ScriptHandling;
@ -6,7 +7,14 @@ public class ScriptContainer : IEnumerable<ScriptContainer>
{
private Script? _script = null;
[MemberNotNullWhen(false, nameof(ScriptHandling.Script))]
public bool IsEmpty => _script is null;
public Script? Script
{
get => _script;
set => _script = value;
}
/// <inheritdoc />
public IEnumerator<ScriptContainer> GetEnumerator()

View File

@ -0,0 +1,16 @@
namespace PkmnLib.Dynamic.ScriptHandling;
public static class ScriptExecution
{
public static void RunScriptHook(this IScriptSource source, Action<Script> hook)
{
var iterator = source.GetScripts();
foreach (var container in iterator)
{
if (container.IsEmpty)
continue;
var script = container.Script;
hook(script);
}
}
}

View File

@ -3,6 +3,7 @@ using System.Diagnostics.CodeAnalysis;
using System.Linq;
using PkmnLib.Dynamic.Libraries;
using PkmnLib.Dynamic.Models;
using PkmnLib.Dynamic.ScriptHandling;
using PkmnLib.Static;
using PkmnLib.Static.Moves;
@ -51,7 +52,8 @@ public class Gen7DamageCalculator(bool hasRandomness) : IDamageCalculator
if (executingMove.User.Types.Contains(hitData.Type))
{
var stabModifier = 1.5f;
// TODO: script hook to change the STAB modifier
executingMove.RunScriptHook(script =>
script.ChangeStabModifier(executingMove, target, hitNumber, ref stabModifier));
floatDamage = MathF.Floor(floatDamage * stabModifier);
}
@ -64,7 +66,7 @@ public class Gen7DamageCalculator(bool hasRandomness) : IDamageCalculator
};
// TODO: script hook to modify the damage
// TODO: script hook to modify incoming damage
return damage;
}
@ -86,7 +88,7 @@ public class Gen7DamageCalculator(bool hasRandomness) : IDamageCalculator
return false;
byte critStage = 0;
// TODO: script hook to modify the crit stage
var random = battle.Random;
return critStage switch
{