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

[Program help] Computer powered request system

Started by JamiePhonic, 26 March 2013 - 05:07 AM
JamiePhonic #1
Posted 26 March 2013 - 06:07 AM
om my server we have a huge area dedicated to "the factory" this place generates all the power used by the machines on the server served like a "national gird" where players request access to power and their hooked up and pay a fee every week or something. we also have a kind of shop thing where players can go in and use a computer to request things for money. the system were using is very very basic. enter a number an the item ends up in the chest after a few seconds. i've been trying to get a new system up and running where it acts more like a website, e.g. you enter a name or number and 32 of that item is added to your cart, when your done, you check out and all the items are delivered at once.

the code i have so far is:

--item list + quantity + side bundle is on + colour to pulse
available_items = {
[1] = {item = "gold bar", quantity = 32, side = "left", color = "yellow"},
[2] = {item = "sliver bar", quantity = 32, side = "left", color = "white"},
[3] = {item = "iron bar", quantity = 32, side = "left", color = "gray"},
[4] = {item = "tin bar", quantity = 32, side = "left", color = "lightgray"},
[5] = {item = "copper bar", quantity = 32, side = "left", color = "orange"},
[6] = {item = "uranium bar", quantity = 32, side = "left", color = "green"},
[7] = {item = "uu matter", quantity = 32, side = "left", color = "purple"},
[8] = {item = "diamond", quantity = 32, side = "left", color = "cyan"},
[9] = {item = "ruby", quantity = 32, side = "left", color = "pink"},
[10] = {item = "sapphire", quantity = 32, side = "left", color = "blue"},
[11] = {item = "emerald", quantity = 32, side = "left", color = "lime"},
[12] = {item = "coal", quantity = 32, side = "left", color = "black"},
[13] = {item = "lapis", quantity = 32, side = "left", color = "lightblue"},
[14] = {item = "redstone", quantity = 32, side = "left", color = "red"},
[15] = {item = "nikolite", quantity = 32, side = "left", color = "magenta"},
}
cart = { } --creates the cart table for items to be added to
--clear screen function (lazyness)
function clear()
term.clear()
term.setCursorPos(1,1)
end
--print content of cart
function checkout()
clear()
for i = 1, #cart do
  print( cart[i].quantity .. string.rep(" ", 4-(string.len(tostring(cart[i].quantity)))) .. cart[i].item )
  sleep(0.5)
end
print("\nEnd of cart")
write("Press enter") read()
end
--main program loop
while true do
clear()
print("choose a number")
print("type 'checkout' to go to checkout")
for i = 1, #available_items do
  print( i .. string.rep(" ", 4-(string.len(tostring(i)))) .. available_items[i].item )
end
choice = read()
if choice == "checkout" then
   checkout()
else
  choice = tonumber(choice)
   if available_items[choice] then
   table.insert(cart, {item = available_items[choice].item, quantity = available_items[choice].quantity})
   print("Added " .. available_items[choice].quantity .. " Of " .. available_items[choice].item .. ".")
   sleep (1)
   end
end
end

so far, the program prints a list of all available items and their associated number. you can enter a number and 32 of the item associated with that item is added to a cart, typing checkout lists the content of your cart. that's all it does. i need help getting it finished. i have tried myself but have gotten almost nowhere. i know the basics of tables(as you can see from the code) but even that i had a little help with to begin with.
each item has a side and color associated with it to help with scalability (so i can just add or remove stuff from the "available_items" table and those items will be available)
the system works by having 1 bundle on each side connected to a set of 16 retrievers per cable in the basement of the building. when an item is requested, the appropriate color on the appropriate bundle is pulsed a number of times to retrieve the right amount.

e.g. if you requested 64 gold bar, the yellow wire on the left side bundle would be pulsed twice (64/32 = 2) and 64 gold bar would end up in the chest.
this is part i'm having trouble implementing. getting it to work its way through the cart and request each item in sequence.

any help you could give would be extremely appreciated.

ps. i know the current program only adds the items name and quantity to the cart, not its associated side or color.
jag #2
Posted 26 March 2013 - 06:34 AM
Oh I see how it is, neat system, it sounds really cool!

Anyways to start off you can just import the entire item into the "cart" table, like so:

-- This is in the main loop btw..
table.insert(cart, available_items[choice])

And from here it's really simple, just do something like this:

