aboutsummaryrefslogtreecommitdiff
path: root/advtrains/models/advtrains_across.obj
diff options
context:
space:
mode:
authorGabriel Pérez-Cerezo <gabriel@gpcf.eu>2017-08-01 13:45:05 +0200
committerorwell96 <orwell@bleipb.de>2017-08-15 14:35:41 +0200
commitc894dee7ef8e43ddca1965e57b5d746b83f4e5b4 (patch)
tree347fa1dbcb03933db8ce7c093f38d0e412f5dc04 /advtrains/models/advtrains_across.obj
parenta34ff7664af45864151d55bd41e2031b322affe2 (diff)
downloadadvtrains-c894dee7ef8e43ddca1965e57b5d746b83f4e5b4.tar.gz
advtrains-c894dee7ef8e43ddca1965e57b5d746b83f4e5b4.tar.bz2
advtrains-c894dee7ef8e43ddca1965e57b5d746b83f4e5b4.zip
Fixed #86
Diffstat (limited to 'advtrains/models/advtrains_across.obj')
0 files changed, 0 insertions, 0 deletions
'>122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815
local def
local F = minetest.formspec_escape
local ifthenelse = ch_core.ifthenelse

local max_stations = 60

local MODE_NORMAL = 0 -- normální zastávka (výchozí nebo mezilehlá)
local MODE_REQUEST_STOP = 1 -- zastávka na znamení (mezilehlá)
local MODE_HIDDEN = 2 -- skrytá zastávka (výchozí nebo mezilehlá)
local MODE_DISABLED = 3 -- vypnutá zastávka (mezilehlá nebo koncová)
local MODE_FINAL = 4 -- koncová zastávka (linka zde jízdu končí)
local MODE_FINAL_HIDDEN = 5 -- koncová zastávka skrytá
local MODE_FINAL_CONTINUE = 6 -- koncová zastávka (vlak pokračuje jako jiná linka)

local color_red = core.get_color_escape_sequence("#ff0000")
local color_green = core.get_color_escape_sequence("#00ff00")

local cancel_linevar = assert(advtrains.lines.cancel_linevar)
local get_last_passages = assert(advtrains.lines.get_last_passages)
local get_line_description = assert(advtrains.lines.get_line_description)
local linevar_decompose = assert(advtrains.lines.linevar_decompose)
local try_get_linevar_def = assert(advtrains.lines.try_get_linevar_def)

local show_last_passages_formspec -- forward declaration

local function check_rights(pinfo, owner)
    if pinfo.role == "new" or pinfo.role == "none" then
        return false
    end
    if owner == nil or pinfo.role == "admin" or pinfo.player_name == owner then
        return true
    end
    return false
end

local function get_stn_from_linevar(linevar)
    local line, stn = linevar_decompose(linevar)
    return ifthenelse(line ~= nil, stn, nil)
end

local function add_linevar(stn, linevar_def)
    local station = advtrains.lines.stations[stn]
    if station == nil then
        return false, "Chybná stanice!"
    end
    local linevars = station.linevars
    if linevars == nil then
        linevars = {}
        station.linevars = linevars
    end
    if linevars[linevar_def.name] ~= nil then
        return false, "Nemohu přidat, varianta linky '"..linevar_def.name.."' již existuje."
    end
    linevars[linevar_def.name] = linevar_def
    return true, nil
end

local function same_stops(stops1, stops2)
    assert(stops1)
    assert(stops2)
    if #stops1 ~= #stops2 then
        return false
    end
    for i, stop in ipairs(stops1) do
        if stop.stn ~= stops2[i].stn then
            return false
        end
    end
    return true
end

local function replace_linevar(stn, linevar_def)
    local station = advtrains.lines.stations[stn]
    if station == nil or station.linevars == nil then
        return false, "Chybná stanice!"
    end
    local linevar = assert(linevar_def.name)
    local linevars = station.linevars
    local old_linevar_def = linevars[linevar]
    if old_linevar_def == nil then
        return false, "Nemohu nahradit, varianta linky '"..linevar.."' dosud neexistuje!"
    end
    linevars[linevar] = linevar_def
    local restart_count = 0
    if not same_stops(old_linevar_def.stops, linevar_def.stops) then
        -- změnily se zastávky, musíme restartovat vlaky:
        for train_id, train in pairs(advtrains.trains) do
            local ls = train.line_status
            if ls ~= nil and ls.linevar == linevar then
                core.log("action", "Train "..train_id.." restarted from index "..ls.linevar_index.." due to replacement of linevar '"..linevar.."'.")
                ls.linevar_index = 1
                restart_count = restart_count + 1
            end
        end
    end
    if restart_count > 0 then
        return true, restart_count.." vlak/ů restartován/o kvůli změnám"
    else
        return true, nil
    end
