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 "JSInterface_Simulation.h"
21 :
22 : #include "graphics/GameView.h"
23 : #include "lib/debug.h"
24 : #include "lib/os_path.h"
25 : #include "lib/path.h"
26 : #include "maths/Fixed.h"
27 : #include "maths/FixedVector2D.h"
28 : #include "maths/FixedVector3D.h"
29 : #include "ps/ConfigDB.h"
30 : #include "ps/Game.h"
31 : #include "ps/GameSetup/Config.h"
32 : #include "ps/Pyrogenesis.h"
33 : #include "ps/VideoMode.h"
34 : #include "scriptinterface/FunctionWrapper.h"
35 : #include "scriptinterface/Object.h"
36 : #include "scriptinterface/Request.h"
37 : #include "scriptinterface/StructuredClone.h"
38 : #include "simulation2/Simulation2.h"
39 : #include "simulation2/components/ICmpAIManager.h"
40 : #include "simulation2/components/ICmpCommandQueue.h"
41 : #include "simulation2/components/ICmpGuiInterface.h"
42 : #include "simulation2/components/ICmpObstruction.h"
43 : #include "simulation2/components/ICmpPosition.h"
44 : #include "simulation2/components/ICmpSelectable.h"
45 : #include "simulation2/helpers/Geometry.h"
46 : #include "simulation2/helpers/Position.h"
47 : #include "simulation2/helpers/Selection.h"
48 : #include "simulation2/system/Component.h"
49 : #include "simulation2/system/Entity.h"
50 :
51 : #include <algorithm>
52 : #include <array>
53 : #include <cstddef>
54 : #include <fstream>
55 : #include <js/RootingAPI.h>
56 : #include <js/TypeDecls.h>
57 : #include <js/Value.h>
58 : #include <string>
59 : #include <vector>
60 :
61 : namespace Script { class Interface; }
62 :
63 : namespace JSI_Simulation
64 : {
65 0 : JS::Value GuiInterfaceCall(const Script::Interface& scriptInterface, const std::wstring& name, JS::HandleValue data)
66 : {
67 0 : if (!g_Game)
68 0 : return JS::UndefinedValue();
69 :
70 0 : CSimulation2* sim = g_Game->GetSimulation2();
71 0 : ENSURE(sim);
72 :
73 0 : CmpPtr<ICmpGuiInterface> cmpGuiInterface(*sim, SYSTEM_ENTITY);
74 0 : if (!cmpGuiInterface)
75 0 : return JS::UndefinedValue();
76 :
77 0 : Script::Request rqSim(sim->GetScriptInterface());
78 0 : JS::RootedValue arg(rqSim.cx, Script::CloneValueFromOtherCompartment(sim->GetScriptInterface(), scriptInterface, data));
79 0 : JS::RootedValue ret(rqSim.cx);
80 0 : cmpGuiInterface->ScriptCall(g_Game->GetViewedPlayerID(), name, arg, &ret);
81 :
82 0 : return Script::CloneValueFromOtherCompartment(scriptInterface, sim->GetScriptInterface(), ret);
83 : }
84 :
85 0 : void PostNetworkCommand(const Script::Interface& scriptInterface, JS::HandleValue cmd)
86 : {
87 0 : if (!g_Game)
88 0 : return;
89 :
90 0 : CSimulation2* sim = g_Game->GetSimulation2();
91 0 : ENSURE(sim);
92 :
93 0 : CmpPtr<ICmpCommandQueue> cmpCommandQueue(*sim, SYSTEM_ENTITY);
94 0 : if (!cmpCommandQueue)
95 : return;
96 :
97 0 : Script::Request rqSim(sim->GetScriptInterface());
98 0 : JS::RootedValue cmd2(rqSim.cx, Script::CloneValueFromOtherCompartment(sim->GetScriptInterface(), scriptInterface, cmd));
99 :
100 0 : cmpCommandQueue->PostNetworkCommand(cmd2);
101 : }
102 :
103 0 : void DumpSimState()
104 : {
105 0 : OsPath path = psLogDir()/"sim_dump.txt";
106 0 : std::ofstream file (OsString(path), std::ofstream::out | std::ofstream::trunc);
107 0 : g_Game->GetSimulation2()->DumpDebugState(file);
108 0 : }
109 :
110 0 : entity_id_t PickEntityAtPoint(int x, int y)
111 : {
112 0 : return EntitySelection::PickEntityAtPoint(*g_Game->GetSimulation2(), g_Game->GetView()->GetCamera(), x, y, g_Game->GetViewedPlayerID(), false);
113 : }
114 :
115 0 : std::vector<entity_id_t> PickPlayerEntitiesInRect(int x0, int y0, int x1, int y1, int player)
116 : {
117 0 : return EntitySelection::PickEntitiesInRect(*g_Game->GetSimulation2(), g_Game->GetView()->GetCamera(), x0, y0, x1, y1, player, false);
118 : }
119 :
120 0 : std::vector<entity_id_t> PickPlayerEntitiesOnScreen(int player)
121 : {
122 0 : return EntitySelection::PickEntitiesInRect(*g_Game->GetSimulation2(), g_Game->GetView()->GetCamera(), 0, 0, g_VideoMode.GetWindowWidth(), g_VideoMode.GetWindowHeight(), player, false);
123 : }
124 :
125 0 : std::vector<entity_id_t> PickNonGaiaEntitiesOnScreen()
126 : {
127 0 : return EntitySelection::PickNonGaiaEntitiesInRect(*g_Game->GetSimulation2(), g_Game->GetView()->GetCamera(), 0, 0, g_VideoMode.GetWindowWidth(), g_VideoMode.GetWindowHeight(), false);
128 : }
129 :
130 0 : std::vector<entity_id_t> GetEntitiesWithStaticObstructionOnScreen()
131 : {
132 0 : struct StaticObstructionFilter
133 : {
134 0 : bool operator()(IComponent* cmp)
135 : {
136 0 : ICmpObstruction* cmpObstruction = static_cast<ICmpObstruction*>(cmp);
137 0 : return cmpObstruction->GetObstructionType() == ICmpObstruction::STATIC;
138 : }
139 : };
140 0 : return EntitySelection::GetEntitiesWithComponentInRect<StaticObstructionFilter>(*g_Game->GetSimulation2(),
141 : IID_Obstruction, g_Game->GetView()->GetCamera(), 0, 0,
142 0 : g_VideoMode.GetWindowWidth(), g_VideoMode.GetWindowHeight());
143 : }
144 :
145 0 : JS::Value GetEdgesOfStaticObstructionsOnScreenNearTo(const Script::Interface& scriptInterface, entity_pos_t x, entity_pos_t z)
146 : {
147 0 : if (!g_Game)
148 0 : return JS::UndefinedValue();
149 :
150 0 : CSimulation2* sim = g_Game->GetSimulation2();
151 0 : ENSURE(sim);
152 :
153 0 : Script::Request rq(scriptInterface);
154 0 : JS::RootedValueVector edgeList{rq.cx};
155 :
156 0 : const float distanceThreshold{g_ConfigDB.Get("gui.session.snaptoedgesdistancethreshold", 10.0f)};
157 0 : CFixedVector2D entityPos(x, z);
158 :
159 0 : std::vector<entity_id_t> entities = GetEntitiesWithStaticObstructionOnScreen();
160 0 : for (entity_id_t entity : entities)
161 : {
162 0 : CmpPtr<ICmpObstruction> cmpObstruction(sim->GetSimContext(), entity);
163 0 : if (!cmpObstruction)
164 0 : continue;
165 :
166 0 : CmpPtr<ICmpPosition> cmpPosition(sim->GetSimContext(), entity);
167 0 : if (!cmpPosition || !cmpPosition->IsInWorld())
168 0 : continue;
169 :
170 0 : CFixedVector2D halfSize = cmpObstruction->GetStaticSize() / 2;
171 0 : if (halfSize.X.IsZero() || halfSize.Y.IsZero() || std::max(halfSize.X, halfSize.Y) <= fixed::FromInt(2))
172 0 : continue;
173 :
174 0 : std::array<CFixedVector2D, 4> corners = {
175 : CFixedVector2D(-halfSize.X, -halfSize.Y),
176 : CFixedVector2D(-halfSize.X, halfSize.Y),
177 : halfSize,
178 : CFixedVector2D(halfSize.X, -halfSize.Y)
179 0 : };
180 0 : const fixed angle = cmpPosition->GetRotation().Y;
181 0 : for (CFixedVector2D& corner : corners)
182 0 : corner = corner.Rotate(angle) + cmpPosition->GetPosition2D();
183 :
184 0 : for (size_t i = 0; i < corners.size(); ++i)
185 : {
186 0 : JS::RootedValue edge(rq.cx);
187 0 : const CFixedVector2D& corner = corners[i];
188 0 : const CFixedVector2D& nextCorner = corners[(i + 1) % corners.size()];
189 :
190 0 : const fixed distanceToEdge =
191 0 : Geometry::DistanceToSegment(entityPos, corner, nextCorner);
192 0 : if (distanceToEdge.ToFloat() > distanceThreshold)
193 0 : continue;
194 :
195 0 : CFixedVector2D normal = -(nextCorner - corner).Perpendicular();
196 0 : normal.Normalize();
197 0 : Script::CreateObject(
198 : rq,
199 : &edge,
200 : "begin", corner,
201 : "end", nextCorner,
202 : "angle", angle,
203 : "normal", normal,
204 : "order", "cw");
205 :
206 0 : if (!edgeList.append(edge))
207 0 : throw std::runtime_error{"Append failed"};
208 0 : }
209 : }
210 0 : return JS::ObjectValue(*JS::NewArrayObject(rq.cx, edgeList));
211 : }
212 :
213 0 : std::vector<entity_id_t> PickSimilarPlayerEntities(const std::string& templateName, bool includeOffScreen, bool matchRank, bool allowFoundations)
214 : {
215 0 : return EntitySelection::PickSimilarEntities(*g_Game->GetSimulation2(), g_Game->GetView()->GetCamera(), templateName, g_Game->GetViewedPlayerID(), includeOffScreen, matchRank, false, allowFoundations);
216 : }
217 :
218 0 : JS::Value GetAIs(const Script::Interface& scriptInterface)
219 : {
220 0 : return ICmpAIManager::GetAIs(scriptInterface);
221 : }
222 :
223 0 : void SetBoundingBoxDebugOverlay(bool enabled)
224 : {
225 0 : ICmpSelectable::ms_EnableDebugOverlays = enabled;
226 0 : }
227 :
228 26 : void RegisterScriptFunctions(const Script::Request& rq)
229 : {
230 26 : Script::Function::Register<&GuiInterfaceCall>(rq, "GuiInterfaceCall");
231 26 : Script::Function::Register<&PostNetworkCommand>(rq, "PostNetworkCommand");
232 26 : Script::Function::Register<&DumpSimState>(rq, "DumpSimState");
233 26 : Script::Function::Register<&GetAIs>(rq, "GetAIs");
234 26 : Script::Function::Register<&PickEntityAtPoint>(rq, "PickEntityAtPoint");
235 26 : Script::Function::Register<&PickPlayerEntitiesInRect>(rq, "PickPlayerEntitiesInRect");
236 26 : Script::Function::Register<&PickPlayerEntitiesOnScreen>(rq, "PickPlayerEntitiesOnScreen");
237 26 : Script::Function::Register<&PickNonGaiaEntitiesOnScreen>(rq, "PickNonGaiaEntitiesOnScreen");
238 26 : Script::Function::Register<&GetEntitiesWithStaticObstructionOnScreen>(rq, "GetEntitiesWithStaticObstructionOnScreen");
239 26 : Script::Function::Register<&GetEdgesOfStaticObstructionsOnScreenNearTo>(rq, "GetEdgesOfStaticObstructionsOnScreenNearTo");
240 26 : Script::Function::Register<&PickSimilarPlayerEntities>(rq, "PickSimilarPlayerEntities");
241 26 : Script::Function::Register<&SetBoundingBoxDebugOverlay>(rq, "SetBoundingBoxDebugOverlay");
242 26 : }
243 : }
|