This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Crwnz4's profile picture

Applied Energistics Inventory precentage help

Started by Crwnz4, 02 November 2013 - 08:04 PM
Crwnz4 #1
Posted 02 November 2013 - 09:04 PM
Alright, what I'm trying to do is have a 4x3 monitor setup that will read:

Free Space:

100%


So far, I have this:

while true do
local.mEController = peripheral.wrap("right")
local intInfo = mEController.getFreeBytes()
print(intInfo)
local intInfo = mEController.getTotalBytes()
print(intInfo)
sleep(30)
end

Simple, eh? That's about as far as I can get with it. I can get the numbers or free and total space, but I'm at a loss on how to code for the percentages. I know how to wrap monitors and such, but getting this to read math wise has me stumped…
Lyqyd #2
Posted 02 November 2013 - 09:45 PM
Split into new topic.

The math looks like this:

free / total = x / 100

So, you simply multiply both sides by 100 to find x:

local percentage = free * 100 / total

Check the computercraft wiki page on monitors to see how to use them. It may be easier to simply use term.redirect and use print than to manually make calls to the monitor, of course.
Crwnz4 #3
Posted 02 November 2013 - 10:12 PM
Alright, then for the code I have something like this: (Still just using print for now)

while true do
local mEController = peripheral.wrap("right")
LoPerc = mEController.getFreeBytes()*100/mEController.getTotalBytes
print("LoPerc")
sleep930)
end


Yes, it is printing "LoPerc" instead of the percentage, but once again I'm not sure how to print the percentage. I' only use to DOS really and what I'm thinking in DOS is:

set /a Perc="Var1"*"Var2"
echo %Perc%

But in Lua…..?


Scratch all that, I wasn't thinking. I have it displaying the variable the right way now. However, is there a way to round up in lua? >.>
CCJJSax #4
Posted 02 November 2013 - 10:33 PM
–snip–

Scratch all that, I wasn't thinking. I have it displaying the variable the right way now. However, is there a way to round up in lua? >.>

math.ceil(number)

Scratch that I wasn't thinking either lol. math.ceil(number) works to bring a decimal up to the next whole number. I would do this


x = 6
while x % 10 ~= 0 do
  x = x+1
end

I think that should work. I'll do some testing real quick, then edit or post agian

edit: that does work, but note that this doesn't round down, only up. So 1 would round all the way up to 10.
Crwnz4 #5
Posted 02 November 2013 - 10:43 PM
How about rounding to the first decimal instead of a whole integer?
CCJJSax #6
Posted 02 November 2013 - 10:45 PM
How about rounding to the first decimal instead of a whole integer?

Do you mean like rounding 1.589 to 1.6?
Crwnz4 #7
Posted 02 November 2013 - 10:50 PM
Exactly. For example, with my code:

while true do
local mEController = peripheral.wrap("right")
LoPerc = mEController.getFreeBytes()*100/mEController.getTotalBytes
print(LoPerc)
sleep(30)
end
I am getting 99.99697 because I have one piece of cable in my AE system. I would like it to read 99.9%
CCJJSax #8
Posted 02 November 2013 - 10:57 PM
Ok that is definitely more tricky, at least to my existing knowledge in LUA. I'll see what I can come up with. give me a sec.
CCJJSax #9
Posted 02 November 2013 - 11:22 PM
Ok I found this online, I didn't make this. I was failing with mine big time lol. :P/>


function math.round(number, decimals, method)
	decimals = decimals or 0
	local factor = 10 ^ decimals
	if (method == "ceil" or method == "floor") then return math[method](number * factor) / factor
	else return tonumber(("%."..decimals.."f"):format(number)) end
end

So you could use it like this


x = 99.99697

print(math.round(x, 1) -- the first parameter is what you want to be rounded, I'm not 100% sure what the second param does or the third.  But from what I have tested that looks right


edit: ok the meaning of the second and third parameter is described at the website I found that at http://wiki.multithe...wiki/Math.round

edit[2]: The decimal param doesn't seem to be working, but to get the first decimal point seems right for what you are trying to do.
Crwnz4 #10
Posted 02 November 2013 - 11:45 PM
Alright, I'm now just getting two prints of 99.99697 instead of 99.9 or 100. Here is the exact code on the computer:


I also tried putting x = LoCo before the last print, as well as x = 99.99697. Still getting 5 decimal points…hrmmm

Alright, I think I finally got it thanks to some Google magic…

while true do
local mEController = peripheral.wrap("right")
local Total = mEController.getTotalBytes()
local Free = mEController.getFreeBytes()
LoCo = mEController.getFreeBytes()*100/mEController.getTotalBytes()
print(LoCo)
function round(num, idp)
   local mult = 10^(idp or 0)+0.5) / mult
   end
   function test(a, B)/>/>/> print(round(a,B)/>/>/>) end
