Prev: 945C Up: Map Next: 9531
9466: Spawn Alien
Roll a 1-in-4 chance, then copy a level-appropriate alien into a free slot.
SpawnAlien 9466 LD A,R A=random number.
9468 AND %00000011 Keep only bits 0-1.
946A RET NZ Return if the value is not zero.
946B LD A,($5E67) Fetch GameDelayTimer.
946E AND A
See No Enemies.
SpawnAlien_TestDelay 946F RET NZ Return if the delay is still running.
9470 CALL FindSlotAlien Call FindSlotAlien.
9473 RET NZ Return if there were no available alien slots.
9474 PUSH HL Stash the free alien slot on the stack.
9475 EX DE,HL Switch the DE and HL registers.
9476 LD A,($5E40) Fetch RandomNumber.
9479 RLA Rotate it left once.
947A AND %00001110 Keep only bits 1-3.
947C LD C,A Create an even offset in BC.
947D LD B,$00
Pick the alien table for this level (cap at level 0C).
947F LD A,($5EA0) Fetch ActivePlayer_Level.
9482 CP $0C Jump to SpawnAlien_PickOffset if the current level is less than 0C.
9484 JR C,SpawnAlien_PickOffset
9486 LD A,$0C A=0C.
SpawnAlien_PickOffset 9488 AND %00001110 Keep only bits 1-3.
948A LD L,A Create an offset in HL - number can be:
  • 0000
  • 0002
  • 0004
  • 0006
  • 0008
  • 000A
  • 000C
