From ece80a4a1f82abcef5c5aa0c12f665a89d7c85e2 Mon Sep 17 00:00:00 2001
From: Eugene Sandulenko <sev@scummvm.org>
Date: Wed, 28 Jan 2004 04:07:54 +0000
Subject: [PATCH] Partial fix for bug #885488 (Some INSANE trouble)

 o Finally fixed seek error. Now I never encounter it, though there are weird
   SAUD errors in some cases.
 o Started to hunt cave-dude-only bug

svn-id: r12653
---
 scumm/insane/insane.cpp        |  3 ---
 scumm/insane/insane_iact.cpp   | 18 ++++++++++++++----
 scumm/insane/insane_scenes.cpp |  2 +-
 scumm/smush/chunk.cpp          |  1 +
 scumm/smush/smush_player.cpp   | 16 +++++++---------
 5 files changed, 23 insertions(+), 17 deletions(-)

diff --git a/scumm/insane/insane.cpp b/scumm/insane/insane.cpp
index 6aa7003839e..23b9ee7ae63 100644
--- a/scumm/insane/insane.cpp
+++ b/scumm/insane/insane.cpp
@@ -40,10 +40,7 @@
 
 // TODO (in no particular order):
 // o Ben's velocity don't get zeroed after crash
-// o Mine road used to have correct behaviour with ESC but now is not.
-//   Instead of skipping portions of road it just restarts
 // o Road signs are not aligned properly
-// o With goggles on there is a seek error
 // o SAUD complaining again
 // o Insane::postCase16() has workaround. Cockpit is not transparent so it is
 //   disabled now
diff --git a/scumm/insane/insane_iact.cpp b/scumm/insane/insane_iact.cpp
index 34469ea8368..a0ff7153e00 100644
--- a/scumm/insane/insane_iact.cpp
+++ b/scumm/insane/insane_iact.cpp
@@ -200,7 +200,7 @@ void Insane::iactScene1(byte *renderBitmap, int32 codecparam, int32 setupsan12,
 }
 
 void Insane::proc62(void) {
-	if (readArray(58) == 0)
+	if (readArray(58) != 0)
 		_enemy[EN_TORQUE].field_10 = 1;
 
 	if (_enemy[EN_TORQUE].field_8 == 0) {
@@ -217,15 +217,22 @@ void Insane::proc62(void) {
 
 	int32 en, edi, ebp, edx, esi, eax, ebx, ecx;
 
-	for (en = 0; _enemy[en].field_10 == 0; en++);
-	en -= 4;
+	edx = eax = 0;
+
+	for (eax = 0; eax < 9; eax++)
+		if (_enemy[eax].field_10 == 0)
+			edx++;
+	  
+	edx -= 4;
+
+	en = edx;
 
 	ebp = 0;
 	edi = 0;
 
 	_loop1:
 	edi++;
-	if (edi > 14)
+	if (edi >= 14)
 		goto loc5;
 
 	edx = rand() / 11;
@@ -351,6 +358,9 @@ void Insane::proc63(void) {
 }
 
 void Insane::proc64(int32 enemy1) {
+	if (enemy1 >= _val215d)
+		return;
+
 	_val215d--;
 
 	for (int en = enemy1; en < _val215d; en++)
diff --git a/scumm/insane/insane_scenes.cpp b/scumm/insane/insane_scenes.cpp
index fce17465adc..aca375ce634 100644
--- a/scumm/insane/insane_scenes.cpp
+++ b/scumm/insane/insane_scenes.cpp
@@ -1052,7 +1052,7 @@ void Insane::postCase16(byte *renderBitmap, int32 codecparam, int32 setupsan12,
 	smlayer_showStatusMsg(-1, renderBitmap, codecparam, 140, 168, 1, 2, 0, "%s", buf);
 	smlayer_showStatusMsg(-1, renderBitmap, codecparam, 170, 43, 1, 2, 0, "%s", buf);
 
-	// FIXME: it should be transparent
+	// FIXME: it should be transparent, so now it is disabled
 	//smlayer_drawSomething(renderBitmap, codecparam, 0, 0, 1, _smush_bensgoggNut, 0, 0, 0);
 	
 	if (!_val124_)
diff --git a/scumm/smush/chunk.cpp b/scumm/smush/chunk.cpp
index 9962ef362ec..6df6a7fb742 100644
--- a/scumm/smush/chunk.cpp
+++ b/scumm/smush/chunk.cpp
@@ -191,6 +191,7 @@ uint32 FileChunk::getDword() {
 
 void FileChunk::reinit(uint32 offset) {
 	_offset = 0;
+	_data->seek(0);
 	_type = _data->readUint32BE();
 	_size = _data->readUint32BE();
 	_curPos = 0;
diff --git a/scumm/smush/smush_player.cpp b/scumm/smush/smush_player.cpp
index fa9dd24a15a..227f3b601cd 100644
--- a/scumm/smush/smush_player.cpp
+++ b/scumm/smush/smush_player.cpp
@@ -872,6 +872,9 @@ void SmushPlayer::parseNextFrame() {
 	case TYPE_FRME:
 		handleFrame(*sub);
 		break;
+	case TYPE_AHDR: // FT INSANE may seek file to the beginning
+		handleAnimHeader(*sub);
+		break;
 	default:
 		error("Unknown Chunk found at %x: %x, %d", _base->tell(), sub->getType(), sub->getSize());
 	}
@@ -964,28 +967,23 @@ void SmushPlayer::insanity(bool flag) {
 }
 
 void SmushPlayer::seekSan(const char *file, const char *directory, int32 pos, int32 contFrame) {
-	Chunk *sub;
-
 	if (file) {
 		if (_base)
 			delete _base;
 
 		_base = new FileChunk(file, directory);
-		pos = 0;
+		if (pos >= 8)
+			pos -= 8;
 	} else {
 		_base->reinit(pos);
 	}
 
 	if (pos != 8 && pos) {
-		_base->seek(pos, FileChunk::seek_start);
 		_middleAudio = true;
-	} else {
-		_base->seek(pos, FileChunk::seek_start);
-		sub = _base->subBlock();
-		checkBlock(*sub, TYPE_AHDR);
-		handleAnimHeader(*sub);
 	}
 
+	_base->seek(pos, FileChunk::seek_start);
+
 	_frame = contFrame;
 }