Advertisement
FoxCunning

MapEntrances

Mar 8th, 2021 (edited)
1,405
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ; (C)2021 Fox Cunning
  2. ; This code is licensed under MIT license
  3. ; (see https://opensource.org/licenses/MIT for details)
  4. ; -----------------------------------------------------------------------------
  5. ; Map data table:
  6. ; Byte 0 = ROM Bank, Byte 1 = Entrance Y, Byte 2 = Entrance X, Byte 3 = Flags (bits 5-7) / ID (bits 0-4)
  7. ;   Flags: bit 7 = dungeon, bit 6 = no guards, bit 5 = continent
  8. ;   ID for continents must be $00-$03, towns and castles $04-$1F
  9. ;   Dungeons are handled separately and can have any ID even if it's the same as a non-dungeon map
  10.  
  11. ; Map pointers table:
  12. ; Bytes 0-1 = Map data pointer, Bytes 2-3 = NPC data pointer
  13.  
  14. ; 48 maps + 8 dungeons
  15. ; 8 town entrances + 2 castle entrances + 8 dungeon entrances per continent
  16. ; Continent indices: $00, $0B, $14, $1F
  17. TownAndCastleEntrances = $E872
  18. DungeonEntrances = $FB50
  19.  
  20. ; Y = A = Tile ID
  21. *=$C427
  22. EntranceTile:
  23.   cmp #$0D  ; Castle tile ID
  24.   beq _towns
  25.   cmp #$0E  ; Dungeon tile ID
  26.   beq _dungeons
  27.  
  28.   ; #$0F = Town tile ID
  29.  
  30. ; The town tile ID is used for the alternative grass tile in towns and castles,
  31. ; so make sure we are in a continent before trying to use it as an entrance
  32. _towns:
  33. ; Make sure this map is a continent
  34.   lda _Map_Flags_ID
  35.   cmp #$04
  36.   bcc $C3FD ; No entrances from this map
  37.  
  38. ; Prepare pointer
  39.   ldy _Map_Flags_ID
  40.   lda TownEntrancePointers,Y
  41.   sta $29   ; Low byte
  42.   lda #$E8  ; High byte always $E8
  43.   sta $2A
  44.  
  45.   ldy #$00
  46. _entranceLoop:
  47.   lda ($29),Y
  48.   cmp _PartyX
  49.   bne _nextEntrance
  50.  
  51.   iny
  52.   lda ($29),Y
  53.   cmp _PartyY
  54.   bne _nextEntrance_1
  55.  
  56. _entranceFound:
  57. ; New map index = current map Id + (Y / 2)
  58.   tya
  59.   lsr A
  60.   clc
  61.   adc _CurrentMapId
  62.   sta _CurrentMapId
  63.   asl A
  64.   asl A
  65.   tax
  66.   ldy #$08
  67.   lda MapDataTable+3,X  ; Map flags
  68.   jsr GetContinentFlag
  69.   sty _Map_Flags
  70.   and #$1F
  71.   sta _Map_Flags_ID
  72.  
  73.   lda _PartyX
  74.   sta $4C       ; Save overworld party location
  75.   lda _PartyY
  76.   sta $4B       ; Will be used when leaving a town/castle
  77.  
  78.   jmp RequestNewMap_Params
  79.  
  80. _nextEntrance:
  81.   iny
  82. _nextEntrance_1:
  83.   iny
  84.   cmp #$14
  85.   bcc _townEntranceLoop
  86.   jmp $C3FD ; No entrance found at these coordinates
  87.  
  88. TownEntrancePointers:
  89. ; Low byte only: high byte is always $E8
  90.   .byte $72, $7C, $86, $90
  91.  
  92. _dungeons:
  93.   ; Backup previous map (needed to set entry point)
  94.   lda _CurrentMapId
  95.   sta $4F
  96.   ; Prepare pointer
  97.   ldy _Map_Flags_ID
  98.   lda DungeonEntrancePointers,Y
  99.   sta $29   ; Low byte
  100.   lda #$FB
  101.   sta $2A   ; High byte is always $FB
  102.  
  103.   ldy #$00
  104. _dungeonEntranceLoop:
  105.   lda ($29),Y
  106.   cmp _PartyX
  107.   bne _nextDungeonEntrance
  108.  
  109.   iny
  110.   lda ($29),Y
  111.   cmp _PartyY
  112.   bne _nextDungeonEntrance_1
  113.  
  114. _dungeonEntranceFound:
  115. ; New map index = $28 + (Y / 2)
  116.   tya
  117.   lsr A
  118.   clc
  119.   adc #$28
  120.   sta _CurrentMapId
  121.   asl
  122.   asl
  123.   tax
  124.   ldy #$09
  125.   lda MapDataTable+3,X  ; Map flags
  126.   sty _Map_Flags
  127.   and #$1F
  128.   sta _Map_Flags_ID
  129.  
  130. ; Don't bother saving the previous party location: it won't be used when
  131. ; leaving a dungeon map
  132.   jmp RequestNewMap_Params
  133.  
  134. _nextDungeonEntrance:
  135.   iny
  136. _nextDungeonEntrance_1:
  137.   iny
  138.   cmp #$10
  139.   bcc _dungeonEntranceLoop
  140.   jmp $C3FD ; No entrance found at these coordinates
  141.  
  142. DungeonEntrancePointers:
  143. ; Low byte only: high byte is always $FB
  144.   .byte $50, $60, $70, $80
  145.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement