Your simple logic breaks down at the end of the turn an automated worker finishes their current task
Start of Turn N
- Worker is automated and working
- Escort has moves set to zero by your code
- Player makes moves other units and clicks Next Turn
- "Auto-player" moves automated units, worker completes task
Start of Turn N+1
- Worker is automated, but not working
- Escort is unchanged by your code
- Player makes moves and clicks Next Turn
- "Auto-player" moves worker off towards next task (say due west)
- "Auto-player" moves escort (which is "automated explore" towards the fog (say due east)
- (Barbarians come from the west and capture the un-escorted worker)
Start Turn N+2
- (Assuming worker hasn't been captured yet) Worker is automated and unescorted
- Your code detects this and starts moving the automated escort back due west, however this escort is now playing "catch-up" until the worker reaches their next target and starts working, so the worker will be unescorted for M+2 turns where M is the number of turns it takes the worker to reach the target plot
- An automated scouting unit (could be a scout, horseman, whatever) passes nearer to the automated worker than the old escort (who is 4 tiles away)
Start Turn N+3
[*]Depending how the code is written, automated scouting unit is re-deployed as the escort, old escort becomes scouting unit