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 Jun 28, 2017
2 parents d4a2014 + d25e7aa commit a801d9e
Show file tree
Hide file tree
Showing 10 changed files with 65 additions and 26 deletions.
3 changes: 3 additions & 0 deletions engine/CallStack.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,9 @@ export class CallStack{
get elements(){
return this.callStack;
}
get depth(){
return this.elements.length;
}
get currentElement(){
return this.callStack[this.callStack.length - 1];
}
Expand Down
12 changes: 6 additions & 6 deletions engine/Container.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,24 +33,24 @@ export class Container extends InkObject{//also implements INamedContent. Not su
this.AddContent(value);
}
get namedOnlyContent(){
var namedOnlyContent = {};
var namedOnlyContentDict = {};

for (var key in this.namedContent){
namedOnlyContent[key] = this.namedContent[key];
namedOnlyContentDict[key] = this.namedContent[key];
}

this.content.forEach(c => {
// var named = c as INamedContent;
var named = c;
if (named.name && named.hasValidName) {
delete namedOnlyContent[named.name];
delete namedOnlyContentDict[named.name];
}
});

if (namedOnlyContent.length == 0)
namedOnlyContent = null;
if (Object.keys(namedOnlyContentDict).length == 0)
namedOnlyContentDict = null;

return namedOnlyContent;
return namedOnlyContentDict;
}
set namedOnlyContent(value){
var existingNamedOnly = this.namedOnlyContent;
Expand Down
4 changes: 4 additions & 0 deletions engine/ControlCommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ export class ControlCommand extends InkObject{
static TurnsSince(){
return new ControlCommand(CommandType.TurnsSince);
}
static ReadCount(){
return new ControlCommand(CommandType.ReadCount);
}
static Random(){
return new ControlCommand(CommandType.Random);
}
Expand Down Expand Up @@ -102,6 +105,7 @@ var CommandType = {
End: 18,
ListFromInt: 19,
ListRange: 20,
ReadCount: 21
}
CommandType.TOTAL_VALUES = Object.keys(CommandType).length - 1;//-1 because NotSet shoudn't count
ControlCommand.CommandType = CommandType;
24 changes: 18 additions & 6 deletions engine/InkList.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,25 +48,37 @@ export class InkListItem{

//in C#, rawlists are based on dictionnary; the equivalent of a dictionnary in js is Object, but we can't use that or it will conflate dictionnary items and InkList class properties.
//instead InkList-js has a special _values property wich contains the actual "Dictionnary", and a few Dictionnary methods are re-implemented on InkList. This also means directly iterating over the InkList won't work as expected. Maybe we can return a proxy if that's required.
//@TODO: actually we could use a Map for this.
export class InkList {
constructor(otherListOrSingleElement){
constructor(polymorphicArgument, originStory){
this._keys = {};
this._values = {};
this.origins = null;
this._originNames = null;

//polymorphioc constructor
if (otherListOrSingleElement){
if (otherListOrSingleElement instanceof InkList){
var otherList = otherListOrSingleElement;
if (polymorphicArgument){
if (polymorphicArgument instanceof InkList){
var otherList = polymorphicArgument;
otherList.forEach((kv)=>{
this.Add(kv.Key, kv.Value);
});

this._originNames = otherList._originNames;
}
else if (otherListOrSingleElement.hasOwnProperty('Key') && otherListOrSingleElement.hasOwnProperty('Value')){
var singleElement = otherListOrSingleElement;
else if (typeof polymorphicArgument === 'string'){
this.SetInitialOriginName(polymorphicArgument);

var def = null;
if (def = originStory.listDefinitions.TryGetDefinition(polymorphicArgument, def)){
this.origins = [def];
}
else{
throw new Error("InkList origin could not be found in story when constructing new list: " + singleOriginListName);
}
}
else if (polymorphicArgument.hasOwnProperty('Key') && polymorphicArgument.hasOwnProperty('Value')){
var singleElement = polymorphicArgument;
this.Add(singleElement.Key, singleElement.Value);
}
}
Expand Down
5 changes: 3 additions & 2 deletions engine/JsonSerialisation.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,10 +228,10 @@ export class JsonSerialisation{
varAss.isGlobal = isGlobalVar;
return varAss;
}
if (propValue = obj["#"]){
if (obj["#"] !== undefined){
propValue = obj["#"]
return new Tag(propValue.toString());
}

//list value
if (propValue = obj["list"]) {
// var listContent = (Dictionary<string, object>)propValue;
Expand Down Expand Up @@ -615,6 +615,7 @@ _controlCommandNames[ControlCommand.CommandType.EndString] = "/str";
_controlCommandNames[ControlCommand.CommandType.NoOp] = "nop";
_controlCommandNames[ControlCommand.CommandType.ChoiceCount] = "choiceCnt";
_controlCommandNames[ControlCommand.CommandType.TurnsSince] = "turns";
_controlCommandNames[ControlCommand.CommandType.ReadCount] = "readc";
_controlCommandNames[ControlCommand.CommandType.Random] = "rnd";
_controlCommandNames[ControlCommand.CommandType.SeedRandom] = "srnd";
_controlCommandNames[ControlCommand.CommandType.VisitIndex] = "visit";
Expand Down
2 changes: 1 addition & 1 deletion engine/ListDefinition.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export class ListDefinition{
return this._itemNameToValues[itemName] !== undefined;
}
TryGetItemWithValue(val, item){//item was an out
//the original function returns a boolean and has a second parameter called item that is an `out`. Both are needed and we can't just return the item because it'll always be truthy. Instead, we return an object containing the bool an dthe item
//the original function returns a boolean and has a second parameter called item that is an `out`. Both are needed and we can't just return the item because it'll always be truthy. Instead, we return an object containing the bool and the item
for (var key in this._itemNameToValues){
if (this._itemNameToValues[key] == val) {
item = new InkListItem(this.name, key);
Expand Down
8 changes: 5 additions & 3 deletions engine/NativeFunctionCall.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ export class NativeFunctionCall extends InkObject{
// var op = _operationFuncs [ValueType.Int] as BinaryOp<int>;
var op = this._operationFuncs[ValueType.Int];
var result = op(v1.isTruthy ? 1 : 0, v2.isTruthy ? 1 : 0);
return parseInt(result);
return new IntValue(result);
}

// Normal (list • list) operation
Expand Down Expand Up @@ -298,7 +298,6 @@ export class NativeFunctionCall extends InkObject{
this.AddStringBinaryOp(this.NotEquals,(x, y) => {return !(x === y) ? 1 : 0});

this.AddListBinaryOp(this.Add, (x, y) => {return x.Union(y)});
this.AddListBinaryOp(this.And, (x, y) => {return x.Union(y)});
this.AddListBinaryOp(this.Subtract, (x, y) => {return x.Without(y)});
this.AddListBinaryOp(this.Has, (x, y) => {return x.Contains(y) ? 1 : 0});
this.AddListBinaryOp(this.Hasnt, (x, y) => {return x.Contains(y) ? 0 : 1});
Expand All @@ -310,6 +309,9 @@ export class NativeFunctionCall extends InkObject{
this.AddListBinaryOp(this.GreaterThanOrEquals, (x, y) => {return x.GreaterThanOrEquals(y) ? 1 : 0});
this.AddListBinaryOp(this.LessThanOrEquals, (x, y) => {return x.LessThanOrEquals(y) ? 1 : 0});
this.AddListBinaryOp(this.NotEquals, (x, y) => {return !x.Equals(y) ? 1 : 0});

this.AddListBinaryOp (this.And, (x, y) => {return x.Count > 0 && y.Count > 0 ? 1 : 0});
this.AddListBinaryOp (this.Or, (x, y) => {return x.Count > 0 || y.Count > 0 ? 1 : 0});

this.AddListUnaryOp(this.Not, (x) => {return x.Count == 0 ? 1 : 0});

Expand Down Expand Up @@ -406,4 +408,4 @@ NativeFunctionCall.Count = "LIST_COUNT";
NativeFunctionCall.ValueOfList = "LIST_VALUE";
NativeFunctionCall.Invert = "LIST_INVERT";

NativeFunctionCall._nativeFunctions = null;
NativeFunctionCall._nativeFunctions = null;
24 changes: 18 additions & 6 deletions engine/Story.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export class Story extends InkObject{

lists = lists || null;

this.inkVersionCurrent = 16;
this.inkVersionCurrent = 17;
this.inkVersionMinimumCompatible = 16;

this._variableObservers = null;
Expand Down Expand Up @@ -748,21 +748,28 @@ export class Story extends InkObject{
break;

case ControlCommand.CommandType.TurnsSince:
case ControlCommand.CommandType.ReadCount:
var target = this.state.PopEvaluationStack();
if( !(target instanceof DivertTargetValue) ) {
var extraNote = "";
if( target instanceof IntValue )
extraNote = ". Did you accidentally pass a read count ('knot_name') instead of a target ('-> knot_name')?";
this.Error("TURNS_SINCE expected a divert target (knot, stitch, label name), but saw "+target+extraNote);
this.Error("TURNS_SINCE / READ_COUNT expected a divert target (knot, stitch, label name), but saw "+target+extraNote);
break;
}

// var divertTarget = target as DivertTargetValue;
var divertTarget = target;
// var container = ContentAtPath (divertTarget.targetPath) as Container;
var container = this.ContentAtPath(divertTarget.targetPath);
var turnCount = this.TurnsSinceForContainer(container);
this.state.PushEvaluationStack(new IntValue(turnCount));

var eitherCount;
if (evalCommand.commandType == ControlCommand.CommandType.TurnsSince)
eitherCount = this.TurnsSinceForContainer(container);
else
eitherCount = this.VisitCountForContainer(container);

this.state.PushEvaluationStack(new IntValue(eitherCount));
break;

case ControlCommand.CommandType.Random:
Expand Down Expand Up @@ -1297,11 +1304,16 @@ export class Story extends InkObject{
return tags;
}
BuildStringOfHierarchy(){
var sb = new StringBuilder;
var sb = new StringBuilder();

this.mainContentContainer.BuildStringOfHierarchy(sb, 0, this.state.currentContentObject);

return sb.toString();
return sb.toString();
}
BuildStringOfContainer(container){
var sb = new StringBuilder();
container.BuildStringOfHierarchy(sb, 0, this.state.currentContentObject);
return sb.toString();
}
NextContent(){
// Setting previousContentObject is critical for VisitChangedContainersDueToDivert
Expand Down
7 changes: 6 additions & 1 deletion engine/StoryState.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,9 @@ export class StoryState{
set previousContentObject(value){
this.callStack.currentThread.previousContentObject = value;
}
get callstackDepth(){
return this.callStack.depth;
}
get jsonToken(){
var obj = {};

Expand Down Expand Up @@ -734,11 +737,13 @@ export class StoryState{
}

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

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

copy.evaluationStack.push.apply(copy.evaluationStack, this.evaluationStack);
copy._originalEvaluationStackHeight = this._originalEvaluationStackHeight;

if (this.divertedTargetObject != null)
copy.divertedTargetObject = this.divertedTargetObject;
Expand Down Expand Up @@ -773,5 +778,5 @@ export class StoryState{
}
}

StoryState.kInkSaveStateVersion = 6;
StoryState.kInkSaveStateVersion = 7;
StoryState.kMinCompatibleLoadVersion = 6;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "inkjs",
"version": "1.5.2",
"version": "1.6.0",
"description": "A javascript port of inkle's ink scripting language (http://www.inklestudios.com/ink/)",
"main": "dist/ink-es2015.js",
"scripts": {
Expand Down

0 comments on commit a801d9e

Please sign in to comment.