Arabic-to-Roman converter redo

-rewrote the Arabic-to-Roman number converter
-moved it to mcl_util
This commit is contained in:
the-real-herowl 2023-10-27 03:19:25 +02:00
parent 5827a7638d
commit d85e582e8c
6 changed files with 34 additions and 37 deletions

@ -1,5 +1,7 @@
mcl_util = {} mcl_util = {}
dofile(minetest.get_modpath(minetest.get_current_modname()).."/roman_numerals.lua")
-- Updates all values in t using values from to*. -- Updates all values in t using values from to*.
function table.update(t, ...) function table.update(t, ...)
for _, to in ipairs {...} do for _, to in ipairs {...} do

@ -0,0 +1,30 @@
local converter = {
{1000, "M"},
{900, "CM"},
{500, "D"},
{400, "CD"},
{100, "C"},
{90, "XC"},
{50, "L"},
{40, "XL"},
{10, "X"},
{9, "IX"},
{5, "V"},
{4, "IV"},
{1, "I"}
}
mcl_util.to_roman = function(number)
local r = ""
local a = number
local i = 1
while a > 0 do
if a >= converter[i][1] then
a = a - converter[i][1]
r = r.. converter[i][2]
else
i = i + 1
end
end
return r
end

@ -56,7 +56,7 @@ end
function mcl_enchanting.get_enchantment_description(enchantment, level) function mcl_enchanting.get_enchantment_description(enchantment, level)
local enchantment_def = mcl_enchanting.enchantments[enchantment] local enchantment_def = mcl_enchanting.enchantments[enchantment]
return enchantment_def.name .. return enchantment_def.name ..
(enchantment_def.max_level == 1 and "" or " " .. mcl_enchanting.roman_numerals.toRoman(level)) (enchantment_def.max_level == 1 and "" or " " .. mcl_util.to_roman(level))
end end
function mcl_enchanting.get_colorized_enchantment_description(enchantment, level) function mcl_enchanting.get_colorized_enchantment_description(enchantment, level)

@ -11,7 +11,6 @@ mcl_enchanting = {
book_animation_steps = {0, 640, 680, 700, 740}, book_animation_steps = {0, 640, 680, 700, 740},
book_animation_loop = {["open"] = true, ["close"] = true}, book_animation_loop = {["open"] = true, ["close"] = true},
book_animation_speed = 40, book_animation_speed = 40,
roman_numerals = dofile(modpath .. "/roman_numerals.lua"), -- https://exercism.io/tracks/lua/exercises/roman-numerals/solutions/73c2fb7521e347209312d115f872fa49
enchantments = {}, enchantments = {},
overlay = "^[colorize:purple:50", overlay = "^[colorize:purple:50",
--overlay = "^[invert:rgb^[multiply:#4df44d:50^[invert:rgb", --overlay = "^[invert:rgb^[multiply:#4df44d:50^[invert:rgb",

@ -1,5 +1,5 @@
name = mcl_enchanting name = mcl_enchanting
description = Enchanting for MineClone2 description = Enchanting for MineClone2
depends = tt, walkover, mcl_sounds, mcl_colors, mcl_experience depends = tt, walkover, mcl_sounds, mcl_colors, mcl_experience, mcl_util
optional_depends = screwdriver optional_depends = screwdriver
author = Fleckenstein author = Fleckenstein

@ -1,34 +0,0 @@
--------------------------------------------------------------------
--! @file
--! @brief Convert from normal numbers to Roman Numerals
---------------------------------------------------------------------
local conversionTable = {
{ number = 1000, symbol = "M" },
{ number = 900, symbol = "CM" },
{ number = 500, symbol = "D" },
{ number = 400, symbol = "CD" },
{ number = 100, symbol = "C" },
{ number = 90, symbol = "XC" },
{ number = 50, symbol = "L" },
{ number = 40, symbol = "XL" },
{ number = 10, symbol = "X" },
{ number = 9, symbol = "IX" },
{ number = 5, symbol = "V" },
{ number = 4, symbol = "IV" },
{ number = 1, symbol = "I" }
}
return{
toRoman = function(number)
local romanNumeral = ""
for _,table in pairs (conversionTable) do
while(number >= table.number) do
romanNumeral = romanNumeral .. table.symbol
number = number - table.number
end
end
return romanNumeral
end
}