Omnimaga

Calculator Community => TI Calculators => TI-BASIC => Topic started by: necro on September 07, 2008, 04:44:32 am

Title: scripts and code snippets
Post by: necro on September 07, 2008, 04:44:32 am
I thought it might be nice to have a place to put bits of useful code.  Make sure to label whether its basic, psuedo code, asm, 68k c, etc.  I should have more up soon.

ti-basic:
Code: (Odd or Even) [Select]
(Ans/2)=round((Ans/2),0
Title: Re: scripts and code snippets
Post by: DJ Omnimaga on September 07, 2008, 02:59:59 pm
we might want to separate ASM from BASIC/hybrid though, else it will get cluttered fast for people who only need help on one of them and even get confusing since people might forget to specify for which calc and language it is.

I'll go with the simple, old but useful and undocumented tricks for TI-83+ BASIC

Quote
Circle(X,Y,Radius,{i}
adding the {i} as 4th argument to Circle commands makes circles display about 4x faster than normal. No more need for ASM routines to have fast circles ^.^

Quote
Text(-1,X,Y,"ENTER TEXT HERE
adding the -1 argument allows you to display text in bigger fonts on the graph screen (same fonts than on the home screen)

The following code is for both xLIB and Celtic III flash app usage, to detect if they are enabled or not. Put at the start of your BASIC game:
Quote
:1
:real(0
:If not(Ans:Then
:Disp "XLIB NOT ENABLED
:Stop
:End
Title: Re: scripts and code snippets
Post by: Tribal on September 07, 2008, 09:30:00 pm
Let's see... I guess I'll put what I remember, I don't feel like rummaging trough my calc for them, I might later ;D


;;TI-BASIC(non-hybrid)

A replacement for randInt((lowerbound), (upperbound)):
Code: [Select]
(lowerbound)+int((upperbound)rand)the only thing about this is if it's zero or below you have to modify upperbound, for example, if you want a 0 to be lowerbound and 1 to be upperbound you would have to do '0+int(2rand)'

If you want to do:
Code: [Select]
62->Ymax
94->Xmax
You could save a few bytes by doing:
Code: [Select]
1->ΔY
1->ΔX

storing a number to a variable without messing up Ans:
Code: [Select]
For((var), (#), 0)
End

;;END IF TI-BASIC(non-hybrid)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;ASM

when reseting the carry flag, use:
Code: [Select]
or Ainstead of:
Code: [Select]
scf
ccf

if you want to multiply the 'a' registry by 2 without effecting the zero flag, use:
Code: [Select]
rlca
;instead of
;add A, A

instead of assigning CurRow and CurCol to different values you could do:
Code: [Select]
ld bc,20*256+34
ld (CurRow), bc ;loads 20 into CurRow and 34 into CurCol

;;END IF ASM
Title: Re: scripts and code snippets
Post by: tr1p1ea on September 07, 2008, 10:31:09 pm
rlca doesnt effect the z flag, but it wont always multiply by 2 since it rotates the bits left circularly.
Title: Re: scripts and code snippets
Post by: DJ Omnimaga on September 07, 2008, 11:29:30 pm
If you want to do:
Code: [Select]
62->Ymax
94->Xmax
You could save a few bytes by doing:
Code: [Select]
1->ΔY
1->ΔX
mhmm it doesn't really works, because for me it sets Xmin to -10, Xmax to 84, Ymin to -10 and Ymax to 52 x.x
Title: Re: scripts and code snippets
Post by: Tribal on September 08, 2008, 07:58:43 am
hm? that's weird... it works for me

EDIT: I think that Xmin and Ymin already have to be set to 0 >.<
Title: Re: scripts and code snippets
Post by: DJ Omnimaga on September 08, 2008, 02:48:37 pm
mhmm maybe, I guess I'll try this again. Hopefully if it works I'll be able to save a few more bytes :P
Title: Re: scripts and code snippets
Post by: Tribal on September 09, 2008, 04:24:32 pm
Yeah it does, I tried it right after I posted that, I would have re-edited but I had to go to my next class  :-\
Title: Re: scripts and code snippets
Post by: kalan_vod on September 10, 2008, 01:06:21 pm
Can we just repost the old code?
Title: Re: scripts and code snippets
Post by: DJ Omnimaga on September 10, 2008, 08:48:48 pm
which old code? O.o
Title: Re: scripts and code snippets
Post by: kalan_vod on September 12, 2008, 04:06:35 am
Just all the old tricks and tips..
Title: Re: scripts and code snippets
Post by: DJ Omnimaga on September 12, 2008, 04:09:16 am
oh that, there are no problem, altough some might have been only on the old board, so it might take a while before we can retrieve them
Title: Re: scripts and code snippets
Post by: bfr on September 28, 2008, 09:51:04 pm
Smallest possible TI-83+ basic quadratic solver (where the input is all done in the program, as opposed to storing inputs in variables first and then running the program, and the user doesn't have to input obscure things, like C/A into some variable):

Code: [Select]
Prompt Y1,A,C
solve(Y1,X,5
{Ans,C/A/Ans

38 bytes

I got this with the help of a little trick (http://en.wikipedia.org/wiki/Quadratic_equation#Vi.C3.A8te.27s_formulas).  Note that it can't handle not having two real roots or when the first root found is zero, and your input should look something like:
Quote
Y1=?"X^2+X-6"
A=?1
C=?-6
to solve the equation x^2+x-6=0, for example.

If you want it to work when the first root detected is zero, you can do:

Code: [Select]
Prompt Y1,A,B
solve(Y1,X,5
{Ans,-B/A-Ans

which is 39 bytes

EDIT: I accidentally was including the names of the programs when giving their sizes, so they're actually 33 and 34 bytes
Title: Re: scripts and code snippets
Post by: DJ Omnimaga on December 08, 2008, 03:54:39 pm
There is a very unprofessional trick to slow down a loop in TI-BASIC but it takes so few RAM.

Let says you want to have a For loop to slow down a loop where there is an animation. Instead of doing "For(Z,0,99:End" for example, just do "Rand". If you want to slow it down even more use something like "RandRand" or even "RandRandRand". This might not be very accurate and it updates the Ans variable but you save a whole lot of RAM when doing this. You can also do stuff like "cos cos cos 1" on the TI-81,82,85 and 86 or "cos(cos(cos(1" on the TI-73,83 and 84+ but it won't cause as much of a delay as rand and it takes more space since you need to use more sin/cos/tan instructions
Title: Re: scripts and code snippets
Post by: simplethinker on December 08, 2008, 04:09:42 pm
[Basic] Here's something for storing very large matrices in programs.  If you do something like
Code: [Select]
:[[1,2,3,4,5][1,2,3,4,5],[1,2,3,4,5],....]]→[A]you'll get your matrix stored in [A], as well as in Ans.  This means that until your next Ans-altering command the matrix you just stored will be taking double :o the amount of RAM used.  If your map is large, let's say 10kb, then you will need to have 20kb of free RAM at initialization, severely limiting your options.  One way to get around this is something like
Code: [Select]
:{32,32→dim([A]
:For(A,1,32
:For(B,1,32
:Z→[A](A,B
:End:End
but it would be extremely slow.

Solution:  Make the columns of the matrix into lists and use List▶matr
Code: [Select]
:List▶matr({1,1,1,...},{2,2,2,...},...,[A])For a 32x32 matrix this takes 1-2 seconds, but it saves you from having to make sure you have double the RAM needed for the matrix and certainly saves a ton of time compared with that little For( loop.
Title: Re: scripts and code snippets
Post by: metagross111 on December 08, 2008, 05:45:54 pm
whoa, i wasn't aware this topic existed! its got some interesting stuff.
Title: Re: scripts and code snippets
Post by: Builderboy on April 22, 2009, 12:45:41 am
here is a good one for people trying to fit in all their subprograms into 1 program.

While 0
Lbl A
Disp "This is inside a subprogram
End

For(F,-1,0
If F
Goto A
End


It allows the program execution to jump to any place in the program, and then return back to where it was called.  And it causes no Memory leaks, so your program will not slow down because of it!

You can even use the subprogram inside your code instead of just pasting it at the begining with a While 0 in front of it

Repeat 1
Lbl A
Disp "This will be executed once
Disp "And then again froma subProgram
End

For(F,-1,0
If F
Goto A
End


That way you can have at least one part in your code where it does't need to jump to the subprogram, because everything else is jumping to it!
Title: Re: scripts and code snippets
Post by: DJ Omnimaga on April 22, 2009, 01:36:19 am
mhmm I'll study this a bit, it will prbly take a while for me to get used to this structure, because I just started coding 2 months ago for the first time in 18 months and most of my programming activity was around 2002, when using lbl/goto anywhere without caring about memory leaks was legion. On top of that I have trouble learning and understanding stuff it seems (probably the reason why I sucked so much at reading tests and why I never managed to succeed at learning assembly, even after 3 hard tries) It might be useful to merge some subroutines together or something. Maybe not in Illusiat, though, because almost everything needs to be stored into archive and only copied temporarly in RAM when needed (sub-routines, map data, etc.) due to the incredible file size and amount of maps (over 200 so far and game is only 20% complete)
Title: Re: scripts and code snippets
Post by: nitacku on April 23, 2009, 05:40:56 pm
I used a similar method in a number of my programs.
You want to make sure that your "sub-program" is located at the top of the program because the time it takes for the interpreter to locate the Lbl is directly proportional to its location from the top of the program.

The main advantage of this method is you can send information via Ans, and you don't use any variables.

Start of program:
:Code
:More code
:Goto 0 (you have to skip to the main code since this next part will be the sub-programs)
:Lbl 1
:woot!
:subprogram
:0 (needed so that the loop will end)
:End (normally this would be extraneous, but we induced a temporary memory leak that this will fix)
:Lbl 0 (start of main code)
:code
:stuff
:the last digit of pi is 0
:lets run a sub-program
:1 (can be any non-zero value. This is useful if you want to send information via Ans to subprograms)
:Repeat Ans
:If Ans
:Goto 1
:1
:End
:back to main code
:example finished.
Title: Re: scripts and code snippets
Post by: Halifax on May 26, 2009, 06:59:26 pm
BASIC: Length of Integer
Code: [Select]
1+iPart(log(N
This will return the number of the digits that make up the number. Note the use of Integer, meaning that it will not work on floating-point values.
Ex: N = 1234, Output = 4

z80 ASM: Pow2 Multiplication Trick
Code: [Select]
;a = input
ld d,a
ld e,0 ; de = a * 256
srl d \ rr e ; de = a * 128
srl d \ rr e ; de = a * 64
etc.

z80 ASM: Raw Key Input Optimization
Code: [Select]
ld a,$FF
out (1),a
ld a,$FE
out (1),a
nop \ nop
in a,(1)
becomes
Code: [Select]
ld a,$FF
out (1),a
dec a
out (1),a
ld a,(de)
in a,(1)
As you can see, the use of the first optimization can only occur when analyzing the key group $FE. The second optimization takes into account the fact that a will be destroyed by the port read, but the instruction will still delay for the same amount of time that two nop's will.
Title: Re: scripts and code snippets
Post by: skuller972 on May 26, 2009, 07:04:54 pm
that basic one is pretty good!