simulator: fix beginning-of-episode detection and simplify the end-of-episode calculation