generated from fgardt/factorio-mod-template
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathshared.lua
171 lines (149 loc) · 4.7 KB
/
shared.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
-- shared code between memory units and fluid memory units
local min = math.min
local floor = math.floor
local function compactify(n)
n = floor(n)
local suffix = 1
local new
while n >= 1000 do
new = floor(n / 100) / 10
if n == new then
return {"big-numbers.infinity"}
else
n = new
end
suffix = suffix + 1
end
if suffix ~= 1 and floor(n) == n then n = tostring(n) .. ".0" end
return {"big-numbers." .. suffix, n}
end
local function open_inventory(player)
if not storage.blank_gui_item then
local inventory = game.create_inventory(1)
inventory[1].set_stack("blank-gui-item")
inventory[1].allow_manual_label_change = false
storage.empty_gui_item = inventory[1]
end
player.opened = nil
player.opened = storage.empty_gui_item
return player.opened
end
local function update_display_text(unit_data, entity, localised_string)
if unit_data.text then
local render_object = rendering.get_object_by_id(unit_data.text)
if render_object then
render_object.text = localised_string
return
end
end
unit_data.text = rendering.draw_text {
surface = entity.surface,
target = entity,
text = localised_string,
alignment = "center",
scale = 1.5,
only_in_alt_mode = true,
color = {r = 1, g = 1, b = 1}
}.id
end
local function update_combinator(combinator, signal, count)
local control = combinator.get_or_create_control_behavior()
local count = min(2147483647, count)
control.get_section(1).set_slot(1, {
value = signal,
min = count,
max = count,
})
end
local power_usages = {
["0W"] = 0,
["60kW"] = 1000,
["180kW"] = 3000,
["300kW"] = 5000,
["480kW"] = 8000,
["600kW"] = 10000,
["1.2MW"] = 20000,
["2.4MW"] = 40000,
["3.6MW"] = 40000 / 2.4 * 3.6,
["5MW"] = 40000 / 2.4 * 5,
["10MW"] = 40000 / 2.4 * 10,
["20MW"] = 40000 / 2.4 * 20,
["50MW"] = 40000 / 2.4 * 50,
}
local base_usage = 1000000 / 60
local function update_power_usage(unit_data, count)
local powersource = unit_data.powersource
local power_usage = (math.ceil(count / (unit_data.stack_size or 1000)) ^ 0.35) * power_usages[settings.global["memory-unit-power-usage"].value]
power_usage = power_usage + base_usage
powersource.power_usage = power_usage
powersource.electric_buffer_size = power_usage
end
local update_rate = 15
local update_slots = 4
local function has_power(powersource, entity)
if powersource.energy < powersource.electric_buffer_size * 0.9 then
if powersource.energy ~= 0 then
rendering.draw_sprite {
sprite = "utility.electricity_icon",
x_scale = 0.5,
y_scale = 0.5,
target = entity,
surface = entity.surface,
time_to_live = 30
}
end
return false
end
return not entity.to_be_deconstructed()
end
local basic_item_types = {["item"] = true, ["capsule"] = true, ["gun"] = true, ["rail-planner"] = true, ["module"] = true}
local function check_for_basic_item(item)
local items_with_metadata = storage.items_with_metadata
if not items_with_metadata then
items_with_metadata = {}
for item_name, prototype in pairs(prototypes.item) do
if not basic_item_types[prototype.type] then
items_with_metadata[item_name] = true
end
end
storage.items_with_metadata = items_with_metadata
end
return not items_with_metadata[item]
end
local function memory_unit_corruption(unit_number, unit_data)
local entity = unit_data.entity
local powersource = unit_data.powersource
local combinator = unit_data.combinator
if entity.valid then entity.destroy() end
if powersource.valid then powersource.destroy() end
if combinator.valid then combinator.destroy() end
game.print {"memory-unit-corruption", unit_data.count, unit_data.item or "nothing"}
storage.units[unit_number] = nil
end
local function validity_check(unit_number, unit_data, force)
if not unit_data.entity.valid or not unit_data.powersource.valid or not unit_data.combinator.valid then
memory_unit_corruption(unit_number, unit_data)
return true
end
if not force and not has_power(unit_data.powersource, unit_data.entity) then return true end
return false
end
local function combine_tempatures(first_count, first_tempature, second_count, second_tempature)
if first_tempature == second_tempature then return first_tempature end
local total_count = first_count + second_count
return (first_tempature * first_count / total_count) + (second_tempature * second_count / total_count)
end
return {
update_display_text = update_display_text,
update_combinator = update_combinator,
has_power = has_power,
update_power_usage = update_power_usage,
update_rate = update_rate,
update_slots = update_slots,
compactify = compactify,
open_inventory = open_inventory,
check_for_basic_item = check_for_basic_item,
memory_unit_corruption = memory_unit_corruption,
validity_check = validity_check,
combine_tempatures = combine_tempatures
}