-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathutil.lua
More file actions
executable file
·519 lines (474 loc) · 11.2 KB
/
Copy pathutil.lua
File metadata and controls
executable file
·519 lines (474 loc) · 11.2 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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
-- util
local CC = require 'cc'
local log = require 'log'
local Util = {}
---@param A number
---@param B number
---@param v number
---@return number
function Util.smoothstep(A, B, v)
-- see http://sol.gfxile.net/interpolation/
if v > 1.0 then v = 1.0 end
v = (v) * (v) * (3.0 - 2.0 * (v));
return (B * v) + (A * (1.0 - v));
end
---@param A number
---@param B number
---@param v number
---@return number
function Util.smootherstep(A, B, v)
-- see http://sol.gfxile.net/interpolation/
if v > 1.0 then v = 1.0 end
v = (v) * (v) * (v) * ((v)*((v) * 6.0 - 15.0) + 10.0);
return (B * v) + (A * (1.0 - v));
end
---@param start number
---@param finish number
---@param factor number
---@return number
function Util.lerp(start, finish, factor)
-- return start*(1-factor) + finish*factor
-- Precise method, which guarantees start = finish when factor = 1.
-- https://en.wikipedia.org/wiki/Linear_interpolation
if factor > 1.0 then factor = 1.0 end
return (1 - factor) * start + factor * finish;
end
---The opposite of lerp. Instead of a range and a factor, we give a range and a value to find out the factor.
---@param start number
---@param finish number
---@param value number
---@return number
function Util.normalize(start, finish, value)
return (value - start) / (finish - start)
end
---Converts a value from the scale [fromMin, fromMax] to a value from the scale[toMin, toMax]. It’s just the normalize and lerp functions working together.
---@param value number
---@param fromMin number
---@param fromMax number
---@param toMin number
---@param toMax number
---@return number
function Util.mapValue(value, fromMin, fromMax, toMin, toMax)
return Util.lerp(toMin, toMax, Util.normalize(fromMin, fromMax, value))
end
---@param value any
---@param min number
---@param max number
---@return number
function Util.clamp(value, min, max)
return math.min(math.max(value, min), max)
end
---returns the intersection area of two rectangles
---@param x number
---@param y number
---@param w number
---@param h number
---@param X number
---@param Y number
---@param W number
---@param H number
---@return number
function Util.overlapArea(x, y, w, h, X, Y, W, H)
local ox = math.max(0, math.min(x + w, X + W) - math.max(x, X));
local oy = math.max(0, math.min(y + h, Y + H) - math.max(y, Y));
return ox * oy;
end
---finds the length of the hypotenuse between two points
---@param x number
---@param y number
---@param X number
---@param Y number
---@return number
function Util.distance(x, y , X, Y)
local first = math.pow(X - x, 2)
local second = math.pow(Y - y, 2)
return math.sqrt(first + second)
end
---@param a number
---@param b number
---@param d number
---@return boolean
function Util.nearEnough(a, b, d)
return math.abs(a-b) < d
end
--[[
function Util.rectContains(X, Y, W, H, x, y, w, h)
if x < X or y < Y then
return false
end
if x + w > X + W then
return false
end
if y + h > Y + H then
return false
end
return true
end
]]
---@param x number
---@param y number
---@param rx number
---@param ry number
---@param rw number
---@param rh number
---@return boolean
function Util.inRect(x, y, rx, ry, rw, rh)
return x >= rx and y >= ry and x < (rx + rw) and y < (ry + rh)
end
--[[
due to the lack of bit operators in Lua 5.1, it's tricky to use CRC32 to compare
two baizes for any changes. So, we just use two tables that record the length of
each pile
TODO LÖVE uses LuaJIT, which includes bit library
]]
---compares two baize snapshots, these being arrays of the length of each pile
---@param old integer[]
---@param new integer[]
---@return boolean
function Util.baizeChanged(old, new)
if (not old) or (not new) then
log.trace('nil passed to baizeChanged')
return true
end
-- assert(type(old)=='table')
-- assert(type(new)=='table')
-- assert(#old==#new)
if #old ~= #new then
return true -- shouldn't ever happen; we don't change number of piles mid-game
end
for i = 1, #old do
if old[i] ~= new[i] then return true end
end
return false
end
---@param s string
---@return number
---@return number
---@return number
function Util.getColorFromSetting(s)
local setting = _G.SETTINGS[s]
if not setting then
log.error('No setting called', s)
return 0.5, 0.5, 0.5
end
local col = _G.LSOL_COLORS[setting]
if not col then
log.error('No color for setting', s)
return 0.5, 0.5, 0.5
end
return love.math.colorFromBytes(unpack(col))
end
---@param s string
---@return nil
function Util.setColorFromSetting(s)
love.graphics.setColor(Util.getColorFromSetting(s))
end
---@param nam string
---@return number
---@return number
---@return number
function Util.getColorFromName(nam)
local col = _G.LSOL_COLORS[nam]
if not col then
log.error('No color named', nam)
return 0.5, 0.5, 0.5
end
return love.math.colorFromBytes(unpack(col))
end
---@param nam string
---@return nil
function Util.setColorFromName(nam)
return love.graphics.setColor(Util.getColorFromName(nam))
end
---@param settingName string
---@param default string
---@param amount number|nil
---@return number[]
---@return number[]
function Util.getGradientColors(settingName, default, amount)
amount = amount or 0.1
local color = _G.SETTINGS[settingName] or default
local r, g, b, a = love.math.colorFromBytes(unpack(_G.LSOL_COLORS[color]))
local amt = amount + 1.0
local frontColor = {r * amt, g * amt, b * amt, a}
amt = 1.0 - amount
local backColor = {r * amt, g * amt, b * amt, a}
return frontColor, backColor
end
---@param backgroundColor string
---@return string
function Util.getForegroundColor(backgroundColor)
if not backgroundColor then
log.error('Unknown color', backgroundColor)
return 'UiForeground'
end
local color = _G.LSOL_COLORS[backgroundColor]
if not color then
log.error('No color', backgroundColor)
return 'UiForeground'
end
local r, g, b = color[1], color[2], color[3]
local foreColor
if r + b + g > 400 then
foreColor = 'UiBackground'
else
foreColor = 'UiForeground'
end
return foreColor
end
---@param tail Card[]
---@return table
function Util.makeCardPairs(tail)
if #tail < 2 then
return {}
end
local cpairs = {}
local c1 = tail[1]
for i = 2, #tail do
local c2 = tail[i]
table.insert(cpairs, {c1, c2})
c1 = c2
end
return cpairs
end
---@param src Pile
---@param dst Pile
---@return Card
function Util.moveCard(src, dst)
local c = src:pop()
if c then
if dst.category == 'Foundation' then
Util.play('move2')
else
Util.play('move1')
end
dst:push(c)
src:flipUpExposedCard()
end
return c
end
---@param src Pile
---@param dst Pile
---@param ord integer
---@return Card|nil
function Util.moveCardByOrd(src, dst, ord)
local c = src:disinterOneCardByOrd(ord)
if c then
return Util.moveCard(src, dst)
else
log.error('Cannot find card', ord)
end
return nil
end
---@param src Pile
---@param dst Pile
---@param ord integer
---@param suit '♣'|'♦'|'♥'|'♠'
---@return Card|nil
function Util.moveCardByOrdAndSuit(src, dst, ord, suit)
local c = src:disinterOneCard(ord, suit)
if c then
return Util.moveCard(src, dst)
else
log.error('Cannot find card', ord, suit)
end
return nil
end
--[[
function Util.moveCards(src, idx, dst)
local tmp = {}
while #src.cards >= idx do
table.insert(tmp, src:pop())
end
if dst.category == 'Foundation' or dst.category == 'Discard' then
Util.play('move2')
elseif #tmp > 1 then
Util.play('move4')
else
Util.play('move1')
end
while #tmp > 0 do
dst:push(table.remove(tmp))
end
src:flipUpExposedCard()
end
]]
---@param card Card
---@param dst Pile
---@return nil
function Util.moveCards2(card, dst)
local tmp = {}
local src = card.parent
while true do
local c = src:pop()
if c == nil then
log.error('Cannot find card', card)
end
table.insert(tmp, c)
if c == card then
break
end
end
if #tmp > 0 then
Util.play('move4')
while #tmp > 0 do
dst:push(table.remove(tmp))
end
end
src:flipUpExposedCard()
end
---@param tail Card[]
---@return table
function Util.findHomesForTail(tail)
local homes = {} -- {dst=<pile>, weight=<number>}
for _, card in ipairs(tail) do
if card.prone then
return homes
end
end
local pileTypesToCheck = {_G.BAIZE.foundations, _G.BAIZE.tableaux, _G.BAIZE.cells}
-- TODO adding discards here didn't move completed tail to discard
local card = tail[1]
local src = card.parent
-- the following two checks should have already been made
-- can the tail be moved in general?
if src:moveTailError(tail) then
log.error('Pile:tailMoveError')
return homes
end
-- is the tail conformant enough to move?
if _G.BAIZE.script:moveTailError(tail) then
log.error('script:tailMoveError')
return homes
end
for _, piles in ipairs(pileTypesToCheck) do
for _, dst in ipairs(piles) do
if dst ~= src then
local err = dst:acceptTailError(tail)
if not err then
local home = {dst=dst, weight=#dst.cards}
if #dst.cards == 0 then
--[[
if #dst.cards == 0 and #tail.tail == #src.cards then
if src.label == dst.label then
if src.category == dst.category then
movable = false
end
end
end
if #tail.tail == #src.cards then
if src.label == dst.label then
if src.category == dst.category then
home = nil
end
end
end
]]
if not dst.label then
-- filling an empty pile can be a poor move
home.weight = 0
end
else
if card.suit == dst:peek().suit then
-- spider
home.weight = home.weight + 26 -- magic number
end
end
if dst.category == 'Foundation' then
home.weight = home.weight + 52 -- magic number
end
table.insert(homes, home)
end
end
end
end
return homes
end
---@param tail Card[]
---@param fn function
---@return integer
function Util.unsortedPairs(tail, fn)
if #tail < 2 then
return 0
end
local unsorted = 0
local cpairs = Util.makeCardPairs(tail)
for _, cpair in ipairs(cpairs) do
if CC.EitherProne(cpair) then
unsorted = unsorted + 1
else
local err = fn(cpair)
if err then
unsorted = unsorted + 1
end
end
end
return unsorted
end
---@param name string
function Util.play(name)
if _G.SETTINGS.muteSounds then
return
end
-- _G.LSOL_SOUNDS[name]:seek(0)
-- love.audio.stop()
if _G.LSOL_SOUNDS[name] then
love.audio.play(_G.LSOL_SOUNDS[name])
else
log.error('no sound for', name)
end
end
---@return string
function Util.orientation()
local _, _, safew, safeh = love.window.getSafeArea()
if safew > safeh then
return 'landscape'
elseif safeh > safew then
return 'portrait'
else
return 'square'
end
end
---@return number
function Util.minFanFactor()
return 0.16666
end
---@return number
function Util.maxFanFactor()
if _G.SETTINGS.simpleCards then
return 0.35
else
return 0.28
end
end
---@param ord number
---@param suit string
---@return string
function Util.cardTextureId(ord, suit)
return string.format('%02u%s', ord, suit)
end
-- function Util.shortStringToOrdinal(ss)
-- for i, s in ipairs({"?", "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"}) do
-- if ss == s then
-- return i
-- end
-- end
-- log.error("Unknown short string", ss)
-- return 0
-- end
---format seconds for status bar and statistics
---@param s number
---@return string
function Util.formatSeconds(s)
if s == 0 then
return '0s'
end
local min = s / 60
local sec = s % 60
if min < 1 then
return string.format('%ds', sec)
else
return string.format('%dm %ds', min, sec)
end
end
return Util