Well, I don't want to refuse the excellent explanations of Dany's twisted, ruthless logic offered by ariich and RuRoRol, but these two points kinda reasonate with me:
- There is a difference between foreshadowing and telecasting something. Her going mad the way she did was telecasted this episode.
- They were NOT building to her murdering the masses outside of earlier in the same episode. They were building to her being a ruthless conqueror who wanted the throne above all. They only included the "I will rule by fear" in this episode. So no, it has not be foreshadowed for seasons that she would ruthlessly murder women, children and innocents. Her murdering masters, and slavers, and queens, and kings, and her enemies is NOT the same as murdering civilians. Sorry. Her deciding that she will rule by fear by murdering everyone was a very very very recent development, not something long established.
The "telecasting" for me came also from the voiceovers at the beginning of the episode, with quotes from everyone about the Targaryen being nuts, even picking up old Maester Aemon's quote about "A Targaryen alone in the world is a terrible thing". It's like they made Cersei saying stuff like "I hate children, I want the Mountain to stomp them all" and then add in the "previously on" segment the quote from Tyrion "you love your children, that's your redeeming quality" to sell better her "I don't want my child to die" at the end. No need for that however 'cause her character traits were well estabilished.
About Cersei's demise... let's remember we're all talking about fictional characters portrayed by actors, otherwise we'd all feel monsters for cheering at a teenage kid dying horribly in front of their helpless parents
but that's what we all cheered for when Joffrey died, and we all cheered at dogs eating Ramsay, so I wanted to cheer as well for the death of that stupid bitch of Cersei.
I won't disagree that dying defeated, alone and full of fear is not satisfying, but she also died in the arms of the person she loved comforting her. Too easy for such a monster who basically nuked the Vatican on top of all the other hateful and spiteful things she did. I believe the situation was a "victim" of Lena Headey's talent - she's been fantastic all the series through, and she could masterfully sell the desperation and fear of Cersei, it was probably a waste of her talents to let her die smirking, but damn I wanted to see that, I wanted to enjoy her death, not almost feel pity for a helpless and afraid pregnant woman.
Going back to the authors' masterplan... I believe they wanted to really, really adapt the Red Wedding, that was their dream. After that, they sort of winged it.
And when I said before "they said Dany's decision was a spur of the moment and that's it", I wasn't completely literaly with my "that's it". Of course there's more beyond a single sentence given in an interview. But I wouldn't be too surprised if, while trying to reach the end point of "Dany goes batshit insane", the answer to "how do we push her from ruthless but somehow rational conqueror to mass murderer of women and children?" was something just slightly more complex than "Eh, let's have her snap in the heat of the moment".
About what to change.... as I've read online somewhere else, it's so much easier to make changes to someone else's finished work, than to come up with a full story of your own. It's so easy so say "I would have done this and that" and tweak the author's story. With this premise, I like also one of the proposals I've read online - having Rhaegal just being wounded at the Iron Fleet ambush, so that he would be most useless in the attack of King's Landing, and then having Euron, survivor of the Iron Fleet destruction, finding somehow a ballista still working and still be the one that takes Rhaegal out. Have all the people of King's Landing cheering at the death of the "monster" - Dany seeing the people that are supposed to love her actually cheer at the violent death of her child would be more than enough to sell her "Burn them all" mentality.
Regarding the Dorne plot... I think it could have been saved with just some minor tweakings as well, maybe I'll go in fanfic mode and attempt to change the final scene of Season 5 later