Fix NULL-pointer issue in "cast_spell_on_npc"

Fixed a NULL-pointer issue in "cast_spell_on_npc" and improved the
function's efficiency.