Update mcl_time to v2

This commit is contained in:
kay27 2022-02-19 01:54:06 +04:00
parent 9fe692fb6f
commit 398f51b1da
3 changed files with 101 additions and 33 deletions

View File

@ -1,9 +1,10 @@
# mcl_time # mcl_time v2.0
## by kay27 for MineClone 5 ## by kay27 for MineClone 5
--------------------------- ---------------------------
This mod counts time when all players sleep or some area is inactive. This mod counts time when all players sleep or some area is inactive.
It depends very much on `time_speed` configuration variable, which could be changed 'on the fly' by a chat command. It depends very much on `time_speed` configuration variable, which could be changed 'on the fly' by a chat command:
* `/set time_speed 72`
If `time_speed` set to 0, this mod logs warnings and returns zeroes. If `time_speed` set to 0, this mod logs warnings and returns zeroes.
@ -15,13 +16,15 @@ Usually this value grow smoothly. But when you skip the night being in the bed,
### mcl_time.get_number_of_times(last_time, interval, chance) ### mcl_time.get_number_of_times(last_time, interval, chance)
------------------------------------------------------------- -------------------------------------------------------------
Handy to process AMBs. Returns the number of how many times something would probably happen if the area was active and we didn't skip the nights.
You pass `last_time` - last known value of `seconds_irl`, also ABM `interval` and ABM `chance`. Arguments:
* `last_time` - you pass last known for you value of `seconds_irl`
* `interval` and `chance` - interval and chance like from ABM setup
Returns: Returns:
* Integer number of how many times ABM function should be called if the area was active all the time and you didn't skip the night. * Integer number of how many times something would probably happen if the area was active all the time and we didn't skip the nights.
* Integer value of realtime (not in-game) seconds since world creation. * Integer value of in-real-life (not in-game) seconds since world creation.
### mcl_time.touch(pos) ### mcl_time.touch(pos)
----------------------- -----------------------
@ -29,24 +32,76 @@ This function 'toches' node at position `pos` by writing `_t` meta variable of `
### mcl_time.get_number_of_times_at_pos(pos, interval, chance) ### mcl_time.get_number_of_times_at_pos(pos, interval, chance)
-------------------------------------------------------------- --------------------------------------------------------------
Much more handy to call from LBM on area load, than `mcl_time.get_number_of_times()`! Returns the number of how many times something would probably happen for node at pos `pos` if the area was active and we didn't skip the nights.
It reads and updates meta variable `_t` from position `pos` and uses it as previous `seconds_irl`, so we don't need to remember it.
It reads meta variable `_t` from position `pos` and uses it as previous `seconds_irl`, which then pass as first argument into `mcl_time.get_number_of_times()`. Argunments:
After calling this, it also 'touches' the node at `pos` by writing `seconds_irl` into meta variable `_t`. * `pos` - node position
* `interval` and `chance` - interval and chance like from ABM setup
Returns: Returns:
* Integer number of how many times ABM function should be called if the area was active all the time and you didn't skip the night. * Integer number of how many times something would happen to the node at position `pos` if the area was active all the time and we didn't skip the nights.
* Integer value of realtime (not in-game) seconds since world creation. * For unclear conditions, like missing meta or zero `time_speed`, this function will return `0`.
*Warning!* This function can return 0. So it's better not to use it for regular ABMs - use `mcl_time.get_number_of_times_at_pos_or_1()` instead.
### mcl_time.get_number_of_times_at_pos_or_1(pos, interval, chance) ### mcl_time.get_number_of_times_at_pos_or_1(pos, interval, chance)
------------------------------------------------------------------- -------------------------------------------------------------------
Much more handy to process ABMs than `mcl_time.get_number_of_times()` and `mcl_time.get_number_of_times_at_pos()`! Returns the number of how many times something would probably happen for node at pos `pos` if the area was active and we didn't skip the nights.
It reads and updates meta variable `_t` from position `pos` and uses it as previous `seconds_irl`, so we don't need to remember it.
It just calls `mcl_time.get_number_of_times_at_pos()` but doesn't return 0, the minimum number it can return is 1, Argunments:
which is the most suitable for regular ABM processing function. * `pos` - node position
* `interval` and `chance` - interval and chance like from ABM setup
Returns: Returns:
* Integer number of how many times ABM function should be called if the area was active all the time and you didn't skip the night. * Integer number of how many times something would happen to the node at position `pos` if the area was active all the time and we didn't skip the nights.
* Integer value of realtime (not in-game) seconds since world creation. * For unclear conditions, like missing meta or zero `time_speed`, this function will return `1`.
### mcl_time.get_number_of_times_at_pos_or_nil(pos, interval, chance)
---------------------------------------------------------------------
Returns the number of how many times something would probably happen for node at pos `pos` if the area was active and we didn't skip the nights.
It reads and updates meta variable `_t` from position `pos` and uses it as previous `seconds_irl`, so we don't need to remember it.
Argunments:
* `pos` - node position
* `interval` and `chance` - interval and chance like from ABM setup
Returns:
* Integer number of how many times something would happen to the node at position `pos` if the area was active all the time and we didn't skip the nights.
* For unclear conditions, like missing meta or zero `time_speed`, this function will return `nil`.
### mcl_time.get_irl_seconds_passed_at_pos(pos)
-----------------------------------------------
Returns the number of how many in-real-life seconds would be passed for the node at position `pos`, if the area was active all the time and we didn't skip the nights.
It uses node meta variable `_t` to calculate this value.
Argunments:
* `pos` - node position
Returns:
* Integer number of how many in-real-life seconds would be passed for the node at position `pos, if the area was active all the time and we didn't skip the nights.
* For unclear conditions, like missing meta or zero `time_speed`, this function will return `0`.
### mcl_time.get_irl_seconds_passed_at_pos_or_1(pos)
----------------------------------------------------
Returns the number of how many in-real-life seconds would be passed for the node at position `pos`, if the area was active all the time and we didn't skip the nights.
It uses node meta variable `_t` to calculate this value.
Argunments:
* `pos` - node position
Returns:
* Integer number of how many in-real-life seconds would be passed for the node at position `pos, if the area was active all the time and we didn't skip the nights.
* For unclear conditions, like missing meta or zero `time_speed`, this function will return `1`.
### mcl_time.get_irl_seconds_passed_at_pos_or_nil(pos)
----------------------------------------------------
Returns the number of how many in-real-life seconds would be passed for the node at position `pos`, if the area was active all the time and we didn't skip the nights.
It uses node meta variable `_t` to calculate this value.
Argunments:
* `pos` - node position
Returns:
* Integer number of how many in-real-life seconds would be passed for the node at position `pos, if the area was active all the time and we didn't skip the nights.
* For unclear conditions, like missing meta or zero `time_speed`, this function will return `nil`.

