· 7 years ago · Oct 22, 2018, 07:06 AM
1SWEP.PrintName = "Ðртефакт 'КомпаÑ'"
2SWEP.Author = "McVipeR"
3SWEP.Slot = 0
4SWEP.SlotPos = 5
5SWEP.Description = ""
6SWEP.Instructions = "ЛКМ - телепортирует в Ñлучайное меÑто на карте"
7SWEP.Contact = ""
8SWEP.Purpose = "Телепортирует на рандомную базу на карте"
9
10SWEP.Spawnable = true
11SWEP.AdminOnly = false
12SWEP.Category = "StalkerRP Sweps"
13
14SWEP.UseHands = false
15
16SWEP.Primary.Recoil = 0
17SWEP.Primary.ClipSize = -1
18SWEP.Primary.DefaultClip = -1
19SWEP.Primary.Automatic = true
20SWEP.Primary.Delay = 0
21SWEP.Primary.Ammo = "none"
22
23SWEP.Secondary.Recoil = 0
24SWEP.Secondary.ClipSize = -1
25SWEP.Secondary.DefaultClip = -1
26SWEP.Secondary.Automatic = false
27SWEP.Secondary.Delay = 0
28SWEP.Secondary.Ammo = "none"
29
30SWEP.checkTime = 0
31SWEP.HoldType = "magic"
32SWEP.ViewModelFOV = 70
33SWEP.ViewModelFlip = false
34SWEP.ViewModel = "models/weapons/v_grenade.mdl";
35SWEP.WorldModel = "models/weapons/w_grenade.mdl";
36
37SWEP.ShowViewModel = false
38SWEP.ShowWorldModel = false
39
40SWEP.IronSightsPos = Vector(2.009, 0, 0)
41SWEP.IronSightsAng = Vector(0, 0, 0)
42
43SWEP.ViewModelBoneMods = {
44 ["ValveBiped.Grenade_body"] = { scale = Vector(0.009, 0.009, 0.009), pos = Vector(0, 0, 0), angle = Angle(0, 0, 0) }
45}
46
47SWEP.VElements = {
48 ["art"] = { type = "Model", model = "models/predatorcz/stalker/artifacts/compass.mdl", bone = "ValveBiped.Grenade_body", rel = "", pos = Vector(1, 1, 0), angle = Angle(180, 50, -15), size = Vector(0.4, 0.4, 0.4), color = Color(255, 255, 255, 255), surpresslightning = false, material = "", skin = 0, bodygroup = {} }
49}
50
51SWEP.WElements = {
52 ["art"] = { type = "Model", model = "models/predatorcz/stalker/artifacts/compass.mdl", bone = "ValveBiped.Bip01_R_Hand", rel = "", pos = Vector(8, 1, 0), angle = Angle(-100, -150, 0), size = Vector(0.5, 0.5, 0.5), color = Color(255, 255, 255, 255), surpresslightning = false, material = "", skin = 0, bodygroup = {} }
53}
54
55function SWEP:PrimaryAttack()
56 self:CompasActivate()
57end
58
59function SWEP:CompasActivate()
60
61 TeleportCompasLocations =
62 {
63 -- Кордон
64 Vector( -2534.234863, -11462.250000, -602.621826 ),
65 -- База долга
66 Vector( -1796.848022, -2781.859863, 56.031250 ),
67 -- База Ñвободы
68 Vector( 632.762207, 5628.930664, -146.514191 ),
69 -- База бандитов
70 Vector( 10730.531250, 12220.626953, -360.601135 ),
71 -- База военных
72 Vector( 8078.980957, 2125.542725, -623.622620 ),
73 -- База наемников
74 Vector( -11248.459961, -10883.117188, -575.968750 ),
75 -- База ЧÐ
76 Vector( -8844.831055, 5211.614258, -191.968750 ),
77 -- Заброшенное здание
78 Vector( 9600.199219, -10429.655273, -250 ),
79 -- Ðедалко от ОазиÑа
80 Vector( 8146.495605, -2045.329590, -191.376755 ),
81 -- Лаба
82 Vector( 633.457153, -1645.199341, -735 )
83 }
84
85 timer.Create( self.Owner:UniqueID() .. "_KompasTeleportTimer", 1.5, 1, function()
86
87 if !IsValid(self.Owner) then return end
88 if self.Owner:InVehicle() then return end
89
90 if SERVER then
91 self.Owner:SetPos( table.Random( TeleportCompasLocations ) )
92 self.Owner:EmitSound("blowout/hit3.wav", 70, 100)
93 end
94
95 local SparkEffect = EffectData()
96 SparkEffect:SetOrigin( self.Owner:GetPos() )
97 SparkEffect:SetMagnitude(1)
98 SparkEffect:SetScale(1)
99 SparkEffect:SetRadius(1)
100 util.Effect("effects/tool_tracer", SparkEffect)
101
102 end)
103
104end
105
106function SWEP:Think()
107 return
108end
109
110function SWEP:Initialize()
111
112 self:SetHoldType( self.HoldType )
113
114 // other initialize code goes here
115
116 if CLIENT then
117
118 // Create a new table for every weapon instance
119 self.VElements = table.FullCopy( self.VElements )
120 self.WElements = table.FullCopy( self.WElements )
121 self.ViewModelBoneMods = table.FullCopy( self.ViewModelBoneMods )
122
123 self:CreateModels(self.VElements) // create viewmodels
124 self:CreateModels(self.WElements) // create worldmodels
125
126 // init view model bone build function
127 if IsValid(self.Owner) then
128 local vm = self.Owner:GetViewModel()
129 if IsValid(vm) then
130 self:ResetBonePositions(vm)
131
132 // Init viewmodel visibility
133 if (self.ShowViewModel == nil or self.ShowViewModel) then
134 vm:SetColor(Color(255,255,255,255))
135 else
136 // we set the alpha to 1 instead of 0 because else ViewModelDrawn stops being called
137 vm:SetColor(Color(255,255,255,1))
138 // ^ stopped working in GMod 13 because you have to do Entity:SetRenderMode(1) for translucency to kick in
139 // however for some reason the view model resets to render mode 0 every frame so we just apply a debug material to prevent it from drawing
140 vm:SetMaterial("Debug/hsv")
141 end
142 end
143 end
144
145 end
146
147end
148
149function SWEP:Holster()
150
151 if CLIENT and IsValid(self.Owner) then
152 local vm = self.Owner:GetViewModel()
153 if IsValid(vm) then
154 self:ResetBonePositions(vm)
155 end
156 end
157
158 return true
159end
160
161function SWEP:OnRemove()
162 self:Holster()
163end
164
165if CLIENT then
166
167 SWEP.vRenderOrder = nil
168 function SWEP:ViewModelDrawn()
169
170 local vm = self.Owner:GetViewModel()
171 if !IsValid(vm) then return end
172
173 if (!self.VElements) then return end
174
175 self:UpdateBonePositions(vm)
176
177 if (!self.vRenderOrder) then
178
179 // we build a render order because sprites need to be drawn after models
180 self.vRenderOrder = {}
181
182 for k, v in pairs( self.VElements ) do
183 if (v.type == "Model") then
184 table.insert(self.vRenderOrder, 1, k)
185 elseif (v.type == "Sprite" or v.type == "Quad") then
186 table.insert(self.vRenderOrder, k)
187 end
188 end
189
190 end
191for k, name in ipairs( self.vRenderOrder ) do
192
193 local v = self.VElements[name]
194 if (!v) then self.vRenderOrder = nil break end
195 if (v.hide) then continue end
196
197 local model = v.modelEnt
198 local sprite = v.spriteMaterial
199
200 if (!v.bone) then continue end
201
202 local pos, ang = self:GetBoneOrientation( self.VElements, v, vm )
203
204 if (!pos) then continue end
205
206 if (v.type == "Model" and IsValid(model)) then
207
208 model:SetPos(pos + ang:Forward() * v.pos.x + ang:Right() * v.pos.y + ang:Up() * v.pos.z )
209 ang:RotateAroundAxis(ang:Up(), v.angle.y)
210 ang:RotateAroundAxis(ang:Right(), v.angle.p)
211 ang:RotateAroundAxis(ang:Forward(), v.angle.r)
212
213 model:SetAngles(ang)
214 //model:SetModelScale(v.size)
215 local matrix = Matrix()
216 matrix:Scale(v.size)
217 model:EnableMatrix( "RenderMultiply", matrix )
218
219 if (v.material == "") then
220 model:SetMaterial("")
221 elseif (model:GetMaterial() != v.material) then
222 model:SetMaterial( v.material )
223 end
224
225 if (v.skin and v.skin != model:GetSkin()) then
226 model:SetSkin(v.skin)
227 end
228
229 if (v.bodygroup) then
230 for k, v in pairs( v.bodygroup ) do
231 if (model:GetBodygroup(k) != v) then
232 model:SetBodygroup(k, v)
233 end
234 end
235 end
236
237 if (v.surpresslightning) then
238 render.SuppressEngineLighting(true)
239 end
240
241 render.SetColorModulation(v.color.r/255, v.color.g/255, v.color.b/255)
242 render.SetBlend(v.color.a/255)
243 model:DrawModel()
244 render.SetBlend(1)
245 render.SetColorModulation(1, 1, 1)
246
247 if (v.surpresslightning) then
248 render.SuppressEngineLighting(false)
249 end
250
251 elseif (v.type == "Sprite" and sprite) then
252
253 local drawpos = pos + ang:Forward() * v.pos.x + ang:Right() * v.pos.y + ang:Up() * v.pos.z
254 render.SetMaterial(sprite)
255 render.DrawSprite(drawpos, v.size.x, v.size.y, v.color)
256
257 elseif (v.type == "Quad" and v.draw_func) then
258
259 local drawpos = pos + ang:Forward() * v.pos.x + ang:Right() * v.pos.y + ang:Up() * v.pos.z
260 ang:RotateAroundAxis(ang:Up(), v.angle.y)
261 ang:RotateAroundAxis(ang:Right(), v.angle.p)
262 ang:RotateAroundAxis(ang:Forward(), v.angle.r)
263
264 cam.Start3D2D(drawpos, ang, v.size)
265 v.draw_func( self )
266 cam.End3D2D()
267
268 end
269
270 end
271
272 end
273
274 SWEP.wRenderOrder = nil
275 function SWEP:DrawWorldModel()
276
277 if (self.ShowWorldModel == nil or self.ShowWorldModel) then
278 self:DrawModel()
279 end
280
281 if (!self.WElements) then return end
282
283 if (!self.wRenderOrder) then
284
285 self.wRenderOrder = {}
286
287 for k, v in pairs( self.WElements ) do
288 if (v.type == "Model") then
289 table.insert(self.wRenderOrder, 1, k)
290 elseif (v.type == "Sprite" or v.type == "Quad") then
291 table.insert(self.wRenderOrder, k)
292 end
293 end
294
295 end
296
297 if (IsValid(self.Owner)) then
298 bone_ent = self.Owner
299 else
300 // when the weapon is dropped
301 bone_ent = self
302 end
303
304 for k, name in pairs( self.wRenderOrder ) do
305
306 local v = self.WElements[name]
307 if (!v) then self.wRenderOrder = nil break end
308 if (v.hide) then continue end
309
310 local pos, ang
311
312 if (v.bone) then
313 pos, ang = self:GetBoneOrientation( self.WElements, v, bone_ent )
314 else
315 pos, ang = self:GetBoneOrientation( self.WElements, v, bone_ent, "ValveBiped.Bip01_R_Hand" )
316 end
317
318 if (!pos) then continue end
319
320 local model = v.modelEnt
321 local sprite = v.spriteMaterial
322
323 if (v.type == "Model" and IsValid(model)) then
324
325 model:SetPos(pos + ang:Forward() * v.pos.x + ang:Right() * v.pos.y + ang:Up() * v.pos.z )
326 ang:RotateAroundAxis(ang:Up(), v.angle.y)
327 ang:RotateAroundAxis(ang:Right(), v.angle.p)
328 ang:RotateAroundAxis(ang:Forward(), v.angle.r)
329
330 model:SetAngles(ang)
331 //model:SetModelScale(v.size)
332 local matrix = Matrix()
333 matrix:Scale(v.size)
334 model:EnableMatrix( "RenderMultiply", matrix )
335
336 if (v.material == "") then
337 model:SetMaterial("")
338 elseif (model:GetMaterial() != v.material) then
339 model:SetMaterial( v.material )
340 end
341
342 if (v.skin and v.skin != model:GetSkin()) then
343 model:SetSkin(v.skin)
344 end
345
346 if (v.bodygroup) then
347 for k, v in pairs( v.bodygroup ) do
348 if (model:GetBodygroup(k) != v) then
349 model:SetBodygroup(k, v)
350 end
351 end
352 end
353
354 if (v.surpresslightning) then
355 render.SuppressEngineLighting(true)
356 end
357
358 render.SetColorModulation(v.color.r/255, v.color.g/255, v.color.b/255)
359 render.SetBlend(v.color.a/255)
360 model:DrawModel()
361 render.SetBlend(1)
362 render.SetColorModulation(1, 1, 1)
363
364 if (v.surpresslightning) then
365 render.SuppressEngineLighting(false)
366 end
367
368 elseif (v.type == "Sprite" and sprite) then
369
370 local drawpos = pos + ang:Forward() * v.pos.x + ang:Right() * v.pos.y + ang:Up() * v.pos.z
371 render.SetMaterial(sprite)
372 render.DrawSprite(drawpos, v.size.x, v.size.y, v.color)
373
374 elseif (v.type == "Quad" and v.draw_func) then
375
376 local drawpos = pos + ang:Forward() * v.pos.x + ang:Right() * v.pos.y + ang:Up() * v.pos.z
377 ang:RotateAroundAxis(ang:Up(), v.angle.y)
378 ang:RotateAroundAxis(ang:Right(), v.angle.p)
379 ang:RotateAroundAxis(ang:Forward(), v.angle.r)
380
381 cam.Start3D2D(drawpos, ang, v.size)
382 v.draw_func( self )
383 cam.End3D2D()
384
385 end
386
387 end
388
389 end
390
391 function SWEP:GetBoneOrientation( basetab, tab, ent, bone_override )
392
393 local bone, pos, ang
394 if (tab.rel and tab.rel != "") then
395
396 local v = basetab[tab.rel]
397
398 if (!v) then return end
399
400 // Technically, if there exists an element with the same name as a bone
401 // you can get in an infinite loop. Let's just hope nobody's that stupid.
402 pos, ang = self:GetBoneOrientation( basetab, v, ent )
403
404 if (!pos) then return end
405
406 pos = pos + ang:Forward() * v.pos.x + ang:Right() * v.pos.y + ang:Up() * v.pos.z
407 ang:RotateAroundAxis(ang:Up(), v.angle.y)
408 ang:RotateAroundAxis(ang:Right(), v.angle.p)
409 ang:RotateAroundAxis(ang:Forward(), v.angle.r)
410
411 else
412
413 bone = ent:LookupBone(bone_override or tab.bone)
414
415 if (!bone) then return end
416
417 pos, ang = Vector(0,0,0), Angle(0,0,0)
418 local m = ent:GetBoneMatrix(bone)
419 if (m) then
420 pos, ang = m:GetTranslation(), m:GetAngles()
421 end
422
423 if (IsValid(self.Owner) and self.Owner:IsPlayer() and
424 ent == self.Owner:GetViewModel() and self.ViewModelFlip) then
425 ang.r = -ang.r // Fixes mirrored models
426 end
427
428 end
429
430 return pos, ang
431 end
432
433 function SWEP:CreateModels( tab )
434
435 if (!tab) then return end
436
437 // Create the clientside models here because Garry says we can't do it in the render hook
438 for k, v in pairs( tab ) do
439 if (v.type == "Model" and v.model and v.model != "" and (!IsValid(v.modelEnt) or v.createdModel != v.model) and
440 string.find(v.model, ".mdl") and file.Exists (v.model, "GAME") ) then
441
442 v.modelEnt = ClientsideModel(v.model, RENDER_GROUP_VIEW_MODEL_OPAQUE)
443 if (IsValid(v.modelEnt)) then
444 v.modelEnt:SetPos(self:GetPos())
445 v.modelEnt:SetAngles(self:GetAngles())
446 v.modelEnt:SetParent(self)
447 v.modelEnt:SetNoDraw(true)
448 v.createdModel = v.model
449 else
450 v.modelEnt = nil
451 end
452
453 elseif (v.type == "Sprite" and v.sprite and v.sprite != "" and (!v.spriteMaterial or v.createdSprite != v.sprite)
454 and file.Exists ("materials/"..v.sprite..".vmt", "GAME")) then
455
456 local name = v.sprite.."-"
457 local params = { ["$basetexture"] = v.sprite }
458 // make sure we create a unique name based on the selected options
459 local tocheck = { "nocull", "additive", "vertexalpha", "vertexcolor", "ignorez" }
460 for i, j in pairs( tocheck ) do
461 if (v[j]) then
462 params["$"..j] = 1
463 name = name.."1"
464 else
465 name = name.."0"
466 end
467 end
468
469 v.createdSprite = v.sprite
470 v.spriteMaterial = CreateMaterial(name,"UnlitGeneric",params)
471
472 end
473 end
474
475 end
476
477 local allbones
478 local hasGarryFixedBoneScalingYet = false
479
480 function SWEP:UpdateBonePositions(vm)
481
482 if self.ViewModelBoneMods then
483
484 if (!vm:GetBoneCount()) then return end
485
486 // !! WORKAROUND !! //
487 // We need to check all model names :/
488 local loopthrough = self.ViewModelBoneMods
489 if (!hasGarryFixedBoneScalingYet) then
490 allbones = {}
491 for i=0, vm:GetBoneCount() do
492 local bonename = vm:GetBoneName(i)
493 if (self.ViewModelBoneMods[bonename]) then
494 allbones[bonename] = self.ViewModelBoneMods[bonename]
495 else
496 allbones[bonename] = {
497 scale = Vector(1,1,1),
498 pos = Vector(0,0,0),
499 angle = Angle(0,0,0)
500 }
501 end
502 end
503
504 loopthrough = allbones
505 end
506 // !! ----------- !! //
507
508 for k, v in pairs( loopthrough ) do
509 local bone = vm:LookupBone(k)
510 if (!bone) then continue end
511
512 // !! WORKAROUND !! //
513 local s = Vector(v.scale.x,v.scale.y,v.scale.z)
514 local p = Vector(v.pos.x,v.pos.y,v.pos.z)
515 local ms = Vector(1,1,1)
516 if (!hasGarryFixedBoneScalingYet) then
517 local cur = vm:GetBoneParent(bone)
518 while(cur >= 0) do
519 local pscale = loopthrough[vm:GetBoneName(cur)].scale
520 ms = ms * pscale
521 cur = vm:GetBoneParent(cur)
522 end
523 end
524
525 s = s * ms
526 // !! ----------- !! //
527
528 if vm:GetManipulateBoneScale(bone) != s then
529 vm:ManipulateBoneScale( bone, s )
530 end
531 if vm:GetManipulateBoneAngles(bone) != v.angle then
532 vm:ManipulateBoneAngles( bone, v.angle )
533 end
534 if vm:GetManipulateBonePosition(bone) != p then
535 vm:ManipulateBonePosition( bone, p )
536 end
537 end
538 else
539 self:ResetBonePositions(vm)
540 end
541
542 end
543
544 function SWEP:ResetBonePositions(vm)
545
546 if (!vm:GetBoneCount()) then return end
547 for i=0, vm:GetBoneCount() do
548 vm:ManipulateBoneScale( i, Vector(1, 1, 1) )
549 vm:ManipulateBoneAngles( i, Angle(0, 0, 0) )
550 vm:ManipulateBonePosition( i, Vector(0, 0, 0) )
551 end
552
553 end
554
555 /**************************
556 Global utility code
557 **************************/
558
559 // Fully copies the table, meaning all tables inside this table are copied too and so on (normal table.Copy copies only their reference).
560 // Does not copy entities of course, only copies their reference.
561 // WARNING: do not use on tables that contain themselves somewhere down the line or you'll get an infinite loop
562 function table.FullCopy( tab )
563
564 if (!tab) then return nil end
565
566 local res = {}
567 for k, v in pairs( tab ) do
568 if (type(v) == "table") then
569 res[k] = table.FullCopy(v) // recursion ho!
570 elseif (type(v) == "Vector") then
571 res[k] = Vector(v.x, v.y, v.z)
572 elseif (type(v) == "Angle") then
573 res[k] = Angle(v.p, v.y, v.r)
574 else
575 res[k] = v
576 end
577 end
578
579 return res
580
581 end
582
583end