Page 1 of 1

[Mod] Clock [testclock]

PostPosted: Wed May 21, 2014 16:52
by cactuz_pl
This mod adds in-game time clock and two countdowns: Time to next sunrise and sunset.

This is improved version of kaeza's code (Licence: WTFPL) which you can find here.

Image

Countdowns shows real time MM:SS to sunrise/sunset.

Licence: WTFPL
Dependences: none
DOWNLOAD

I'm not planning to develop this mod, this is final version.

Re: [Mod] Clock [testclock]

PostPosted: Wed May 21, 2014 16:57
by Krock
And why is this useful?

Re: [Mod] Clock [testclock]

PostPosted: Wed May 21, 2014 17:01
by Hybrid Dog
Krock wrote:And why is this useful?

it's useful because if you set the time and time speed to real time you can play minetest with fullscreen and still be able to view the clock
EDIT: Where's the download?

Re: [Mod] Clock [testclock]

PostPosted: Wed May 21, 2014 17:07
by Krock
Hybrid Dog wrote:it's useful because if you set the time and time speed to real time you can play minetest with fullscreen and still be able to view the clock
EDIT: Where's the download?

Okay, for me, this would have no use..don't know about other players.

The download is an attachment.

Re: [Mod] Clock [testclock]

PostPosted: Wed May 21, 2014 17:09
by cactuz_pl
You know how much time you have before dark, and how long you need to wait for daylight. I guess nobody likes to build in night.

Re: [Mod] Clock [testclock]

PostPosted: Wed May 21, 2014 17:10
by Amaz
Great mod! Really useful for me...

Re: [Mod] Clock [testclock]

PostPosted: Fri May 23, 2014 13:03
by Kilarin
This mod does some fascinating and advanced things!
I'm trying to understand how updating the hud works, would you mind answering some stupid newbie questions? The only documentation I can find on this at all is in https://github.com/minetest/minetest/blob/master/doc/lua_api.txt

Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
minetest.register_globalstep(function ( dtime )
    timer = timer + dtime;
    if (timer >= 1.0) then
        timer = 0;
        for _,p in ipairs(minetest.get_connected_players()) do
            local name = p:get_player_name();
            local h = p:hud_add({
                hud_elem_type = "text";
                position = {x=0.5, y=0.01};
                text = get_time();
                number = 0xFFFF00;
      scale = 20;
            });
            if (player_hud[name]) then
                p:hud_remove(player_hud[name]);
            end
            player_hud[name] = h;
        end
    end
end);

So, you establish one globalstep that will run every cycle. In a multiplayer game this globalstep will run for every single player, right? That's why you specifically loop through all the players to update everyone's huds? And I presume this would be true of any globalstep added by a mod. If an item adds a globalstep, then in multiplayer, that globalstep will run for ALL players unless the globalstep takes specific care to only affect the item that added it.

local h = p:hud_add
hud_add adds a new element to player p's hud. You are adding a text element that will appear relatively half way across the players screen in the x coord, and 0.1 way down the screen in the y coord. scale sets the text size and number assigns the color I would assume?

The h that is returned is the id of new hud element right?

Where I get really confused is the section right after the hud_add.

Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
            if (player_hud[name]) then
                p:hud_remove(player_hud[name]);
            end
            player_hud[name] = h;


player_hud was defined at the top of this lua as blank. So player_hud[name] will be nil on the first check, and will be assigned to h which came out of our hud_add. But then the NEXT time through the loop, player_hud[name] will be defined, and so, for this player, we remove element h.

So, if I'm following, we add the clock text to the hud every cycle. If the clock text was there last cycle as well, we remove the OLD clock text from the hud. I'm guessing that if we didn't do this the new element and old element would overlap each other and after just a few seconds that section of the screen would just be a mess. Is that right?

Is adding a new element and removing the old element more efficient then using hud_change to just change the text element of the previously added item?

This is fascinating code, but if you (or anyone) could help explain to me how it works, I would be very grateful.

Thanks!

Re: [Mod] Clock [testclock]

