Craigery
Active member
I have written an AI script that will change the monsters Action Step when the player gets close enough to it. It will detect a different area depending on which direction the monster is facing.
To continue you will need to enable the userScreenBytes variables as see in this helpful tutorial by chronicleroflegends. This allows you to change how far the monsters will look in front of them on a screen by screen basis, so if you want different visibility depending on your screen its easy to change. The only draw back is you need to set userScreenByte values for every screen.
How to Setup This Code:
Copy and paste this code to a new ASM file called AI_DirectionalProxy.asm and save in your BASE_4_5\Game\AI_Scripts folder.
In NESMaker go to Settings -> Script Settings -> Assign our new AI_DirectionProxy.ASM to an empty spot under AI Behaviors.
Be sure to change the Project Label to something like DirectionProxy.
Under your monsters Object Details set the Action of your action step to the new Directional Proxy and set your EndAction/EndAnimation to Repeat. The code only looks for the player once so we need to to repeat.
Here is How it Works:
First we determine the monster's facing direction. Then we compare the player X position against the monster's X position, against minus (left) or plus (right) the userScreenByte1's value (this is how many pixels in the monster's facing directions we should look for the player), then we will check the monster's peripheral vision being userScreenByte2's value it will check on each side. If the player is within that XY area it will change to Action Step 5.
Our Variables:
userScreenByte1 is the distance, in pixels, the monster will search in the direction they are facing.
userScreenByte2 is the peripheral vision of the monster. The userScreenByte2 value is checked on each side of the monsters peripheral vision, so if userScreenByte2 is set to search 32 pixels, it will search 32 going in each direction for a total of 64 pixels.
Things to know:
This code was very difficult for me learn and write, but I finally got my head wrapped around it and am getting more confident in my ASM skills with NESMaker. I hope some other people get some use out of this!
To continue you will need to enable the userScreenBytes variables as see in this helpful tutorial by chronicleroflegends. This allows you to change how far the monsters will look in front of them on a screen by screen basis, so if you want different visibility depending on your screen its easy to change. The only draw back is you need to set userScreenByte values for every screen.
How to Setup This Code:
Copy and paste this code to a new ASM file called AI_DirectionalProxy.asm and save in your BASE_4_5\Game\AI_Scripts folder.
;;userScreenByte1 = Facing Proxy Distance
;;userScreenByte2 = Peripheral Proxy Distance
;;Load Player X and Y
TXA
STA temp
PHA
LDX player1_object
;;Load and Save X with offset to center of player, and Y
LDA Object_x_hi,x
ADC #$08 ;;this moves the X to the player center
STA tempA
LDA Object_y_hi,x
STA tempB
LDA Object_screen,x
STA temp1
;;Load Monster direction
PLA
TAX
LDX temp
;LDA temp1
;CMP Object_screen,x
;BEQ +doneProxy
;;Check if Left Right
LDA Object_x_hi,x
ADC #$08 ;;adds 8 pixels so it is centered on 16 pixel wide monster.
STA tempC
LDA Object_y_hi,x
STA tempD
LDA Object_direction,x
CMP #%00000110
BEQ +isLeftUp
CMP #%00000010
BEQ +isRightDown
;;check Up Down
LDA Object_y_hi,x
STA tempC
LDA Object_x_hi,x
ADC #$08 ;;adds 8 pixels so it is centered on 16 pixel wide monster.
STA tempD
LDA Object_direction,x
CMP #%00000100
BEQ +isLeftUp
CMP #%00000000
BEQ +isRightDown
JMP +doneProxy
+isLeftUp
LDA tempC
CMP tempA
BCC +doneProxy
LDA tempC
SBC userScreenByte1
CMP tempA
BCC +checkPeripheralU
JMP +doneProxy
+isRightDown
LDA tempC
CMP tempA
BCS +doneProxy
ADC userScreenByte1
CMP tempA
BCS +checkPeripheralU
JMP +doneProxy
+checkPeripheralU
LDA tempD
SBC userScreenByte2
CMP tempB
BCC +checkPeripheralD
JMP +doneProxy
+checkPeripheralD
LDA tempD
ADC userScreenByte2
CMP tempB
BCS +doAttack
JMP +doneProxy
+doAttack
ChangeActionStep temp, #$05
+doneProxy
In NESMaker go to Settings -> Script Settings -> Assign our new AI_DirectionProxy.ASM to an empty spot under AI Behaviors.
Be sure to change the Project Label to something like DirectionProxy.
Under your monsters Object Details set the Action of your action step to the new Directional Proxy and set your EndAction/EndAnimation to Repeat. The code only looks for the player once so we need to to repeat.
Here is How it Works:
First we determine the monster's facing direction. Then we compare the player X position against the monster's X position, against minus (left) or plus (right) the userScreenByte1's value (this is how many pixels in the monster's facing directions we should look for the player), then we will check the monster's peripheral vision being userScreenByte2's value it will check on each side. If the player is within that XY area it will change to Action Step 5.
Our Variables:
userScreenByte1 is the distance, in pixels, the monster will search in the direction they are facing.
userScreenByte2 is the peripheral vision of the monster. The userScreenByte2 value is checked on each side of the monsters peripheral vision, so if userScreenByte2 is set to search 32 pixels, it will search 32 going in each direction for a total of 64 pixels.
Things to know:
- The center point for this script is the monsters feet. My monster is 16 pixels wide, so I have moved the center point 8 pixels to the right to actually be center. Your monster may be different.
- You don't have to use the userScreenBytes variables, you could have a set value in there if that works for you. In my game I needed to to be flexible like if I wanted monsters to see farther in later levels to make it more difficult.
- If you leave userScreenByte1 or 2 at 0 it won't detect anything.
- When the player is seen it changes to Action Step 5. You may want to make Action Step 5 a MoveToPlayer script or ShootAtPlayer.
This code was very difficult for me learn and write, but I finally got my head wrapped around it and am getting more confident in my ASM skills with NESMaker. I hope some other people get some use out of this!
Last edited: