the unpleasant onesBad dreams are rehearsals
A nightmare is your brain running a threat with the volume up and no real risk attached. One decent theory is that it is practice, another is that it takes the sting out of memories by replaying them without the panic chemicals.
When that process jams, as it does in trauma, the same nightmare loops instead of settling. There is a treatment for it: you rehearse a new ending while awake, night after night, until the dream takes the note.