From 1b46ddb0486276de0b0691e10277d206a0763300 Mon Sep 17 00:00:00 2001 From: Miquel Sabaté Solà Date: Tue, 10 Feb 2026 22:45:20 +0100 Subject: Simplify the random_valid_y_coordinate function MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This function relies on a pre-computed table to get the "random" numbers from, but most of the times this is used, it needs to be between some safe boundaries. Before this commit, this was done inside of the function, correcting the fetched value to be above or below these limits. But there was a bug on the below the grounds limit, in which the 'sbc' instruction could subtract too much from the fetched value and make enemies appear below the sky. Now, this is easy to correct, but since we are cheating with a pre-computed table, I thought it would be vastly easier to just get a random table with the "proper" values. That is, random but within the required boundaries already. Plus, it makes the 'random_valid_y_coordinate' function much faster. This commit adds a ruby script in bin/ which produces a pseudo-random table when called, making sure all the requirements are met. We have to make sure to call this script if we ever change the boundaries at some point. Signed-off-by: Miquel Sabaté Solà --- bin/rand.rb | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 bin/rand.rb (limited to 'bin') diff --git a/bin/rand.rb b/bin/rand.rb new file mode 100644 index 0000000..f627748 --- /dev/null +++ b/bin/rand.rb @@ -0,0 +1,18 @@ +# frozen_string_literal: true + +# See values on background.s +UPPER_MARGIN_Y_COORD = 0x1A +GROUND_Y_COORD = 0xC8 - 32 # NOTE: As in background.s - twice the size of the enemy. + +# The available values for the Y axis for enemies are above ground, below the +# sky, and avoiding the left-most and right-most platforms. +available = (UPPER_MARGIN_Y_COORD..GROUND_Y_COORD).to_a - (0x58..0x69).to_a - (0x40..0x50).to_a + +# With this produce the array containing a randomized sample from the +# 'available' values. +random_byte_array = Array.new(256) { '$%02X' % available.sample } + +# And now print it in the assembler format. +random_byte_array.each_slice(16) do |row| + puts ".byte #{row.join(', ')}" +end -- cgit v1.2.3