Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- "Naval", ship warfare strategy game. because.
- ]]
- local mods = {
- }
- local tAPIsLoading = {}
- local function loadMod(dir)
- if dir ~= nil then
- local _sPath = dir
- local sName = fs.getName( _sPath )
- local n = 1
- while tAPIsLoading[sName] == true and n < 300 do
- n = n + 1
- sleep(0)
- end
- if n < 300 then
- tAPIsLoading[sName] = true
- local tEnv = {}
- setmetatable( tEnv, { __index = _G } )
- local fnAPI, err = loadfile( _sPath )
- if fnAPI then
- setfenv( fnAPI, tEnv )
- fnAPI()
- else
- printError( err )
- tAPIsLoading[sName] = nil
- return nil
- end
- local tAPI = {}
- for k,v in pairs( tEnv ) do
- tAPI[k] = v
- end
- tAPIsLoading[sName] = nil
- return tAPI
- else
- return nil
- end
- else
- return nil
- end
- end
- if not fs.exists("multinet") then
- shell.run("pastebin get WGCM34TP multinet")
- end
- for k,v in ipairs(rs.getSides()) do
- rednet.open(v)
- end
- print("Enter server name:")
- local name = read()
- local server = multinet.createServer("navy#"..name, "right")
- server:addSData("width", 20)
- server:addSData("height", 18)
- local g_maps = {}
- local function genMap(name, ocean)
- local w = server.data.width
- local h = server.data.height
- local points = {}
- for i=1,math.random(6) do
- points[#points + 1] = {math.random(w), math.random(h)}
- end
- local g_map = {}
- for y=1,h do
- local line = ""
- for x=1,w do
- local ch = "~"
- if not ocean then ch = "#" end
- for i=1,#points do
- local p = points[i]
- local ds = {p[1]-x, p[2]-y}
- if ds[1] < 0 then ds[1] = ds[1]*-1 end
- if ds[2] < 0 then ds[2] = ds[2]*-1 end
- local dist = ds[1]+ds[2]
- if dist <= math.random(2,4) then
- if not ocean then ch = "~" else ch = "#" end
- end
- end
- line = line..ch
- end
- g_map[y] = line
- end
- g_maps[name] = g_map
- end
- print("Generating map..")
- genMap("map", true)
- print("Finished!")
- local maps = {"map"}
- local lMods = {
- {},
- {},
- {},
- {},
- {}
- }
- if #mods > 0 then
- for i=1,#mods do
- local mod = loadMod(mods[i])
- if mod ~= nil then
- mod.startUp(server)
- print("Loaded mod: "..mod.getName())
- local modTab = lMods[mod.getLevel()]
- modTab[#modTab + 1] = mod
- else
- print("Failed to load mod at dir:")
- print(" "..mods[i])
- end
- sleep(0.1)
- end
- end
- local function update()
- for i=1,#maps do
- local mname = maps[i]
- local g_map = g_maps[mname]
- if g_map ~= nil then
- for y=1,server.data.height do
- local line = ""
- for x=1,server.data.width do
- local char = g_map[y]:sub(x, x)
- local ok = true
- for k,v in pairs(server:getUsers()) do
- if tonumber(v[2][1]) == x and tonumber(v[2][2]) == y and v[2][3] == mname then
- print(k..":MATCHED")
- char = "^"
- ok = false
- elseif v[2][1] ~= nil and v[2][2] ~= nil and ok then
- --print(v[2][1]..":"..x)
- --print(v[2][2]..":"..y)
- end
- end
- line = line..char
- end
- server:addSData(mname..":"..y, line)
- end
- end
- end
- local data = server.data
- local dat = multinet.formatComplexTable(data, "#@#", "#=#")
- if dat ~= nil then
- server:pingUsers("upt::"..dat)
- print("updated")
- end
- end
- update()
- local function callCommand(cmd)
- local splt = multinet.split(cmd, ":")
- local name = splt[1]
- if name == "gen" then
- local typ = tonubmer(splt[2])
- local ocean = (typ == 0)
- local world = ""
- world = multinet.formatTable(splt, " ", 3)
- genMap(world, typ)
- elseif name == "update" then
- update()
- end
- end
- local function getInput()
- return read()
- end
- while true do
- local omsg = server.data.msg
- local ocmd = server.data.cmd
- local result = server:waitForCommand()
- local isCMD = (type(result) == "number")
- local ok = true
- for i=1,#lMods do
- local modTab = lMods[i]
- for i=1,#modTab do
- local mod = modTab[i]
- local result, cmds, data = mod.update({server}, {isCMD, result}, {maps, g_maps})
- if data ~= nil then
- if data[1] ~= nil then
- if data[1][1] ~= nil then server = data[1][1] end
- end
- if data[2] ~= nil then
- if data[2][1] ~= nil then event = data[2][1] end
- if data[2][2] ~= nil then p1 = data[2][2] end
- if data[2][3] ~= nil then p2 = data[2][3] end
- if data[2][4] ~= nil then p3 = data[2][4] end
- end
- if data[3] ~= nil then
- if data[3][1] ~= nil then maps = data[3][1] end
- if data[3][2] ~= nil then g_maps = data[3][2] end
- end
- end
- if cmds ~= nil then
- if #cmds > 0 then
- for i=1,#cmds do
- callCommand(cmds[i])
- end
- end
- end
- if not result then ok = false end
- end
- end
- if ok then
- if type(result) == "number" then
- local typ = result
- print(typ)
- if typ == 8 then
- update()
- elseif typ == 7 then --message recieved
- local msg = server.data.msg
- local cmd = server.data.cmd
- if msg ~= nil and omsg ~= msg then
- server:pingUsers("msg::"..msg)
- print("sent message: "..msg)
- elseif cmd ~= nil and ocmd ~= cmd then
- if cmd:sub(1, 4) == "new " then
- local splt = multinet.split(cmd, " ")
- local nm = multinet.formatTable(splt, " ", 2)
- local nm_t = multinet.split(nm, "@@@")
- local ocean = (tonumber(nm_t[2]) == 0)
- local nam = nm_t[1]
- if g_maps[nam] == nil then
- genMap(nam, ocean)
- maps[#maps + 1] = nam
- end
- end
- end
- elseif typ == 1 or typ == 2 then
- update()
- end
- else
- local input = result
- local splt = multinet.split(input, " ")
- local cmd = splt[1]
- if cmd == "kick" then
- local name = multinet.formatTablle(splt, " ", 2)
- if server.users[name] ~= nil then
- rednet.send(server.users[name], "::KICK::")
- end
- end
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement