Skip to content

Commit

Permalink
Rework NetworkComponents #27 Removing entities on clients. Changed ne…
Browse files Browse the repository at this point in the history
…tworkComponent a bit. EntityManger also changed. WIP!
  • Loading branch information
Ismoh committed Apr 11, 2022
1 parent bfc7aee commit 8601121
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 73 deletions.
24 changes: 12 additions & 12 deletions .debug/run-noita.bat
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
@REM setlocal enabledelayedexpansion

set noita-exe=""
set noita-dev-exe=""
@REM FOR /f "tokens=2 delims==" %%d IN ('wmic logicaldisk where "drivetype=3" get name /format:value') DO (

FOR /f "tokens=2 delims==" %%d IN ('wmic logicaldisk where "drivetype=3" get name /format:value') DO (
echo %%d
@REM echo %%d
@REM cd /d %%d

set %noita-exe% = CALL where /r %%d noita.exe
echo %noita-exe%
@REM for /f "tokens=* delims=" %%a in ('dir /s /b noita.exe') do set "noita_exe=%%a"
@REM echo %%noita_exe%%=%noita_exe%

set %noita-dev-exe% = CALL where /r %%d noita_dev.exe
echo %noita-dev-exe%
)
@REM for /f "tokens=*" %%a in ('dir /s /b noita_dev.exe') do set "noita_dev_exe=%%a"
@REM echo %%noita_dev_exe%%=%noita_dev_exe%
@REM )

exec %noita-exe%
exec %noita-dev-exe%
@REM endlocal

pause
start "" "C:\Program Files (x86)\Steam\steamapps\common\Noita\noita.exe"
start "" "C:\Program Files (x86)\Steam\steamapps\common\Noita\noita_dev.exe" -lua_debug
4 changes: 3 additions & 1 deletion mods/noita-mp/files/lib/external/sock.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1380,6 +1380,7 @@ sock.newServer = function(address, port, maxPeers, maxChannels, inBandwidth, out
-- NoitaMP additions
username = "",
guid = nil,
whoAmI = "SERVER",
}, Server_mt)

-- ip, max peers, max channels, in bandwidth, out bandwidth
Expand Down Expand Up @@ -1452,7 +1453,8 @@ sock.newClient = function(serverOrAddress, port, maxChannels)
username = "",
guid = nil,
isAllowed = false,
isMapReceived = false
isMapReceived = false,
whoAmI = "CLIENT",
}, Client_mt)

-- Two different forms for client creation:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ NetworkComponent = {
}

----------- Statics
NetworkComponent.name = "nc"
--NetworkComponent.name = "nc"
NetworkComponent.field_name = "value_string"
-- NetworkComponent.owner_name = "nc_owner"
-- NetworkComponent.nuid_name = "nc_nuid"
-- NetworkComponent.local_entity_id_name = "nc_local_entity_id"
-- NetworkComponent.component_id_name = "nc_component_id"
NetworkComponent.storage_name_owner_name = "noita-mp.nc_owner.name"
NetworkComponent.storage_name_owner_guid = "noita-mp.nc_owner.guid"
NetworkComponent.storage_name_nuid = "noita-mp.nc_nuid"
NetworkComponent.storage_name_local_entity_id = "noita-mp.nc_local_entity_id" -- TODO: REMOVE: no need for this, because can be found by NUID and entity get id
NetworkComponent.storage_name_component_id = "noita-mp.nc_component_id" -- TODO: REMOVE: no need for this, because can be found by NUID and entity get component_id

--- Provides a serialisable table by its own fields
---@param nc table deserialised network component - without any functions
Expand Down
163 changes: 110 additions & 53 deletions mods/noita-mp/files/scripts/entity_manager.lua
Original file line number Diff line number Diff line change
Expand Up @@ -27,43 +27,41 @@ function em:GetNextNuid()
return self.nuid_counter
end

function em:AddNetworkComponentsResumeCoroutine()
-- if not self.co_add_network_components then
-- self.co_add_network_components = coroutine.create(self.AddNetworkComponentsCoroutine)
-- end

