Sorry I'm late, but I think there is a problem with

if(self.flags & FL_ONGROUND)
{
self.crouch = 0;
}
...