Prev: 37980 Up: Map Next: 38193
37990: Spawn Alien
Roll a 1-in-4 chance, then copy a level-appropriate alien into a free slot.
SpawnAlien 37990 LD A,R A=random number.
37992 AND %00000011 Keep only bits 0-1.
37994 RET NZ Return if the value is not zero.
37995 LD A,(24167) Fetch GameDelayTimer.
37998 AND A
See No Enemies.
SpawnAlien_TestDelay 37999 RET NZ Return if the delay is still running.
38000 CALL FindSlotAlien Call FindSlotAlien.
38003 RET NZ Return if there were no available alien slots.
38004 PUSH HL Stash the free alien slot on the stack.
38005 EX DE,HL Switch the DE and HL registers.
38006 LD A,(24128) Fetch RandomNumber.
38009 RLA Rotate it left once.
38010 AND %00001110 Keep only bits 1-3.
38012 LD C,A Create an even offset in BC.
38013 LD B,0
Pick the alien table for this level (cap at level 12).
38015 LD A,(24224) Fetch ActivePlayer_Level.
38018 CP 12 Jump to SpawnAlien_PickOffset if the current level is less than 12.
38020 JR C,SpawnAlien_PickOffset
38022 LD A,12 A=12.
SpawnAlien_PickOffset 38024 AND %00001110 Keep only bits 1-3.
38026 LD L,A Create an offset in HL - number can be:
  • 0000
  • 0002
  • 0004
  • 0006
  • 0008
  • 0010
  • 0012
