Jump to content
  • Sky
  • Blueberry
  • Slate
  • Blackcurrant
  • Watermelon
  • Strawberry
  • Orange
  • Banana
  • Apple
  • Emerald
  • Chocolate
  • Charcoal
Harkole

Using the Adapter to Read RailCraft Tanks

Recommended Posts

Hi All,

 

Hopefully this is in the right place and going to be helpful to someone! I've come from an older version of Computer Craft and Minecraft to 1.7.10 and OC and I've been brushing off the rust and trying to get things going. Having done the tutorials and messed around I thought I'd try doing a simple(!) tank reading program so I can check the fuel levels of my generator.

 

Turns out that either my Google-foo is weak or that not many people have tried/done it. So below is how I did it with some explanations of techniques I used to work things out (so hopefully you can tell me a better way of doing it or I can save some blood/sweat/tears of someone following).

 

First things first, there is a good tutorial on here and the Wiki about the computer so I'm skipping straight to the "connection of the tank" and code.

 

The Adapter block must be connected to a valve, it also needs to have the Tank Controller Upgrade installed for the example to work.

 

I did notice that you can actually see a RailCraft tank with no upgrade but it threw errors if I tried to access any of it's methods, if I work out where the log files that it said to check are I can forward them as bug reports...

 

So, with the Adapter in place and attached by either cable or directly to the computer and OpenOS installed/loaded, we can use the following code in the LUA shell (type lua and press enter if you are at the start up prompt). Note the = proceeding the command is needed to see the results:

-- list all components, with the unique address of said component (for specific attachment)
-- and the name you can use as a component name (this may not be unique and the system will use the first it finds)
=component.list()

This, as the comments say will list all attached components of the computer, if there are a lot you may see "..." at the bottom, in which case use the Analyzer to get the unique address for the part in question (you can use the address or name, it's just easier to use a name for readability)

 

Our Adapter with the Tank Upgrade becomes known as tank_controller, with this information we can call:

-- List all methods/functions of the component
-- component.<component name> 
=component.tank_controller

This will tell us what we can do with the tank_controller (or other component name), with this information we can actually use LUA to see what this information would be by calling it and also tell what format the data is coming back in...

-- Example Connecting to a single tank on the south of the Adapter with a fluid upgrade in it
-- this stores the result in a table called tInfo (south side connection)
=component.tank_controller.getFluidInTank(3)

-- would return a table with the key values:
--		amount		->	current volume of fluid
--		capacity	->	total storage available for fuilds in this tank
--		hasTag		->	?
--		label		->	In game name value of fluid
--		name		->	??? Possibly the forge fluid name ???

We can tell it's a table because of the {{}} symbols around the bulk of the data and that it's data pairs (someone with more LUA skill will explain this better I'm sure).

 

So with all this science out of the way we can now create a simple LUA script to display just the information we want in a tidy way, below is the entire code commented to explain what we are doing. There are some key differences between using the LUA shell and a LUA script regarding talking to the component, but it's still quite basic:

-- ********************************************************
-- Example of simple tank reader (using Adapter with Tank Controller Upgrade)
-- ********************************************************

-- Hook the component
component = require("component")
tank = component.tank_controller

-- Get the tank information, the side is the Forge value for sides (int), results are in table format
tInfo = tank.getFluidInTank(1) -- Tank is above the Adapter

-- Access the key values of the table (LUA is base 1 not 0 for first index values)
-- We don't actually have to do this, but it helps if the code gets big to be clear, we could of actually
-- just used the values straight in the print command.
amount = tInfo[1].amount
capacity = tInfo[1].capacity
label = tInfo[1].label

-- get the tanks fill value as a percentage
-- this is another reason to assign things to variables, as this alone returns a value of up to 5 decimals places...
percent = (tInfo[1].amount / tInfo[1].capacity) * 100

-- ... but as we can pass the variable about to do things we can use other methods/functions to clean it up
-- while still keeping the code readable
-- format the percentage to a nice number
math.floor(percent + 0.5)

-- Output as you want... a very simple way:
print("Fuel Type: "..label)
print("Amount   : "..amount)
print("Tank Cap.: "..capacity)
print("% Full   : "..percent)

Whilst it's not perfect, it'll hopefully serve as a bridging point in to the world of components not native to OC. There are other things we could of done like make a function of the check and passed it to a function for displaying then looped through the code so it updates the screen constantly but that's all for you guys to think about, this was just a brief example of where to start!

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use and Privacy Policy.