Skip to content

Commit

Permalink
Add value support to ReplaceTile events
Browse files Browse the repository at this point in the history
  • Loading branch information
chrismaltby committed Apr 10, 2024
1 parent 369d863 commit 16b735c
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 77 deletions.
73 changes: 67 additions & 6 deletions src/lib/compiler/scriptBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4510,15 +4510,15 @@ extern void __mute_mask_${symbol};
variablesScriptValueOperation = (
setVariable: string,
operation: ScriptBuilderRPNOperation,
value: ScriptValue,
value: ScriptValue
) => {
this._addComment(`Variables ${operation}`);
const [rpnOps, fetchOps] = precompileScriptValue(
optimiseScriptValue(value)
);
const localsLookup = this._performFetchOperations(fetchOps);
const rpn = this._rpn();
rpn.refVariable(setVariable)
rpn.refVariable(setVariable);
this._performValueRPN(rpn, rpnOps, localsLookup);
rpn.operator(operation);
rpn.refSetVariable(setVariable);
Expand Down Expand Up @@ -4960,6 +4960,61 @@ extern void __mute_mask_${symbol};
this._stackPop(1);
};

replaceTileXYScriptValue = (
x: number,
y: number,
tilesetId: string,
tileIndexValue: ScriptValue,
tileSize: "8px" | "16px"
) => {
const { tilesets } = this.options;
const tileset = tilesets.find((t) => t.id === tilesetId) ?? tilesets[0];
if (!tileset) {
return;
}
const tileIndex = this._declareLocal("tile_index", 1, true);

this._addComment(`Replace Tile XY`);
const [rpnOps, fetchOps] = precompileScriptValue(
optimiseScriptValue(tileIndexValue)
);
const localsLookup = this._performFetchOperations(fetchOps);
const rpn = this._rpn();
this._performValueRPN(rpn, rpnOps, localsLookup);
rpn.refSet(tileIndex);
rpn.stop();
if (tileSize === "16px") {
// Top left tile
this._replaceTileXY(x, y, tileset.symbol, tileIndex);
// Top right tile
this._rpn() //
.ref(tileIndex)
.int8(1)
.operator(".ADD")
.refSet(tileIndex)
.stop();
this._replaceTileXY(x + 1, y, tileset.symbol, tileIndex);
// Bottom right tile
this._rpn() //
.ref(tileIndex)
.int8(tileset.width)
.operator(".ADD")
.refSet(tileIndex)
.stop();
this._replaceTileXY(x + 1, y + 1, tileset.symbol, tileIndex);
// Bottom left tile
this._rpn() //
.ref(tileIndex)
.int8(1)
.operator(".SUB")
.refSet(tileIndex)
.stop();
this._replaceTileXY(x, y + 1, tileset.symbol, tileIndex);
} else {
this._replaceTileXY(x, y, tileset.symbol, tileIndex);
}
};

// --------------------------------------------------------------------------
// Music

Expand Down Expand Up @@ -5434,17 +5489,23 @@ extern void __mute_mask_${symbol};
if (rpnOps.length === 1 && rpnOps[0].type === "number") {
this._ifVariableConst(operator, variable, rpnOps[0].value, trueLabel, 0);
} else if (rpnOps.length === 1 && rpnOps[0].type === "direction") {
this._ifVariableCmpVariable(operator, variable, rpnOps[0].value, trueLabel, 0);
this._ifVariableCmpVariable(
operator,
variable,
rpnOps[0].value,
trueLabel,
0
);
} else {
this._addComment(`-- Calculate value`);
const localsLookup = this._performFetchOperations(fetchOps);
const ifValueRef = this._declareLocal("if_value", 3, true);
const ifValueRef = this._declareLocal("if_value", 1, true);
const rpn = this._rpn();
this._performValueRPN(rpn, rpnOps, localsLookup);
rpn.refSet(ifValueRef).stop();
this._ifVariableCmpVariable(operator, variable, ifValueRef, trueLabel, 0);
}

this._compilePath(falsePath);
this._jump(endLabel);
this._label(trueLabel);
Expand All @@ -5462,7 +5523,7 @@ extern void __mute_mask_${symbol};
optimiseScriptValue(value)
);
const localsLookup = this._performFetchOperations(fetchOps);
const ifValueRef = this._declareLocal("if_value", 3, true);
const ifValueRef = this._declareLocal("if_value", 1, true);
this._addComment(`If`);

