Added first script, bugfixes

This commit is contained in:
Deukhoofd 2024-07-28 12:18:12 +02:00
parent 5b495ac871
commit 7c0bd879b8
6 changed files with 89 additions and 7 deletions

View File

@ -1,10 +1,14 @@
using JetBrains.Annotations;
using PkmnLib.Static.Utils;
namespace PkmnLib.Dynamic.ScriptHandling;
[AttributeUsage(AttributeTargets.Class)]
[MeansImplicitUse]
public class ScriptAttribute : Attribute
{
public ScriptCategory Category { get; }
public string Name { get; }
public StringKey Name { get; }
public ScriptAttribute(ScriptCategory category, string name)
{

View File

@ -1,12 +1,13 @@
using System.Linq.Expressions;
using System.Reflection;
using PkmnLib.Dynamic.Libraries;
using PkmnLib.Static.Utils;
namespace PkmnLib.Dynamic.ScriptHandling;
public class ScriptRegistry
{
private Dictionary<(ScriptCategory category, string name), Func<Script>> _scriptTypes = new();
private Dictionary<(ScriptCategory category, StringKey name), Func<Script>> _scriptTypes = new();
private IBattleStatCalculator? _battleStatCalculator;
private IDamageCalculator? _damageCalculator;
private IMiscLibrary? _miscLibrary;
@ -24,10 +25,8 @@ public class ScriptRegistry
}
}
public void RegisterScriptType(ScriptCategory category, string name, Type type)
public void RegisterScriptType(ScriptCategory category, StringKey name, Type type)
{
if (name == null)
throw new ArgumentNullException(nameof(name));
if (type == null)
throw new ArgumentNullException(nameof(type));
@ -49,7 +48,7 @@ public class ScriptRegistry
public void RegisterMiscLibrary<T>(T miscLibrary) where T : IMiscLibrary
=> _miscLibrary = miscLibrary;
internal Dictionary<(ScriptCategory category, string name), Func<Script>> ScriptTypes => _scriptTypes;
internal Dictionary<(ScriptCategory category, StringKey name), Func<Script>> ScriptTypes => _scriptTypes;
internal IBattleStatCalculator? BattleStatCalculator => _battleStatCalculator;
internal IDamageCalculator? DamageCalculator => _damageCalculator;
internal IMiscLibrary? MiscLibrary => _miscLibrary;

View File

@ -0,0 +1,52 @@
namespace PkmnLib.Static.Utils;
/// <summary>
/// Helper methods for numeric operations.
/// </summary>
public static class NumericHelpers
{
/// <summary>
/// Multiplies two values. If this overflows, returns <see cref="byte.MaxValue"/>.
/// </summary>
public static byte MultiplyOrMax(this byte value, byte multiplier)
{
var result = value * multiplier;
return result > byte.MaxValue ? byte.MaxValue : (byte)result;
}
/// <summary>
/// Multiplies two values. If this overflows, returns <see cref="byte.MaxValue"/>.
/// </summary>
public static byte MultiplyOrMax(this byte value, float multiplier)
{
var result = value * multiplier;
return result > byte.MaxValue ? byte.MaxValue : (byte)result;
}
/// <summary>
/// Multiplies two values. If this overflows, returns <see cref="sbyte.MaxValue"/>.
/// </summary>
public static sbyte MultiplyOrMax(this sbyte value, sbyte multiplier)
{
var result = value * multiplier;
return result > sbyte.MaxValue ? sbyte.MaxValue : (sbyte)result;
}
/// <summary>
/// Multiplies two values. If this overflows, returns <see cref="ushort.MaxValue"/>.
/// </summary>
public static ushort MultiplyOrMax(this ushort value, ushort multiplier)
{
var result = value * multiplier;
return result > ushort.MaxValue ? ushort.MaxValue : (ushort)result;
}
/// <summary>
/// Multiplies two values. If this overflows, returns <see cref="short.MaxValue"/>.
/// </summary>
public static short MultiplyOrMax(this short value, short multiplier)
{
var result = value * multiplier;
return result > short.MaxValue ? short.MaxValue : (short)result;
}
}

View File

@ -13,6 +13,8 @@ public readonly record struct StringKey
/// <inheritdoc cref="StringKey"/>
public StringKey(string key)
{
if (string.IsNullOrWhiteSpace(key))
throw new ArgumentException("Key cannot be null or whitespace.", nameof(key));
_key = key;
}

View File

@ -77,7 +77,7 @@ public class Gen7DamageCalculator(bool hasRandomness) : IDamageCalculator
{
if (executingMove.UseMove.Category == MoveCategory.Status)
return 0;
var basePower = hitData.BasePower;
var basePower = executingMove.UseMove.BasePower;
executingMove.RunScriptHook(script =>
script.ChangeBasePower(executingMove, target, hitNumber, ref basePower));
return basePower;

View File

@ -0,0 +1,25 @@
using PkmnLib.Dynamic.Models;
using PkmnLib.Dynamic.ScriptHandling;
using PkmnLib.Static.Utils;
namespace PkmnLib.Plugin.Gen7.Moves;
/// <summary>
/// The user nimbly strikes the target. If the user is not holding an item, this attack inflicts massive damage.
/// </summary>
/// <remarks>
/// Does double base power if the user is not holding an item.
/// </remarks>
[Script(ScriptCategory.Move, "acrobatics")]
public class Acrobatics : Script
{
/// <inheritdoc />
public override string Name => "acrobatics";
/// <inheritdoc />
public override void ChangeBasePower(IExecutingMove move, IPokemon target, byte hit, ref byte basePower)
{
if (move.User.HeldItem == null)
basePower = basePower.MultiplyOrMax(2);
}
}