#textdomain wesnoth-multiplayer
#define RANDOM_SIDE
    [multiplayer_side]
        id=Random
        name= _"Random"
        image="units/random-dice.png"
        {MAGENTA_IS_THE_TEAM_COLOR}
        random_faction=yes
    [/multiplayer_side]
#enddef

#define QUICK_4MP_LEADERS
    # This makes all leaders with 4 MP receive the quick trait, except ones with
    # unit.variables.dont_make_me_quick=yes (boolean)

    [event]
        name=prestart

        [store_unit]
            [filter]
                canrecruit=yes

                [filter_wml]
                    max_moves=4
                [/filter_wml]
            [/filter]

            kill=yes
            variable=leaders_to_make_quick
        [/store_unit]

        {FOREACH leaders_to_make_quick i}
            [if]
                # The variable check is done here instead of in the [store_unit]
                # so that all boolean values work
                [variable]
                    name=leaders_to_make_quick[$i].variables.dont_make_me_quick
                    boolean_equals=yes
                [/variable]

                [then]
                    [unstore_unit]
                        variable=leaders_to_make_quick[$i]
                    [/unstore_unit]
                [/then]

                [else]
                    [set_variables]
                        name=temp

                        [literal]
                            {TRAIT_QUICK}
                        [/literal]
                    [/set_variables]

                    [set_variables]
                        name=leaders_to_make_quick[$i].modifications.trait
                        mode=append

                        [insert_tag]
                            name=literal
                            variable=temp.trait
                        [/insert_tag]
                    [/set_variables]

                    {CLEAR_VARIABLE leaders_to_make_quick[$i].max_moves,leaders_to_make_quick[$i].moves,leaders_to_make_quick[$i].max_hitpoints,leaders_to_make_quick[$i].hitpoints}

                    [unstore_unit]
                        variable=leaders_to_make_quick[$i]
                    [/unstore_unit]
                [/else]
            [/if]
        {NEXT i}

        {CLEAR_VARIABLE leaders_to_make_quick,temp}
    [/event]
#enddef

#define TURNS_OVER_ADVANTAGE
    [event]
        name=time over
        [lua]
            code=<<
                local function all_sides()
                    local function f(s, i)
                        i = i + 1
                        local t = wesnoth.get_side(i)
                        return t and i, t
                    end
                    return f, nil, 0
                end

                local income_factor = 5

                local side_num = -1
                local total_score = -1
                local side_comparison = ""
                for side, team in all_sides() do
                    repeat -- ugly hack to convert 'break' into 'continue'
                    if team.__cfg.hidden then
                        break
                    end
                    local income = team.total_income * income_factor
                    local units = 0
                    -- Calc the total unit-score here
                    for i, unit in ipairs( wesnoth.get_units { side = side } ) do
                        if not unit.__cfg.canrecruit then
                            wesnoth.fire("unit_worth", { id = unit.id })
                            units = units + wesnoth.get_variable("unit_worth")
                        end
                    end
                    -- Up to here
                    local total = units + team.gold + income
                    local r, g, b = 255, 255, 255
                    if     team.__cfg.colour == 1 then r, g, b = 255,   0,   0
                    elseif team.__cfg.colour == 2 then r, g, b =   0,   0, 255
                    elseif team.__cfg.colour == 3 then r, g, b =   0, 255,   0
                    elseif team.__cfg.colour == 4 then r, g, b = 155,  48, 255
                    elseif team.__cfg.colour == 5 then r, g, b =   0,   0,   0
                    elseif team.__cfg.colour == 6 then r, g, b = 165,  42,  42
                    elseif team.__cfg.colour == 7 then r, g, b = 255, 165,   0
                    elseif team.__cfg.colour == 8 then r, g, b = 255, 255, 255
                    elseif team.__cfg.colour == 9 then r, g, b =   0, 128, 128 end
                    side_comparison = side_comparison ..
                        string.format("<span foreground=\"#%02x%02x%02x\">Side %d</span>:  Income score = %d  Unit score = %d  Gold = %d\nGrand total: <b>%d</b>\n",
                            r, g, b, side, income, units, team.gold, total)
                    if total > total_score then
                        colour = string.format("#%02x%02x%02x", r, g, b)
                        side_num = side
                        total_score = total
                    end
                    until true -- end ugly hack
                end
                side_comparison = side_comparison ..
                    string.format("\n<span foreground=\"%s\">Side %d</span> has the advantage.", colour, side_num)
                wesnoth.fire("message", { message = side_comparison, speaker = "narrator", image = "wesnoth-icon.png"})
            >>
        [/lua]
    [/event]
#enddef

[era]
    id=era_default
    name= _ "Default"

    {RANDOM_SIDE}
    {multiplayer/factions/loyalists-default.cfg}
    {multiplayer/factions/rebels-default.cfg}
    {multiplayer/factions/northerners-default.cfg}
    {multiplayer/factions/undead-default.cfg}
    {multiplayer/factions/knalgans-default.cfg}
    {multiplayer/factions/drakes-default.cfg}

    {QUICK_4MP_LEADERS}
    {TURNS_OVER_ADVANTAGE}
[/era]

[era]
    id=era_heroes
    name= _ "Age of Heroes"

    {RANDOM_SIDE}
    {multiplayer/factions/loyalists-aoh.cfg}
    {multiplayer/factions/rebels-aoh.cfg}
    {multiplayer/factions/northerners-aoh.cfg}
    {multiplayer/factions/undead-aoh.cfg}
    {multiplayer/factions/knalgans-aoh.cfg}
    {multiplayer/factions/drakes-aoh.cfg}

    {QUICK_4MP_LEADERS}
    {TURNS_OVER_ADVANTAGE}
[/era]
