commit 2e8500ffaac7f7cae19a1c53262d97d49e1a0360 Author: Till Affeldt Date: Sat Apr 11 04:00:56 2020 +0200 Initialize repository diff --git a/LICENSE.md b/LICENSE.md new file mode 100644 index 0000000..0927556 --- /dev/null +++ b/LICENSE.md @@ -0,0 +1,157 @@ +### GNU LESSER GENERAL PUBLIC LICENSE + +Version 3, 29 June 2007 + +Copyright (C) 2007 Free Software Foundation, Inc. + + +Everyone is permitted to copy and distribute verbatim copies of this +license document, but changing it is not allowed. + +This version of the GNU Lesser General Public License incorporates the +terms and conditions of version 3 of the GNU General Public License, +supplemented by the additional permissions listed below. + +#### 0. Additional Definitions. + +As used herein, "this License" refers to version 3 of the GNU Lesser +General Public License, and the "GNU GPL" refers to version 3 of the +GNU General Public License. + +"The Library" refers to a covered work governed by this License, other +than an Application or a Combined Work as defined below. + +An "Application" is any work that makes use of an interface provided +by the Library, but which is not otherwise based on the Library. +Defining a subclass of a class defined by the Library is deemed a mode +of using an interface provided by the Library. + +A "Combined Work" is a work produced by combining or linking an +Application with the Library. The particular version of the Library +with which the Combined Work was made is also called the "Linked +Version". + +The "Minimal Corresponding Source" for a Combined Work means the +Corresponding Source for the Combined Work, excluding any source code +for portions of the Combined Work that, considered in isolation, are +based on the Application, and not on the Linked Version. + +The "Corresponding Application Code" for a Combined Work means the +object code and/or source code for the Application, including any data +and utility programs needed for reproducing the Combined Work from the +Application, but excluding the System Libraries of the Combined Work. + +#### 1. Exception to Section 3 of the GNU GPL. + +You may convey a covered work under sections 3 and 4 of this License +without being bound by section 3 of the GNU GPL. + +#### 2. Conveying Modified Versions. + +If you modify a copy of the Library, and, in your modifications, a +facility refers to a function or data to be supplied by an Application +that uses the facility (other than as an argument passed when the +facility is invoked), then you may convey a copy of the modified +version: + +- a) under this License, provided that you make a good faith effort + to ensure that, in the event an Application does not supply the + function or data, the facility still operates, and performs + whatever part of its purpose remains meaningful, or +- b) under the GNU GPL, with none of the additional permissions of + this License applicable to that copy. + +#### 3. Object Code Incorporating Material from Library Header Files. + +The object code form of an Application may incorporate material from a +header file that is part of the Library. You may convey such object +code under terms of your choice, provided that, if the incorporated +material is not limited to numerical parameters, data structure +layouts and accessors, or small macros, inline functions and templates +(ten or fewer lines in length), you do both of the following: + +- a) Give prominent notice with each copy of the object code that + the Library is used in it and that the Library and its use are + covered by this License. +- b) Accompany the object code with a copy of the GNU GPL and this + license document. + +#### 4. Combined Works. + +You may convey a Combined Work under terms of your choice that, taken +together, effectively do not restrict modification of the portions of +the Library contained in the Combined Work and reverse engineering for +debugging such modifications, if you also do each of the following: + +- a) Give prominent notice with each copy of the Combined Work that + the Library is used in it and that the Library and its use are + covered by this License. +- b) Accompany the Combined Work with a copy of the GNU GPL and this + license document. +- c) For a Combined Work that displays copyright notices during + execution, include the copyright notice for the Library among + these notices, as well as a reference directing the user to the + copies of the GNU GPL and this license document. +- d) Do one of the following: + - 0) Convey the Minimal Corresponding Source under the terms of + this License, and the Corresponding Application Code in a form + suitable for, and under terms that permit, the user to + recombine or relink the Application with a modified version of + the Linked Version to produce a modified Combined Work, in the + manner specified by section 6 of the GNU GPL for conveying + Corresponding Source. + - 1) Use a suitable shared library mechanism for linking with + the Library. A suitable mechanism is one that (a) uses at run + time a copy of the Library already present on the user's + computer system, and (b) will operate properly with a modified + version of the Library that is interface-compatible with the + Linked Version. +- e) Provide Installation Information, but only if you would + otherwise be required to provide such information under section 6 + of the GNU GPL, and only to the extent that such information is + necessary to install and execute a modified version of the + Combined Work produced by recombining or relinking the Application + with a modified version of the Linked Version. (If you use option + 4d0, the Installation Information must accompany the Minimal + Corresponding Source and Corresponding Application Code. If you + use option 4d1, you must provide the Installation Information in + the manner specified by section 6 of the GNU GPL for conveying + Corresponding Source.) + +#### 5. Combined Libraries. + +You may place library facilities that are a work based on the Library +side by side in a single library together with other library +facilities that are not Applications and are not covered by this +License, and convey such a combined library under terms of your +choice, if you do both of the following: + +- a) Accompany the combined library with a copy of the same work + based on the Library, uncombined with any other library + facilities, conveyed under the terms of this License. +- b) Give prominent notice with the combined library that part of it + is a work based on the Library, and explaining where to find the + accompanying uncombined form of the same work. + +#### 6. Revised Versions of the GNU Lesser General Public License. + +The Free Software Foundation may publish revised and/or new versions +of the GNU Lesser General Public License from time to time. Such new +versions will be similar in spirit to the present version, but may +differ in detail to address new problems or concerns. + +Each version is given a distinguishing version number. If the Library +as you received it specifies that a certain numbered version of the +GNU Lesser General Public License "or any later version" applies to +it, you have the option of following the terms and conditions either +of that published version or of any later version published by the +Free Software Foundation. If the Library as you received it does not +specify a version number of the GNU Lesser General Public License, you +may choose any version of the GNU Lesser General Public License ever +published by the Free Software Foundation. + +If the Library as you received it specifies that a proxy can decide +whether future versions of the GNU Lesser General Public License shall +apply, that proxy's public statement of acceptance of any version is +permanent authorization for you to choose that version for the +Library. diff --git a/README.md b/README.md new file mode 100644 index 0000000..4bb00f9 --- /dev/null +++ b/README.md @@ -0,0 +1,38 @@ +# Moon phases +Make your sky change follow a cycle + +__Important__: This mod requires at least Minetest 5.2.0 for the game's new sky API. +Make sure you have the latest version installed or [update your game](https://www.minetest.net/downloads/). + +## Dependencies +This mod has no hard dependencies whatsoever, so you can use it as you will. +However, I do recommend using the [skylayer](https://gitlab.com/rautars/skylayer) mod. +With the Minetest's new sky API it is likely for more mods to change the sky configuration, +possibly resulting in conflict. This utility mod can help circumvent these issues if both mods use it. + +## Commands +This mod comes with two commands to print or change the current moon phase. +- Use ``/moonphase`` to view the currently active phase. +- Use ``/set_moonphase `` to change it. ```` has to be a full number between 1 and 8. + +In order to change the phase, you will need the corresponding privilege. +Use ``/grant moonphase`` to grant it. + +## Mod Integration +Just like the chat commands, this mod provides a LUA api for accessing the moon phase. +It contains a method called ``moon_phases.get_phase()`` that will return a numeric value representing the current moon phase. +You can also set the phase via ``moon_phases.set_phase(phase)`` where ``phase`` is an integer between 1 and 8. + +## Configuration +The mod provides the option to change the length of the moon cycle. +By default, the moon texture will change every four (in-game) nights. +This results in a total cycle of 32 days. + +## LICENSE +All source code is licensed under GNU LESSER GENERAL PUBLIC LICENSE version 3. +You can find a copy of that license in the repository. + +## Media +All included moon textures are resized versions of graphics from *NASA's Scientific Visualization Studio*. +These images are part of the Public Domain. +You can access the entire (high resolution) album on [their website](https://svs.gsfc.nasa.gov/4769#28564). \ No newline at end of file diff --git a/commands.lua b/commands.lua new file mode 100644 index 0000000..467b7bd --- /dev/null +++ b/commands.lua @@ -0,0 +1,31 @@ +minetest.register_privilege("moonphase", { + description = "Change the phase of the moon", + give_to_singleplayer = false +}) + +minetest.register_chatcommand("moonphase", { + description ="Display current moon phase", + func = function(playername, param) + local msg = "Current moon phase: " .. moon_phases.get_phase() + .. "\nRun set_moonphase [1-8] to change this." + minetest.chat_send_player(playername, msg) + end +}) + +minetest.register_chatcommand("set_moonphase", { + params = "", + description = "Set moon phase to given value", + privs = {moonphase = true}, + func = function(playername, param) + if param == nil or param == "" then + minetest.chat_send_player(playername, "Provide a number between 1 and 8") + else + local change = moon_phases.set_phase(param) + if change then + minetest.chat_send_player(playername, "Moon phase changed successfully") + else + minetest.chat_send_player(playername, "Invalid argument. Provide a number between 1 and 8.") + end + end + end +}) \ No newline at end of file diff --git a/datastorage.lua b/datastorage.lua new file mode 100644 index 0000000..6c9298d --- /dev/null +++ b/datastorage.lua @@ -0,0 +1,48 @@ +local mod_datastorage = minetest.get_modpath("datastorage") ~= nil + +local default_state = { + change_time = true, + day = 1, + phase = 1 +} + +local function use_datastorage() + local state = datastorage.get("moon_phases", "moon_state") + for key, val in pairs(default_state) do + if type(state[key]) == "nil" then + state[key] = val + end + end + return state +end + +local storage +local function use_filesystem() + local file_name = minetest.get_worldpath() .. "/moon_phases" + minetest.register_on_shutdown(function() + local file = io.open(file_name, "w") + file:write(minetest.serialize(storage)) + file:close() + end) + + local file = io.open(file_name, "r") + if file ~= nil then + storage = minetest.deserialize(file:read("*a")) + file:close() + if type(storage) == "table" then + return storage + end + end + storage = default_state + return storage +end + +local function get_storage() + if mod_datastorage then + return use_datastorage() + else + return use_filesystem() + end +end + +return get_storage \ No newline at end of file diff --git a/depends.txt b/depends.txt new file mode 100644 index 0000000..5280a97 --- /dev/null +++ b/depends.txt @@ -0,0 +1 @@ +skylayer? \ No newline at end of file diff --git a/description.txt b/description.txt new file mode 100644 index 0000000..a47995f --- /dev/null +++ b/description.txt @@ -0,0 +1,3 @@ +Changes the moon to follow a cycle between eight different phases. +Expect the sky to change every four nights (or configure a custom schedule). +Requires at least Minetest 5.2.0 \ No newline at end of file diff --git a/init.lua b/init.lua new file mode 100644 index 0000000..5a57b5b --- /dev/null +++ b/init.lua @@ -0,0 +1,86 @@ +local mod_skylayer = minetest.get_modpath("skylayer") ~= nil + +local modpath = minetest.get_modpath("moon_phases"); +local state = dofile(modpath .. "/datastorage.lua")() + +local GSCYCLE = 0.5 +moon_phases = {} + +local function get_cycle_config() + local DEFAULT_LENGTH = 4 + local config = minetest.settings:get("moon_phases_cycle") or DEFAULT_LENGTH + config = math.floor(tonumber(config)) + if (not config) or config < 0 then + minetest.log("warning", "[Moon Phases] Invalid cycle configuration") + return DEFAULT_LENGTH + end + return config +end + +local PHASE_LENGTH = get_cycle_config() +if state.day >= PHASE_LENGTH then + state.day = 1 +end + +local function set_texture(player, texture) + local sl = {} + sl.name = "moon_phases:custom" + sl.moon_data = { + visible = true, + texture = texture + } + if mod_skylayer then + skylayer.add_layer(player:get_player_name(), sl) + else + player:set_moon(sl.moon_data) + end +end + +local function update_textures() + for _, player in ipairs(minetest.get_connected_players()) do + set_texture(player, "moon_" .. state.phase .. ".png") + end +end + +local function handle_time_progression() + local time = minetest.get_timeofday() + if time >= 0.5 and state.change_time then + state.day = state.day + 1 + if state.day == PHASE_LENGTH then + state.day = 1 + state.phase = (state.phase % 8) + 1 + state.change_time = false + update_textures() + end + elseif time < 0.5 and not state.change_time then + state.change_time = true + end +end + +function moon_phases.get_phase() + return state.phase +end + +function moon_phases.set_phase(phase) + phase = math.floor(tonumber(phase)) + if (not phase) or phase < 0 or phase > 8 then + return false + end + state.phase = phase + update_textures() + return true +end + +minetest.register_on_joinplayer(function(player) + set_texture(player, "moon_" .. state.phase .. ".png") +end) + +local timer = 0 +minetest.register_globalstep(function(dtime) + timer = timer + dtime + if timer < GSCYCLE then return end + handle_time_progression() + timer = 0 +end) + +dofile(modpath .. "/commands.lua") \ No newline at end of file diff --git a/mod.conf b/mod.conf new file mode 100644 index 0000000..c207165 --- /dev/null +++ b/mod.conf @@ -0,0 +1,4 @@ +name = moon_phases +title = Moon Phases +author = TestificateMods +release = 1 \ No newline at end of file diff --git a/screenshot.png b/screenshot.png new file mode 100644 index 0000000..c9d550a Binary files /dev/null and b/screenshot.png differ diff --git a/settingtypes.txt b/settingtypes.txt new file mode 100644 index 0000000..8a40347 --- /dev/null +++ b/settingtypes.txt @@ -0,0 +1 @@ +moon_phases_cycle (Change moon phase every X days) int 4 \ No newline at end of file diff --git a/textures/moon_1.png b/textures/moon_1.png new file mode 100644 index 0000000..1db16a6 Binary files /dev/null and b/textures/moon_1.png differ diff --git a/textures/moon_2.png b/textures/moon_2.png new file mode 100644 index 0000000..111291f Binary files /dev/null and b/textures/moon_2.png differ diff --git a/textures/moon_3.png b/textures/moon_3.png new file mode 100644 index 0000000..f7cbfc7 Binary files /dev/null and b/textures/moon_3.png differ diff --git a/textures/moon_4.png b/textures/moon_4.png new file mode 100644 index 0000000..ece9d22 Binary files /dev/null and b/textures/moon_4.png differ diff --git a/textures/moon_5.png b/textures/moon_5.png new file mode 100644 index 0000000..3421136 Binary files /dev/null and b/textures/moon_5.png differ diff --git a/textures/moon_6.png b/textures/moon_6.png new file mode 100644 index 0000000..a79b922 Binary files /dev/null and b/textures/moon_6.png differ diff --git a/textures/moon_7.png b/textures/moon_7.png new file mode 100644 index 0000000..3aa984f Binary files /dev/null and b/textures/moon_7.png differ diff --git a/textures/moon_8.png b/textures/moon_8.png new file mode 100644 index 0000000..54b63e1 Binary files /dev/null and b/textures/moon_8.png differ