I have a bad code block, needs some optimization. Help much appreciated.

Code:
[[12,13,13,13,13,13,13,13][12,13,13,13,13,13,13,13][12,13,13,13,13,13,13,13][12,13,13,13,13,13,13,13][12,13,13,13,13,13,25,25][12,25,25,25,25,26,6,13][15,27,13,13,13,13,13,0]]->[A]  //Matrix for map
{2,3,4,7,8,15,49,53}->L1 //Key codes to accept
{5,6,8,13,27}->L2 //Blocks you can walk through

//Setup Stuff
Func
StoreGDB 1
FnOff
PlotsOff
AxesOff
CoordOff
ExprOff
GridOff
RectGC
ClrDraw
Full
ZStandard
ZInteger
UnArchive Pic1
Horizontal ~25

DrawTileMap(0,0,0,8,7,2,10,0,7,1,0,8,1
DelVar IDelVar JDelVar KDelVar HDelVar FDelVar G32->A:48->B:1->E
DrawSprite(A,B,1,8,1,7,0,3,1

While K!=15 //Code Loop for everything

//pixel to matrix #
A/8-1->C
B/8+1->D

If max(K=L1 //If it gets a key, remove sprite
DrawSprite(A,B,1,8,1,7,0,3,0,1

If H //If jump and move, erase/draw sprite
DrawSprite(I,J,1,8,1,7,0,3,0,1

GetKey( //real(8
Ans->K

If max(K=L1) or H //If you have moved, draw sprite
DrawSprite(A,B,1,8,1,7,0,3,0,1

//Code for detecting jump/ drawing
DelVar H
not(not(F->G
F-(F!=0->F
If not(F) and G
1->H

//movement
If (K=3 or K=8) and C<8
Then
If max([A](D,C+1)=L2
A+8->A
End

If (K=2 or K=7) and C>1
Then
If max([A](D,C-1)=L2
A-8->A
End

B-8(K=4 and max([A](D-1,C)=L2->B

If C>1
B-8(K=7 and max([A](D-1,C-1)=L2->B

If C<8
B-8(K=8 and max([A](D-1,C+1)=L2->B

If (A!=I or B!=J) and F
Then
DelVar FDelVar HDelVar G
End

A/8-1->C
B/8+1->D
(K=4)->E

If D<7:Then
If max([A](D+1,C)=L2)
Then
A->I
B->J
If not(E) or K=2 or K=3
B+8->B
7->F
DelVar H
End
End

//code to see what F is doing
Text(~1,0,0,"  "
Text(~1,0,0,F

DelVar E
End

I got most of the bugs out of it, only one left is one that deals with F and jumping.
I am trying to get it to jump, but fall after ~5 runs through the movement code. It just has a little trouble jumping/falling.

Also, it runs slower than I would like it to, if it's possible to speed it up that would be great. Thanks!

Code:
[[12,13,13,13,13,13,13,13][12,13,13,13,13,13,13,13][12,13,13,13,13,13,13,13][12,13,13,13,13,13,13,13][12,13,13,13,13,13,25,25][12,25,25,25,25,26,6,13][15,27,13,13,13,13,13,0]]→[A]  //Matrix for map
{2,3,4,7,8,15,49,53}→L1 //Key codes to accept
{5,6,8,13,27}→L2 //Blocks you can walk through

//Setup Stuff
StoreGDB 1
Func
FnOff
PlotsOff
AxesOff
CoordOff
ExprOff
GridOff
RectGC
Full
ZStandard
ZInteger
UnArchive Pic1
Horizontal ~25

DrawTileMap(0,0,0,8,7,2,10,0,7,1,0,8,1
DelVar IDelVar JDelVar KDelVar HDelVar FDelVar G32→A:48→B:1→E
DrawSprite(A,B,1,8,1,7,0,3,1

While K!=15 //Code Loop for everything

//pixel to matrix #
A/8-1→C
B/8+1→D

If max(K=L1:DrawSprite(A,B,1,8,1,7,0,3,0,1 //If it gets a key, remove sprite

If H:DrawSprite(I,J,1,8,1,7,0,3,0,1 //If jump and move, erase/draw sprite

GetKey→K //real(8

If max(K=L1) or H:DrawSprite(A,B,1,8,1,7,0,3,0,1 //If you have moved, draw sprite

//Code for detecting jump/ drawing
DelVar H
not(not(F→G
F-(F!=0→F
If not(F) and G:1→H

//movement
If (K=3 or K=8) and C<8
Then
If max([A](D,C+1)=L2
A+8→A
End

If (K=2 or K=7) and C>1
Then
If max([A](D,C-1)=L2
A-8→A
End

B-8(K=4 and max([A](D-1,C)=L2→B

If C>1
B-8(K=7 and max([A](D-1,C-1)=L2→B

If C<8
B-8(K=8 and max([A](D-1,C+1)=L2→B

If (A!=I or B!=J) and F
Then
DelVar FDelVar HDelVar G
End

A/8-1→C
B/8+1→D
(K=4)→E

If D<7:Then
If max([A](D+1,C)=L2)
Then
A→I
B→J
If not(E) or K=2 or K=3
B+8→B
7→F
DelVar H
End
End

//code to see what F is doing
Text(~1,0,0,"  "
Text(~1,0,0,F

DelVar E
End

//assuming this is where the code ends
RecallGDB 1
Can you explain a little bit about the approach you took, Comments?
The condensing of the code does help a little, but I was thinking more about the speed of execution of this code, more like optimizing it for better speed. Are there any suggestions for how I should go about writing jumping code? It sets a jump and then just runs through the loop of code. I feel like there might be a better way to do that?
There are two ways I can see to speed up your code.

First, calling DelVar on a variable and creating it again repeatedly takes significantly longer than storing 0 to it. Every time you create a new variable, the calculator needs to create a new VAT entry, and that takes many milliseconds.

Second, you store the walkability as a list of walkable blocks in L2. This requires looking up the block type in L2—therein comparing it to five different numbers—to determine whether a block is walkable. It would be faster to make a 27-element list that stores whether each block type is walkable. That is,

Code:

{5,6,8,13,27}
; can be (assuming there are only 27 block types)
{0,0,0,0,1,1,0,1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1}

max([A](D,C-1)=L2
; would be
L2([A](D,C-1)

If the numbering isn't important, consider making the walkable blocks types just 1 through 5 or something, which would simplify the check even more.

There are some optimizable snippets, too:

Code:
DelVar H
not(not(F->G
F-(F!=0->F
If not(F) and G
1->H

; can be

F=1->H
F-not(not(F->F

unless you use G somehow.

I don't have the time to find the bug right now.
Thanks so much
The numbering of the blocks is important because of where that tile is on the sprite sheet. I can go through and move the sprites around though.
I Haven't found any big optimizations, although I do see some repetitive parts which usually means there is room for optimization, however, just skimming through it real quick, I noticed two little things...
1)
Code:
If (A!=I or B!=J) and F
Then
DelVar FDelVar HDelVar G
End

Consecutive DelVars don't need the Then:End statements, so it could be:

Code:
If (A!=I or B!=J) and F
DelVar FDelVar HDelVar G

2)Assuming you don't use F and G elsewhere,

Code:
DelVar H
not(not(F→G
F-(F!=0→F
If not(F) and G:1→H

could be

Code:
F=1→H

However, if you do make use of it and need to have F and G changed, G is already 1/0 depending on whether F is nonzero or not. so this:

Code:
not(not(F→G
F-(F!=0→F

could still be

Code:
not(not(F→G
F-G→F

Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

»
» All times are UTC - 5 Hours

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum