Assorted micro-optimizations.

Some of these are pure token optimizations, some spend tokens for performance. Function calls are particularly expensive in Pico-8, so using the table form of `pal` whenever changing multiple colors seems worth it.

Also fixes a bug in palette swapping.
This commit is contained in:
Kistaro Windrider 2022-12-17 17:03:24 -08:00
parent d0eba82c7b
commit ba878b501f
Signed by: kistaro
SSH Key Fingerprint: SHA256:TBE2ynfmJqsAf0CP6gsflA0q5X5wD5fVKWPsZ7eVUg8

View File

@ -101,19 +101,13 @@ function _apply(x,ts,a)
end end
function sgn0(x) function sgn0(x)
if (x==0) return x if (x==0) return x
return sgn(x) return sgn(x)
end end
function _mnmx(x,y) function _mnmx(x,y)
return min(x,y),max(x,y) if (x>y)return y,x
end return x,y
-->8
bg={}
add(modules,bg)
function bg:draw0()
cls(0)
end end
-->8 -->8
level={} level={}
@ -139,6 +133,7 @@ function level:advance()
self:reinit(self.ix+1) self:reinit(self.ix+1)
end end
pitpal = {[0]=1, [7]=0}
function level:draw() function level:draw()
cls(1) cls(1)
pal(1,0) pal(1,0)
@ -150,11 +145,11 @@ function level:draw()
for _,pit in pairs(self._pits) do for _,pit in pairs(self._pits) do
spr(pit.s,pit.px,pit.py) spr(pit.s,pit.px,pit.py)
if pit.contents then if pit.contents then
pal(7,0) pal(pitpal)
pal(0,1)
palt(0,false) palt(0,false)
spr(pit.contents,pit.px,pit.py) spr(pit.contents,pit.px,pit.py)
pal() pal()
pal(1,0)
end end
for _,crate in pairs(self._crates) do for _,crate in pairs(self._crates) do
spr(crate.s,crate.px,crate.py) spr(crate.s,crate.px,crate.py)
@ -179,7 +174,7 @@ function level:update()
if #crate.todo==0 then if #crate.todo==0 then
local pit=self._pits[_mix(crate.mx,crate.my)] local pit=self._pits[_mix(crate.mx,crate.my)]
if pit!=nil and pit.contents==nil then if pit and not pit.contents then
add(remove,cix) add(remove,cix)
crate.dead=true crate.dead=true
pit.contents=crate.s pit.contents=crate.s
@ -205,7 +200,7 @@ function level:load_dynobjs()
local px,py=mx*8,my*8 local px,py=mx*8,my*8
local s=self:_mget(mx,my) local s=self:_mget(mx,my)
local def=self:_get_cratedef(s) local def=self:_get_cratedef(s)
if def!=nil then if def then
self._crates[mxy]={ self._crates[mxy]={
s=s,def=def, s=s,def=def,
mx=mx,my=my, mx=mx,my=my,
@ -228,8 +223,8 @@ end
function level:recollide() function level:recollide()
self._coll={} self._coll={}
for mx=0,15,1 do for mx=0,15 do
for my=0,15,1 do for my=0,15 do
local mxy=_mix(mx,my) local mxy=_mix(mx,my)
self._coll[mxy]= self._coll[mxy]=
fget(self:_mget(mx,my),7) or fget(self:_mget(mx,my),7) or
@ -239,16 +234,16 @@ function level:recollide()
end end
function level:reanchor(remove) function level:reanchor(remove)
if remove or self._anch==nil then if remove or not self._anch then
self._anch={} self._anch={}
end end
for ax0=0,31,1 do for ax0=0,31 do
for ay0=0,31,1 do local ax1 = ax0-1+2*(ax0%2)
local ax1,ay1=ax0-1+2*(ax0%2),ay0-1+2*(ay0%2) local mx0,mx1 = ax0\2,ax1\2
for ay0=0,31 do
local mx0,my0=ax0\2,ay0\2 local ay1=ay0-1+2*(ay0%2)
local mx1,my1=ax1\2,ay1\2 local my0,my1=ay0\2,ay1\2
if ( if (
not self:mcoll(mx0,my0) and not self:mcoll(mx0,my0) and