-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.lua
190 lines (128 loc) · 3.73 KB
/
common.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
local _tl_compat; if (tonumber((_VERSION or ''):match('[%d.]*$')) or 0) < 5.3 then local p, m = pcall(require, 'compat53.module'); if p then _tl_compat = m end end; local ipairs = _tl_compat and _tl_compat.ipairs or ipairs; local string = _tl_compat and _tl_compat.string or string; require("love")
require("nbtypes")
require("constants")
local i18n = require("i18n")
local gooi = require("gooi.gooi")
local serpent = require("serpent")
__DEBUG__ = false
onAndroid = love.system.getOS() == "Android"
useKeyboard = true
preventiveFirstRun = true
function pack(...)
return { ... }
end
function div(a, b)
return (a - a % b) / b
end
function make_screenshot()
local i = 0
local info
repeat
i = i + 1
info = love.filesystem.getInfo("screenshot" .. i .. ".png")
until not info
love.graphics.captureScreenshot("screenshot" .. i .. ".png")
end
function calcPercent(eq, pressed_arr)
if not eq then return 0 end
local succ, mistake, count = 0, 0, 0
for k, v in ipairs(eq) do
if v then
count = count + 1
end
if v and pressed_arr[k] then
succ = succ + 1
end
if not v and pressed_arr[k] then
mistake = mistake + 1
end
end
print(string.format("calcPercent() count = %d, succ = %d, mistake = %d", count, succ, mistake))
return succ / count - mistake / count
end
function percentage(signals, pressed)
local p1, p2, p3, p4
p1 = calcPercent(signals.eq.sound, pressed.sound)
p2 = calcPercent(signals.eq.color, pressed.color)
p3 = calcPercent(signals.eq.form, pressed.form)
p4 = calcPercent(signals.eq.pos, pressed.pos)
local percent = {
sound = p1 > 0.0 and p1 or 0.0,
color = p2 > 0.0 and p2 or 0.0,
form = p3 > 0.0 and p3 or 0.0,
pos = p4 > 0.0 and p4 or 0.0,
}
percent.common = (percent.sound + percent.color + percent.form + percent.form) / 4
return percent
end
function storeGooi()
local g = { components = gooi.components }
gooi.components = {}
return g
end
function restoreGooi(g)
if g == nil then
error("g == nil")
end
gooi.components = g.components
end
function compareDates(now, date)
local ranges = {
{ 0, 6, i18n("today") },
{ 7, 24, i18n("yesterday") },
{ 25, 48, i18n("twoDays") },
{ 49, 72, i18n("threeDays") },
{ 73, 96, i18n("fourDays") },
{ 97, 24 * 7, i18n("lastWeek") },
{ 24 * 7 + 1, 24 * 14, i18n("lastTwoWeek") },
{ 24 * 14 + 1, 24 * 30, i18n("lastMonth") },
{ 24 * 30 + 1, 24 * 365, i18n("lastYear") },
}
local result = i18n("moreTime")
local t1, t2, diff = now.yday * 24 + now.hour, date.yday * 24 + date.hour, 0
if date.year == now.year then
diff = t1 - t2
else
diff = (now.year - date.year) * 365 * 24 - (t1 - t2)
end
for _, v in ipairs(ranges) do
local v1, v2 = v[1], v[2]
if diff >= v1 and diff <= v2 then
result = v[3]
break
end
end
return result
end
function getDefaultSettings()
print("getDefaultSettings")
return {
volume = 0.2,
firstRun = true,
}
end
function readSettings()
local data, _ = love.filesystem.read(SETTINGS_FILENAME)
local result
if data then
local ok, func = serpent.load(data)
if not ok then
result = getDefaultSettings()
else
result = func()
end
else
result = getDefaultSettings()
end
return result
end
function writeSettings()
local serialized = serpent.dump(settings)
local ok, msg = love.filesystem.write(SETTINGS_FILENAME, serialized)
if not ok then
print("Could write settings to ", SETTINGS_FILENAME, " file", msg)
end
end
function isPositionEqual(a, b)
return a.x == a.y and b.x == b.y
end