<Library Name="charlib">
    <Functions>
        <Function Name="charlib_cost_a">
            <Return>Max(1, [param.0] - 11)</Return>
        </Function>
        <Function Name="charlib_cost_b">
            <Return>Max(1, [param.0] - 11) * 2</Return>
        </Function>
        <Function Name="charlib_cost_c">
            <Return>Max(1, [param.0] - 11) * 3</Return>
        </Function>
        <Function Name="charlib_cost_d">
            <Return>Max(1, [param.0] - 11) * 4</Return>
        </Function>
        <Function Name="charlib_cost_e">
            <Return>Max(1, [param.0] - 11) * 5</Return>
        </Function>
        <Function Name="charlib_cost_f">
            <Return>Max(1, [param.0] - 11) * 6</Return>
        </Function>
        <Function Name="charlib_cost_attr">
            <Return>Max(1, [param.0] - 13) * 15</Return>
        </Function>
        <Function Name="charlib_cost_mana">
            <Actions>
                <Action>
                    <Do>2</Do>
                    <Output>[local.price]</Output>
                </Action>
                <Action>
                    <If>[param.0] > 20</If>
                    <Do>3</Do>
                    <Output>[local.price]</Output>
                </Action>
                <Action>
                    <If>[param.0] > 25</If>
                    <Do>4</Do>
                    <Output>[local.price]</Output>
                </Action>
                <Action>
                    <If>[param.0] > 30</If>
                    <Do>5</Do>
                    <Output>[local.price]</Output>
                </Action>
            </Actions>
            <Return>[local.price]</Return>
        </Function>
        <!--<Function Name="charlib_cost_b">
            <Actions>
                <Action>
                    <If>[param.0] &lt;= 12</If>
                    <Do>1</Do>
                    <Output>[local.cost]</Output>
                </Action>
                <Action>
                    <If>[param.0] > 12</If>
                    <Do>[param.0] - 11</Do>
                    <Output>[local.cost]</Output>
                </Action>
            </Actions>
            <Return>[local.cost]</Return>
        </Function>-->

        <Function Name="charlib_magiccatprice_sum">
            <Actions>
                <Action>
                    <Do>0</Do>
                    <Output>[local.sum]</Output>
                </Action>
                <ForLoop>
                    <LoopCount>[param.0] + 1</LoopCount>
                    <Loop>
                        <Action>
                            <Do>[local.sum] + charlib_magiccatprice([param.loop])</Do>
                            <Output>[local.sum]</Output>
                        </Action>
                        <Log>
                            <Text>[local.sum]</Text>
                        </Log>
                    </Loop>
                </ForLoop>
            </Actions>
            <Return>[local.sum]</Return>
        </Function>
        <Function Name="charlib_magiccatprice">
            <Actions>
                <Action>
                    <If>[param.0] == 0</If>
                    <Do>20</Do>
                    <Output>[local.price]</Output>
                </Action>
                <Action>
                    <If>[param.0] == 1</If>
                    <Do>5</Do>
                    <Output>[local.price]</Output>
                </Action>
                <Action>
                    <If>[param.0] == 2</If>
                    <Do>10</Do>
                    <Output>[local.price]</Output>
                </Action>
                <Action>
                    <If>[param.0] == 3</If>
                    <Do>15</Do>
                    <Output>[local.price]</Output>
                </Action>
                <Action>
                    <If>[param.0] == 4</If>
                    <Do>20</Do>
                    <Output>[local.price]</Output>
                </Action>
            </Actions>
            <Return>[local.price]</Return>
        </Function>

        <Function Name="charlib_checkpreprint">
            <Actions>
                <Action>
                    <Do>true</Do>
                    <Output>[local.check]</Output>
                </Action>
                <Action>
                    <If>[data.character.control.xp] &gt; 10</If>
                    <Do>false</Do>
                    <Output>[local.check]</Output>
                </Action>
                <Action>
                    <If>[data.character.control.xp] &lt; 0</If>
                    <Do>false</Do>
                    <Output>[local.check]</Output>
                </Action>
                <Action>
                    <If>[data.character.control.coins] &lt; 0</If>
                    <Do>false</Do>
                    <Output>[local.check]</Output>
                </Action>
            </Actions>
            <Return>[local.check]</Return>
        </Function>

        <Function Name="charlib_maxhp">
            <Return>[data.character.attributes.physique] + [data.character.attributes.physique] + [data.character.attributes.strength] + [data.character.race.healthbonus]</Return>
        </Function>
        <Function Name="charlib_intrinsicmagic">
            <Actions>
                <Action>
                    <Do>if([data.character.attributes.courage] >= 10, 0.25, 0) + if([data.character.attributes.intelligence] >= 10, 0.25, 0) + if([data.character.attributes.intuition] >= 10, 0.25, 0) + if([data.character.attributes.charisma] >= 10, 0.25, 0) + if([data.character.attributes.dexterity] >= 10, 0.25, 0) + if([data.character.attributes.agility] >= 10, 0.25, 0) + if([data.character.attributes.physique] >= 10, 0.25, 0) + if([data.character.attributes.strength] >= 10, 0.25, 0)</Do>
                    <Output>[local.sum10]</Output>
                </Action>
                <Action>
                    <Do>if([data.character.attributes.courage] >= 12, 0.5, 0) + if([data.character.attributes.intelligence] >= 12, 0.5, 0) + if([data.character.attributes.intuition] >= 12, 0.5, 0) + if([data.character.attributes.charisma] >= 12, 0.5, 0) + if([data.character.attributes.dexterity] >= 12, 0.5, 0) + if([data.character.attributes.agility] >= 12, 0.5, 0) + if([data.character.attributes.physique] >= 12, 0.5, 0) + if([data.character.attributes.strength] >= 12, 0.5, 0)</Do>
                    <Output>[local.sum12]</Output>
                </Action>
                <Action>
                    <Do>if([data.character.attributes.courage] >= 14, 0.75, 0) + if([data.character.attributes.intelligence] >= 14, 0.75, 0) + if([data.character.attributes.intuition] >= 14, 0.75, 0) + if([data.character.attributes.charisma] >= 14, 0.75, 0) + if([data.character.attributes.dexterity] >= 14, 0.75, 0) + if([data.character.attributes.agility] >= 14, 0.75, 0) + if([data.character.attributes.physique] >= 14, 0.75, 0) + if([data.character.attributes.strength] >= 14, 0.75, 0)</Do>
                    <Output>[local.sum14]</Output>
                </Action>
                <Action>
                    <Do>if([data.character.attributes.courage] >= 16, 1, 0) + if([data.character.attributes.intelligence] >= 16, 1, 0) + if([data.character.attributes.intuition] >= 16, 1, 0) + if([data.character.attributes.charisma] >= 16, 1, 0) + if([data.character.attributes.dexterity] >= 16, 1, 0) + if([data.character.attributes.agility] >= 16, 1, 0) + if([data.character.attributes.physique] >= 16, 1, 0) + if([data.character.attributes.strength] >= 16, 1, 0)</Do>
                    <Output>[local.sum16]</Output>
                </Action>
                <Action>
                    <Do>Truncate(Min([local.sum10], 1.5) + Min([local.sum12], 2) + Min([local.sum14], 1.5) + Min([local.sum16], 1))</Do>
                    <Output>[local.magic]</Output>
                </Action>
            </Actions>
            <Return>[local.magic]</Return>
        </Function>
        <Function Name="charlib_fightcourageadd">
            <Return>Floor(([data.character.attributes.courage] - 8) / 3)</Return>
        </Function>
        <Function Name="charlib_fightdexterityadd">
            <Return>Floor(([data.character.attributes.dexterity] - 8) / 3)</Return>
        </Function>
        <Function Name="charlib_fightagilityadd">
            <Return>Floor(([data.character.attributes.agility] - 8) / 3)</Return>
        </Function>
        <Function Name="charlib_fightstrengthadd">
            <Return>Floor(([data.character.attributes.strength] - 8) / 3)</Return>
        </Function>
        <!-- AT Functions -->
        <Function Name="charlib_crossbows_at">
            <Return>[data.character.fight.crossbows] + charlib_fightdexterityadd()</Return>
        </Function>
        <Function Name="charlib_blowpipe_at">
            <Return>[data.character.fight.blowpipe] + charlib_fightdexterityadd()</Return>
        </Function>
        <Function Name="charlib_bows_at">
            <Return>[data.character.fight.bows] + charlib_fightdexterityadd()</Return>
        </Function>
        <Function Name="charlib_daggers_at">
            <Return>[data.character.fight.daggers] + charlib_fightcourageadd()</Return>
        </Function>
        <Function Name="charlib_impactweapons_at">
            <Return>[data.character.fight.impactweapons] + charlib_fightcourageadd()</Return>
        </Function>
        <Function Name="charlib_chainweapons_at">
            <Return>[data.character.fight.chainweapons] + charlib_fightcourageadd()</Return>
        </Function>
        <Function Name="charlib_lances_at">
            <Return>[data.character.fight.lances] + charlib_fightcourageadd()</Return>
        </Function>
        <Function Name="charlib_brawling_at">
            <Return>[data.character.fight.brawling] + charlib_fightcourageadd()</Return>
        </Function>
        <Function Name="charlib_shields_at">
            <Return>[data.character.fight.shields] + charlib_fightcourageadd()</Return>
        </Function>
        <Function Name="charlib_swords_at">
            <Return>[data.character.fight.swords] + charlib_fightcourageadd()</Return>
        </Function>
        <Function Name="charlib_throwingweapons_at">
            <Return>[data.character.fight.throwingweapons] + charlib_fightdexterityadd()</Return>
        </Function>
        <!-- PA Functions -->
        <Function Name="charlib_daggers_pa">
            <Return>[data.character.fight.daggers] / 2 + charlib_fightagilityadd()</Return>
        </Function>
        <Function Name="charlib_impactweapons_pa">
            <Return>[data.character.fight.impactweapons] / 2 + charlib_fightstrengthadd()</Return>
        </Function>
        <Function Name="charlib_chainweapons_pa">
            <Return>[data.character.fight.chainweapons] / 2 + charlib_fightstrengthadd()</Return>
        </Function>
        <Function Name="charlib_lances_pa">
            <Return>[data.character.fight.lances] / 2 + charlib_fightstrengthadd()</Return>
        </Function>
        <Function Name="charlib_brawling_pa">
            <Return>[data.character.fight.brawling] / 2 + charlib_fightstrengthadd()</Return>
        </Function>
        <Function Name="charlib_shields_pa">
            <Return>[data.character.fight.shields] / 2 + charlib_fightstrengthadd()</Return>
        </Function>
        <Function Name="charlib_swords_pa">
            <Return>[data.character.fight.swords] / 2 + charlib_fightagilityadd()</Return>
        </Function>

        <Function Name="charlib_maininv_borders">
            <Actions>
                <Action>
                    <Do>[data.character.attributes.strength] * 0.15 + 4</Do>
                    <Output>[data.character.print.borderp2]</Output>
                </Action>
                <Action>
                    <Do>[data.character.attributes.strength] * 0.25 + 5</Do>
                    <Output>[data.character.print.borderp1]</Output>
                </Action>
                <Action>
                    <Do>[data.character.attributes.strength] * 0.4 + 6</Do>
                    <Output>[data.character.print.borderp0]</Output>
                </Action>
                <Action>
                    <Do>[data.character.attributes.strength] * 0.45 + 7</Do>
                    <Output>[data.character.print.borderm1]</Output>
                </Action>
                <Action>
                    <Do>[data.character.attributes.strength] * 0.5 + 8</Do>
                    <Output>[data.character.print.borderm2]</Output>
                </Action>
                <Action>
                    <Do>[data.character.attributes.strength] * 0.55 + 9</Do>
                    <Output>[data.character.print.borderm3]</Output>
                </Action>
            </Actions>
        </Function>
        <Function Name="charlib_maininv_aw">
            <Actions>
                <Action>
                    <Do>"X"</Do>
                    <Output>[local.aw]</Output>
                </Action>
                <Action>
                    <If>[param.0] &lt;= [data.character.print.borderm3]</If>
                    <Do>"-3"</Do>
                    <Output>[local.aw]</Output>
                </Action>
                <Action>
                    <If>[param.0] &lt;= [data.character.print.borderm2]</If>
                    <Do>"-2"</Do>
                    <Output>[local.aw]</Output>
                </Action>
                <Action>
                    <If>[param.0] &lt;= [data.character.print.borderm1]</If>
                    <Do>"-1"</Do>
                    <Output>[local.aw]</Output>
                </Action>
                <Action>
                    <If>[param.0] &lt;= [data.character.print.borderp0]</If>
                    <Do>"0"</Do>
                    <Output>[local.aw]</Output>
                </Action>
                <Action>
                    <If>[param.0] &lt;= [data.character.print.borderp1]</If>
                    <Do>"+1"</Do>
                    <Output>[local.aw]</Output>
                </Action>
                <Action>
                    <If>[param.0] &lt;= [data.character.print.borderp2]</If>
                    <Do>"+2"</Do>
                    <Output>[local.aw]</Output>
                </Action>
            </Actions>
            <Return>[local.aw]</Return>
        </Function>
        <Function Name="charlib_maininv_gs">
            <Actions>
                <Action>
                    <Do>"X"</Do>
                    <Output>[local.gs]</Output>
                </Action>
                <Action>
                    <If>[param.0] &lt;= [data.character.print.borderm3]</If>
                    <Do>"-1"</Do>
                    <Output>[local.gs]</Output>
                </Action>
                <Action>
                    <If>[param.0] &lt;= [data.character.print.borderm1]</If>
                    <Do>"0"</Do>
                    <Output>[local.gs]</Output>
                </Action>
                <Action>
                    <If>[param.0] &lt;= [data.character.print.borderp2]</If>
                    <Do>"+1"</Do>
                    <Output>[local.gs]</Output>
                </Action>
            </Actions>
            <Return>[local.gs]</Return>
        </Function>
        <Function Name="charlib_maininv">
            <Actions>
                <Action>
                    <Do>charlib_maininv_gs([param.0])</Do>
                    <Output>[local.gs]</Output>
                </Action>
                <Action>
                    <Do>charlib_maininv_aw([param.0])</Do>
                    <Output>[local.aw]</Output>
                </Action>
                <AddListEntry>
                    <List>[data.character.print.maininventory]</List>
                    <PropertyOutputs>
                        <PropertyOutput>
                            <Property>[local.gs]</Property>
                            <Output>[gs]</Output>
                        </PropertyOutput>
                        <PropertyOutput>
                            <Property>[local.aw]</Property>
                            <Output>[aw]</Output>
                        </PropertyOutput>
                        <PropertyOutput>
                            <Property>[param.0]</Property>
                            <Output>[slotindex]</Output>
                        </PropertyOutput>
                    </PropertyOutputs>
                </AddListEntry>
            </Actions>
        </Function>

        <Function Name="charlib_magic_willpower">
            <Actions>
                <Action>
                    <Do>-2</Do>
                    <Output>[local.val]</Output>
                </Action>
                <Action>
                    <If>[data.character.talents.willpower] &gt;= 1</If>
                    <Do>-1</Do>
                    <Output>[local.val]</Output>
                </Action>
                <Action>
                    <If>[data.character.talents.willpower] &gt;= 4</If>
                    <Do>0</Do>
                    <Output>[local.val]</Output>
                </Action>
                <Action>
                    <If>[data.character.talents.willpower] &gt;= 7</If>
                    <Do>1</Do>
                    <Output>[local.val]</Output>
                </Action>
                <Action>
                    <If>[data.character.talents.willpower] &gt;= 10</If>
                    <Do>2</Do>
                    <Output>[local.val]</Output>
                </Action>
                <Action>
                    <If>[data.character.talents.willpower] &gt;= 14</If>
                    <Do>3</Do>
                    <Output>[local.val]</Output>
                </Action>
                <Action>
                    <If>[data.character.talents.willpower] &gt;= 18</If>
                    <Do>4</Do>
                    <Output>[local.val]</Output>
                </Action>
            </Actions>
            <Return>[local.val]</Return>
        </Function>
        <Function Name="charlib_magic_talent">
            <Actions>
                <Action>
                    <Do>0</Do>
                    <Output>[local.val]</Output>
                </Action>
                <Action>
                    <If>[param.0] &gt;= 1</If>
                    <Do>1</Do>
                    <Output>[local.val]</Output>
                </Action>
                <Action>
                    <If>[param.0] &gt;= 3</If>
                    <Do>2</Do>
                    <Output>[local.val]</Output>
                </Action>
                <Action>
                    <If>[param.0] &gt;= 5</If>
                    <Do>3</Do>
                    <Output>[local.val]</Output>
                </Action>
                <Action>
                    <If>[param.0] &gt;= 7</If>
                    <Do>4</Do>
                    <Output>[local.val]</Output>
                </Action>
                <Action>
                    <If>[param.0] &gt;= 10</If>
                    <Do>5</Do>
                    <Output>[local.val]</Output>
                </Action>
                <Action>
                    <If>[param.0] &gt;= 13</If>
                    <Do>6</Do>
                    <Output>[local.val]</Output>
                </Action>
                <Action>
                    <If>[param.0] &gt;= 16</If>
                    <Do>7</Do>
                    <Output>[local.val]</Output>
                </Action>
                <Action>
                    <If>[param.0] &gt;= 18</If>
                    <Do>8</Do>
                    <Output>[local.val]</Output>
                </Action>
            </Actions>
            <Return>[local.val]</Return>
        </Function>
    </Functions>
</Library>