this._addComment(`-- Calculate value`);
Expand Down
42 changes: 11 additions & 31 deletions src/lib/events/eventReplaceTileXY.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,46 +53,26 @@ const fields = [
key: "tileIndex",
label: l10n("FIELD_TILE"),
description: l10n("FIELD_TILE_DESC"),
type: "union",
types: ["number", "variable", "property"],
defaultType: "number",
type: "value",
min: 0,
defaultValue: {
number: 0,
variable: "LAST_VARIABLE",
property: "$self$:xpos",
type: "number",
value: 0,
},
},
],
},
];

const compile = (input, helpers) => {
const {
replaceTileXY,
replaceTileXYVariable,
localVariableFromUnion,
markLocalsUsed,
} = helpers;
if (input.tileIndex.type === "number") {
replaceTileXY(
input.x,
input.y,
input.tilesetId,
input.tileIndex.value,
input.tileSize
);
} else {
const indexVar = localVariableFromUnion(input.tileIndex);
replaceTileXYVariable(
input.x,
input.y,
input.tilesetId,
indexVar,
input.tileSize
);
markLocalsUsed(indexVar);
}
const { replaceTileXYScriptValue } = helpers;
replaceTileXYScriptValue(
input.x,
input.y,
input.tilesetId,
input.tileIndex,
input.tileSize
);
};

module.exports = {
Expand Down
84 changes: 44 additions & 40 deletions src/lib/events/eventReplaceTileXYSequence.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,30 +53,24 @@ const fields = [
key: "tileIndex",
label: l10n("FIELD_FROM_TILE"),
description: l10n("FIELD_FROM_TILE_DESC"),
type: "union",
types: ["number", "variable", "property"],
defaultType: "number",
type: "value",
min: 0,
width: "50%",
defaultValue: {
number: 0,
variable: "LAST_VARIABLE",
property: "$self$:xpos",
type: "number",
value: 0,
},
},
{
key: "frames",
label: l10n("FIELD_ANIMATION_FRAMES"),
description: l10n("FIELD_ANIMATION_FRAMES_DESC"),
type: "union",
types: ["number", "variable", "property"],
defaultType: "number",
type: "value",
min: 1,
width: "50%",
defaultValue: {
number: 1,
variable: "LAST_VARIABLE",
property: "$self$:xpos",
type: "number",
value: 1,
},
},
],
Expand All @@ -93,49 +87,59 @@ const fields = [
const compile = (input, helpers) => {
const {
replaceTileXYVariable,
localVariableFromUnion,
ifVariableCompare,
ifVariableCompareScriptValue,
variableInc,
variableAdd,
variableSetToUnionValue,
markLocalsUsed,
variableSetToScriptValue,
_declareLocal,
_rpn,
_addComment,
_addNL,
markLocalsUsed,
} = helpers;

const fromVar = localVariableFromUnion(input.tileIndex);
const framesVar = localVariableFromUnion(input.frames);
const toVar = _declareLocal("to_var", 1, true);

// Calculate max frame
_addComment("Calculate max frame");
if (input.tileSize === "16px") {
_rpn() //
.refVariable(fromVar)
.refVariable(framesVar)
.int8(1)
.operator(".SUB")
.int8(2)
.operator(".MUL")
.operator(".ADD")
.refSet(toVar)
.stop();
variableSetToScriptValue(toVar, {
type: "add",
valueA: input.tileIndex,
valueB: {
type: "mul",
valueA: {
type: "sub",
valueA: input.frames,
valueB: {
type: "number",
value: 1,
},
},
valueB: {
type: "number",
value: 2,
},
},
});
} else {
_rpn() //
.refVariable(fromVar)
.refVariable(framesVar)
.int8(1)
.operator(".SUB")
.operator(".ADD")
.refSet(toVar)
.stop();
variableSetToScriptValue(toVar, {
type: "add",
valueA: input.tileIndex,
valueB: {
type: "sub",
valueA: input.frames,
valueB: {
type: "number",
value: 1,
},
},
});
}
_addNL();

ifVariableCompare(input.variable, ".LT", fromVar, () => {
variableSetToUnionValue(input.variable, input.tileIndex);
ifVariableCompareScriptValue(input.variable, ".LT", input.tileIndex, () => {
variableSetToScriptValue(input.variable, input.tileIndex);
});

replaceTileXYVariable(
Expand All @@ -153,10 +157,10 @@ const compile = (input, helpers) => {
}

ifVariableCompare(input.variable, ".GT", toVar, () => {
variableSetToUnionValue(input.variable, input.tileIndex);
variableSetToScriptValue(input.variable, input.tileIndex);
});

markLocalsUsed(fromVar, framesVar, toVar);
markLocalsUsed(toVar);
};

module.exports = {
Expand Down

0 comments on commit 16b735c

Please sign in to comment.