Advertisement
joebodo

beeBreeding.lua

May 5th, 2014
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.90 KB | None | 0 0
  1. _G.requireInjector(_ENV)
  2.  
  3. local Event = require('event')
  4. local UI = require('ui')
  5. local Util = require('util')
  6.  
  7. local chest = peripheral.wrap('bottom')
  8.  
  9. local data
  10. local monitor = UI.Device({
  11.   deviceType = 'monitor',
  12.   textScale = .5
  13. })
  14.  
  15. UI:setDefaultDevice(monitor)
  16.  
  17. local breedingPage = UI.Page({
  18.   titleBar = UI.TitleBar(),
  19.   grid = UI.Grid({
  20.     columns = {
  21.       { heading  = '  ', key = 'chance' },
  22.       { heading = 'Princess', key = 'princess', },
  23.       { heading = 'Drone', key = 'drone' },
  24.       { heading = 'Result', key = 'result', },
  25.     },
  26.     y = 2, ey = -8,
  27.     sortColumn = 'result',
  28.     autospace = true
  29.   }),
  30.   specialConditions = UI.Window({
  31.     backgroundColor = colors.red,
  32.     y = -7,
  33.     height = 2
  34.   }),
  35.   buttons = UI.Window({
  36.     y = monitor.height - 4,
  37.     width = monitor.width,
  38.     height = 5,
  39.     backgroundColor = colors.gray,
  40.     prevButton = UI.Button({
  41.       event = 'previous',
  42.       x = 2,
  43.       y = 2,
  44.       height = 3,
  45.       width = 5,
  46.       text = ' < '
  47.     }),
  48.     resetButton = UI.Button({
  49.       event = 'clear',
  50.       x = 8,
  51.       y = 2,
  52.       height = 3,
  53.       width = monitor.width - 14,
  54.       text = 'Clear'
  55.     }),
  56.     nextButton = UI.Button({
  57.       event = 'next',
  58.       x = monitor.width - 5,
  59.       y = 2,
  60.       height = 3,
  61.       width = 5,
  62.       text = ' > '
  63.     })
  64.   })
  65. })
  66.  
  67. function breedingPage:getBreedingData()
  68.   self.grid.values = { }
  69.   local stacks = chest.getAllStacks(false)
  70.   local stack = stacks[1]
  71.   self.titleBar.title = stack.individual.displayName
  72.   for _,d in pairs(data) do
  73.     if d.allele1 == stack.individual.displayName or
  74.        d.allele2 == stack.individual.displayName then
  75.       local ind = ''
  76.       if d.specialConditions then
  77.         ind = '*'
  78.       end
  79.       table.insert(self.grid.values, {
  80.         princess = d.allele1 .. ind,
  81.         drone = d.allele2,
  82.         result = d.result,
  83.         chance = d.chance .. '%',
  84.         specialConditions = d.specialConditions
  85.       })
  86.     end
  87.   end
  88.   self.grid.index = 1
  89.   self.grid:adjustWidth()
  90.   self.grid:update()
  91.   self:draw()
  92.   self:sync()
  93. end
  94.  
  95. function breedingPage.specialConditions:draw()
  96.   local selected = self.parent.grid:getSelected()
  97.   if selected then
  98.     local sc = ''
  99.     if selected.specialConditions then
  100.       for _,v in ipairs(selected.specialConditions) do
  101.         if sc ~= '' then
  102.           sc = sc .. ', '
  103.         end
  104.         sc = sc .. v
  105.       end
  106.     end
  107.     self:clear()
  108.     self:setCursorPos(2, 1)
  109.     self:print(sc)
  110.   else
  111.     self:clear(colors.black)
  112.   end
  113. end
  114.  
  115. function breedingPage.grid:draw()
  116.   UI.Grid.draw(self)
  117.   self.parent.specialConditions:draw()
  118. end
  119.  
  120. function breedingPage:eventHandler(event)
  121.   if event.type == 'next' then
  122.     self.grid:setPage(self.grid:getPage() + 1)
  123.   elseif event.type == 'previous' then
  124.     self.grid:setPage(self.grid:getPage() - 1)
  125.   elseif event.type == 'clear' then
  126.     self.grid:setTable({})
  127.     self.grid:draw()
  128.   else
  129.     return UI.Page.eventHandler(self, event)
  130.   end
  131.   return false
  132. end
  133.  
  134. Event.on('turtle_inventory', function()
  135.   local slot = turtle.selectSlotWithQuantity(1)
  136.  
  137.   if slot then
  138.     turtle.dropDown()
  139.     breedingPage:getBreedingData()
  140.     turtle.suckDown()
  141.     turtle.drop()
  142.   end
  143.  
  144. end)
  145.  
  146. if not fs.exists('.bee.data') then
  147.   local p = peripheral.wrap("back")
  148.   local data = p.getBeeBreedingData()
  149.   local t = { }
  150.   for _,d in pairs(data) do
  151.     d = Util.shallowCopy(d)
  152.     if type(d.specialConditions) == 'string' then
  153.       if d.specialConditions == '[]' then
  154.         d.specialConditions = ''
  155.       end
  156.     end
  157.     if #d.specialConditions == 0 then
  158.       d.specialConditions = nil
  159.     else
  160.       d.specialConditions = Util.shallowCopy(d.specialConditions)
  161.     end
  162.     table.insert(t, d)
  163.   end
  164.   Util.writeTable('.bee.data', t)
  165. else
  166.   data = Util.readTable('.bee.data')
  167. end
  168.  
  169. UI:setPage(breedingPage)
  170.  
  171. UI:pullEvents()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement