Guest User

p90 main.lua

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