knucklebones/globals.lua

110 lines
2.7 KiB
Lua
Raw Normal View History

2023-08-12 11:36:14 +02:00
include "events"
-- trace = function(msg)
-- end
ALIGN = {
Left = 0,
Center = 1,
Right = 2
}
PALETTE_MAP = 0x3ff0
COLOR_PLAYER = 8
COLOR_ENEMY = 1
dt = 1 / 60
pt = 0
die_spr = {
1, 3, 5, 7, 9, 11
}
--- @class PLACED_DICE
PLACED_DICE = {
player = {
-- Each line represents a column
{ 0, 0, 0 },
{ 0, 0, 0 },
{ 0, 0, 0 },
-- { 1, 2, 3 },
-- { 4, 5, 6 },
-- { 1, 2, 0 },
},
enemy = {
{ 0, 0, 0 },
{ 0, 0, 0 },
{ 0, 0, 0 },
-- { 4, 5, 6 },
-- { 1, 2, 3 },
-- { 4, 5, 0 },
},
---@param self PLACED_DICE
---@param player 'player'|'enemy'
---@param x number
---@param y number
---@param score number
set = function(self, player, x, y, score)
self[player][x][y] = score
end,
---@param self PLACED_DICE
---@param player 'player'|'enemy'
---@param x number
---@param y number
---@return number
get = function(self, player, x, y)
return self[player][x][y]
end,
---returns true if a column has a free slot
---@param self PLACED_DICE
---@param player 'player'|'enemy'
---@param col number
has_free_slot = function(self, player, col)
return table.count(self[player][col], 0) >= 1
end,
---Returns if one of the boards is full
---@param self PLACED_DICE
is_game_over = function(self)
for _, player in ipairs({ "player", "enemy" }) do
local game_over = true
for col = 1, 3 do
for row = 1, 3 do
if self[player][col][row] == 0 then
game_over = false
end
end
end
if game_over then return true end
end
return false
end
}
--- Score values for each column
--- Must be manually updated
SCORES = {
player = { 0, 0, 0 },
enemy = { 0, 0, 0 },
update = function(self)
local players = { "player", "enemy" }
for _, player in ipairs(players) do
for col = 1, 3 do
local dice_col = PLACED_DICE[player][col]
local score = 0
for row = 1, 3 do
local value = dice_col[row]
local combo = table.count(dice_col, value)
local mul = combo == 2 and 1.5 or combo == 3 and 2 or 1
-- A 4-4-4 combo == 4 + 4*2 + 4*3
-- score = score + value * mul
--- A 4-4-4 combo == 4*3 + 4*3 + 4*3
score = score + combo * value
end
self[player][col] = math.floor(score)
end
end
end
}