Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local event = require("event")
- local component = require("component")
- local DetectorAugments = component.list("ir_augment_detector")
- local ControllerAugments = component.list("ir_augment_control")
- local sides = require("sides")
- -- Settings that need changing to work --
- local Control = M -- 1 = wait for redstone signal, 2 = wait for seconds only, 3 = redstone signal or Wait for seconds
- local StationX = X -- X Coordinate - Station detector block X coordinate
- local StationZ = Z -- Z Coordinate - Station detector block Z coordinate
- local Direction = "south" -- direction ["south", "north"] - Direction of main travel
- -- Settings --
- local Side = sides.top -- Side [sides.top, sides.north, etc] - Side of redstone control block
- local InitialSetSpeed = 10 -- speed [0 to +100] - Initial Speed set
- local Deadzone = 2 -- speed [0 to +100] - does not control loco is within this Deadzone speed of the set speed
- local Threshold = 20 -- speed [0 to +100] - Above this speed difference, SetChange = 1.0
- local OtherDirectionThrottle = 0-- Throttle [-1 to 1] - Currently does nothing to loco
- local MinSpeed = 2 -- speed [0 to +100] - Minimum Station speed
- local MinThrottle = 0.05 -- Throttle [-1 to 1] default 0.005 - Min throttle speed when accelerating
- local Wait = 20 -- seconds [0+] - seconds held at station
- local ExitThrottle = 0.4 -- Throttle [-1 to 1] - throttle set to exit the station
- local ExtraInfo = 1 -- [1 = Yes, 0 = No] - Yes or no to extra information being displayed
- local TweakThrottle = 0.9 -- [0 to 1] - Tweaks the throttle applied on acceleration, 1 = no change
- -- Do not alter --
- local MaxX = 0
- local MaxZ = 0
- -- Code ----------------------------------
- print("\nAutomated station script, Slows loco down from 'WantedSpeed' down to the 'MinSpeed' and stop at a specified augment detector. \nEdit the script settings to permanently setup the script \n")
- -- If Station block not set above, prompt user for X and Z coordinates and Control mode
- if StationX == X then
- print("Type X coordinate of the Detector Augment used for the Station:")
- StationX = io.read()
- print("StationX position set to:", StationX, "\n")
- end
- if StationZ == Z then
- print("Type Z coordinate of the Detector Augment used for the Station:")
- StationZ = io.read()
- print("StationZ position set to:", StationZ, "\n")
- end
- if Control == M then
- print("Select control mode: 1 = wait for redstone signal, 2 = (default) wait for seconds only, 3 = redstone signal or Wait for seconds")
- Control = io.read()
- print("Mode selected:", Control)
- end
- print("\nStation is operational \n")
- -- Listen for the Train Overhead event
- event.listen("ir_train_overhead", function(name, address, augment_type, uuid)
- if name == "ir_train_overhead" then
- if augment_type == "DETECTOR" then
- -- Get the detector
- local Detector = component.proxy(address)
- local DetectorInfo = Detector.info()
- -- Check that it's a loco
- if not string.find(DetectorInfo.id, "loco") then
- return
- end
- local x, y, z = Detector.getPos()
- -- Calculate difference in x to station
- DiffX = StationX - x
- if DiffX < 0 then
- DiffX = DiffX * -1
- end
- if DiffX > MaxX then
- MaxX = DiffX
- end
- if MaxX ~= 0 then
- AdjustX = DiffX / MaxX
- else
- AdjustX = 0
- end
- -- Calculate difference in z to station
- DiffZ = StationZ - z
- if DiffZ < 0 then
- DiffZ = DiffZ * -1
- end
- if DiffZ > MaxZ then
- MaxZ = DiffZ
- end
- if MaxZ ~= 0 then
- AdjustZ = DiffZ / MaxZ
- else
- AdjustZ = 0
- end
- -- Calculate Adjustment to the speed based off Distance from station
- if (AdjustX ~= 0) then
- Adjustment = AdjustX
- end
- if (AdjustZ ~= 0) then
- Adjustment = AdjustZ
- end
- if (AdjustX ~= 0 and AdjustZ ~= 0) then
- Adjustment = AdjustX * AdjustZ
- end
- if (AdjustX == 0) and (AdjustZ == 0) then
- Adjustment = 0
- end
- if Adjustment > 1 then
- Adjustment = 1
- end
- -- Adjust the WantedSpeed by the percentage distance travelled by the loco (accounting for MinSpeed)
- NewWantedSpeed = ((InitialSetSpeed - MinSpeed) * Adjustment) + MinSpeed
- if ExtraInfo == 1 then
- print("Coords:", x, y, z, "NewSpeed: ", NewWantedSpeed)
- end
- -- Brake/accelerate proportional to the speed difference (smooths change in speed)
- if DetectorInfo then
- Difference = DetectorInfo.speed - NewWantedSpeed
- if Difference < 0 then
- Difference = Difference * -1
- end
- SetChange = Difference / Threshold
- if Difference > Threshold then
- SetChange = 1.0
- end
- if ExtraInfo == 1 then
- print("Diff:", Difference, " Speed:", DetectorInfo.speed, " SetChange:", SetChange)
- end
- end
- -- If at station block, halt loco for set Wait then depart loco at ExitThrottle
- if Adjustment == 0 then
- if ExtraInfo == 1 then
- print("At station")
- end
- -- Loop over all the controllers to update them
- for ControllerUUID, ControllerName in pairs(ControllerAugments) do
- local Controller = component.proxy(ControllerUUID)
- Controller.setThrottle(0.0)
- Controller.setBrake(1.0)
- end
- -- Depending upon Control - use redstone or wait
- if tonumber(Control) == 1 then
- if ExtraInfo == 1 then
- print("Control 1")
- end
- local rs = component.redstone
- while rs.getInput(Side) == 0 do
- os.sleep(0.5)
- end
- else if tonumber(Control) == 2 then
- if ExtraInfo == 1 then
- print("Control 2")
- end
- os.sleep(Wait)
- else if tonumber(Control) == 3 then
- if ExtraInfo == 1 then
- print("Control 3")
- end
- local rs = component.redstone
- local time = 0
- while time < Wait do
- time = time + 1
- if rs.getInput(Side) ~= 0 then
- time = Wait
- end
- os.sleep(1)
- end
- end
- end
- end
- if ExtraInfo == 1 then
- print("Departing station")
- end
- for ControllerUUID, ControllerName in pairs(ControllerAugments) do
- local Controller = component.proxy(ControllerUUID)
- Controller.setBrake(0.0)
- Controller.setThrottle(ExitThrottle)
- end
- os.sleep(20)
- end
- -- Loop over all the controllers to update them
- for ControllerUUID, ControllerName in pairs(ControllerAugments) do
- local Controller = component.proxy(ControllerUUID)
- -- If travelling in not the main Direction then do nothing
- if not string.find(DetectorInfo.direction, Direction) then
- -- Do nothing
- -- Controller.setThrottle(OtherDirectionThrottle)
- -- Controller.setBrake(0.0)
- if ExtraInfo == Y then
- print("OtherDirection")
- end
- else
- -- If we're within a certain range of NewWantedSpeed then don't do anything
- if DetectorInfo and math.abs(DetectorInfo.speed - NewWantedSpeed) > Deadzone then
- -- If we're too far above or below WantedSpeed then speed up or slow down
- if DetectorInfo.speed < NewWantedSpeed then
- -- Increase loco speed
- -- MinThrottle + SetChange
- SetChange = SetChange + MinThrottle
- if SetChange > 1 then
- SetChange = 1
- end
- SetChange = SetChange * TweakThrottle
- Controller.setThrottle(SetChange)
- Controller.setBrake(0.0)
- else
- -- Decrease loco speed
- Controller.setThrottle(0.040)
- Controller.setBrake(SetChange)
- end
- else
- -- Speed within Deadzone
- Controller.setThrottle(0.066)
- Controller.setBrake(0.0)
- end
- end
- end
- end
- end
- end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement