Line data Source code
1 : /* Copyright (C) 2026 Wildfire Games.
2 : * This file is part of 0 A.D.
3 : *
4 : * 0 A.D. is free software: you can redistribute it and/or modify
5 : * it under the terms of the GNU General Public License as published by
6 : * the Free Software Foundation, either version 2 of the License, or
7 : * (at your option) any later version.
8 : *
9 : * 0 A.D. is distributed in the hope that it will be useful,
10 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 : * GNU General Public License for more details.
13 : *
14 : * You should have received a copy of the GNU General Public License
15 : * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16 : */
17 :
18 : #include "precompiled.h"
19 :
20 : #include "ICmpFootprint.h"
21 :
22 : #include "maths/FixedVector3D.h"
23 : #include "scriptinterface/Conversions.h"
24 : #include "scriptinterface/Request.h"
25 : #include "simulation2/system/InterfaceScripted.h"
26 :
27 : #include <js/PropertyAndElement.h>
28 : #include <js/RootingAPI.h>
29 : #include <js/TypeDecls.h>
30 : #include <jsapi.h>
31 : #include <string>
32 :
33 150 : JS::Value ICmpFootprint::GetShape_wrapper() const
34 : {
35 150 : EShape shape;
36 150 : entity_pos_t size0, size1, height;
37 150 : GetShape(shape, size0, size1, height);
38 :
39 150 : Script::Request rq(GetSimContext().GetScriptInterface());
40 :
41 150 : JS::RootedObject obj(rq.cx, JS_NewPlainObject(rq.cx));
42 150 : if (!obj)
43 0 : return JS::UndefinedValue();
44 :
45 150 : if (shape == CIRCLE)
46 : {
47 132 : JS::RootedValue ptype(rq.cx);
48 132 : JS::RootedValue pradius(rq.cx);
49 132 : JS::RootedValue pheight(rq.cx);
50 132 : Script::ToJSVal<std::string>(rq, &ptype, "circle");
51 132 : Script::ToJSVal(rq, &pradius, size0);
52 132 : Script::ToJSVal(rq, &pheight, height);
53 132 : JS_SetProperty(rq.cx, obj, "type", ptype);
54 132 : JS_SetProperty(rq.cx, obj, "radius", pradius);
55 132 : JS_SetProperty(rq.cx, obj, "height", pheight);
56 132 : }
57 : else
58 : {
59 18 : JS::RootedValue ptype(rq.cx);
60 18 : JS::RootedValue pwidth(rq.cx);
61 18 : JS::RootedValue pdepth(rq.cx);
62 18 : JS::RootedValue pheight(rq.cx);
63 18 : Script::ToJSVal<std::string>(rq, &ptype, "square");
64 18 : Script::ToJSVal(rq, &pwidth, size0);
65 18 : Script::ToJSVal(rq, &pdepth, size1);
66 18 : Script::ToJSVal(rq, &pheight, height);
67 18 : JS_SetProperty(rq.cx, obj, "type", ptype);
68 18 : JS_SetProperty(rq.cx, obj, "width", pwidth);
69 18 : JS_SetProperty(rq.cx, obj, "depth", pdepth);
70 18 : JS_SetProperty(rq.cx, obj, "height", pheight);
71 18 : }
72 :
73 150 : return JS::ObjectValue(*obj);
74 150 : }
75 :
76 : BEGIN_INTERFACE_WRAPPER(Footprint)
77 0 : DEFINE_INTERFACE_METHOD("PickSpawnPoint", ICmpFootprint, PickSpawnPoint)
78 0 : DEFINE_INTERFACE_METHOD("PickSpawnPointBothPass", ICmpFootprint, PickSpawnPointBothPass)
79 : DEFINE_INTERFACE_METHOD("GetShape", ICmpFootprint, GetShape_wrapper)
80 323 : END_INTERFACE_WRAPPER(Footprint)
|