Guest User

controller

a guest
Jun 21st, 2013
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.44 KB | None | 0 0
  1. os.loadAPI("ocs/apis/sensor")
  2. rednet.open("left")
  3. local prox = sensor.wrap("right")
  4.  
  5. local ghostnames =
  6. {
  7. [6] = "Blinky",
  8. [11] = "Pinky",
  9. [12] = "Inky",
  10. [13] = "Clyde"
  11. }
  12.  
  13. local ghosts =
  14. {
  15.     ["Blinky"] = {},
  16.     ["Pinky"]= {},
  17.     ["Inky"] = {},
  18.     ["Clyde"] = {}
  19. }
  20.  
  21. ghosts["Blinky"].state = "scatter"
  22. ghosts["Blinky"].X = 14
  23. ghosts["Blinky"].Y = 12
  24. ghosts["Pinky"].state = "scatter"
  25. ghosts["Pinky"].X = 14
  26. ghosts["Pinky"].Y = 12
  27. ghosts["Inky"].state = "scatter"
  28. ghosts["Inky"].X = 14
  29. ghosts["Inky"].Y = 12
  30. ghosts["Clyde"].state = "scatter"
  31. ghosts["Clyde"].X = 14
  32. ghosts["Clyde"].Y = 12
  33.  
  34. local playername = "mattivapa"
  35.  
  36. local data = {}
  37. data.X = 14
  38. data.Y = 24
  39. data.dX = -1
  40. data.dY = 0
  41. data.gold = 0
  42. data.diamonds = 0
  43.  
  44. local score = 0
  45.  
  46. local offset = {}
  47. offset.X = 20
  48. offset.Z = 14
  49.  
  50. local facings = {{-1,0},{0,1},{1,0},{0,-1}}
  51.  
  52. local yawToFacing = function(yaw)
  53.   local ind = math.floor(math.fmod(math.abs(yaw)+45,360)/90)
  54.   return facings[ind+1]
  55. end
  56.  
  57. local worldToGame = function(X,Z)
  58.   return {offset.Z+Z,offset.X-X}  
  59. end
  60.  
  61. local updateData = function()
  62. --  print(1)
  63.   local details = prox.getTargetDetails(playername)
  64. --  print(2)
  65.   if details ~= nil then
  66.     local pos = worldToGame(details.Position.X,details.Position.Z)
  67.     local facing = yawToFacing(details.Yaw)
  68.     data.X = pos[1]
  69.     data.Y = pos[2]
  70.     data.dX = facing[1]
  71.     data.dY = facing[2]
  72.     local g = 0
  73.     local d = 0
  74.     for k,v in pairs(details.Inventory) do
  75.       if v["Name"] == "Diamond" then
  76.         d = d+v["Size"]
  77.       elseif v["Name"] == "Gold Ore" then
  78.         g = g+v["Size"]
  79.       end
  80.     end
  81.     if g > data.gold then
  82.       data.gold = g
  83.       score = score + 10*g
  84.       print("Score: "..tostring(score))
  85.     end
  86.     if d > data.diamonds then
  87.       data.diamonds = d
  88.       score = score + 50*d
  89.       print("Score: "..tostring(score))
  90.     end
  91.   end
  92. end
  93.  
  94. print("Game starting, get to the starting position")
  95. sleep(5)
  96. print("Sending start message...")
  97. rednet.broadcast("Begin!")
  98.  
  99. while true do
  100.   updateData()
  101.   local id, message, dist = rednet.receive(0.2)
  102.   local ghost = ghostnames[id]
  103.   if ghost ~= nil then
  104.     message = textutils.unserialize(message)
  105.     ghosts[ghost].X = message.X
  106.     ghosts[ghost].Y = message.Y
  107.     if message.state == "dead" and ghosts[ghost].state ~= "dead" then
  108.         score = score + 400
  109.     end
  110.     ghosts[ghost].state = message.state
  111.     rednet.send(id,textutils.serialize(data))
  112.   end
  113. end
Advertisement
Add Comment
Please, Sign In to add comment