Wikifang:Network Translation Patchsite/SpriteManager: Difference between revisions
No edit summary |
|||
Line 115: | Line 115: | ||
| INOP $52 || $52 || $33FD || NOP | | INOP $52 || $52 || $33FD || NOP | ||
|- | |- | ||
| IDOWN || $53 || $341F || | | IDOWN || $53 || $341F || Run the sprite's move-down animation. | ||
|- | |- | ||
| IUP || $54 || $3436 || | | IUP || $54 || $3436 || Run the sprite's move-up animation. | ||
|- | |- | ||
| ILEFT || $55 || $344D || | | ILEFT || $55 || $344D || Run the sprite's move-left animation. | ||
|- | |- | ||
| IRIGHT || $56 || $3464 || | | IRIGHT || $56 || $3464 || Run the sprite's move-right animation. | ||
|- | |- | ||
| INOP $57 || $57 || $347B || NOP | | INOP $57 || $57 || $347B || NOP | ||
|- | |- | ||
| IWAIT || $58 || $3400 || Run the sprite's | | IWAIT || $58 || $3400 || Run the stationary animation for the sprite's current direction a random number of times, from $0 to $20. | ||
The repeat value of this instruction is ignored. | The repeat value of this instruction is ignored. | ||
|- | |- | ||
| ISTOP || $59 || $341C || Run the sprite's | | ISTOP || $59 || $341C || Run the stationary animation for the sprite's current direction. | ||
|- | |- | ||
| EFGAME $nn || $60-$EF || N/A || Invalid opcodes, will likely crash game. | | EFGAME $nn || $60-$EF || N/A || Invalid opcodes, will likely crash game. | ||
Line 141: | Line 141: | ||
| EFGAME $nn || $F2-$FF || N/A || Invalid opcodes, will likely crash game. | | EFGAME $nn || $F2-$FF || N/A || Invalid opcodes, will likely crash game. | ||
|} | |} | ||
''Please keep in mind that directional commands such as IDOWN, IUP, ILEFT, and IRIGHT do not actually move the sprite.'' Instead, they queue the animation script associated with that direction. The animation script itself moves the sprite; it's only by convention that, e.g. IDOWN always executes a script that moves the sprite down two tiles. Furthermore, script execution is predicated upon the target tiles being free; if they are occupied, then the idle script will halt until that slot becomes free. This has the following consequences: | |||
* The movement path specified by the idle script will be followed exactly. | |||
* Sprites can get 'stuck' on stationary walls, or even each other. | |||
* NPCs will 'box in' players, never moving until the player moves out of their way, as the player is just another sprite. | |||
=== Animation Script Instructions === | === Animation Script Instructions === |
Revision as of 17:16, 21 December 2017
Bugsite's metasprite system is a scriptable SpriteManager which manages the movement of objects on a playfield. These objects need to be managed as a collection of hardware sprites (on systems which support them). In GB retroprogramming parlance these collections of sprites are called metasprites. SpriteManager is Bugsite's implementation of a metasprite system.
Metasprite Data Structure
Each metasprite consists of a metasprite struct and, if animated, an optional scripting context. Both data structures exist as an array of 8 slots at $C700 and $C800, respectively. The offsets of each structure are as follows:
Field | Offset | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
State | +$00 | A bitmask of various sprite properties.
Bit 0: Metasprite slot is initialized and valid. If this bit is clear, the slot can be treated as unused and overwritten with a new metasprite. Bit 1: Metasprite is active, meaning the user is expected to be able to see it. Sprites are usually activated and deactivated by a per-frame clip test; however, sprites can also manually deactivate themselves. Deactivated sprites are processed normally, but they do not create visible hardware sprites on screen. Bit 2: Metasprite is running an animation. New animations won't be queued, and idle loop scripts won't run, until the animation completes. Stationary animations do not set this flag and are always interruptible animations. Bit 3: Metasprite has been locked to it's stationary animation. Bit 4: Metasprite has been locked to it's stationary animation. For some reason, there are two bits with the same effect. It is unknown if one bit has different semantics from the other. | ||||||||||
SpriteY | +$01 | Y coordinate of sprite (16bit) | ||||||||||
SpriteX | +$03 | X coordinate of sprite (16bit) | ||||||||||
MovePerms | +$05 | Pointer into the move permissions array which references the sprite's current location. | ||||||||||
AnimAllowed | +$07 | Must be non-zero for animations to run on this sprite. | ||||||||||
FileIndex | +$08 | Offset from the base files for stationary and moving animations.
The file index appears to be restricted to the following common indices; BugVM instructions that directly set FileIndex will wrap any further values to zero.
| ||||||||||
FileAnimStationary | +$09 | BugFS file ID containing an animation script to be used for the stationary animation. | ||||||||||
FileAnimMoving | +$0B | BugFS file ID containing an animation script to be used for walk cycles and other sprite-moving animations. | ||||||||||
IdleRepeat | +$0D | How many idle frames to continue executing the current IdleInstr. | ||||||||||
IdleInstr | +$0E | The current IdleInstr being executed.
See "Idle Loop Instructions". | ||||||||||
IdlePC | +$0F | Pointer to the next idle loop instruction to execute. | ||||||||||
IdleScript | +$11 | Pointer to the start point of the current idle loop. | ||||||||||
+$13 to +$1F | Presumed empty |
If a sprite is animated, the corresponding slot in the scripting context array will also run.
Field | Offset | |
---|---|---|
+$00 | Purpose unknown. Zeroed upon initialization. | |
Delay | +$01 | Amount of frames to wait until next frame of animation. |
FileBank | +$02 | ROM bank of the start of the script's BugFS file. |
FilePtr | +$03 | ROM pointer of the start of the script's BugFS file. |
AnimScript | +$05 | Pointer to start of the current animation script. |
AnimPC | +$07 | Pointer to the next animation instruction to execute. |
SpriteIndex | +$09 | Index of the metasprite slot that this animation serves.
In all known instances, the script context slot index matches the metasprite it serves, even though it's not strictly necessary. |
SpritePtr | +$0A | Pointer to the metasprite slot that this animation serves.
This appears to be strictly a convenience feature; the sprite pointer can be generated from the index using four instructions. |
+$0C to +$0F | Presumed empty |
Sprite Script Engines
Yes, Bugsite has two of them.
Idle Loop Instructions
Idle loops are small lists of instructions that dictate how a non-player-controlled sprite, such as an NPC, will move across the screen. Each instruction is preceded by a repeat count; each instruction will execute that many times before moving to the next idle loop. A maximum of one idle instruction will execute per frame, and idle instructions wait for animations to finish before proceeding. Idle loop opcodes do not have operands, and only one flow control instruction.
Setting a repeat value of 0 causes the loop to stop executing instructions. The current instruction will still execute.
Opcode | Encoding(s) | Native Impl. | Description |
---|---|---|---|
ENOP $nn | $00-$4F | N/A | Treated as effective NOP by the script interpreter. |
INOP $50 | $50 | $33F7 | NOP |
INOP $51 | $51 | $33FA | NOP |
INOP $52 | $52 | $33FD | NOP |
IDOWN | $53 | $341F | Run the sprite's move-down animation. |
IUP | $54 | $3436 | Run the sprite's move-up animation. |
ILEFT | $55 | $344D | Run the sprite's move-left animation. |
IRIGHT | $56 | $3464 | Run the sprite's move-right animation. |
INOP $57 | $57 | $347B | NOP |
IWAIT | $58 | $3400 | Run the stationary animation for the sprite's current direction a random number of times, from $0 to $20.
The repeat value of this instruction is ignored. |
ISTOP | $59 | $341C | Run the stationary animation for the sprite's current direction. |
EFGAME $nn | $60-$EF | N/A | Invalid opcodes, will likely crash game. |
IHALT | $F0 | $33C0 | Reset the idle loop back to IdleScript.
The repeat value of this instruction is always treated as 0 (e.g. halt). |
IRESET | $F1 | $33E2 | Reset the idle loop back to IdleScript. |
EFGAME $nn | $F2-$FF | N/A | Invalid opcodes, will likely crash game. |
Please keep in mind that directional commands such as IDOWN, IUP, ILEFT, and IRIGHT do not actually move the sprite. Instead, they queue the animation script associated with that direction. The animation script itself moves the sprite; it's only by convention that, e.g. IDOWN always executes a script that moves the sprite down two tiles. Furthermore, script execution is predicated upon the target tiles being free; if they are occupied, then the idle script will halt until that slot becomes free. This has the following consequences:
- The movement path specified by the idle script will be followed exactly.
- Sprites can get 'stuck' on stationary walls, or even each other.
- NPCs will 'box in' players, never moving until the player moves out of their way, as the player is just another sprite.
Animation Script Instructions
This scripting engine does the actual job of specifying how a sprite will animate from frame to frame when moving across the screen.
Move Permissions
Bugsite handles collision detection with an array covering the whole of WRAM6; with a backup copy at WRAM7 for restoring old data in the array.
The array appears to exclusively bit 7 zero for unoccupied tiles and bit 7 one for occupied tiles. This is a rather inefficient use of working memory, even with the GBC's copious amounts of it; though it's unknown if the lower 7 bits of each tile byte stores flags or data other than the occupation state of the tile.