Skip to content

Commit

Permalink
Allow using script values in Actor Move+Set Relative (needs migration)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrismaltby committed Apr 11, 2024
1 parent f690889 commit afa869c
Show file tree
Hide file tree
Showing 4 changed files with 187 additions and 16 deletions.
143 changes: 143 additions & 0 deletions src/lib/compiler/scriptBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ import {
sortFetchOperations,
multiplyScriptValueConst,
addScriptValueConst,
addScriptValueToScriptValue,
} from "shared/lib/scriptValue/helpers";

export type ScriptOutput = string[];
Expand Down Expand Up @@ -2510,6 +2511,80 @@ extern void __mute_mask_${symbol};
this._addNL();
};

actorMoveRelativeByScriptValues = (
actorId: string,
valueX: ScriptValue,
valueY: ScriptValue,
useCollisions: boolean,
moveType: ScriptBuilderMoveType,
units: DistanceUnitType = "tiles"
) => {
const actorRef = this._declareLocal("actor", 4);
const stackPtr = this.stackPtr;
this._addComment("Actor Move Relative");

const [rpnOpsX, fetchOpsX] = precompileScriptValue(
optimiseScriptValue(
multiplyScriptValueConst(
addScriptValueToScriptValue(
{
type: "property",
target: actorId,
property: units === "tiles" ? "xpos" : "pxpos",
},
valueX
),
(units === "tiles" ? 8 : 1) * 16
)
),
"x"
);
const [rpnOpsY, fetchOpsY] = precompileScriptValue(
optimiseScriptValue(
multiplyScriptValueConst(
addScriptValueToScriptValue(
{
type: "property",
target: actorId,
property: units === "tiles" ? "ypos" : "pypos",
},
valueY
),
(units === "tiles" ? 8 : 1) * 16
)
),
"y"
);

const localsLookup = this._performFetchOperations([
...fetchOpsX,
...fetchOpsY,
]);

const rpn = this._rpn();

this._addComment(`-- Calculate coordinate values`);

// X Value
this._performValueRPN(rpn, rpnOpsX, localsLookup);
rpn.refSet(this._localRef(actorRef, 1));

// Y Value
this._performValueRPN(rpn, rpnOpsY, localsLookup);
rpn.refSet(this._localRef(actorRef, 2));

rpn.stop();
this._setConst(
this._localRef(actorRef, 3),
toASMMoveFlags(moveType, useCollisions)
);
this._addComment(`-- Move Actor`);
this.actorSetById(actorId);
this._actorMoveTo(actorRef);
this._assertStackNeutral(stackPtr);
this._addNL();
};

actorMoveCancel = () => {
const actorRef = this._declareLocal("actor", 4);
this._actorMoveCancel(actorRef);
Expand Down Expand Up @@ -2633,6 +2708,74 @@ extern void __mute_mask_${symbol};
this._addNL();
};

actorSetPositionRelativeByScriptValues = (
actorId: string,
valueX: ScriptValue,
valueY: ScriptValue,
units: DistanceUnitType = "tiles"
) => {
const actorRef = this._declareLocal("actor", 4);
const stackPtr = this.stackPtr;
this._addComment("Actor Set Position Relative");

const [rpnOpsX, fetchOpsX] = precompileScriptValue(
optimiseScriptValue(
multiplyScriptValueConst(
addScriptValueToScriptValue(
{
type: "property",
target: actorId,
property: units === "tiles" ? "xpos" : "pxpos",
},
valueX
),
(units === "tiles" ? 8 : 1) * 16
)
),
"x"
);
const [rpnOpsY, fetchOpsY] = precompileScriptValue(
optimiseScriptValue(
multiplyScriptValueConst(
addScriptValueToScriptValue(
{
type: "property",
target: actorId,
property: units === "tiles" ? "ypos" : "pypos",
},
valueY
),
(units === "tiles" ? 8 : 1) * 16
)
),
"y"
);

const localsLookup = this._performFetchOperations([
...fetchOpsX,
...fetchOpsY,
]);

const rpn = this._rpn();

this._addComment(`-- Calculate coordinate values`);

// X Value
this._performValueRPN(rpn, rpnOpsX, localsLookup);
rpn.refSet(this._localRef(actorRef, 1));

// Y Value
this._performValueRPN(rpn, rpnOpsY, localsLookup);
rpn.refSet(this._localRef(actorRef, 2));

rpn.stop();
this._addComment(`-- Position Actor`);
this.actorSetById(actorId);
this._actorSetPosition(actorRef);
this._assertStackNeutral(stackPtr);
this._addNL();
};

