diff --git a/res/config.ini b/res/config.ini index 526c6f9..492319c 100644 --- a/res/config.ini +++ b/res/config.ini @@ -104,13 +104,19 @@ colormix_col3 = 0x20000000 # Available inputs: info_line, session, login, password default_input = login -# DOOM animation top color (low intensity flames) -doom_top_color = 0x00FF0000 +# DOOM animation fire height (1 thru 9) +doom_fire_height = 6 -# DOOM animation middle color (medium intensity flames) -doom_middle_color = 0x00FFFF00 +# DOOM animation fire spread (0 thru 4) +doom_fire_spread = 2 -# DOOM animation bottom color (high intensity flames) +# DOOM animation custom top color (low intensity flames) +doom_top_color = 0x009F2707 + +# DOOM animation custom middle color (medium intensity flames) +doom_middle_color = 0x00C78F17 + +# DOOM animation custom bottom color (high intensity flames) doom_bottom_color = 0x00FFFFFF # Error background color id diff --git a/src/animations/Doom.zig b/src/animations/Doom.zig index 41de5ee..76850fa 100644 --- a/src/animations/Doom.zig +++ b/src/animations/Doom.zig @@ -7,21 +7,22 @@ const TerminalBuffer = @import("../tui/TerminalBuffer.zig"); const Doom = @This(); pub const STEPS = 12; +pub const HEIGHT_MAX = 9; +pub const SPREAD_MAX = 4; allocator: Allocator, terminal_buffer: *TerminalBuffer, buffer: []u8, +height: u8, +spread: u8, fire: [STEPS + 1]Cell, -pub fn init(allocator: Allocator, terminal_buffer: *TerminalBuffer, top_color: u32, middle_color: u32, bottom_color: u32) !Doom { +pub fn init(allocator: Allocator, terminal_buffer: *TerminalBuffer, top_color: u32, middle_color: u32, bottom_color: u32, fire_height: u8, fire_spread: u8) !Doom { const buffer = try allocator.alloc(u8, terminal_buffer.width * terminal_buffer.height); initBuffer(buffer, terminal_buffer.width); - return .{ - .allocator = allocator, - .terminal_buffer = terminal_buffer, - .buffer = buffer, - .fire = [_]Cell{ + const levels = + [_]Cell{ Cell.init(' ', TerminalBuffer.Color.DEFAULT, TerminalBuffer.Color.DEFAULT), Cell.init(0x2591, top_color, TerminalBuffer.Color.DEFAULT), Cell.init(0x2592, top_color, TerminalBuffer.Color.DEFAULT), @@ -35,7 +36,15 @@ pub fn init(allocator: Allocator, terminal_buffer: *TerminalBuffer, top_color: u Cell.init(0x2592, bottom_color, middle_color), Cell.init(0x2593, bottom_color, middle_color), Cell.init(0x2588, bottom_color, middle_color), - }, + }; + + return .{ + .allocator = allocator, + .terminal_buffer = terminal_buffer, + .buffer = buffer, + .height = @min(HEIGHT_MAX, fire_height), + .spread = @min(SPREAD_MAX, fire_spread), + .fire = levels, }; } @@ -57,20 +66,35 @@ fn draw(self: *Doom) void { for (0..self.terminal_buffer.width) |x| { // We start from 1 so that we always have the topmost line when spreading fire for (1..self.terminal_buffer.height) |y| { - // Get current cell + // Get index of current cell in fire level buffer const from = y * self.terminal_buffer.width + x; - const cell_index = self.buffer[from]; - // Spread fire - const propagate = self.terminal_buffer.random.int(u1); - const to = from - self.terminal_buffer.width; // Get the line above + // Generate random data for fire propagation + const rand_loss = self.terminal_buffer.random.intRangeAtMost(u8, 0, HEIGHT_MAX); + const rand_spread = self.terminal_buffer.random.intRangeAtMost(u8, 0, self.spread * 2); - self.buffer[to] = if (cell_index > 0) cell_index - propagate else cell_index; + // Select semi-random target cell + const to = from -| self.terminal_buffer.width + self.spread -| rand_spread; + const to_x = to % self.terminal_buffer.width; + const to_y = to / self.terminal_buffer.width; - // Put the cell - const cell = self.fire[cell_index]; - cell.put(x, y); + // Get fire level of current cell + const level_buf_from = self.buffer[from]; + + // Choose new fire level and store in level buffer + const level_buf_to = level_buf_from -| @intFromBool(rand_loss >= self.height); + self.buffer[to] = level_buf_to; + + // Send known fire levels to terminal buffer + const from_cell = self.fire[level_buf_from]; + const to_cell = self.fire[level_buf_to]; + from_cell.put(x, y); + to_cell.put(to_x, to_y); } + + // Draw bottom line (fire source) + const src_cell = self.fire[STEPS]; + src_cell.put(x, self.terminal_buffer.height - 1); } } diff --git a/src/config/Config.zig b/src/config/Config.zig index 874a75e..8b020f0 100644 --- a/src/config/Config.zig +++ b/src/config/Config.zig @@ -29,6 +29,8 @@ colormix_col1: u32 = 0x00FF0000, colormix_col2: u32 = 0x000000FF, colormix_col3: u32 = 0x20000000, default_input: Input = .login, +doom_fire_height: u8 = 6, +doom_fire_spread: u8 = 2, doom_top_color: u32 = 0x00FF0000, doom_middle_color: u32 = 0x00FFFF00, doom_bottom_color: u32 = 0x00FFFFFF, diff --git a/src/main.zig b/src/main.zig index 9e51691..fb42923 100644 --- a/src/main.zig +++ b/src/main.zig @@ -354,7 +354,7 @@ pub fn main() !void { animation = dummy.animation(); }, .doom => { - var doom = try Doom.init(allocator, &buffer, config.doom_top_color, config.doom_middle_color, config.doom_bottom_color); + var doom = try Doom.init(allocator, &buffer, config.doom_top_color, config.doom_middle_color, config.doom_bottom_color, config.doom_fire_height, config.doom_fire_spread); animation = doom.animation(); }, .matrix => {