File: resourceprovider.cpp

package info (click to toggle)
residualvm 0.3.1%2Bdfsg-1
  • links: PTS, VCS
  • area: contrib
  • in suites: buster
  • size: 31,284 kB
  • sloc: cpp: 227,029; sh: 7,256; xml: 1,731; perl: 1,067; java: 861; asm: 738; python: 691; ansic: 272; makefile: 139; objc: 81; sed: 22; php: 1
file content (384 lines) | stat: -rw-r--r-- 12,883 bytes parent folder | download | duplicates (2)
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
/* ResidualVM - A 3D game interpreter
 *
 * ResidualVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the AUTHORS
 * 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.
 *
 */

#include "engines/stark/services/resourceprovider.h"

#include "engines/stark/resources/bookmark.h"
#include "engines/stark/resources/camera.h"
#include "engines/stark/resources/floor.h"
#include "engines/stark/resources/item.h"
#include "engines/stark/resources/knowledgeset.h"
#include "engines/stark/resources/layer.h"
#include "engines/stark/resources/level.h"
#include "engines/stark/resources/location.h"
#include "engines/stark/resources/root.h"
#include "engines/stark/resources/script.h"
#include "engines/stark/resources/sound.h"

#include "engines/stark/services/services.h"
#include "engines/stark/services/archiveloader.h"
#include "engines/stark/services/global.h"
#include "engines/stark/services/stateprovider.h"
#include "engines/stark/services/userinterface.h"

