From 398f51b1da61738c69e0edd1a7cc290840191db4 Mon Sep 17 00:00:00 2001 From: kay27 Date: Sat, 19 Feb 2022 01:54:06 +0400 Subject: [PATCH] Update mcl_time to v2 --- mods/CORE/mcl_time/README.md | 91 +++++++++++++++++++++------ mods/CORE/mcl_time/init.lua | 37 +++++++---- mods/ITEMS/mcl_nether/nether_wart.lua | 6 +- 3 files changed, 101 insertions(+), 33 deletions(-) diff --git a/mods/CORE/mcl_time/README.md b/mods/CORE/mcl_time/README.md index c3a1bb3af..24a4cd1cd 100644 --- a/mods/CORE/mcl_time/README.md +++ b/mods/CORE/mcl_time/README.md @@ -1,9 +1,10 @@ -# mcl_time +# mcl_time v2.0 ## by kay27 for MineClone 5 --------------------------- 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. @@ -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) ------------------------------------------------------------- -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: - * 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 value of realtime (not in-game) seconds since world creation. + * 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 in-real-life (not in-game) seconds since world creation. ### 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) -------------------------------------------------------------- -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()`. -After calling this, it also 'touches' the node at `pos` by writing `seconds_irl` into meta variable `_t`. +Argunments: + * `pos` - node position + * `interval` and `chance` - interval and chance like from ABM setup 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 value of realtime (not in-game) seconds since world creation. - -*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. + * 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 `0`. ### 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, -which is the most suitable for regular ABM processing function. +Argunments: + * `pos` - node position + * `interval` and `chance` - interval and chance like from ABM setup 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 value of realtime (not in-game) seconds since world creation. + * 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 `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`. + diff --git a/mods/CORE/mcl_time/init.lua b/mods/CORE/mcl_time/init.lua index 8e437406b..2d7e93f9e 100644 --- a/mods/CORE/mcl_time/init.lua +++ b/mods/CORE/mcl_time/init.lua @@ -76,14 +76,14 @@ local function time_runner() end function mcl_time.get_number_of_times(last_time, interval, chance) - if not last_time then return 0 end - if seconds_irl_public < 2 then return 0 end - if not interval then return 0 end - if not chance then return 0 end - if interval < 1 then return 0 end - if chance < 1 then return 0 end + if not last_time then return 0, seconds_irl_publicend end + if seconds_irl_public < 2 then return 0, seconds_irl_public end + if not interval then return 0, seconds_irl_public end + if not chance then return 0, seconds_irl_public end + if interval < 1 then return 0, seconds_irl_public end + if chance < 1 then return 0, seconds_irl_public end 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 number_of_times = math.floor(number_of_intervals / average_chance) return number_of_times, seconds_irl_public @@ -98,45 +98,56 @@ end function mcl_time.get_number_of_times_at_pos(pos, interval, chance) if not pos then return 0 end + if not time_speed_is_ok then return 0 end local meta = minetest.get_meta(pos) 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) - 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 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) - 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 function mcl_time.get_irl_seconds_passed_at_pos(pos) if not pos then return 0 end + if not time_speed_is_ok then return 0 end local meta = minetest.get_meta(pos) 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) + local irl_seconds_passed = (last_time == 0) and 0 or (seconds_irl_public - last_time) return irl_seconds_passed end function mcl_time.get_irl_seconds_passed_at_pos_or_1(pos) if not pos then return 1 end + if not time_speed_is_ok then return 1 end local meta = minetest.get_meta(pos) 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) + local irl_seconds_passed = (last_time == 0) and 1 or (seconds_irl_public - last_time) return irl_seconds_passed end function mcl_time.get_irl_seconds_passed_at_pos_or_nil(pos) if not pos then return end + if not time_speed_is_ok then return end local meta = minetest.get_meta(pos) local last_time = meta:get_int(meta_name) + meta:set_int(meta_name, seconds_irl_public) if last_time == 0 then return end local delta_time = seconds_irl_public - last_time if delta_time <= 0 then return end - meta:set_int(meta_name, seconds_irl_public) return delta_time end diff --git a/mods/ITEMS/mcl_nether/nether_wart.lua b/mods/ITEMS/mcl_nether/nether_wart.lua index 0fe1a990a..8e26c529b 100644 --- a/mods/ITEMS/mcl_nether/nether_wart.lua +++ b/mods/ITEMS/mcl_nether/nether_wart.lua @@ -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}, sounds = mcl_sounds.node_sound_leaves_defaults(), + after_place_node = mcl_time.touch, }) minetest.register_node("mcl_nether:nether_wart_1", { @@ -170,7 +171,6 @@ local function grow(pos, node) minetest.set_node(pos, new_node) local meta = minetest.get_meta(pos) meta:set_string("gametime", tostring(mcl_time:get_seconds_irl())) - end minetest.register_abm({ @@ -186,9 +186,10 @@ minetest.register_abm({ end 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) end + mcl_time.touch(pos) end }) @@ -206,6 +207,7 @@ minetest.register_lbm({ for i = 1, mcl_time.get_number_of_times_at_pos(pos, interval, chance) do grow(pos, node) end + mcl_time.touch(pos) end })