Skip to content

Commit

Permalink
Merge branch 'dev-no-delay'
Browse files Browse the repository at this point in the history
  • Loading branch information
xerool committed Apr 20, 2022
2 parents d1e9a8e + d9edf1b commit b27b759
Show file tree
Hide file tree
Showing 8 changed files with 116 additions and 32 deletions.
8 changes: 8 additions & 0 deletions spec/aux_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,12 @@ describe('aux', function()
assert.equals('100', helper.get_mod('funny2'))
end)

it('disables the mod name checks', function()
xero.aux {"invert,"}

xero.ease {0, 1, xero.outExpo, 100, "invert,"}

update(1)
end)

end)
22 changes: 21 additions & 1 deletion spec/ease_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,25 @@ describe('ease', function()
it('should detect bad mod magnitudes', function()
assert.errors(function() xero.ease{0, 1, xero.outExpo, {100}, 'invert'} end)
end)
it('should detect bad mod names', function()
it('should detect bad mod names (case 1)', function()
assert.errors(function()
-- tricky comma
xero.ease{0, 1, xero.outExpo, 100, 'invert,'}
update()
end)
end)
it('should detect bad mod names (case 2)', function()
assert.errors(function()
xero.ease{0, 1, xero.outExpo, 100, '1.2x'}
update()
end)
end)
it('should detect bad mod names (case 3)', function()
assert.errors(function()
xero.ease{0, 1, xero.outExpo, 100, 'c500'}
update()
end)
end)
it('should detect missing mod names', function()
assert.errors(function() xero.ease{0, 1, xero.outExpo, 100, nil} end)
end)
Expand Down Expand Up @@ -105,4 +117,12 @@ describe('ease', function()
assert.equal(helper.get_mod('movex0'), helper.get_mod('movex1'))
assert.equal(helper.get_mod('movex0'), helper.get_mod('movex2'))
end)
it('shouldn\'t apply multiple times', function()
xero.ease {0, 1, xero.outExpo, 100, 'invert', 100, 'invert'}
xero.set {0, 300, 'drunk'}
xero.set {1, 0, 'drunk', 0, 'drunk', 0, 'drunk', 0, 'drunk'}
update(1)
assert.equal(helper.get_mod('invert'), '100')
assert.equal(helper.get_mod('drunk'), '0')
end)
end)
1 change: 1 addition & 0 deletions spec/helper.lua
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ function helper.on()
end
end
mock.on_happened = true
helper.update(0)
end

function helper.update(dt)
Expand Down
1 change: 0 additions & 1 deletion spec/node_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,5 @@ describe('node', function()
xero = nil
end)

-- TODO

end)
23 changes: 23 additions & 0 deletions spec/reset_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -125,4 +125,27 @@ describe('reset', function()
assert.errors(function() xero.reset{0, 1} end)
end)

it('works with definemod', function()
local modvalue
xero.definemod{'mymod', function(mymod) modvalue = mymod end}

xero.set {0, 100, 'mymod'}
xero.reset {2, exclude = {'mymod'}}
xero.reset {4}
xero.set {6, 100, 'mymod'}
xero.reset {8, only = 'mymod'}

update(1) -- at beat 1
assert.equal(modvalue, 100)
update(2) -- at beat 3
assert.equal(modvalue, 100)
update(2) -- at beat 5
assert.equal(modvalue, 0)
update(2) -- at beat 7
assert.equal(modvalue, 100)
update(2) -- at beat 9
assert.equal(modvalue, 0)

end)

end)
8 changes: 7 additions & 1 deletion spec/set_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,17 @@ describe('set', function()
end)

it('sets values at the right time', function()
xero.set{1, 100, 'bumpy'}
xero.set {1, 100, 'bumpy'}
update(0.5)
assert.equal(nil, helper.get_mod('bumpy'))
update(1)
assert.equal('100', helper.get_mod('bumpy'))
end)

it('it errors with this particular bad signature', function()
assert.errors(function()
xero.set {0, nil, 'modname'}
end)
end)

