1 #include <bz/scripting/bindings_internal.h>
3 #include <bz/game/scene_internal.h>
4 #include <bz/game/tilemap.h>
5 #include <bz/gfx/drawing.h>
6 //#include <bz/gfx/gfx.h>
7 #include <bz/gfx/tilemap.h>
8 #include <bz/input/input.h>
9 #include <bz/math/math.h>
10 #include <bz/math/random.h>
12 #include <stb_sprintf.h>
14 #include "static_crc32.hpp"
16 #define _FUNCTION_PARAMETER(idx,t) stack[idx].t##Literal
17 #define _FUNCTION_BINDING(n, f, ...) static void native_##n(SVMModuleInstance *instance, int32_t parameterCount, SVMOperand stack[]) { BZScriptBindingMetadata *metadata = (BZScriptBindingMetadata *)svmGetModuleInstanceUserParameter(instance); f( __VA_ARGS__ ); }
18 #define _FUNCTION_BINDING_RTN(n, f, ...) static void native_##n(SVMModuleInstance *instance, int32_t parameterCount, SVMOperand stack[]) { BZScriptBindingMetadata *metadata = (BZScriptBindingMetadata *)svmGetModuleInstanceUserParameter(instance); stack[0].__raw = (int32_t)(f( __VA_ARGS__ )); }
19 #define _FUNCTION_BINDING_RTN_WRAP(n, f, ...) static void native_##n(SVMModuleInstance *instance, int32_t parameterCount, SVMOperand stack[]) { BZScriptBindingMetadata *metadata = (BZScriptBindingMetadata *)svmGetModuleInstanceUserParameter(instance); stack[0].__raw = FIXED(f( __VA_ARGS__ )); }
20 #define _FUNCTION_BINDING_RTN_WRAP_FLOAT(n, f, ...) static void native_##n(SVMModuleInstance *instance, int32_t parameterCount, SVMOperand stack[]) { BZScriptBindingMetadata *metadata = (BZScriptBindingMetadata *)svmGetModuleInstanceUserParameter(instance); stack[0].__raw = FIXED_FROM_FLOAT(f( __VA_ARGS__ )); }
22 #define _CONTEXT_PARAMETER(name) (metadata->name)
24 #define _RAW_PARAMETER(idx) stack[idx].__raw
25 #define _INT_PARAMETER(idx) FIXED_TO_INT(_FUNCTION_PARAMETER(idx, integer))
26 #define _FLOAT_PARAMETER(idx) FIXED_TO_FLOAT(_FUNCTION_PARAMETER(idx, integer))
27 #define _STRING_PARAMETER(idx) svmGetString(instance, _FUNCTION_PARAMETER(idx, pointer))
29 #define FUNCTION_BIND(n) case COMPILE_TIME_CRC32_STR(#n): return native_##n;
31 static void calculateTransformMatrix(BZScriptBindingMetadata
*metadata
) {
32 bzMatrixSRT(&metadata
->matrix
, metadata
->position
.x
, metadata
->position
.y
, metadata
->angle
, metadata
->scale
.x
, metadata
->scale
.y
);
35 static void resetTransform(BZScriptBindingMetadata
*metadata
) {
36 bzVectorSet(&metadata
->position
, 0, 0);
37 metadata
->angle
= 0.0f
;
38 bzVectorSet(&metadata
->scale
, 1, 1);
39 //bzMatrixIdentity(&metadata->matrix);
40 calculateTransformMatrix(metadata
);
43 void bzScriptingInitialiseMetadata(BZScriptBindingMetadata
*metadata
, BZActorID actor
, BZSceneID scene
, uint32_t uuid
) {
44 *metadata
= (BZScriptBindingMetadata
) {
51 resetTransform(metadata
);
54 _FUNCTION_BINDING(log
, bzLog
, "%s", svmGetString(instance
, stack
[1].pointerLiteral
))
56 static void native_logf(SVMModuleInstance
*instance
, int32_t parameterCount
, SVMOperand stack
[]) {
57 BZScriptBindingMetadata
*metadata
= (BZScriptBindingMetadata
*)svmGetModuleInstanceUserParameter(instance
);
58 const char *formatString
= _STRING_PARAMETER(1);
60 char outputString
[256]; // fixme
61 char *o
= outputString
;
65 for (const char *f
= formatString
; *f
!= '\0'; ++f
) {
68 o
+= stbsp_sprintf(o
, "%s", _STRING_PARAMETER(paramIdx
++));
72 o
+= stbsp_sprintf(o
, "%d", _INT_PARAMETER(paramIdx
++));
77 bzLog("%s", outputString
);
80 //_FUNCTION_BINDING(logNum, bzLog, "%f", FIXED_TO_FLOAT(stack[1].floatLiteral))
82 _FUNCTION_BINDING_RTN(sqrt
, fixedSqrt
, _FUNCTION_PARAMETER(1, float))
83 _FUNCTION_BINDING_RTN(sin
, fixedSin
, _FUNCTION_PARAMETER(1, float))
84 _FUNCTION_BINDING_RTN(cos
, fixedCos
, _FUNCTION_PARAMETER(1, float))
85 _FUNCTION_BINDING_RTN(atan2
, fixedAtan2
, _FUNCTION_PARAMETER(1, float), _FUNCTION_PARAMETER(2, float))
86 _FUNCTION_BINDING_RTN_WRAP_FLOAT(adelta
, bzAngleDelta
, _FLOAT_PARAMETER(1), _FLOAT_PARAMETER(2))
88 _FUNCTION_BINDING_RTN_WRAP_FLOAT(distance
, bzDistance
, _FLOAT_PARAMETER(1), _FLOAT_PARAMETER(2), _FLOAT_PARAMETER(3), _FLOAT_PARAMETER(4))
89 _FUNCTION_BINDING_RTN(distanceCheck
, bzDistanceCheck
, _FLOAT_PARAMETER(1), _FLOAT_PARAMETER(2), _FLOAT_PARAMETER(3), _FLOAT_PARAMETER(4), _FLOAT_PARAMETER(5))
91 _FUNCTION_BINDING_RTN(min
, fixedMin
, _FUNCTION_PARAMETER(1, float), _FUNCTION_PARAMETER(2, float))
92 _FUNCTION_BINDING_RTN(max
, fixedMax
, _FUNCTION_PARAMETER(1, float), _FUNCTION_PARAMETER(2, float))
93 _FUNCTION_BINDING_RTN(mid
, fixedMid
, _FUNCTION_PARAMETER(1, float), _FUNCTION_PARAMETER(2, float), _FUNCTION_PARAMETER(3, float))
94 _FUNCTION_BINDING_RTN(sgn
, fixedSgn
, _FUNCTION_PARAMETER(1, float))
95 _FUNCTION_BINDING_RTN(abs
, fixedAbs
, _FUNCTION_PARAMETER(1, float))
96 _FUNCTION_BINDING_RTN(floor
, fixedFloor
, _FUNCTION_PARAMETER(1, float))
97 _FUNCTION_BINDING_RTN(ceil
, fixedCeil
, _FUNCTION_PARAMETER(1, float))
99 static void native_rnd(SVMModuleInstance
*instance
, int32_t parameterCount
, SVMOperand stack
[]) {
100 switch (parameterCount
) {
102 stack
[0].__raw
= FIXED_FROM_FLOAT(bzRandomFloat(1));
106 stack
[0].__raw
= FIXED_FROM_FLOAT(bzRandomFloat(_FLOAT_PARAMETER(1)));
110 stack
[0].__raw
= FIXED_FROM_FLOAT(bzRandomFloatRange(_FLOAT_PARAMETER(1), _FLOAT_PARAMETER(2)));
115 _FUNCTION_BINDING(srand
, bzRandomReseed
, _INT_PARAMETER(1))
117 _FUNCTION_BINDING_RTN(lerp
, fixedLerp
, _FUNCTION_PARAMETER(1, float), _FUNCTION_PARAMETER(2, float), _FUNCTION_PARAMETER(3, float))
119 static void native_dot(SVMModuleInstance
*instance
, int32_t parameterCount
, SVMOperand stack
[]) {
120 BZVector v1
= bzVectorMake(_FLOAT_PARAMETER(1), _FLOAT_PARAMETER(2));
121 BZVector v2
= bzVectorMake(_FLOAT_PARAMETER(3), _FLOAT_PARAMETER(4));
122 float dot
= bzVectorDot(&v1
, &v2
);
123 stack
[0].floatLiteral
= FIXED_FROM_FLOAT(dot
);
126 _FUNCTION_BINDING_RTN_WRAP_FLOAT(time
, bzGameSceneGetTime
, _CONTEXT_PARAMETER(scene
))
128 //FUNCTION_BINDING_P4(clip, bzClip, integer, integer, integer, integer)
129 ////FUNCTION_BINDING_P3(pset, bzPSet, integer, integer, integer)
130 ////FUNCTION_BINDING_RTN_P2(sget, bzSGet, integer, integer)
132 _FUNCTION_BINDING(cls
, bzDrawCls
, _CONTEXT_PARAMETER(drawQueue
), _CONTEXT_PARAMETER(sortIdx
), &_CONTEXT_PARAMETER(matrix
), _INT_PARAMETER(1))//FUNCTION_BINDING_P1(cls, bzDrawCls, integer)
133 _FUNCTION_BINDING(camera
, bzDrawCamera
, _CONTEXT_PARAMETER(drawQueue
), _CONTEXT_PARAMETER(sortIdx
), &_CONTEXT_PARAMETER(matrix
), _INT_PARAMETER(1), _INT_PARAMETER(2))//FUNCTION_BINDING_P2(camera, bzDrawCamera, integer, integer)
134 _FUNCTION_BINDING(circ
, bzDrawCirc
, _CONTEXT_PARAMETER(drawQueue
), _CONTEXT_PARAMETER(sortIdx
), &_CONTEXT_PARAMETER(matrix
), _INT_PARAMETER(1), _INT_PARAMETER(2), _INT_PARAMETER(3), _INT_PARAMETER(4))//FUNCTION_BINDING_P4(circ, bzDrawCirc, integer, integer, integer, integer)
135 _FUNCTION_BINDING(circfill
, bzDrawCircFill
, _CONTEXT_PARAMETER(drawQueue
), _CONTEXT_PARAMETER(sortIdx
), &_CONTEXT_PARAMETER(matrix
), _INT_PARAMETER(1), _INT_PARAMETER(2), _INT_PARAMETER(3), _INT_PARAMETER(4))//FUNCTION_BINDING_P4(circfill, bzDrawCircFill, integer, integer, integer, integer)
136 _FUNCTION_BINDING(line
, bzDrawLine
, _CONTEXT_PARAMETER(drawQueue
), _CONTEXT_PARAMETER(sortIdx
), &_CONTEXT_PARAMETER(matrix
), _INT_PARAMETER(1), _INT_PARAMETER(2), _INT_PARAMETER(3), _INT_PARAMETER(4), _INT_PARAMETER(5))//FUNCTION_BINDING_P5(line, bzDrawLine, integer, integer, integer, integer, integer)
137 _FUNCTION_BINDING(rect
, bzDrawRect
, _CONTEXT_PARAMETER(drawQueue
), _CONTEXT_PARAMETER(sortIdx
), &_CONTEXT_PARAMETER(matrix
), _INT_PARAMETER(1), _INT_PARAMETER(2), _INT_PARAMETER(3), _INT_PARAMETER(4), _INT_PARAMETER(5))//FUNCTION_BINDING_P5(rect, bzDrawRect, integer, integer, integer, integer, integer)
138 _FUNCTION_BINDING(rectfill
, bzDrawRectFill
, _CONTEXT_PARAMETER(drawQueue
), _CONTEXT_PARAMETER(sortIdx
), &_CONTEXT_PARAMETER(matrix
), _INT_PARAMETER(1), _INT_PARAMETER(2), _INT_PARAMETER(3), _INT_PARAMETER(4), _INT_PARAMETER(5))//FUNCTION_BINDING_P5(rectfill, bzDrawRectFill, integer, integer, integer, integer, integer)
139 _FUNCTION_BINDING(triangle
, bzDrawTriangle
, _CONTEXT_PARAMETER(drawQueue
), _CONTEXT_PARAMETER(sortIdx
), &_CONTEXT_PARAMETER(matrix
), _INT_PARAMETER(1), _INT_PARAMETER(2), _INT_PARAMETER(3), _INT_PARAMETER(4), _INT_PARAMETER(5), _INT_PARAMETER(6), _INT_PARAMETER(7))
140 //_FUNCTION_BINDING(spr, bzDrawSpr, _CONTEXT_PARAMETER(drawQueue), _CONTEXT_PARAMETER(sortIdx), &_CONTEXT_PARAMETER(matrix), _INT_PARAMETER(1), _INT_PARAMETER(2), _INT_PARAMETER(3))//FUNCTION_BINDING_P3(spr, bzDrawSpr, integer, integer, integer)
142 _FUNCTION_BINDING(sspr
, bzDrawSSpr
, _CONTEXT_PARAMETER(drawQueue
), _CONTEXT_PARAMETER(sortIdx
), &_CONTEXT_PARAMETER(matrix
), _INT_PARAMETER(1), _INT_PARAMETER(2), _INT_PARAMETER(3), _INT_PARAMETER(4), _INT_PARAMETER(5), _INT_PARAMETER(6))//FUNCTION_BINDING_P6(sspr, bzDrawSSpr, integer, integer, integer, integer, integer, integer)
144 _FUNCTION_BINDING(print
, bzDrawPrint
, _CONTEXT_PARAMETER(drawQueue
), _CONTEXT_PARAMETER(sortIdx
), &_CONTEXT_PARAMETER(matrix
), _INT_PARAMETER(1), _INT_PARAMETER(2), _INT_PARAMETER(3), _STRING_PARAMETER(4))
145 _FUNCTION_BINDING(fillp
, bzDrawFillP
, _CONTEXT_PARAMETER(drawQueue
), _CONTEXT_PARAMETER(sortIdx
), &_CONTEXT_PARAMETER(matrix
), _RAW_PARAMETER(1))//FUNCTION_BINDING_P1(fillp, bzDrawFillP, integer)
146 _FUNCTION_BINDING(palette
, bzDrawSetPaletteColor
, _CONTEXT_PARAMETER(drawQueue
), _CONTEXT_PARAMETER(sortIdx
), &_CONTEXT_PARAMETER(matrix
), _INT_PARAMETER(1), _INT_PARAMETER(2), _INT_PARAMETER(3))//FUNCTION_BINDING_P3(palette, bzDrawSetPaletteColor, integer, integer, integer)
147 _FUNCTION_BINDING(output
, bzDrawSetOutputBuffer
, _CONTEXT_PARAMETER(drawQueue
), _CONTEXT_PARAMETER(sortIdx
), &_CONTEXT_PARAMETER(matrix
), _INT_PARAMETER(1)) //FUNCTION_BINDING_P1(output, bzSetOutputBuffer, integer)
149 static void native_printf(SVMModuleInstance
*instance
, int32_t parameterCount
, SVMOperand stack
[]) {
150 BZScriptBindingMetadata
*metadata
= (BZScriptBindingMetadata
*)svmGetModuleInstanceUserParameter(instance
);
151 const char *formatString
= _STRING_PARAMETER(4);
153 char outputString
[256]; // fixme
154 char *o
= outputString
;
158 for (const char *f
= formatString
; *f
!= '\0'; ++f
) {
161 o
+= stbsp_sprintf(o
, "%s", _STRING_PARAMETER(paramIdx
++));
165 o
+= stbsp_sprintf(o
, "%d", _INT_PARAMETER(paramIdx
++));
170 bzDrawPrint(_CONTEXT_PARAMETER(drawQueue
), _CONTEXT_PARAMETER(sortIdx
), &_CONTEXT_PARAMETER(matrix
), _INT_PARAMETER(1), _INT_PARAMETER(2), _INT_PARAMETER(3), outputString
);
173 static void native_spr(SVMModuleInstance
*instance
, int32_t parameterCount
, SVMOperand stack
[]) {
174 BZScriptBindingMetadata
*metadata
= (BZScriptBindingMetadata
*)svmGetModuleInstanceUserParameter(instance
);
175 int w
= (parameterCount
>= 4) ? _INT_PARAMETER(4) : 1;
176 int h
= (parameterCount
>= 5) ? _INT_PARAMETER(5) : 1;
177 bool flipX
= (parameterCount
>= 6) ? _INT_PARAMETER(6) : false;
178 bool flipY
= (parameterCount
>= 7) ? _INT_PARAMETER(7) : false;
179 bzDrawSprExt(_CONTEXT_PARAMETER(drawQueue
), _CONTEXT_PARAMETER(sortIdx
), &_CONTEXT_PARAMETER(matrix
), _INT_PARAMETER(1), _INT_PARAMETER(2), _INT_PARAMETER(3), w
, h
, flipX
, flipY
);
182 static void native_map(SVMModuleInstance
*instance
, int32_t parameterCount
, SVMOperand stack
[]) {
183 BZScriptBindingMetadata
*metadata
= (BZScriptBindingMetadata
*)svmGetModuleInstanceUserParameter(instance
);
184 BZTilemapID tilemap
= bzGameGetSceneLayerTilemap(_CONTEXT_PARAMETER(scene
), _CONTEXT_PARAMETER(layer
));
185 uint32_t mask
= (parameterCount
>= 7) ? bzGameCalculateTilemapFlagsMask(tilemap
, parameterCount
- 6, &stack
[7].pointerLiteral
) : 0;
186 bzDrawMap(_CONTEXT_PARAMETER(drawQueue
), _CONTEXT_PARAMETER(sortIdx
), &_CONTEXT_PARAMETER(matrix
), tilemap
, _INT_PARAMETER(1), _INT_PARAMETER(2), _INT_PARAMETER(3), _INT_PARAMETER(4), _INT_PARAMETER(5), _INT_PARAMETER(6), mask
);
189 _FUNCTION_BINDING_RTN_WRAP(mget
, bzGameGetTilemapTile
, bzGameGetSceneLayerTilemap(_CONTEXT_PARAMETER(scene
), _CONTEXT_PARAMETER(layer
)), _INT_PARAMETER(1), _INT_PARAMETER(2))
190 _FUNCTION_BINDING(mset
, bzGameSetTilemapTile
, bzGameGetSceneLayerTilemap(_CONTEXT_PARAMETER(scene
), _CONTEXT_PARAMETER(layer
)), _INT_PARAMETER(1), _INT_PARAMETER(2), _INT_PARAMETER(3))
191 _FUNCTION_BINDING_RTN(mfget
, bzGameGetTilemapHasFlag
, bzGameGetSceneLayerTilemap(_CONTEXT_PARAMETER(scene
), _CONTEXT_PARAMETER(layer
)), _INT_PARAMETER(1), _INT_PARAMETER(2), _RAW_PARAMETER(3))
193 static void native_particle(SVMModuleInstance
*instance
, int32_t parameterCount
, SVMOperand stack
[]) {
194 BZScriptBindingMetadata
*metadata
= (BZScriptBindingMetadata
*)svmGetModuleInstanceUserParameter(instance
);
197 bzVectorCopy(&position
, &_CONTEXT_PARAMETER(position
));
198 float angle
= _CONTEXT_PARAMETER(angle
);
200 if (parameterCount
>= 3) {
201 position
.x
= _FLOAT_PARAMETER(2);
202 position
.y
= _FLOAT_PARAMETER(3);
205 if (parameterCount
>= 4) {
206 angle
= _FLOAT_PARAMETER(4);
209 size_t layerCount
= bzGameGetSceneLayerCount(_CONTEXT_PARAMETER(scene
));
210 for (size_t i
= 0; i
< layerCount
; ++i
) {
211 BZSceneLayerID layer
= bzGameGetSceneLayerAtIndex(_CONTEXT_PARAMETER(scene
), i
);
212 BZParticleSimulationID simulation
= bzGameGetSceneLayerParticleSimulation2(layer
);
213 if (simulation
!= NULL
) {
214 bzFXSpawnParticles(simulation
, stack
[1].pointerLiteral
, &position
, angle
);
219 static void native_sound(SVMModuleInstance
*instance
, int32_t parameterCount
, SVMOperand stack
[]) {
220 BZScriptBindingMetadata
*metadata
= (BZScriptBindingMetadata
*)svmGetModuleInstanceUserParameter(instance
);
221 BZAudioPlaybackEngineID audioPlaybackEngine
= bzGameGetAudioPlaybackEngine(_CONTEXT_PARAMETER(scene
));
222 bzAudioPlaybackPostEvent(audioPlaybackEngine
, stack
[1].pointerLiteral
);
225 static void native_speech(SVMModuleInstance
*instance
, int32_t parameterCount
, SVMOperand stack
[]) {
226 BZScriptBindingMetadata
*metadata
= (BZScriptBindingMetadata
*)svmGetModuleInstanceUserParameter(instance
);
227 BZAudioPlaybackEngineID audioPlaybackEngine
= bzGameGetAudioPlaybackEngine(_CONTEXT_PARAMETER(scene
));
228 bzAudioPlaybackPostEvent(audioPlaybackEngine
, stack
[1].pointerLiteral
, _STRING_PARAMETER(2));
231 static void native_speechf(SVMModuleInstance
*instance
, int32_t parameterCount
, SVMOperand stack
[]) {
232 BZScriptBindingMetadata
*metadata
= (BZScriptBindingMetadata
*)svmGetModuleInstanceUserParameter(instance
);
233 const char *formatString
= _STRING_PARAMETER(2);
235 char outputString
[256]; // fixme
236 char *o
= outputString
;
240 for (const char *f
= formatString
; *f
!= '\0'; ++f
) {
243 o
+= stbsp_sprintf(o
, "%s", _STRING_PARAMETER(paramIdx
++));
247 o
+= stbsp_sprintf(o
, "%d", _INT_PARAMETER(paramIdx
++));
252 BZAudioPlaybackEngineID audioPlaybackEngine
= bzGameGetAudioPlaybackEngine(_CONTEXT_PARAMETER(scene
));
253 bzAudioPlaybackPostEvent(audioPlaybackEngine
, stack
[1].pointerLiteral
, outputString
);
256 static void native_collision(SVMModuleInstance
*instance
, int32_t parameterCount
, SVMOperand stack
[]) {
257 BZScriptBindingMetadata
*metadata
= (BZScriptBindingMetadata
*)svmGetModuleInstanceUserParameter(instance
);
258 BZCollisionSpaceID space
= bzGameGetSceneLayerCollisionSpace(_CONTEXT_PARAMETER(scene
), _CONTEXT_PARAMETER(layer
));
260 bzAssert(space
!= NULL
);
262 BZCollisionShape shape
;
263 BZIdentifierHash tagHash
;
264 switch (stack
[1].pointerLiteral
) {
265 case COMPILE_TIME_CRC32_STR("circle"):
267 .type
= BZCollisionShapeTypeCircle
,
268 .circleOrigin
= bzVectorMake(_FLOAT_PARAMETER(2), _FLOAT_PARAMETER(3)),
269 .circleRadius
= _FLOAT_PARAMETER(4),
271 bzMatrixTransformVector(&shape
.circleOrigin
, &shape
.circleOrigin
, &_CONTEXT_PARAMETER(matrix
));
273 tagHash
= _RAW_PARAMETER(5);
276 case COMPILE_TIME_CRC32_STR("circlePerimeter"):
278 .type
= BZCollisionShapeTypeCirclePerimeter
,
279 .circleOrigin
= bzVectorMake(_FLOAT_PARAMETER(2), _FLOAT_PARAMETER(3)),
280 .circleRadius
= _FLOAT_PARAMETER(4),
282 bzMatrixTransformVector(&shape
.circleOrigin
, &shape
.circleOrigin
, &_CONTEXT_PARAMETER(matrix
));
284 tagHash
= _RAW_PARAMETER(5);
287 case COMPILE_TIME_CRC32_STR("triangle"):
289 .type
= BZCollisionShapeTypeTriangle
,
290 .trianglePoint1
= bzVectorMake(_FLOAT_PARAMETER(2), _FLOAT_PARAMETER(3)),
291 .trianglePoint2
= bzVectorMake(_FLOAT_PARAMETER(4), _FLOAT_PARAMETER(5)),
292 .trianglePoint3
= bzVectorMake(_FLOAT_PARAMETER(6), _FLOAT_PARAMETER(7)),
294 bzMatrixTransformVector(&shape
.trianglePoint1
, &shape
.trianglePoint1
, &_CONTEXT_PARAMETER(matrix
));
295 bzMatrixTransformVector(&shape
.trianglePoint2
, &shape
.trianglePoint2
, &_CONTEXT_PARAMETER(matrix
));
296 bzMatrixTransformVector(&shape
.trianglePoint3
, &shape
.trianglePoint3
, &_CONTEXT_PARAMETER(matrix
));
297 tagHash
= _RAW_PARAMETER(8);
301 bzCollisionAddBody(space
, BZCollisionBodyTypeSolid
, &shape
, tagHash
, instance
);
304 static void native_collisionCheck(SVMModuleInstance
*instance
, int32_t parameterCount
, SVMOperand stack
[]) {
305 BZScriptBindingMetadata
*metadata
= (BZScriptBindingMetadata
*)svmGetModuleInstanceUserParameter(instance
);
306 //BZCollisionSpaceID space = bzGameGetSceneLayerCollisionSpace(_CONTEXT_PARAMETER(scene), _CONTEXT_PARAMETER(layer));
308 //bzAssert(space != NULL);
310 bool getOutput
= true; // FIXME FIXME FIXME, this should only return when we need it?
311 bool removeParticles
= true;
313 BZCollisionShape shape
;
314 BZIdentifierHash tagHash
;
315 switch (stack
[1].pointerLiteral
) {
316 case COMPILE_TIME_CRC32_STR("circle"):
318 .type
= BZCollisionShapeTypeCircle
,
319 .circleOrigin
= bzVectorMake(_FLOAT_PARAMETER(2), _FLOAT_PARAMETER(3)),
320 .circleRadius
= _FLOAT_PARAMETER(4),
322 bzMatrixTransformVector(&shape
.circleOrigin
, &shape
.circleOrigin
, &_CONTEXT_PARAMETER(matrix
));
324 tagHash
= _RAW_PARAMETER(5);
325 if (parameterCount
>= 6) removeParticles
= _INT_PARAMETER(6) != 0;
328 case COMPILE_TIME_CRC32_STR("circlePerimeter"):
330 .type
= BZCollisionShapeTypeCirclePerimeter
,
331 .circleOrigin
= bzVectorMake(_FLOAT_PARAMETER(2), _FLOAT_PARAMETER(3)),
332 .circleRadius
= _FLOAT_PARAMETER(4),
334 bzMatrixTransformVector(&shape
.circleOrigin
, &shape
.circleOrigin
, &_CONTEXT_PARAMETER(matrix
));
336 tagHash
= _RAW_PARAMETER(5);
337 if (parameterCount
>= 6) removeParticles
= _INT_PARAMETER(6) != 0;
340 case COMPILE_TIME_CRC32_STR("triangle"):
342 .type
= BZCollisionShapeTypeTriangle
,
343 .trianglePoint1
= bzVectorMake(_FLOAT_PARAMETER(2), _FLOAT_PARAMETER(3)),
344 .trianglePoint2
= bzVectorMake(_FLOAT_PARAMETER(4), _FLOAT_PARAMETER(5)),
345 .trianglePoint3
= bzVectorMake(_FLOAT_PARAMETER(6), _FLOAT_PARAMETER(7)),
347 bzMatrixTransformVector(&shape
.trianglePoint1
, &shape
.trianglePoint1
, &_CONTEXT_PARAMETER(matrix
));
348 bzMatrixTransformVector(&shape
.trianglePoint2
, &shape
.trianglePoint2
, &_CONTEXT_PARAMETER(matrix
));
349 bzMatrixTransformVector(&shape
.trianglePoint3
, &shape
.trianglePoint3
, &_CONTEXT_PARAMETER(matrix
));
350 tagHash
= _RAW_PARAMETER(8);
351 if (parameterCount
>= 9) removeParticles
= _INT_PARAMETER(9) != 0;
357 size_t layerCount
= bzGameGetSceneLayerCount(_CONTEXT_PARAMETER(scene
));
358 for (size_t i
= 0; i
< layerCount
; ++i
) {
359 BZSceneLayerID layer
= bzGameGetSceneLayerAtIndex(_CONTEXT_PARAMETER(scene
), i
);
360 BZCollisionSpaceID space
= bzGameGetSceneLayerCollisionSpace2(layer
);
364 if (getOutput
) { // FIXME FIXME FIXME
366 stack
[0].__raw
= bzCollisionResolve(&out
, &userParameter
, space
, &shape
, tagHash
);
368 // FIXME: This is terrible
369 bzScriptingSetEnvironmentPublic(_CONTEXT_PARAMETER(scene
), COMPILE_TIME_CRC32_STR("collisionX"), { .floatLiteral
= FIXED_FROM_FLOAT(out
.x
) });
370 bzScriptingSetEnvironmentPublic(_CONTEXT_PARAMETER(scene
), COMPILE_TIME_CRC32_STR("collisionY"), { .floatLiteral
= FIXED_FROM_FLOAT(out
.y
) });
372 stack
[0].__raw
= bzCollisionResolve(NULL
, &userParameter
, space
, &shape
, tagHash
);
375 if (stack
[0].__raw
) {
376 if (removeParticles
) {
377 BZParticleSimulationID particleSimulation
= bzGameGetSceneLayerParticleSimulation2(layer
);
378 if (particleSimulation
!= NULL
) {
379 bzFXDespawnParticle(particleSimulation
, (size_t)userParameter
);
388 #include <bz/game/actor_internal.h>
389 static void native_actor(SVMModuleInstance
*instance
, int32_t parameterCount
, SVMOperand stack
[]) {
390 BZScriptBindingMetadata
*metadata
= (BZScriptBindingMetadata
*)svmGetModuleInstanceUserParameter(instance
);
391 BZActorID actor
= bzGameSceneAddActor(_CONTEXT_PARAMETER(scene
), "%s", svmGetString(instance
, stack
[1].pointerLiteral
));
392 for (size_t i
= 2, j
= 1; i
<= parameterCount
; ++i
, ++j
) {
393 actor
->parameters
[j
] = stack
[i
].__raw
;
397 static void native_find(SVMModuleInstance
*instance
, int32_t parameterCount
, SVMOperand stack
[]) {
398 BZScriptBindingMetadata
*metadata
= (BZScriptBindingMetadata
*)svmGetModuleInstanceUserParameter(instance
);
399 BZActorID actor
= bzGameSceneFindActor(_CONTEXT_PARAMETER(scene
), "%s", svmGetString(instance
, stack
[1].pointerLiteral
));
400 stack
[0].pointerLiteral
= actor
->identifierHash
; // FIXME, use idx or something...
403 static void native_parameter(SVMModuleInstance
*instance
, int32_t parameterCount
, SVMOperand stack
[]) {
404 BZScriptBindingMetadata
*metadata
= (BZScriptBindingMetadata
*)svmGetModuleInstanceUserParameter(instance
);
405 stack
[0].__raw
= _CONTEXT_PARAMETER(actor
)->parameters
[FIXED_TO_INT(stack
[1].integerLiteral
)];
408 #include <bz/scripting/environment_internal.h>
409 #include <bz/scripting/script_internal.h>
410 static void native_public(SVMModuleInstance
*instance
, int32_t parameterCount
, SVMOperand stack
[]) {
411 BZScriptBindingMetadata
*metadata
= (BZScriptBindingMetadata
*)svmGetModuleInstanceUserParameter(instance
);
413 if (parameterCount
>= 2) {
414 bzScriptingSetEnvironmentPublic(_CONTEXT_PARAMETER(scene
), stack
[1].pointerLiteral
, stack
[2]);
417 if (bzScriptingGetEnvironmentPublic(&stack
[0], _CONTEXT_PARAMETER(scene
), stack
[1].pointerLiteral
) == false) {
423 static void native_btn(SVMModuleInstance
*instance
, int32_t parameterCount
, SVMOperand stack
[]) {
424 switch (stack
[1].pointerLiteral
) {
425 case COMPILE_TIME_CRC32_STR("up"):
426 stack
[0].__raw
= bzInputBtn(1) ? 1 : 0;
429 case COMPILE_TIME_CRC32_STR("down"):
430 stack
[0].__raw
= bzInputBtn(2) ? 1 : 0;
433 case COMPILE_TIME_CRC32_STR("left"):
434 stack
[0].__raw
= bzInputBtn(3) ? 1 : 0;
437 case COMPILE_TIME_CRC32_STR("right"):
438 stack
[0].__raw
= bzInputBtn(4) ? 1 : 0;
441 case COMPILE_TIME_CRC32_STR("action1"):
442 stack
[0].__raw
= bzInputBtn(5) ? 1 : 0;
445 case COMPILE_TIME_CRC32_STR("action2"):
446 stack
[0].__raw
= bzInputBtn(6) ? 1 : 0;
449 case COMPILE_TIME_CRC32_STR("alpha-up"):
450 stack
[0].__raw
= bzInputBtn(7) ? 1 : 0;
453 case COMPILE_TIME_CRC32_STR("alpha-down"):
454 stack
[0].__raw
= bzInputBtn(8) ? 1 : 0;
457 case COMPILE_TIME_CRC32_STR("alpha-left"):
458 stack
[0].__raw
= bzInputBtn(9) ? 1 : 0;
461 case COMPILE_TIME_CRC32_STR("alpha-right"):
462 stack
[0].__raw
= bzInputBtn(10) ? 1 : 0;
466 // Warn about button??
472 static void native_btnp(SVMModuleInstance
*instance
, int32_t parameterCount
, SVMOperand stack
[]) {
473 switch (stack
[1].pointerLiteral
) {
474 case COMPILE_TIME_CRC32_STR("up"):
475 stack
[0].__raw
= bzInputBtnP(1) ? 1 : 0;
478 case COMPILE_TIME_CRC32_STR("down"):
479 stack
[0].__raw
= bzInputBtnP(2) ? 1 : 0;
482 case COMPILE_TIME_CRC32_STR("left"):
483 stack
[0].__raw
= bzInputBtnP(3) ? 1 : 0;
486 case COMPILE_TIME_CRC32_STR("right"):
487 stack
[0].__raw
= bzInputBtnP(4) ? 1 : 0;
490 case COMPILE_TIME_CRC32_STR("action1"):
491 stack
[0].__raw
= bzInputBtnP(5) ? 1 : 0;
494 case COMPILE_TIME_CRC32_STR("action2"):
495 stack
[0].__raw
= bzInputBtnP(6) ? 1 : 0;
498 case COMPILE_TIME_CRC32_STR("alpha-up"):
499 stack
[0].__raw
= bzInputBtnP(7) ? 1 : 0;
502 case COMPILE_TIME_CRC32_STR("alpha-down"):
503 stack
[0].__raw
= bzInputBtnP(8) ? 1 : 0;
506 case COMPILE_TIME_CRC32_STR("alpha-left"):
507 stack
[0].__raw
= bzInputBtnP(9) ? 1 : 0;
510 case COMPILE_TIME_CRC32_STR("alpha-right"):
511 stack
[0].__raw
= bzInputBtnP(10) ? 1 : 0;
515 // Warn about button??
521 static void native_depth(SVMModuleInstance
*instance
, int32_t parameterCount
, SVMOperand stack
[]) {
522 BZScriptBindingMetadata
*metadata
= (BZScriptBindingMetadata
*)svmGetModuleInstanceUserParameter(instance
);
523 _CONTEXT_PARAMETER(sortIdx
) = FIXED_TO_INT(stack
[1].integerLiteral
);
526 static void native_layer(SVMModuleInstance
*instance
, int32_t parameterCount
, SVMOperand stack
[]) {
527 BZScriptBindingMetadata
*metadata
= (BZScriptBindingMetadata
*)svmGetModuleInstanceUserParameter(instance
);
528 _CONTEXT_PARAMETER(layer
) = stack
[1].pointerLiteral
;
529 _CONTEXT_PARAMETER(drawQueue
) = bzGameGetSceneLayerDrawQueue(_CONTEXT_PARAMETER(scene
), _CONTEXT_PARAMETER(layer
));
532 static void native_resetMatrix(SVMModuleInstance
*instance
, int32_t parameterCount
, SVMOperand stack
[]) {
533 BZScriptBindingMetadata
*metadata
= (BZScriptBindingMetadata
*)svmGetModuleInstanceUserParameter(instance
);
534 resetTransform(metadata
);
535 //bzMatrixIdentity(&_CONTEXT_PARAMETER(matrix));
538 static void native_translate(SVMModuleInstance
*instance
, int32_t parameterCount
, SVMOperand stack
[]) {
539 BZScriptBindingMetadata
*metadata
= (BZScriptBindingMetadata
*)svmGetModuleInstanceUserParameter(instance
);
540 _CONTEXT_PARAMETER(position
).x
+= _FLOAT_PARAMETER(1);
541 _CONTEXT_PARAMETER(position
).y
+= _FLOAT_PARAMETER(2);
542 calculateTransformMatrix(metadata
);
545 // bzMatrixTranslation(&m, _FLOAT_PARAMETER(1), _FLOAT_PARAMETER(2));
546 // bzMatrixMultiply(&_CONTEXT_PARAMETER(matrix), &_CONTEXT_PARAMETER(matrix), &m);
549 static void native_rotate(SVMModuleInstance
*instance
, int32_t parameterCount
, SVMOperand stack
[]) {
550 BZScriptBindingMetadata
*metadata
= (BZScriptBindingMetadata
*)svmGetModuleInstanceUserParameter(instance
);
551 _CONTEXT_PARAMETER(angle
) += _FLOAT_PARAMETER(1);
552 calculateTransformMatrix(metadata
);
554 // bzMatrixRotation(&m, _FLOAT_PARAMETER(1));
555 // bzMatrixMultiply(&_CONTEXT_PARAMETER(matrix), &_CONTEXT_PARAMETER(matrix), &m);
558 static void native_scale(SVMModuleInstance
*instance
, int32_t parameterCount
, SVMOperand stack
[]) {
559 BZScriptBindingMetadata
*metadata
= (BZScriptBindingMetadata
*)svmGetModuleInstanceUserParameter(instance
);
560 _CONTEXT_PARAMETER(scale
).x
*= _FLOAT_PARAMETER(1);
561 if (parameterCount
== 1) {
562 _CONTEXT_PARAMETER(scale
).y
*= _FLOAT_PARAMETER(1);
564 _CONTEXT_PARAMETER(scale
).y
*= _FLOAT_PARAMETER(2);
566 calculateTransformMatrix(metadata
);
569 //if (parameterCount == 1) {
570 // bzMatrixScale(&m, _FLOAT_PARAMETER(1), _FLOAT_PARAMETER(1));
572 // bzMatrixScale(&m, _FLOAT_PARAMETER(1), _FLOAT_PARAMETER(2));
574 //bzMatrixMultiply(&_CONTEXT_PARAMETER(matrix), &_CONTEXT_PARAMETER(matrix), &m);
577 static void native_event(SVMModuleInstance
*instance
, int32_t parameterCount
, SVMOperand stack
[]) {
578 BZScriptBindingMetadata
*metadata
= (BZScriptBindingMetadata
*)svmGetModuleInstanceUserParameter(instance
);
580 if (parameterCount
== 1) {
581 bzGameScenePostEvent(_CONTEXT_PARAMETER(scene
), _RAW_PARAMETER(1), NULL
);
582 } else if (parameterCount
>= 2) {
583 BZSceneEventData data
;
584 data
.integerValue
= _RAW_PARAMETER(2);
585 bzGameScenePostEvent(_CONTEXT_PARAMETER(scene
), _RAW_PARAMETER(1), &data
);
589 static void native_eventCheck(SVMModuleInstance
*instance
, int32_t parameterCount
, SVMOperand stack
[]) {
590 BZScriptBindingMetadata
*metadata
= (BZScriptBindingMetadata
*)svmGetModuleInstanceUserParameter(instance
);
591 BZSceneEventData data
;
592 bool found
= bzGameSceneQueryEvent(&data
, _CONTEXT_PARAMETER(scene
), _RAW_PARAMETER(1));
593 if (found
&& parameterCount
>= 2) {
594 found
= (data
.integerValue
== _RAW_PARAMETER(2));
596 stack
[0].__raw
= found
? 1 : 0;
599 /*static void native_agent(SVMModuleInstance *instance, int32_t parameterCount, SVMOperand stack[]) {
600 BZScriptBindingMetadata *metadata = (BZScriptBindingMetadata *)svmGetModuleInstanceUserParameter(instance);
601 BZAgentSimulationID simulation = bzGameGetAgentSimulation(metadata->scene);
603 BZAgentDetails details = {
604 .angle = metadata->angle,
605 .mass = bzAgentDefaultMass,
606 .maxForce = bzAgentDefaultMaxForce,
607 .maxSpeed = bzAgentDefaultMaxSpeed,
608 .position = metadata->position,
611 if (parameterCount >= 1) {
612 details.position.x = _FLOAT_PARAMETER(1);
614 if (parameterCount >= 2) {
615 details.position.y = _FLOAT_PARAMETER(2);
617 if (parameterCount >= 3) {
618 details.angle = _FLOAT_PARAMETER(3);
620 if (parameterCount >= 4) {
621 details.mass = _FLOAT_PARAMETER(4);
623 if (parameterCount >= 5) {
624 details.maxForce = _FLOAT_PARAMETER(5);
626 if (parameterCount >= 6) {
627 details.maxSpeed = _FLOAT_PARAMETER(6);
630 metadata->agent = bzFXAgentSimulationAddAgent(simulation, &details);
633 static void native_agentSync(SVMModuleInstance *instance, int32_t parameterCount, SVMOperand stack[]) {
634 BZScriptBindingMetadata *metadata = (BZScriptBindingMetadata *)svmGetModuleInstanceUserParameter(instance);
635 BZAgentSimulationID simulation = bzGameGetAgentSimulation(metadata->scene);
637 BZAgentDetails details;
638 bzFXAgentSimulationGetAgentDetails(&details, simulation, metadata->agent);
640 metadata->angle = details.angle;
641 metadata->position = details.position;
643 calculateTransformMatrix(metadata);
646 static void native_scene(SVMModuleInstance
*instance
, int32_t parameterCount
, SVMOperand stack
[]) {
647 BZScriptBindingMetadata
*metadata
= (BZScriptBindingMetadata
*)svmGetModuleInstanceUserParameter(instance
);
648 BZSceneEventData data
= {
649 .stringValue
= _STRING_PARAMETER(1),
651 bzGameScenePostEvent(_CONTEXT_PARAMETER(scene
), bzSceneChangeEventIdentifier
, &data
);
653 // BZActorID actor = bzGameSceneAddActor(_CONTEXT_PARAMETER(scene), "%s", svmGetString(instance, stack[1].pointerLiteral));
654 // for (size_t i = 2, j = 1; i <= parameterCount; ++i, ++j) {
655 // actor->parameters[j] = stack[i].__raw;
659 static void native_id(SVMModuleInstance
*instance
, int32_t parameterCount
, SVMOperand stack
[]) {
660 BZScriptBindingMetadata
*metadata
= (BZScriptBindingMetadata
*)svmGetModuleInstanceUserParameter(instance
);
661 stack
[0].__raw
= metadata
->uuid
;
664 SVMFunctionCallback
bzScriptingBindingsLookupNativeFunction(uint32_t nameCRC
, void *userParam
) {
679 FUNCTION_BIND(adelta
)
692 FUNCTION_BIND(distance
)
693 FUNCTION_BIND(distanceCheck
)
697 // FUNCTION_BIND(pset)
698 // FUNCTION_BIND(sget)
700 FUNCTION_BIND(camera
)
702 FUNCTION_BIND(circfill
)
705 FUNCTION_BIND(rectfill
)
706 FUNCTION_BIND(triangle
)
710 FUNCTION_BIND(printf
)
712 FUNCTION_BIND(palette
)
713 FUNCTION_BIND(output
)
719 FUNCTION_BIND(resetMatrix
)
720 FUNCTION_BIND(translate
)
721 FUNCTION_BIND(rotate
)
724 FUNCTION_BIND(particle
)
727 FUNCTION_BIND(speech
)
728 FUNCTION_BIND(speechf
)
730 FUNCTION_BIND(collision
)
731 FUNCTION_BIND(collisionCheck
)
735 FUNCTION_BIND(parameter
)
736 FUNCTION_BIND(public)
739 FUNCTION_BIND(eventCheck
)
741 // FUNCTION_BIND(agent)
742 // FUNCTION_BIND(agentSync)
749 bzLog("Warning: Could not find function %d", nameCRC
);