Biopsy

Biolisten 1.0

Apr 27th, 2012
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.91 KB | None | 0 0
  1. rednet.open("left")
  2. maxx, maxy = term.getSize()
  3. xscale=0
  4. yscale=0
  5. xbounds = {}
  6. ybounds = {}
  7. entities = {}
  8.  
  9. function termwrite(message)
  10.   cx, cy = term.getCursorPos()
  11.   mx, my = term.getSize()
  12.  
  13.   if(cy == my) then
  14.     term.scroll(1)
  15.     term.setCursorPos(1, cy)
  16.   else
  17.     term.setCursorPos(1, cy + 1)
  18.   end
  19.  
  20.   term.write(message)
  21. end
  22.  
  23. function parsedraw(message)
  24.   separated=textutils.unserialize(message)
  25.   id=separated[1]
  26.   category=separated[2]
  27.   position=textutils.unserialize(separated[3])
  28.   updatebounds(position)
  29.   updateentity(id, category, position)
  30.   turtledraw()
  31. end
  32.  
  33. function updateentity(id, category, position)
  34.   local s=category
  35.   if s == "T" then
  36.     if    (position[4]==0) then s="v"
  37.     elseif(position[4]==1) then s="<"
  38.     elseif(position[4]==2) then s="^"
  39.     elseif(position[4]==3) then s=">" end
  40.   end
  41.  
  42.   local coords={position[1],position[2],s}
  43.   entities[id] = coords
  44. end
  45.  
  46. function updatebounds(position)
  47.   if(xbounds[1] == null) or (xbounds[1] > position[1]) then
  48.     xbounds[1] = position[1]
  49.   end
  50.   if(xbounds[2] == null) or (xbounds[2] < position[1]) then
  51.     xbounds[2] = position[1]
  52.   end
  53.   if(ybounds[1] == null) or (ybounds[1] > position[2]) then
  54.     ybounds[1] = position[2]
  55.   end
  56.   if(ybounds[2] == null) or (ybounds[2] < position[2]) then
  57.     ybounds[2] = position[2]
  58.   end
  59.   xscale=xbounds[2]-xbounds[1]
  60.   yscale=ybounds[2]-ybounds[1]
  61. end
  62.  
  63. function calccp(x, y)
  64.   xcp = math.ceil(((x-xbounds[1])/xscale)*(maxx-1))+1
  65.   ycp = math.ceil(((y-ybounds[1])/yscale)*(maxy-1))+1
  66.   return xcp, ycp
  67. end
  68.  
  69. function turtledraw()
  70.   term.clear()
  71.   for id, position in pairs(entities) do
  72.     x,y = calccp(position[1],position[2])
  73.     term.setCursorPos(x,y)
  74.     term.write(position[3])
  75.   end
  76. end
  77.  
  78. term.clear()
  79. term.setCursorPos(1,0)
  80. termwrite("Listening...")
  81.  
  82. while true do
  83.   id, msg = rednet.receive()
  84.   if id ~= null then
  85.     parsedraw(msg)
  86.   end
  87. end
Advertisement
Add Comment
Please, Sign In to add comment