Skeetendo

’Cause all games were better on the GBC

You are not logged in.

#1 2012-07-10 15:00:17

Miksy91
Member
Registered: 2010-10-16
Post 958/2,308

G/S - Adding more backsprites for the player

So, I'd like to make it so that you're able to play with more than just one hero, there will be three playable characters in Dark Future + "DUDE" of course.
So basically, what I did for my hack was to write

push af
ld hl, $495F
ld a, $22
rst $8
ret

in 0F:7951 in Pokemon Silver (U) rom. Following, is the data written in there originally:

0F:7951

call $7958
call $7972
ret


0F:7958

ld hl, [$backsprite of player]
ld a, $D119
cp a, $03 ("DUDE" battle)
jr nz (+3b)
ld hl, [$backsprite of DUDE]
ld de, $9310
ld b, $0F
ld c, $31
ld a, $3F
call $2E49
ret

What I did for myself was to input all that data (including the code in 0F:7972) in blank space in bank 0x22 (before the two call commands, there is a pop af though). However, the game "freezes" (keeps running but most of the palettes turn black and game is "restarted") at the "call $2E49" as part of call to original 0F:7958. ret after it is never accessed and I can't really say what's wrong with the code. Should I try to input more data than just that in the blank space and does somebody know what I've done wrong?

Last edited by Miksy91 (2012-07-10 15:02:14)

Offline

#2 2012-07-10 19:29:09

Sawakita
Administrator
Registered: 2010-10-16
Post 282/364

Re: G/S - Adding more backsprites for the player

Could you provide your custom code? Or did you just copy the original code to another location?

Also, have you tried debugging your code step by step?

Offline

#3 2012-07-10 19:38:57

Miksy91
Member
Registered: 2010-10-16
Post 959/2,308

Re: G/S - Adding more backsprites for the player

I copied the original code to another location (expect for the fact that I added "pop af" in the beginning of the code like I explained) and fixed the pointers of backsprites loaded in hl to match with the new offsets in bank 0x22 where I moved the backsprites of Hiro and DUDE. I'm not sure whether the game still tries to load the backsprites in original bank 0xF but when I tried changing the pointers of backsprites to match with the offsets in bank 0xF, the same thing happened ("freeze").

I also tried debugging the code and everything works fine until call $2E49 is accessed. That routine, which is called, is almost endless when debugged step by step at its current state but I haven't checked if it works otherwise in the original rom yet.

Offline

#4 2012-07-10 20:07:02

Sawakita
Administrator
Registered: 2010-10-16
Post 283/364

Re: G/S - Adding more backsprites for the player

Miksy91 wrote:
push af
ld hl, $495F
ld a, $22
rst $8
ret

