|
1 | | -from typing import TYPE_CHECKING |
| 1 | +from typing import TYPE_CHECKING, Optional |
2 | 2 | from worlds.generic.Rules import set_rule |
3 | 3 | from .Regions import regionMap |
4 | 4 | from .hooks import Rules |
5 | 5 | from BaseClasses import MultiWorld, CollectionState |
6 | | -from .Helpers import clamp, is_item_enabled |
| 6 | +from .Helpers import clamp, is_item_enabled, get_items_with_value, is_option_enabled |
| 7 | +from worlds.AutoWorld import World |
7 | 8 |
|
8 | 9 | import re |
9 | 10 | import math |
@@ -84,7 +85,15 @@ def checkRequireStringForArea(state: CollectionState, area: dict): |
84 | 85 | func_args = item[1].split(",") |
85 | 86 | if func_args == ['']: |
86 | 87 | func_args.pop() |
87 | | - func = getattr(Rules, func_name) |
| 88 | + |
| 89 | + func = globals().get(func_name) |
| 90 | + |
| 91 | + if func is None: |
| 92 | + func = getattr(Rules, func_name) |
| 93 | + |
| 94 | + if not callable(func): |
| 95 | + raise ValueError(f"Invalid function `{func_name}` in {area}.") |
| 96 | + |
88 | 97 | result = func(world, multiworld, state, player, *func_args) |
89 | 98 | if isinstance(result, bool): |
90 | 99 | requires_list = requires_list.replace("{" + func_name + "(" + item[1] + ")}", "1" if result else "0") |
@@ -261,3 +270,112 @@ def allRegionsAccessible(state): |
261 | 270 |
|
262 | 271 | # Victory requirement |
263 | 272 | multiworld.completion_condition[player] = lambda state: state.has("__Victory__", player) |
| 273 | + |
| 274 | +def ItemValue(world: "ManualWorld", multiworld: MultiWorld, state: CollectionState, player: int, args: str): |
| 275 | + """When passed a string with this format: 'valueName:int', |
| 276 | + this function will check if the player has collect at least 'int' valueName worth of items\n |
| 277 | + eg. {ItemValue(Coins:12)} will check if the player has collect at least 12 coins worth of items |
| 278 | + """ |
| 279 | + |
| 280 | + args_list = args.split(":") |
| 281 | + if not len(args_list) == 2 or not args_list[1].isnumeric(): |
| 282 | + raise Exception(f"ItemValue needs a number after : so it looks something like 'ItemValue({args_list[0]}:12)'") |
| 283 | + args_list[0] = args_list[0].lower().strip() |
| 284 | + args_list[1] = int(args_list[1].strip()) |
| 285 | + |
| 286 | + if not hasattr(world, 'item_values_cache'): #Cache made for optimization purposes |
| 287 | + world.item_values_cache = {} |
| 288 | + |
| 289 | + if not world.item_values_cache.get(player, {}): |
| 290 | + world.item_values_cache[player] = { |
| 291 | + 'state': {}, |
| 292 | + 'count': {}, |
| 293 | + } |
| 294 | + |
| 295 | + if (args_list[0] not in world.item_values_cache[player].get('count', {}).keys() |
| 296 | + or world.item_values_cache[player].get('state') != dict(state.prog_items[player])): |
| 297 | + #Run First Time or if state changed since last check |
| 298 | + existing_item_values = get_items_with_value(world, multiworld, args_list[0]) |
| 299 | + total_Count = 0 |
| 300 | + for name, value in existing_item_values.items(): |
| 301 | + count = state.count(name, player) |
| 302 | + if count > 0: |
| 303 | + total_Count += count * value |
| 304 | + world.item_values_cache[player]['count'][args_list[0]] = total_Count |
| 305 | + world.item_values_cache[player]['state'] = dict(state.prog_items[player]) #save the current gotten items to check later if its the same |
| 306 | + return world.item_values_cache[player]['count'][args_list[0]] >= args_list[1] |
| 307 | + |
| 308 | + |
| 309 | +# Two useful functions to make require work if an item is disabled instead of making it inaccessible |
| 310 | +def OptOne(world: "ManualWorld", multiworld: MultiWorld, state: CollectionState, player: int, item: str, items_counts: Optional[dict] = None): |
| 311 | + """Check if the passed item (with or without ||) is enabled, then this returns |item:count| |
| 312 | + where count is clamped to the maximum number of said item in the itempool.\n |
| 313 | + Eg. requires: "{OptOne(|DisabledItem|)} and |other items|" become "|DisabledItem:0| and |other items|" if the item is disabled. |
| 314 | + """ |
| 315 | + if item == "": |
| 316 | + return "" #Skip this function if item is left blank |
| 317 | + if not items_counts: |
| 318 | + items_counts = world.get_item_counts() |
| 319 | + |
| 320 | + require_type = 'item' |
| 321 | + |
| 322 | + if '@' in item[:2]: |
| 323 | + require_type = 'category' |
| 324 | + |
| 325 | + item = item.lstrip('|@$').rstrip('|') |
| 326 | + |
| 327 | + item_parts = item.split(":") |
| 328 | + item_name = item |
| 329 | + item_count = '1' |
| 330 | + |
| 331 | + if len(item_parts) > 1: |
| 332 | + item_name = item_parts[0] |
| 333 | + item_count = item_parts[1] |
| 334 | + |
| 335 | + if require_type == 'category': |
| 336 | + if item_count.isnumeric(): |
| 337 | + #Only loop if we can use the result to clamp |
| 338 | + category_items = [item for item in world.item_name_to_item.values() if "category" in item and item_name in item["category"]] |
| 339 | + category_items_counts = sum([items_counts.get(category_item["name"], 0) for category_item in category_items]) |
| 340 | + item_count = clamp(int(item_count), 0, category_items_counts) |
| 341 | + return f"|@{item_name}:{item_count}|" |
| 342 | + elif require_type == 'item': |
| 343 | + if item_count.isnumeric(): |
| 344 | + item_current_count = items_counts.get(item_name, 0) |
| 345 | + item_count = clamp(int(item_count), 0, item_current_count) |
| 346 | + return f"|{item_name}:{item_count}|" |
| 347 | + |
| 348 | +# OptAll check the passed require string and loop every item to check if they're enabled, |
| 349 | +def OptAll(world: "ManualWorld", multiworld: MultiWorld, state: CollectionState, player: int, requires: str): |
| 350 | + """Check the passed require string and loop every item to check if they're enabled, |
| 351 | + then returns the require string with items counts adjusted using OptOne\n |
| 352 | + eg. requires: "{OptAll(|DisabledItem| and |@CategoryWithModifedCount:10|)} and |other items|" |
| 353 | + become "|DisabledItem:0| and |@CategoryWithModifedCount:2| and |other items|" """ |
| 354 | + requires_list = requires |
| 355 | + |
| 356 | + items_counts = world.get_item_counts() |
| 357 | + |
| 358 | + functions = {} |
| 359 | + if requires_list == "": |
| 360 | + return True |
| 361 | + for item in re.findall(r'\{(\w+)\(([^)]*)\)\}', requires_list): |
| 362 | + #so this function doesn't try to get item from other functions, in theory. |
| 363 | + func_name = item[0] |
| 364 | + functions[func_name] = item[1] |
| 365 | + requires_list = requires_list.replace("{" + func_name + "(" + item[1] + ")}", "{" + func_name + "(temp)}") |
| 366 | + # parse user written statement into list of each item |
| 367 | + for item in re.findall(r'\|[^|]+\|', requires): |
| 368 | + itemScanned = OptOne(world, multiworld, state, player, item, items_counts) |
| 369 | + requires_list = requires_list.replace(item, itemScanned) |
| 370 | + |
| 371 | + for function in functions: |
| 372 | + requires_list = requires_list.replace("{" + function + "(temp)}", "{" + func_name + "(" + functions[func_name] + ")}") |
| 373 | + return requires_list |
| 374 | + |
| 375 | +def YamlEnabled(world: "ManualWorld", multiworld: MultiWorld, state: CollectionState, player: int, param: str) -> bool: |
| 376 | + """Is a yaml option enabled?""" |
| 377 | + return is_option_enabled(multiworld, player, param) |
| 378 | + |
| 379 | +def YamlDisabled(world: "ManualWorld", multiworld: MultiWorld, state: CollectionState, player: int, param: str) -> bool: |
| 380 | + """Is a yaml option disabled?""" |
| 381 | + return not is_option_enabled(multiworld, player, param) |
0 commit comments