-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.lua
820 lines (735 loc) · 27.5 KB
/
main.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
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
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
-- main.lua
local log = require 'log'
local Baize = require 'baize'
local Stats = require 'stats'
local UI = require 'ui'
local Util = require 'util'
local Settings = require 'settings'
_G.LSOL_VERSION = '30'
_G.LSOL_VERSION_DATE = '2024-07-11'
if not _G.table.contains then
function _G.table.contains(tab, val)
for index, value in ipairs(tab) do
if value == val then
return true, index
end
end
return false, 0
end
end
if not _G.string.split then
function _G.string.split (inputstr, sep)
if sep == nil then
sep = '%s'
end
local t = {}
for str in string.gmatch(inputstr, '([^' .. sep ..']+)') do
table.insert(t, str)
end
return t
end
end
_G.LSOL_VARIANTS = {
Accordian = {file='accordian.lua', cc=4},
['Agnes Bernauer'] = {file='agnes.lua', cc=2, bernauer=true},
['Agnes Sorel'] = {file='agnes.lua', cc=4, sorel=true},
Algerian = {file='algerian.lua', easy=true, cc=4},
Alhambra = {file='alhambra.lua', cc=2},
['American Toad'] = {file='amtoad.lua', cc=4},
Athena = {file='klondike.lua', athena=true, cc=2},
Assembly = {file='assembly.lua', cc=1},
Australian = {file='australian.lua', cc=4},
['Baker\'s Dozen'] = {file='bakers.lua', cc=1},
['Baker\'s Dozen (Wide)'] = {file='bakers.lua', wide=true, cc=1, statsName='Baker\'s Dozen'},
['Beleaguered Castle'] = {file='castle.lua', cc=1},
['Busy Aces'] = {file='forty.lua', cc=4, tabs=12, cardsPerTab=1, dealAces=false},
['Flat Castle'] = {file='castle.lua', cc=1, flat=true, statsName='Beleaguered Castle'},
Bisley = {file='bisley.lua', cc=4},
['Black Hole'] = {file='blackhole.lua', cc=1},
Blockade = {file='blockade.lua', cc=4},
-- ['Bisley Debug'] = {file='bisley.lua', cc=4, debug=true},
Canfield = {file='canfield.lua', cc=2},
Cruel = {file='cruel.lua', cc=4, subtype='Cruel'},
Perseverance = {file='cruel.lua', cc=4, subtype='Perseverance'},
['Rainbow Canfield'] = {file='canfield.lua', cc=1, rainbow=true},
['Storehouse Canfield'] = {file='canfield.lua', cc=4, storehouse=true},
Duchess = {file='duchess.lua', cc=2},
-- ['Debug Klon'] = {file='debug.lua', cc=4, spiderLike=false},
-- ['Debug Spid'] = {file='debug.lua', cc=4, spiderLike=true},
['Eight Off'] = {file='eightoff.lua', cc=4},
['Eight Off Relaxed'] = {file='eightoff.lua', cc=4, relaxed=true},
Freecell = {file='freecell.lua', cc=2, bakers=false, relaxed=true},
['Double Freecell'] = {file='freecell.lua', cc=2, relaxed=true, double=true},
['Chinese Freecell'] = {file='freecell.lua', cc=4, relaxed=false, chinese=true},
['Selective Freecell'] = {file='freecell.lua', cc=2, relaxed=true, selective=true},
['Blind Freecell'] = {file='freecell.lua', cc=2, relaxed=true, blind=true},
['Easy Freecell'] = {file='freecell.lua', cc=2, relaxed=true, easy=true},
['Sea Haven Towers'] = {file='seahaven.lua', cc=4},
['Baker\'s Game'] = {file='freecell.lua', bakers=true, cc=4, relaxed=false},
['Baker\'s Game Relaxed'] = {file='freecell.lua', bakers=true, cc=4, relaxed=true},
Gate = {file='gate.lua', cc=2},
Klondike = {file='klondike.lua', cc=2},
Thoughtful = {file='klondike.lua', cc=2, thoughtful=true},
Whitehead = {file='klondike.lua', cc=2, whitehead=true},
Gargantua = {file='klondike.lua', cc=2, gargantua=true},
['Triple Klondike'] = {file='klondike.lua', cc=2, triple=true},
['Klondike (Turn Three)'] = {file='klondike.lua', cc=2, turn=3},
['Forty Thieves'] = {file='forty.lua', cc=4, tabs=10, cardsPerTab=4},
['Forty and Eight'] = {file='forty.lua', cc=4, tabs=10, cardsPerTab=5, recycles=1},
Josephine = {file='forty.lua', cc=4, tabs=10, cardsPerTab=4, josephine=true},
Limited = {file='forty.lua', cc=4, tabs=12, cardsPerTab=3},
Frog = {file='frog.lua', cc=1},
Fly = {file='frog.lua', cc=1, dealAllAces=true},
['Little Spider'] = {file='littlespider.lua', cc=2},
['Little Spider (Fanned)'] = {file='littlespider.lua', cc=2, fanned=true},
Lucas = {file='forty.lua', cc=4, tabs=13, cardsPerTab=3, dealAces=true},
Martha = {file='martha.lua', cc=2},
['Miss Milligan'] = {file='miss milligan.lua', cc=2},
['Mount Olympus'] = {file='mount olympus.lua', cc=4},
Giant = {file='miss milligan.lua', giant=true, cc=2},
Penguin = {file='penguin.lua', cc=4},
['Red and Black'] = {file='redandblack.lua', cc=2},
['Royal Cotillion'] = {file='royal cotillion.lua', cc=4},
Pyramid = {file='pyramid.lua', relaxed=false, cc=2},
['Pyramid Relaxed'] = {file='pyramid.lua', relaxed=true, cc=2},
Rosamund = {file='rosamund.lua', cc=2},
Scorpion = {file='scorpion.lua', cc=4},
Wasp = {file='scorpion.lua', cc=4, relaxed=true},
['Simple Simon'] = {file='simplesimon.lua', cc=4},
Spider = {file='spider.lua', packs=2, cc=4, suitFilter={'♣','♦','♥','♠'}},
['Spider One Suit'] = {file='spider.lua', cc=1, packs=8, suitFilter={'♠'}},
['Spider Two Suits'] = {file='spider.lua', cc=2, packs=4, suitFilter={'♥', '♠'}},
Spiderette = {file='spider.lua', spiderette=true, cc=4, packs=1},
['Spiderette One Suit'] = {file='spider.lua', spiderette=true, cc=1, packs=4, suitFilter={'♠'}},
['Spiderette Two Suits'] = {file='spider.lua', spiderette=true, cc=2, packs=2, suitFilter={'♥', '♠'}},
['Good Thirteen'] = {file='thirteens.lua', packs=1, cc=1},
['Classic Westcliff'] = {file='westcliff.lua', cc=2, classic=true},
['American Westcliff'] = {file='westcliff.lua', cc=2, american=true},
Easthaven = {file='westcliff.lua', cc=2, easthaven=true},
['Tri Peaks'] = {file='tripeaks.lua', cc=2},
['Tri Peaks Open'] = {file='tripeaks.lua', open=true, cc=2},
Yukon = {file='yukon.lua', cc=2},
['Yukon Relaxed'] = {file='yukon.lua', cc=2, relaxed=true},
['Yukon Cells'] = {file='yukon.lua', cc=2, cells=true},
['Russian'] = {file='yukon.lua', cc=4, russian=true},
['Crimean'] = {file='crimean.lua', cc=4, crimean=true},
['Ukrainian'] = {file='crimean.lua', cc=4, ukrainian=true},
['Usk'] = {file='usk.lua', cc=2, relaxed=false},
['Usk Relaxed'] = {file='usk.lua', cc=2, relaxed=true},
['Somerset'] = {file='somerset.lua', cc=2, relaxed=true},
Trefoil = {file='trefoil.lua', cc=4},
['La Belle Lucie'] = {file='labelle.lua', cc=4, merciAllowed=false},
['The Fan'] = {file='thefan.lua', cc=4},
['Three Shuffles and a Draw'] = {file='labelle.lua', cc=4, merciAllowed=true},
}
_G.VARIANT_TYPES = {
-- '> All' and maybe '> Favorites' will automatically be added
['> Animals'] = {'Scorpion','Wasp','Spider One Suit','Spider Two Suits','Spider','Little Spider','Penguin','Frog','Fly'},
['> Canfields'] = {'American Toad','Canfield','Duchess','Gate','Rainbow Canfield','Storehouse Canfield'},
['> Easier'] = {'Accordian','American Toad','American Westcliff','Blockade','Classic Westcliff','Gate','Lucas','Martha','Mount Olympus','Spider One Suit','Red and Black','Tri Peaks','Tri Peaks Open','Wasp','Usk Relaxed','Easy Freecell'},
['> Fan'] = {'La Belle Lucie', 'The Fan', 'Three Shuffles and a Draw', 'Trefoil'},
['> Forty Thieves'] = {'Forty Thieves','Josephine','Limited','Lucas','Forty and Eight','Busy Aces','Red and Black'},
['> Freecells'] = {'Blind Freecell','Easy Freecell','Selective Freecell','Chinese Freecell','Double Freecell', 'Eight Off','Eight Off Relaxed','Freecell','Baker\'s Game','Baker\'s Game Relaxed','Sea Haven Towers'},
['> Klondikes'] = {'Athena','Gargantua','Triple Klondike','Klondike','Klondike (Turn Three)','Easthaven', 'Classic Westcliff','American Westcliff','Agnes Bernauer','Thoughtful','Whitehead'},
['> People'] = {'Agnes Bernauer','Agnes Sorel','Athena','Baker\'s Game','Baker\'s Game Relaxed','Josephine','Martha','Miss Milligan','Rosamund'},
['> Places'] = {'Algerian','Alhambra','Australian','Mount Olympus','Yukon','Yukon Relaxed','Russian','Crimean','Ukrainian','Usk','Usk Relaxed','Somerset'},
['> Popular'] = {'Klondike', 'Forty Thieves','Freecell','Spider','Yukon','Tri Peaks'},
['> Puzzlers'] = {'Beleaguered Castle','Flat Castle','Eight Off','Freecell','Penguin','Simple Simon','Baker\'s Dozen','Baker\'s Dozen (Wide)'},
['> Redealers'] = {'Cruel','Perseverance','Usk','Usk Relaxed'},
['> Spiders'] = {'Spider One Suit','Spider Two Suits', 'Spider','Little Spider','Little Spider (Fanned)','Spiderette','Spiderette One Suit','Spiderette Two Suits'},
}
local function createAllVariants()
local lst = {}
-- local kLongest = ''
for k,_ in pairs(_G.LSOL_VARIANTS) do
table.insert(lst, k)
-- if #k > #kLongest then
-- kLongest = k
-- end
end
-- log.info('Longest variant name is ', kLongest) -- Little Spider (Fanned)
-- sorting happens after widgets are added to types/variants drawers
_G.VARIANT_TYPES['> All'] = lst
-- for k,_ in pairs(_G.VARIANT_TYPES) do
-- print(k)
-- for k2,v2 in pairs(v) do
-- print(k2, v2)
-- end
-- end
end
_G.LSOL_COLORS = {
-- Basic colors (complete)
White = {255,255,255},
Silver = {192,192,192},
Gray = {128,128,128},
Black = {0,0,0},
Red = {255,0,0},
Maroon = {128,0,0},
Yellow = {255,255,0},
Olive = {128,128,0},
Lime = {0,255,0},
Green = {0,128,0},
Aqua = {0,255,255},
Teal = {0,128,128},
Blue = {0,0,255},
Navy = {0,0,128},
Fuchsia = {255,0,255},
Purple = {128,0,128},
-- Pink colors (complete)
MediumVioletRed = {199, 21, 133},
DeepPink = {255,20,147},
PaleVioletRed = {219, 112, 147},
HotPink = {255,105,180},
LightPink = {255,182,203},
Pink = {255,192,203},
-- Red colors (complete)
DarkRed = {139,0,0},
-- Red
Firebrick = {178,34,34},
Crimson = {220,20,60},
IndianRed = {205,92,92},
LightCoral = {240,128,128},
Salmon = {250,128,114},
DarkSalmon = {233,150,122},
LightSalmon = {255,160,122},
-- Orange colors (complete)
OrangeRed = {255,69,0},
Tomato = {255, 99, 71},
DarkOrange = {255, 140, 0},
Coral = {255, 127, 80},
Orange = {255, 165, 0},
-- Yellow colors (complete)
DarkKhaki = {189, 183, 107},
Gold = {255,215,0},
Khaki = {240, 230, 140},
PeachPuff = {255, 218, 185},
-- Yellow
PaleGoldenrod = {238, 232, 170},
Moccasin = {255, 228, 181},
PapayaWhip = {255, 239, 213},
LightGoldenrodYellow = {250, 250, 210},
LemonChiffon = {255, 250, 205},
LightYellow = {255, 255, 224},
-- Brown colors (complete)
-- Maroon
Brown = {165, 42, 42},
SaddleBrown = {139, 69, 19},
Sienna = {160, 82, 45},
Chocolate = {210, 105, 30},
DarkGoldenrod = {184, 134, 11},
Peru = {205, 133, 63},
RosyBrown = {188, 143, 143},
Goldenrod = {218, 165, 32},
SandyBrown = {244, 164, 96},
Tan = {210, 180, 140},
Burlywood = {222, 184, 135},
Wheat = {245, 222, 179},
NavajoWhite = {255, 222, 173},
Bisque = {255, 228, 196},
BlanchedAlmond = {255, 235, 205},
Cornsilk = {255, 248, 220},
-- Green colors (complete)
DarkGreen = { 0, 100, 0},
-- Green
DarkOliveGreen = { 85, 107, 47},
ForestGreen = { 34, 139, 34},
SeaGreen = { 46, 139, 87},
-- Olive
OliveDrab = {107, 142, 35},
MediumSeaGreen = { 60, 179, 113},
LimeGreen = { 50, 205, 50},
-- Lime
SpringGreen = { 0, 255, 127},
MediumSpringGreen = { 0, 250, 154},
DarkSeaGreen = {143, 188, 143},
MediumAquamarine = {102, 205, 170},
YellowGreen = {154, 205, 50},
LawnGreen = {124, 252, 0},
Chartreuse = {127, 255, 0},
LightGreen = {144, 238, 144},
GreenYellow = {173, 255, 47},
PaleGreen = {152, 251, 152},
-- Cyan colors (complete)
-- Teal
DarkCyan = {0, 139, 139},
LightSeaGreen = {32, 178, 170},
CadetBlue = {95, 158, 160},
DarkTurquoise = {0, 206, 209},
MediumTurquoise = {72, 209, 204},
Turquoise = {64, 224, 208},
-- Aqua
Cyan = {0, 255, 255},
Aquamarine = {127, 255, 212},
PaleTurquoise = {175, 238, 238},
LightCyan = {224, 255, 255},
-- Blue colors (complete)
-- Navy
DarkBlue = {0, 0, 139},
MediumBlue = {0, 0, 205},
-- Blue
MidnightBlue = {25, 25, 112},
RoyalBlue = {65, 105, 225},
SteelBlue = {70, 130, 180},
DodgerBlue = {30, 144, 255},
DeepSkyBlue = {0, 191, 255},
CornflowerBlue = {100, 149, 237},
SkyBlue = {135, 206, 235},
LightSkyBlue = {135, 206, 250},
LightSteelBlue = {176, 196, 222},
LightBlue = {173, 216, 230},
PowderBlue = {176, 224, 230},
-- Purple, violet, and magenta colors (complete)
Indigo = {75, 0, 130},
-- Purple
DarkMagenta = {139, 0, 139},
DarkViolet = {148, 0, 211},
DarkSlateBlue = {72, 61, 139},
BlueViolet = {138, 43, 226},
DarkOrchid = {153, 50, 204},
-- Fuchsia
Magenta = {255, 0, 255},
SlateBlue = {106, 90, 205},
MediumSlateBlue = {123, 104, 238},
MediumOrchid = {186, 85, 211},
MediumPurple = {147, 112, 219},
Orchid = {218, 112, 214},
Violet = {238, 130, 238},
Plum = {221, 160, 221},
Thistle = {216, 191, 216},
Lavender = {230, 230, 250},
-- White colors (complete)
MistyRose = {255, 228, 225},
AntiqueWhite = {250, 235, 215},
Linen = {250, 240, 230},
Beige = {245, 245, 220},
WhiteSmoke = {245, 245, 245},
LavenderBlush = {255, 240, 245},
OldLace = {253, 245, 230},
AliceBlue = {240, 248, 255},
Seashell = {255, 245, 238},
GhostWhite = {248, 248, 255},
Honeydew = {240, 255, 240},
FloralWhite = {255, 250, 240},
Azure = {240, 255, 255},
MintCream = {245, 255, 250},
Snow = {255, 250, 250},
Ivory = {255, 255, 240},
-- Gray and black colors (complete)
-- Black
DarkSlateGray = {47,79,79},
DimGray = {105,105,105},
SlateGray = {112, 128, 144},
-- Gray
LightSlateGray = {119, 136, 153},
DarkGray = {169, 169, 169},
-- Silver
LightGray = {211, 211, 211},
Gainsboro = {220, 220, 220},
UiBackground = {0x32,0x32,0x32,0xff},
UiForeground = {0xff,0xff,0xff,0xff},
UiGrayedOut = {0x80,0x80,0x80,0xff},
}
--[[
Using Acme (semi-bold) for card ordinals and pile labels
Alternatives:
CARDC___.TTF (also includes suit symbols)
RobotoSlab-SemiBold.ttf (more traditional card look)
RobotoCondensed-Regular.ttf
https://fonts.google.com/
]]
-- _G.ORD_FONT = 'assets/fonts/RobotoCondensed-Regular.ttf'
_G.ORD_FONT = 'assets/fonts/Acme-Regular.ttf'
_G.SUIT_FONT = 'assets/fonts/DejaVuSans.ttf'
_G.UI_MEDIUM_FONT = 'assets/fonts/Roboto-Medium.ttf'
_G.UI_REGULAR_FONT = 'assets/fonts/Roboto-Regular.ttf'
_G.LSOL_SOUNDS = {
deal = love.audio.newSource('assets/sounds/cardFan1.wav', 'static'),
load = love.audio.newSource('assets/sounds/cardFan2.wav', 'static'),
move1 = love.audio.newSource('assets/sounds/cardPlace3.wav', 'static'),
move2 = love.audio.newSource('assets/sounds/cardPlace4.wav', 'static'),
move3 = love.audio.newSource('assets/sounds/cardPlace1.wav', 'static'),
move4 = love.audio.newSource('assets/sounds/cardPlace2.wav', 'static'),
undo = love.audio.newSource('assets/sounds/cardOpenPackage2.wav', 'static'),
menushow = love.audio.newSource('assets/sounds/cardSlide1.wav', 'static'),
menuhide = love.audio.newSource('assets/sounds/cardSlide2.wav', 'static'),
uitap = love.audio.newSource('assets/sounds/cardSlide8.wav', 'static'),
complete = love.audio.newSource('assets/sounds/complete.wav', 'static'),
blip = love.audio.newSource('assets/sounds/249895__alienxxx__blip2.wav', 'static'),
fail = love.audio.newSource('assets/sounds/237422__plasterbrain__hover-1.ogg', 'static'),
}
_G.ORD2STRING = {'A','2','3','4','5','6','7','8','9','10','J','Q','K'}
_G.consoleLogMessages = {}
function _G.consoleLog(msg)
table.insert(_G.consoleLogMessages, 1, msg)
end
function _G.drawConsoleLogMessages()
love.graphics.setColor(1,1,1,1)
local y = (_G.UI_SAFEY + _G.UI_SAFEH) - _G.STATUSBARHEIGHT
for i = 1, #_G.consoleLogMessages do
y = y - 24
if y < _G.TITLEBARHEIGHT then
break
end
love.graphics.print(_G.consoleLogMessages[i], 8, y)
end
end
local function createWindowIcon()
local size = 32 -- small size let the OS fuzz it up
local heart = '♥'
local canvas = love.graphics.newCanvas(size, size)
love.graphics.setCanvas(canvas) -- direct drawing operations to the canvas
love.graphics.setColor(love.math.colorFromBytes(unpack(_G.LSOL_COLORS['HotPink'])))
local fnt = love.graphics.newFont(_G.SUIT_FONT, size)
love.graphics.setFont(fnt)
local w = fnt:getWidth(heart)
local h = fnt:getHeight()
love.graphics.print(heart, size/2 - w/2, size/2 - h/2)
love.graphics.setCanvas() -- reset render target to the screen
return canvas:newImageData()
end
function love.load(args)
--[[
Lua (and some engines based on it, like LÖVE) has output buffered by default,
so if you only print a small number of bytes,
you may see the results only after the script is completed.
If you want to see the print output immediately,
add io.stdout:setvbuf("no") to your script,
which will turn the buffering off.
There may be a small performance penalty as the output will be flushed after each print.
]]
-- required for ZeroBrane
--[[
io.stdout:setvbuf('no') -- 'no', 'full' or 'line'
if args then
for k, v in pairs(args) do
print(k, v)
end
if arg[#arg] == "-debug" then require("mobdebug").start() end
end
]]
--[[
print('down', Util.smoothstep(1.0, 0.0, 0.0))
print('down', Util.smoothstep(1.0, 0.0, 0.25))
print('down', Util.smoothstep(1.0, 0.0, 0.5))
print('down', Util.smoothstep(1.0, 0.0, 0.75))
print('down', Util.smoothstep(1.0, 0.0, 1.0))
print('up', Util.smoothstep(0.0, 1.0, 0.0))
print('up', Util.smoothstep(0.0, 1.0, 0.25))
print('up', Util.smoothstep(0.0, 1.0, 0.5))
print('up', Util.smoothstep(0.0, 1.0, 0.75))
print('up', Util.smoothstep(0.0, 1.0, 1.0))
]]
math.randomseed(os.time())
--[[
Lenovo Tab M10 HD Gen 2 800 x 1280 pixels, 16:10 ratio (~149 ppi density)
https://www.gsmarena.com/lenovo_tab_m10_hd_gen_2-10406.php
Motorola Moto G4 1080 x 1920 pixels, 16:9 ratio (~401 ppi density)
https://www.gsmarena.com/motorola_moto_g4-8103.php
Motorola Moto G31 1080 x 2400 pixels, 20:9 ratio (~411 ppi density)
https://www.gsmarena.com/motorola_moto_g31-11225.php
DPIScale = 2.625
]]
_G.UI_SCALE = 1
local DPIScale = love.window.getDPIScale()
if DPIScale > 1 then
_G.UI_SCALE = 1 - (DPIScale/10) -- so a DPIScale of 3 would scale the UI from 1.0 to 0.7
end
-- _G.consoleLog(string.format('DPIScale %f, UI_SCALE %f', DPIScale, _G.UI_SCALE))
-- https://love2d.org/forums/viewtopic.php?f=3&t=84348&p=215242&hilit=rounded+rectangle#p215242
local limits = love.graphics.getSystemLimits( )
-- log.info(limits.canvasmsaa) -- 16
-- log.info(limits.texturesize) -- 16384
-- log.info(limits.multicanvas) -- 8
_G.SETTINGS = Settings.load()
if args then
for _, v in ipairs(args) do -- using ipairs ignores the -ve args (-1 embedded boot.lua, -2 love)
local t = _G.string.split(v, '=')
if #t == 2 then
local set = t[1]
local val = t[2]
if _G.SETTINGS[set] then
log.info('setting', set, 'to', val)
_G.SETTINGS[set] = val
else
log.error('unknown setting', set)
end
else
log.warn('ignoring', v)
end
end
end
--[[
do
local width, height, flags = love.window.getMode( )
print('width', width, 'height', height)
for k, v in pairs(flags) do
print(k, '=', v)
end
end
]]
-- trying to use antialiasing to get rid of jagged rounded rectangles
-- https://love2d.org/forums/viewtopic.php?f=3&t=84348&p=215242&hilit=rounded+rectangle&sid=6ce64568192cd62b80b22ec79b4fdcda
do
local s = _G.SETTINGS
if love.system.getOS() == 'Android' then
-- w, h seem to be ignored when resizable=true, window is sized by Android
-- set w, h when resizable=false to get correct orientation
local opts = {usedpiscale=true, msaa=limits.canvasmsaa, resizable=s.allowOrientation}
local w, h = love.window.getSafeArea()
love.window.setMode(w, h, opts)
else
love.window.setIcon(createWindowIcon())
-- log.info('limits.canvasmsaa', limits.canvasmsaa) -- 16 on BLACKBOX
local opts = {resizable=true, minwidth=640, minheight=480, msaa=limits.canvasmsaa}
if s.windowWidth and s.windowHeight then
love.window.setMode(s.windowWidth, s.windowHeight, opts)
else
love.window.setMode(1080/2, 1920/2, opts)
end
end
end
_G.TITLEBARHEIGHT = 48 * _G.UI_SCALE
_G.STATUSBARHEIGHT = 24 * _G.UI_SCALE
_G.UIFONTSIZE = 22 * _G.UI_SCALE
_G.UIFONTSIZE_TITLEBAR = 22 * _G.UI_SCALE
_G.UIFONTSIZE_SMALL = 14 * _G.UI_SCALE -- 14 created artifacts with antialiasing turned off
-- default lineStyle is 'smooth'
-- print('default lineStyle = ', love.graphics.getLineStyle())
-- love.graphics.setLineStyle('rough')
_G.UI_SAFEX,
_G.UI_SAFEY,
_G.UI_SAFEW,
_G.UI_SAFEH = love.window.getSafeArea()
-- _G.consoleLog(string.format('safe area %d %d %d %d', love.window.getSafeArea()))
-- _G.consoleLog(string.format('safe area toPixels %d %d %d %d',
-- love.window.toPixels(_G.UI_SAFEX),
-- love.window.toPixels(_G.UI_SAFEY),
-- love.window.toPixels(_G.UI_SAFEW),
-- love.window.toPixels(_G.UI_SAFEH)
-- ))
-- implies safe area returns scaled values (which we want to work in), window size reported in pixels
-- preload the recycle icons
-- https://materialdesignicons.com/icon/restart (>1 recycles)
-- https://materialdesignicons.com/icon/restart-off (no more recycles)
-- https://materialdesignicons.com/icon/restart-alert (one remaining recycle)
do
local fname = 'assets/icons/restart.png'
local imageData = love.image.newImageData(fname)
if not imageData then
log.error('could not load', fname)
else
_G.LSOL_ICON_RESTART = love.graphics.newImage(imageData)
end
fname = 'assets/icons/restart-off.png'
imageData = love.image.newImageData(fname)
if not imageData then
log.error('could not load', fname)
else
_G.LSOL_ICON_RESTART_OFF = love.graphics.newImage(imageData)
end
end
_G.BAIZE = Baize.new()
-- love.handlers['permissionButton'] = function(text)
-- log.trace('event handler', text)
-- end
_G.BAIZE.stats = Stats.new()
createAllVariants() -- create '> All'
_G.VARIANT_TYPES['> Favorites'] = {}
_G.BAIZE.ui = UI.new()
love.graphics.setBackgroundColor(Util.getColorFromSetting('baizeColor'))
_G.BAIZE.ui:updateWidget('title', _G.SETTINGS.variantName)
_G.BAIZE:loadUndoStack()
if _G.BAIZE.undoStack then
_G.BAIZE.script = _G.BAIZE:loadScript(_G.SETTINGS.variantName)
if not _G.BAIZE.script then
os.exit()
end
_G.BAIZE:resetPiles()
_G.BAIZE.script:buildPiles()
if _G.SETTINGS.mirrorBaize then
_G.BAIZE:mirrorSlots()
end
_G.BAIZE:layout()
-- don't reset
-- don't startGame
_G.BAIZE.ui:toast('Resuming a saved game of ' .. _G.SETTINGS.variantName, 'load')
_G.BAIZE:undo() -- pop extra state written when saved, will updateUI
else
_G.BAIZE.script = _G.BAIZE:loadScript(_G.SETTINGS.variantName)
if not _G.BAIZE.script then
_G.SETTINGS.variantName = 'Klondike' -- TODO save settings
_G.BAIZE.script = _G.BAIZE:loadScript(_G.SETTINGS.variantName)
if not _G.BAIZE.script then
os.exit()
end
end
_G.BAIZE:resetPiles()
_G.BAIZE.script:buildPiles()
if _G.SETTINGS.mirrorBaize then
_G.BAIZE:mirrorSlots()
end
_G.BAIZE:layout()
_G.BAIZE:resetState()
_G.BAIZE.ui:toast('Starting a new game of ' .. _G.SETTINGS.variantName, 'deal')
_G.BAIZE.script:startGame()
_G.BAIZE:undoPush()
_G.BAIZE:updateStatus()
_G.BAIZE:updateUI()
end
if _G.SETTINGS.lastVersion == 0 then
_G.BAIZE.ui:toast(string.format('Welcome to %s', love.filesystem.getIdentity()))
elseif _G.SETTINGS.lastVersion ~= _G.LSOL_VERSION then
_G.BAIZE.ui:toast(string.format('%s version updated from %d to %d', love.filesystem.getIdentity(), _G.SETTINGS.lastVersion, _G.LSOL_VERSION))
end
-- _G.BAIZE.ui:toast(string.format('safe x=%d y=%d w=%d h=%d', love.window.getSafeArea()))
--[[
print(love.filesystem.getAppdataDirectory()) -- /home/gilbert/.local/share/
print(love.filesystem.getSourceBaseDirectory()) -- /home/gilbert
print(love.filesystem.getUserDirectory()) -- /home/gilbert/
print(love.filesystem.getWorkingDirectory()) -- /home/gilbert/LÖVE Solitaire
print(love.filesystem.getSaveDirectory()) -- /home/gilbert/.local/share/love/LÖVE Solitaire
]]
end
function love.update(dt_seconds)
_G.BAIZE:update(dt_seconds)
end
function love.draw()
_G.BAIZE:draw()
end
function love.resize(w, h)
-- _G.consoleLog(string.format('resize %d %d', w, h))
-- w, h = love.window.getMode()
-- _G.consoleLog(string.format('window %d %d', w, h))
-- _G.consoleLog(string.format('safe area %d %d %d %d', love.window.getSafeArea()))
_G.BAIZE.backgroundCanvas = nil -- will be recreated by Baize:draw()
_G.BAIZE:layout()
end
function love.keyreleased(key)
-- log.info(key)
if key == 'u' then
_G.BAIZE:undo()
elseif key == 'c' then
_G.BAIZE:collect()
elseif key == 'h' then
_G.BAIZE:hint()
elseif key == 'n' then
_G.BAIZE:newDeal()
-- _G.BAIZE.ui:showFAB{icon='star', baizeCmd='newDeal'}
elseif key == 'r' then
_G.BAIZE:restartDeal()
elseif key == 'b' then
if love.keyboard.isDown('lshift') or love.keyboard.isDown('lctrl') then
_G.BAIZE:gotoBookmark()
else
_G.BAIZE:setBookmark()
end
elseif key == 'd' and love.keyboard.isDown('lalt') then
_G.SETTINGS.debug = not _G.SETTINGS.debug
Settings.save(_G.SETTINGS)
_G.BAIZE:createCardTextures()
end
if love.system.getOS() == 'Android' then
if key == 'escape' then -- Android return/back?
love.event.quit(0)
elseif key == 'home' then
love.event.quit(0)
elseif key == 'menu' then
_G.BAIZE.ui:toggleMenuDrawer()
elseif key == 'search' then
_G.BAIZE.ui:showVariantTypesDrawer()
end
else
if key == 'escape' then
_G.BAIZE.ui:hideDrawers()
_G.BAIZE.ui:cancelModalDialog()
end
end
if _G.SETTINGS.debug then
if key == 't' then
_G.BAIZE.ui:toast(string.format('Toast %f', math.random()))
elseif key == 'up' then
_G.BAIZE:startSpinning()
elseif key == 'down' then
_G.BAIZE:stopSpinning()
elseif key == 'f' then
_G.BAIZE.ui:showFAB{icon='star', baizeCmd='newDeal'}
-- elseif key == 'm' then
-- local result = _G.BAIZE:getPermission('This game will count as a loss. Continue?')
-- log.trace(result)
elseif key == '8' and love.keyboard.isDown('lctrl') then
for _, tab in ipairs(_G.BAIZE.tableaux) do
table.sort(tab.cards, function(a,b) return a.ord > b.ord end)
tab:refan()
end
_G.BAIZE:updateStatus()
_G.BAIZE:updateUI()
elseif key == '9' and love.keyboard.isDown('lctrl') then
for _, tab in ipairs(_G.BAIZE.tableaux) do
table.sort(tab.cards, function(a,b) return a.ord < b.ord end)
tab:refan()
end
_G.BAIZE:updateStatus()
_G.BAIZE:updateUI()
elseif key == '0' and love.keyboard.isDown('lctrl') then
for _, tab in ipairs(_G.BAIZE.tableaux) do
for i = #tab.cards, 2, -1 do
local j = math.random(i)
if i ~= j then
tab.cards[i], tab.cards[j] = tab.cards[j], tab.cards[i]
end
end
tab:refan()
end
_G.BAIZE:updateStatus()
_G.BAIZE:updateUI()
elseif key == 'f1' then
_G.BAIZE:solver()
end
end
end
function love.mousepressed(x, y, button, istouch, presses)
_G.BAIZE:mousePressed(x, y, button)
end
function love.mousemoved(x, y, dx, dy, istouch)
_G.BAIZE:mouseMoved(x, y, dx, dy)
end
function love.mousereleased(x, y, button, istouch, presses)
_G.BAIZE:mouseReleased(x, y, button)
end
function love.wheelmoved(x, y)
local drw = _G.BAIZE.ui:findOpenDrawer()
if drw then
drw:startDrag()
drw:dragBy(0, y*24)
drw:stopDrag()
else
_G.BAIZE:startDrag()
_G.BAIZE:dragBy(x*24, y*24)
_G.BAIZE:stopDrag()
end
end
function love.displayrotated(index, orientation)
-- https://love2d.org/wiki/love.displayrotated
-- Due to a bug in LOVE 11.3, the orientation value is boolean true instead. A workaround is as follows:
-- orientation = love.window.getDisplayOrientation(index)
-- _G.consoleLog(string.format('displayrotated %d %s', index, orientation))
_G.BAIZE.backgroundCanvas = nil -- will be recreated by Baize:draw()
_G.BAIZE:layout()
-- if _G.SETTINGS.debug then
-- _G.BAIZE.ui:toast('displayrotated ' .. tostring(orientation))
-- _G.BAIZE.ui:toast(string.format('safe x=%d y=%d w=%d h=%d', love.window.getSafeArea()))
-- end
end
function love.quit()
-- no args
-- don't save stats here (with _G.BAIZE.stats:save()) because never quite sure when app quits
-- or is forced-stopped; instead, save stats when they change
Settings.save(_G.SETTINGS) -- in case window has moved or resized
-- don't save completed game, to stop win being recorded when it's reloaded
if --[[ #_G.BAIZE.undoStack == 1 or ]] _G.BAIZE.status == 'complete' then
_G.BAIZE:rmUndoStack()
else
_G.BAIZE:saveUndoStack()
end
return false -- allow app to quit
end