<eval index="1" phase="First" priority="10000"><![CDATA[
      ~ Set our Minimum bound.
      if (tagexpr[Helper.AttrPhyAtt | Helper.AttrMenAtt] <> 0) then
        field[abUsrMin].value = 2
      else
        field[abUsrMin].value = 1
        endif
      ~ This is the number of picks we are allowed for each category
      if (tagis[Helper.WepAttune] <> 0) then
        #resspent[resWepAtNm] += 1
      elseif (tagis[Helper.ArmAttune] <> 0) then
        #resspent[resArmAtNm] += 1
      elseif (tagis[Helper.AttrPhyAtt] <> 0) then
        #resspent[resPhyAtNm] += 1
      elseif (tagis[Helper.AttrMenAtt] <> 0) then
        #resspent[resMenAtNm] += 1
        endif
      ~ This is the value of each of the picks.
      ~ Weapon and armor attunements can be split into a primary and secondary grouping
      if (tagis[Helper.WepAttune] <> 0) then
        ~ If nothing has added to the primary resource tracker yet, then we are
        ~ the first, so add to that!
        if (#resspent[resWepPrMx] = 0) then
          #resspent[resWepPrMx] += field[abUserVal].value
        ~ Otherwise, add to the secondary
        else
          #resspent[resWepScMx] += field[abUserVal].value
          endif
      elseif (tagis[Helper.ArmAttune] <> 0) then
        ~ If nothing has added to the primary resource tracker yet, then we are
        ~ the first, so add to that!
        if (#resspent[resArmPrMx] = 0) then
          #resspent[resArmPrMx] += field[abUserVal].value
        ~ Otherwise, add to the secondary
        else
          #resspent[resArmScMx] += field[abUserVal].value
          endif
      ~ For attribute enhancements, we count anything beyond our initial value (2) as an upgrade
      elseif (tagis[Helper.AttrPhyAtt] <> 0) then
        #resspent[resPhyAtUp] += (field[abUserVal].value/2) - 1
      elseif (tagis[Helper.AttrMenAtt] <> 0) then
        #resspent[resMenAtUp] += (field[abUserVal].value/2) - 1
        endif
      ]]></eval>
    <eval index="2" phase="PostLevel" priority="10000" name="Attunements Apply Enhancement Bonuses"><![CDATA[
      field[abValue].value += field[abUserVal].value
      doneif (field[usrChosen1].ischosen = 0)
      perform field[usrChosen1].chosen.setfocus
      ~ Armor attunements can be applied to whatever clothing is worn
      if (field[usrChosen1].chosen.tagis[thingid.selClothin] <> 0) then
        perform hero.child[ArmorClass].setfocus
        endif
      ~ Can't be more than 10. That is the max legendary gifts can bring us to.
      field[abValue].value = minimum(field[abValue].value,10)
      ~ The attunement bonus we apply is offset by any item powers on the target
      ~ item.
      ~ This only applies to weapon/armor attunements
      if (tagexpr[Helper.ArmAttune | Helper.WepAttune] <> 0) then
        if (focus.tagis[component.BaseMagicI] <> 0) then
          field[abValue].value -= focus.field[iPowEqiBon].value
          endif
        ~ We can't have more than 5 enhancement bonus left over to apply to
        ~ attack and damage, regardless of how much was or was not taken up by
        ~ the selection's item powers!
        field[abValue].value = minimum(field[abValue].value,5)
        endif
      ~ Can't be less than 0. Our attunement never makes us take a negative.
      field[abValue].value = maximum(field[abValue].value,0)
      #enhancementbonus[focus, field[abValue].value]
      ~ If this is selecting a mental attribute, and that attribute is Int, then
      ~ there are some other things which accompany permanent increases, like
      ~ bonus skill points and languages
      if (tagis[Helper.AttrMenAtt] <> 0) then
        if (focus.tagis[IsAttr.aINT] <> 0) then
          ~ We get half our value as bonus languages
          herofield[tLangsSta].value += round(field[abValue].value/2,0,-1)
          ~ And half our value x our hit dice as extra skill points
          #resmax[resSkill] += round(field[abValue].value/2,0,-1) * herofield[tHitDice].value
          endif
        endif
      ]]></eval>
    <eval index="3" phase="Final" priority="999"><![CDATA[
      if (tagis[Helper.WepAttune] <> 0) then
        field[usrCandid1].text = "component.BaseWep & !Helper.Static"
        ~ We can only target manufactured weapons, but Unarmed strikes count as
        ~ manufactured for monks (or other classes which gain their unarmed
        ~ strike abilities, so enable or forbid them accordingly).
        if (hero.tagis[Ability.cMnkUnarm] = 0) then
          field[usrCandid1].text &= " & !thingid.wUnarmed"
          endif
      elseif (tagis[Helper.ArmAttune] <> 0) then
        field[usrCandid1].text = "(component.BaseArmor & !Helper.Helper & !Helper.Static) | thingid.selClothin"
      elseif (tagis[Helper.AttrPhyAtt] <> 0) then
        field[usrCandid1].text = "component.BaseAttr & AttribCat.Physical"
      elseif (tagis[Helper.AttrMenAtt] <> 0) then
        field[usrCandid1].text = "component.BaseAttr & AttribCat.Mental"
        endif
      ]]></eval>
    <eval index="4" phase="Render" priority="1000000"><![CDATA[
      doneif (field[usrChosen1].ischosen = 0)
      perform field[usrChosen1].chosen.setfocus
      field[livename].text = focus.field[livename].text
      ]]></eval>