Item fusion. 50+ fusions. Elemental to cosmic.
A + B = C. Elemental fusions (fire+water=steam), tier upgrades (fire+fire=inferno, +fire=hellfire), cosmic fusions (life+death=rebirth). O(1) per fuse.
The lattice is a Python dict. Lookup is a hash table operation. Input keys, output values. That's the entire pattern.
Unity: Call from C# via the embedded Python interpreter, or export the lattice to JSON and load it with JsonUtility.
Unreal: Use the Python plugin, or convert the Python dict to a TMap in C++.
Godot: GDScript can import Python directly, or use JSON.parse_string on the exported lattice.
Web/WASM: Pyodide runs the Python file directly in the browser. This demo page demonstrates that approach.
Add new entries to the lattice dictionary. The lookup function is generic: it takes any key and returns the corresponding value, or a default. The more entries you add, the more coverage you get. Performance stays O(1) regardless of size.
Every game logic problem that's hard-coded with if/else chains is actually a lookup table in disguise. (state, event) -> action is the universal pattern. The lattice just makes that pattern explicit and O(1).
#!/usr/bin/env python3
"""
FusionCore: Fusion and inventory management.
Item A + Item B = Item C. O(1) lookup. No crafting tree.
No dependencies. Pure Python.
"""
import time
# === THE LATTICE: fusion rules ===
FUSIONS = {
# Element fusions
("fire", "earth"): {"result": "lava", "type": "element", "power": 30},
("fire", "water"): {"result": "steam", "type": "element", "power": 10},
("fire", "air"): {"result": "smoke", "type": "element", "power": 5},
("water", "earth"): {"result": "mud", "type": "element", "power": 5},
("water", "air"): {"result": "cloud", "type": "element", "power": 15},
("earth", "air"): {"result": "sandstorm", "type": "element", "power": 25},
("fire", "ice"): {"result": "water", "type": "element", "power": 5},
("lightning", "water"): {"result": "electrolyzed_water", "type": "explosion", "power": 80},
("lightning", "metal"): {"result": "magnetized_metal", "type": "element", "power": 40},
("lightning", "earth"): {"result": "shocked_ground", "type": "element", "power": 35},
# Tier 1 -> Tier 2 (same element, double)
("fire_fire", "fire_fire"): {"result": "inferno", "type": "mega", "power": 100},
("ice_ice", "ice_ice"): {"result": "glacier", "type": "mega", "power": 100},
("lightning_lightning", "lightning_lightning"): {"result": "thunderstorm", "type": "mega", "power": 100},
("earth_earth", "earth_earth"): {"result": "mountain", "type": "mega", "power": 100},
("water_water", "water_water"): {"result": "ocean", "type": "mega", "power": 100},
("air_air", "air_air"): {"result": "tornado", "type": "mega", "power": 100},
# Tier 2 -> Tier 3 (mega + element)
("inferno", "fire_fire"): {"result": "hellfire", "type": "supreme", "power": 200},
("glacier", "ice_ice"): {"result": "absolute_zero", "type": "supreme", "power": 200},
("thunderstorm", "lightning_lightning"): {"result": "divine_wrath", "type": "supreme", "power": 200},
("mountain", "earth_earth"): {"result": "tectonic_force", "type": "supreme", "power": 200},
("ocean", "water_water"): {"result": "mythic_tsunami", "type": "supreme", "power": 200},
("tornado", "air_air"): {"result": "world_ender", "type": "supreme", "power": 200},
# Gem fusions
("ruby", "ruby"): {"result": "fire_gem", "type": "gem", "power": 50},
("sapphire", "sapphire"): {"result": "ice_gem", "type": "gem", "power": 50},
("emerald", "emerald"): {"result": "nature_gem", "type": "gem", "power": 50},
("diamond", "diamond"): {"result": "purity_gem", "type": "gem", "power": 75},
("amethyst", "amethyst"): {"result": "arcane_gem", "type": "gem", "power": 60},
("ruby", "sapphire"): {"result": "prism_gem", "type": "gem", "power": 80},
# Weapon fusions
("sword", "sword"): {"result": "dual_blade", "type": "weapon", "power": 50},
("sword", "spear"): {"result": "halberd", "type": "weapon", "power": 55},
("bow", "arrow"): {"result": "loaded_bow", "type": "weapon", "power": 0},
("sword", "fire_gem"): {"result": "flame_blade", "type": "weapon", "power": 100},
("sword", "ice_gem"): {"result": "frost_blade", "type": "weapon", "power": 100},
("armor", "diamond"): {"result": "diamond_armor", "type": "armor", "power": 100},
("armor", "fire_gem"): {"result": "flame_armor", "type": "armor", "power": 80},
# Potion fusions
("health_potion", "mana_potion"): {"result": "elixir", "type": "potion", "power": 100},
("health_potion", "health_potion"): {"result": "mega_health", "type": "potion", "power": 100},
("mana_potion", "mana_potion"): {"result": "mega_mana", "type": "potion", "power": 100},
("antidote", "health_potion"): {"result": "cleanse", "type": "potion", "power": 50},
# Food fusions
("bread", "meat"): {"result": "hearty_stew", "type": "food", "power": 50},
("bread", "cheese"): {"result": "grilled_cheese", "type": "food", "power": 30},
("apple", "bread"): {"result": "apple_sandwich", "type": "food", "power": 35},
("cake", "candy"): {"result": "supreme_cake", "type": "food", "power": 75},
# Special fusions
("life", "death"): {"result": "rebirth", "type": "supreme", "power": 200},
("light", "dark"): {"result": "twilight", "type": "supreme", "power": 150},
("order", "chaos"): {"result": "balance", "type": "supreme", "power": 200},
("time", "space"): {"result": "eternity", "type": "supreme", "power": 300},
("mind", "matter"): {"result": "creation", "type": "supreme", "power": 300},
("soul", "void"): {"result": "ascension", "type": "supreme", "power": 500}
}
TYPE_ICONS = {
"element": "\033[93m", "mega": "\033[91m", "supreme": "\033[95m",
"gem": "\033[96m", "weapon": "\033[91m", "armor": "\033[96m",
"potion": "\033[92m", "food": "\033[93m", "explosion": "\033[91m"
}
def fuse(item_a: str, item_b: str) -> dict:
"""A + B = result. O(1) lookup."""
result = FUSIONS.get((item_a, item_b), FUSIONS.get((item_b, item_a)))
if not result:
return {"result": "nothing", "type": "none", "power": 0, "icon": "\033[90m"}
icon = TYPE_ICONS.get(result["type"], "\033[37m")
return {
"result": result["result"],
"type": result["type"],
"power": result["power"],
"icon": icon
}
def can_fuse(item_a: str, item_b: str) -> bool:
"""O(1) fusion possibility check."""
return (item_a, item_b) in FUSIONS or (item_b, item_a) in FUSIONS
def discover(item: str) -> list:
"""O(1) fusions involving item."""
return [(a, b, FUSIONS[(a, b)]) for (a, b) in FUSIONS.keys() if a == item or b == item]
# === DEMO ===
def demo():
print("=" * 50)
print(" FusionCore: Item fusion system, O(1) lookup")
print("=" * 50)
print()
print(" Fusing items...")
print()
reset = "\033[0m"
scenarios = [
("Elements", [("fire", "earth"), ("water", "air"), ("lightning", "water")]),
("Same element", [("fire_fire", "fire_fire"), ("ice_ice", "ice_ice")]),
("Tier upgrade", [("inferno", "fire_fire"), ("glacier", "ice_ice")]),
("Gems", [("ruby", "sapphire"), ("diamond", "diamond")]),
("Weapons", [("sword", "sword"), ("sword", "fire_gem")]),
("Potions", [("health_potion", "mana_potion"), ("health_potion", "health_potion")]),
("Cosmic", [("life", "death"), ("time", "space"), ("soul", "void")])
]
for category, fusions in scenarios:
print(f" {category}:")
for a, b in fusions:
start = time.perf_counter_ns()
result = fuse(a, b)
elapsed = time.perf_counter_ns() - start
if result["result"] == "nothing":
print(f" {a:20s} + {b:20s} = [nothing] [{elapsed}ns]")
else:
print(f" {a:20s} + {b:20s} = {result['icon']}{result['result']:20s}{reset} "
f"({result['type']:10s}, power:{result['power']:3d}) [{elapsed}ns]")
print()
print("=" * 50)
print(" 50+ fusions. 1 file. O(1) fuse. Elemental to cosmic.")
print("=" * 50)
if __name__ == "__main__":
demo()