/engines/wintermute/base/scriptables/script_ext_math.cpp

https://github.com/heather162/scummvm · C++ · 294 lines · 146 code · 46 blank · 102 comment · 66 complexity · 6481cb417bb13bbe66bc6e2e96b66796 MD5 · raw file

  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. * This file is based on WME Lite.
  24. * http://dead-code.org/redir.php?target=wmelite
  25. * Copyright (c) 2011 Jan Nedoma
  26. */
  27. #include "engines/wintermute/base/scriptables/script_ext_math.h"
  28. #include "engines/wintermute/base/scriptables/script_stack.h"
  29. #include "engines/wintermute/base/scriptables/script_value.h"
  30. #include "engines/wintermute/persistent.h"
  31. #include "common/math.h"
  32. namespace Wintermute {
  33. //////////////////////////////////////////////////////////////////////
  34. // Construction/Destruction
  35. //////////////////////////////////////////////////////////////////////
  36. IMPLEMENT_PERSISTENT(SXMath, true)
  37. BaseScriptable *makeSXMath(BaseGame *inGame) {
  38. return new SXMath(inGame);
  39. }
  40. //////////////////////////////////////////////////////////////////////////
  41. SXMath::SXMath(BaseGame *inGame) : BaseScriptable(inGame) {
  42. }
  43. //////////////////////////////////////////////////////////////////////////
  44. SXMath::~SXMath() {
  45. }
  46. //////////////////////////////////////////////////////////////////////////
  47. bool SXMath::scCallMethod(ScScript *script, ScStack *stack, ScStack *thisStack, const char *name) {
  48. //////////////////////////////////////////////////////////////////////////
  49. // Abs
  50. //////////////////////////////////////////////////////////////////////////
  51. if (strcmp(name, "Abs") == 0) {
  52. stack->correctParams(1);
  53. stack->pushFloat(fabs(stack->pop()->getFloat()));
  54. return STATUS_OK;
  55. }
  56. //////////////////////////////////////////////////////////////////////////
  57. // Acos
  58. //////////////////////////////////////////////////////////////////////////
  59. else if (strcmp(name, "Acos") == 0) {
  60. stack->correctParams(1);
  61. stack->pushFloat(acos(stack->pop()->getFloat()));
  62. return STATUS_OK;
  63. }
  64. //////////////////////////////////////////////////////////////////////////
  65. // Asin
  66. //////////////////////////////////////////////////////////////////////////
  67. else if (strcmp(name, "Asin") == 0) {
  68. stack->correctParams(1);
  69. stack->pushFloat(asin(stack->pop()->getFloat()));
  70. return STATUS_OK;
  71. }
  72. //////////////////////////////////////////////////////////////////////////
  73. // Atan
  74. //////////////////////////////////////////////////////////////////////////
  75. else if (strcmp(name, "Atan") == 0) {
  76. stack->correctParams(1);
  77. stack->pushFloat(atan(stack->pop()->getFloat()));
  78. return STATUS_OK;
  79. }
  80. //////////////////////////////////////////////////////////////////////////
  81. // Atan2
  82. //////////////////////////////////////////////////////////////////////////
  83. else if (strcmp(name, "Atan2") == 0) {
  84. stack->correctParams(2);
  85. double y = stack->pop()->getFloat();
  86. double x = stack->pop()->getFloat();
  87. stack->pushFloat(atan2(y, x));
  88. return STATUS_OK;
  89. }
  90. //////////////////////////////////////////////////////////////////////////
  91. // Ceil
  92. //////////////////////////////////////////////////////////////////////////
  93. else if (strcmp(name, "Ceil") == 0) {
  94. stack->correctParams(1);
  95. stack->pushFloat(ceil(stack->pop()->getFloat()));
  96. return STATUS_OK;
  97. }
  98. //////////////////////////////////////////////////////////////////////////
  99. // Cos
  100. //////////////////////////////////////////////////////////////////////////
  101. else if (strcmp(name, "Cos") == 0) {
  102. stack->correctParams(1);
  103. stack->pushFloat(cos(degreeToRadian(stack->pop()->getFloat())));
  104. return STATUS_OK;
  105. }
  106. //////////////////////////////////////////////////////////////////////////
  107. // Cosh
  108. //////////////////////////////////////////////////////////////////////////
  109. else if (strcmp(name, "Cosh") == 0) {
  110. stack->correctParams(1);
  111. stack->pushFloat(cosh(degreeToRadian(stack->pop()->getFloat())));
  112. return STATUS_OK;
  113. }
  114. //////////////////////////////////////////////////////////////////////////
  115. // Exp
  116. //////////////////////////////////////////////////////////////////////////
  117. else if (strcmp(name, "Exp") == 0) {
  118. stack->correctParams(1);
  119. stack->pushFloat(exp(stack->pop()->getFloat()));
  120. return STATUS_OK;
  121. }
  122. //////////////////////////////////////////////////////////////////////////
  123. // Floor
  124. //////////////////////////////////////////////////////////////////////////
  125. else if (strcmp(name, "Floor") == 0) {
  126. stack->correctParams(1);
  127. stack->pushFloat(floor(stack->pop()->getFloat()));
  128. return STATUS_OK;
  129. }
  130. //////////////////////////////////////////////////////////////////////////
  131. // Log
  132. //////////////////////////////////////////////////////////////////////////
  133. else if (strcmp(name, "Log") == 0) {
  134. stack->correctParams(1);
  135. stack->pushFloat(log(stack->pop()->getFloat()));
  136. return STATUS_OK;
  137. }
  138. //////////////////////////////////////////////////////////////////////////
  139. // Log10
  140. //////////////////////////////////////////////////////////////////////////
  141. else if (strcmp(name, "Log10") == 0) {
  142. stack->correctParams(1);
  143. stack->pushFloat(log10(stack->pop()->getFloat()));
  144. return STATUS_OK;
  145. }
  146. //////////////////////////////////////////////////////////////////////////
  147. // Pow
  148. //////////////////////////////////////////////////////////////////////////
  149. else if (strcmp(name, "Pow") == 0) {
  150. stack->correctParams(2);
  151. double x = stack->pop()->getFloat();
  152. double y = stack->pop()->getFloat();
  153. stack->pushFloat(pow(x, y));
  154. return STATUS_OK;
  155. }
  156. //////////////////////////////////////////////////////////////////////////
  157. // Sin
  158. //////////////////////////////////////////////////////////////////////////
  159. else if (strcmp(name, "Sin") == 0) {
  160. stack->correctParams(1);
  161. stack->pushFloat(sin(degreeToRadian(stack->pop()->getFloat())));
  162. return STATUS_OK;
  163. }
  164. //////////////////////////////////////////////////////////////////////////
  165. // Sinh
  166. //////////////////////////////////////////////////////////////////////////
  167. else if (strcmp(name, "Sinh") == 0) {
  168. stack->correctParams(1);
  169. stack->pushFloat(sinh(degreeToRadian(stack->pop()->getFloat())));
  170. return STATUS_OK;
  171. }
  172. //////////////////////////////////////////////////////////////////////////
  173. // Tan
  174. //////////////////////////////////////////////////////////////////////////
  175. else if (strcmp(name, "Tan") == 0) {
  176. stack->correctParams(1);
  177. stack->pushFloat(tan(degreeToRadian(stack->pop()->getFloat())));
  178. return STATUS_OK;
  179. }
  180. //////////////////////////////////////////////////////////////////////////
  181. // Tanh
  182. //////////////////////////////////////////////////////////////////////////
  183. else if (strcmp(name, "Tanh") == 0) {
  184. stack->correctParams(1);
  185. stack->pushFloat(tanh(degreeToRadian(stack->pop()->getFloat())));
  186. return STATUS_OK;
  187. }
  188. //////////////////////////////////////////////////////////////////////////
  189. // Sqrt
  190. //////////////////////////////////////////////////////////////////////////
  191. else if (strcmp(name, "Sqrt") == 0) {
  192. stack->correctParams(1);
  193. stack->pushFloat(sqrt(stack->pop()->getFloat()));
  194. return STATUS_OK;
  195. }
  196. //////////////////////////////////////////////////////////////////////////
  197. // DegToRad
  198. //////////////////////////////////////////////////////////////////////////
  199. else if (strcmp(name, "DegToRad") == 0) {
  200. stack->correctParams(1);
  201. stack->pushFloat(degreeToRadian(stack->pop()->getFloat()));
  202. return STATUS_OK;
  203. }
  204. //////////////////////////////////////////////////////////////////////////
  205. // RadToDeg
  206. //////////////////////////////////////////////////////////////////////////
  207. else if (strcmp(name, "RadToDeg") == 0) {
  208. stack->correctParams(1);
  209. stack->pushFloat(radianToDegree(stack->pop()->getFloat()));
  210. return STATUS_OK;
  211. } else {
  212. return STATUS_FAILED;
  213. }
  214. }
  215. //////////////////////////////////////////////////////////////////////////
  216. ScValue *SXMath::scGetProperty(const Common::String &name) {
  217. _scValue->setNULL();
  218. //////////////////////////////////////////////////////////////////////////
  219. // Type
  220. //////////////////////////////////////////////////////////////////////////
  221. if (name == "Type") {
  222. _scValue->setString("math");
  223. return _scValue;
  224. }
  225. //////////////////////////////////////////////////////////////////////////
  226. // PI
  227. //////////////////////////////////////////////////////////////////////////
  228. else if (name == "PI") {
  229. _scValue->setFloat(M_PI);
  230. return _scValue;
  231. } else {
  232. return _scValue;
  233. }
  234. }
  235. //////////////////////////////////////////////////////////////////////////
  236. double SXMath::degreeToRadian(double value) {
  237. return value * (M_PI / 180.0f);
  238. }
  239. //////////////////////////////////////////////////////////////////////////
  240. double SXMath::radianToDegree(double value) {
  241. return value * (180.0f / M_PI);
  242. }
  243. //////////////////////////////////////////////////////////////////////////
  244. bool SXMath::persist(BasePersistenceManager *persistMgr) {
  245. BaseScriptable::persist(persistMgr);
  246. return STATUS_OK;
  247. }
  248. } // End of namespace Wintermute