Merge branch 'moon_phases'

This commit is contained in:
Wuzzy 2024-04-17 19:27:29 +02:00
commit 704b1980de
14 changed files with 123 additions and 0 deletions

View File

@ -64,6 +64,7 @@ Mods with documented APIs:
* `rp_localize`: Localize numbers
* `rp_locks`: Get info about lockable nodes
* `rp_mobs`: Add mobs (animals, monsters) (EXPERIMENTAL!)
* `rp_moon`: Get moon phase
* `rp_music`: Add or remove tracks; start/stop/toggle a music player
* `rp_paint`: Add paintable nodes; set/remove paint of node
* `rp_partialblocks`: Register partial blocks (slabs, stairs)

30
mods/rp_moon/API.md Normal file
View File

@ -0,0 +1,30 @@
# `rp_moon` API
## How moon phases work
Internally, the moon phase is directly derivied from
the current game day (`minetest.get_day_count()`) plus
a random offset that is specific to the world seed.
So on day 0, the moon start with any random phase.
The moon phase is read-only.
## Function reference
### `rp_moon.get_moon_phase()`
Returns current moon phase (0-3).
* 0 = Full Moon
* 1 = Waning Half Moon
* 2 = New Moon
* 3 = Waxing Half Moon
### `rp_moon.update_moon()`
Updates the moon for all players.
You should call this function after the the time of day
has been changed in order for the moon phase to be updated
instantly.
It is not neccessary to call this function for any other reason.

90
mods/rp_moon/init.lua Normal file
View File

@ -0,0 +1,90 @@
-- number of moon phases
local MOON_PHASES = 4
-- time in seconds after which to check/update the moon status
local MOON_UPDATE_TIME = 8.0
-- Randomize initial moon phase, based on map seed
local mg_seed = minetest.get_mapgen_setting("seed")
local rand = PseudoRandom(mg_seed)
local phase_offset = rand:next(0, MOON_PHASES - 1)
minetest.log("info", "[rp_moon] Moon phase offset of this world: "..phase_offset)
rp_moon = {}
rp_moon.MOON_PHASES = MOON_PHASES
local moon_textures = {
[0] = "rp_moon_full_moon.png",
[1] = "rp_moon_waning_moon.png",
[2] = "rp_moon_new_moon.png",
[3] = "rp_moon_waxing_moon.png",
}
local function get_moon_texture()
local phase = rp_moon.get_moon_phase()
return moon_textures[phase]
end
-- Updates the moon for a single player
local function update_moon_for_player(player)
player:set_moon({texture = get_moon_texture()})
end
-- Updates the moon for all players
local function update_moon()
local moon_arg = {texture = get_moon_texture()}
local players = minetest.get_connected_players()
for p=1, #players do
players[p]:set_moon(moon_arg)
end
end
local timer = 0
local last_reported_phase = nil
-- Update the moon for all players after some time has passed
minetest.register_globalstep(function(dtime)
timer = timer + dtime
if timer < MOON_UPDATE_TIME then
return
end
timer = 0
local phase = rp_moon.get_moon_phase()
-- No-op when moon phase didn't change yet
if last_reported_phase == phase then
return
end
minetest.log("info", "[rp_moon] New moon phase: "..phase)
last_reported_phase = phase
update_moon()
end)
-- Initialize moon for joining player
minetest.register_on_joinplayer(function(player)
update_moon_for_player(player)
end)
-- Update moon for all players when "time" command was called
minetest.register_on_chatcommand(function(name, command, params)
if command == "time" then
minetest.after(0, function()
update_moon()
end)
end
end)
-- API functions
function rp_moon.get_moon_phase()
local after_midday = 0
-- Moon phase changes after midday
local tod = minetest.get_timeofday()
if tod > 0.5 then
after_midday = 1
end
return (minetest.get_day_count() + phase_offset + after_midday) % MOON_PHASES
end
function rp_moon.update_moon()
update_moon()
end

2
mods/rp_moon/mod.conf Normal file
View File

@ -0,0 +1,2 @@
name = rp_moon
description = Moon phases

View File

Before

Width:  |  Height:  |  Size: 109 B

After

Width:  |  Height:  |  Size: 109 B

View File

Before

Width:  |  Height:  |  Size: 242 B

After

Width:  |  Height:  |  Size: 242 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 234 B

Binary file not shown.

Binary file not shown.

After

Width:  |  Height:  |  Size: 378 B

Binary file not shown.

Binary file not shown.

After

Width:  |  Height:  |  Size: 364 B

Binary file not shown.