- ComputerCraft | Programmable Computers for Minecraft
- → hilburn's Content
hilburn's Content
There have been 150 items by hilburn (Search limited from 10-February 22)
#193381 Chest/inventory Peripherals
Posted by
hilburn
on 12 September 2014 - 11:27 AM
in
Ask a Pro
#193372 How would a Pro do it?
Posted by
hilburn
on 12 September 2014 - 01:14 AM
in
Ask a Pro
In case you don't know what these are you add a field to each entry called next, this indicates the next entry in the list. You can have multiple nexts, eg nextName, nextAddress, as well as prevName etc if you want. Then when it comes to printing it out, you can do so in order without having done any actual sorting.
An example implementation:
local llist={}
llist[1]={nextName=2,nextSurname=2}
local function addToList(entry)
llist[#llist+1]=entry
local thisName=1
local thisSurname=1
local nextNameEntry=llist[1].nextName
local nextSurnameEntry=llist[1].nextSurname
while llist[#llist].nextName==nil and llist[#llist].nextSurname==nil do
if nextNameEntry>0 and (llist[nextNameEntry]=nil or llist[nextNameEntry].name>entry.name) then
llist[#llist].nextName=llist[thisName].nextName
llist[thisName].nextName=#llist
nextNameEntry=-1
elseif nextNameEntry>0 then
nextNameEntry=llist[thisName].nextName
end
if nextSurnameEntry>0 and (llist[nextSurnameEntry]=nil or llist[nextSurnameEntry].surname>entry.surname) then
llist[#llist].nextSurname=llist[thisSurname].nextSurname
llist[thisSurname].nextSurname=#llist
nextSurnameEntry=-1
elseif nextSurnameEntry>0 then
nextSurnameEntry=llist[thisSurname].nextSurname
end
end
end
local function listByName()
nextName=llist[1].nextName
while nextName~=nil do
print(llist[nextName].name.." "..llist[nextName].surname)
nextName=llist[nextName].nextName
end
end
local function listBySurname()
nextSurname=llist[1].nextSurname
while nextSurname~=nil do
print(llist[nextSurname].surname..", "..llist[nextSurname].name)
nextSurname=llist[nextSurname].nextSurname
end
end
I'm pretty sure I've cocked up the implementation of addToList, but it shows you the general shape of how it works. Removing entries basically involves looping through the list and counting your position until your position gets to the entry you want to delete, then just link the previous entry to the next entry and it will vanish from the list (don't forget to set that entry to nil, but don't use table.remove as that will break everything)
They are a very powerful tool and well worth looking into
#193332 Chest/inventory Peripherals
Posted by
hilburn
on 11 September 2014 - 10:42 AM
in
Ask a Pro
#193298 [Math] Matrices
Posted by
hilburn
on 10 September 2014 - 04:21 PM
in
Ask a Pro
So: on to division. The simple way to think about it is that any matrix can be represented as the product of 2 other matrices (even if one of those matrices is the index)
So [C]=[A]*[B] where [B] is defined as the matrix we want to "divide" by. We achieve this by post-multiplying (remember order is important) by the inverse of [B], [B]^(-1) to get
[C]*[B]^(-1) = [A]*[B]*[B]^(-1) = [A]
Where [A] is the matrix that when multiplied by [B], gives [C]. Which is pretty close, but not exactly, what the result of a division is.
#193143 Big Reactor and Energy Cell Control
Posted by
hilburn
on 08 September 2014 - 11:20 AM
in
Ask a Pro
#193129 Redstone signal not emmiting
Posted by
hilburn
on 08 September 2014 - 07:56 AM
in
Ask a Pro
if turbine1active or turbine2active then
rs.setOutput("top",false)
else
rs.setOutput("top", true)
end
Additionally, you could make your code much more compact if you used tables to store your peripherals, so instead of mon1, mon2 you have mon[1] and mon[2], that way you could just loop through your sensing/display code twice, with "for i=1,2 do"
hope this helps
#192801 rednet.send variable + string
Posted by
hilburn
on 01 September 2014 - 09:44 PM
in
Ask a Pro
Alternatively if you really want to send it in one you could do something with string functions. Something like:
local anzahl=io.read() rednet.send(13,"<anzahl>"..anzahl.."</anzahl>".."hello")
and then on the receiving computer:
temp=string.sub(msg,string.find(msg,"<anzahl>(.-)</anzahl>")) --#removes the "hello" and anything else not included within the <></> tags temp=string.gsub(temp,"<(.-)>","") --#removes the tags local anzahl=temp --#returns the variable
#192704 Let turtle place a turtle with a startup program
Posted by
hilburn
on 31 August 2014 - 05:41 PM
in
Ask a Pro
#192570 TC Smeltery problem.
Posted by
hilburn
on 30 August 2014 - 12:11 AM
in
Ask a Pro
tank=peripheral.wrap("back")
while true do
output=false
for _,liquid in pairs(tank.getLiquid()) do --#check this is the function, there is one which will return an array of liquid data for the smeltery
if liquid.name=="liquid.emerald" then --#not sure if this is the field or that's the name of liquid emerald, again you'd need to test that
output=true
end
end
rs.setOutput("right",output)
sleep(2)
end
#192484 TC Smeltery problem.
Posted by
hilburn
on 28 August 2014 - 05:01 PM
in
Ask a Pro
I actually made a program you might like to use, it's designed to trade for pages but you can just cut that bit out if you want emeralds. the setup is reasonably well explained though
http://www.computerc...penperipherals/
#192241 Sorting Lua Tables: Multiple Columns
Posted by
hilburn
on 26 August 2014 - 08:45 AM
in
Ask a Pro
For generating the points why not just solve the circle equation? you onto need to do it in the +ve x and +ve y quadrant and then rotate it to the other quadrants. In my head it looks something like:
local function rotate(point, quadrants)
if quadrant==1 then
return -point.z, point.x, point.theta+math.pi/2
elseif quadrant==2 then
return -point.x, -point.z, point.theta+math.pi
end
end
for x=0,r,0.1 do --#necessary to do steps smaller than 1 for cases when there are more than 1 block per x value
z=math.floor((r^2-x^2)^0.5)
xfloor=math.floor(x)
points[#points+1]={["x"]=xfloor,["z"]=z,["theta"]=math.atan2(x,z)}
end
for i,j in pairs(points) do
if j.x==points[i+1].x and j.z==points[i+1].z then
table.remove(points,i+1) --#strips duplicate points from the table
end
end
table.sort(points, function(p1,p2) return p1.theta<p2.theta end) --#sort table (though it should already be sorted by our generation method)
for= thetaoffset=1,2 do
for i,j in pairs(points) do
points[#points+1]={rotate(j,offset)}
end
end
not saying this would work perfectly without some tweaking, but it could be a good starting point
#192202 Sorting Lua Tables: Multiple Columns
Posted by
hilburn
on 25 August 2014 - 11:12 PM
in
Ask a Pro
{[1]={x=1,y=0,theta=0},[2]={x=1,y=1,theta=45},...}
you could then use
table.sort(table, function(point1,point2) return point1.theta<point2.theta end)
to sort it, alternatively, if you didn't want to store the angle in the table, you could modify the comparison function to calculate the angles and then compare them
#192200 Minor problems with a timer loop
Posted by
hilburn
on 25 August 2014 - 11:00 PM
in
Ask a Pro
if events[1] == "mouse_click" and events[2] == 1 then if events[4]==8 and events[3]~=1 then if events[3] < 5 then ... elseif events[3] < 7 then ... end end ... end
the same logic could be used earlier in your program with your other ifs however another thing I would tend to do in this case would be to create a lookup table with keys from 0 to 100 that listed the colour to use, so instead of doing a big control statement where really the only thing you are changing is the colour you are using as the background, you just pass
term.setBackgroundColour(colourLookup[fluidInfo[selected].percent])
As again, a lookup is faster than a bunch of ifs
I'm not sure how much this will help, but I hope it does a bit
#190996 How Do I Make a Toggle menu
Posted by
hilburn
on 13 August 2014 - 06:28 PM
in
Ask a Pro
Previously set up: Receiver on Frequency (X) goes into a T flip flop, output of this goes to the lights and a transmitter on frequency (X+1)
Computer can find the status of the lights by setting it's receiver to frequency (X+1), can toggle the status of the lights by transmitting on X eg.
function TurnOn(freq)
receiver.setFreq(freq+1) --#set receiver to the monitoring frequency
if not rs.getInput("left") then --#if it's not on
transmitter.setFreq(freq) --#set transmitter to the toggle frequency
rs.setOutput("back",true)
sleep(0.1)
rs.setOuptut("back",false) --#pulse the frequency to toggle the lights on
end
end
As for the menu itself you could do use tables which would make it quite neat. Something like:
menu={[1]={menudisplay="Fence Lighting", selecttext="Fence Lighting Control Selected", submenu=
{[1]={menudisplay="Turn Off", selecttext="Turning Fence Lighting Off", selectfunction=turnOff(1)},
[2]={menudisplay="Turn On", selecttext="Turning Fence Lighting On", selectfunction=turnOn(1),
[3]=...}
You could then have a function called DisplayMenu which would do something like:
function DisplayMenu(dmenu)
for i,j in pairs(dmenu) do
print("["..i.."] "..j.menudisplay) --#prints out all the menu items of the table supplied to it
end
input=0
while not (input>0 and input<=#menu) do --#loops until valid input
input=read()
end
print(dmenu[input].selecttext) --#print out the confirmation text
if dmenu[input].submenu then --#if there is a submenu
DisplayMenu(dmenu[input].submenu) --#display it
else --#otherwise
dmenu[input].selectfunction --#do the function the menu item is tied to
end
end
#190347 False AND True goes to then all the time
Posted by
hilburn
on 07 August 2014 - 02:18 AM
in
Ask a Pro
Are you sure you haven't typo'd? Or declared one of the variables as a local in a different function so your if can't see it and so reads it as nil?
Other than those generalities I can't help without code
- ComputerCraft | Programmable Computers for Minecraft
- → hilburn's Content


