Skip to content

Commit

Permalink
Better algorithm for generating a set of consistent set of random num…
Browse files Browse the repository at this point in the history
…bers, and fix for SEED_RANDOM()
  • Loading branch information
joethephish committed Sep 11, 2016
1 parent fc3b27f commit 1998410
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 11 deletions.
15 changes: 10 additions & 5 deletions ink-engine-runtime/Story.cs
Original file line number Diff line number Diff line change
Expand Up @@ -823,18 +823,19 @@ bool PerformLogicAndFlowControl(Runtime.Object contentObj)
Error ("Invalid value for maximum parameter of RANDOM(min, max)");

// +1 because it's inclusive of min and max, for e.g. RANDOM(1,6) for a dice roll.
//var randomRange = maxInt.value - minInt.value + 1;
if (maxInt.value < minInt.value)
var randomRange = maxInt.value - minInt.value + 1;
if (randomRange <= 0)
Error ("RANDOM was called with minimum as " + minInt.value + " and maximum as " + maxInt.value + ". The maximum must be larger");

var resultSeed = state.storySeed + state.randomIndex;
var resultSeed = state.storySeed + state.previousRandom;
var random = new Random(resultSeed);

var chosenValue = random.Next (minInt.value, maxInt.value+1);// //(random.Next () % randomRange) + minInt.value;
var nextRandom = random.Next ();
var chosenValue = (nextRandom % randomRange) + minInt.value;
state.PushEvaluationStack (new IntValue (chosenValue));

// Next random number (rather than keeping the Random object around)
state.randomIndex++;
state.previousRandom = nextRandom;
break;

case ControlCommand.CommandType.SeedRandom:
Expand All @@ -844,6 +845,10 @@ bool PerformLogicAndFlowControl(Runtime.Object contentObj)

// Story seed affects both RANDOM and shuffle behaviour
state.storySeed = seed.value;
state.previousRandom = 0;

// SEED_RANDOM returns nothing.
state.PushEvaluationStack (new Runtime.Void ());
break;

case ControlCommand.CommandType.VisitIndex:
Expand Down
10 changes: 5 additions & 5 deletions ink-engine-runtime/StoryState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public int VisitCountAtPathString(string pathString)
internal Dictionary<string, int> turnIndices { get; private set; }
internal int currentTurnIndex { get; private set; }
internal int storySeed { get; set; }
internal int randomIndex { get; set; }
internal int previousRandom { get; set; }
internal bool didSafeExit { get; set; }

internal Story story { get; set; }
Expand Down Expand Up @@ -168,7 +168,7 @@ internal StoryState (Story story)
// Seed the shuffle random numbers
int timeSeed = DateTime.Now.Millisecond;
storySeed = (new Random (timeSeed)).Next () % 100;
randomIndex = 0;
previousRandom = 0;

currentChoices = new List<Choice> ();

Expand Down Expand Up @@ -216,7 +216,7 @@ internal StoryState Copy()
copy.turnIndices = new Dictionary<string, int> (turnIndices);
copy.currentTurnIndex = currentTurnIndex;
copy.storySeed = storySeed;
copy.randomIndex = randomIndex;
copy.previousRandom = previousRandom;

copy.didSafeExit = didSafeExit;

Expand Down Expand Up @@ -274,7 +274,7 @@ public Dictionary<string, object> jsonToken
obj ["turnIndices"] = Json.IntDictionaryToJObject (turnIndices);
obj ["turnIdx"] = currentTurnIndex;
obj ["storySeed"] = storySeed;
obj ["randomIndex"] = randomIndex;
obj ["previousRandom"] = previousRandom;

obj ["inkSaveVersion"] = kInkSaveStateVersion;

Expand Down Expand Up @@ -322,7 +322,7 @@ public Dictionary<string, object> jsonToken
turnIndices = Json.JObjectToIntDictionary ((Dictionary<string, object>)jObject ["turnIndices"]);
currentTurnIndex = (int)jObject ["turnIdx"];
storySeed = (int)jObject ["storySeed"];
randomIndex = (int)jObject ["randomIndex"];
previousRandom = (int)jObject ["previousRandom"];

object jChoiceThreadsObj = null;
jObject.TryGetValue("choiceThreads", out jChoiceThreadsObj);
Expand Down
2 changes: 1 addition & 1 deletion inklecate/ParsedHierarchy/FunctionCall.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public override void GenerateIntoContainer (Runtime.Container container)
}

else if (isSeedRandom) {
if (arguments.Count != 2)
if (arguments.Count != 1)
Error ("SEED_RANDOM should take 1 parameter - an integer seed");

var num = arguments [0] as Number;
Expand Down

0 comments on commit 1998410

Please sign in to comment.