function request()
  for id,_ in pairs( cart ) do -- this is basically same as "for id = 1, #cart do".
    rs.setBundledOutput( cart[id].side, colors[cart[id].color] )
    -- colors.* is a table of all the available colors in number form, ex: colors.blue = 2048.
    sleep( 0.5 )
    rs.setBundledOutput( cart[id].side, 0 ) -- to turn it off
    sleep( 0.5 )
  end
end

If you are unsure of how the redstone API works, take a look at the wiki's page.
jag #3
Posted 26 March 2013 - 06:49 AM
Either way, if you got Misc.Peripherals installed you can use the interactive sorter, wich would be much more smooth and customizable. But will need a bit more knowledge of programming, but that wont be a big problem with the help from this forum :)/>
JamiePhonic #4
Posted 26 March 2013 - 08:37 AM
2 things:

-- This is in the main loop btw..
table.insert(cart, available_items[choice])

And from here it's really simple, just do something like this:

function request()
  for id,_ in pairs( cart ) do -- this is basically same as "for id = 1, #cart do".
	rs.setBundledOutput( cart[id].side, colors[cart[id].color] )
	-- colors.* is a table of all the available colors in number form, ex: colors.blue = 2048.
	rs.setBundledOutput( cart[id].side, 0 ) -- to turn it off
	sleep( 0.5 )
  end
end
first:

-- colors.* is a table of all the available colors in number form, ex: colors.blue = 2048.
Whats this referring to?

second, this code works, it doesn't handle the "if you requested 64 gold bar, the yellow wire on the left side bundle would be pulsed twice (64/32 = 2) and 64 gold bar would end up in the chest."

i know the code doesn't exist to make this function worth adding because adding 2 lots of 32 gold just adds 32 gold 2 times instead of making it 1 lot of 64, but i'd still like that functionality because i'm going to try to implement it that function (2 lots of 32 gold combined into 1 of 64
jag #5
Posted 26 March 2013 - 09:17 AM
I see what you mean, but what I understand from your code each request get imported into the list/table namned "cart". So when it goes trough them it could look like following:


cart = {
  [1] = {item = "uranium bar", quantity = 32, side = "left", color = "green"},
  [2] = {item = "uranium bar", quantity = 32, side = "left", color = "green"},
  [3] = {item = "coal", quantity = 32, side = "left", color = "black"},
  [4] = {item = "iron bar", quantity = 32, side = "left", color = "gray"},
}

In that case it will go trough the list it will first pull 32 uranium, then 32 uranium, following by 32 coal and 32 iron. See how that works?
JamiePhonic #6
Posted 26 March 2013 - 09:52 AM
I see what you mean, but what I understand from your code each request get imported into the list/table namned "cart". So when it goes trough them it could look like following:


cart = {
  [1] = {item = "uranium bar", quantity = 32, side = "left", color = "green"},
  [2] = {item = "uranium bar", quantity = 32, side = "left", color = "green"},
  [3] = {item = "coal", quantity = 32, side = "left", color = "black"},
  [4] = {item = "iron bar", quantity = 32, side = "left", color = "gray"},
}

In that case it will go trough the list it will first pull 32 uranium, then 32 uranium, following by 32 coal and 32 iron. See how that works?
i know, that i wrote it that way. i could change the values that are in the "available_items" table, but i want to give users the option to request only what they need because i'm going to eventually implement a payment system too. e.g. 32 gold is 20 industrial credits and 32 iron is 8 industrial credits so the total is 28 credits. see what i mean?

you also didn't answer my first question:
what does the comment:
– colors.* is a table of all the available colors in number form, ex: colors.blue = 2048.
refer to?
jag #7
Posted 26 March 2013 - 10:18 AM
Yes I just tried briefly explain it.

Dont mind it, it's hard to explain.
Engineer #8
Posted 26 March 2013 - 11:04 AM
Search on google 'computercraft color api'.
Basically colors is a table. Just like this:
colors = {
['white'] = 1.
– list on the wiki
}

Now whenever you call colors.white it basically fills in 1.
This is very usefull when using multiple colors, this is the basics of it. If you want more, call
It. Tomorrow I post it though because my iPod doesnt do the job for that story.
jag #9
Posted 27 March 2013 - 04:29 AM
Yea that explanation works!

If you have for example NEI and lookup wool, they will come in a certain order. (white, orange, magenta, light blue etc.)
As you might notice, there are 16 colors. And their meta-values goes from 0 (white) to 15 (black).
And so the color code is 2 to the power of the meta value.
Example:

