Modding => Help! => Topic started by: dw6561 on October 07, 2016, 03:24:06 am
Title: Can someone help me with my code?
Post by: dw6561 on October 07, 2016, 03:24:06 am
I'm trying to make brave and faith work differently. What my hack is supposed to do is whenever brave and faith are changed, it would add or subtract 5 brave when the unit gets their turn, gradually bringing the unit back to their original brave and faith. I want this to work even if a unit is dead, and after the unit's turn is over. But the hack is just immediately returning the unit to their original stats, or doing it per clocktick instead of per turn (not entirely sure which). I'm thinking I put this hack in the wrong place. I've been putting it in the end section of the "in between turn control routine" if that helps.
Don't mind the somewheres, those are just how I code things when I don't know the exact locations of jumps. I wrote all of this by hand btw, but I think I ironed out the myriads of mistakes I made when I first coded it. There are probably several ways I can optimize this code too, but first I need to know where to put the dang thing.
Location of hack: Battle.bin, E83AC (0x0014f3ac) Place where hack jumps from: Battle.bin, 11C020 (0x00183020)
3c038019 lui r3,0x8019 8c632d94 lw r3,0x2d94(r3) Load attacker data pointer 00000000 nop 90640023 lbu r4,0x0023(r3) Load original brave 90650024 lbu r5,0x0024(r3) Load current brave 10850014 beq r4,r5,somewhere Branch to faith changes if current = original 00000000 nop 0085082a slt r1,r4,r5 Set if r4 < r5 10200009 beq r1,r0,somewhere Branch if less than original??? 00850823 subu r1,r5,r4 r1 = current - original 28210005 slti r1,r1,0x0005 Set if less than 5 10200003 beq r1,r0,somewhere Branch if greater than or equal to 5 00000000 nop 1000000c beq r0,r0,somewhere Jump to faith changes a0640024 sb r4,0x0024(r3) Store current = original 24a5fffb addiu r5,r5,0xfffb Current brave - 5 10000009 beq r0,r0,somewhere Jump to faith changes a0650024 sb r5,0x0024(r3) Store new brave
00010823 subu r1,r0,r1 r1 = original - current 28210005 slti r1,r1,0x0005 Set if less than 5 10200003 beq r1,r0,somewhere Branch if greater than or equal to 5 00000000 nop 10000003 beq r0,r0,somewhere Jump to faith changes a0640024 sb r4,0x0024(r3) Store current = original 24a50005 addiu r5,r5,0x0005 Current brave + 5 a0650024 sb r5,0x0024(r3) Store new brave
90640025 lbu r4,0x0025(r3) Load original faith 90650026 lbu r5,0x0026(r3) Load current faith 10850014 beq r4,r5,somewhere Branch to end if current = original 00000000 nop 0085082a slt r1,r4,r5 Set if r4 < r5 10200009 beq r1,r0,somewhere Branch if less than original??? 00850823 subu r1,r5,r4 r1 = current - original 28210005 slti r1,r1,0x0005 Set if less than 5 10200003 beq r1,r0,somewhere Branch if greater than or equal to 5 00000000 nop 1000000c beq r0,r0,somewhere Jump to end a0640026 sb r4,0x0026(r3) Store current = original 24a5fffb addiu r5,r5,0xfffb Current faith - 5 10000009 beq r0,r0,somewhere Jump to end a0650026 sb r5,0x0026(r3) Store new faith
00010823 subu r1,r0,r1 r1 = original - current 28210005 slti r1,r1,0x0005 Set if less than 5 10200003 beq r1,r0,somewhere Branch if greater than or equal to 5 00000000 nop 10000003 beq r0,r0,somewhere Jump to end a0640026 sb r4,0x0026(r3) Store current = original 24a50005 addiu r5,r5,0x0005 Current faith + 5 a0650026 sb r5,0x0026(r3) Store new faith 08060c0a j 0x00183028 Back to main routine 8fa60010 lw r6,0x0010(r29)
If anyone knows where this should go or sees any glaring mistakes, please let me know.
Title: Re: Can someone help me with my code?
Post by: Glain on October 07, 2016, 03:10:56 pm
I believe the "In between turn control routine" does run every clocktick. You'll probably need to put the hook to your new code in a section that only runs when the unit has a turn. The loop in the "Validate units turn" section in the same routine seems like it would work, at a glance.
A few other observations:
1. There are some load delay problems here. See this thread (http://ffhacktics.com/smf/index.php?topic=7346.0) for some more information about it (as well as other standards that PSX code should follow). So, for example:
lbu r5,0x0024(r3) # Load current brave **(Need a nop here, or some other instruction that doesn't use r5) beq r4,r5,somewhere # Branch to faith changes if current = original
2. You generally don't need to know the exact locations of jumps ever, as they should be calculated automatically using labels! This pattern works just fine in an XML patch (with mode="ASM") or MassHex:
beq r4, r5, somewhere # You can also have comments like this preceded by "#" that get ignored by the assembler nop
somewhere: lbu r4,0x0025(r3)
On the same vein, I'd consider calling your new code like a subroutine (jal instead of j) and using jr r31 instead of jumping back to an exact address at the end. (Substituting "somewhere" instead of using different labels also means I can't really trace what the code is doing, but I think I get the gist of it.)
3. The acting unit's data pointer (0x80192d94) isn't necessarily loaded with the right value unless there's actually an action going on! You should be getting the unit pointer from elsewhere (it should be loaded into a register inside the calling routine).
4. BATTLE.BIN 0xE83AC doesn't seem to be in the kanji table! Are you sure that's free space that you're writing to?
5. The final two commands, with a load instruction inside a branch delay slot - I'm not sure if this is a problem, but FFT's compiled code avoids this like the plague. Might be safer to rewrite as load, jump (jr r31), nop.
Title: Re: Can someone help me with my code?
Post by: dw6561 on October 07, 2016, 03:28:12 pm
1. Oh did I not have a nop there? I know about the load delay thing already, but sometimes a pair of fresh eyes is all it takes to catch mistakes like that.
2. I've kinda just been writing all my code by hand because I don't like using external programs. Sorry about that, I just use notepad and a hex calculator to do my work.
3. You're right, I think its in r17. I posted this prematurely it looks like, considered that and eventually said that it didn't matter.
4. Oof. I took the address from the kanji space nopper. I'll have to check that later.