local U = require "scriptbank\\utillib" local abs = math.abs local rad = math.rad local sin = math.sin local cos = math.cos local min = math.min local atan = math.atan2 local enter_range = 200 -- how close player needs to be to mount the vehicle local enter_angle = 70 -- player looking at the vehicle (smaller = tighter angle) local pos_mod = { x = 0, -- how far left/right in the vehicle the player will be (negative value = left) y = 60, -- how high above/below the vehicle height the player will be z = 0 } -- how far back/forward in the vehicle the player will be (negative value = backwards) local max_speed = 150 local min_speed = -40 local pos_x = nil local pos_z = nil local posy = nil local turn_speed = 0.5 -- how quickly the vehicle turns local acceleration = 1 -- how quickly the vehicle gains speed (in reverse too) local decceleration = 2 -- how quickly the vehicle slows down if player not pressing W or S local impact_range = 200 -- when enemies will get run over local impact_angle = 90 -- adjusts angle for when enemies will get run over (smaller value = narrow vehicle) local angle_mod = 0 -- if you want the player to face a different direction (other than forward) when first entering the vehicle local init_health = 1000 -- how much health to give vehicle (adjusts player health while in the vehicle) ------------------------------------------------------------------------------------------------------------------- local old_health = 0 local vpressed = false local vehicles = {} function vehicle_gunner2_init( e ) end function vehicle_gunner2_main(e) local Ent = g_Entity[ e ] local v = vehicles[ e ] if v == nil then vehicles[ e ] = { state = 'wait', speed = 0, health = init_health, timer = 0 } return end if v.state == "wait" then CollisionOn(e) if U.PlayerLookingNear( e, enter_range, enter_angle ) then Prompt("Mount the gun? *E*") if g_KeyPressE == 1 and not vpressed then vpressed = true old_health = g_PlayerHealth SetPlayerHealth( v.health ) local xo, yo, zo = U.Rotate3D( pos_mod.x, pos_mod.y, pos_mod.z, rad( Ent.anglex ), rad( Ent.angley ), rad( Ent.anglez ) ) SetFreezePosition( Ent.x + xo, Ent.y + yo, Ent.z + zo ) SetFreezeAngle( Ent.anglex, Ent.angley + angle_mod, Ent.anglez ) TransportToFreezePosition() v.state = "drive" end end elseif v.state == "drive" then SetGamePlayerControlWobble( 0 ) if g_KeyPressW == 1 then if v.speed < max_speed then v.speed = v.speed + acceleration else v.speed = max_speed end elseif g_KeyPressS == 1 then if v.speed > min_speed then v.speed = v.speed - acceleration else v.speed = min_speed end else if v.speed > 0 then v.speed = v.speed - decceleration elseif v.speed < 0 then v.speed = v.speed + decceleration end end CollisionOff( e ) local newx, newy, newz = Ent.x, Ent.y, Ent.z local xA, yA, zA = rad( Ent.anglex ), rad( Ent.angley ), rad( Ent.anglez ) if abs( v.speed ) > 0 then local xo, yo, zo = U.Rotate3D( 0, 0, v.speed / 10, xA, yA, zA ) newx, newy, newz = newx + xo, newy + yo, newz + zo newy = min( newy, GetTerrainHeight( newx, newz ) ) SetPosition( e, newx, newy + 5, newz ) Prompt( v.speed ) end local xo, yo, zo = U.Rotate3D( pos_mod.x, pos_mod.y, pos_mod.z, rad( Ent.anglex ), rad( Ent.angley ), rad( Ent.anglez ) ) SetFreezePosition( newx + xo, newy + yo, newz + zo ) TransportToFreezePositionOnly() if g_KeyPressA == 1 then SetRotation( e, Ent.anglex, Ent.angley - turn_speed, Ent.anglez ) elseif g_KeyPressD == 1 then SetRotation( e, Ent.anglex, Ent.angley + turn_speed, Ent.anglez ) end CollisionOn( e ) if g_Time > v.timer then v.timer = g_Time + 100 for _, a in pairs( U.ClosestEntities( impact_range ) ) do PromptLocal( a, a .. ", " .. ( ai_bot_state[ a ] or 'nil' ) ) if ai_soldier_state[ a ] ~= nil or ai_bot_state[ a ] ~= nil then local tEnt = g_Entity[ a ] if ( EntityLooking( Ent, tEnt, impact_angle ) and v.speed > 0 ) or ( not EntityLooking( Ent, tEnt, 360 - impact_angle ) and v.speed < 0 ) then SetEntityHealth( a, 0 ) SetEntityRagdollForce( a, "head", tEnt.x - Ent.x, 10, tEnt.z - Ent.z, math.random( math.abs( v.speed ) * 50, math.abs( v.speed ) * 75 ) ) end end end end if v.speed > 0 - ( max_speed / 10 ) and v.speed < 0 + ( max_speed / 10 ) then Prompt("Leave the gun? *E*") if g_KeyPressE == 1 and not vpressed then vpressed = true v.speed = 0 v.health = g_PlayerHealth local new_y = rad( Ent.angley ) local pos_x = Ent.x + sin( new_y ) * enter_range local pos_z = Ent.z + cos( new_y ) * enter_range SetFreezePosition( pos_x, GetTerrainHeight( pos_x, pos_y ), pos_z ) TransportToFreezePositionOnly() CollisionOn(e) SetPlayerHealth( old_health ) v.state = "wait" end end if g_PlayerHealth < 1 then SetEntityHealth( e, 0 ) end end if g_KeyPressE == 0 then vpressed = false end end function EntityLooking( Ent, tEnt, fov ) v = v or 1 local function limitAngle ( Angle ) while Angle < 0 do Angle = 360 + Angle end while Angle > 360 do Angle = Angle - 360 end return Angle end local angle = limitAngle( atan( tEnt.x - Ent.x, tEnt.z - Ent.z ) * ( 180.0 / math.pi ) ) local tAng = limitAngle( Ent.angley ) local L = limitAngle( angle - fov / 2 ) local R = limitAngle( angle + fov / 2 ) return ( L < R and ( tAng > L and tAng < R ) ) or ( L > R and ( tAng > L or tAng < R ) ) end