Skip to content

Commit

Permalink
lufia2ac: new features, bug fixes, and more (ArchipelagoMW#1549)
Browse files Browse the repository at this point in the history
### New features

- ***Architect mode***
  Usually the cave is randomized by the game, meaning that each attempt will produce a different dungeon. However, with this new feature the player can, between runs, opt into keeping the same cave. If activated, they will then encounter the same floor layouts, same enemy spawns, and same red chest contents as on their previous attempt.   

- ***Custom item pool***
  Previously, the multiworld item pool consisted entirely of random blue chest items because, well, the permanent checks are blue chests and that's what one would normally get from these. While blue chest items often greatly increase your odds against regular enemies, being able to defeat the Master can be contingent on having an appropriate equipment setup of red chest items (such as Dekar blade) or even enemy drops (such as Hidora rock), most of which cannot normally be obtained from blue chests.
  With the custom item pool option, players now have the freedom to place any cave item into the multiworld itempool for their world.

- ***Enemy floor number, enemy sprite, and enemy movement pattern randomization***
  Experienced players can deduce a lot of information about the opposition they will be facing, for example: Given the current floor number, one can know in advance which of the enemy types will have a chance to spawn on that floor. And when seeing a particular enemy sprite, one can already know which enemy types one might have to face in battle if one were to come in contact with it, and also how that enemy group will move through the dungeon.
  Three new randomization options are added for players who want to spice up their game: one can shuffle which enemy types appear on which floor, one can shuffle which sprite is used by which enemy type, and one can shuffle which movement pattern is used by which sprite.

- ***EXP modifier***
  Just a simple multiplier option to allow people to level up faster. (For technical reasons, the maximum amount of EXP that can be awarded for a single enemy is limited to 65535, but even with the maximum allowed modifier of 500% there are only 6 enemy types in the cave that can reach this cap.)


### Balance change

- ***proportionally adjust chest type distribution to accommodate increased blue chest chance***
  One of the main problems that became apparent in the current version has to do with the distribution of chest contents. The game considers 6 categories, namely: consumable (mostly non-restorative), consumable (restorative), blue chest item, spell, gear, and weapon. Since only blue chests count as multiworld locations, we want to have a mechanism to customize the blue chest chance.
  Given how the chest types are detetermined in game, a naive implementation of an increased blue chest chance causes only the consumable chance to be decreased in return. In practice, this has resulted in some players of worlds with a high blue chest chance struggling (more than usual) to keep their party alive because they were always low on comsumables that restore HP and MP.
  The new algorithm tries to avoid this one-sided effect by having an increase in blue chest chance resulting in a decrease of all other types, calculated in such a way that the relative distribution of the other 5 categories stays (approximately) the same.


### Bug fixes

- ***prevent using party member items if character is already in party***
  This should have been changed at the same time that 6eb0062 was made, but oh well... 

- ***fix glitched sprite when opening a chest immediately after receiving an item***
  When opening a chest right after receiving a multiworld item (such that there were two item get animations in the exact same iteration of the game main loop), the item from the chest would display an incorrect sprite in the wrong place. Fixed by cleaning up some relevant memory addresses after getting the multiworld item.

- ***fix death link***
  There was a condition in `deathlink_kill_player` that looked kinda smart (it checked the time against `last_death_link`), but actually wasn't smart at all because `deathlink_kill_player` is executed as an async task and the main thread will update `last_death_link` after creating the task, meaning that whether or not the incoming death link would actually be passed to the game seems to have been up to a race condition. Fixed by simply removing that check.


### Other

- ***add Lufia II Ancient Cave (and SMW) to the network diagram***
  These two games were missing from the SNES sector.

- ***implement get_filler_item_name***
  Place a restorative consumable instead of a completely random item. (Now the only known problem with item links in lufia2ac is... that noone has ever tested item links. But this should be an improvement at least. Anyway, now ArchipelagoMW#1172 can come ;)
  And btw., if you think that the implementation of random selection in this method looks weird, that's because it is indeed weird. (It tries to recreate the algorithm that the game itself uses when it generates a replacement item for a chest that would contain a spell that the party already knows.)

- ***store all options in a dataclass***
  This is basically like using ArchipelagoMW#993 (but without actual support from core). It makes the lufia2ac world code much nicer to maintain because one doesn't have to change 5 different places anymore when adding or renaming an option.

- ***remove master_hp.scale***
  I have to admit: `scale` was a mistake. Never have I seen a single option value cause so many user misconceptions. Some people assume it affects enemies other than the Master; some people assume it affects stats other than HP; and many people will just assume it is a magic option that will somehow counterbalance whatever settings combination they are currently trying to shoot themselves in the foot with.
  On top of that, the `scale` mechanism probably doesn't provide a good user experience even when used for its intended purpose (since having reached floor XY in general doesn't mean you will have the power to deplete XY% of the Masters usual HP; especially given that, due to the randomness of loot, you are never guaranteed to be able to defeat the vanilla Master even when you have cleared 100% of the floors).
  The intended target audience of the `master_hp` option are people who want to fight the Master (and know how to fight it), but also want to lessen (to a degree of their choosing) the harsh dependence on the specific equipment setups that are usually required to win this fight even when having done all 99 floors. They can achieve this by setting the `master_hp` option to a numeric value appropriate for the level of challenge they are seeking. Therefore, nothing of value should be lost by removing the special `scale` value from the `master_hp` option, while at the same time a major source of user confusion will be eliminated.

- ***typing***
  This (combined with the switch to the option dataclass) greatly reduces the typing problems in the lufia2ac world. The remaining typing errors mostly fall into 4 categories:
  1. Lambdas with defaults (which seem to be incorrectly reported as an error due to a mypy bug)
  1. Classmethods that return instances (which could probably be improved using PEP 673 "Self" types, but that would require Python 3.11 as the minimum supported version)
  1. Everything that inherits from TextChoice (which is a typing mess in core)
  1. Everything related to asar.py (which does not have proper typing and lies outside of this project)

## How was this tested?

https://discord.com/channels/731205301247803413/1080852357442707476 and others
  • Loading branch information
el-u authored and FlySniper committed Nov 14, 2023
1 parent a81cdb1 commit 559bcb0
Show file tree
Hide file tree
Showing 15 changed files with 1,033 additions and 443 deletions.
4 changes: 2 additions & 2 deletions SNIClient.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,8 @@ def _cmd_snes_close(self) -> bool:

class SNIContext(CommonContext):
command_processor: typing.Type[SNIClientCommandProcessor] = SNIClientCommandProcessor
game = None # set in validate_rom
items_handling = None # set in game_watcher
game: typing.Optional[str] = None # set in validate_rom
items_handling: typing.Optional[int] = None # set in game_watcher
snes_connect_task: "typing.Optional[asyncio.Task[None]]" = None
snes_autoreconnect_task: typing.Optional["asyncio.Task[None]"] = None

Expand Down
Binary file modified docs/network diagram/network diagram.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 12 additions & 0 deletions docs/network diagram/network diagram.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,18 @@ flowchart LR
end
SNI <-- Various, depending on SNES device --> DK3
%% Super Mario World
subgraph Super Mario World
SMW[SNES]
end
SNI <-- Various, depending on SNES device --> SMW
%% Lufia II Ancient Cave
subgraph Lufia II Ancient Cave
L2AC[SNES]
end
SNI <-- Various, depending on SNES device --> L2AC
%% Native Clients or Games
%% Games or clients which compile to native or which the client is integrated in the game.
subgraph "Native"
Expand Down
2 changes: 1 addition & 1 deletion docs/network diagram/network diagram.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
252 changes: 13 additions & 239 deletions worlds/lufia2ac/Client.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
import time
import typing
from logging import Logger
from typing import Dict
from typing import Optional

from NetUtils import ClientStatus, NetworkItem
from worlds.AutoSNIClient import SNIClient
from .Enemies import enemy_id_to_name
from .Items import start_id as items_start_id
from .Locations import start_id as locations_start_id

Expand All @@ -24,241 +25,14 @@
L2AC_TX_ADDR: int = SRAM_START + 0x2040
L2AC_RX_ADDR: int = SRAM_START + 0x2800

enemy_names: Dict[int, str] = {
0x00: "a Goblin",
0x01: "an Armor goblin",
0x02: "a Regal Goblin",
0x03: "a Goblin Mage",
0x04: "a Troll",
0x05: "an Ork",
0x06: "a Fighter ork",
0x07: "an Ork Mage",
0x08: "a Lizardman",
0x09: "a Skull Lizard",
0x0A: "an Armour Dait",
0x0B: "a Dragonian",
0x0C: "a Cyclops",
0x0D: "a Mega Cyclops",
0x0E: "a Flame genie",
0x0F: "a Well Genie",
0x10: "a Wind Genie",
0x11: "an Earth Genie",
0x12: "a Cobalt",
0x13: "a Merman",
0x14: "an Aqualoi",
0x15: "an Imp",
0x16: "a Fiend",
0x17: "an Archfiend",
0x18: "a Hound",
0x19: "a Doben",
0x1A: "a Winger",
0x1B: "a Serfaco",
0x1C: "a Pug",
0x1D: "a Salamander",
0x1E: "a Brinz Lizard",
0x1F: "a Seahorse",
0x20: "a Seirein",
0x21: "an Earth Viper",
0x22: "a Gnome",
0x23: "a Wispy",
0x24: "a Thunderbeast",
0x25: "a Lunar bear",
0x26: "a Shadowfly",
0x27: "a Shadow",
0x28: "a Lion",
0x29: "a Sphinx",
0x2A: "a Mad horse",
0x2B: "an Armor horse",
0x2C: "a Buffalo",
0x2D: "a Bruse",
0x2E: "a Bat",
0x2F: "a Big Bat",
0x30: "a Red Bat",
0x31: "an Eagle",
0x32: "a Hawk",
0x33: "a Crow",
0x34: "a Baby Frog",
0x35: "a King Frog",
0x36: "a Lizard",
0x37: "a Newt",
0x38: "a Needle Lizard",
0x39: "a Poison Lizard",
0x3A: "a Medusa",
0x3B: "a Ramia",
0x3C: "a Basilisk",
0x3D: "a Cokatoris",
0x3E: "a Scorpion",
0x3F: "an Antares",
0x40: "a Small Crab",
0x41: "a Big Crab",
0x42: "a Red Lobster",
0x43: "a Spider",
0x44: "a Web Spider",
0x45: "a Beetle",
0x46: "a Poison Beetle",
0x47: "a Mosquito",
0x48: "a Coridras",
0x49: "a Spinner",
0x4A: "a Tartona",
0x4B: "an Armour Nail",
0x4C: "a Moth",
0x4D: "a Mega Moth",
0x4E: "a Big Bee",
0x4F: "a Dark Fly",
0x50: "a Stinger",
0x51: "an Armor Bee",
0x52: "a Sentopez",
0x53: "a Cancer",
0x54: "a Garbost",
0x55: "a Bolt Fish",
0x56: "a Moray",
0x57: "a She Viper",
0x58: "an Angler fish",
0x59: "a Unicorn",
0x5A: "an Evil Shell",
0x5B: "a Drill Shell",
0x5C: "a Snell",
0x5D: "an Ammonite",
0x5E: "an Evil Fish",
0x5F: "a Squid",
0x60: "a Kraken",
0x61: "a Killer Whale",
0x62: "a White Whale",
0x63: "a Grianos",
0x64: "a Behemoth",
0x65: "a Perch",
0x66: "a Current",
0x67: "a Vampire Rose",
0x68: "a Desert Rose",
0x69: "a Venus Fly",
0x6A: "a Moray Vine",
0x6B: "a Torrent",
0x6C: "a Mad Ent",
0x6D: "a Crow Kelp",
0x6E: "a Red Plant",
0x6F: "La Fleshia",
0x70: "a Wheel Eel",
0x71: "a Skeleton",
0x72: "a Ghoul",
0x73: "a Zombie",
0x74: "a Specter",
0x75: "a Dark Spirit",
0x76: "a Snatcher",
0x77: "a Jurahan",
0x78: "a Demise",
0x79: "a Leech",
0x7A: "a Necromancer",
0x7B: "a Hade Chariot",
0x7C: "a Hades",
0x7D: "a Dark Skull",
0x7E: "a Hades Skull",
0x7F: "a Mummy",
0x80: "a Vampire",
0x81: "a Nosferato",
0x82: "a Ghost Ship",
0x83: "a Deadly Sword",
0x84: "a Deadly Armor",
0x85: "a T Rex",
0x86: "a Brokion",
0x87: "a Pumpkin Head",
0x88: "a Mad Head",
0x89: "a Snow Gas",
0x8A: "a Great Coca",
0x8B: "a Gargoyle",
0x8C: "a Rogue Shape",
0x8D: "a Bone Gorem",
0x8E: "a Nuborg",
0x8F: "a Wood Gorem",
0x90: "a Mad Gorem",
0x91: "a Green Clay",
0x92: "a Sand Gorem",
0x93: "a Magma Gorem",
0x94: "an Iron Gorem",
0x95: "a Gold Gorem",
0x96: "a Hidora",
0x97: "a Sea Hidora",
0x98: "a High Hidora",
0x99: "a King Hidora",
0x9A: "an Orky",
0x9B: "a Waiban",
0x9C: "a White Dragon",
0x9D: "a Red Dragon",
0x9E: "a Blue Dragon",
0x9F: "a Green Dragon",
0xA0: "a Black Dragon",
0xA1: "a Copper Dragon",
0xA2: "a Silver Dragon",
0xA3: "a Gold Dragon",
0xA4: "a Red Jelly",
0xA5: "a Blue Jelly",
0xA6: "a Bili Jelly",
0xA7: "a Red Core",
0xA8: "a Blue Core",
0xA9: "a Green Core",
0xAA: "a No Core",
0xAB: "a Mimic",
0xAC: "a Blue Mimic",
0xAD: "an Ice Roge",
0xAE: "a Mushroom",
0xAF: "a Big Mushr'm",
0xB0: "a Minataurus",
0xB1: "a Gorgon",
0xB2: "a Ninja",
0xB3: "an Asashin",
0xB4: "a Samurai",
0xB5: "a Dark Warrior",
0xB6: "an Ochi Warrior",
0xB7: "a Sly Fox",
0xB8: "a Tengu",
0xB9: "a Warm Eye",
0xBA: "a Wizard",
0xBB: "a Dark Sum'ner",
0xBC: "the Big Catfish",
0xBD: "a Follower",
0xBE: "the Tarantula",
0xBF: "Pierre",
0xC0: "Daniele",
0xC1: "the Venge Ghost",
0xC2: "the Fire Dragon",
0xC3: "the Tank",
0xC4: "Idura",
0xC5: "Camu",
0xC6: "Gades",
0xC7: "Amon",
0xC8: "Erim",
0xC9: "Daos",
0xCA: "a Lizard Man",
0xCB: "a Goblin",
0xCC: "a Skeleton",
0xCD: "a Regal Goblin",
0xCE: "a Goblin",
0xCF: "a Goblin Mage",
0xD0: "a Slave",
0xD1: "a Follower",
0xD2: "a Groupie",
0xD3: "the Egg Dragon",
0xD4: "a Mummy",
0xD5: "a Troll",
0xD6: "Gades",
0xD7: "Idura",
0xD8: "a Lion",
0xD9: "the Rogue Flower",
0xDA: "a Gargoyle",
0xDB: "a Ghost Ship",
0xDC: "Idura",
0xDD: "a Soldier",
0xDE: "Gades",
0xDF: "the Master",
}


class L2ACSNIClient(SNIClient):
game: str = "Lufia II Ancient Cave"

async def validate_rom(self, ctx: SNIContext) -> bool:
from SNIClient import snes_read

rom_name: bytes = await snes_read(ctx, L2AC_ROMNAME_START, 0x15)
rom_name: Optional[bytes] = await snes_read(ctx, L2AC_ROMNAME_START, 0x15)
if rom_name is None or rom_name[:4] != b"L2AC":
return False

Expand All @@ -272,7 +46,7 @@ async def validate_rom(self, ctx: SNIContext) -> bool:
async def game_watcher(self, ctx: SNIContext) -> None:
from SNIClient import snes_buffered_write, snes_flush_writes, snes_read

rom: bytes = await snes_read(ctx, L2AC_ROMNAME_START, 0x15)
rom: Optional[bytes] = await snes_read(ctx, L2AC_ROMNAME_START, 0x15)
if rom != ctx.rom:
ctx.rom = None
return
Expand All @@ -281,30 +55,30 @@ async def game_watcher(self, ctx: SNIContext) -> None:
# not successfully connected to a multiworld server, cannot process the game sending items
return

signature: bytes = await snes_read(ctx, L2AC_SIGN_ADDR, 16)
signature: Optional[bytes] = await snes_read(ctx, L2AC_SIGN_ADDR, 16)
if signature != b"ArchipelagoLufia":
return

# Goal
if not ctx.finished_game:
goal_data: bytes = await snes_read(ctx, L2AC_GOAL_ADDR, 10)
goal_data: Optional[bytes] = await snes_read(ctx, L2AC_GOAL_ADDR, 10)
if goal_data is not None and goal_data[goal_data[0]] == 0x01:
await ctx.send_msgs([{"cmd": "StatusUpdate", "status": ClientStatus.CLIENT_GOAL}])
ctx.finished_game = True

# DeathLink TX
death_data: bytes = await snes_read(ctx, L2AC_DEATH_ADDR, 3)
death_data: Optional[bytes] = await snes_read(ctx, L2AC_DEATH_ADDR, 3)
if death_data is not None:
await ctx.update_death_link(bool(death_data[0]))
if death_data[1] != 0x00:
snes_buffered_write(ctx, L2AC_DEATH_ADDR + 1, b"\x00")
if "DeathLink" in ctx.tags and ctx.last_death_link + 1 < time.time():
player_name: str = ctx.player_names.get(ctx.slot, str(ctx.slot))
enemy_name: str = enemy_names.get(death_data[1] - 1, hex(death_data[1] - 1))
enemy_name: str = enemy_id_to_name.get(death_data[1] - 1, hex(death_data[1] - 1))
await ctx.send_death(f"{player_name} was totally defeated by {enemy_name}.")

# TX
tx_data: bytes = await snes_read(ctx, L2AC_TX_ADDR, 8)
tx_data: Optional[bytes] = await snes_read(ctx, L2AC_TX_ADDR, 8)
if tx_data is not None:
snes_items_sent = int.from_bytes(tx_data[:2], "little")
client_items_sent = int.from_bytes(tx_data[2:4], "little")
Expand All @@ -316,7 +90,7 @@ async def game_watcher(self, ctx: SNIContext) -> None:
client_items_sent += 1

ctx.locations_checked.add(location_id)
await ctx.send_msgs([{"cmd": 'LocationChecks', "locations": [location_id]}])
await ctx.send_msgs([{"cmd": "LocationChecks", "locations": [location_id]}])

snes_logger.info("New Check: %s (%d/%d)" % (
location,
Expand All @@ -329,7 +103,7 @@ async def game_watcher(self, ctx: SNIContext) -> None:
snes_buffered_write(ctx, L2AC_TX_ADDR + 4, ap_items_found.to_bytes(2, "little"))

# RX
rx_data: bytes = await snes_read(ctx, L2AC_RX_ADDR, 4)
rx_data: Optional[bytes] = await snes_read(ctx, L2AC_RX_ADDR, 4)
if rx_data is not None:
snes_items_received = int.from_bytes(rx_data[:2], "little")

Expand All @@ -343,7 +117,7 @@ async def game_watcher(self, ctx: SNIContext) -> None:
ctx.player_names[item.player],
ctx.location_names[item.location],
snes_items_received, len(ctx.items_received)))
snes_buffered_write(ctx, L2AC_RX_ADDR + 2 * (snes_items_received + 1), item_code.to_bytes(2, 'little'))
snes_buffered_write(ctx, L2AC_RX_ADDR + 2 * (snes_items_received + 1), item_code.to_bytes(2, "little"))
snes_buffered_write(ctx, L2AC_RX_ADDR, snes_items_received.to_bytes(2, "little"))

await snes_flush_writes(ctx)
Expand All @@ -352,7 +126,7 @@ async def deathlink_kill_player(self, ctx: SNIContext) -> None:
from SNIClient import DeathState, snes_buffered_write, snes_flush_writes

# DeathLink RX
if "DeathLink" in ctx.tags and ctx.last_death_link + 1 < time.time():
if "DeathLink" in ctx.tags:
snes_buffered_write(ctx, L2AC_DEATH_ADDR + 2, b"\x01")
else:
snes_buffered_write(ctx, L2AC_DEATH_ADDR + 2, b"\x00")
Expand Down
Loading

0 comments on commit 559bcb0

Please sign in to comment.