Shiny Quagsire

I'm Still Alive, Elsewhere

Age 22
Male
Hoenn Safari Zone
Seen November 13th, 2019
Posted April 17th, 2017
700 posts
11 Years
Using the Berry Bag for Item Selection

So, recently I've been working on making a complete berry system based on JPAN's Berry System on Fire Red. For a planting script, it required some sort of berry selection, and having a scripted multichoice menu for it was a no-go for me. So, I got to work, searching and searching, and I got it!

While you may think doing this would be complicated, there are is actually only one ASM routine required for this. Basically, what it does is it forces the bag into thinking every item is the Exit item, which will exit the bag immediately as it's selected. Along the way to exiting the routine, it stores the berry ID to variable 0x800E, or 0x0 if the user didn't select anything or pressed B. Under normal circumstances, this is wiped upon exiting the bag, but because of the way we call the bag, it preserves this variable.

hijackbag.asm:
@Forces the bag to exit instead of opening the menu.
@Insert bx r3 at (18 47) 0813DB18, and the pointer to this routine (+1) at 0813DB30

.thumb
.thumb_func
.global hijackbag

main:
	ldr r3, var_8000
	ldrh r3, [r3]
	cmp r3, #0x7E
	beq hack
	ldr r3, unk_0203F370
	mov r8, r3
	ldrb r0, [r3,#0x4]
	b return
hack:
	ldr r3, unk_0203F370
	mov r8, r3
	mov r0, #0x5
return:
	ldr r3, returnaddress
	bx r3
.align 2
returnaddress: 		.word 0x0813DB1D
unk_0203F370:		.word 0x0203F370
var_8000:		.word 0x020370B8
All this does it it checks variable 0x8000 (you can change it to use the variable decrypter if you wish) for a strange value that wouldn't misfire our hack. In this case, I chose 0x7E. Any other weird values will work if you feel 0x7E isn't apropriate.

To insert this, place 18 47 at 0x13DB18, and the pointer to this routine + 1 at 0x13DB30.

Now, to use this hack, simply place the following into a script:
fadescreen 1        'Fade the screen to prevent any graphical glitches from being visible. And it looks nice. :)
setvar 0x8000 0x7E  'Set 0x8000 to trigger our script
setvar 0x800E 0x0   'Make sure 0x800E isn't set, because if it is, weird things happen.
callasm 0x0a1881    'Call the berry bag. This is stored internally in the ROM
waitmsg             'I'll explain this later.
setvar 0x8000 0x0   'Reset our hack to prevent weirdness.
Ok, so basically this fades the screen, sets our trigger variable, calls the routine, and resets. However, if your a scripter, you might be thinking "What the heck is up with that waitmsg? Did we even call an msgbox?" Well, there's a reason for doing that. When the ASM code is called, it basically hands it to a table so that it is updated every time the game runs the main loop. However, this also means that the script keeps going during this time, which can be bad if you're calling a bufferitem 0x0 0x800E afterwards, or anything that borrows the value at 0x800E. So, the waitmsg acts as a delay so that the script waits until our player is finished choosing which berry he wants.

As for applications to apply this to, here's an example script I wrote to test this:
Spoiler:

#dynamic 0x100011B
#include stditems.rbh

#org @start
lock
faceplayer
msgbox @msg 0x5
compare 0x800d 0x1
if 0x1 goto @plant
release
end

#org @plant
giveitem ITEM_ORANBERRY 3 0
giveitem ITEM_CHERIBERRY 1 0
giveitem ITEM_SITRUSBERRY 1 0
fadescreen 1
setvar 0x8000 0x7E
setvar 0x800E 0x0
callasm 0x0a1881
waitmsg
setvar 0x8000 0x0
compare 0x800E 0x0
if 0x1 goto @noberry
bufferitem 0x0 0x800E
msgbox @planted 2
release
end

#org @noberry
msgbox @noplant 2
release
end

#org @msg
= There is a dude standing here.\pWould you like to plant a berry?
#org @planted
= You planted a [buffer0].
#org @noplant
= The trees cried because you\ndidn't plant anything. :'(


Final Notes (and things to consider)
So, this is an interesting hack, but what if we could use it with the normal bag, and have a way to manually give items to someone, ie a junk dealer, or a quest where you have to guess what the person likes? EDIT: Done! See section below!

As a final note, if you find any bugs, report them, and suggestions are welcome. Also, be sure to give credits. It's a good morale booster for me. :P


Using the Normal Bag for Item Selection

So after completing my berry bag hack, I posted some final notes and things I thought about afterwards. Since both bags functioned relatively the same, I decided to port the same hack from the Berry Bag to the Main Bag.

Since I'm lazy, I'm going to use quotes:
... there are is actually only one ASM routine required for this. Basically, what it does is it forces the bag into thinking every item is the Exit item, which will exit the bag immediately as it's selected. Along the way to exiting the routine, it stores the berry ID to variable 0x800E, or 0x0 if the user didn't select anything or pressed B. Under normal circumstances, this is wiped upon exiting the bag, but because of the way we call the bag, it preserves this variable.
hijacknormbag.asm
@Forces the bag to exit instead of showing the item menu.
@Place the pointer to this routine at 0x081090D0, and place bx r1 (08 47) at 0x081090B6

.thumb
.thumb_func
.global BagHack

main:
	ldr r1, var_8000
	ldrh r1, [r1]
	cmp r1, #0x7F
	beq skip
	ldr r1, var_800E
	strh r0, [r1]
	ldr r1, return
	bx r1
skip:
	ldr r1, var_800E
	strh r0, [r1]
	ldr r1, skipaddr
	bx r1
	
var_8000:	.word 0x020370B8
var_800E:	.word 0x0203AD30
skipaddr:	.word 0x08109065
return:	.word 0x081090B9
To insert this, place the pointer to the compiled routine (+ 1) at 0x1090D0, and place 08 47 at 0x1090B6.

Now, one thing to notice if you can read this is that there is one major difference from the berry bag hack, and that's in this section of code:
cmp r1, #0x7F
Instead of using 0x7E like the berry bag, I wanted to distinguish this hack from the other one to avoid confusion while scripting. so I made it check variable 0x8000 for 0x7F instead.

Now, since this is almost exactly like the berry bag hack, the script calling is pretty much the same. I'll comment out any major differences.
fadescreen 1
setvar 0x8000 0x7F    'Set 0x8000 to 0x7F instead of 0x7E
setvar 0x800E 0x0
callasm 0x10a67d       'This calls the bag. If you ever want to use this without the hack, it works.
waitmsg
setvar 0x8000 0x0
Final Notes (and things to consider)
As for applications for this, pretty much anything you want. Truthfully, I just reused my berry script for testing, because I was too lazy to do anything special or cool with it as seen by this pic:


However, some ideas for this are:
  • A Junk Dealer who buys random items
  • An event where you have to give a certain item
  • An event where you trade items
  • A script where you have to guess an item that someone wants
  • Fossil scripts
  • Etc.

As usual, report any bugs associated with the routine and suggestions are welcome. Also, be sure to give credits. It's a good morale booster for me. :P

FBI

Free supporter

Male
Unknown Island
Seen 2 Days Ago
Posted December 17th, 2019
1,910 posts
7.4 Years
Real nice work there, I'm sure hackers using or looking to use a berry system will find this very useful. I read through the whole thing and I'm glad you've explained how things work rather than just posting the code and instructing us how to insert things.

What really caught my eye is this little part here:
Along the way to exiting the routine, it stores the berry ID to variable 0x800E, or 0x0 if the user didn't select anything or pressed B.
I assume this process can be duplicated for other things as well? Would be interesting and sounds like the key to using custom items outside of battle (maybe there's an easy way that I don't know of :P).

Anyways, nice work my dear Quagsire.
...

Shiny Quagsire

I'm Still Alive, Elsewhere

Age 22
Male
Hoenn Safari Zone
Seen November 13th, 2019
Posted April 17th, 2017
700 posts
11 Years
Updated! Now has tutorial for using the main bag for item selection. Luckily for me, the routines were pretty much identical.

When I get the chance I'll upload a video of the routine in action. ;)

tajaros

Hi I'm dawg

Age 21
Male
Philippines
Seen August 7th, 2015
Posted April 15th, 2013
856 posts
8.1 Years
Cool thing here Shiny Quagsire! Just in time cause I was thinking to add the berry system a week ago.

I'll try this out this weekend since I'll have free to do this at that time.

Lance32497

LanceKoijer of Pokemon_Addicts

Male
Criscanto town-Ginoa Region xD
Seen August 13th, 2017
Posted January 1st, 2017
793 posts
5.8 Years
is callasm 0x10a67d a fixed pointer to asm, or I replace it with the offset where I placed the routine
This signature has been disabled.
Scrollbar appears
Please review and fix the issues by reading the signature rules.

You must edit it to meet the limits set by the rules before you may remove the [sig-reason] code from your signature. Removing this tag will re-enable it.

Do not remove the tag until you fix the issues in your signature. You may be infracted for removing this tag if you do not fix the specified issues. Do not use this tag for decoration purposes.