aboutsummaryrefslogtreecommitdiff
path: root/engines/sci/engine/kmath.cpp
blob: 6ccc161e193f8099ea809d5549cd6db8a52d4f14 (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
/* 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$
 *
 */

#include "sci/engine/state.h"
#include "sci/engine/kernel.h"

namespace Sci {

reg_t kRandom(EngineState *s, int argc, reg_t *argv) {
	switch (argc) {
	case 1: // set seed to argv[0]
		// SCI0/SCI01 just reset the seed to 0 instead of using argv[0] at all
		return NULL_REG;

	case 2: { // get random number
		// These are unsigned (e.g. in LSL5, this is used for the 5-digit
		// door access code with Patty at k rap)
		int fromNumber = argv[0].toUint16();
		int toNumber = argv[1].toUint16();

		// HACK: Hoyle 4 calls this with (0x0, 0xFFFF) in some dialogs for x,
		// then it calls it with (0x0, 0x40) and puts x plus the result in y
		// to displace view 924 in kDrawCel right afterwards and create a
		// semi-random background in those dialogs. With the current code,
		// this case isn't handled properly, and the dialog is drawn off-screen.
		// -1 seems to be incorrect here, so we just return a 0 for now, till
		// we figure out how to handle this.
		if (fromNumber == 0 && toNumber == 0xFFFF) {
			if (g_sci->getGameId() == GID_HOYLE4) {
				warning("HACK: Special case for Hoyle 4 dialogs in kRandom");
				return NULL_REG;
			} else {
				// Just throw a warning in other games for now, but don't modify
				// the result...
				warning("kRandom(0x0, 0xFFFF called - special case?");
			}
			
		}

		// TODO/CHECKME: It is propbably not required to check whether
		// toNumber is greater than fromNumber, at least not when one
		// goes by their names, but let us be on the safe side and
		// allow toNumber to be smaller than fromNumber too.
		if (fromNumber > toNumber)
			SWAP(fromNumber, toNumber);

		const uint diff = (uint)(toNumber - fromNumber);

		const int randomNumber = fromNumber + (int)g_sci->getRNG().getRandomNumber(diff);
		return make_reg(0, randomNumber);
	}

	case 3: // get seed
		// SCI0/01 did not support this at all
		// Actually we would have to return the previous seed
		error("kRandom: scripts asked for previous seed");
		break;

	default:
		error("kRandom: unsupported argc");
	}
}

reg_t kAbs(EngineState *s, int argc, reg_t *argv) {
	return make_reg(0, ABS(argv[0].toSint16()));
}

reg_t kSqrt(EngineState *s, int argc, reg_t *argv) {
	return make_reg(0, (int16) sqrt((float) ABS(argv[0].toSint16())));
}

reg_t kGetAngle(EngineState *s, int argc, reg_t *argv) {
	// Based on behavior observed with a test program created with
	// SCI Studio.
	int x1 = argv[0].toSint16();
	int y1 = argv[1].toSint16();
	int x2 = argv[2].toSint16();
	int y2 = argv[3].toSint16();
	int xrel = x2 - x1;
	int yrel = y1 - y2; // y-axis is mirrored.
	int angle;

	// Move (xrel, yrel) to first quadrant.
	if (y1 < y2)
		yrel = -yrel;
	if (x2 < x1)
		xrel = -xrel;

	// Compute angle in grads.
	if (yrel == 0 && xrel == 0)
		angle = 0;
	else
		angle = 100 * xrel / (xrel + yrel);

	// Fix up angle for actual quadrant of (xrel, yrel).
	if (y1 < y2)
		angle = 200 - angle;
	if (x2 < x1)
		angle = 400 - angle;

	// Convert from grads to degrees by merging grad 0 with grad 1,
	// grad 10 with grad 11, grad 20 with grad 21, etc. This leads to
	// "degrees" that equal either one or two grads.
	angle -= (angle + 9) / 10;

	return make_reg(0, angle);
}

reg_t kGetDistance(EngineState *s, int argc, reg_t *argv) {
	int xdiff = (argc > 3) ? argv[3].toSint16() : 0;
	int ydiff = (argc > 2) ? argv[2].toSint16() : 0;
	int angle = (argc > 5) ? argv[5].toSint16() : 0;
	int xrel = (int)(((float) argv[1].toSint16() - xdiff) / cos(angle * PI / 180.0)); // This works because cos(0)==1
	int yrel = argv[0].toSint16() - ydiff;
	return make_reg(0, (int16)sqrt((float) xrel*xrel + yrel*yrel));
}

reg_t kTimesSin(EngineState *s, int argc, reg_t *argv) {
	int angle = argv[0].toSint16();
	int factor = argv[1].toSint16();

	return make_reg(0, (int)(factor * sin(angle * PI / 180.0)));
}

reg_t kTimesCos(EngineState *s, int argc, reg_t *argv) {
	int angle = argv[0].toSint16();
	int factor = argv[1].toSint16();

	return make_reg(0, (int)(factor * cos(angle * PI / 180.0)));
}

reg_t kCosDiv(EngineState *s, int argc, reg_t *argv) {
	int angle = argv[0].toSint16();
	int value = argv[1].toSint16();
	double cosval = cos(angle * PI / 180.0);

	if ((cosval < 0.0001) && (cosval > -0.0001)) {
		error("kCosDiv: Attempted division by zero");
		return SIGNAL_REG;
	} else
		return make_reg(0, (int16)(value / cosval));
}

reg_t kSinDiv(EngineState *s, int argc, reg_t *argv) {
	int angle = argv[0].toSint16();
	int value = argv[1].toSint16();
	double sinval = sin(angle * PI / 180.0);

	if ((sinval < 0.0001) && (sinval > -0.0001)) {
		error("kSinDiv: Attempted division by zero");
		return SIGNAL_REG;
	} else
		return make_reg(0, (int16)(value / sinval));
}

reg_t kTimesTan(EngineState *s, int argc, reg_t *argv) {
	int param = argv[0].toSint16();
	int scale = (argc > 1) ? argv[1].toSint16() : 1;

	param -= 90;
	if ((param % 90) == 0) {
		error("kTimesTan: Attempted tan(pi/2)");
		return SIGNAL_REG;
	} else
		return make_reg(0, (int16) - (tan(param * PI / 180.0) * scale));
}

reg_t kTimesCot(EngineState *s, int argc, reg_t *argv) {
	int param = argv[0].toSint16();
	int scale = (argc > 1) ? argv[1].toSint16() : 1;

	if ((param % 90) == 0) {
		error("kTimesCot: Attempted tan(pi/2)");
		return SIGNAL_REG;
	} else
		return make_reg(0, (int16)(tan(param * PI / 180.0) * scale));
}

#ifdef ENABLE_SCI32

reg_t kMulDiv(EngineState *s, int argc, reg_t *argv) {
	int16 multiplicant = argv[0].toSint16();
	int16 multiplier = argv[1].toSint16();
	int16 denominator = argv[2].toSint16();

	// Sanity check...
	if (!denominator) {
		error("kMulDiv: attempt to divide by zero (%d * %d / %d", multiplicant, multiplier, denominator);
		return NULL_REG;
	}

	return make_reg(0, multiplicant * multiplier / denominator);
}

#endif

} // End of namespace Sci