Add i18n support, add negative conditions

This commit is contained in:
Till Affeldt 2020-05-29 21:52:45 +02:00
parent 06f337b23e
commit dbce69be3b
7 changed files with 139 additions and 47 deletions

View File

@ -41,3 +41,4 @@ end
climate_api.register_effect(EFFECT_NAME, handle_effect, "tick") climate_api.register_effect(EFFECT_NAME, handle_effect, "tick")
climate_api.register_effect(EFFECT_NAME, remove_effect, "stop") climate_api.register_effect(EFFECT_NAME, remove_effect, "stop")
climate_api.set_effect_cycle(EFFECT_NAME, climate_api.LONG_CYCLE)

View File

@ -43,6 +43,8 @@ climate_mod.settings = {
volume = get_setting_number("volume", 1) volume = get_setting_number("volume", 1)
} }
climate_mod.i18n = minetest.get_translator("climate_api")
-- initialize empty registers -- initialize empty registers
climate_mod.weathers = {} climate_mod.weathers = {}
climate_mod.effects = {} climate_mod.effects = {}

View File

@ -1,3 +1,5 @@
local S = climate_mod.i18n
-- parse heat values into readable format -- parse heat values into readable format
-- also convert to Celsius if configured -- also convert to Celsius if configured
local function parse_heat(heat) local function parse_heat(heat)
@ -13,13 +15,13 @@ end
-- register weather privilege in order to modify the weather status -- register weather privilege in order to modify the weather status
minetest.register_privilege("weather", { minetest.register_privilege("weather", {
description = "Make changes to the current weather", description = S("Make changes to the current weather"),
give_to_singleplayer = false give_to_singleplayer = false
}) })
-- display general information on current weather -- display general information on current weather
minetest.register_chatcommand("weather", { minetest.register_chatcommand("weather", {
description ="Display weather information", description = S("Display weather information"),
func = function(playername) func = function(playername)
local player = minetest.get_player_by_name(playername) local player = minetest.get_player_by_name(playername)
local ppos = player:get_pos() local ppos = player:get_pos()
@ -29,27 +31,26 @@ minetest.register_chatcommand("weather", {
local humidity = math.floor(climate_api.environment.get_humidity(ppos) * 100) / 100 local humidity = math.floor(climate_api.environment.get_humidity(ppos) * 100) / 100
local msg = "" local msg = ""
if #weathers > 0 then if #weathers > 0 then
msg = msg .. "The following weather presets are active for you: " msg = msg .. S("The following weather presets are active for you:") .. " "
for _, weather in ipairs(weathers) do for _, weather in ipairs(weathers) do
msg = msg .. weather .. ", " msg = msg .. weather .. ", "
end end
msg = msg:sub(1, #msg-2) .. "\n" msg = msg:sub(1, #msg-2) .. "\n"
else else
msg = msg .. "Your sky is clear. No weather presets are currently active.\n" msg = msg .. S("Your sky is clear. No weather presets are currently active.") .. "\n"
end end
if #effects > 0 then if #effects > 0 then
msg = msg .. "As a result, the following environment effects are applied: " msg = msg .. S("As a result, the following environment effects are applied:") .. " "
for _, effect in ipairs(effects) do for _, effect in ipairs(effects) do
msg = msg .. effect .. ", " msg = msg .. effect .. ", "
end end
msg = msg:sub(1, #msg-2) .. "\n" msg = msg:sub(1, #msg-2) .. "\n"
end end
local heat_desc local heat_desc
if heat > 80 then heat_desc = "scorching" if heat > 80 then heat_desc = S("scorching")
elseif heat > 50 then heat_desc = "pleasant" elseif heat > 50 then heat_desc = S("pleasant")
else heat_desc = "chilly" end else heat_desc = S("chilly") end
msg = msg .. "It is a " .. heat_desc .. " " .. parse_heat(heat) .. " right now and " msg = msg .. S("It is a @1 @2 right now and humidity is at @3%.", heat_desc, parse_heat(heat), humidity) .. "\n"
msg = msg .. "humidity is at " .. humidity .. "%.\n"
minetest.chat_send_player(playername, msg) minetest.chat_send_player(playername, msg)
end end
}) })
@ -57,35 +58,35 @@ minetest.register_chatcommand("weather", {
-- set base heat to increase or decrease global climate temperatures -- set base heat to increase or decrease global climate temperatures
minetest.register_chatcommand("set_base_heat", { minetest.register_chatcommand("set_base_heat", {
params = "<heat>", params = "<heat>",
description = "Override the weather algorithm's base heat", description = S("Override the weather algorithm's base heat"),
privs = { weather = true }, privs = { weather = true },
func = function(playername, param) func = function(playername, param)
if param == nil or param == "" then if param == nil or param == "" then
minetest.chat_send_player(playername, "Provide a number to modify the base heat") minetest.chat_send_player(playername, S("Provide a number to modify the base heat"))
return return
end end
if param == "auto" then param = 0 end if param == "auto" then param = 0 end
climate_mod.settings.heat = tonumber(param) or 0 climate_mod.settings.heat = tonumber(param) or 0
minetest.chat_send_player(playername, "Base heat changed") minetest.chat_send_player(playername, S("Base heat changed"))
end end
}) })
-- override global heat levels with given value -- override global heat levels with given value
minetest.register_chatcommand("set_heat", { minetest.register_chatcommand("set_heat", {
params = "<heat>", params = "<heat>",
description = "Override the weather algorithm's base heat", description = S("Override the weather algorithm's heat"),
privs = { weather = true }, privs = { weather = true },
func = function(playername, param) func = function(playername, param)
if param == nil or param == "" then if param == nil or param == "" then
minetest.chat_send_player(playername, "Provide a number to modify the base heat") minetest.chat_send_player(playername, S("Provide a number to modify the heat"))
return return
end end
if param == "auto" then if param == "auto" then
climate_mod.forced_enviroment.heat = nil climate_mod.forced_enviroment.heat = nil
minetest.chat_send_player(playername, "Heat value reset") minetest.chat_send_player(playername, S("Heat value reset"))
else else
climate_mod.forced_enviroment.heat = tonumber(param) or 0 climate_mod.forced_enviroment.heat = tonumber(param) or 0
minetest.chat_send_player(playername, "Heat value changed") minetest.chat_send_player(playername, S("Heat value changed"))
end end
end end
}) })
@ -93,35 +94,35 @@ minetest.register_chatcommand("set_heat", {
-- set base heat to increase or decrease global climate humidity -- set base heat to increase or decrease global climate humidity
minetest.register_chatcommand("set_base_humidity", { minetest.register_chatcommand("set_base_humidity", {
params = "<humidity>", params = "<humidity>",
description = "Override the weather algorithm's base humidity", description = S("Override the weather algorithm's base humidity"),
privs = { weather = true }, privs = { weather = true },
func = function(playername, param) func = function(playername, param)
if param == "auto" then param = 0 end if param == "auto" then param = 0 end
if param == nil or param == "" then if param == nil or param == "" then
minetest.chat_send_player(playername, "Provide a number to modify the base humidity") minetest.chat_send_player(playername, S("Provide a number to modify the base humidity"))
return return
end end
climate_mod.settings.humidity = tonumber(param) or 0 climate_mod.settings.humidity = tonumber(param) or 0
minetest.chat_send_player(playername, "Base humidity changed") minetest.chat_send_player(playername, S("Base humidity changed"))
end end
}) })
-- override global humidity with given value -- override global humidity with given value
minetest.register_chatcommand("set_humidity", { minetest.register_chatcommand("set_humidity", {
params = "<humidity>", params = "<humidity>",
description = "Override the weather algorithm's base humidity", description = S("Override the weather algorithm's humidity"),
privs = { weather = true }, privs = { weather = true },
func = function(playername, param) func = function(playername, param)
if param == nil or param == "" then if param == nil or param == "" then
minetest.chat_send_player(playername, "Provide a number to modify the base humidity") minetest.chat_send_player(playername, S("Provide a number to modify the humidity"))
return return
end end
if param == "auto" then if param == "auto" then
climate_mod.forced_enviroment.humidity = nil climate_mod.forced_enviroment.humidity = nil
minetest.chat_send_player(playername, "Humidity value reset") minetest.chat_send_player(playername, S("Humidity value reset"))
else else
climate_mod.forced_enviroment.humidity = tonumber(param) or 0 climate_mod.forced_enviroment.humidity = tonumber(param) or 0
minetest.chat_send_player(playername, "Humidity value changed") minetest.chat_send_player(playername, S("Humidity value changed"))
end end
end end
}) })
@ -129,11 +130,11 @@ minetest.register_chatcommand("set_humidity", {
-- override wind direction and speed with given values -- override wind direction and speed with given values
minetest.register_chatcommand("set_wind", { minetest.register_chatcommand("set_wind", {
params = "<wind>", params = "<wind>",
description = "Override the weather algorithm's windspeed", description = S("Override the weather algorithm's windspeed"),
privs = { weather = true }, privs = { weather = true },
func = function(playername, param) func = function(playername, param)
if param == nil or param == "" then if param == nil or param == "" then
minetest.chat_send_player(playername, "Provide a number to modify the base humidity") minetest.chat_send_player(playername, S("Provide a vector of two numbers to modify the wind"))
return return
end end
local arguments = {} local arguments = {}
@ -143,25 +144,25 @@ minetest.register_chatcommand("set_wind", {
local wind_z = tonumber(arguments[2]) local wind_z = tonumber(arguments[2])
if arg1 == "auto" then if arg1 == "auto" then
climate_mod.forced_enviroment.wind = nil climate_mod.forced_enviroment.wind = nil
minetest.chat_send_player(playername, "Wind reset") minetest.chat_send_player(playername, S("Wind reset"))
elseif wind_x == nil or wind_z == nil then elseif wind_x == nil or wind_z == nil then
minetest.chat_send_player(playername, "Invalid wind configuration") minetest.chat_send_player(playername, S("Invalid wind configuration"))
else else
climate_mod.forced_enviroment.wind = vector.new({ climate_mod.forced_enviroment.wind = vector.new({
x = wind_x, x = wind_x,
y = 0, y = 0,
z = wind_z z = wind_z
}) })
minetest.chat_send_player(playername, "Wind changed") minetest.chat_send_player(playername, S("Wind changed"))
end end
end end
}) })
-- display current mod config -- display current mod config
minetest.register_chatcommand("weather_settings", { minetest.register_chatcommand("weather_settings", {
description = "Print the active Climate API configuration", description = S("Print the active Climate API configuration"),
func = function(playername) func = function(playername)
minetest.chat_send_player(playername, "Current Settings\n================") minetest.chat_send_player(playername, S("Current Settings") .. "\n================")
for setting, value in pairs(climate_mod.settings) do for setting, value in pairs(climate_mod.settings) do
minetest.chat_send_player(playername, dump2(value, setting)) minetest.chat_send_player(playername, dump2(value, setting))
end end
@ -171,14 +172,14 @@ minetest.register_chatcommand("weather_settings", {
-- force a weather preset or disable it -- force a weather preset or disable it
minetest.register_chatcommand("set_weather", { minetest.register_chatcommand("set_weather", {
params ="<weather> <status>", params ="<weather> <status>",
description ="Turn the specified weather preset on or off for all players or reset it to automatic", description = S("Turn the specified weather preset on or off for all players or reset it to automatic"),
privs = { weather = true }, privs = { weather = true },
func = function(playername, param) func = function(playername, param)
local arguments = {} local arguments = {}
for w in param:gmatch("%S+") do table.insert(arguments, w) end for w in param:gmatch("%S+") do table.insert(arguments, w) end
local weather = arguments[1] local weather = arguments[1]
if weather == nil or climate_mod.weathers[weather] == nil then if weather == nil or climate_mod.weathers[weather] == nil then
minetest.chat_send_player(playername, "Unknown weather preset") minetest.chat_send_player(playername, S("Unknown weather preset"))
return return
end end
local status local status
@ -192,19 +193,19 @@ minetest.register_chatcommand("set_weather", {
elseif arguments[2] == "auto" then elseif arguments[2] == "auto" then
status = nil status = nil
else else
minetest.chat_send_player(playername, "Invalid weather status. Set the preset to either on, off or auto.") minetest.chat_send_player(playername, S("Invalid weather status. Set the preset to either on, off or auto."))
return return
end end
climate_mod.forced_weather[weather] = status climate_mod.forced_weather[weather] = status
minetest.chat_send_player(playername, "Weather " .. weather .. " successfully set to " .. arguments[2]) minetest.chat_send_player(playername, S("Weather @1 successfully set to @2"), weather, arguments[2])
end end
}) })
-- list all weather presets and whether they have been forced or disabled -- list all weather presets and whether they have been forced or disabled
minetest.register_chatcommand("weather_status", { minetest.register_chatcommand("weather_status", {
description = "Prints which weather presets are enforced or disabled", description = S("Prints which weather presets are enforced or disabled"),
func = function(playername) func = function(playername)
minetest.chat_send_player(playername, "Current activation rules:\n================") minetest.chat_send_player(playername, S("Current activation rules:") .. "\n================")
for weather, _ in pairs(climate_mod.weathers) do for weather, _ in pairs(climate_mod.weathers) do
local status = "auto" local status = "auto"
if climate_mod.forced_weather[weather] == true then if climate_mod.forced_weather[weather] == true then
@ -219,9 +220,9 @@ minetest.register_chatcommand("weather_status", {
-- show all environment influences and their values for the executing player -- show all environment influences and their values for the executing player
minetest.register_chatcommand("weather_influences", { minetest.register_chatcommand("weather_influences", {
description = "Prints which weather influences cause your current weather", description = S("Prints which weather influences cause your current weather"),
func = function(playername) func = function(playername)
minetest.chat_send_player(playername, "Current influences rules:\n================") minetest.chat_send_player(playername, S("Current influences rules:") .. "\n================")
local player = minetest.get_player_by_name(playername) local player = minetest.get_player_by_name(playername)
local influences = climate_mod.trigger.get_player_environment(player) local influences = climate_mod.trigger.get_player_environment(player)
for influence, value in pairs(influences) do for influence, value in pairs(influences) do

View File

@ -50,12 +50,12 @@ end)
climate_api.register_influence("light", function(pos) climate_api.register_influence("light", function(pos)
pos = vector.add(pos, {x = 0, y = 1, z = 0}) pos = vector.add(pos, {x = 0, y = 1, z = 0})
return minetest.env:get_node_light(pos) or 0 return minetest.get_node_light(pos) or 0
end) end)
climate_api.register_influence("daylight", function(pos) climate_api.register_influence("daylight", function(pos)
pos = vector.add(pos, {x = 0, y = 1, z = 0}) pos = vector.add(pos, {x = 0, y = 1, z = 0})
return minetest.env:get_node_light(pos, 0.5) or 0 return minetest.get_node_light(pos, 0.5) or 0
end) end)
climate_api.register_global_influence("time", climate_api.register_global_influence("time",

View File

@ -18,6 +18,7 @@ end
function trigger.get_player_environment(player) function trigger.get_player_environment(player)
local ppos = player:get_pos() local ppos = player:get_pos()
if ppos == nil then return end
local env = trigger.get_position_environment(ppos) local env = trigger.get_position_environment(ppos)
env.player = player env.player = player
return env return env
@ -26,15 +27,24 @@ end
function trigger.test_condition(condition, env, goal) function trigger.test_condition(condition, env, goal)
local value = env[condition:sub(5)] local value = env[condition:sub(5)]
if condition:sub(1, 4) == "min_" then if condition:sub(1, 4) == "min_" then
return type(value) ~= "nil" and goal <= value return value ~= nil and goal <= value
elseif condition:sub(1, 4) == "max_" then elseif condition:sub(1, 4) == "max_" then
return type(value) ~= "nil" and goal > value return value ~= nil and goal > value
elseif condition:sub(1, 4) == "has_" then elseif condition:sub(1, 4) == "has_" then
if type(value) == "nil" then return false end if value == nil then return false end
for _, g in ipairs(goal) do for _, g in ipairs(goal) do
if value == g then return true end if value == g then return true end
end end
return false return false
elseif condition:sub(1, 4) == "not_" then
if value == nil then return true end
if type(goal) ~= "table" then
return value ~= goal
end
for _, g in ipairs(goal) do
if value == g then return false end
end
return true
else else
value = env[condition] value = env[condition]
return type(value) == "nil" or goal == value return type(value) == "nil" or goal == value
@ -66,7 +76,7 @@ local function get_weather_effects(player, weather_config, env)
config = weather_config.effects config = weather_config.effects
end end
for effect, value in pairs(config) do for effect, value in pairs(config) do
if type(climate_mod.effects[effect]) ~= "nil" then if climate_mod.effects[effect] ~= nil then
effects[effect] = value effects[effect] = value
end end
end end
@ -92,7 +102,7 @@ function trigger.get_active_effects()
local env = environments[pname] local env = environments[pname]
if env ~= nil then if env ~= nil then
if is_weather_active(player, wname, env) then if is_weather_active(player, wname, env) then
if type(climate_mod.current_weather[pname]) == "nil" then if climate_mod.current_weather[pname] == nil then
climate_mod.current_weather[pname] = {} climate_mod.current_weather[pname] = {}
end end
table.insert(climate_mod.current_weather[pname], wname) table.insert(climate_mod.current_weather[pname], wname)

39
locale/climate_api.de.tr Normal file
View File

@ -0,0 +1,39 @@
# textdomain:climate_api
Make changes to the current weather=Ändere das aktuelle Wetter
Display weather information=Betrachte Informationen zum Wetter
The following weather presets are active for you:=Die folgenden Wetterklassen sind gerade aktiv
Your sky is clear. No weather presets are currently active.=Der Himmel ist klar. Es sind keine Wetterklassen aktiv.
As a result, the following environment effects are applied:=Deshalb werden die folgenden Umwelt-Effekte dargestellt:
scorching=stechend heiße
pleasant=angenehme
chilly=kühle
It is a @1 @2 right now and humidity is at @3%.=Es sind gerade @1 @2 und die Luftfeuchtigkeit liegt bei @3%.
Override the weather algorithm's base heat=Überschreibe die Standard-Temperatur
Provide a number to modify the base heat=Gebe eine Zahl an, um die Standard-Temperatur anzupassen
Base heat changed=Die Standard-Temperatur wurde geändert
Override the weather algorithm's heat=Überschreibe die tatsächliche Temperatur
Provide a number to modify the heat=Gebe eine Zahl an, um die Temperatur anzupassen
Heat value reset=Die Temperatur wurde zurückgesetzt
Heat value changed=Die Temperatur wurde geändert
Override the weather algorithm's base humidity=Überschreibe die Standard-Luftfeuchtigkeit
Provide a number to modify the base humidity=Gebe eine Zahl an, um die Standard-Temperatur anzupassen
Base humidity changed=Die Standard-Luftfeuchtigkeit wurde geändert
Override the weather algorithm's humidity=Überschreibe die tatsächliche Luftfeuchtigkeit
Provide a number to modify the humidity=Gebe eine Zahl an, um die Luftfeuchtigkeit anzupassen
Humidity value reset=Die Luftfeuchtigkeit wurde zurückgesetzt
Humidity value changed=Die Luftfeuchtigkeit wurde geändert
Override the weather algorithm's windspeed=Überschreibe die Windgeschwindigkeit
Provide a vector of two numbers to modify the wind=Gebe einen Vektor aus zwei Zahlen an, um die Windgeschwindigkeit anzupassen
Wind reset=Der Wind wurde zurückgesetzt
Invalid wind configuration=Fehlerhafte Windkonfiguration
Wind changed=Der Wind wurde geändert
Print the active Climate API configuration=Betrachte die Einstellungen für Climate API
Current Settings=Aktuelle Einstellungen
Turn the specified weather preset on or off for all players or reset it to automatic=Schalte für alle Spieler die angegebene Wetterklasse an, aus oder schalte sie auf automatisch
Unknown weather preset=Unbekannte Wetterklasse
Invalid weather status. Set the preset to either on, off or auto.=Unbekannter Status. Setze die Wetterklasse entweder auf on, off oder auto
Weather @1 successfully set to @2=Wetter @1 wurde erfolgreich auf @2 gesetzt
Prints which weather presets are enforced or disabled=Betrachte, welche Wetterklassen erzwungen oder deaktiviert sind
Current activation rules:=Aktuelle Aktivierungsregeln
Prints which weather influences cause your current weather=Betrachte, welche Umwelteinflüsse das aktuelle Wetter hervorrufen
Current influences rules:=Aktuelle Einflussfaktoren

39
locale/template.txt Normal file
View File

@ -0,0 +1,39 @@
# textdomain:climate_api
Make changes to the current weather=
Display weather information=
The following weather presets are active for you:=
Your sky is clear. No weather presets are currently active.=
As a result, the following environment effects are applied:=
scorching=
pleasant=
chilly=
It is a @1 @2 right now and humidity is at @3%.=
Override the weather algorithm's base heat=
Provide a number to modify the base heat=
Base heat changed=
Override the weather algorithm's heat=
Provide a number to modify the heat=
Heat value reset=
Heat value changed=
Override the weather algorithm's base humidity=
Provide a number to modify the base humidity=
Base humidity changed=
Override the weather algorithm's humidity=
Provide a number to modify the humidity=
Humidity value reset=
Humidity value changed=
Override the weather algorithm's windspeed=
Provide a vector of two numbers to modify the wind=
Wind reset=
Invalid wind configuration=
Wind changed=
Print the active Climate API configuration=
Current Settings=
Turn the specified weather preset on or off for all players or reset it to automatic=
Unknown weather preset=
Invalid weather status. Set the preset to either on, off or auto.=
Weather @1 successfully set to @2=
Prints which weather presets are enforced or disabled=
Current activation rules:=
Prints which weather influences cause your current weather=
Current influences rules:=