File: scene.cpp

package info (click to toggle)
scummvm 2.9.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 450,268 kB
  • sloc: cpp: 4,297,604; asm: 28,322; python: 12,901; sh: 11,219; java: 8,477; xml: 7,843; perl: 2,633; ansic: 2,465; yacc: 1,670; javascript: 1,020; makefile: 933; lex: 578; awk: 275; objc: 82; sed: 11; php: 1
file content (572 lines) | stat: -rw-r--r-- 16,077 bytes parent folder | download | duplicates (3)
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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
/* 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 3 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, see <http://www.gnu.org/licenses/>.
 *
 * Starts up new scenes.
 */

#include "tinsel/actors.h"
#include "tinsel/anim.h"
#include "tinsel/background.h"
#include "tinsel/config.h"
#include "tinsel/cursor.h"
#include "tinsel/dialogs.h"
#include "tinsel/dw.h"
#include "tinsel/graphics.h"
#include "tinsel/handle.h"
#include "tinsel/film.h"
#include "tinsel/font.h"
#include "tinsel/mareels.h"
#include "tinsel/move.h"
#include "tinsel/music.h"
#include "tinsel/object.h"
#include "tinsel/pcode.h"
#include "tinsel/pid.h"	// process IDs
#include "tinsel/play.h"
#include "tinsel/polygons.h"
#include "tinsel/movers.h"
#include "tinsel/sched.h"
#include "tinsel/scn.h"
#include "tinsel/scroll.h"
#include "tinsel/sound.h"	// stopAllSamples()
#include "tinsel/sysvar.h"
#include "tinsel/token.h"

#include "common/memstream.h"
#include "common/textconsole.h"