View File

@ -76,14 +76,14 @@ local function time_runner()
end end
function mcl_time.get_number_of_times(last_time, interval, chance) function mcl_time.get_number_of_times(last_time, interval, chance)
if not last_time then return 0 end if not last_time then return 0, seconds_irl_publicend end
if seconds_irl_public < 2 then return 0 end if seconds_irl_public < 2 then return 0, seconds_irl_public end
if not interval then return 0 end if not interval then return 0, seconds_irl_public end
if not chance then return 0 end if not chance then return 0, seconds_irl_public end
if interval < 1 then return 0 end if interval < 1 then return 0, seconds_irl_public end
if chance < 1 then return 0 end if chance < 1 then return 0, seconds_irl_public end
local number_of_intervals = (seconds_irl_public - last_time) / interval local number_of_intervals = (seconds_irl_public - last_time) / interval
if number_of_intervals < 1 then return 0 end if number_of_intervals < 1 then return 0, seconds_irl_public end
local average_chance = (1 + chance) / 2 local average_chance = (1 + chance) / 2
local number_of_times = math.floor(number_of_intervals / average_chance) local number_of_times = math.floor(number_of_intervals / average_chance)
return number_of_times, seconds_irl_public return number_of_times, seconds_irl_public
@ -98,45 +98,56 @@ end
function mcl_time.get_number_of_times_at_pos(pos, interval, chance) function mcl_time.get_number_of_times_at_pos(pos, interval, chance)
if not pos then return 0 end if not pos then return 0 end
if not time_speed_is_ok then return 0 end
local meta = minetest.get_meta(pos) local meta = minetest.get_meta(pos)
local last_time = meta:get_int(meta_name) local last_time = meta:get_int(meta_name)
local number_of_times = (last_time == 0) and 0 or get_number_of_times(last_time, interval, chance)
meta:set_int(meta_name, seconds_irl_public) meta:set_int(meta_name, seconds_irl_public)
return number_of_times, seconds_irl_public local number_of_times = (last_time == 0) and 0 or get_number_of_times(last_time, interval, chance)
return number_of_times
end end
local get_number_of_times_at_pos = mcl_time.get_number_of_times_at_pos local get_number_of_times_at_pos = mcl_time.get_number_of_times_at_pos
function mcl_time.get_number_of_times_at_pos_or_1(pos, interval, chance) function mcl_time.get_number_of_times_at_pos_or_1(pos, interval, chance)
return math.max(get_number_of_times_at_pos(pos, interval, chance), 1), seconds_irl_public return math.max(get_number_of_times_at_pos(pos, interval, chance), 1)
end
function mcl_time.get_number_of_times_at_pos_or_nil(pos, interval, chance)
local number_of_times_at_pos = get_number_of_times_at_pos(pos, interval, chance)
if number_of_times_at_pos > 0 then
return number_of_times_at_pos
end
end end
function mcl_time.get_irl_seconds_passed_at_pos(pos) function mcl_time.get_irl_seconds_passed_at_pos(pos)
if not pos then return 0 end if not pos then return 0 end
if not time_speed_is_ok then return 0 end
local meta = minetest.get_meta(pos) local meta = minetest.get_meta(pos)
local last_time = meta:get_int(meta_name) local last_time = meta:get_int(meta_name)
local irl_seconds_passed = (last_time == 0) and 0 or (seconds_irl_public - last_time)
meta:set_int(meta_name, seconds_irl_public) meta:set_int(meta_name, seconds_irl_public)
local irl_seconds_passed = (last_time == 0) and 0 or (seconds_irl_public - last_time)
return irl_seconds_passed return irl_seconds_passed
end end
function mcl_time.get_irl_seconds_passed_at_pos_or_1(pos) function mcl_time.get_irl_seconds_passed_at_pos_or_1(pos)
if not pos then return 1 end if not pos then return 1 end
if not time_speed_is_ok then return 1 end
local meta = minetest.get_meta(pos) local meta = minetest.get_meta(pos)
local last_time = meta:get_int(meta_name) local last_time = meta:get_int(meta_name)
local irl_seconds_passed = (last_time == 0) and 1 or (seconds_irl_public - last_time)
meta:set_int(meta_name, seconds_irl_public) meta:set_int(meta_name, seconds_irl_public)
local irl_seconds_passed = (last_time == 0) and 1 or (seconds_irl_public - last_time)
return irl_seconds_passed return irl_seconds_passed
end end
function mcl_time.get_irl_seconds_passed_at_pos_or_nil(pos) function mcl_time.get_irl_seconds_passed_at_pos_or_nil(pos)
if not pos then return end if not pos then return end
if not time_speed_is_ok then return end
local meta = minetest.get_meta(pos) local meta = minetest.get_meta(pos)
local last_time = meta:get_int(meta_name) local last_time = meta:get_int(meta_name)
meta:set_int(meta_name, seconds_irl_public)
if last_time == 0 then return end if last_time == 0 then return end
local delta_time = seconds_irl_public - last_time local delta_time = seconds_irl_public - last_time
if delta_time <= 0 then return end if delta_time <= 0 then return end
meta:set_int(meta_name, seconds_irl_public)
return delta_time return delta_time
end end

