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
|
local module = {}
setmetatable(module, {__index=_G})
setfenv(1, module)
local m = require 'marigold-svg.marigold'
local function merge2(a, b)
local tbl = {}
for k, v in pairs(a) do tbl[k] = v end
for k, v in pairs(b) do tbl[k] = v end
return tbl
end
local function merge_recursive(array, acc)
local tbl = array[1]
table.remove(array, 1)
local acc = merge2(acc, tbl)
if #array == 0 then return acc end
return merge_recursive(array, acc)
end
local function merge(...)
return merge_recursive({...}, {})
end
function capitalize(str)
return string.gsub(str, "^.", string.upper)
end
--===== boxes =====--
Box = {}
function Box.new(_, tbl)
local parent = tbl.parent or { x0=0, y0=0 }
local width = tbl.width or parent.width
local height = tbl.height or parent.width
local offset = tbl.offset or { x=0, y=0 }
local padding = tbl.padding or { left=0, right=0, top=0, bottom=0 }
padding.left = padding.left or 0
padding.right = padding.right or 0
padding.top = padding.top or 0
padding.bottom = padding.bottom or 0
local self = {
x0 = parent.x0 + offset.x + padding.left,
y0 = parent.y0 + offset.y + padding.top,
width = width - padding.left - padding.right,
height = height - padding.top - padding.bottom,
}
setmetatable(self, {__index=Box})
return self
end
setmetatable(Box, {__call=Box.new})
function Box.point(self, x, y)
return x + self.x0, y + self.y0
end
function Box.center(self)
return self.x0 + (self.width/2), self.y0 + (self.height/2)
end
local function hsplit(box, acc, count, settings)
local index = #acc
if index == count then return acc end
local z = box.width / count
table.insert(
acc,
Box(merge({
parent=box,
width=z,
offset= { x=index*z, y=0 }
}, settings))
)
return hsplit(box, acc, count, settings)
end
function Box.hsplit(self, count, settings)
local count = count or 2
local settings = settings or {}
return hsplit(self, {}, count, settings)
end
local function vsplit(box, acc, count, settings)
local index = #acc
if index == count then return acc end
local z = box.height / count
table.insert(
acc,
Box(merge({
parent=box,
height=z,
offset= { y=index*z, x=0 }
}, settings))
)
return vsplit(box, acc, count, settings)
end
function Box.vsplit(self, count, settings)
local count = count or 2
local settings = settings or {}
return vsplit(self, {}, count, settings)
end
function Box.split_down(self, height)
local a = Box{
parent=self,
height=height,
}
local b = Box{
parent=self,
height=self.height - height,
offset={x=0, y=height},
}
return a, b
end
--===== misc =====--
local function stack(...)
local array = {...}
local l
l = function(acc_f, acc_h)
if #array == 0 then return acc_f, acc_h end
local f, h = array[1][1], array[1][2]
table.remove(array, 1)
local acc_f = function(y)
local tbl = acc_f(y)
table.insert(tbl, f(y+acc_h))
return tbl
end
local acc_h = acc_h + h
return l(acc_f, acc_h)
end
local f, h = l(function(y) return {} end, 0)
return function(y) return m.g(f(y)) end, h
end
local function pad(top, bottom, f, h)
return function(y) return f(y+top) end, top + h + bottom
end
local function _divider()
local size = 3
return function(y) return m.rect{
x=0, y=y, width=500, height=size,
style="fill:darkred;",
} end, size
end
local divider = function() return pad(0, 10, _divider()) end
--===== basic stats =====--
local function title(stats)
local size = 40
return function(y)
return m.text(stats.title, {
x=10, y=y+size,
style="font-size:"..size.."px;font-variant-caps:small-caps;stroke:black;fill:darkred;font-family:serif",
})
end, size
end
local function subheading(stats)
local size = 14
return function(y)
return m.text(
string.format("%s %s, %s", capitalize(stats.size), stats.type, stats.alignment),
{
x=10, y=y+size, style="font-size:"..size.."px;font-style:italic;"
}
)
end, size
end
local function header(stats)
local f, h = stack(
{pad(10, 10, title(stats))},
{pad(0, 10, subheading(stats))}
)
return f, h
end
local function _statline(name, value)
local size = 14
return function(y) return m.text(
'<tspan style="font-weight:bold">'..name..'</tspan> '..value,
{ x=10, y=y+size, style="font-size:"..size.."px;fill:darkred;" }
) end, 14
end
local statline = function(name, value) return pad(0, 2, _statline(name, value)) end
local function armor_hp(stats)
local f, h = pad(0, 10, stack(
{statline('Armor Class', stats.ac)},
{statline('Hit Points', stats.hp)},
{statline('Speed', stats.speed)}
))
return f, h
end
local function bonus(raw)
return math.floor( (raw-10)/2 )
end
local function rawbox(name, raw, x, y)
local raw = tonumber(raw)
local value = string.format("%d (%s%d)", raw, (raw<0 and '-') or '+', bonus(raw))
return m.g{
m.text(name, { x=x+10, y=y+15, style="font-weight:bold;font-size:15px;fill:darkred" }),
m.text(value, {x=x, y=y+30, style="font-size:15px;fill:darkred;" }),
}
end
local function raw(stats)
return function(y) return m.g{
rawbox('STR', stats.str, 10, y),
rawbox('DEX', stats.dex, 90, y),
rawbox('CON', stats.con, 170, y),
rawbox('INT', stats.int, 250, y),
rawbox('WIS', stats.wis, 330, y),
rawbox('CHA', stats.cha, 410, y),
} end, 40
end
local function base(stats)
local f, h = stack(
{header(stats)},
{divider()},
{armor_hp(stats)},
{divider()},
{raw(stats)},
{divider()}
)
return f, h
end
--===== attributes =====--
local function attribs(stats)
return function(y) return m.rect{
x=0, y=y, width=500, height=20,
style="fill:blue",
} end, 20
end
--===== actions =====--
local function actions(stats)
return function(y) return m.g{} end, 0
end
--===== draw =====--
function draw(stats)
local f, h = stack({base(stats)}, {attribs(stats)}, {actions(stats)})
return m.render(m.svg{
viewBox = string.format("0 0 500 %d", h),
width = 500,
height = height,
style = "font-family:sans-serif;",
f(0),
})
end
return module
|