namespace Tinsel {

//----------------- EXTERNAL FUNCTIONS ---------------------

// in EFFECT.C
extern void EffectPolyProcess(CORO_PARAM, const void *);

// in PDISPLAY.C
#ifdef DEBUG
extern void CursorPositionProcess(CORO_PARAM, const void *);
#endif
extern void TagProcess(CORO_PARAM, const void *);
extern void PointProcess(CORO_PARAM, const void *);
extern void EnableTags();


//----------------- LOCAL DEFINES --------------------

#include "common/pack-start.h"	// START STRUCT PACKING

/** scene structure - one per scene */
struct SCENE_STRUC {
	int32 defRefer;		// Default refer direction (REFTYPE)
	SCNHANDLE hSceneScript;	// handle to scene script
	SCNHANDLE hSceneDesc;	// handle to scene description
	int32 numEntrance;	// number of entrances in this scene
	SCNHANDLE hEntrance;	// handle to table of entrances
	int32 numPoly;		// number of various polygons in this scene
	SCNHANDLE hPoly;	// handle to table of polygons
	int32 numTaggedActor;	// number of tagged actors in this scene
	SCNHANDLE hTaggedActor;	// handle to table of tagged actors
	int32 numProcess;	// number of processes in this scene
	SCNHANDLE hProcess;	// handle to table of processes
	SCNHANDLE hMusicScript;	// handle to music script data - Tinsel 2 only
	SCNHANDLE hMusicSegment;	// handle to music segments - Tinsel 2 only
	int32 numCameras;
	SCNHANDLE hCamera;
	int32 numLights;
	SCNHANDLE hLight;
} PACKED_STRUCT;

/** entrance structure - one per entrance */
struct ENTRANCE_STRUC {
	int32 eNumber;		///< entrance number
	SCNHANDLE hScript;	///< handle to entrance script
	// Tinsel 2 fields
	SCNHANDLE hEntDesc;	// handle to entrance description
	uint32 flags;
} PACKED_STRUCT;

/** camera strucutre, Noir only */
struct CAMERA_STRUC {
	int32 sceneId;
	int32 rotX;
	int32 rotY;
	int32 rotZ;
	int32 posX;
	int32 posY;
	int32 posZ;
	int32 aperture;
} PACKED_STRUCT;

struct LIGHT_STRUC {
	int32 sceneId;
	int32 posX;
	int32 posY;
	int32 posZ;
} PACKED_STRUCT;

#include "common/pack-end.h"	// END STRUCT PACKING


//----------------- LOCAL GLOBAL DATA --------------------

// These vars are reset upon engine destruction

#ifdef DEBUG
static bool g_ShowPosition = false;	// Set when showpos() has been called
#endif

int g_sceneCtr = 0;
static int g_initialMyEscape = 0;

static SCNHANDLE g_SceneHandle = 0;	// Current scene handle - stored in case of Save_Scene()

SCENE_STRUC g_tempStruc;

static bool g_isViewSet = false;

struct TP_INIT {
	SCNHANDLE hTinselCode;		// Code
	TINSEL_EVENT event;			// Triggering event
};

void ResetVarsScene() {
	g_sceneCtr = 0;
	g_initialMyEscape = 0;

	g_SceneHandle = 0;
	g_isViewSet = false;

	memset(&g_tempStruc, 0, sizeof(SCENE_STRUC));
}

SCENE_STRUC* parseSceneV1(const byte *p) {
	g_tempStruc.numEntrance    = READ_UINT32(p); p += sizeof(uint32);
	g_tempStruc.numPoly        = READ_UINT32(p); p += sizeof(uint32);
	g_tempStruc.numTaggedActor = READ_UINT32(p); p += sizeof(uint32);
	g_tempStruc.defRefer       = READ_UINT32(p); p += sizeof(uint32);
	g_tempStruc.hSceneScript   = READ_UINT32(p); p += sizeof(uint32);
	g_tempStruc.hEntrance      = READ_UINT32(p); p += sizeof(uint32);
	g_tempStruc.hPoly          = READ_UINT32(p); p += sizeof(uint32);
	g_tempStruc.hTaggedActor   = READ_UINT32(p); p += sizeof(uint32);

	return &g_tempStruc;
}

SCENE_STRUC* parseSceneV2(const byte *p) {
	g_tempStruc.defRefer       = READ_UINT32(p); p += sizeof(uint32);
	g_tempStruc.hSceneScript   = READ_UINT32(p); p += sizeof(uint32);
	g_tempStruc.hSceneDesc     = READ_UINT32(p); p += sizeof(uint32);
	g_tempStruc.numEntrance    = READ_UINT32(p); p += sizeof(uint32);
	g_tempStruc.hEntrance      = READ_UINT32(p); p += sizeof(uint32);
	g_tempStruc.numPoly        = READ_UINT32(p); p += sizeof(uint32);
	g_tempStruc.hPoly          = READ_UINT32(p); p += sizeof(uint32);
	g_tempStruc.numTaggedActor = READ_UINT32(p); p += sizeof(uint32);
	g_tempStruc.hTaggedActor   = READ_UINT32(p); p += sizeof(uint32);
	g_tempStruc.numProcess     = READ_UINT32(p); p += sizeof(uint32);
	g_tempStruc.hProcess       = READ_UINT32(p); p += sizeof(uint32);
	g_tempStruc.hMusicScript   = READ_UINT32(p); p += sizeof(uint32);
	g_tempStruc.hMusicSegment  = READ_UINT32(p); p += sizeof(uint32);

	return &g_tempStruc;
}

SCENE_STRUC* parseSceneV3(const byte *p) {
	g_tempStruc.defRefer       = READ_UINT32(p); p += sizeof(uint32);
	g_tempStruc.hSceneScript   = READ_UINT32(p); p += sizeof(uint32);
	g_tempStruc.hSceneDesc     = READ_UINT32(p); p += sizeof(uint32);
	g_tempStruc.numEntrance    = READ_UINT32(p); p += sizeof(uint32);
	g_tempStruc.hEntrance      = READ_UINT32(p); p += sizeof(uint32);
	g_tempStruc.numCameras     = READ_UINT32(p); p += sizeof(uint32);
	g_tempStruc.hCamera        = READ_UINT32(p); p += sizeof(uint32);
	g_tempStruc.numLights      = READ_UINT32(p); p += sizeof(uint32);
	g_tempStruc.hLight         = READ_UINT32(p); p += sizeof(uint32);
	g_tempStruc.numPoly        = READ_UINT32(p); p += sizeof(uint32);
	g_tempStruc.hPoly          = READ_UINT32(p); p += sizeof(uint32);
	g_tempStruc.numTaggedActor = READ_UINT32(p); p += sizeof(uint32);
	g_tempStruc.hTaggedActor   = READ_UINT32(p); p += sizeof(uint32);
	g_tempStruc.numProcess     = READ_UINT32(p); p += sizeof(uint32);
	g_tempStruc.hProcess       = READ_UINT32(p); p += sizeof(uint32);
	g_tempStruc.hMusicScript   = READ_UINT32(p); p += sizeof(uint32);
	g_tempStruc.hMusicSegment  = READ_UINT32(p); p += sizeof(uint32);
	// scollfields scaling/speed - 4 ints
	warning("TODO: Complete scene loading logic for Noir");

	return &g_tempStruc;
}

const SCENE_STRUC *GetSceneStruc(const byte *pStruc) {
	memset(&g_tempStruc, 0, sizeof(SCENE_STRUC));

	if (TinselVersion == 3) {
		return parseSceneV3(pStruc);
	} else if (TinselVersion == 2) {
		return parseSceneV2(pStruc);
	} else {
		return parseSceneV1(pStruc);
	}
}

/**
 * Started up for scene script and entrance script.
 */
static void SceneTinselProcess(CORO_PARAM, const void *param) {
	// COROUTINE
	CORO_BEGIN_CONTEXT;
		INT_CONTEXT *pic;
		const TP_INIT *pInit;
		int myEscape;
	CORO_END_CONTEXT(_ctx);

	CORO_BEGIN_CODE(_ctx);

	// The following myEscape value setting is used for enabling title screen skipping in DW1
	if ((TinselVersion == 1) && (g_sceneCtr == 1)) g_initialMyEscape = GetEscEvents();
	// DW1 PSX, Saturn and Mac has its own scene skipping script code for scenes 2 and 3 (bug #6094).
	_ctx->myEscape = ((TinselVersion == 1) && (g_sceneCtr < ((TinselV1PSX || TinselV1Saturn || TinselV1Mac) ? 2 : 4))) ? g_initialMyEscape : 0;

	// get the stuff copied to process when it was created
	_ctx->pInit = (const TP_INIT *)param;
	assert(_ctx->pInit);
	assert(_ctx->pInit->hTinselCode);		// Must have some code to run

	_ctx->pic = InitInterpretContext(GS_SCENE,
		FROM_32(_ctx->pInit->hTinselCode),
		(TinselVersion >= 2) ? _ctx->pInit->event : NOEVENT,
		NOPOLY,			// No polygon
		0,				// No actor
		NULL,			// No object
		_ctx->myEscape);
	CORO_INVOKE_1(Interpret, _ctx->pic);

	CORO_END_CODE;
}

/**
 * Start up a SceneTinselProcess() running the scene
 * script for various events.
 */
void SendSceneTinselProcess(TINSEL_EVENT event) {
	SCENE_STRUC	*ss;

	if (g_SceneHandle != 0) {
		ss = (SCENE_STRUC *) FindChunk(g_SceneHandle, CHUNK_SCENE);

		if (ss->hSceneScript) {
			TP_INIT	init;

			init.event = event;
			init.hTinselCode = ss->hSceneScript;

			CoroScheduler.createProcess(PID_TCODE, SceneTinselProcess, &init, sizeof(init));
		}
	}
}


/**
 * Get the SCENE_STRUC
 * Initialize polygons for the scene
 * Initialize the actors for this scene
 * Run the appropriate entrance code (if any)
 * Get the default refer type
 */

static void LoadScene(SCNHANDLE scene, int entry) {
	uint32	i;
	TP_INIT init;
	const SCENE_STRUC	*ss;
	const ENTRANCE_STRUC	*es;

	// Scene handle
	g_SceneHandle = scene;		// Save scene handle in case of Save_Scene()
	_vm->_handle->LockMem(g_SceneHandle); // Make sure scene is loaded
	_vm->_handle->LockScene(g_SceneHandle); // Prevent current scene from being discarded

	if (TinselVersion >= 2) {
		// CdPlay() stuff
		byte *cptr = FindChunk(scene, CHUNK_CDPLAY_FILENUM);
		assert(cptr);
		i = READ_32(cptr);
		assert(i < 512);
		cptr = FindChunk(scene, CHUNK_CDPLAY_FILENAME);
		assert(cptr);
		_vm->_handle->SetCdPlaySceneDetails((const char *)cptr);
	}

	// Find scene structure
	ss = GetSceneStruc(FindChunk(scene, CHUNK_SCENE));
	assert(ss != NULL);

	if (TinselVersion >= 2) {
		// Music stuff
		char *cptr = (char *)FindChunk(scene, CHUNK_MUSIC_FILENAME);
		assert(cptr);
		_vm->_pcmMusic->setMusicSceneDetails(FROM_32(ss->hMusicScript), FROM_32(ss->hMusicSegment), cptr);
	}

	if (entry == NO_ENTRY_NUM) {
		// Restoring scene

		// Initialize all the polygons for this scene
		InitPolygons(FROM_32(ss->hPoly), FROM_32(ss->numPoly), true);

		// Initialize the actors for this scene
		_vm->_actor->StartTaggedActors(FROM_32(ss->hTaggedActor), FROM_32(ss->numTaggedActor), false);

		if (TinselVersion >= 2)
			// Returning from cutscene
			SendSceneTinselProcess(RESTORE);

	} else {
		// Genuine new scene

		// Initialize all the polygons for this scene
		InitPolygons(FROM_32(ss->hPoly), FROM_32(ss->numPoly), false);

		// Initialize the actors for this scene
		_vm->_actor->StartTaggedActors(FROM_32(ss->hTaggedActor), FROM_32(ss->numTaggedActor), true);

		// Run the appropriate entrance code (if any)
		es = (const ENTRANCE_STRUC *)_vm->_handle->LockMem(FROM_32(ss->hEntrance));
		for (i = 0; i < FROM_32(ss->numEntrance); i++) {
			if (FROM_32(es->eNumber) == (uint)entry) {
				if (es->hScript) {
					init.event = STARTUP;
					init.hTinselCode = es->hScript;

					CoroScheduler.createProcess(PID_TCODE, SceneTinselProcess, &init, sizeof(init));
				}
				break;
			}

			// Move to next entrance
			if (TinselVersion >= 2)
				++es;
			else
				es = (const ENTRANCE_STRUC *)((const byte *)es + 8);

		}

		if (i == FROM_32(ss->numEntrance))
			error("Non-existent scene entry number");

		if (ss->hSceneScript) {
			init.event = STARTUP;
			init.hTinselCode = ss->hSceneScript;

			CoroScheduler.createProcess(PID_TCODE, SceneTinselProcess, &init, sizeof(init));
		}
	}

	// Default refer type
	SetDefaultRefer(FROM_32(ss->defRefer));

	// Scene's processes
	SceneProcesses(FROM_32(ss->numProcess), FROM_32(ss->hProcess));
}


/**
 * Wrap up the last scene.
 */
void EndScene() {
	if (g_SceneHandle != 0) {
		_vm->_handle->UnlockScene(g_SceneHandle);
		g_SceneHandle = 0;
	}

	_vm->_dialogs->killInventory(); // Close down any open inventory

	DropPolygons();		// No polygons
	_vm->_scroll->DropScroll(); // No no-scrolls
	_vm->_bg->DropBackground();	// No background
	DropMovers();		// No moving actors
	_vm->_cursor->DropCursor(); // No cursor
	_vm->_actor->DropActors();      // No actor reels running
	FreeAllTokens();	// No-one has tokens
	FreeMostInterpretContexts();	// Only master script still interpreting

	if (TinselVersion >= 2) {
		SetSysVar(ISV_DIVERT_ACTOR, 0);
		SetSysVar(ISV_GHOST_ACTOR, 0);
		SetSysVar(SV_MinimumXoffset, 0);
		SetSysVar(SV_MaximumXoffset, 0);
		SetSysVar(SV_MinimumYoffset, 0);
		SetSysVar(SV_MaximumYoffset, 0);
		_vm->_font->ResetFontHandles();
		NoSoundReels();
	}

	_vm->_sound->stopAllSamples();		// Kill off any still-running sample
	//_vm->_mixer->stopAll();

	// init the palette manager
	ResetPalAllocator();

	// init the object manager
	KillAllObjects();

	// kill all destructable process
	CoroScheduler.killMatchingProcess(PID_DESTROY, PID_DESTROY);
}

/**
 * Start up the standard stuff for the next scene.
 */

void PrimeScene() {
	SetNoBlocking(false);
	SetSysVar(SYS_SceneFxDimFactor, SysVar(SYS_DefaultFxDimFactor));

	_vm->_cursor->RestartCursor(); // Restart the cursor
	if (TinselVersion <= 1)
		EnableTags();		// Next scene with tags enabled

	CoroScheduler.createProcess(PID_SCROLL, ScrollProcess, NULL, 0);
	CoroScheduler.createProcess(PID_SCROLL, EffectPolyProcess, NULL, 0);

#ifdef DEBUG
	if (g_ShowPosition)
		CoroScheduler.createProcess(PID_POSITION, CursorPositionProcess, NULL, 0);
#endif

	CoroScheduler.createProcess(PID_TAG, TagProcess, NULL, 0);
	CoroScheduler.createProcess(PID_TAG, PointProcess, NULL, 0);

	// init the current background
	_vm->_bg->InitBackground();
}

/**
 * Wrap up the last scene and start up the next scene.
 */

void StartNewScene(SCNHANDLE scene, int entry) {
	EndScene();	// Wrap up the last scene.

	if (TinselVersion >= 2) {
		TouchMoverReels();

		_vm->_handle->LockMem(scene); // Do CD change before PrimeScene
	}

	PrimeScene();	// Start up the standard stuff for the next scene.

	LoadScene(scene, entry);

	g_isViewSet = false;
}

#ifdef DEBUG
/**
 * Sets the ShowPosition flag, causing the cursor position process to be
 * created in each scene.
 */

void setshowpos() {
	g_ShowPosition = true;
}
#endif

/**
 * Return the current scene handle.
 */

SCNHANDLE GetSceneHandle() {
	return g_SceneHandle;
}

/**
 * DoHailScene
 */

void DoHailScene(SCNHANDLE scene) {
	// Find scene structure
	const SCENE_STRUC *ss = GetSceneStruc(FindChunk(scene, CHUNK_SCENE));

	if (ss != NULL && ss->hSceneScript) {
		TP_INIT	init;

		init.event = NOEVENT;
		init.hTinselCode = ss->hSceneScript;

		CoroScheduler.createProcess(PID_SCENE, SceneTinselProcess, &init, sizeof(init));
	}
}

/**
 * WrapScene
 */
void WrapScene() {
	SendSceneTinselProcess(CLOSEDOWN);
}



/**
 * Set parameters for 3D rendering for Noir.
 */
void SetView(int sceneId, int scale) {
	if (sceneId == SysVar(SV_SPRITER_SCENE_ID)) {
		if (scale == SysVar(SV_SPRITER_SCALE)) {
			debug("Ignoring SetView()");
			return;
		}
	}
	debug("SetView(%d, %d)", sceneId, scale);

	SetSysVar(SV_SPRITER_SCALE, scale);
	SetSysVar(SV_USER3 ,0x28);

	if (g_isViewSet) {
		//EndActors();
	} else {
		g_isViewSet = true;
	}

	int i = 0;
	CAMERA_STRUC *pCamera = (CAMERA_STRUC *)_vm->_handle->LockMem(g_tempStruc.hCamera);
	for (i = 0; i < g_tempStruc.numCameras; ++i, ++pCamera) {
		if (sceneId == (int)FROM_32(pCamera->sceneId)) {
			// set camera
			SetSysVar(SV_SPRITER_SCENE_ID, sceneId);
			break;
		}
	}

	if (i == g_tempStruc.numCameras) {
		// no suitable camera found
		return;
	}

	LIGHT_STRUC *pLight = (LIGHT_STRUC *)_vm->_handle->LockMem(g_tempStruc.hLight);
	for (i = 0; i < g_tempStruc.numLights; ++i, ++pLight) {
		if (sceneId == (int)FROM_32(pLight->sceneId)) {
			// set light
			break;
		}
	}

	if (i == g_tempStruc.numLights) {
		// use default light
	}

	//update ground plane
}

} // End of namespace Tinsel