Pursuing Tom Ram uses flags to find out what has happened and what has not happened. For example, the game knows if a door is open or closed by assigning a flag to the door and checking its value. It is an example. There are not doors in this game.
Here I use the same technique I used for the exits. To save memory, the flags will be bits. A flag can only have a value of 0 or 1.
; Geeral purpouse flags
; Each flag is 1 bit, so the game have 16 flags
flags
BYTE $0, $0
; Masks to manipulate flags and exits
masks
BYTE %00000001, %00000010, %00000100, %00001000
BYTE %00010000, %00100000, %01000000, %10000000
I have written routines to read and change a particular bit. I do that with masks and binary operators.
I have 8 flags in a byte. If I want to know, for example, the value of flag 5, I read only that bit with AND %00100000. If I have a 0 in the accumulator, the flag is set to 0 and if not 0, the flag is set to 1.
I do an OR to set a flag. For example, I set flag 5 to 1 with OR %00100000.
To set a flag to 0 I use the opposite mask of the one to read. For example, I set flag 5 to 0 with AND #%11011111. I do not need to store this masks because I can generate from the original mask with XOR.
I need eight masks, one mask for each flag. I store them in a vector, instead of creating them with code.
set_x_y
; Innter subroutine
ldx #$0
tya
and masks+$3
beq set_rts
inx
tya
sec
sbc #$8
tay
set_rts
rts
set_flag_y
jsr set_x_y
lda flags,x
ora masks,y
sta flags,x
rts
The mask I need to set a flag to 0 is created by calculating the inverse of the original mask. I use EOR #$FF to calculate the inverse of a byte. In this adventure I do not need to set a flag to 0, except at the start of the game, so this code can be removed to save space.
clear_flag_y
jsr set_x_y
lda masks,y
eor #$FF ; Invert mask
and flags,x
sta flags,x
rts
I use two bytes for flags, so I have 16 flags. I have to check if the flag I want to use is in the first byte (flags 0 to 7), or in the second byte (flags 8 to 15).