Bucket Mouse
Active member
As a programming exercise, I decided to try to make a new monster AI that makes the monster move in the direction of the player at all times.
I tend to learn code tricks by reverse-engineering code that already exists. There's already a routine that moves something in the player's direction: the "Shoot Toward Player" action. So I followed the breadcrumbs. In Macros you have this:
In Zero Out Assets, it's referenced:
And that, in turn, is referenced in ShootTowardsPlayer.asm:
The code before that deals with drawing the projectile and the code afterward deals with its speed. However, it confuses me that Object_y is stored in temp3 given that the notes in the Macro say it should be stored in temp2. In fact, If we're reading the X and Y for the player object's position, those things should go in temp2 and temp3! What's really going on?
I think "TempZ" is a variable exclusive to the projectile. Here's the full ShootTowardsPlayer code if you think there's anything important in here I missed:
So this, as near as I can figure, is the ASM that will make the enemy progress toward the player at the speed they choose:
There's only one problem: The code for the other enemy AI scripts looks completely different! For example, "Move randomly in 8 directions" looks like this:
Logically, what I have should work. Is this just a different way of writing such things? I don't know.
Another thing I don't know is how to import this so I can test it out! There are some empty spaces in Object Details / Actions tab that say AI_Behavior_09, AI_Behavior_10, etc. But nothing in the tool lets me assign new scripts to those empty spaces.
A little help would be appreciated. If we get this working, we all can use it.
I tend to learn code tricks by reverse-engineering code that already exists. There's already a routine that moves something in the player's direction: the "Shoot Toward Player" action. So I followed the breadcrumbs. In Macros you have this:
Code:
MACRO MoveTowardsPoint arg0, arg1, arg2, arg3, arg4
;; arg0 = point of origin, x
;; arg1 = poinrt of origin y
;; arg2 = point to move towards x
;; arg3 = point to move towards y
In Zero Out Assets, it's referenced:
Code:
MoveTowardsPlayer:
MoveTowardsPoint temp, temp1, temp2, temp3, #$01
RTS
And that, in turn, is referenced in ShootTowardsPlayer.asm:
Code:
LDX player1_object
LDA Object_x_hi,x
STA temp1
LDA Object_y_hi,x
STA temp3
LDX tempz ;; restore newly created projectile object.
JSR MoveTowardsPlayer
The code before that deals with drawing the projectile and the code afterward deals with its speed. However, it confuses me that Object_y is stored in temp3 given that the notes in the Macro say it should be stored in temp2. In fact, If we're reading the X and Y for the player object's position, those things should go in temp2 and temp3! What's really going on?
I think "TempZ" is a variable exclusive to the projectile. Here's the full ShootTowardsPlayer code if you think there's anything important in here I missed:
Code:
;; shoot towards player.
TXA
STA tempx
;; get offset
LDA Object_x_hi,x
CLC
ADC #$04 ;; arbitrary...would put an 8x8 proj in the center of a 16x16 object
STA temp1
CLC
ADC #$04 ;; arbitrary...would put a 8x8 proj in the center of a 16x16 object
LDA Object_y_hi,x
STA temp2
CreateObject temp1, temp2, #$0A, #$01 ;; maybe use a different state for ignore physics?
LDA #$00
STA Object_movement,x
;; we will skip traditional movement
;; and use direct speed instead.
;; will this cause deacceleration?
;; what we need is a bit to "ignore physics engine" which will ignore acc/dec
LDA Object_x_hi,x
STA temp
LDA Object_y_hi,x
STA temp2
TXA
STA tempz ;; store the newly created object's x
;; shoot at player one - if there are two players, will we randomize somhow?
LDX player1_object
LDA Object_x_hi,x
STA temp1
LDA Object_y_hi,x
STA temp3
LDX tempz ;; restore newly created projectile object.
JSR MoveTowardsPlayer
LDX tempz
LDA myHvel
STA Object_h_speed_lo,x
LDA #$00
STA Object_h_speed_hi,x
LDA myVvel
STA Object_v_speed_lo,x
LDA #$00
STA Object_v_speed_hi,x
gotVAimSpeeds:
LDX tempx
So this, as near as I can figure, is the ASM that will make the enemy progress toward the player at the speed they choose:
Code:
LDX Object_type,x
LDA Object_x_hi,x
STA temp
LDA Object_y_hi,x
STA temp1
LDX player1_object
LDA Object_x_hi,x
STA temp2
LDA Object_y_hi,x
STA temp3
JSR MoveTowardsPlayer
There's only one problem: The code for the other enemy AI scripts looks completely different! For example, "Move randomly in 8 directions" looks like this:
Code:
;;;; Choose out of 8 directions.
JSR GetRandomDirection
AND #%00000111
TAY
LDA DirectionMovementTable,y
STA temp
TYA ;; the 0-7 value for direction
ORA temp
STA Object_movement,x
Logically, what I have should work. Is this just a different way of writing such things? I don't know.
Another thing I don't know is how to import this so I can test it out! There are some empty spaces in Object Details / Actions tab that say AI_Behavior_09, AI_Behavior_10, etc. But nothing in the tool lets me assign new scripts to those empty spaces.
A little help would be appreciated. If we get this working, we all can use it.