aboutsummaryrefslogtreecommitdiff
path: root/sword2/function.cpp
blob: 4a250892b4643f14260c0205a90e1ab257312954 (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
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
/* Copyright (C) 1994-2003 Revolution Software Ltd
 *
 * 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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * $Header$
 */

#include "stdafx.h"
#include "bs2/driver/driver96.h"
#include "bs2/driver/rdwin.h"
#include "bs2/build_display.h"
#include "bs2/credits.h"
#include "bs2/debug.h"
#include "bs2/defs.h"
#include "bs2/function.h"
#include "bs2/interpreter.h"
#include "bs2/layers.h"		// for 'this_screen' structure
#include "bs2/logic.h"
#include "bs2/protocol.h"
#include "bs2/resman.h"
#include "bs2/sound.h"
#include "bs2/sword2.h"

namespace Sword2 {

Object_graphic engine_graph;	// global for engine
Object_mega engine_mega;	// global for engine

int32 FN_test_function(int32 *params) {
	// param	0 address of a flag

	debug(5, " TEST %d %d", params[0], RESULT);
	return IR_CONT;
}

int32 FN_test_flags(int32 *params) {
	// param	0 value of flag

	debug(5, "FN_test_flags %d, %d\n", params[0], params[1]);
	return IR_CONT;
}

int32 FN_gosub(int32 *params) {
	// hurray, script subroutines
	// param	0 id of script

	LLogic.logicUp(params[0]);

	// logic goes up - pc is saved for current level
	return IR_GOSUB;
}

int32 FN_new_script(int32 *params) {
	// change current script - must be followed by a TERMINATE script
	// directive
	// param	0 id of script

	debug(5, "FN_new_script %d", params[0]);

	// must clear this
	PLAYER_ACTION = 0;

	LLogic.logicReplace(params[0]);

	//drop out no pc save - and around again
	return IR_TERMINATE;
}

int32 FN_interact(int32 *params) {
	// run targets action on a subroutine
	// called by player on his base level 0 idle, for example
	// param	0 id of target from which we derive action script
	//		  reference

	debug(5, "FN_interact %d", params[0]);

	// must clear this
	PLAYER_ACTION = 0;

	// 3rd script of clicked on id
	LLogic.logicUp((params[0] * 65536) + 2);

	// out, up and around again - pc is saved for current level to be
	// returned to
	return IR_GOSUB;
}

int32 FN_preload(int32 *params)	{
	// Open & close a resource.
	// Forces a resource into memory before it's "officially" opened for
	// use. eg. if an anim needs to run on smoothly from another,
	// "preloading" gets it into memory in advance to avoid the cacheing
	// delay that normally occurs before the first frame.

	res_man.open(params[0]);	// open resource
	res_man.close(params[0]);	// close resource
	return IR_CONT;			// continue script
}

int32 FN_prefetch(int32 *params) {
	// Go fetch resource in the background.

	return IR_CONT;
}

int32 FN_fetch_wait(int32 *params) {
	// Fetches a resource in the background but prevents the script from
	// continuing until the resource is in memory.

	return IR_CONT;
}

int32 FN_release(int32 *params) {
	// Releases a resource from memory. Used for freeing memory for
	// sprites that have just been used and will not be used again.
	// Sometimes it is better to kick out a sprite straight away so that
	// the memory can be used for more frequent animations.

	return IR_CONT;
}

int32 FN_random(int32 *params) {
	// Generates a random number between 'min' & 'max' inclusive, and
	// sticks it in the script flag 'result'

	// params:	0 min
	//		1 max

	// return_value = random integer between min and max, inclusive
	RESULT = g_sword2->_rnd.getRandomNumberRng(params[0], params[1]);
	
	// continue script
	return IR_CONT;
}

int32 FN_pause(int32 *params) {
	// params:	0 pointer to object's logic structure
	//		1 number of game-cycles to pause

	// NB. Pause-value of 0 causes script to continue, 1 causes a 1-cycle
	// quit, 2 gives 2 cycles, etc.

	Object_logic *ob_logic = (Object_logic *) params[0];

	if (ob_logic->looping == 0) {
		// start the pause
		ob_logic->looping = 1;

		// no. of game cycles
		ob_logic->pause = params[1];
	}

	if (ob_logic->pause) {
		// decrement the pause count
		ob_logic->pause--;

		// drop out of script, but call this again next cycle
		return IR_REPEAT;
	} else {
		ob_logic->looping = 0;

		// continue script
		return IR_CONT;
	}
}

int32 FN_random_pause(int32 *params) {
	// params:	0 pointer to object's logic structure
	//		1 minimum number of game-cycles to pause
	//		2 maximum number of game-cycles to pause

	Object_logic *ob_logic = (Object_logic *) params[0];
	int32 pars[2];

	if (ob_logic->looping == 0) {
		pars[0] = params[1];	// min
		pars[1] = params[2];	// max

		FN_random(pars);

		// random value between 'min' & 'max' inclusive
		pars[1] = RESULT;
	}

	pars[0] = params[0];		// &logic
	return FN_pause(pars);
}

int32 FN_pass_graph(int32 *params) {
	// makes an engine local copy of passed graphic_structure and
	// mega_structure - run script 4 of an object to request this
	// used by FN_turn_to(id) etc
	//
	// remember, we cannot simply read a compact any longer but instead
	// must request it from the object itself

	//params	0 pointer to a graphic structure (might not need this?)

	memcpy(&engine_graph, (uint8 *) params[0], sizeof(Object_graphic));

	// makes no odds
	return IR_CONT;
}

int32 FN_pass_mega(int32 *params) {
	// makes an engine local copy of passed graphic_structure and
	// mega_structure - run script 4 of an object to request this
	// used by FN_turn_to(id) etc
	//
	// remember, we cannot simply read a compact any longer but instead
	// must request it from the object itself

	// params 	0 pointer to a mega structure

	memcpy(&engine_mega, (uint8 *) params[0], sizeof(Object_mega));

	//makes no odds
	return IR_CONT;
}

int32 FN_set_value(int32 *params) {
	// temp. function!
	// used for setting far-referenced megaset resource field in mega
	// object, from start script

	// params:	0 pointer to object's mega structure
	//		1 value to set it to

	Object_mega *ob_mega = (Object_mega *) params[0];

	ob_mega->megaset_res = params[1];

	// continue script
	return IR_CONT;
}

#define BLACK	0
#define WHITE	1
#define RED	2
#define GREEN	3
#define BLUE	4

uint8 black[4]	= {  0,    0,   0,   0 };
uint8 white[4]	= { 255, 255, 255,   0 };
uint8 red[4]	= { 255,   0,   0,   0 };
uint8 green[4]	= {   0, 255,   0,   0 };
uint8 blue[4]	= {   0,   0, 255,   0 };

int32 FN_flash(int32 *params) {
	// flash colour 0 (ie. border) - useful during script development
	// eg. FN_flash(BLUE) where a text line is missed; RED when some code
	// missing, etc

	// params	0: colour to flash

#ifdef _SWORD2_DEBUG
	// what colour?
	switch (params[0]) {
	case WHITE:
		g_display->setPalette(0, 1, white, RDPAL_INSTANT);
		break;
	case RED:
		g_display->setPalette(0, 1, red, RDPAL_INSTANT);
		break;
	case GREEN:
		g_display->setPalette(0, 1, green, RDPAL_INSTANT);
		break;
	case BLUE:
		g_display->setPalette(0, 1, blue, RDPAL_INSTANT);
		break;
	}

	// There used to be a busy-wait loop here, so I don't know how long
	// the delay was meant to be. Probably doesn't matter much.

	g_display->updateDisplay();
	g_system->delay_msecs(250);
	g_display->setPalette(0, 1, black, RDPAL_INSTANT);
#endif

	return IR_CONT;
}


int32 FN_colour(int32 *params) {
	// set border colour - useful during script development
	// eg. set to colour during a timer situation, then black when timed
	// out

	// params	0: colour (see defines above)

#ifdef _SWORD2_DEBUG
	// what colour?
	switch (params[0]) {
	case BLACK:
		g_display->setPalette(0, 1, black, RDPAL_INSTANT);
		break;
	case WHITE:
		g_display->setPalette(0, 1, white, RDPAL_INSTANT);
		break;
	case RED:
		g_display->setPalette(0, 1, red, RDPAL_INSTANT);
		break;
	case GREEN:
		g_display->setPalette(0, 1, green, RDPAL_INSTANT);
		break;
	case BLUE:
		g_display->setPalette(0, 1, blue, RDPAL_INSTANT);
		break;
	}
#endif

	return IR_CONT;
}

int32 FN_display_msg(int32 *params) {
	// Display a message to the user on the screen.
	//
	// params	0: Text number of message to be displayed.

	uint32 local_text = params[0] & 0xffff;
	uint32 text_res = params[0] / SIZE;

	// Display message for three seconds.

	// +2 to skip the encoded text number in the first 2 chars; 3 is
	// duration in seconds

	DisplayMsg(FetchTextLine(res_man.open(text_res), local_text) + 2, 3);
	res_man.close(text_res);
	RemoveMsg();

	return IR_CONT;
}

int32 FN_reset_globals(int32 *params) {
	// FN_reset_globals is used by the demo - so it can loop back &
	// restart itself

	int32 size;
	uint32 *globals;
	int j;

	size = res_man.fetchLen(1);
	size -= sizeof(_standardHeader);

	debug(5, "globals size %d", size / 4);

	globals = (uint32 *) ((uint8 *) res_man.open(1) + sizeof(_standardHeader));

	// blank each global variable
	for (j = 0; j < size / 4; j++)
		globals[j] = 0;

	res_man.close(1);

	// all objects but george
	res_man.killAllObjects(0);

	// FOR THE DEMO - FORCE THE SCROLLING TO BE RESET!
	// - this is taken from FN_init_background

	// switch on scrolling (2 means first time on screen)
	this_screen.scroll_flag = 2;

	return IR_CONT;
}

int32 FN_play_credits(int32 *params) {
	// params:	none

	// This function just quits the game if this is the playable demo, ie.
	// credits are NOT played in the demo any more!

	if (!DEMO) {
		uint8 oldPal[1024];
		uint8 tmpPal[1024];
		int32 music_length;
		int32 pars[2];

		g_sound->saveMusicState();

		g_sound->muteFx(1);
		g_sound->muteSpeech(1);
		g_sound->stopMusic();

		memcpy(oldPal, g_display->_palCopy, 1024);
		memset(tmpPal, 0, 1024);

		g_display->waitForFade();
		g_display->fadeDown();
		g_display->waitForFade();

		tmpPal[4] = 255;
		tmpPal[5] = 255;
		tmpPal[6] = 255;
		g_display->setPalette(0, 256, tmpPal, RDPAL_INSTANT);

		// Play the credits music. Is it enough with just one
		// repetition of it?

		pars[0] = 309;
		pars[1] = FX_SPOT;
		FN_play_music(pars);

		music_length = 1000 * g_sound->musicTimeRemaining();

		debug(0, "Credits music length: ~%d ms", music_length);

		g_display->closeMenuImmediately();

		while (g_sound->musicTimeRemaining()) {
			g_display->clearScene();
			g_display->setNeedFullRedraw();

			// FIXME: Draw the credits text. The actual text
			// messages are stored in credits.clu, and I'm guessing
			// that credits.bmp or font.clu may be the font.

			g_display->updateDisplay();

			_keyboardEvent ke;

			if (ReadKey(&ke) == RD_OK && ke.keycode == 27)
				break;

			g_system->delay_msecs(30);
		}

		FN_stop_music(NULL);
		g_sound->restoreMusicState();

		g_display->setPalette(0, 256, oldPal, RDPAL_FADE);
		g_display->fadeUp();
		g_display->updateDisplay();
		Build_display();
		g_display->waitForFade();

		g_sound->muteFx(0);
		g_sound->muteSpeech(0);
	}

	// FIXME: This probably isn't the correct way of shutting down ScummVM
	// Anyway, I don't know if we ever call this from the demo.

	if (g_sword2->_gameId == GID_SWORD2_DEMO) {
		Close_game();		// close engine systems down
		exit(0);		// quit the game
	}

	return IR_CONT;
}

} // End of namespace Sword2