diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 00000000..a83b313e --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "Lua.runtime.version": "LuaJIT" +} \ No newline at end of file diff --git a/quant.ew/files/src/ctx.lua b/quant.ew/files/src/ctx.lua index 4f40b383..ca1e5894 100644 --- a/quant.ew/files/src/ctx.lua +++ b/quant.ew/files/src/ctx.lua @@ -14,6 +14,7 @@ ctx.init = function() ctx.item_prevent_localize = {} ctx.events = {} ctx.is_inventory_open = false + ctx.rpc_peer_id = nil end function ctx.dofile_and_add_hooks(path) diff --git a/quant.ew/files/src/net.lua b/quant.ew/files/src/net.lua index 5ac2f181..1338164c 100644 --- a/quant.ew/files/src/net.lua +++ b/quant.ew/files/src/net.lua @@ -16,6 +16,44 @@ end local string_split = util.string_split +net._rpc_inner = { + rpcs = {}, + opts = {}, +} +net.rpc = {} + +local rpc_inner = net._rpc_inner + +local rpc_base = {} + +function rpc_base.opts_reliable() + rpc_inner.opts.reliable = true +end + +local rpc_meta = { + __newindex = function (t, k, v) + table.insert(rpc_inner.rpcs, v) + local index = #rpc_inner.rpcs + local reliable = rpc_inner.opts.reliable == true + rawset(t, k, function(...) + net.send(index, {...}, reliable) + end) + net_handling.mod[index] = function(peer_id, args) + ctx.rpc_peer_id = peer_id + v(unpack(args)) + ctx.rpc_peer_id = nil + end + rpc_inner.opts = {} + end, + __index = rpc_base, +} + +function net.new_rcp_namespace() + local ret = {} + setmetatable(ret, rpc_meta) + return ret +end + function net.init() local ready = false local addr = os.getenv("NP_NOITA_ADDR") or "127.0.0.1:21251" @@ -136,10 +174,6 @@ function net.send_enemy_death_data(death_data) net.send("edeath", death_data, true) end -function net.send_world_data(world_data) - net.send("world", world_data) -end - function net.send_host_player_info(player_info) net.send("host_player", player_info) end diff --git a/quant.ew/files/src/net_handling.lua b/quant.ew/files/src/net_handling.lua index 7e57810b..0fcb4a76 100644 --- a/quant.ew/files/src/net_handling.lua +++ b/quant.ew/files/src/net_handling.lua @@ -2,7 +2,6 @@ local player_fns = dofile_once("mods/quant.ew/files/src/player_fns.lua") local ctx = dofile_once("mods/quant.ew/files/src/ctx.lua") local util = dofile_once("mods/quant.ew/files/src/util.lua") local enemy_sync = dofile_once("mods/quant.ew/files/src/enemy_sync.lua") -local world_sync = dofile_once("mods/quant.ew/files/src/world_sync.lua") local perk_fns = dofile_once("mods/quant.ew/files/src/perk_fns.lua") local inventory_helper = dofile_once("mods/quant.ew/files/src/inventory_helper.lua") local item_sync = dofile_once("mods/quant.ew/files/src/item_sync.lua") @@ -85,12 +84,6 @@ function net_handling.mod.edeath(peer_id, death_data) end end -function net_handling.mod.world(peer_id, world_data) - if peer_id == ctx.host_id then - world_sync.handle_world_data(world_data) - end -end - function net_handling.mod.host_player(peer_id, player_infos) if peer_id ~= ctx.host_id then return diff --git a/quant.ew/files/src/world_sync.lua b/quant.ew/files/src/world_sync.lua index 847e0e09..8d18b3b6 100644 --- a/quant.ew/files/src/world_sync.lua +++ b/quant.ew/files/src/world_sync.lua @@ -4,6 +4,9 @@ local rect = require("noitapatcher.nsew.rect") local ffi = require("ffi") local ctx = dofile_once("mods/quant.ew/files/src/ctx.lua") +local net = dofile_once("mods/quant.ew/files/src/net.lua") + +local rpc = net.new_rcp_namespace() local rect_optimiser = rect.Optimiser_new() local encoded_area = world.EncodedArea() @@ -31,7 +34,7 @@ local function send_pending() end if #will_send > 0 then -- GamePrint(#pending_send_wd.." "..#will_send.." "..total_len) - ctx.lib.net.send_world_data(will_send) + rpc.send_world_data(will_send) end end @@ -116,5 +119,10 @@ function world_sync.handle_world_data(world_data) end end +function rpc.send_world_data(world_data) + if ctx.rpc_peer_id == ctx.host_id then + world_sync.handle_world_data(world_data) + end +end return world_sync \ No newline at end of file