Compare commits

...

11 Commits

30 changed files with 318 additions and 132 deletions

View File

@ -44,8 +44,8 @@ minetest.register_on_leaveplayer(function(player)
controls.players[name] = nil
end)
minetest.register_globalstep(function(dtime)
for _, player in pairs(get_connected_players()) do
mcl_util.register_player_globalstep({
execute = function(player, dtime)
local player_name = player:get_player_name()
local player_controls = player:get_player_control()
if controls.players[player_name] then
@ -71,5 +71,5 @@ minetest.register_globalstep(function(dtime)
end
end
end
end
end)
end,
})

View File

@ -1,4 +1,5 @@
name = controls
author = Arcelmi
depends = mcl_util
description = Controls framework by Arcelmi

24
mods/CORE/mcl_util/API.md Normal file
View File

@ -0,0 +1,24 @@
# mcl_util
Provide utility functions for mcl2
## mcl_util.register_player_globalstep(def)
Used to register a player globalstep.
Use this insteed of global step if you need to execute action on all players to avoid calling the same functions multiple time.
```
mcl_util.register_player_globalstep({
can_execute = function(dtime)
return true --return false if you want to skip "execute" function
end,
execute = function(player, dtime)
--do some cool stuff
--will be executed for every player at each server step
end,
after_execute = function(dtime)
end,
})
```
## mcl_util.registered_player_globalstep
Table containing all registered player globalstep.

View File

@ -1,3 +1,5 @@
local modpath = minetest.get_modpath(minetest.get_current_modname())
mcl_util = {}
-- Based on minetest.rotate_and_place
@ -538,3 +540,5 @@ function mcl_util.get_object_name(object)
return luaentity.nametag and luaentity.nametag ~= "" and luaentity.nametag or luaentity.description or luaentity.name
end
end
dofile(modpath.."/playerstep.lua")

View File

@ -0,0 +1,25 @@
local get_connected_players = minetest.get_connected_players
mcl_util.registered_player_globalstep = {}
local registered_player_globalstep = mcl_util.registered_player_globalstep
function mcl_util.register_player_globalstep(def)
if not def.can_execute then def.can_execute = function() return true end end
table.insert(mcl_util.registered_player_globalstep, {can_execute=def.can_execute, func=def.execute})
end
minetest.register_on_mods_loaded(function()
minetest.register_globalstep(function(dtime)
local players = get_connected_players()
for i = 1, #registered_player_globalstep do
if registered_player_globalstep[i].can_execute and registered_player_globalstep[i].can_execute(dtime) then
for p = 1, #players do
registered_player_globalstep[i].func(players[p], dtime)
end
if registered_player_globalstep[i].after_execute then
registered_player_globalstep[i].after_execute(dtime)
end
end
end
end)
end)

View File

@ -49,3 +49,33 @@ minetest.register_globalstep(function(dtime)
timer = 0
end
end)
--[[mcl_util.register_player_globalstep({
can_execute = function(dtime)
timer = timer + dtime;
if timer >= 0.3 then
return true
else
return false
end
end,
execute = function(player, dtime)
local pp = player:get_pos()
pp.y = ceil(pp.y)
local loc = vector_add(pp, {x=0,y=-1,z=0})
if loc ~= nil then
local nodeiamon = get_node(loc)
if nodeiamon ~= nil then
if on_walk[nodeiamon.name] then
on_walk[nodeiamon.name](loc, nodeiamon, player)
end
for i = 1, #registered_globals do
registered_globals[i](loc, nodeiamon, player)
end
end
end
end,
after_execute = function(dtime)
timer = 0
end,
})]]

View File

@ -1,4 +1,5 @@
name = walkover
author = lordfingle
depends = mcl_util
description = Some mode developers have shown an interest in having an `on_walk_over` event. This is useful for pressure-plates and the like.

View File

@ -6,7 +6,6 @@ local dim_change = mcl_worlds.dimension_change
local is_in_void = mcl_worlds.is_in_void
local get_spawn_pos = mcl_spawn.get_player_spawn_pos
local send_chat = minetest.chat_send_player
local get_connected = minetest.get_connected_players
local voidtimer = 0
local VOID_DAMAGE_FREQ = 0.5
@ -52,16 +51,17 @@ minetest.register_on_mods_loaded(function()
end)
-- Hurt players or teleport them back to spawn if they are too deep in the void
minetest.register_globalstep(function(dtime)
mcl_util.register_player_globalstep({
can_execute = function(dtime)
voidtimer = voidtimer + dtime
if voidtimer > VOID_DAMAGE_FREQ then
voidtimer = 0
local enable_damage = minetest.settings:get_bool("enable_damage")
local players = get_connected()
for p=1, #players do
local player = players[p]
return true
end
end,
execute = function(player, dtime)
local pos = player:get_pos()
local _, void_deadly = is_in_void(pos)
local void, void_deadly = is_in_void(pos)
if void_deadly then
local immortal_val = player:get_armor_groups().immortal
local is_immortal = false
@ -82,6 +82,5 @@ minetest.register_globalstep(function(dtime)
end
end
end
end
end
end)
end,
})

View File

@ -1,6 +1,8 @@
local S = minetest.get_translator("doc_items")
local N = function(s) return s end
local math = math
doc.sub.items = {}
-- Template texts
@ -1386,16 +1388,18 @@ TODO: If possible, try to replace this functionality by updating the revealed it
soon the player obtained a new item (probably needs new Minetest callbacks). ]]
local checktime = 8
local timer = 0
minetest.register_globalstep(function(dtime)
timer = timer + dtime
if timer > checktime then
local players = minetest.get_connected_players()
for p=1, #players do
reveal_items_in_inventory(players[p])
end
mcl_util.register_player_globalstep({
can_execute = function(dtime)
timer = timer + dtime
if timer > checktime then return true end
end,
execute = function(player, dtime)
reveal_items_in_inventory(player)
end,
after_execute = function(dtime)
timer = math.fmod(timer, checktime)
end
end)
end,
})
minetest.register_on_mods_loaded(gather_descs)

