-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimberborndrawing.lua
More file actions
367 lines (332 loc) · 9.02 KB
/
timberborndrawing.lua
File metadata and controls
367 lines (332 loc) · 9.02 KB
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
--- Timberborn TikZ graph generator.
--
-- This module is used from LuaLaTeX to generate TikZ code describing
-- production graphs for the game *Timberborn*. The module reads a JSON
-- description of buildings ("huts"), their input/output resources, and
-- their production duration. From this it computes resource flow rates
-- and prints the corresponding TikZ nodes and edges.
-- The module is intended to be used through `\directlua` calls in LaTeX.
local function pr(...)
tex.print(...)
-- print(...)
end
-----------
-- UTILS --
-----------
--- Return the keys of a table.
-- @tparam table t table to extract keys from
-- @treturn table list of keys
local function getKeys(t)
local keys={}
for key,_ in pairs(t) do
table.insert(keys, key)
end
return keys
end
--- Check whether any element satisfies a predicate.
--
-- @tparam table ls list
-- @tparam function pred predicate
-- @treturn boolean
local function any(ls, pred)
for _, l in ipairs(ls) do
if pred(l) then
return true
end
end
return false
end
--- Duration broken into components.
-- Produced by @{parseDur}.
--
-- @table Duration
-- @field days number days
-- @field hours number hours
-- @field mins number minutes
-- @field secs number seconds
--- Parse a duration string.
--
-- Supported format is a concatenation of
--
-- * `d` - days
-- * `h` - hours
-- * `m` - minutes
-- * `s` - seconds
--
-- Example: `"1d2h30m"`
--
-- @tparam string s duration string
-- @treturn Duration parsed duration
local function parseDur(s)
local days = s:gsub('d.*', '')
local hours = s:gsub('.*%f[^d]', ''):gsub('%f[h].*', '')
local mins = s:gsub('.*%f[^h]', ''):gsub('%f[m].*', '')
local secs = s:gsub('.*%f[^m]', ''):gsub('%f[s].*', '')
return {
days = tonumber(days) or 0,
hours = tonumber(hours) or 0,
mins = tonumber(mins) or 0,
secs = tonumber(secs) or 0,
}
end
local function durInMin(s)
local x = parseDur(s)
return
x.days * 24*60 +
x.hours * 60 +
x.mins +
x.secs / 60
end
-- local function dur_in_hour(s)
-- return dur_in_min(s) / 60
-- end
------------
-- MODULE --
------------
-- @module timberborndrawing
local _M = {}
--- Reference to a resource used by a hut.
--
-- @table ResourceRef
-- @field name string normalized resource name (internal node id)
-- @field cnt number amount of the resource used/produced
--- Representation of a production building.
--
-- Loaded from the JSON input file.
--
-- @table Hut
-- @field cnt number number of buildings
-- @field dur string production duration string (e.g. `"1h30m"`)
-- @field durMin number duration in minutes
-- @field durUnit string unit used for displaying rates
-- @field i ResourceRef[] input resources
-- @field o ResourceRef[] output resources
-- @field recipe string|nil optional recipe identifier
--- Loaded huts indexed by hut name.
-- @type table<string,Hut>
local huts = {}
--- Representation of a resource node in the graph.
--
-- A resource connects huts that produce and consume it.
--
-- @table Resource
-- @field name string display name of the resource
-- @field huts string[] list of huts using the resource
-- @field underline boolean whether the label should be underlined
-- @field node string additional TikZ node options
-- @field hide_empty_io boolean whether empty IO should be hidden
--- Loaded resources indexed by normalized resource name.
-- @type table<string,Resource>
local resources = {}
--- Reset internal state.
--
-- Clears all loaded huts and resources. This must be called before
-- loading a new JSON description.
--
-- @function reset
function _M.reset()
huts = {}
resources = {}
end
--- Register the usage of a resource.
--
-- Internal helper that connects a resource to a hut and records
-- the input/output count.
--
-- @tparam string res resource name
-- @tparam number cnt amount of resource
-- @tparam string h_name hut name
-- @tparam ResourceRef[] dict input/output list
-- @tparam[opt=false] boolean hide_empty_io mark resource as optional
local function addRes(res, cnt, h_name, dict, hide_empty_io)
local norm_name = res.."-r"
table.insert(dict, {name=norm_name, cnt=cnt})
if not resources[norm_name] then
resources[norm_name] = {name=res, huts={}, underline=false, node="", hide_empty_io=hide_empty_io}
end
table.insert(resources[norm_name].huts, h_name)
end
local json = require "json"
--- Load a JSON description of huts.
--
-- The JSON file must contain an array of hut objects.
--
-- Example structure:
--
-- ```json
-- [
-- {
-- "name": "Lumber Mill",
-- "cnt": 2,
-- "dur": "1h",
-- "inputs": {"log": 1},
-- "outputs": {"plank": 1}
-- }
-- ]
-- ```
--
-- @tparam string path path to the JSON file
-- @function loadJSON
function _M.loadJSON(path)
local f = assert(io.open(path, "r"))
local content = f:read("*a")
f:close()
local data = json.decode(content)
for _, h in ipairs(data) do
local h_name = string.gsub(h.name or "", "%.", "-")
-- build hut entry
local x = {
cnt = h.cnt or 0,
dur = h.dur or "0s",
durMin = durInMin(h.dur or "0s"),
durUnit = "1h",
i = {},
o = {},
recipe = h.recipe
}
-- add inputs
for res, cnt in pairs(h.inputs or {}) do
if res == "anyOf" then
for res, cnt in pairs(cnt) do
addRes(res, cnt, h_name, x.i, true)
end
else
addRes(res, cnt, h_name, x.i)
end
end
-- add outputs
for res, cnt in pairs(h.outputs or {}) do
if res == "anyOf" then
for res, cnt in pairs(cnt) do
addRes(res, cnt, h_name, x.o, true)
end
else
addRes(res, cnt, h_name, x.o)
end
end
-- insert to huts
huts[h_name] = x
end
end
--- Resource flow rates.
--
-- Returned by @{getResRate}.
--
-- @table Rate
-- @field i number production rate
-- @field o number consumption rate
-- @field left number net production (`i - o`)
--- Compute the production/consumption rate of a resource.
--
-- @tparam string self resource identifier
-- @tparam string[] hs huts using the resource
-- @tparam string durUnit target duration unit
-- @treturn Rate
local function getResRate(self, hs, durUnit)
local res = {i=0, o=0}
for _, h in pairs(hs) do
-- collect producer rate
for _, r in ipairs(huts[h].o) do
if r.name == self then
res.i = res.i + (r.cnt * huts[h].cnt / (huts[h].durMin / durInMin(durUnit or "1h")))
end
end
-- collect consumer rate
for _, r in ipairs(huts[h].i) do
if r.name == self then
res.o = res.o + (r.cnt * huts[h].cnt / (huts[h].durMin / durInMin(durUnit or "1h")))
end
end
end
res.left = res.i - res.o
return res
end
--- Draw resource nodes.
--
-- Internal helper used by @{drawAll}.
--
-- @tparam table<string,boolean> nodes set of already drawn nodes (mostly output argument)
local function drawResourceNodes(nodes)
local rs = getKeys(resources)
table.sort(rs)
for _,name in ipairs(rs) do
local r = resources[name]
if r.huts and any(r.huts, function(x) return huts[x].cnt > 0 end) then
local rates = getResRate(name, r.huts, "1h")
if not (rates.i == 0 and rates.o == 0) then
local stateKey
if rates.left < 0 then
stateKey = "/timberborndrawing/resource/neg"
else
stateKey = "/timberborndrawing/resource/pos"
end
pr(
([[\node[/timberborndrawing/resource/node,%s,%s] (%s) {%s (in $\frac{%.2f}{\text{%s}}$ / out $\frac{%.2f}{\text{%s}}$ $\to$ left $\frac{%.2f}{\text{%s}}$)};]]):format(
stateKey,
r.node,
name,
r.underline and ([[\underline{%s}]]):format(r.name) or r.name,
rates.i, "1h",
rates.o, "1h",
rates.left, "1h"
))
nodes[name] = true
end
end
end
end
--- Draw the complete production graph.
--
-- Generates TikZ nodes and edges for all loaded huts and resources.
--
-- @tparam boolean showCnt if true, show building multiplicity in rate formulas
-- @function drawAll
function _M.drawAll(showCnt)
local nodes = {}
drawResourceNodes(nodes)
local hs = getKeys(huts)
table.sort(hs)
for _,name in ipairs(hs) do
local h = huts[name]
if h.recipe then
name = name.."-"..h.recipe
end
if h.cnt <= 0 and showCnt then
goto continue_huts
end
pr(
([[\node [/timberborndrawing/hut/node] (%s) {%s};]]):format(
name,
name
))
nodes[name] = true
for _,r in ipairs(h.i) do
if nodes[r.name] and nodes[name] then
pr(
([[\path (%s) edge[/timberborndrawing/every edge, /timberborndrawing/edgeIn] node[align=left,] {$\frac{%s}{\text{%s}} =$\\[.75ex]$\frac{%.2f}{\text{%s}}$} (%s);]]):format(
r.name,
showCnt and ([[%.2f \times %.2f]]):format(r.cnt, h.cnt) or ([[%d]]):format(r.cnt), h.dur,
(showCnt and h.cnt or 1) * r.cnt / (h.durMin / durInMin(h.durUnit)) ,
h.durUnit,
name
)
)
end
end
for _,r in ipairs(h.o) do
if nodes[r.name] and nodes[name] then
pr(
([[\path (%s) edge[/timberborndrawing/every edge, /timberborndrawing/edgeOut] node[align=left,] {$\frac{%s}{\text{%s}} =$\\[.75ex]$\frac{%.2f}{\text{%s}}$} (%s);]]):format(
name,
showCnt and ([[%.2f \times %.2f]]):format(r.cnt, h.cnt) or ([[%d]]):format(r.cnt), h.dur,
(showCnt and h.cnt or 1) * r.cnt / (h.durMin / durInMin(h.durUnit)) ,
h.durUnit,
r.name
)
)
end
end
::continue_huts::
end
end
return _M