5 Commits

Author SHA1 Message Date
MarioSpore
e17895902e Add unique id to compensate for multiple grinch games in one multiworld 2025-09-13 00:58:12 -04:00
MarioSpore
1596550111 Merge pull request #2 from MarioSpore/devjake
Fixed previous fixes for address optimization.
2025-09-12 23:16:01 -04:00
SomeJakeGuy
c888e17845 Fixed previous fixes for address optimization. 2025-09-12 23:10:19 -04:00
MarioSpore
3dee611b51 Fixed double receive eggs to self 2025-09-12 22:22:54 -04:00
MarioSpore
d6c7a04316 Fixed negative int bug 2025-09-12 21:27:11 -04:00

View File

@@ -3,7 +3,7 @@ from typing import TYPE_CHECKING, Sequence
import asyncio
import NetUtils
import copy
import uuid
import Utils
from .Locations import grinch_locations, GrinchLocation
from .Items import ALL_ITEMS_TABLE, MISSION_ITEMS_TABLE, GADGETS_TABLE, KEYS_TABLE, GrinchItemData #, SLEIGH_PARTS_TABLE
@@ -40,12 +40,14 @@ class GrinchClient(BizHawkClient):
ingame_log: bool = False
previous_egg_count: int = 0
send_ring_link: bool = False
unique_client_id: int = 0
def __init__(self):
super().__init__()
self.last_received_index = 0
self.loading_bios_msg = False
self.loc_unlimited_eggs = False
self.unique_client_id = 0
async def validate_rom(self, ctx: "BizHawkClientContext") -> bool:
from CommonClient import logger
@@ -85,6 +87,7 @@ class GrinchClient(BizHawkClient):
match cmd:
case "Connected": # On Connect
self.loc_unlimited_eggs = bool(ctx.slot_data["give_unlimited_eggs"])
self.unique_client_id = self._get_uuid()
logger.info("You are now connected to the client. "+
"There may be a slight delay to check you are not in demo mode before locations start to send.")
@@ -105,7 +108,7 @@ class GrinchClient(BizHawkClient):
if "tags" not in args:
return
if "RingLink" in ctx.tags and "RingLink" in args["tags"] and args["data"]["source"] != ctx.player_names[ctx.slot]:
if "RingLink" in ctx.tags and "RingLink" in args["tags"] and args["data"]["source"] != self.unique_client_id:
Utils.async_start(self.ring_link_input(args["data"]["amount"], ctx), "SyncEggs")
async def set_auth(self, ctx: "BizHawkClientContext") -> None:
@@ -195,7 +198,7 @@ class GrinchClient(BizHawkClient):
# Ensures we only get the new items that we want to give the player
new_items_only = ctx.items_received[self.last_received_index:]
addr_list_to_update: list[tuple[int, Sequence[int], str]] = []
ram_addr_dict: dict[int, list[int]] = {}
for item_received in new_items_only:
local_item = ctx.item_names.lookup_in_game(item_received.item)
@@ -203,8 +206,11 @@ class GrinchClient(BizHawkClient):
for addr_to_update in grinch_item_ram_data.update_ram_addr:
is_binary = True if not addr_to_update.binary_bit_pos is None else False
current_ram_address_value = int.from_bytes((await bizhawk.read(ctx.bizhawk_ctx, [(
addr_to_update.ram_address, addr_to_update.bit_size, "MainRAM")]))[0], "little")
if addr_to_update.ram_address in ram_addr_dict.keys():
current_ram_address_value = ram_addr_dict[addr_to_update.ram_address][0]
else:
current_ram_address_value = int.from_bytes((await bizhawk.read(ctx.bizhawk_ctx, [(
addr_to_update.ram_address, addr_to_update.bit_size, "MainRAM")]))[0], "little")
if is_binary:
current_ram_address_value = (current_ram_address_value | (1 << addr_to_update.binary_bit_pos))
elif addr_to_update.update_existing_value:
@@ -215,15 +221,13 @@ class GrinchClient(BizHawkClient):
current_ram_address_value = addr_to_update.value
# Write the updated value back into RAM
addr_list_to_update.append((addr_to_update.ram_address,
current_ram_address_value.to_bytes(addr_to_update.bit_size, "little"),"MainRAM"))
ram_addr_dict[addr_to_update.ram_address] = [current_ram_address_value, addr_to_update.bit_size]
self.last_received_index += 1
addr_list_to_update.append((RECV_ITEM_ADDR,
self.last_received_index.to_bytes(RECV_ITEM_BITSIZE,"little"), "MainRAM"))
await bizhawk.write(ctx.bizhawk_ctx, addr_list_to_update)
# Update the latest received item index to ram as well.
ram_addr_dict[RECV_ITEM_ADDR] = [self.last_received_index, RECV_ITEM_BITSIZE]
await bizhawk.write(ctx.bizhawk_ctx, self.convert_dict_to_ram_list(ram_addr_dict))
async def goal_checker(self, ctx: "BizHawkClientContext"):
if not ctx.finished_game:
@@ -240,17 +244,16 @@ class GrinchClient(BizHawkClient):
# This function's entire purpose is to take away items we physically received ingame, but have not received from AP
async def remove_physical_items(self, ctx: "BizHawkClientContext"):
addr_list_to_update: list[tuple[int, Sequence[int], str]] = []
ram_addr_dict: dict[int, list[int]] = {}
list_recv_itemids: list[int] = [netItem.item for netItem in ctx.items_received]
items_to_check: dict[str, GrinchItemData] = {**GADGETS_TABLE} #, **SLEIGH_PARTS_TABLE
heart_count = len(list(item_id for item_id in list_recv_itemids if item_id == 42570))
heart_item_data = ALL_ITEMS_TABLE["Heart of Stone"]
addr_list_to_update.append((heart_item_data.update_ram_addr[0].ram_address,
min(heart_count, 4).to_bytes(1, "little"), "MainRAM"))
ram_addr_dict[heart_item_data.update_ram_addr[0].ram_address] = [min(heart_count, 4), 1]
# Setting mission count for all accesses back to 0 to prevent warping/unlocking after completing 3 missions
addr_list_to_update.append((0x0100F0, int(0).to_bytes(4, "little"), "MainRAM"))
ram_addr_dict[0x0100F0] = [0, 4]
for (item_name, item_data) in items_to_check.items():
# If item is an event or already been received, ignore.
@@ -261,20 +264,30 @@ class GrinchClient(BizHawkClient):
for addr_to_update in item_data.update_ram_addr:
is_binary = True if not addr_to_update.binary_bit_pos is None else False
if is_binary:
current_bin_value = int.from_bytes((await bizhawk.read(ctx.bizhawk_ctx, [(
addr_to_update.ram_address, addr_to_update.bit_size, "MainRAM")]))[0], "little")
if addr_to_update.ram_address in ram_addr_dict.keys():
current_bin_value = ram_addr_dict[addr_to_update.ram_address][0]
else:
current_bin_value = int.from_bytes((await bizhawk.read(ctx.bizhawk_ctx, [(
addr_to_update.ram_address, addr_to_update.bit_size, "MainRAM")]))[0], "little")
current_bin_value &= ~(1 << addr_to_update.binary_bit_pos)
addr_list_to_update.append((addr_to_update.ram_address,
current_bin_value.to_bytes(1, "little"), "MainRAM"))
ram_addr_dict[addr_to_update.ram_address] = [current_bin_value, 1]
else:
addr_list_to_update.append((addr_to_update.ram_address,
int(0).to_bytes(1, "little"), "MainRAM"))
ram_addr_dict[addr_to_update.ram_address] = [0, addr_to_update.bit_size]
await bizhawk.write(ctx.bizhawk_ctx, addr_list_to_update)
await bizhawk.write(ctx.bizhawk_ctx, self.convert_dict_to_ram_list(ram_addr_dict))
def convert_dict_to_ram_list(self, addr_dict: dict[int, list[int]]) -> list[tuple[int, Sequence[int], str]]:
addr_list_to_update: list[tuple[int, Sequence[int], str]] = []
for (key, val) in addr_dict.items():
addr_list_to_update.append((key, val[0].to_bytes(val[1], "little"), "MainRAM"))
return addr_list_to_update
# Removes the regional access until you actually received it from AP.
async def constant_address_update(self, ctx: "BizHawkClientContext"):
addr_list_to_update: list[tuple[int, Sequence[int], str]] = []
ram_addr_dict: dict[int, list[int]] = {}
list_recv_itemids: list[int] = [netItem.item for netItem in ctx.items_received]
items_to_check: dict[str, GrinchItemData] = {**KEYS_TABLE, **MISSION_ITEMS_TABLE}
@@ -288,23 +301,24 @@ class GrinchClient(BizHawkClient):
for addr_to_update in item_data.update_ram_addr:
is_binary = True if not addr_to_update.binary_bit_pos is None else False
if is_binary:
current_bin_value = int.from_bytes((await bizhawk.read(ctx.bizhawk_ctx, [(
addr_to_update.ram_address, addr_to_update.bit_size, "MainRAM")]))[0], "little")
if addr_to_update.ram_address in ram_addr_dict.keys():
current_bin_value = ram_addr_dict[addr_to_update.ram_address][0]
else:
current_bin_value = int.from_bytes((await bizhawk.read(ctx.bizhawk_ctx, [(
addr_to_update.ram_address, addr_to_update.bit_size, "MainRAM")]))[0], "little")
if GrinchLocation.get_apid(item_data.id) in list_recv_itemids:
current_bin_value |= (1 << addr_to_update.binary_bit_pos)
else:
current_bin_value &= ~(1 << addr_to_update.binary_bit_pos)
addr_list_to_update.append((addr_to_update.ram_address,
current_bin_value.to_bytes(1, "little"), "MainRAM"))
ram_addr_dict[addr_to_update.ram_address] = [current_bin_value, 1]
else:
if GrinchLocation.get_apid(item_data.id) in list_recv_itemids:
addr_list_to_update.append((addr_to_update.ram_address,
addr_to_update.value.to_bytes(1, "little"), "MainRAM"))
ram_addr_dict[addr_to_update.ram_address] = [addr_to_update.value, addr_to_update.bit_size]
else:
addr_list_to_update.append((addr_to_update.ram_address,
int(0).to_bytes(1, "little"), "MainRAM"))
ram_addr_dict[addr_to_update.ram_address] = [0, addr_to_update.bit_size]
await bizhawk.write(ctx.bizhawk_ctx, addr_list_to_update)
await bizhawk.write(ctx.bizhawk_ctx, self.convert_dict_to_ram_list(ram_addr_dict))
async def ingame_checker(self, ctx: "BizHawkClientContext"):
from CommonClient import logger
@@ -361,7 +375,7 @@ class GrinchClient(BizHawkClient):
"cmd": "Bounce",
"data": {
"time": time.time(),
"source": ctx.slot,
"source": self.unique_client_id,
"amount": current_egg_count - self.previous_egg_count
},
"tags": ["RingLink"]
@@ -379,10 +393,18 @@ class GrinchClient(BizHawkClient):
async def ring_link_input(self, egg_amount: int, ctx: "BizHawkClientContext"):
from CommonClient import logger
current_egg_count = int.from_bytes(
game_egg_count = int.from_bytes(
(await bizhawk.read(ctx.bizhawk_ctx, [(EGG_COUNT_ADDR, EGG_ADDR_BYTESIZE, "MainRAM")]))[0], "little")
current_egg_count = min(current_egg_count + egg_amount, MAX_EGGS)
non_neg_eggs = game_egg_count + egg_amount if game_egg_count + egg_amount > 0 else 0
current_egg_count = min(non_neg_eggs, MAX_EGGS)
await bizhawk.write(ctx.bizhawk_ctx, [(EGG_COUNT_ADDR,
int(current_egg_count).to_bytes(EGG_ADDR_BYTESIZE, "little"), "MainRAM")])
self.previous_egg_count = current_egg_count
# logger.info(f"RingLink: You received {str(egg_amount)} rotten eggs.")
# logger.info(f"RingLink: You received {str(egg_amount)} rotten eggs.")
def _get_uuid(self) -> int:
string_id = str(uuid.uuid4())
uid: int = 0
for char in string_id:
uid += ord(char)
return uid