Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion compiler/backend/emit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ export function unlowered(s: Stmt, decls: Map<string, VarDecl>, arrays: Map<stri
case "unitAlive": return "whether a unit is alive";
case "unitSame": return "two units compared";
case "unitFlag": return `a unit's flag ${o.flag as string}`;
case "unitNull": case "unitVar": case "pick": case "unitAt": case "selected": return "a unit";
case "unitNull": case "unitVar": case "pick": case "unitAt": case "selected": case "unitOrderTarget": return "a unit";
case "textLength": return "a text's length";
case "textIndexOf": return "a search in a text";
case "textCode": return "a character's code";
Expand Down
33 changes: 33 additions & 0 deletions compiler/backend/units.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
*/
import type { ArrayDecl, At, BoolExpr, Call, CompareOp, FuncDecl, NumExpr, Program, Stmt, UnitExpr, UnitFilter, UnitFlag, UnitNumField, VarDecl } from "../ir";
import type { ActionRecord, ConditionRecord } from "../../vendor/triggers";
import { GATHER_ORDERS, GATHER_WORKERS } from "../orders";
import { STATEMENT_KINDS } from "./emit";
import { MOUSE_CELLS } from "./input";

Expand Down Expand Up @@ -76,6 +77,7 @@ const FIELDS: Partial<Record<UnitNumField, Bits>> = {
owner: OWNER, type: TYPE, x: X, y: Y, kills: word(0x8f, 1), orderId: ORDER, cooldown: word(0x55, 1), resources: word(0xd0, 2),
stim: word(0x115, 1), ensnare: word(0x116, 1), lockdown: word(0x117, 1), irradiate: word(0x118, 1), stasis: word(0x119, 1), plague: word(0x11a, 1), maelstrom: word(0x124, 1),
buildTime: word(0xac, 2), rank: word(0x8e, 1), matrix: word(0x114, 1), speed: word(0x38, 4),
orderTargetX: word(0x58, 2), orderTargetY: word(0x5a, 2),
};
const HP = 0x08 >> 2, SHIELDS = 0x60 >> 2, ENERGY = word(0xa2, 2), STATUS = 0xdc >> 2, USER_ACTION = word(0x93, 1);
/**
Expand All @@ -90,6 +92,8 @@ const ATTACKED = word(0x87, 1);
const FLAG_BITS: Partial<Record<UnitFlag, number>> = { invincible: 0x04000000, hallucinated: 0x40000000, burrowed: 0x10, cloaked: 0x300, speedUpgrade: 0x10000000, inTransport: 0x40 };
/** The unit an order is aimed at (a pointer, 0 for none), and the defensive matrix's hit points (a word, 256 a point) beside its timer. */
const ORDER_TARGET = 0x5c >> 2, MATRIX_HP = word(0x112, 2), MATRIX_POINTS = 250 * 256;
/** The order's state, beside the order in its dword: what `gather()` puts back to the start. */
const ORDER_STATE = word(0x4e, 1);
/** The most a field written holds, as the simulator stops it: points for hit points and shields, a byte, a word. */
const TOPS: Partial<Record<UnitNumField, number>> = { hp: 0xffffff, shields: 0xffffff, energy: 255, kills: 255, cooldown: 255, resources: 0xffff, stim: 255, ensnare: 255, plague: 255, lockdown: 255, maelstrom: 255, irradiate: 255, stasis: 255, buildTime: 0xffff, rank: 255, matrix: 255 };
/** The Order action's orders. */
Expand Down Expand Up @@ -655,6 +659,17 @@ export function unitValues(program: Program, events?: { sources: Set<EventSource
}
case "pick": return pick(e, pre);
case "selected": return fix(e, pre) as Unit;
case "unitOrderTarget": {
// The order's target is a pointer, 0 for none: read as a unit whose uniqueness byte is taken when read, as `selected` is.
const holder = held(unit(e.unit, pre), pre);
if (isNone(holder)) return NONE;
const p = fresh("aim");
pre.push(dec(p, k(0), 0x3fffffff), ...when(alive(holder), [
...through(of(holder.e, ORDER_TARGET), (h) => [set(p, h(POINTER, 2))]),
...when(cmp(">=", V(p), k(IN_TABLE)), [set(p, bin("-", V(p), k(DEATHS / 4)))], [set(p, k(0))]),
]));
return { e: ranged(V(p), 0, UNIT_TABLE + STRIDE * PLACES), k: null, none: true, killed: true };
}
case "unitAt": {
const ptr = plain(fix(e.ptr, pre) as NumExpr, pre);
fix(e.epd, pre);
Expand Down Expand Up @@ -767,6 +782,7 @@ export function unitValues(program: Program, events?: { sources: Set<EventSource
const v = s.verb;
// (The amount is worked out whether or not there is a unit.)
const amount = v.do === "damage" || v.do === "heal" ? plain(fix(v.amount, pre) as NumExpr, pre) : null;
const target = v.do === "gather" ? held(unit(v.target, pre), pre) : null;
if (isNone(u0)) return;
const u = held(u0, pre);
const e = u.e;
Expand Down Expand Up @@ -825,6 +841,23 @@ export function unitValues(program: Program, events?: { sources: Set<EventSource
);
break;
}
case "gather": {
// The order's target, its state back to the start and the order itself, as the game's right-click leaves them.
if (!target || isNone(target)) break;
const worker = fresh("ty"), aimed = fresh("ty"), order = fresh("order");
const is = (id: string, types: readonly number[]) => any(types.map((type) => cmp("==", ranged(V(id), 0, 0xffff), k(type))));
body.push(dec(worker, k(0), 0xffff), dec(aimed, k(0), 0xffff), dec(order, k(0), 255), ...readInto(worker, e, TYPE));
body.push(...when(all([is(worker, GATHER_WORKERS), alive(target)]), [
...readInto(aimed, target.e, TYPE),
...GATHER_ORDERS.flatMap((g) => when(is(aimed, g.targets), [set(order, k(g.order))])),
]));
body.push(...when(cmp(">=", ranged(V(order), 0, 255), k(1)), [
poke(of(e, ORDER_TARGET), bin("+", bin("*", target.e, k(4)), k(DEATHS))),
poke(of(e, ORDER_STATE.dword), k(0), ORDER_STATE.mask),
poke(of(e, ORDER.dword), bin("*", ranged(V(order), 0, 255), k(256)), ORDER.mask),
]));
break;
}
case "damage": case "heal": {
// Hit points down — at 0 the unit dies — or up to the type's most, in the game's own units (256 to a point).
const now = fresh("hp"), ty = fresh("ty"), top = fresh("top"), step = fresh("step");
Expand Down
14 changes: 10 additions & 4 deletions compiler/check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export function checkProgram(program: Program): { errors: ProgramDiagnostic[]; h

/** Every expression of a statement list, calls' bodies included. */
function expressions(body: Stmt[], visit: (e: NumExpr | BoolExpr) => void, pick?: (u: UnitExpr & { kind: "pick" }) => void) {
const unit = (u: UnitExpr) => { if (u.kind === "call") call(u.call); else if (u.kind === "pick") pick?.(u); else if (u.kind === "unitAt") { expr(u.ptr); expr(u.epd); expr(u.uid); } };
const unit = (u: UnitExpr): void => { if (u.kind === "call") call(u.call); else if (u.kind === "pick") pick?.(u); else if (u.kind === "unitAt") { expr(u.ptr); expr(u.epd); expr(u.uid); } else if (u.kind === "unitOrderTarget") unit(u.unit); };
const any = (e: NumExpr | BoolExpr | UnitExpr | TextExpr) => (isTextExpr(e) ? text(e) : isUnitExpr(e) ? unit(e) : expr(e));
const looks: TextMap = { num: (e) => { expr(e); return e; }, bool: (e) => { expr(e); return e; }, call: (c) => { call(c); return c; } };
const text = (t: TextExpr) => { mapText(t, looks); };
Expand Down Expand Up @@ -76,7 +76,7 @@ function expressions(body: Stmt[], visit: (e: NumExpr | BoolExpr) => void, pick?
case "textLoop": text(s.of); s.body.forEach(stmt); break;
case "unitLoop": s.body.forEach(stmt); break;
case "unitWrite": unit(s.unit); expr(s.value); break;
case "unitDo": unit(s.unit); if (s.verb.do === "damage" || s.verb.do === "heal") expr(s.verb.amount); break;
case "unitDo": unit(s.unit); if (s.verb.do === "damage" || s.verb.do === "heal") expr(s.verb.amount); if (s.verb.do === "gather") unit(s.verb.target); break;
case "select": s.units.forEach(unit); break;
case "tableWrite": any(s.value); break;
case "if": expr(s.cond); s.then.forEach(stmt); s.else?.forEach(stmt); break;
Expand Down Expand Up @@ -280,6 +280,12 @@ const THE_GAME = "(the game)";
let WINDOWS = new Map<string, string>();
const whole = (array: string): string => WINDOWS.get(array) ?? array;

/** The unit variable a unit is read through, and `THE_GAME` when it is the unit another one's order is aimed at. */
function unitReads(u: UnitExpr, into: Set<string>): void {
if (u.kind === "unitVar") into.add(u.id);
else if (u.kind === "unitOrderTarget") { into.add(THE_GAME); unitReads(u.unit, into); }
}

/** The variables an expression reads, by id — and `THE_GAME` when it reads a value of the game or tests a condition. */
function reads(e: NumExpr | BoolExpr, into = new Set<string>()): Set<string> {
// A text reads the variables it is made from; a list of texts the script has never changes.
Expand All @@ -292,8 +298,8 @@ function reads(e: NumExpr | BoolExpr, into = new Set<string>()): Set<string> {
case "read": case "cond": case "tableRead": into.add(THE_GAME); break;
// What the players did is as it was when the frame began: nothing a loop does within the frame changes it.
case "input": break;
case "unitField": case "unitAlive": case "unitFlag": into.add(THE_GAME); if (e.unit.kind === "unitVar") into.add(e.unit.id); break;
case "unitSame": for (const u of [e.left, e.right]) if (u.kind === "unitVar") into.add(u.id); break;
case "unitField": case "unitAlive": case "unitFlag": into.add(THE_GAME); unitReads(e.unit, into); break;
case "unitSame": for (const u of [e.left, e.right]) unitReads(u, into); break;
case "randomInt": reads(e.bound, into); break;
case "unary": case "cast": reads(e.expr, into); break;
case "binary": case "compare": reads(e.left, into); reads(e.right, into); break;
Expand Down
10 changes: 10 additions & 0 deletions compiler/declarations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,11 @@ ${kw}interface Unit {
kills: number;
/** What the unit is doing: \`if (u.orderId == orders.Move)\`. orders.PlayerGuard is standing guard, orders.AttackUnit attacking. */
readonly orderId: Order;
/** The unit the order is aimed at (the one followed, attacked, mined …), or null when it is aimed at a place or at nothing. */
readonly orderTarget: Unit | null;
/** Where the order is aimed, in pixels: where a Move goes, or the target unit's centre. */
readonly orderTargetX: number;
readonly orderTargetY: number;
/** Frames until the unit can attack or cast again, 0 … 255; writing it holds the unit's fire that long (the ground weapon's, the air weapon's and the spells' alike). */
cooldown: number;
/**
Expand Down Expand Up @@ -199,6 +204,11 @@ ${kw}interface Unit {
heal(amount: number | { percent: number }): void;
/** Centre a location on the unit, its size kept: then createUnit(), moveUnit() and the rest can happen where the unit is. */
locate(location: Location): void;
/**
* Send an SCV, Probe or Drone to mine a mineral field or harvest a Refinery, Extractor or Assimilator, as a right-click
* on it does. Anything else, either way round, is left as it is.
*/
gather(target: Unit): void;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"As a right-click on it does" promises more than the lowering does. A right-click goes through the game's command, which refuses some workers; this writes the order, its state and its target into the unit whatever state it is in.

The library only calls it on a worker just out of production, so that use is fine. A map maker can call it on any worker, though. Could you trace two cases: a burrowed Drone, and an SCV inside a Dropship? A right-click does nothing for either. If the written order makes them misbehave, I'd guard them in the lowering (burrowed and inTransport are already among the flags it reads) and add them to the emitter test. If they turn out harmless, say here what it is: the order written to the unit, with those cases named.

}
/** Which units a loop or a pick looks at; a part left out matches all. units.Men, units.Buildings and units.Factories work as a type. */
${kw}interface UnitFilter {
Expand Down
2 changes: 2 additions & 0 deletions compiler/gameSim.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,8 @@ class GameUnit implements SimUnit {
get speedUpgrade() { return this.read("speedUpgrade", () => this.flag(0x10000000)); }
get hasTarget() { return this.read("hasTarget", () => this.m.getUint32(this.at + 0x5c, true) !== 0); }
get inTransport() { return this.read("inTransport", () => this.flag(0x00000040)); }
get orderTargetX() { return this.read("orderTargetX", () => this.m.getUint16(this.at + 0x58, true)); }
get orderTargetY() { return this.read("orderTargetY", () => this.m.getUint16(this.at + 0x5a, true)); }

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

orderTarget is missing here beside the two positions, so a test run in the lifted game reads nothing for u.orderTarget where the game has a unit. The pointer at +0x5c to a slot, as hasTarget reads it, would do.

// Not read from the game yet: the spell timers and whether the unit is under attack.
stim = 0; ensnare = 0; plague = 0; lockdown = 0; maelstrom = 0; irradiate = 0; stasis = 0;
/** The attack-notify timer (+0x87) running: the game raised its under-attack alert for the unit within the last second or so. */
Expand Down
Loading
Loading