Author Topic: Bullet adding subroutine  (Read 3464 times)

0 Members and 1 Guest are viewing this topic.

Offline boathouse2112

  • LV2 Member (Next: 40)
  • **
  • Posts: 23
  • Rating: +0/-0
    • View Profile
Bullet adding subroutine
« on: August 02, 2012, 12:47:00 pm »
I was going through deepthought's shoot-em-up tut and doing fine until I came to the bullet-adding subroutine.


Can someone tell me what the hell is going on here? I looked at his array tutorial and still have no idea.
« Last Edit: August 02, 2012, 12:52:31 pm by boathouse2112 »

Offline blue_bear_94

  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 801
  • Rating: +25/-35
  • Touhou Enthusiast / Former Troll / 68k Programmer
    • View Profile
Re: Bullet adding subroutine
« Reply #1 on: August 02, 2012, 02:58:59 pm »
Okay... first, this is a library file with ASB and RSB as subroutines. Also, r1 through r6 are the arguments, so if you use sub(ASB,A,B,C,D), then A, B, C, and D get loaded into r1, r2, r3, and r4 respectively.

Code: [Select]
r4->{r3->{r2->{r1->{B+1->B*4-L4-4}+1}+1}+1}
means...
*(*(*(*(4*(++b)-tempSwapArea-4)=r1)=r2)=r3)=r4;
or...
b++;
int aaa=4*b-tempSwapArea-4
*(a)=r1;
*(r1)=r2;
*(r2)=r3;
*(r3)=r4;
As for the second part...
Code: [Select]
Copy(r1*4+L4+4,-4,B-1->B-r1*4+1)
means...
memcpy(-4,4*r1+tempSwapArea+4,(--b)*4+1);
/*-4 is same as 65532*/
or...
b--;
memcpy((char*)(-4),4*r1+tempSwapArea+1,4*b+1);
/*dest,src,size*/

