Fix #8316: Make sort industries by production and transported with a cargo filter...
[openttd-github.git] / src / script / squirrel_std.cpp
blob82943e526880161920e76171338582221703f75c
1 /*
2 * This file is part of OpenTTD.
3 * OpenTTD 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, version 2.
4 * OpenTTD 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.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
6 */
8 /** @file squirrel_std.cpp Implements the Squirrel Standard Function class */
10 #include "../stdafx.h"
11 #include <squirrel.h>
12 #include <sqstdmath.h>
13 #include "../debug.h"
14 #include "squirrel_std.hpp"
15 #include "../core/alloc_func.hpp"
16 #include "../core/math_func.hpp"
17 #include "../string_func.h"
19 #include "../safeguards.h"
22 SQInteger SquirrelStd::min(HSQUIRRELVM vm)
24 SQInteger tmp1, tmp2;
26 sq_getinteger(vm, 2, &tmp1);
27 sq_getinteger(vm, 3, &tmp2);
28 sq_pushinteger(vm, std::min(tmp1, tmp2));
29 return 1;
32 SQInteger SquirrelStd::max(HSQUIRRELVM vm)
34 SQInteger tmp1, tmp2;
36 sq_getinteger(vm, 2, &tmp1);
37 sq_getinteger(vm, 3, &tmp2);
38 sq_pushinteger(vm, std::max(tmp1, tmp2));
39 return 1;
42 SQInteger SquirrelStd::require(HSQUIRRELVM vm)
44 SQInteger top = sq_gettop(vm);
45 const SQChar *filename;
47 sq_getstring(vm, 2, &filename);
49 /* Get the script-name of the current file, so we can work relative from it */
50 SQStackInfos si;
51 sq_stackinfos(vm, 1, &si);
52 if (si.source == nullptr) {
53 Debug(misc, 0, "[squirrel] Couldn't detect the script-name of the 'require'-caller; this should never happen!");
54 return SQ_ERROR;
57 char path[MAX_PATH];
58 strecpy(path, si.source, lastof(path));
59 /* Keep the dir, remove the rest */
60 SQChar *s = strrchr(path, PATHSEPCHAR);
61 if (s != nullptr) {
62 /* Keep the PATHSEPCHAR there, remove the rest */
63 s++;
64 *s = '\0';
66 strecat(path, filename, lastof(path));
67 #if (PATHSEPCHAR != '/')
68 for (char *n = path; *n != '\0'; n++) if (*n == '/') *n = PATHSEPCHAR;
69 #endif
71 Squirrel *engine = (Squirrel *)sq_getforeignptr(vm);
72 bool ret = engine->LoadScript(vm, path);
74 /* Reset the top, so the stack stays correct */
75 sq_settop(vm, top);
77 return ret ? 0 : SQ_ERROR;
80 SQInteger SquirrelStd::notifyallexceptions(HSQUIRRELVM vm)
82 SQBool b;
84 if (sq_gettop(vm) >= 1) {
85 if (SQ_SUCCEEDED(sq_getbool(vm, -1, &b))) {
86 sq_notifyallexceptions(vm, b);
87 return 0;
91 return SQ_ERROR;
94 void squirrel_register_global_std(Squirrel *engine)
96 /* We don't use squirrel_helper here, as we want to register to the global
97 * scope and not to a class. */
98 engine->AddMethod("require", &SquirrelStd::require, 2, ".s");
99 engine->AddMethod("notifyallexceptions", &SquirrelStd::notifyallexceptions, 2, ".b");
102 void squirrel_register_std(Squirrel *engine)
104 /* We don't use squirrel_helper here, as we want to register to the global
105 * scope and not to a class. */
106 engine->AddMethod("min", &SquirrelStd::min, 3, ".ii");
107 engine->AddMethod("max", &SquirrelStd::max, 3, ".ii");
109 sqstd_register_mathlib(engine->GetVM());