Differences From
Artifact [5046584198]:
- File
lib/tbl.lua
— part of check-in
[72eebac4bc]
at
2020-09-26 18:49:51
on branch trunk
— add writing stand for editing codexes; add scissors, ink, erasure fluid, pens; touch up codex UI; add many recipe notes; add craft divination type for crafttools; defuckulate fucktarded crafttool impl; enhance table library with missing features like lua's table.unpack; many bug fixes and enhancements; blood for the blood god
(user:
lexi,
size: 1805)
[annotate]
[blame]
[check-ins using]
- File
lib/tbl.lua
— part of check-in
[3f6a913e4e]
at
2020-09-29 12:40:28
on branch trunk
— * remove former hacky registration system, replace with consistent and flexible API; rewrite metal/gem generation to take advantage of this new API; tweaks to init system to enable world-local tweaks to lore and sorcery behavior
* initial documentation commit
* initial steps towards calendar - add default date format, astrolabe; prepare infra for division/melding/transmutation spells, various tweaks and fixes
(user:
lexi,
size: 2543)
[annotate]
[blame]
[check-ins using]
15 15
16 16 fn.copy = function(t)
17 17 local new = {}
18 18 for i,v in pairs(t) do new[i] = v end
19 19 setmetatable(new,getmetatable(t))
20 20 return new
21 21 end
22 +
23 +fn.deepcopy = table.copy or function(t)
24 + new = {}
25 + for k,v in pairs(t) do
26 + if type(v) == 'table' then
27 + new[k] = fn.deepcopy(v)
28 + else
29 + new[k] = v
30 + end
31 + end
32 + return new
33 +end
22 34
23 35 fn.merge = function(base,override)
24 36 local new = fn.copy(base)
25 37 for k,v in pairs(override) do
26 38 new[k] = v
39 + end
40 + return new
41 +end
42 +
43 +fn.deepmerge = function(base,override)
44 + local new = {}
45 + local keys = fn.merge(fn.keys(base),fn.keys(override))
46 + for _,k in pairs(keys) do
47 + if type(base[k]) == 'table' and
48 + type(override[k]) == 'table' then
49 + new[k] = fn.deepmerge(base[k], override[k])
50 + elseif override[k] then
51 + new[k] = override[k]
52 + else
53 + new[k] = base[k]
54 + end
27 55 end
28 56 return new
29 57 end
30 58
31 59 fn.append = function(r1, r2)
32 60 local new = fn.copy(r1)
33 61 for i=1,#r2 do
................................................................................
105 133 else
106 134 for i=0,#tbl do
107 135 acc = fn(acc,tbl[i],i)
108 136 end
109 137 end
110 138 return acc
111 139 end
140 +
141 +fn.walk = function(tbl,path)
142 + if type(path) == 'table' then
143 + for _,p in pairs(path) do
144 + if tbl[p] == nil then return nil end
145 + tbl = tbl[p]
146 + end
147 + else
148 + tbl = tbl[path]
149 + end
150 + return tbl
151 +end
112 152
113 153 return fn