actorGetPosition = (
variableX: string,
variableY: string,
Expand Down
21 changes: 14 additions & 7 deletions src/lib/events/eventActorMoveRelative.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,14 @@ const fields = [
key: "x",
label: l10n("FIELD_X"),
description: l10n("FIELD_X_RELATIVE_DESC"),
type: "number",
type: "value",
min: -31,
max: 31,
width: "50%",
defaultValue: 0,
defaultValue: {
type: "number",
value: 0,
},
unitsField: "units",
unitsDefault: "tiles",
unitsAllowed: ["tiles", "pixels"],
Expand All @@ -44,11 +47,14 @@ const fields = [
key: "y",
label: l10n("FIELD_Y"),
description: l10n("FIELD_Y_RELATIVE_DESC"),
type: "number",
type: "value",
min: -31,
max: 31,
width: "50%",
defaultValue: 0,
defaultValue: {
type: "number",
value: 0,
},
unitsField: "units",
unitsDefault: "tiles",
unitsAllowed: ["tiles", "pixels"],
Expand All @@ -64,6 +70,7 @@ const fields = [
defaultValue: "horizontal",
flexBasis: 30,
flexGrow: 0,
alignBottom: true,
},
{
key: "useCollisions",
Expand All @@ -76,9 +83,9 @@ const fields = [
];

const compile = (input, helpers) => {
const { actorSetActive, actorMoveRelative } = helpers;
actorSetActive(input.actorId);
actorMoveRelative(
const { actorMoveRelativeByScriptValues } = helpers;
actorMoveRelativeByScriptValues(
input.actorId,
input.x,
input.y,
input.useCollisions,
Expand Down
24 changes: 17 additions & 7 deletions src/lib/events/eventActorSetPositionRelative.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,14 @@ const fields = [
key: "x",
label: l10n("FIELD_X"),
description: l10n("FIELD_X_RELATIVE_DESC"),
type: "number",
type: "value",
min: -31,
max: 31,
width: "50%",
defaultValue: 0,
defaultValue: {
type: "number",
value: 0,
},
unitsField: "units",
unitsDefault: "tiles",
unitsAllowed: ["tiles", "pixels"],
Expand All @@ -44,11 +47,14 @@ const fields = [
key: "y",
label: l10n("FIELD_Y"),
description: l10n("FIELD_Y_RELATIVE_DESC"),
type: "number",
type: "value",
min: -31,
max: 31,
width: "50%",
defaultValue: 0,
defaultValue: {
type: "number",
value: 0,
},
unitsField: "units",
unitsDefault: "tiles",
unitsAllowed: ["tiles", "pixels"],
Expand All @@ -58,9 +64,13 @@ const fields = [
];

const compile = (input, helpers) => {
const { actorSetActive, actorSetPositionRelative } = helpers;
actorSetActive(input.actorId);
actorSetPositionRelative(input.x, input.y, input.units);
const { actorSetPositionRelativeByScriptValues } = helpers;
actorSetPositionRelativeByScriptValues(
input.actorId,
input.x,
input.y,
input.units
);
};

module.exports = {
Expand Down
15 changes: 13 additions & 2 deletions src/shared/lib/scriptValue/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,9 +199,9 @@ export const precompileScriptValue = (
type: input.type,
});
} else if (input.type === "true") {
rpnOperations.push({type:"number", value: 1})
rpnOperations.push({ type: "number", value: 1 });
} else if (input.type === "false") {
rpnOperations.push({type:"number", value: 0})
rpnOperations.push({ type: "number", value: 0 });
} else {
rpnOperations.push(input);
}
Expand Down Expand Up @@ -252,3 +252,14 @@ export const addScriptValueConst = (
},
};
};

export const addScriptValueToScriptValue = (
valueA: ScriptValue,
valueB: ScriptValue
): ScriptValue => {
return {
type: "add",
valueA: valueA,
valueB: valueB,
};
};

0 comments on commit afa869c

Please sign in to comment.