38027 LD H,0
Look up the template for the alien to spawn.
38029 LD DE,40306 Point HL into LevelsAlienLookupTable.
38032 ADD HL,DE
38033 LD A,(HL) Fetch the alien table for this level.
38034 INC HL
38035 LD H,(HL)
38036 LD L,A
38037 ADD HL,BC Add the random offset.
38038 LD A,(HL) Fetch that alien's template.
38039 INC HL
38040 LD H,(HL)
38041 LD L,A
38042 LD BC,24 Set the byte count in BC to 24 for one actor slot.
38045 POP DE Restore the free slot from the stack, but stash a copy of it on the stack again.
38046 PUSH DE
38047 LDIR Copy the alien data into the free slot.
38049 POP HL HL=the free alien slot.
Place the alien relative to Jetman_Position, with special paths for types 136 and 184.
38050 LD A,(HL) Fetch the alien type.
38051 INC HL Move HL onto the world X.
38052 INC HL
38053 PUSH HL Stash it on the stack.
38054 LD HL,(24698) Fetch Jetman_Position.
38057 CP 136 Jump to SpawnAlien_Type88 if the type is 136.
38059 JR Z,SpawnAlien_Type88
38061 CP 184 Jump to SpawnAlien_TypeB8 if the type is 184.
38063 JP Z,SpawnAlien_TypeB8
Place any other alien a random distance either side of the scroll position.
38066 LD A,(24128) Fetch RandomNumber.
38069 LD C,A Store it as an 8-bit offset in BC.
38070 LD B,0
Place the alien left of the scroll position if bit 1 of the random number is set, else right.
38072 LD DE,128 DE=0128.
38075 AND %00000010 Keep only bit 1.
38077 JR Z,SpawnAlien_AddScroll Jump to SpawnAlien_AddScroll if that bit is reset.
38079 SBC HL,BC Subtract the offset from the scroll position.
38081 SBC HL,DE
38083 JR SpawnAlien_MaskWorldX Jump to SpawnAlien_MaskWorldX.
SpawnAlien_AddScroll 38085 ADD HL,BC Add the offset to the scroll position.
38086 ADD HL,DE
Wrap the position into the world.
SpawnAlien_MaskWorldX 38087 LD A,H Fetch the high byte.
38088 AND %00011111 Keep only bits 0-4.
38090 LD H,A Write it back to H.
38091 EX DE,HL Copy that position into DE.
Store the world X and randomly reverse the horizontal speed.
38092 POP HL Restore the slot pointer from the stack.
38093 LD (HL),E Write the world X into the slot.
38094 INC HL
38095 LD (HL),D
38096 INC HL Move onto the horizontal speed.
38097 INC HL
38098 LD A,C Fetch the random byte.
38099 AND %10000000 Keep only bit 7.
38101 RET Z Return if that bit is reset.
38102 LD A,(HL) Negate the horizontal speed.
38103 NEG
38105 LD (HL),A
38106 RET Return.
Type 136: spawn off the left or right edge.
SpawnAlien_Type88 38107 LD A,(24128) Fetch RandomNumber.
38110 LD B,A Stash it in B.
38111 AND %10000000 Keep only bit 7.
38113 SUB 64 Subtract 64 to make the speed.
38115 LD C,A Copy the speed into C.
Pick the edge: a positive speed spawns off the left, otherwise off the right.
38116 JP P,SpawnAlien_LeftEdge Jump to SpawnAlien_LeftEdge if the result is positive.
38119 LD DE,144 DE=0144.
38122 JR SpawnAlien_ApplyEdge Jump to SpawnAlien_ApplyEdge.
SpawnAlien_LeftEdge 38124 LD DE,65392 DE=65392.
Apply the edge offset to the scroll position and wrap it.
SpawnAlien_ApplyEdge 38127 ADD HL,DE Add the edge offset to the scroll position.
38128 LD A,H Fetch the high byte.
38129 AND %00011111 Keep only bits 0-4.
38131 LD H,A Write it back to H.
38132 EX DE,HL Copy that position into DE.
Store the world X, then set a random Y position and the speed.
38133 POP HL Restore the slot pointer from the stack.
38134 LD (HL),E Write the world X into the slot.
38135 INC HL
38136 LD (HL),D
38137 INC HL Move onto the Y position.
Choose a random Y position.
38138 LD A,B Fetch the random byte.
38139 AND %01111000 Keep only bits 3-6.
38141 JR NZ,SpawnAlien_WriteY Jump to SpawnAlien_WriteY if those bits are not zero, else force 1.
38143 INC A
SpawnAlien_WriteY 38144 ADD A,55 Add 55 to make a Y position.
38146 LD (HL),A Write the Y position.
38147 INC HL Move onto the speed.
38148 LD (HL),C Write the speed.
38149 RET Return.
Type 184: the same edge spawn as type 136.
SpawnAlien_TypeB8 38150 LD A,(24128) Fetch RandomNumber.
38153 LD B,A Stash it in B.
38154 AND %10000000 Keep only bit 7.
38156 SUB 64 Subtract 64 to make the speed.
38158 LD C,A Copy the speed into C.
Pick the edge: a positive speed spawns off the left, otherwise off the right.
38159 JP P,SpawnAlien_TypeB8_LeftEdge Jump to SpawnAlien_TypeB8_LeftEdge if the result is positive.
38162 LD DE,144 DE=0144.
38165 JR SpawnAlien_TypeB8_ApplyEdge Jump to SpawnAlien_TypeB8_ApplyEdge.
SpawnAlien_TypeB8_LeftEdge 38167 LD DE,65392 DE=65392.
Apply the edge offset to the scroll position and wrap it.
SpawnAlien_TypeB8_ApplyEdge 38170 ADD HL,DE Add the edge offset to the scroll position.
38171 LD A,H Fetch the high byte.
38172 AND %00011111 Keep only bits 0-4.
38174 LD H,A Write it back to H.
38175 EX DE,HL Copy that position into DE.
Store the world X, then set a random Y position and the speed.
38176 POP HL Restore the slot pointer from the stack.
38177 LD (HL),E Write the world X into the slot.
38178 INC HL
38179 LD (HL),D
38180 INC HL Move onto the Y position.
Choose a random Y position.
38181 LD A,B Fetch the random byte.
38182 AND %01111000 Keep only bits 3-6.
38184 JR NZ,SpawnAlien_TypeB8_WriteY Jump to SpawnAlien_TypeB8_WriteY if those bits are not zero, else force 1.
38186 INC A
SpawnAlien_TypeB8_WriteY 38187 ADD A,55 Add 55 to make a Y position.
38189 LD (HL),A Write the Y position.
38190 INC HL Move onto the speed.
38191 LD (HL),C Write the speed.
38192 RET Return.
Prev: 37980 Up: Map Next: 38193