Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save EncodeTheCode/93145ad89538a6449a05cfb968bbd3f0 to your computer and use it in GitHub Desktop.
Save EncodeTheCode/93145ad89538a6449a05cfb968bbd3f0 to your computer and use it in GitHub Desktop.
local socket = require("socket")
-- create a TCP server and bind it to port 12345
local server = socket.tcp()
server:bind("*", 12345)
server:listen()
-- set the server to non-blocking mode
server:settimeout(0)
-- initialize variables for the turret angle and cursor position
local turretAngle = 0
local cursorX, cursorY = 0, 0
-- loop indefinitely
while true do
-- accept any incoming client connections
local client = server:accept()
-- if a client connected, read its input
if client then
local input = client:receive()
-- if the input is the escape key, exit the program
if input == "\27" then
break
end
-- otherwise, update the turret angle based on the cursor position
if input == "up" then
turretAngle = math.atan2(cursorY - 1, cursorX) * 180 / math.pi
elseif input == "down" then
turretAngle = math.atan2(cursorY + 1, cursorX) * 180 / math.pi
elseif input == "left" then
turretAngle = math.atan2(cursorY, cursorX - 1) * 180 / math.pi
elseif input == "right" then
turretAngle = math.atan2(cursorY, cursorX + 1) * 180 / math.pi
end
-- send the updated turret angle back to the client
client:send(tostring(turretAngle) .. "\n")
-- close the client connection
client:close()
end
-- update the cursor position (in this example, cursorX and cursorY are updated externally)
cursorX, cursorY = getCursorPosition()
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment