aboutsummaryrefslogtreecommitdiff
path: root/advtrains/protection.lua
blob: 73b725f1a6a46a81b95f6dfc06ca6f3783d6a0df (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
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
-- advtrains
-- protection.lua: privileges and rail protection, and some helpers


-- Privileges to control TRAIN DRIVING/COUPLING
minetest.register_privilege("train_operator", {
	description = "Without this privilege, a player can't do anything about trains, neither place or remove them nor drive or couple them (but he can build tracks if he has track_builder)",
	give_to_singleplayer= true,
});

minetest.register_privilege("train_admin", {
	description = "Player may drive, place or remove any trains from/to anywhere, regardless of owner, whitelist or protection",
	give_to_singleplayer= true,
});

-- Privileges to control TRACK BUILDING
minetest.register_privilege("track_builder", {
	description = "Player can place and/or dig rails not protected from him. If he also has protection_bypass, he can place/dig any rails",
	give_to_singleplayer= true,
});

-- Privileges to control OPERATING TURNOUTS/SIGNALS
minetest.register_privilege("railway_operator", {
	description = "Player can operate turnouts and signals not protected from him. If he also has protection_bypass, he can operate any turnouts/signals",
	give_to_singleplayer= true,
});

-- there is a configuration option "allow_build_only_owner". If this is active, a player having track_builder can only build rails and operate signals/turnouts in an area explicitly belonging to him
-- (checked using a dummy player called "*dummy*" (which is not an allowed player name))

--[[
Protection/privilege concept:
Tracks:
	Protected 1 node all around a rail and 4 nodes upward (maybe make this dynamically determined by the rail...)
	if track_builder privilege:
		if not protected from* player:
			if allow_build_only_owner:
				if unprotected:
					deny
			else:
				allow
	deny
Wagons in general:
	Players can only place/destroy wagons if they have train_operator
Wagon driving controls:
	The former seat_access tables are unnecessary, instead there is a whitelist for the driving stands
	on player trying to access a driver stand:
	if is owner or is on whitelist:
		allow
	else:
		deny
Wagon coupling:
	Derived from the privileges for driving stands. The whitelist is shared (and also settable on non-driverstand wagons)
	for each of the two bordering wagons:
		if is owner or is on whitelist:
			allow

*"protected from" means the player is not allowed to do things, while "protected by" means that the player is (one of) the owner(s) of this area

]]--

local boo = minetest.settings:get_bool("advtrains_allow_build_to_owner")


local nocheck
-- Check if the node we are about to check is in the range of a track that is protected from a player
--WARN: true means here that the action is forbidden!
function advtrains.check_track_protection(pos, pname)
	if nocheck or pname=="" then
		return false
	end
	nocheck=true --prevent recursive calls, never check this again if we're already in
	local r, vr = 1, 3
	local nodes = minetest.find_nodes_in_area(
		{x = pos.x - r, y = pos.y - vr, z = pos.z - r},
		{x = pos.x + r, y = pos.y, z = pos.z + r},
		{"group:advtrains_track"})
	for _,npos in ipairs(nodes) do
		if not minetest.check_player_privs(pname, {track_builder = true}) then
			if boo and not minetest.is_protected(npos, pname) and minetest.is_protected(npos, "*dummy*") then
				nocheck = false
				return false
			else
				minetest.chat_send_player(pname, "You are not allowed to dig or place nodes near tracks (missing track_builder privilege)")
				minetest.log("action", pname.." tried to dig/place nodes near the track at "..minetest.pos_to_string(npos).." but does not have track_builder")
				nocheck = false
				return true
			end
		end
		if not minetest.check_player_privs(pname, {protection_bypass = true}) then
			if minetest.is_protected(npos, pname) then
				nocheck = false
				minetest.record_protection_violation(pos, pname)
				return true
			end
		end
	end
	nocheck=false
	return false
end

local old_is_protected = minetest.is_protected
minetest.is_protected = function(pos, pname)
	if advtrains.check_track_protection(pos, pname) then
		return true
	end
	return old_is_protected(pos, pname)
end

--WARN: true means here that the action is allowed!
function advtrains.check_driving_couple_protection(pname, owner, whitelist)
	if minetest.check_player_privs(pname, {train_admin = true}) then
		return true
	end
	if not minetest.check_player_privs(pname, {train_operator = true}) then
		return false
	end
	if not owner or owner == pname then
		return true
	end
	if whitelist and string.find(" "..whitelist.." ", " "..pname.." ", nil, true) then
		return true
	end
	return false
end
function advtrains.check_turnout_signal_protection(pos, pname)
	nocheck=true
	if not minetest.check_player_privs(pname, {railway_operator = true}) then
		if boo and not minetest.is_protected(pos, pname) and minetest.is_protected(pos, "*dummy*") then
			nocheck=false
			return true
		else
			minetest.chat_send_player(pname, "You are not allowed to operate turnouts and signals (missing railway_operator privilege)")
			minetest.log("action", pname.." tried to operate turnout/signal at "..minetest.pos_to_string(pos).." but does not have railway_operator")
			nocheck=false
			return false
		end
	end
	if not minetest.check_player_privs(pname, {protection_bypass = true}) then
		if minetest.is_protected(pos, pname) then
			minetest.record_protection_violation(pos, pname)
			nocheck=false
			return false
		end
	end
	nocheck=false
	return true
end
/a> 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
\batchmode
\makeatletter
\def\input@path{{/home/moritz/Home/Projekte/Minetest/minetest/mods/advtrains/assets/}}
\makeatother
\documentclass[english]{paper}
\usepackage[T1]{fontenc}
\usepackage[latin9]{inputenc}
\usepackage{geometry}
\geometry{verbose,tmargin=1cm,bmargin=1cm,lmargin=1cm,rmargin=1cm}
\setlength{\parindent}{0bp}
\usepackage{graphicx}
\usepackage{babel}
\begin{document}

\title{Minetest Mod - Advanced Trains {[}advtrains{]}}

\title{Interlocking System Guide}
\maketitle

\section{Introduction}

In real-world railways, a so-called interlocking system is a set of
railway signals and trackside equipment. Its purpose is to prevent
conflicting train movements which otherwise could result in derailing
or colliding trains. If you want more information, just search for
``railway interlocking'' on the internet.

Real-world interlocking systems perform this task by setting routes.
A route is a path along a track that a train can safely pass. To set
a route for a train, the signalman (the operator of a signal box)
has to set switches (turnouts) to the correct position and lock them
in order to make a signal for a train show ``Proceed''. In newer
systems, this is done automatically by the interlocking system. A
route can not be set if switches are locked to a wrong position by
another route or if any portion of the route is occupied by a train.

The interlocking system in this Minetest mod tries to follow real-world
interlocking systems as far as applicable. It divides tracks into
track sections and implements a route setting mechanism following
the same principle.

However, for the sake of simplicity of implementation and usage, not
all concepts of real-world interlocking have been taken over. Especially,
there is no mechanism for overlap.

If you are looking for a place to learn how real-world interlocking
systems work, have a look at ``SimSig''. By looking at their simulations,
you can obtain experience on how to set up your own interlocking systems
in AdvTrains. The SimSig glossary is a good place to look up unknown
terms in this document.

\section{Setting up track sections}

In the real world, a line of track is divided into so-called track
sections, or track circuits. Those systems often can not tell where
exactly a train is, but only which track sections it occupies. A route
can never be set through an occupied track section.

A track section often covers:
\begin{itemize}
\item A section on a main running line, between two signals
\item A single turnout
\item A rail crossing, or a set of turnouts acting as a double/single slip
switch
\item A siding
\end{itemize}
You will find some examples on how to interlock certain patterns later.

\subsection{Track Circuit Breaks}

In this mod, you will not directly configure the locations of track
sections. Instead, you designate the borders of each track section
using a special node, the Track Circuit Break, abbreviated TCB.

For example, if you want to create a track section for a piece of
a main running line, you set up two TCBs at the ends of this track
circuit.

Setting up a TCB works as follows:
\begin{enumerate}
\item Place a TCB node somewhere near the place where the circuit break
is going to be located.
\item Right-click the TCB node
\item Punch the rail which should act as TCB
\end{enumerate}
The result should look like this:

\includegraphics[width=10cm]{0_home_moritz_Home_Projekte_Minetest_minetest_m___s_assets_lyx_img_screenshot_20180830_142551.png}

Now you have assigned the TCB node to a rail. Right-click the TCB
node once again. This will bring up a form which looks as follows:

\includegraphics[width=10cm]{1_home_moritz_Home_Projekte_Minetest_minetest_m____lyx_img_Bildschirmfoto_2018-08-30_14-26-35.png}

You see that the form is divided in side A and side B. To designate
where each side is, a marker is displayed on the rail. You can always
make this marker show up by punching the TCB node, and remove it by
punching the marker. Both sides are shown as ``End of interlocking''.
This means that there is no track section set up at this place.

You should repeat this procedure once again a few meters away from
the first TCB to create a second TCB on the same track.

\includegraphics[width=10cm]{2_home_moritz_Home_Projekte_Minetest_minetest_m____lyx_img_Bildschirmfoto_2018-08-30_14-32-48.png}

Once you have both bordering TCBs set up, you can now create the actual
track section. To do this:
\begin{enumerate}
\item Right-click one of the TCBs
\item Locate the correct side (A or B) to create the track section
\item Click ``Create interlocked Track Section'' in the formspec on the
chosen side.
\end{enumerate}
Now, the text on the formspec has changed. It shows something like
this:

\includegraphics[width=5cm]{3_home_moritz_Home_Projekte_Minetest_minetest_m____lyx_img_Bildschirmfoto_2018-08-30_14-27-25.png}

Clicking ``Show Track Section'' brings up another formspec:

\includegraphics[width=5cm]{4_home_moritz_Home_Projekte_Minetest_minetest_m____lyx_img_Bildschirmfoto_2018-08-30_14-28-32.png}

On the top, you see a list of all TCBs that border this track section.
In your case, there should be two TCBs listed. If there's only one,
head over to \ref{subsec:Long-track-sections,}. You should now select
a name for the track section, to identify it later.

The same procedure is applicable when you create a turnout track section,
except that you have to set up three or more TCBs.

The AdvTrains interlocking system allows you to add more TCBs after
you have created a track section. This works without problems in most
cases. For example, you can easily insert a turnout into an already
set-up track section and create another TCB behind it, and AdvTrains
will automatically detect the existing track section. Problems arise
only if you try to insert a TCB in-between a section, in which case
both sides of the TCB will end up assigned to the same section. The
code currently does not handle this case properly, so try to avoid
this situation by all means. As a last resort, you can always dissolve
a faulty track section, as described in the next chapter.

\subsection{Long track sections, crossings and other edge cases\label{subsec:Long-track-sections,}}

\subsubsection{Very long track sections}

If you try to set up a track section that is longer than 1000 nodes,
advtrains won't recognize the TCB at the other end because of a safety
limit in the traverser function, which is supposed to prevent deadlocks.
This case has happened when the Track Section overview screen only
shows one TCB in the list. The procedure for this is as follows:
\begin{enumerate}
\item Go to the second TCB (the one that wasn't recognized). It should show
``End of Interlocking'' on the relevant side.
\item Click ``Create interlocked track section''. The section created
will be different from the one that is already present.
\item In the track section overview, click ``Join into other section''
\item Go back to the first TCB, bring up the Track Section overview screen
of the first track section and click ``Join with ???''
\end{enumerate}
The other, missing TCB should now appear in the list. If you accidentally
started such a joining procedure, click the ``X'' button on the
right.

\subsubsection{Rail crosses}

Since rail crosses are created by laying tracks across each other
without logical connection, there's no way for advtrains to know whether
rails cross each other.

Rail crossings in interlocking systems are always one single track
section, which in most cases has 4 TCBs adjacent.

\includegraphics[width=5cm]{5_home_moritz_Home_Projekte_Minetest_minetest_m____lyx_img_Bildschirmfoto_2018-08-30_14-51-25.png}

The procedure is quite similar to the one for long sections: First,
create two track sections for the branches, and then use the ``Join''
function to merge both sections into one.

\subsubsection{Deleting and re-adding single TCBs to a section}

In some occasions, for example when you remove a siding or a crossover,
it can be necessary to unassign a TCB from a track section. There
are multiple ways to do this:
\begin{itemize}
\item In the TCB form, click the ``Remove from section'' button
\item In the track section form, first select the TCB in the list and then
click ``Unlink selected TCB''
\end{itemize}
The result is that the TCB shows ``End of Interlocking'' and the
section does not list the TCB as an endpoint anymore.

The other case is adding a siding or a crossover, in which case one
or more TCBs still show ``End of Interlocking'' although they should
be part of a section:
\begin{itemize}
\item Go to another TCB that is registered in the track section and click
``Update near TCBs''
\item If that did not work, follow the procedure of creating a long track
section
\end{itemize}

\subsubsection{Dissolving sections}

If you made a mistake setting up something and you don't see any other
way to fix a misconfigured track section, you can always delete it
using the ``Dissolve section'' button. This operation removes the
track section and sets all TCBs that previously belonged to the section
as ``End of Interlocking''. This will always work and lets you start
over new with setting up track sections.

\subsection{Interlocking patterns}

Have a look at the following images. They show you how you can set
up sections so that reasonable train moves are possible.

You should settle on a naming scheme for your sections. This way,
you can determine the source of an issue faster

\section{Signals and routes}

Signals are appliances that can give instructions to trains. That
can be the permission to proceed, a speed restriction, or other information.

There are 2 types of signals:
\begin{itemize}
\item Static signals always display the same information to the train. This
can be a speed restriction (or the end of one), a disallowal to proceed
as shunt move or similar things. In most cases, these are signs.
\item Dynamic signals are what most people would call a ``signal''. Its
function is to inform trains about whether and at which speed they
can proceed into the next section safely.
\end{itemize}

\subsection{Signal Influence Point}

Every signal is associated to a track on which the instruction should
be followed. Signals are usually placed right next to the track on
the right side. Human observers do know then that the signal belongs
to the track left of it, however, train safety systems (like the one
in advtrains) can not.

This is the reason why a so-called ``influence point'' needs to
be assigned to any signal that should actually give instructions to
trains, should the driver (if even there is one) fail to recognize
the instructions.

Depending on the signal and the mod that adds the signal, there are
different ways to configure this. Signals integrated into advtrains
behave as follows:
\begin{itemize}
\item Static signals and all red-green light signals from core advtrains
that are not assigned to a TCB can be configured by holding the ``Use''
key and then right-clicking the signal
\item All signals that are assigned to a TCB can be configured by first
right-clicking them, then selecting ``Influence Point'' in the signalling
formspec.
\end{itemize}
The small formspec that opens allows you to set and later view or
clear the Influence Point. To set the influence point, click the ``Set''
button, face towards the signal and punch a rail about 2m in front
of the signal. A small marker will be shown, indicating success. To
cancel setting an influence point, punch anything other. (note that
then the influence point remains unset, regardless of its previous
state)

The advtrains-internal train safety system ensures that the train
always obeys any restrictions imposed by signals, if (and only if)
the influence point is set properly.

\subsection{Signal Aspects}

While static signals are mainly used for speed restrictions, the interesting
ones are variable signals. Of course, you can always control any variable
signal by traditional means (mesecons, digiline, right-click) if the
signal allows it, but that misses the point of this interlocking system.

A signal aspect is a piece of information that a signal shows to the
train driver. It contains information about whether and at what speed
the driver may proceed at the signal.

Every signal, both static and dynamic ones, imposes a certain aspect
to trains passing the signal. For static signals, this is always the
same aspect, such as ``Proceed at speed of 8'' or ``Shunt moves
may not pass''. Dynamic signals, however, can display multiple different
aspects. The default for them is always as restrictive as possible,
mostly ``Halt!''.

You should know that both static and dynamic signals use exactly the
same properties for signal aspects. There is no difference in the
meaning of the aspect definitions.

In the following sections, we will talk about main signals. By this,
we mean a variable signal that can display both a ``Danger'' aspect
(trains are not allowed to proceed) and at least one ``Proceed''
aspect (train may proceed as train/shunt move, with optional speed
restriction), which act as an ``entry signal'' for one or multiple
routes.

\subsection{Train moves and Shunt Moves}
\begin{itemize}
\item A ``Train move'' is a train that is running, going to run on or
coming from a main line between stations, passing through or stopping
at a station. Train moves can expect that there are no obstacles on
the route and they can proceed at the maximum permitted speed of the
line. This is the regular operation mode for trains. Shunt signals
have no meaning for train moves.
\item A ``Shunt move'' is a train that moves within a station and/or is
coupling or decoupling wagons or engines. A shunt move may never leave
a station except into a siding. Also, shunt moves may drive at a maximum
speed of 6 speed units, because it is usually not ensured that the
path is free of obstacles. (however, advtrains ensures that every
route is free of railway vehicles, even shunt routes)
\end{itemize}
There are also 2 general types of signals: Main signals and Shunt
signals. While main signals have a meaning for all types of trains,
Shunt signals only have to be followed by shunt moves. Usually, Shunt
signals that are on a train move route are set to a Proceed aspect
as well.