-- LUA Script - precede every function and global member with lowercase name of script + '_main' -- Player Collects Weapon local U = require "scriptbank\\utillib" ------------------------ -- user editable bits -- ------------------------ local max_Weapons = 2 -- nuimber of weapons the player can carry ------------------------------------------------------- -- Edit anything below here at your own peril. :-) -- ------------------------------------------------------- local weapons = {} local weapon_therecanbeonlyone = 0 function weapon_init_name( e, name ) Include( "utillib.lua" ) weapons[ e ] = { name = name, state = "wait pick up", obj = g_Entity[ e ].obj } end local function getSlot( wId ) for a = 1, 10 do if GetWeaponSlot( a ) == wId then return a end end end local function getNumWpns() local have_weapons = 0 for a = 1, max_Weapons do if GetWeaponSlot( a ) ~= 0 then have_weapons = have_weapons + 1 end end return have_weapons end local function getWeaponName( wId ) for _, v in pairs( weapons ) do if v.id == wId and v.state == 'carrying' then return v.name end end end local function haveWeapon( name ) for _, v in pairs( weapons ) do if v.name == name and v.state == 'carrying' then return true end end return false end function weapon_main(e) local wpn = weapons[ e ] if wpn == nil then return end local wpnId = GetPlayerWeaponID() local slot = getSlot( wpnId ) if slot > max_Weapons then ChangePlayerWeaponID( GetWeaponSlot( 1 ) ) wpnId = GetPlayerWeaponID() slot = 1 end if wpn.state == 'carrying' then PositionObject( wpn.obj, g_PlayerPosX + wpn.px, g_PlayerPosY + 200, g_PlayerPosZ + wpn.pz ) elseif wpn.state == "wait pick up" then if weapon_therecanbeonlyone == -1 then if g_KeyPressE == 0 and g_InKey == "" then weapon_therecanbeonlyone = 0 end end if U.PlayerLookingNear( e, 150, 40 ) then if weapon_therecanbeonlyone == 0 then weapon_therecanbeonlyone = e end if weapon_therecanbeonlyone == e then local have_weapons = getNumWpns() local wName = getWeaponName( wpnId ) or 'something' if have_weapons < max_Weapons and wName ~= wpn.name then Prompt( "Press E To pick up the " .. wpn.name ) if g_KeyPressE == 1 then PromptDuration( "Collected the " .. wpn.name, 1000 ) PlaySound( e, 0 ) wpn.state = "picking up" AddPlayerWeapon( e ) ActivateIfUsed( e ) weapon_therecanbeonlyone = -1 end else if wName ~= wpn.name and haveWeapon( wpn.name ) then Prompt( "Already Have " .. wpn.name ) else Prompt( "Press E to replace " .. wName .. " with " .. wpn.name ) if g_KeyPressE == 1 then local tX, tY, tZ, tAX, tAY, tAZ = GetObjectPosAng( wpn.obj ) for a, b in pairs( weapons ) do if a ~= e and b.id == wpnId and b.state == 'carrying' then b.state = 'dropping' b.pos = { x = tX, y = tY, z = tZ, Ax = tAX, Ay = tAY, Az = tAZ } b.ammo = GetWeaponAmmo( slot ) local pool = GetFireModeSettingsPoolIndex( wpnId ) if pool ~= nil then b.clip = GetWeaponPoolAmmo( pool ) end break end end PromptDuration( "Swapped the " .. wName .. " with " .. wpn.name, 1000 ) PlaySound( e, 0 ) wpn.state = 'picking up' ReplacePlayerWeapon( e ) ActivateIfUsed( e ) weapon_therecanbeonlyone = -1 end end end end elseif weapon_therecanbeonlyone == e then weapon_therecanbeonlyone = 0 end elseif wpn.state == 'dropping' then local p = wpn.pos PositionObject( wpn.obj, p.x, p.y, p.z ) RotateObject( wpn.obj, p.Ax, p.Ay, p.Az ) CollisionOn( e ) Show( e ) wpn.state = 'wait pick up' elseif wpn.state == 'picking up' then wpn.id = GetPlayerWeaponID() SetGamePlayerStatePlrReloading( 1 ) wpn.state = 'carrying' wpn.px, wpn.pz = U.RandomPos( 50 ) wpn.px = wpn.px - g_PlayerPosX wpn.pz = wpn.pz - g_PlayerPosZ Hide( e ) CollisionOff( e ) if wpn.ammo ~= nil then local slot = getSlot( wpn.id ) SetWeaponAmmo( slot, wpn.ammo ) local pool = GetFireModeSettingsPoolIndex( wpn.id ) if pool ~= nil then SetWeaponPoolAmmo( pool, wpn.clip ) end end end if wpn.ammo ~= nil then PromptLocal( e, wpn.ammo .. " / " .. wpn.clip ) end -- PromptLocal( e, wpn.state .. ", " .. ( wpn.id or 'nil' ) ) end