aboutsummaryrefslogtreecommitdiff
path: root/advtrains_train_subway
ModeNameSize
-rw-r--r--depends.txt41logplain
-rw-r--r--init.lua5860logplain
d---------locale34logplain
d---------models54logplain
d---------sounds325logplain
d---------textures772logplain
'#n108'>108 109 110 111 112 113 114 115 116 117 118 119 120 121 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 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856
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 selection_index > 1 and has_rights_to_open_variant 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.."]"..
        "field[13.5,7;3,0.75;continues;pokračování:;"..F(custom_state.continues).."]")

    if custom_state.message ~= "" then
        table.insert(formspec, "label[0.5,8.25;"..F(custom_state.message).."]")
    end
    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
    if has_rights_to_open_variant then
        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]")
    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;stání]"..
        "label[3.5,0.25;kód dop.]"..
        "label[6.25,0.25;režim zastávky]"..
        "label[11,0.25;kolej]"..
        "label[12.5,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;1.25,0.75;s01_wait;;"..F(custom_state.stops[1].wait).."]"..
        "field[3,0;2.5,0.75;s01_stn;;"..F(custom_state.stops[1].stn).."]"..
        "dropdown[5.75,0;4.5,0.75;s01_mode;výchozí,skrytá (výchozí);"..custom_state.stops[1].mode..";true]"..
        "field[10.5,0;1.25,0.75;s01_track;;"..F(custom_state.stops[1].track).."]"..
        "field[12,0;3,0.75;s01_pos;;"..F(custom_state.stops[1].pos).."]"..
        "label[15.25,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..";1.25,0.75;s"..n.."_wait;;"..F(stop.wait).."]"..
            "field[3,"..y..";2.5,0.75;s"..n.."_stn;;"..F(stop.stn).."]"..
            "dropdown[5.75,"..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[10.5,"..y..";1.25,0.75;s"..n.."_track;;"..F(stop.track).."]"..
            "field[12,"..y..";3,0.75;s"..n.."_pos;;"..F(stop.pos).."]"..
            "label[15.25,"..y2..";"..F(stop.label).."]")
    end

    table.insert(formspec,
        "scroll_container_end[]"..
        "tooltip[0,0;1.5,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[1.5,0;1.5,1;Stání: očekáváná doba stání před odjezdem. Pro koncové zastávky očekávaná doba stání po příjezdu.]"..
        "tooltip[3.5,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[6.25,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[10.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[12.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