Save some tokens in horrible, horrible ways. (#13)

replace comparisons with bit math bullshit

integers in the range [0, 15] fit entirely in the bit mask 0x000F. integers out of that range will have at least one bit 0x0010 or higher, or will have the sign bit 0x8000 set. so to find out if one of two numbers is out of range [0, 15], we can check the bit mask of their bitwise or.

this saves tokens and cycles. it is also completely illegible. very in the spirit of Pico-8, I love it.

comment the bullshit

it needs it

packed crate representation

don't bother exploding crates into four bools, and then comparing them all individually to a bunch of conditions. absurd bit manipulation bullshit saves cycles and tokens. leaving a crate's movement rule represented as four bits means we can exploit our previous calculation of dx1 and dy1, which must each either be 0x0001 or 0x8FFF, and violently hammer them down to align with this bit-packed representation, giving this glorious little atrocity.

Fix crate math.

I forgot that -1 & 1 = 1 rather than 0 so all the bit math didn't work. But I can fix it with polynomial algebra! this is much better.

Save tokens on movemebnt checks

I promise this is mathematically equivalent-ish to the original. (0.2 and its multiples are nonterminating decimals in base 2, so there's a little jank when the negative shift right is a shift left.)

Trimming

Trim up redundant nil checks, sequential assignments that could be on a shared line, and repeated references to a deeply nested variable.

Reviewed-on: pyrex/chameleonic#13
Co-authored-by: Kistaro Windrider <kistaro@gmail.com>
Co-committed-by: Kistaro Windrider <kistaro@gmail.com>
This commit is contained in:
Kistaro Windrider 2022-12-24 00:08:10 +00:00 committed by Pyrex
parent 8f65f884f2
commit c480295b41

View File

@ -438,7 +438,9 @@ function level:spawn_exit()
if (self:_mget(x,y)!=18) return
for nx=x-1,x+1 do
for ny=y-1,y+1 do
if nx<0 or ny<0 or nx>15 or ny>15 then
-- next check: is at least one of
-- nx or ny out of range [0, 15]?
if (nx | ny) & 0xFFF0 ~= 0 then
self._wins[_mix(nx,ny)]=true
end
end
@ -478,16 +480,18 @@ function _mix(mx,my)
return mx..","..my
end
-- crate spec:
-- "up" == 1
-- "right" == 2
-- "down" == 4
-- "left" == 8
--
-- +1+
-- 8 2
-- +4+
function level:_get_cratedef(s)
if (s<64 or s>=80) return nil
local s2=s-64
return {
up=s2&1!=0,
right=s2&2!=0,
down=s2&4!=0,
left=s2&8!=0
}
if (s<64 or s>=80) return
return s & 0x000F
end
function level:get_latch(dx,dy,px,py)
@ -498,12 +502,7 @@ function level:get_latch(dx,dy,px,py)
local dx1,dy1=-sgn0(dx),-sgn0(dy)
if crate then
if
(crate.def.up and dy>0) or
(crate.def.down and dy<0) or
(crate.def.left and dx>0) or
(crate.def.right and dx<0)
then
if crate.def & dy1*dy1*(2.5+1.5*dy1)+dx1*dx1*(5-3*dx1) ~= 0 then
return {
el="crate",
dx=dx1,dy=dy1,
@ -545,12 +544,12 @@ function level:can_move(
if player.rope then
local chk=false
local w,h=1.6,0.2
if (dmx==0) w,h=0.2,1.6
if (dmy==-1) rectx,recty=0.4,-0.8
if (dmy==1) rectx,recty=0.4,0.2
if (dmx==-1) rectx,recty=-0.8,0.4
if (dmx==1) rectx,recty=0.2,0.4
if dmx==0 then
w,h=0.2,1.6
else
dmy = 0
end
rectx,recty=dmx*(0.4>>>dmx),dmy*(0.4>>>dmy)
if (player.rope:collide_mrect(mx0+rectx,my0+recty,w,h,exclude_src,exclude_dst)) return false
end
@ -627,7 +626,7 @@ function player:update()
end
if kbd:btn(4) then
if kbd:btnp(4) and self.rope!=nil then
if kbd:btnp(4) and self.rope then
self.rope:destroy()
kbd:release(4)
end
@ -660,7 +659,7 @@ function player:update()
self.todo={{
update=function()
return self.rope==nil or self.rope:latched()
return not self.rope or self.rope:latched()
end
}}
elseif kbd:btnp(5) then
@ -765,7 +764,7 @@ function player:draw()
if self.rope then
local rx_adj,ry_adj=self.rope:affected_src_xy(rx,ry)
if rx_adj!=nil then
if rx_adj then
local drx,dry=rx_adj-rx,ry_adj-ry
rx,ry=rx+drx,ry+dry
px,py=px+drx,py+dry
@ -833,10 +832,7 @@ function rope:update()
elseif self.state.name=="latched" then
if (self.latch==nil) wrongbleep:bleep(5) self:destroy() return
if
self.latch!=nil and
self.latch.rec!=nil
then
if self.latch and self.latch.rec then
self:drag_src(
self.latch.rec.mx+0.5+self.latch.ax_offset,
self.latch.rec.my+0.5+self.latch.ay_offset
@ -898,11 +894,10 @@ end
function rope:draw(artificial_px,artificial_py)
local points,highlight=self:_tug(true)
if (self.state.name=="done") return
local perc_to_show=1.0
local from_end=false
if (self.state.name=="cast") perc_to_show=self.state.frame/2
if (self.state.name=="destroy") perc_to_show=(1.0-self.state.frame/8)^2
local n,perc_to_show,from_end = self.state.name,1.0,false
if (n=="done") return
if (n=="cast") perc_to_show=self.state.frame/2
if (n=="destroy") perc_to_show=(1.0-self.state.frame/8)^2
if (self.state.reelin) from_end=true
points[#points]={x=artificial_px,y=artificial_py}
@ -1191,7 +1186,7 @@ function rope:be_pushed_by(anchor,ax_old,ay_old)
local ax_new,ay_new=anchor.ax,anchor.ay
while true do
n1=n0.next
if (n1==nil) return
if (not n1) return
local nx0,ny0=n0.ax,n0.ay
local nx1,ny1=n1.ax,n1.ay
@ -1318,7 +1313,7 @@ function _stepfrom(x0,x1)
local done=false
if x0==x1 then
return function()
if (done) return nil
if (done) return
done=true return x0
end
end