Factorio: support new colors in-game

Various: cleanup and comments
This commit is contained in:
Fabian Dill 2022-01-18 06:16:16 +01:00
parent c9fa49d40f
commit 028207022a
7 changed files with 39 additions and 38 deletions

View File

@ -1012,6 +1012,10 @@ class Item():
def pedestal_hint_text(self): def pedestal_hint_text(self):
return getattr(self, "_pedestal_hint_text", self.name.replace("_", " ").replace("-", " ")) return getattr(self, "_pedestal_hint_text", self.name.replace("_", " ").replace("-", " "))
@property
def flags(self) -> int:
return self.advancement + (self.never_exclude << 1) + (self.trap << 2)
def __eq__(self, other): def __eq__(self, other):
return self.name == other.name and self.player == other.player return self.name == other.name and self.player == other.player

View File

@ -333,12 +333,8 @@ class FactorioJSONtoTextParser(JSONtoTextParser):
def _handle_color(self, node: JSONMessagePart): def _handle_color(self, node: JSONMessagePart):
colors = node["color"].split(";") colors = node["color"].split(";")
for color in colors: for color in colors:
if color in {"red", "green", "blue", "orange", "yellow", "pink", "purple", "white", "black", "gray", if color in self.color_codes:
"brown", "cyan", "acid"}: node["text"] = f"[color=#{self.color_codes[color]}]{node['text']}[/color]"
node["text"] = f"[color={color}]{node['text']}[/color]"
return self._handle_text(node)
elif color == "magenta":
node["text"] = f"[color=pink]{node['text']}[/color]"
return self._handle_text(node) return self._handle_text(node)
return self._handle_text(node) return self._handle_text(node)

View File

@ -61,8 +61,8 @@ def mystery_argparse():
return args, options return args, options
def get_seed_name(random): def get_seed_name(random_source) -> str:
return f"{random.randint(0, pow(10, seeddigits) - 1)}".zfill(seeddigits) return f"{random_source.randint(0, pow(10, seeddigits) - 1)}".zfill(seeddigits)
def main(args=None, callback=ERmain): def main(args=None, callback=ERmain):
@ -220,11 +220,11 @@ def read_weights_yaml(path):
return parse_yaml(yaml) return parse_yaml(yaml)
def interpret_on_off(value): def interpret_on_off(value) -> bool:
return {"on": True, "off": False}.get(value, value) return {"on": True, "off": False}.get(value, value)
def convert_to_on_off(value): def convert_to_on_off(value) -> str:
return {True: "on", False: "off"}.get(value, value) return {True: "on", False: "off"}.get(value, value)
@ -527,10 +527,10 @@ def roll_item_plando(world_type, weights):
options = weights.get("plando_items", []) options = weights.get("plando_items", [])
for placement in options: for placement in options:
if roll_percentage(get_choice_legacy("percentage", placement, 100)): if roll_percentage(get_choice("percentage", placement, 100)):
from_pool = get_choice_legacy("from_pool", placement, PlandoItem._field_defaults["from_pool"]) from_pool = get_choice("from_pool", placement, PlandoItem._field_defaults["from_pool"])
location_world = get_choice_legacy("world", placement, PlandoItem._field_defaults["world"]) location_world = get_choice("world", placement, PlandoItem._field_defaults["world"])
force = str(get_choice_legacy("force", placement, PlandoItem._field_defaults["force"])).lower() force = str(get_choice("force", placement, PlandoItem._field_defaults["force"])).lower()
if "items" in placement and "locations" in placement: if "items" in placement and "locations" in placement:
items = placement["items"] items = placement["items"]
locations = placement["locations"] locations = placement["locations"]
@ -546,8 +546,8 @@ def roll_item_plando(world_type, weights):
for item, location in zip(items, locations): for item, location in zip(items, locations):
add_plando_item(item, location) add_plando_item(item, location)
else: else:
item = get_choice_legacy("item", placement, get_choice_legacy("items", placement)) item = get_choice("item", placement, get_choice("items", placement))
location = get_choice_legacy("location", placement) location = get_choice("location", placement)
add_plando_item(item, location) add_plando_item(item, location)
return plando_items return plando_items

View File

