function worldmapen_generateNewMap($defaultTerrain = "Forest")
{
    global $worldmapen_globals;
    $retValue = array();
    worldmapen_loadTerrainDefs();
    if (!array_key_exists($defaultTerrain, $worldmapen_globals["terrainDefs"])) {
        debug("Invalid terrain type '{$defaultTerrain}'. Using 'Forest' instead.");
        $defaultTerrain = "Forest";
    }
    // Level 0 will be "Earth", level 2 will be "Air" until otherwise defined.
    $maxX = get_module_setting("worldmapsizeX");
    $maxY = get_module_setting("worldmapsizeY");
    for ($x = 1; $x <= $maxX; $x++) {
        for ($y = 1; $y <= $maxY; $y++) {
            $retValue[0][$x][$y] = "Earth";
            $retValue[1][$x][$y] = $defaultTerrain;
            $retValue[2][$x][$y] = "Air";
        }
    }
    debug("Map with size {$maxX} x {$maxY} generated with default '{$defaultTerrain}'.");
    return $retValue;
}
function worldmapen_editor_terrain($op, $subop, $act)
{
    global $_POST, $worldmapen_globals;
    if ($act == "save") {
        $terrainDefs = worldmapen_loadTerrainDefs();
        reset($_POST);
        foreach ($_POST as $key => $value) {
            list($x, $y) = explode("_", $key, 2);
            if (is_numeric($x) && is_numeric($y)) {
                worldmapen_setTerrain($x, $y, 1, $value);
            }
        }
        worldmapen_saveMap();
        output("Worldmap saved");
    }
    $colors = '';
    foreach ($worldmapen_globals['terrainDefs'] as $defs) {
        $colors .= '"' . $defs['color'] . '", ';
    }
    $colors = rtrim($colors, ', ');
    // -----------------------------------------------------------------------
    // BEGIN - Java script to determine the terrain type by clicking on a td cell
    // -----------------------------------------------------------------------
    rawoutput("<script type=\"text/javascript\"><!--");
    rawoutput('colors = [' . $colors . '];');
    rawoutput("cRGB = [];");
    rawoutput("function toRGB(color){");
    rawoutput("var rgb = \"rgb(\" + parseInt(color.substring(1,3), 16) + \", \" + parseInt(color.substring(3,5), 16) + \", \" + parseInt(color.substring(5,8), 16) + \")\";   ");
    rawoutput("return rgb;");
    rawoutput("}");
    rawoutput("for(var i=0; i<colors.length; i++){");
    rawoutput("cRGB[i] = toRGB(colors[i]);");
    rawoutput("}");
    rawoutput("function changeColor(target){");
    rawoutput("var swapper = navigator.appVersion.indexOf(\"MSIE\")!=-1 ? toRGB(document.getElementById(target).style.backgroundColor) : document.getElementById(target).style.backgroundColor;");
    rawoutput("var set = false;");
    rawoutput("var xx;");
    rawoutput("for(var i=0; i<cRGB.length; i++){");
    rawoutput("if(swapper == cRGB[i]){");
    rawoutput("if(((i+1)) >= cRGB.length){");
    rawoutput("xx = 0;");
    rawoutput("}else{");
    rawoutput("xx = i+1;");
    rawoutput("}");
    rawoutput("document.getElementById(target).style.background = colors[xx];");
    rawoutput("document.getElementById(target+\"b\").value = xx;");
    rawoutput("set = true;");
    rawoutput("i=cRGB.length;");
    rawoutput("}");
    rawoutput("}");
    rawoutput("set ? null : (document.getElementById(target).style.background = colors[1], document.getElementById(target+\"b\").value = 1);");
    rawoutput("}//-->");
    rawoutput("</script>");
    // -----------------------------------------------------------------------
    // END - Java script to determine the terrain type by clicking on a td cell
    // -----------------------------------------------------------------------
    output('<noscript>JavaScript must be enabled for the terrain editor to work.</noscript>', true);
    worldmapen_viewmap(false);
}