Advertisement
Guest User

Original Script

a guest
Jan 15th, 2014
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 38.21 KB | None | 0 0
  1. import com.nolimitscoaster.*;
  2.  
  3. /**
  4. * This script implements a Block System Controller for the Cursing Mommy Coaster.
  5. * It may look complicated, but it is not that complex actually.
  6. * The biggest complexity comes from implementing the proper way to handle manual block mode events and to control the switches.
  7. */
  8. public class BlockScript extends Script implements BlockSystemController
  9. {
  10. // Declare some constants...
  11. // These constants represents our block states, those values are user defineable
  12. private static final int STATE_BLOCK_FREE = 0;
  13. private static final int STATE_BLOCK_APPROACHING = 1;
  14. private static final int STATE_BLOCK_LEAVING = 2;
  15. private static final int STATE_BLOCK_BEFORE_TRIGGER = 3;
  16. private static final int STATE_BLOCK_BEHIND_TRIGGER = 4;
  17. private static final int STATE_BLOCK_WAITING = 5;
  18. private static final int STATE_BLOCK_WAIT_FOR_CLEAR = 6;
  19. private static final int STATE_BLOCK_WAIT_FOR_ADVANCE = 7;
  20. private static final int STATE_BLOCK_IN_STATION = 8;
  21. private static final int STATE_BLOCK_APPROACHING_B = 9;
  22. private static final int STATE_BLOCK_LEAVING_B = 10;
  23. private static final int STATE_BLOCK_BEFORE_TRIGGER_B = 11;
  24.  
  25. // The name of the script for error messages
  26. private static final String scriptName = "BlockScript";
  27.  
  28. // The coaster operation mode
  29. private static final int AUTO_MODE = 0;
  30. private static final int MANUAL_BLOCK_MODE = 1;
  31. private static final int FULL_MANUAL_MODE = 2;
  32.  
  33. // Member variables...
  34. private Coaster coaster;
  35. private Block liftBlock;
  36. private Block beforeLaunchBlock;
  37. private Block stationExitBlock;
  38. private Block stationEnterBlock;
  39. private Block reverse1Block;
  40. private Block reverse2Block;
  41. private Block beforeStationBlock;
  42. private Block storage1Block;
  43. private Block storage2Block;
  44. private SpecialTrack reverse1Switch;
  45. private SpecialTrack reverse2Switch;
  46. private float reverse1BlockTime;
  47. private float reverse2BlockTime;
  48. private int mode;
  49.  
  50. /**
  51. * This method overrides the default implementation of onInit() from the Script class.
  52. * Gets called at Script startup.
  53. */
  54. public bool onInit()
  55. {
  56. String name;
  57.  
  58. // Detect the coaster this script belongs to...
  59. coaster = sim.getCoasterForEntityId(getParentEntityId());
  60. if (coaster == null)
  61. {
  62. System.err.println(scriptName + ": Not attached to coaster");
  63. return false;
  64. }
  65.  
  66. // Assign the block system controller to the coaster
  67. coaster.setBlockSystemController(this);
  68.  
  69. /////
  70.  
  71. // Get and initialize all blocks
  72.  
  73. name = "Lift";
  74. liftBlock = coaster.getBlock(name);
  75. if (!checkAndSetInitialBlockState(liftBlock, name))
  76. {
  77. return false;
  78. }
  79. liftBlock.setAdvanceFwdVisible(true); // Set buttons used on the control panel
  80.  
  81. name = "Before Launch";
  82. beforeLaunchBlock = coaster.getBlock(name);
  83. if (!checkAndSetInitialBlockState(beforeLaunchBlock, name))
  84. {
  85. return false;
  86. }
  87. beforeLaunchBlock.setAdvanceFwdVisible(true);
  88.  
  89. name = "Before Station";
  90. beforeStationBlock = coaster.getBlock(name);
  91. if (!checkAndSetInitialBlockState(beforeStationBlock, name))
  92. {
  93. return false;
  94. }
  95. beforeStationBlock.setAdvanceFwdVisible(true);
  96.  
  97. name = "Station Exit";
  98. stationExitBlock = coaster.getBlock(name);
  99. if (!checkAndSetInitialBlockState(stationExitBlock, name))
  100. {
  101. return false;
  102. }
  103. stationExitBlock.setAdvanceFwdVisible(true);
  104.  
  105. name = "Station Enter";
  106. stationEnterBlock = coaster.getBlock(name);
  107. if (!checkAndSetInitialBlockState(stationEnterBlock, name))
  108. {
  109. return false;
  110. }
  111. stationEnterBlock.setAdvanceFwdVisible(true);
  112.  
  113. name = "First Dir Change Brake";
  114. reverse1Block = coaster.getBlock(name);
  115. if (!checkAndSetInitialBlockState(reverse1Block, name))
  116. {
  117. return false;
  118. }
  119. reverse1Block.setAdvanceFwdVisible(true);
  120.  
  121. name = "Reverse 2 Brake";
  122. reverse2Block = coaster.getBlock(name);
  123. if (!checkAndSetInitialBlockState(reverse2Block, name))
  124. {
  125. return false;
  126. }
  127. reverse2Block.setAdvanceFwdVisible(true);
  128. reverse2Block.setAdvanceBwdVisible(true);
  129.  
  130. name = "Storage 1";
  131. storage1Block = coaster.getBlock(name);
  132. if (!checkAndSetInitialBlockState(storage1Block, name))
  133. {
  134. return false;
  135. }
  136. storage1Block.setAdvanceFwdVisible(true);
  137. storage1Block.setAdvanceBwdVisible(true);
  138.  
  139. name = "Storage 2";
  140. storage2Block = coaster.getBlock(name);
  141. if (!checkAndSetInitialBlockState(storage2Block, name))
  142. {
  143. return false;
  144. }
  145. storage2Block.setAdvanceFwdVisible(true);
  146.  
  147. /////
  148.  
  149. // Get and initialize switches
  150.  
  151. name = "Reverse 1 Switch";
  152. reverse1Switch = coaster.getSpecialTrack(name);
  153. if (reverse1Switch == null)
  154. {
  155. System.err.println(scriptName + ": SpecialTrack '" + name + "' not found");
  156. return false;
  157. }
  158.  
  159. name = "Reverse 2 Switch";
  160. reverse2Switch = coaster.getSpecialTrack(name);
  161. if (reverse2Switch == null)
  162. {
  163. System.err.println(scriptName + ": SpecialTrack '" + name + "' not found");
  164. return false;
  165. }
  166.  
  167. mode = AUTO_MODE;
  168.  
  169. return true;
  170. }
  171.  
  172. /**
  173. * This method overrides the default implementation of the Script class.
  174. * Gets called when the Script is about to exit.
  175. */
  176. public void onExit()
  177. {
  178. }
  179.  
  180. /**
  181. * This method overrides the default implementation of OnNextFrame() from the Script class.
  182. * Gets called for each frame.
  183. */
  184. public void onNextFrame(float tick)
  185. {
  186. if (mode != FULL_MANUAL_MODE)
  187. {
  188. // process all blocks...
  189.  
  190. processStation(stationExitBlock, stationEnterBlock);
  191. processStation(stationEnterBlock, liftBlock);
  192. processLiftBlock();
  193. processBeforeLaunchBlock();
  194. processReverse1Block();
  195. processReverse2Block();
  196. processBeforeStationBlock();
  197. processStorage1Block();
  198. processStorage2Block();
  199.  
  200. if (mode == MANUAL_BLOCK_MODE)
  201. {
  202. // update the control panel user interface
  203. updateControlPanel();
  204. }
  205. }
  206. }
  207.  
  208. /**
  209. * This method is part of the BlockSystemController interface
  210. * Gets called when the Auto Block mode gets selected on the control panel.
  211. */
  212. public void onAutoMode(Coaster c)
  213. {
  214. //System.out.println("onAutoMode");
  215. if (mode == FULL_MANUAL_MODE)
  216. {
  217. // previous mode was full manual, we need to check the new position of the trains now
  218. setInitialBlockState(liftBlock);
  219. setInitialBlockState(beforeLaunchBlock);
  220. setInitialBlockState(beforeStationBlock);
  221. setInitialBlockState(stationExitBlock);
  222. setInitialBlockState(stationEnterBlock);
  223. setInitialBlockState(reverse1Block);
  224. setInitialBlockState(reverse2Block);
  225. }
  226.  
  227. mode = AUTO_MODE;
  228. updateControlPanel();
  229. }
  230.  
  231. /**
  232. * This method is part of the BlockSystemController interface
  233. * Gets called when the Manual Block gets selected on the control panel.
  234. */
  235. public void onManualBlockMode(Coaster c)
  236. {
  237. //System.out.println("onManualBlockMode");
  238. if (mode == FULL_MANUAL_MODE)
  239. {
  240. // previous mode was full manual, we need to check the new position of the trains now
  241. setInitialBlockState(liftBlock);
  242. setInitialBlockState(beforeLaunchBlock);
  243. setInitialBlockState(beforeStationBlock);
  244. setInitialBlockState(stationExitBlock);
  245. setInitialBlockState(stationEnterBlock);
  246. setInitialBlockState(reverse1Block);
  247. setInitialBlockState(reverse2Block);
  248. setInitialBlockState(storage1Block);
  249. setInitialBlockState(storage2Block);
  250. }
  251. mode = MANUAL_BLOCK_MODE;
  252. updateControlPanel();
  253. }
  254.  
  255. /**
  256. * This method is part of the BlockSystemController interface
  257. * Gets called when the Full Manual mode gets selected on the control panel.
  258. */
  259. public void onFullManualMode(Coaster c)
  260. {
  261. //System.out.println("onFullManualMode");
  262. mode = FULL_MANUAL_MODE;
  263. updateControlPanel();
  264. }
  265.  
  266. /**
  267. * This method is part of the BlockSystemController interface
  268. * Gets called when the user clicks on the Advance Fwd Button on the control panel.
  269. */
  270. public void onAdvanceFWDButton(Block block)
  271. {
  272. if (block == beforeLaunchBlock)
  273. {
  274. reverse1Block.setState(STATE_BLOCK_APPROACHING);
  275. beforeLaunchBlock.setState(STATE_BLOCK_LEAVING);
  276. }
  277. else if (block == liftBlock)
  278. {
  279. beforeLaunchBlock.setState(STATE_BLOCK_APPROACHING);
  280. liftBlock.setState(STATE_BLOCK_LEAVING);
  281. }
  282. else if (block == stationEnterBlock)
  283. {
  284. beforeLaunchBlock.setState(STATE_BLOCK_APPROACHING);
  285. stationEnterBlock.setState(STATE_BLOCK_LEAVING);
  286. stationEnterBlock.getSection().setStationLeaving();
  287. }
  288. else if (block == stationExitBlock)
  289. {
  290. stationEnterBlock.setState(STATE_BLOCK_APPROACHING);
  291. stationExitBlock.setState(STATE_BLOCK_LEAVING);
  292. stationExitBlock.getSection().setStationLeaving();
  293. }
  294. else if (block == beforeStationBlock)
  295. {
  296. stationExitBlock.setState(STATE_BLOCK_APPROACHING);
  297. beforeStationBlock.setState(STATE_BLOCK_LEAVING);
  298. }
  299. else if (block == reverse1Block)
  300. {
  301. reverse2Block.setState(STATE_BLOCK_APPROACHING);
  302. reverse1Block.setState(STATE_BLOCK_LEAVING);
  303. }
  304. else if (block == reverse2Block)
  305. {
  306. beforeStationBlock.setState(STATE_BLOCK_APPROACHING);
  307. reverse2Block.setState(STATE_BLOCK_LEAVING);
  308. }
  309. else if (block == storage1Block)
  310. {
  311. Train train = block.getSection().getTrainOnSection();
  312. if (train != null) train.setLashedToTrack(false);
  313. reverse2Block.setState(STATE_BLOCK_APPROACHING_B);
  314. storage1Block.setState(STATE_BLOCK_LEAVING);
  315. }
  316. else if (block == storage2Block)
  317. {
  318. Train train = block.getSection().getTrainOnSection();
  319. if (train != null) train.setLashedToTrack(false);
  320. storage1Block.setState(STATE_BLOCK_APPROACHING);
  321. storage2Block.setState(STATE_BLOCK_LEAVING);
  322. }
  323. }
  324.  
  325. /**
  326. * This method is part of the BlockSystemController interface
  327. * Gets called when the user clicks on the Advance Bwd Button on the control panel.
  328. */
  329. public void onAdvanceBWDButton(Block block)
  330. {
  331. if (block == reverse2Block)
  332. {
  333. storage1Block.setState(STATE_BLOCK_APPROACHING_B);
  334. reverse2Block.setState(STATE_BLOCK_LEAVING_B);
  335. }
  336. else if (block == storage1Block)
  337. {
  338. Train train = block.getSection().getTrainOnSection();
  339. if (train != null) train.setLashedToTrack(false);
  340. storage2Block.setState(STATE_BLOCK_APPROACHING_B);
  341. storage1Block.setState(STATE_BLOCK_LEAVING_B);
  342. }
  343. }
  344.  
  345. /**
  346. * 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.
  347. */
  348. private static bool checkAndSetInitialBlockState(Block block, String name)
  349. {
  350. if (block == null)
  351. {
  352. System.err.println(scriptName + ": Block '" + name + "' not found");
  353. return false;
  354. }
  355. registerBlockStates(block);
  356. setInitialBlockState(block);
  357. return true;
  358. }
  359.  
  360. /**
  361. * This method checks if a train is on the block or not and sets the corresponding state.
  362. */
  363. private static void setInitialBlockState(Block block)
  364. {
  365. if (block.getNumberOfTrainsOnBlock() > 0)
  366. {
  367. if (block.getSection().iStation())
  368. {
  369. block.setState(STATE_BLOCK_IN_STATION);
  370. }
  371. else
  372. {
  373. block.setState(STATE_BLOCK_WAIT_FOR_CLEAR);
  374. }
  375. }
  376. else
  377. {
  378. block.setState(STATE_BLOCK_FREE);
  379. }
  380. }
  381.  
  382. /**
  383. * Adds labels for each possible state to a block.
  384. * The labels are recommended for display on the control panel, there is no other purpose.
  385. */
  386. private static void registerBlockStates(Block block)
  387. {
  388. // register the states, so that some usefull text will be shown on the block tab of the control panel
  389. block.registerState(STATE_BLOCK_FREE, "Free", Block.LAMP_OFF);
  390.  
  391. block.registerState(STATE_BLOCK_APPROACHING, "Approaching", Block.LAMP_FLASHING);
  392. block.registerState(STATE_BLOCK_APPROACHING_B, "Approaching", Block.LAMP_FLASHING);
  393.  
  394. block.registerState(STATE_BLOCK_LEAVING, "Leaving", Block.LAMP_ON);
  395. block.registerState(STATE_BLOCK_LEAVING_B, "Leaving", Block.LAMP_ON);
  396.  
  397. block.registerState(STATE_BLOCK_BEFORE_TRIGGER, "Before Trigger", Block.LAMP_ON);
  398. block.registerState(STATE_BLOCK_BEFORE_TRIGGER_B, "Before Trigger", Block.LAMP_ON);
  399.  
  400. block.registerState(STATE_BLOCK_BEHIND_TRIGGER, "Behind Trigger", Block.LAMP_ON);
  401.  
  402. block.registerState(STATE_BLOCK_WAITING, "Waiting", Block.LAMP_ON);
  403. block.registerState(STATE_BLOCK_WAIT_FOR_CLEAR, "Waiting for Clear Block", Block.LAMP_ON);
  404. block.registerState(STATE_BLOCK_WAIT_FOR_ADVANCE, "Waiting for Advance", Block.LAMP_ON);
  405.  
  406. block.registerState(STATE_BLOCK_IN_STATION, "In Station", Block.LAMP_ON);
  407. }
  408.  
  409. /**
  410. * Will update the user interface elements based on the current states
  411. */
  412. private void updateControlPanel()
  413. {
  414. liftBlock.setAdvanceFwdEnabled(liftBlock.getState() == STATE_BLOCK_WAIT_FOR_ADVANCE);
  415. beforeLaunchBlock.setAdvanceFwdEnabled(beforeLaunchBlock.getState() == STATE_BLOCK_WAIT_FOR_ADVANCE);
  416. stationEnterBlock.setAdvanceFwdEnabled(stationEnterBlock.getState() == STATE_BLOCK_WAIT_FOR_ADVANCE);
  417. stationExitBlock.setAdvanceFwdEnabled(stationExitBlock.getState() == STATE_BLOCK_WAIT_FOR_ADVANCE);
  418. beforeStationBlock.setAdvanceFwdEnabled(beforeStationBlock.getState() == STATE_BLOCK_WAIT_FOR_ADVANCE);
  419. reverse1Block.setAdvanceFwdEnabled(reverse1Block.getState() == STATE_BLOCK_WAIT_FOR_ADVANCE);
  420.  
  421. reverse2Block.setAdvanceFwdEnabled((reverse2Block.getState() == STATE_BLOCK_WAIT_FOR_ADVANCE) && prepareReverse2BlockLeaving());
  422. reverse2Block.setAdvanceBwdEnabled((reverse2Block.getState() == STATE_BLOCK_WAIT_FOR_ADVANCE) && canReverse2BlockLeaveBwd());
  423.  
  424. storage1Block.setAdvanceFwdEnabled((storage1Block.getState() == STATE_BLOCK_WAIT_FOR_ADVANCE) && canEnterReverse2BlockFromStorage());
  425. storage1Block.setAdvanceBwdEnabled((storage1Block.getState() == STATE_BLOCK_WAIT_FOR_ADVANCE) && (storage2Block.getState() == STATE_BLOCK_FREE));
  426.  
  427. storage2Block.setAdvanceFwdEnabled(storage2Block.getState() == STATE_BLOCK_WAIT_FOR_ADVANCE);
  428.  
  429. reverse1Switch.setCanManualSwitchDirection((reverse1Block.getState() == STATE_BLOCK_FREE) || (reverse1Block.getState() == STATE_BLOCK_WAIT_FOR_ADVANCE) || (reverse1Block.getState() == STATE_BLOCK_WAIT_FOR_CLEAR));
  430. reverse2Switch.setCanManualSwitchDirection((reverse2Block.getState() == STATE_BLOCK_FREE) || (reverse2Block.getState() == STATE_BLOCK_WAIT_FOR_ADVANCE) || (reverse2Block.getState() == STATE_BLOCK_WAIT_FOR_CLEAR));
  431. }
  432.  
  433. /**
  434. * A universal method to process a station block.
  435. * This method works for both stations.
  436. */
  437. private void processStation(Block stationBlock, Block nextBlock)
  438. {
  439. switch (stationBlock.getState())
  440. {
  441. case STATE_BLOCK_IN_STATION:
  442. if (stationBlock.getSection().isStationWaitingForClearBlock())
  443. {
  444. if (nextBlock.getState() == STATE_BLOCK_FREE)
  445. {
  446. stationBlock.getSection().setStationNextBlockClear();
  447. }
  448. }
  449. else if (stationBlock.getSection().isStationWaitingForAdvance())
  450. {
  451. if (mode == MANUAL_BLOCK_MODE)
  452. {
  453. if (nextBlock.getState() == STATE_BLOCK_FREE)
  454. {
  455. stationBlock.setState(STATE_BLOCK_WAIT_FOR_ADVANCE);
  456. }
  457. else
  458. {
  459. stationBlock.getSection().setStationNextBlockOccupied();
  460. }
  461. }
  462. else
  463. {
  464. if (nextBlock.getState() == STATE_BLOCK_FREE)
  465. {
  466. nextBlock.setState(STATE_BLOCK_APPROACHING);
  467. stationBlock.setState(STATE_BLOCK_LEAVING);
  468. stationBlock.getSection().setStationLeaving();
  469. }
  470. else
  471. {
  472. stationBlock.getSection().setStationNextBlockOccupied();
  473. }
  474. }
  475. }
  476. break;
  477. case STATE_BLOCK_WAIT_FOR_ADVANCE:
  478. if ((mode != MANUAL_BLOCK_MODE) || !stationBlock.getSection().isStationWaitingForAdvance() || (nextBlock.getState() != STATE_BLOCK_FREE))
  479. {
  480. stationBlock.setState(STATE_BLOCK_IN_STATION);
  481. }
  482. break;
  483. case STATE_BLOCK_LEAVING:
  484. if (stationBlock.getNumberOfTrainsOnBlock() != 0)
  485. {
  486. // Train is still on the block
  487. stationBlock.getSection().setBrakesOff();
  488. stationBlock.getSection().setTransportsStandardFwdOn();
  489. }
  490. else
  491. {
  492. // Train has left the block
  493. stationBlock.setState(STATE_BLOCK_FREE);
  494. }
  495. break;
  496. case STATE_BLOCK_FREE:
  497. stationBlock.getSection().setTransportsOff();
  498. stationBlock.getSection().setBrakesOn();
  499. break;
  500. case STATE_BLOCK_APPROACHING:
  501. if (stationBlock.getSection().isTrainOnSection())
  502. {
  503. stationBlock.getSection().setStationEntering();
  504. stationBlock.setState(STATE_BLOCK_IN_STATION);
  505. }
  506. else
  507. {
  508. stationBlock.getSection().setBrakesOff();
  509. stationBlock.getSection().setTransportsOff();
  510. }
  511. break;
  512. }
  513. }
  514.  
  515. /**
  516. * Process the lift block
  517. */
  518. private void processLiftBlock()
  519. {
  520. switch (liftBlock.getState())
  521. {
  522. case STATE_BLOCK_FREE:
  523. liftBlock.getSection().setLiftOff();
  524. break;
  525. case STATE_BLOCK_APPROACHING:
  526. if (liftBlock.getSection().isTrainOnSection())
  527. {
  528. liftBlock.setState(STATE_BLOCK_BEFORE_TRIGGER);
  529. }
  530. else
  531. {
  532. liftBlock.getSection().setLiftOff();
  533. }
  534. break;
  535. case STATE_BLOCK_BEFORE_TRIGGER:
  536. liftBlock.getSection().setLiftFwdOn();
  537. if (liftBlock.getSection().isTrainBehindLiftTrigger())
  538. {
  539. liftBlock.setState(STATE_BLOCK_BEHIND_TRIGGER);
  540. }
  541. break;
  542. case STATE_BLOCK_BEHIND_TRIGGER:
  543. if (mode == MANUAL_BLOCK_MODE)
  544. {
  545. liftBlock.setState(STATE_BLOCK_WAIT_FOR_ADVANCE);
  546. }
  547. else
  548. {
  549. if ((beforeLaunchBlock.getState() == STATE_BLOCK_FREE) && (mode == AUTO_MODE))
  550. {
  551. beforeLaunchBlock.setState(STATE_BLOCK_APPROACHING);
  552. liftBlock.setState(STATE_BLOCK_LEAVING);
  553. }
  554. else
  555. {
  556. liftBlock.getSection().setLiftOff();
  557. }
  558. }
  559. break;
  560. case STATE_BLOCK_WAIT_FOR_ADVANCE:
  561. if (mode == AUTO_MODE)
  562. {
  563. liftBlock.setState(STATE_BLOCK_BEHIND_TRIGGER);
  564. }
  565. else
  566. {
  567. liftBlock.getSection().setLiftOff();
  568. }
  569. break;
  570. case STATE_BLOCK_LEAVING:
  571. liftBlock.getSection().setLiftFwdOn();
  572. if (liftBlock.getNumberOfTrainsOnBlock() == 0)
  573. {
  574. liftBlock.setState(STATE_BLOCK_FREE);
  575. }
  576. break;
  577. case STATE_BLOCK_WAIT_FOR_CLEAR:
  578. if (liftBlock.getSection().isTrainBehindLiftTrigger())
  579. {
  580. liftBlock.setState(STATE_BLOCK_BEHIND_TRIGGER);
  581. }
  582. else
  583. {
  584. liftBlock.setState(STATE_BLOCK_BEFORE_TRIGGER);
  585. }
  586. break;
  587. }
  588. }
  589.  
  590.  
  591. /**
  592. * Process the block before the first launch section.
  593. */
  594. private void processBeforeLaunchBlock()
  595. {
  596. switch (beforeLaunchBlock.getState())
  597. {
  598. case STATE_BLOCK_FREE:
  599. beforeLaunchBlock.getSection().setTransportsOff();
  600. beforeLaunchBlock.getSection().setBrakesOn();
  601. break;
  602. case STATE_BLOCK_APPROACHING:
  603. if (beforeLaunchBlock.getSection().isTrainOnSection())
  604. {
  605. beforeLaunchBlock.setState(STATE_BLOCK_BEFORE_TRIGGER);
  606. }
  607. else
  608. {
  609. if (prepareReverse1BlockEntering())
  610. {
  611. beforeLaunchBlock.getSection().setBrakesOff();
  612. }
  613. else
  614. {
  615. beforeLaunchBlock.getSection().setBrakesOn();
  616. }
  617. beforeLaunchBlock.getSection().setTransportsOff();
  618. }
  619. break;
  620. case STATE_BLOCK_BEFORE_TRIGGER:
  621. prepareReverse1BlockEntering();
  622. beforeLaunchBlock.getSection().setBrakesTrim();
  623. beforeLaunchBlock.getSection().setTransportsStandardFwdDependingOnBrake();
  624. if (beforeLaunchBlock.getSection().isTrainBehindBrakeTrigger())
  625. {
  626. beforeLaunchBlock.setState(STATE_BLOCK_BEHIND_TRIGGER);
  627. }
  628. break;
  629. case STATE_BLOCK_BEHIND_TRIGGER:
  630. if (mode == MANUAL_BLOCK_MODE)
  631. {
  632. if (prepareReverse1BlockEntering())
  633. {
  634. beforeLaunchBlock.setState(STATE_BLOCK_WAIT_FOR_ADVANCE);
  635. }
  636. else
  637. {
  638. beforeLaunchBlock.getSection().setTransportsOff();
  639. beforeLaunchBlock.getSection().setBrakesOn();
  640. }
  641. }
  642. else
  643. {
  644. if (prepareReverse1BlockEntering() && (mode == AUTO_MODE))
  645. {
  646. reverse1Block.setState(STATE_BLOCK_APPROACHING);
  647. beforeLaunchBlock.setState(STATE_BLOCK_LEAVING);
  648. }
  649. else
  650. {
  651. beforeLaunchBlock.getSection().setTransportsOff();
  652. beforeLaunchBlock.getSection().setBrakesOn();
  653. }
  654. }
  655. break;
  656. case STATE_BLOCK_WAIT_FOR_ADVANCE:
  657. if (mode == AUTO_MODE)
  658. {
  659. beforeLaunchBlock.setState(STATE_BLOCK_BEHIND_TRIGGER);
  660. }
  661. else
  662. {
  663. if (!prepareReverse1BlockEntering())
  664. {
  665. beforeLaunchBlock.setState(STATE_BLOCK_BEHIND_TRIGGER);
  666. }
  667. else
  668. {
  669. beforeLaunchBlock.getSection().setTransportsOff();
  670. beforeLaunchBlock.getSection().setBrakesOn();
  671. }
  672. }
  673. break;
  674. case STATE_BLOCK_LEAVING:
  675. beforeLaunchBlock.getSection().setBrakesOff();
  676. beforeLaunchBlock.getSection().setTransportsLaunchFwdOn();
  677. if (beforeLaunchBlock.getNumberOfTrainsOnBlock() == 0)
  678. {
  679. beforeLaunchBlock.setState(STATE_BLOCK_FREE);
  680. }
  681. break;
  682. case STATE_BLOCK_WAIT_FOR_CLEAR:
  683. if (beforeLaunchBlock.getSection().isTrainBehindBrakeTrigger())
  684. {
  685. beforeLaunchBlock.setState(STATE_BLOCK_BEHIND_TRIGGER);
  686. }
  687. else
  688. {
  689. beforeLaunchBlock.setState(STATE_BLOCK_BEFORE_TRIGGER);
  690. }
  691. break;
  692. }
  693. }
  694.  
  695. /**
  696. * This method processes the Reverse1 block
  697. */
  698. private void processReverse1Block()
  699. {
  700. switch (reverse1Block.getState())
  701. {
  702. case STATE_BLOCK_APPROACHING:
  703. reverse1Block.getSection().setBrakesOff();
  704. reverse1Block.getSection().setTransportsOff();
  705. if (reverse1Block.getSection().isTrainOnSection())
  706. {
  707. reverse1Block.setState(STATE_BLOCK_BEFORE_TRIGGER);
  708. }
  709. break;
  710. case STATE_BLOCK_BEFORE_TRIGGER:
  711. reverse1Block.getSection().setBrakesTrim();
  712. reverse1Block.getSection().setTransportsStandardFwdDependingOnBrake();
  713. if (reverse1Block.getSection().isTrainBehindBrakeTrigger())
  714. {
  715. reverse1Block.setState(STATE_BLOCK_BEHIND_TRIGGER);
  716. }
  717. break;
  718. case STATE_BLOCK_BEHIND_TRIGGER:
  719. reverse1Block.getSection().setBrakesOn();
  720. reverse1Block.getSection().setTransportsOff();
  721. {
  722. Train train = reverse1Block.getSection().getTrainOnSection();
  723. if (train.getSpeed() == 0)
  724. {
  725. reverse1BlockTime = 0;
  726. reverse1Block.setState(STATE_BLOCK_WAITING);
  727. }
  728. }
  729. break;
  730. case STATE_BLOCK_WAITING:
  731. reverse1Block.getSection().setBrakesOn();
  732. reverse1Block.getSection().setTransportsOff();
  733. reverse1BlockTime += sim.getCurSimulationTickSec();
  734. prepareReverse1BlockLeaving();
  735. if (reverse1BlockTime >= reverse1Block.getSection().getBrakeWaitTime())
  736. {
  737. reverse1Block.setState(STATE_BLOCK_WAIT_FOR_CLEAR);
  738. }
  739. break;
  740. case STATE_BLOCK_WAIT_FOR_CLEAR:
  741. reverse1Block.getSection().setBrakesOn();
  742. reverse1Block.getSection().setTransportsOff();
  743. if (prepareReverse1BlockLeaving())
  744. {
  745. if (mode == MANUAL_BLOCK_MODE)
  746. {
  747. reverse1Block.setState(STATE_BLOCK_WAIT_FOR_ADVANCE);
  748. }
  749. else
  750. {
  751. reverse2Block.setState(STATE_BLOCK_APPROACHING);
  752. reverse1Block.setState(STATE_BLOCK_LEAVING);
  753. }
  754. }
  755. break;
  756. case STATE_BLOCK_WAIT_FOR_ADVANCE:
  757. reverse1Block.getSection().setBrakesOn();
  758. reverse1Block.getSection().setTransportsOff();
  759. if (!prepareReverse1BlockLeaving())
  760. {
  761. reverse1Block.setState(STATE_BLOCK_WAIT_FOR_CLEAR);
  762. }
  763. break;
  764. case STATE_BLOCK_LEAVING:
  765. if (reverse1Block.getSection().isTrainOnSection())
  766. {
  767. reverse1Block.getSection().setBrakesOff();
  768. reverse1Block.getSection().setTransportsLaunchBwdOn();
  769. }
  770. else
  771. {
  772. reverse1Block.getSection().setBrakesOn();
  773. reverse1Block.getSection().setTransportsOff();
  774. }
  775. if (reverse1Block.getNumberOfTrainsOnBlock() == 0)
  776. {
  777. reverse1Block.setState(STATE_BLOCK_FREE);
  778. }
  779. break;
  780. case STATE_BLOCK_FREE:
  781. reverse1Block.getSection().setBrakesOn();
  782. reverse1Block.getSection().setTransportsOff();
  783. break;
  784. }
  785. }
  786.  
  787. /**
  788. * This method processes the Reverse2 block
  789. */
  790. private void processReverse2Block()
  791. {
  792. switch (reverse2Block.getState())
  793. {
  794. case STATE_BLOCK_FREE:
  795. reverse2Block.getSection().setBrakesOn();
  796. reverse2Block.getSection().setTransportsOff();
  797. break;
  798. case STATE_BLOCK_APPROACHING:
  799. reverse2Block.getSection().setBrakesOff();
  800. reverse2Block.getSection().setTransportsOff();
  801. if (reverse2Block.getSection().isTrainOnSection())
  802. {
  803. reverse2Block.setState(STATE_BLOCK_BEFORE_TRIGGER);
  804. }
  805. break;
  806. case STATE_BLOCK_APPROACHING_B:
  807. reverse2Block.getSection().setBrakesOff();
  808. reverse2Block.getSection().setTransportsOff();
  809. if (reverse2Block.getSection().isTrainOnSection())
  810. {
  811. reverse2Block.setState(STATE_BLOCK_BEFORE_TRIGGER_B);
  812. }
  813. break;
  814. case STATE_BLOCK_BEFORE_TRIGGER:
  815. reverse2Block.getSection().setBrakesTrim();
  816. reverse2Block.getSection().setTransportsStandardBwdDependingOnBrake();
  817. if (reverse2Block.getSection().isTrainBeforeBrakeTrigger())
  818. {
  819. reverse2Block.setState(STATE_BLOCK_BEHIND_TRIGGER);
  820. }
  821. break;
  822. case STATE_BLOCK_BEFORE_TRIGGER_B:
  823. reverse2Block.getSection().setBrakesTrim();
  824. reverse2Block.getSection().setTransportsStandardFwdDependingOnBrake();
  825. if (reverse2Block.getSection().isTrainBehindBrakeTrigger())
  826. {
  827. reverse2Block.setState(STATE_BLOCK_BEHIND_TRIGGER);
  828. }
  829. break;
  830. case STATE_BLOCK_BEHIND_TRIGGER:
  831. reverse2Block.getSection().setBrakesOn();
  832. reverse2Block.getSection().setTransportsOff();
  833. {
  834. Train train = reverse2Block.getSection().getTrainOnSection();
  835. if (train.getSpeed() == 0)
  836. {
  837. reverse2Block.setState(STATE_BLOCK_WAIT_FOR_CLEAR);
  838. reverse2BlockTime = 0;
  839. }
  840. }
  841. break;
  842. case STATE_BLOCK_WAIT_FOR_CLEAR:
  843. // wait at least one second before we switch the track
  844. if (reverse2BlockTime >= 1.0)
  845. {
  846. if (mode == MANUAL_BLOCK_MODE)
  847. {
  848. if (prepareReverse2BlockLeaving() || canReverse2BlockLeaveBwd())
  849. {
  850. reverse2Block.setState(STATE_BLOCK_WAIT_FOR_ADVANCE);
  851. }
  852. }
  853. else
  854. {
  855. if (prepareReverse2BlockLeaving())
  856. {
  857. beforeStationBlock.setState(STATE_BLOCK_APPROACHING);
  858. reverse2Block.setState(STATE_BLOCK_LEAVING);
  859. }
  860. }
  861. }
  862. else
  863. {
  864. reverse2BlockTime += sim.getCurSimulationTickSec();
  865. }
  866. reverse2Block.getSection().setBrakesOn();
  867. reverse2Block.getSection().setTransportsOff();
  868. break;
  869. case STATE_BLOCK_WAIT_FOR_ADVANCE:
  870. if ((mode != MANUAL_BLOCK_MODE) || (!prepareReverse2BlockLeaving() && !canReverse2BlockLeaveBwd()))
  871. {
  872. reverse2Block.setState(STATE_BLOCK_WAIT_FOR_CLEAR);
  873. }
  874. break;
  875. case STATE_BLOCK_LEAVING:
  876. reverse2Block.getSection().setBrakesOff();
  877. reverse2Block.getSection().setTransportsStandardFwdOn();
  878. if (reverse2Block.getNumberOfTrainsOnBlock() == 0)
  879. {
  880. reverse2Block.setState(STATE_BLOCK_FREE);
  881. }
  882. break;
  883. case STATE_BLOCK_LEAVING_B:
  884. reverse2Block.getSection().setBrakesOff();
  885. reverse2Block.getSection().setTransportsStandardBwdOn();
  886. if (reverse2Block.getNumberOfTrainsOnBlock() == 0)
  887. {
  888. reverse2Block.setState(STATE_BLOCK_FREE);
  889. }
  890. break;
  891. }
  892. }
  893.  
  894. /**
  895. * This method processes the block before the stations
  896. */
  897. private void processBeforeStationBlock()
  898. {
  899. switch (beforeStationBlock.getState())
  900. {
  901. case STATE_BLOCK_FREE:
  902. beforeStationBlock.getSection().setBrakesOn();
  903. beforeStationBlock.getSection().setTransportsOff();
  904. break;
  905. case STATE_BLOCK_APPROACHING:
  906. beforeStationBlock.getSection().setBrakesOff();
  907. beforeStationBlock.getSection().setTransportsOff();
  908. if (beforeStationBlock.getSection().isTrainOnSection())
  909. {
  910. beforeStationBlock.setState(STATE_BLOCK_BEFORE_TRIGGER);
  911. }
  912. break;
  913. case STATE_BLOCK_BEFORE_TRIGGER:
  914. beforeStationBlock.getSection().setBrakesTrim();
  915. beforeStationBlock.getSection().setTransportsStandardFwdDependingOnBrake();
  916. if (beforeStationBlock.getSection().isTrainBehindBrakeTrigger())
  917. {
  918. beforeStationBlock.setState(STATE_BLOCK_BEHIND_TRIGGER);
  919. }
  920. break;
  921. case STATE_BLOCK_BEHIND_TRIGGER:
  922. if (stationExitBlock.getState() == STATE_BLOCK_FREE)
  923. {
  924. if (mode == MANUAL_BLOCK_MODE)
  925. {
  926. beforeStationBlock.setState(STATE_BLOCK_WAIT_FOR_ADVANCE);
  927. }
  928. else
  929. {
  930. stationExitBlock.setState(STATE_BLOCK_APPROACHING);
  931. beforeStationBlock.setState(STATE_BLOCK_LEAVING);
  932. }
  933. }
  934. else
  935. {
  936. beforeStationBlock.getSection().setBrakesOn();
  937. beforeStationBlock.getSection().setTransportsOff();
  938. {
  939. Train train = beforeStationBlock.getSection().getTrainOnSection();
  940. if (train.getSpeed() == 0)
  941. {
  942. beforeStationBlock.setState(STATE_BLOCK_WAIT_FOR_CLEAR);
  943. }
  944. }
  945. }
  946. break;
  947. case STATE_BLOCK_WAIT_FOR_ADVANCE:
  948. if ((mode != MANUAL_BLOCK_MODE) || (stationExitBlock.getState() != STATE_BLOCK_FREE))
  949. {
  950. beforeStationBlock.setState(STATE_BLOCK_WAIT_FOR_CLEAR);
  951. }
  952. beforeStationBlock.getSection().setBrakesOn();
  953. beforeStationBlock.getSection().setTransportsOff();
  954. break;
  955. case STATE_BLOCK_WAIT_FOR_CLEAR:
  956. if (stationExitBlock.getState() == STATE_BLOCK_FREE)
  957. {
  958. if (mode == MANUAL_BLOCK_MODE)
  959. {
  960. beforeStationBlock.setState(STATE_BLOCK_WAIT_FOR_ADVANCE);
  961. }
  962. else
  963. {
  964. stationExitBlock.setState(STATE_BLOCK_APPROACHING);
  965. beforeStationBlock.setState(STATE_BLOCK_LEAVING);
  966. }
  967. }
  968. else
  969. {
  970. beforeStationBlock.getSection().setBrakesOn();
  971. beforeStationBlock.getSection().setTransportsOff();
  972. }
  973. break;
  974. case STATE_BLOCK_LEAVING:
  975. beforeStationBlock.getSection().setBrakesOff();
  976. beforeStationBlock.getSection().setTransportsStandardFwdOn();
  977. if (beforeStationBlock.getNumberOfTrainsOnBlock() == 0)
  978. {
  979. beforeStationBlock.setState(STATE_BLOCK_FREE);
  980. }
  981. break;
  982. }
  983. }
  984.  
  985. /**
  986. * This method processes the Storage1 block
  987. */
  988. private void processStorage1Block()
  989. {
  990. switch (storage1Block.getState())
  991. {
  992. case STATE_BLOCK_FREE:
  993. storage1Block.getSection().setTransportsOff();
  994. break;
  995. case STATE_BLOCK_APPROACHING:
  996. storage1Block.getSection().setTransportsOff();
  997. if (storage1Block.getSection().isTrainOnSection())
  998. {
  999. storage1Block.setState(STATE_BLOCK_BEFORE_TRIGGER);
  1000. }
  1001. break;
  1002. case STATE_BLOCK_APPROACHING_B:
  1003. storage1Block.getSection().setTransportsOff();
  1004. if (storage1Block.getSection().isTrainOnSection())
  1005. {
  1006. storage1Block.setState(STATE_BLOCK_BEFORE_TRIGGER_B);
  1007. }
  1008. break;
  1009. case STATE_BLOCK_BEFORE_TRIGGER:
  1010. storage1Block.getSection().setTransportsStandardFwdOn();
  1011. if (storage1Block.getSection().isTrainBehindCenterOfSection())
  1012. {
  1013. storage1Block.setState(STATE_BLOCK_WAIT_FOR_CLEAR);
  1014. }
  1015. break;
  1016. case STATE_BLOCK_BEFORE_TRIGGER_B:
  1017. storage1Block.getSection().setTransportsStandardBwdOn();
  1018. if (storage1Block.getSection().isTrainBeforeCenterOfSection())
  1019. {
  1020. storage1Block.getSection().getTrainOnSection().setLashedToTrack(true);
  1021. storage1Block.setState(STATE_BLOCK_WAIT_FOR_CLEAR);
  1022. }
  1023. break;
  1024. case STATE_BLOCK_WAIT_FOR_CLEAR:
  1025. storage1Block.getSection().setTransportsOff();
  1026. if (mode == MANUAL_BLOCK_MODE)
  1027. {
  1028. if ((storage2Block.getState() == STATE_BLOCK_FREE) || canEnterReverse2BlockFromStorage())
  1029. {
  1030. storage1Block.setState(STATE_BLOCK_WAIT_FOR_ADVANCE);
  1031. }
  1032. }
  1033. break;
  1034. case STATE_BLOCK_WAIT_FOR_ADVANCE:
  1035. storage1Block.getSection().setTransportsOff();
  1036. if ((mode != MANUAL_BLOCK_MODE) || ((storage2Block.getState() != STATE_BLOCK_FREE) && !canEnterReverse2BlockFromStorage()))
  1037. {
  1038. storage1Block.setState(STATE_BLOCK_WAIT_FOR_CLEAR);
  1039. }
  1040. break;
  1041. case STATE_BLOCK_LEAVING:
  1042. storage1Block.getSection().setTransportsStandardFwdOn();
  1043. if (!storage1Block.getSection().isTrainOnSection())
  1044. {
  1045. storage1Block.setState(STATE_BLOCK_FREE);
  1046. }
  1047. break;
  1048. case STATE_BLOCK_LEAVING_B:
  1049. storage1Block.getSection().setTransportsStandardBwdOn();
  1050. if (!storage1Block.getSection().isTrainOnSection())
  1051. {
  1052. storage1Block.setState(STATE_BLOCK_FREE);
  1053. }
  1054. break;
  1055. }
  1056. }
  1057.  
  1058. /**
  1059. * This method processes the Storage2 block
  1060. */
  1061. private void processStorage2Block()
  1062. {
  1063. switch (storage2Block.getState())
  1064. {
  1065. case STATE_BLOCK_FREE:
  1066. storage2Block.getSection().setTransportsOff();
  1067. break;
  1068. case STATE_BLOCK_APPROACHING_B:
  1069. storage2Block.getSection().setTransportsOff();
  1070. if (storage2Block.getSection().isTrainOnSection())
  1071. {
  1072. storage2Block.setState(STATE_BLOCK_BEFORE_TRIGGER_B);
  1073. }
  1074. break;
  1075. case STATE_BLOCK_BEFORE_TRIGGER_B:
  1076. storage2Block.getSection().setTransportsStandardBwdOn();
  1077. if (storage2Block.getSection().isTrainBeforeCenterOfSection())
  1078. {
  1079. storage2Block.getSection().getTrainOnSection().setLashedToTrack(true);
  1080. storage2Block.setState(STATE_BLOCK_WAIT_FOR_CLEAR);
  1081. }
  1082. break;
  1083. case STATE_BLOCK_WAIT_FOR_CLEAR:
  1084. storage2Block.getSection().setTransportsOff();
  1085. if (mode == MANUAL_BLOCK_MODE)
  1086. {
  1087. if (storage1Block.getState() == STATE_BLOCK_FREE)
  1088. {
  1089. storage2Block.setState(STATE_BLOCK_WAIT_FOR_ADVANCE);
  1090. }
  1091. }
  1092. break;
  1093. case STATE_BLOCK_WAIT_FOR_ADVANCE:
  1094. storage2Block.getSection().setTransportsOff();
  1095. if ((mode != MANUAL_BLOCK_MODE) || (storage1Block.getState() != STATE_BLOCK_FREE))
  1096. {
  1097. storage2Block.setState(STATE_BLOCK_WAIT_FOR_CLEAR);
  1098. }
  1099. break;
  1100. case STATE_BLOCK_LEAVING:
  1101. storage2Block.getSection().setTransportsStandardFwdOn();
  1102. if (!storage2Block.getSection().isTrainOnSection())
  1103. {
  1104. storage2Block.setState(STATE_BLOCK_FREE);
  1105. }
  1106. break;
  1107. }
  1108. }
  1109.  
  1110. /**
  1111. * This method checks if the Reverse1 block can be entered and tries to set the corresponding track switch in auto-mode.
  1112. */
  1113. private bool prepareReverse1BlockEntering()
  1114. {
  1115. if (reverse1Block.getState() == STATE_BLOCK_FREE)
  1116. {
  1117. if (reverse1Switch.getSwitchDirection() == 0)
  1118. {
  1119. return true;
  1120. }
  1121. else
  1122. {
  1123. if (mode == AUTO_MODE)
  1124. {
  1125. reverse1Switch.setSwitchDirection(0);
  1126. }
  1127. }
  1128. }
  1129. return false;
  1130. }
  1131.  
  1132. /**
  1133. * This method checks if a train on the Reverse1 block can leave the block and enter the next block.
  1134. * It tries to set the corresponding track switches in auto-mode.
  1135. */
  1136. private bool prepareReverse1BlockLeaving()
  1137. {
  1138. bool b1 = false;
  1139. bool b2 = false;
  1140. if (reverse1Switch.getSwitchDirection() == 1)
  1141. {
  1142. b1 = true;
  1143. }
  1144. else
  1145. {
  1146. if (mode == AUTO_MODE)
  1147. {
  1148. reverse1Switch.setSwitchDirection(1);
  1149. }
  1150. }
  1151. if (reverse2Block.getState() == STATE_BLOCK_FREE)
  1152. {
  1153. if (reverse2Switch.getSwitchDirection() == 0)
  1154. {
  1155. b2 = true;
  1156. }
  1157. else
  1158. {
  1159. if (mode == AUTO_MODE)
  1160. {
  1161. reverse2Switch.setSwitchDirection(0);
  1162. }
  1163. }
  1164. }
  1165. return b1 && b2;
  1166. }
  1167.  
  1168. /**
  1169. * This method checks if a train on the Reverse2 block can leave the block and enter the next block.
  1170. * It tries to set the corresponding track switch in auto-mode.
  1171. */
  1172. private bool prepareReverse2BlockLeaving()
  1173. {
  1174. if (reverse2Switch.getSwitchDirection() == 1)
  1175. {
  1176. if (beforeStationBlock.getState() == STATE_BLOCK_FREE)
  1177. {
  1178. return true;
  1179. }
  1180. }
  1181. else
  1182. {
  1183. if (mode == AUTO_MODE)
  1184. {
  1185. reverse2Switch.setSwitchDirection(1);
  1186. }
  1187. }
  1188. return false;
  1189. }
  1190.  
  1191. /**
  1192. * This method checks if a train on the Reverse2 block can leave to the storage tracks.
  1193. */
  1194. private bool canReverse2BlockLeaveBwd()
  1195. {
  1196. return (reverse2Switch.getSwitchDirection() == 2) && (storage1Block.getState() == STATE_BLOCK_FREE);
  1197. }
  1198.  
  1199. /**
  1200. * This method checks if a train on the storage track can enter the Reverse2 block
  1201. */
  1202. private bool canEnterReverse2BlockFromStorage()
  1203. {
  1204. return (reverse2Switch.getSwitchDirection() == 2) && (reverse2Block.getState() == STATE_BLOCK_FREE);
  1205. }
  1206.  
  1207. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement