Removed old train scripts, added cobble generator script.

This commit is contained in:
Andrew Lalis 2018-06-24 09:16:23 +02:00
parent d2f5211cce
commit 419281e2e9
4 changed files with 85 additions and 165 deletions

View File

@ -0,0 +1,12 @@
# TreeFarm.lua
Robot script for automatic chopping of trees, given a lumber axe, bonemeal, and saplings.
## Pastebin
[mRTULKY0](https://pastebin.com/mRTULKY0)
## Module Requirements
* tractor_beam
* inventory_controller
## Instructions
To operate this program, simply execute it, and it will prompt the user to decide if they wish to choose a number of trees to chop, or `-1` for chopping until out of resources. The robot will stop if its axe has less than 10% durability, it runs out of bonemeal, or runs out of saplings.

View File

@ -0,0 +1,73 @@
--[[
Author: Andrew Lalis
File: cobble_generator.lua
Version: 1.0
Last Modified: 24-06-2018
Description:
This script makes a robot operate a square cobble generator, in the setup below
0CO
CRC
OC0
Where 0 = lava, O = water, C = cobble, and R = robot.
The items gathered will be dropped below the robot, into a hopper or chest.
--]]
--Require statements and componenent definitions.
local robot = require("robot")
--Global variables:
local cyclesPerformed = 0
--[[
Mines one block of cobble, checking for a proper tool first.
--]]
local function mineCobble()
local success, msg = robot.durability()
while not success do
print("No valid tool. Please place one in the tool slot and press enter.")
io.read()
end
robot.swing()
end
--[[
Performs one cycle of cobblestone harvesting. Will check if it has a tool.
--]]
local function cycle()
for i=1,4 do
mineCobble()
robot.turnRight()
end
robot.dropDown()
print("Cycles done: "..cyclesPerformed)
cyclesPerformed = cyclesPerformed + 1
end
--[[
Main function in which the iterations of the cycle are performed.
--]]
local function main()
print("# Andrew's Cobblestone Generator #")
print("# Copyright 2018 Andrew Lalis #")
print("#--------------------------------#")
print("Please enter the number of cycles to perform, or -1 to continue forever.")
local choice = tonumber(io.read())
if (choice == nil or choice == -1) then
print("Beginning infinite cycles.")
while true do
cycle()
end
else
print("Beginning "..choice.." cycles.")
for i=1,choice do
cycle()
end
end
print("#------Program completed.--------#")
end
main()

View File

@ -1,81 +0,0 @@
--[[
Author: Andrew Lalis
File: train_speed_checkpoint.lua
Version: 1.0
Last Modified: 16-06-2018
Script used for helping to control the speed of a train. Use many
computers running this script to calibrate trains to run at a certain speed.
--]]
--Require statements and component definitions.
local component = require("component")
local event = require("event")
local computer = require("computer")
local term = require("term")
local detector = component.ir_augment_detector
local controller = component.ir_augment_control
-- Optimal Speed.
local DESIRED_SPEED = 20
--[[
Shortcut for pulling only train events.
func - function(net_address, augment_type, stock_uuid): function to handle the
train event, with augment type being either DETECTOR or LOCO_CONTROL.
--]]
local function pullTrainEvent(func)
local event_name, net_address, augment_type, stock_uuid = event.pull("ir_train_overhead")
func(net_address, augment_type, stock_uuid)
end
--[[
Determines if a string starts with a given substring.
return - boolean: true if str starts with start, false otherwise.
--]]
local function strStarts(str, start)
return string.sub(str, 1, string.len(start)) == start
end
--[[
Determines if a stock car over the detector is a locomotive by reading info.id
from the detector's info() method. If it begins with rolling_stock/loc, then
it is a locomotive.
--]]
local function isStockLocomotive(id)
return strStarts(id, "rolling_stock/loc")
end
local function balanceSpeed(info, target_speed)
local diff = target_speed - info.speed
local percent_diff = info.speed / target_speed
if diff > 0 then -- We are too slow.
controller.setBrake(0)
controller.setThrottle(info.throttle + percent_diff)
else -- We are too fast.
controller.setThrottle(0)
controller.setBrake(percent_diff)
end
end
local function handleTrainEvent(net_address, augment_type, stock_uuid)
local info = detector.info()
local data = detector.consist()
if not (data and info and isStockLocomotive(info.id)) then
return
end
if (augment_type == "DETECTOR") then
term.clear()
term.setCursor(1, 1)
term.write(" Speed: "..data.speed_km.." Km/h")
term.write(" Dir: "..data.direction)
term.write(" Weight: "..data.weight_kg.." Kg")
end
if (augment_type == "LOCO_CONTROL") then
balanceSpeed(info, DESIRED_SPEED)
end
end
while true do
pullTrainEvent(handleTrainEvent)
end

View File

@ -1,84 +0,0 @@
--[[
Author: Andrew Lalis
File: train_speed_checkpoint.lua
Version: 1.0
Last Modified: 16-06-2018
Script used for helping to control the speed of a train. Use many
computers running this script to calibrate trains to run at a certain speed.
--]]
--Require statements and component definitions.
local component = require("component")
local event = require("event")
local computer = require("computer")
local detector = component.ir_augment_detector
local controller = component.ir_augment_control
local BRAKE_ENABLED = false
--[[
Shortcut for pulling only train events.
func - function(net_address, augment_type, stock_uuid): function to handle the
train event, with augment type being either DETECTOR or LOCO_CONTROL.
--]]
local function pullTrainEvent(func)
local event_name, net_address, augment_type, stock_uuid = event.pull("ir_train_overhead")
func(net_address, augment_type, stock_uuid)
end
--[[
Determines if a string starts with a given substring.
return - boolean: true if str starts with start, false otherwise.
--]]
local function strStarts(str, start)
return string.sub(str, 1, string.len(start)) == start
end
--[[
Determines if a stock car over the detector is a locomotive by reading info.id
from the detector's info() method. If it begins with rolling_stock/loc, then
it is a locomotive.
--]]
local function isStockLocomotive()
local info = detector.info()
return strStarts(info.id, "rolling_stock/loc")
end
--[[
Main function to perform test. Currently configured to determine the distance
needed to stop a train. It does this by waiting for a train to go over the
detector, and then applies full brakes, idle throttle, and the train will slow
to a halt. It also records the current time at which the locomotive was
detected, and some other information that is of use.
net_address - string: the network address of the adapter for the augment.
augment_type - string: either DETECTOR or LOCO_CONTROL.
stock_uuid - string: the unique id of the stock detected.
--]]
local function handleTrainEvent(net_address, augment_type, stock_uuid)
if (augment_type == "DETECTOR") then
local data = detector.consist()
-- Only if there's data, and this is a locomotive, do we continue.
if (data == nil or not isStockLocomotive()) then
return
end
print("Locomotive Data from: "..net_address)
print(" Speed: "..data.speed_km.." Km/h")
print(" Direction: "..data.direction)
print(" Weight: "..data.weight_kg.." Kg")
print(" Tractive Effort: "..data.tractive_effort_N.." N")
print(" Cars: "..data.cars)
print(" TIME: "..os.time())
end
-- In the case that the train is over the control augment, apply changes to locomotive control.
-- Also sound horn.
if (augment_type == "LOCO_CONTROL" and BRAKE_ENABLED) then
controller.setThrottle(0)
controller.setBrake(1)
controller.horn()
end
end
while true do
pullTrainEvent(handleTrainEvent)
end