forked from clarabstract/factorio-calc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfactorio.lua
157 lines (144 loc) · 4.39 KB
/
factorio.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
data = {}
local FACTORIO_BASE_PATH = "C:\\Program Files\\Factorio"
package.path = package.path .. ";" .. FACTORIO_BASE_PATH .. "\\data\\?.lua"
package.path = package.path .. ";" .. FACTORIO_BASE_PATH .. "\\data\\base\\?.lua"
package.path = package.path .. ";" .. FACTORIO_BASE_PATH .. "\\data\\core\\lualib\\?.lua"
require('dataloader')
require('base.data')
-- Print contents of `tbl`, with indentation.
-- `indent` sets the initial level of indentation.
function tprint (tbl, indent)
if not indent then indent = 0 end
for k, v in pairs(tbl) do
formatting = string.rep(" ", indent) .. k .. ": "
if type(v) == "table" then
print(formatting)
tprint(v, indent+1)
else
print(formatting .. tostring(v))
end
end
end
function get_recipe(name)
local rdata = data.raw.recipe[name]
local recipe = {}
if not rdata then
return rdata
end
recipe.name = rdata.name
recipe.time = rdata.energy_required or 0.5
if rdata.category == 'smelting' then
-- recipe.time = recipe.time / 2
end
recipe.outputs = rdata.result_count or 1
recipe.ips = recipe.outputs / recipe.time
recipe.inputs = {}
for i, ingr in ipairs(rdata.ingredients) do
local ingredient = {}
if ingr.name then
ingredient.name = ingr.name
ingredient.amount = ingr.amount
else
ingredient.name = ingr[1]
ingredient.amount = ingr[2]
end
table.insert(recipe.inputs, ingredient)
end
return recipe
end
local BELT_THROUGHPUT = 14.2 -- wiki measure is 14.8, but mine is closer to 14
function request(name, ips)
local recipe = get_recipe(name)
if not recipe then
return {name = name, ips=ips, lines_required=(BELT_THROUGHPUT/ips)}
end
local req = {}
req.name = recipe.name
req.ips = ips
req.ipspa = recipe.ips
req.assemblers = req.ips / recipe.ips
req.assembler_max_line = BELT_THROUGHPUT / recipe.ips
req.lines_required = req.assemblers / math.floor(req.assembler_max_line)
req.cycle_time = recipe.time
req.inputs = {}
for i, input in ipairs(recipe.inputs) do
local ingr_per_cycle = input.amount * req.assemblers
local ingr_required_ips = ingr_per_cycle / req.cycle_time
table.insert(req.inputs,
request(input.name, ingr_required_ips))
end
return req
end
function write_req(file, req, depth)
file:write('<tr>')
file:write('<th style="padding-left: ' .. (10 * depth)..'px;">')
file:write(req.name)
file:write('</th>')
file:write('<td>')
file:write(string.format("%.3f",req.ips))
file:write('/s')
file:write('<td>')
file:write(string.format("%.3f",1/req.ips))
file:write('s')
file:write('</td>')
if req.assemblers then
file:write('<td>')
file:write(string.format("%.3f",req.assemblers))
file:write('</td>')
file:write('<td>')
file:write(string.format("%.3f",req.lines_required))
file:write('</td>')
file:write('<td>')
file:write(req.assembler_max_line)
file:write('</td>')
file:write('<td>')
file:write(req.cycle_time)
file:write('s')
file:write('</td>')
file:write('<td>')
file:write(string.format("%.3f",req.ipspa))
file:write('</td>')
file:write('</tr>')
file:write('<tr class="inputs">')
file:write('<th colspan="8" style="padding-left: ' .. (10 * depth)..'px;">')
file:write('Inputs:</th>')
for i,ingr in ipairs(req.inputs) do
write_req(file, ingr, depth + 1)
end
end
file:write('</tr>')
end
function html_report(file_name, req)
file = io.open(file_name, 'w')
file:write('<html>')
file:write('<head>')
file:write('<style>')
file:write('td, th { padding: 3px; border: 1px solid #ccc; margin: 0;}')
file:write('table { border-spacing: 0; border-collapse: collapse;}')
file:write('td { text-align: right;}')
file:write('tbody th { text-align: left;}')
file:write('tbody { font-family: monospace;}')
file:write('.inputs { font-size:smaller; color:#999;}')
file:write('</style>')
file:write('</head>')
file:write('<body>')
file:write('<table>')
file:write('<thead>')
file:write('<tr>')
file:write('<th>Recipe</th>')
file:write('<th>items / <br/> second</th>')
file:write('<th>seconds /<br/> item</th>')
file:write('<th>Assemblers <br />Required</th>')
file:write('<th>Lines<br /> Required</th>')
file:write('<th>Max Assembly <br />Line Length</th>')
file:write('<th>Assembly<br />Cycle</th>')
file:write('<th>i/s/a</th>')
file:write('</tr>')
file:write('</thead>')
file:write('<tbody>')
write_req(file, req, 0)
file:write('</tbody></table>')
file:write('</body></html>')
file:close()
end
html_report('factorio.html', request('science-pack-1', 1))