Skip to content

Commit

Permalink
#67 Added several Noita API wrapper functions. Added EntityCache in L…
Browse files Browse the repository at this point in the history
…ua. Fixed MinaUtils.getLocalMinaEntityId(). Slightly clean up. NetworkVscUtils throwing error?
  • Loading branch information
Ismoh committed Mar 14, 2023
1 parent 61ed9f0 commit d990e88
Show file tree
Hide file tree
Showing 7 changed files with 310 additions and 67 deletions.
261 changes: 245 additions & 16 deletions mods/noita-mp/config.lua

Large diffs are not rendered by default.

12 changes: 0 additions & 12 deletions mods/noita-mp/files/scripts/noita-components/nuid_updater.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,6 @@ GlobalsUtils = dofile_once("mods/noita-mp/files/scripts/util/GlobalsUtil

local executeOnAdded = GetValueBool("executeOnAdded", true)

--#region local functions

--#endregion

--#region executeOnAdded = added() and executeOnRemove = remove()

local function added()
Logger.debug(Logger.channels.nuid, "nuid_updater.lua added..")
local currentEntityId = GetUpdatedEntityID()
Expand Down Expand Up @@ -45,11 +39,6 @@ local function remove()
:format(currentEntityId, nuid, globalsEntityId, tonumber(globalsEntityId * -1)))
end

--#endregion


--#region Decision maker if executed on added or remove

if executeOnAdded then
-- this was executed on added
added()
Expand All @@ -61,4 +50,3 @@ if not executeOnAdded then
remove()
end

--#endregion
58 changes: 53 additions & 5 deletions mods/noita-mp/files/scripts/util/EntityCacheUtils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,65 @@ end
----------------------------------------
--- EntityCache
----------------------------------------
--EntityCache.set = function(entityId, nuid, ownerGuid, ownerName, filepath, x, y, rotation, velX, velY, currentHealth, maxHealth)
--
--end
EntityCache = {}

EntityCache.set = function(entityId, nuid, ownerGuid, ownerName, filepath, x, y, rotation, velX, velY, currentHealth, maxHealth)
local cpc = CustomProfiler.start("EntityCache.set")
if not EntityCache[entityId] then
EntityCache[entityId] = {
entityId = entityId,
nuid = nuid,
ownerGuid = ownerGuid,
ownerName = ownerName,
filename = filepath,
x = x,
y = y,
rotation = rotation,
velX = velX,
velY = velY,
currentHealth = currentHealth,
maxHealth = maxHealth
}
end
CustomProfiler.stop("EntityCache.set", cpc)
end

EntityCache.get = function(entityId)
local cpc = CustomProfiler.start("EntityCache.get")
if EntityCache[entityId] then
CustomProfiler.stop("EntityCache.get", cpc)
return EntityCache[entityId]
end
CustomProfiler.stop("EntityCache.get", cpc)
return nil
end

EntityCache.delete = function(entityId)
local cpc = CustomProfiler.start("EntityCache.delete")
EntityCache[entityId] = nil
CustomProfiler.stop("EntityCache.delete", cpc)
end

EntityCache.deleteNuid = function(nuid)
local cpc = CustomProfiler.start("EntityCache.deleteNuid")
for entry in pairs(EntityCache) do
if entry.nuid == nuid then
EntityCache[entry.entityId] = nil
end
end
CustomProfiler.stop("EntityCache.deleteNuid", cpc)
end

EntityCache.size = function()
return table.size(EntityCache)
end
----------------------------------------
--- EntityCacheUtils
----------------------------------------
--- Utils class only for cache of entities.
EntityCacheUtils = {}
EntityCacheUtils = {}

EntityCacheUtils.set = function(entityId, nuid, ownerGuid, ownerName, filepath, x, y, rotation, velX, velY, currentHealth, maxHealth)
EntityCacheUtils.set = function(entityId, nuid, ownerGuid, ownerName, filepath, x, y, rotation, velX, velY, currentHealth, maxHealth)
if util.IsEmpty(entityId) then
error(("entityId must not be nil or empty!"):format(entityId), 2)
end
Expand Down
7 changes: 3 additions & 4 deletions mods/noita-mp/files/scripts/util/EntityUtils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -311,8 +311,7 @@ function EntityUtils.processAndSyncEntityNetworking()
if who == Client.iAm then
if not table.contains(playerEntityIds, entityId) then
if EntityUtils.isEntityAlive(entityId) and
entityId ~= EntityUtils.localPlayerEntityId and
entityId ~= EntityUtils.localPlayerEntityIdPolymorphed and
entityId ~= MinaUtils.getLocalMinaEntityId() and
not EntityUtils.isRemoteMinae(entityId) and
not NetworkVscUtils.hasNetworkLuaComponents(entityId)
then
Expand Down Expand Up @@ -617,8 +616,8 @@ function EntityUtils.destroyByNuid(peer, nuid)
return
end

