# HG changeset patch # User truebrain # Date 1213293830 0 # Node ID df6235dd2b7af4a9e3331ea179dc53ee4fc5f666 # Parent 1232882bfe7a40f2c23f74b56b2c10dd574c800f (svn r13492) [NoAI] -Add: added the 'standard' functions max() and min() in global scope diff -r 1232882bfe7a -r df6235dd2b7a bin/ai/regression/regression.nut --- a/bin/ai/regression/regression.nut Thu Jun 12 13:24:33 2008 +0000 +++ b/bin/ai/regression/regression.nut Thu Jun 12 18:03:50 2008 +0000 @@ -19,6 +19,10 @@ print(" IsValid(vehicle.plane_speed): " + AIGameSettings.IsValid("vehicle.plane_speed")); print(" vehicle.plane_speed: " + AIGameSettings.GetValue("vehicle.plane_speed")); require("require.nut"); + print(" min(6, 3): " + min(6, 3)); + print(" min(3, 6): " + min(3, 6)); + print(" max(6, 3): " + max(6, 3)); + print(" max(3, 6): " + max(3, 6)); } function Regression::Std() diff -r 1232882bfe7a -r df6235dd2b7a bin/ai/regression/regression.txt --- a/bin/ai/regression/regression.txt Thu Jun 12 13:24:33 2008 +0000 +++ b/bin/ai/regression/regression.txt Thu Jun 12 18:03:50 2008 +0000 @@ -6,6 +6,10 @@ IsValid(vehicle.plane_speed): true vehicle.plane_speed: 2 Required this file + min(6, 3): 3 + min(3, 6): 3 + max(6, 3): 6 + max(3, 6): 6 --Std-- abs(-21): 21 diff -r 1232882bfe7a -r df6235dd2b7a src/squirrel_std.cpp --- a/src/squirrel_std.cpp Thu Jun 12 13:24:33 2008 +0000 +++ b/src/squirrel_std.cpp Thu Jun 12 18:03:50 2008 +0000 @@ -5,6 +5,7 @@ #include "squirrel.hpp" #include "squirrel_std.hpp" #include "core/alloc_func.hpp" +#include "core/math_func.hpp" /* abs() is normally defined to myabs(), which we don't want in this file */ #undef abs @@ -18,6 +19,26 @@ return 1; } +SQInteger SquirrelStd::min(HSQUIRRELVM vm) +{ + SQInteger tmp1, tmp2; + + sq_getinteger(vm, 2, &tmp1); + sq_getinteger(vm, 3, &tmp2); + sq_pushinteger(vm, ::min(tmp1, tmp2)); + return 1; +} + +SQInteger SquirrelStd::max(HSQUIRRELVM vm) +{ + SQInteger tmp1, tmp2; + + sq_getinteger(vm, 2, &tmp1); + sq_getinteger(vm, 3, &tmp2); + sq_pushinteger(vm, ::max(tmp1, tmp2)); + return 1; +} + SQInteger SquirrelStd::require(HSQUIRRELVM vm) { SQInteger top = sq_gettop(vm); @@ -66,5 +87,7 @@ { /* 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"); + engine->AddMethod("abs", &SquirrelStd::abs, 2, "?i"); + engine->AddMethod("min", &SquirrelStd::min, 3, "?ii"); + engine->AddMethod("max", &SquirrelStd::max, 3, "?ii"); } diff -r 1232882bfe7a -r df6235dd2b7a src/squirrel_std.hpp --- a/src/squirrel_std.hpp Thu Jun 12 13:24:33 2008 +0000 +++ b/src/squirrel_std.hpp Thu Jun 12 18:03:50 2008 +0000 @@ -24,6 +24,16 @@ static SQInteger abs(HSQUIRRELVM vm); /** + * Get the lowest of two integers. + */ + static SQInteger min(HSQUIRRELVM vm); + + /** + * Get the highest of two integers. + */ + static SQInteger max(HSQUIRRELVM vm); + + /** * Load an other file on runtime. * @note This is always loaded on the root-level, no matter where you call this. * @note The filename is always relative from the script it is called from. Absolute calls are NOT allowed!