easy AI Proxy - a simple player detection script for your monsters 4.5.9

SHLIM

Active member
Hi guys, theres a few AI proxy scripts floating around which all seem to have various issues when trying to use them. The best i could find was the tutorial by Craigery HERE. However, that still had various issues with it (ie monsters always had blind spots and there was no screen edge check) so i took the liberty of making a new one that works without any hassles.

Please note that this will only work on single screens, It will not work with scrolling!

To start the installation you are first going to need to have access to UserScreenBytes by completing this tutorial HERE
Now copy and paste the code below into a new ASM file called easyAIProxy.asm and save in your BASE_4_5\Game\AI_Scripts folder.

Code:
;; easyAIProxy by SHLIM
;;userScreenByte1 = Facing Proxy Distance
;;userScreenByte2 = Peripheral Proxy Distance

;; transfer Monster
TXA ;; move x to a (monster is already stored in x)
STA temp ;; store monster in temp

LDX player1_object ;; load player
LDA Object_x_hi,x ;; horizontal position
ADC #$08 ;;this moves the X position to the players center (if using a 16 pixel wide player)
STA temp2
LDA Object_y_hi,x ;; vertical position
STA tempB

JSR getPointColTable ;; here you can use a special tile to hide the player if necessary
CheckCollisionPoint temp2, tempB, #$0C, tempA ;; checks if player is on special tile
    BNE +notBehindObject
    JMP +doneProxy
    +notBehindObject

;;Load Monster
LDX temp
LDA Object_x_hi,x
ADC #$08 ;;adds 8 pixels so it is centered on 16 pixel wide monster. Change if necessary
STA tempC ;; horizontal
LDA Object_y_hi,x
STA tempD ;; vertical

;; check enemy direction
LDA Object_direction,x
CMP #%00000110
BEQ +NPCfacesLeft
CMP #%00000010
BEQ +NPCfacesRight
CMP #%00000100
BEQ +NPCfacesUp
CMP #%00000000
BEQ +NPCfacesDown
JMP +doneProxy

+NPCfacesLeft ;; npc is facing left
LDA tempC ;; monsters hori position
CMP temp2 ;; compare to players hori position
BCC +gotodoneProxy ;; if player is behind enemy end
CMP userScreenByte1 ;; compare byte to enemys position (check for screen edge)
BCC +checkPeripheralU ;; if distance is more than space left on screen we know player is in range so skip next check
SBC userScreenByte1 ;; deduct byte from temp c
CMP temp2 ;; compare to see if carry is now set (checks if player is now behind enemy once byte is deducted, this indicates hes in range)
BCC +checkPeripheralU ;; if temp2 is now higher than tempc
JMP +doneProxy

+NPCfacesRight
LDA tempC
CMP temp2
BCS +doneProxy
CMP userScreenByte1
BCS +checkPeripheralU
ADC userScreenByte1
CMP temp2
BCS +checkPeripheralU
JMP +doneProxy

+checkPeripheralU ;; check sides for l/r sight
LDA tempD
SBC userScreenByte2
CMP tempB
BCC +checkPeripheralD
JMP +doneProxy

+checkPeripheralD   ;; check sides for l/r sight
LDA tempD
ADC userScreenByte2
CMP tempB
BCS +doAttack
JMP +doneProxy

gotodoneProxy:
JMP +doneProxy

+NPCfacesUp
LDA tempD
CMP tempB
BCC +doneProxy
CMP userScreenByte1
BCC +checkPeripheralL ;; if byte is higher than monster we can skip next check as we know player is in range of screen wrap
SBC userScreenByte1 ;;
CMP tempB
BCC +checkPeripheralL
JMP +doneProxy

+NPCfacesDown
LDA tempD
CMP tempB
BCS +doneProxy
CMP userScreenByte1
BCS +checkPeripheralL
ADC userScreenByte1
CMP tempB
BCS +checkPeripheralL
JMP +doneProxy

+checkPeripheralL ;; check sides for up/down sight
LDA tempC
SBC userScreenByte2
CMP temp2
BCC +checkPeripheralR
JMP +doneProxy

