[MOD 0.12.12] Clock

Topics and discussion about specific mods
User avatar
bioxz
Burner Inserter
Burner Inserter
Posts: 18
Joined: Thu Oct 22, 2015 1:53 pm
Contact:

[MOD 0.12.12] Clock

Post by bioxz »

Mod-Info-Header
  • Name: Clock
  • Version: 0.0.1
  • Factorio-Version: 0.12.12
  • Description: A 24h clock for ingame time in the top left corner.
  • License: MIT
  • Release: 2015-10-22
  • Download-Url: Download
  • Source: github
My first try of a simple mod to show the ingame time as a 24h clock. I found a mod like this, but it was only for an older version of the game. I appreciate tips how to improve the performance, i want to avoid to make all those intense calculation at every tick.

Bizz Keryear
Long Handed Inserter
Long Handed Inserter
Posts: 91
Joined: Thu Oct 22, 2015 5:08 am
Contact:

Re: [MOD 0.12.12] Clock

Post by Bizz Keryear »

Wow thanks a bunch!
Was just looking for something like this.

As asked will have a look at the code... but to be honest I am not the best coder out there either (and you making this saved me the trouble to have a look in the API myself ... not that I am only incompetent ... I am lazy, too)

User avatar
steinio
Smart Inserter
Smart Inserter
Posts: 2638
Joined: Sat Mar 12, 2016 4:19 pm
Contact:

Re: [MOD 0.12.12] Clock

Post by steinio »

Hello,

nice little mod.
Is it possible to also show the real world clock?
I always play for hours and forgot to check the clock.
Maybe an alarm after playing x hours to prevent addiction would be useful :)

Greetings steinio
Image

Transport Belt Repair Man

View unread Posts

daniel34
Global Moderator
Global Moderator
Posts: 2761
Joined: Thu Dec 25, 2014 7:30 am
Contact:

Re: [MOD 0.12.12] Clock

Post by daniel34 »

steinio wrote:Hello,

nice little mod.
Is it possible to also show the real world clock?
I always play for hours and forgot to check the clock.
Maybe an alarm after playing x hours to prevent addiction would be useful :)

Greetings steinio
Setting an alarm (such as: remind me in 2 hours) would be possible, but showing the real world clock in the game is not.
quick links: log file | graphical issues | wiki

Factorio2016
Fast Inserter
Fast Inserter
Posts: 136
Joined: Wed Dec 23, 2015 1:17 pm
Contact:

Re: [MOD 0.12.12] Clock

Post by Factorio2016 »

Yes the alarm is a good idea!
English is not my native language. Translator.

User avatar
steinio
Smart Inserter
Smart Inserter
Posts: 2638
Joined: Sat Mar 12, 2016 4:19 pm
Contact:

Re: [MOD 0.12.12] Clock

Post by steinio »

Hello,

ok i learned, that lua is capable of date and time functions but the needed module 'os' is disabled.
This is totally correct because otherwise aggresive mods could gather control of the world outside of this game.

I solved this problem with PlayClaw, which is normally used for screen recordings but also provide an overlay for clock and fps values.
The demo version is free but limited in features.
No advertising intended.

Greetings steinio
Attachments
Ingame Clock.png
Ingame Clock.png (77.54 KiB) Viewed 9532 times
Image

Transport Belt Repair Man

View unread Posts

Kruparker
Burner Inserter
Burner Inserter
Posts: 16
Joined: Wed May 25, 2016 6:16 pm
Contact:

Re: [MOD 0.12.12] Clock

Post by Kruparker »

Hi,
thanks for your nice, small but very usefull little mod.

I did some "benchmarks" and found an other less time consuming function. It's not so big, but it's always good to take.

here the full modified version of your code : bench commented at the end

edit : code is also modified to update only 4 times per second instead of every tick
--------------------------------------------------------------------

Code: Select all

require "defines"


script.on_event(defines.events.on_tick, function(event)
	if not global.clock_enabled then
		init()
	end

	if global.control_tick > 14 then -- Uptime GUI 4 times by second (every 15 ticks) -> because 4 games minutes ~= 60 ticks ( 1s )
		update()
	else
		global.control_tick = global.control_tick + 1
	end

	
end)

function init()
	game.players[1].gui.left.add{type = "label", name = "clock_game", caption = "Factorio Clock", style = "description_title_label_style"}
	global.clock_enabled = 0
	global.control_tick = 0
	
end

function update()
		global.control_tick = 0
		
		local useless, gametime = math.modf(game.daytime+0.5) -- get daytime -> add 0.5  -> split result : integer part IN useless, decimal part IN gametime
		local hours, minutes = math.modf(gametime * 24 )      -- multiply game time by 24 -> split result : integer IN hours, decimal hours (.5H = 30min) IN minutes
		minutes = math.floor(minutes * 60)                    -- convert decimal hours to minutes
		
		if minutes < 10 then
			minutes = 0 .. minutes
		end
		game.players[1].gui.left.clock_game.caption = hours .. ":" .. minutes

		
		--local i 
		--local ptime

		---- APPROX 20s
		--for i = 1, 100000000 do 
		--	useless, gametime = math.modf(game.daytime+0.5)
		--end
		
		
		---- ORIGINAL CODE : APPROX 21.5s
		--for i = 1, 100000000 do
		--	ptime = math.fmod(game.daytime + 0.5, 1)
		--end
