Skip to content

Commit

Permalink
misc
Browse files Browse the repository at this point in the history
- AttackingScript and CastingScript now use PickRandomWeightedSingleOrDefault, rather than consecutive chances
  • Loading branch information
Sichii committed May 1, 2024
1 parent 5e68abe commit 5d93adc
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 14 deletions.
24 changes: 24 additions & 0 deletions Chaos.Common/Utilities/DecimalRandomizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,30 @@ public static class DecimalRandomizer
return weightedChoices.PickRandomWeighted();
}

/// <summary>
/// Picks a random choice based on the common weight. Chances are NOT exhaustive. Only picks at most 1 item. Each item
/// has the same chance to be picked.
/// </summary>
/// <param name="choices">
/// A collection of choiced
/// </param>
/// <param name="commonWeight">
/// The weight of each choice
/// </param>
/// <typeparam name="T">
/// The type of object to return
/// </typeparam>
/// <returns>
/// A random element from the specified collection if a choice is taken, otherwise
/// <c>
/// default
/// </c>
/// </returns>
public static T? PickRandomWeightedSingleOrDefault<T>(this IEnumerable<T> choices, decimal commonWeight)
=> choices.Select(x => new KeyValuePair<T, decimal>(x, commonWeight))
.ToList()
.PickRandomWeightedSingleOrDefault();

/// <summary>
/// Picks a random choice based on the weights. The higher the weight, the more likely it is to be picked. Chances are
/// NOT exhaustive. Only picks at most 1 item.
Expand Down
24 changes: 24 additions & 0 deletions Chaos.Common/Utilities/IntegerRandomizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,30 @@ public static class IntegerRandomizer
return weightedChoices.PickRandomWeighted();
}

/// <summary>
/// Picks a random choice based on the common weight. Chances are NOT exhaustive. Only picks at most 1 item. Each item
/// has the same chance to be picked.
/// </summary>
/// <param name="choices">
/// A collection of choiced
/// </param>
/// <param name="commonWeight">
/// The weight of each choice
/// </param>
/// <typeparam name="T">
/// The type of object to return
/// </typeparam>
/// <returns>
/// A random element from the specified collection if a choice is taken, otherwise
/// <c>
/// default
/// </c>
/// </returns>
public static T? PickRandomWeightedSingleOrDefault<T>(this IEnumerable<T> choices, int commonWeight)
=> choices.Select(x => new KeyValuePair<T, int>(x, commonWeight))
.ToList()
.PickRandomWeightedSingleOrDefault();

/// <summary>
/// Picks a random choice based on the weights. The higher the weight, the more likely it is to be picked. Chances are
/// NOT exhaustive. Only picks at most 1 item.
Expand Down
12 changes: 6 additions & 6 deletions Chaos/Scripting/MonsterScripts/AttackingScript.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ public override void Update(TimeSpan delta)
attacked |= Subject.TryUseSkill(skill);

if (ShouldUseSkill)
foreach (var skill in Skills)
if (!skill.Template.IsAssail && IntegerRandomizer.RollChance(7) && Subject.TryUseSkill(skill))
{
attacked = true;
{
var skill = Skills.Where(skill => Subject.CanUse(skill, out _))
.PickRandomWeightedSingleOrDefault(7);

break;
}
if (skill is not null)
attacked |= Subject.TryUseSkill(skill);
}

if (attacked)
{
Expand Down
22 changes: 14 additions & 8 deletions Chaos/Scripting/MonsterScripts/CastingScript.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Chaos.Common.Utilities;
using Chaos.Extensions;
using Chaos.Extensions.Common;
using Chaos.Models.World;
Expand All @@ -22,14 +23,19 @@ public override void Update(TimeSpan delta)

Spells.ShuffleInPlace();

foreach (var spell in Spells)
if (Subject.TryUseSpell(spell, Target.Id))
{
Subject.WanderTimer.Reset();
Subject.MoveTimer.Reset();
Subject.SkillTimer.Reset();
var spell = Spells.Where(
spell => Subject.CanUse(
spell,
Target,
null,
out _))
.PickRandomWeightedSingleOrDefault(7);

break;
}
if (spell is not null && Subject.TryUseSpell(spell, Target.Id))
{
Subject.WanderTimer.Reset();
Subject.MoveTimer.Reset();
Subject.SkillTimer.Reset();
}
}
}

0 comments on commit 5d93adc

Please sign in to comment.