aboutsummaryrefslogtreecommitdiff
path: root/srb_lib.py
diff options
context:
space:
mode:
Diffstat (limited to 'srb_lib.py')
-rw-r--r--srb_lib.py49
1 files changed, 29 insertions, 20 deletions
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.
# "Z<dddddddd" is the start of a profile.
@@ -262,14 +262,15 @@ def get_money(data_object,profile_id):
money_dec = struct.unpack_from('<1I',data,PROFILE_START_POSITION[profile_id]+POS_MONEY)[0]
return money_dec
-def set_money(data_object,profile_id, money_dec):
+def set_money(data_object,profile_id, money_dec, silent = False):
""" Using data, set the money given in decimal for the given profile_id. """
data = _get_data_from_data_object(data_object)
data = srb_pack('<I',data,PROFILE_START_POSITION[profile_id]+POS_MONEY,money_dec)
- print(f"after setting money to {money_dec}, we checked and got {get_money(data,profile_id)}")
+ if not silent:
+ print(f"after setting money to {money_dec}, we checked and got {get_money(data,profile_id)}")
return data
-def get_weapon(data_object,profile_id):
+def get_weapon(data_object,profile_id,silent=True):
""" For given profile, print currently equipped weapon. """
data = _get_data_from_data_object(data_object)
weapon_id = data[PROFILE_START_POSITION[profile_id]+POS_EQUIPPED_WEAPON]
@@ -280,10 +281,11 @@ def get_weapon(data_object,profile_id):
return f"unable to determine weapon"
if message != "":
return f"failed to get weapon due to {message}"
- print(f"debug: got weapon {weapon}")
+ if not silent:
+ print(f"debug: got weapon {weapon}")
return weapon["name"]
-def set_weapon(data_object,profile_id,weapon):
+def set_weapon(data_object,profile_id,weapon, silent = False):
data = _get_data_from_data_object(data_object)
weapon, message = get_weapon_info(weapon)
if (not bool(weapon)) or message != "":
@@ -291,7 +293,8 @@ def set_weapon(data_object,profile_id,weapon):
return -1
# armed with weapon as the index number, let's change the savegame data
data = srb_pack('<I',data,PROFILE_START_POSITION[profile_id]+POS_EQUIPPED_WEAPON,weapon["id"])
- print(f"after setting weapon to {weapon['name']}, we checked and got {get_weapon(data,profile_id)}")
+ if not silent:
+ print(f"after setting weapon to {weapon['name']}, we checked and got {get_weapon(data,profile_id)}")
return data
def get_weapon_info(weapon):
@@ -318,7 +321,7 @@ def get_weapon_info(weapon):
return {}, f"invalid index {weapon}; use 0-15" # must be <0 or >15
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, ""
bgstack15