Advertisement
hevohevo

ComputerCrafting2015-01-30

Jan 30th, 2015
330
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.31 KB | None | 0 0
  1. -- #深夜のComputerCrafting60分一本勝負
  2. -- お題: 「turtle.inspect()」または「turtle.getItemDetail()」を使ったプログラム
  3. -- 2015/01/30 23:00スタート by hevo2
  4. --
  5.  
  6. -- turtle.getItemDetail(slot_num)
  7. -- nil or
  8. -- { count=1, name="minecraft:dirt", damage=0}
  9.  
  10. -- #############################################################
  11. -- sortInventory()
  12. -- インベントリの中のアイテムを整理する。
  13. -- #############################################################
  14.  
  15.  
  16. -- いままで作った関数持ってくるよ。
  17.  
  18. -- インベントリ内にバラバラに入っているアイテムをできるだけまとめる
  19. -- turtle.transferTo(slot [, quantity]), 1/27作
  20. function collectItem()
  21.   local selected_slot = turtle.getSelectedSlot()
  22.   for i=1,16 do
  23.     local detail = turtle.getItemDetail(i)
  24.     if detail then -- スロットになにかアイテムある
  25.       for j=i,16 do
  26.         local detail2 = turtle.getItemDetail(j)
  27.         if detail2 and detail["name"]==detail2["name"] and detail["damage"]==detail2["damage"] then
  28.           turtle.select(j)
  29.           turtle.transferTo(i)
  30.         end
  31.       end
  32.     else -- スロットにアイテムない
  33.     end
  34.   end
  35.   turtle.select(selected_slot)
  36. end
  37.  
  38.  
  39. -- turtle.getItemCounrt(slot_num) , 1/27作
  40. -- 発展→ getItemCountAll(item_name, damage) => アイテム総数
  41. function getItemCountAll(item_name, damage)
  42.   local count = 0
  43.   for i=1,16 do
  44.     local detail = turtle.getItemDetail(i)
  45.     if detail then  -- スロットになにかアイテムある
  46.       if string.match(detail["name"],item_name) then  -- アイテム名がマッチする
  47.         if damage == nil or damage==detail["damage"] then  -- ダメージ値の指定があり、それが一致する
  48.           count = count + detail["count"]
  49.         end
  50.       else  -- アイテム名がマッチしない
  51.       end
  52.     else  -- スロットにアイテムない
  53.     end
  54.   end
  55.   return count
  56. end
  57.  
  58. -- selectItem(name, damage) , 1/26作
  59. -- スロット内を探し、name(damage値指定があるときはそれも)とマッチするアイテムを選択する。
  60. function selectItem(name,damage)
  61.   -- local selected_slot = turtle.getSelectedSlot()
  62.  
  63.   for i=1,16 do
  64.     local item = turtle.getItemDetail(i)
  65.     if type(item)=="table" then -- スロットに何かアイテム
  66.       -- print("slot=",i," ",item["name"]," ",item["damage"])
  67.       if string.match(item["name"],name) then -- 指定アイテムの名前と一致
  68.         if damage==nil or damage == item["damage"] then -- ダメージ値指定がない、または指定と一致
  69.           turtle.select(i)
  70.           return true, item["count"]
  71.         else -- 名前は一致したがダメージ値が違う
  72.           -- do nothing
  73.         end
  74.       else -- 指定アイテムの名前と不一致
  75.         -- do nothing
  76.       end
  77.     else -- スロット内にアイテムなし
  78.       -- do nothing
  79.     end
  80.   end
  81.   -- turtle.select(selected_slot)
  82.   return false, "No item to match"
  83. end
  84.  
  85. -- スロット番号の小さいほうから探し、空っぽのスロットを選択する。
  86. function selectEmptySlot()
  87.   for i=1, 16 do
  88.     local item = turtle.getItemDetail(i)
  89.     if item == nil then
  90.       turtle.select(i)
  91.       return true
  92.     end
  93.   end
  94.   return false
  95. end
  96.  
  97. function changeItems(slot0, slot1, tmp_slot)
  98.   if turtle.getItemCount(tmp_slot)>0 then return false, "tmp_slot must be empty." end
  99.   if turtle.getItemCount(slot1)>0 then -- 移動先が埋まっている
  100.     turtle.select(slot1)
  101.     turtle.transferTo(tmp_slot) -- slot1のアイテムを退避
  102.     turtle.select(slot0)
  103.     turtle.transferTo(slot1) -- slot0のアイテムをslot1に移動
  104.     turtle.select(tmp_slot)
  105.     turtle.transferTo(slot0) -- 退避スロットのアイテムをslot1に移動
  106.   else
  107.     turtle.select(slot0)
  108.     turtle.transferTo(slot1) -- slot0のアイテムをslot1に移動
  109.   end
  110. end
  111.  
  112. -- sortInventory()
  113. function sortInventory()
  114.   local tmp_slot = 16
  115.   collectItem() -- まずは整理しようね。
  116.   -- スロット16(tmp_slot)があいてない場合はエラー返して終了するよ
  117.   if turtle.getItemCount(tmp_slot) > 0 then return false, "Slot "..tmp_slot.." must be empty." end
  118.  
  119.   -- インベントリアイテムの情報を収集するよ
  120.   local items = {}
  121.   for i=1,15 do
  122.     local item = turtle.getItemDetail(i)
  123.     if item then -- スロットにアイテムあります。
  124.       local item_label = item["name"]..(item["damage"]<10 and "0"..item["damage"] or item["damage"])
  125.       -- "minecraft:dirt00" のように名前とダメージ値を連結したラベルを作って集めるよ。
  126.       table.insert(items, item_label)
  127.     else
  128.       -- スロットは空です。
  129.     end
  130.   end
  131.   table.sort(items) -- 文字列順にソートじゃい。
  132.  
  133.   for i,sort_item_label in ipairs(items) do
  134.     sort_item_name = string.sub(sort_item_label, 1,-3) -- 文字列の、頭~後ろから3番目まで取り出すと名前
  135.     sort_item_damage = tonumber(string.sub(sort_item_label, -2)) -- 文字列の、後ろから3番目~最後まで取り出すとダメージ値
  136.     selectItem(sort_item_name, sort_item_damage)
  137.     local slot0 = turtle.getSelectedSlot()
  138.     --print(slot0..'->'..i)
  139.     changeItems(slot0, i, tmp_slot)
  140.   end
  141. end
  142.  
  143. print(sortInventory())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement