I have (finally) written two functions for getting and releasing permits if you can't use schedule (ie, player train. ) Here is the information: If you would like a copy sent by email as well, just fire an email to me. I have also asked MaxTrainz to post this on his website with my other code snipets, examples.
/* Here are the functions created to allow Permits
to be used with player trains. Here are the instructions:
Place both of these functions in the main thread section.
In the thread you have the player train, make sure that
you have set a Permit variable. The command is:
Permit[] name; // where the name is the variable name for
that trains permits.
Then, to get a permit, call the function get.
here is the syntax :
PermitName = get ("Name of Junction",Train,"Direction",Permit)
where Direction is the direction you want to go.
Options are :
"Left" from single to left arm of junction.
"Right" from single to right arm of junction.
"Forward" from single to centre of thee arm junction.
"BackwardLeft" From Left arm of junction to single side
"BackwardRight" From Right arm of junction to single side
"BackwardForward" from centre arm to single side.
You only need to get permits for the junctions you need,
unlike Schedules where you need all junctions listed.
To remove permits, simply call release once you know the train
is past where it is needed. Syntax:
release (Permit name,# of permits to be released)
The number of permits should always be a number of two, since
there are two permits generated. This operates on the FIFO principle
the first permit created will be the first permit released. So, to
release the first permit, it would be Name = release (Name,2). If you want
to release a set of 4 permits (say a section of main line), it would
be release (Name,8).
If I can make a suggestion:
Place a signal before the junction(s) you need a permit for and using
Navigate.SetSignal, set it to Red. After the get commands, then
set it back to Automatic. That will keep everything sync'd.
If you have any questions please email acparker@cablerocket.com
This may be freely distributed and used, although I would appreciate
any mention if you find this helpful.
Adam Parker
adamsloco
*/
PHP:
public Permit
[]
release
(
Permit
[]
P
,
int i
)
//Release permit
{
P
[
0
,
i
]==
null
;
//Remove reference of the first permit, for i times. i should always be a multiple of 2
Interface
.
Log
(
"Size of Permit array is "
+
P
.
size
());
//Error trapping so you can see in log how many permits remain
return
P
;
// if is not returned, all permits are cancelled
}
public Permit
[]
get
(
string J
,
Train t
,
string D
,
Permit
[]
P
)
//Get Permit
{
Permit newtpermit
;
//temp variables
Permit newjpermit
;
Sniff
(
t
,
"Permit"
,
"Granted"
,
true
);
//Set up to watch for the train to get a permit
Junction j
=
cast
<
Junction
>
Router
.
GetGameObject
(
J
);
//Set which junction
int d
;
int e
;
if (
D
==
"Left"
)
// Choose which direction to get permit, and to set junction
{
d
=
0
;
e
=
0
;
}
else if (
D
==
"Forward"
)
{
d
=
1
;
e
=
1
;
}
else if (
D
==
"Right"
)
{
d
=
2
;
e
=
2
;
}
else if (
D
==
"BackwardLeft"
)
// These were necessary to avoid intermitent errors
{
// with Junction.SwitchForTrain() command.
d
=-
1
;
// This allows for setting junction at a lower level.
e
=
0
;
}
else if (
D
==
"BackwardForward"
)
{
d
=-
1
;
e
=
1
;
}
else if (
D
==
"BackwardRight"
)
{
d
=-
1
;
e
=
2
;
}
newtpermit
=
j
.
RequestTrackPermit
(
t
,
d
);
// Get Permit for the track past the junction
Interface
.
Log
(
"Verifying Permit"
);
wait
()
// Make sure permit is granted before proceeding
{
on
"Permit"
,
"Granted"
:
{
if (
newtpermit
.
WasGranted
())
break;
continue;
}
}
newjpermit
=
j
.
RequestPermit
(
t
);
// Get Permit for the junction itself
wait
()
{
// Make sure permit is granted before proceeding.
on
"Permit"
,
"Granted"
:
{
if (
newjpermit
.
WasGranted
())
break;
continue;
}
}
Interface
.
Log
(
"Permit GRANTED"
);
// Error trapping
if (
P
==
null
)
// Set up array if necessary
P
= new
Permit
[
0
];
P
[
P
.
size
()] =
newtpermit
;
// Store permits in the array
P
[
P
.
size
()] =
newjpermit
;
j
.
SetDirection
(
e
);
// Set junction appropriately
Interface
.
Log
(
"Size of Permit array is "
+
P
.
size
());
// Error trapping to see how many permits there are, should always be a multiple of 2
return
P
;
// Return Permit back to the thread. Otherwise, all permits are cancelled.
}