Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- Server for the testgame
- ]]
- local socket = require "socket"
- local udp = socket.udp()
- local OBJECT_WIDTH = 159
- print(tostring(socket))
- udp:settimeout(0)
- udp:setsockname("*", 12345)
- local world = {
- [1] = {
- type = "metal_block",
- x = 0,
- y = 0
- },
- [2] = {
- type = "metal_block",
- x = OBJECT_WIDTH,
- y = 0
- },
- [3] = {
- type = "metal_block",
- x = OBJECT_WIDTH*2,
- y = 0,
- },
- [4] = {
- type = "metal_block",
- x = OBJECT_WIDTH*2,
- y = OBJECT_WIDTH*3
- }
- }
- local data, msg_or_ip, port_or_nil
- local entity, cmd, parms
- local running = true
- print("Beginning server loop.")
- while running do
- data, msg_or_ip, port_or_nil = udp:receivefrom()
- if data then
- entity, cmd, parms = data:match("^(%S*) (%S*) (.*)")
- if cmd == "move" then
- local x, y = parms:match("^(%-?[%d.e]*) (%-?[%d.e]*)$")
- assert(x and y)
- x, y = tonumber(x), tonumber(y)
- local ent = world[entity] or {x=0, y=0}
- world[entity] = {x=ent.x+x, y=ent.y+y, type=ent.type}
- elseif cmd == "at" then
- local x, y, typ = parms:match("^(%-?[%d.e]*) (%-?[%d.e]*) (%S*)")
- print("parms: "..parms.." Typ: "..typ)
- assert(x and y and typ)
- x, y = tonumber(x), tonumber(y)
- world[entity] = {x=x, y=y, type=typ}
- world[entity].type = tostring(typ)
- print(tostring(entity).." "..tostring(world[entity].type))
- elseif cmd == "update" then
- for k, v in pairs(world) do
- print("ITEM: "..tostring(k).." "..tostring(v.type))
- udp:sendto(string.format("%s %s %d %d %s", k, 'at', v.x, v.y, v.type), msg_or_ip, port_or_nil)
- end
- elseif cmd == "quit" then
- running = false
- else
- print("Unrecognized command: ", cmd)
- end
- elseif msg_or_ip ~= "timeout" then
- error("Unknown network error: "..tostring(msg))
- end
- socket.sleep(0.001)
- end
- print("Thank you.")
Advertisement
Add Comment
Please, Sign In to add comment