View File

@ -26,6 +26,7 @@ minetest.register_node("mcl_nether:nether_wart_0", {
}, },
groups = {dig_immediate=3, not_in_creative_inventory=1,plant=1,attached_node=1,dig_by_water=1,destroy_by_lava_flow=1,dig_by_piston=1}, groups = {dig_immediate=3, not_in_creative_inventory=1,plant=1,attached_node=1,dig_by_water=1,destroy_by_lava_flow=1,dig_by_piston=1},
sounds = mcl_sounds.node_sound_leaves_defaults(), sounds = mcl_sounds.node_sound_leaves_defaults(),
after_place_node = mcl_time.touch,
}) })
minetest.register_node("mcl_nether:nether_wart_1", { minetest.register_node("mcl_nether:nether_wart_1", {
@ -170,7 +171,6 @@ local function grow(pos, node)
minetest.set_node(pos, new_node) minetest.set_node(pos, new_node)
local meta = minetest.get_meta(pos) local meta = minetest.get_meta(pos)
meta:set_string("gametime", tostring(mcl_time:get_seconds_irl())) meta:set_string("gametime", tostring(mcl_time:get_seconds_irl()))
end end
minetest.register_abm({ minetest.register_abm({
@ -186,9 +186,10 @@ minetest.register_abm({
end end
pos.y = pos.y+1 pos.y = pos.y+1
for i = 1, mcl_time.get_number_of_times_at_pos_or_1(pos, interval, chance) do for i = 1, mcl_time.get_number_of_times_at_pos(pos, interval, chance) do
grow(pos, node) grow(pos, node)
end end
mcl_time.touch(pos)
end end
}) })
@ -206,6 +207,7 @@ minetest.register_lbm({
for i = 1, mcl_time.get_number_of_times_at_pos(pos, interval, chance) do for i = 1, mcl_time.get_number_of_times_at_pos(pos, interval, chance) do
grow(pos, node) grow(pos, node)
end end
mcl_time.touch(pos)
end end
}) })