Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- os.loadAPI("gcapi")
- print("NOTICE: The computer is not storing data of any kind so the player using it is not breaking the rules of the server. Regarding the tracking of players the program doesn't track staff players.")
- gb = peripheral.wrap("left")
- sensor = peripheral.wrap("back")
- --------------------------------------------
- --> Variables <--
- --------------------------------------------
- local playersRange = {} --{[player]=range}
- local lastPosition = {}
- local config = {}
- --------------------------------------------
- --> Tracking <--
- --------------------------------------------
- function updatePositions()
- while true do
- displayClosePlayers()
- sleep(1)
- end
- end
- function getPosPlayers()
- local dataPlayers = gcapi.getPosPlayers()
- if sensor then
- local xo = config.sensor_position.x
- local yo = config.sensor_position.y
- local zo = config.sensor_position.z
- for i=1,#dataPlayers do
- local player_data = dataPlayers[i]
- local player_name = player_data["name"]
- local sensor_data = sensor.getPlayerData(player_name)
- if sensor_data then
- dataPlayers[i].x = math.floor(xo + sensor_data.position.x)
- dataPlayers[i].y = math.floor(yo + sensor_data.position.y)
- dataPlayers[i].z = math.floor(zo + sensor_data.position.z)
- dataPlayers[i].world = "world"
- dataPlayers[i].updated_at = "Updated now"
- end
- end
- end
- return dataPlayers
- end
- function getClosePlayers(user, range)
- local playerPositions = getPosPlayers()
- local closePlayers = {}
- local player_data_1 = nil
- for _,player_data in pairs(playerPositions) do
- if player_data.name == user then
- player_data_1 = player_data
- end
- end
- if not player_data_1 then
- return nil, false, "Player not found"
- end
- local xo = player_data_1.x
- local yo = player_data_1.y
- local zo = player_data_1.z
- for _,player_data in pairs(playerPositions) do
- local name = player_data.name
- if name ~= user then
- local distance_v = {["x"] = (player_data.x - xo),["y"] = (player_data.y - yo),["z"] = (player_data.z - zo)}
- local distance = (distance_v.y^2+distance_v.x^2 + distance_v.z^2)^0.5
- if distance <= range then
- table.insert(closePlayers, {["name"]= name, ["distance"] = math.floor(distance)})
- end
- end
- end
- table.sort(closePlayers, function(a,b) return a.distance<b.distance end)
- return closePlayers, true, nil
- end
- --------------------------------------------------------
- --> Setup File <--
- --------------------------------------------------------
- function readConfigFile(cfg)
- local list = gcapi.getListFromFile("/config.cfg")
- for key,value in pairs(list) do
- cfg[key] = value
- end
- return cfg
- end
- function saveConfigFile(cfg)
- gcapi.saveListToFile("/config.cfg", cfg)
- end
- --------------------------------------------------------
- --> Manual Setup <--
- --------------------------------------------------------
- config = readConfigFile(config)
- if sensor and config.sensor_position == nil then
- local sensor_position = {}
- print("Where is the sensor?")
- write("X: ")
- sensor_position.x = read():gsub(' ','')
- write("Y: ")
- sensor_position.y = read():gsub(' ','')
- write("Z: ")
- sensor_position.z = read():gsub(' ','')
- config.sensor_position = sensor_position
- saveConfigFile(config)
- end
- config = readConfigFile(config)
- --------------------------------------------
- --> Glasses <--
- --------------------------------------------
- function displayClosePlayers()
- for _,user in pairs(gb.getUsers()) do
- local range = playersRange[user]
- if not range then
- range = 600
- end
- local closePlayers, ok, err = getClosePlayers(user, range)
- if ok then
- local glasses = gb.getUserSurface(user)
- glasses.clear()
- for i=1, #closePlayers, 1 do
- local playerData = closePlayers[i]
- if playerData then
- local player = playerData["name"]
- local distance = playerData["distance"]
- glasses.addText(10,10+10*i, player .. " - " .. distance .. "[m]")
- end
- end
- end
- end
- end
- --------------------------------------------
- --> Commands <--
- --------------------------------------------
- function listener()
- while true do
- local tEvent = {os.pullEvent()}
- if tEvent[1] == "chat_command" then
- co = tostring(tEvent[2])
- player = tostring(tEvent[3])
- print(player..": "..co)
- if tonumber(co) then
- playersRange[player] = tonumber(co)
- end
- end
- end
- end
- --------------------------------------------
- --> Main program <--
- --------------------------------------------
- parallel.waitForAny(listener, updatePositions)
Add Comment
Please, Sign In to add comment