test(LoCo,1)
sleep(120)
end

This is rounding to the first decimal, taking all the others into consideration. So LoCo ends up being 100%, but say 29.14373 would end up being 29.1. Tested and works…

Whew that was fun…now to get
Free Space:

LoCo


To show up on a monitor about 30 blocks away….>.<
CCJJSax #11
Posted 03 November 2013 - 12:37 AM
Alright, I'm now just getting two prints of 99.99697 instead of 99.9 or 100. Here is the exact code on the computer:


I also tried putting x = LoCo before the last print, as well as x = 99.99697. Still getting 5 decimal points…hrmmm

Alright, I think I finally got it thanks to some Google magic…

   return math.floor(num * mult while true do
local mEController = peripheral.wrap("right")
local Total = mEController.getTotalBytes()
local Free = mEController.getFreeBytes()
LoCo = mEController.getFreeBytes()*100/mEController.getTotalBytes()
print(LoCo)
function round(num, idp)
   local mult = 10^(idp or 0)+0.5) / mult
   end
   function test(a, B)/>/> print(round(a,B)/>/>) end
test(LoCo,1)
sleep(120)
end

This is rounding to the first decimal, taking all the others into consideration. So LoCo ends up being 100%, but say 29.14373 would end up being 29.1. Tested and works…

Whew that was fun…now to get
Free Space:

LoCo


To show up on a monitor about 30 blocks away….>.<

Beats my failing way of making it a string and working with it that way. lol
Crwnz4 #12
Posted 03 November 2013 - 12:42 AM
Hey, at least it's working now…heh. But now, any ideas on linking it up with the monitors? Again, about 30 blocks away. am I able to keep one computer next to the ME controller, use a wireless modem, have another next to the monitors, and send the output of test(LoCo, 1) to the monitors?

I really like coding, but man this is giving me a headache :D/>

EDIT: Now I'm stuck on this…I moved the ME Controller, and the monitors right on either side of the computer, and I'm trying to get it to display the results of text(LoCo,1)….

Why you not work? =/

while true do
local monitor = peripheral.wrap("left")
local mEController = peripheral.wrap("right")
local Total = mEController.getTotalBytes()
local Free = mEController.getFreeBytes()
print(LoCo)
function round(num, idp)
	 local mult = 10^(idp or 0)
	 return math.floor(num * mult + 0.5) / mult
end
function test (a, B)/> print(round(a,B)/>) end
x = test(LoCo,1)
monitor.write(x)
sleep(120)
end
CCJJSax #13
Posted 03 November 2013 - 02:10 AM
Hey, at least it's working now…heh. But now, any ideas on linking it up with the monitors? Again, about 30 blocks away. am I able to keep one computer next to the ME controller, use a wireless modem, have another next to the monitors, and send the output of test(LoCo, 1) to the monitors?

–snip–

A few ways I can think of are as follows.

1: Wireless rednet. though I think that has a 20 or so block range. (configurable)
2: wired rednet.
3: have your computer make a file (fs API) and then upload that to a server to be downloaded at the other computer.
4: Send a specific number of things that you will always have (like cobble) through an enderchest and have a turtle pull them out of it. have the turtle calculate the number of items and then there you have your data. this would be a bit of a pain, but infinite range/extradimentional "signal" is a big plus.
5: have a turtle move over to that location and change the display on the monitor.

– the rest are mostly jokes sprouted from a lack of sleep XD
6: Have a stretch of redstone and have the computer signal in binary/morse code
7: make a deal with the devil
8: type out the data yourself each time
9: I'll stop now. :P/>
Crwnz4 #14
Posted 03 November 2013 - 02:30 AM
Heh, the devil was close to being called……

But, I did it! I don't like how the code looks, but it gets the job done, mostly centered :D/>


while true do
local monitor = peripheral.wrap("left")
local mEController = peripheral.wrap("right")
local Total = mEController.getTotalBytes()
local Free = mEController.getFreeBytes()
LoCo = mEController.getFreeBytes()*100/mEController.getTotalBytes()
function round(num, idp)
   local mult = 10^(idp or 0)
   return math.floor(num * mult +0.5) / mult
   end
   function test(a, B)/>
   monitor.setTextScale(3)
   monitor.setCursorPos(5,4)
   monitor.write(round(a,B)/>
   end
monitor.setTextScale(3)
monitor.setCursorPos(3,2)
monitor.write("Free Space:")
test(LoCo,0)
sleep(120)
monitor.clear()
end

I ended up just moving the ME controller closer and running some hidden cables.