Skeetendo

’Cause all games were better on the GBC

You are not logged in.

#1 2013-09-15 16:30:02

Crystal_
Member
From: Spain
Registered: 2012-09-16
Post 91/435
Website

Non-hold items used by trainers?

Some trainers like gym leaders and elite four have one or a couple of hyper potions or full restores under their belt. What determines which trainer classes can use which items? My objective is to find a way to remove every item on every class so that no items are used by trainers (other than hold items).

Thanks in advance.

Offline

#2 2013-09-15 18:41:47

Danny-E 33
Administrator
Registered: 2012-06-09
Post 641/1,044

Re: Non-hold items used by trainers?

I know in Red, different trainer id's read from different AI routines telling them to do different things like use potions. There might be something similar in Crystal.
For example, you can look in pokered's main.asm as look at "LoreleiAI:".


Red Hack: Pokémon Prototype

Total number of registered users: 8000+
Total number of active users: ~12

Offline

#3 2013-09-15 21:22:34

Crystal_
Member
From: Spain
Registered: 2012-09-16
Post 92/435
Website

Re: Non-hold items used by trainers?

I wasn't aware about how it worked in Pokemon red, but I had already searched about stuff in scoring.asm of pokecrystal, but the AI routines only have data regarding the priority of the moves that will be used and these things it seems.

Offline

#4 2013-09-16 05:43:23

Miksy91
Member
Registered: 2010-10-16
Post 1,886/2,317

Re: Non-hold items used by trainers?

Isn't the amount of hold items limited by each gym leader that has them? If so, their data has to be loaded into ram.
I'd imagine, you can find the hold items numbers stored somewhere in "Battle Ram" area (see Gold Ram Map for reference).

Once you have located them there, you can see what address they're stored in, and then you can simply debug the routine how they're stored in that address.

If you can't find their data in battle ram however, you could try debugging the routine that prints the text data "[LEADER] used [ITEM]", or how it's spelled there.

And if that doesn't work either, look for the disassembled routine in crystal disassembly if it's documented there. This could actually be step #1 to do.

Last edited by Miksy91 (2013-09-16 05:45:51)

Offline

#5 2013-09-16 12:33:12

Crystal_
Member
From: Spain
Registered: 2012-09-16
Post 93/435
Website

Re: Non-hold items used by trainers?

I think the items and amount of items is determined by just trainer class, so in that case I suppose that only the trainer class (byte at 0xd233) needs to be loaded into ram.

I've found the text [trainer used item] in the ROM. It's at offset 0x1bcfb0. Does that mean that the routine that makes the items be used, or the possible data containing the items used by trainer class, is necessarily near that offset?

I also searched through Pokecrystal before even making this thread btw, but couldn't find anything related.

Offline

#6 2013-09-16 13:32:15

Miksy91
Member
Registered: 2010-10-16
Post 1,887/2,317

Re: Non-hold items used by trainers?

Crystal_ wrote:

I think the items and amount of items is determined by just trainer class, so in that case I suppose that only the trainer class (byte at 0xd233) needs to be loaded into ram.

Nope :P

The opponent will "randomly" use a status restoring item if the pokemon it uses goes under some status element, or it will use a healing item if the pokemon it uses goes low on HP. However, the opponent trainer only has 1 or 2 of these items, correct? So if its trainer class was only loaded into ram, the game could only know that this certain opponent can use healing items, but we wouldn't have an idea, has this person used his/her items already. Also because a certain opponent can hold different kinds of items (like Full Heal and Hyper Potion), the item numbers are probably stored in the ram, not the amount of items this trainer has.

This is nothing more than an assumption though. But it could likely be correct.

Crystal_ wrote:

I've found the text [trainer used item] in the ROM. It's at offset 0x1bcfb0. Does that mean that the routine that makes the items be used, or the possible data containing the items used by trainer class, is necessarily near that offset?

Not necessarily but it could be. If the text data is in middle of other battle related general text data, or such, the code that points to that text data is probably elsewhere in the rom.

