Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new function isPlayerCrosshairVisible #3720

Merged

Conversation

FileEX
Copy link
Contributor

@FileEX FileEX commented Sep 13, 2024

This PR adds the function isPlayerCrosshairVisible, which allows checking if the crosshair is actually visible on the screen. Sometimes, checking if a player is aiming based on tasks can be inaccurate, as simply holding the mouse button for a second can falsely indicate that the player is aiming. This function was created to address the issue reported here, and I believe it can also help in detecting aimbots more easily. For instance, if a player is hitting headshots without even seeing the crosshair, it's a clear sign that something might be wrong with him.

If the crosshair is hidden using setPlayerHudComponentVisible, the function returns false.

I have one question though: The function currently returns true for weapons like the spraycan and fire extinguisher. In reality, the crosshair is intentionally hidden for these weapons in GTA’s code. Should i leave it as it is, or should i add a special condition to always return false for these two weapons?

Fixes #3711

@tederis tederis added the enhancement New feature or request label Sep 13, 2024
@Dutchman101
Copy link
Member

Dutchman101 commented Sep 28, 2024

The below script (from me) is 100% accurate to stop "zero recoil GTA bug", by blocking fire without aiming. I've not spotted task states inconsistency with well-implemented scripts.

--- Put weapons here, set to true if you want hipfire blocked for it
-- @warning don't use 0 or 36 as they are already blocked
local antiHipfireWeapons = {
    [22] = true,
    [23] = true,
    [24] = true,
    [25] = true,
    [26] = true,
    [27] = true,
    [28] = true,
    [29] = true,
    [30] = true,
    [31] = true,
    [32] = true,
    [33] = true,
    [38] = true,
}

-- So we can't lose track of our state if some other script decides to use toggleControl
-- or when this script gets stopped we need to reset the state
-- this is set true when we're using it, false when we're not
local fireControlLock = false
-- Time since last antiHipfire check
local lastTick = 0
-- Time interval between each hipfire check
local checkInterval = 300

--- Disable hipfire for certain weapons
local function antiHipfire()
    local curTick = getTickCount()
    if curTick - lastTick > checkInterval then
        if getPedOccupiedVehicle(localPlayer) then
            return
        end

        local wp = getPedWeapon(localPlayer)

        -- Stop here if it's not anti hipfire weapon
        if not antiHipfireWeapons[wp] and fireControlLock then
            toggleControl("fire", true)
            fireControlLock = false
            return
        end

        local aiming = getPedControlState(localPlayer, "aim_weapon")
        if antiHipfireWeapons[wp] and isControlEnabled("fire") ~= aiming then
            toggleControl("fire", aiming)
            fireControlLock = not aiming
        end

        lastTick = curTick
    end
end
addEventHandler("onClientPreRender", root, antiHipfire)

-- If we stop this resource make sure we undo toggleControl
addEventHandler("onClientResourceStop", resourceRoot, function ()
    if fireControlLock then
        toggleControl("fire", true)
    end
end)

@derxgbb
Copy link

derxgbb commented Sep 28, 2024

The problem is that ControlState "aim_weapon" and task "TASK_SIMPLE_USE_GUN" returns true at the start of aiming when the aiming animation just started, so even with this code its possible to shoot without a fully visible crosshair. If you use a sawed, your first shoot won't have problem, but the second shoot will have no-recoil and it will be shot at head level. (if you have low fps settings, you can make this with the first shoot too)

But. Your code still fixes a problem: If you press TAB it opens the scoreboard. Hold TAB, then press right click, you will now shoot, because TAB also fires the gun. You'll have a cursor as well, because scoreboard has settings. If you move the cursor to the center of the scoreboard and stop holding the TAB, the crosshair and scoreboard will disappear but you will be able to shoot if you press left click, or ctrl, without crosshair, so without recoil.
Your code fixes this problem :)
(I hope I described it well)

@Dutchman101
Copy link
Member

I see.

This will be nice to have in the next main rolling update..

@Dutchman101 Dutchman101 merged commit 03e851a into multitheftauto:master Sep 28, 2024
6 checks passed
@FileEX FileEX deleted the feature/isPlayerCrosshairVisible branch September 29, 2024 13:10
@FileEX FileEX restored the feature/isPlayerCrosshairVisible branch October 1, 2024 22:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Ability to detect when the crosshair is shown
5 participants