Skip to content

Commit 7eac68e

Browse files
committed
Changed how ticks are handled
Eliminated all unnecessary API calls. Removed redundant table creations.
1 parent 5360a68 commit 7eac68e

File tree

8 files changed

+436
-16
lines changed

8 files changed

+436
-16
lines changed

changelog.txt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,9 @@
66
+ Added locale
77

88
##0.1.2##
9-
* Minor performance improvement
9+
* Minor performance improvement
10+
11+
##0.1.3##
12+
* Switches don't emit light when on anymore
13+
* Reduced energy usage to 1kW to match the other combinators
14+
* Performance improvement

control.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
migration = require "script.migration"
22

33
script.on_init(migration.init)
4+
script.on_configuration_changed(migration.migrate)
45

56
require("script.events")

data.lua

Lines changed: 165 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,165 @@
11
data:extend({
22
--proxies for the wires to be switched
3+
{
4+
type = "constant-combinator",
5+
name = "circuit-network-switch-proxy-trans",
6+
icon = "__circuit-network-switch__/graphics/trans.png",
7+
flags = {"placeable-neutral", "player-creation"},
8+
selectable_in_game = false,
9+
max_health = 100000,
10+
order = "itsdafirstman",
11+
12+
collision_box = {{-0.35, -0.35}, {0.35, 0.35}},
13+
selection_box = {{-0.5, -0.5}, {0.5, 0.5}},
14+
15+
item_slot_count = 0,
16+
17+
sprites =
18+
{
19+
north =
20+
{
21+
filename = "__circuit-network-switch__/graphics/trans.png",
22+
x = 0,
23+
y = 0,
24+
width = 0,
25+
height = 0,
26+
frame_count = 1,
27+
shift = {0.140625, 0.140625},
28+
},
29+
east =
30+
{
31+
filename = "__circuit-network-switch__/graphics/trans.png",
32+
y = 0,
33+
width = 0,
34+
height = 0,
35+
frame_count = 1,
36+
shift = {0.140625, 0.140625},
37+
},
38+
south =
39+
{
40+
filename = "__circuit-network-switch__/graphics/trans.png",
41+
x = 0,
42+
y = 0,
43+
width = 0,
44+
height = 0,
45+
frame_count = 1,
46+
shift = {0.140625, 0.140625},
47+
},
48+
west =
49+
{
50+
filename = "__circuit-network-switch__/graphics/trans.png",
51+
y = 0,
52+
width = 0,
53+
height = 0,
54+
frame_count = 1,
55+
shift = {0.140625, 0.140625},
56+
}
57+
},
58+
59+
activity_led_sprites =
60+
{
61+
north =
62+
{
63+
filename = "__circuit-network-switch__/graphics/trans.png",
64+
width = 0,
65+
height = 0,
66+
frame_count = 1,
67+
shift = {0.296875, -0.40625},
68+
},
69+
east =
70+
{
71+
filename = "__circuit-network-switch__/graphics/trans.png",
72+
width = 0,
73+
height = 0,
74+
frame_count = 1,
75+
shift = {0.25, -0.03125},
76+
},
77+
south =
78+
{
79+
filename = "__circuit-network-switch__/graphics/trans.png",
80+
width = 0,
81+
height = 0,
82+
frame_count = 1,
83+
shift = {-0.296875, -0.078125},
84+
},
85+
west =
86+
{
87+
filename = "__circuit-network-switch__/graphics/trans.png",
88+
width = 0,
89+
height = 0,
90+
frame_count = 1,
91+
shift = {-0.21875, -0.46875},
92+
}
93+
},
94+
95+
activity_led_light =
96+
{
97+
intensity = 0,
98+
size = 0,
99+
},
100+
101+
activity_led_light_offsets =
102+
{
103+
{0.296875, -0.40625},
104+
{0.25, -0.03125},
105+
{-0.296875, -0.078125},
106+
{-0.21875, -0.46875}
107+
},
108+
109+
circuit_wire_connection_points =
110+
{
111+
{
112+
shadow =
113+
{
114+
red = {0.15625, -0.28125},
115+
green = {0.65625, -0.25}
116+
},
117+
wire =
118+
{
119+
red = {-0.28125, -0.5625},
120+
green = {0.21875, -0.5625},
121+
}
122+
},
123+
{
124+
shadow =
125+
{
126+
red = {0.75, -0.15625},
127+
green = {0.75, 0.25},
128+
},
129+
wire =
130+
{
131+
red = {0.46875, -0.5},
132+
green = {0.46875, -0.09375},
133+
}
134+
},
135+
{
136+
shadow =
137+
{
138+
red = {0.75, 0.5625},
139+
green = {0.21875, 0.5625}
140+
},
141+
wire =
142+
{
143+
red = {0.28125, 0.15625},
144+
green = {-0.21875, 0.15625}
145+
}
146+
},
147+
{
148+
shadow =
149+
{
150+
red = {-0.03125, 0.28125},
151+
green = {-0.03125, -0.125},
152+
},
153+
wire =
154+
{
155+
red = {-0.46875, 0},
156+
green = {-0.46875, -0.40625},
157+
}
158+
}
159+
},
160+
161+
circuit_wire_max_distance = 7.5
162+
},
3163
{
4164
type = "constant-combinator",
5165
name = "circuit-network-switch-proxy",
@@ -177,11 +337,11 @@ data:extend({
177337
type = "electric",
178338
usage_priority = "secondary-input"
179339
},
180-
energy_usage_per_tick = "5KW",
181-
light = {intensity = 0.9, size = 40},
182-
light_when_colored = {intensity = 1, size = 6},
183-
glow_size = 6,
184-
glow_color_intensity = 0.135,
340+
energy_usage_per_tick = "1KW",
341+
--light = {intensity = 0.9, size = 40},
342+
--light_when_colored = {intensity = 1, size = 6},
343+
--glow_size = 6,
344+
--glow_color_intensity = 0.135,
185345
picture_off =
186346
{
187347
filename = "__base__/graphics/entity/small-lamp/light-off.png",

info.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
{
22
"name": "circuit-network-switch",
3-
"version": "0.1.2",
3+
"version": "0.1.3",
44
"factorio_version": "0.14",
55
"title": "Circuit Network Switch",
66
"author": "TheRustyKnife",
77
"description": "Like the power switch but with red and green wires. Proof-of-concept stage - looking for feedback!",
88
"homepage": "http://mods.factorio.com/mods/theRustyKnife/circuit-network-switch",
9-
"date": "22.09.2016",
9+
"date": "07.10.2016",
1010
"dependencies": []
1111
}

script/events.lua

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ function on_built(event)
44
if event.created_entity.name == "circuit-network-switch"then
55
local res = {
66
switch = event.created_entity,
7-
proxies = util.create_proxies(event.created_entity)
7+
proxies = util.create_proxies(event.created_entity),
8+
state = true,
9+
control_behavior = switch.get_or_create_control_behavior()
810
}
911
table.insert(global.switches, res)
1012
end
@@ -21,11 +23,21 @@ function on_destroyed(event)
2123
end
2224

2325
local method = {[true] = 'disconnect_neighbour', [false] = 'connect_neighbour'}
26+
local red_args = {wire = defines.wire_type.red}
27+
local green_args = {wire = defines.wire_type.green}
2428
script.on_event(defines.events.on_tick, function(event)
29+
local state
2530
for _, v in pairs(global.switches) do
26-
local state = v.switch.get_or_create_control_behavior().disabled
27-
v.proxies[1][method[state]]{target_entity = v.proxies[2], wire = defines.wire_type.red}
28-
v.proxies[1][method[state]]{target_entity = v.proxies[2], wire = defines.wire_type.green}
31+
state = v.control_behavior.disabled
32+
if state ~= v.state then
33+
v.state = state
34+
35+
red_args.target_entity = v.proxies[2].inner
36+
green_args.target_entity = v.proxies[2].inner
37+
38+
v.proxies[1].inner[method[state]](red_args)
39+
v.proxies[1].inner[method[state]](green_args)
40+
end
2941
end
3042
end)
3143

script/migration.lua

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,30 @@
1+
version = require "semver"
2+
util = require "util"
3+
14
local migration = {}
25

36
function migration.init()
47
if type(global.switches) ~= "table" then global.switches = {} end
58
end
69

10+
function migration.migrate(data)
11+
if data.mod_changes["circuit-network-switch"] and data.mod_changes["circuit-network-switch"].old_version then
12+
local old_version = version(data.mod_changes["circuit-network-switch"].old_version)
13+
if old_version < version"0.1.3" then
14+
for i, v in pairs(global.switches) do
15+
v.proxies[1].destroy()
16+
v.proxies[2].destroy()
17+
18+
local res = {
19+
switch = v.switch,
20+
proxies = util.create_proxies(v.switch),
21+
state = true,
22+
control_behavior = v.switch.get_or_create_control_behavior()
23+
}
24+
global.switches[i] = res
25+
end
26+
end
27+
end
28+
end
29+
730
return migration

0 commit comments

Comments
 (0)