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_GameView.h"
21 :
22 : #include "graphics/Camera.h"
23 : #include "graphics/GameView.h"
24 : #include "graphics/Terrain.h"
25 : #include "maths/Fixed.h"
26 : #include "maths/FixedVector3D.h"
27 : #include "maths/Vector3D.h"
28 : #include "ps/CLogger.h"
29 : #include "ps/Game.h"
30 : #include "ps/World.h"
31 : #include "scriptinterface/FunctionWrapper.h"
32 : #include "scriptinterface/Object.h"
33 : #include "scriptinterface/Request.h"
34 : #include "simulation2/helpers/Position.h"
35 : #include "simulation2/system/Entity.h"
36 :
37 : #include <js/RootingAPI.h>
38 : #include <js/TypeDecls.h>
39 : #include <js/Value.h>
40 :
41 : namespace JSI_GameView
42 : {
43 : #define IMPLEMENT_BOOLEAN_SCRIPT_SETTING(NAME) \
44 : bool Get##NAME##Enabled() \
45 : { \
46 : if (!g_Game || !g_Game->GetView()) \
47 : { \
48 : LOGERROR("Trying to get a setting from GameView when it's not initialized!"); \
49 : return false; \
50 : } \
51 : return g_Game->GetView()->Get##NAME##Enabled(); \
52 : } \
53 : \
54 : void Set##NAME##Enabled(bool Enabled) \
55 : { \
56 : if (!g_Game || !g_Game->GetView()) \
57 : { \
58 : LOGERROR("Trying to set a setting of GameView when it's not initialized!"); \
59 : return; \
60 : } \
61 : g_Game->GetView()->Set##NAME##Enabled(Enabled); \
62 : }
63 :
64 0 : IMPLEMENT_BOOLEAN_SCRIPT_SETTING(Culling);
65 0 : IMPLEMENT_BOOLEAN_SCRIPT_SETTING(LockCullCamera);
66 0 : IMPLEMENT_BOOLEAN_SCRIPT_SETTING(ConstrainCamera);
67 :
68 : #undef IMPLEMENT_BOOLEAN_SCRIPT_SETTING
69 :
70 :
71 : #define REGISTER_BOOLEAN_SCRIPT_SETTING(NAME) \
72 : Script::Function::Register<&Get##NAME##Enabled>(rq, "GameView_Get" #NAME "Enabled"); \
73 : Script::Function::Register<&Set##NAME##Enabled>(rq, "GameView_Set" #NAME "Enabled");
74 :
75 26 : void RegisterScriptFunctions_Settings(const Script::Request& rq)
76 : {
77 26 : REGISTER_BOOLEAN_SCRIPT_SETTING(Culling);
78 26 : REGISTER_BOOLEAN_SCRIPT_SETTING(LockCullCamera);
79 26 : REGISTER_BOOLEAN_SCRIPT_SETTING(ConstrainCamera);
80 26 : }
81 :
82 : #undef REGISTER_BOOLEAN_SCRIPT_SETTING
83 :
84 0 : JS::Value GetCameraRotation(const Script::Request& rq)
85 : {
86 0 : if (!g_Game || !g_Game->GetView())
87 0 : return JS::UndefinedValue();
88 :
89 0 : const CVector3D rotation = g_Game->GetView()->GetCameraRotation();
90 0 : JS::RootedValue val(rq.cx);
91 0 : Script::CreateObject(rq, &val, "x", rotation.X, "y", rotation.Y);
92 0 : return val;
93 0 : }
94 :
95 0 : JS::Value GetCameraZoom()
96 : {
97 0 : if (!g_Game || !g_Game->GetView())
98 0 : return JS::UndefinedValue();
99 0 : return JS::NumberValue(g_Game->GetView()->GetCameraZoom());
100 : }
101 :
102 0 : JS::Value GetCameraPivot(const Script::Request& rq)
103 : {
104 0 : if (!g_Game || !g_Game->GetView())
105 0 : return JS::UndefinedValue();
106 :
107 0 : const CVector3D pivot = g_Game->GetView()->GetCameraPivot();
108 0 : JS::RootedValue pivotValue(rq.cx);
109 0 : Script::CreateObject(rq, &pivotValue, "x", pivot.X, "z", pivot.Z);
110 0 : return pivotValue;
111 0 : }
112 :
113 0 : JS::Value GetCameraPosition(const Script::Request& rq)
114 : {
115 0 : if (!g_Game || !g_Game->GetView())
116 0 : return JS::UndefinedValue();
117 :
118 0 : const CVector3D position = g_Game->GetView()->GetCameraPosition();
119 0 : JS::RootedValue positionValue(rq.cx);
120 0 : Script::CreateObject(rq, &positionValue, "x", position.X, "y", position.Y, "z", position.Z);
121 0 : return positionValue;
122 0 : }
123 :
124 : /**
125 : * Reload camera configuration
126 : */
127 0 : void LoadCameraConfig()
128 : {
129 0 : if (!g_Game || !g_Game->GetView())
130 : return;
131 0 : g_Game->GetView()->LoadCameraConfig();
132 : }
133 :
134 : /**
135 : * Move camera to a 2D location.
136 : */
137 0 : void CameraMoveTo(entity_pos_t x, entity_pos_t z)
138 : {
139 0 : if (!g_Game || !g_Game->GetWorld() || !g_Game->GetView())
140 0 : return;
141 :
142 0 : const CTerrain& terrain = g_Game->GetWorld()->GetTerrain();
143 :
144 0 : CVector3D target;
145 0 : target.X = x.ToFloat();
146 0 : target.Z = z.ToFloat();
147 0 : target.Y = terrain.GetExactGroundLevel(target.X, target.Z);
148 :
149 0 : g_Game->GetView()->MoveCameraTarget(target);
150 : }
151 :
152 : /**
153 : * Set the camera to look at the given location.
154 : */
155 0 : void SetCameraTarget(float x, float y, float z)
156 : {
157 0 : if (!g_Game || !g_Game->GetView())
158 : return;
159 0 : g_Game->GetView()->MoveCameraTarget(CVector3D(x, y, z));
160 : }
161 :
162 : /**
163 : * Set the data (position, orientation and zoom) of the camera.
164 : */
165 0 : void SetCameraData(entity_pos_t x, entity_pos_t y, entity_pos_t z, entity_pos_t rotx, entity_pos_t roty, entity_pos_t zoom)
166 : {
167 0 : if (!g_Game || !g_Game->GetView())
168 0 : return;
169 :
170 0 : CVector3D pos(x.ToFloat(), y.ToFloat(), z.ToFloat());
171 :
172 0 : g_Game->GetView()->SetCamera(pos, rotx.ToFloat(), roty.ToFloat(), zoom.ToFloat());
173 : }
174 :
175 : /**
176 : * Start / stop camera following mode.
177 : * @param entityid unit id to follow. If zero, stop following mode
178 : */
179 0 : void CameraFollow(entity_id_t entityid)
180 : {
181 0 : if (!g_Game || !g_Game->GetView())
182 : return;
183 :
184 0 : g_Game->GetView()->FollowEntity(entityid, false);
185 : }
186 :
187 : /**
188 : * Start / stop first-person camera following mode.
189 : * @param entityid unit id to follow. If zero, stop following mode.
190 : */
191 0 : void CameraFollowFPS(entity_id_t entityid)
192 : {
193 0 : if (!g_Game || !g_Game->GetView())
194 : return;
195 :
196 0 : g_Game->GetView()->FollowEntity(entityid, true);
197 : }
198 :
199 0 : entity_id_t GetFollowedEntity()
200 : {
201 0 : if (!g_Game || !g_Game->GetView())
202 : return INVALID_ENTITY;
203 :
204 0 : return g_Game->GetView()->GetFollowedEntity();
205 : }
206 :
207 0 : CFixedVector3D GetTerrainAtScreenPoint(int x, int y)
208 : {
209 0 : CVector3D pos = g_Game->GetView()->GetCamera().GetWorldCoordinates(x, y, true);
210 0 : return CFixedVector3D(fixed::FromFloat(pos.X), fixed::FromFloat(pos.Y), fixed::FromFloat(pos.Z));
211 : }
212 :
213 26 : void RegisterScriptFunctions(const Script::Request& rq)
214 : {
215 26 : RegisterScriptFunctions_Settings(rq);
216 :
217 26 : Script::Function::Register<&GetCameraRotation>(rq, "GetCameraRotation");
218 26 : Script::Function::Register<&GetCameraZoom>(rq, "GetCameraZoom");
219 26 : Script::Function::Register<&GetCameraPivot>(rq, "GetCameraPivot");
220 26 : Script::Function::Register<&GetCameraPosition>(rq, "GetCameraPosition");
221 26 : Script::Function::Register<&CameraMoveTo>(rq, "CameraMoveTo");
222 26 : Script::Function::Register<&LoadCameraConfig>(rq, "LoadCameraConfig");
223 26 : Script::Function::Register<&SetCameraTarget>(rq, "SetCameraTarget");
224 26 : Script::Function::Register<&SetCameraData>(rq, "SetCameraData");
225 26 : Script::Function::Register<&CameraFollow>(rq, "CameraFollow");
226 26 : Script::Function::Register<&CameraFollowFPS>(rq, "CameraFollowFPS");
227 26 : Script::Function::Register<&GetFollowedEntity>(rq, "GetFollowedEntity");
228 26 : Script::Function::Register<&GetTerrainAtScreenPoint>(rq, "GetTerrainAtScreenPoint");
229 26 : }
230 : }
|