Skip to content

Commit

Permalink
Added changes from ink 0.5.1
Browse files Browse the repository at this point in the history
Ported this dif : inkle/ink@0.5...0.5.1
  • Loading branch information
y-lohse committed Sep 20, 2016
1 parent 41ad12d commit 390c400
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 52 deletions.
18 changes: 13 additions & 5 deletions engine/ControlCommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ export class ControlCommand extends InkObject{
static TurnsSince(){
return new ControlCommand(CommandType.TurnsSince);
}
static Random(){
return new ControlCommand(CommandType.Random);
}
static SeedRandom(){
return new ControlCommand(CommandType.SeedRandom);
}
static VisitIndex(){
return new ControlCommand(CommandType.VisitIndex);
}
Expand Down Expand Up @@ -81,11 +87,13 @@ var CommandType = {
NoOp: 9,
ChoiceCount: 10,
TurnsSince: 11,
VisitIndex: 12,
SequenceShuffleIndex: 13,
StartThread: 14,
Done: 15,
End: 16,
Random: 12,
SeedRandom: 13,
VisitIndex: 14,
SequenceShuffleIndex: 15,
StartThread: 16,
Done: 17,
End: 18,
}
CommandType.TOTAL_VALUES = Object.keys(CommandType).length - 1;//-1 because NotSet shoudn't count
ControlCommand.CommandType = CommandType;
2 changes: 2 additions & 0 deletions engine/JsonSerialisation.js
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,8 @@ _controlCommandNames[ControlCommand.CommandType.EndString] = "/str";
_controlCommandNames[ControlCommand.CommandType.NoOp] = "nop";
_controlCommandNames[ControlCommand.CommandType.ChoiceCount] = "choiceCnt";
_controlCommandNames[ControlCommand.CommandType.TurnsSince] = "turns";
_controlCommandNames[ControlCommand.CommandType.Random] = "rnd";
_controlCommandNames[ControlCommand.CommandType.SeedRandom] = "srnd";
_controlCommandNames[ControlCommand.CommandType.VisitIndex] = "visit";
_controlCommandNames[ControlCommand.CommandType.SequenceShuffleIndex] = "seq";
_controlCommandNames[ControlCommand.CommandType.StartThread] = "thread";
Expand Down
50 changes: 46 additions & 4 deletions engine/Story.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export class Story extends InkObject{
constructor(jsonString){
super();

this.inkVersionCurrent = 12;
this.inkVersionCurrent = 13;
this.inkVersionMinimumCompatible = 12;

this._variableObservers = null;
Expand Down Expand Up @@ -121,7 +121,7 @@ export class Story extends InkObject{
this._state.ResetErrors();
}
ResetCallstack(){
this._state.ForceEndFlow();
this._state.ForceEnd();
}
ResetGlobals(){
if (this._mainContentContainer.namedContent["global decl"]){
Expand Down Expand Up @@ -706,6 +706,45 @@ export class Story extends InkObject{
this.state.PushEvaluationStack(new IntValue(turnCount));
break;

case ControlCommand.CommandType.Random:
var maxInt = this.state.PopEvaluationStack();
var minInt = this.state.PopEvaluationStack();

if (minInt == null || minInt instanceof IntValue === false)
this.Error("Invalid value for minimum parameter of RANDOM(min, max)");

if (maxInt == null || minInt instanceof IntValue === false)
this.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 (randomRange <= 0)
this.Error("RANDOM was called with minimum as " + minInt.value + " and maximum as " + maxInt.value + ". The maximum must be larger");

var resultSeed = this.state.storySeed + this.state.previousRandom;
var random = new PRNG(resultSeed);

var nextRandom = random.Next();
var chosenValue = (nextRandom % randomRange) + minInt.value;
this.state.PushEvaluationStack(new IntValue(chosenValue));

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

case ControlCommand.CommandType.SeedRandom:
var seed = this.state.PopEvaluationStack();
if (seed == null || seed instanceof IntValue === false)
this.Error("Invalid value passed to SEED_RANDOM");

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

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

case ControlCommand.CommandType.VisitIndex:
var count = this.VisitCountForContainer(this.state.currentContainer) - 1; // index not count
this.state.PushEvaluationStack(new IntValue(count));
Expand All @@ -732,13 +771,16 @@ export class Story extends InkObject{
// In normal flow - allow safe exit without warning
else {
this.state.didSafeExit = true;

// Stop flow in current thread
this.state.currentContentObject = null;
}

break;

// Force flow to end completely
case ControlCommand.CommandType.End:
this.state.ForceEndFlow();
this.state.ForceEnd();
break;

default:
Expand Down Expand Up @@ -1351,6 +1393,6 @@ export class Story extends InkObject{
this.state.AddError(message);

// In a broken state don't need to know about any other errors.
this.state.ForceEndFlow();
this.state.ForceEnd();
}
}
67 changes: 24 additions & 43 deletions engine/StoryState.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,12 @@ export class StoryState{
this.divertedTargetObject = null;

var timeSeed = (new Date()).getTime();
this._storySeed = timeSeed + '-' + Math.round(Math.random() * 9999);
this._storySeed = (new PRNG(timeSeed)).next() % 100;
this.storySeed = (new PRNG(timeSeed)).next() % 100;
this.previousRandom = 0;

this._currentChoices = [];
this._currentErrors = null;

this._currentRightGlue;

this.didSafeExit = false;

this.GoToStart();
Expand All @@ -58,9 +56,6 @@ export class StoryState{
get variablesState(){
return this._variablesState;
}
get storySeed(){
return this._storySeed;
}
get currentContentObject(){
return this.callStack.currentElement.currentObject;
}
Expand Down Expand Up @@ -117,6 +112,18 @@ export class StoryState{
}
return -1;
}
get currentRightGlue(){
for (var i = this._outputStream.length - 1; i >= 0; i--) {
var c = this._outputStream[i];
// var glue = c as Glue;
var glue = c;
if (glue instanceof Glue && glue.isRight)
return glue;
else if (c instanceof ControlCommand) // e.g. BeginString
break;
}
return null;
}
get inStringEvaluation(){
for (var i = this._outputStream.length - 1; i >= 0; i--) {
// var cmd = this._outputStream[i] as ControlCommand;
Expand Down Expand Up @@ -193,14 +200,7 @@ export class StoryState{
obj["outputStream"] = JsonSerialisation.ListToJArray(this._outputStream);

obj["currentChoices"] = JsonSerialisation.ListToJArray(this.currentChoices);

if (this._currentRightGlue) {
var rightGluePos = this._outputStream.indexOf(this._currentRightGlue);
if( rightGluePos != -1 ) {
obj["currRightGlue"] = this._outputStream.indexOf(this._currentRightGlue);
}
}


if( this.divertedTargetObject != null )
obj["currentDivertTarget"] = this.divertedTargetObject.path.componentsString;

Expand Down Expand Up @@ -237,15 +237,6 @@ export class StoryState{
// currentChoices = Json.JArrayToRuntimeObjList<Choice>((JArray)jObject ["currentChoices"]);
this._currentChoices = JsonSerialisation.JArrayToRuntimeObjList(jObject["currentChoices"]);

var propValue;
if( propValue = jObject["currRightGlue"] ) {
var gluePos = parseInt(propValue);
if( gluePos >= 0 ) {
// _currentRightGlue = _outputStream [gluePos] as Glue;
this._currentRightGlue = this._outputStream[gluePos];
}
}

var currentDivertTargetPath = jObject["currentDivertTarget"];
if (currentDivertTargetPath != null) {
var divertPath = new Path(currentDivertTargetPath.toString());
Expand All @@ -255,7 +246,7 @@ export class StoryState{
this._visitCounts = JsonSerialisation.JObjectToIntDictionary(jObject["visitCounts"]);
this._turnIndices = JsonSerialisation.JObjectToIntDictionary(jObject["turnIndices"]);
this._currentTurnIndex = parseInt(jObject["turnIdx"]);
this._storySeed = parseInt(jObject["storySeed"]);
this.storySeed = parseInt(jObject["storySeed"]);

// var jChoiceThreads = jObject["choiceThreads"] as JObject;
var jChoiceThreads = jObject["choiceThreads"];
Expand Down Expand Up @@ -396,10 +387,8 @@ export class StoryState{

if (glue instanceof Glue) {
// Found matching left-glue for right-glue? Close it.
var foundMatchingLeftGlue = glue.isLeft && this._currentRightGlue && glue.parent == this._currentRightGlue.parent;
if (foundMatchingLeftGlue) {
this._currentRightGlue = null;
}
var existingRightGlue = this.currentRightGlue;
var foundMatchingLeftGlue = !!(glue.isLeft && existingRightGlue && glue.parent == existingRightGlue.parent);

// Left/Right glue is auto-generated for inline expressions
// where we want to absorb newlines but only in a certain direction.
Expand All @@ -408,13 +397,7 @@ export class StoryState{
this.TrimNewlinesFromOutputStream(foundMatchingLeftGlue);
}

// New right-glue
var isNewRightGlue = glue.isRight && this._currentRightGlue == null;
if (isNewRightGlue) {
this._currentRightGlue = glue;
}

includeInOutput = glue.isBi || isNewRightGlue;
includeInOutput = glue.isBi || glue.isRight;
}

else if( text instanceof StringValue ) {
Expand All @@ -432,7 +415,6 @@ export class StoryState{
// Able to completely reset when
else if (text.isNonWhitespace) {
this.RemoveExistingGlue();
this._currentRightGlue = null;
}
} else if (text.isNewline) {
if (this.outputStreamEndsInNewline || !this.outputStreamContainsContent)
Expand Down Expand Up @@ -516,7 +498,7 @@ export class StoryState{
}
}
}
ForceEndFlow(){
ForceEnd(){
this.currentContentObject = null;

while (this.callStack.canPopThread)
Expand Down Expand Up @@ -563,9 +545,7 @@ export class StoryState{
}

copy.callStack = new CallStack(this.callStack);

copy._currentRightGlue = this._currentRightGlue;


copy._variablesState = new VariablesState(copy.callStack);
copy.variablesState.CopyFrom(this.variablesState);

Expand All @@ -579,7 +559,8 @@ export class StoryState{
copy._visitCounts = this._visitCounts;
copy._turnIndices = this._turnIndices;
copy._currentTurnIndex = this.currentTurnIndex;
copy._storySeed = this.storySeed;
copy.storySeed = this.storySeed;
copy.previousRandom = this.previousRandom;

copy.didSafeExit = this.didSafeExit;

Expand All @@ -594,5 +575,5 @@ export class StoryState{
}
}

StoryState.kInkSaveStateVersion = 4;
StoryState.kInkSaveStateVersion = 5;
StoryState.kMinCompatibleLoadVersion = 4;

0 comments on commit 390c400

Please sign in to comment.