Skip to content

Commit

Permalink
Factorio: Inventory Spill Traps (#4457)
Browse files Browse the repository at this point in the history
  • Loading branch information
Berserker66 authored Jan 26, 2025
1 parent 90417e0 commit 8622cb6
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 12 deletions.
7 changes: 7 additions & 0 deletions worlds/factorio/Options.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,11 @@ class EvolutionTrapIncrease(Range):
range_end = 100


class InventorySpillTrapCount(TrapCount):
"""Trap items that when received trigger dropping your main inventory and trash inventory onto the ground."""
display_name = "Inventory Spill Traps"


class FactorioWorldGen(OptionDict):
"""World Generation settings. Overview of options at https://wiki.factorio.com/Map_generator,
with in-depth documentation at https://lua-api.factorio.com/latest/Concepts.html#MapGenSettings"""
Expand Down Expand Up @@ -484,6 +489,7 @@ class FactorioOptions(PerGameCommonOptions):
artillery_traps: ArtilleryTrapCount
atomic_rocket_traps: AtomicRocketTrapCount
atomic_cliff_remover_traps: AtomicCliffRemoverTrapCount
inventory_spill_traps: InventorySpillTrapCount
attack_traps: AttackTrapCount
evolution_traps: EvolutionTrapCount
evolution_trap_increase: EvolutionTrapIncrease
Expand Down Expand Up @@ -518,6 +524,7 @@ class FactorioOptions(PerGameCommonOptions):
ArtilleryTrapCount,
AtomicRocketTrapCount,
AtomicCliffRemoverTrapCount,
InventorySpillTrapCount,
],
start_collapsed=True
),
Expand Down
22 changes: 10 additions & 12 deletions worlds/factorio/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ class FactorioItem(Item):
all_items["Artillery Trap"] = factorio_base_id - 6
all_items["Atomic Rocket Trap"] = factorio_base_id - 7
all_items["Atomic Cliff Remover Trap"] = factorio_base_id - 8
all_items["Inventory Spill Trap"] = factorio_base_id - 9


class Factorio(World):
Expand Down Expand Up @@ -112,6 +113,8 @@ class Factorio(World):
science_locations: typing.List[FactorioScienceLocation]
removed_technologies: typing.Set[str]
settings: typing.ClassVar[FactorioSettings]
trap_names: tuple[str] = ("Evolution", "Attack", "Teleport", "Grenade", "Cluster Grenade", "Artillery",
"Atomic Rocket", "Atomic Cliff Remover", "Inventory Spill")

def __init__(self, world, player: int):
super(Factorio, self).__init__(world, player)
Expand All @@ -136,15 +139,11 @@ def create_regions(self):
random = self.random
nauvis = Region("Nauvis", player, self.multiworld)

location_count = len(base_tech_table) - len(useless_technologies) - self.skip_silo + \
self.options.evolution_traps + \
self.options.attack_traps + \
self.options.teleport_traps + \
self.options.grenade_traps + \
self.options.cluster_grenade_traps + \
self.options.atomic_rocket_traps + \
self.options.atomic_cliff_remover_traps + \
self.options.artillery_traps
location_count = len(base_tech_table) - len(useless_technologies) - self.skip_silo

for name in self.trap_names:
name = name.replace(" ", "_").lower()+"_traps"
location_count += getattr(self.options, name)

location_pool = []

Expand Down Expand Up @@ -196,9 +195,8 @@ def sorter(loc: FactorioScienceLocation):
def create_items(self) -> None:
self.custom_technologies = self.set_custom_technologies()
self.set_custom_recipes()
traps = ("Evolution", "Attack", "Teleport", "Grenade", "Cluster Grenade", "Artillery", "Atomic Rocket",
"Atomic Cliff Remover")
for trap_name in traps:

for trap_name in self.trap_names:
self.multiworld.itempool.extend(self.create_item(f"{trap_name} Trap") for _ in
range(getattr(self.options,
f"{trap_name.lower().replace(' ', '_')}_traps")))
Expand Down
37 changes: 37 additions & 0 deletions worlds/factorio/data/mod/lib.lua
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,40 @@ function fire_entity_at_entities(entity_name, entities, speed)
target=target, speed=speed}
end
end

function spill_character_inventory(character)
if not (character and character.valid) then
return false
end

-- grab attrs once pre-loop
local position = character.position
local surface = character.surface

local inventories_to_spill = {
defines.inventory.character_main, -- Main inventory
defines.inventory.character_trash, -- Logistic trash slots
}

for _, inventory_type in pairs(inventories_to_spill) do
local inventory = character.get_inventory(inventory_type)
if inventory and inventory.valid then
-- Spill each item stack onto the ground
for i = 1, #inventory do
local stack = inventory[i]
if stack and stack.valid_for_read then
local spilled_items = surface.spill_item_stack{
position = position,
stack = stack,
enable_looted = false, -- do not mark for auto-pickup
force = nil, -- do not mark for auto-deconstruction
allow_belts = true, -- do mark for putting it onto belts
}
if #spilled_items > 0 then
stack.clear() -- only delete if spilled successfully
end
end
end
end
end
end
5 changes: 5 additions & 0 deletions worlds/factorio/data/mod_template/control.lua
Original file line number Diff line number Diff line change
Expand Up @@ -750,6 +750,11 @@ end,
fire_entity_at_entities("atomic-rocket", {cliffs[math.random(#cliffs)]}, 0.1)
end
end,
["Inventory Spill Trap"] = function ()
for _, player in ipairs(game.forces["player"].players) do
spill_character_inventory(player.character)
end
end,
}

commands.add_command("ap-get-technology", "Grant a technology, used by the Archipelago Client.", function(call)
Expand Down

0 comments on commit 8622cb6

Please sign in to comment.