Skip to content

Commit

Permalink
fix: when replaying inputs mirror directions if necessary
Browse files Browse the repository at this point in the history
  • Loading branch information
alanoliveira committed Jul 27, 2024
1 parent d41624a commit ea64a25
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
16 changes: 15 additions & 1 deletion src/command_recorder.zig
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const std = @import("std");
const emu = @import("emulator.zig");
const game = @import("game.zig");
const view = @import("view.zig");
const Command = emu.Command;

Expand All @@ -14,6 +15,7 @@ const Slot = struct {
const MAX_LENGTH = 0x1000;

commands: [MAX_LENGTH]Command = undefined,
is_facing_right: bool = undefined,
size: usize = 0,

fn push(self: *Slot, command: Command) bool {
Expand Down Expand Up @@ -56,10 +58,13 @@ pub fn process() void {
view.drawText(view.Text.new("REC {d}", .{buffer.size}, 0, 50, 0xFFFF0000), .{});
},
.Replay => {
const recorded = buffer.pop() orelse {
var recorded = buffer.pop() orelse {
stopReplay();
return;
};
if (isDummyFacingRigt() != buffer.is_facing_right) {
recorded.direction = recorded.direction.mirror();
}
emu.setCommand(.P2, recorded);
view.drawText(view.Text.new("PLAY {d}", .{buffer.size}, 0, 50, 0xFF00FF00), .{});
},
Expand Down Expand Up @@ -95,6 +100,7 @@ fn prepareRecording() void {

fn startRecording() void {
buffer.size = 0;
buffer.is_facing_right = isDummyFacingRigt();
status = .Recording;
}

Expand Down Expand Up @@ -124,6 +130,14 @@ fn swapCommands() void {
emu.setCommand(.P2, temp);
}

fn isDummyFacingRigt() bool {
return game.getObject(.{ .Constant = .P2 }).?.isFacingRight();
}

fn swapCommandDirection(command: Command) Command {
return command;
}

pub const State = struct {
status: Status,
slot: Slot,
Expand Down
18 changes: 17 additions & 1 deletion src/emulator.zig
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,18 @@ pub const Command = packed struct {
Right = 8,
UpRight = 9,
DownRight = 10,

pub fn mirror(self: Direction) Direction {
const raw_direction: u4 = @intFromEnum(self);
const l = raw_direction >> 2;
const r = raw_direction >> 3;
return @enumFromInt((raw_direction & 0b0011) | (l << 3 | r << 2));
}

test "Direction mirror" {
const t = @import("std").testing;
try t.expectEqual(Direction.UpLeft, Direction.UpRight.mirror());
}
};

direction: Direction = .Neutral,
Expand All @@ -248,7 +260,7 @@ pub const Command = packed struct {
try t.expectEqual(Command{ .a = false, .b = false, .c = false, .d = false, .direction = .Neutral }, Command.fromRaw(0xFF));
try t.expectEqual(Command{ .a = false, .b = false, .c = false, .d = false, .direction = .Up }, Command.fromRaw(0xFE));
try t.expectEqual(Command{ .a = false, .b = false, .c = false, .d = false, .direction = .UpRight }, Command.fromRaw(0xF6));
try t.expectEqual(Command{ .a = true, .b = false, .c = false, .d = false, .direction = .DownLeft }, Command.fromRaw(0xE5));
try t.expectEqual(Command{ .a = true, .b = false, .c = false, .d = false, .direction = .DownRight }, Command.fromRaw(0xE5));
try t.expectEqual(Command{ .a = false, .b = false, .c = true, .d = true, .direction = .Right }, Command.fromRaw(0x37));
}
};
Expand Down Expand Up @@ -319,3 +331,7 @@ test "writeM64KInt" {
writeM64KInt(u32, @ptrCast(ptr + 1), 0xFFFFFFFF);
try t.expectEqual([_]u8{ 0xFF, 0xB2, 0xFF, 0xFF, 0xE5, 0xFF }, data);
}

test {
std.testing.refAllDecls(@This());
}

0 comments on commit ea64a25

Please sign in to comment.