This repository was archived by the owner on Jan 8, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathmain.cpp
968 lines (771 loc) · 23.9 KB
/
main.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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
#include "declarations.h"
#include "util.h"
#include <string>
#include "GarrysMod/Lua/LuaBase.h"
using namespace GarrysMod::Lua;
std::shared_ptr<FLEX_API> FLEX_Simulation;
GarrysMod::Lua::ILuaBase* GlobalLUA;
std::mutex* bufferMutex;
float4* particleBufferHost;
float4* diffuseBufferHost;
float simTimescale = 1;
int ParticleCount = 0;
int diffuseCount = 0;
int PropCount = 0;
bool SimValid = true;
int RenderDistance = pow(5000, 2);
//overloaded printlua func
void LUA_Print(std::string text)
{
GlobalLUA->PushSpecial(SPECIAL_GLOB);
GlobalLUA->GetField(-1, "print");
GlobalLUA->PushString(text.c_str());
GlobalLUA->Call(1, 0);
GlobalLUA->Pop();
}
void LUA_Print(char* text)
{
GlobalLUA->PushSpecial(SPECIAL_GLOB);
GlobalLUA->GetField(-1, "print");
GlobalLUA->PushString(text);
GlobalLUA->Call(1, 0);
GlobalLUA->Pop();
}
////////// LUA FUNCTIONS /////////////
//renders particles
LUA_FUNCTION(RenderParticles) {
LUA->CheckType(-1, Type::Vector); //dir left
LUA->CheckType(-2, Type::Vector); //dir right
LUA->CheckType(-3, Type::Vector); //dir down
LUA->CheckType(-4, Type::Vector); //dir up
LUA->CheckType(-5, Type::Vector); //eye pos
LUA->CheckType(-6, Type::Number); //override
LUA->CheckType(-7, Type::Bool); //render diffuse?
if (ParticleCount < 1) return 0;
float3 directionArray[4];
for (int i = 0; i < 4; i++)
directionArray[i] = LUA->GetVector(-i - 1);
float3 eyePos = LUA->GetVector(-5);
// draw sprite or sphere?
bool renderDiffuse = LUA->GetBool(-7);
float overrideNum = static_cast<float>(LUA->GetNumber(-6));
float particleRadius = FLEX_Simulation->radius * overrideNum;
const char* overrideString = overrideNum != 0.6f ? "GWater_DrawSprite" : "GWater_DrawSphere";
LUA->Pop(6);
LUA->PushSpecial(SPECIAL_GLOB);
float4 particlePos; // Reassign these, dont redeclare them
float3 particleMinusPos;
Vector gmodPos;
Vector gmodColor;
Vector gmodLastColor;
gmodLastColor.x = -1.f;
//we need to optimize the crap out of this because it can run 65 thousand times per frame!
for (int i = 0; i < ParticleCount; i++) {
particlePos = particleBufferHost[i];
if (particlePos.w != 0.5f) continue;
particleMinusPos = float3(particlePos) - eyePos;
if (
Dot(particleMinusPos, directionArray[0]) < 0 ||
Dot(particleMinusPos, directionArray[1]) < 0 ||
Dot(particleMinusPos, directionArray[2]) < 0 ||
Dot(particleMinusPos, directionArray[3]) < 0
) continue;
if (DistanceSquared(particlePos, eyePos) > RenderDistance) continue;
//reassign, do not redeclare
gmodPos.x = particlePos.x;
gmodPos.y = particlePos.y;
gmodPos.z = particlePos.z;
// if the color is different (or the first one) then we should update it
gmodColor = FLEX_Simulation->particleColors[i];
bool shouldChange = (gmodColor.x != gmodLastColor.x || gmodColor.y != gmodLastColor.y || gmodColor.z != gmodLastColor.z);
if (shouldChange) {
LUA->GetField(-1, "GWater_SetDrawColor");
LUA->PushVector(gmodColor);
LUA->Call(1, 0);
}
gmodLastColor = gmodColor;
//draws the sprite
LUA->GetField(-1, overrideString);
LUA->PushVector(gmodPos);
LUA->PushNumber(particleRadius);
LUA->Call(2, 0); //pops literally everything above except _G
}
if (!renderDiffuse) {
LUA->Pop(1);
FLEX_Simulation->flexParams->diffuseLifetime = 0.f;
return 0;
}
else {
FLEX_Simulation->flexParams->diffuseLifetime = 30.f;
}
// now render diffuse particles!
gmodColor.x = gmodColor.x + 0.5;
gmodColor.y = gmodColor.y + 0.5;
gmodColor.z = gmodColor.z + 0.5;
LUA->GetField(-1, "GWater_SetDrawColor");
LUA->PushVector(gmodColor);
LUA->Call(1, 0);
for (int i = 0; i < diffuseCount; i++) {
particlePos = diffuseBufferHost[i];
particleMinusPos = float3(particlePos) - eyePos;
if (particlePos.w < 1) continue;
if (
Dot(particleMinusPos, directionArray[0]) < 0 ||
Dot(particleMinusPos, directionArray[1]) < 0 ||
Dot(particleMinusPos, directionArray[2]) < 0 ||
Dot(particleMinusPos, directionArray[3]) < 0
) continue;
if (DistanceSquared(particlePos, eyePos) > RenderDistance) continue;
gmodPos.x = particlePos.x;
gmodPos.y = particlePos.y;
gmodPos.z = particlePos.z;
//draws the sprite
LUA->GetField(-1, overrideString);
LUA->PushVector(gmodPos);
LUA->PushNumber((particlePos.w / 120.f) * particleRadius);
LUA->Call(2, 0); //pops literally everything above except _G
}
LUA->Pop(1); //pop _G from stack
return 0;
}
//Gets particles near a position in radius
LUA_FUNCTION(ParticlesNear) {
//get headpos
LUA->CheckType(1, Type::Vector);
LUA->CheckType(2, Type::Number);
float4 particlePos;
float3 pos = float3(LUA->GetVector(1));
float particleRadius = (float)pow(LUA->GetNumber(2), 2);
int particlesBeside = 0;
std::vector<int> range = std::vector<int>();
for (int i = 0; i < ParticleCount; i++) {
particlePos = particleBufferHost[i];
if (particlePos.w != 0.5) continue;
if (DistanceSquared(float3(particlePos), pos) < particleRadius) {
particlesBeside++;
range.push_back(i);
}
}
LUA->PushNumber(particlesBeside);
for (int i : range) LUA->PushNumber(i);
return 1 + range.size();
}
LUA_FUNCTION(CleanLostParticles) {
FLEX_Simulation->cleanLostParticles();
return 0;
}
LUA_FUNCTION(CleanLoneParticles) {
LUA->PushNumber(FLEX_Simulation->cleanLoneParticles());
return 1;
}
LUA_FUNCTION(GetData) {
LUA->CreateTable();
//loop thru all particles & add to table (on stack)
float4 thisPos;
Vector gmodPos;
for (int i = 0; i < ParticleCount; i++) {
thisPos = particleBufferHost[i];
gmodPos.x = thisPos.x;
gmodPos.y = thisPos.y;
gmodPos.z = thisPos.z;
LUA->PushNumber((double)i + 1);
LUA->PushVector(gmodPos);
LUA->SetTable(-3);
}
return 1;
}
LUA_FUNCTION(GetSkewedData) {
LUA->CreateTable();
//loop thru all particles & add to table (on stack)
float4 thisPos;
Vector gmodPos;
int n = 0;
for (int i = 0; i < ParticleCount; i++) {
if (particleBufferHost[i].w != 0.5) continue;
thisPos = particleBufferHost[i];
gmodPos.x = thisPos.x;
gmodPos.y = thisPos.y;
gmodPos.z = thisPos.z;
LUA->PushNumber((double)n + 1);
LUA->PushVector(gmodPos);
LUA->SetTable(-3);
n++;
}
return 1;
}
LUA_FUNCTION(GetClothData) {
LUA->CreateTable();
//loop thru all particles & add to table (on stack)
float3 particlePos;
Vector gmodPos;
for (int i = 0; i < FLEX_Simulation->triangles.size(); i++) {
LUA->PushNumber((double)i + 1);
LUA->CreateTable(); //table inside bigger table
for (int j = 0; j < FLEX_Simulation->triangles[i].size(); j++) {
particlePos = particleBufferHost[FLEX_Simulation->triangles[i][j]];
gmodPos.x = particlePos.x;
gmodPos.y = particlePos.y;
gmodPos.z = particlePos.z;
LUA->PushNumber((double)j + 1);
LUA->PushVector(gmodPos);
LUA->SetTable(-3);
}
LUA->SetTable(-3);
}
return 1;
}
//removes all particles in the simulation
LUA_FUNCTION(RemoveAll) {
bufferMutex->lock();
if (!SimValid) {
bufferMutex->unlock();
return 0;
}
FLEX_Simulation->removeAllParticles();
bufferMutex->unlock();
return 0;
}
LUA_FUNCTION(RemoveAllCloth) {
bufferMutex->lock();
if (!SimValid) {
bufferMutex->unlock();
return 0;
}
FLEX_Simulation->removeAllCloth();
bufferMutex->unlock();
return 0;
}
//sphere intersection code for function below (gotta pick up particles somehow)
float sphereIntersection(float3 center, float radius, float3 origin, float3 direction) {
float3 oc = origin - center;
float a = Dot(direction, direction);
float b = 2.f * Dot(oc, direction);
float c = Dot(oc, oc) - radius * radius;
float discriminant = b * b - 4 * a * c;
if (discriminant < 0) {
return -1.f;
}
else {
return (-b - sqrt(discriminant)) / (2.f * a);
}
}
//sets radius of particles
LUA_FUNCTION(TraceLine) {
LUA->CheckType(1, Type::Vector);
LUA->CheckType(2, Type::Vector);
int particleIndex = -1;
float dist = -1.f;
float r = FLEX_Simulation->radius / 2.f;
float4 pos;
float3 rayOrigin = LUA->GetVector(1);
float3 rayDirection = LUA->GetVector(2);
for (int i = 0; i < ParticleCount; i++) {
float particleHit = sphereIntersection(particleBufferHost[i], r, rayOrigin, rayDirection);
if ((particleHit > 0.f && particleHit < dist) || dist < 0.f) {
dist = particleHit;
pos = particleBufferHost[i];
particleIndex = i;
}
}
Vector gmodVector;
gmodVector.x = pos.x;
gmodVector.y = pos.y;
gmodVector.z = pos.z;
LUA->Pop(2);
LUA->PushNumber(dist);
LUA->PushVector(gmodVector);
LUA->PushNumber(particleIndex);
return 3;
}
LUA_FUNCTION(SetParticlePos) {
LUA->CheckType(1, Type::Number);
LUA->CheckType(2, Type::Vector);
LUA->CheckType(3, Type::Number);
float invMass = FLEX_Simulation->editParticle(LUA->GetNumber(1), LUA->GetVector(2), float3(), LUA->GetNumber(3));
LUA->Pop(2);
LUA->PushNumber(invMass);
return 1;
}
//sets radius of particles
LUA_FUNCTION(SetRadius) {
LUA->CheckType(1, Type::Number);
float radius = (float)LUA->GetNumber();
if (radius > 8192 || !(radius > 0)) {
LUA->ThrowError(("Tried to set GWater particle radius to " + std::to_string(radius) + "! (1 min, 8192 max)").c_str());
return 0;
}
FLEX_Simulation->initParamsRadius(radius);
LUA->Pop();
return 0;
}
LUA_FUNCTION(GetRadius) {
LUA->PushNumber(FLEX_Simulation->radius);
return 1;
}
//A: sets max particles, this can't actually change the 65535 hard limit without editing the cpp
//so this will just change the limit that is checked by the code, and additionally erase any extra particles when maxparticles < particlecount
LUA_FUNCTION(SetMaxParticles) {
LUA->CheckType(1, Type::Number);
FLEX_Simulation->SetParticleLimit((int)LUA->GetNumber());
LUA->Pop();
return 0;
}
//A: getter
LUA_FUNCTION(GetMaxParticles) {
LUA->PushNumber(FLEX_Simulation->flexSolverDesc.maxParticles);
return 1;
}
//stops simulation
LUA_FUNCTION(DeleteSimulation) {
if (!SimValid) return 0;
FLEX_Simulation.reset();
//set .gwater to nil
LUA->PushSpecial(SPECIAL_GLOB);
LUA->PushNil();
LUA->SetField(-2, "gwater");
LUA->Pop(); // pop _G
return 0;
}
LUA_FUNCTION(SpawnParticle) {
//check to see if they are both vectors
LUA->CheckType(1, Type::Vector); // pos
LUA->CheckType(2, Type::Vector); // vel
LUA->CheckType(3, Type::Vector); // color
//gmod Vector and fleX float4
Vector gmodPos = LUA->GetVector(1); //pos
Vector gmodVel = LUA->GetVector(2); //vel
Vector gmodColor = LUA->GetVector(3); //color
FLEX_Simulation->addParticle(gmodPos, gmodVel, gmodColor);
//remove vel and pos from stack
LUA->Pop(2);
return 0;
}
LUA_FUNCTION(SpawnCloth) {
//check to see if they are both vectors
LUA->CheckType(1, Type::Vector); // pos
LUA->CheckType(2, Type::Number); // width
LUA->CheckType(3, Type::Number); // how close apart particles are
LUA->CheckType(4, Type::Number); // stiffness
bufferMutex->lock();
if (!SimValid) {
bufferMutex->unlock();
return 1;
}
float num = LUA->GetNumber(5);
if (LUA->GetNumber(5) <= 0) { //getnumber returns 0 on fail
num = 1.f;
}
FLEX_Simulation->addCloth(LUA->GetVector(1), LUA->GetNumber(2), LUA->GetNumber(3), LUA->GetNumber(4), num);
bufferMutex->unlock();
LUA->Pop(4);
return 0;
}
LUA_FUNCTION(SpawnRigidbody) {
//check to see if they are both vectors
LUA->CheckType(1, Type::Vector); // pos
LUA->CheckType(2, Type::Vector); // width
LUA->CheckType(3, Type::Number); // how close apart particles are
LUA->CheckType(4, Type::Bool); // constraint force
//LUA->CheckType(4, Type::Number); // mass
bufferMutex->lock();
if (!SimValid) {
bufferMutex->unlock();
return 0;
}
float3 rigidRes = LUA->GetVector(2);
FLEX_Simulation->addRigidbody(LUA->GetVector(1), rigidRes.x, rigidRes.y, rigidRes.z, LUA->GetNumber(3), float3(), 10.f, LUA->GetBool(4));
bufferMutex->unlock();
LUA->Pop(3);
return 0;
}
LUA_FUNCTION(SpawnSphere) {
//check to see if they are both vectors
LUA->CheckType(1, Type::Vector); // pos
LUA->CheckType(2, Type::Number); // size
LUA->CheckType(3, Type::Number); // size apart (usually radius)
LUA->CheckType(4, Type::Vector); // vel
//gmod Vector
Vector gmodPos = LUA->GetVector(1); //pos
float radius = LUA->GetNumber(2); //radius
float size = LUA->GetNumber(3); //size apart
Vector gmodVel = LUA->GetVector(4); //vel
Vector gmodColor = LUA->GetVector(5); //color
for (int z = -radius; z <= radius; z++) {
for (int y = -radius; y <= radius; y++) {
for (int x = -radius; x <= radius; x++) {
if (x * x + y * y + z * z >= radius * radius) continue;
Vector offset;
offset.x = gmodPos.x + x * size;
offset.y = gmodPos.y + y * size;
offset.z = gmodPos.z + z * size;
FLEX_Simulation->addParticle(offset, gmodVel, gmodColor);
}
}
}
//remove pos, size, size, and vel
LUA->Pop(4);
return 0;
}
//andreweathan
LUA_FUNCTION(SpawnCube) {
//check to see if they are both vectors
LUA->CheckType(1, Type::Vector); // pos
LUA->CheckType(2, Type::Vector); // size
LUA->CheckType(3, Type::Number); // size apart (usually radius)
LUA->CheckType(4, Type::Vector); // vel
LUA->CheckType(5, Type::Vector); // color
//gmod Vector and fleX float4
Vector gmodPos = LUA->GetVector(1); //pos
Vector gmodSize = LUA->GetVector(2); //size
float size = LUA->GetNumber(3); //size apart
Vector gmodVel = LUA->GetVector(4); //vel
Vector gmodColor = LUA->GetVector(5); //color
gmodSize.x /= 2;
gmodSize.y /= 2;
gmodSize.z /= 2;
for (float z = -gmodSize.z; z < gmodSize.z; z++) {
for (float y = -gmodSize.y ; y < gmodSize.y; y++) {
for (float x = -gmodSize.x; x < gmodSize.x; x++) {
Vector newPos;
newPos.x = (x * size) + gmodPos.x;
newPos.y = (y * size) + gmodPos.y;
newPos.z = (z * size) + gmodPos.z;
FLEX_Simulation->addParticle(newPos, gmodVel, gmodColor);
}
}
}
LUA->Pop(4);
return 0;
}
//mesh creation funcs
LUA_FUNCTION(AddConvexMesh) {
LUA->CheckType(1, Type::Table); // Sorted verts
LUA->CheckType(2, Type::Vector); // Min
LUA->CheckType(3, Type::Vector); // Max
LUA->CheckType(4, Type::Vector); // prop pos
LUA->CheckType(5, Type::Angle); // prop angle
//is the mesh valid?
size_t len = LUA->ObjLen(-5);
if (len < 1 || len % 3 != 0) {
LUA->PushBool(false);
return 1;
}
//lock buffer
bufferMutex->lock();
if (!SimValid) {
bufferMutex->unlock();
LUA->PushBool(false);
return 0;
}
//flex can only handle 64 convex planes!
if (len / 3 <= 64)
FLEX_Simulation->addMeshConvex(LUA);
else
FLEX_Simulation->addMeshConcave(LUA);
bufferMutex->unlock();
LUA->PushBool(true);
return 1;
}
LUA_FUNCTION(AddConcaveMesh) {
LUA->CheckType(1, Type::Table); // Sorted verts
LUA->CheckType(2, Type::Vector); // Min
LUA->CheckType(3, Type::Vector); // Max
LUA->CheckType(4, Type::Vector); // prop pos
LUA->CheckType(5, Type::Angle); // prop angle
//is the mesh valid?
size_t len = LUA->ObjLen(-5);
if (len < 1 || len % 3 != 0) {
LUA->PushBool(false);
return 1;
}
//lock buffer
bufferMutex->lock();
if (!SimValid) {
bufferMutex->unlock();
LUA->PushBool(false);
return 1;
}
FLEX_Simulation->addMeshConcave(LUA);
bufferMutex->unlock();
LUA->PushBool(true);
return 1;
}
LUA_FUNCTION(AddCapsuleMesh) {
LUA->CheckType(1, Type::Number); // Length
LUA->CheckType(2, Type::Number); // Radius
LUA->CheckType(3, Type::Vector); // prop pos
LUA->CheckType(4, Type::Angle); // prop angle
//lock buffer
bufferMutex->lock();
if (!SimValid) {
bufferMutex->unlock();
return 0;
}
FLEX_Simulation->addMeshCapsule(LUA);
bufferMutex->unlock();
return 0;
}
LUA_FUNCTION(SetMeshPos) {
LUA->CheckType(1, Type::Vector); // pos
LUA->CheckType(2, Type::Angle); // angle
LUA->CheckType(3, Type::Number); // ID
FLEX_Simulation->updateMeshPos(LUA->GetVector(-3), LUA->GetAngle(-2), LUA->GetNumber(-1));
LUA->Pop(3); //pop all
return 0;
}
//Removes particles in a radius
LUA_FUNCTION(Blackhole) {
LUA->CheckType(1, Type::Vector); // pos
LUA->CheckType(2, Type::Number); // radius
LUA->PushNumber(FLEX_Simulation->removeInRadius(LUA->GetVector(-2), LUA->GetNumber()));
return 1;
}
// Andrew: this applies force to particles in an area
LUA_FUNCTION(ApplyForce) {
LUA->CheckType(1, Type::Vector); // pos
LUA->CheckType(2, Type::Vector); // vel
LUA->CheckType(3, Type::Number); // radius
LUA->CheckType(4, Type::Bool); // linear?
// if it's linear then particles at Radius position get the weakest force while the closest to Pos get the strongest
// if it's not then every particle in that radius gets the same force
Vector pos = LUA->GetVector(1);
Vector vel = LUA->GetVector(2);
float radius = LUA->GetNumber(3);
bool linear = LUA->GetBool(4);
FLEX_Simulation->applyForce(float3(pos), float3(vel), radius, linear);
return 0;
}
// i am so tired
LUA_FUNCTION(ApplyForceRange) {
LUA->CheckType(1, Type::Vector); // pos
LUA->CheckType(2, Type::Vector); // vel
LUA->CheckType(3, Type::Number); // radius
LUA->CheckType(4, Type::Bool); // linear?
LUA->CheckType(5, Type::Number); // amount of particles
// if it's linear then particles at Radius position get the weakest force while the closest to Pos get the strongest
// if it's not then every particle in that radius gets the same force
std::vector<int> range = std::vector<int>();
Vector pos = LUA->GetVector(1);
Vector vel = LUA->GetVector(2);
float radius = LUA->GetNumber(3);
bool linear = LUA->GetBool(4);
int count = (int)LUA->GetNumber(5);
for (int i = 0; i < count; i++) {
range.push_back((int)LUA->GetNumber(6 + i));
}
FLEX_Simulation->applyForceRange(float3(pos), float3(vel), radius, linear, range);
return 0;
}
// this will help with simulating explosions and stuff affecting water
LUA_FUNCTION(ApplyForceOutwards) {
LUA->CheckType(1, Type::Vector); // pos
LUA->CheckType(2, Type::Number); // force
LUA->CheckType(3, Type::Number); // radius
LUA->CheckType(4, Type::Bool); // linear?
// if it's linear then particles at Radius position get the weakest force while the closest to Pos get the strongest
// if it's not then every particle in that radius gets the same force
Vector pos = LUA->GetVector(1);
float force = LUA->GetNumber(2);
float radius = LUA->GetNumber(3);
bool linear = LUA->GetBool(4);
FLEX_Simulation->applyForceOutwards(pos, force, radius, linear);
return 0;
}
// this should help with physgun
LUA_FUNCTION(ApplyForceOutwardsRange) {
LUA->CheckType(1, Type::Vector); // pos
LUA->CheckType(2, Type::Number); // force
LUA->CheckType(3, Type::Number); // radius
LUA->CheckType(4, Type::Bool); // linear?
LUA->CheckType(5, Type::Number); // amount of particles
std::vector<int> range = std::vector<int>();
Vector pos = LUA->GetVector(1);
float force = LUA->GetNumber(2);
float radius = LUA->GetNumber(3);
bool linear = LUA->GetBool(4);
int count = (int)LUA->GetNumber(5);
for (int i = 0; i < count; i++) {
range.push_back((int)LUA->GetNumber(6 + i));
}
FLEX_Simulation->applyForceOutwardsRange(pos, force, radius, linear, range);
return 0;
}
//Forcefield functions
LUA_FUNCTION(SpawnForceField) {
LUA->CheckType(1, Type::Vector); // pos
LUA->CheckType(2, Type::Number); // radius
LUA->CheckType(3, Type::Number); // strength
LUA->CheckType(4, Type::Bool); // linear
LUA->CheckType(5, Type::Number); // type
FLEX_Simulation->addForceField(LUA->GetVector(1), LUA->GetNumber(2), LUA->GetNumber(3), LUA->GetBool(4), LUA->GetNumber(5));
LUA->Pop(5);
return 0;
}
LUA_FUNCTION(EditForceField) {
LUA->CheckType(1, Type::Number); // ID
LUA->CheckType(2, Type::Number); // radius
LUA->CheckType(3, Type::Number); // strength
LUA->CheckType(4, Type::Bool); // linear
LUA->CheckType(5, Type::Number); // type
int idx = LUA->GetNumber(1);
if (idx < 0 || idx > 63) {
LUA->ThrowError("Tried to edit a forcefield under index 0 or above 63!");
return 0;
}
FLEX_Simulation->editForceField(idx, LUA->GetNumber(2), LUA->GetNumber(3), LUA->GetBool(4), LUA->GetNumber(5));
LUA->Pop(5);
return 0;
}
LUA_FUNCTION(SetForceFieldPos) {
LUA->CheckType(1, Type::Number); // id
LUA->CheckType(2, Type::Vector); // pos
int idx = LUA->GetNumber(1);
if (idx < 0 || idx > 63) {
LUA->ThrowError("Tried to set a forcefield's position under index 0 or above 63!");
return 0;
}
FLEX_Simulation->setForceFieldPos(idx, LUA->GetVector(2));
LUA->Pop(2);
return 0;
}
LUA_FUNCTION(RemoveForceField) {
LUA->CheckType(1, Type::Number); // ID of forcefield
int idx = LUA->GetNumber(1);
if (idx < 0 || idx > 63) {
LUA->ThrowError("Tried to remove a forcefield under index 0 or above 63!");
return 0;
}
FLEX_Simulation->deleteForceField(idx);
LUA->Pop();
return 0;
}
LUA_FUNCTION(RemoveMesh) {
LUA->CheckType(1, Type::Number); // ID
int propidx = LUA->GetNumber(1);
if (propidx < 0) {
LUA->ThrowError("Tried to remove a mesh under index 0!");
return 0;
}
bufferMutex->lock();
if (!SimValid) {
bufferMutex->unlock();
return 0;
}
FLEX_Simulation->freeProp(propidx);
bufferMutex->unlock();
LUA->Pop(); //pop id
return 0;
}
LUA_FUNCTION(SetConfig) {
LUA->CheckType(1, Type::String); //ID of param
LUA->CheckType(2, Type::Number);
FLEX_Simulation->updateParam(LUA->GetString(1), LUA->GetNumber(2));
LUA->Pop(2);
return 0;
}
LUA_FUNCTION(GetConfig) {
LUA->CheckType(1, Type::String); //ID of param
LUA->PushNumber(*FLEX_Simulation->flexMap[LUA->GetString()]);
return 1;
}
LUA_FUNCTION(GetModuleVersion) {
LUA->PushNumber(GWATER_VERSION);
return 1;
}
LUA_FUNCTION(SetTimescale) {
LUA->CheckType(-1, Type::Number);
simTimescale = LUA->GetNumber();
if (simTimescale < 0) simTimescale = 0;
LUA->Pop();
return 0;
}
LUA_FUNCTION(GetTimescale) {
LUA->PushNumber(simTimescale);
return 1;
}
LUA_FUNCTION(SetRenderDistance) {
LUA->CheckType(-1, Type::Number);
RenderDistance = pow(LUA->GetNumber(), 2);
if (RenderDistance < 0) RenderDistance = 0;
LUA->Pop();
return 0;
}
LUA_FUNCTION(GetRenderDistance) {
LUA->PushNumber(sqrt(RenderDistance));
return 1;
}
LUA_FUNCTION(GetParticleCount) {
LUA->PushNumber(ParticleCount);
return 1;
}
void PopulateFunctions(ILuaBase* LUA) {
LUA->PushSpecial(SPECIAL_GLOB);
LUA->CreateTable();
//particle-related
ADD_FUNC(SpawnParticle, "SpawnParticle");
ADD_FUNC(SpawnSphere, "SpawnSphere");
ADD_FUNC(SpawnCube, "SpawnCube");
ADD_FUNC(SpawnCloth, "SpawnCloth");
ADD_FUNC(SpawnRigidbody, "SpawnRigidbody");
ADD_FUNC(CleanLostParticles, "CleanLostParticles");
ADD_FUNC(CleanLoneParticles, "CleanLoneParticles");
ADD_FUNC(SetMaxParticles, "SetMaxParticles");
ADD_FUNC(GetMaxParticles, "GetMaxParticles");
ADD_FUNC(GetParticleCount, "GetParticleCount");
ADD_FUNC(TraceLine, "TraceLine");
ADD_FUNC(SetParticlePos, "SetParticlePos")
//meshes
ADD_FUNC(AddConvexMesh, "AddConvexMesh");
ADD_FUNC(AddConcaveMesh, "AddConcaveMesh");
ADD_FUNC(AddCapsuleMesh, "AddCapsuleMesh");
ADD_FUNC(SetMeshPos, "SetMeshPos");
ADD_FUNC(RemoveMesh, "RemoveMesh");
//rendering & data transfer
ADD_FUNC(RenderParticles, "RenderParticles");
ADD_FUNC(RemoveAll, "RemoveAll");
ADD_FUNC(RemoveAllCloth, "RemoveAllCloth");
ADD_FUNC(ParticlesNear, "ParticlesNear");
ADD_FUNC(GetData, "GetData");
ADD_FUNC(GetSkewedData, "GetSkewedData");
ADD_FUNC(GetClothData, "GetClothData");
ADD_FUNC(SetRenderDistance, "SetRenderDistance");
ADD_FUNC(GetRenderDistance, "GetRenderDistance");
//forces
ADD_FUNC(SpawnForceField, "SpawnForceField");
ADD_FUNC(RemoveForceField, "RemoveForceField");
ADD_FUNC(SetForceFieldPos, "SetForceFieldPos");
ADD_FUNC(EditForceField, "EditForceField");
ADD_FUNC(ApplyForce, "ApplyForce");
ADD_FUNC(ApplyForceRange, "ApplyForceRange");
ADD_FUNC(ApplyForceOutwards, "ApplyForceOutwards");
ADD_FUNC(ApplyForceOutwardsRange, "ApplyForceOutwardsRange");
//param funcs
ADD_FUNC(SetRadius, "SetRadius");
ADD_FUNC(GetRadius, "GetRadius");
ADD_FUNC(SetConfig, "SetConfig");
ADD_FUNC(GetConfig, "GetConfig");
//extras
ADD_FUNC(Blackhole, "Blackhole");
ADD_FUNC(GetModuleVersion, "GetModuleVersion");
ADD_FUNC(SetTimescale, "SetTimescale");
ADD_FUNC(GetTimescale, "GetTimescale");
LUA->SetField(-2, "gwater");
LUA->Pop(); //remove _G
}
//called when module is opened
GMOD_MODULE_OPEN() {
GlobalLUA = LUA;
PopulateFunctions(LUA);
// Initialize FleX api class
FLEX_Simulation = std::make_shared<FLEX_API>();
return 0;
}
// Called when the module is unloaded
GMOD_MODULE_CLOSE() {
FLEX_Simulation.reset();
LUA->PushSpecial(SPECIAL_GLOB);
LUA->PushNil();
LUA->SetField(-2, "gwater");
LUA->Pop(); // pop _G
return 0;
}