Skip to content

Commit

Permalink
Fix a problem caused by the Noita mod API returning nil when a settin…
Browse files Browse the repository at this point in the history
…g that's false by default hasn't been changed yet.

Signed-off-by: Kevadroz <kevinfdezdominguez@gmail.com>
  • Loading branch information
Kevadroz committed Jun 9, 2024
1 parent 80ddbf8 commit a512c94
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 45 deletions.
96 changes: 52 additions & 44 deletions init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,12 @@ local function genWormXML(worm)
name = spawned_worm_var_name
}))

if not settings.spawned_bleed then
if not settings.spawned_eat_ground then
for component in xml_worm:each_of("CellEaterComponent") do
component.attr._enabled = false
end
end
if not settings.spawned_eat_ground then
if not settings.spawned_bleed then
for component in xml_worm:each_of("DamageModelComponent") do
local attr = component.attr
if attr.blood_material == "blood_worm" then
Expand Down Expand Up @@ -114,7 +114,9 @@ local function setSetting(id, container, prefix)
local value = ModSettingGet(this_prefix .. id)
local value_type = type(value)
local setting_type = type(container[id])
if value_type == setting_type then
if value == nil and setting_type == "boolean" then
container[id] = false
elseif value_type == setting_type then
---@diagnostic disable-next-line: assign-type-mismatch
container[id] = value
else
Expand Down Expand Up @@ -343,7 +345,8 @@ local function doWormSpawnChance(entity_id, factor)
SetRandomSeed(GameGetFrameNum() + GetUpdatedComponentID(), Randomf(-65536, 65536) + y)
SetRandomSeed(GameGetFrameNum() + GetUpdatedComponentID(), Randomf(-65536, 65536) + entity_id)
frame_rand = frame_rand + Randomf(-65536, 65536)
if Randomf() < chance then
local spawn_chance = Randomf()
if spawn_chance < chance then
local direction = Randomf(0, math.pi * 2)
local vec_x, vec_y = vec_rotate(0, 1, direction)
local distance = Randomf(300, 400)
Expand Down Expand Up @@ -503,11 +506,16 @@ function OnWorldPreUpdate()
addWormRageFactor(player, settings.factor_active)
if debug then
print("[territorial_worms] Added " ..
tostring(settings.factor_active) .. " rage, new rage is " .. tostring(getWormRageFactor(player)))
tostring(settings.factor_active) .. " active rage, new rage is " .. tostring(getWormRageFactor(player)))
end
end

factor = addWormRageFactor(player, settings.factor_passive)
if debug and GameGetFrameNum() % 60 == 0 then
print("[territorial_worms] Added " ..
tostring(settings.factor_passive * 60) ..
" passive rage, new rage is " .. tostring(getWormRageFactor(player)))
end

syncWacData(player, factor)

Expand All @@ -521,45 +529,45 @@ function OnWorldPreUpdate()
::continue::
end
-- TODO: find a way to force the worms to target the player
-- for _, worm in ipairs(EntityGetWithTag("worm")) do
-- local components = EntityGetComponent(worm, "VariableStorageComponent")
-- if components then
-- for _, spawned_flag_comp in ipairs(components) do
-- if ComponentGetValue2(spawned_flag_comp, "name") == spawned_worm_var_name then
-- local components2 = EntityGetComponent(worm, "WormAIComponent")
-- if components2 then
-- local x, y = EntityGetTransform(worm)
-- local players = EntityGetWithTag("player_unit")
-- local distances = {}
-- for _, player in ipairs(players) do
-- local px, py = EntityGetTransform(player)
-- px, py = vec_sub(px, py, x, y)
-- table.insert(distances, { vec_length(px, py), player })
-- end
-- if distances[1] then
-- local closest_player
-- if distances[2] == nil then
-- closest_player = distances[1][2]
-- else
-- local closest_distance = -1
-- for _, dist in ipairs(distances) do
-- if closest_distance == -1 or dist[1] < closest_distance then
-- closest_player = dist[2]
-- closest_distance = dist[1]
-- end
-- end
-- end
-- for _, comp_id in ipairs(components2) do
-- if ComponentGetValue2(comp_id, "mTargetEntityId") ~= closest_player then
-- ComponentSetValue2(comp_id, "mTargetEntityId", closest_player)
-- end
-- end
-- end
-- end
-- end
-- end
-- end
-- end
-- for _, worm in ipairs(EntityGetWithTag("worm")) do
-- local components = EntityGetComponent(worm, "VariableStorageComponent")
-- if components then
-- for _, spawned_flag_comp in ipairs(components) do
-- if ComponentGetValue2(spawned_flag_comp, "name") == spawned_worm_var_name then
-- local components2 = EntityGetComponent(worm, "WormAIComponent")
-- if components2 then
-- local x, y = EntityGetTransform(worm)
-- local players = EntityGetWithTag("player_unit")
-- local distances = {}
-- for _, player in ipairs(players) do
-- local px, py = EntityGetTransform(player)
-- px, py = vec_sub(px, py, x, y)
-- table.insert(distances, { vec_length(px, py), player })
-- end
-- if distances[1] then
-- local closest_player
-- if distances[2] == nil then
-- closest_player = distances[1][2]
-- else
-- local closest_distance = -1
-- for _, dist in ipairs(distances) do
-- if closest_distance == -1 or dist[1] < closest_distance then
-- closest_player = dist[2]
-- closest_distance = dist[1]
-- end
-- end
-- end
-- for _, comp_id in ipairs(components2) do
-- if ComponentGetValue2(comp_id, "mTargetEntityId") ~= closest_player then
-- ComponentSetValue2(comp_id, "mTargetEntityId", closest_player)
-- end
-- end
-- end
-- end
-- end
-- end
-- end
-- end
end

function OnPlayerDied(player_entity)
Expand Down
2 changes: 1 addition & 1 deletion workshop_id.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
id=3264636547
3264667823

0 comments on commit a512c94

Please sign in to comment.