Hope this clears things up a bit.
Due to dissatisfaction, I will be inactive on Omnimaga until further notice. (?? THP hasn't been much success and there's also the CE. I might possibly be here for a while.)
If you want to implore me to come back, or otherwise contact me, I can be found on GitHub (bluebear94), Twitter (@melranosF_), Reddit (/u/Fluffy8x), or e-mail (if you know my address). As a last resort, send me a PM on Cemetech (bluebear94) or join Touhou Prono (don't be fooled by the name). I've also enabled notifications for PMs on Omnimaga, but I don't advise using that since I might be banned.
Elvyna (Sunrise) 4 5%
TI-84+SE User (2.30 2.55 MP 2.43)
TI-89 Titanium User (3.10)
Casio Prizm User? (1.02)
Bag  東方ぷろの

Offline boathouse2112

  • LV2 Member (Next: 40)
  • **
  • Posts: 23
  • Rating: +0/-0
    • View Profile
Re: Bullet adding subroutine
« Reply #2 on: August 02, 2012, 05:33:37 pm »
Thanks for the help!

Offline Deep Toaster

  • So much to do, so much time, so little motivation
  • Administrator
  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 8217
  • Rating: +758/-15
    • View Profile
    • ClrHome
Re: Bullet adding subroutine
« Reply #3 on: August 02, 2012, 05:39:01 pm »
blue_bear_94, I wouldn't assume everyone here knows C. Many new members come here for whom TI-BASIC (or Axe) is their first exposure to a programming language, and even among calculator programmers who start learning to program on a computer, C isn't one of more common languages unless they're planning to make 68K/Nspire/Prizm games.

The other problem is that your pseudocode in C isn't actually doing what the Axe code does. Specifically, lines 8, 9, and 10 are wrong because value→{pointer} in Axe returns pointer after the operation, not value, when pointer isn't a constant expression. So if you were to translate it to C, you would actually have *(a) = r1; *(a + 1) = r2; *(a + 2) = r3; *(a + 3) = r4;. (Yeah, yeah, the evils of side-effect programming and all that. But in Axe it's called optimization. And it looks cool.) There's another minor error in line 3 because L4 should be added, not subtracted.

So here's a complete breakdown of the code from the guy who wrote it :D I'll start with routine ASB.

Say that somewhere in the middle of the game, there are two bullets. Both bullets are of the same type that you call type 1, and they're both traveling upwards at 3 pixels per frame. One is at position (13,37) and the other one is at (4,2). In that particular program I organized the array so that each element was composed of four bytes—type, speed, x-position, and y-position, in that order. If I remember correctly I used L4 as the start of the array that would hold the all the bullets and the variable Bn (as in bullet) to keep track of the number of bullets in that array. So at that moment in the game, B would be 2 and the array at L4 would look like this:
Bullet 1Bullet 2
Address
Value
L4+0L4+1L4+2L4+3
131337
L4+4L4+5L4+6L4+7
1342
Now we look at the code.

First, like with any math expression, you start at the innermost parentheses/brackets/braces. That would be this line:



B+1→B returns the new value of B as a result. (Every single operation in Axe will return some number as a result, which you can use to chain into the next operation to save a few bytes.) In other words, this code is just an optimized version of this:



The first line takes B, adds one, and stores it back to B. (This is exactly the same operation as B++, but the tutorial was written before the ++ command was added, and there's literally no difference between the two, so it doesn't matter.) So now B is 3, which is the number of bullets we are about to have.

The value of the second line, B*4+L4-4, is simply L4+8. The next level up looks like this:



Since we know that the value inside the braces is L4+8, you can see that now we're storing r1 to {L4+8}, and the data at L4 now looks like this:
Bullet 1Bullet 2Bullet 3 (incomplete)
Address
Value
L4+0L4+1L4+2L4+3
131337
L4+4L4+5L4+6L4+7
1342
L4+8L4+9L4+10L4+11
r1
But what about that +1?

Turns out when you store something to a pointer that isn't constant (since B*4+L4-4 isn't a constant number), the result it returns is the pointer you stored to—in this case, L4+8. So the result of that entire line, r1→{L4+8}+1, is L4+9.

That gets passed to the next level up:



Again, we already know what the number in the braces is—it's L4+9! So here's our array at L4 again:
Bullet 1Bullet 2Bullet 3 (incomplete)
Address
Value
L4+0L4+1L4+2L4+3
131337
L4+4L4+5L4+6L4+7
1342
L4+8L4+9L4+10L4+11
r1r2
We've added two bytes to the array at L4 already. If you take a look at the code for the rest of ASB, you should be able to see that we're just repeating that last step twice with r3 and r4: r3 ends up in L4+10 and r4 ends up in L4+11. When the routine is done, L4 looks like this:
Bullet 1Bullet 2Bullet 3
Address
Value
L4+0L4+1L4+2L4+3
131337
L4+4L4+5L4+6L4+7
1342
L4+8L4+9L4+10L4+11
r1r2r3r4
Which is what we're trying to do.

Enjoy!

EDIT: Epic post time. Kinda makes me want to finish up those tutorials :P
« Last Edit: August 04, 2012, 10:27:10 pm by Deep Thought »




Offline boathouse2112

  • LV2 Member (Next: 40)
  • **
  • Posts: 23
  • Rating: +0/-0
    • View Profile
Re: Bullet adding subroutine
« Reply #4 on: August 02, 2012, 05:51:03 pm »
Thanks for the more helpful help:P
How does one access the r1-r6 variables?

Offline Deep Toaster

  • So much to do, so much time, so little motivation
  • Administrator
  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 8217
  • Rating: +758/-15
    • View Profile
    • ClrHome
Re: Bullet adding subroutine
« Reply #5 on: August 02, 2012, 05:55:42 pm »
[VARS][RIGHT][3]

I may be wrong, because I don't have a calculator with me. That's just from memory >.>
« Last Edit: August 02, 2012, 05:55:54 pm by Deep Thought »




Offline boathouse2112

  • LV2 Member (Next: 40)
  • **
  • Posts: 23
  • Rating: +0/-0
    • View Profile
Re: Bullet adding subroutine
« Reply #6 on: August 02, 2012, 08:04:43 pm »
Yep. Thanks.