Skip to content

Commit

Permalink
Replace tile XY events updated to support using variables for coordin…
Browse files Browse the repository at this point in the history
…ate values
  • Loading branch information
chrismaltby committed May 15, 2024
1 parent 44dc3fc commit cf0ec11
Show file tree
Hide file tree
Showing 8 changed files with 350 additions and 71 deletions.
2 changes: 1 addition & 1 deletion appData/templates/blank/project.gbsproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "___PROJECT_NAME___",
"author": "___AUTHOR___",
"_version": "4.0.0",
"_release": "1",
"_release": "3",
"scenes": [],
"backgrounds": [
{
Expand Down
2 changes: 1 addition & 1 deletion appData/templates/gbhtml/project.gbsproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "___PROJECT_NAME___",
"author": "___AUTHOR___",
"_version": "4.0.0",
"_release": "1",
"_release": "3",
"scenes": [
{
"id": "94c18861-b352-4f49-a64d-52f2e3415077",
Expand Down
32 changes: 25 additions & 7 deletions appData/templates/gbs2/project.gbsproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "___PROJECT_NAME___",
"author": "___AUTHOR___",
"_version": "4.0.0",
"_release": "1",
"_release": "3",
"scenes": [
{
"id": "94c18861-b352-4f49-a64d-52f2e3415077",
Expand Down Expand Up @@ -4728,8 +4728,14 @@
{
"command": "EVENT_REPLACE_TILE_XY_SEQUENCE",
"args": {
"x": 22,
"y": 6,
"x": {
"type": "number",
"value": 22
},
"y": {
"type": "number",
"value": 6
},
"tileIndex": {
"type": "number",
"value": 0
Expand All @@ -4746,8 +4752,14 @@
{
"command": "EVENT_REPLACE_TILE_XY_SEQUENCE",
"args": {
"x": 22,
"y": 17,
"x": {
"type": "number",
"value": 22
},
"y": {
"type": "number",
"value": 17
},
"tileIndex": {
"type": "number",
"value": 4
Expand All @@ -4764,8 +4776,14 @@
{
"command": "EVENT_REPLACE_TILE_XY_SEQUENCE",
"args": {
"x": 4,
"y": 11,
"x": {
"type": "number",
"value": 4
},
"y": {
"type": "number",
"value": 11
},
"tileIndex": {
"type": "number",
"value": 0
Expand Down
169 changes: 132 additions & 37 deletions src/lib/compiler/scriptBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2142,7 +2142,11 @@ extern void __mute_mask_${symbol};
);
};

_getTileXY = (addr: ScriptBuilderStackVariable, x: number, y: number) => {
_getTileXY = (
addr: ScriptBuilderStackVariable,
x: ScriptBuilderStackVariable,
y: ScriptBuilderStackVariable
) => {
this._addCmd("VM_GET_TILE_XY", addr, x, y);
};

Expand Down Expand Up @@ -5205,8 +5209,8 @@ extern void __mute_mask_${symbol};
};

replaceTileXYScriptValue = (
x: number,
y: number,
x: ScriptValue,
y: ScriptValue,
tilesetId: string,
tileIndexValue: ScriptValue,
tileSize: "8px" | "16px"
Expand All @@ -5219,43 +5223,134 @@ extern void __mute_mask_${symbol};
const tileIndex = this._declareLocal("tile_index", 1, true);

this._addComment(`Replace Tile XY`);
const [rpnOps, fetchOps] = precompileScriptValue(

const [rpnOpsX, fetchOpsX] = precompileScriptValue(optimiseScriptValue(x));
const [rpnOpsY, fetchOpsY] = precompileScriptValue(optimiseScriptValue(y));
const [rpnOpsTile, fetchOpsTile] = 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);

if (
rpnOpsX.length === 1 &&
rpnOpsX[0].type === "number" &&
rpnOpsY.length === 1 &&
rpnOpsY[0].type === "number"
) {
// Can optimise using constant values for X and Y coordinates
const localsLookup = this._performFetchOperations(fetchOpsTile);
const constX = rpnOpsX[0].value;
const constY = rpnOpsY[0].value;
const rpn = this._rpn();
this._performValueRPN(rpn, rpnOpsTile, localsLookup);
rpn.refSet(tileIndex);
rpn.stop();
if (tileSize === "16px") {
// 16px tiles - Top left tile
this._replaceTileXY(constX, constY, tileset.symbol, tileIndex);
// 16px tiles - Top right tile
this._rpn() //
.ref(tileIndex)
.int8(1)
.operator(".ADD")
.refSet(tileIndex)
.stop();
this._replaceTileXY(constX + 1, constY, tileset.symbol, tileIndex);
// 16px tiles - Bottom right tile
this._rpn() //
.ref(tileIndex)
.int8(tileset.width)
.operator(".ADD")
.refSet(tileIndex)
.stop();
this._replaceTileXY(constX + 1, constY + 1, tileset.symbol, tileIndex);
// 16px tiles - Bottom left tile
this._rpn() //
.ref(tileIndex)
.int8(1)
.operator(".SUB")
.refSet(tileIndex)
.stop();
this._replaceTileXY(constX, constY + 1, tileset.symbol, tileIndex);
} else {
// 8px tiles
this._replaceTileXY(constX, constY, tileset.symbol, tileIndex);
}
} else {
this._replaceTileXY(x, y, tileset.symbol, tileIndex);
// Using RPN for X/Y values
const tileX = this._declareLocal("tile_x", 1, true);
const tileY = this._declareLocal("tile_y", 1, true);
const tileAddr = this._declareLocal("tile_addr", 1, true);

const localsLookup = this._performFetchOperations([
...fetchOpsX,
...fetchOpsY,
...fetchOpsTile,
]);
const rpn = this._rpn();
this._performValueRPN(rpn, rpnOpsX, localsLookup);
rpn.refSet(tileX);
this._performValueRPN(rpn, rpnOpsY, localsLookup);
rpn.refSet(tileY);
this._performValueRPN(rpn, rpnOpsTile, localsLookup);
rpn.refSet(tileIndex);
rpn.stop();

if (tileSize === "16px") {
// 16px tiles - Top left tile
this._getTileXY(tileAddr, tileX, tileY);
this._replaceTile(tileAddr, tileset.symbol, tileIndex, 1);
// 16px tiles - Top right tile
this._rpn() //
// Inc Tile X
.ref(tileX)
.int8(1)
.operator(".ADD")
.refSetVariable(tileX)
// Inc Tile Index
.ref(tileIndex)
.int8(1)
.operator(".ADD")
.refSet(tileIndex)
.stop();
this._getTileXY(tileAddr, tileX, tileY);
this._replaceTile(tileAddr, tileset.symbol, tileIndex, 1);
// 16px tiles - Bottom right tile
this._rpn() //
// Inc Tile Y
.ref(tileY)
.int8(1)
.operator(".ADD")
.refSetVariable(tileY)
// Inc Tile Index
.ref(tileIndex)
.int8(tileset.width)
.operator(".ADD")
.refSet(tileIndex)
.stop();
this._getTileXY(tileAddr, tileX, tileY);
this._replaceTile(tileAddr, tileset.symbol, tileIndex, 1);
// 16px tiles - Bottom left tile
this._rpn() //
// Inc Tile X
.ref(tileX)
.int8(1)
.operator(".SUB")
.refSetVariable(tileX)
// Inc Tile Index
.ref(tileIndex)
.int8(1)
.operator(".SUB")
.refSet(tileIndex)
.stop();
this._getTileXY(tileAddr, tileX, tileY);
this._replaceTile(tileAddr, tileset.symbol, tileIndex, 1);
} else {
// 8px tiles
this._getTileXY(tileAddr, tileX, tileY);
this._replaceTile(tileAddr, tileset.symbol, tileIndex, 1);
}

this.markLocalsUsed(tileIndex, tileAddr, tileX, tileY);
}
};

Expand Down
14 changes: 10 additions & 4 deletions src/lib/events/eventReplaceTileXY.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,27 @@ const fields = [
key: "x",
label: l10n("FIELD_X"),
description: l10n("FIELD_X_DESC"),
type: "number",
type: "value",
min: 0,
max: 255,
width: "50%",
defaultValue: 0,
defaultValue: {
type: "number",
value: 0,
},
},
{
key: "y",
label: l10n("FIELD_Y"),
description: l10n("FIELD_Y_DESC"),
type: "number",
type: "value",
min: 0,
max: 255,
width: "50%",
defaultValue: 0,
defaultValue: {
type: "number",
value: 0,
},
},
],
},
Expand Down
23 changes: 16 additions & 7 deletions src/lib/events/eventReplaceTileXYSequence.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,27 @@ const fields = [
key: "x",
label: l10n("FIELD_X"),
description: l10n("FIELD_X_DESC"),
type: "number",
type: "value",
min: 0,
max: 255,
width: "50%",
defaultValue: 0,
defaultValue: {
type: "number",
value: 0,
},
},
{
key: "y",
label: l10n("FIELD_Y"),
description: l10n("FIELD_Y_DESC"),
type: "number",
type: "value",
min: 0,
max: 255,
width: "50%",
defaultValue: 0,
defaultValue: {
type: "number",
value: 0,
},
},
],
},
Expand Down Expand Up @@ -89,7 +95,7 @@ const fields = [

const compile = (input, helpers) => {
const {
replaceTileXYVariable,
replaceTileXYScriptValue,
ifVariableCompare,
ifVariableCompareScriptValue,
variableInc,
Expand Down Expand Up @@ -145,11 +151,14 @@ const compile = (input, helpers) => {
variableSetToScriptValue(input.variable, input.tileIndex);
});

replaceTileXYVariable(
replaceTileXYScriptValue(
input.x,
input.y,
input.tilesetId,
input.variable,
{
type: "variable",
value: input.variable,
},
input.tileSize
);

Expand Down
Loading

0 comments on commit cf0ec11

Please sign in to comment.