Fix countless bugs, implement chat commands

This commit is contained in:
Till Affeldt
2020-04-14 05:44:46 +02:00
parent fdc457bd09
commit 2090aff6bd
12 changed files with 169 additions and 27 deletions

View File

@ -5,10 +5,6 @@ api.DEFAULT_CYCLE = 0.1 -- for most effect types
api.MEDIUM_CYCKE = 2.0 -- for ressource intensive tasks
api.LONG_CYCLE = 5.0 -- for write operations and skybox changes
climate_mod.weathers = {}
climate_mod.effects = {}
climate_mod.cycles = {}
function api.register_weather(name, conditions, effects)
-- TODO: check and sanitize
climate_mod.weathers[name] = {

89
lib/commands.lua Normal file
View File

@ -0,0 +1,89 @@
local function parse_heat(heat)
local indicator = "°F"
if not climate_mod.settings.fahrenheit then
heat = (heat - 32) * 5 / 9
indicator = "°C"
end
heat = math.floor(heat * 100) / 100
return heat .. indicator
end
minetest.register_privilege("weather", {
description = "Make changes to the current weather",
give_to_singleplayer = false
})
minetest.register_chatcommand("weather", {
description ="Display weather information",
func = function(playername)
local player = minetest.get_player_by_name(playername)
local ppos = player:get_pos()
local weathers = climate_api.environment.get_weather_presets(player)
local effects = climate_api.environment.get_effects(player)
local heat = climate_api.environment.get_heat(ppos)
local humidity = math.floor(climate_api.environment.get_humidity(ppos) * 100) / 100
local msg = ""
if #weathers > 0 then
msg = msg .. "The following weather presets are active for you: "
for _, weather in ipairs(weathers) do
msg = msg .. weather .. ", "
end
msg = msg:sub(1, #msg-2) .. "\n"
else
msg = msg .. "Your sky is clear. No weather presets are currently active.\n"
end
if #effects > 0 then
msg = msg .. "As a result, the following environment effects are applied: "
for _, effect in ipairs(effects) do
msg = msg .. effect .. ", "
end
msg = msg:sub(1, #msg-2) .. "\n"
end
local heat_desc
if heat > 80 then heat_desc = "scorching"
elseif heat > 50 then heat_desc = "pleasant"
else heat_desc = "chilly" end
msg = msg .. "It is a " .. heat_desc .. " " .. parse_heat(heat) .. " right now and "
msg = msg .. "humidity is at " .. humidity .. "%.\n"
minetest.chat_send_player(playername, msg)
end
})
minetest.register_chatcommand("set_heat", {
params = "<heat>",
description = "Override the weather algorithm's base heat",
privs = { weather = true },
func = function(playername, param)
if param == nil or param == "" then
minetest.chat_send_player(playername, "Provide a number to modify the base heat")
return
end
climate_mod.settings.heat = tonumber(param)
minetest.chat_send_player(playername, "Heat changed")
end
})
minetest.register_chatcommand("set_humidity", {
params = "<humidity>",
description = "Override the weather algorithm's base humidity",
privs = { weather = true },
func = function(playername, param)
if param == nil or param == "" then
minetest.chat_send_player(playername, "Provide a number to modify the base humidity")
return
end
climate_mod.settings.humidity = tonumber(param)
minetest.chat_send_player(playername, "Humidity changed")
end
})
minetest.register_chatcommand("weather_settings", {
description = "Print the active Climate API configuration",
privs = { weather = true },
func = function(playername)
minetest.chat_send_player(playername, "Current Settings\n================")
for setting, value in pairs(climate_mod.settings) do
minetest.chat_send_player(playername, dump2(value, setting))
end
end
})

View File

@ -41,4 +41,22 @@ function environment.get_humidity(pos)
return (base + biome * 0.7 + random_base * 0.3) * random
end
function environment.get_weather_presets(player)
local pname = player:get_player_name()
local weathers = climate_mod.current_weather[pname]
if type(weathers) == "nil" then weathers = {} end
return weathers
end
function environment.get_effects(player)
local pname = player:get_player_name()
local effects = {}
for effect, players in pairs(climate_mod.current_effects) do
if type(players[pname]) ~= "nil" then
table.insert(effects, effect)
end
end
return effects
end
return environment

View File

@ -16,12 +16,14 @@ minetest.register_globalstep(function(dtime)
climate_mod.world.update_status(noise_timer)
end
local effects = climate_mod.trigger.get_active_effects()
climate_mod.current_effects = climate_mod.trigger.get_active_effects()
for name, effect in pairs(climate_mod.effects) do
if climate_mod.cycles[name].timespan < climate_mod.cycles[name].timer + dtime then
climate_mod.cycles[name].timer = 0
climate_mod.trigger.call_handlers(name, effects[name])
climate_mod.trigger.call_handlers(name, climate_mod.current_effects[name])
else
climate_mod.cycles[name].timer = climate_mod.cycles[name].timer + dtime
end
end
end)

View File

@ -43,10 +43,19 @@ local function is_weather_active(player, weather_config, env)
end
local function get_weather_effects(player, weather_config, env)
local config = {}
local effects = {}
if type(weather_config.effects) == "function" then
return weather_config.effects(env)
config = weather_config.effects(env)
else
config = weather_config.effects
end
return weather_config.effects
for effect, value in pairs(config) do
if type(climate_mod.effects[effect]) ~= "nil" then
effects[effect] = value
end
end
return effects
end
function trigger.get_active_effects()
@ -56,11 +65,16 @@ function trigger.get_active_effects()
end
local effects = {}
climate_mod.current_weather = {}
for wname, wconfig in pairs(climate_mod.weathers) do
for _, player in ipairs(minetest.get_connected_players()) do
local pname = player:get_player_name()
local env = environments[pname]
if is_weather_active(player, wconfig, env) then
if type(climate_mod.current_weather[pname]) == "nil" then
climate_mod.current_weather[pname] = {}
end
table.insert(climate_mod.current_weather[pname], wname)
local player_effects = get_weather_effects(player, wconfig, env)
for effect, value in pairs(player_effects) do
if type(effects[effect]) == "nil" then