+checkPeripheralR ;; check sides for up/down sight
LDA tempC
ADC userScreenByte2
CMP temp2
BCS +doAttack
JMP +doneProxy

+doAttack
ChangeActionStep temp, #$05 ;; Action Step 5

+doneProxy

Now assign this script to one of your Monster action steps and set UserScreeByte1 to how far youd like your monsters forward vision to be, and set UserScreeByte2 to your monsters wide vision. These numbers are calculated in pixels and you will need to set them for each screen the easyAIProxy script will be used on. If you do not set either of the bytes then the script will not execute.

This script will work no matter where you place your monster, it will check for the screen edge and all fields of vision dependant on the direction that the enemy is facing! Youl also see theres some tile detection code added which was created by BucketMouse! With this you can create special tiles which if the player is stood on it will act as if the player is hiding from the NPC and will not be seen!

And thats it :) easy peasy!
Love from SHLIM
 
Last edited:

SHLIM

Active member
Interesting.
Does it also work with the scrolling modules?
i havent tested it with scrolling, but it should work fine. it compares the distance between player and monster so i dont see why it wouldnt work with scrolling, but if anyone has a scrolling game they could test it on then that would be great!
 

dale_coop

Moderator
Staff member
Because in the scrolling, when you're (player) on a screen at horizontal x position #$90... and a monster is on the next screen (to the right) at a position of #$80... the monster is not next to you on your left... he's actually on your right and at a quite far distance.
Position/distance comparisons with scrolling (multiple screens) is more complex.
 

SHLIM

Active member
Because in the scrolling, when you're (player) on a screen at horizontal x position #$90... and a monster is on the next screen (to the right) at a position of #$80... the monster is not next to you on your left... he's actually on your right and at a quite far distance.
Position/distance comparisons with scrolling (multiple screens) is more complex.
ahh, well in that case it most likely wont work with scrolling then. this uses the monster/player location and then determins if they are in range of each other! so if scrolling loops the same screen positions as if its a single screen game then it would cause issues, as like you mentioned a player on the left could be seen as if they were on the right!
 

dale_coop

Moderator
Staff member
@SHLIM Maybe you should mention in your tutorial that is ONLY for non scrolling projets. This will spare you the comments from people saying it doesn't work on their MetroidVania/SuperMario clone game ;)
 
Would this AI script work in a scrolling game in a boss room where it's a single screen none scrolling? Or when left and right are ticked? And will it still pull data from and adjacent screen?
 

SciNEStist

Well-known member
Here's a version of a proximity script I use sometimes. I put it in an ai behaviour script. I'm sure it can be rewritten more efficiently. its super basic so it doesnt take hitboxes into account, its just measuring the basic position of the player and the monster (the top left corner of each, to be precise) so if you have dramatically different sized enemies and players, this might not be for you.

Code:
RANGE = #$10 ; minimum distance needed to trigger

TXA
STA temp
LDA #$00
STA tempx
LDY player1_object

LDA Object_screen, y
CMP Object_screen, x
BNE +notclose
    
LDA Object_x_hi,x
CMP Object_x_hi,y
BCC +less
JMP +more
    
+less
 LDA Object_x_hi,y
 SBC Object_x_hi,x
 JMP +testH
+more
 LDA Object_x_hi,x
 SBC Object_x_hi,y
 
+testH
 CMP #SWINGDIST
 BCC +iscloseH
 JMP +notclose

+iscloseH
 LDA Object_y_hi,x
 CMP Object_y_hi,y
 BCC +less
 JMP +more
    
+less
 LDA Object_y_hi,y
 SBC Object_y_hi,x
 JMP +testV
+more
 LDA Object_y_hi,x
 SBC Object_y_hi,y

+testV
 CMP #RANGE
 BCC +iscloseV
 JMP +notclose

+iscloseV
;MONSTER AND PLAYER1 IS WITHIN DISTANCE
    ;DO WHATEVER YOU WANT HERE
+notclose
 
Top Bottom