From fe3a68284fe74fe127a34e13e95c4b1ce2c9cd2a Mon Sep 17 00:00:00 2001 From: Kistaro Windrider Date: Thu, 22 Dec 2022 22:49:27 -0800 Subject: [PATCH 1/6] 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. --- chameleonic.p8 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/chameleonic.p8 b/chameleonic.p8 index 812c428..0d28ada 100644 --- a/chameleonic.p8 +++ b/chameleonic.p8 @@ -438,7 +438,7 @@ 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 + if (nx | ny) & 0xFFF0 ~= 0 then self._wins[_mix(nx,ny)]=true end end -- 2.34.1 From b10447bb865d1728221b038db9791a2f47f177b1 Mon Sep 17 00:00:00 2001 From: Kistaro Windrider Date: Thu, 22 Dec 2022 22:50:34 -0800 Subject: [PATCH 2/6] comment the bullshit it needs it --- chameleonic.p8 | 2 ++ 1 file changed, 2 insertions(+) diff --git a/chameleonic.p8 b/chameleonic.p8 index 0d28ada..d91f9e5 100644 --- a/chameleonic.p8 +++ b/chameleonic.p8 @@ -438,6 +438,8 @@ 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 + -- 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 -- 2.34.1 From 0fe3b1699f3ce2cf3ca6f5466fa8f9930563dda0 Mon Sep 17 00:00:00 2001 From: Kistaro Windrider Date: Thu, 22 Dec 2022 23:15:03 -0800 Subject: [PATCH 3/6] 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. --- chameleonic.p8 | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/chameleonic.p8 b/chameleonic.p8 index d91f9e5..498cf3f 100644 --- a/chameleonic.p8 +++ b/chameleonic.p8 @@ -480,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) @@ -500,12 +502,11 @@ 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 >>> 15 | + (dy1 & 0x1) << 2 | + (dx1 & 0x8000) >>> 12 | + (dx1 & 0x1) << 1) ~= 0 then return { el="crate", dx=dx1,dy=dy1, -- 2.34.1 From 9d93f30426921a8b48ef6a57cf92879d9cd2a67c Mon Sep 17 00:00:00 2001 From: Kistaro Windrider Date: Fri, 23 Dec 2022 00:02:02 -0800 Subject: [PATCH 4/6] 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. --- chameleonic.p8 | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/chameleonic.p8 b/chameleonic.p8 index 498cf3f..a711592 100644 --- a/chameleonic.p8 +++ b/chameleonic.p8 @@ -502,11 +502,7 @@ function level:get_latch(dx,dy,px,py) local dx1,dy1=-sgn0(dx),-sgn0(dy) if crate then - if crate.def & ( - dy1 >>> 15 | - (dy1 & 0x1) << 2 | - (dx1 & 0x8000) >>> 12 | - (dx1 & 0x1) << 1) ~= 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, -- 2.34.1 From 19955015839065c16bf60ef239102d6950f0bb3f Mon Sep 17 00:00:00 2001 From: Kistaro Windrider Date: Fri, 23 Dec 2022 00:18:50 -0800 Subject: [PATCH 5/6] 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.) --- chameleonic.p8 | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/chameleonic.p8 b/chameleonic.p8 index a711592..aabe1c0 100644 --- a/chameleonic.p8 +++ b/chameleonic.p8 @@ -544,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 -- 2.34.1 From 528e67045dce0a2c95953b520add1fd484d21604 Mon Sep 17 00:00:00 2001 From: Kistaro Windrider Date: Fri, 23 Dec 2022 00:34:00 -0800 Subject: [PATCH 6/6] Trimming Trim up redundant nil checks, sequential assignments that could be on a shared line, and repeated references to a deeply nested variable. --- chameleonic.p8 | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/chameleonic.p8 b/chameleonic.p8 index aabe1c0..8688ebf 100644 --- a/chameleonic.p8 +++ b/chameleonic.p8 @@ -626,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 @@ -659,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 @@ -764,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 @@ -832,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 @@ -897,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} @@ -1190,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 @@ -1317,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 -- 2.34.1