Prev: 58106 Up: Map Next: 58316
58166: Handle creature/graphic animation
Used by the routines at 51255 and 59689.
For animated creatures and items, e.g. servant spell cursor, animated sabre.
Some will need left/right facing direction determined (e.g. goblin warrior), others won't (e.g. demon)
This routine advances the animation frame to the next one, or resets it to frame 1, as long as the creature/item isn't affected by an active FREEZE spell.
58166 LD A,(60074) Incrementing timer byte
58169 AND 1 Check bit 1
58171 JP Z,58315 Skip animation routine (straight to RET) every other frame, so that animation rate isn't too fast
58174 LD A,(60227)
58177 CP 3 Is it a creature?
58179 JR NZ,58195 If not, animation shouldn't be affected by FREEZE spell so can skip the check for this
It's a creature, so it'll need animating. Check if creature is affected by FREEZE spell:
58181 LD A,(60238) Creature properties byte
58184 AND 224 If any of the top 3 bits are set, it means the creature is disintegrating/materialising - this animation isn't be affected by the FREEZE spell (the animation should continue)
58186 JR NZ,58195 If any of the 3 bits are set, skip the following FREEZE spell check
If the FREEZE spell is active, creatures do not move:
58188 LD A,(60572) Get FREEZE spell timer counter
58191 CP 0
58193 JR NZ,58315 If it's > 0 the FREEZE spell is active, so skip this routine and go straight to RET
Check if the creature has a left/right facing version of the graphic that needs calculating:
58195 LD A,(60239) Get pre-graphics data byte (Graphic properties)
58198 LD B,A Temp store in B register
58199 AND 64 Bit 6 (64), if set, indicates that this creature has left/right facing graphics (e.g. goblins, guardian of chaos, and wraith)
58201 JR Z,58260 Jump if NOT set (i.e. not any of the above) - creature isn't left/right facing, e.g. the demon)
Figure out whether we need a left or right facing graphic for this item/creature.
Bit 3 (8) of the byte at 60240 indicates whether the creature is facing right (1 - set) or left (0 - not set)
58203 LD A,(60240) Get graphic address frame number offset
58206 LD D,A Temp store in D
58207 AND 8 Get bit 3
58209 LD C,A C now contains either 8 (right) or 0 (left) depending on whether bit 3 was set
58210 LD A,(60236) Movement speed/direction
58213 CP 0 Stationary?
58215 JR Z,58260 If so, don't need to calculate anything - skip to the end
58217 CP 128 Negative value = move left.
58219 JR NC,58238
Creature needs to move right - check if it is already moving/facing in this direction
58221 LD A,C Retrieve byte from C register - if bit 3 (8) of 60240 was set, C will contain 8, otherwise 0.
58222 CP 8 If the byte is 8 it means the creature is already moving right
58224 JR Z,58260 If so, no need to change which way the creature is facing
Creature is moving left but needs to switch to right. Calculate the graphics pointer offset to the opposite facing graphics frame.
58226 LD A,B Retrieve pre-graphics data byte
58227 AND 7 Lowest 3 bits = number of frames
58229 ADD A,A Multiply by 2 as we're dealing with 2 byte graphic address pointers
58230 LD E,A Store offset in E register
58231 LD A,D Get graphic address frame number offset
58232 OR 8 Set bit 4 to indicate the creature is now moving right - for storing at 60240 at 58255
58234 LD D,0 Set high byte (D register) to 0. The offset, now in DE, will be added to the graphics address pointer later.
58236 JR 58255
Creature needs to move left - check if it is already moving/facing in this direction
58238 LD A,C Retrieve byte from C register - if bit 3 (8) of 60240 was set, C will contain 8, otherwise 0.
58239 CP 0 If the byte is 0 it means the creature is already moving left
58241 JR Z,58260 If so, no need to change which way the creature is facing
Creature is moving right but needs to switch to left. Calculate the graphics pointer offset to the opposite facing graphics frame. This needs to be negative as the address pointer needs to move back up the table at 38851.
58243 LD A,B Retrieve pre-graphics data byte
58244 AND 7 Lowest 3 bits = number of frames
58246 ADD A,A Multiply by 2 as we're dealing with 2 byte graphic address pointers
58247 NEG Make negative so when added later, the offset will effectively be subtracted to get to an earlier address pointer
58249 LD E,A Store offset in E register
58250 LD A,D Get graphic address frame number offset
58251 AND 247 Reset bit 4 to indicate the creature is now moving left - for storing at 60240 at 58255
58253 LD D,255 Set high byte (D register) to 255. The negative offset, now in DE, will be added to the graphics address pointer later.
Creature direction and graphic address calculated
58255 LD (60240),A Re-store the graphic address frame number offset, with bit 3 set/unset to indicate right/left facing respectively
58258 JR 58300 As we've just switched direction, we're already changing the graphic frame, so there's no need to change the animation frame again till next time round - so skip the next few sections.
ANIMATE THE CREATURE/GRAPHIC
Advance the animation frame to the next one, or if it's reached the total number of animation frames, reset it to frame 1:
58260 LD A,B Retrieve pre-graphics data byte
58261 AND 128 Bit 7 indicates the graphic has animation frames
58263 JR Z,58315 If NOT set (not animated), jump to straight to RET
Calculate graphics animation frame:
58265 LD A,B Retrieve pre-graphics data byte
58266 AND 7 Get number of frames for this sprite
58268 LD B,A ...and store in B register
58269 LD A,(60240) Get graphic frame number offset for this graphic
58272 LD C,A Temp store in C register
58273 AND 7 Keep bits 0-2 to get the sprite's current frame number
58275 LD H,A Temp store in H register
58276 LD DE,0 DE will be the calculated offset value to re-calculate the graphics frame address pointer shortly
58279 CP B Is the graphic's current frame number <= the total frames for this sprite?
58280 JR C,58293 If so, skip next few instructions, OK to advance to the next frame so jump out here
Frame number has passed total frames for this graphic so we need to reset its frame number:
58282 LD A,C Retrieve current frame number
58283 AND 248 Reset frame number to 0 (stored in bits 0,1,2 so set these to zero)
58285 LD C,A ...And store in C register
Next, return the sprite graphics address pointer to the address for the first frame.
This is done by getting the total number of frames for the graphic, multiplying by 2, and subtracting it from the current frame address.
58286 LD A,H Retrieve the earlier (final) frame number
58287 ADD A,A Double it (as address pointers are 2 bytes)
58288 NEG This value will be added later, so NEG here will mean it gets subtracted
58290 LD E,A Store in E, and set D to 255 to effectively create a 2 byte negative offset
58291 LD D,255
Recalculate new sprite graphics address pointer for the next frame:
58293 LD A,C Retrieve the sprite's frame number...
58294 INC A ...Increment...
58295 LD (60240),A ...and re-store.
58298 INC DE Increment the offset by 2 (to move to the next graphics frame address)
58299 INC DE
Recalculate and re-store sprite frame graphic pointer:
58300 LD HL,(60241) Get the sprite frame graphic pointer
58303 ADD HL,DE Add the offset - either adding two bytes to get to the next frame, or adding the negative offset to return to the first frame
58304 LD (60241),HL ...and re-store
58307 LD A,(60238) Item (creature) properties byte
58310 OR 4 Ensure bit 2 (4) is set - indicates graphic (i.e. the old frame) will need erasing before the new one is drawn
58312 LD (60238),A ...and re-store
58315 RET
Prev: 58106 Up: Map Next: 58316