i fixed
local mod_data = prototypes.mod_data.planet_crucible_mod_data.data
function ensure_storage_cache_is_setup()
storage.gps_tag_to_lens_insert_plan = storage.gps_tag_to_lens_insert_plan or {}
end
local function check_inventory_for_explosion(entity, inventory, prototype_name, projectile_name)
-- 检查物品原型是否存在
if not prototypes.item[prototype_name] then
log("Warning: Unknown item name: " .. prototype_name .. " - skipping explosion check")
return false
end
if inventory.get_item_count_filtered({name = prototype_name}) <= 0 then
return false
end
return entity.surface.create_entity({
name = projectile_name,
position = entity.position,
target = entity.position,
source = entity
}) ~= nil
end
local function check_inventory_for_explosions(entity, inventory)
if not inventory then
return
end
-- 添加安全检查
if not mod_data or not mod_data.items_destroyed_into_huge_explosion then
return
end
for _, prototype_name in pairs(mod_data.items_destroyed_into_huge_explosion) do
if check_inventory_for_explosion(entity, inventory, prototype_name, "planet-crucible-dummy-entity") then
return
end
end
if not mod_data.items_destroyed_into_medium_explosion then
return
end
for _, prototype_name in pairs(mod_data.items_destroyed_into_medium_explosion) do
if check_inventory_for_explosion(entity, inventory, prototype_name, "planet-crucible-explosive-rocket") then
return
end
end
end
function check_inventories_for_explosions(entity)
-- 添加实体有效性检查
if not entity or not entity.valid then
return
end
local max_index = entity.get_max_inventory_index()
if not max_index then
return
end
for i = 1, max_index do
local inventory = entity.get_inventory(i)
if inventory and inventory.valid then
check_inventory_for_explosions(entity, inventory)
end
end
end
function get_lens_insert_plan(entity)
if not entity or not entity.valid or not entity.force.create_ghost_on_entity_death or entity.name ~= "planet-crucible-laser-turret" then
return
end
if entity.item_request_proxy and entity.item_request_proxy.insert_plan then
return entity.item_request_proxy.insert_plan
end
if entity.gps_tag then
storage.gps_tag_to_lens_insert_plan[entity.gps_tag] = nil
end
local inventory = entity.get_inventory(defines.inventory.turret_ammo)
if not inventory or not inventory.valid or inventory.is_empty() then
return
end
local removal_indices = {}
if entity.item_request_proxy and entity.item_request_proxy.removal_plan then
for _, removal_plan in pairs(entity.item_request_proxy.removal_plan) do
for _, item in pairs(removal_plan.items) do
if item.in_inventory then
removal_indices[item.in_inventory.stack + 1] = true
end
end
end
end
local insert_plan = {}
for i = 1, #inventory do
if removal_indices[i] or not inventory[i].valid_for_read then
goto continue
end
insert_plan[i] = {
id = {
name = inventory[i].name,
quality = inventory[i].quality.name
},
items = {
in_inventory = {{
inventory = defines.inventory.turret_ammo,
stack = i - 1,
count = inventory[i].count
}}
}
}
::continue::
end
return insert_plan
end
function update_insert_plan(ghost_or_bp_entity, new_insert_plan)
if not new_insert_plan or table_size(new_insert_plan) == 0 then
return false
end
local property_name = ((ghost_or_bp_entity.type == "entity-ghost") and "insert_plan") or "items"
if ghost_or_bp_entity[property_name] then
for insert_plan in pairs(ghost_or_bp_entity[property_name]) do
for inventory_position in pairs(insert_plan.in_inventory) do
if inventory_position.inventory == defines.inventory.turret_ammo then
return false
end
end
end
for insert_plan in pairs(ghost_or_bp_entity[property_name]) do
table.insert(new_insert_plan, insert_plan)
end
end
ghost_or_bp_entity[property_name] = new_insert_plan
return true
end
function handle_lens_turret_died(entity)
if not entity or not entity.valid then
return
end
local insert_plan = get_lens_insert_plan(entity)
if insert_plan and table_size(insert_plan) > 0 and entity.gps_tag then
storage.gps_tag_to_lens_insert_plan[entity.gps_tag] = insert_plan
end
end
function handle_post_lens_turret_died(ghost)
-- 修复语法错误:应该使用 == 而不是单个 =
if not ghost or not ghost.valid or ghost.ghost_type ~= "planet-crucible-laser-turret" then
return
end
local stored_insert_plan = storage.gps_tag_to_lens_insert_plan[ghost.gps_tag]
if not stored_insert_plan then
return
end
storage.gps_tag_to_lens_insert_plan[ghost.gps_tag] = nil
update_insert_plan(ghost, stored_insert_plan)
end
function add_lenses_to_bp(bp_entities, bp_to_world)
local was_modified = false
for i, bp_entity in ipairs(bp_entities) do
if bp_entity.name ~= "planet-crucible-laser-turret" then
goto continue
end
local entity = bp_to_world[i]
if not entity or not entity.valid or entity.name ~= "planet-crucible-laser-turret" then
goto continue
end
if update_insert_plan(bp_entity, get_lens_insert_plan(entity)) then
was_modified = true
end
::continue::
end
return was_modified
end
function handle_bplib_extract(event)
-- Preserve external wire connections
if remote.call("rigor-module", "is_player_holding_cut_paste_tool", event.player_index) then
return
end
if not event.blueprint then
return
end
local bp_entities = event.blueprint.get_blueprint_entities()
if not bp_entities then
return
end
if not event.entities or table_size(event.entities) == 0 then
return
end
if add_lenses_to_bp(bp_entities, event.entities) then
event.blueprint.set_blueprint_entities(bp_entities)
end
end