end

local function delete_linevar(stn, linevar)
    local station = advtrains.lines.stations[stn]
    if station == nil or station.linevars == nil then
        return false, "Chybná stanice!"
    end
    local linevars = station.linevars
    if linevars[assert(linevar)] == nil then
        return false, "Nemohu odstranit, varianta linky '"..linevar.."' nebyla nalezena!"
    end
    linevars[linevar] = nil
    for train_id, train in pairs(advtrains.trains) do
        local ls = train.line_status
        if ls ~= nil and ls.linevar == linevar then
            core.log("action", "Train "..train_id.." removed from deleted linevar '"..linevar.."'.")
            cancel_linevar(train)
        end
    end
    return true, nil
end

local function get_formspec(custom_state)
	local pinfo = ch_core.normalize_player(assert(custom_state.player_name))
	if pinfo.player == nil then
		minetest.log("error", "Expected player not in game!")
		return ""
	end

    local selection_index_raw = custom_state.selection_index
	local selection_index = selection_index_raw or 1
    local formspec = {
        ch_core.formspec_header({formspec_version = 6, size = {20, 16}, auto_background = true}),
        "label[0.5,0.6;Editor variant linek]"..
        "style[s01_pos",
    }
    for i = 2, max_stations do
        table.insert(formspec, string.format(",s%02d_pos", i))
    end
    table.insert(formspec, ";font=mono;font_size=-4]"..
        "style[rc;font=mono]"..
        "tablecolumns[color;text,align=right;text;text,align=center;color;text,width=7;color;text]"..
        "table[0.5,1.25;19,5;linevar;#ffffff,LINKA,TRASA,SM. KÓD,#ffffff,SPRAVUJE,#ffffff,STAV")

    for _, linevar_def in ipairs(custom_state.linevars) do
        local lv_line, lv_stn, lv_rc = linevar_decompose(linevar_def.name)
        local color = ifthenelse(linevar_def.disabled, "#cccccc", "#ffffff")
        table.insert(formspec,
            ","..color..","..F(lv_line)..","..F(get_line_description(linevar_def, {first_stop = true, last_stop = true}))..
            ","..F(lv_rc)..","..ifthenelse(linevar_def.owner == pinfo.player_name, "#00ff00", color)..",")
        table.insert(formspec, F(ch_core.prihlasovaci_na_zobrazovaci(linevar_def.owner)))
        table.insert(formspec, ","..color..",")
        if linevar_def.disabled then
            table.insert(formspec, "vypnutá")
        end
    end
    if selection_index_raw ~= nil then
        table.insert(formspec, ";"..selection_index.."]")
    else
        table.insert(formspec, ";]")
    end
    if pinfo.role ~= "new" then
        table.insert(formspec, "button[14.5,0.3;3.5,0.75;create;nová varianta...]")
    end
    local has_rights_to_open_variant =
        pinfo.role == "admin" or selection_index == 1 or
        pinfo.player_name == custom_state.linevars[selection_index - 1].owner

    if has_rights_to_open_variant and selection_index ~= 1 then
        table.insert(formspec, "button[10.5,0.3;3.5,0.75;delete;smazat variantu]")
    end
    table.insert(formspec, "button_exit[18.75,0.3;0.75,0.75;close;X]"..
        "field[0.5,7;1.25,0.75;line;linka:;"..F(custom_state.line).."]"..
        "field[2,7;1.5,0.75;rc;sm.kód:;"..F(custom_state.rc).."]"..
        "field[3.75,7;3,0.75;train_name;jméno vlaku:;"..F(custom_state.train_name).."]")
    if pinfo.role ~= "admin" then
        table.insert(formspec, "label[7,6.75;spravuje:\n")
    else
        table.insert(formspec, "field[7,7;4,0.75;owner;spravuje:;")
    end
    table.insert(formspec, F(custom_state.owner).."]"..
        "checkbox[11.25,7.25;disable_linevar;vypnout;"..custom_state.disable_linevar.."]")

    if custom_state.message ~= "" then
        table.insert(formspec, "label[0.5,8.25;"..F(custom_state.message).."]")
    end
    if pinfo.role ~= "new" then
        if has_rights_to_open_variant then
            if selection_index > 1 then
                table.insert(formspec, "button[5,15;4.5,0.75;last_passages;poslední jízdy...]"..
                "tooltip[last_passages;Zobrazí přehled časů několika posledních jízd na dané variantě linky.]")
            end
            table.insert(formspec, "button[10,15;4.5,0.75;save;"..
                ifthenelse(custom_state.compiled_linevar == nil, "ověřit změny\npřed uložením]", "uložit změny]"))
        end
        table.insert(formspec, "button[15,15.25;4,0.5;reset;vrátit změny]")
    end

    table.insert(formspec, "tooltip[line;"..
        "Označení linky. Musí být neprázdné. Varianta linky bude použita pouze na vlaky s tímto označením linky.]"..
        "tooltip[rc;Směrový kód. Může být prázdný. Varianta linky bude použita pouze na vlaky\\,\n"..
        "jejichž směrový kód se přesně shoduje se zadaným. Obvykle se toto pole nechává prázdné.]"..
        "tooltip[train_name;Volitelný údaj. Je-li zadán\\, jízdní řády budou uvádět u spojů této\n"..
        "varianty zadané jméno.]"..
        "tooltip[disable_linevar;Zaškrtnutím variantu linky vypnete. Vypnutá varianta linky není používána\n"..
        "na žádné další vlaky\\, stávající vlaky však mohou dojet do svých koncových zastávek.]")

    table.insert(formspec, "container[0,8.75]"..
        "label[0.5,0.25;odjezd]"..
        "label[2,0.25;kód dop.]"..
        "label[4.75,0.25;režim zastávky]"..
        "label[9.5,0.25;kolej]"..
        "label[11,0.25;omezení pozice]"..
        "scrollbaroptions[min=0;max=550;arrows=show]"..
        "scrollbar[19,0.5;0.5,5.5;vertical;evl_scroll;"..custom_state.evl_scroll.."]"..
        "scroll_container[0.5,0.5;18.5,5.5;evl_scroll;vertical]"..
        "box[0,0;20,70;#00808040]") -- box[] = pozadí

    -- výchozí zastávka:
    table.insert(formspec,
        "label[0.1,0.4;0]"..
        "field[1.5,0;2.5,0.75;s01_stn;;"..F(custom_state.stops[1].stn).."]"..
        "dropdown[4.25,0;4.5,0.75;s01_mode;výchozí,skrytá (výchozí);"..custom_state.stops[1].mode..";true]"..
        "field[9,0;1.25,0.75;s01_track;;"..F(custom_state.stops[1].track).."]"..
        "field[10.5,0;3,0.75;s01_pos;;"..F(custom_state.stops[1].pos).."]"..
        "label[13.75,0.4;"..F(custom_state.stops[1].label).."]")

    -- ostatní zastávky:
    local y_base, y_scale = 0, 1
    for i = 2, max_stations do
        local stop = custom_state.stops[i]
        local n
        if i < 10 then
            n = "0"..i
        else
            n = tostring(i)
        end
        local y = string.format("%f", y_base + (i - 1) * y_scale)
        local y2 = string.format("%f", y_base + (i - 1) * y_scale + 0.4) -- for a label
        table.insert(formspec,
            "field[0,"..y..";1.25,0.75;s"..n.."_dep;;"..F(stop.dep).."]"..
            "field[1.5,"..y..";2.5,0.75;s"..n.."_stn;;"..F(stop.stn).."]"..
            "dropdown[4.25,"..y..";4.5,0.75;s"..n..
                "_mode;normální,na znamení (experimentální),skrytá (mezilehlá),vypnutá,koncová,koncová skrytá,"..
            "koncová (pokračuje);"..stop.mode..";true]"..
            "field[9,"..y..";1.25,0.75;s"..n.."_track;;"..F(stop.track).."]"..
            "field[10.5,"..y..";3,0.75;s"..n.."_pos;;"..F(stop.pos).."]"..
            "label[13.75,"..y2..";"..F(stop.label).."]")
    end

    table.insert(formspec,
        "scroll_container_end[]"..
        "tooltip[0,0;2,1;Odjezd: očekávaná jízdní doba v sekundách od odjezdu z výchozí zastávky\n"..
        "do odjezdu z dané zastávky. Podle ní se počítá zpoždění. Hodnota musí být jedinečná\n"..
        "pro každou zastávku na lince a podle ní se zastávky seřadí.\n"..
        "Pro úplné smazání dopravny z linky nechte pole prázdné.]"..
        "tooltip[2,0;2.75,1;Kód dopravny: kód dopravny\\, kde má vlak zastavit. Vlak bude ignorovat\n"..
        "ARS pravidla a zastaví na první zastávkové koleji v dopravně pro odpovídající počet vagonů.\n"..
        "Kód dopravny se na lince může opakovat.]"..
        "tooltip[4.75,0;4.75,1;Režim zastávky: výchozí/normální - vždy zastaví\\;\n"..
        "na znamení: zastaví na znamení (zatím experimentální)\\;\n"..
        "skrytá – vždy zastaví\\, ale nezobrazí se v jízdních řádech\\;\n"..
        "vypnutá – nezastaví (použijte při výlukách nebo při zrušení zastávky)\\;\n"..
        "koncová – vždy zastaví a tím ukončí spoj\\, vlak se stane nelinkovým\\;\n"..
        "koncová (pokračuje) – jako koncová\\, ale vlak se může na odjezdu opět stát linkovým.]"..
        "tooltip[9.5,0;1.5,1;Kolej: nepovinný\\, orientační údaj do jízdních řádů – na které koleji\n"..
        "vlaky obvykle zastavují. Nepovinný údaj.]"..
        "tooltip[10.5,0;3.5,1;Omezení pozice: Zadávejte jen v případě potřeby.\n"..
        "Je-li zadáno\\, vlak v dané dopravně nezastaví na žádné jiné zastávkové koleji\n"..
        "než na té\\, která leží přesně na zadané pozici. Příklad platné hodnoty:\n123,7,-13]"..
        "container_end[]")

	-- if pinfo.role ~= "new" then
	return table.concat(formspec)