end)
2 changes: 1 addition & 1 deletion template/std.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ setmetatable(xero, {
-- make require work
xero.package = {
-- mirin template loader path
path = 'lua/?.lua;lua/?/init.lua;plugin/?.lua;plugin/?/init.lua',
path = 'lua/?.lua;lua/?/init.lua;plugins/?.lua;plugins/?/init.lua',
preload = {},
loaded = {},
loaders = {
Expand Down
83 changes: 55 additions & 28 deletions template/template.lua
Original file line number Diff line number Diff line change
Expand Up @@ -81,20 +81,40 @@ local _ = string.gsub('\'\\{}(),;* ', '.', function(t)
banned_chars[t] = true
end)

local function normalize_mod_no_checks(name)
name = string.lower(name)
return aliases[name] or name
end

-- convert a mod to its lowercase dealiased name
local function normalize_mod(name)
-- A mod name isn't valid if it would cause problems when put into
-- the "*-1 100 {}" format that GAMESTATE:ApplyModifiers expects.
-- For example, the space in 'invert ' means the game engine would treat
-- it identically to regular 'invert', which means it should be denied.
local function ensure_mod_name_is_valid(name)
if banned_chars[string.sub(name, 1, 1)] or banned_chars[string.sub(name, #name, #name)] then
error(
'You have a typo in your mod name. '..
'You wrote \''..name..'\', but you probably meant '..
'\''..string.gsub(name, '[\'\\{}(),;* ]', '')..'\''
)
end
if string.find(name, '^c[0-9]+$') then
error(
'You can\'t name your mod \''..name..'\'.\n'..
'Use \'cmod\' if you want to set a cmod.'
)
end
if string.find(name, '^[0-9.]+x$') then
error(
'You can\'t name your mod \''..name..'\'.\n'..
'Use \'xmod\' if you want to set an xmod.'
)
end
end

local function normalize_mod_no_checks(name)
name = string.lower(name)
return aliases[name] or name
end

-- convert a mod to its lowercase dealiased name
local function normalize_mod(name)
if not auxes[name] then ensure_mod_name_is_valid(name) end
return normalize_mod_no_checks(name)
end

Expand Down Expand Up @@ -622,6 +642,13 @@ end
-- runs once during ScreenReadyCommand, after the user code is loaded
-- replaces aliases with their respective mods
local function resolve_aliases()
-- aux
local old_auxes = copy(auxes)
clear(auxes)
for mod, _ in pairs(old_auxes) do
-- auxes bypass name checks
auxes[normalize_mod_no_checks(mod)] = true
end
-- ease
for _, e in ipairs(eases) do
for i = 5, #e, 2 do
Expand All @@ -640,12 +667,6 @@ local function resolve_aliases()
end
end
end
-- aux
local old_auxes = copy(auxes)
clear(auxes)
for mod, _ in pairs(old_auxes) do
auxes[normalize_mod(mod)] = true
end
-- node
for _, node_entry in ipairs(nodes) do
local input = node_entry[1]
Expand Down Expand Up @@ -816,7 +837,7 @@ local function run_eases(beat, time)
-- `eases_index` is pointing to the next ease in the timeline that hasn't started yet
while eases_index <= #eases do
local e = eases[eases_index]
-- measure by beat by default, or time if time=true was set
-- The ease measures timings by beat by default, or time if time=true was set
local measure = e.time and time or beat
-- if it's not ready, break out of the loop
-- the eases table is sorted, so none of the later eases will be done either
Expand Down Expand Up @@ -863,23 +884,25 @@ local function run_eases(beat, time)
end
end

-- If it isn't using relative already, it needs to be adjusted to be relative (ie, like 'add', not like 'ease')
-- Adjusted based on what the current target is set to
-- This is the reason why the sorting the eases table needs to be stable.
if not e.relative then
for i = 4, #e, 2 do
local mod = e[i + 1]
e[i] = e[i] - targets[plr][mod]
end
end

e.offset = 0
-- If the ease value ends with 0.5 or more, the ease should "stick".
-- Ie, if you use outExpo, the value should stay on until turned off.
-- this is a poor quality comment
if e[3](1) >= 0.5 then
e.offset = 1
for i = 4, #e, 2 do
local ease_ends_at_different_position = e[3](1) >= 0.5
e.offset = ease_ends_at_different_position and 1 or 0

for i = 4, #e, 2 do
-- If it isn't using relative already, it needs to be adjusted to be relative
-- (ie, like 'add', not like 'ease')
-- Adjusted based on what the current target is set to
-- This is the reason why the sorting the eases table needs to be stable.
if not e.relative then
local mod = e[i + 1]
e[i] = e[i] - targets[plr][mod]
end

-- Update the target if it needs to be updated
if ease_ends_at_different_position then
local mod = e[i + 1]
targets[plr][mod] = targets[plr][mod] + e[i]
end
Expand Down Expand Up @@ -1532,7 +1555,11 @@ function xero.init_command(self)

-- Update command is called every frame. It is what sets the mod values every frame,
-- and reads through everything that's been queued by the user.
self:addcommand('Update', update_command)
-- Delay one frame because the escape menu issue
self:addcommand('Update', function()
self:removecommand('Update')
self:addcommand('Update', update_command)
end)

-- NotITG and OpenITG have a long standing bug where the InitCommand on an actor can run twice in certain cases.
-- By removing the command here (at the end of init_command), we prevent it from being run again.
Expand Down

0 comments on commit b27b759

Please sign in to comment.