The problem is exactly in that push/pop operation: you push af before calling the far routine at 22:495F, but then you pop af inside the sub-routine, which causes serious stack troubles.
As a general rule (unless you're editing SP by hand during the process) you should always push/pop in the same routine (or in the same level), never have a push in a routine, and a pop in a sub-routine, or push in a routine and never pop it before the ret (unless you want to run the code pointed by the pushed value).

RST $8 is a shorter way to perform a call, so it pushes the value of the Program Counter on the stack, so you have:

[SP + 2]  *AF <-- inside the Stack
[SP]      *PC <-- Stack Pointer points to this

And when you pop af during your sub-routine, that's what you have:

[SP]      *AF <-- Stack Pointer points to this
[SP - 2]  *PC <-- outside the Stack

So, when code reach the return, it actually return to the address pointed by the the 16-bit value contained in af when you pushed it initially.
Why are pushing af in the first place? Do you need it's value inside your sub-routine?

Last edited by Sawakita (2012-07-10 20:07:58)

Offline

#5 2012-07-10 20:52:57

Miksy91
Member
Registered: 2010-10-16
Post 960/2,308

Re: G/S - Adding more backsprites for the player

I see, I actually noticed partly how this works by debugging it again and seeing what rst $8 mostly did.
Well, I removed the push & pop commands of af, yet it still isn't working any better.

Should I explain when exactly the engine stops "working" like it normally would? I found a couple of useful addresses but don't have them written down anywhere but I can find them easily by starting to debug at 0F:7951.

Offline

#6 2012-07-10 23:03:02

Tauwasser
Member
Registered: 2010-10-16
Post 292/447

Re: G/S - Adding more backsprites for the player

The problem is probably in how you call things within your custom code which you won't show us.

Regardless, just move the sprites to a different bank and use a LUT instead of the original code.

Btw your notation for pointers is seriously wrong. [] denotes access, not just an immediate value. You're having the notation exactly switched.

ld hl, $my_cool_table
ld a, [$D119]
add a, a  ;; a = [D119] * 2
add a, a  ;; a = [D119] * 4
add a, l
ld l, a
;; Make sure your h will not overflow, or adjust like below
;; ld a, $00
;; adc a, h
;; ld h, a
;; ;; or ;;
;; jr nc, .skip
;; inc h
;; .skip:
ld b, [hl] ;; Load bank
inc hl
ldi a, [hl] ;; Load Pointer
ld h, [hl]
ld l, a
ld de, $9310 ;; Destination
ld c, $31    ;; Number of tiles
ld a, $3F    ;; Starting tile index
call $2E49
ret

@my_cool_table:
db $backsprite of player bank
dw $backsprite of player
db $00

db $backsprite of player bank
dw $backsprite of player
db $00

db $backsprite of player bank
dw $backsprite of player
db $00

db $backsprite of dude bank
dw $backsprite of dude
db $00

db $backsprite of custom player0 bank
dw $backsprite of custom player0
db $00

db $backsprite of custom player1 bank
dw $backsprite of custom player1
db $00

This of course will only work if you extend the original enumeration for backsprites with your indices and trace some code that does special behavior for the catch pokemon sequence to make sure it will only load for the dude. IMHO this would be the cleanest non-hackish solution. Also, you'll learn a lot about the fight engine that way.

cYa,

Tauwasser

Last edited by Tauwasser (2012-07-11 20:52:05)

Offline

#7 2012-07-11 05:05:34

Miksy91
Member
Registered: 2010-10-16
Post 961/2,308

Re: G/S - Adding more backsprites for the player

So basically, should I try to make it so that if the value in D119 is 00, 01 or 02, battle with player's backsprite would occur, 03 for dude, and 04, 05 for custom backsprites?

Also, what dw and db (size of word and byte?) are ?
I'm starting to understand the code at the beginning of your explanation but shouldn't I still end the code in the original way by jumping to the following instruction set?

ld de, $9310
ld b, $0F
ld c, $31
ld a, $3F
call $2E49
ret

P.S
If I don't get this working, I'll provide all the info about that code in rom bank 0x22 in 12 hours or so.

Shouldn't there be another "add a,a" before "add a,l" ?

Last edited by Miksy91 (2012-07-11 13:43:33)

Offline

#8 2012-07-11 15:01:55

Miksy91
Member
Registered: 2010-10-16
Post 962/2,308

Re: G/S - Adding more backsprites for the player

Finally, I understand what all this code means!
So I got an idea to check whether the value loaded in register b is the rom bank of the looked battle sprite (no idea why I didn't figure this out before) and I noticed I could easily look for the battle sprites in another rom bank by modifying that register value not a big surprise but for me, it was a positive found.

I've a feeling I get this routine working on my own now expect for one thing:
Any ideas how the palette for the player is determined? I guess I need to change that routine too if I'm going to keep Oscar's palette as blue like as of now in the game.

Offline

#9 2012-07-11 20:57:47

Tauwasser
Member
Registered: 2010-10-16
Post 293/447

Re: G/S - Adding more backsprites for the player

Miksy91 wrote:

So basically, should I try to make it so that if the value in D119 is 00, 01 or 02, battle with player's backsprite would occur, 03 for dude, and 04, 05 for custom backsprites?

Yep.

Miksy91 wrote:

Also, what dw and db (size of word and byte?) are ?

Yes, data byte and data word respectively.

Miksy91 wrote:

I'm starting to understand the code at the beginning of your explanation but shouldn't I still end the code in the original way by jumping to the following instruction set?

ld de, $9310
ld b, $0F
ld c, $31
ld a, $3F
call $2E49
ret

I forgot about de in my code above. That's fixed now :)
Other than that, now, you should not jump back to the other code.

Miksy91 wrote:

Shouldn't there be another "add a,a" before "add a,l" ?

See comments added in the code above. Basically, it's a multiplication by 4, not just a "times 2".

Miksz91 wrote:

Any ideas how the palette for the player is determined? I guess I need to change that routine too if I'm going to keep Oscar's palette as blue like as of now in the game.

I'm pretty sure that's going to be almost right after the routine you already found. Keep debugging, then add branching code to load different palettes if you need to.

cYa,

Tauwasser

Offline

Board footer

Powered by FluxBB