Compare commits

...

29 Commits

Author SHA1 Message Date
NewSoupVi
80e89db812 Update docs/world api.md
Co-authored-by: Scipio Wright <scipiowright@gmail.com>
2024-10-28 22:34:23 +01:00
NewSoupVi
8d0f1be791 Update world api.md 2024-10-28 22:25:07 +01:00
NewSoupVi
febcd84995 Update docs/world api.md
Co-authored-by: Scipio Wright <scipiowright@gmail.com>
2024-10-28 22:24:00 +01:00
NewSoupVi
d0a555a185 Update docs/world api.md
Co-authored-by: Scipio Wright <scipiowright@gmail.com>
2024-10-28 22:23:36 +01:00
NewSoupVi
da72d40f14 Update docs/world api.md
Co-authored-by: Scipio Wright <scipiowright@gmail.com>
2024-10-28 22:23:04 +01:00
NewSoupVi
f78f906ced line break issues 2024-10-28 21:20:39 +01:00
NewSoupVi
245a66828d Update world api.md 2024-10-28 21:18:17 +01:00
NewSoupVi
798556fd3f Update world api.md 2024-10-28 21:16:47 +01:00
NewSoupVi
14609100cf Write a big motivation paragraph 2024-10-28 21:14:33 +01:00
NewSoupVi
fe366e2985 Update world api.md 2024-10-28 21:03:40 +01:00
NewSoupVi
819710ff61 Reorganize / Rewrite the parts about optimisations a bit 2024-10-28 21:00:27 +01:00
NewSoupVi
c0cd03f436 Update world api.md 2024-09-27 01:21:11 +02:00
NewSoupVi
e694804509 Update world api.md 2024-09-27 01:20:47 +02:00
NewSoupVi
c21c10a602 Update docs/world api.md
Co-authored-by: Scipio Wright <scipiowright@gmail.com>
2024-09-25 13:13:06 +02:00
NewSoupVi
54ab181514 Code corrections / actually follow own spec 2024-09-22 15:27:49 +02:00
NewSoupVi
88de34dfd7 Update world api.md 2024-09-22 15:26:00 +02:00
NewSoupVi
555b7211ef Update docs/world api.md 2024-09-21 19:13:41 +02:00
NewSoupVi
7b097c918e Update docs/world api.md
Co-authored-by: Exempt-Medic <60412657+Exempt-Medic@users.noreply.github.com>
2024-09-21 19:13:10 +02:00
NewSoupVi
4eeac945fb Update world api.md 2024-09-21 19:03:24 +02:00
NewSoupVi
db577ad899 Update world api.md 2024-09-21 19:03:01 +02:00
NewSoupVi
8aed3efe97 Update world api.md 2024-09-21 19:02:30 +02:00
NewSoupVi
f7d6f689fb Update world api.md 2024-09-21 19:00:57 +02:00
NewSoupVi
910d4b93c9 Update world api.md 2024-09-21 19:00:03 +02:00
NewSoupVi
584dd8fe75 Update world api.md 2024-09-21 18:59:36 +02:00
NewSoupVi
259d010a86 Update world api.md 2024-09-21 18:56:46 +02:00
NewSoupVi
98d808bb87 Update world api.md 2024-09-21 18:55:57 +02:00
NewSoupVi
f9108f4331 Update world api.md 2024-09-21 18:52:02 +02:00
NewSoupVi
4b6ad12192 Update world api.md 2024-09-21 18:51:30 +02:00
NewSoupVi
a08fbc66a8 Docs: Make an actual LogicMixin spec & explanation 2024-09-21 18:46:56 +02:00

View File

