--@name No flash
--@version 1.0.0
--@author wolfhack docs
--@api 1.1
--@permissions memory,memory_write
--@description Keeps your flashbang alpha at 0 so a flash does not whiten the screen. Writes game memory: read the warning in the guide first.
-- Needs three things before it does anything: (1) this script's memory and memory_write permissions granted on the Scripts page,
-- (2) the master switch "Let scripts write game memory" on the Scripts page, (3) a match. When you switch the script off the host puts
-- the original value back by itself.

local OFFSET_KEY = "C_CSPlayerPawnBase->m_flFlashMaxAlpha"
local off                                        -- looked up once the overlay knows it

ui.tab("No flash", function()
  ui.card("No flash", function()
    ui.toggle("enabled", "No flash", true)
    local can = mem.can_write()
    ui.note(can and "Writes are on." or "Writes are off: grant memory_write to this script and switch on the master switch.")
  end)
end)

events.on("frame", function()
  if not store.get("enabled", true) then
    if mem.writes() > 0 then mem.undo_all() end   -- toggled off: put the game's value back at once
    return
  end
  if not mem.can_write() then return end          -- permission or master switch missing: do nothing (no error spam)

  off = off or mem.offset(OFFSET_KEY)
  local pawn = mem.global("local_pawn")
  if not (off and pawn) then return end           -- no offset yet, or not in a match

  local alpha = mem.read_f32(pawn + off)
  if alpha and alpha > 0 then
    mem.write_f32(pawn + off, 0.0)
  end
end)
