Advertisement
billysback

Navy

Nov 24th, 2012
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.69 KB | None | 0 0
  1. --[[
  2.     "Naval", ship warfare strategy game. because.
  3. ]]
  4.  
  5. local mods = {
  6. }
  7.  
  8. local tAPIsLoading = {}
  9. local function loadMod(dir)
  10.     if dir ~= nil then
  11.         local _sPath = dir
  12.         local sName = fs.getName( _sPath )
  13.         local n = 1
  14.         while tAPIsLoading[sName] == true and n < 300 do
  15.             n = n + 1
  16.             sleep(0)
  17.         end
  18.         if n < 300 then
  19.             tAPIsLoading[sName] = true
  20.                
  21.             local tEnv = {}
  22.             setmetatable( tEnv, { __index = _G } )
  23.             local fnAPI, err = loadfile( _sPath )
  24.             if fnAPI then
  25.                 setfenv( fnAPI, tEnv )
  26.                 fnAPI()
  27.             else
  28.                 printError( err )
  29.                 tAPIsLoading[sName] = nil
  30.                 return nil
  31.             end
  32.            
  33.             local tAPI = {}
  34.             for k,v in pairs( tEnv ) do
  35.                 tAPI[k] =  v
  36.             end
  37.            
  38.             tAPIsLoading[sName] = nil
  39.             return tAPI
  40.         else
  41.             return nil
  42.         end
  43.     else
  44.         return nil
  45.     end
  46. end
  47.  
  48. if not fs.exists("multinet") then
  49.     shell.run("pastebin get WGCM34TP multinet")
  50. end
  51.  
  52. for k,v in ipairs(rs.getSides()) do
  53.     rednet.open(v)
  54. end
  55.  
  56. print("Enter server name:")
  57. local name = read()
  58. local server = multinet.createServer("navy#"..name, "right")
  59. server:addSData("width", 20)
  60. server:addSData("height", 18)
  61.  
  62. local g_maps = {}
  63. local function genMap(name, ocean)
  64.     local w = server.data.width
  65.     local h = server.data.height
  66.     local points = {}
  67.     for i=1,math.random(6) do
  68.         points[#points + 1] = {math.random(w), math.random(h)}
  69.     end
  70.     local g_map = {}
  71.     for y=1,h do
  72.         local line = ""
  73.         for x=1,w do
  74.             local ch = "~"
  75.             if not ocean then ch = "#" end
  76.             for i=1,#points do
  77.                 local p = points[i]
  78.                 local ds = {p[1]-x, p[2]-y}
  79.                 if ds[1] < 0 then ds[1] = ds[1]*-1 end
  80.                 if ds[2] < 0 then ds[2] = ds[2]*-1 end
  81.                 local dist = ds[1]+ds[2]
  82.                 if dist <= math.random(2,4) then
  83.                     if not ocean then ch = "~" else ch = "#" end
  84.                 end
  85.             end
  86.             line = line..ch
  87.         end
  88.         g_map[y] = line
  89.     end
  90.     g_maps[name] = g_map
  91. end
  92. print("Generating map..")
  93. genMap("map", true)
  94. print("Finished!")
  95.  
  96. local maps = {"map"}
  97.  
  98. local lMods = {
  99.     {},
  100.     {},
  101.     {},
  102.     {},
  103.     {}
  104. }
  105.    
  106.  
  107. if #mods > 0 then
  108.     for i=1,#mods do
  109.         local mod = loadMod(mods[i])
  110.         if mod ~= nil then
  111.             mod.startUp(server)
  112.             print("Loaded mod: "..mod.getName())
  113.             local modTab = lMods[mod.getLevel()]
  114.             modTab[#modTab + 1] = mod
  115.         else
  116.             print("Failed to load mod at dir:")
  117.             print("   "..mods[i])
  118.         end
  119.         sleep(0.1)
  120.     end
  121. end
  122.  
  123. local function update()
  124.     for i=1,#maps do
  125.         local mname = maps[i]
  126.         local g_map = g_maps[mname]
  127.         if g_map ~= nil then
  128.             for y=1,server.data.height do
  129.                 local line = ""
  130.                 for x=1,server.data.width do
  131.                     local char = g_map[y]:sub(x, x)
  132.                     local ok = true
  133.                     for k,v in pairs(server:getUsers()) do
  134.                         if tonumber(v[2][1]) == x and tonumber(v[2][2]) == y and v[2][3] == mname then
  135.                             print(k..":MATCHED")
  136.                             char = "^"
  137.                             ok = false
  138.                         elseif v[2][1] ~= nil and v[2][2] ~= nil and ok then
  139.                             --print(v[2][1]..":"..x)
  140.                             --print(v[2][2]..":"..y)
  141.                         end
  142.                     end
  143.                     line = line..char
  144.                 end
  145.                 server:addSData(mname..":"..y, line)
  146.             end
  147.         end
  148.     end
  149.     local data = server.data
  150.     local dat = multinet.formatComplexTable(data, "#@#", "#=#")
  151.     if dat ~= nil then
  152.         server:pingUsers("upt::"..dat)
  153.         print("updated")
  154.     end
  155. end
  156.  
  157. update()
  158.  
  159. local function callCommand(cmd)
  160.     local splt = multinet.split(cmd, ":")
  161.     local name = splt[1]
  162.     if name == "gen" then
  163.         local typ = tonubmer(splt[2])
  164.         local ocean = (typ == 0)
  165.         local world = ""
  166.         world = multinet.formatTable(splt, " ", 3)
  167.         genMap(world, typ)
  168.     elseif name == "update" then
  169.         update()
  170.     end
  171. end
  172.  
  173. local function getInput()
  174.     return read()
  175. end
  176.  
  177. while true do
  178.     local omsg = server.data.msg
  179.     local ocmd = server.data.cmd
  180.     local result = server:waitForCommand()
  181.     local isCMD = (type(result) == "number")
  182.     local ok = true
  183.     for i=1,#lMods do
  184.         local modTab = lMods[i]
  185.         for i=1,#modTab do
  186.             local mod = modTab[i]
  187.             local result, cmds, data = mod.update({server}, {isCMD, result}, {maps, g_maps})
  188.             if data ~= nil then
  189.                 if data[1] ~= nil then
  190.                     if data[1][1] ~= nil then server = data[1][1] end
  191.                 end
  192.                 if data[2] ~= nil then
  193.                     if data[2][1] ~= nil then event = data[2][1] end
  194.                     if data[2][2] ~= nil then p1 = data[2][2] end
  195.                     if data[2][3] ~= nil then p2 = data[2][3] end
  196.                     if data[2][4] ~= nil then p3 = data[2][4] end
  197.                 end
  198.                 if data[3] ~= nil then
  199.                     if data[3][1] ~= nil then maps = data[3][1] end
  200.                     if data[3][2] ~= nil then g_maps = data[3][2] end
  201.                 end
  202.             end
  203.             if cmds ~= nil then
  204.                 if #cmds > 0 then
  205.                     for i=1,#cmds do
  206.                         callCommand(cmds[i])
  207.                     end
  208.                 end
  209.             end
  210.             if not result then ok = false end
  211.         end
  212.     end
  213.     if ok then
  214.         if type(result) == "number" then
  215.             local typ = result
  216.             print(typ)
  217.             if typ == 8 then
  218.                 update()
  219.             elseif typ == 7 then --message recieved
  220.                 local msg = server.data.msg
  221.                 local cmd = server.data.cmd
  222.                 if msg ~= nil and omsg ~= msg then
  223.                     server:pingUsers("msg::"..msg)
  224.                     print("sent message: "..msg)
  225.                 elseif cmd ~= nil and ocmd ~= cmd then
  226.                     if cmd:sub(1, 4) == "new " then
  227.                         local splt = multinet.split(cmd, " ")
  228.                         local nm = multinet.formatTable(splt, " ", 2)
  229.                         local nm_t = multinet.split(nm, "@@@")
  230.                         local ocean = (tonumber(nm_t[2]) == 0)
  231.                         local nam = nm_t[1]
  232.                         if g_maps[nam] == nil then
  233.                             genMap(nam, ocean)
  234.                             maps[#maps + 1] = nam
  235.                         end
  236.                     end
  237.                 end
  238.             elseif typ == 1 or typ == 2 then
  239.                 update()
  240.             end
  241.         else
  242.             local input = result
  243.             local splt = multinet.split(input, " ")
  244.             local cmd = splt[1]
  245.             if cmd == "kick" then
  246.                 local name = multinet.formatTablle(splt, " ", 2)
  247.                 if server.users[name] ~= nil then
  248.                     rednet.send(server.users[name], "::KICK::")
  249.                 end
  250.             end
  251.         end
  252.     end
  253. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement