From fde5f8865f3cb8b6215a81fb4cec4dfc7691e8d3 Mon Sep 17 00:00:00 2001 From: "B. Stack" Date: Mon, 1 Apr 2024 07:57:34 -0400 Subject: lib: add silent params to most functions --- srb_lib.py | 49 +++++++++++++++++++++++++++++-------------------- 1 file changed, 29 insertions(+), 20 deletions(-) (limited to 'srb_lib.py') diff --git a/srb_lib.py b/srb_lib.py index 09c9d26..8b4c9fe 100644 --- a/srb_lib.py +++ b/srb_lib.py @@ -27,7 +27,7 @@ # Dependencies: import sys, struct -srb_lib_version = "20240320c" +srb_lib_version = "20240331c" # Table of byte positions of values in the savegame file, minus the first four bytes which are the checksum. Due to zero-indexing of python lists, but for ease of usage, we will always put a zero as the value of index 0. That is, profile 1 will use index 1 of the list. # "Z15 return {}, f"invalid way to reference weapon: [{type(weapon)}]. Use index or name." -def get_purchased_weapons(data_object,profile_id,silent=False): +def get_purchased_weapons(data_object,profile_id, silent = False): """ For the given profile, return which weapons are already purchased, as a comma-separated string and a bitmask. """ @@ -341,6 +344,7 @@ def get_purchased_weapons(data_object,profile_id,silent=False): if weapons_purchased & i["p"] and (i["name"] not in ["all","none"]): weapons_list.append(i["name"]) weapons_mask += i["p"] + #print(f"debug (get_purchased_weapons): silent={silent}") if not silent: print(f"debug: currently have 0x{weapons_mask:04x} b{weapons_mask:016b}, {weapons_list}") return ','.join(weapons_list), weapons_mask @@ -407,6 +411,7 @@ def get_collected_breakables(data_object,profile_id, level, silent=False): return ','.join(breakables_list), breakables_mask def set_level_status(data_object,profile_id,level,status): + """ Set completion rank for a level, e.g., general """ data = _get_data_from_data_object(data_object) level_obj, message = get_level_info(level) if message != "" or level_obj == -1: @@ -495,7 +500,7 @@ def get_levelset_status(data_object,profile_id,levelset,silent=False): if not silent: print(f"Debug: letters, uppercase is collected: {completed_letters}") # it comes back as an int, but does it look better as a hex? - return profile_levelset_status + return profile_levelset_status, completed_letters def set_level_letters(data_object,profile_id,level,letters): """ Set collected letters for given level to all or none. """ @@ -770,7 +775,7 @@ def set_plane_stat(data_object,profile_id,stat,value): data = srb_pack('<1I',data,PROFILE_START_POSITION[profile_id]+thispos,value-1) return data, "" -def set_purchased_weapons(data_object,profile_id,action,weapons_list): +def set_purchased_weapons(data_object,profile_id,action,weapons_list, silent=False): """ For the given profile, take action on weapons_list, where action is in ["add","remove"] from the player. """ data = _get_data_from_data_object(data_object) if action not in ["add","remove"]: @@ -782,7 +787,7 @@ def set_purchased_weapons(data_object,profile_id,action,weapons_list): if message != "": return -1, f"unable to {action} weapons because {message} on weapon {w}" action_mask = action_mask | weapon["p"] - cur_weapons, cur_mask = get_purchased_weapons(data,profile_id) + cur_weapons, cur_mask = get_purchased_weapons(data, profile_id, silent=silent) #print(f"debug: action_mask(type {type(action_mask)})={action_mask}") #print(f"debug: cur_mask(type {type(cur_mask)})={cur_mask}") #print(f"debug: need to {action}-combine {cur_mask:016b} and {action_mask:016b}") @@ -791,14 +796,16 @@ def set_purchased_weapons(data_object,profile_id,action,weapons_list): elif action == "remove": final_mask = cur_mask & ~action_mask if final_mask != cur_mask: - print(f"debug: beginning 0x{cur_mask:04x}, b{cur_mask:016b} {cur_weapons}") - print( f"debug: {action:6s} 0x{action_mask:04x}, b{action_mask:016b} {','.join(weapons_list)}") + if not silent: + print(f"debug: beginning 0x{cur_mask:04x}, b{cur_mask:016b} {cur_weapons}") + print( f"debug: {action:6s} 0x{action_mask:04x}, b{action_mask:016b} {','.join(weapons_list)}") data = srb_pack('<1I',data,PROFILE_START_POSITION[profile_id]+POS_BYTES_WEAPONS_PURCHASED, final_mask) - print( f"debug: final 0x{final_mask:04x}, b{final_mask:016b} {get_purchased_weapons(data,profile_id,silent=True)[0]}") + if not silent: + print( f"debug: final 0x{final_mask:04x}, b{final_mask:016b} {get_purchased_weapons(data,profile_id,silent=True)[0]}") # if we make it to the end return data, "" -def get_purchased_planes(data_object,profile_id, silent=False): +def get_purchased_planes(data_object, profile_id, silent=False): """ List which planes are purchased already. """ data = _get_data_from_data_object(data_object) planes_purchased = struct.unpack_from('<1I',data,PROFILE_START_POSITION[profile_id]+POS_PLANES_PURCHASED)[0] @@ -820,7 +827,7 @@ def get_purchased_planes(data_object,profile_id, silent=False): print(f"debug: currently have 0x{planes_mask:04x} b{planes_mask:016b}, {planes_list}") return ','.join(planes_list), planes_mask -def set_purchased_planes(data_object,profile_id,action,planes_list): +def set_purchased_planes(data_object, profile_id, action, planes_list, silent = False): """ For the given profile, take action on planes_list, where action is in ["add","remove"] from the player. """ data = _get_data_from_data_object(data_object) if action not in ["add","remove"]: @@ -833,7 +840,7 @@ def set_purchased_planes(data_object,profile_id,action,planes_list): except: return -1, f"unable to {action} planes because {message} on plane {p}" action_mask = action_mask | plane["p"] - cur_planes, cur_mask = get_purchased_planes(data,profile_id) + cur_planes, cur_mask = get_purchased_planes(data,profile_id,silent=silent) #print(f"debug: action_mask(type {type(action_mask)})={action_mask}") #print(f"debug: cur_mask(type {type(cur_mask)})={cur_mask}") #print(f"debug: need to {action}-combine {cur_mask:016b} and {action_mask:016b}") @@ -842,10 +849,12 @@ def set_purchased_planes(data_object,profile_id,action,planes_list): elif action == "remove": final_mask = cur_mask & ~action_mask if final_mask != cur_mask: - print(f"debug: beginning 0x{cur_mask:04x}, b{cur_mask:016b} {cur_planes}") - print( f"debug: {action:6s} 0x{action_mask:04x}, b{action_mask:016b} {','.join(planes_list)}") + if not silent: + print(f"debug: beginning 0x{cur_mask:04x}, b{cur_mask:016b} {cur_planes}") + print( f"debug: {action:6s} 0x{action_mask:04x}, b{action_mask:016b} {','.join(planes_list)}") data = srb_pack('<1I',data,PROFILE_START_POSITION[profile_id]+POS_PLANES_PURCHASED, final_mask) - print( f"debug: final 0x{final_mask:04x}, b{final_mask:016b} {get_purchased_planes(data,profile_id,silent=True)[0]}") + if not silent: + print( f"debug: final 0x{final_mask:04x}, b{final_mask:016b} {get_purchased_planes(data,profile_id,silent=True)[0]}") # if we make it to the end return data, "" -- cgit