From 003e8b7bd02c2e273d3e78f69c15f362b789b0de Mon Sep 17 00:00:00 2001
From: Tim Felgentreff <timfelgentreff@gmail.com>
Date: Mon, 30 May 2022 22:20:09 +0200
Subject: [PATCH] fix lazy mng loading

---
 src/sound/sound.cpp | 2 +-
 src/video/mng.cpp   | 6 ++++++
 2 files changed, 7 insertions(+), 1 deletion(-)

diff --git a/src/sound/sound.cpp b/src/sound/sound.cpp
index 8b8acf5d9..144b7dfdf 100644
--- a/src/sound/sound.cpp
+++ b/src/sound/sound.cpp
@@ -306,7 +306,7 @@ void PlayUnitSound(const CUnit &unit, UnitVoiceGroup voice, bool sampleUnique)
 	SetChannelStereo(channel, CalculateStereo(unit));
 #ifdef USE_MNG
 	const CUnitType &type = *unit.Type;
-	if (type.Portrait.Num && type.Portrait.Talking) {
+	if (type.Portrait.Num && type.Portrait.Talking && type.Portrait.Mngs) {
 		type.Portrait.Mngs[type.Portrait.CurrMng]->Reset();
 		type.Portrait.CurrMng = (MyRand() % (type.Portrait.Num - type.Portrait.Talking)) + type.Portrait.Talking;
 		type.Portrait.NumIterations = 1;
diff --git a/src/video/mng.cpp b/src/video/mng.cpp
index 2f88e5d04..4f665e549 100644
--- a/src/video/mng.cpp
+++ b/src/video/mng.cpp
@@ -246,6 +246,9 @@ void Mng::Free(Mng *mng)
 */
 bool Mng::Load()
 {
+	if (handle) {
+		return handle != MNG_NULL && surface && iteration != 0x7fffffff;
+	}
 	handle = mng_initialize(this, my_alloc, my_free, MNG_NULL);
 	if (handle == MNG_NULL) {
 		return false;
@@ -277,6 +280,9 @@ bool Mng::Load()
 */
 void Mng::Reset()
 {
+	if (!handle) {
+		return;
+	}
 	mng_display_reset(handle);
 	iteration = 0;
 	mng_display(handle);