end

User avatar
steinio
Smart Inserter
Smart Inserter
Posts: 2638
Joined: Sat Mar 12, 2016 4:19 pm
Contact:

Re: [MOD 0.12.12] Clock

Post by steinio »

Seems nobody knows the util.lua in Factorio/data/core/lualib otherwise everybody would know the formattime function.

Just take a look and require "util".

Greetings steinio
Image

Transport Belt Repair Man

View unread Posts

Kruparker
Burner Inserter
Burner Inserter
Posts: 16
Joined: Wed May 25, 2016 6:16 pm
Contact:

Re: [MOD 0.12.12] Clock

Post by Kruparker »

indeed, there's a formattime function in util.lua

but it converts ticks to time. I don't see how we can use it here to display ingame day time.
in game day time (game.daytime) is not a value in ticks but a float from to 0 to 1 where 0 is midday (afternoon begins) and 1 is (also) midday (morning ending). so 0.5 is midnight.

User avatar
steinio
Smart Inserter
Smart Inserter
Posts: 2638
Joined: Sat Mar 12, 2016 4:19 pm
Contact:

Re: [MOD 0.12.12] Clock

Post by steinio »

Sorry it's more like show time differences between tick a and tick b in hours and minutes.
Maybe a stopwatch function?
Image

Transport Belt Repair Man

View unread Posts

Kruparker
Burner Inserter
Burner Inserter
Posts: 16
Joined: Wed May 25, 2016 6:16 pm
Contact:

Re: [MOD 0.12.12] Clock

Post by Kruparker »

Okay, after some test, I dropped dropped the util.formattime function because it doesn't count hours, so I've written my own function, and I've finished with this :

@ bioxz : You can use everything in your own mod, or some parts only, or whatever you want.

@ steinio : There is no way to get SystemTime but, I've implemented a timer showing how long you've been playing today =)


copy/paste in bioxz "control.lua" file (overwrite everything)

Code: Select all

require "defines"

script.on_load( function()
	-- init each time game is loaded
	global.initializer = 1
end)

script.on_init( function()
	-- init when a new game is created (or when mod is installed)
	global.initializer = 1
end)


script.on_event(defines.events.on_tick, function(event)
	if global.initializer == 1 then
		init()
	end

	global.clock_control_tick = global.clock_control_tick + 1
	if global.clock_control_tick > 15 then -- Uptime GUI 4 times by second (every 15 ticks) -> because 4 games minutes ~= 60 ticks ( 1s )
		update()
	end
	
	
end)

function init()
	global.initializer = 0
	if game.players[1].gui.left.clock_game then
		game.players[1].gui.left.clock_game.destroy()
	end
	
	game.players[1].gui.left.add{type="flow",name = "clock_game", direction = "vertical"}
	game.players[1].gui.left.clock_game.add{type = "label", name = "clock", caption = "Factorio Clock", style = "description_title_label_style"}
	game.players[1].gui.left.clock_game.add{type="label", name = "session_time", caption = "Session play time 00:00", style = "description_title_label_style"}
	game.players[1].gui.left.clock_game.add{type="label", name = "map_time", caption = "Map play time 00:00", style = "description_title_label_style"}
	
	global.session_start_tick = game.tick
	global.clock_control_tick = 0
end

function mytime(lticks)
	local seconds
	local hours, minutes = math.modf( lticks / 216000) -- 3600sec * 60 ticks = 216000
	
	minutes, seconds = math.modf(minutes*60) -- we have 0.xxxxx minutes -> convert to minutes and 0.xx seconds
	seconds = math.floor(seconds * 60) -- converts 0.xx seconds to seconds
	return string.format("%01d:%02d:%02d", hours,minutes, seconds)
end
	
function update()
		global.clock_control_tick = 0
		
		local useless, gametime = math.modf(game.daytime+0.5) -- get daytime -> add 0.5  -> split result : integer part IN useless, decimal part IN gametime
		local hours, minutes = math.modf(gametime * 24 )      -- multiply game time by 24 -> split result : integer IN hours, decimal hours (.5H = 30min) IN minutes
		minutes = math.floor(minutes * 60)                    -- convert decimal hours to minutes
		
		if minutes < 10 then
			minutes = 0 .. minutes
		end
		game.players[1].gui.left.clock_game.clock.caption = "Alien time " .. hours .. ":" .. minutes
		game.players[1].gui.left.clock_game.session_time.caption = "Session " .. mytime(game.tick - global.session_start_tick)
		game.players[1].gui.left.clock_game.map_time.caption = "Map " .. mytime(game.tick)

		
		--local i 
		--local ptime

		---- APPROX 20s
		--for i = 1, 100000000 do 
		--	useless, gametime = math.modf(game.daytime+0.5)
		--end
		
		
		---- ORIGINAL CODE : APPROX 21.5s
		--for i = 1, 100000000 do
		--	ptime = math.fmod(game.daytime + 0.5, 1)
		--end
end
Attachments
sample of what it does now
sample of what it does now
factorio clock mod.jpg (143.76 KiB) Viewed 7530 times

Post Reply

Return to “Mods”