# CC: Tweaked API

```lua
startup.lua:
local side = "back"

if not peripheral.isPresent(side) then
  print("No peripheral on "..side)
  return
end

local pType = peripheral.getType(side)

print("Detected:", pType)

if pType == "chicken_trainer" then
  shell.run("trainer.lua")
elseif pType == "chicken_roost" then
  shell.run("roost.lua")
elseif pType == "chicken_breeder" then
  shell.run("breeder.lua")
elseif pType == "chicken_soul_extractor" then
  shell.run("extractor.lua")
else
  print("Unknown machine:", pType)
end


ui.lua:
local UI = {}

function UI.box(mon,x,y,w,h,c)
  mon.setBackgroundColor(c)
  for i=0,h-1 do
    mon.setCursorPos(x,y+i)
    mon.write(string.rep(" ",w))
  end
  mon.setBackgroundColor(colors.black)
end

function UI.text(mon,x,y,t,c)
  mon.setCursorPos(x,y)
  mon.setTextColor(c or colors.white)
  mon.write(t)
  mon.setTextColor(colors.white)
end

function UI.progress(mon,x,y,w,p)
  p = math.max(0, math.min(1,p))
  local fill = math.floor(w*p)

  mon.setBackgroundColor(colors.gray)
  mon.setCursorPos(x,y)
  mon.write(string.rep(" ",w))

  mon.setBackgroundColor(colors.lime)
  mon.setCursorPos(x,y)
  mon.write(string.rep(" ",fill))

  mon.setBackgroundColor(colors.black)
end

function UI.inside(btn,x,y)
  return x>=btn.x and x<btn.x+btn.w
     and y>=btn.y and y<btn.y+btn.h
end

return UI


common.lua:
local C = {}

function C.newCache()
  return {
    lvl=-1, xp=-1, prog=-1, max=-1,
    ao=nil, alvl=-1
  }
end

function C.safe(v,d)
  v = tonumber(v)
  if v == nil then return d end
  return v
end

return C


breeder.lua
local ui = require("ui")
local mon = peripheral.wrap("top")
local e   = peripheral.wrap("back")

mon.setTextScale(0.5)
mon.clear()

ui.text(mon,2,1,e.getName(),colors.orange)

while true do
  ui.text(mon,2,3,"Has Chicken: "..tostring(e.hasChicken()))
  ui.text(mon,2,4,"Working    : "..tostring(e.isWorking()))

  ui.progress(mon,2,6,20,e.getProgress()/math.max(1,e.getMaxProgress()))
  sleep(0.2)
end


extractor.lua:
local s = peripheral.wrap("back")
local mon = peripheral.wrap("top")

mon.setTextScale(0.8)
local BTN = { x=2, y=9, w=16, h=3 }

local function box(x,y,w,h,c)
  mon.setBackgroundColor(c)
  for i=0,h-1 do
    mon.setCursorPos(x,y+i)
    mon.write(string.rep(" ",w))
  end
  mon.setBackgroundColor(colors.black)
end

local function text(x,y,t,c)
  mon.setCursorPos(x,y)
  mon.setTextColor(c or colors.white)
  mon.write(t)
end

local function draw()
  mon.clear()
  text(2,1,s.getName(),colors.orange)

  text(2,3,"Chicken: "..tostring(s.hasChicken()),
       s.hasChicken() and colors.lime or colors.red)

  box(2,5,20,1,s.isWorking() and colors.lime or colors.gray)

  local ao = s.getAutoOutput()
  box(BTN.x,BTN.y,BTN.w,BTN.h,ao and colors.lime or colors.red)
  text(BTN.x+3,BTN.y+1,ao and "AutoOutput ON" or "AutoOutput OFF",colors.black)

  text(2,13,os.date("%H:%M:%S"),colors.gray)
end

draw()
while true do
  local e,_,x,y = os.pullEvent()
  if e=="monitor_touch"
     and x>=BTN.x and x<BTN.x+BTN.w
     and y>=BTN.y and y<BTN.y+BTN.h then
       s.setAutoOutput(not s.getAutoOutput())
       draw()
  elseif e=="timer" then draw() end
  sleep(1)
end


trainer.lua:
local ui = require("ui")
local c  = require("common")

local t   = peripheral.wrap("back")
local mon = peripheral.wrap("top")

mon.setTextScale(0.5)
mon.setBackgroundColor(colors.black)

local AO_BTN  = {x=2,y=12,w=20,h=3}
local LVL_DEC = {x=2,y=8,w=4,h=3}
local LVL_INC = {x=18,y=8,w=4,h=3}
local LVL_MID = {x=6,y=8,w=12,h=3}

local cache = c.newCache()

local function drawStatic()
  mon.clear()
  ui.text(mon,2,1,t.getName(),colors.orange)
  ui.text(mon,2,3,"Level:")
  ui.text(mon,2,4,"XP:")
  ui.text(mon,2,6,"Progress")

  ui.box(mon,LVL_DEC.x,LVL_DEC.y,LVL_DEC.w,LVL_DEC.h,colors.red)
  ui.text(mon,LVL_DEC.x+1,LVL_DEC.y+1,"-")

  ui.box(mon,LVL_INC.x,LVL_INC.y,LVL_INC.w,LVL_INC.h,colors.green)
  ui.text(mon,LVL_INC.x+1,LVL_INC.y+1,"+")
end

local function update()
  local lvl  = t.getChickenLevel()
  local xp   = t.getChickenXp()
  local p    = t.getProgress()
  local max  = math.max(1, t.getMaxProgress())
  local ao   = t.getAutoOutput()
  local alvl = t.getAutoOutputLevel()

  if lvl~=cache.lvl then
    ui.text(mon,10,3,string.rep(" ",6))
    ui.text(mon,10,3,lvl)
    cache.lvl=lvl
  end

  if xp~=cache.xp then
    ui.text(mon,10,4,string.rep(" ",6))
    ui.text(mon,10,4,xp)
    cache.xp=xp
  end

  if p~=cache.prog or max~=cache.max then
    ui.progress(mon,2,7,20,p/max)
    cache.prog=p
    cache.max=max
  end

  if alvl~=cache.alvl then
    ui.box(mon,LVL_MID.x,LVL_MID.y,LVL_MID.w,LVL_MID.h,colors.cyan)
    ui.text(mon,LVL_MID.x+1,LVL_MID.y+1,"AutoLvl:"..alvl,colors.black)
    cache.alvl=alvl
  end

  if ao~=cache.ao then
    ui.box(mon,AO_BTN.x,AO_BTN.y,AO_BTN.w,AO_BTN.h,ao and colors.lime or colors.red)
    ui.text(mon,AO_BTN.x+2,AO_BTN.y+1,ao and "AutoOutput ON" or "AutoOutput OFF",colors.black)
    cache.ao=ao
  end
end

drawStatic()
update()

local timer=os.startTimer(0.15)

while true do
  local e,p1,x,y = os.pullEvent()

  if e=="timer" and p1==timer then
    update()
    timer=os.startTimer(0.15)

  elseif e=="monitor_touch" then
    if ui.inside(LVL_DEC,x,y) then
      t.setAutoOutputLevel(math.max(0,t.getAutoOutputLevel()-1))
    elseif ui.inside(LVL_INC,x,y) then
      t.setAutoOutputLevel(t.getAutoOutputLevel()+1)
    elseif ui.inside(AO_BTN,x,y) then
      t.setAutoOutput(not t.getAutoOutput())
    end
    update()
  end
end


roost.lua:
local ui = require("ui")
local mon = peripheral.wrap("top")
local r   = peripheral.wrap("back")

mon.setTextScale(0.5)
mon.clear()

ui.text(mon,2,1,r.getName(),colors.orange)

while true do
  ui.text(mon,2,3,"Level: "..r.getChickenLevel())
  ui.text(mon,2,4,"XP: "..r.getChickenXp())

  ui.progress(mon,2,6,20,r.getProgress()/math.max(1,r.getMaxProgress()))
  sleep(0.2)
end

```
