1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  */
22 
23 #include "common/config-manager.h"
24 #include "ags/engine/ac/math.h"
25 #include "ags/shared/ac/common.h" // quit
26 #include "ags/shared/util/math.h"
27 #include "ags/shared/debugging/out.h"
28 #include "ags/engine/script/script_api.h"
29 #include "ags/engine/script/script_runtime.h"
30 #include "ags/ags.h"
31 #include "ags/globals.h"
32 
33 namespace AGS3 {
34 
FloatToInt(float value,int roundDirection)35 int FloatToInt(float value, int roundDirection) {
36 	if (value >= 0.0) {
37 		if (roundDirection == eRoundDown)
38 			return static_cast<int>(value);
39 		else if (roundDirection == eRoundNearest)
40 			return static_cast<int>(value + 0.5);
41 		else if (roundDirection == eRoundUp)
42 			return static_cast<int>(value + 0.999999);
43 		else
44 			error("!FloatToInt: invalid round direction");
45 	} else {
46 		// negative number
47 		if (roundDirection == eRoundUp)
48 			return static_cast<int>(value); // this just truncates
49 		else if (roundDirection == eRoundNearest)
50 			return static_cast<int>(value - 0.5);
51 		else if (roundDirection == eRoundDown)
52 			return static_cast<int>(value - 0.999999);
53 		else
54 			error("!FloatToInt: invalid round direction");
55 	}
56 	return 0;
57 }
58 
IntToFloat(int value)59 float IntToFloat(int value) {
60 	return static_cast<float>(value);
61 }
62 
StringToFloat(const char * theString)63 float StringToFloat(const char *theString) {
64 	return static_cast<float>(atof(theString));
65 }
66 
Math_Cos(float value)67 float Math_Cos(float value) {
68 	return cos(value);
69 }
70 
Math_Sin(float value)71 float Math_Sin(float value) {
72 	return sin(value);
73 }
74 
Math_Tan(float value)75 float Math_Tan(float value) {
76 	return tan(value);
77 }
78 
Math_ArcCos(float value)79 float Math_ArcCos(float value) {
80 	return acos(value);
81 }
82 
Math_ArcSin(float value)83 float Math_ArcSin(float value) {
84 	return asin(value);
85 }
86 
Math_ArcTan(float value)87 float Math_ArcTan(float value) {
88 	return atan(value);
89 }
90 
Math_ArcTan2(float yval,float xval)91 float Math_ArcTan2(float yval, float xval) {
92 	return atan2(yval, xval);
93 }
94 
Math_Log(float value)95 float Math_Log(float value) {
96 	return log(value);
97 }
98 
Math_Log10(float value)99 float Math_Log10(float value) {
100 	return ::log10(value);
101 }
102 
Math_Exp(float value)103 float Math_Exp(float value) {
104 	return exp(value);
105 }
106 
Math_Cosh(float value)107 float Math_Cosh(float value) {
108 	return cosh(value);
109 }
110 
Math_Sinh(float value)111 float Math_Sinh(float value) {
112 	return sinh(value);
113 }
114 
Math_Tanh(float value)115 float Math_Tanh(float value) {
116 	return tanh(value);
117 }
118 
Math_RaiseToPower(float base,float exp)119 float Math_RaiseToPower(float base, float exp) {
120 	return ::pow(base, exp);
121 }
122 
Math_DegreesToRadians(float value)123 float Math_DegreesToRadians(float value) {
124 	return static_cast<float>(value * (M_PI / 180.0));
125 }
126 
Math_RadiansToDegrees(float value)127 float Math_RadiansToDegrees(float value) {
128 	return static_cast<float>(value * (180.0 / M_PI));
129 }
130 
Math_GetPi()131 float Math_GetPi() {
132 	return static_cast<float>(M_PI);
133 }
134 
Math_Sqrt(float value)135 float Math_Sqrt(float value) {
136 	if (value < 0.0)
137 		error("!Sqrt: cannot perform square root of negative number");
138 
139 	return ::sqrt(value);
140 }
141 
__Rand(int upto)142 int __Rand(int upto) {
143 	// WORKAROUND: Fix crash in Captain Disaster in Death Has a Million Stomping Boots
144 	// at the start of Act 2, walking to Aquarium
145 	if (upto == -1 && ConfMan.get("gameid") == "captaindisaster")
146 		upto = INT32_MAX;
147 
148 	if (upto < 0)
149 		error("!Random: invalid parameter passed -- must be at least 0.");
150 
151 	return ::AGS::g_vm->getRandomNumber(upto);
152 }
153 
154 
155 //=============================================================================
156 //
157 // Script API Functions
158 //
159 //=============================================================================
160 
161 // float (float value)
Sc_Math_ArcCos(const RuntimeScriptValue * params,int32_t param_count)162 RuntimeScriptValue Sc_Math_ArcCos(const RuntimeScriptValue *params, int32_t param_count) {
163 	API_SCALL_FLOAT_PFLOAT(Math_ArcCos);
164 }
165 
166 // float (float value)
Sc_Math_ArcSin(const RuntimeScriptValue * params,int32_t param_count)167 RuntimeScriptValue Sc_Math_ArcSin(const RuntimeScriptValue *params, int32_t param_count) {
168 	API_SCALL_FLOAT_PFLOAT(Math_ArcSin);
169 }
170 
171 // float (float value)
Sc_Math_ArcTan(const RuntimeScriptValue * params,int32_t param_count)172 RuntimeScriptValue Sc_Math_ArcTan(const RuntimeScriptValue *params, int32_t param_count) {
173 	API_SCALL_FLOAT_PFLOAT(Math_ArcTan);
174 }
175 
176 // float (SCRIPT_FLOAT(yval), SCRIPT_FLOAT(xval))
Sc_Math_ArcTan2(const RuntimeScriptValue * params,int32_t param_count)177 RuntimeScriptValue Sc_Math_ArcTan2(const RuntimeScriptValue *params, int32_t param_count) {
178 	API_SCALL_FLOAT_PFLOAT2(Math_ArcTan2);
179 }
180 
181 // float (float value)
Sc_Math_Cos(const RuntimeScriptValue * params,int32_t param_count)182 RuntimeScriptValue Sc_Math_Cos(const RuntimeScriptValue *params, int32_t param_count) {
183 	API_SCALL_FLOAT_PFLOAT(Math_Cos);
184 }
185 
186 // float (float value)
Sc_Math_Cosh(const RuntimeScriptValue * params,int32_t param_count)187 RuntimeScriptValue Sc_Math_Cosh(const RuntimeScriptValue *params, int32_t param_count) {
188 	API_SCALL_FLOAT_PFLOAT(Math_Cosh);
189 }
190 
191 // float (float value)
Sc_Math_DegreesToRadians(const RuntimeScriptValue * params,int32_t param_count)192 RuntimeScriptValue Sc_Math_DegreesToRadians(const RuntimeScriptValue *params, int32_t param_count) {
193 	API_SCALL_FLOAT_PFLOAT(Math_DegreesToRadians);
194 }
195 
196 // float (float value)
Sc_Math_Exp(const RuntimeScriptValue * params,int32_t param_count)197 RuntimeScriptValue Sc_Math_Exp(const RuntimeScriptValue *params, int32_t param_count) {
198 	API_SCALL_FLOAT_PFLOAT(Math_Exp);
199 }
200 
201 // float (float value)
Sc_Math_Log(const RuntimeScriptValue * params,int32_t param_count)202 RuntimeScriptValue Sc_Math_Log(const RuntimeScriptValue *params, int32_t param_count) {
203 	API_SCALL_FLOAT_PFLOAT(Math_Log);
204 }
205 
206 // float (float value)
Sc_Math_Log10(const RuntimeScriptValue * params,int32_t param_count)207 RuntimeScriptValue Sc_Math_Log10(const RuntimeScriptValue *params, int32_t param_count) {
208 	API_SCALL_FLOAT_PFLOAT(Math_Log10);
209 }
210 
211 // float (float value)
Sc_Math_RadiansToDegrees(const RuntimeScriptValue * params,int32_t param_count)212 RuntimeScriptValue Sc_Math_RadiansToDegrees(const RuntimeScriptValue *params, int32_t param_count) {
213 	API_SCALL_FLOAT_PFLOAT(Math_RadiansToDegrees);
214 }
215 
216 // float (SCRIPT_FLOAT(base), SCRIPT_FLOAT(exp))
Sc_Math_RaiseToPower(const RuntimeScriptValue * params,int32_t param_count)217 RuntimeScriptValue Sc_Math_RaiseToPower(const RuntimeScriptValue *params, int32_t param_count) {
218 	API_SCALL_FLOAT_PFLOAT2(Math_RaiseToPower);
219 }
220 
221 // float (float value)
Sc_Math_Sin(const RuntimeScriptValue * params,int32_t param_count)222 RuntimeScriptValue Sc_Math_Sin(const RuntimeScriptValue *params, int32_t param_count) {
223 	API_SCALL_FLOAT_PFLOAT(Math_Sin);
224 }
225 
226 // float (float value)
Sc_Math_Sinh(const RuntimeScriptValue * params,int32_t param_count)227 RuntimeScriptValue Sc_Math_Sinh(const RuntimeScriptValue *params, int32_t param_count) {
228 	API_SCALL_FLOAT_PFLOAT(Math_Sinh);
229 }
230 
231 // float (float value)
Sc_Math_Sqrt(const RuntimeScriptValue * params,int32_t param_count)232 RuntimeScriptValue Sc_Math_Sqrt(const RuntimeScriptValue *params, int32_t param_count) {
233 	API_SCALL_FLOAT_PFLOAT(Math_Sqrt);
234 }
235 
236 // float (float value)
Sc_Math_Tan(const RuntimeScriptValue * params,int32_t param_count)237 RuntimeScriptValue Sc_Math_Tan(const RuntimeScriptValue *params, int32_t param_count) {
238 	API_SCALL_FLOAT_PFLOAT(Math_Tan);
239 }
240 
241 // float (float value)
Sc_Math_Tanh(const RuntimeScriptValue * params,int32_t param_count)242 RuntimeScriptValue Sc_Math_Tanh(const RuntimeScriptValue *params, int32_t param_count) {
243 	API_SCALL_FLOAT_PFLOAT(Math_Tanh);
244 }
245 
246 // float ()
Sc_Math_GetPi(const RuntimeScriptValue * params,int32_t param_count)247 RuntimeScriptValue Sc_Math_GetPi(const RuntimeScriptValue *params, int32_t param_count) {
248 	API_SCALL_FLOAT(Math_GetPi);
249 }
250 
251 
RegisterMathAPI()252 void RegisterMathAPI() {
253 	ccAddExternalStaticFunction("Maths::ArcCos^1", Sc_Math_ArcCos);
254 	ccAddExternalStaticFunction("Maths::ArcSin^1", Sc_Math_ArcSin);
255 	ccAddExternalStaticFunction("Maths::ArcTan^1", Sc_Math_ArcTan);
256 	ccAddExternalStaticFunction("Maths::ArcTan2^2", Sc_Math_ArcTan2);
257 	ccAddExternalStaticFunction("Maths::Cos^1", Sc_Math_Cos);
258 	ccAddExternalStaticFunction("Maths::Cosh^1", Sc_Math_Cosh);
259 	ccAddExternalStaticFunction("Maths::DegreesToRadians^1", Sc_Math_DegreesToRadians);
260 	ccAddExternalStaticFunction("Maths::Exp^1", Sc_Math_Exp);
261 	ccAddExternalStaticFunction("Maths::Log^1", Sc_Math_Log);
262 	ccAddExternalStaticFunction("Maths::Log10^1", Sc_Math_Log10);
263 	ccAddExternalStaticFunction("Maths::RadiansToDegrees^1", Sc_Math_RadiansToDegrees);
264 	ccAddExternalStaticFunction("Maths::RaiseToPower^2", Sc_Math_RaiseToPower);
265 	ccAddExternalStaticFunction("Maths::Sin^1", Sc_Math_Sin);
266 	ccAddExternalStaticFunction("Maths::Sinh^1", Sc_Math_Sinh);
267 	ccAddExternalStaticFunction("Maths::Sqrt^1", Sc_Math_Sqrt);
268 	ccAddExternalStaticFunction("Maths::Tan^1", Sc_Math_Tan);
269 	ccAddExternalStaticFunction("Maths::Tanh^1", Sc_Math_Tanh);
270 	ccAddExternalStaticFunction("Maths::get_Pi", Sc_Math_GetPi);
271 }
272 
273 } // namespace AGS3
274