Advertisement
Guest User

Untitled

a guest
Jan 19th, 2013
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- This program is for OpenCCSensors 0.1.2
  2.  
  3. os.loadAPI("ocs/apis/sensor")
  4. local sides = rs.getSides();
  5.  
  6. local width = 0
  7. local height = 0
  8. local monitor = nil
  9.  
  10. local function findPeripheralSide(type)
  11.   for key, side in pairs(sides) do
  12.     if peripheral.isPresent(side) and peripheral.getType(side) == type then
  13.       return side;
  14.     end
  15.   end
  16. end
  17.  
  18. local function findMonitor()
  19.   local side = findPeripheralSide("monitor")
  20.   if side then
  21.     return peripheral.wrap(side)
  22.   end
  23. end
  24.  
  25. local function findSensor()
  26.   local side = findPeripheralSide("sensor")
  27.   if side then
  28.     return sensor.wrap(side)
  29.   end
  30. end
  31.  
  32.  
  33. local function getColorIndex(x, y)
  34.   return ((x-1) + (y-1) * 128) + 1
  35. end
  36.  
  37. local function drawPixel(x, y, color)
  38.   monitor.setCursorPos(x, y)
  39.   monitor.setBackgroundColor(color)
  40.   monitor.write(" ")
  41. end
  42.  
  43. local function displayMapColors(colors)
  44.   for x=1, width do
  45.     for y=1, height do
  46.       local index = getColorIndex(x, y)
  47.       local color = colors[index]
  48.       drawPixel(x, y, color)
  49.     end
  50.   end
  51. end
  52.  
  53. local function findPossibleTargets(sen)
  54.   local maps = {};
  55.   for key, data in pairs(sen.getTargets()) do
  56.     local details = sen.getTargetDetails(key)
  57.     for slot, slotd in pairs(details.Slots) do
  58.       if slotd.Name == "item.map" then
  59.         table.insert(maps, { target = key, slot = slot })      
  60.       end
  61.     end
  62.   end
  63.   return maps;
  64. end
  65.  
  66. while true do
  67.  
  68.   monitor = findMonitor();
  69.   if monitor == nil then
  70.     error("no monitor attached")
  71.   end
  72.  
  73.   width, height = monitor.getSize()
  74.  
  75.   local inventory = findSensor()
  76.   if inventory == nil then
  77.     error("no sensor attached")
  78.   end
  79.  
  80.   local possibleMaps = findPossibleTargets(inventory)
  81.   local firstMap = table.remove(possibleMaps, 1)
  82.  
  83.   if firstMap ~= nil then
  84.  
  85.     local width, height = monitor.getSize()
  86.    
  87.     local mapData = inventory.sensorCardCall("getMapData", firstMap.target, firstMap.slot)
  88.    
  89.     displayMapColors(mapData.Colors)
  90.  
  91.   end
  92.  
  93.   sleep(0.1)
  94.  
  95. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement