-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathStampScript.luau
More file actions
3426 lines (3058 loc) · 93.3 KB
/
StampScript.luau
File metadata and controls
3426 lines (3058 loc) · 93.3 KB
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
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
-- STAMP SCRIPT
-- Heliodex 2025/11/21
-- Last edited 2025/11/21
--[[
Functions that are identical to RbxStamper:
findSeatsInModel()
setSeatEnabledStatus()
getClosestAlignedWorldDirection()
Functions that have some duplicated code:
findConfigAtMouseTarget() - the end bit
clusterPartsInRegion() - all except first few lines
autoAlignToFace()
flashRedBox() - some less error checking here
]]
-- basic functions
local function waitForChild(instance, name)
while not instance:findFirstChild(name) do
instance.ChildAdded:wait()
end
end
----------------------------------------------------------------------------------------
-- Locals
local Tool = script.Parent
local Mouse
local mouseMoveCon
local mouseButton1DownCon
local mouseButton1UpCon
local cameraChangeCon
local walking = false
local pressedEsc = false
-- local billBoardOwnerGui
local cluster = workspace:FindFirstChild "Terrain"
local InsertService = game:GetService "InsertService"
local Lighting = game:GetService "Lighting"
local Teams = game:GetService "Teams"
local gInitial90DegreeRotations = 0
local gStaticTrans = 1
local gDesiredTrans = 0.7
local transFadeInTime = 0.5
local fadeInDelayTime = 0.5
local eyedropperOffGridTolerance = 0.01
local insertBoundingBoxOverlapVector = Vector3.new(1, 1, 1) -- we can still stamp if our character extrudes into the target stamping space by 1 or fewer units
-- local useAssetVersionId = false
-- for high-scalability display
local adornPart = Instance.new "Part"
-- adornPart.Parent = nil
adornPart.formFactor = "Custom"
adornPart.Size = Vector3.new(4, 4, 4)
adornPart.CFrame = CFrame.new()
local adorn = Instance.new "SelectionBox"
adorn.Color = BrickColor.new "Toothpaste"
adorn.Adornee = adornPart
adorn.Visible = true
adorn.Transparency = 0
adorn.Name = "HighScalabilityStamperLine"
-- adorn.Parent = nil
local terrainSelectionBox = Instance.new "Part"
-- terrainSelectionBox.Parent = nil
terrainSelectionBox.formFactor = "Custom"
terrainSelectionBox.Size = Vector3.new(4, 4, 4)
terrainSelectionBox.CFrame = CFrame.new()
local HighScalabilityLine = {}
HighScalabilityLine.Start = nil
HighScalabilityLine.End = nil
HighScalabilityLine.Adorn = adorn
HighScalabilityLine.AdornPart = adornPart
HighScalabilityLine.InternalLine = nil
HighScalabilityLine.NewHint = true
-- for higher dimensional megacluster part stamping
HighScalabilityLine.MorePoints = { nil, nil }
HighScalabilityLine.MoreLines = { nil, nil }
HighScalabilityLine.Dimensions = 1
waitForChild(Tool, "LuaGlobalVariables")
local variables = Tool.LuaGlobalVariables
waitForChild(variables, "ShowInvalidPlacement")
waitForChild(variables, "Stamped")
waitForChild(Tool, "ErrorBox")
waitForChild(variables, "ShowAdminCategories")
local errorBox = Tool.ErrorBox
waitForChild(variables, "IsRestricted")
waitForChild(variables, "MouseClick")
click = variables.MouseClick
local Data = {
Stamp = {},
Loading = {},
}
local guiScriptIsLoadingSomething = false
local unstampableSurface = false
local eyeDropperConnection, eyeDropperMoveConnection
local playerModel
local player
local lastTargetCFrame = nil
local lastTargetTerrainOrientation = 0
-- For Restricting Stamper Tool
local isRestricted = variables.IsRestricted.Value
local adminAccess = variables.ShowAdminCategories.Value
-- For Delete highlighting
local selectionBox
local currentSelection
local currentSelectionColors = {}
if isRestricted then
waitForChild(workspace, "BaseplateBumpers")
end
----------------------------------------------------------------------------------------
-- Functions
local function hint(label)
-- Pass in a string, it shows a top hint. (Replaces previous hint, if exists)
local _player = game.Players:GetPlayerFromCharacter(Tool.Parent)
local topHint = _player.PlayerGui:FindFirstChild "topHint"
if not topHint then
return
end
topHint.Add.Label.Value = label
topHint.Add.Width.Value = 3 -- widest width
topHint.Add.Time.Value = 5
topHint.Add.Disabled = true
topHint.Add.Disabled = false
end
local function getClosestColourToTerrainMaterial(terrainValue)
if terrainValue == 1 then
return BrickColor.new "Bright green"
elseif terrainValue == 2 then
return BrickColor.new "Bright yellow"
elseif terrainValue == 3 then
return BrickColor.new "Bright red"
elseif terrainValue == 4 then
return BrickColor.new "Medium stone grey"
end
return BrickColor.new "Bright green"
end
local manualWeldTable = {}
local manualWeldParentTable = {}
local function saveTheWelds(object)
if object:IsA "ManualWeld" or object:IsA "Rotate" then
table.insert(manualWeldTable, object)
table.insert(manualWeldParentTable, object.Parent)
return
end
local children = object:GetChildren()
for i = 1, #children do
saveTheWelds(children[i])
end
end
local function restoreTheWelds()
for i = 1, #manualWeldTable do
manualWeldTable[i].Parent = manualWeldParentTable[i]
end
end
local function findSeatsInModel(parent, seatTable)
if not parent then
return
end
if parent.className == "Seat" or parent.className == "VehicleSeat" then
table.insert(seatTable, parent)
end
local myChildren = parent:GetChildren()
for j = 1, #myChildren do
findSeatsInModel(myChildren[j], seatTable)
end
end
local function setSeatEnabledStatus(model, isEnabled)
local seatList = {}
findSeatsInModel(model, seatList)
if isEnabled then
-- remove any welds called "SeatWeld" in seats
for i = 1, #seatList do
local nextSeat = seatList[i]:FindFirstChild "SeatWeld"
while nextSeat do
nextSeat:Remove()
nextSeat = seatList[i]:FindFirstChild "SeatWeld"
end
end
return
end
-- put a weld called "SeatWeld" in every seat
-- this tricks it into thinking there's already someone sitting there, and it won't make you sit XD
for i = 1, #seatList do
local fakeWeld = Instance.new "Weld"
fakeWeld.Name = "SeatWeld"
fakeWeld.Parent = seatList[i]
end
end
local function UnlockInstances(object)
if object:IsA "BasePart" then
object.Locked = false
end
for index, child in pairs(object:GetChildren()) do
UnlockInstances(child)
end
end
--[[
local function generateOwnerGui(playerName)
local gui = Instance.new("BillboardGui")
gui.Name = "PlayerStamperTagGui"
gui.StudsOffset = Vector3.new(0,1,0)
gui.ExtentsOffset = Vector3.new(0,1,0)
gui.Size = UDim2.new(5,0,2,0)
pcall(function() gui.PlayerToHideFrom = game.Players.LocalPlayer end)
local frame = Instance.new("Frame")
frame.BackgroundColor3 = Color3.new(0,0,0)
frame.BackgroundTransparency = 0.5
frame.Name = "OwnerFrame"
frame.Size = UDim2.new(1,0,1,0)
frame.Parent = gui
local ownerName = Instance.new("TextLabel")
ownerName.Name = "OwnerName"
ownerName.Size = UDim2.new(1,0,1,0)
ownerName.Text = playerName
ownerName.Font = Enum.Font.ArialBold
ownerName.FontSize = Enum.FontSize.Size14
ownerName.TextWrap = true
ownerName.TextColor3 = Color3.new(1,1,1)
ownerName.TextStrokeTransparency = 0
ownerName.BackgroundTransparency = 1
ownerName.Parent = frame
return gui
end
]]
local function getPlayer()
return game.Players:GetPlayerFromCharacter(script.Parent.Parent)
end
local function beginInsertDecal(decal)
Data.Stamp.DecalSelection = Instance.new "SurfaceSelection"
Data.Stamp.DecalSelection.Color = BrickColor.new "Bright orange"
Data.Stamp.DecalSelection.archivable = false
Data.Stamp.DecalSelection.Parent = getPlayer().PlayerGui
--Save the decal in our Lua code for later use
Data.Stamp.Decal = decal
Data.Stamp.Decal.Parent = nil
end
local function clearSelection()
if currentSelection then
for part, color in pairs(currentSelectionColors) do
part.BrickColor = color
end
selectionBox.Adornee = nil
end
currentSelectionColors = {}
-- I put these inside if statements, because we can't assume these exist. (Jahr, 12-29-2010)
if currentSelection then
currentSelection = nil
end
if selectionBox then
selectionBox.Adornee = nil
end
end
local function setSelection(partOrModel)
if partOrModel ~= currentSelection then
clearSelection()
currentSelection = partOrModel
selectionBox.Adornee = currentSelection
end
end
local function partInBounds(part)
if not part then
return false
end
local xOne = buildingPlate.Position.x + buildingPlate.Size.x / 2
local xTwo = buildingPlate.Position.x - buildingPlate.Size.x / 2
local zOne = buildingPlate.Position.z + buildingPlate.Size.z / 2
local zTwo = buildingPlate.Position.z - buildingPlate.Size.z / 2
if part.Position.x > xOne or part.Position.x < xTwo then
return false
end
if part.Position.z > zOne or part.Position.z < zTwo then
return false
end
return true
end
-- For Restricting Stamper Tool (isRestricted)
local function inBounds(object)
for part, transparency in pairs(object) do
if
part:IsA "Part"
or part:IsA "WedgePart"
or part:IsA "CornerWedgePart"
or part:IsA "TrussPart"
then
if not partInBounds(part) then
return false
end
elseif part:IsA "Model" then
local primPart = object.PrimaryPart
if not partInBounds(primPart) then
return false
end
end
end
return true
end
--[[
function noManualWelds(part)
local partChildren = part:GetChildren()
for i = 1, #partChildren do
if partChildren[i]:IsA("ManualWeld") or partChildren[i]:IsA("Rotate") then
return false
end
end
return true
end
]]
-- local debris = game:GetService("Debris")
local function flashRedBox()
errorBox.Parent = player.PlayerGui
if Data.Stamp.CurrentParts[1]:IsA "Tool" then
errorBox.Adornee = Data.Stamp.CurrentParts[1].Handle
else
errorBox.Adornee = Data.Stamp.CurrentParts[1]
end
delay(0, function()
for i = 1, 3 do
errorBox.Visible = true
wait(0.13)
errorBox.Visible = false
wait(0.13)
end
errorBox.Adornee = nil
errorBox.Parent = Tool
end)
end
-- below function should work as a Region3 query, returning true if a single cluster part is within this region
local function clusterPartsInRegion(startVector, endVector)
if not cluster then
return false
end
local startCell = cluster:WorldToCell(startVector)
local endCell = cluster:WorldToCell(endVector)
local startX = startCell.X
local startY = startCell.Y
local startZ = startCell.Z
local endX = endCell.X
local endY = endCell.Y
local endZ = endCell.Z
if startX < cluster.MaxExtents.Min.X then
startX = cluster.MaxExtents.Min.X
end
if startY < cluster.MaxExtents.Min.Y then
startY = cluster.MaxExtents.Min.Y
end
if startZ < cluster.MaxExtents.Min.Z then
startZ = cluster.MaxExtents.Min.Z
end
if endX > cluster.MaxExtents.Max.X then
endX = cluster.MaxExtents.Max.X
end
if endY > cluster.MaxExtents.Max.Y then
endY = cluster.MaxExtents.Max.Y
end
if endZ > cluster.MaxExtents.Max.Z then
endZ = cluster.MaxExtents.Max.Z
end
for x = startX, endX do
for y = startY, endY do
for z = startZ, endZ do
if cluster:GetCell(x, y, z).Value > 0 then
return true
end
end
end
end
return false
end
local function autoAlignToFace()
local aatf = Data.Stamp.CurrentParts[1]:FindFirstChild "AutoAlignToFace"
if aatf then
return aatf.Value
end
return false
end
local function getBoundingBox2(partOrModel)
-- for models, the bounding box is defined as the minimum and maximum individual part bounding boxes
-- relative to the first part's coordinate frame.
local minVec = Vector3.new(math.huge, math.huge, math.huge)
local maxVec = Vector3.new(-math.huge, -math.huge, -math.huge)
if
partOrModel:IsA "Part"
or partOrModel:IsA "WedgePart"
or partOrModel:IsA "CornerWedgePart"
or partOrModel:IsA "TrussPart"
then
minVec = -0.5 * partOrModel.Size
maxVec = -minVec
elseif partOrModel:IsA "Terrain" then
minVec = Vector3.new(-2, -2, -2)
maxVec = Vector3.new(2, 2, 2)
else
local part1 = partOrModel:GetChildren()[1]
if partOrModel:IsA "Tool" then
part1 = partOrModel.Handle
if not part1 then
return
end
end
if part1:IsA "Flag" then
part1 = partOrModel:FindFirstChild "Part"
if not part1 then
return
end
end
for i, object in pairs(partOrModel:GetChildren()) do
if
object:IsA "Part"
or object:IsA "WedgePart"
or object:IsA "CornerWedgePart"
or object:IsA "TrussPart"
then
boxMinInWorld =
object.CFrame:pointToWorldSpace(-0.5 * object.Size)
local boxMinInPart1 =
part1.CFrame:pointToObjectSpace(boxMinInWorld)
boxMaxInWorld =
object.CFrame:pointToWorldSpace(0.5 * object.Size)
local boxMaxInPart1 =
part1.CFrame:pointToObjectSpace(boxMaxInWorld)
local minX = minVec.x
local minY = minVec.y
local minZ = minVec.z
local maxX = maxVec.x
local maxY = maxVec.y
local maxZ = maxVec.z
if boxMinInPart1.x < minVec.x then
minX = boxMinInPart1.x
end
if boxMinInPart1.y < minVec.y then
minY = boxMinInPart1.y
end
if boxMinInPart1.z < minVec.z then
minZ = boxMinInPart1.z
end
if boxMaxInPart1.x < minX then
minX = boxMaxInPart1.x
end
if boxMaxInPart1.y < minY then
minY = boxMaxInPart1.y
end
if boxMaxInPart1.z < minZ then
minZ = boxMaxInPart1.z
end
if boxMinInPart1.x > maxVec.x then
maxX = boxMinInPart1.x
end
if boxMinInPart1.y > maxVec.y then
maxY = boxMinInPart1.y
end
if boxMinInPart1.z > maxVec.z then
maxZ = boxMinInPart1.z
end
if boxMaxInPart1.x > maxX then
maxX = boxMaxInPart1.x
end
if boxMaxInPart1.y > maxY then
maxY = boxMaxInPart1.y
end
if boxMaxInPart1.z > maxZ then
maxZ = boxMaxInPart1.z
end
minVec = Vector3.new(minX, minY, minZ)
maxVec = Vector3.new(maxX, maxY, maxZ)
end
end
end
-- Adjust bounding box to reflect what the model or part author wants in terms of justification
local justifyValue = partOrModel:FindFirstChild "Justification"
if justifyValue then
-- find the multiple of 4 that contains the model
local justify = justifyValue.Value
local two = Vector3.new(2, 2, 2)
local actualBox = maxVec - minVec - Vector3.new(0.01, 0.01, 0.01)
local containingGridBox = Vector3.new(
4 * math.ceil(actualBox.x / 4),
4 * math.ceil(actualBox.y / 4),
4 * math.ceil(actualBox.z / 4)
)
local adjustment = containingGridBox - actualBox
minVec = minVec - 0.5 * adjustment * justify
maxVec = maxVec + 0.5 * adjustment * (two - justify)
end
return minVec, maxVec
end
local function getTargetPartBoundingBox(targetPart)
if targetPart.Parent:FindFirstChild "RobloxModel" then
return getBoundingBox2(targetPart.Parent)
else
return getBoundingBox2(targetPart)
end
end
local function getClosestAlignedWorldDirection(aVector3InWorld)
local xDir = Vector3.new(1, 0, 0)
local yDir = Vector3.new(0, 1, 0)
local zDir = Vector3.new(0, 0, 1)
local xDot = aVector3InWorld.x * xDir.x
+ aVector3InWorld.y * xDir.y
+ aVector3InWorld.z * xDir.z
local yDot = aVector3InWorld.x * yDir.x
+ aVector3InWorld.y * yDir.y
+ aVector3InWorld.z * yDir.z
local zDot = aVector3InWorld.x * zDir.x
+ aVector3InWorld.y * zDir.y
+ aVector3InWorld.z * zDir.z
if math.abs(xDot) > math.abs(yDot) and math.abs(xDot) > math.abs(zDot) then
if xDot > 0 then
return 0
end
return 3
elseif
math.abs(yDot) > math.abs(xDot) and math.abs(yDot) > math.abs(zDot)
then
if yDot > 0 then
return 1
end
return 4
end
if zDot > 0 then
return 2
end
return 5
end
local function getMouseTargetCFrame(targetPart)
if not targetPart.Parent:FindFirstChild "RobloxModel" then
return targetPart.CFrame
end
if targetPart.Parent:IsA "Tool" then
return targetPart.Parent.Handle.CFrame
end
return targetPart.Parent:GetChildren()[1].CFrame
end
--[[
local function surfaceToVector(surf)
local vect = 1
if surf < 0 then
surf = surf * -1
vect = vect * -1
end
if surf == 1 then return vect*Vector3.new(1, 0, 0)
elseif surf == 2 then return vect*Vector3.new(0, 1, 0)
elseif surf == 3 then return vect*Vector3.new(0, 0, 1)
elseif Mouse then return Vector3.FromNormalId(Mouse.TargetSurface) end -- if we somehow got a "0", then we just revert to old behavior
return Vector3.new(0,0,0)
end
]]
local function getBoundingBoxInWorldCoordinates(partOrModel)
local minVec = Vector3.new(math.huge, math.huge, math.huge)
local maxVec = Vector3.new(-math.huge, -math.huge, -math.huge)
if partOrModel:IsA "BasePart" then
local vec1 =
partOrModel.CFrame:pointToWorldSpace(-0.5 * partOrModel.Size)
local vec2 =
partOrModel.CFrame:pointToWorldSpace(0.5 * partOrModel.Size)
minVec = Vector3.new(
math.min(vec1.X, vec2.X),
math.min(vec1.Y, vec2.Y),
math.min(vec1.Z, vec2.Z)
)
maxVec = Vector3.new(
math.max(vec1.X, vec2.X),
math.max(vec1.Y, vec2.Y),
math.max(vec1.Z, vec2.Z)
)
elseif partOrModel:IsA "Terrain" then
-- we shouldn't have to deal with this case
--minVec = Vector3.new(-2, -2, -2)
--maxVec = Vector3.new(2, 2, 2)
else
-- local part1 = partOrModel:GetChildren()[1]
for i, object in pairs(partOrModel:GetChildren()) do
if object:IsA "BasePart" then
boxMinInWorld =
object.CFrame:pointToWorldSpace(-0.5 * object.Size)
boxMaxInWorld =
object.CFrame:pointToWorldSpace(0.5 * object.Size)
local minX = minVec.x
local minY = minVec.y
local minZ = minVec.z
local maxX = maxVec.x
local maxY = maxVec.y
local maxZ = maxVec.z
if boxMinInWorld.x < minX then
minX = boxMinInWorld.x
end
if boxMinInWorld.y < minY then
minY = boxMinInWorld.y
end
if boxMinInWorld.z < minZ then
minZ = boxMinInWorld.z
end
if boxMaxInWorld.x < minX then
minX = boxMaxInWorld.x
end
if boxMaxInWorld.y < minY then
minY = boxMaxInWorld.y
end
if boxMaxInWorld.z < minZ then
minZ = boxMaxInWorld.z
end
if boxMinInWorld.x > maxX then
maxX = boxMinInWorld.x
end
if boxMinInWorld.y > maxY then
maxY = boxMinInWorld.y
end
if boxMinInWorld.z > maxZ then
maxZ = boxMinInWorld.z
end
if boxMaxInWorld.x > maxX then
maxX = boxMaxInWorld.x
end
if boxMaxInWorld.y > maxY then
maxY = boxMaxInWorld.y
end
if boxMaxInWorld.z > maxZ then
maxZ = boxMaxInWorld.z
end
minVec = Vector3.new(minX, minY, minZ)
maxVec = Vector3.new(maxX, maxY, maxZ)
end
end
end
return minVec, maxVec
end
local function checkPartLimit()
local numPoints = player.PointsUsed.Value
local maxPoints = player.MaxPoints.Value
return numPoints < maxPoints
end
local function findConfigAtMouseTarget(partsTable)
-- *Critical Assumption* :
-- This function assumes the target CF axes are orthogonal with the target bounding box faces
-- And, it assumes the insert CF axes are orthongonal with the insert bounding box faces
-- Therefore, insertion will not work with angled faces on wedges or other "non-block" parts, nor
-- will it work for parts in a model that are not orthogonally aligned with the model's CF.
local grid = 4.0
local admissibleConfig = false
local targetConfig = CFrame.new(0, 0, 0)
local minBB, maxBB = getBoundingBox2(Data.Stamp.CurrentParts[1])
local diagBB = maxBB - minBB
local insertCFrame
if
Data.Stamp.CurrentParts[1]:IsA "Model"
or Data.Stamp.CurrentParts[1]:IsA "Tool"
then
local i = 1
while
i < (#Data.Stamp.CurrentParts[1]:GetChildren())
and not Data.Stamp.CurrentParts[1]:GetChildren()[i]:IsA "Part"
and not Data.Stamp.CurrentParts[1]:GetChildren()[i]:IsA "TrussPart"
and not Data.Stamp.CurrentParts[1]:GetChildren()[i]:IsA "WedgePart"
and not Data.Stamp.CurrentParts[1]
:GetChildren()[i]
:IsA "CornerWedgePart"
do
i = i + 1
end
insertCFrame = Data.Stamp.CurrentParts[1]:GetChildren()[i].CFrame
else
insertCFrame = Data.Stamp.CurrentParts[1].CFrame
end
if not isRestricted and Mouse then
if Data.Stamp.CurrentParts[1]:IsA "Tool" then
Mouse.TargetFilter = Data.Stamp.CurrentParts[1].Handle
else
Mouse.TargetFilter = Data.Stamp.CurrentParts[1]
end
end
local targetPart = nil
local success = pcall(function()
targetPart = Mouse.Target
end)
if not success or targetPart == nil then
return admissibleConfig, targetConfig
end
-- test mouse hit location
local minBBTarget, maxBBTarget = getTargetPartBoundingBox(targetPart)
local diagBBTarget = maxBBTarget - minBBTarget
local targetCFrame = getMouseTargetCFrame(targetPart)
local hitCFrame = CFrame.new(0, 0, 0)
if Mouse then
hitCFrame = Mouse.Hit
end
local mouseHitInWorld = hitCFrame.p
-- find which axis of the insertion objects should match with the target surface
-- this should use targetPart CFrame, not the model CFrame
--[[ attempt at fixing Mouse.TargetSurface below...
local targetModel = targetPart
if not targetPart:FindFirstChild("RobloxModel") and targetPart.Parent and targetPart.Parent:FindFirstChild("RobloxModel") then targetModel = targetPart.Parent end
local correctedTargetSurfaceVector = surfaceToVector(modelTargetSurface(targetModel, workspace.CurrentCamera.CoordinateFrame.p, mouseHitInWorld))
local targetVectorInWorld = targetPart.CFrame:vectorToWorldSpace(correctedTargetSurfaceVector)
--]]
if targetPart:IsA "Terrain" then
if not cluster then
cluster = workspace.Terrain
end
local cellID = cluster:WorldToCellPreferSolid(mouseHitInWorld)
targetCFrame =
CFrame.new(cluster:CellCenterToWorld(cellID.x, cellID.y, cellID.z))
end
local mouseHitInTarget = targetCFrame:pointToObjectSpace(mouseHitInWorld)
local targetVectorInWorld = Vector3.new(0, 0, 0)
if Mouse then
-- DON'T WANT THIS IN TERMS OF THE MODEL CFRAME! (.TargetSurface is in terms of the part CFrame, so this would break, right? [HotThoth])
-- (ideally, we would want to make the Mouse.TargetSurface a model-targetsurface instead, but for testing will be using the converse)
--targetVectorInWorld = targetCFrame:vectorToWorldSpace(Vector3.FromNormalId(Mouse.TargetSurface))
targetVectorInWorld = targetPart.CFrame:vectorToWorldSpace(
Vector3.FromNormalId(Mouse.TargetSurface)
) -- better, but model cframe would be best
--[[
if targetPart.Parent:IsA "Model" then
local hitFace = modelTargetSurface(
targetPart.Parent,
Mouse.Hit.p,
workspace.CurrentCamera.CoordinateFrame.p
) -- best, if you get it right
local WORLD_AXES = {
Vector3.new(1, 0, 0),
Vector3.new(0, 1, 0),
Vector3.new(0, 0, 1),
}
if hitFace > 0 then
targetVectorInWorld =
targetCFrame:vectorToWorldSpace(WORLD_AXES[hitFace])
elseif hitFace < 0 then
targetVectorInWorld =
targetCFrame:vectorToWorldSpace(-WORLD_AXES[-hitFace])
end
end
]]
end
local targetRefPointInTarget, insertRefPointInInsert
local clampToSurface
if getClosestAlignedWorldDirection(targetVectorInWorld) == 0 then
targetRefPointInTarget =
targetCFrame:vectorToObjectSpace(Vector3.new(1, -1, 1))
insertRefPointInInsert =
insertCFrame:vectorToObjectSpace(Vector3.new(-1, -1, 1))
clampToSurface = Vector3.new(0, 1, 1)
elseif getClosestAlignedWorldDirection(targetVectorInWorld) == 3 then
targetRefPointInTarget =
targetCFrame:vectorToObjectSpace(Vector3.new(-1, -1, -1))
insertRefPointInInsert =
insertCFrame:vectorToObjectSpace(Vector3.new(1, -1, -1))
clampToSurface = Vector3.new(0, 1, 1)
elseif getClosestAlignedWorldDirection(targetVectorInWorld) == 1 then
targetRefPointInTarget =
targetCFrame:vectorToObjectSpace(Vector3.new(-1, 1, 1))
insertRefPointInInsert =
insertCFrame:vectorToObjectSpace(Vector3.new(-1, -1, 1))
clampToSurface = Vector3.new(1, 0, 1)
elseif getClosestAlignedWorldDirection(targetVectorInWorld) == 4 then
targetRefPointInTarget =
targetCFrame:vectorToObjectSpace(Vector3.new(-1, -1, 1))
insertRefPointInInsert =
insertCFrame:vectorToObjectSpace(Vector3.new(-1, 1, 1))
clampToSurface = Vector3.new(1, 0, 1)
elseif getClosestAlignedWorldDirection(targetVectorInWorld) == 2 then
targetRefPointInTarget =
targetCFrame:vectorToObjectSpace(Vector3.new(-1, -1, 1))
insertRefPointInInsert =
insertCFrame:vectorToObjectSpace(Vector3.new(-1, -1, -1))
clampToSurface = Vector3.new(1, 1, 0)
else
targetRefPointInTarget =
targetCFrame:vectorToObjectSpace(Vector3.new(1, -1, -1))
insertRefPointInInsert =
insertCFrame:vectorToObjectSpace(Vector3.new(1, -1, 1))
clampToSurface = Vector3.new(1, 1, 0)
end
targetRefPointInTarget = targetRefPointInTarget * (0.5 * diagBBTarget)
+ 0.5 * (maxBBTarget + minBBTarget)
insertRefPointInInsert = insertRefPointInInsert * (0.5 * diagBB)
+ 0.5 * (maxBB + minBB)
-- To Do: For cases that are not aligned to the world grid, account for the minimal rotation
-- needed to bring the Insert part(s) into alignment with the Target Part
-- Apply the rotation here
local delta = mouseHitInTarget - targetRefPointInTarget
local deltaClamped = Vector3.new(
grid * math.modf(delta.x / grid),
grid * math.modf(delta.y / grid),
grid * math.modf(delta.z / grid)
)
deltaClamped = deltaClamped * clampToSurface
local targetTouchInTarget = deltaClamped + targetRefPointInTarget
local TargetTouchRelToWorld =
targetCFrame:pointToWorldSpace(targetTouchInTarget)
local InsertTouchInWorld =
insertCFrame:vectorToWorldSpace(insertRefPointInInsert)
local posInsertOriginInWorld = TargetTouchRelToWorld - InsertTouchInWorld
local _, _, _, R00, R01, R02, R10, R11, R12, R20, R21, R22 =
insertCFrame:components()
targetConfig = CFrame.new(
posInsertOriginInWorld.x,
posInsertOriginInWorld.y,
posInsertOriginInWorld.z,
R00,
R01,
R02,
R10,
R11,
R12,
R20,
R21,
R22
)
admissibleConfig = true
return admissibleConfig,
targetConfig,
getClosestAlignedWorldDirection(targetVectorInWorld)
end
-- helper function for truncating to 45-degree angles on a 2D plane
local function truncateToCircleEighth(bigValue, littleValue)
local big = math.abs(bigValue)
local little = math.abs(littleValue)
local hypotenuse = math.sqrt(big * big + little * little)
local frac = little / hypotenuse
local bigSign = 1
local littleSign = 1
if bigValue < 0 then
bigSign = -1
end
if littleValue < 0 then
littleSign = -1
end
if frac > 0.382683432 then
-- between 22.5 and 45 degrees, so truncate to 45-degree tilt
return 0.707106781 * hypotenuse * bigSign,
0.707106781 * hypotenuse * littleSign
else
-- between 0 and 22.5 degrees, so truncate to 0-degree tilt
return hypotenuse * bigSign, 0
end
end
local function collectParts(object, baseParts, scripts, decals)
if object:IsA "BasePart" then
baseParts[#baseParts + 1] = object
elseif object:IsA "Script" then
scripts[#scripts + 1] = object
elseif object:IsA "Decal" then
decals[#decals + 1] = object
end
for index, child in pairs(object:GetChildren()) do
collectParts(child, baseParts, scripts, decals)
end
end
-- below is a helper function to help get the model surface instead of the part surface [for allowing a side to elect out of making joints automatically]
local function calcRayHitTime(rayStart, raySlope, intersectionPlane)
if math.abs(raySlope) < 0.01 then
return 0
end -- 0 slope --> we just say intersection time is 0, and sidestep this dimension
-- rayStart + t*raySlope = intersectionPlane, so t = (intersectionPlane - rayStart) / raySlope
return (intersectionPlane - rayStart) / raySlope
end
local function modelTargetSurface(partOrModel, rayStart, rayEnd)
if not partOrModel then
return 0
end
local modelCFrame = nil
local modelSize = nil
if partOrModel:IsA "Model" then
modelCFrame = partOrModel:GetModelCFrame()
modelSize = partOrModel:GetModelSize()
else
modelCFrame = partOrModel.CFrame
modelSize = partOrModel.Size
end
local mouseRayStart = modelCFrame:pointToObjectSpace(rayStart)
local mouseRayEnd = modelCFrame:pointToObjectSpace(rayEnd)
local mouseSlope = mouseRayEnd - mouseRayStart
local xPositive = 1
local yPositive = 1
local zPositive = 1
if mouseSlope.X > 0 then
xPositive = -1
end
if mouseSlope.Y > 0 then
yPositive = -1
end
if mouseSlope.Z > 0 then
zPositive = -1
end
-- find which surface the transformed mouse ray hits (using modelSize):
local xHitTime = calcRayHitTime(