Prevent use of an invalid index in NPC AI code.

Thanks jkaessens for providing the backtrace that enabled solving it.
Addresses #1201