View File

@ -1,4 +1,4 @@
name = doc_items
author = Wuzzy
description = Adds automatically generated help texts for items.
depends = doc
depends = doc, mcl_util

View File

@ -108,7 +108,7 @@ end)
local main_timer = 0
local timer = 0
minetest.register_globalstep(function(dtime)
--[[minetest.register_globalstep(function(dtime)
--TODO: replace this by playerglobalstep API then implemented
main_timer = main_timer + dtime
timer = timer + dtime
@ -129,4 +129,29 @@ minetest.register_globalstep(function(dtime)
end
end
if timer > 4 then timer = 0 end
end)
end)]]
mcl_util.register_player_globalstep({
can_execute = function(dtime)
main_timer = main_timer + dtime
timer = timer + dtime
if main_timer > mcl_hbarmor.tick or timer > 4 then
if minetest.settings:get_bool("enable_damage") then
if main_timer > mcl_hbarmor.tick then main_timer = 0 end
return true
end
end
if timer > 4 then timer = 0 end
end,
execute = function(player, dtime)
local name = player:get_player_name()
if mcl_hbarmor.player_active[name] == true then
local ret = mcl_hbarmor.get_armor(player)
if ret == false then
minetest.log("error", "[mcl_hbarmor] Call to mcl_hbarmor.get_armor in globalstep returned with false!")
end
-- update all hud elements
update_hud(player)
end
end,
})

View File

@ -1,4 +1,4 @@
name = mcl_hbarmor
author = BlockMen
description = Adds a HUD bar displaying the current damage of the player's armor.
depends = hudbars, mcl_armor
depends = hudbars, mcl_armor, mcl_util

View File

@ -52,7 +52,6 @@ end
minetest.register_on_joinplayer(function(player)
set_hud(player)
local name = player:get_player_name()
wield[name] = player:get_wielded_item():get_name()
wieldindex[name] = player:get_wield_index()
@ -64,13 +63,12 @@ minetest.register_on_leaveplayer(function(player)
wieldindex[name] = nil
end)
minetest.register_globalstep(function(dtime)
for _, player in pairs(minetest.get_connected_players()) do
mcl_util.register_player_globalstep({
execute = function(player, dtime)
local player_name = player:get_player_name()
local wstack = player:get_wielded_item()
local wname = wstack:get_name()
local windex = player:get_wield_index()
if dtimes[player_name] and dtimes[player_name] < dlimit then
dtimes[player_name] = dtimes[player_name] + dtime
if dtimes[player_name] > dlimit and huds[player_name] then
@ -83,12 +81,9 @@ minetest.register_globalstep(function(dtime)
wieldindex[player_name] = windex
wield[player_name] = wname
dtimes[player_name] = 0
if huds[player_name] then
local def = minetest.registered_items[wname]
local meta = wstack:get_meta()
--[[ Get description. Order of preference:
* description from metadata
* description from item definition
@ -108,6 +103,5 @@ minetest.register_globalstep(function(dtime)
player:hud_change(huds[player_name], 'text', desc)
end
end
end
end)
end,
})

View File

@ -1,4 +1,5 @@
name = show_wielded_item
author = 4aiman
description = Displays the name of the wielded item.
depends = mcl_util
optional_depends = hudbars

View File

@ -340,17 +340,15 @@ controls.register_on_hold(function(player, key, time)
end
end)
minetest.register_globalstep(function(dtime)
for _, player in pairs(minetest.get_connected_players()) do
mcl_util.register_player_globalstep({
execute = function(player, dtime)
local name = player:get_player_name()
local wielditem = player:get_wielded_item()
local wieldindex = player:get_wield_index()
--local controls = player:get_player_control()
if type(bow_load[name]) == "number" and ((wielditem:get_name()~="mcl_bows:bow_0" and wielditem:get_name()~="mcl_bows:bow_1" and wielditem:get_name()~="mcl_bows:bow_2" and wielditem:get_name()~="mcl_bows:bow_0_enchanted" and wielditem:get_name()~="mcl_bows:bow_1_enchanted" and wielditem:get_name()~="mcl_bows:bow_2_enchanted") or wieldindex ~= bow_index[name]) then
if type(bow_load[name]) == "number" and ((wielditem:get_name()~="mcl_bows:bow_0" and wielditem:get_name()~="mcl_bows:bow_1" and wielditem:get_name()~="mcl_bows:bow_2" and wielditem:get_name()~="mcl_bows:bow_0_enchanted" and wielditem:get_name()~="mcl_bows:bow_1_enchanted" and wielditem:get_name()~="mcl_bows:bow_2_enchanted") or player:get_wield_index() ~= bow_index[name]) then
reset_bow_state(player, true)
end
end
end)
end,
})
minetest.register_on_joinplayer(function(player)
reset_bows(player)

View File

@ -1,6 +1,6 @@
name = mcl_bows
author = Arcelmi
description = This mod adds bows and arrows for MineClone 2.
depends = controls, mcl_particles, mcl_enchanting, mcl_init
depends = controls, mcl_particles, mcl_enchanting, mcl_init, mcl_util
optional_depends = awards, mcl_achievements, mcl_core, mcl_mobitems, playerphysics, doc, doc_identifier, mesecons_button

View File

@ -111,8 +111,8 @@ minetest.register_globalstep(function(dtime)
player:get_inventory():set_stack("main", s, "mcl_clock:clock_"..frame.." "..count)
end
end
end
end)
end,
})
-- Immediately set correct clock time after crafting
minetest.register_on_craft(function(itemstack)

View File

@ -1,4 +1,4 @@
name = mcl_clock
description = A fantasy clock item roughly shows the time of day.
depends = mcl_init, mcl_worlds, mesecons
depends = mcl_init, mcl_worlds, mesecons, mcl_util
optional_depends = doc

View File

@ -13,6 +13,8 @@ local random_timer_trigger = 0.5 -- random compass spinning tick in seconds. Inc
local random_frame = math.random(0, compass_frames-1)
local math = math
function mcl_compass.get_compass_image(pos, dir)
-- Compasses do not work in certain zones
if mcl_worlds.compass_works(pos) then
@ -34,14 +36,6 @@ function mcl_compass.get_compass_image(pos, dir)
end
end
minetest.register_globalstep(function(dtime)
random_timer = random_timer + dtime
if random_timer >= random_timer_trigger then
random_frame = (random_frame + math.random(-1, 1)) % compass_frames
random_timer = 0
end
for i,player in pairs(minetest.get_connected_players()) do
local function has_compass(player)
for _,stack in pairs(player:get_inventory():get_list("main")) do
if minetest.get_item_group(stack:get_name(), "compass") ~= 0 then
@ -50,6 +44,17 @@ minetest.register_globalstep(function(dtime)
end
return false
end
mcl_util.register_player_globalstep({
can_execute = function(dtime)
random_timer = random_timer + dtime
if random_timer >= random_timer_trigger then
random_frame = (random_frame + math.random(-1, 1)) % compass_frames
random_timer = 0
end
return true
end,
execute = function(player, dtime)
if has_compass(player) then
local pos = player:get_pos()
local compass_image = mcl_compass.get_compass_image(pos, player:get_look_horizontal())
@ -63,8 +68,8 @@ minetest.register_globalstep(function(dtime)
end
end
end
end
end)
end,
})
local images = {}
for frame = 0, compass_frames-1 do

View File

@ -1,4 +1,4 @@
name = mcl_compass
description = A compass item which points towards the world origin.
depends = mcl_core, mcl_worlds, mesecons
depends = mcl_core, mcl_worlds, mesecons, mcl_util
optional_depends = doc

View File

@ -19,7 +19,7 @@ local find_nodes_in_area = minetest.find_nodes_in_area
local find_node_near = minetest.find_node_near
local get_item_group = minetest.get_item_group
local get_connected_players = minetest.get_connected_players
--local get_connected_players = minetest.get_connected_players
local vector = vector
local math = math
@ -385,7 +385,7 @@ if flame_sound then
-- Cycle for updating players sounds
minetest.register_globalstep(function(dtime)
--[[minetest.register_globalstep(function(dtime)
timer = timer + dtime
if timer < cycle then
return
@ -396,7 +396,21 @@ if flame_sound then
for n = 1, #players do
mcl_fire.update_player_sound(players[n])
end
end)
end)]]
mcl_util.register_player_globalstep({
can_execute = function(dtime)
timer = timer + dtime
if timer < cycle then
return false
end
timer = 0
return true
end,
execute = function(player, dtime)
mcl_fire.update_player_sound(player)
end,
})
-- Stop sound and clear handle on player leave

View File

@ -1,3 +1,3 @@
name = mcl_fire
depends = mcl_core, mcl_worlds, mcl_sounds, mcl_particles
depends = mcl_core, mcl_worlds, mcl_sounds, mcl_particles, mcl_util
optional_depends = mcl_portals

View File

@ -285,12 +285,12 @@ minetest.register_on_leaveplayer(function(player)
huds[player] = nil
end)
minetest.register_globalstep(function(dtime)
for _, player in pairs(minetest.get_connected_players()) do
mcl_util.register_player_globalstep({
execute = function(player, dtime)
local wield = player:get_wielded_item()
local texture = mcl_maps.load_map_item(wield)
local hud = huds[player]
if texture then
local hud = huds[player]
local wield_def = wield:get_definition()
local hand_def = player:get_inventory():get_stack("hand", 1):get_definition()
@ -339,5 +339,5 @@ minetest.register_globalstep(function(dtime)
player:hud_change(hud.marker, "text", "blank.png")
maps[player] = nil
end
end
end)
end,
})

View File

@ -175,6 +175,54 @@ minetest.register_globalstep(function(dtime)
end
end)
--[[mcl_util.register_player_globalstep({
can_execute = function(dtime)
main_timer = main_timer + dtime
timer = timer + dtime
if main_timer > mcl_hunger.HUD_TICK or timer > 0.5 then
if main_timer > mcl_hunger.HUD_TICK then main_timer = 0 end
return true
end
end,
execute = function(player, dtime)
local name = player:get_player_name()
local h = tonumber(mcl_hunger.get_hunger(player))
local hp = player:get_hp()
if timer > 0.5 then
-- Slow health regeneration, and hunger damage (every 4s).
-- Regeneration rate based on tutorial video <https://www.youtube.com/watch?v=zs2t-xCVHBo>.
-- Minecraft Wiki seems to be wrong in claiming that full hunger gives 0.5s regen rate.
if timerMult == 0 then
if h >= 18 and hp > 0 and hp < 20 then
-- +1 HP, +exhaustion
player:set_hp(hp+1)
mcl_hunger.exhaust(name, mcl_hunger.EXHAUST_REGEN)
mcl_hunger.update_exhaustion_hud(player, mcl_hunger.get_exhaustion(player))
elseif h == 0 then
-- Damage hungry player down to 1 HP
-- TODO: Allow starvation at higher difficulty levels
if hp-1 > 0 then
if mod_death_messages then
mcl_death_messages.player_damage(player, S("@1 starved to death.", name))
end
player:set_hp(hp-1)
end
end
end
end
end,
after_execute = function(dtime)
if timer > 0.5 then
timer = 0
timerMult = timerMult + 1
if timerMult > 7 then
timerMult = 0
end
end
end,
})]]
--[[ IF HUNGER IS NOT ENABLED ]]
else

View File

@ -1,4 +1,4 @@
name = mcl_hunger
author = BlockMen
description = Adds a simple hunger meachanic with satiation, food poisoning and different healing.
depends = hudbars
depends = hudbars, mcl_util

View File

@ -168,8 +168,8 @@ local player_set_animation = mcl_player.player_set_animation
local player_attached = mcl_player.player_attached
-- Check each player and apply animations
minetest.register_globalstep(function(dtime)
for _, player in pairs(minetest.get_connected_players()) do
mcl_util.register_player_globalstep({
execute = function(player, dtime)
local name = player:get_player_name()
local model_name = player_model[name]
local model = model_name and models[model_name]
@ -188,8 +188,6 @@ minetest.register_globalstep(function(dtime)
animation_speed_mod = animation_speed_mod / 2
end
-- ask if player is swiming
local head_in_water = minetest.get_item_group(mcl_playerinfo[name].node_head, "water") ~= 0
-- ask if player is sprinting

View File

@ -1,3 +1,4 @@
name = mcl_player
author = celeron55
depends = mcl_util
description = Adds the 3D player model, taken from Minetest Game 0.4.16.

View File

@ -41,37 +41,51 @@ local function get_player_nodes(player_pos)
end
minetest.register_globalstep(function(dtime)
time = time + dtime
-- Run the rest of the code every 0.5 seconds
if time < 0.5 then
return
end
-- reset time for next check
-- FIXME: Make sure a regular check interval applies
time = 0
-- check players
for _,player in pairs(minetest.get_connected_players()) do
-- who am I?
local name = player:get_player_name()
-- where am I?
local pos = player:get_pos()
-- what is around me?
local node_stand, node_stand_below, node_head, node_feet = get_player_nodes(pos)
mcl_playerinfo[name].node_stand = node_stand
mcl_playerinfo[name].node_stand_below = node_stand_below
mcl_playerinfo[name].node_head = node_head
mcl_playerinfo[name].node_feet = node_feet
end
end)
--[[mcl_util.register_player_globalstep({
can_execute = function(dtime)
time = time + dtime
if time < 0.5 then
return false
end
-- reset time for next check
-- FIXME: Make sure a regular check interval applies
time = 0
return true
end,
execute = function(player, dtime)
local name = player:get_player_name()
local pos = player:get_pos()
local node_stand, node_stand_below, node_head, node_feet = get_player_nodes(pos)
mcl_playerinfo[name].node_stand = node_stand
mcl_playerinfo[name].node_stand_below = node_stand_below
mcl_playerinfo[name].node_head = node_head
mcl_playerinfo[name].node_feet = node_feet
end,
})]]
-- set to blank on join (for 3rd party mods)
minetest.register_on_joinplayer(function(player)
local name = player:get_player_name()

View File

@ -1,4 +1,4 @@
name = mcl_playerinfo
author = TenPlus1
description = This is a helper mod for other mod to query the nodes around the player.
depends = mcl_init, mcl_core, mcl_particles
depends = mcl_init, mcl_core, mcl_particles, mcl_util

View File

@ -1,4 +1,4 @@
name = mcl_wieldview
author = stujones11
description = Makes hand wielded items visible to other players.
depends = mcl_player
depends = mcl_player, mcl_util