Always set "distance to player" to absolute value of difference in x coordinates.
Set "direction towards player"
If player if facing, set "player is facing me" to 1
Every 01"-12, whilst not moving, randomize direction to left or right.
Always countdown from 40.
When count down hits 0- "distance to player" greater or equal to 80: Set direction towards player based on "direction towards player", and set speed from 17 to 22.
- "distance to player" lower or equal to 80, and "player is facing me" = 0: Set direction towards player based on "direction towards player", and set speed to 40.
- "distance to player" lower or equal to 80, and "player is facing me" = 1: Set direction away from player based on "direction towards player", and set speed from 17 to 22.