end

local mode_from_formspec_map = {MODE_NORMAL, MODE_REQUEST_STOP, MODE_HIDDEN, MODE_DISABLED, MODE_FINAL, MODE_FINAL_HIDDEN, MODE_FINAL_CONTINUE}
local mode_to_formspec_map = table.key_value_swap(mode_from_formspec_map)

local function mode_to_formspec(i, raw_mode)
    if i == 1 then
        return ifthenelse(raw_mode ~= nil and raw_mode == MODE_HIDDEN, 2, 1)
    elseif raw_mode == nil then
        return 1
    else
        return mode_to_formspec_map[raw_mode] or 1
    end
end

local function mode_from_formspec(i, fs_mode)
    if i == 1 then
        return ifthenelse(fs_mode == 2, MODE_HIDDEN, nil)
    else
        local result = mode_from_formspec_map[fs_mode]
        return ifthenelse(result ~= nil and result ~= MODE_NORMAL, result, nil)
    end
end

local function custom_state_set_selection_index(custom_state, new_selection_index)
    -- this will also refresh stops and resets the changes
    assert(custom_state.player_name)
    assert(custom_state.linevars)
    assert(new_selection_index)
    local current_linevar = custom_state.linevars[new_selection_index - 1]
    custom_state.selection_index = new_selection_index or 1
    local stops = custom_state.stops
    if stops == nil then
        stops = {}
        custom_state.stops = stops
    end
    local linevar_stops
    if current_linevar ~= nil then
        linevar_stops = current_linevar.stops
    else
        linevar_stops = {}
    end

    for i = 1, max_stations do
        local stop = linevar_stops[i]
        if stop ~= nil then
            stops[i] = {
                dep = tostring(assert(stop.dep)),
                stn = assert(stop.stn),
                mode = mode_to_formspec(i, stop.mode),
                track = stop.track or "",
                pos = stop.pos or "",
                label = "",
            }
        else
            stops[i] = {
                dep = ifthenelse(i == 1, "0", ""),
                stn = "",
                mode = 1,
                track = "",
                pos = "",
                label = "",
            }
        end
    end
    if current_linevar ~= nil then
        local lv_line, lv_stn, lv_rc = linevar_decompose(current_linevar.name)
        custom_state.line = lv_line or ""
        custom_state.rc = lv_rc or ""
        custom_state.train_name = current_linevar.train_name or ""
        custom_state.owner = assert(current_linevar.owner)
        custom_state.disable_linevar = ifthenelse(current_linevar.disabled, "true", "false")
    else
        custom_state.line = ""
        custom_state.rc = ""
        custom_state.train_name = ""
        custom_state.owner = custom_state.player_name
        custom_state.disable_linevar = "false"
    end
    custom_state.owner = ch_core.prihlasovaci_na_zobrazovaci(custom_state.owner)
    custom_state.compiled_linevar = nil
    custom_state.evl_scroll = 0
    custom_state.message = ""
