- name: containers
  regex: '(?-s)capacity="(?<capacity>!num)"(?:!anyslotsperrow="(?<slotsperrow>!num))?'
  lua: |
    if file:find("fabricators.xml") then return false end
    if capacity == 1 then return false end
    capacity=round(capacity*2.2)
    if not slotsperrow then return true end
    slotsperrow=1
    while slotsperrow * slotsperrow < capacity do
      slotsperrow = slotsperrow + 1
    end
  files:
    - "**/*.xml"

- name: containerscontainable
  regex: (?-s)Containable!anyitems="([^"]+)"
  lua: |
    -- If we fuck with the diving gear the game fucking explodes
    -- Thinking we don't have oxygen tanks and killing us
    if file:find("divinggear.xml") then
      return false
    end
    local gurantee = {"ammobox", "deepdiving", "smallitem", "mediumitem", "largeitem"}
    local has = {}

    local items = strsplit(s1, ",")
    for i, item in pairs(items) do
      for _, g in pairs(gurantee) do
        if string.find(item, g) then
          has[g] = true
        end
      end
    end

    for _, g in pairs(gurantee) do
      if not has[g] then
        s1 = s1 .. "," .. g
      end
    end
  files:
    - "**/*.xml"

- name: excludeditems
  regex: <Containable!any(\s?excludeditems="[^"]+")
  lua: |
    s1 = ""
  files:
    - "**/*.xml"

- name: upgrade
  regex: (?:maxforce|capacity|maxrechargespeed|overloadvoltage|healthmultiplier|maxhealth|fabricationspeed|deconstructionspeed|rotationspeedlowskill|offsetonselectedmultiplier|range|maxpoweroutput|meltdowndelay|maxflow|fixdurationlowskill|skillrequirementmultiplier)="\+?!num%?"
  lua: |
    *2
  files:
    - "**/UpgradeModules.xml"
- name: upgradetooltip
  regex: 'increaseontooltip="(?<increaseontooltip>!num)"'
  lua: |
    increaseontooltip=increaseontooltip*2
  files:
    - "**/UpgradeModules.xml"

- name: missionreward
  regex: 'reward="!num'
  lua: |
    *4
  files:
    - "**/Missions.xml"

- name: maxstacksize
  regex: 'maxstacksize="!num'
  lua: |
    *8
  files:
    - "**/*.xml"

- name: maxstacksizecharacterinventory
  regex: 'maxstacksizecharacterinventory="!num'
  lua: |
    *8
  files:
    - "**/*.xml"

- name: lightrange
  regex: '(?-s)LightComponent!anyrange="(!num)"'
  lua: |
    *4
  files:
    - "**/*.xml"

- name: toolrange
  regex: (?-s)RepairTool!anyrange="!num"
  lua: |
    *4
  files:
    - "**/*.xml"

- name: talentnoblocks
  isolate: true
  regex: (\s?blocks="[^"]+")
  lua: |
    s1 = ""
  files:
    - "**/TalentTrees.xml"

- name: maxchosentalents
  isolate: true
  regex: (<TalentOptions!any</TalentOptions>)
  lua: |
    local talentOptions = 0
    local lines = strsplit(s1, "\n")
    for i, line in pairs(lines) do
      if string.find(line, "<TalentOption ") then
        talentOptions = talentOptions + 1
      end
    end

    if lines[1]:find("maxchosentalents") then
      --print("maxchosentalents found, updating")
      local optionsElement = lines[1]
      optionsElement = optionsElement:gsub("maxchosentalents=\"%d+\"", "maxchosentalents=\"" .. talentOptions .. "\"")
      replacement = s1:gsub(lines[1], optionsElement)
      return false
    end

    print("maxchosentalents not found, creating")
    local optionsElement = lines[1]
    local optionsElement = optionsElement:gsub("<TalentOptions", "<TalentOptions maxchosentalents=\"" .. talentOptions .. "\"")
    replacement = s1:gsub(lines[1], optionsElement)
  files:
    - "**/TalentTrees.xml"

- name: norequiredtalents
  isolate: true
  regex: (?-s)(<TalentOptions )(?:!anyrequiredtalents="(?<requiredtalents>!num)")?
  lua: |
    if requiredtalents then
      requiredtalents = 1
    else
      print("requiredtalents not found, creating")
      replacement = s1 .. "requiredtalents=\"1\" "
    end
  files:
    - "**/TalentTrees.xml"

- name: alltalents
  isolate: true
  regex: <TalentTrees>(!any)</TalentTrees>
  lua: |
    -- group by job
    local subtrees = {}
    local lines = strsplit(s1, "\n")
    local cookedlines = {}

    local isInSubtree = false
    local currentjob = nil
    for i, line in pairs(lines) do
      if line:match("<TalentTree jobidentifier=\"(%w+)\"") then
        currentjob = line:match("<TalentTree jobidentifier=\"(%w+)\"")
        subtrees[currentjob] = subtrees[currentjob] or {}
      end
      if line:find("<SubTree") then
        isInSubtree = true
      end
      if line:find("</SubTree") then
        isInSubtree = false
        subtrees[currentjob][#subtrees[currentjob] + 1] = line
      end
      if isInSubtree then
        subtrees[currentjob][#subtrees[currentjob] + 1] = line
      else
        if not line:find("SubTree") then
          cookedlines[#cookedlines + 1] = line
        end
      end
    end

    currentjob = nil
    local alltalents = {}
    for i, line in pairs(cookedlines) do
      alltalents[#alltalents + 1] = line
      if line:match("<TalentTree jobidentifier=\"(%w+)\"") then
        currentjob = line:match("<TalentTree jobidentifier=\"(%w+)\"")

        if currentjob then
          for job, subtree in pairs(subtrees) do
            alltalents[#alltalents + 1] = table.concat(subtree, "\n")
          end
        end
      end
    end

    s1 = table.concat(alltalents, "\n")
  files:
    - "Content/**/TalentTrees.xml"

- name: merchantstock
  regex: (?-s)<Price(?:!anymultiplier="(?<multiplier>!num)")?(?:!anyminavailable="(?<minavailable>!num)")?
  lua: |
    if multiplier then
      multiplier = round(multiplier * 3, 2)
    end
    if minavailable then
      minavailable = round(minavailable * 3, 2)
    end
  files:
    - "**/*.xml"

- name: repairthroughwalls
  regex: <RepairTool!anyrepairthroughwalls="(?<repairthroughwalls>!any)"
  lua: |
    repairthroughwalls = "true"
  files:
    - "**/*.xml"

- name: itembonanzaamount
  regex: (?-s)(?i)<Preferredcontainer!anyamount="!num"
  lua: |
    v1 = round(v1 * 2, 2)
  files:
    - "**/*.xml"
- name: itembonanzaspawnprobability
  regex: (?-s)(?i)<Preferredcontainer!anyspawnprobability="!num"
  lua: |
    v1 = round(v1 * 2, 2)
    if v1 > 1 then
      v1 = 1
    end
  files:
    - "**/*.xml"
- name: itembonanzaminamount
  regex: (?-s)(?i)<Preferredcontainer!anyminamount="!num"
  lua: |
    v1 = round(v1 * 2, 2)
  files:
    - "**/*.xml"
- name: itembonanzamaxamount
  regex: (?-s)(?i)<Preferredcontainer!anymaxamount="!num"
  lua: |
    v1 = round(v1 * 2, 2)
  files:
    - "**/*.xml"
