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

[1.48/1.5] Inventory API - Manage inventories (No magic involved)

Started by Firedroide, 09 December 2012 - 02:42 AM
Firedroide #1
Posted 09 December 2012 - 03:42 AM
Hi everyone!

I know things similar to this have been suggested quite some times, and people tend to just say:
"Ehrm…. You can use CCSensors to do this!" or "No magic in ComputerCraft".
This suggestion, however, contains no magic, can't be done with CCSensors and would greatly improve the turtles!

What should the API do?

The API should improve the interaction of turtles with adjacent inventories,
allowing the turtle to pull out items from a certain slot and so on.
The API should contain the following functions:


inventory.getItemCount(side, slotNumber)
  Returns how many of an item are in a certain slot in the inventory.
  Returns 0, if there's nothing in the slot.

inventory.getItemSpace(side, slotNumber)
  Similar to getItemCount, just returns how many items are missing to fill the stack up.
  Returns 64 if there's air in the slot

inventory.slotEmpty(side, slotNumber)
  Returns true if the slot is empty, false otherwise.

inventory.suck(side, slotNumber, [quantity])
  Sucks out items from a slot in the inventory and fills them in the selected slot of the turtle. Takes out all items, if no quantity is given.
  Returns true if the action succeeded.

inventory.drop(side, slotNumber, [quantity])
  Drops items from the turtle's selected slot into a certain slot in the inventory. Drops all items, if no quantity is given.
  Returns true if the action succeeded.

inventory.move(side, slot1, slot2, [quantity])
  Moves items INSIDE the inventory from one slot to another.
  Moves all items if no quantity is given.
  Returns true if the action succeeded.

inventory.getSlotCount(side)
  Returns a number how many slots there actually are.

But why is this API really necessary?

Mainly, because the suck and drop functions there are at the moment don't allow the turtle to do anything complicated with inventories.
Refuel a furnace?
- Nope, can't put it into the bottom slot, if you just do turtle.drop(), the coal will go into the top slot.
You'll have to move your turtle below the furnace and do turtle.dropUp() then.

Take out items from another slot than just the first one?
- Really not possible. The only way to do something like that would be to suck up items until you have reached your slot count, and then drop all the items back in.
This is not really user- and turtle-friendly though, because it only works up to slot 16 of the inventory (because of the turtle's inventory size) and takes quite some time to do.

Take out just a few items from an inventory?
- Yes, but only in a hacky way, sucking the items out, transferTo some of them, and put the others back in.


Long story short, this API would make the interaction with inventories much easier, more comfortable and more reliable, and would allow easy interaction with furnaces or machines, without having to set up complicated piping systems everywhere just so your turtle can interact with it.

Magic, magic everywhere!

Nope, no magic.
There are no item or block IDs and everything listed could totally be done with something like a robot.

It would be extremely awesome if something like that could be implemented.
ChunLing #2
Posted 09 December 2012 - 05:25 AM
So you have to arrange things to be turtle accessible. That is a design choice, and one that I rather like.
Firedroide #3
Posted 09 December 2012 - 07:30 AM
So you have to arrange things to be turtle accessible. That is a design choice, and one that I rather like.

Yeah, but I personally, and a lot of other people I guess, don't like to be forced to create multiple separate storage rooms, so the turtle can access them properly.
With those functions, a turtle would be able to interact with all inventories by just adding some more code, and without having to set up special turtle-friendly environments.

Other than that, what about people who don't have BuildCraft or RedPower installed and still want turtles to easily interact with inventories and machines?
Cranium #4
Posted 09 December 2012 - 07:59 AM
Why do people want to make things so complicated? Computercraft is, by design, made to be simple and easy to use. That is why Lua was chosen as the scripting language. Advanced functions like these (in my opinion) belong in a peripheral.
It may be challenging to have your turtle do what you want, but that makes it all the more fun.
Try building a massive storage warehouse to keep all of your items. One chest per item. Make a catalogue of where the items are, and build a program that can send a turtle to fetch the items you need. This would not only solve your problems with inventory management, but it would be really freaking awesome!
I don't think that this should be implemented to the mod itself. I think it should be made possible through the OpenCCSensors project.
Cloudy #5
Posted 09 December 2012 - 08:05 AM
TL;DR answer - No.

The API should improve the interaction of turtles with adjacent inventories,
allowing the turtle to pull out items from a certain slot and so on.

Turtles are blind - and this is how it is going to remain.

But why is this API really necessary?

Mainly, because the suck and drop functions there are at the moment don't allow the turtle to do anything complicated with inventories.
Refuel a furnace?
- Nope, can't put it into the bottom slot, if you just do turtle.drop(), the coal will go into the top slot.
You'll have to move your turtle below the furnace and do turtle.dropUp() then.

Would you want an API for buildcraft/rp pipes to select their slot regardless of where they're placed? This is how sided inventories in forge work - depending on the side, they put in a different slot. I see no reason for turtles to work any different.

Take out items from another slot than just the first one?
- Really not possible. The only way to do something like that would be to suck up items until you have reached your slot count, and then drop all the items back in.
This is not really user- and turtle-friendly though, because it only works up to slot 16 of the inventory (because of the turtle's inventory size) and takes quite some time to do.

It is possible. Exactly how you said. Just because it isn't quite straight forward, doesn't mean it isn't possible. I'm not a fan of handing everything to you on a plate - if you want to work with inventories, you need to take into the account how the mod in question works.

Take out just a few items from an inventory?
- Yes, but only in a hacky way, sucking the items out, transferTo some of them, and put the others back in.

Or just take a stack, and do turtle.drop(amount). Much less hacky.

Long story short, this API would make the interaction with inventories much easier, more comfortable and more reliable, and would allow easy interaction with furnaces or machines, without having to set up complicated piping systems everywhere just so your turtle can interact with it.

You don't need complicated piping systems. You can move the turtle round an inventory. I don't understand why this is so difficult? As turtles are blind, you need to tell it where inventories are anyway. If you're doing that, you will have designed it round turtles anyway. You just need to design your systems carefully - just like every other mod.
Firedroide #6
Posted 09 December 2012 - 09:26 AM
I'm not a fan of handing everything to you on a plate

Well, me neither! But in my opinion, the interaction with inventories is really more complicated than it would need to be.
What I really like about computers and turtles in fact, is, that you can realize even something complicated in a really short time, if you know something about programming, and usually, it's even quite simple to do.
A disk that self-installs its content to a computer on startup, then ejects itself and reboots? Sure, no problem, let's do it!
Let a turtle build a factory structure? Not a problem either.

But interaction with inventories? Unless you have - like Ninja Scripter mentioned - a giant storage building where you have every single item assigned to a single chest, and saved everything into a file with the coordinates and the contents of the chests, it's simply impossible to do.

At least for me, it's quite a severe limitation of what turtles are able to do, and I really don't understand why this has to be.
It could be its own peripheral, it could have diamond blocks in its recipe, I don't mind.
But really not being able to do interactions with more complicated storage rooms is quite a bummer, I think.
Tiin57 #7
Posted 09 December 2012 - 10:25 AM
Just saying, our titles are not our names. Cranium is not "Ninja Scripter", as I am not "Java Lunatic". :P/>
Firedroide #8
Posted 09 December 2012 - 10:46 AM
Just saying, our titles are not our names. Cranium is not "Ninja Scripter", as I am not "Java Lunatic". :P/>

Oh wow, fail, lol :D/>
I read it correctly for ChunLing and Cloudy, but I totally just jumped to the title there for Cranium.
Sorry man ^^