Guest User

controller

a guest
Jun 19th, 2013
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.70 KB | None | 0 0
  1. os.loadAPI("ocs/apis/sensor")
  2. rednet.open("left")
  3. local prox = sensor.wrap("right")
  4.  
  5. local playername = "mattivapa"
  6.  
  7. local player = {}
  8. player.X = 14
  9. player.Y = 24
  10. player.dX = -1
  11. player.dY = 0
  12. player.energize = false
  13.  
  14. local gold = 0
  15. local diamonds = 0
  16.  
  17. local offset = {}
  18. offset.X = 20
  19. offset.Z = 14
  20.  
  21. local facings = {{-1,0},{0,1},{1,0},{0,-1}}
  22.  
  23. local yawToFacing = function(yaw)
  24.   local ind = math.floor(math.fmod(math.abs(yaw)+45,360)/90)
  25.   return facings[ind+1]
  26. end
  27.  
  28. local worldToGame = function(X,Z)
  29.   return {offset.Z+Z,offset.X-X}  
  30. end
  31.  
  32. local updatePlayerPos = function(p)
  33. --  print(1)
  34.   local details = prox.getTargetDetails(playername)
  35. --  print(2)
  36.   if details ~= nil then
  37.     local pos = worldToGame(details.Position.X,details.Position.Z)
  38.     local facing = yawToFacing(details.Yaw)
  39.     p.X = pos[1]
  40.     p.Y = pos[2]
  41.     p.dX = facing[1]
  42.     p.dY = facing[2]
  43.     local g = 0
  44.     local d = 0
  45.     for k,v in pairs(details.Inventory) do
  46.       if v["Name"] == "Diamond" then
  47.         d = d+v["Size"]
  48.       elseif v["Name"] == "Gold Ore" then
  49.         g = g+v["Size"]
  50.       end
  51.     end
  52.     if g > gold then
  53.       gold = g
  54.       print("Score: "..tostring(10*gold+50*diamonds))
  55.     end
  56.     if d > diamonds then
  57.       diamonds = d
  58.       print("Score: "..tostring(10*gold+50*diamonds))
  59.       player.energize = true
  60.     end
  61.   end
  62. end
  63.  
  64.  
  65. while true do
  66.   updatePlayerPos(player)
  67. --print("Sending:")
  68. --print(textutils.serialize(player))
  69.   rednet.broadcast(textutils.serialize(player))
  70.   if player.energize == true then
  71.     player.energize = false
  72.     rednet.broadcast(textutils.serialize(player))
  73.     rednet.broadcast(textutils.serialize(player))
  74.   end
  75.   sleep(0.1)
  76. end
Advertisement
Add Comment
Please, Sign In to add comment