PostPosted: Fri May 23, 2014 13:20
by kaeza
Kilarin wrote:Is adding a new element and removing the old element more efficient then using hud_change to just change the text element of the previously added item?

There was a bug in the early stages of the Lua HUD (when the original mod was made) whereby calling hud_change() was completely ignored by the client, so this was a quick workaround.

I believe the bug is already fixed, so probably using hud_change() is better.

Edit:
About the globalstep:

The globalstep runs only once per server step (normally every 0.05 seconds, 50 milliseconds), which may be more or less depending on whether the server got busy with other tasks, or needs to "catch up" because of that, respectively. This is regardless of how many clients are connected. The `timer' variable is incremented by `dtime' seconds (which is the time since the previous call to the function was made, i.e. the time since the last server step). Now, the conditional ensures that the HUD only gets updated once at least a second has elapsed.

The loop iterates through the connected players exactly how you described it.

All in all the mod is quite simple and straightforward. If you need more explanations about this, don't hesitate to ask :)

Re: [Mod] Clock [testclock]

PostPosted: Fri May 23, 2014 17:20
by Hybrid Dog
kaeza wrote:
Kilarin wrote:Is adding a new element and removing the old element more efficient then using hud_change to just change the text element of the previously added item?

There was a bug in the early stages of the Lua HUD (when the original mod was made) whereby calling hud_change() was completely ignored by the client, so this was a quick workaround.

I believe the bug is already fixed, so probably using hud_change() is better.

Edit:
About the globalstep:

The globalstep runs only once per server step (normally every 0.05 seconds, 50 milliseconds), which may be more or less depending on whether the server got busy with other tasks, or needs to "catch up" because of that, respectively. This is regardless of how many clients are connected. The `timer' variable is incremented by `dtime' seconds (which is the time since the previous call to the function was made, i.e. the time since the last server step). Now, the conditional ensures that the HUD only gets updated once at least a second has elapsed.

The loop iterates through the connected players exactly how you described it.

All in all the mod is quite simple and straightforward. If you need more explanations about this, don't hesitate to ask :)

I compared os.clock() and dtime and noticed that dtime doesn't work correctly (real time).

Re: [Mod] Clock [testclock]

PostPosted: Fri May 23, 2014 17:56
by Excalibur Zero
I tried this mod out and I have so far found it interesting. However I dodn't like the text being placed in the top middle so I decided to move it to the bottom right. If anyone esle wants to do the same they can do so by changing the lines:

Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
local h = p:hud_add({
                hud_elem_type = "text";
                position = {x=0.5, y=0.01};
                text = get_time();
                number = 0xFFFF00;
      scale = 20;
            });


To these lines:


Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
local h = p:hud_add({
                hud_elem_type = "text";
                position = {x=0.75, y=0.99};
                text = get_time();
                number = 0xFFFF00;
      scale = 20;
            });

Re: [Mod] Clock [testclock]

PostPosted: Fri May 23, 2014 18:06
by Hybrid Dog
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
local player_hud = {}

local timer = 0

local function floormod(x, y)
   return math.floor(x) % y
end

local function get_time()
   local time = minetest.get_timeofday()
   --local secs = (60*60*24*time)
   --local s = floormod(secs, 60)
   local tmp = 24*time
   local m = floormod(60*tmp, 60)
   local h = floormod(tmp, 60)
   --local time_to_sunrise

   --if (time >= (0.198)) then
   --   time_to_sunrise = 1200*(1.198 - time)
   --else
   --   time_to_sunrise = 1200*(0.198 - time)
   --end

   -- time to next sunrise in real-time seconds, 0.198 is time of sunrise in 0..1 range, 72" is world time speed const;

   --local a = floormod(time_to_sunrise, 60)
   --local b = floormod(time_to_sunrise/60, 60)
   --local c = floormod(time_to_sunrise/3600, 60)



   --return ("Time: %02d:%02d:%02d Next sunrise in: %02d:%02d:%02d"):format(h, m, s, c, b, a) --return ("%02d:%02d:%02d"):format(h, m, s);
   return ("%02d:%02d"):format(h, m) --return ("%02d:%02d:%02d"):format(h, m, s);
