Skip to content

Commit

Permalink
Have added curves for plotting scaling values
Browse files Browse the repository at this point in the history
This can be used for things such as player stat scaling on level ups or other use cases.
  • Loading branch information
grofit committed Jun 15, 2021
1 parent 7baed44 commit 68b5d3a
Show file tree
Hide file tree
Showing 13 changed files with 232 additions and 1 deletion.
3 changes: 2 additions & 1 deletion build/pack.bat
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ dotnet pack ../src/OpenRpg.Localization -c Release -o ../../_dist /p:version=%ve
dotnet pack ../src/OpenRpg.Data -c Release -o ../../_dist /p:version=%version%
dotnet pack ../src/OpenRpg.Quests -c Release -o ../../_dist /p:version=%version%
dotnet pack ../src/OpenRpg.Genres.Fantasy -c Release -o ../../_dist /p:version=%version%
dotnet pack ../src/OpenRpg.Cards -c Release -o ../../_dist /p:version=%version%
dotnet pack ../src/OpenRpg.Cards -c Release -o ../../_dist /p:version=%version%
dotnet pack ../src/OpenRpg.CurveFunctions -c Release -o ../../_dist /p:version=%version%
27 changes: 27 additions & 0 deletions src/OpenRpg.CurveFunctions/Curves/LogisticCurveFunction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System;
using OpenRpg.CurveFunctions.Extensions;

namespace OpenRpg.CurveFunctions.Curves
{
public class LogisticCurveFunction : ICurveFunction
{
public float Slope { get; }
public float VerticalSize { get; }
public float YShift { get; }
public float XShift { get; }

public LogisticCurveFunction(float slope, float xShift, float yShift, float verticalSize)
{
Slope = slope;
XShift = xShift;
YShift = yShift;
VerticalSize = verticalSize;
}

public float Plot(float value)
{
var outputValue = (float)(Slope / (1 + Math.Exp(-10.0 * VerticalSize * (value - 0.5 - XShift))) + YShift);
return this.SanitizeValue(outputValue);
}
}
}
25 changes: 25 additions & 0 deletions src/OpenRpg.CurveFunctions/Curves/LogitCurveFunction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System;
using OpenRpg.CurveFunctions.Extensions;

namespace OpenRpg.CurveFunctions.Curves
{
public class LogitCurveFunction : ICurveFunction
{
public float Slope { get; }
public float YShift { get; }
public float XShift { get; }

public LogitCurveFunction(float slope, float xShift, float yShift)
{
Slope = slope;
XShift = xShift;
YShift = yShift;
}

public float Plot(float value)
{
var outputValue = (float)(Slope * Math.Log((value - XShift) / (1.0 - (value - XShift))) / 5.0 + 0.5 + YShift);
return this.SanitizeValue(outputValue);
}
}
}
27 changes: 27 additions & 0 deletions src/OpenRpg.CurveFunctions/Curves/NormalCurveFunction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System;
using OpenRpg.CurveFunctions.Extensions;

namespace OpenRpg.CurveFunctions.Curves
{
public class NormalCurveFunction : ICurveFunction
{
public float Slope { get; }
public float YShift { get; }
public float XShift { get; }
public float Exponent { get; }

public NormalCurveFunction(float slope, float xShift, float yShift, float exponent)
{
Slope = slope;
XShift = xShift;
YShift = yShift;
Exponent = exponent;
}

public float Plot(float value)
{
var outputValue = (float)(Slope * Math.Exp(-30.0 * Exponent * (value - XShift - 0.5) * (value - XShift - 0.5)) + YShift);
return this.SanitizeValue(outputValue);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace OpenRpg.CurveFunctions.Curves
{
public class PassThroughlCurveFunction : ICurveFunction
{
public float Plot(float value)
{ return value; }
}
}
27 changes: 27 additions & 0 deletions src/OpenRpg.CurveFunctions/Curves/PolynomialCurveFunction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System;
using OpenRpg.CurveFunctions.Extensions;

namespace OpenRpg.CurveFunctions.Curves
{
public class PolynomialCurveFunction : ICurveFunction
{
public float Slope { get; }
public float Exponent { get; }
public float YShift { get; }
public float XShift { get; }

public PolynomialCurveFunction(float slope, float xShift, float yShift, float exponent)
{
Slope = slope;
XShift = xShift;
YShift = yShift;
Exponent = exponent;
}

public float Plot(float value)
{
var outputValue = Slope * (float)Math.Pow((value - XShift), Exponent) + YShift;
return this.SanitizeValue(outputValue);
}
}
}
25 changes: 25 additions & 0 deletions src/OpenRpg.CurveFunctions/Curves/SineCurveFunction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System;
using OpenRpg.CurveFunctions.Extensions;

namespace OpenRpg.CurveFunctions.Curves
{
public class SineCurveFunction : ICurveFunction
{
public float Slope { get; }
public float YShift { get; }
public float XShift { get; }

public SineCurveFunction(float slope, float xShift, float yShift)
{
Slope = slope;
XShift = xShift;
YShift = yShift;
}

public float Plot(float value)
{
var outputValue = (float)(0.5 * Slope * Math.Sin(2.0 * Math.PI * (value - XShift)) + 0.5 + YShift);
return this.SanitizeValue(outputValue);
}
}
}
19 changes: 19 additions & 0 deletions src/OpenRpg.CurveFunctions/Curves/StepCurveFunction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
namespace OpenRpg.CurveFunctions.Curves
{
public class StepCurveFunction : ICurveFunction
{
public float StepValue { get; }
public float MinValue { get; }
public float MaxValue { get; }

public StepCurveFunction(float stepValue, float minValue = 0.0f, float maxValue = 1.0f)
{
StepValue = stepValue;
MinValue = minValue;
MaxValue = maxValue;
}

public float Plot(float value)
{ return value < StepValue ? MinValue : MaxValue; }
}
}
14 changes: 14 additions & 0 deletions src/OpenRpg.CurveFunctions/Extensions/IEvaluatorExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace OpenRpg.CurveFunctions.Extensions
{
public static class ICurveFunctionExtensions
{
public static float SanitizeValue(this ICurveFunction curve, float value)
{
if(float.IsInfinity(value)) { return 0.0f; }
if(float.IsNaN(value)) { return 0.0f; }
if(value < 0 ) { return 0.0f; }
if(value > 1.0f ) { return 1.0f; }
return value;
}
}
}
7 changes: 7 additions & 0 deletions src/OpenRpg.CurveFunctions/ICurveFunction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace OpenRpg.CurveFunctions
{
public interface ICurveFunction
{
float Plot(float value);
}
}
14 changes: 14 additions & 0 deletions src/OpenRpg.CurveFunctions/OpenRpg.CurveFunctions.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<Version>0.0.0</Version>
<TargetFrameworks>netstandard2.0;net46</TargetFrameworks>
<Title>OpenRpg.CurveFunctions</Title>
<Authors>Grofit (LP)</Authors>
<PackageLicenseUrl>https://github.com/openrpg/OpenRpg/blob/master/LICENSE</PackageLicenseUrl>
<PackageProjectUrl>https://github.com/openrpg/OpenRpg</PackageProjectUrl>
<Description>Adds premade curve functions for scaling values or other use cases</Description>
<PackageTags>rpg game-development xna monogame unity godot</PackageTags>
</PropertyGroup>

</Project>
28 changes: 28 additions & 0 deletions src/OpenRpg.CurveFunctions/PresetCurves.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using OpenRpg.CurveFunctions.Curves;

namespace OpenRpg.CurveFunctions
{
public class PresetCurves
{
public static ICurveFunction Constant = new PolynomialCurveFunction(0f, 0, 0.5f, 0);
public static ICurveFunction Linear = new PolynomialCurveFunction(1.0f, 0, 0, 1.0f);
public static ICurveFunction InverseLinear = new PolynomialCurveFunction(-1.0f, 1.0f, 0, 1.0f);
public static ICurveFunction StandardCooldown = new PolynomialCurveFunction(1.0f, 0f, 0, 6.0f);
public static ICurveFunction StandardRuntime = new PolynomialCurveFunction(-1.0f, 0f, 1.0f, 6.0f);
public static ICurveFunction QuadraticLowerLeft = new PolynomialCurveFunction(1.0f, 1.0f, 0.0f, 4.0f);
public static ICurveFunction QuadraticLowerRight = new PolynomialCurveFunction(1.0f, 0.0f, 0.0f, 4.0f);
public static ICurveFunction QuadraticUpperLeft = new PolynomialCurveFunction(-1.0f, 1.0f, 1.0f, 4.0f);
public static ICurveFunction QuadraticUpperRight = new PolynomialCurveFunction(-1.0f, 0f, 1.0f, 4.0f);
public static ICurveFunction Logistic = new LogisticCurveFunction(1.0f, 0f, 0.0f, 1.0f);
public static ICurveFunction InverseLogistic = new LogisticCurveFunction(-1.0f, 0f, 1.0f, 1.0f);
public static ICurveFunction Logit = new LogitCurveFunction(1.0f, 0, 0);
public static ICurveFunction InverseLogit = new LogitCurveFunction(-1.0f, 0, 0);
public static ICurveFunction BellCurve = new NormalCurveFunction(1.0f, 0, 0, 1.0f);
public static ICurveFunction InverseBellCurve = new NormalCurveFunction(-1.0f, 0, 1.0f, 1.0f);
public static ICurveFunction SineWave = new SineCurveFunction(1.0f, 0, 0);
public static ICurveFunction InverseSineWave = new SineCurveFunction(-1.0f, 0, 0);
public static ICurveFunction GreaterThanHalf = new StepCurveFunction(0.5f);
public static ICurveFunction LessThanHalf = new StepCurveFunction(0.5f, 1.0f, 0.0f);
public static ICurveFunction PassThrough = new PassThroughlCurveFunction();
}
}
9 changes: 9 additions & 0 deletions src/OpenRpg.sln
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Cards", "Cards", "{C5E5EB5E
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OpenRpg.Cards", "OpenRpg.Cards\OpenRpg.Cards.csproj", "{2A8FA13E-2E89-457B-9457-46C345768495}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Scaling", "Scaling", "{1566BC2E-78DC-4998-BEC8-65CEC3EC9A88}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OpenRpg.CurveFunctions", "OpenRpg.CurveFunctions\OpenRpg.CurveFunctions.csproj", "{91C35205-4EB6-4204-964E-D94427D362AA}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -58,6 +62,10 @@ Global
{2A8FA13E-2E89-457B-9457-46C345768495}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2A8FA13E-2E89-457B-9457-46C345768495}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2A8FA13E-2E89-457B-9457-46C345768495}.Release|Any CPU.Build.0 = Release|Any CPU
{91C35205-4EB6-4204-964E-D94427D362AA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{91C35205-4EB6-4204-964E-D94427D362AA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{91C35205-4EB6-4204-964E-D94427D362AA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{91C35205-4EB6-4204-964E-D94427D362AA}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{86FD715B-0B33-40EB-B8AC-25EE62398A66} = {6BE3D2A4-F53A-4879-868E-A7522A445FE9}
Expand All @@ -67,5 +75,6 @@ Global
{8D5C52B0-B68B-4539-9D2F-DA22C825B923} = {56A39779-8FA4-499E-8EF9-4757765CC230}
{754A79DD-AC13-4EC1-9DDA-AF5D5F6DABCE} = {98CD79A9-0E41-49CE-8246-3A32C1A2E004}
{2A8FA13E-2E89-457B-9457-46C345768495} = {C5E5EB5E-7F08-4CF4-B4DF-F20434DE2930}
{91C35205-4EB6-4204-964E-D94427D362AA} = {1566BC2E-78DC-4998-BEC8-65CEC3EC9A88}
EndGlobalSection
EndGlobal

0 comments on commit 68b5d3a

Please sign in to comment.