Skip to content

Commit

Permalink
fix: Fixed abyssal diving gear not properly providing light.
Browse files Browse the repository at this point in the history
  • Loading branch information
notnotmelon committed Dec 5, 2024
1 parent 82453f5 commit f4e4c18
Show file tree
Hide file tree
Showing 5 changed files with 147 additions and 5 deletions.
1 change: 1 addition & 0 deletions control.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@ require "scripts.hydro-plant"
require "scripts.project-seadragon"
require "scripts.swimming"
require "scripts.trench-duct"
require "scripts.abyssal-diving-gear"

maraxsis.finalize_events()
1 change: 1 addition & 0 deletions locale/en/locale.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ maraxsis-underwater-machines=[entity=chemical-plant] and [entity=maraxsis-hydro-
[equipment-description]
nightvision-disabled-underwater=[img=utility/warning_icon] Nightvision does not function in maraxsis trenches.
maraxsis-abyssal-diving-gear=Grants immunity to presssure and breath damage underwater. Greatly increases swimming speed. Provides light underwater.
maraxsis-abyssal-diving-gear-disabled=[img=utility/warning_icon] Abyssal diving gear does not function on land.

[modifier-description]
maraxsis-torpedoes-damage-bonus=Torpedo damage: +__1__
Expand Down
10 changes: 9 additions & 1 deletion prototypes/abyssal-diving-gear.lua
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,12 @@ data:extend {{
height = 3,
type = "full"
},
}}
}}

local disabled = table.deepcopy(data.raw["movement-bonus-equipment"]["maraxsis-abyssal-diving-gear"])
disabled.name = "maraxsis-abyssal-diving-gear-disabled"
disabled.movement_bonus = 0
disabled.energy_consumption = "1W"
disabled.localised_name = {"equipment-name.maraxsis-abyssal-diving-gear"}
disabled.localised_description = {"", {"equipment-description.maraxsis-abyssal-diving-gear"}, "\n", {"equipment-description.maraxsis-abyssal-diving-gear-disabled"}}
data:extend {disabled}
134 changes: 134 additions & 0 deletions scripts/abyssal-diving-gear.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
--note: much of this code is duplicated from the nightvision script.

local is_abyssal_diving_gear = {
["maraxsis-abyssal-diving-gear"] = true,
["maraxsis-abyssal-diving-gear-disabled"] = true,
}

-- returns the default buff amount per quality level in vanilla
local function get_quality_buff(quality_level)
return 1 + quality_level * 0.3
end

local function update_abyssal_light_cone(player)
local is_on_maraxsis = not not maraxsis.MARAXSIS_SURFACES[player.physical_surface.name]
storage.abyssal_light_cones = storage.abyssal_light_cones or {}

local cone = storage.abyssal_light_cones[player.index]
if cone and not is_on_maraxsis then
cone.destroy()
storage.abyssal_light_cones[player.index] = nil
return
end

local character = player.character
if not character then return end
local grid = character.grid
if not grid then return end

local light_size = 0
for _, equipment in pairs(grid.get_contents()) do
if equipment.name == "maraxsis-abyssal-diving-gear" then
local quality = prototypes.quality[equipment.quality]
light_size = light_size + (equipment.count * get_quality_buff(quality.level))
break
end
end

if cone then
cone.destroy()
storage.abyssal_light_cones[player.index] = nil
end

if light_size == 0 then return end

storage.abyssal_light_cones[player.index] = rendering.draw_light {
sprite = "utility/light_medium",
scale = light_size + 1.5,
target = character,
surface = character.surface,
players = {player},
intensity = 1,
color = {r = 1, g = 0.8, b = 0.6},
minimum_darkness = 0.5,
}
end

---does the same thing as swap_diving_gear_to_correct_prototype, but only for one equipment
local function swap_diving_gear(grid, player, equipment)
local equipment_name = equipment.name
local position = equipment.position
local quality = equipment.quality.name
local target
if not maraxsis.MARAXSIS_SURFACES[player.physical_surface.name] then
if equipment_name:match("%-disabled$") then return end
target = equipment_name .. "-disabled"
else
target = equipment_name:gsub("%-disabled$", "")
if target == equipment_name then return end
end

assert(is_abyssal_diving_gear[target], "invalid target equipment: " .. target)

local energy = equipment.energy
grid.take {equipment = equipment}
local new_equipment = grid.put {name = target, position = position, quality = quality}
new_equipment.energy = energy
end

local function swap_diving_gear_to_correct_prototype(grid, player)
for _, equipment in pairs(grid.equipment) do
if is_abyssal_diving_gear[equipment.name] then
swap_diving_gear(grid, player, equipment)
end
end
end

maraxsis.on_event({
defines.events.on_player_changed_surface,
defines.events.on_player_armor_inventory_changed,
defines.events.on_player_created,
}, function(event)
local player = game.get_player(event.player_index)
if not player then return end
local armor = player.get_inventory(defines.inventory.character_armor)
if not armor then return end

for i = 1, #armor do
local stack = armor[i]
if stack.valid_for_read and stack.is_armor then
local grid = stack.grid
if grid then
swap_diving_gear_to_correct_prototype(grid, player)
end
end
end

update_abyssal_light_cone(player)
end)

maraxsis.on_event(defines.events.on_equipment_inserted, function(event)
local equipment = event.equipment
if not equipment.valid or not is_abyssal_diving_gear[equipment.name] then return end
local grid = event.grid
if not grid.valid then return end

for _, player in pairs(game.players) do
local armor = player.get_inventory(defines.inventory.character_armor)
if not armor then goto continue end
for i = 1, #armor do
local stack = armor[i]
if stack.valid_for_read and stack.is_armor and stack.grid == grid then
swap_diving_gear(grid, player, equipment)
end
end
::continue::
update_abyssal_light_cone(player)
end
end)

maraxsis.on_event({defines.events.on_equipment_removed, defines.events.on_player_controller_changed}, function(event)
for _, player in pairs(game.players) do
update_abyssal_light_cone(player)
end
end)
6 changes: 2 additions & 4 deletions scripts/nightvision.lua
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ maraxsis.on_event({
defines.events.on_player_armor_inventory_changed,
defines.events.on_player_created,
}, function(event)
if storage.stop_infinite_nightvision_recursion_loop then return end

local player = game.get_player(event.player_index)
if not player then return end
local armor = player.get_inventory(defines.inventory.character_armor)
Expand All @@ -52,7 +50,7 @@ maraxsis.on_event({
if stack.valid_for_read and stack.is_armor then
local grid = stack.grid
if grid then
swap_nightvision_to_correct_prototype(grid, player.surface)
swap_nightvision_to_correct_prototype(grid, player.physical_surface)
end
end
end
Expand All @@ -70,7 +68,7 @@ maraxsis.on_event(defines.events.on_equipment_inserted, function(event)
for i = 1, #armor do
local stack = armor[i]
if stack.valid_for_read and stack.is_armor and stack.grid == grid then
swap_nightvision(grid, player.surface, equipment)
swap_nightvision(grid, player.physical_surface, equipment)
end
end
::continue::
Expand Down

0 comments on commit f4e4c18

Please sign in to comment.