Alternatively if you just want to copy&paste code: FOV / Field of Vision for NPC AI
Yuka is worth a look in any case!