I've managed to solve a number picking algorithm that I'm happy with the performance of in NextBASIC. It's a Fisher Yates sort, then picking the first 4 items (actually I cut the sort at the fourth iteration).
#autoline 10,10
%N=20: ; select from 0-20 (excluding 20)
%P=4: ; pick 4 numbers
; init the 0-N array
REPEAT
%S(i)=%i
%i=%i+1
REPEAT UNTIL %i=N
REPEAT
%T=% RND i
%W(N-i)=%S(T)
%i=%i-1
%S(T)=%S(i)
REPEAT UNTIL %i=(N-P)
; the picked results are stored in W
Now I need to check this array when the tomb is surrounded. I could check the whole of this array each time the tomb is surrounded, but it makes more sense to add which are the "winning" tombs to the tomb array(20) I already have.
The tomb array itself holds it's state in bit values. As such, the current line of code for marking one side of a tomb as "completed" looks like this (horribly):
IF %k THEN %T[g]=%T[g]&@11111101:%T[h]=%T[h]&@11110111: ; new line for readability
ELSE %T[g]=%T[g]&@11111011:%T[h]=%T[h]&@11111110
The low nibble represent the side of the tomb that has been completed, and the high nibble for whether the tomb has been unlocked already:
0x01=top
0x02=right
0x04=bottom
0x08=left
0xF0=tomb has been unlocked
So the IF
statement from earlier is always run and always applies the logical &
but this is to create consistent timings. Normally, in modern coding, I'd avoid unnecessary code execution, but with NextBASIC to get the game to run at a consistent speed, I'm making sure that each loop does roughly the same amount of work.
Thus far I'm only using 8 bits of the value to indicate the status of the tomb and I need to be able to flag the tomb to say what's contained inside of it, either a scroll, a mummy, a key, a tomb or nothing at all. I could keep all this data inside a single byte, but the integers in NextBASIC as 16bit so I could also expand out to the high byte to store the value of the tomb.
Meh. I prefer to be efficient, so how about this:
0x00-0x04=tomb contents
0x08=tomb has been unlocked
0x10=top
0x20=right
0x40=bottom
0x80=left
This shifts around where the data is stored, but I put the wall status on the high nibble for simplicity, so that I can then do something like %T = %T + INT (key)
and set key = 3
somewhere as a constant (though in reality I'll stick to integer expressions).