@ -266,12 +266,9 @@ def main(args, seed=None, baked_server_options: Optional[Dict[str, object]] = No
if world.worlds[slot].sending_visible: if world.worlds[slot].sending_visible:
sending_visible_players.add(slot) sending_visible_players.add(slot)
def get_item_flags(item: Item) -> int:
return item.advancement + (item.never_exclude << 1) + (item.trap << 2)
def precollect_hint(location): def precollect_hint(location):
hint = NetUtils.Hint(location.item.player, location.player, location.address, hint = NetUtils.Hint(location.item.player, location.player, location.address,
location.item.code, False, "", get_item_flags(location.item)) location.item.code, False, "", location.item.flags)
precollected_hints[location.player].add(hint) precollected_hints[location.player].add(hint)
precollected_hints[location.item.player].add(hint) precollected_hints[location.item.player].add(hint)
@ -281,7 +278,7 @@ def main(args, seed=None, baked_server_options: Optional[Dict[str, object]] = No
# item code None should be event, location.address should then also be None # item code None should be event, location.address should then also be None
assert location.item.code is not None assert location.item.code is not None
locations_data[location.player][location.address] = \ locations_data[location.player][location.address] = \
location.item.code, location.item.player, get_item_flags(location.item) location.item.code, location.item.player, location.item.flags
if location.player in sending_visible_players: if location.player in sending_visible_players:
precollect_hint(location) precollect_hint(location)
elif location.name in world.start_location_hints[location.player]: elif location.name in world.start_location_hints[location.player]:

View File

@ -665,6 +665,7 @@ def register_location_checks(ctx: Context, team: int, slot: int, locations: typi
if len(ctx.locations[slot][location]) == 3: if len(ctx.locations[slot][location]) == 3:
item_id, target_player, flags = ctx.locations[slot][location] item_id, target_player, flags = ctx.locations[slot][location]
else: else:
# TODO: remove around version 0.2.5
item_id, target_player = ctx.locations[slot][location] item_id, target_player = ctx.locations[slot][location]
flags = 0 flags = 0
@ -702,6 +703,7 @@ def collect_hints(ctx: Context, team: int, slot: int, item: str) -> typing.List[
if len(result) == 3: if len(result) == 3:
item_id, receiving_player, item_flags = result item_id, receiving_player, item_flags = result
else: else:
# TODO: remove around version 0.2.5
item_id, receiving_player = result item_id, receiving_player = result
item_flags = 0 item_flags = 0
@ -720,6 +722,7 @@ def collect_hints_location(ctx: Context, team: int, slot: int, location: str) ->
if len(result) == 3: if len(result) == 3:
item_id, receiving_player, item_flags = result item_id, receiving_player, item_flags = result
else: else:
# TODO: remove around version 0.2.5
item_id, receiving_player = result item_id, receiving_player = result
item_flags = 0 item_flags = 0
@ -1204,9 +1207,7 @@ class ClientMessageProcessor(CommonCommandProcessor):
@mark_raw @mark_raw
def _cmd_hint_location(self, location: str = "") -> bool: def _cmd_hint_location(self, location: str = "") -> bool:
"""Use !hint_location {location_name}, """Use !hint_location {location_name},
for example !hint_location atomic-bomb to get a spoiler peek for that location. for example !hint_location atomic-bomb to get a spoiler peek for that location."""
(In the case of factorio, or any other game where item names and location names are identical,
this command must be used explicitly.)"""
return self.get_hints(location, True) return self.get_hints(location, True)
@ -1361,6 +1362,7 @@ async def process_client_cmd(ctx: Context, client: Client, args: dict):
if len(ctx.locations[client.slot][location]) == 3: if len(ctx.locations[client.slot][location]) == 3:
target_item, target_player, flags = ctx.locations[client.slot][location] target_item, target_player, flags = ctx.locations[client.slot][location]
else: else:
# TODO: remove around version 0.2.5
target_item, target_player = ctx.locations[client.slot][location] target_item, target_player = ctx.locations[client.slot][location]
flags = 0 flags = 0

View File

@ -164,6 +164,22 @@ class JSONTypes(str, enum.Enum):
class JSONtoTextParser(metaclass=HandlerMeta): class JSONtoTextParser(metaclass=HandlerMeta):
color_codes = {
# not exact color names, close enough but decent looking
"black": "000000",
"red": "EE0000",
"green": "00FF7F",
"yellow": "FAFAD2",
"blue": "6495ED",
"magenta": "EE00EE",
"cyan": "00EEEE",
"slateblue": "6D8BE8",
"plum": "AF99EF",
"salmon": "FA8072",
"white": "FFFFFF"
}
def __init__(self, ctx): def __init__(self, ctx):
self.ctx = ctx self.ctx = ctx

14
kvui.py
View File

@ -411,20 +411,6 @@ class E(ExceptionHandler):
class KivyJSONtoTextParser(JSONtoTextParser): class KivyJSONtoTextParser(JSONtoTextParser):
color_codes = {
# not exact color names, close enough but decent looking
"black": "000000",
"red": "EE0000",
"green": "00FF7F",
"yellow": "FAFAD2",
"blue": "6495ED",
"magenta": "EE00EE",
"cyan": "00EEEE",
"slateblue": "6D8BE8",
"plum": "AF99EF",
"salmon": "FA8072",
"white": "FFFFFF"
}
def _handle_color(self, node: JSONMessagePart): def _handle_color(self, node: JSONMessagePart):
colors = node["color"].split(";") colors = node["color"].split(";")