[TI BASIC] Optimizing

Got questions? Got answers? Go here for both.

Moderator: MaxCoderz Staff

Guest

Post by Guest »

You code has a couple major flaws in it. The first flaw is that you don't initialize all of the variables that you are using. This causes domain errors because the variables may be large or smaller than the screen size before the program even begins. The second flaw that you have is that you use a for( loop to loop. The way that your program is coded, if 99 turns come along and you have still not gotten your guy, the program ends. I don't think this is the intended behavior of the program. Instead, what you should have used is a while( or a repeat( loop. Besides these two flaws, there is just general optimizations that your program lacks. This is the code that resulted from taking all of these things into account.

Code: Select all

1->A:1->B
8->C:8->D
Repeat A=C and B=D
ClrHome
getKey->K
A+(Ans=34 and A<8)-(Ans=25 and A>1->A
B+(K=26 and B<16)-(K=24 and B>1->B
Output(A,B,9
Output(C,D,6
randInt(-1,1
C+(Ans=1 and C<8)-(Ans=-1 and C>1->C
randInt(-1,1
D+(Ans=1 and D<16)-(Ans=-1 and D>1->D
End
Guest

Post by Guest »

Anonymous wrote:You code has a couple major flaws in it. The first flaw is that you don't initialize all of the variables that you are using. This causes domain errors because the variables may be large or smaller than the screen size before the program even begins. This is where the user sets it, saves code space. The second flaw that you have is that you use a for( loop to loop. The way that your program is coded, if 99 turns come along and you have still not gotten your guy, the program ends. I don't think this is the intended behavior of the program. Actually, this is intended. Also a form of keeping score, thought about using For(99,1,-1, but that uses more space ;] Instead, what you should have used is a while( or a repeat( loop. Besides these two flaws, there is just general optimizations that your program lacks. This is the code that resulted from taking all of these things into account.

Code: Select all

1->A:1->B
8->C:8->D
Repeat A=C and B=D
ClrHome    ;Flashing effect, don't want this
getKey->K
A+(Ans=34 and A<8)-(Ans=25 and A>1->A
B+(K=26 and B<16)-(K=24 and B>1->B
Output(A,B,9
Output(C,D,6
randInt(-1,1
C+(Ans=1 and C<8)-(Ans=-1 and C>1->C
randInt(-1,1
D+(Ans=1 and D<16)-(Ans=-1 and D>1->D
End
Guest

Post by Guest »

Oh yeah, and your optimizations

randInt(-1,1

can be changed to

randInt(1,3
Gambit
Sir Posts-A-Lot
Posts: 252
Joined: Mon 21 Feb, 2005 5:34 am
Location: Laveen, Arizona

Post by Gambit »

Have you two Guests even considered registering to this board? I'm finding myself having to backtrack to distinguish between you two. :? In the long run, registering will save us a lot of headache.
"If SOURCE is outlawed, only outlaws will have SOURCE."
Guest

Post by Guest »

Oh yeah, and your optimizations

randInt(-1,1

can be changed to

randInt(1,3
This depends on how you want the AI player to move. Do you want to have the AI player sporadically jumping or do you want it moving by one?
koolmansam375
Extreme Poster
Posts: 479
Joined: Fri 17 Dec, 2004 11:09 pm
Contact:

Post by koolmansam375 »

Anonymous wrote:
Oh yeah, and your optimizations

randInt(-1,1

can be changed to

randInt(1,3
This depends on how you want the AI player to move. Do you want to have the AI player sporadically jumping or do you want it moving by one?
are you quoting yourself or is this a different 'Guest'. Please register as it will save me headaches too. Afaik the two 'Guest's could be the same person having a convo with himself
Image

Pongwars shall live!

blog is down atm. :-(
User avatar
kv83
Maxcoderz Staff
Posts: 2735
Joined: Wed 15 Dec, 2004 7:26 pm
Location: The Hague, Netherlands
Contact:

Post by kv83 »

Guests can also enter an own temporary username when posting
Image
Virtuoso

Post by Virtuoso »

To get rid of flashing effects you can store the coordinates in another variable so you can do this

1.Get new coordinates
2.Output " " at the old coordinates
3.Output # at the new coordinates
4.Put new coord in old

Or you could place step 4 at the top or step 1 at the bottom

~Virtuoso
User avatar
DJ_O
Calc King
Posts: 2324
Joined: Mon 20 Dec, 2004 6:47 pm
Location: Quebec (Canada)
Contact:

Post by DJ_O »

Just a lil something off-topic, but in the other topic CoBB told me to use boolean logic so yesterday I looked at my walking engine code and I figured out a new way to handle animated walking sprite, a method that can be used for many thing, like switching back and forth a variable. Assuming that you store 0 in H. Instead of

Code: Select all

If H=0
Then
1->H
Else
0->H
End
use

Code: Select all

H=0->H
You save 8 bytes and it's a lot faster since it doesn't even have a If statement.
Image Image Image Now active at https://discord.gg/cuZcfcF (CodeWalrus server)
Guest

Post by Guest »

You can just use the not operator

Code: Select all

not(H->H
User avatar
DJ_O
Calc King
Posts: 2324
Joined: Mon 20 Dec, 2004 6:47 pm
Location: Quebec (Canada)
Contact:

Post by DJ_O »

Even better (not much speed difference though :? )
Image Image Image Now active at https://discord.gg/cuZcfcF (CodeWalrus server)
AsDf

Post by AsDf »

Anonymous wrote:
Oh yeah, and your optimizations

randInt(-1,1

can be changed to

randInt(1,3
This depends on how you want the AI player to move. Do you want to have the AI player sporadically jumping or do you want it moving by one?
Well, you're using it in this context:

Code: Select all

randInt(-1,1
C+(Ans=1 and C<8)-(Ans=-1 and C>1->C
randInt(-1,1
D+(Ans=1 and D<16)-(Ans=-1 and D>1->D 
In essence, the outcome of the randInt will not affect the movement. Instead, it only decides which direction it travels, not the distance it travels.
DarkerLine
Calc Wizard
Posts: 526
Joined: Tue 08 Mar, 2005 1:37 am
Location: who wants to know?
Contact:

Post by DarkerLine »

You could also allow wrapping from one side of the screen to the other:

Code: Select all

C+randInt(-1,1 
Ans+8(not(Ans)-(Ans>8 -> C
D+randInt(-1,1 
Ans+16(not(Ans)-(Ans>16 -> C
User avatar
dysfunction
Calc Master
Posts: 1454
Joined: Wed 22 Dec, 2004 3:07 am
Location: Through the Aura

Post by dysfunction »

I need help optimizing the code in Aura's overworld engine. Keep in mind, in many places where the code is clunky I would be unable t do it any other way, because of the ridiculous abilities required of this engine.

Code: Select all

//Main walking loop

35->X
Asm(prgmZPIC     //each screen-sized section of map is represented by a                                      pic; this map is 2x6, therefore pic 31 is the top left corner and pic 36 is the bottom right
19:Asm(prgmCODEX
24->A     //chrcter x coord
32->B     //character y coord
2->S
prgmZUP     //displays character facing upwards
While 1
getKey->K
If K=24
Then
If A>=8     //if character is not about to step off the left edge of the screen
Then
prgmZHITLEFT   //runs collision detection

Else
If X/=31 and X/=33 and X/=35   //if moving the character will not move him off the left edge of the map 
Then
X-1->X
Asm(prgmZPIC
72->A
prgmZLEFT   //displays character facing left
End
End
End
If K=25
Then
If B>=4
Then
prgmZHITUP     //collision detection

Else
If X>32
Then
X-2->X
Asm(prgmZPIC
32->B
prgmZUP
End
End
End
If K=26
Then
If A<=68
Then
prgmZHITRIGH


Else
If X/=32 and X/=34 and X/=36
Then
X+1->X
Asm(prgmZPIC
0->A
prgmZRIGHT
End
End
End
If K=34
Then
If B÷28
Then
prgmZHITDOWN


Else
If X<35
Then
X+2->X
Asm(prgmZPIC
0->B
prgmZDOWN
End
End
End
End
Also need help optimizing my collision detection (this is realy ugly code):

Code: Select all

//downwards collision detection

If X=31     //if pic=31
Then
For(L,1,dim(LX131     //scans the list containing the x value of the top right corner of each collideable object in pic31
If LX131(L)<=A+24 and LX231(L)>=A and LY131(L)<=B+40 and LY231(L)>=B+36     //if moving the character will move him over an object
Then
LX131(L)->T
LY131(L)->U
If LT31(L)=1
Then
B+8->B
1->M
prgmZDOWN     //draws the chcracter facing downwards
prgmZTILE1     //masks the tile over the character
Return
End
prgmZDOWN
Return
End
End
End
// this is repeated for each pic, and that whole program is repeated for the other three directions
Image


"You're very clever, young man, but it's turtles all the way down!"
DarkerLine
Calc Wizard
Posts: 526
Joined: Tue 08 Mar, 2005 1:37 am
Location: who wants to know?
Contact:

Post by DarkerLine »

What if you had only one ZHIT program and passed a variable, say D, to it, to indicate the direction?

It would go somewhat like this:

Code: Select all

//up = 1, down = 2, left = 3, right = 4

If X=31     //if pic=31 
Then 
For(L,1,dim(LX131
If //this code will probably need to be changed depending on D
Then 
LX131(L)->T 
LY131(L)->U 
If LT31(L)=1 
Then 
B+8(D=2) - 8(D=1 -> B 
A+8(D=4) - 8(D=3 -> A
1->M
prgmZDIR     //draws the character
prgmZTILE1     //masks the tile over the character 
Return 
End 
prgmZDIR
Return 
End 
End 
End 

prgmZDIR:
If D= 2 : prgmZDOWN
If D= 1 : prgmZUP
If D= 3 : prgmZLEFT
If D= 4 : prgmZRIGHT
Post Reply