local OldRunInEditorMenu = RunInEditorMenu
function RunInEditorMenu()
    Editor.Running = EditorNotRunning;
    local done = false
    repeat
        done = true
        Editor:CreateRandomMap(true)

        local race = math.random(0,1)
        local unit

        Map.Info.PlayerType[0] = PlayerPerson
        Players[0].Type = PlayerPerson
        Players[0].Race = race
        Players[0].AiName = "wc2-sea-attack"
        Players[0].Resources[1] = 5000
        Players[0].Resources[2] = 3000
        local playerUnitName
        if race == 0 then
            playerUnitName = "unit-peasant"
        else
            playerUnitName = "unit-peon"
        end
        local posx = Map.Info.MapWidth
        local posy = Map.Info.MapHeight
        for i,u in ipairs(GetUnits("any")) do
            if GetUnitVariable(u, "Ident") == "unit-gold-mine" then
                local ux = GetUnitVariable(u, "PosX")
                local uy = GetUnitVariable(u, "PosY")
                if ux + uy < posx + posy then
                    posx = ux
                    posy = uy
                end
            end
        end
        unit = CreateUnit(playerUnitName, 0, {posx - 1, posy - 1})
        if FindNextResource(unit, "gold", 6) == nil then
            done = false
        end

        local numOpponents = math.random(1, 10)
        if numOpponents > 7 then
            numOpponents = 2
        elseif numOpponents > 3 then
            numOpponents = 3
        end
        local opponentRace
        local opponentUnit
        if race == 0 then
            opponentRace = 1
            opponentUnit = "unit-peon"
        else
            opponentRace = 0
            opponentUnit = "unit-peasant"
        end

        for i=1,numOpponents,1 do
            print("Setting up opponent " .. i)
            Map.Info.PlayerType[i] = PlayerComputer
            Players[i].Type = PlayerComputer
            Players[i].Race = opponentRace
            Players[i].AiName = "wc2-sea-attack"
            Players[i].Resources[1] = 5000
            Players[i].Resources[2] = 3000
            unit = CreateUnit(opponentUnit, i, {(Map.Info.MapWidth / i) / 4 * 3, Map.Info.MapHeight / 4 * 3})
            if FindNextResource(unit, "gold", 60) == nil then
                done = false
            end
        end
    until done

    mapname = "maps/randommap.smp"
    EditorSaveMap(mapname)
end
Map.Info.Description = "Random map"
Map.Info.MapWidth = 128
Map.Info.MapHeight = 128
LoadTileModels("scripts/tilesets/summer.lua")
StartEditor(nil)
InitGameSettings()
RunInEditorMenu = OldRunInEditorMenu
GameSettings.Difficulty = math.random(1, 3)
GameSettings.NetGameType = 1
GameSettings.RevealMap = 1
GetMapInfo(mapname)
Load(mapname)
RunMap(mapname)
