I can barely understand what you say, would you have time to do it, Fergus?
Here it is with comments to indicate the main changes I made. I have not tested this. It will be up to you to do that.
sub stalemated kingpos:
store;
local from piece to movetype ltm;
// store the value of last_type_moved into another variable
set ltm last_type_moved;
set movetype MOVE;
if isupper space #kingpos:
def friends onlyupper;
def friend isupper #0;
set cspaces var wcastle;
else:
def friends onlylower;
def friend islower #0;
set cspaces var bcastle;
endif;
// While the royal piece is called the King in these comments,
// it may be any piece. These variables determine what the royal piece is.
set royal space var kingpos;
store;
// Can any piece legally move?
for (from piece) fn friends:
for to fn join const alias #piece "-Range" #from:
if fn const alias #piece #from #to and not fn friend space #to and onboard #to:
move #from #to;
if not sub checked cond == #from #kingpos #to #kingpos:
setlegal #from #to;
// Whenever a piece is moved for the sake of trying out a potential move, the value
// of last_type_moved should be changed to match the type of the piece moved.
if isupper #piece:
if != const alias #piece White_Joker:
if != const alias #piece White_Pawn:
set last_type_moved const alias #piece;
else:
set last_type_moved Black_Barren_Pawn;
endif;
endif;
elseif != const alias #piece Black_Joker:
if != const alias #piece Black_Pawn:
set last_type_moved const alias #piece;
else:
set last_type_moved White_Barren_Pawn;
endif;
endif;
endif;
endif;
restore;
// Each time the evaluation of a potential move is over, and the position has been
// restored, the value of last_type_moved should be restored to its original value.
set last_type_moved #ltm;
next;
next;
// Castling code removed, since Apothecary Chess does not include castling.
// All done. Set $legalmoves and return;
return cond count system legalmoves false true;
endsub;
Here it is with comments to indicate the main changes I made. I have not tested this. It will be up to you to do that.