local _name = "" epressed = 0 local state = "" local throw_speed = 700 local hold_height = 10 local hold_dist = 50 local max_throw_dist = 5 --maximum time (in seconds) the hook will travel if it doesnt hit anything local max_charge_time = 1 --seconds til max throw length reached local show_timer = 1 local ghtimer = 0 local perc = 0 local angle_Y = 0 local n_max_time = 0 local thisID = 0 function grapplinghook_init_name(e,name) _name = name state = "wait pick up" max_charge_time = max_charge_time*1000 max_throw_dist = max_throw_dist*1000 end function grapplinghook_main(e) --Text(1,1,3,"state = "..state) if state == "wait pick up" then if PlayerLooking(e,150,20) > 0 then Prompt("Collect the ".._name.."? (E)") if g_KeyPressE == 1 and epressed == 0 then epressed = 1 state = "picking" Prompt("Collected the " .. _name) PlaySound(e,0) AddPlayerWeapon(e) Hide(e) CollisionOff(e) end end elseif state == "picking" then if g_KeyPressE == 0 then thisID = g_PlayerGunID StartTimer(e) state = "holding" end elseif state == "holding" then if g_PlayerGunFired > 0 and g_PlayerGunID == thisID then g_projectileevent_explosion = 0 state = "firing" end elseif state == "firing" then if g_projectileevent_explosion ~= 0 then ghx = g_projectileevent_x ghy = g_projectileevent_y ghz = g_projectileevent_z playerStartY = g_PlayerPosY ychange = ghy - playerStartY flatdist = GetPlayerFlatDistanceToPoint(ghx,ghz) oldposX = g_PlayerPosX oldposY = g_PlayerPosY oldposZ = g_PlayerPosZ state = "hit" StartTimer(e) end elseif state == "hit" then local blocked = 0 angle_Y = g_PlayerAngY local x1 = g_PlayerPosX local y1 = g_PlayerPosY+20 local z1 = g_PlayerPosZ local y2 = y1 local x2 = x1 + (math.sin(angle_Y) * 80) local z2 = z1 + (math.cos(angle_Y) * 80) local obstructionhit = IntersectAll(x1,y1,z1,x2,y2,z2,0) if obstructionhit == nil then obstructionhit = 0 end if obstructionhit == 0 then y2 = y1 - 5 obstructionhit = IntersectAll(x1,y1,z1,x2,y2,z2,0) if obstructionhit == nil then obstructionhit = 0 end if obstructionhit == 0 then y2 = y1 + 5 obstructionhit = IntersectAll(x1,y1,z1,x2,y2,z2,0) if obstructionhit == nil then obstructionhit = 0 end if obstructionhit == 0 then else blocked = 1 end else blocked = 1 end else blocked = 1 end if blocked == 1 then angle_Y = RotatePlayerToPoint(ghx,ghy,ghz) angle_Y = math.rad(angle_Y) local pos_X = g_PlayerPosX + (math.sin(angle_Y) * -(throw_speed/20)) local pos_Z = g_PlayerPosZ + (math.cos(angle_Y) * -(throw_speed/20)) SetFreezePosition(pos_X,g_PlayerPosY,pos_Z) SetFreezeAngle(0,g_PlayerAngY,0) TransportToFreezePosition() state = "holding" return end if GetPlayerDistanceToPoint(ghx,ghy,ghz) < 200 then state = "holding" return end angle_Y = RotatePlayerToPoint(ghx,ghy,ghz) angle_Y = math.rad(angle_Y) local pos_X = g_PlayerPosX + (math.sin(angle_Y) * (throw_speed/10)) local pos_Z = g_PlayerPosZ + (math.cos(angle_Y) * (throw_speed/10)) local temp = ychange * (1-(GetPlayerFlatDistanceToPoint(ghx,ghz)/flatdist)) if g_PlayerPosY < ghy-10 then pos_Y = playerStartY + temp elseif g_PlayerPosY > ghy+10 then pos_Y = playerStartY + temp else pos_Y = ghy end if GetTimer(e) < 10 then oldposX = g_PlayerPosX oldposY = g_PlayerPosY oldposZ = g_PlayerPosZ end SetFreezePosition(pos_X,pos_Y,pos_Z) SetFreezeAngle(0,g_PlayerAngY,0) TransportToFreezePosition() if GetTimer(e) > 100 then StartTimer(e) if GetPlayerDistanceToPoint(oldposX,oldposY,oldposZ) < 70 then state = "holding" end end end end function grapplinghook_preexit(e) end function grapplinghook_exit(e) end function round(num, idp) local mult = 10^(idp or 0) return math.floor(num * mult + 0.5) / mult end function GetPlayerDistanceToPoint(x,y,z) local disx = x - g_PlayerPosX local disy = y - g_PlayerPosY local disz = z - g_PlayerPosZ return math.sqrt(disx^2 + disy^2 + disz^2) end function GetPlayerFlatDistanceToPoint(x,z) local disx = x - g_PlayerPosX local disz = z - g_PlayerPosZ return math.sqrt(disx^2 + disz^2) end function RotatePlayerToPoint(dx,dy,dz) local x = dx - g_PlayerPosX local y = dy - g_PlayerPosY local z = dz - g_PlayerPosZ local angle = math.atan2(x,z) angle = angle * (180.0 / math.pi) if angle < 0 then angle = 360 + angle elseif angle > 360 then angle = angle - 360 end SetFreezeAngle(g_PlayerAngX,angle,g_PlayerAngZ) SetFreezePosition(g_PlayerPosX,g_PlayerPosY,g_PlayerPosZ) TransportToFreezePosition() return angle end function PlayerLooking(e,dis,v) if g_Entity[e] ~= nil then if dis == nil then dis = 3000 end if v == nil then v = 0.5 end if GetPlayerDistance(e) <= dis then local destx = g_Entity[e]['x'] - g_PlayerPosX local destz = g_Entity[e]['z'] - g_PlayerPosZ local angle = math.atan2(destx,destz) angle = angle * (180.0 / math.pi) if angle <= 0 then angle = 360 + angle elseif angle > 360 then angle = angle - 360 end while g_PlayerAngY < 0 or g_PlayerAngY > 360 do if g_PlayerAngY <= 0 then g_PlayerAngY = 360 + g_PlayerAngY elseif g_PlayerAngY > 360 then g_PlayerAngY = g_PlayerAngY - 360 end end local L = angle - v local R = angle + v if L <= 0 then L = 360 + L elseif L > 360 then L = L - 360 end if R <= 0 then R = 360 + R elseif R > 360 then R = R - 360 end if (L < R and math.abs(g_PlayerAngY) > L and math.abs(g_PlayerAngY) < R) then return 1 elseif (L > R and (math.abs(g_PlayerAngY) > L or math.abs(g_PlayerAngY) < R)) then return 1 else return 0 end else return 0 end end end