Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integrate dice rolling functionality #15

Merged
merged 6 commits into from Jul 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
56 changes: 56 additions & 0 deletions Docs/LicensesConsumed.md
@@ -0,0 +1,56 @@
#Included Package Licenses:

As part of the SOSCSRPG project, we use a few NuGet packages from external developers. As part of their license agreements, we will list those components here with their license.

##D20Tek.DiceNotation.Net47 & D20Tek.Common.Net47:
--------------------------------------------------
MIT License

Copyright (c) 2020 DarthPedro

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

https://dev.azure.com/d20Tek/Libraries/_git/dicenotation?path=%2FLICENSE

##Newtonsoft.Json:
------------------
The MIT License (MIT)

Copyright (c) 2007 James Newton-King

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

https://github.com/JamesNK/Newtonsoft.Json/blob/master/LICENSE.md

##MathNet.Numerics:
-------------------
Copyright (c) 2002-2020 Math.NET

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

https://github.com/mathnet/mathnet-numerics/blob/master/LICENSE.md

21 changes: 7 additions & 14 deletions Engine/Actions/AttackWithWeapon.cs
Expand Up @@ -6,29 +6,22 @@ namespace Engine.Actions
{
public class AttackWithWeapon : BaseAction, IAction
{
private readonly int _maximumDamage;
private readonly int _minimumDamage;
private readonly string _damageDice;

public AttackWithWeapon(GameItem itemInUse, int minimumDamage, int maximumDamage)
public AttackWithWeapon(GameItem itemInUse, string damageDice)
: base(itemInUse)
{
if(itemInUse.Category != GameItem.ItemCategory.Weapon)
if (itemInUse.Category != GameItem.ItemCategory.Weapon)
{
throw new ArgumentException($"{itemInUse.Name} is not a weapon");
}

if(minimumDamage < 0)
if (string.IsNullOrWhiteSpace(damageDice))
{
throw new ArgumentException("minimumDamage must be 0 or larger");
throw new ArgumentException("damageDice must be valid dice notation");
}

if(maximumDamage < minimumDamage)
{
throw new ArgumentException("maximumDamage must be >= minimumDamage");
}

_minimumDamage = minimumDamage;
_maximumDamage = maximumDamage;
_damageDice = damageDice;
}

public void Execute(LivingEntity actor, LivingEntity target)
Expand All @@ -38,7 +31,7 @@ public void Execute(LivingEntity actor, LivingEntity target)

if(CombatService.AttackSucceeded(actor, target))
{
int damage = RandomNumberGenerator.NumberBetween(_minimumDamage, _maximumDamage);
int damage = DiceService.Instance.Roll(_damageDice).Value;

ReportResult($"{actorName} hit {targetName} for {damage} point{(damage > 1 ? "s" : "")}.");

Expand Down
49 changes: 47 additions & 2 deletions Engine/Engine.csproj
Expand Up @@ -9,8 +9,9 @@
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Engine</RootNamespace>
<AssemblyName>Engine</AssemblyName>
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
<TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
Expand All @@ -30,11 +31,53 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="D20Tek.Common.Net47, Version=3.1.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\D20Tek.Common.Net47.3.1.56\lib\netstandard2.0\D20Tek.Common.Net47.dll</HintPath>
</Reference>
<Reference Include="D20Tek.DiceNotation.Net47, Version=3.1.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\D20Tek.DiceNotation.Net47.3.1.30\lib\netstandard2.0\D20Tek.DiceNotation.Net47.dll</HintPath>
</Reference>
<Reference Include="MathNet.Numerics, Version=4.11.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\MathNet.Numerics.4.11.0\lib\net461\MathNet.Numerics.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Bcl.AsyncInterfaces, Version=1.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.Bcl.AsyncInterfaces.1.1.1\lib\net461\Microsoft.Bcl.AsyncInterfaces.dll</HintPath>
</Reference>
<Reference Include="Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.12.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Buffers, Version=4.0.3.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.Buffers.4.5.1\lib\net461\System.Buffers.dll</HintPath>
</Reference>
<Reference Include="System.ComponentModel.Annotations, Version=4.2.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.ComponentModel.Annotations.4.7.0\lib\net461\System.ComponentModel.Annotations.dll</HintPath>
</Reference>
<Reference Include="System.ComponentModel.DataAnnotations" />
<Reference Include="System.Core" />
<Reference Include="System.Memory, Version=4.0.1.1, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.Memory.4.5.4\lib\net461\System.Memory.dll</HintPath>
</Reference>
<Reference Include="System.Numerics" />
<Reference Include="System.Numerics.Vectors, Version=4.1.4.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Numerics.Vectors.4.5.0\lib\net46\System.Numerics.Vectors.dll</HintPath>
</Reference>
<Reference Include="System.Runtime.CompilerServices.Unsafe, Version=4.0.6.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Runtime.CompilerServices.Unsafe.4.7.1\lib\net461\System.Runtime.CompilerServices.Unsafe.dll</HintPath>
</Reference>
<Reference Include="System.Runtime.Serialization" />
<Reference Include="System.Text.Encodings.Web, Version=4.0.5.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.Text.Encodings.Web.4.7.1\lib\net461\System.Text.Encodings.Web.dll</HintPath>
</Reference>
<Reference Include="System.Text.Json, Version=4.0.1.2, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.Text.Json.4.7.2\lib\net461\System.Text.Json.dll</HintPath>
</Reference>
<Reference Include="System.Threading.Tasks.Extensions, Version=4.2.0.1, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.Threading.Tasks.Extensions.4.5.4\lib\net461\System.Threading.Tasks.Extensions.dll</HintPath>
</Reference>
<Reference Include="System.ValueTuple, Version=4.0.3.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.ValueTuple.4.5.0\lib\net47\System.ValueTuple.dll</HintPath>
</Reference>
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
Expand Down Expand Up @@ -72,9 +115,10 @@
<Compile Include="Models\Trader.cs" />
<Compile Include="Models\World.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="RandomNumberGenerator.cs" />
<Compile Include="Models\Battle.cs" />
<Compile Include="Services\CombatService.cs" />
<Compile Include="Services\DiceService.cs" />
<Compile Include="Services\IDiceService.cs" />
<Compile Include="Services\InventoryService.cs" />
<Compile Include="Services\LoggingService.cs" />
<Compile Include="Services\MessageBroker.cs" />
Expand All @@ -83,6 +127,7 @@
<Compile Include="ViewModels\GameSession.cs" />
</ItemGroup>
<ItemGroup>
<None Include="app.config" />
<None Include="Images\Locations\FarmFields.png">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
Expand Down
4 changes: 1 addition & 3 deletions Engine/Factories/ItemFactory.cs
Expand Up @@ -62,9 +62,7 @@ private static void LoadItemsFromNodes(XmlNodeList nodes)
if(itemCategory == GameItem.ItemCategory.Weapon)
{
gameItem.Action =
new AttackWithWeapon(gameItem,
node.AttributeAsInt("MinimumDamage"),
node.AttributeAsInt("MaximumDamage"));
new AttackWithWeapon(gameItem, node.AttributeAsString("DamageDice"));
}
else if(itemCategory == GameItem.ItemCategory.Consumable)
{
Expand Down
10 changes: 5 additions & 5 deletions Engine/GameData/GameItems.xml
@@ -1,11 +1,11 @@
<?xml version="1.0" encoding="utf-8" ?>
<GameItems>
<Weapons>
<Weapon ID="1001" Name="Pointy stick" Price="1" MinimumDamage="1" MaximumDamage="2"/>
<Weapon ID="1002" Name="Rusty sword" Price="5" MinimumDamage="1" MaximumDamage="3"/>
<Weapon ID="1501" Name="Snake fang" Price="0" MinimumDamage="1" MaximumDamage="2"/>
<Weapon ID="1502" Name="Rat claw" Price="0" MinimumDamage="1" MaximumDamage="2"/>
<Weapon ID="1503" Name="Spider fang" Price="0" MinimumDamage="1" MaximumDamage="4"/>
<Weapon ID="1001" Name="Pointy stick" Price="1" DamageDice="1d2"/>
<Weapon ID="1002" Name="Rusty sword" Price="5" DamageDice="1d3"/>
<Weapon ID="1501" Name="Snake fang" Price="0" DamageDice="1d2"/>
<Weapon ID="1502" Name="Rat claw" Price="0" DamageDice="1d2"/>
<Weapon ID="1503" Name="Spider fang" Price="0" DamageDice="1d4"/>
</Weapons>
<HealingItems>
<HealingItem ID="2001" Name="Granola bar" Price="5" HitPointsToHeal="2"/>
Expand Down
3 changes: 2 additions & 1 deletion Engine/Models/Location.cs
@@ -1,6 +1,7 @@
using System.Collections.Generic;
using System.Linq;
using Engine.Factories;
using Engine.Services;
using Newtonsoft.Json;

namespace Engine.Models
Expand Down Expand Up @@ -62,7 +63,7 @@ public Monster GetMonster()
int totalChances = MonstersHere.Sum(m => m.ChanceOfEncountering);

// Select a random number between 1 and the total (in case the total chances is not 100).
int randomNumber = RandomNumberGenerator.NumberBetween(1, totalChances);
int randomNumber = DiceService.Instance.Roll(totalChances, 1).Value;

// Loop through the monster list,
// adding the monster's percentage chance of appearing to the runningTotal variable.
Expand Down
3 changes: 2 additions & 1 deletion Engine/Models/Monster.cs
@@ -1,5 +1,6 @@
using System.Collections.Generic;
using Engine.Factories;
using Engine.Services;

namespace Engine.Models
{
Expand Down Expand Up @@ -46,7 +47,7 @@ public Monster GetNewInstance()
newMonster.AddItemToLootTable(itemPercentage.ID, itemPercentage.Percentage);

// Populate the new monster's inventory, using the loot table
if(RandomNumberGenerator.NumberBetween(1, 100) <= itemPercentage.Percentage)
if(DiceService.Instance.Roll(100).Value <= itemPercentage.Percentage)
{
newMonster.AddItemToInventory(ItemFactory.CreateGameItem(itemPercentage.ID));
}
Expand Down
44 changes: 0 additions & 44 deletions Engine/RandomNumberGenerator.cs

This file was deleted.

8 changes: 4 additions & 4 deletions Engine/Services/CombatService.cs
Expand Up @@ -17,10 +17,10 @@ public static Combatant FirstAttacker(Player player, Monster opponent)
int playerDexterity = player.Dexterity * player.Dexterity;
int opponentDexterity = opponent.Dexterity * opponent.Dexterity;
decimal dexterityOffset = (playerDexterity - opponentDexterity) / 10m;
int randomOffset = RandomNumberGenerator.NumberBetween(-10, 10);
int randomOffset = DiceService.Instance.Roll(20).Value - 10;
decimal totalOffset = dexterityOffset + randomOffset;

return RandomNumberGenerator.NumberBetween(0, 100) <= 50 + totalOffset
return DiceService.Instance.Roll(100).Value <= 50 + totalOffset
? Combatant.Player
: Combatant.Opponent;
}
Expand All @@ -33,10 +33,10 @@ public static bool AttackSucceeded(LivingEntity attacker, LivingEntity target)
int playerDexterity = attacker.Dexterity * attacker.Dexterity;
int opponentDexterity = target.Dexterity * target.Dexterity;
decimal dexterityOffset = (playerDexterity - opponentDexterity) / 10m;
int randomOffset = RandomNumberGenerator.NumberBetween(-10, 10);
int randomOffset = DiceService.Instance.Roll(20).Value - 10;
decimal totalOffset = dexterityOffset + randomOffset;

return RandomNumberGenerator.NumberBetween(0, 100) <= 50 + totalOffset;
return DiceService.Instance.Roll(100).Value <= 50 + totalOffset;
}
}
}
66 changes: 66 additions & 0 deletions Engine/Services/DiceService.cs
@@ -0,0 +1,66 @@
using D20Tek.DiceNotation;
using D20Tek.DiceNotation.DieRoller;
using System;

namespace Engine.Services
{
public class DiceService : IDiceService
{
private static readonly IDiceService _instance = new DiceService();

/// <summary>
/// Make constructor private to implement singletone pattern.
/// </summary>
private DiceService()
{
}

/// <summary>
/// Static singleton property
/// </summary>
public static IDiceService Instance => _instance;

//--- IDiceService implementation

public IDice Dice { get; } = new Dice();

public IDieRoller DieRoller { get; private set; } = new RandomDieRoller();

public IDiceConfiguration Configuration => Dice.Configuration;

public IDieRollTracker RollTracker { get; private set; } = null;

public void Configure(RollerType rollerType, bool enableTracker = false, int constantValue = 1)
{
RollTracker = enableTracker ? new DieRollTracker() : null;

switch (rollerType)
{
case RollerType.Random:
DieRoller = new RandomDieRoller(RollTracker);
break;
case RollerType.Crypto:
DieRoller = new CryptoDieRoller(RollTracker);
break;
case RollerType.MathNet:
DieRoller = new MathNetDieRoller(RollTracker);
break;
case RollerType.Constant:
DieRoller = new ConstantDieRoller(constantValue);
break;
default:
throw new ArgumentOutOfRangeException(nameof(rollerType));
}
}

public DiceResult Roll(string diceNotation) => Dice.Roll(diceNotation, DieRoller);

public DiceResult Roll(int sides, int numDice = 1, int modifier = 0)
{
DiceResult result = Dice.Dice(sides, numDice).Constant(modifier).Roll(DieRoller);
Dice.Clear();

return result;
}
}
}