1/**
2 * Copyright (c) 2006-2023 LOVE Development Team
3 *
4 * This software is provided 'as-is', without any express or implied
5 * warranty. In no event will the authors be held liable for any damages
6 * arising from the use of this software.
7 *
8 * Permission is granted to anyone to use this software for any purpose,
9 * including commercial applications, and to alter it and redistribute it
10 * freely, subject to the following restrictions:
11 *
12 * 1. The origin of this software must not be misrepresented; you must not
13 * claim that you wrote the original software. If you use this software
14 * in a product, an acknowledgment in the product documentation would be
15 * appreciated but is not required.
16 * 2. Altered source versions must be plainly marked as such, and must not be
17 * misrepresented as being the original software.
18 * 3. This notice may not be removed or altered from any source distribution.
19 **/
20
21#include "wrap_World.h"
22
23namespace love
24{
25namespace physics
26{
27namespace box2d
28{
29
30World *luax_checkworld(lua_State *L, int idx)
31{
32 World *w = luax_checktype<World>(L, idx);
33 if (!w->isValid())
34 luaL_error(L, "Attempt to use destroyed world.");
35 return w;
36}
37
38int w_World_update(lua_State *L)
39{
40 World *t = luax_checkworld(L, 1);
41 float dt = (float)luaL_checknumber(L, 2);
42
43 // Make sure the world callbacks are using the calling Lua thread.
44 t->setCallbacksL(L);
45
46 if (lua_isnoneornil(L, 3))
47 luax_catchexcept(L, [&](){ t->update(dt); });
48 else
49 {
50 int velocityiterations = (int) luaL_checkinteger(L, 3);
51 int positioniterations = (int) luaL_checkinteger(L, 4);
52 luax_catchexcept(L, [&](){ t->update(dt, velocityiterations, positioniterations); });
53 }
54
55 return 0;
56}
57
58int w_World_setCallbacks(lua_State *L)
59{
60 World *t = luax_checkworld(L, 1);
61 lua_remove(L, 1);
62 return t->setCallbacks(L);
63}
64
65int w_World_getCallbacks(lua_State *L)
66{
67 World *t = luax_checkworld(L, 1);
68 lua_remove(L, 1);
69 return t->getCallbacks(L);
70}
71
72int w_World_setContactFilter(lua_State *L)
73{
74 World *t = luax_checkworld(L, 1);
75 lua_remove(L, 1);
76 return t->setContactFilter(L);
77}
78
79int w_World_getContactFilter(lua_State *L)
80{
81 World *t = luax_checkworld(L, 1);
82 lua_remove(L, 1);
83 return t->getContactFilter(L);
84}
85
86int w_World_setGravity(lua_State *L)
87{
88 World *t = luax_checkworld(L, 1);
89 float arg1 = (float)luaL_checknumber(L, 2);
90 float arg2 = (float)luaL_checknumber(L, 3);
91 t->setGravity(arg1, arg2);
92 return 0;
93}
94
95int w_World_getGravity(lua_State *L)
96{
97 World *t = luax_checkworld(L, 1);
98 lua_remove(L, 1);
99 return t->getGravity(L);
100}
101
102int w_World_translateOrigin(lua_State *L)
103{
104 World *t = luax_checkworld(L, 1);
105 float arg1 = (float)luaL_checknumber(L, 2);
106 float arg2 = (float)luaL_checknumber(L, 3);
107 luax_catchexcept(L, [&](){ t->translateOrigin(arg1, arg2); });
108 return 0;
109}
110
111int w_World_setSleepingAllowed(lua_State *L)
112{
113 World *t = luax_checkworld(L, 1);
114 bool b = luax_checkboolean(L, 2);
115 t->setSleepingAllowed(b);
116 return 0;
117}
118
119int w_World_isSleepingAllowed(lua_State *L)
120{
121 World *t = luax_checkworld(L, 1);
122 luax_pushboolean(L, t->isSleepingAllowed());
123 return 1;
124}
125
126int w_World_isLocked(lua_State *L)
127{
128 World *t = luax_checkworld(L, 1);
129 luax_pushboolean(L, t->isLocked());
130 return 1;
131}
132
133int w_World_getBodyCount(lua_State *L)
134{
135 World *t = luax_checkworld(L, 1);
136 lua_pushinteger(L, t->getBodyCount());
137 return 1;
138}
139
140int w_World_getJointCount(lua_State *L)
141{
142 World *t = luax_checkworld(L, 1);
143 lua_pushinteger(L, t->getJointCount());
144 return 1;
145}
146
147int w_World_getContactCount(lua_State *L)
148{
149 World *t = luax_checkworld(L, 1);
150 lua_pushinteger(L, t->getContactCount());
151 return 1;
152}
153
154int w_World_getBodies(lua_State *L)
155{
156 World *t = luax_checkworld(L, 1);
157 lua_remove(L, 1);
158 int ret = 0;
159 luax_catchexcept(L, [&](){ ret = t->getBodies(L); });
160 return ret;
161}
162
163int w_World_getJoints(lua_State *L)
164{
165 World *t = luax_checkworld(L, 1);
166 lua_remove(L, 1);
167 int ret = 0;
168 luax_catchexcept(L, [&](){ ret = t->getJoints(L); });
169 return ret;
170}
171
172int w_World_getContacts(lua_State *L)
173{
174 World *t = luax_checkworld(L, 1);
175 lua_remove(L, 1);
176 int ret = 0;
177 luax_catchexcept(L, [&](){ ret = t->getContacts(L); });
178 return ret;
179}
180
181int w_World_queryBoundingBox(lua_State *L)
182{
183 World *t = luax_checkworld(L, 1);
184 lua_remove(L, 1);
185 return t->queryBoundingBox(L);
186}
187
188int w_World_rayCast(lua_State *L)
189{
190 World *t = luax_checkworld(L, 1);
191 lua_remove(L, 1);
192 int ret = 0;
193 luax_catchexcept(L, [&](){ ret = t->rayCast(L); });
194 return ret;
195}
196
197int w_World_destroy(lua_State *L)
198{
199 World *t = luax_checkworld(L, 1);
200 luax_catchexcept(L, [&](){ t->destroy(); });
201 return 0;
202}
203
204int w_World_isDestroyed(lua_State *L)
205{
206 World *w = luax_checktype<World>(L, 1);
207 luax_pushboolean(L, !w->isValid());
208 return 1;
209}
210
211int w_World_getBodyList(lua_State *L)
212{
213 luax_markdeprecated(L, "World:getBodyList", API_METHOD, DEPRECATED_RENAMED, "World:getBodies");
214 return w_World_getBodies(L);
215}
216
217int w_World_getJointList(lua_State *L)
218{
219 luax_markdeprecated(L, "World:getJointList", API_METHOD, DEPRECATED_RENAMED, "World:getJoints");
220 return w_World_getJoints(L);
221}
222
223int w_World_getContactList(lua_State *L)
224{
225 luax_markdeprecated(L, "World:getContactList", API_METHOD, DEPRECATED_RENAMED, "World:getContacts");
226 return w_World_getContacts(L);
227}
228
229static const luaL_Reg w_World_functions[] =
230{
231 { "update", w_World_update },
232 { "setCallbacks", w_World_setCallbacks },
233 { "getCallbacks", w_World_getCallbacks },
234 { "setContactFilter", w_World_setContactFilter },
235 { "getContactFilter", w_World_getContactFilter },
236 { "setGravity", w_World_setGravity },
237 { "getGravity", w_World_getGravity },
238 { "translateOrigin", w_World_translateOrigin },
239 { "setSleepingAllowed", w_World_setSleepingAllowed },
240 { "isSleepingAllowed", w_World_isSleepingAllowed },
241 { "isLocked", w_World_isLocked },
242 { "getBodyCount", w_World_getBodyCount },
243 { "getJointCount", w_World_getJointCount },
244 { "getContactCount", w_World_getContactCount },
245 { "getBodies", w_World_getBodies },
246 { "getJoints", w_World_getJoints },
247 { "getContacts", w_World_getContacts },
248 { "queryBoundingBox", w_World_queryBoundingBox },
249 { "rayCast", w_World_rayCast },
250 { "destroy", w_World_destroy },
251 { "isDestroyed", w_World_isDestroyed },
252
253 // Deprecated
254 { "getBodyList", w_World_getBodyList },
255 { "getJointList", w_World_getJointList },
256 { "getContactList", w_World_getContactList },
257
258 { 0, 0 }
259};
260
261extern "C" int luaopen_world(lua_State *L)
262{
263 return luax_register_type(L, &World::type, w_World_functions, nullptr);
264}
265
266} // box2d
267} // physics
268} // love
269