end

local function num_transform(s)
    local prefix = s:match("^([0-9]+)/")
    if prefix == nil then
        return s
    end
    return string.format(" %020d%s", tonumber(prefix) or 0, s:sub(#prefix, -1))
end

local function linevars_sorter(a, b)
    return num_transform(a.name) < num_transform(b.name)
end

local function custom_state_refresh_linevars(custom_state, linevar_to_select)
    assert(custom_state.player_name)
    local linevars = {}
    for _, stdata in pairs(advtrains.lines.stations) do
        if stdata.linevars ~= nil then
            for _, linevar_def in pairs(stdata.linevars) do
                table.insert(linevars, linevar_def)
            end
        end
    end
    table.sort(linevars, linevars_sorter)
    custom_state.selection_index = nil
    custom_state.linevars = linevars
    custom_state.compiled_linevar = nil
    if linevar_to_select ~= nil then
        for i, linevar_def in ipairs(linevars) do
            if linevar_def.name == linevar_to_select then
                custom_state_set_selection_index(custom_state, i + 1)
                return true
            end
        end
        return false
    end
end

local function custom_state_compile_linevar(custom_state)
    local stations = advtrains.lines.stations
    local line = assert(custom_state.line)
    local stn = assert(custom_state.stops[1].stn)
    local rc = assert(custom_state.rc)
    local train_name = assert(custom_state.train_name)
    local owner = assert(custom_state.owner)
    local stops = {}
    if line == "" then
        return false, "Označení linky nesmí být prázdné!"
    elseif line:find("[/|\\]") then
        return false, "Označení linky nesmí obsahovat znaky '/', '|' a '\\'!"
    elseif line:len() > 256 then
        return false, "Označení linky je příliš dlouhé!"
    elseif stn == "" then
        return false, "Výchozí zastávka musí být vyplněná!"
    elseif rc:find("[/|\\]") then
        return false, "Směrový kód nesmí obsahovat znaky '/', '|' a '\\'!"
    elseif owner == "" then
        return false, "Správa linky musí být vyplněná!"
    elseif train_name:len() > 256 then
        return false, "Jméno vlaku je příliš dlouhé!"
    end
    -- Zkontrolovat zastávky:
    local errcount = 0
    local finalcount = 0
    local dep_to_index = {}
    for i, stop in ipairs(assert(custom_state.stops)) do
        local good_label
        stop.label = ""
        if stop.dep == "" then
            -- přeskočit
        elseif not stop.dep:match("^[0-9][0-9]*$") then
            errcount = errcount + 1
            stop.label = color_red.."Chybný formát času odjezdu!"
        elseif tonumber(stop.dep) < 0 or tonumber(stop.dep) > 3600 then
            errcount = errcount + 1
            stop.label = color_red.."Čas odjezdu musí být v rozsahu 0 až 3600!"
        elseif dep_to_index[tonumber(stop.dep)] ~= nil then
            errcount = errcount + 1
            stop.label = color_red.."Duplicitní čas odjezdu!"
        else
            dep_to_index[tonumber(stop.dep)] = i
            if stop.stn == "" or stations[stop.stn] == nil or stations[stop.stn].name == nil then
                errcount = errcount + 1
                stop.label = color_red.."Neznámý kód dopravny!"
            elseif stop.stn:find("[/|\\]") then
                errcount = errcount + 1
                stop.label = color_red.."Kód dopravny nesmí obsahovat znaky '/', '|' a '\\'!"
            elseif stop.track:len() > 16 then
                errcount = errcount + 1
                stop.label = color_red.."Označení koleje je příliš dlouhé!"
            elseif stop.pos ~= "" and not stop.pos:match("^[-0-9][0-9]*,[-0-9][0-9]*,[-0-9][0-9]*$") then
                errcount = errcount + 1
                stop.label = color_red.."Neplatný formát pozice!"
            elseif stop.pos:len() > 22 then
                errcount = errcount + 1
                stop.label = color_red.."Specifikace pozice je příliš dlouhá!"
            else
                -- v pořádku:
                local new_stop = {
                    stn = stop.stn,
                    dep = tonumber(stop.dep),
                }
                local new_mode = mode_from_formspec(i, stop.mode)
                if new_mode ~= nil then
                    new_stop.mode = new_mode
                    if i > 1 and (new_mode == MODE_FINAL or new_mode == MODE_FINAL_CONTINUE or new_mode == MODE_FINAL_HIDDEN) then
                        finalcount = finalcount + 1
                    end
                end
                if stop.pos ~= "" then
                    new_stop.pos = stop.pos
                end
                if stop.track ~= "" then
                    new_stop.track = stop.track
                end
                table.insert(stops, new_stop)
                if stop.stn ~= "" then
                    stop.label = color_green.."= "..assert(stations[stop.stn].name)
                end
            end
        end
    end
    if errcount > 0 then
        return false, errcount.." chyb v seznamu zastávek!"
    end
    if finalcount == 0 then
        return false, "Varianta linky musí obsahovat alespoň jednu koncovou zastávku!"
    end
    table.sort(stops, function(a, b) return a.dep < b.dep end)
    custom_state.compiled_linevar = {
        name = line.."/"..stops[1].stn.."/"..rc,
        line = line,
        owner = ch_core.jmeno_na_prihlasovaci(owner),
        stops = stops,
    }
    if train_name ~= "" then
        custom_state.compiled_linevar.train_name = train_name
    end
    if custom_state.disable_linevar == "true" then
        custom_state.compiled_linevar.disabled = true
    end
    return true, nil
end

local function formspec_callback(custom_state, player, formname, fields)
    local reload_stations, update_formspec = false, false
    -- print("DEBUG: "..dump2({custom_state = custom_state, formname = formname, fields = fields}))

	if fields.quit then
		return
	end
    -- scrollbar:
    if fields.evl_scroll then
        local event = core.explode_scrollbar_event(fields.evl_scroll)
        if event.type == "CHG" then
            custom_state.evl_scroll = event.value
        end
    end
    -- checkbox:
    if fields.disable_linevar then
        custom_state.disable_linevar = fields.disable_linevar
    end
    -- dropdowns:
    for i = 1, max_stations do
        local id = string.format("s%02d_mode", i)
        local n = tonumber(fields[id])
        if n ~= nil then
            custom_state.stops[i].mode = n
        end
    end
    -- fields:
    for _, key in ipairs({"line", "rc", "train_name", "owner"}) do
        if fields[key] then
            custom_state[key] = fields[key]
        end
    end
    for i, stop in ipairs(custom_state.stops) do
        local prefix = string.format("s%02d_", i)
        for _, key in ipairs({"dep", "stn", "track", "pos"}) do
            local value = fields[prefix..key]
            if value then
                stop[key] = value
            end
        end
    end
    -- selection:
    if fields.linevar then
        local event = core.explode_table_event(fields.linevar)
        if event.type == "CHG" or event.type == "DCL" then
            custom_state_set_selection_index(custom_state, assert(tonumber(event.row)))
            update_formspec = true
        end
    end

    -- buttons:
    if fields.create then
        custom_state_set_selection_index(custom_state, 1)
        update_formspec = true
    elseif fields.reset then
        custom_state_set_selection_index(custom_state, custom_state.selection_index or 1)
        update_formspec = true
    elseif fields.save then
        local pinfo = ch_core.normalize_player(player)