From 6897bbd602eacf998b60cec436ce4e9d69053ca5 Mon Sep 17 00:00:00 2001 From: Arturas Norkus Date: Sun, 5 Mar 2017 12:07:01 +0200 Subject: [PATCH] init commit --- README.md | 3 +- happy_weather_api.lua | 78 +++++++++++++++++++++++++++++++++++++++++++ init.lua | 2 ++ mod.conf | 1 + 4 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 happy_weather_api.lua create mode 100644 init.lua create mode 100644 mod.conf diff --git a/README.md b/README.md index 2e0acf8..05cd761 100644 --- a/README.md +++ b/README.md @@ -1 +1,2 @@ -# happy_weather_api \ No newline at end of file +# happy_weather_api + diff --git a/happy_weather_api.lua b/happy_weather_api.lua new file mode 100644 index 0000000..48e19a4 --- /dev/null +++ b/happy_weather_api.lua @@ -0,0 +1,78 @@ +-- +-- Happy Weather API + +-- License: MIT + +-- Credits: +-- * xeranas + + +happy_weather = {} + +local registered_weathers = {} +local active_weathers = {} + + +happy_weather.register_weather = function(weather_obj) + table.insert(registered_weathers, weather_obj) +end + +happy_weather.is_weather_active = function(weather_cd) + for k, weather_ in ipairs(active_weathers) do + if weather_.code == weather_cd then + return true + end + end + return false +end + +local add_active_weather = function(weather_obj) + table.insert(active_weathers, weather_obj) +end + +local remove_active_weather = function(weather_cd) + for k, weather_ in ipairs(active_weathers) do + if weather_.code == weather_cd then + table.remove(active_weathers, k) + return + end + end +end + +-- Global step function +-- loop through registered weathers and check which weather is about to start or end +minetest.register_globalstep(function(dtime) + + if #registered_weathers == 0 then + -- no registered weathers, do nothing. + return + end + + -- Loop through registered weathers + for i, weather_ in ipairs(registered_weathers) do + -- Loop through connected players (weathers are attached to players) + for ii, player in ipairs(minetest.get_connected_players()) do + + -- Weaher is active checking if it about to end + if (weather_.active) then + if (weather_.about_to_end(dtime)) then + weather_.clear_up(player) + weather_.active = false + remove_active_weather(weather_.code) + + -- Weather still active updating it + else + weather_.update(dtime, player) + end + + -- Weaher is not active checking if it about to start + else + if (weather_.about_to_start(dtime)) then + weather_.setup(player) + weather_.active = true + add_active_weather(weather_) + end + end + end + end +end) diff --git a/init.lua b/init.lua new file mode 100644 index 0000000..e3868d3 --- /dev/null +++ b/init.lua @@ -0,0 +1,2 @@ +local modpath = minetest.get_modpath("happy_weather_api"); +dofile(modpath.."/happy_weather_api.lua") \ No newline at end of file diff --git a/mod.conf b/mod.conf new file mode 100644 index 0000000..1d5f8ba --- /dev/null +++ b/mod.conf @@ -0,0 +1 @@ +name = happy_weather_api