Guest User

Untitled

a guest
Aug 17th, 2021
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.96 KB | None | 0 0
  1. #include "utils.lua"
  2.  
  3. function init()
  4.     RegisterTool("P90", "P90", "MOD/vox/P90.vox")
  5.     SetBool("game.tool.P90.enabled", true)
  6.     SetFloat("game.tool.P90.ammo", 101)
  7.    
  8.     unlimited_ammo = GetBool("savegame.mod.unlimitedammo")
  9.     unlimited_clip = GetBool("savegame.mod.unlimitedclip")
  10.     input_mode = GetString("savegame.mod.mode_input")
  11.     input_type = GetString("savegame.mod.type_input")
  12.     input_scope = GetString("savegame.mod.scope_input")
  13.     input_bullet_spread = GetString("savegame.mod.bullet_spread")
  14.     smoke_firing = GetFloat("savegame.mod.smoke_firing")
  15.     smoke_impact = GetFloat("savegame.mod.smoke_impact")
  16.     if input_mode == "" then input_mode = "C" end
  17.     if input_type == "" then input_type = "V" end
  18.     if input_scope == "" then input_scope = "T" end
  19.     if input_bullet_spread == "" then input_bullet_spread = "Z" end
  20.     if smoke_firing == 0 then smoke_firing = 5 end
  21.     if smoke_impact == 0 then smoke_impact = 20 end
  22.     if smoke_firing == 0.1 then smoke_firing = 0 end
  23.     if smoke_impact == 0.1 then smoke_impact = 0 end
  24.    
  25.     bullet_spread = 0
  26.    
  27.     aim_progress = 0
  28.     shoot_timer = 0
  29.     automatic = true
  30.     bullets = {}
  31.     bullet_step = 3
  32.     ammo = 50
  33.     clip = 50*10
  34.     reload_timer = 0
  35.     full_reload_timer = 0
  36.     ammo_type = 0
  37.     ammo_needed_reload = 0
  38.     scope_type = 0
  39.     firing_sound = LoadSound("MOD/snd/Firing.ogg")
  40.     reloading_sound = LoadSound("MOD/snd/Reloading.ogg")
  41.     changing_mode_sound = LoadSound("MOD/snd/ChangingMode.ogg")
  42.  
  43. end
  44. function Shoot()
  45.     camera = GetPlayerCameraTransform()
  46.     if not unlimited_ammo then ammo = ammo - 1 end
  47.     local smokepos = 0
  48.     if aim_progress == 0 then smokepos = TransformToParentPoint(camera, VecSub(Vec(0, -0.2, -1.75), Vec(-0.325, 0.225, 0.20))) else smokepos = TransformToParentPoint(camera, Vec(0, -0.2, -1.75)) end
  49.     if smoke_firing > 0 then for i = 1, smoke_firing do
  50.         local smokedir = TransformToParentVec(camera, Vec(math.random(-1, 1), math.random(-1, 1), -5))
  51.         smokedir = VecAdd(smokedir, VecScale(GetPlayerVelocity(), 0.25))
  52.         SpawnParticle("darksmoke", smokepos, smokedir, .075, 1)
  53.     end end
  54.     if bullet_spread ~= 0 then
  55.         angle = math.random() * 2 * math.pi
  56.         radius = math.random() * (bullet_spread / 100)
  57.         spread_x = math.cos(angle) * radius
  58.         spread_y = math.sin(angle) * radius
  59.     else
  60.         spread_x = 0
  61.         spread_y = 0
  62.     end
  63.     table.insert(bullets, {smokepos, TransformToParentVec(camera, Vec(spread_x, spread_y, -1)), 0})
  64.     PlaySound(firing_sound, camera.pos)
  65. end
  66. function tick(dt)
  67.     if GetString("game.player.tool") == "P90" and GetPlayerVehicle() == 0 then
  68.         if InputDown("lmb") and grabing then
  69.             grabing = 1
  70.         else
  71.             grabing = GetPlayerGrabShape()
  72.         end
  73.         if InputPressed(input_mode) then
  74.             automatic = not automatic
  75.             PlaySound(changing_mode_sound, GetPlayerCameraTransform().pos)
  76.         end
  77.         if InputPressed(input_type) then
  78.             ammo_type = ammo_type+1
  79.             if ammo_type == 3 then ammo_type = 0 end
  80.         end
  81.         if grabing > 0 then grabing = true else grabing = false end
  82.         if InputDown("rmb") and not grabing then
  83.             aim_progress = math.min(aim_progress + dt * 5, 1)
  84.             if scope_type == 1 and not(InputDown("right") or InputDown("left") or InputDown("up") or InputDown("down") or InputDown("jump")) then SetCameraFov(10) end
  85.         else
  86.             aim_progress = math.max(aim_progress - dt * 5, 0)
  87.         end
  88.         if InputDown(input_bullet_spread) then
  89.             SetBool("game.input.locktool", true)
  90.             if InputValue("mousewheel") ~= 0 then
  91.                 local offset = 0.5 * InputValue("mousewheel")
  92.                 bullet_spread = clamp(bullet_spread + offset, 0, 10)
  93.             end
  94.         else
  95.             SetBool("game.input.locktool", false)
  96.             debug_line = ""
  97.         end
  98.        
  99.         local P90_Shapes = GetBodyShapes(GetToolBody())
  100.         local Scope_Shape = P90_Shapes[2]
  101.         local Laser_Shape = P90_Shapes[3]
  102.         local Scope_Clean_Shape = P90_Shapes[4]
  103.         local move_vec
  104.         if scope_type == 1 then
  105.             move_vec = {-0.325, 0.025, 0.2}
  106.             if aim_progress == 1 and not(InputDown("right") or InputDown("left") or InputDown("up") or InputDown("down") or InputDown("jump")) then
  107.                 SetShapeLocalTransform(Scope_Shape, Transform(Vec(0, 0, 999999), QuatEuler(-90, 0, 0)))
  108.                 SetShapeLocalTransform(Scope_Clean_Shape, Transform(Vec(0.25, -0.15, -1.1), QuatEuler(-90, 0, 0)))
  109.                 SetShapeLocalTransform(Laser_Shape, Transform(Vec(0, 0, 999999), QuatEuler(-90, 0, 0)))
  110.             else
  111.                 SetShapeLocalTransform(Scope_Shape, Transform(Vec(0.25, -0.15, -1.1), QuatEuler(-90, 0, 0)))
  112.                 SetShapeLocalTransform(Scope_Clean_Shape, Transform(Vec(0, 0, 999999), QuatEuler(-90, 0, 0)))
  113.                 SetShapeLocalTransform(Laser_Shape, Transform(Vec(0, 0, 999999), QuatEuler(-90, 0, 0)))
  114.             end
  115.         elseif scope_type == 2 then
  116.             move_vec = {-0.325, 0.025, 0.4}
  117.             SetShapeLocalTransform(Scope_Shape, Transform(Vec(0, 0, 999999), QuatEuler(-90, 0, 0)))
  118.             SetShapeLocalTransform(Scope_Clean_Shape, Transform(Vec(0, 0, 99999), QuatEuler(-90, 0, 0)))
  119.             SetShapeLocalTransform(Laser_Shape, Transform(Vec(0.25, -0.15, -1.25), QuatEuler(-90, 0, 0)))
  120.         else
  121.             move_vec = {-0.325, 0.225, 0.20}
  122.             SetShapeLocalTransform(Scope_Shape, Transform(Vec(0, 0, 999999), QuatEuler(-90, 0, 0)))
  123.             SetShapeLocalTransform(Scope_Clean_Shape, Transform(Vec(0, 0, 999999), QuatEuler(-90, 0, 0)))
  124.             SetShapeLocalTransform(Laser_Shape, Transform(Vec(0, 0, 99999), QuatEuler(-90, 0, 0)))
  125.         end
  126.         tool_transform = Transform()
  127.         tool_transform.pos = VecAdd(tool_transform.pos, VecScale(Vec(move_vec[1], move_vec[2], move_vec[3]), aim_progress))
  128.         tool_transform.pos = VecAdd(tool_transform.pos, Vec(0, 0, shoot_timer))
  129.         SetToolTransform(tool_transform)
  130.         if (InputDown("lmb") and automatic) or (InputPressed("lmb") and not automatic) then
  131.             if shoot_timer < 0.001 and ammo > 0 and reload_timer == 0 and not grabing then
  132.                 Shoot()
  133.                 shoot_timer = 0.050
  134.             end
  135.         end
  136.         if shoot_timer > 0 then shoot_timer = shoot_timer - dt else shoot_timer = 0 end
  137.        
  138.         if InputDown("R") and ammo ~= 50 and reload_timer == 0 then
  139.             if unlimited_clip then
  140.                 PlaySound(reloading_sound, GetPlayerCameraTransform().pos, 1, false)
  141.                 reload_timer = 2.5
  142.                 ammo = 0
  143.                 ammo_needed_reload = 50
  144.             else
  145.                 full_reload_timer = full_reload_timer + dt
  146.                 if ammo == 0 then full_reload_timer = 3 end            
  147.             end
  148.         end
  149.         if (InputReleased("R") or full_reload_timer >= 3) and not unlimited_clip and full_reload_timer ~= 0 then
  150.             PlaySound(reloading_sound, GetPlayerCameraTransform().pos, 1, false)
  151.             reload_timer = 2.5
  152.             if full_reload_timer >= 3 then
  153.                 ammo_needed_reload = 50-ammo
  154.             else
  155.                 ammo_needed_reload = 50
  156.             end
  157.             full_reload_timer = 0
  158.         end
  159.         if InputPressed(input_scope) and aim_progress == 0 then
  160.             scope_type = scope_type + 1
  161.             if scope_type == 3 then scope_type = 0 end
  162.         end
  163.         if reload_timer > 0 then
  164.             reload_timer = reload_timer - dt
  165.             if reload_timer <= 0 then
  166.                 reload_timer = 0
  167.                 ammo = math.min(ammo+ammo_needed_reload, math.min(clip, 50))
  168.                 if not unlimited_clip then clip = math.max(clip - ammo_needed_reload, 0) end
  169.             end
  170.         end
  171.     end
  172.     local delete_bullet = {}
  173.     for i = 1, #bullets do
  174.         local hit, dis = QueryRaycast(bullets[i][1], bullets[i][2], bullet_step+0.1, 0.025)
  175.         if hit then
  176.             if ammo_type == 0 then MakeHole(VecAdd(bullets[i][1], VecScale(bullets[i][2], dis)), 1, 0.75, 0.50) --Normal Ammo
  177.             elseif ammo_type == 1 then Explosion(VecAdd(bullets[i][1], VecScale(bullets[i][2], dis)), 1) --Explosive Ammo
  178.             else MakeHole(VecAdd(bullets[i][1], VecScale(bullets[i][2], dis)), 0.25, 0.15, 0.15) end --Small Ammo
  179.             table.insert(delete_bullet, 1, i)
  180.             if smoke_impact > 0 then for u = 1, smoke_impact do
  181.                 SpawnParticle("smoke", VecAdd(bullets[i][1], VecScale(bullets[i][2], dis)), VecNormalize(Vec(math.random(-1, 1), math.random(-1, 1), math.random(-1, 1))), .75, 2)
  182.             end end
  183.         else
  184.             bullets[i][1] = VecAdd(bullets[i][1], VecScale(bullets[i][2], bullet_step))
  185.             bullets[i][3] = bullets[i][3] + 1
  186.             DrawLine(VecSub(bullets[i][1], bullets[i][2]), bullets[i][1])
  187.         end
  188.         if bullets[i][3] >= 100 then table.insert(delete_bullet, 1, i) end
  189.     end
  190.     if #delete_bullet ~= 0 then
  191.         for i = 1, #delete_bullet do
  192.             table.remove(bullets, delete_bullet[i])
  193.         end
  194.     end
  195. end
  196. function draw()
  197.     if GetString("game.player.tool") == "P90" and GetPlayerVehicle() == 0 then
  198.         UiPush()
  199.             UiTranslate(UiCenter(), UiHeight()-60)
  200.             UiAlign("center middle")
  201.             UiColor(1, 1, 1)
  202.             UiFont("bold.ttf", 32)
  203.             UiTextOutline(0,0,0,1,0.1)
  204.             local clip_text
  205.             local ammo_text
  206.             if unlimited_clip then clip_text = "inf" else clip_text = clip end
  207.             if unlimited_ammo then ammo_text = "inf" else ammo_text = ammo end
  208.             if reload_timer == 0 then UiText(ammo_text .. "/"..clip_text) else UiText("Reloading...") end
  209.         UiPop()
  210.         UiPush()
  211.             if aim_progress == 1 and scope_type == 2 then
  212.                 local start_pos = TransformToParentPoint(GetPlayerCameraTransform(), Vec(0, -0.2, -1.75))
  213.                 local Vo = TransformToParentVec(GetPlayerCameraTransform(), Vec(0, 0, -1))
  214.                 local hit, dis = QueryRaycast(start_pos, Vo, 50, 0.025)
  215.                 if hit then
  216.                     hit_pos = VecAdd(start_pos, VecScale(Vo, dis))
  217.                     x, y, dist = UiWorldToPixel(hit_pos)
  218.                     UiTranslate(x-(5-dis/10), y-(5-dis/10))
  219.                     UiScale(1-dis/50)
  220.                     if scope_type == 2 and dis > 1.05 then UiImage("MOD/img/GreenDot.png") end
  221.                 end
  222.             end
  223.             --RayCast for scope, disabled because of lag issues.
  224.             --[[
  225.             if aim_progress == 1 and scope_type == 1 and not(InputDown("right") or InputDown("left") or InputDown("up") or InputDown("down") or InputDown("jump")) and math.abs(InputValue("mousedx"))+math.abs(InputValue("mousedy")) < 2 then
  226.                 local start_pos = TransformToParentPoint(GetPlayerCameraTransform(), Vec(0, -0.2, -1.75))
  227.                 local Vo = TransformToParentVec(GetPlayerCameraTransform(), Vec(0, 0, -1))
  228.                 local hit, dis = QueryRaycast(start_pos, Vo, 300, 0.025)
  229.                 if hit then
  230.                     hit_pos = VecAdd(start_pos, VecScale(Vo, dis))
  231.                     x, y, dist = UiWorldToPixel(hit_pos)
  232.                     UiTranslate(x-30, y-30)
  233.                     if scope_type == 1 and dis > 8.8 then UiImage("MOD/img/ScopeHit.png") end
  234.                 end
  235.             end
  236.             ]]
  237.         UiPop()
  238.         UiPush()
  239.             UiTranslate(UiCenter(), UiHeight()-120)
  240.             UiAlign("center middle")
  241.             UiColor(1, 1, 1)
  242.             UiFont("bold.ttf", 32)
  243.             UiTextOutline(0,0,0,1,0.1)
  244.             local type_text
  245.             if ammo_type == 0 then type_text = "Normal Ammo" elseif ammo_type == 1 then type_text =  "Explosive Ammo" else type_text =  "Small Ammo" end
  246.             local mode_text
  247.             if automatic then mode_text = "Automatic" else mode_text = "Semi-Auto" end
  248.             UiText(type_text.." | "..mode_text.." | Spread: "..bullet_spread)
  249.         UiPop()
  250.         if full_reload_timer ~= 0 then
  251.             UiPush()
  252.                 UiTranslate(UiCenter(), UiHeight()-180)
  253.                 UiAlign("center middle")
  254.                 UiColor(1, 1, 1)
  255.                 UiFont("bold.ttf", 32)
  256.                 UiTextOutline(0,0,0,1,0.1)
  257.                 UiText(math.floor(math.min(full_reload_timer/3*100, 100)).."%")
  258.             UiPop()
  259.         end
  260.     end
  261. end
Add Comment
Please, Sign In to add comment