[0] White = 20 = 1
[1] Orange = 21 = 2
[2] Magenta = 22 = 4
[3] Light blue = 23 = 8
[4] Yellow = 24 = 16
[5] Lime = 25 = 32



[13] Green = 213 = 8'192
[14] Red = 214 = 16'384
[15] Black = 215 = 32'768

So that should get it explained enough.
jag #10
Posted 27 March 2013 - 04:38 AM
This means, if you want a color test you can just do this:
This requires an advanced computer/monitor.

for count = 0,15 do
	term.setTextColor(2^count)
	print("Color #"..2^count)
end

Wich would result into this (not that accurate)
SpoilerColor #1
Color #2
Color #4
Color #8
Color #16
Color #32
Color #64
Color #128
Color #256
Color #512
Color #1024
Color #2048
Color #4096
Color #8192
Color #16384
Color #32768
theoriginalbit #11
Posted 27 March 2013 - 05:14 AM
Search on google 'computercraft color api'.
or you just type the following into the url bar "computercraft.info/wiki/<api_name_with_first_letterl_capital>_(API)" so the colors api would be computercraft.info/wiki/Colors_(API)

Basically colors is a table. Just like this:
colors = {
['white'] = 1.
– list on the wiki
}
Its actually not a table. the api colors is a table, but the colours inside that api are not in a table. here is a screenshot of the colours api in its entirety http://puu.sh/2om50 as you can see, no table.
JamiePhonic #12
Posted 27 March 2013 - 05:54 AM
Yea that explanation works!

If you have for example NEI and lookup wool, they will come in a certain order. (white, orange, magenta, light blue etc.)
As you might notice, there are 16 colors. And their meta-values goes from 0 (white) to 15 (black).
And so the color code is 2 to the power of the meta value.
Example:

[0] White = 20 = 1
[1] Orange = 21 = 2
[2] Magenta = 22 = 4
[3] Light blue = 23 = 8
[4] Yellow = 24 = 16
[5] Lime = 25 = 32



[13] Green = 213 = 8'192
[14] Red = 214 = 16'384
[15] Black = 215 = 32'768

So that should get it explained enough.
i know that already, i was just wondering why the comment was there with no matching statment.
also, this program is for use in a tekkit classic server, so advanced computers/monitors don't matter because i cant use them. thanks for the help by the way.
im going to work on the UI a little now.

how could i get it that a user can just type the name of the item if they know it, or use the number of the item if they know that, of if they don't know either, type "list" to see a list of all items?
Engineer #13
Posted 27 March 2013 - 07:44 AM
Search on google 'computercraft color api'.
or you just type the following into the url bar "computercraft.info/wiki/<api_name_with_first_letterl_capital>_(API)" so the colors api would be computercraft.info/wiki/Colors_(API)

Basically colors is a table. Just like this:
colors = {
['white'] = 1.
– list on the wiki
}
Its actually not a table. the api colors is a table, but the colours inside that api are not in a table. here is a screenshot of the colours api in its entirety http://puu.sh/2om50 as you can see, no table.
How do you explain this?:


I see you are inside the actual API, but it is somehow stored in a table right?

and aside: ST2 ftw!:D/>
theoriginalbit #14
Posted 27 March 2013 - 09:40 AM
How do you explain this?:


I see you are inside the actual API, but it is somehow stored in a table right?
The answer comes from an extract of os.loadAPI


function os.loadAPI( _sPath )
  local sName = fs.getName( _sPath ) -- get the name of the api

  local tEnv = {} -- create a table for it
  setmetatable( tEnv, { __index = _G } ) -- metatables! :)/>
  local fnAPI, err = loadfile( _sPath ) -- load the file

  if fnAPI then -- if something is loaded
	setfenv( fnAPI, tEnv ) -- setup the environment
	fnAPI() -- run the api
  end

  local tAPI = {} -- make a new table
  for k,v in pairs( tEnv ) do -- copy everything from the environment into our environment
	tAPI[k] =  v
  end

  _G[sName] = tAPI -- store it in the global
end

The actual file is loaded into a table. hence colors, then there are variables in the api which makes colors.variable. if the colours were stored in their own table in the colours api you would need to access them in this way
colors.colorsTable.red
Engineer #15
Posted 27 March 2013 - 09:54 AM
Well that makes sense, thanks!

Sorry for kinda hijack on the actual topic.