Skip to content

Commit

Permalink
feat(alp45,alp46): implement consist doors closed lamp
Browse files Browse the repository at this point in the history
  • Loading branch information
YoRyan committed Jul 25, 2024
1 parent be39f4a commit e2f952a
Show file tree
Hide file tree
Showing 9 changed files with 103 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/lib/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,6 @@ export enum ConsistMessageId {
BrakeLight = 10101,
NjtDestination = 10100,
LowPlatforms = 10146,
DoorsLeft = 121011,
DoorsRight = 121021,
}
80 changes: 78 additions & 2 deletions src/lib/nec/nj-transit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,20 @@
import * as c from "lib/constants";
import * as frp from "lib/frp";
import { FrpEngine } from "lib/frp-engine";
import { rejectRepeats, rejectUndefined } from "lib/frp-extra";
import { fsm, mapBehavior, rejectRepeats, rejectUndefined } from "lib/frp-extra";
import { FrpVehicle } from "lib/frp-vehicle";
import * as rw from "lib/railworks";
import * as ui from "lib/ui";

/**
* Represents door open/close events transmitted through the consist by NJ
* Transit coaches.
*/
export enum DoorsEvent {
Opening = "1",
Closed = "-1",
}

export const destinationNames = ["Trenton", "New York", "Long Branch", "Hoboken", "Dover", "Bay Head"];

const destinationNodes = [
Expand Down Expand Up @@ -171,8 +180,75 @@ function createManualDoorsStream(
stayOpen: false,
}
),
frp.map(accum => accum.position)
frp.map(({ position }) => position)
);
}

/**
* Notify the player engine of the consist's door open status by communicating
* this coach's status, and/or by forwarding messages through the consist.
*
* Note that unlike the vanilla scripts, we don't bother tracking the number of
* coaches with open doors or on which side they've opened, as this information
* is not necessary and can easily get out of sync if the player changes the
* makeup of the train.
* @param v The vehicle.
* @param doorsOpen A manual doors behavior that indicates the positions of the
* coach's left and right doors.
* @returns A stream of door events produced by this vehicle, or received from
* the rest of the consist.
*/
export function createConsistDoorsOpenStream(
v: FrpVehicle,
doorsOpen?: frp.Behavior<[number, number]>
): frp.Stream<boolean> {
const forward$ = frp.compose(
v.createOnConsistMessageStream(),
frp.filter(([id]) => id === c.ConsistMessageId.DoorsLeft || id === c.ConsistMessageId.DoorsRight)
);
forward$(msg => {
v.rv.SendConsistMessage(...msg);
});

const fromConsist$ = frp.compose(
v.createOnConsistMessageStream(),
frp.filter(([id]) => id === c.ConsistMessageId.DoorsLeft || id === c.ConsistMessageId.DoorsRight),
frp.map(([, msg]) => msg === DoorsEvent.Opening)
);

if (doorsOpen !== undefined) {
const events$ = frp.compose(
v.createPlayerUpdateStream(),
mapBehavior(doorsOpen),
fsm<[number, number]>([0, 0]),
frp.map(([[fromL, fromR], [toL, toR]]) => {
if (fromL <= 0 && toL > 0) {
return DoorsEvent.Opening;
} else if (fromR <= 0 && toR > 0) {
return DoorsEvent.Opening;
} else if (fromL > 0 && toL <= 0) {
return DoorsEvent.Closed;
} else if (fromR > 0 && toR <= 0) {
return DoorsEvent.Closed;
} else {
return undefined;
}
}),
rejectUndefined(),
frp.hub()
);
events$(event => {
v.rv.SendConsistMessage(c.ConsistMessageId.DoorsLeft, event, rw.ConsistDirection.Forward);
v.rv.SendConsistMessage(c.ConsistMessageId.DoorsLeft, event, rw.ConsistDirection.Backward);
});
return frp.compose(
events$,
frp.map(event => event === DoorsEvent.Opening),
frp.merge(fromConsist$)
);
} else {
return fromConsist$;
}
}

/**
Expand Down
3 changes: 3 additions & 0 deletions src/lib/shared/multilevel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,9 @@ export function onInit(me: FrpEngine, version: Version) {
me.rv.SetTime("Doors_R", position);
});

// Send door open status.
njt.createConsistDoorsOpenStream(me, passengerDoors);

// Throttle, dynamic brake, and air brake controls
const isPenaltyBrake = frp.liftN(
(aduState, alerterState) => (aduState?.penaltyBrake || alerterState?.penaltyBrake) ?? false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,7 @@ const me = new FrpEngine(() => {
// Miscellaneous NJT features
njt.createDestinationSignSelector(me);
njt.createManualDoorsPopup(me);
njt.createConsistDoorsOpenStream(me);

// Set in-cab vehicle number.
readRvNumber();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -608,6 +608,7 @@ const me = new FrpEngine(() => {
// Miscellaneous NJT features
njt.createDestinationSignSelector(me);
njt.createManualDoorsPopup(me);
njt.createConsistDoorsOpenStream(me);

// Enable updates.
me.e.BeginUpdate();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,9 @@ const me = new FrpEngine(() => {
me.rv.SetTime("Doors_r", position * 2);
});

// Send door open status.
njt.createConsistDoorsOpenStream(me, passengerDoors);

// Throttle, dynamic brake, and air brake controls
const isPenaltyBrake = frp.liftN(
(aduState, alerterState) => (aduState?.penaltyBrake || alerterState?.penaltyBrake) ?? false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -655,6 +655,12 @@ const me = new FrpEngine(() => {
me.rv.ActivateNode("LightsBlue", on);
});

// Door closed lamp
const doorsOpen$ = njt.createConsistDoorsOpenStream(me);
doorsOpen$(open => {
me.rv.SetControlValue("DoorsCount", open ? 1 : 0);
});

// Set platform door height.
fx.createLowPlatformStreamForEngine(me, false);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -705,6 +705,12 @@ const me = new FrpEngine(() => {
me.rv.ActivateNode("LightsBlue", on);
});

// Door closed lamp
const doorsOpen$ = njt.createConsistDoorsOpenStream(me);
doorsOpen$(open => {
me.rv.SetControlValue("DoorsCount", open ? 1 : 0);
});

// Set platform door height.
fx.createLowPlatformStreamForEngine(me, false);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,9 @@ const me = new FrpEngine(() => {
me.rv.SetTime("Doors_R", position * 2);
});

// Send door open status.
njt.createConsistDoorsOpenStream(me, passengerDoors);

// Center door control
const aiCenterDoors$ = frp.compose(
me.createAiUpdateStream(),
Expand Down

0 comments on commit e2f952a

Please sign in to comment.