Last active
August 29, 2015 14:14
-
-
Save Python1320/128de2a48f7632d0f591 to your computer and use it in GitHub Desktop.
GMod Lua sv_airaccelerate 1000 un****er
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Status: Almost working. Fork and help make it better! | |
-- Objective: | |
-- Slow down the "instant" stops considerably | |
-- when trying to go opposite direction | |
-- of current movement while airmoving | |
-- and do not break airmoving too much | |
-- Setup: sv_airaccelerate 1000 | |
-- required inputs: | |
-- sv_airaccelerate, GetVelocity, | |
-- GetViewAngles.yaw, GetForwardMove, | |
-- GetSideMove, maxplayerspeed? | |
-- outputs: | |
-- SetForwardMove, SetSideMove | |
-- | |
-- A 2d vectors problem | |
-- | |
-- License: http://unlicense.org | |
local deg2rad=0.0174532925 | |
local math=math | |
local airacc=tonumber(GetConVar"sv_airaccelerate":GetString()) | |
local hud1,hud2,hud3=0,0,0 | |
local hud4,hud5=0,0 | |
local hudc=0 | |
local sz=12 | |
local scl=128 | |
local szh=sz*0.5 | |
hook.Add("HUDPaint","a",function() | |
local cx,cy=ScrW()*0.5,ScrH()*0.5 | |
local bn=math.BadNumber | |
if bn and bn(hud1) then hud1=1 end | |
if bn and bn(hud2) then hud2=1 end | |
if bn and bn(hud3) then hud3=1 end | |
if bn and bn(hud4) then hud4=1 end | |
if bn and bn(hud5) then hud5=1 end | |
local dx,dy= | |
hud1*(scl-sz)*0.5, | |
-hud2*(scl-sz)*0.5 | |
surface.SetDrawColor(111,55,244,150) | |
surface.DrawRect(cx-szh+dx,cy-szh,sz,sz) | |
surface.SetDrawColor(100,200,50,150) | |
surface.DrawRect(cx-szh,cy-szh+dy,sz,sz) | |
local dx,dy= | |
hud5*scl, | |
hud4*scl | |
surface.SetDrawColor(111,55,244,150) | |
surface.DrawRect(cx-scl*0.5-sz,cy-scl*0.5,sz,dy) | |
surface.SetDrawColor(100,200,50,150) | |
surface.DrawRect(cx-scl*0.5,cy-scl*0.5-sz,dx,sz) | |
surface.SetDrawColor(33,33,33,100) | |
surface.DrawRect(cx-scl*0.5,cy-scl*0.5,scl,scl) | |
local f=hud3 | |
if f<0 then f=-f end | |
surface.SetDrawColor(hud3<0 and 222 or 33,hud3>=0 and 222 or 33,33,100) | |
surface.DrawOutlinedRect(cx-scl*0.5*f,cy-scl*0.5*f,scl*f,scl*f) | |
-- hudc=math.sin(CurTime()*3)*math.pi*2 | |
-- print(hudc) | |
local sig=hudc<0 and 1 or -1 | |
local c=-math.Round(hudc/deg2rad) | |
c=c>360*5 and 360*5 or c<-360*5 and -360*5 or c | |
if sig<0 then | |
surface.SetDrawColor(33,220,33,200) | |
else | |
surface.SetDrawColor(220,33,33,200) | |
end | |
local px,py | |
for i=0,c,sig<0 and -1 or 1 do | |
local fff = (i/360*5)*0.01 | |
fff=fff<0 and -fff or fff | |
fff=1-fff | |
local lx,ly= | |
cx+math.cos(i*deg2rad-90*deg2rad)*scl*0.5*fff, | |
cy+math.sin(i*deg2rad-90*deg2rad)*scl*0.5*fff | |
if px then | |
surface.DrawLine(px,py,lx,ly) | |
end | |
px,py=lx,ly | |
end | |
local pl=LocalPlayer() | |
if pl:KeyDown(IN_ATTACK2) then return end | |
hud1,hud2,hud3=0,0,0 | |
hud4,hud5=0,0 | |
hudc=0 | |
end) | |
local prevgrnd=true | |
local nyaws=math.sin(90*deg2rad) | |
local nyawc=math.cos(90*deg2rad) | |
hook.Add("CreateMove","a",function(cmd) | |
local pl=LocalPlayer() | |
if pl:KeyDown(IN_ATTACK2) then return end | |
if pl:GetMoveType() ~= MOVETYPE_WALK then return end | |
local prev = prevgrnd | |
local grnd = pl:IsOnGround() | |
prevgrnd=grnd | |
-- do nothing if previously was on ground and on ground. no prediction here | |
if prev and grnd then return end | |
if pl:IsFlying() then return end | |
-- Get where we want to move | |
local fwd_mv,side_mv= | |
cmd:GetForwardMove(), | |
cmd:GetSideMove() | |
local len=fwd_mv*fwd_mv+side_mv*side_mv | |
if len==0 then return end | |
len=len^0.5 | |
-- normalize | |
fwd_mv,side_mv=fwd_mv/len,side_mv/len | |
-- wtf speeds | |
local max_speed = pl:GetMaxSpeed() | |
len=len>max_speed and max_speed or len | |
-- Get normalized velocity | |
local vel=pl:GetVelocity() | |
local xvel,yvel=vel[1],vel[2] | |
local vlen=xvel*xvel+yvel*yvel | |
if vlen==0 then return end | |
vlen=vlen^0.5 | |
-- normalize | |
xvel,yvel=xvel/vlen,yvel/vlen | |
-- ROTATE GetVelocity to our movedir space (so we dont need to rotate our movedir back) | |
local yaw=-cmd:GetViewAngles()[2]*deg2rad | |
-- 2d vector rotate | |
local yaws=math.sin(yaw) | |
local yawc=math.cos(yaw) | |
local _x = xvel * yawc - yvel * yaws | |
local _y = xvel * yaws + yvel * yawc | |
xvel,yvel=_x,-_y | |
--print(("%2.1f %2.1f\n%2.1f %2.1f"):format(fwd_mv,side_mv,xvel,yvel)) | |
-- are they pointing at the same direction how much | |
local antisimilarity = fwd_mv *xvel | |
+ side_mv*yvel -- dot product | |
assert(antisimilarity>=-1.001 and antisimilarity<=1.001,antisimilarity) | |
antisimilarity=-antisimilarity | |
if antisimilarity<0 then | |
return | |
end | |
antisimilarity=antisimilarity>1 and 1 or antisimilarity | |
hudc=antisimilarity*math.pi*2 | |
local antisimilarity_complement=1-antisimilarity | |
hud2=antisimilarity | |
print(antisimilarity_complement*100) | |
--hard cutoff | |
local min=math.sin(0.0174532925*45) | |
if antisimilarity_complement<min then | |
antisimilarity_complement=1 | |
len=1 | |
end | |
local fwd_mv2=fwd_mv*antisimilarity_complement | |
local side_mv2=side_mv*antisimilarity_complement | |
-- how much remaining of the original | |
local frac_fwd=fwd_mv2/fwd_mv | |
local frac_side=side_mv2/side_mv | |
hud4=frac_fwd | |
hud5=frac_side | |
cmd:SetForwardMove(fwd_mv2*len) | |
cmd:SetSideMove(side_mv2*len) | |
end) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment