Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse stage events #135

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Binary file added slp/FODPlatforms.slp
AlexanderHarrison marked this conversation as resolved.
Show resolved Hide resolved
Binary file not shown.
Binary file added slp/Whispy.slp
Binary file not shown.
Binary file added slp/stadiumTransformations.slp
Binary file not shown.
55 changes: 54 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ export enum Command {
ITEM_UPDATE = 0x3b,
FRAME_BOOKEND = 0x3c,
GECKO_LIST = 0x3d,
FOD_PLATFORM = 0x3f,
WHISPY = 0x40,
STADIUM_TRANSFORMATION = 0x41,
}

export type PlayerType = {
Expand Down Expand Up @@ -274,6 +277,50 @@ export type GeckoCodeType = {
contents: Uint8Array;
};

export enum FODPlatformSide {
AlexanderHarrison marked this conversation as resolved.
Show resolved Hide resolved
RIGHT = 0,
LEFT = 1,
}

export type FODPlatformType = {
AlexanderHarrison marked this conversation as resolved.
Show resolved Hide resolved
frame: number;
platform: FODPlatformSide;
height: number;
};

export enum WhispyBlowDirection {
NONE = 0,
LEFT = 1,
RIGHT = 2,
}

export type WhispyType = {
frame: number;
direction: WhispyBlowDirection;
};

export enum StadiumTransformation {
FIRE = 3,
GRASS = 4,
NORMAL = 5,
ROCK = 6,
WATER = 9,
}

export enum StadiumTransformationEvent {
INITIATE = 2,
ON_MONITOR = 3,
RECEDING = 4,
RISING = 5,
FINISH = 6,
}

export type StadiumTransformationType = {
frame: number;
event: StadiumTransformationEvent;
transformation: StadiumTransformation;
};

export type MetadataType = {
startAt?: string | null;
playedOn?: string | null;
Expand All @@ -300,14 +347,19 @@ export type EventPayloadTypes =
| ItemUpdateType
| FrameBookendType
| GameEndType
| GeckoListType;
| GeckoListType
| FODPlatformType
| WhispyType
| StadiumTransformationType;

export type EventCallbackFunc = (
command: Command,
payload?: EventPayloadTypes | null,
buffer?: Uint8Array | null,
) => boolean;

export type StageEventTypes = FODPlatformType | WhispyType | StadiumTransformationType;

export type FrameEntryType = {
frame: number;
start?: FrameStartType;
Expand All @@ -324,6 +376,7 @@ export type FrameEntryType = {
} | null;
};
items?: ItemUpdateType[];
stageEvents?: StageEventTypes[];
};

export enum Frames {
Expand Down
22 changes: 22 additions & 0 deletions src/utils/slpParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ import type {
ItemUpdateType,
PostFrameUpdateType,
PreFrameUpdateType,
FODPlatformType,
WhispyType,
StadiumTransformationType,
RollbackFrames,
StageEventTypes,
} from "../types";
import { ItemSpawnType } from "../types";
import { Command, Frames, GameMode } from "../types";
Expand Down Expand Up @@ -91,6 +95,15 @@ export class SlpParser extends EventEmitter {
case Command.GECKO_LIST:
this._handleGeckoList(payload as GeckoListType);
break;
case Command.FOD_PLATFORM:
this._handleStageEvent(payload as FODPlatformType);
break;
case Command.WHISPY:
this._handleStageEvent(payload as WhispyType);
break;
case Command.STADIUM_TRANSFORMATION:
this._handleStageEvent(payload as StadiumTransformationType);
break;
}
}

Expand Down Expand Up @@ -293,6 +306,15 @@ export class SlpParser extends EventEmitter {
}
}

private _handleStageEvent(payload: StageEventTypes): void {
const currentFrameNumber = payload.frame!;
const stageEvents = this.frames[currentFrameNumber]?.stageEvents ?? [];
stageEvents.push(payload);

// Set stageEvents with newest
set(this.frames, [currentFrameNumber, "stageEvents"], stageEvents);
}

/**
* Fires off the FINALIZED_FRAME event for frames up until a certain number
* @param num The frame to finalize until
Expand Down
17 changes: 17 additions & 0 deletions src/utils/slpReader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,23 @@ export function parseMessage(command: Command, payload: Uint8Array): EventPayloa
contents: payload.slice(1),
codes: codes,
};
case Command.FOD_PLATFORM:
return {
frame: readInt32(view, 0x1),
platform: readInt8(view, 0x5),
height: readFloat(view, 0x6),
};
case Command.WHISPY:
return {
frame: readInt32(view, 0x1),
direction: readInt8(view, 0x5),
};
case Command.STADIUM_TRANSFORMATION:
return {
frame: readInt32(view, 0x1),
event: readUint16(view, 0x5),
transformation: readUint16(view, 0x7),
};
default:
return null;
}
Expand Down
75 changes: 75 additions & 0 deletions test/stages.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { Frames, SlippiGame, StadiumTransformationEvent, FODPlatformSide, WhispyBlowDirection } from "../src";

describe("when extracting stadium transformation information", () => {
it("should properly increment event ids", () => {
const game = new SlippiGame("slp/stadiumTransformations.slp");
const frames = game.getFrames();

let lastEventId = -1;
let lastTransformationId = -1;
for (let frameNum = Frames.FIRST; frames[frameNum]; frameNum++) {
const frame = frames[frameNum];
if (frame.stageEvents) {
frame.stageEvents.forEach((e) => {
if (e.transformation != lastTransformationId) {
expect(e.event).toBe(StadiumTransformationEvent.INITIATE);
lastTransformationId = e.transformation;
lastEventId = e.event;
} else {
expect(e.event).toBe((lastEventId + 1) % 7);
lastEventId = e.event;
}
});
}
}
});
});

describe("when extracting FOD platform information", () => {
it("should properly parse platform height", () => {
const game = new SlippiGame("slp/FODPlatforms.slp");
const frames = game.getFrames();

let prevHeightLeft = 20.0;
let prevHeightRight = 28.0;
for (let frameNum = Frames.FIRST; frames[frameNum]; frameNum++) {
const frame = frames[frameNum];
if (frame.stageEvents) {
frame.stageEvents.forEach((e) => {
if (e.platform == FODPlatformSide.LEFT) {
expect(Math.abs(e.height - prevHeightLeft)).toBeLessThan(0.2);
prevHeightLeft = e.height;
} else {
expect(Math.abs(e.height - prevHeightRight)).toBeLessThan(0.2);
prevHeightRight = e.height;
}
});
}
}
});
});

describe("when extracting Dreamland Whispy information", () => {
it("should properly parse blow directions", () => {
const game = new SlippiGame("slp/Whispy.slp");
const frames = game.getFrames();

let prevBlowDirection = WhispyBlowDirection.NONE;
for (let frameNum = Frames.FIRST; frames[frameNum]; frameNum++) {
const frame = frames[frameNum];
if (frame.stageEvents) {
frame.stageEvents.forEach((e) => {
if (prevBlowDirection == WhispyBlowDirection.LEFT) {
expect(e.direction).toBe(WhispyBlowDirection.NONE);
} else if (prevBlowDirection == WhispyBlowDirection.RIGHT) {
expect(e.direction).toBe(WhispyBlowDirection.NONE);
} else {
expect(e.direction).not.toBe(WhispyBlowDirection.NONE);
}

prevBlowDirection = e.direction;
});
}
}
});
});