-- local status, result = coroutine.status(self.co_add_network_components)
--- Despawns all entities of the clients.
---@param clientOrServer table
function em:DespawnClientEntities(clientOrServer)
if clientOrServer.whoAmI == "SERVER" then
return
end

-- if status == "dead" then
-- -- re-set the coroutine if status is dead otherwise new spawned entities won't be handled
-- self.co_add_network_components = coroutine.create(self.AddNetworkComponentsCoroutine)
-- -- refresh status
-- status, result = coroutine.status(self.co_add_network_components)
-- end
local player_unit_ids = EntityGetWithTag("player_unit")
for i_p = 1, #player_unit_ids do
-- get all player units
local x, y, rot, scale_x, scale_y = EntityGetTransform(player_unit_ids[i_p])

-- if status ~= "dead" and status == "suspended" then
-- local success, resume_result, third = coroutine.resume(self.co_add_network_components)
-- if not success then
-- error(("Coroutine failed: %s. %s").format(resume_result, third), 2)
-- end
-- end
-- find all entities in a specific radius based on the player units position
local entity_ids = EntityGetInRadiusWithTag(x, y, 1000, "enemy")

-- status, result = coroutine.status(self.co_add_network_components)
em.AddNetworkComponentsCoroutine()
for i_e = 1, #entity_ids do
local entity_id = entity_ids[i_e]
util.debug_entity(entity_id)
EntityKill(entity_id)
end
end
end

--- Checks for entities (only for his own - locally) in a specific range/radius to the player_units.
--- If there are entities in this radius, a NetworkComponent will be added as a VariableStorageComponent.
--- If entities does not have VelocityComponents, those will be ignored.
--- Every checked entity will be put into a cache list, to stop iterating over the same entities again and again.
function em.AddNetworkComponentsCoroutine()
function em.AddNetworkComponents()
local player_unit_ids = EntityGetWithTag("player_unit")
for i_p = 1, #player_unit_ids do
-- get all player units
local x, y, rot, scale_x, scale_y = EntityGetTransform(player_unit_ids[i_p])

-- find all entities in a specific radius based on the player units position
local entity_ids = EntityGetInRadius(x, y, 100)
local entity_ids = EntityGetInRadius(x, y, 1000)

for i_e = 1, #entity_ids do
local entity_id = entity_ids[i_e]
Expand Down Expand Up @@ -148,56 +146,115 @@ function em:AddNetworkComponentToEntity(entity_id, owner, nuid)
EntityGetComponentIncludingDisabled(entity_id, "VariableStorageComponent") or {}

-- check if the entity already has a NetworkComponent. If so skip this function by returning the component_id
for i_v = 1, #variable_storage_component_ids do
local variable_storage_component_id = variable_storage_component_ids[i_v]
local variable_storage_component_name = ComponentGetValue2(variable_storage_component_id, "name") or nil
--local nc_serialised = ComponentGetValue2(variable_storage_component_id, NetworkComponent.field_name) or nil
local nc_as_json = ComponentGetValue2(variable_storage_component_id, NetworkComponent.field_name) or nil
local nc = nil

if variable_storage_component_name == NetworkComponent.name then
if not util.IsEmpty(nc_as_json) then
nc = json.decode(nc_as_json)

if nc.nuid == nuid then
-- if entity already has a VariableStorageComponent with the name of 'network_component_class', skip it
self.cache.nc_entity_ids[entity_id] = {
component_id = variable_storage_component_id,
nuid = nuid
}
self.cache.all_entity_ids[entity_id] = true
return variable_storage_component_id
end
end
end
end
-- for i_v = 1, #variable_storage_component_ids do
-- local variable_storage_component_id = variable_storage_component_ids[i_v]
-- local variable_storage_component_name = ComponentGetValue2(variable_storage_component_id, "name") or nil
-- --local nc_serialised = ComponentGetValue2(variable_storage_component_id, NetworkComponent.field_name) or nil
-- local nc_as_json = ComponentGetValue2(variable_storage_component_id, NetworkComponent.field_name) or nil
-- local nc = nil

-- if variable_storage_component_name == NetworkComponent.name then
-- if not util.IsEmpty(nc_as_json) then
-- nc = json.decode(nc_as_json)

