[Mod] Clock [testclock]

User avatar
cactuz_pl
Member
 
Posts: 874
Joined: Tue Jun 05, 2012 16:34

[Mod] Clock [testclock]

by cactuz_pl » Wed May 21, 2014 16:52

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.
Attachments
testclock.zip
(866 Bytes) Downloaded 180 times
Zrzut ekranu.png
Zrzut ekranu.png (209.46 KiB) Viewed 2432 times
Last edited by cactuz_pl on Wed May 21, 2014 17:05, edited 1 time in total.
 

User avatar
Krock
Member
 
Posts: 3598
Joined: Thu Oct 03, 2013 07:48
GitHub: SmallJoker

Re: [Mod] Clock [testclock]

by Krock » Wed May 21, 2014 16:57

And why is this useful?
Newest Win32 builds - Find a mod - All my mods
ALL YOUR DONATION ARE BELONG TO PARAMAT (Please support him and Minetest)
New DuckDuckGo !bang: !mtmod <keyword here>
 

Hybrid Dog
Member
 
Posts: 2460
Joined: Thu Nov 01, 2012 12:46

Re: [Mod] Clock [testclock]

by Hybrid Dog » Wed May 21, 2014 17:01

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?
 

User avatar
Krock
Member
 
Posts: 3598
Joined: Thu Oct 03, 2013 07:48
GitHub: SmallJoker

Re: [Mod] Clock [testclock]

by Krock » Wed May 21, 2014 17:07

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.
Newest Win32 builds - Find a mod - All my mods
ALL YOUR DONATION ARE BELONG TO PARAMAT (Please support him and Minetest)
New DuckDuckGo !bang: !mtmod <keyword here>
 

User avatar
cactuz_pl
Member
 
Posts: 874
Joined: Tue Jun 05, 2012 16:34

Re: [Mod] Clock [testclock]

by cactuz_pl » Wed May 21, 2014 17:09

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.
 

Amaz
Member
 
Posts: 328
Joined: Wed May 08, 2013 08:26
GitHub: Amaz1
IRC: Amaz
In-game: Amaz

Re: [Mod] Clock [testclock]

by Amaz » Wed May 21, 2014 17:10

Great mod! Really useful for me...
 

Kilarin
Member
 
Posts: 649
Joined: Mon Mar 10, 2014 00:36

Re: [Mod] Clock [testclock]

by Kilarin » Fri May 23, 2014 13:03

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!
 

User avatar
kaeza
Member
 
Posts: 2141
Joined: Thu Oct 18, 2012 05:00
GitHub: kaeza
IRC: kaeza diemartin blaaaaargh
In-game: kaeza

Re: [Mod] Clock [testclock]

by kaeza » Fri May 23, 2014 13:20

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 :)
Your signature is not the place for a blog post. Please keep it as concise as possible. Thank you!

Check out my stuff! | Donations greatly appreciated! PayPal | BTC: 1DFZAa5VtNG7Levux4oP6BuUzr1e83pJK2
 

Hybrid Dog
Member
 
Posts: 2460
Joined: Thu Nov 01, 2012 12:46

Re: [Mod] Clock [testclock]

by Hybrid Dog » Fri May 23, 2014 17:20

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).
 

User avatar
Excalibur Zero
Member
 
Posts: 142
Joined: Tue Apr 02, 2013 19:45
GitHub: ExcaliburZero

Re: [Mod] Clock [testclock]

by Excalibur Zero » Fri May 23, 2014 17:56

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;
            });
 

Hybrid Dog
Member
 
Posts: 2460
Joined: Thu Nov 01, 2012 12:46

Re: [Mod] Clock [testclock]

by Hybrid Dog » Fri May 23, 2014 18:06

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.
 

User avatar
cHyper
Member
 
Posts: 587
Joined: Fri May 06, 2011 08:49
IRC: cHyper
In-game: cHyper

Re: [Mod] Clock [testclock]

by cHyper » Fri May 23, 2014 19:35

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?
 

Kilarin
Member
 
Posts: 649
Joined: Mon Mar 10, 2014 00:36

Re: [Mod] Clock [testclock]

by Kilarin » Fri May 23, 2014 19:38

Thanks very much folks. I'm learning!
 

User avatar
philipbenr
Member
 
Posts: 1665
Joined: Fri Jun 14, 2013 01:56
GitHub: philipbenr
IRC: philipbenr
In-game: WisdomFire or philipbenr

Re: [Mod] Clock [testclock]

by philipbenr » Fri May 23, 2014 23:05

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.
 

Hybrid Dog
Member
 
Posts: 2460
Joined: Thu Nov 01, 2012 12:46

Re: [Mod] Clock [testclock]

by Hybrid Dog » Sat May 24, 2014 09:35

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
 

User avatar
cHyper
Member
 
Posts: 587
Joined: Fri May 06, 2011 08:49
IRC: cHyper
In-game: cHyper

Re: [Mod] Clock [testclock]

by cHyper » Sat May 24, 2014 13:44

try the modmenu mod instead of inventory plus

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

Hybrid Dog
Member
 
Posts: 2460
Joined: Thu Nov 01, 2012 12:46

Re: [Mod] Clock [testclock]

by Hybrid Dog » Sat May 24, 2014 18:09

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
 

User avatar
RealBadAngel
Member
 
Posts: 556
Joined: Wed Jul 18, 2012 16:30

Re: [Mod] Clock [testclock]

by RealBadAngel » Sun May 25, 2014 01:20

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
 

Hybrid Dog
Member
 
Posts: 2460
Joined: Thu Nov 01, 2012 12:46

Re: [Mod] Clock [testclock]

by Hybrid Dog » Sun May 25, 2014 08:40

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?
 

User avatar
crazyginger72
Member
 
Posts: 69
Joined: Wed Jan 01, 2014 07:57
GitHub: crazyginger72
IRC: cg72
In-game: crazyginger72

Re: [Mod] Clock [testclock]

by crazyginger72 » Sat Jun 28, 2014 05:46

i got one similar that shows lag to


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


Return to WIP Mods

Who is online

Users browsing this forum: No registered users and 36 guests

cron