-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrbxmathlib.cpp
300 lines (238 loc) · 7.12 KB
/
rbxmathlib.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
#include "luau_ext.h"
#include <string.h>
#define HANDMADE_MATH_NO_SSE
#include "HandmadeMath.h"
#include "Luau/Common.h"
#include "lualib.h"
static int vector2_new(lua_State* L);
static int vector2_add(lua_State* L) {
hmm_vec2 t1 = { 0 };
hmm_vec2 t2 = { 0 };
LUAU_ASSERT(lua_rawgetfield(L, 1, "X") == LUA_TNUMBER);
t1.X = lua_tonumber(L, -1);
lua_pop(L, 1);
LUAU_ASSERT(lua_rawgetfield(L, 1, "Y") == LUA_TNUMBER);
t1.Y = lua_tonumber(L, -1);
lua_pop(L, 1);
LUAU_ASSERT(lua_rawgetfield(L, 2, "X") == LUA_TNUMBER);
t2.X = lua_tonumber(L, -1);
lua_pop(L, 1);
LUAU_ASSERT(lua_rawgetfield(L, 2, "Y") == LUA_TNUMBER);
t2.Y = lua_tonumber(L, -1);
lua_pop(L, 1);
hmm_vec2 out = HMM_AddVec2(t1, t2);
lua_settop(L, 0);
vector2_new(L);
lua_pushstring(L, "X");
lua_pushnumber(L, out.X);
lua_rawset(L, -3);
lua_pushstring(L, "Y");
lua_pushnumber(L, out.Y);
lua_rawset(L, -3);
return 1;
}
static int vector2_mul(lua_State* L) {
hmm_vec2 t1 = { 0 };
hmm_vec2 t2 = { 0 };
LUAU_ASSERT(lua_rawgetfield(L, 1, "X") == LUA_TNUMBER);
t1.X = lua_tonumber(L, -1);
lua_pop(L, 1);
LUAU_ASSERT(lua_rawgetfield(L, 1, "Y") == LUA_TNUMBER);
t1.Y = lua_tonumber(L, -1);
lua_pop(L, 1);
LUAU_ASSERT(lua_rawgetfield(L, 2, "X") == LUA_TNUMBER);
t2.X = lua_tonumber(L, -1);
lua_pop(L, 1);
LUAU_ASSERT(lua_rawgetfield(L, 2, "Y") == LUA_TNUMBER);
t2.Y = lua_tonumber(L, -1);
lua_pop(L, 1);
hmm_vec2 out = HMM_MultiplyVec2(t1, t2);
lua_settop(L, 0);
vector2_new(L);
lua_pushstring(L, "X");
lua_pushnumber(L, out.X);
lua_rawset(L, -3);
lua_pushstring(L, "Y");
lua_pushnumber(L, out.Y);
lua_rawset(L, -3);
return 1;
}
static int vector2_div(lua_State* L) {
hmm_vec2 t1 = { 0 };
hmm_vec2 t2 = { 0 };
LUAU_ASSERT(lua_rawgetfield(L, 1, "X") == LUA_TNUMBER);
t1.X = lua_tonumber(L, -1);
lua_pop(L, 1);
LUAU_ASSERT(lua_rawgetfield(L, 1, "Y") == LUA_TNUMBER);
t1.Y = lua_tonumber(L, -1);
lua_pop(L, 1);
LUAU_ASSERT(lua_rawgetfield(L, 2, "X") == LUA_TNUMBER);
t2.X = lua_tonumber(L, -1);
lua_pop(L, 1);
LUAU_ASSERT(lua_rawgetfield(L, 2, "Y") == LUA_TNUMBER);
t2.Y = lua_tonumber(L, -1);
lua_pop(L, 1);
hmm_vec2 out = HMM_DivideVec2(t1, t2);
lua_settop(L, 0);
vector2_new(L);
lua_pushstring(L, "X");
lua_pushnumber(L, out.X);
lua_rawset(L, -3);
lua_pushstring(L, "Y");
lua_pushnumber(L, out.Y);
lua_rawset(L, -3);
return 1;
}
static int vector2_sub(lua_State* L) {
hmm_vec2 t1 = { 0 };
hmm_vec2 t2 = { 0 };
LUAU_ASSERT(lua_rawgetfield(L, 1, "X") == LUA_TNUMBER);
t1.X = lua_tonumber(L, -1);
lua_pop(L, 1);
LUAU_ASSERT(lua_rawgetfield(L, 1, "Y") == LUA_TNUMBER);
t1.Y = lua_tonumber(L, -1);
lua_pop(L, 1);
LUAU_ASSERT(lua_rawgetfield(L, 2, "X") == LUA_TNUMBER);
t2.X = lua_tonumber(L, -1);
lua_pop(L, 1);
LUAU_ASSERT(lua_rawgetfield(L, 2, "Y") == LUA_TNUMBER);
t2.Y = lua_tonumber(L, -1);
lua_pop(L, 1);
hmm_vec2 out = HMM_SubtractVec2(t1, t2);
lua_settop(L, 0);
vector2_new(L);
lua_pushstring(L, "X");
lua_pushnumber(L, out.X);
lua_rawset(L, -3);
lua_pushstring(L, "Y");
lua_pushnumber(L, out.Y);
lua_rawset(L, -3);
return 1;
}
static int vector2_neg(lua_State* L) {
hmm_vec2 t1 = { 0 };
LUAU_ASSERT(lua_rawgetfield(L, 1, "X") == LUA_TNUMBER);
t1.X = lua_tonumber(L, -1);
lua_pop(L, 1);
LUAU_ASSERT(lua_rawgetfield(L, 1, "Y") == LUA_TNUMBER);
t1.Y = lua_tonumber(L, -1);
lua_pop(L, 1);
lua_settop(L, 0);
vector2_new(L);
lua_pushstring(L, "X");
lua_pushnumber(L, -t1.X);
lua_rawset(L, -3);
lua_pushstring(L, "Y");
lua_pushnumber(L, -t1.Y);
lua_rawset(L, -3);
return 1;
}
static int vector2_cross(lua_State* L) {
int n = lua_gettop(L); /* number of arguments */
if (n == 1) {
luaL_error(L, "Call function Cross with function notation i.e. ':' not '.' ");
lua_pushnumber(L, 0);
return 1;
}
hmm_vec3 t1 = { 0 };
hmm_vec3 t2 = { 0 };
LUAU_ASSERT(lua_rawgetfield(L, -2, "X") == LUA_TNUMBER);
t1.X = lua_tonumber(L, -1);
lua_pop(L, 1);
LUAU_ASSERT(lua_rawgetfield(L, -2, "Y") == LUA_TNUMBER);
t1.Y = lua_tonumber(L, -1);
lua_pop(L, 1);
LUAU_ASSERT(lua_rawgetfield(L, -1, "X") == LUA_TNUMBER);
t2.X = lua_tonumber(L, -1);
lua_pop(L, 1);
LUAU_ASSERT(lua_rawgetfield(L, -1, "Y") == LUA_TNUMBER);
t2.Y = lua_tonumber(L, -1);
lua_pop(L, 1);
double out = HMM_Cross(t1,t2).Z;
lua_pushnumber(L, out);
return 1;
}
static int vector2_dot(lua_State* L) {
int n = lua_gettop(L); /* number of arguments */
if (n == 1) {
luaL_error(L, "Call function Dot with function notation i.e. ':' not '.' ");
lua_pushnumber(L, 0);
return 1;
}
hmm_vec2 t1 = { 0 };
hmm_vec2 t2 = { 0 };
LUAU_ASSERT(lua_rawgetfield(L, -2, "X") == LUA_TNUMBER);
t1.X = lua_tonumber(L, -1);
lua_pop(L, 1);
LUAU_ASSERT(lua_rawgetfield(L, -2, "Y") == LUA_TNUMBER);
t1.Y = lua_tonumber(L, -1);
lua_pop(L, 1);
LUAU_ASSERT(lua_rawgetfield(L, -1, "X") == LUA_TNUMBER);
t2.X = lua_tonumber(L, -1);
lua_pop(L, 1);
LUAU_ASSERT(lua_rawgetfield(L, -1, "Y") == LUA_TNUMBER);
t2.Y = lua_tonumber(L, -1);
lua_pop(L, 1);
double out = HMM_DotVec2(t1, t2);
lua_pushnumber(L, out);
return 1;
}
static int vector2_new(lua_State* L) {
int n = lua_gettop(L); /* number of arguments */
lua_newtable(L);
lua_pushstring(L, "X");
double tempX = n >= 1 ? lua_tonumber(L, 1) : 0;
lua_pushnumber(L, n >= 1 ? lua_tonumber(L, 1) : 0);
lua_settable(L, -3);
lua_pushstring(L, "Y");
double tempY = n == 2 ? (float)lua_tonumber(L, 2) : 0;
lua_pushnumber(L, tempY);
lua_settable(L, -3);
lua_pushcfunction(L, vector2_cross, "vec2Cross");
lua_setfield(L, -2, "Cross");
lua_pushcfunction(L, vector2_dot, "vec2Dot");
lua_setfield(L, -2, "Dot");
luaL_getmetatable(L, "Vector2Meta");
LUAU_ASSERT(lua_istable(L, -1));
lua_setmetatable(L, -2);
return 1;
}
/* Vector2
* Magnitude: number
Unit: Vector2
X: number
Y: number
function Cross(self, other: Vector2): number
function Dot(self, v: Vector2): number
function Lerp(self, v: Vector2, alpha: number): Vector2
function __add(self, other: Vector2): Vector2
function __div(self, other: Vector2 | number): Vector2
function __mul(self, other: Vector2 | number): Vector2
function __sub(self, other: Vector2): Vector2
function __unm(self): Vector2
*/
LUALIB_API int (luaopen_rblx_math)(lua_State* L) {
lua_newtable(L);
int vec2Idx = lua_gettop(L);
lua_pushvalue(L, vec2Idx);
lua_setglobal(L, "Vector2");
lua_pushcfunction(L, vector2_new,"vec2New");
lua_setfield(L, -2, "new");
luaL_newmetatable(L, "Vector2Meta");
lua_pushstring(L, "__add");
lua_pushcfunction(L, vector2_add, "vec2Add");
lua_settable(L, -3);
lua_pushstring(L, "__sub");
lua_pushcfunction(L, vector2_sub, "vec2Sub");
lua_settable(L, -3);
lua_pushstring(L, "__mul");
lua_pushcfunction(L, vector2_mul, "vec2Mul");
lua_settable(L, -3);
lua_pushstring(L, "__div");
lua_pushcfunction(L, vector2_div, "vec2Div");
lua_settable(L, -3);
lua_pushstring(L, "__unm");
lua_pushcfunction(L, vector2_neg, "vec2ToNeg");
lua_settable(L, -3);
return 1;
}