if entityId ~= EntityUtils.localPlayerEntityId and
entityId ~= EntityUtils.localPlayerEntityIdPolymorphed
if entityId ~= MinaUtils.getLocalMinaEntityId() and
entityId ~= MinaUtils.getLocalPolymorphedMinaEntityId
then
EntityKill(entityId)
end
Expand Down
34 changes: 7 additions & 27 deletions mods/noita-mp/files/scripts/util/MinaUtils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ MinaUtils = {}

local localMinaName = nil
local localMinaGuid = nil
local localMinaEntityId = nil
local localMinaEntityIdPolymorphed = nil

function MinaUtils.setLocalMinaName(name)
localMinaName = name
Expand All @@ -42,31 +40,10 @@ function MinaUtils.getLocalMinaGuid()
return localMinaGuid
end

function MinaUtils.setLocalMinaEntityId(entityId)
localMinaEntityId = entityId
end

function MinaUtils.getLocalMinaEntityId()
return localMinaEntityId
end

function MinaUtils.setLocalMinaEntityIdPolymorphed(entityId)
localMinaEntityIdPolymorphed = entityId
end

function MinaUtils.getLocalMinaEntityId()
return localMinaEntityIdPolymorphed
end

--- Returns the entity id of the local mina. It also takes care of polymorphism!
--- @return number|nil localMinaEntityId
function MinaUtils.getLocalMinaEntityId()
local cpc = CustomProfiler.start("MinaUtils.getLocalMinaEntityId")
--if EntityUtils.isEntityAlive(EntityUtils.localPlayerEntityId) then
-- -- TODO: I think this can lead to problems. Think of polymorphed minä. EntityId will change!
-- CustomProfiler.stop("MinaUtils.getLocalMinaEntityId", cpc)
-- return EntityUtils.localPlayerEntityId
--end

local polymorphed, entityId = MinaUtils.isLocalMinaPolymorphed()

if polymorphed then
Expand All @@ -80,16 +57,20 @@ function MinaUtils.getLocalMinaEntityId()
if NetworkVscUtils.hasNetworkLuaComponents(playerEntityIds[i]) then
local compOwnerName, compOwnerGuid, compNuid = NetworkVscUtils.getAllVcsValuesByEntityId(playerEntityIds[i])
if compOwnerGuid == localMinaGuid then
EntityUtils.localPlayerEntityId = playerEntityIds[i]
CustomProfiler.stop("MinaUtils.getLocalMinaEntityId", cpc)
return playerEntityIds[i]
end
end
end
if util.IsEmpty(playerEntityIds) then
Logger.warn(Logger.channels.entity,
("There isn't any Minä spawned yet or all died! EntityGetWithTag('player_unit') = {}")
:format(playerEntityIds))
return nil
end
Logger.debug(Logger.channels.entity,
("Unable to get local player entity id. Returning first entity id(%s), which was found.")
:format(playerEntityIds[1]))
EntityUtils.localPlayerEntityId = playerEntityIds[1]
CustomProfiler.stop("MinaUtils.getLocalMinaEntityId", cpc)
return playerEntityIds[1]
end
Expand Down Expand Up @@ -146,7 +127,6 @@ function MinaUtils.isLocalMinaPolymorphed()
if isPlayer then
local compOwnerName, compOwnerGuid, compNuid = NetworkVscUtils.getAllVcsValuesByEntityId(polymorphedEntityIds[e])
if compOwnerGuid == localMinaGuid then
EntityUtils.localPlayerEntityIdPolymorphed = polymorphedEntityIds[e]
CustomProfiler.stop("MinaUtils.isLocalMinaPolymorphed", cpc)
return true, polymorphedEntityIds[e]
else
Expand Down
4 changes: 2 additions & 2 deletions mods/noita-mp/files/scripts/util/NetworkVscUtils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -384,8 +384,8 @@ function NetworkVscUtils.getAllVcsValuesByEntityId(entityId)
if ownerNameCompId and ownerGuidCompId then
return NetworkVscUtils.getAllVcsValuesByComponentIds(ownerNameCompId, ownerGuidCompId, nuidCompId)
else
error(("getAllVcsValuesByEntityId: Got unexpected nil id. entityId, = %s ownerNameCompId = %s, ownerGuidCompId = %s, nuidCompId = %s")
:format(entityId, ownerNameCompId, ownerGuidCompId, nuidCompId), 2)
--error(("getAllVcsValuesByEntityId: Got unexpected nil id. entityId, = %s ownerNameCompId = %s, ownerGuidCompId = %s, nuidCompId = %s")
-- :format(entityId, ownerNameCompId, ownerGuidCompId, nuidCompId), 2)
end
end

Expand Down
1 change: 0 additions & 1 deletion mods/noita-mp/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ end
function OnPlayerSpawned(player_entity)
local cpc = CustomProfiler.start("init.OnPlayerSpawned")
Logger.info(Logger.channels.initialize, ("Player spawned with entityId = %s!"):format(player_entity))
EntityUtils.localPlayerEntityId = player_entity

if not GameHasFlagRun("nameTags_script_applied") then
GameAddFlagRun("nameTags_script_applied")
Expand Down

0 comments on commit d990e88

Please sign in to comment.