@@ -696,9 +696,92 @@ When importing a file that defines a class that inherits from `worlds.AutoWorld.
is automatically extended by the mixin's members. These members should be prefixed with the name of the implementing
world since the namespace is shared with all other logic mixins.
Some uses could be to add additional variables to the state object, or to have a custom state machine that gets modified
with the state.
Please do this with caution and only when necessary.
LogicMixin is handy when your logic is more complex than one-to-one location-item relationships.
A game in which "The red key opens the red door" can just express this relationship through a one-line access rule.
But now, consider a game with a heavy focus on combat, where the main logical consideration is which enemies you can
defeat with your current items.
There could be dozens of weapons, armor pieces, or consumables that each improve your ability to defeat
specific enemies to varying degrees. It would be useful to be able to keep track of "defeatable enemies" as a state variable,
and have this variable be recalculated as necessary based on newly collected/removed items.
This is the capability of LogicMixin: Adding custom variables to state that get recalculated as necessary.
In general, a LogicMixin class should have at least one mutable variable that is tracking some custom state per player,
as well as `init_mixin` and `copy_mixin` functions so that this variable gets initialized and copied correctly when
`CollectionState()` and `CollectionState.copy()` are called respectively.
```python
from BaseClasses import CollectionState, MultiWorld
from worlds.AutoWorld import LogicMixin
class MyGameState(LogicMixin):
mygame_defeatable_enemies: Dict[int, Set[str]] # per player
def init_mixin(self, multiworld: MultiWorld) -> None:
# Initialize per player with the corresponding "nothing" value, such as 0 or an empty set.
# You can also use something like Collections.defaultdict
self.mygame_defeatable_enemies = {
player: set() for player in multiworld.get_game_players("My Game")
}
def copy_mixin(self, new_state: CollectionState) -> CollectionState:
# Be careful to make a "deep enough" copy here!
new_state.mygame_defeatable_enemies = {
player: enemies.copy() for player, enemies in self.mygame_defeatable_enemies.items()
}
```
After doing this, you can now access `state.mygame_defeatable_enemies[player]` from your access rules.
Usually, doing this coincides with an override of `World.collect` and `World.remove`, where the custom state variable
gets recalculated when a relevant item is collected or removed.
```python
# __init__.py
def collect(self, state: CollectionState, item: Item) -> bool:
change = super().collect(state, item)
if change and item in COMBAT_ITEMS:
state.mygame_defeatable_enemies[self.player] |= get_newly_unlocked_enemies(state)
return change
def remove(self, state: CollectionState, item: Item) -> bool:
change = super().remove(state, item)
if change and item in COMBAT_ITEMS:
state.mygame_defeatable_enemies[self.player] -= get_newly_locked_enemies(state)
return change
```
Using LogicMixin can greatly slow down your code if you don't use it intelligently. This is because `collect`
and `remove` are called very frequently during fill. If your `collect` & `remove` cause a heavy calculation
every time, your code might end up being *slower* than just doing calculations in your access rules.
One way to optimise recalculations is to make use of the fact that `collect` should only unlock things,
and `remove` should only lock things.
In our example, we have two different functions: `get_newly_unlocked_enemies` and `get_newly_locked_enemies`.
`get_newly_unlocked_enemies` should only consider enemies that are *not already in the set*
and check whether they were **unlocked**.
`get_newly_locked_enemies` should only consider enemies that are *already in the set*
and check whether they **became locked**.
Another impactful way to optimise LogicMixin is to use caching.
Your custom state variables don't actually need to be recalculated on every `collect` / `remove`, because there are
often multiple calls to `collect` / `remove` between access rule calls. Thus, it would be much more efficient to hold
off on recaculating until the an actual access rule call happens.
A common way to realize this is to define a `mygame_state_is_stale` variable that is set to True in `collect`, `remove`,
and `init_mixin`. The calls to the actual recalculating functions are then moved to the start of the relevant
access rules like this:
```python
def can_defeat_enemy(state: CollectionState, player: int, enemy: str) -> bool:
if state.mygame_state_is_stale[player]:
state.mygame_defeatable_enemies[player] = recalculate_defeatable_enemies(state)
state.mygame_state_is_stale[player] = False
return enemy in state.mygame_defeatable_enemies[player]
```
Only use LogicMixin if necessary. There are often other ways to achieve what it does, like making clever use of
`state.prog_items`, using event items, pseudo-regions, etc.
#### pre_fill