-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathomppc.lua
435 lines (359 loc) · 10.2 KB
/
omppc.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
local Class = {}
Class.new = function(self, object)
object = object or {}
setmetatable(object, self)
self.__index = self
return object
end
local Mods = Class:new()
Mods.scoreRate = 1
Mods.timeRate = 1
Mods.odRate = 1
Mods.parse = function(self, mods)
self.modsData = mods
if tonumber(mods) then
self:parseNumber(tonumber(mods))
elseif type(mods) == "string" then
self:parseString(mods)
end
return self
end
Mods.parseString = function(self, modsString)
if modsString:find("EZ") then
self.Easy = true
self.scoreRate = self.scoreRate * 0.5
self.odRate = 0.5
end
if modsString:find("NF") then
self.NoFail = true
self.scoreRate = self.scoreRate * 0.5
end
if modsString:find("HT") then
self.HalfTime = true
self.scoreRate = self.scoreRate * 0.5
self.timeRate = 3/4
end
if modsString:find("DT") then
self.DoubleTime = true
self.timeRate = 3/2
end
end
Mods.parseNumber = function(self, modsNumber)
if bit.band(modsNumber, 2) == 2 then
self.Easy = true
self.scoreRate = self.scoreRate * 0.5
self.odRate = 0.5
end
if bit.band(modsNumber, 1) == 1 then
self.NoFail = true
self.scoreRate = self.scoreRate * 0.5
end
if bit.band(modsNumber, 256) == 256 then
self.HalfTime = true
self.scoreRate = self.scoreRate * 0.5
self.timeRate = 3/4
end
if bit.band(modsNumber, 64) == 64 then
self.DoubleTime = true
self.timeRate = 3/2
end
end
local Note = Class:new()
Note.parse = function(self, line, keymode)
local x, startTime = line:match("^(%d-),%d-,(%d-),.+$")
local endTime = line:match("^.+,(%d-):.+:.+:.+:.+:.-$")
self.key = math.ceil(x / 512 * keymode)
self.keymode = keymode
self.startTime = tonumber(startTime)
self.endTime = tonumber(endTime) or self.startTime
self.heldUntil = {}
self.individualStrains = {}
for i = 1, keymode do
self.individualStrains[i] = 0
self.heldUntil[i] = 0
end
return self
end
Note.INDIVIDUAL_DECAY_BASE = 0.125
Note.OVERALL_DECAY_BASE = 0.30
Note.overallStrain = 1
Note.getIndividualStrain = function(self)
return self.individualStrains[self.key]
end
Note.setIndividualStrain = function(self, value)
self.individualStrains[self.key] = value
end
Note.calculateStrains = function(self, pNote, timeRate)
local addition = 1
local timeElapsed = (self.startTime - pNote.startTime) / timeRate
local individualDecay = math.pow(self.INDIVIDUAL_DECAY_BASE, timeElapsed / 1000)
local overallDecay = math.pow(self.OVERALL_DECAY_BASE, timeElapsed / 1000)
local holdFactor = 1
local holdAddition = 0
for i = 1, self.keymode do
self.heldUntil[i] = pNote.heldUntil[i]
if self.startTime < self.heldUntil[i] and self.endTime > self.heldUntil[i] then
holdAddition = 1
end
if self.endTime == self.heldUntil[i] then
holdAddition = 0
end
if self.heldUntil[i] > self.endTime then
holdFactor = 1.25
end
end
self.heldUntil[self.key] = self.endTime
for i = 1, self.keymode do
self.individualStrains[i] = pNote.individualStrains[i] * individualDecay
end
self:setIndividualStrain(self:getIndividualStrain() + 2 * holdFactor)
self.overallStrain = pNote.overallStrain * overallDecay + (addition + holdAddition) * holdFactor
end
local Beatmap = Class:new()
Beatmap.parse = function(self, beatmapString)
self.noteData = {}
local blockName
for line in string.gmatch(beatmapString .. "\n", "(.-)\n") do
line = line:match("^%s*(.-)%s*$")
if line:find("^%[") then
blockName = line:match("^%[(.*)%]")
elseif blockName == "General" or blockName == "Difficulty" then
if line:match("^Mode:") then
self.mode = tonumber(line:match(":(%d+)$"))
elseif line:match("^OverallDifficulty") then
self.od = tonumber(line:match(":(.+)$"))
elseif line:match("^CircleSize") then
self.keymode = tonumber(line:match(":(.+)$"))
end
elseif blockName == "HitObjects" and not line:match("^%s*$") then
table.insert(self.noteData, Note:new():parse(line, self.keymode))
end
end
table.sort(self.noteData, function(a, b) return a.startTime < b.startTime end)
self.noteCount = #self.noteData
return self
end
Beatmap.getOD = function(self)
return self.od * self.mods.odRate
end
Beatmap.STAR_SCALING_FACTOR = 0.018
Beatmap.calculateStarRate = function(self)
self:calculateStrainValues()
self.starRate = self:calculateDifficulty() * self.STAR_SCALING_FACTOR
end
Beatmap.getStarRate = function(self)
return self.starRate
end
Beatmap.calculateStrainValues = function(self)
local cNote = self.noteData[1]
local nNote
for i = 2, #self.noteData do
nNote = self.noteData[i]
nNote:calculateStrains(cNote, self.mods.timeRate)
cNote = nNote
end
end
Beatmap.STRAIN_STEP = 400
Beatmap.DECAY_WEIGHT = 0.9
Beatmap.calculateDifficulty = function(self)
local actualStrainStep = self.STRAIN_STEP * self.mods.timeRate
local highestStrains = {}
local intervalEndTime = actualStrainStep
local maximumStrain = 0
local pNote
for _, note in ipairs(self.noteData) do
while note.startTime > intervalEndTime do
table.insert(highestStrains, maximumStrain)
if not pNote then
maximumStrain = 0
else
local individualDecay = math.pow(note.INDIVIDUAL_DECAY_BASE, (intervalEndTime - pNote.startTime) / 1000)
local overallDecay = math.pow(note.OVERALL_DECAY_BASE, (intervalEndTime - pNote.startTime) / 1000)
maximumStrain = pNote:getIndividualStrain() * individualDecay + pNote.overallStrain * overallDecay
end
intervalEndTime = intervalEndTime + actualStrainStep
end
local strain = note:getIndividualStrain() + note.overallStrain
if strain > maximumStrain then
maximumStrain = strain
end
pNote = note
end
local difficulty = 0
local weight = 1
table.sort(highestStrains, function(a, b) return a > b end)
for _, strain in ipairs(highestStrains) do
difficulty = difficulty + weight * strain
weight = weight * self.DECAY_WEIGHT
end
return difficulty
end
local Calculator = Class:new()
Calculator.computeTotalValue = function(self)
local multiplier = 0.8
if self.mods.NoFail then
multiplier = multiplier * 0.90
end
if self.mods.Easy then
multiplier = multiplier * 0.50
end
self.realScore = self.score / self.mods.scoreRate
self:computeStrainValue()
self:computeAccValue()
self.totalValue = math.pow(math.pow(self.strainValue, 1.1) + math.pow(self.accValue, 1.1), 1 / 1.1) * multiplier
end
Calculator.computeStrainValue = function(self)
if self.mods.scoreRate <= 0 then
self.strainValue = 0
return
end
local score = self.realScore
self.strainValue = math.pow(5 * math.max(1, self.beatmap:getStarRate() / 0.2) - 4, 2.2) / 135
self.strainValue = self.strainValue * (1 + 0.1 * math.min(1, self.beatmap.noteCount / 1500))
if score <= 500000 then
self.strainValue = 0
elseif score <= 600000 then
self.strainValue = self.strainValue * ((score - 500000) / 100000 * 0.3)
elseif score <= 700000 then
self.strainValue = self.strainValue * (0.3 + (score - 600000) / 100000 * 0.25)
elseif score <= 800000 then
self.strainValue = self.strainValue * (0.55 + (score - 700000) / 100000 * 0.20)
elseif score <= 900000 then
self.strainValue = self.strainValue * (0.75 + (score - 800000) / 100000 * 0.15)
else
self.strainValue = self.strainValue * (0.9 + (score - 900000) / 100000 * 0.1)
end
end
Calculator.computeAccValue = function(self)
local hitWindow300 = 34 + 3 * (math.min(10, math.max(0, 10 - self.beatmap.od)))
if hitWindow300 <= 0 then
self.accValue = 0
return
end
self.accValue = math.max(0, 0.2 - ((hitWindow300 - 34) * 0.006667)) * self.strainValue * math.pow((math.max(0, self.realScore - 960000) / 40000), 1.1)
end
local PlayData = Class:new()
PlayData.process = function(self)
self.mods = Mods:new():parse(self.modsData)
self.beatmap = Beatmap:new()
self.beatmap.mods = self.mods
self.beatmap:parse(self.beatmapString)
self.beatmap:calculateStarRate()
self.calculator = Calculator:new()
self.calculator.beatmap = self.beatmap
self.calculator.score = self.score
self.calculator.mods = self.mods
self.calculator:computeTotalValue()
end
PlayData.getData = function(self)
return {
modsData = self.mods.modsData,
scoreRate = self.mods.scoreRate,
timeRate = self.mods.timeRate,
odRate = self.mods.odRate,
starRate = self.beatmap.starRate,
noteCount = self.beatmap.noteCount,
scaledOD = self.beatmap:getOD(),
od = self.beatmap.od,
score = self.score,
realScore = self.calculator.realScore,
strainValue = self.calculator.strainValue,
accValue = self.calculator.accValue,
pp = self.calculator.totalValue
}
end
PlayData.getJSON = function(self)
local data = self:getData()
local out = {}
out[1] = "{"
for key, value in pairs(data) do
out[#out + 1] = "\"" .. key .. "\":"
if type(value) == "number" then
out[#out + 1] = value .. ","
elseif type(value) == "string" then
out[#out + 1] = "\"" .. value .. "\","
end
end
out[#out] = out[#out]:sub(1, -2) .. "}"
return table.concat(out)
end
if arg and arg[0] and arg[0]:find("omppc%.lua$") then
local input = {}
for i = 1, #arg do
local cArg = arg[i]
local nArg = arg[i + 1]
if cArg:match("^%-") then
local key = cArg:match("^%-(.*)$")
if key == "b" then
input.path = nArg
elseif key == "m" then
input.mods = nArg
elseif key == "s" then
input.score = tonumber(nArg)
elseif key == "v" then
input.verbose = true
elseif key == "j" then
input.json = true
end
i = i + 2
end
end
local file = io.open(input.path, "r")
local playData = PlayData:new()
playData.modsData = input.mods
playData.beatmapString = file:read("*a")
playData.score = input.score
playData:process()
file:close()
local data = playData:getData()
if input.verbose then
print(
([[
Mods info
mods %8s
scoreRate %8.2f
timeRate %8.2f
odRate %8.2f
Beatmap info:
starRate %8.2f
noteCount %8d
scaled OD %8.1f
real OD %8.1f
Play info
scaled score %8d
real score %8d
strainValue %8.2f
accValue %8.2f
PP %8.2f
]]
):format(
data.modsData,
data.scoreRate,
data.timeRate,
data.odRate,
data.starRate,
data.noteCount,
data.scaledOD,
data.od,
data.score,
data.realScore,
data.strainValue,
data.accValue,
data.pp
)
)
elseif input.json then
print(playData:getJSON())
else
print(data.pp)
end
return
end
return {
Class = Class,
Mods = Mods,
Note = Note,
PlayData = PlayData,
Beatmap = Beatmap,
Calculator = Calculator
}