Edit:
Actually, I checked it out for ya. The code using this text data is definitely elsewhere, and this text data is most likely accessed with a certain text data calling routine. What this means that we load value 4FAF (yes, the text data starts in x1BCFAF, not x1BCFB0) into some register pair (probably hl), rom bank value (6F) into register a , and call a routine that prints the text data in address a:hl (6F:4FAF).

At least, I remember such/similar routine being used in pokemon G/S, but not sure about crystal.
Well anyway, the item name is stored in $D050 when this text data is accessed. So, you could as well backtrack (by debugging), how the item's name goes there, or go for the simple solution and just try to find the item numbers, used by a certain opponent, in battle ram.

Last edited by Miksy91 (2013-09-16 13:34:56)

Offline

#7 2013-09-16 20:14:20

Crystal_
Member
From: Spain
Registered: 2012-09-16
Post 94/435
Website

Re: Non-hold items used by trainers?

Okay, I think I have been able to find where the routine that shows the "trainer used item on pokemon" text is located by searching for af 4f 6f in the rom. I don't understand a thing of it but I know it has to do with what i'm looking for because chaning any byte there glitches up the game (or the text) at the moment the opponent uses an item. I think I have been able to determine the starting point of the routine (changes on bytes before what I deemed the start don't seem to cause any glitches when the item is used, and 00 being the first byte makes sense):
At 0x38582:
00 CD 26 30 21 8C 45 C3 57 10 16 AF 4F 6F 50.
That 0x50 seems to be the end, but I'm not too sure. (the next byte is a 21, for some reason I think 21 has to do with registers; changing this byte doesn't cause any glitch while the item is used).

So now my idea is that, if this works like for example the PrintDidntAffect function documented in pokecrystal, whatever the function is that makes items be used at low health or however it works, should call this function to show the corresponding text. So maybe if I search for the pointer to 0x38582 (if I search for 82 45), one of the results is part of the function I'm looking for? In that case, if I somehow manage to get to the function, will a C9 in the first position of the function suffice to just return the function? (so that the game will continue the battle as it normally would by selecting an attack and attacking in the turn the item would be used)

Last edited by Crystal_ (2013-09-16 20:18:15)

Offline

#8 2013-09-17 05:25:51

Miksy91
Member
Registered: 2010-10-16
Post 1,888/2,317

Re: Non-hold items used by trainers?

You have indeed found something there.
That byte "16" is an in-text-code that prints text data in address [Pointer] [Bank]. It requires byte "50" to be placed after these bytes.
(Like shown there - 16 + AF 4F + 6F + 50)

Though yesterday, I did some research on this aspect and I've got to say, I didn't track down the way how this thing works. I probably could have if I had debugged but I just followed my intuition by looking at the asm routines that are gone through before this text data is printed.
You see, anytime this text data is printed, the opponent uses an item. So it's safe to say that there has been a check whether the opponent is still holding an item, and if it is, remove one item with "decrement" instruction or sort of. And because of this, you can track down the code that removes an item from the opponent (in a certain memory slot in ram) so that it is located "before" this text data printing.

Here is what we've got starting at 0E:457A (0x3857A):

ld hl, D073
ld de, D050
ld bc, 000D
call 3026 ' routine that copies 000D bytes starting at D073 to D050 --> Is the name of the currently used item located in D073? It should be, because the text data that prints "Opponent used Item" calls Item's name from D050.
ld hl, 458C ' pointer to "16 AF 4F 50"
jp 1057 ' print text data from hl - in other words, from address 0x3858C.

Last edited by Miksy91 (2013-09-17 05:28:01)

Offline

#9 2013-09-17 21:52:35

Crystal_
Member
From: Spain
Registered: 2012-09-16
Post 95/435
Website

Re: Non-hold items used by trainers?

So is it safe to say that, wherever the code that makes the item be used / removes one item is, it will contain a pointer to 0x3857A (7A 45)?

Offline

#10 2013-09-17 22:43:14

Tauwasser
Member
Registered: 2010-10-16
Post 406/448

Re: Non-hold items used by trainers?

