-
Notifications
You must be signed in to change notification settings - Fork 60
/
advdupe2.lua
1908 lines (1642 loc) · 60 KB
/
advdupe2.lua
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
--[[
Title: Adv. Dupe 2 Tool
Desc: Defines the AD2 tool and assorted functionalities.
Author: TB
Version: 1.0
]]
TOOL.Category = "Construction"
TOOL.Name = "#Tool.advdupe2.name"
cleanup.Register( "AdvDupe2" )
require( "controlpanel" )
if(SERVER) then
CreateConVar("sbox_maxgmod_contr_spawners",5)
local phys_constraint_system_types = {
Weld = true,
Rope = true,
Elastic = true,
Slider = true,
Axis = true,
AdvBallsocket = true,
Motor = true,
Pulley = true,
Ballsocket = true,
Winch = true,
Hydraulic = true,
WireMotor = true,
WireHydraulic = true
}
--Orders constraints so that the dupe uses as little constraint systems as possible
local function GroupConstraintOrder( ply, constraints )
--First separate the nocollides, sorted, and unsorted constraints
local sorted, unsorted = {}, {}
for k, v in pairs(constraints) do
if phys_constraint_system_types[v.Type] then
sorted[#sorted+1] = v
else
unsorted[#unsorted+1] = v
end
end
local sortingSystems = {}
local fullSystems = {}
local function buildSystems(input)
while next(input) ~= nil do
for k, v in pairs(input) do
for systemi, system in pairs(sortingSystems) do
for _, target in pairs(system) do
for x = 1, 4 do
if v.Entity[x] then
for y = 1, 4 do
if target.Entity[y] and v.Entity[x].Index == target.Entity[y].Index then
system[#system + 1] = v
if #system == 100 then
fullSystems[#fullSystems + 1] = system
table.remove(sortingSystems, systemi)
end
input[k] = nil
goto super_loopbreak
end
end
end
end
end
end
end
--Normally skipped by the goto unless no cluster is found. If so, make a new one.
local k = next(input)
sortingSystems[#sortingSystems + 1] = {input[k]}
input[k] = nil
::super_loopbreak::
end
end
buildSystems(sorted)
local ret = {}
for _, system in pairs(fullSystems) do
for _, v in pairs(system) do
ret[#ret + 1] = v
end
end
for _, system in pairs(sortingSystems) do
for _, v in pairs(system) do
ret[#ret + 1] = v
end
end
for k, v in pairs(unsorted) do
ret[#ret + 1] = v
end
if #fullSystems ~= 0 then
ply:ChatPrint("DUPLICATOR: WARNING, Number of constraints exceeds 100: (".. #ret .."). Constraint sorting might not work as expected.")
end
return ret
end
local function CreationConstraintOrder( constraints )
local ret = {}
for k, v in pairs( constraints ) do
ret[#ret + 1] = k
end
table.sort(ret)
for i = 1, #ret do
ret[i] = constraints[ret[i]]
end
return ret
end
local function GetSortedConstraints( ply, constraints )
if ply:GetInfo("advdupe2_sort_constraints") ~= "0" then
return GroupConstraintOrder( ply, constraints )
else
return CreationConstraintOrder( constraints )
end
end
local areacopy_classblacklist = {
gmod_anchor = true
}
local function PlayerCanDupeCPPI(ply, ent)
if not AdvDupe2.duplicator.IsCopyable(ent) or areacopy_classblacklist[ent:GetClass()] then return false end
return ent:CPPIGetOwner()==ply
end
-- Code from WireLib.CanTool
local zero = Vector(0, 0, 0)
local norm = Vector(1, 0, 0)
local tr = { ---@type TraceResult
Hit = true, HitNonWorld = true, HitNoDraw = false, HitSky = false, AllSolid = true,
HitNormal = zero, Normal = norm,
Fraction = 1, FractionLeftSolid = 0,
HitBox = 0, HitGroup = 0, HitTexture = "**studio**",
MatType = 0, PhysicsBone = 0, SurfaceProps = 0, DispFlags = 0, Contents = 0,
Entity = NULL, HitPos = zero, StartPos = zero,
}
local function PlayerCanDupeTool(ply, ent)
if not AdvDupe2.duplicator.IsCopyable(ent) or areacopy_classblacklist[ent:GetClass()] then return false end
local pos = ent:GetPos()
tr.Entity, tr.HitPos, tr.StartPos = ent, pos, pos
return hook.Run( "CanTool", ply, tr, "advdupe2" ) ~= false
end
--Find all the entities in a box, given the adjacent corners and the player
local function FindInBox(min, max, ply)
local PPCheck = (tobool(ply:GetInfo("advdupe2_copy_only_mine")) and ply.CPPIGetOwner~=nil) and PlayerCanDupeCPPI or PlayerCanDupeTool
local EntTable = {}
for _, ent in ents.Iterator() do
local pos = ent:GetPos()
if (pos.X>=min.X) and (pos.X<=max.X) and
(pos.Y>=min.Y) and (pos.Y<=max.Y) and
(pos.Z>=min.Z) and (pos.Z<=max.Z) and PPCheck( ply, ent ) then
EntTable[ent:EntIndex()] = ent
end
end
return EntTable
end
--[[
Name: GetDupeAngleOffset
Desc: Retrieves duplication angle offsets from player
Returns: <angle> Created angle
]]
local function GetDupeAngleOffset(ply)
local p = math.Clamp(ply:GetInfoNum("advdupe2_offset_pitch", 0), -180, 180)
local y = math.Clamp(ply:GetInfoNum("advdupe2_offset_yaw" , 0), -180, 180)
local r = math.Clamp(ply:GetInfoNum("advdupe2_offset_roll" , 0), -180, 180)
return Angle(p, y, r)
end
--[[
Name: GetDupeElevation
Desc: Retrieves duplication Z elevation
Returns: <number> Dupe elevation
]]
local function GetDupeElevation(ply)
local con = ply:GetInfoNum("advdupe2_offset_z", 0)
local enz = (tonumber(ply.AdvDupe2.HeadEnt.Z) or 0)
return math.Clamp(con + enz, -32000, 32000)
end
--[[
Name: LeftClick
Desc: Defines the tool's behavior when the player left-clicks.
Params: <trace> trace
Returns: <boolean> success
]]
function TOOL:LeftClick( trace )
if(not trace) then return false end
local ply = self:GetOwner()
local dupe = ply.AdvDupe2
if not (dupe and dupe.Entities) then return false end
if(dupe.Pasting or dupe.Downloading) then
AdvDupe2.Notify(ply,"Advanced Duplicator 2 is busy.",NOTIFY_ERROR)
return false
end
dupe.Angle = GetDupeAngleOffset(ply)
dupe.Position = Vector(trace.HitPos)
dupe.Position.z = dupe.Position.z + GetDupeElevation(ply)
if(tobool(ply:GetInfo("advdupe2_offset_world"))) then
dupe.Angle = dupe.Angle - dupe.Entities[dupe.HeadEnt.Index].PhysicsObjects[0].Angle
end
dupe.Pasting = true
AdvDupe2.Notify(ply,"Pasting...")
local origin
if(tobool(ply:GetInfo("advdupe2_original_origin"))) then
origin = dupe.HeadEnt.Pos
end
AdvDupe2.InitPastingQueue(ply, dupe.Position, dupe.Angle, origin,
tobool(ply:GetInfo("advdupe2_paste_constraints")),
tobool(ply:GetInfo("advdupe2_paste_parents")),
tobool(ply:GetInfo("advdupe2_paste_disparents")),
tobool(ply:GetInfo("advdupe2_paste_protectoveride")))
return true
end
--[[
Name: RightClick
Desc: Defines the tool's behavior when the player right-clicks.
Params: <trace> trace
Returns: <boolean> success
]]
function TOOL:RightClick( trace )
local ply = self:GetOwner()
local dupe = ply.AdvDupe2
if not dupe then dupe = {}; ply.AdvDupe2 = dupe end
if(dupe.Pasting or dupe.Downloading) then
AdvDupe2.Notify(ply,"Advanced Duplicator 2 is busy.", NOTIFY_ERROR)
return false
end
--Set Area Copy on or off
if( ply:KeyDown(IN_SPEED) and not ply:KeyDown(IN_WALK) ) then
if(self:GetStage()==0) then
AdvDupe2.DrawSelectBox(ply)
self:SetStage(1)
return false
elseif(self:GetStage()==1) then
AdvDupe2.RemoveSelectBox(ply)
self:SetStage(0)
return false
end
end
if(not trace or not trace.Hit) then return false end
local Entities, Constraints, AddOne
local HeadEnt = {}
--If area copy is on
if(self:GetStage()==1) then
local area_size = math.Clamp(tonumber(ply:GetInfo("advdupe2_area_copy_size")) or 50, 0, 30720)
local Pos = trace.HitNonWorld and trace.Entity:GetPos() or trace.HitPos
local T = (Vector(area_size,area_size,area_size)+Pos)
local B = (Vector(-area_size,-area_size,-area_size)+Pos)
local Ents = FindInBox(B,T, ply)
local _, Ent = next(Ents)
if not Ent then
self:SetStage(0)
AdvDupe2.RemoveSelectBox(ply)
return true
end
Ent = trace.HitNonWorld and trace.Entity or Ent
HeadEnt.Index = Ent:EntIndex()
HeadEnt.Pos = Ent:GetPos()
Entities, Constraints = AdvDupe2.duplicator.AreaCopy(ply, Ents, HeadEnt.Pos, tobool(ply:GetInfo("advdupe2_copy_outside")))
self:SetStage(0)
AdvDupe2.RemoveSelectBox(ply)
elseif trace.HitNonWorld then --Area Copy is off
-- Filter duplicator blocked entities out.
if not AdvDupe2.duplicator.IsCopyable( trace.Entity ) then
return false
end
--If Alt is being held, add a prop to the dupe
if(ply:KeyDown(IN_WALK) and dupe.Entities~=nil and next(dupe.Entities)~=nil) then
Entities = dupe.Entities
Constraints = dupe.Constraints
HeadEnt = dupe.HeadEnt
AdvDupe2.duplicator.Copy( ply, trace.Entity, Entities, Constraints, HeadEnt.Pos)
--Only add the one ghost
AddOne = Entities[trace.Entity:EntIndex()]
else
Entities = {}
Constraints = {}
HeadEnt.Index = trace.Entity:EntIndex()
HeadEnt.Pos = trace.HitPos
AdvDupe2.duplicator.Copy( ply, trace.Entity, Entities, Constraints, trace.HitPos )
end
else --Non valid entity or clicked the world
if dupe.Entities then
--clear the dupe
net.Start("AdvDupe2_RemoveGhosts")
net.Send(ply)
dupe.Entities = nil
dupe.Constraints = nil
net.Start("AdvDupe2_ResetDupeInfo")
net.Send(ply)
AdvDupe2.ResetOffsets(ply)
return true
else
--select all owned props
Entities = {}
local PPCheck = (tobool(ply:GetInfo("advdupe2_copy_only_mine")) and CPPI~=nil) and PlayerCanDupeCPPI or PlayerCanDupeTool
for _, ent in ents.Iterator() do
if PPCheck( ply, ent ) then
Entities[ent:EntIndex()] = ent
end
end
local _, Ent = next(Entities)
if not Ent then
net.Start("AdvDupe2_RemoveGhosts")
net.Send(ply)
return true
end
HeadEnt.Index = Ent:EntIndex()
HeadEnt.Pos = Ent:GetPos()
Entities, Constraints = AdvDupe2.duplicator.AreaCopy(ply, Entities, HeadEnt.Pos, tobool(ply:GetInfo("advdupe2_copy_outside")))
end
end
if not HeadEnt.Z then
local WorldTrace = util.TraceLine({
mask = MASK_NPCWORLDSTATIC,
start = HeadEnt.Pos + Vector(0,0,1),
endpos = HeadEnt.Pos-Vector(0,0,50000)
})
HeadEnt.Z = WorldTrace.Hit and math.abs(HeadEnt.Pos.Z - WorldTrace.HitPos.Z) or 0
end
dupe.HeadEnt = HeadEnt
dupe.Entities = Entities
dupe.Constraints = GetSortedConstraints(ply, Constraints)
dupe.Revision = AdvDupe2.CodecRevision
net.Start("AdvDupe2_SetDupeInfo")
net.WriteString("")
net.WriteString(ply:Nick())
net.WriteString(os.date("%d %B %Y"))
net.WriteString(os.date("%I:%M %p"))
net.WriteString("")
net.WriteString("")
net.WriteString(table.Count(dupe.Entities))
net.WriteString(#dupe.Constraints)
net.Send(ply)
if AddOne then
AdvDupe2.SendGhost(ply, AddOne)
else
AdvDupe2.SendGhosts(ply)
end
AdvDupe2.ResetOffsets(ply)
return true
end
--Checks table, re-draws loading bar, and recreates ghosts when tool is pulled out
function TOOL:Deploy()
local ply = self:GetOwner()
local dupe = ply.AdvDupe2
if not dupe then dupe = {}; ply.AdvDupe2 = dupe end
if(not dupe.Entities) then return end
net.Start("AdvDupe2_StartGhosting")
net.Send(ply)
if(dupe.Queued) then
AdvDupe2.InitProgressBar(ply, "Queued: ")
return
end
if(dupe.Pasting) then
AdvDupe2.InitProgressBar(ply, "Pasting: ")
return
else
if(dupe.Uploading) then
AdvDupe2.InitProgressBar(ply, "Opening: ")
return
elseif(dupe.Downloading) then
AdvDupe2.InitProgressBar(ply, "Saving: ")
return
end
end
end
--Removes progress bar
function TOOL:Holster()
AdvDupe2.RemoveProgressBar(self:GetOwner())
end
--[[
Name: Reload
Desc: Creates an Advance Contraption Spawner.
Params: <trace> trace
Returns: <boolean> success
]]
function TOOL:Reload( trace )
if(not trace.Hit) then return false end
local ply = self:GetOwner()
local dupe = ply.AdvDupe2
if not dupe then dupe = {}; ply.AdvDupe2 = dupe end
if(self:GetStage()==1) then
local areasize = math.Clamp(tonumber(ply:GetInfo("advdupe2_area_copy_size")) or 50, 0, 30720)
net.Start("AdvDupe2_CanAutoSave")
net.WriteVector(trace.HitPos)
net.WriteFloat(areasize)
if(trace.Entity) then
net.WriteUInt(trace.Entity:EntIndex(), 16)
else
net.WriteUInt(0, 16)
end
net.Send(ply)
self:SetStage(0)
AdvDupe2.RemoveSelectBox(ply)
dupe.TempAutoSavePos = trace.HitPos
dupe.TempAutoSaveSize = areasize
dupe.TempAutoSaveOutSide = tobool(ply:GetInfo("advdupe2_copy_outside"))
return true
end
--If a contraption spawner was clicked then update it with the current settings
if(trace.Entity:GetClass()=="gmod_contr_spawner") then
local delay = tonumber(ply:GetInfo("advdupe2_contr_spawner_delay"))
local undo_delay = tonumber(ply:GetInfo("advdupe2_contr_spawner_undo_delay"))
local min
local max
if(not delay) then
delay = tonumber(GetConVarString("AdvDupe2_MinContraptionSpawnDelay")) or 0.2
else
if(not game.SinglePlayer()) then
min = tonumber(GetConVarString("AdvDupe2_MinContraptionSpawnDelay")) or 0.2
if (delay < min) then
delay = min
end
elseif(delay<0) then
delay = 0
end
end
if(not undo_delay) then
undo_delay = tonumber(GetConVarString("AdvDupe2_MinContraptionUndoDelay"))
else
if(not game.SinglePlayer()) then
min = tonumber(GetConVarString("AdvDupe2_MinContraptionUndoDelay")) or 0.1
max = tonumber(GetConVarString("AdvDupe2_MaxContraptionUndoDelay")) or 60
if(undo_delay < min) then
undo_delay = min
elseif(undo_delay > max) then
undo_delay = max
end
elseif(undo_delay < 0) then
undo_delay = 0
end
end
trace.Entity:GetTable():SetOptions(
ply, delay, undo_delay,
tonumber(ply:GetInfo("advdupe2_contr_spawner_key")),
tonumber(ply:GetInfo("advdupe2_contr_spawner_undo_key")),
tonumber(ply:GetInfo("advdupe2_contr_spawner_disgrav")) or 0,
tonumber(ply:GetInfo("advdupe2_contr_spawner_disdrag")) or 0,
tonumber(ply:GetInfo("advdupe2_contr_spawner_addvel")) or 1 )
return true
end
--Create a contraption spawner
if dupe and dupe.Entities then
local headent = dupe.Entities[dupe.HeadEnt.Index]
local Pos, Ang
if(headent) then
if(tobool(ply:GetInfo("advdupe2_original_origin"))) then
Pos = dupe.HeadEnt.Pos + headent.PhysicsObjects[0].Pos
Ang = headent.PhysicsObjects[0].Angle
else
local EntAngle = headent.PhysicsObjects[0].Angle
if(tobool(ply:GetInfo("advdupe2_offset_world"))) then EntAngle = Angle(0,0,0) end
trace.HitPos.Z = trace.HitPos.Z + GetDupeElevation(ply)
Pos, Ang = LocalToWorld(headent.PhysicsObjects[0].Pos, EntAngle, trace.HitPos, GetDupeAngleOffset(ply))
end
else
AdvDupe2.Notify(ply, "Invalid head entity to spawn contraption spawner.")
return false
end
if(headent.Class=="gmod_contr_spawner") then
AdvDupe2.Notify(ply, "Cannot make a contraption spawner from a contraption spawner.")
return false
end
local spawner = MakeContraptionSpawner(
ply, Pos, Ang, dupe.HeadEnt.Index,
table.Copy(dupe.Entities),
table.Copy(dupe.Constraints),
tonumber(ply:GetInfo("advdupe2_contr_spawner_delay")),
tonumber(ply:GetInfo("advdupe2_contr_spawner_undo_delay")), headent.Model,
tonumber(ply:GetInfo("advdupe2_contr_spawner_key")),
tonumber(ply:GetInfo("advdupe2_contr_spawner_undo_key")),
tonumber(ply:GetInfo("advdupe2_contr_spawner_disgrav")) or 0,
tonumber(ply:GetInfo("advdupe2_contr_spawner_disdrag")) or 0,
tonumber(ply:GetInfo("advdupe2_contr_spawner_addvel")) or 1,
tonumber(ply:GetInfo("advdupe2_contr_spawner_hideprops")) or 0)
ply:AddCleanup( "AdvDupe2", spawner )
undo.Create("gmod_contr_spawner")
undo.AddEntity( spawner )
undo.SetPlayer( ply )
undo.Finish()
return true
end
end
--Called to clean up the tool when pasting is finished or undo during pasting
function AdvDupe2.FinishPasting(Player, Paste)
Player.AdvDupe2.Pasting=false
AdvDupe2.RemoveProgressBar(Player)
if(Paste) then AdvDupe2.Notify(Player,"Finished Pasting!") end
end
--function for creating a contraption spawner
function MakeContraptionSpawner( ply, Pos, Ang, HeadEnt, EntityTable, ConstraintTable, delay, undo_delay, model, key, undo_key, disgrav, disdrag, addvel, hideprops)
if not ply:CheckLimit("gmod_contr_spawners") then return nil end
if(not game.SinglePlayer()) then
if(table.Count(EntityTable)>tonumber(GetConVarString("AdvDupe2_MaxContraptionEntities"))) then
AdvDupe2.Notify(ply,"Contraption Spawner exceeds the maximum amount of "..GetConVarString("AdvDupe2_MaxContraptionEntities").." entities for a spawner!",NOTIFY_ERROR)
return false
end
if(#ConstraintTable>tonumber(GetConVarString("AdvDupe2_MaxContraptionConstraints"))) then
AdvDupe2.Notify(ply,"Contraption Spawner exceeds the maximum amount of "..GetConVarString("AdvDupe2_MaxContraptionConstraints").." constraints for a spawner!",NOTIFY_ERROR)
return false
end
end
local spawner = ents.Create("gmod_contr_spawner")
if not IsValid(spawner) then return end
spawner:SetPos(Pos)
spawner:SetAngles(Ang)
spawner:SetModel(model)
spawner:SetRenderMode(RENDERMODE_TRANSALPHA)
spawner:SetCreator(ply)
spawner:Spawn()
duplicator.ApplyEntityModifiers(ply, spawner)
if IsValid(spawner:GetPhysicsObject()) then
spawner:GetPhysicsObject():EnableMotion(false)
end
local min
local max
if(not delay) then
delay = tonumber(GetConVarString("AdvDupe2_MinContraptionSpawnDelay")) or 0.2
else
if(not game.SinglePlayer()) then
min = tonumber(GetConVarString("AdvDupe2_MinContraptionSpawnDelay")) or 0.2
if (delay < min) then
delay = min
end
elseif(delay<0) then
delay = 0
end
end
if(not undo_delay) then
undo_delay = tonumber(GetConVarString("AdvDupe2_MinContraptionUndoDelay"))
else
if(not game.SinglePlayer()) then
min = tonumber(GetConVarString("AdvDupe2_MinContraptionUndoDelay")) or 0.1
max = tonumber(GetConVarString("AdvDupe2_MaxContraptionUndoDelay")) or 60
if(undo_delay < min) then
undo_delay = min
elseif(undo_delay > max) then
undo_delay = max
end
elseif(undo_delay < 0) then
undo_delay = 0
end
end
-- Set options
spawner:SetPlayer(ply)
spawner:GetTable():SetOptions(ply, delay, undo_delay, key, undo_key, disgrav, disdrag, addvel, hideprops)
local tbl = {
ply = ply,
delay = delay,
undo_delay = undo_delay,
disgrav = disgrav,
disdrag = disdrag,
addvel = addvel,
hideprops = hideprops
}
table.Merge(spawner:GetTable(), tbl)
spawner:SetDupeInfo(HeadEnt, EntityTable, ConstraintTable)
spawner:AddGhosts(ply)
ply:AddCount("gmod_contr_spawners", spawner)
ply:AddCleanup("gmod_contr_spawner", spawner)
return spawner
end
duplicator.RegisterEntityClass("gmod_contr_spawner", MakeContraptionSpawner,
"Pos", "Ang", "HeadEnt", "EntityTable", "ConstraintTable", "delay",
"undo_delay", "model", "key", "undo_key", "disgrav", "disdrag", "addvel", "hideprops")
function AdvDupe2.InitProgressBar(ply,label)
net.Start("AdvDupe2_InitProgressBar")
net.WriteString(label)
net.Send(ply)
end
function AdvDupe2.DrawSelectBox(ply)
net.Start("AdvDupe2_DrawSelectBox")
net.Send(ply)
end
function AdvDupe2.RemoveSelectBox(ply)
net.Start("AdvDupe2_RemoveSelectBox")
net.Send(ply)
end
function AdvDupe2.UpdateProgressBar(ply,percent)
net.Start("AdvDupe2_UpdateProgressBar")
net.WriteFloat(percent)
net.Send(ply)
end
function AdvDupe2.RemoveProgressBar(ply)
net.Start("AdvDupe2_RemoveProgressBar")
net.Send(ply)
end
--Reset the offsets of height, pitch, yaw, and roll back to default
function AdvDupe2.ResetOffsets(ply, keep)
if(not keep) then
ply.AdvDupe2.Name = nil
end
net.Start("AdvDupe2_ResetOffsets")
net.Send(ply)
end
net.Receive("AdvDupe2_CanAutoSave", function(len, ply, len2)
local desc = net.ReadString()
local ent = net.ReadInt(16)
local dupe = ply.AdvDupe2
if(ent~=0) then
dupe.AutoSaveEnt = ent
if(ply:GetInfo("advdupe2_auto_save_contraption")=="1") then
dupe.AutoSaveEnt = ents.GetByIndex( dupe.AutoSaveEnt )
end
else
if(ply:GetInfo("advdupe2_auto_save_contraption")=="1") then
AdvDupe2.Notify(ply, "No entity selected to auto save contraption.", NOTIFY_ERROR)
return
end
dupe.AutoSaveEnt = nil
end
dupe.AutoSavePos = dupe.TempAutoSavePos
dupe.AutoSaveSize = dupe.TempAutoSaveSize
dupe.AutoSaveOutSide = dupe.TempAutoSaveOutSide
dupe.AutoSaveContr = ply:GetInfo("advdupe2_auto_save_contraption")=="1"
dupe.AutoSaveDesc = desc
local time = math.Clamp(tonumber(ply:GetInfo("advdupe2_auto_save_time")) or 2, 2, 30)
if(game.SinglePlayer()) then
dupe.AutoSavePath = net.ReadString()
end
AdvDupe2.Notify(ply, "Your area will be auto saved every "..(time*60).." seconds.")
local name = "AdvDupe2_AutoSave_"..ply:UniqueID()
if(timer.Exists(name)) then
timer.Adjust(name, time*60, 0)
return
end
timer.Create(name, time*60, 0, function()
if(not IsValid(ply)) then
timer.Remove(name)
return
end
local dupe = ply.AdvDupe2
if(dupe.Downloading) then
AdvDupe2.Notify(ply, "Skipping auto save, tool is busy.", NOTIFY_ERROR)
return
end
local Tab = {Entities={}, Constraints={}, HeadEnt={}}
if(dupe.AutoSaveContr) then
if(not IsValid(dupe.AutoSaveEnt)) then
timer.Remove(name)
AdvDupe2.Notify(ply, "Head entity for auto save no longer valid; stopping auto save.", NOTIFY_ERROR)
return
end
Tab.HeadEnt.Index = dupe.AutoSaveEnt:EntIndex()
Tab.HeadEnt.Pos = dupe.AutoSaveEnt:GetPos()
local WorldTrace = util.TraceLine({
mask = MASK_NPCWORLDSTATIC,
start = Tab.HeadEnt.Pos + Vector(0,0,1),
endpos = Tab.HeadEnt.Pos - Vector(0,0,50000)
})
Tab.HeadEnt.Z = WorldTrace.Hit and math.abs(Tab.HeadEnt.Pos.Z - WorldTrace.HitPos.Z) or 0
AdvDupe2.duplicator.Copy( ply, dupe.AutoSaveEnt, Tab.Entities, Tab.Constraints, Tab.HeadEnt.Pos )
else
local i = dupe.AutoSaveSize
local Pos = dupe.AutoSavePos
local T = Vector( i, i, i); T:Add(Pos)
local B = Vector(-i,-i,-i); B:Add(Pos)
local Entities = FindInBox(B,T, ply)
local _, HeadEnt = next(Entities)
if not HeadEnt then
AdvDupe2.Notify(ply, "Area Auto Save copied 0 entities; be sure to turn it off.", NOTIFY_ERROR)
return
end
if(dupe.AutoSaveEnt and Entities[dupe.AutoSaveEnt]) then
Tab.HeadEnt.Index = dupe.AutoSaveEnt
else
Tab.HeadEnt.Index = HeadEnt:EntIndex()
end
Tab.HeadEnt.Pos = HeadEnt:GetPos()
local WorldTrace = util.TraceLine({
mask = MASK_NPCWORLDSTATIC,
start = Tab.HeadEnt.Pos + Vector(0,0,1),
endpos = Tab.HeadEnt.Pos - Vector(0,0,50000)
})
Tab.HeadEnt.Z = WorldTrace.Hit and math.abs(Tab.HeadEnt.Pos.Z - WorldTrace.HitPos.Z) or 0
Tab.Entities, Tab.Constraints = AdvDupe2.duplicator.AreaCopy(ply, Entities, Tab.HeadEnt.Pos, dupe.AutoSaveOutSide)
end
Tab.Constraints = GetSortedConstraints(ply, Tab.Constraints)
Tab.Description = dupe.AutoSaveDesc
AdvDupe2.Encode( Tab, AdvDupe2.GenerateDupeStamp(ply), function(data)
AdvDupe2.SendToClient(ply, data, 1)
end)
dupe.FileMod = CurTime()+tonumber(GetConVarString("AdvDupe2_FileModificationDelay"))
end)
timer.Start(name)
end)
concommand.Add("AdvDupe2_SetStage", function(ply, cmd, args)
ply:GetTool("advdupe2"):SetStage(1)
end)
concommand.Add("AdvDupe2_RemoveAutoSave", function(ply, cmd, args)
timer.Remove("AdvDupe2_AutoSave_"..ply:UniqueID())
end)
concommand.Add("AdvDupe2_SaveMap", function(ply, cmd, args)
if(not ply:IsAdmin()) then
AdvDupe2.Notify(ply, "You do not have permission to this function.", NOTIFY_ERROR)
return
end
local Entities = {}
for _, v in ents.Iterator() do
if not v:CreatedByMap() and AdvDupe2.duplicator.IsCopyable(v) then
Entities[v:EntIndex()] = v
end
end
local _, HeadEnt = next(Entities)
if not HeadEnt then return end
local Tab = {Entities={}, Constraints={}, HeadEnt={}, Description=""}
Tab.HeadEnt.Index = HeadEnt:EntIndex()
Tab.HeadEnt.Pos = HeadEnt:GetPos()
local WorldTrace = util.TraceLine({
mask = MASK_NPCWORLDSTATIC,
start = Tab.HeadEnt.Pos + Vector(0,0,1),
endpos = Tab.HeadEnt.Pos - Vector(0,0,50000)
})
Tab.HeadEnt.Z = WorldTrace.Hit and math.abs(Tab.HeadEnt.Pos.Z - WorldTrace.HitPos.Z) or 0
Tab.Entities, Tab.Constraints = AdvDupe2.duplicator.AreaCopy(ply, Entities, Tab.HeadEnt.Pos, true)
Tab.Constraints = GetSortedConstraints(ply, Tab.Constraints)
Tab.Map = true
AdvDupe2.Encode( Tab, AdvDupe2.GenerateDupeStamp(ply), function(data)
if #data > AdvDupe2.MaxDupeSize then
AdvDupe2.Notify(ply, "Copied duplicator filesize is too big!",NOTIFY_ERROR)
return
end
if(not file.IsDir("advdupe2_maps", "DATA")) then
file.CreateDir("advdupe2_maps")
end
file.Write("advdupe2_maps/"..args[1]..".txt", data)
AdvDupe2.Notify(ply, "Map save, saved successfully.")
end)
end)
end
if(CLIENT) then
function TOOL:LeftClick(trace)
if(trace and AdvDupe2.HeadGhost) then
return true
end
return false
end
function TOOL:RightClick(trace)
if( self:GetOwner():KeyDown(IN_SPEED) and not self:GetOwner():KeyDown(IN_WALK) ) then
return false
end
return true
end
--Removes progress bar and removes ghosts when tool is put away
function TOOL:ReleaseGhostEntity()
AdvDupe2.RemoveGhosts()
AdvDupe2.RemoveSelectBox()
if(AdvDupe2.Rotation) then
hook.Remove("PlayerBindPress", "AdvDupe2_BindPress")
hook.Remove("CreateMove", "AdvDupe2_MouseControl")
end
return
end
function TOOL:Reload( trace )
if(trace and (AdvDupe2.HeadGhost or self:GetStage() == 1)) then
return true
end
return false
end
--Take control of the mouse wheel bind so the player can modify the height of the dupe
local function MouseWheelScrolled(ply, bind, pressed)
if(bind == "invprev") then
if(ply:GetTool("advdupe2"):GetStage() == 1) then
local size = math.min(tonumber(ply:GetInfo("advdupe2_area_copy_size")) + 25, 30720)
RunConsoleCommand("advdupe2_area_copy_size",size)
else
local Z = tonumber(ply:GetInfo("advdupe2_offset_z")) + 5
RunConsoleCommand("advdupe2_offset_z",Z)
end
return true
elseif(bind == "invnext") then
if(ply:GetTool("advdupe2"):GetStage() == 1) then
local size = math.max(tonumber(ply:GetInfo("advdupe2_area_copy_size")) - 25, 25)
RunConsoleCommand("advdupe2_area_copy_size",size)
else
local Z = tonumber(ply:GetInfo("advdupe2_offset_z")) - 5
RunConsoleCommand("advdupe2_offset_z",Z)
end
return true
end
GAMEMODE:PlayerBindPress(ply, bind, pressed)
end
local YawTo = 0
local BsAng = Angle()
local function GetRotationSign(ply)
local VY = tonumber(ply:GetInfo("advdupe2_offset_yaw")) or 0
BsAng:Zero(); BsAng:RotateAroundAxis(BsAng:Up(), VY)
local PR = ply:GetRight()
local DP = BsAng:Right():Dot(PR)
local DR = BsAng:Forward():Dot(PR)
if(math.abs(DR) > math.abs(DP)) then -- Roll priority
if(DR >= 0) then return -1, 1 else return 1, -1 end
else -- Pitch axis takes priority. Normal X-Y map
if(DP >= 0) then return 1, 1 else return -1, -1 end
end
end
local function MouseControl( cmd )
local ply = LocalPlayer()
local X = cmd:GetMouseX() / 20
local Y = -cmd:GetMouseY() / 20
local ru = ply:KeyDown(IN_SPEED)
local mm = input.IsMouseDown(MOUSE_MIDDLE)
if(mm) then
if(ru) then
YawTo = 0 -- Reset total integrated yaw
RunConsoleCommand("advdupe2_offset_pitch", 0)
RunConsoleCommand("advdupe2_offset_yaw" , 0)
RunConsoleCommand("advdupe2_offset_roll" , 0)
else
if(Y ~= 0) then
local VR = tonumber(ply:GetInfo("advdupe2_offset_roll")) or 0
local VP = tonumber(ply:GetInfo("advdupe2_offset_pitch")) or 0
local SP, SR, P, R = GetRotationSign(ply)
if(SP ~= SR) then
P = math.NormalizeAngle(VP + X * SR)
R = math.NormalizeAngle(VR + Y * SP)
else
P = math.NormalizeAngle(VP + Y * SP)
R = math.NormalizeAngle(VR + X * SR)
end
RunConsoleCommand("advdupe2_offset_pitch", P)
RunConsoleCommand("advdupe2_offset_roll" , R)
end
end
else
if(X ~= 0) then
VY = tonumber(ply:GetInfo("advdupe2_offset_yaw")) or 0
if(ru) then
YawTo = YawTo + X -- Integrate the mouse on the X value from the mouse
RunConsoleCommand("advdupe2_offset_yaw", math.SnapTo(math.NormalizeAngle(YawTo), 45))
else
YawTo = VY + X -- Update the last yaw with the current value from the mouse
RunConsoleCommand("advdupe2_offset_yaw", math.NormalizeAngle(YawTo))
end
end
end
end
--Checks binds to modify dupes position and angles
function TOOL:Think()
if AdvDupe2.HeadGhost then
AdvDupe2.UpdateGhosts()
end
if(LocalPlayer():KeyDown(IN_USE)) then
if(not AdvDupe2.Rotation) then
hook.Add("PlayerBindPress", "AdvDupe2_BindPress", MouseWheelScrolled)
hook.Add("CreateMove", "AdvDupe2_MouseControl", MouseControl)
AdvDupe2.Rotation = true
end
else
if(AdvDupe2.Rotation) then
AdvDupe2.Rotation = false
hook.Remove("PlayerBindPress", "AdvDupe2_BindPress")
hook.Remove("CreateMove", "AdvDupe2_MouseControl")
end
end
end
--Hinder the player from looking to modify offsets with the mouse
function TOOL:FreezeMovement()
return AdvDupe2.Rotation
end
language.Add( "Tool.advdupe2.name", "Advanced Duplicator 2" )
language.Add( "Tool.advdupe2.desc", "Duplicate things." )
language.Add( "Tool.advdupe2.0", "Primary: Paste, Secondary: Copy, Secondary+World: Select/Deselect All, Secondary+Shift: Area copy." )
language.Add( "Tool.advdupe2.1", "Primary: Paste, Secondary: Copy an area, Reload: Autosave an area, Secondary+Shift: Cancel." )