Skip to content

Commit

Permalink
added rest of the utilities (I'll learn them later)
Browse files Browse the repository at this point in the history
  • Loading branch information
GeorgeIpsum committed Nov 6, 2019
1 parent 552818a commit 6c70eb7
Show file tree
Hide file tree
Showing 12 changed files with 1,559 additions and 7 deletions.
41 changes: 41 additions & 0 deletions WeWereBound/Utilities/Cache.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System;
using System.Collections.Generic;

namespace WeWereBound
{
public static class Cache
{
public static Dictionary<Type, Stack<Entity>> cache;

private static void Init<T>() where T : Entity, new()
{
if (cache == null) cache = new Dictionary<Type, Stack<Entity>>();

if (!cache.ContainsKey(typeof(T))) cache.Add(typeof(T), new Stack<Entity>());
}

public static void Store<T>(T instance) where T : Entity, new()
{
Init<T>();
cache[typeof(T)].Push(instance);
}

public static T Create<T>() where T : Entity, new()
{
Init<T>();
if (cache[typeof(T)].Count > 0) return cache[typeof(T)].Pop() as T;
else return new T();
}

public static void Clear<T>() where T : Entity, new()
{
if (cache != null && cache.ContainsKey(typeof(T))) cache[typeof(T)].Clear();
}

public static void ClearAll()
{
if (cache != null)
foreach (var kv in cache) kv.Value.Clear();
}
}
}
75 changes: 75 additions & 0 deletions WeWereBound/Utilities/CheatListener.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
using System;
using System.Collections.Generic;

namespace WeWereBound
{
public class CheatListener : Entity
{
public string CurrentInput;
public bool Logging;

private List<Tuple<char, Func<bool>>> inputs;
private List<Tuple<string, Action>> cheats;
private int maxInput;

public CheatListener()
{
Visible = false;
CurrentInput = "";

inputs = new List<Tuple<char, Func<bool>>>();
cheats = new List<Tuple<string, Action>>();
}

public override void Update()
{
//Detect input
bool changed = false;
foreach (var input in inputs)
{
if (input.Item2())
{
CurrentInput += input.Item1;
changed = true;
}
}

//Handle changes
if (changed)
{
if (CurrentInput.Length > maxInput)
CurrentInput = CurrentInput.Substring(CurrentInput.Length - maxInput);

if (Logging)
Calc.Log(CurrentInput);

foreach (var cheat in cheats)
{
if (CurrentInput.Contains(cheat.Item1))
{
CurrentInput = "";
if (cheat.Item2 != null)
cheat.Item2();
cheats.Remove(cheat);

if (Logging)
Calc.Log("Cheat Activated: " + cheat.Item1);

break;
}
}
}
}

public void AddCheat(string code, Action onEntered = null)
{
cheats.Add(new Tuple<string, Action>(code, onEntered));
maxInput = Math.Max(code.Length, maxInput);
}

public void AddInput(char id, Func<bool> checker)
{
inputs.Add(new Tuple<char, Func<bool>>(id, checker));
}
}
}
170 changes: 170 additions & 0 deletions WeWereBound/Utilities/ChoiceSet.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
using System;
using System.Collections.Generic;

namespace WeWereBound
{
public class ChoiceSet<T>
{
public int TotalWeight { get; private set; }
private Dictionary<T, int> choices;

public ChoiceSet()
{
choices = new Dictionary<T, int>();
TotalWeight = 0;
}

/// <summary>
/// Sets the weight of a choice
/// </summary>
/// <param name="choice"></param>
/// <param name="weight"></param>
public void Set(T choice, int weight)
{
int oldWeight = 0;
choices.TryGetValue(choice, out oldWeight);
TotalWeight -= oldWeight;

if (weight <= 0)
{
if (choices.ContainsKey(choice))
choices.Remove(choice);
}
else
{
TotalWeight += weight;
choices[choice] = weight;
}
}

/// <summary>
/// Sets the weight of a choice, or gets its weight
/// </summary>
/// <param name="choice"></param>
/// <returns></returns>
public int this[T choice]
{
get
{
int weight = 0;
choices.TryGetValue(choice, out weight);
return weight;
}

set
{
Set(choice, value);
}
}

/// <summary>
/// Sets the chance of a choice. The chance is calculated based on the current state of ChoiceSet, so if
/// other choices are changed later the chance will not be guaranteed to remain the same
/// </summary>
/// <param name="choice"></param>
/// <param name="chance">A chance between 0 and 1.0f</param>
public void Set(T choice, float chance)
{
int oldWeight = 0;
choices.TryGetValue(choice, out oldWeight);
TotalWeight -= oldWeight;

int weight = (int)Math.Round(TotalWeight / (1f - chance));
if (weight <= 0 && chance > 0)
weight = 1;

if (weight <= 0)
{
if (choices.ContainsKey(choice))
choices.Remove(choice);
}
else
{
TotalWeight += weight;
choices[choice] = weight;
}
}

/// <summary>
/// Sets the chance of many choices. Takes the chance of any of the given choices being picked, not the chance of
/// any individual choice. The chances are calculated based on the current state of ChoiceSet, so if
/// other choices are changed later the chances will not be guaranteed to remain the same
/// </summary>
/// <param name="totalChance"></param>
/// <param name="choices">A chance between 0 and 1.0f</param>
public void SetMany(float totalChance, params T[] choices)
{
if (choices.Length > 0)
{
float chance = totalChance / choices.Length;

int oldTotalWeight = 0;
foreach (var c in choices)
{
int oldWeight = 0;
this.choices.TryGetValue(c, out oldWeight);
oldTotalWeight += oldWeight;
}
TotalWeight -= oldTotalWeight;

int weight = (int)Math.Round((TotalWeight / (1f - totalChance)) / choices.Length);
if (weight <= 0 && totalChance > 0)
weight = 1;

if (weight <= 0)
{
foreach (var c in choices)
if (this.choices.ContainsKey(c))
this.choices.Remove(c);
}
else
{
TotalWeight += weight * choices.Length;
foreach (var c in choices)
this.choices[c] = weight;
}
}
}

/// <summary>
/// Chooses a random choice in the set
/// </summary>
/// <param name="random"></param>
/// <returns></returns>
public T Get(Random random)
{
int at = random.Next(TotalWeight);

foreach (var kv in choices)
{
if (at < kv.Value)
return kv.Key;
else
at -= kv.Value;
}

throw new Exception("Random choice error!");
}

/// <summary>
/// Chooses a random choice in the set, using Calc.Random to choose
/// </summary>
/// <returns></returns>
public T Get()
{
return Get(Calc.Random);
}

private struct Choice
{
public T Data;
public int Weight;

public Choice(T data, int weight)
{
Data = data;
Weight = weight;
}
}
}
}
Loading

0 comments on commit 6c70eb7

Please sign in to comment.