948B LD H,$00
Look up the template for the alien to spawn.
948D LD DE,$9D72 Point HL into LevelsAlienLookupTable.
9490 ADD HL,DE
9491 LD A,(HL) Fetch the alien table for this level.
9492 INC HL
9493 LD H,(HL)
9494 LD L,A
9495 ADD HL,BC Add the random offset.
9496 LD A,(HL) Fetch that alien's template.
9497 INC HL
9498 LD H,(HL)
9499 LD L,A
949A LD BC,$0018 Set the byte count in BC to 18 for one actor slot.
949D POP DE Restore the free slot from the stack, but stash a copy of it on the stack again.
949E PUSH DE
949F LDIR Copy the alien data into the free slot.
94A1 POP HL HL=the free alien slot.
Place the alien relative to Jetman_Position, with special paths for types 88 and B8.
94A2 LD A,(HL) Fetch the alien type.
94A3 INC HL Move HL onto the world X.
94A4 INC HL
94A5 PUSH HL Stash it on the stack.
94A6 LD HL,($607A) Fetch Jetman_Position.
94A9 CP $88 Jump to SpawnAlien_Type88 if the type is 88.
94AB JR Z,SpawnAlien_Type88
94AD CP $B8 Jump to SpawnAlien_TypeB8 if the type is B8.
94AF JP Z,SpawnAlien_TypeB8
Place any other alien a random distance either side of the scroll position.
94B2 LD A,($5E40) Fetch RandomNumber.
94B5 LD C,A Store it as an 8-bit offset in BC.
94B6 LD B,$00
Place the alien left of the scroll position if bit 1 of the random number is set, else right.
94B8 LD DE,$0080 DE=0080.
94BB AND %00000010 Keep only bit 1.
94BD JR Z,SpawnAlien_AddScroll Jump to SpawnAlien_AddScroll if that bit is reset.
94BF SBC HL,BC Subtract the offset from the scroll position.
94C1 SBC HL,DE
94C3 JR SpawnAlien_MaskWorldX Jump to SpawnAlien_MaskWorldX.
SpawnAlien_AddScroll 94C5 ADD HL,BC Add the offset to the scroll position.
94C6 ADD HL,DE
Wrap the position into the world.
SpawnAlien_MaskWorldX 94C7 LD A,H Fetch the high byte.
94C8 AND %00011111 Keep only bits 0-4.
94CA LD H,A Write it back to H.
94CB EX DE,HL Copy that position into DE.
Store the world X and randomly reverse the horizontal speed.
94CC POP HL Restore the slot pointer from the stack.
94CD LD (HL),E Write the world X into the slot.
94CE INC HL
94CF LD (HL),D
94D0 INC HL Move onto the horizontal speed.
94D1 INC HL
94D2 LD A,C Fetch the random byte.
94D3 AND %10000000 Keep only bit 7.
94D5 RET Z Return if that bit is reset.
94D6 LD A,(HL) Negate the horizontal speed.
94D7 NEG
94D9 LD (HL),A
94DA RET Return.
Type 88: spawn off the left or right edge.
SpawnAlien_Type88 94DB LD A,($5E40) Fetch RandomNumber.
94DE LD B,A Stash it in B.
94DF AND %10000000 Keep only bit 7.
94E1 SUB $40 Subtract 40 to make the speed.
94E3 LD C,A Copy the speed into C.
Pick the edge: a positive speed spawns off the left, otherwise off the right.
94E4 JP P,SpawnAlien_LeftEdge Jump to SpawnAlien_LeftEdge if the result is positive.
94E7 LD DE,$0090 DE=0090.
94EA JR SpawnAlien_ApplyEdge Jump to SpawnAlien_ApplyEdge.
SpawnAlien_LeftEdge 94EC LD DE,$FF70 DE=FF70.
Apply the edge offset to the scroll position and wrap it.
SpawnAlien_ApplyEdge 94EF ADD HL,DE Add the edge offset to the scroll position.
94F0 LD A,H Fetch the high byte.
94F1 AND %00011111 Keep only bits 0-4.
94F3 LD H,A Write it back to H.
94F4 EX DE,HL Copy that position into DE.
Store the world X, then set a random Y position and the speed.
94F5 POP HL Restore the slot pointer from the stack.
94F6 LD (HL),E Write the world X into the slot.
94F7 INC HL
94F8 LD (HL),D
94F9 INC HL Move onto the Y position.
Choose a random Y position.
94FA LD A,B Fetch the random byte.
94FB AND %01111000 Keep only bits 3-6.
94FD JR NZ,SpawnAlien_WriteY Jump to SpawnAlien_WriteY if those bits are not zero, else force 01.
94FF INC A
SpawnAlien_WriteY 9500 ADD A,$37 Add 37 to make a Y position.
9502 LD (HL),A Write the Y position.
9503 INC HL Move onto the speed.
9504 LD (HL),C Write the speed.
9505 RET Return.
Type B8: the same edge spawn as type 88.
SpawnAlien_TypeB8 9506 LD A,($5E40) Fetch RandomNumber.
9509 LD B,A Stash it in B.
950A AND %10000000 Keep only bit 7.
950C SUB $40 Subtract 40 to make the speed.
950E LD C,A Copy the speed into C.
Pick the edge: a positive speed spawns off the left, otherwise off the right.
950F JP P,SpawnAlien_TypeB8_LeftEdge Jump to SpawnAlien_TypeB8_LeftEdge if the result is positive.
9512 LD DE,$0090 DE=0090.
9515 JR SpawnAlien_TypeB8_ApplyEdge Jump to SpawnAlien_TypeB8_ApplyEdge.
SpawnAlien_TypeB8_LeftEdge 9517 LD DE,$FF70 DE=FF70.
Apply the edge offset to the scroll position and wrap it.
SpawnAlien_TypeB8_ApplyEdge 951A ADD HL,DE Add the edge offset to the scroll position.
951B LD A,H Fetch the high byte.
951C AND %00011111 Keep only bits 0-4.
951E LD H,A Write it back to H.
951F EX DE,HL Copy that position into DE.
Store the world X, then set a random Y position and the speed.
9520 POP HL Restore the slot pointer from the stack.
9521 LD (HL),E Write the world X into the slot.
9522 INC HL
9523 LD (HL),D
9524 INC HL Move onto the Y position.
Choose a random Y position.
9525 LD A,B Fetch the random byte.
9526 AND %01111000 Keep only bits 3-6.
9528 JR NZ,SpawnAlien_TypeB8_WriteY Jump to SpawnAlien_TypeB8_WriteY if those bits are not zero, else force 01.
952A INC A
SpawnAlien_TypeB8_WriteY 952B ADD A,$37 Add 37 to make a Y position.
952D LD (HL),A Write the Y position.
952E INC HL Move onto the speed.
952F LD (HL),C Write the speed.
9530 RET Return.
Prev: 945C Up: Map Next: 9531