end

minetest.register_globalstep(function(dtime)
   timer = timer + dtime
   if timer >= 1 then
      timer = 0
      for _,p in pairs(minetest.get_connected_players()) do
         local name = p:get_player_name()
         local h = p:hud_add({
            hud_elem_type = "text";
            position = {x=0.35, y=0.900}; --position = {x=0.35, y=0.900};
            text = get_time();
            number = 0xFFFF00;   --colour
         })
         if player_hud[name] then
            p:hud_remove(player_hud[name])
         end
         player_hud[name] = h
      end
   end
end)
I only want to see the clock, so I shorted the code a bit.

Re: [Mod] Clock [testclock]

PostPosted: Fri May 23, 2014 19:35
by cHyper
waypoints do not work with the mod unified inventory.
the overlay-informations of waypoints disappears on the screen.

is this a feature of the hud or what else?

Re: [Mod] Clock [testclock]

PostPosted: Fri May 23, 2014 19:38
by Kilarin
Thanks very much folks. I'm learning!

Re: [Mod] Clock [testclock]

PostPosted: Fri May 23, 2014 23:05
by philipbenr
Krock wrote:And why is this useful?


When you are underground, mining/exploring, and you don't want to come up in the middle of a dirt monster cluster... It happens.

Re: [Mod] Clock [testclock]

PostPosted: Sat May 24, 2014 09:35
by Hybrid Dog
cHyper wrote:waypoints do not work with the mod unified inventory.
the overlay-informations of waypoints disappears on the screen.

is this a feature of the hud or what else?

try the modmenu mod instead of inventory plus

Re: [Mod] Clock [testclock]

PostPosted: Sat May 24, 2014 13:44
by cHyper
try the modmenu mod instead of inventory plus

who made this mod?
is this a GUI for minetest?

Re: [Mod] Clock [testclock]

PostPosted: Sat May 24, 2014 18:09
by Hybrid Dog
cHyper wrote:
try the modmenu mod instead of inventory plus

who made this mod?
is this a GUI for minetest?

You can use the unified inventory mod
viewtopic.php?id=3933
or the inventory plus mod
viewtopic.php?id=3100
. Both change the inventory, so inventory_plus doesn't work if unified inventory is used.
inventory plus changes the inventory that people can add their own stuff to it with mods
unified inventory changes the inventory by adding some buttons... but mods can't modify it, I think
modmenu
viewtopic.php?id=5824
shows the stuff of inventory plus with /m, so unified inventory can be used

Re: [Mod] Clock [testclock]

PostPosted: Sun May 25, 2014 01:20
by RealBadAngel
Hybrid Dog wrote:
cHyper wrote:
try the modmenu mod instead of inventory plus

who made this mod?
is this a GUI for minetest?

You can use the unified inventory mod
viewtopic.php?id=3933
or the inventory plus mod
viewtopic.php?id=3100
. Both change the inventory, so inventory_plus doesn't work if unified inventory is used.
inventory plus changes the inventory that people can add their own stuff to it with mods
unified inventory changes the inventory by adding some buttons... but mods can't modify it, I think
modmenu
viewtopic.php?id=5824
shows the stuff of inventory plus with /m, so unified inventory can be used


Mods can easily add own buttons and tabs to UI,
Like this one: https://forum.minetest.net/viewtopic.php?t=8816

Re: [Mod] Clock [testclock]

PostPosted: Sun May 25, 2014 08:40
by Hybrid Dog
RealBadAngel wrote:
Mods can easily add own buttons and tabs to UI,
Like this one: https://forum.minetest.net/viewtopic.php?t=8816

ok, could a button be added to UI where you can find the stuff which would be added to inventory plus?

Re: [Mod] Clock [testclock]

PostPosted: Sat Jun 28, 2014 05:46
by crazyginger72
i got one similar that shows lag to


viewtopic.php?f=9&t=9615&p=146480#p146480