-- if nc.nuid == nuid then
-- -- if entity already has a VariableStorageComponent with the name of 'network_component_class', skip it
-- self.cache.nc_entity_ids[entity_id] = {
-- component_id = variable_storage_component_id,
-- nuid = nuid
-- }
-- self.cache.all_entity_ids[entity_id] = true
-- return variable_storage_component_id
-- end
-- end
-- end
-- end

local component_id =
EntityAddComponent2(
EntityAddComponent2(
entity_id,
"VariableStorageComponent",
{
name = NetworkComponent.name,
value_string = nil -- will be set after creation, because NetworkComponent has to be serialised
name = NetworkComponent.storage_name_owner_name,
value_string = owner.name
}
)
logger:debug(
"VariableStorageComponent (nuid=%s) added with noita component_id = %s to entity_id = %s!",
nuid,
"VariableStorageComponent (%s = %s) added with noita component_id = %s to entity_id = %s!",
NetworkComponent.storage_name_owner_name,
owner.name,
component_id,
entity_id
)

local nc = NetworkComponent:new(owner, nuid, entity_id, component_id)
em.setComponentValue(component_id, nc)
component_id =
EntityAddComponent2(
entity_id,
"VariableStorageComponent",
{
name = NetworkComponent.storage_name_owner_guid,
value_string = owner.guid
}
)
logger:debug(
"VariableStorageComponent (%s = %s) added with noita component_id = %s to entity_id = %s!",
NetworkComponent.storage_name_owner_guid,
owner.guid,
component_id,
entity_id
)

local component_id_nuid =
EntityAddComponent2(
entity_id,
"VariableStorageComponent",
{
name = NetworkComponent.storage_name_nuid,
value_string = nuid
}
)
logger:debug(
"Added network component (nuid=%s) to the VariableStorageComponent as a serialised string: component_id=%s to entity_id=%s!",
"VariableStorageComponent (%s = %s) added with noita component_id = %s to entity_id = %s!",
NetworkComponent.storage_name_nuid,
nuid,
component_id_nuid,
entity_id
)

component_id =
EntityAddComponent2(
entity_id,
"VariableStorageComponent",
{
name = NetworkComponent.storage_name_local_entity_id,
value_string = entity_id
}
)
logger:debug(
"VariableStorageComponent (%s = %s) added with noita component_id = %s to entity_id = %s!",
NetworkComponent.storage_name_local_entity_id,
entity_id,
component_id,
entity_id
)

component_id = -- TODO: REMOVE: no need for VariableStorageComponent of component_id
EntityAddComponent2(
entity_id,
"VariableStorageComponent",
{
name = NetworkComponent.storage_name_component_id,
value_string = component_id_nuid
}
)
logger:debug(
"VariableStorageComponent (%s = %s) added with noita component_id = %s to entity_id = %s!",
NetworkComponent.storage_name_component_id,
component_id_nuid,
component_id_nuid,
entity_id
)

self.cache.nc_entity_ids[entity_id] = {
component_id = component_id,
nuid = nuid
Expand Down
13 changes: 12 additions & 1 deletion mods/noita-mp/files/scripts/net/client_class.lua
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,9 @@ function Client:update()
return -- Client not established
end

em:AddNetworkComponentsResumeCoroutine()
em:DespawnClientEntities(self.super)

em:AddNetworkComponents()

em:UpdateEntities()

Expand All @@ -294,5 +296,14 @@ function Client:sendNeedNuid(owner, entity_id, velocity)
self.super:send("needNuid", {owner, entity_id, x, y, rot, velocity, filename})
end

--- Checks if the current local user is a client
--- @return boolean iAm true if client
function Client:amIClient()
if self.super ~= nil then
return self.super.whoAmI == "CLIENT"
end
return false
end

-- Create a new global object of the server
_G.Client = Client:new()
2 changes: 1 addition & 1 deletion mods/noita-mp/files/scripts/net/server_class.lua
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ function Server:update()
return -- server not established
end

em:AddNetworkComponentsResumeCoroutine()
em:AddNetworkComponents()

em:UpdateEntities()

Expand Down

0 comments on commit 8601121

Please sign in to comment.