Page 1 of 1

[MOD 0.12.12] Clock

Posted: Thu Oct 22, 2015 2:10 pm
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.

Re: [MOD 0.12.12] Clock

Posted: Sat Oct 24, 2015 2:34 am
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)

Re: [MOD 0.12.12] Clock

Posted: Mon Mar 21, 2016 6:40 pm
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

Re: [MOD 0.12.12] Clock

Posted: Mon Mar 21, 2016 7:27 pm
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.

Re: [MOD 0.12.12] Clock

Posted: Mon Mar 21, 2016 7:57 pm
by Factorio2016
Yes the alarm is a good idea!

Re: [MOD 0.12.12] Clock

Posted: Tue Mar 22, 2016 4:54 pm
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

Re: [MOD 0.12.12] Clock

Posted: Wed May 25, 2016 6:26 pm
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

Re: [MOD 0.12.12] Clock

Posted: Wed May 25, 2016 10:50 pm
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

Re: [MOD 0.12.12] Clock

Posted: Thu May 26, 2016 10:07 am
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.

Re: [MOD 0.12.12] Clock

Posted: Thu May 26, 2016 4:56 pm
by steinio
Sorry it's more like show time differences between tick a and tick b in hours and minutes.
Maybe a stopwatch function?

Re: [MOD 0.12.12] Clock

Posted: Thu May 26, 2016 6:04 pm
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