(svn r9356) [NoAI] -Add: add abs() function to global scope in SQ. This means we now have an own squirrel_std class which registers such functions. (on request by Zuu)
--- a/bin/ai/regression/regression.nut Mon Mar 19 14:30:56 2007 +0000
+++ b/bin/ai/regression/regression.nut Mon Mar 19 23:43:20 2007 +0000
@@ -18,6 +18,14 @@
print(" TickTest: " + this.GetTick());
}
+function Regression::Std()
+{
+ print("");
+ print("--Std--");
+ print(" abs(-21): " + abs(-21));
+ print(" abs( 21): " + abs(21));
+}
+
function Regression::Base()
{
local base = AIBase();
@@ -208,6 +216,7 @@
function Regression::Start()
{
this.TestInit();
+ this.Std();
this.Base();
this.Cargo();
this.Company();
--- a/bin/ai/regression/regression.txt Mon Mar 19 14:30:56 2007 +0000
+++ b/bin/ai/regression/regression.txt Mon Mar 19 23:43:20 2007 +0000
@@ -3,6 +3,10 @@
TickTest: 0
TickTest: 1
+--Std--
+ abs(-21): 21
+ abs( 21): 21
+
--AIBase--
Rand(): 985188034
Rand(): -1871827240
--- a/source.list Mon Mar 19 14:30:56 2007 +0000
+++ b/source.list Mon Mar 19 23:43:20 2007 +0000
@@ -69,6 +69,7 @@
strings.cpp
squirrel.cpp
squirrel_helper.cpp
+squirrel_std.cpp
texteff.cpp
tgp.cpp
thread.cpp
@@ -167,6 +168,7 @@
squirrel.hpp
squirrel_class.hpp
squirrel_helper.hpp
+squirrel_std.hpp
video/sdl_v.h
settings.h
signs.h
--- a/src/ai/ai_squirrel.cpp Mon Mar 19 14:30:56 2007 +0000
+++ b/src/ai/ai_squirrel.cpp Mon Mar 19 23:43:20 2007 +0000
@@ -14,6 +14,7 @@
#include "../squirrel.hpp"
#include "../squirrel_helper.hpp"
#include "../squirrel_class.hpp"
+#include "../squirrel_std.hpp"
#include "api/ai_controller.hpp"
#include "ai_factory.hpp"
#include "ai_squirrel.hpp"
@@ -184,6 +185,7 @@
this->engine->AddClassEnd();
/* Register all classes */
+ squirrel_register_std(this->engine);
SQAIBaseRegister(this->engine);
SQAICargoRegister(this->engine);
SQAICompanyRegister(this->engine);
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/squirrel_std.cpp Mon Mar 19 23:43:20 2007 +0000
@@ -0,0 +1,25 @@
+
+#include <squirrel.h>
+#include "stdafx.h"
+#include "squirrel.hpp"
+#include "squirrel_std.hpp"
+#include "helpers.hpp"
+
+/* abs() is normally defined to myabs(), which we don't want in this file */
+#undef abs
+
+SQInteger SquirrelStd::abs(HSQUIRRELVM vm)
+{
+ SQInteger tmp;
+
+ sq_getinteger(vm, 2, &tmp);
+ sq_pushinteger(vm, myabs(tmp));
+ return 1;
+}
+
+void squirrel_register_std(Squirrel *engine)
+{
+ /* We don't use squirrel_helper here, as we want to register to the global
+ * scope and not to a class. */
+ engine->AddMethod("abs", &SquirrelStd::abs, 2, "xi");
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/squirrel_std.hpp Mon Mar 19 23:43:20 2007 +0000
@@ -0,0 +1,27 @@
+/* $Id$ */
+
+/** @file squirrel_std.hpp defines the Squirrel Standard Function class */
+
+#ifndef SQUIRREL_STD_HPP
+#define SQUIRREL_STD_HPP
+
+/**
+ * By default we want to give a set of standard commands to a SQ script.
+ * Most of them are easy wrappers around internal functions. Of course we
+ * could just as easy include things like the stdmath of SQ, but of those
+ * functions we are sure they work on all our supported targets.
+ */
+class SquirrelStd {
+public:
+ /**
+ * Make an integer absolute.
+ */
+ static SQInteger abs(HSQUIRRELVM vm);
+};
+
+/**
+ * Register all standard functions we want to give to a script.
+ */
+void squirrel_register_std(Squirrel *engine);
+
+#endif /* SQUIRREL_STD_HPP */