Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import com.nolimitscoaster.*;
- /**
- * This script implements a Block System Controller for the Cursing Mommy Coaster.
- * It may look complicated, but it is not that complex actually.
- * The biggest complexity comes from implementing the proper way to handle manual block mode events and to control the switches.
- */
- public class BlockScript extends Script implements BlockSystemController
- {
- // Declare some constants...
- // These constants represents our block states, those values are user defineable
- private static final int STATE_BLOCK_FREE = 0;
- private static final int STATE_BLOCK_APPROACHING = 1;
- private static final int STATE_BLOCK_LEAVING = 2;
- private static final int STATE_BLOCK_BEFORE_TRIGGER = 3;
- private static final int STATE_BLOCK_BEHIND_TRIGGER = 4;
- private static final int STATE_BLOCK_WAITING = 5;
- private static final int STATE_BLOCK_WAIT_FOR_CLEAR = 6;
- private static final int STATE_BLOCK_WAIT_FOR_ADVANCE = 7;
- private static final int STATE_BLOCK_IN_STATION = 8;
- private static final int STATE_BLOCK_APPROACHING_B = 9;
- private static final int STATE_BLOCK_LEAVING_B = 10;
- private static final int STATE_BLOCK_BEFORE_TRIGGER_B = 11;
- // The name of the script for error messages
- private static final String scriptName = "BlockScript";
- // The coaster operation mode
- private static final int AUTO_MODE = 0;
- private static final int MANUAL_BLOCK_MODE = 1;
- private static final int FULL_MANUAL_MODE = 2;
- // Member variables...
- private Coaster coaster;
- private Block liftBlock;
- private Block beforeLaunchBlock;
- private Block stationExitBlock;
- private Block stationEnterBlock;
- private Block reverse1Block;
- private Block reverse2Block;
- private Block beforeStationBlock;
- private Block storage1Block;
- private Block storage2Block;
- private SpecialTrack reverse1Switch;
- private SpecialTrack reverse2Switch;
- private float reverse1BlockTime;
- private float reverse2BlockTime;
- private int mode;
- /**
- * This method overrides the default implementation of onInit() from the Script class.
- * Gets called at Script startup.
- */
- public bool onInit()
- {
- String name;
- // Detect the coaster this script belongs to...
- coaster = sim.getCoasterForEntityId(getParentEntityId());
- if (coaster == null)
- {
- System.err.println(scriptName + ": Not attached to coaster");
- return false;
- }
- // Assign the block system controller to the coaster
- coaster.setBlockSystemController(this);
- /////
- // Get and initialize all blocks
- name = "Lift";
- liftBlock = coaster.getBlock(name);
- if (!checkAndSetInitialBlockState(liftBlock, name))
- {
- return false;
- }
- liftBlock.setAdvanceFwdVisible(true); // Set buttons used on the control panel
- name = "Before Launch";
- beforeLaunchBlock = coaster.getBlock(name);
- if (!checkAndSetInitialBlockState(beforeLaunchBlock, name))
- {
- return false;
- }
- beforeLaunchBlock.setAdvanceFwdVisible(true);
- name = "Before Station";
- beforeStationBlock = coaster.getBlock(name);
- if (!checkAndSetInitialBlockState(beforeStationBlock, name))
- {
- return false;
- }
- beforeStationBlock.setAdvanceFwdVisible(true);
- name = "Station Exit";
- stationExitBlock = coaster.getBlock(name);
- if (!checkAndSetInitialBlockState(stationExitBlock, name))
- {
- return false;
- }
- stationExitBlock.setAdvanceFwdVisible(true);
- name = "Station Enter";
- stationEnterBlock = coaster.getBlock(name);
- if (!checkAndSetInitialBlockState(stationEnterBlock, name))
- {
- return false;
- }
- stationEnterBlock.setAdvanceFwdVisible(true);
- name = "First Dir Change Brake";
- reverse1Block = coaster.getBlock(name);
- if (!checkAndSetInitialBlockState(reverse1Block, name))
- {
- return false;
- }
- reverse1Block.setAdvanceFwdVisible(true);
- name = "Reverse 2 Brake";
- reverse2Block = coaster.getBlock(name);
- if (!checkAndSetInitialBlockState(reverse2Block, name))
- {
- return false;
- }
- reverse2Block.setAdvanceFwdVisible(true);
- reverse2Block.setAdvanceBwdVisible(true);
- name = "Storage 1";
- storage1Block = coaster.getBlock(name);
- if (!checkAndSetInitialBlockState(storage1Block, name))
- {
- return false;
- }
- storage1Block.setAdvanceFwdVisible(true);
- storage1Block.setAdvanceBwdVisible(true);
- name = "Storage 2";
- storage2Block = coaster.getBlock(name);
- if (!checkAndSetInitialBlockState(storage2Block, name))
- {
- return false;
- }
- storage2Block.setAdvanceFwdVisible(true);
- /////
- // Get and initialize switches
- name = "Reverse 1 Switch";
- reverse1Switch = coaster.getSpecialTrack(name);
- if (reverse1Switch == null)
- {
- System.err.println(scriptName + ": SpecialTrack '" + name + "' not found");
- return false;
- }
- name = "Reverse 2 Switch";
- reverse2Switch = coaster.getSpecialTrack(name);
- if (reverse2Switch == null)
- {
- System.err.println(scriptName + ": SpecialTrack '" + name + "' not found");
- return false;
- }
- mode = AUTO_MODE;
- return true;
- }
- /**
- * This method overrides the default implementation of the Script class.
- * Gets called when the Script is about to exit.
- */
- public void onExit()
- {
- }
- /**
- * This method overrides the default implementation of OnNextFrame() from the Script class.
- * Gets called for each frame.
- */
- public void onNextFrame(float tick)
- {
- if (mode != FULL_MANUAL_MODE)
- {
- // process all blocks...
- processStation(stationExitBlock, stationEnterBlock);
- processStation(stationEnterBlock, liftBlock);
- processLiftBlock();
- processBeforeLaunchBlock();
- processReverse1Block();
- processReverse2Block();
- processBeforeStationBlock();
- processStorage1Block();
- processStorage2Block();
- if (mode == MANUAL_BLOCK_MODE)
- {
- // update the control panel user interface
- updateControlPanel();
- }
- }
- }
- /**
- * This method is part of the BlockSystemController interface
- * Gets called when the Auto Block mode gets selected on the control panel.
- */
- public void onAutoMode(Coaster c)
- {
- //System.out.println("onAutoMode");
- if (mode == FULL_MANUAL_MODE)
- {
- // previous mode was full manual, we need to check the new position of the trains now
- setInitialBlockState(liftBlock);
- setInitialBlockState(beforeLaunchBlock);
- setInitialBlockState(beforeStationBlock);
- setInitialBlockState(stationExitBlock);
- setInitialBlockState(stationEnterBlock);
- setInitialBlockState(reverse1Block);
- setInitialBlockState(reverse2Block);
- }
- mode = AUTO_MODE;
- updateControlPanel();
- }
- /**
- * This method is part of the BlockSystemController interface
- * Gets called when the Manual Block gets selected on the control panel.
- */
- public void onManualBlockMode(Coaster c)
- {
- //System.out.println("onManualBlockMode");
- if (mode == FULL_MANUAL_MODE)
- {
- // previous mode was full manual, we need to check the new position of the trains now
- setInitialBlockState(liftBlock);
- setInitialBlockState(beforeLaunchBlock);
- setInitialBlockState(beforeStationBlock);
- setInitialBlockState(stationExitBlock);
- setInitialBlockState(stationEnterBlock);
- setInitialBlockState(reverse1Block);
- setInitialBlockState(reverse2Block);
- setInitialBlockState(storage1Block);
- setInitialBlockState(storage2Block);
- }
- mode = MANUAL_BLOCK_MODE;
- updateControlPanel();
- }
- /**
- * This method is part of the BlockSystemController interface
- * Gets called when the Full Manual mode gets selected on the control panel.
- */
- public void onFullManualMode(Coaster c)
- {
- //System.out.println("onFullManualMode");
- mode = FULL_MANUAL_MODE;
- updateControlPanel();
- }
- /**
- * This method is part of the BlockSystemController interface
- * Gets called when the user clicks on the Advance Fwd Button on the control panel.
- */
- public void onAdvanceFWDButton(Block block)
- {
- if (block == beforeLaunchBlock)
- {
- reverse1Block.setState(STATE_BLOCK_APPROACHING);
- beforeLaunchBlock.setState(STATE_BLOCK_LEAVING);
- }
- else if (block == liftBlock)
- {
- beforeLaunchBlock.setState(STATE_BLOCK_APPROACHING);
- liftBlock.setState(STATE_BLOCK_LEAVING);
- }
- else if (block == stationEnterBlock)
- {
- beforeLaunchBlock.setState(STATE_BLOCK_APPROACHING);
- stationEnterBlock.setState(STATE_BLOCK_LEAVING);
- stationEnterBlock.getSection().setStationLeaving();
- }
- else if (block == stationExitBlock)
- {
- stationEnterBlock.setState(STATE_BLOCK_APPROACHING);
- stationExitBlock.setState(STATE_BLOCK_LEAVING);
- stationExitBlock.getSection().setStationLeaving();
- }
- else if (block == beforeStationBlock)
- {
- stationExitBlock.setState(STATE_BLOCK_APPROACHING);
- beforeStationBlock.setState(STATE_BLOCK_LEAVING);
- }
- else if (block == reverse1Block)
- {
- reverse2Block.setState(STATE_BLOCK_APPROACHING);
- reverse1Block.setState(STATE_BLOCK_LEAVING);
- }
- else if (block == reverse2Block)
- {
- beforeStationBlock.setState(STATE_BLOCK_APPROACHING);
- reverse2Block.setState(STATE_BLOCK_LEAVING);
- }
- else if (block == storage1Block)
- {
- Train train = block.getSection().getTrainOnSection();
- if (train != null) train.setLashedToTrack(false);
- reverse2Block.setState(STATE_BLOCK_APPROACHING_B);
- storage1Block.setState(STATE_BLOCK_LEAVING);
- }
- else if (block == storage2Block)
- {
- Train train = block.getSection().getTrainOnSection();
- if (train != null) train.setLashedToTrack(false);
- storage1Block.setState(STATE_BLOCK_APPROACHING);
- storage2Block.setState(STATE_BLOCK_LEAVING);
- }
- }
- /**
- * This method is part of the BlockSystemController interface
- * Gets called when the user clicks on the Advance Bwd Button on the control panel.
- */
- public void onAdvanceBWDButton(Block block)
- {
- if (block == reverse2Block)
- {
- storage1Block.setState(STATE_BLOCK_APPROACHING_B);
- reverse2Block.setState(STATE_BLOCK_LEAVING_B);
- }
- else if (block == storage1Block)
- {
- Train train = block.getSection().getTrainOnSection();
- if (train != null) train.setLashedToTrack(false);
- storage2Block.setState(STATE_BLOCK_APPROACHING_B);
- storage1Block.setState(STATE_BLOCK_LEAVING_B);
- }
- }
- /**
- * This method checks if a block was found and registers all possible states to the block and checks if a train is on the block.
- */
- private static bool checkAndSetInitialBlockState(Block block, String name)
- {
- if (block == null)
- {
- System.err.println(scriptName + ": Block '" + name + "' not found");
- return false;
- }
- registerBlockStates(block);
- setInitialBlockState(block);
- return true;
- }
- /**
- * This method checks if a train is on the block or not and sets the corresponding state.
- */
- private static void setInitialBlockState(Block block)
- {
- if (block.getNumberOfTrainsOnBlock() > 0)
- {
- if (block.getSection().iStation())
- {
- block.setState(STATE_BLOCK_IN_STATION);
- }
- else
- {
- block.setState(STATE_BLOCK_WAIT_FOR_CLEAR);
- }
- }
- else
- {
- block.setState(STATE_BLOCK_FREE);
- }
- }
- /**
- * Adds labels for each possible state to a block.
- * The labels are recommended for display on the control panel, there is no other purpose.
- */
- private static void registerBlockStates(Block block)
- {
- // register the states, so that some usefull text will be shown on the block tab of the control panel
- block.registerState(STATE_BLOCK_FREE, "Free", Block.LAMP_OFF);
- block.registerState(STATE_BLOCK_APPROACHING, "Approaching", Block.LAMP_FLASHING);
- block.registerState(STATE_BLOCK_APPROACHING_B, "Approaching", Block.LAMP_FLASHING);
- block.registerState(STATE_BLOCK_LEAVING, "Leaving", Block.LAMP_ON);
- block.registerState(STATE_BLOCK_LEAVING_B, "Leaving", Block.LAMP_ON);
- block.registerState(STATE_BLOCK_BEFORE_TRIGGER, "Before Trigger", Block.LAMP_ON);
- block.registerState(STATE_BLOCK_BEFORE_TRIGGER_B, "Before Trigger", Block.LAMP_ON);
- block.registerState(STATE_BLOCK_BEHIND_TRIGGER, "Behind Trigger", Block.LAMP_ON);
- block.registerState(STATE_BLOCK_WAITING, "Waiting", Block.LAMP_ON);
- block.registerState(STATE_BLOCK_WAIT_FOR_CLEAR, "Waiting for Clear Block", Block.LAMP_ON);
- block.registerState(STATE_BLOCK_WAIT_FOR_ADVANCE, "Waiting for Advance", Block.LAMP_ON);
- block.registerState(STATE_BLOCK_IN_STATION, "In Station", Block.LAMP_ON);
- }
- /**
- * Will update the user interface elements based on the current states
- */
- private void updateControlPanel()
- {
- liftBlock.setAdvanceFwdEnabled(liftBlock.getState() == STATE_BLOCK_WAIT_FOR_ADVANCE);
- beforeLaunchBlock.setAdvanceFwdEnabled(beforeLaunchBlock.getState() == STATE_BLOCK_WAIT_FOR_ADVANCE);
- stationEnterBlock.setAdvanceFwdEnabled(stationEnterBlock.getState() == STATE_BLOCK_WAIT_FOR_ADVANCE);
- stationExitBlock.setAdvanceFwdEnabled(stationExitBlock.getState() == STATE_BLOCK_WAIT_FOR_ADVANCE);
- beforeStationBlock.setAdvanceFwdEnabled(beforeStationBlock.getState() == STATE_BLOCK_WAIT_FOR_ADVANCE);
- reverse1Block.setAdvanceFwdEnabled(reverse1Block.getState() == STATE_BLOCK_WAIT_FOR_ADVANCE);
- reverse2Block.setAdvanceFwdEnabled((reverse2Block.getState() == STATE_BLOCK_WAIT_FOR_ADVANCE) && prepareReverse2BlockLeaving());
- reverse2Block.setAdvanceBwdEnabled((reverse2Block.getState() == STATE_BLOCK_WAIT_FOR_ADVANCE) && canReverse2BlockLeaveBwd());
- storage1Block.setAdvanceFwdEnabled((storage1Block.getState() == STATE_BLOCK_WAIT_FOR_ADVANCE) && canEnterReverse2BlockFromStorage());
- storage1Block.setAdvanceBwdEnabled((storage1Block.getState() == STATE_BLOCK_WAIT_FOR_ADVANCE) && (storage2Block.getState() == STATE_BLOCK_FREE));
- storage2Block.setAdvanceFwdEnabled(storage2Block.getState() == STATE_BLOCK_WAIT_FOR_ADVANCE);
- reverse1Switch.setCanManualSwitchDirection((reverse1Block.getState() == STATE_BLOCK_FREE) || (reverse1Block.getState() == STATE_BLOCK_WAIT_FOR_ADVANCE) || (reverse1Block.getState() == STATE_BLOCK_WAIT_FOR_CLEAR));
- reverse2Switch.setCanManualSwitchDirection((reverse2Block.getState() == STATE_BLOCK_FREE) || (reverse2Block.getState() == STATE_BLOCK_WAIT_FOR_ADVANCE) || (reverse2Block.getState() == STATE_BLOCK_WAIT_FOR_CLEAR));
- }
- /**
- * A universal method to process a station block.
- * This method works for both stations.
- */
- private void processStation(Block stationBlock, Block nextBlock)
- {
- switch (stationBlock.getState())
- {
- case STATE_BLOCK_IN_STATION:
- if (stationBlock.getSection().isStationWaitingForClearBlock())
- {
- if (nextBlock.getState() == STATE_BLOCK_FREE)
- {
- stationBlock.getSection().setStationNextBlockClear();
- }
- }
- else if (stationBlock.getSection().isStationWaitingForAdvance())
- {
- if (mode == MANUAL_BLOCK_MODE)
- {
- if (nextBlock.getState() == STATE_BLOCK_FREE)
- {
- stationBlock.setState(STATE_BLOCK_WAIT_FOR_ADVANCE);
- }
- else
- {
- stationBlock.getSection().setStationNextBlockOccupied();
- }
- }
- else
- {
- if (nextBlock.getState() == STATE_BLOCK_FREE)
- {
- nextBlock.setState(STATE_BLOCK_APPROACHING);
- stationBlock.setState(STATE_BLOCK_LEAVING);
- stationBlock.getSection().setStationLeaving();
- }
- else
- {
- stationBlock.getSection().setStationNextBlockOccupied();
- }
- }
- }
- break;
- case STATE_BLOCK_WAIT_FOR_ADVANCE:
- if ((mode != MANUAL_BLOCK_MODE) || !stationBlock.getSection().isStationWaitingForAdvance() || (nextBlock.getState() != STATE_BLOCK_FREE))
- {
- stationBlock.setState(STATE_BLOCK_IN_STATION);
- }
- break;
- case STATE_BLOCK_LEAVING:
- if (stationBlock.getNumberOfTrainsOnBlock() != 0)
- {
- // Train is still on the block
- stationBlock.getSection().setBrakesOff();
- stationBlock.getSection().setTransportsStandardFwdOn();
- }
- else
- {
- // Train has left the block
- stationBlock.setState(STATE_BLOCK_FREE);
- }
- break;
- case STATE_BLOCK_FREE:
- stationBlock.getSection().setTransportsOff();
- stationBlock.getSection().setBrakesOn();
- break;
- case STATE_BLOCK_APPROACHING:
- if (stationBlock.getSection().isTrainOnSection())
- {
- stationBlock.getSection().setStationEntering();
- stationBlock.setState(STATE_BLOCK_IN_STATION);
- }
- else
- {
- stationBlock.getSection().setBrakesOff();
- stationBlock.getSection().setTransportsOff();
- }
- break;
- }
- }
- /**
- * Process the lift block
- */
- private void processLiftBlock()
- {
- switch (liftBlock.getState())
- {
- case STATE_BLOCK_FREE:
- liftBlock.getSection().setLiftOff();
- break;
- case STATE_BLOCK_APPROACHING:
- if (liftBlock.getSection().isTrainOnSection())
- {
- liftBlock.setState(STATE_BLOCK_BEFORE_TRIGGER);
- }
- else
- {
- liftBlock.getSection().setLiftOff();
- }
- break;
- case STATE_BLOCK_BEFORE_TRIGGER:
- liftBlock.getSection().setLiftFwdOn();
- if (liftBlock.getSection().isTrainBehindLiftTrigger())
- {
- liftBlock.setState(STATE_BLOCK_BEHIND_TRIGGER);
- }
- break;
- case STATE_BLOCK_BEHIND_TRIGGER:
- if (mode == MANUAL_BLOCK_MODE)
- {
- liftBlock.setState(STATE_BLOCK_WAIT_FOR_ADVANCE);
- }
- else
- {
- if ((beforeLaunchBlock.getState() == STATE_BLOCK_FREE) && (mode == AUTO_MODE))
- {
- beforeLaunchBlock.setState(STATE_BLOCK_APPROACHING);
- liftBlock.setState(STATE_BLOCK_LEAVING);
- }
- else
- {
- liftBlock.getSection().setLiftOff();
- }
- }
- break;
- case STATE_BLOCK_WAIT_FOR_ADVANCE:
- if (mode == AUTO_MODE)
- {
- liftBlock.setState(STATE_BLOCK_BEHIND_TRIGGER);
- }
- else
- {
- liftBlock.getSection().setLiftOff();
- }
- break;
- case STATE_BLOCK_LEAVING:
- liftBlock.getSection().setLiftFwdOn();
- if (liftBlock.getNumberOfTrainsOnBlock() == 0)
- {
- liftBlock.setState(STATE_BLOCK_FREE);
- }
- break;
- case STATE_BLOCK_WAIT_FOR_CLEAR:
- if (liftBlock.getSection().isTrainBehindLiftTrigger())
- {
- liftBlock.setState(STATE_BLOCK_BEHIND_TRIGGER);
- }
- else
- {
- liftBlock.setState(STATE_BLOCK_BEFORE_TRIGGER);
- }
- break;
- }
- }
- /**
- * Process the block before the first launch section.
- */
- private void processBeforeLaunchBlock()
- {
- switch (beforeLaunchBlock.getState())
- {
- case STATE_BLOCK_FREE:
- beforeLaunchBlock.getSection().setTransportsOff();
- beforeLaunchBlock.getSection().setBrakesOn();
- break;
- case STATE_BLOCK_APPROACHING:
- if (beforeLaunchBlock.getSection().isTrainOnSection())
- {
- beforeLaunchBlock.setState(STATE_BLOCK_BEFORE_TRIGGER);
- }
- else
- {
- if (prepareReverse1BlockEntering())
- {
- beforeLaunchBlock.getSection().setBrakesOff();
- }
- else
- {
- beforeLaunchBlock.getSection().setBrakesOn();
- }
- beforeLaunchBlock.getSection().setTransportsOff();
- }
- break;
- case STATE_BLOCK_BEFORE_TRIGGER:
- prepareReverse1BlockEntering();
- beforeLaunchBlock.getSection().setBrakesTrim();
- beforeLaunchBlock.getSection().setTransportsStandardFwdDependingOnBrake();
- if (beforeLaunchBlock.getSection().isTrainBehindBrakeTrigger())
- {
- beforeLaunchBlock.setState(STATE_BLOCK_BEHIND_TRIGGER);
- }
- break;
- case STATE_BLOCK_BEHIND_TRIGGER:
- if (mode == MANUAL_BLOCK_MODE)
- {
- if (prepareReverse1BlockEntering())
- {
- beforeLaunchBlock.setState(STATE_BLOCK_WAIT_FOR_ADVANCE);
- }
- else
- {
- beforeLaunchBlock.getSection().setTransportsOff();
- beforeLaunchBlock.getSection().setBrakesOn();
- }
- }
- else
- {
- if (prepareReverse1BlockEntering() && (mode == AUTO_MODE))
- {
- reverse1Block.setState(STATE_BLOCK_APPROACHING);
- beforeLaunchBlock.setState(STATE_BLOCK_LEAVING);
- }
- else
- {
- beforeLaunchBlock.getSection().setTransportsOff();
- beforeLaunchBlock.getSection().setBrakesOn();
- }
- }
- break;
- case STATE_BLOCK_WAIT_FOR_ADVANCE:
- if (mode == AUTO_MODE)
- {
- beforeLaunchBlock.setState(STATE_BLOCK_BEHIND_TRIGGER);
- }
- else
- {
- if (!prepareReverse1BlockEntering())
- {
- beforeLaunchBlock.setState(STATE_BLOCK_BEHIND_TRIGGER);
- }
- else
- {
- beforeLaunchBlock.getSection().setTransportsOff();
- beforeLaunchBlock.getSection().setBrakesOn();
- }
- }
- break;
- case STATE_BLOCK_LEAVING:
- beforeLaunchBlock.getSection().setBrakesOff();
- beforeLaunchBlock.getSection().setTransportsLaunchFwdOn();
- if (beforeLaunchBlock.getNumberOfTrainsOnBlock() == 0)
- {
- beforeLaunchBlock.setState(STATE_BLOCK_FREE);
- }
- break;
- case STATE_BLOCK_WAIT_FOR_CLEAR:
- if (beforeLaunchBlock.getSection().isTrainBehindBrakeTrigger())
- {
- beforeLaunchBlock.setState(STATE_BLOCK_BEHIND_TRIGGER);
- }
- else
- {
- beforeLaunchBlock.setState(STATE_BLOCK_BEFORE_TRIGGER);
- }
- break;
- }
- }
- /**
- * This method processes the Reverse1 block
- */
- private void processReverse1Block()
- {
- switch (reverse1Block.getState())
- {
- case STATE_BLOCK_APPROACHING:
- reverse1Block.getSection().setBrakesOff();
- reverse1Block.getSection().setTransportsOff();
- if (reverse1Block.getSection().isTrainOnSection())
- {
- reverse1Block.setState(STATE_BLOCK_BEFORE_TRIGGER);
- }
- break;
- case STATE_BLOCK_BEFORE_TRIGGER:
- reverse1Block.getSection().setBrakesTrim();
- reverse1Block.getSection().setTransportsStandardFwdDependingOnBrake();
- if (reverse1Block.getSection().isTrainBehindBrakeTrigger())
- {
- reverse1Block.setState(STATE_BLOCK_BEHIND_TRIGGER);
- }
- break;
- case STATE_BLOCK_BEHIND_TRIGGER:
- reverse1Block.getSection().setBrakesOn();
- reverse1Block.getSection().setTransportsOff();
- {
- Train train = reverse1Block.getSection().getTrainOnSection();
- if (train.getSpeed() == 0)
- {
- reverse1BlockTime = 0;
- reverse1Block.setState(STATE_BLOCK_WAITING);
- }
- }
- break;
- case STATE_BLOCK_WAITING:
- reverse1Block.getSection().setBrakesOn();
- reverse1Block.getSection().setTransportsOff();
- reverse1BlockTime += sim.getCurSimulationTickSec();
- prepareReverse1BlockLeaving();
- if (reverse1BlockTime >= reverse1Block.getSection().getBrakeWaitTime())
- {
- reverse1Block.setState(STATE_BLOCK_WAIT_FOR_CLEAR);
- }
- break;
- case STATE_BLOCK_WAIT_FOR_CLEAR:
- reverse1Block.getSection().setBrakesOn();
- reverse1Block.getSection().setTransportsOff();
- if (prepareReverse1BlockLeaving())
- {
- if (mode == MANUAL_BLOCK_MODE)
- {
- reverse1Block.setState(STATE_BLOCK_WAIT_FOR_ADVANCE);
- }
- else
- {
- reverse2Block.setState(STATE_BLOCK_APPROACHING);
- reverse1Block.setState(STATE_BLOCK_LEAVING);
- }
- }
- break;
- case STATE_BLOCK_WAIT_FOR_ADVANCE:
- reverse1Block.getSection().setBrakesOn();
- reverse1Block.getSection().setTransportsOff();
- if (!prepareReverse1BlockLeaving())
- {
- reverse1Block.setState(STATE_BLOCK_WAIT_FOR_CLEAR);
- }
- break;
- case STATE_BLOCK_LEAVING:
- if (reverse1Block.getSection().isTrainOnSection())
- {
- reverse1Block.getSection().setBrakesOff();
- reverse1Block.getSection().setTransportsLaunchBwdOn();
- }
- else
- {
- reverse1Block.getSection().setBrakesOn();
- reverse1Block.getSection().setTransportsOff();
- }
- if (reverse1Block.getNumberOfTrainsOnBlock() == 0)
- {
- reverse1Block.setState(STATE_BLOCK_FREE);
- }
- break;
- case STATE_BLOCK_FREE:
- reverse1Block.getSection().setBrakesOn();
- reverse1Block.getSection().setTransportsOff();
- break;
- }
- }
- /**
- * This method processes the Reverse2 block
- */
- private void processReverse2Block()
- {
- switch (reverse2Block.getState())
- {
- case STATE_BLOCK_FREE:
- reverse2Block.getSection().setBrakesOn();
- reverse2Block.getSection().setTransportsOff();
- break;
- case STATE_BLOCK_APPROACHING:
- reverse2Block.getSection().setBrakesOff();
- reverse2Block.getSection().setTransportsOff();
- if (reverse2Block.getSection().isTrainOnSection())
- {
- reverse2Block.setState(STATE_BLOCK_BEFORE_TRIGGER);
- }
- break;
- case STATE_BLOCK_APPROACHING_B:
- reverse2Block.getSection().setBrakesOff();
- reverse2Block.getSection().setTransportsOff();
- if (reverse2Block.getSection().isTrainOnSection())
- {
- reverse2Block.setState(STATE_BLOCK_BEFORE_TRIGGER_B);
- }
- break;
- case STATE_BLOCK_BEFORE_TRIGGER:
- reverse2Block.getSection().setBrakesTrim();
- reverse2Block.getSection().setTransportsStandardBwdDependingOnBrake();
- if (reverse2Block.getSection().isTrainBeforeBrakeTrigger())
- {
- reverse2Block.setState(STATE_BLOCK_BEHIND_TRIGGER);
- }
- break;
- case STATE_BLOCK_BEFORE_TRIGGER_B:
- reverse2Block.getSection().setBrakesTrim();
- reverse2Block.getSection().setTransportsStandardFwdDependingOnBrake();
- if (reverse2Block.getSection().isTrainBehindBrakeTrigger())
- {
- reverse2Block.setState(STATE_BLOCK_BEHIND_TRIGGER);
- }
- break;
- case STATE_BLOCK_BEHIND_TRIGGER:
- reverse2Block.getSection().setBrakesOn();
- reverse2Block.getSection().setTransportsOff();
- {
- Train train = reverse2Block.getSection().getTrainOnSection();
- if (train.getSpeed() == 0)
- {
- reverse2Block.setState(STATE_BLOCK_WAIT_FOR_CLEAR);
- reverse2BlockTime = 0;
- }
- }
- break;
- case STATE_BLOCK_WAIT_FOR_CLEAR:
- // wait at least one second before we switch the track
- if (reverse2BlockTime >= 1.0)
- {
- if (mode == MANUAL_BLOCK_MODE)
- {
- if (prepareReverse2BlockLeaving() || canReverse2BlockLeaveBwd())
- {
- reverse2Block.setState(STATE_BLOCK_WAIT_FOR_ADVANCE);
- }
- }
- else
- {
- if (prepareReverse2BlockLeaving())
- {
- beforeStationBlock.setState(STATE_BLOCK_APPROACHING);
- reverse2Block.setState(STATE_BLOCK_LEAVING);
- }
- }
- }
- else
- {
- reverse2BlockTime += sim.getCurSimulationTickSec();
- }
- reverse2Block.getSection().setBrakesOn();
- reverse2Block.getSection().setTransportsOff();
- break;
- case STATE_BLOCK_WAIT_FOR_ADVANCE:
- if ((mode != MANUAL_BLOCK_MODE) || (!prepareReverse2BlockLeaving() && !canReverse2BlockLeaveBwd()))
- {
- reverse2Block.setState(STATE_BLOCK_WAIT_FOR_CLEAR);
- }
- break;
- case STATE_BLOCK_LEAVING:
- reverse2Block.getSection().setBrakesOff();
- reverse2Block.getSection().setTransportsStandardFwdOn();
- if (reverse2Block.getNumberOfTrainsOnBlock() == 0)
- {
- reverse2Block.setState(STATE_BLOCK_FREE);
- }
- break;
- case STATE_BLOCK_LEAVING_B:
- reverse2Block.getSection().setBrakesOff();
- reverse2Block.getSection().setTransportsStandardBwdOn();
- if (reverse2Block.getNumberOfTrainsOnBlock() == 0)
- {
- reverse2Block.setState(STATE_BLOCK_FREE);
- }
- break;
- }
- }
- /**
- * This method processes the block before the stations
- */
- private void processBeforeStationBlock()
- {
- switch (beforeStationBlock.getState())
- {
- case STATE_BLOCK_FREE:
- beforeStationBlock.getSection().setBrakesOn();
- beforeStationBlock.getSection().setTransportsOff();
- break;
- case STATE_BLOCK_APPROACHING:
- beforeStationBlock.getSection().setBrakesOff();
- beforeStationBlock.getSection().setTransportsOff();
- if (beforeStationBlock.getSection().isTrainOnSection())
- {
- beforeStationBlock.setState(STATE_BLOCK_BEFORE_TRIGGER);
- }
- break;
- case STATE_BLOCK_BEFORE_TRIGGER:
- beforeStationBlock.getSection().setBrakesTrim();
- beforeStationBlock.getSection().setTransportsStandardFwdDependingOnBrake();
- if (beforeStationBlock.getSection().isTrainBehindBrakeTrigger())
- {
- beforeStationBlock.setState(STATE_BLOCK_BEHIND_TRIGGER);
- }
- break;
- case STATE_BLOCK_BEHIND_TRIGGER:
- if (stationExitBlock.getState() == STATE_BLOCK_FREE)
- {
- if (mode == MANUAL_BLOCK_MODE)
- {
- beforeStationBlock.setState(STATE_BLOCK_WAIT_FOR_ADVANCE);
- }
- else
- {
- stationExitBlock.setState(STATE_BLOCK_APPROACHING);
- beforeStationBlock.setState(STATE_BLOCK_LEAVING);
- }
- }
- else
- {
- beforeStationBlock.getSection().setBrakesOn();
- beforeStationBlock.getSection().setTransportsOff();
- {
- Train train = beforeStationBlock.getSection().getTrainOnSection();
- if (train.getSpeed() == 0)
- {
- beforeStationBlock.setState(STATE_BLOCK_WAIT_FOR_CLEAR);
- }
- }
- }
- break;
- case STATE_BLOCK_WAIT_FOR_ADVANCE:
- if ((mode != MANUAL_BLOCK_MODE) || (stationExitBlock.getState() != STATE_BLOCK_FREE))
- {
- beforeStationBlock.setState(STATE_BLOCK_WAIT_FOR_CLEAR);
- }
- beforeStationBlock.getSection().setBrakesOn();
- beforeStationBlock.getSection().setTransportsOff();
- break;
- case STATE_BLOCK_WAIT_FOR_CLEAR:
- if (stationExitBlock.getState() == STATE_BLOCK_FREE)
- {
- if (mode == MANUAL_BLOCK_MODE)
- {
- beforeStationBlock.setState(STATE_BLOCK_WAIT_FOR_ADVANCE);
- }
- else
- {
- stationExitBlock.setState(STATE_BLOCK_APPROACHING);
- beforeStationBlock.setState(STATE_BLOCK_LEAVING);
- }
- }
- else
- {
- beforeStationBlock.getSection().setBrakesOn();
- beforeStationBlock.getSection().setTransportsOff();
- }
- break;
- case STATE_BLOCK_LEAVING:
- beforeStationBlock.getSection().setBrakesOff();
- beforeStationBlock.getSection().setTransportsStandardFwdOn();
- if (beforeStationBlock.getNumberOfTrainsOnBlock() == 0)
- {
- beforeStationBlock.setState(STATE_BLOCK_FREE);
- }
- break;
- }
- }
- /**
- * This method processes the Storage1 block
- */
- private void processStorage1Block()
- {
- switch (storage1Block.getState())
- {
- case STATE_BLOCK_FREE:
- storage1Block.getSection().setTransportsOff();
- break;
- case STATE_BLOCK_APPROACHING:
- storage1Block.getSection().setTransportsOff();
- if (storage1Block.getSection().isTrainOnSection())
- {
- storage1Block.setState(STATE_BLOCK_BEFORE_TRIGGER);
- }
- break;
- case STATE_BLOCK_APPROACHING_B:
- storage1Block.getSection().setTransportsOff();
- if (storage1Block.getSection().isTrainOnSection())
- {
- storage1Block.setState(STATE_BLOCK_BEFORE_TRIGGER_B);
- }
- break;
- case STATE_BLOCK_BEFORE_TRIGGER:
- storage1Block.getSection().setTransportsStandardFwdOn();
- if (storage1Block.getSection().isTrainBehindCenterOfSection())
- {
- storage1Block.setState(STATE_BLOCK_WAIT_FOR_CLEAR);
- }
- break;
- case STATE_BLOCK_BEFORE_TRIGGER_B:
- storage1Block.getSection().setTransportsStandardBwdOn();
- if (storage1Block.getSection().isTrainBeforeCenterOfSection())
- {
- storage1Block.getSection().getTrainOnSection().setLashedToTrack(true);
- storage1Block.setState(STATE_BLOCK_WAIT_FOR_CLEAR);
- }
- break;
- case STATE_BLOCK_WAIT_FOR_CLEAR:
- storage1Block.getSection().setTransportsOff();
- if (mode == MANUAL_BLOCK_MODE)
- {
- if ((storage2Block.getState() == STATE_BLOCK_FREE) || canEnterReverse2BlockFromStorage())
- {
- storage1Block.setState(STATE_BLOCK_WAIT_FOR_ADVANCE);
- }
- }
- break;
- case STATE_BLOCK_WAIT_FOR_ADVANCE:
- storage1Block.getSection().setTransportsOff();
- if ((mode != MANUAL_BLOCK_MODE) || ((storage2Block.getState() != STATE_BLOCK_FREE) && !canEnterReverse2BlockFromStorage()))
- {
- storage1Block.setState(STATE_BLOCK_WAIT_FOR_CLEAR);
- }
- break;
- case STATE_BLOCK_LEAVING:
- storage1Block.getSection().setTransportsStandardFwdOn();
- if (!storage1Block.getSection().isTrainOnSection())
- {
- storage1Block.setState(STATE_BLOCK_FREE);
- }
- break;
- case STATE_BLOCK_LEAVING_B:
- storage1Block.getSection().setTransportsStandardBwdOn();
- if (!storage1Block.getSection().isTrainOnSection())
- {
- storage1Block.setState(STATE_BLOCK_FREE);
- }
- break;
- }
- }
- /**
- * This method processes the Storage2 block
- */
- private void processStorage2Block()
- {
- switch (storage2Block.getState())
- {
- case STATE_BLOCK_FREE:
- storage2Block.getSection().setTransportsOff();
- break;
- case STATE_BLOCK_APPROACHING_B:
- storage2Block.getSection().setTransportsOff();
- if (storage2Block.getSection().isTrainOnSection())
- {
- storage2Block.setState(STATE_BLOCK_BEFORE_TRIGGER_B);
- }
- break;
- case STATE_BLOCK_BEFORE_TRIGGER_B:
- storage2Block.getSection().setTransportsStandardBwdOn();
- if (storage2Block.getSection().isTrainBeforeCenterOfSection())
- {
- storage2Block.getSection().getTrainOnSection().setLashedToTrack(true);
- storage2Block.setState(STATE_BLOCK_WAIT_FOR_CLEAR);
- }
- break;
- case STATE_BLOCK_WAIT_FOR_CLEAR:
- storage2Block.getSection().setTransportsOff();
- if (mode == MANUAL_BLOCK_MODE)
- {
- if (storage1Block.getState() == STATE_BLOCK_FREE)
- {
- storage2Block.setState(STATE_BLOCK_WAIT_FOR_ADVANCE);
- }
- }
- break;
- case STATE_BLOCK_WAIT_FOR_ADVANCE:
- storage2Block.getSection().setTransportsOff();
- if ((mode != MANUAL_BLOCK_MODE) || (storage1Block.getState() != STATE_BLOCK_FREE))
- {
- storage2Block.setState(STATE_BLOCK_WAIT_FOR_CLEAR);
- }
- break;
- case STATE_BLOCK_LEAVING:
- storage2Block.getSection().setTransportsStandardFwdOn();
- if (!storage2Block.getSection().isTrainOnSection())
- {
- storage2Block.setState(STATE_BLOCK_FREE);
- }
- break;
- }
- }
- /**
- * This method checks if the Reverse1 block can be entered and tries to set the corresponding track switch in auto-mode.
- */
- private bool prepareReverse1BlockEntering()
- {
- if (reverse1Block.getState() == STATE_BLOCK_FREE)
- {
- if (reverse1Switch.getSwitchDirection() == 0)
- {
- return true;
- }
- else
- {
- if (mode == AUTO_MODE)
- {
- reverse1Switch.setSwitchDirection(0);
- }
- }
- }
- return false;
- }
- /**
- * This method checks if a train on the Reverse1 block can leave the block and enter the next block.
- * It tries to set the corresponding track switches in auto-mode.
- */
- private bool prepareReverse1BlockLeaving()
- {
- bool b1 = false;
- bool b2 = false;
- if (reverse1Switch.getSwitchDirection() == 1)
- {
- b1 = true;
- }
- else
- {
- if (mode == AUTO_MODE)
- {
- reverse1Switch.setSwitchDirection(1);
- }
- }
- if (reverse2Block.getState() == STATE_BLOCK_FREE)
- {
- if (reverse2Switch.getSwitchDirection() == 0)
- {
- b2 = true;
- }
- else
- {
- if (mode == AUTO_MODE)
- {
- reverse2Switch.setSwitchDirection(0);
- }
- }
- }
- return b1 && b2;
- }
- /**
- * This method checks if a train on the Reverse2 block can leave the block and enter the next block.
- * It tries to set the corresponding track switch in auto-mode.
- */
- private bool prepareReverse2BlockLeaving()
- {
- if (reverse2Switch.getSwitchDirection() == 1)
- {
- if (beforeStationBlock.getState() == STATE_BLOCK_FREE)
- {
- return true;
- }
- }
- else
- {
- if (mode == AUTO_MODE)
- {
- reverse2Switch.setSwitchDirection(1);
- }
- }
- return false;
- }
- /**
- * This method checks if a train on the Reverse2 block can leave to the storage tracks.
- */
- private bool canReverse2BlockLeaveBwd()
- {
- return (reverse2Switch.getSwitchDirection() == 2) && (storage1Block.getState() == STATE_BLOCK_FREE);
- }
- /**
- * This method checks if a train on the storage track can enter the Reverse2 block
- */
- private bool canEnterReverse2BlockFromStorage()
- {
- return (reverse2Switch.getSwitchDirection() == 2) && (reverse2Block.getState() == STATE_BLOCK_FREE);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement