From d6bc8300b3f3154f34b9754ce8cbcac6ca90fe99 Mon Sep 17 00:00:00 2001 From: heav Date: Sun, 26 Mar 2023 18:44:39 +0000 Subject: seeded randomness for bag --- game/bag.lua | 27 ++++++++++++++++----------- game/init.lua | 25 ++++++++++++++----------- 2 files changed, 30 insertions(+), 22 deletions(-) diff --git a/game/bag.lua b/game/bag.lua index 76e6308..b9d84cf 100644 --- a/game/bag.lua +++ b/game/bag.lua @@ -3,10 +3,13 @@ local tetrominoes = require "game.tetrominoes" local M = {} M.__index = M -M.new = function() +M.new = function(set, settings) local new = setmetatable({}, M) new.queue = {} new.pool = {} + new.set = set + new.randomly_add = settings.randomly_add or {} + new.rng = love.math.newRandomGenerator(settings.seed) return new end @@ -16,22 +19,24 @@ function M:lookahead(amount) if self.queue[i] then table.insert(res, self.queue[i]) else + for k,v in pairs(self.randomly_add) do + if self.rng:random(1, v.inverse_chance) == 1 then + table.insert(self.queue, k) + table.insert(res, k) + goto bypass + end + end if #self.pool == 0 then - self.pool = { - tetrominoes.i, - tetrominoes.j, - tetrominoes.l, - tetrominoes.o, - tetrominoes.s, - tetrominoes.t, - tetrominoes.z, - } + for _, v in ipairs(self.set) do + table.insert(self.pool, v) + end end - local choice = math.random(1, #self.pool) + local choice = self.rng:random(1, #self.pool) local choice_tetr = self.pool[choice] table.remove(self.pool, choice) table.insert(self.queue, choice_tetr) table.insert(res, choice_tetr) + ::bypass:: end end return res diff --git a/game/init.lua b/game/init.lua index 48f9a15..8da8b8d 100644 --- a/game/init.lua +++ b/game/init.lua @@ -1,12 +1,23 @@ local evloop = require "evloop" local playfield = require "game.playfield" local tetrominoes = require "game.tetrominoes" +local heav_optimal_shapes = require "game.heav_optimal_shapes" local gfx = require "game.gfx" local bag = require "game.bag" local M = {} M.__index = M +local pieces = { + tetrominoes.i, + tetrominoes.j, + tetrominoes.l, + tetrominoes.o, + tetrominoes.s, + tetrominoes.t, + tetrominoes.z, +} + function M.new(params) local new = setmetatable({}, M) new.params = params @@ -15,7 +26,9 @@ function M.new(params) new.can_hold = true new.gfx = gfx.new(new) new.gravity_delay = 0.5 - new.bag = bag.new() -- TODO: bag should be seeded + new.bag = bag.new(pieces, {seed = 324, randomly_add = { + [heav_optimal_shapes.heav] = {inverse_chance = 8192}, + }}) return new end @@ -61,16 +74,6 @@ function M:input_loop() return loop end -local pieces = { - tetrominoes.i, - tetrominoes.j, - tetrominoes.l, - tetrominoes.o, - tetrominoes.s, - tetrominoes.t, - tetrominoes.z, -} - function M:next_piece() self.can_hold = true self.piece = self.bag:next_piece():drop(self.field) -- cgit v1.2.3