Medar

pickup_one.lua

Sep 15th, 2015
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.94 KB | None | 0 0
  1. -- Autopickup single one of an item based on subtype or basename.
  2. --  (aux armours, buckler/shield, blowgun etc.)
  3. -- Smart needle pickup. (Until blowgun is found, and while kept in inventory.)
  4.  
  5. -- Doesn't work if item's name/subtype isn't known before picking it up.
  6. -- Ignores useless items.
  7.  
  8. -- -- Config
  9.  
  10. local pickup_one_subtypes = { "cloak", "boots", "helmet", "gloves" }
  11. local pickup_one_basenames = { "blowgun", "buckler" }
  12. local pickup_needles_smart = true
  13.  
  14. if you.genus() == "troll" or you.genus() == "ogre" then
  15.     table.insert(pickup_one_basenames, "shield")
  16. end
  17.  
  18. -- -- Helper functions
  19.  
  20. local function Set(list)
  21.     local set = {}
  22.     for _, l in ipairs(list) do set[l] = true end
  23.     return set
  24. end
  25.  
  26. local function get_subtype(item)
  27.     local subtype, _ = item.subtype()
  28.     return subtype
  29. end
  30.  
  31. local function get_basename(item)
  32.     if item.artefact then
  33.         local name = item.name("base")
  34.         return name:gsub(" \".-\"$", "")
  35.     else
  36.         return item.name("base")
  37.     end
  38. end
  39.  
  40. local function inventory_match(item_func, value)
  41.     for item in iter.invent_iterator:new(items.inventory()) do
  42.         if item_func(item) == value then
  43.             return item
  44.         end
  45.     end
  46.     return nil
  47. end
  48.  
  49. local function check_item(item)
  50.     if pickup_one_subtypes[get_subtype(item)] then
  51.         c_persist.pickup_one.got_subtype[get_subtype(item)] = true
  52.     end
  53.     if get_basename(item) == "blowgun" or
  54.         pickup_one_basenames[get_basename(item)] then
  55.         c_persist.pickup_one.got_basename[get_basename(item)] = true
  56.     end
  57. end
  58.  
  59. -- -- Interfacing with the game
  60.  
  61. local initialized = false
  62.  
  63. local function pickup_one(item, name)
  64.     if not initialized then return end
  65.  
  66.     if item.is_useless then
  67.         return false
  68.     end
  69.  
  70.     if pickup_one_subtypes[get_subtype(item)] and
  71.         not c_persist.pickup_one.got_subtype[get_subtype(item)] then
  72.         return true
  73.     end
  74.  
  75.     if pickup_one_basenames[get_basename(item)] and
  76.         not c_persist.pickup_one.got_basename[get_basename(item)] then
  77.         return true
  78.     end
  79.  
  80.     if pickup_needles_smart and name:find("needle") then
  81.         if not c_persist.pickup_one.got_basename["blowgun"] or
  82.             inventory_match(get_basename, "blowgun") then
  83.             return true
  84.         end
  85.     end
  86. end
  87. add_autopickup_func(pickup_one)
  88.  
  89. function c_assign_invletter(item)
  90.     check_item(item)
  91. end
  92.  
  93. function ready()
  94.     if not initialized then
  95.         pickup_one_subtypes = Set(pickup_one_subtypes)
  96.         pickup_one_basenames = Set(pickup_one_basenames)
  97.         if you.turns() == 0 or c_persist.pickup_one == nil then
  98.             c_persist.pickup_one = {}
  99.             c_persist.pickup_one.got_subtype = {}
  100.             c_persist.pickup_one.got_basename = {}
  101.             for item in iter.invent_iterator:new(items.inventory()) do
  102.                 check_item(item)
  103.             end
  104.         end
  105.         initialized = true
  106.     end
  107. end
Advertisement
Add Comment
Please, Sign In to add comment