Skip to content

Commit

Permalink
Merge remote-tracking branch 'refs/remotes/origin/dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
y-lohse committed Dec 27, 2016
2 parents bd8a308 + ceec953 commit 706b6b9
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 44 deletions.
13 changes: 13 additions & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,19 @@ module.exports = function(grunt) {
},
release: {
dest: 'dist/ink-es2015.js',
src: exposedFiles
},
release_min: {
options: {
plugins: [
babel({
exclude: 'node_modules/**',
presets: ['babili'],
comments: false
}),
],
},
dest: 'dist/ink-es2015.min.js',
src: exposedFiles
},
legacy: {
Expand Down
5 changes: 4 additions & 1 deletion engine/Path.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,10 @@ export class Path{

var componentStrings = componentsStr.split('.');
componentStrings.forEach(str => {
if (!isNaN(parseInt(str))){
//we need to distinguish between named components that start with a number, eg "42somewhere", and indexed components
//the normal parseInt won't do for the detection because it's too relaxed.
//see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt
if (/^(\-|\+)?([0-9]+|Infinity)$/.test(str)){
this.components.push(new Component(parseInt(str)));
}
else{
Expand Down
5 changes: 0 additions & 5 deletions engine/Polyfill.js

This file was deleted.

24 changes: 15 additions & 9 deletions engine/Story.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,14 @@ import {VariableReference} from './VariableReference';
import {NativeFunctionCall} from './NativeFunctionCall';
import {StoryException} from './StoryException';
import {PRNG} from './PRNG';
import {Polyfill} from './Polyfill';
import {StringBuilder} from './StringBuilder';

if (!Number.isInteger) {
Number.isInteger = function isInteger (nVal) {
return typeof nVal === "number" && isFinite(nVal) && nVal > -9007199254740992 && nVal < 9007199254740992 && Math.floor(nVal) === nVal;
};
}

export class Story extends InkObject{
constructor(jsonString){
super();
Expand All @@ -28,6 +33,7 @@ export class Story extends InkObject{

this._variableObservers = null;
this._externals = {};
this._prevContainerSet = null;

if (jsonString instanceof Container){
this._mainContentContainer = jsonString;
Expand Down Expand Up @@ -104,7 +110,7 @@ export class Story extends InkObject{
}
}
get canContinue(){
return this.state.currentContentObject != null && !this.state.hasError;
return this.state.canContinue;
}

get globalTags(){
Expand Down Expand Up @@ -233,7 +239,7 @@ export class Story extends InkObject{
// Hello world\n // record state at the end of here
// ~ complexCalculation() // don't actually need this unless it generates text
if( stateAtLastNewline == null ) {
stateAtLastNewline = this.StateSnapshot();
stateAtLastNewline = this.StateSnapshot();
}
}

Expand Down Expand Up @@ -261,7 +267,7 @@ export class Story extends InkObject{
this.Error("Thread available to pop, threads should always be flat by the end of evaluation?");
}

if( this.currentChoices.length == 0 && !this.state.didSafeExit && this._temporaryEvaluationContainer == null) {
if( this.state.generatedChoices.length == 0 && !this.state.didSafeExit && this._temporaryEvaluationContainer == null) {
if( this.state.callStack.CanPop(PushPopType.Tunnel) ) {
this.Error("unexpectedly reached end of content. Do you need a '->->' to return from a tunnel?");
} else if( this.state.callStack.CanPop(PushPopType.Function) ) {
Expand Down Expand Up @@ -356,7 +362,7 @@ export class Story extends InkObject{
if (choicePoint instanceof ChoicePoint) {
var choice = this.ProcessChoice(choicePoint);
if (choice) {
this.state.currentChoices.push(choice);
this.state.generatedChoices.push(choice);
}

currentContentObj = null;
Expand Down Expand Up @@ -422,12 +428,12 @@ export class Story extends InkObject{
return;

// First, find the previously open set of containers
var prevContainerSet = [];
if (this._prevContainerSet == null) this._prevContainerSet = [];
if (previousContentObject) {
// Container prevAncestor = previousContentObject as Container ?? previousContentObject.parent as Container;
var prevAncestor = (previousContentObject instanceof Container) ? previousContentObject : previousContentObject.parent;
while (prevAncestor instanceof Container) {
prevContainerSet.push(prevAncestor);
this._prevContainerSet.push(prevAncestor);
// prevAncestor = prevAncestor.parent as Container;
prevAncestor = prevAncestor.parent;
}
Expand All @@ -438,7 +444,7 @@ export class Story extends InkObject{
var currentChildOfContainer = newContentObject;
// Container currentContainerAncestor = currentChildOfContainer.parent as Container;
var currentContainerAncestor = currentChildOfContainer.parent;
while (currentContainerAncestor instanceof Container && prevContainerSet.indexOf(currentContainerAncestor) < 0) {
while (currentContainerAncestor instanceof Container && this._prevContainerSet.indexOf(currentContainerAncestor) < 0) {

// Check whether this ancestor container is being entered at the start,
// by checking whether the child object is the first.
Expand Down Expand Up @@ -719,7 +725,7 @@ export class Story extends InkObject{
break;

case ControlCommand.CommandType.ChoiceCount:
var choiceCount = this.currentChoices.length;
var choiceCount = this.state.generatedChoices.length;
this.state.PushEvaluationStack(new IntValue(choiceCount));
break;

Expand Down
92 changes: 66 additions & 26 deletions engine/StoryState.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ export class StoryState{
this.story = story;

this._outputStream = [];
this._outputStreamTextDirty = true;
this._outputStreamTagsDirty = true;
this.OutputStreamDirty();

this._evaluationStack = [];

Expand All @@ -36,6 +39,8 @@ export class StoryState{
this.previousRandom = 0;

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

this.didSafeExit = false;
Expand All @@ -47,6 +52,13 @@ export class StoryState{
this.GoToStart();
}
get currentChoices(){
// If we can continue generating text content rather than choices,
// then we reflect the choice list as being empty, since choices
// should always come at the end.
if ( this.canContinue ) return [];
return this._currentChoices;
}
get generatedChoices(){
return this._currentChoices;
}
get currentErrors(){
Expand All @@ -70,6 +82,9 @@ export class StoryState{
set currentContentObject(value){
this.callStack.currentElement.currentObject = value;
}
get canContinue(){
return this.currentContentObject != null && !this.hasError;
}
get hasError(){
return this.currentErrors != null && this.currentErrors.length > 0;
}
Expand Down Expand Up @@ -144,30 +159,39 @@ export class StoryState{
return false;
}
get currentText(){
var sb = new StringBuilder();

this._outputStream.forEach(outputObj => {
// var textContent = outputObj as StringValue;
var textContent = outputObj;
if (textContent instanceof StringValue) {
sb.Append(textContent.value);
}
});
if( this._outputStreamTextDirty ) {
var sb = new StringBuilder();

this._outputStream.forEach(outputObj => {
// var textContent = outputObj as StringValue;
var textContent = outputObj;
if (textContent instanceof StringValue) {
sb.Append(textContent.value);
}
});

return sb.toString();
this._currentText = sb.toString();
this._outputStreamTextDirty = false;
}

return this._currentText;
}
get currentTags(){
var tags = [];
if( this._outputStreamTagsDirty ) {
this._currentTags = [];

this._outputStream.forEach(outputObj => {
// var tag = outputObj as Tag;
var tag = outputObj;
if (tag instanceof Tag) {
this._currentTags.push(tag.text);
}
});

this._outputStreamTagsDirty = false;
}

this._outputStream.forEach(outputObj => {
// var tag = outputObj as Tag;
var tag = outputObj;
if (tag instanceof Tag) {
tags.push(tag.text);
}
});

return tags;
return this._currentTags;
}
get outputStream(){
return this._outputStream;
Expand Down Expand Up @@ -197,7 +221,7 @@ export class StoryState{
var obj = {};

var choiceThreads = null;
this.currentChoices.forEach(c => {
this._currentChoices.forEach(c => {
c.originalChoicePath = c.choicePoint.path.componentsString;
c.originalThreadIndex = c.threadAtGeneration.threadIndex;

Expand All @@ -220,7 +244,7 @@ export class StoryState{

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

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

if( this.divertedTargetObject != null )
obj["currentDivertTarget"] = this.divertedTargetObject.path.componentsString;
Expand Down Expand Up @@ -254,6 +278,7 @@ export class StoryState{
this._evaluationStack = JsonSerialisation.JArrayToRuntimeObjList(jObject["evalStack"]);

this._outputStream = JsonSerialisation.JArrayToRuntimeObjList(jObject["outputStream"]);
this.OutputStreamDirty();

// currentChoices = Json.JArrayToRuntimeObjList<Choice>((JArray)jObject ["currentChoices"]);
this._currentChoices = JsonSerialisation.JArrayToRuntimeObjList(jObject["currentChoices"]);
Expand All @@ -272,7 +297,7 @@ export class StoryState{
// var jChoiceThreads = jObject["choiceThreads"] as JObject;
var jChoiceThreads = jObject["choiceThreads"];

this.currentChoices.forEach(c => {
this._currentChoices.forEach(c => {
c.choicePoint = this.story.ContentAtPath(new Path(c.originalChoicePath));

var foundActiveThread = this.callStack.ThreadWithIndex(c.originalThreadIndex);
Expand Down Expand Up @@ -309,6 +334,7 @@ export class StoryState{
}
ResetOutput(){
this._outputStream.length = 0;
this.OutputStreamDirty();
}
PushEvaluationStack(obj){
this.evaluationStack.push(obj);
Expand Down Expand Up @@ -344,6 +370,7 @@ export class StoryState{
}

this.PushToOutputStreamIndividual(obj);
this.OutputStreamDirty();
}
TrySplittingHeadTailWhitespace(single){
var str = single.value;
Expand Down Expand Up @@ -464,6 +491,7 @@ export class StoryState{

if (includeInOutput) {
this._outputStream.push(obj);
this.OutputStreamDirty();
}
}
TrimNewlinesFromOutputStream(rightGlueToStopAt){
Expand Down Expand Up @@ -522,6 +550,8 @@ export class StoryState{
}
}
}

this.OutputStreamDirty();
}
TrimFromExistingGlue(){
var i = this.currentGlueIndex;
Expand All @@ -533,6 +563,8 @@ export class StoryState{
else
i++;
}

this.OutputStreamDirty();
}
RemoveExistingGlue(){
for (var i = this._outputStream.length - 1; i >= 0; i--) {
Expand All @@ -543,6 +575,8 @@ export class StoryState{
break;
}
}

this.OutputStreamDirty();
}
ForceEnd(){
while (this.callStack.canPopThread)
Expand All @@ -551,7 +585,7 @@ export class StoryState{
while (this.callStack.canPop)
this.callStack.Pop();

this.currentChoices.length = 0;
this._currentChoices.length = 0;

this.currentContentObject = null;
this.previousContentObject = null;
Expand All @@ -560,7 +594,7 @@ export class StoryState{
}
SetChosenPath(path){
// Changing direction, assume we need to clear current set of choices
this.currentChoices.length = 0;
this._currentChoices.length = 0;

this.currentPath = path;

Expand Down Expand Up @@ -647,6 +681,10 @@ export class StoryState{

this._currentErrors.push(message);
}
OutputStreamDirty(){
this._outputStreamTextDirty = true;
this._outputStreamTagsDirty = true;
}
VisitCountAtPathString(pathString){
var visitCountOut;
if (visitCountOut = this.visitCounts[pathString])
Expand All @@ -658,7 +696,9 @@ export class StoryState{
var copy = new StoryState(this.story);

copy.outputStream.push.apply(copy.outputStream, this._outputStream);
copy.currentChoices.push.apply(copy.currentChoices, this.currentChoices);
this.OutputStreamDirty();

copy._currentChoices.push.apply(copy._currentChoices, this._currentChoices);

if (this.hasError) {
copy.currentErrors = [];
Expand Down
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@
"license": "MIT",
"devDependencies": {
"babel-plugin-transform-object-assign": "^6.8.0",
"babel-preset-es2015-rollup": "^1.1.1",
"babel-preset-babili": "0.0.9",
"babel-preset-es2015-rollup": "^3.0.0",
"grunt": "^1.0.1",
"grunt-contrib-watch": "^1.0.0",
"grunt-jasmine-node": "^0.3.1",
"grunt-rollup": "^0.7.1",
"rollup-plugin-babel": "^2.4.0",
"grunt-rollup": "^1.0.1",
"rollup-plugin-babel": "^2.7.1",
"rollup-plugin-uglify": "^1.0.1"
},
"dependencies": {}
Expand Down

0 comments on commit 706b6b9

Please sign in to comment.