You're a long way from home and winter is coming... ;)

You're looking at the very end of the action. The game is round based, so the next thing you will find is the routine that calls the "item used by opponent" message. However, this piece of code will probably not be directly inside some AI code or anything, but instead it will be in a generic piece of code used for all battles:

run AI routine
-- inside routine:
set to use item No 0x???
return ITEM_USED
--
ans <= last result

<draw some boxes here>
<was attack No 117 used in the second to last round? if so, color all Pokéballs purple and xor ans with 0x55>
<is the enemy Pokémon asleep? Draw sleeping animation!>
<draw some more boxes; this routine also pushes Pokéball purpleness to stack for no reason, or so you thought>
<make Pokéballs red again>
<redraw screen here: the purple Pokéballs were never seen by the player to begin with!>
<stack magic here: retrieve temporary Pokéball purpleness value.
 Use that value + 0x80 as an index into GB SRAM and read the 5th byte of the data structure there.
 Write that value to a randomized location within [C000;C07F].
 Whatever that does...
>

switch (ans)
case ATTACK_USED:
   ...
case ITEM_USED:
   call appropriate message and execute item subroutine
   break;
case PKMN_CHANGE:
   ...

You will then find a need to determine where the appropriate variables for that were set in the first place. IMO, this is a good place to work backwards from. However, having worked with the battle routine before, I know one has to know a lot of RAM offsets to make even partway sense of it, because most of the time, you will find very many conditional branches in seemingly random code. For instance, all the status effects and interactions ("Sun is shining."; special attacks that effect only future rounds) are monitored in a wild fashion. It will take some nerve to navigate through all the mess, find what you are looking for, changing it and making sure that you didn't break anything in the progress.

While hacking for the German Midnight hack, we encountered some very interesting bugs while inserting/changing graphics. Such as, wild Pokémon fall asleep in the second round, or you encounter them already poisoned etc. I never could track that down, we resorted to using a backup instead, because everything is checked a gazillion time and by the time you are at the piece of code you were looking for you passed so many branches on which you had doubts on what enabled them in the first place that the origin of the problem still remains elusive.

The excerpt above is obviously a dramatization, but that is pretty much how I felt the last time I looked at that code :-/

cYa,

Tauwasser

Last edited by Tauwasser (2013-09-17 22:49:11)

Offline

#11 2013-09-18 06:14:15

Miksy91
Member
Registered: 2010-10-16
Post 1,892/2,317

Re: Non-hold items used by trainers?

Tauwasser wrote:

You're a long way from home and winter is coming... ;)

Sorry Crystal_, but I found this sentence kinda funny :)

But yeah... like Tauwasser said there, the piece of code that checks if the trainer is still holding an item, and if it should use that item, is probably quite far away.
The CPU we are working with is actually really fast (well not really if you compare it to the processors that laptops, or desk computers use nowadays). It should be able to execute over 4 million simple machine instructions (that take from one to three bytes) in a second. So in other words, if the processor was made to "scroll" the rom data from the beginning to its end (and doing nothing significant while at it such as looping some parts, calling some other parts etc.), it would do it in a second.

I'd say, it would be better to try tracking down where in ram the item numbers used by trainer groups are stored. Without trying to backtrack to the right routine. Now that Tauwasser explained it, I would have hard time with this too (and well, likely wouldn't find the right piece of code either).

Edit:
Do yourself a favor and take a look at my video tutorials.
Watching parts from 6 to 8 should be all you need to get the grasp of reading and writing routines that even handle tables while at it.

Last edited by Miksy91 (2013-09-18 06:24:14)

Offline

#12 2013-09-18 17:00:16

Crystal_
Member
From: Spain
Registered: 2012-09-16
Post 97/435
Website

Re: Non-hold items used by trainers?

You're a long way from home and winter is coming... ;)

Lol, I appreciate your honestity :)

Either way I think I'm going to give this a break at least for now since it's clearly too much for me and it's probably not worth the effort. Thanks to all of you for your help.

Offline

Board footer

Powered by FluxBB