Skip to content
This repository has been archived by the owner on Oct 22, 2024. It is now read-only.

Commit

Permalink
Use events better (reduce on_tick activities)
Browse files Browse the repository at this point in the history
- group utility functions up in `control.lua`.
- use `on_init`, `on_configuration_changed`, and `on_player_created` for
initialization.
- remove manual checks for version number; if `on_configuration_changed`
fires, we can assume we need to do something about it.
- try to provide a better stack trace when handing over control to
sensors for `{create,update,delete}_ui`
  • Loading branch information
narc0tiq committed Oct 22, 2015
1 parent f981b75 commit 08f9b77
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 44 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.4.5
0.4.6
38 changes: 37 additions & 1 deletion control.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,44 @@ function evogui.log(message)
end
end


function evogui.format_number(n) -- credit http://richard.warburton.it
local left,num,right = string.match(n,'^([^%d]*%d)(%d*)(.-)$')
return left..(num:reverse():gsub('(%d%d%d)','%1,'):reverse())..right
end


local octant_names = {
[0] = {"direction.east"},
[1] = {"direction.southeast"},
[2] = {"direction.south"},
[3] = {"direction.southwest"},
[4] = {"direction.west"},
[5] = {"direction.northwest"},
[6] = {"direction.north"},
[7] = {"direction.northeast"},
}

function evogui.get_octant_name(vector)
local radians = math.atan2(vector.y, vector.x)
local octant = math.floor( 8 * radians / (2*math.pi) + 8.5 ) % 8

return octant_names[octant]
end


script.on_init(evogui.mod_init)
script.on_configuration_changed(evogui.mod_update)


script.on_event(defines.events.on_player_created, function(event)
local status, err = pcall(evogui.new_player, event)
if err then evogui.log({"err_generic", "on_player_created", err}) end
end)


script.on_event(defines.events.on_tick, function(event)
local status, err = pcall(evogui.update_gui)
local status, err = pcall(evogui.update_gui, event)
if err then evogui.log({"err_generic", "on_tick", err}) end
end)

Expand Down
81 changes: 39 additions & 42 deletions evoGUI.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,45 @@ require "remote"
if not evogui then evogui = {} end
if not evogui.on_click then evogui.on_click = {} end

local EXPECTED_VERSION = "{{VERSION}}"

function evogui.mod_init()
if not global.settings then global.settings = {} end
if not global.settings.update_delay then global.settings.update_delay = 60 end

function evogui.format_number(n) -- credit http://richard.warburton.it
local left,num,right = string.match(n,'^([^%d]*%d)(%d*)(.-)$')
return left..(num:reverse():gsub('(%d%d%d)','%1,'):reverse())..right
for _, player in pairs(game.players) do
evogui.create_player_globals(player)
evogui.create_sensor_display(player)
end
end


function evogui.update_gui()
if not global.settings then global.settings = {} end
if not global.settings.update_delay then global.settings.update_delay = 60 end
function evogui.mod_update(data)
if data.mod_changes["{{MOD_NAME}}"] then
-- TODO: If a more major migration ever needs doing, do that here.
-- Otherwise, just falling back to mod_init should work fine.
evogui.mod_init()
end
end

if (game.tick % global.settings.update_delay) == 0 then
for i, player in ipairs(game.players) do
evogui.create_player_globals(player)
evogui.create_sensor_display(player)

local player_settings = global.evogui[player.name]
function evogui.new_player(event)
local player = game.get_player(event.player_index)

evogui.create_player_globals(player)
evogui.create_sensor_display(player)
end

local sensor_flow = player.gui.top.evoGUI.sensor_flow
evogui.update_av(player, sensor_flow.always_visible)
if player_settings.popup_open then
evogui.update_ip(player, sensor_flow.in_popup)
end

function evogui.update_gui(event)
if (event.tick % global.settings.update_delay) ~= 0 then return end

for _, player in pairs(game.players) do
local player_settings = global.evogui[player.name]

local sensor_flow = player.gui.top.evoGUI.sensor_flow
evogui.update_av(player, sensor_flow.always_visible)
if player_settings.popup_open then
evogui.update_ip(player, sensor_flow.in_popup)
end
end
end
Expand Down Expand Up @@ -82,6 +96,7 @@ function evogui.create_player_globals(player)
['show_offline'] = false,
}
elseif player_settings.sensor_settings['player_locations'].show_offline == nil then
-- 0.4.3 new feature (783e3d68)
player_settings.sensor_settings['player_locations'].show_offline = false
end

Expand Down Expand Up @@ -110,7 +125,7 @@ end
function evogui.create_sensor_display(player)
local root = player.gui.top.evoGUI
local destroyed = false
if root and global.evogui[player.name].version ~= EXPECTED_VERSION then
if root then
player.gui.top.evoGUI.destroy()
destroyed = true
end
Expand Down Expand Up @@ -147,43 +162,25 @@ function evogui.create_sensor_display(player)
name="in_popup",
direction="vertical",
style="description_flow_style"}

global.evogui[player.name].version = EXPECTED_VERSION
end
end


local function update_sensors(element, sensor_list, active_sensors)
for _, sensor in ipairs(sensor_list) do
if active_sensors[sensor.name] then
sensor:create_ui(element)
sensor:update_ui(element)
local status, err = pcall(sensor.create_ui, sensor, element)
if err then error({"err_specific", sensor.name, "create_ui", err}) end
status, err = pcall(sensor.update_ui, sensor, element)
if err then error({"err_specific", sensor.name, "update_ui", err}) end
else
sensor:delete_ui(element)
local status, err = pcall(sensor.delete_ui, sensor, element)
if err then error({"err_specific", sensor.name, "delete_ui", err}) end
end
end
end


local octant_names = {
[0] = {"direction.east"},
[1] = {"direction.southeast"},
[2] = {"direction.south"},
[3] = {"direction.southwest"},
[4] = {"direction.west"},
[5] = {"direction.northwest"},
[6] = {"direction.north"},
[7] = {"direction.northeast"},
}

function evogui.get_octant_name(vector)
local radians = math.atan2(vector.y, vector.x)
local octant = math.floor( 8 * radians / (2*math.pi) + 8.5 ) % 8

return octant_names[octant]
end


function evogui.update_av(player, element)
local always_visible = global.evogui[player.name].always_visible

Expand Down

0 comments on commit 08f9b77

Please sign in to comment.