-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchecks.lua
173 lines (167 loc) · 5.85 KB
/
checks.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
require("consts")
require("log")
function CHECK_IO(entity)
local errors = {}
if IO_SEPARATED[entity.get_control_behavior().type] ~= nil then
local has_input = false
local has_output = false
for _, wire in pairs(WIRES) do
if entity.get_circuit_network(wire, defines.circuit_connector_id.combinator_input) ~= nil then
has_input = true
end
if entity.get_circuit_network(wire, defines.circuit_connector_id.combinator_output) ~= nil then
has_output = true
end
end
if not has_input then
table.insert(errors, "message.no_input_wires")
end
if not has_output then
table.insert(errors, "message.no_output_wires")
end
end
return errors
end
local function is_input_matched(input, outputs)
if SIGNALS_MATCH_ANYTHING[input] then
return true
end
for signal, _ in pairs(SIGNALS_MATCH_ANYTHING) do
if outputs[signal] then
return true
end
end
-- if blank, virtual, fluid, item do not warn
if input == "blank" or TYPES[input] then
return true
end
if outputs[input] ~= nil then
return true
end
-- if output contains any-item and input is an item. For example, chest -> combinator with condition on iron plates
if outputs["any-item"] ~= nil and GET_SIGNAL_TYPE(input) == "item" then
return true
end
-- if output contains any-fluid and input is a fluid. For example, storage tank -> combinator with condition on lubricant.
if outputs["any-fluid"] ~= nil and GET_SIGNAL_TYPE(input) == "fluid" then
return true
end
-- if output contains concrete item and input is any item. For example, constant combinator -> set filter in inserter.
if outputs["item"] ~= nil and input == "any-item" then
return true
end
return false
end
local function is_output_matched(output, inputs)
-- if output is signal-each, signal-anything, signal-everything
if SIGNALS_MATCH_ANYTHING[output] then
return true
end
-- if inputs contain signal-each, signal-anything, signal-everything
for signal, _ in pairs(SIGNALS_MATCH_ANYTHING) do
if inputs[signal] then
return true
end
end
-- if blank, virtual, fluid or item, do not warn
if output == "blank" or TYPES[output] then
return true
end
-- if inputs contain exactly output
if inputs[output] ~= nil then
return true
end
-- if inputs have any-item, and output is concrete item, for example, constant-combinator -> inserter with "Set filter" mode
if inputs["any-item"] ~= nil and GET_SIGNAL_TYPE(output) == "item" then
return true
end
-- No check for any-fluid, since nothing takes any fluid as input
-- if output is any-item and input contain some concrete item. For example, chest -> combinator with condition on iron plates
if output == "any-item" and inputs["item"] ~= nil then
return true
end
-- if output is any-fluid and input contains a fluid. For example, storage tank -> combinator with condition on lubricant.
if output == "any-fluid" and inputs["fluid"] ~= nil then
return true
end
-- if output is color and input contains "color" (lamp with "Use colors" mode)
if COLORS[output] and inputs["color"] ~= nil then
return true
end
return false
end
local function contain_color(outputs)
for signal, _ in pairs(outputs) do
if COLORS[signal] or SIGNALS_MATCH_ANYTHING[signal] then
return true
end
end
return false
end
local function check_input(signal, connected_networks, networks)
local matched = false
for network_id, _ in pairs(connected_networks.input) do
if is_input_matched(signal, networks[network_id].output) then
matched = true
end
end
return matched
end
local function check_color(connected_networks, networks)
local matched = false
for network_id, _ in pairs(connected_networks.input) do
if contain_color(networks[network_id].output) then
matched = true
end
end
return matched
end
function CHECK_ENTITY(entity, entity_inputs, entity_outputs, networks)
local errors = {}
-- get list of connected networks to entity
local connected_networks = GET_CONNECTED_NETWORKS(entity)
LOG("connected_networks: " .. serpent.line(connected_networks))
-- check inputs
for signal, _ in pairs(entity_inputs) do
-- check if signal is matched at at least one network
if signal == "color" then
if not check_color(connected_networks, networks) then
table.insert(errors,
{
level="I",
msg="message.no_color_for_use_colors"
})
end
else
if not signal == "blank" and not check_input(signal, connected_networks, networks) then
table.insert(errors,
{
level="E",
msg="message.input_required",
params={GET_PRETTY_SIGNAL(signal)}
})
end
end
end
-- check outputs
for signal, _ in pairs(entity_outputs) do
LOG(signal)
-- check if signal is matched at at least one network
local matched = false
for network_id, _ in pairs(connected_networks.output) do
if is_output_matched(signal, networks[network_id].input) then
matched = true
end
end
-- if not matched, create warning.
if not matched and not signal == 'blank' then
table.insert(errors,
{
level="W",
msg="message.output_unused",
params={GET_PRETTY_SIGNAL(signal)}
})
end
end
return errors
end