WIP: reworking room flag access
authorLukas Krickl <lukas@krickl.dev>
Wed, 4 Dec 2024 07:11:11 +0000 (08:11 +0100)
committerLukas Krickl <lukas@krickl.dev>
Wed, 4 Dec 2024 07:11:11 +0000 (08:11 +0100)
src/collision.s
src/macros.inc
src/map.s

index 5d84f71722e5a328b9f3911ba6bd7005efcaa24c..4ecd53c8b09e1b4016210edd1e6b5bdf3c9b9791 100644 (file)
@@ -35,8 +35,6 @@ collision_player:
   col_head 1
   col_point 0, 0
 
-collision_tile_lut:
-  .rep cti, ROOM_H, 1, .db cti * ROOM_W 
 
   ; checks an entire collision table based on an original point
   ; and a ptr to a collision point table
@@ -108,45 +106,7 @@ collision_tile_table_check:
   ;   a = 0 -> no collision
   ;   a = 1 -> collision
 collision_tile:
-  ; y pos / 16 -> tile pos
-  srl d ; / 2
-  srl d ; / 4
-  srl d ; / 8
-  srl d ; / 16
-
-  ; x pos / 16 -> tile pos 
-  srl e ; / 2
-  srl e ; / 4
-  srl e ; / 8
-  srl e ; / 16
-  ld a, e ; a = x pos
-
-  ld e, d
-  ld d, 0 ; de = y position now
-
-  ; -> now convert position to 
-  ; map flag index using a simple lut
-  ; for y position + x
-  ld hl, collision_tile_lut
-  add hl, de ; hl + y offset 
-  ; hl = row ptr
-  ld e, a ; e = x pos
-  ld a, [hl] ; a = map flags row offset 
-  add a, e ; row + x = position of tile in map 
-
-  push af
-
-  ld hl, curr_room_flags ; hl = flags for currently loaded room 
-  ld a, [hl+] ; load curr room flags ptr
-  ld e, a 
-  ld a, [hl]
-  ld d, a ; de = flags pointer 
-
-  pop af
-  ld h, 0
-  ld l, a ; hl = tile offset 
-  add hl, de ; hl + de 
-
+  call room_get_flag_ptr
   ; hl = current tile flags 
 
   ld a, [hl] ; load flags
index a0fd3c86e111fc8b658f6c58df966b69806d9ea5..91a04e938d06d0db8cc11b3289f45d0618b6a7e2 100644 (file)
   ld e, a 
   add hl, de ; hl = correct tile index 
   ld a, [hl]
+#endmacro
+
+  ; divides a regiser by 16
+  ; inputs:
+  ;   $1: the register 
+#macro div16 
+  srl $1 ; / 2
+  srl $1 ; / 4
+  srl $1 ; / 8
+  srl $1 ; / 16
 #endmacro 
index 41acf8974c6f454ed3deb82f1d2530a9e06aa4ff..d6e4409bfd7509a306b3ad46513a1db71a5d270b 100644 (file)
--- a/src/map.s
+++ b/src/map.s
@@ -195,6 +195,51 @@ room_draw:
 
   ret
 
+room_tile_lut:
+  .rep cti, ROOM_H, 1, .db cti * ROOM_W 
+
+  ; loads the flag pointer for a position into hl
+  ; inputs:
+  ;   de: y/x
+  ; returns:
+  ;   hl: pointer to flag at position y/x 
+  ; uses:
+  ;   de, hl, a
+room_get_flag_ptr:
+  ; y pos / 16 -> tile pos
+  div16 d
+
+  ; x pos / 16 -> tile pos 
+  div16 e
+  ld a, e ; a = x pos
+
+  ld e, d
+  ld d, 0 ; de = y position now
+
+  ; -> now convert position to 
+  ; map flag index using a simple lut
+  ; for y position + x
+  ld hl, room_tile_lut
+  add hl, de ; hl + y offset 
+  ; hl = row ptr
+  ld e, a ; e = x pos
+  ld a, [hl] ; a = map flags row offset 
+  add a, e ; row + x = position of tile in map 
+
+  push af
+
+  ld hl, curr_room_flags ; hl = flags for currently loaded room 
+  ld a, [hl+] ; load curr room flags ptr
+  ld e, a 
+  ld a, [hl]
+  ld d, a ; de = flags pointer 
+
+  pop af
+  ld h, 0
+  ld l, a ; hl = tile offset 
+  add hl, de ; hl + de 
+  ret
+
   ; sets a tile position flag
   ; as a flag  
   ; for actor -> actor collision detection