persistidents 0

// Initialize the list (if it doesn't already exist) using the current gamma as initial values.
if (! (checkalias ofc_gamma_list)) [
  ofc_gamma_list = []
  loop ogl (listlen $defaultmaps) [
    add2list ofc_gamma_list $gamma
  ]
]

// setgamma - allows clients to set custom gamma values for each official map, disabling/enabling the entire system
//            can be done with the dyngamma variable.
const setgamma [
  if (checkrange $arg1 30 300) [
    if (strstr $defaultmaps (curmap 1)) [
      tmp_gamma_list = $ofc_gamma_list
      ofc_gamma_list = []
      cmapp = (findlist $defaultmaps (curmap 1)) // Find the position in the list of the current map
      loop sgl (listlen $defaultmaps) [
        if (= $sgl $cmapp) [ // Add the new gamma at the correct position in the list
          add2list ofc_gamma_list $arg1
        ] [
          add2list ofc_gamma_list (at $tmp_gamma_list $sgl)
        ]
      ]
      echo (blue)Note: (white) $arg1 has been set as your default gamma for (yellow) (curmap 1)
    ] [ echo (red)Error: (white)You are not on a official map! ]
  ] [ echo (red)Error: (white)gamma has a minimum value of 30 and maximum value of 300! ]
]

const applygamma [ if (strstr $defaultmaps (curmap 1)) [ gamma (at $ofc_gamma_list (findlist $defaultmaps (curmap 1))) ] ]

// Plugin to mapstartalways on every run if it doesn't already exist.
check2init dyngamma 0
checkinit mapstartalways [if $dyngamma applygamma]

persistidents 1
