Note: this patch disables you from sliding and jumping off the last wall that you have jumped off of (or the last “direction” you jump away from). This is not the case on the other Mario games on the method of preventing the player from gaining height from the same wall by merely relying on a timer that prevents the player from returning back to the wall too soon.
The culprit is the RAM address !flags ($61 by default) stores its data bitwise like this: ----LRlr
r is when you are sliding down a left-facing wall (you hold right on the D-pad), value will be #$01 (#%00000001)
l is when you are sliding down a right-facing wall (you hold left on the D-pad), value will be #$02 (#%00000010)
R is when you jumped off left-facing wall (you jump towards the left), value will be #$04 (#%00000100)
L is when you jumped off right-facing wall (you jump towards the right), value will be #$08 (#%00001000)
After jumping off the wall, the value in RAM defined as !flags remains #$04 or #$08 until the player is on the ground. While the RAM is in this state, the player cannot jump off the wall of the direction he have jumped off of:
If #$04, disable sliding and jumping off a leftwards-facing wall
If #$08, disable sliding and jumping off a rightwards-facing wall
See here (I added comments):
LDA !flags ;\>----LRlrLSR ;|>0----LRlLSR ;|>00----LRAND $77 ;|>Blocked status: SxxMUDLRBNE .return ;/>If result both bits 0 and 1 clear, return (not touching any walls)
LDA $15 ;\Controller: byetUDLR
AND #$03 ;|>000000LR
CMP #$03 ;|\If both L and R pressed, return.
BEQ .return ;//
LDA $15 ;\Return if ((PressingLeft and BlockedLeft being set)||(PressingRight and BlockedRight being set)).
AND $77 ;|
BEQ .return ;/
Changed the hijack and merged into one patch. Tested on pre- and post-3.00 Lunar Magic, with and without SA-1 with no errors observed.
The implementation and physics of the wall kick provided by this patch are very well done, and I frequently use it as a base myself. I kind of wish it had a couple more configurable options, like allowing jumping off the same wall twice, or allowing sliding down a wall even if you can't jump off of it. I'd also like to see the wall jump flags reset if the player falls into water or climbs on something. Even without these extras, though, this patch is well-built and worth considering for any project looking to spice up its movement options.
EDIT: Fixed a small error that crept up right after approval, my mistake. Everything ought to be in order now.