#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 _ = wesnoth.textdomain "wesnoth-multiplayer"
                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.color == 1 then r, g, b = 255,   0,   0
                    elseif team.__cfg.color == 2 then r, g, b =   0,   0, 255
                    elseif team.__cfg.color == 3 then r, g, b =   0, 255,   0
                    elseif team.__cfg.color == 4 then r, g, b = 155,  48, 255
                    elseif team.__cfg.color == 5 then r, g, b =   0,   0,   0
                    elseif team.__cfg.color == 6 then r, g, b = 165,  42,  42
                    elseif team.__cfg.color == 7 then r, g, b = 255, 165,   0
                    elseif team.__cfg.color == 8 then r, g, b = 255, 255, 255
                    elseif team.__cfg.color == 9 then r, g, b =   0, 128, 128 end
                    side_comparison = side_comparison ..
                        string.format( tostring( _ "<span foreground='#%02x%02x%02x'>Side %d</span>:  Income score = %d  Unit score = %d  Gold = %d") .. "\n" .. tostring( _ "Grand total: <b>%d</b>") .. "\n",
                            r, g, b, side, income, units, team.gold, total)
                    if total > total_score then
                        color = 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" .. tostring( _ "<span foreground='%s'>Side %d</span> has the advantage."), color, side_num)
                wesnoth.fire("message", { message = side_comparison, speaker = "narrator", image = "wesnoth-icon.png"})
            >>
        [/lua]
    [/event]
#enddef

# These macros make things easier for UMC eras which are additions to or modifications of the default eras.
# If anything gets changed or added to the default eras, please do it in the code included in these macros.
#define ERA_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}
#enddef

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

    {ERA_DEFAULT}
[/era]

#define ERA_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}
#enddef

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

    {ERA_HEROES}
[/era]
