aboutsummaryrefslogtreecommitdiff
path: root/engines/saga/resource_res.cpp
blob: 2cc12747afb7f41321edd4bf1fd4d3fefd406421 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/* ScummVM - Graphic Adventure Engine
 *
 * ScummVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.

 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * $URL$
 * $Id$
 *
 */

// RSC Resource file management module (SAGA 1, used in IHNM)

#include "saga/saga.h"

#include "saga/actor.h"
#include "saga/animation.h"
#include "saga/interface.h"
#include "saga/music.h"
#include "saga/resource.h"
#include "saga/scene.h"
#include "saga/sndres.h"

#include "engines/advancedDetector.h"

namespace Saga {

#ifdef ENABLE_IHNM

static int metaResourceTable[] = { 0, 326, 517, 677, 805, 968, 1165, 0, 1271 };
static int metaResourceTableDemo[] = { 0, 0, 0, 0, 0, 0, 0, 285, 0 };

void Resource_RES::loadGlobalResources(int chapter, int actorsEntrance) {
	if (chapter < 0)
		chapter = !_vm->isIHNMDemo() ? 8 : 7;

	_vm->_script->_globalVoiceLUT.clear();

	ResourceContext *resourceContext;
	ResourceContext *soundContext;
	uint i;

	resourceContext = _vm->_resource->getContext(GAME_RESOURCEFILE);
	if (resourceContext == NULL) {
		error("Resource::loadGlobalResources() resource context not found");
	}

	soundContext = _vm->_resource->getContext(GAME_SOUNDFILE);
	if (soundContext == NULL) {
		error("Resource::loadGlobalResources() sound context not found");
	}

	ByteArray resourceData;

	if (!_vm->isIHNMDemo()) {
		_vm->_resource->loadResource(resourceContext, metaResourceTable[chapter], resourceData);
	} else {
		_vm->_resource->loadResource(resourceContext, metaResourceTableDemo[chapter], resourceData);
	}

	if (resourceData.empty()) {
		error("Resource::loadGlobalResources wrong metaResource");
	}

	{
		ByteArrayReadStreamEndian metaS(resourceData);

		_metaResource.sceneIndex = metaS.readSint16LE();
		_metaResource.objectCount = metaS.readSint16LE();
		_metaResource.objectsStringsResourceID = metaS.readSint32LE();
		_metaResource.inventorySpritesID = metaS.readSint32LE();
		_metaResource.mainSpritesID = metaS.readSint32LE();
		_metaResource.objectsResourceID = metaS.readSint32LE();
		_metaResource.actorCount = metaS.readSint16LE();
		_metaResource.actorsStringsResourceID = metaS.readSint32LE();
		_metaResource.actorsResourceID = metaS.readSint32LE();
		_metaResource.protagFaceSpritesID = metaS.readSint32LE();
		_metaResource.field_22 = metaS.readSint32LE();
		_metaResource.field_26 = metaS.readSint16LE();
		_metaResource.protagStatesCount = metaS.readSint16LE();
		_metaResource.protagStatesResourceID = metaS.readSint32LE();
		_metaResource.cutawayListResourceID = metaS.readSint32LE();
		_metaResource.songTableID = metaS.readSint32LE();
	}

	_vm->_actor->loadActorList(actorsEntrance, _metaResource.actorCount,
						  _metaResource.actorsResourceID, _metaResource.protagStatesCount,
						  _metaResource.protagStatesResourceID);

	_vm->_actor->_protagonist->_sceneNumber = _metaResource.sceneIndex;

	_vm->_actor->_objectsStrings.clear();

	_vm->_resource->loadResource(resourceContext, _metaResource.objectsStringsResourceID, resourceData);
	_vm->loadStrings(_vm->_actor->_objectsStrings, resourceData);

	if (uint(chapter) >= _vm->_sndRes->_fxTableIDs.size()) {
		error("Chapter ID exceeds fxTableIDs length");
	}

	debug(0, "Going to read %d of %d", chapter, _vm->_sndRes->_fxTableIDs[chapter]);
	_vm->_resource->loadResource(soundContext, _vm->_sndRes->_fxTableIDs[chapter],
								 resourceData);

	if (resourceData.empty()) {
		error("Resource::loadGlobalResources Can't load sound effects for current track");
	}

	_vm->_sndRes->_fxTable.resize(resourceData.size() / 4);

	{
		ByteArrayReadStreamEndian fxS(resourceData);

		for (i = 0; i < _vm->_sndRes->_fxTable.size(); i++) {
			_vm->_sndRes->_fxTable[i].res = fxS.readSint16LE();
			_vm->_sndRes->_fxTable[i].vol = fxS.readSint16LE();
		}
	}

	_vm->_interface->_defPortraits.clear();
	_vm->_sprite->loadList(_metaResource.protagFaceSpritesID, _vm->_interface->_defPortraits);

	_vm->_actor->_actorsStrings.clear();

	_vm->_resource->loadResource(resourceContext, _metaResource.actorsStringsResourceID, resourceData);
	_vm->loadStrings(_vm->_actor->_actorsStrings, resourceData);

	_vm->_sprite->_inventorySprites.clear();
	_vm->_sprite->loadList(_metaResource.inventorySpritesID, _vm->_sprite->_inventorySprites);

	_vm->_sprite->_mainSprites.clear();
	_vm->_sprite->loadList(_metaResource.mainSpritesID, _vm->_sprite->_mainSprites);

	_vm->_actor->loadObjList(_metaResource.objectCount, _metaResource.objectsResourceID);

	_vm->_resource->loadResource(resourceContext, _metaResource.cutawayListResourceID, resourceData);

	if (resourceData.empty()) {
		error("Resource::loadGlobalResources Can't load cutaway list");
	}

	_vm->_anim->loadCutawayList(resourceData);

	if (_metaResource.songTableID > 0) {
		_vm->_resource->loadResource(resourceContext, _metaResource.songTableID, resourceData);

		if (chapter == 6) {
			if (resourceData.size() < (uint(actorsEntrance) * 4 + 4)) {
				error("Resource::loadGlobalResources chapter 6 has wrong resource");
			}
			int32 id = READ_LE_UINT32(&resourceData[actorsEntrance * 4]);
			_vm->_resource->loadResource(resourceContext, id, resourceData);
		}

		if (resourceData.empty()) {
			error("Resource::loadGlobalResources Can't load songs list for current track");
		}

		_vm->_music->_songTable.resize(resourceData.size() / 4);

		ByteArrayReadStreamEndian songS(resourceData);

		for (i = 0; i < _vm->_music->_songTable.size(); i++)
			_vm->_music->_songTable[i] = songS.readSint32LE();
	} else {
		// The IHNM demo has a fixed music track and doesn't load a song table
		_vm->_music->setVolume(_vm->_musicVolume, 1);
		_vm->_music->play(3, MUSIC_LOOP);
	}

	int voiceLUTResourceID = 0;

	if (chapter != 7) {
		int voiceBank = (chapter == 8) ? 0 : chapter;
		_vm->_sndRes->setVoiceBank(voiceBank);
		voiceLUTResourceID = 22 + voiceBank;
	} else {
		// IHNM demo
		_vm->_sndRes->setVoiceBank(0);
		voiceLUTResourceID = 17;
	}

	if (voiceLUTResourceID) {
		_vm->_resource->loadResource(resourceContext, voiceLUTResourceID, resourceData);
		_vm->_script->loadVoiceLUT(_vm->_script->_globalVoiceLUT, resourceData);
	}

	_vm->_spiritualBarometer = 0;
	_vm->_scene->setChapterNumber(chapter);
}

void ResourceContext_RES::processPatches(Resource *resource, const GamePatchDescription *patchFiles) {
	uint16 subjectResourceType;
	ResourceContext *subjectContext;
	uint32 subjectResourceId;
	uint32 patchResourceId;
	ResourceData *subjectResourceData;
	ResourceData *resourceData;

	// Process internal patch files
	if (_fileType & GAME_PATCHFILE) {
		subjectResourceType = ~GAME_PATCHFILE & _fileType;
		subjectContext = resource->getContext((GameFileTypes)subjectResourceType);
		if (subjectContext == NULL) {
			error("ResourceContext::load() Subject context not found");
		}
		ByteArray tableBuffer;

		resource->loadResource(this, _table.size() - 1, tableBuffer);

		ByteArrayReadStreamEndian readS2(tableBuffer, _isBigEndian);
		for (uint32 i = 0; i < tableBuffer.size() / 8; i++) {
			subjectResourceId = readS2.readUint32();
			patchResourceId = readS2.readUint32();
			subjectResourceData = subjectContext->getResourceData(subjectResourceId);
			resourceData = getResourceData(patchResourceId);
			subjectResourceData->patchData = new PatchData(&_file, _fileName);
			subjectResourceData->offset = resourceData->offset;
			subjectResourceData->size = resourceData->size;
		}
	}
}

#endif

} // End of namespace Saga