namespace Stark {

ResourceProvider::ResourceProvider(ArchiveLoader *archiveLoader, StateProvider *stateProvider, Global *global) :
		_archiveLoader(archiveLoader),
		_stateProvider(stateProvider),
		_global(global),
		_locationChangeRequest(false),
		_restoreCurrentState(false),
		_nextDirection(0) {
}

void ResourceProvider::initGlobal() {
	// Load the root archive
	_archiveLoader->load("x.xarc");

	// Set the root tree
	Resources::Root *root = _archiveLoader->useRoot<Resources::Root>("x.xarc");
	_global->setRoot(root);

	// Resources lifecycle update
	root->onAllLoaded();

	// Find the global level node
	Resources::Level *global = root->findChildWithSubtype<Resources::Level>(Resources::Level::kGlobal);

	// Load the global archive
	Common::String globalArchiveName = _archiveLoader->buildArchiveName(global);
	_archiveLoader->load(globalArchiveName);

	// Set the global tree
	global = _archiveLoader->useRoot<Resources::Level>(globalArchiveName);
	_global->setLevel(global);

	// Resources lifecycle update
	global->onAllLoaded();

	// Load the state
	_stateProvider->restoreLevelState(global);

	_global->setInventory(global->findChildWithSubtype<Resources::KnowledgeSet>(Resources::KnowledgeSet::kInventory));
	_global->setApril(global->findChildWithSubtype<Resources::GlobalItemTemplate>(Resources::Item::kItemGlobalTemplate));
}

Current *ResourceProvider::findLevel(uint16 level) {
	for (CurrentList::const_iterator it = _locations.begin(); it != _locations.end(); it++) {
		if ((*it)->getLevel()->getIndex() == level) {
			return *it;
		}
	}

	return nullptr;
}

Current *ResourceProvider::findLocation(uint16 level, uint16 location) {
	for (CurrentList::const_iterator it = _locations.begin(); it != _locations.end(); it++) {
		if ((*it)->getLevel()->getIndex() == level
				&& (*it)->getLocation()->getIndex() == location) {
			return *it;
		}
	}

	return nullptr;
}

Resources::Level *ResourceProvider::getLevel(uint16 level) {
	Current *current = findLevel(level);

	if (current) {
		return current->getLevel();
	}

	return nullptr;
}

Resources::Location *ResourceProvider::getLocation(uint16 level, uint16 location) {
	Current *current = findLocation(level, location);

	if (current) {
		return current->getLocation();
	}

	return nullptr;
}

void ResourceProvider::pushAndChangeLocation(int16 level, int16 location) {
	pushCurrentLocation();
	requestLocationChange(level, location);
}

void ResourceProvider::returnToPushedLocation() {
	popCurrentLocation();
}

void ResourceProvider::pushCurrentLocation() {
	PreviousLocation current;
	current.level = _global->getCurrent()->getLevel()->getIndex();
	current.location = _global->getCurrent()->getLocation()->getIndex();
	current.inventoryOpen = StarkUserInterface->isInventoryOpen();

	_locationStack.push_back(current);

	StarkUserInterface->inventoryOpen(false);
}

void ResourceProvider::popCurrentLocation() {
	if (_locationStack.empty()) {
		error("Empty location stack");
	} else {
		PreviousLocation previous = _locationStack.back();
		_locationStack.pop_back();

		requestLocationChange(previous.level, previous.location);
		StarkUserInterface->inventoryOpen(previous.inventoryOpen);
	}
}

void ResourceProvider::requestLocationChange(uint16 level, uint16 location) {
	Current *currentLocation = new Current();
	_locations.push_back(currentLocation);

	// Retrieve the level archive name
	Resources::Root *root = _global->getRoot();
	Resources::Level *rootLevelResource = root->findChildWithIndex<Resources::Level>(level);
	Common::String levelArchive = _archiveLoader->buildArchiveName(rootLevelResource);

	// Load the archive, and get the resource sub-tree root
	bool newlyLoaded = _archiveLoader->load(levelArchive);
	currentLocation->setLevel(_archiveLoader->useRoot<Resources::Level>(levelArchive));

	// If we just loaded a resource tree, restore its state
	if (newlyLoaded) {
		currentLocation->getLevel()->onAllLoaded();
		_stateProvider->restoreLevelState(currentLocation->getLevel());
	}

	// Retrieve the location archive name
	Resources::Level *levelResource = currentLocation->getLevel();
	Resources::Location *levelLocationResource = levelResource->findChildWithIndex<Resources::Location>(location);
	Common::String locationArchive = _archiveLoader->buildArchiveName(levelResource, levelLocationResource);

	// Load the archive, and get the resource sub-tree root
	newlyLoaded = _archiveLoader->load(locationArchive);
	currentLocation->setLocation(_archiveLoader->useRoot<Resources::Location>(locationArchive));

	if (currentLocation->getLocation()->has3DLayer()) {
		Resources::Layer3D *layer = currentLocation->getLocation()->findChildWithSubtype<Resources::Layer3D>(Resources::Layer::kLayer3D);
		currentLocation->setFloor(layer->findChild<Resources::Floor>());
		currentLocation->setCamera(layer->findChild<Resources::Camera>());
	} else {
		currentLocation->setFloor(nullptr);
		currentLocation->setCamera(nullptr);
	}

	// If we just loaded a resource tree, restore its state
	if (newlyLoaded) {
		currentLocation->getLocation()->onAllLoaded();
		_stateProvider->restoreLocationState(currentLocation->getLevel(), currentLocation->getLocation());
	}

	_locationChangeRequest = true;
}

void ResourceProvider::performLocationChange() {
	Current *current = _locations.back();
	Current *previous = _global->getCurrent();
	bool levelChanged = !previous || previous->getLevel() != current->getLevel();

	// Exit the previous location
	if (previous) {
		// Trigger location change scripts
		if (levelChanged) {
			runLocationChangeScripts(previous->getLevel(), Resources::Script::kCallModeExitLocation);
		}
		runLocationChangeScripts(previous->getLocation(), Resources::Script::kCallModeExitLocation);

		// Resources lifecycle update
		previous->getLocation()->onExitLocation();
		previous->getLevel()->onExitLocation();
		_global->getLevel()->onExitLocation();
	}

	// Clear all pointers to location objects in the UI instances
	StarkUserInterface->clearLocationDependentState();

	// Set the new current location
	_global->setCurrent(current);

	// Resources lifecycle update
	_global->getLevel()->onEnterLocation();
	current->getLevel()->onEnterLocation();
	current->getLocation()->onEnterLocation();

	if (current->getLocation()->has3DLayer()) {
		// Fetch the scene item for April
		current->setInteractive(Resources::Object::cast<Resources::ModelItem>(_global->getApril()->getSceneInstance()));
	}

	if (_restoreCurrentState) {
		_stateProvider->restoreGlobalState(_global->getLevel());
		_stateProvider->restoreCurrentLevelState(current->getLevel());
		_stateProvider->restoreCurrentLocationState(current->getLevel(), current->getLocation());
		_restoreCurrentState = false;
	} else {
		setAprilInitialPosition();
		setScrollInitialPosition();

		// Trigger location change scripts
		if (levelChanged) {
			runLocationChangeScripts(current->getLevel(), Resources::Script::kCallModeEnterLocation);
		}
		runLocationChangeScripts(current->getLocation(), Resources::Script::kCallModeEnterLocation);
	}

	purgeOldLocations();

	_locationChangeRequest = false;
}

void ResourceProvider::runLocationChangeScripts(Resources::Object *resource, uint32 scriptCallMode) {
	Common::Array<Resources::Script *> scripts = resource->listChildrenRecursive<Resources::Script>();

	if (scriptCallMode == Resources::Script::kCallModeEnterLocation) {
		for (uint i = 0; i < scripts.size(); i++) {
			scripts[i]->reset();
		}
	}

	for (uint i = 0; i < scripts.size(); i++) {
		scripts[i]->execute(scriptCallMode);
	}

	if (scriptCallMode == Resources::Script::kCallModeExitLocation) {
		Common::Array<Resources::Sound *> sounds = resource->listChildrenRecursive<Resources::Sound>();
		for (uint i = 0; i < sounds.size(); i++) {
			sounds[i]->stop();
		}
	}
}

void ResourceProvider::setNextLocationPosition(const ResourceReference &bookmark, int32 direction) {
	_nextPositionBookmarkReference = bookmark;
	_nextDirection = direction;
}

void ResourceProvider::setAprilInitialPosition() {
	Current *current = _global->getCurrent();
	Resources::ModelItem *april = current->getInteractive();
	if (!april) {
		return; // No character
	}

	// Set the initial location for April
	if (!_nextPositionBookmarkReference.empty()) {
		Resources::Bookmark *position = _nextPositionBookmarkReference.resolve<Resources::Bookmark>();
		april->placeOnBookmark(position);
		april->setDirection(_nextDirection);
	} else if (april->getFloorFaceIndex() <= 0) {
		// No target location provided, place April on the first floor face
		april->placeDefaultPosition();
	}

	_nextPositionBookmarkReference = ResourceReference();
	_nextDirection = 0;
}

void ResourceProvider::setScrollInitialPosition() {
	Current *current = _global->getCurrent();
	Resources::Location *location = current->getLocation();
	location->scrollToCharacterImmediate();
}

void ResourceProvider::purgeOldLocations() {
	while (_locations.size() > 2) {
		Current *location = _locations.front();

		_stateProvider->saveLocationState(location->getLevel(), location->getLocation());
		_stateProvider->saveLevelState(location->getLevel());

		_archiveLoader->returnRoot(_archiveLoader->buildArchiveName(location->getLevel(), location->getLocation()));
		_archiveLoader->returnRoot(_archiveLoader->buildArchiveName(location->getLevel()));

		delete location;

		_locations.pop_front();
	}

	_archiveLoader->unloadUnused();
}

void ResourceProvider::commitActiveLocationsState() {
	// Save active location states
	for (CurrentList::const_iterator it = _locations.begin(); it != _locations.end(); it++) {
		_stateProvider->saveLocationState((*it)->getLevel(), (*it)->getLocation());
		_stateProvider->saveLevelState((*it)->getLevel());
	}

	_stateProvider->saveLevelState(_global->getLevel());

	// Save the current location "extended" state, to be able to restore them to the exact same state.
	Current *location = _global->getCurrent();
	_stateProvider->saveCurrentLocationState(location->getLevel(), location->getLocation());
	_stateProvider->saveCurrentLevelState(location->getLevel());

	_stateProvider->saveGlobalState(_global->getLevel());
}

void ResourceProvider::shutdown() {
	// Flush the locations list
	for (CurrentList::const_iterator it = _locations.begin(); it != _locations.end(); it++) {
		Current *location = *it;

		_archiveLoader->returnRoot(_archiveLoader->buildArchiveName(location->getLevel(), location->getLocation()));
		_archiveLoader->returnRoot(_archiveLoader->buildArchiveName(location->getLevel()));

		delete location;
	}
	_locations.clear();

	// Return the global resources
	_archiveLoader->returnRoot(_archiveLoader->buildArchiveName(_global->getLevel()));
	_archiveLoader->returnRoot("x.xarc");

	_global->setLevel(nullptr);
	_global->setRoot(nullptr);
	_global->setCurrent(nullptr);
	_global->setInventory(nullptr);
	_global->setApril(nullptr);

	_archiveLoader->unloadUnused();
}

Resources::Level *ResourceProvider::getLevelFromLocation(Resources::Location *location) {
	for (CurrentList::const_iterator it = _locations.begin(); it != _locations.end(); it++) {
		if ((*it)->getLocation() == location) {
			return (*it)->getLevel();
		}
	}

	return nullptr;
}

} // End of namespace Stark