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

Playing With Inventory

Started by Miotonir, 15 July 2012 - 03:36 AM
Miotonir #1
Posted 15 July 2012 - 05:36 AM
shell.run("Inventory.txt"); change lua to txt in test

Some Invenotry functions
Test:
[attachment=329:Test.txt]


shell.run("Inventory.lua");
local inv = Inventory:new()
shell.run('clear');

--Inventory Menu
inv.Functions();
local int;
local str;
while true do
print("Type Function: (help? any key)");
str = read();
if str == "break" or str == "Break()" then
  break;
elseif str == "HasInInventory()" then
  print("Detect Where?0=fr,1=up,-1=down");
  int = tonumber(read());
elseif str == "Drop()" then
  print("How Many Blocks?");
  int = tonumber(read());
end
inv:Do(str,int);
end



Inventory:
[attachment=330:Inventory.txt]


-- Inventory Management

Inventory = {};
function Inventory:new()
local obj = { Empty,Selected,STable };
obj.Empty = 64;
obj.Selected = 1;
turtle.select(obj.Selected);

function obj:Do(str,int) --interface
  shell.run('clear');
  if str == nil then
   self:Functions();
  elseif str == "HasEmpty()" then
   self:HasEmpty();
  elseif str == "SelectEmpty()" then
   self:SelectEmpty();
  elseif str == "HasBlock()" then
   self:HasBlock();
  elseif str == "ShowSpace()" then
   self:ShowSpace();
  elseif str == "DiscardAll()" then
   self:DiscardAll();
  elseif str == "HasInInventory()" then
   self.HasInInventory(int);
  elseif str == "clear()" then
   shell.run('clear');self.Functions();
  elseif str == "Drop()" then
   self:Drop(int);
  elseif str == "DrawTable()" then
   self:DrawTable();
  else
   self:Functions();
  end
end

function obj:Drop(int)
local count;
  for i=1,int do
   if self:HasBlock() then
    turtle.drop()
   else
    break;
   end
   count = i;
  end
  print("Dropped from " ,count , " Slots");
end
function obj.GetTbl()
  local tbl = {};
  for i=1,9 do
   tbl[i] = turtle.getItemSpace(i);
  end
  return tbl;
end
function obj:HasEmpty(bool) -- Checks For Empty Slot
  local tbl = self.GetTbl();
  for i=1,10 do
   if tbl[i] > self.Empty-1 then
    print("SlotNo:",i," - IsEmpty");
    if bool == false then
	 return true;
    else
	 return i;
    end
   end
  end
end
function obj:SelectEmpty()
  local first = self:HasEmpty(true);
  turtle.select(first);
  self.Selected = first;
  print("Selected first empty slot.");
end
function obj:ShowSpace()
  local tbl = self.GetTbl();
  for i=1,9 do
   local spc = tbl[i];
   print("Slot:",i," Space:", spc);
  end
  return tbl;
end
function obj:HasBlock()
  local tbl = self.GetTbl();
  for i=1,9 do
   if tbl[i] < 64 then
    print("Slot :",i, " has block");
    turtle.select(i);
    print("Selected found block...");
    return true;
   end
  end
  return false;
end
function obj:DiscardAll()
  if self:HasBlock() then
   for i=1,9 do
    turtle.drop();
   end
   print("Dropped all Blocks");
  else
   return;
  end
end
function obj.HasInInventory(int)-- 0=Front,1=Up,-1=Down
  local match = false;
  for i=1,9 do
   turtle.select(i)
   if int == 0 then
   match = turtle.compare();
   print("Compare Front:",match);
   elseif int == 1 then
   match = turtle.compareUp();
   print("Compare Up:",match);
   elseif int == -1 then
   match = turtle.compareDown();
   print("Compare Down:", match);
   else
    print("lol?")
   end
  
   if match == true then
    print("Match Found ... Selecting Slot:",i);
    turtle.select(i);
    return match;
   end
  end
  print("Not Found");
  turtle.select(1);
end
function obj.Functions()
  print("-- InventoryFunctions:");
  print("HasEmpty();");
  print("SelectEmpty()");
  print("ShowSpace()");
  print("HasBlock()");
  print("DiscardAll()");
  print("HasInInventory()");
  print("Drop()");
  print("DrawTable()");
end
function obj:DrawTable()
  local tbl = self.GetTbl();
  print("Spaces left:");
  for y=1,3 do
    if y ~= 3 then
    print("[ " , tbl[y*y] , " , " , tbl[y*y+1] , " , " , tbl[y*y+2]," ]");
    else
    print("[ " , tbl[7] , " , " , tbl[8] , " , " , tbl[9]," ]");
    end
  end
end

return obj;
end
print("#InventoryInitialized");
Pharap #2
Posted 06 August 2012 - 09:27 AM
I can see an issue with this.

The Has in inventory function doesn't test what the items are, it just compares them with other items in the turtle.