# Find the wrap around borders for the ball, missiles and sprites

37 replies to this topic

### #26 Atarius MaximusOFFLINE

Atarius Maximus

Stargunner

• 1,884 posts
• 10 PRINT CHR\$(205.5+RND(1));
• Location:St. Louis, Missouri USA

Posted Thu Mar 14, 2013 6:03 PM

I don't know how the math behind it works, exactly. Would it require the div_mul.asm module or is it basically just a very convenient wrap-around function?

I don't know exactly how it works, I've never dived in to trying to understand it. It's amazing how much I've relied on RT's website over the years. His website is the bible of bB.

### #27 CybeargOFFLINE

Cybearg

Dragonstomper

• 950 posts

Posted Thu Mar 14, 2013 6:05 PM

Well, looking at the Assembly, a & 5 translates to a logical AND operation:

[code=auto:0]
AND #5
[code=auto:0]

... Though I'm not sure if that relies on being a power of 2 or not. And does it have to be an odd number or could one have a value between 0 and 12 by using & 12 ?

### #28 RevEngOFFLINE

RevEng

Bit Player

• 5,172 posts
• Location:bottom of the stack

Posted Thu Mar 14, 2013 6:49 PM

It relies on the range being a power-of-two.

### #29 CybeargOFFLINE

Cybearg

Dragonstomper

• 950 posts

Posted Thu Mar 14, 2013 7:02 PM

It relies on the range being a power-of-two.

Is there any quick trick for doing the equivalent of a & 5 or whatnot without relying on something like the following?

```tryagain
temp1 = rand & 7
if temp1 > 5 then goto tryagain
```

or maybe

```temp1 = rand & 7
if temp1 > 5 then temp1 = temp1 - 5
```

... but that last one would give unfair priority to 0 and 1

Edited by Cybearg, Thu Mar 14, 2013 7:04 PM.

### #30 RevEngOFFLINE

RevEng

Bit Player

• 5,172 posts
• Location:bottom of the stack

Posted Thu Mar 14, 2013 7:13 PM

There are other options, but with varying degrees of distribution uniformity.

A lot of the options were posted in the "random placement" thread.

### #31 bogaxOFFLINE

bogax

Dragonstomper

• 791 posts

Posted Thu Mar 14, 2013 8:34 PM

I incorporated R.T.s score debug code when you press fire. This can't be used realtime because as the debug variable reaches 200+ the for .. next loop goes over cycle.

I almost had success by doing something like this:

score = 0
score = score + debugval
if debugval > 165 then temp1 = debugval - 165 : score = score + temp1

Basically, to me it looked like the score rolls over after 165. I figured if I added the part over 165 AFTERWARDs then I'd need just two lines instead of a long for .. next loop.

Didn't work completely.

here's some quick and dirty you could try

``` dim scr0 = \$93
dim scr1 = \$94
dim scr2 = \$95

scorecolor = \$AA

temp1 = 125

rem puts temp1 in the upper three score digits

scr0= 0 : scr1 = scr1 & 15
if temp1 > 99 then scr0= scr0+ 16 : temp1 = temp1 - 100
if temp1 > 99 then scr0= scr0+ 16 : temp1 = temp1 - 100
if temp1 > 49 then scr0= scr0+ 5 : temp1 = temp1 - 50
if temp1 > 29 then scr0= scr0+ 3 : temp1 = temp1 - 30
if temp1 > 19 then scr0= scr0+ 2 : temp1 = temp1 - 20
if temp1 > 9 then scr0= scr0+ 1 : temp1 = temp1 - 10
scr1 = (temp1 * 4 * 4)| scr1

temp1 = 234

rem puts temp1 in the lower three digits of score

scr1 = scr1 & 240 : scr2 = 0
if temp1 > 99 then scr1 = scr1 + 1 : temp1 = temp1 - 100
if temp1 > 99 then scr1 = scr1 + 1 : temp1 = temp1 - 100
if temp1 > 49 then scr2 = scr2 + 80 : temp1 = temp1 - 50
if temp1 > 29 then scr2 = scr2 + 48 : temp1 = temp1 - 30
if temp1 > 19 then scr2 = scr2 + 32 : temp1 = temp1 - 20
if temp1 > 9 then scr2 = scr2 + 16 : temp1 = temp1 - 10
scr2 = scr2 | temp1

LOOP
drawscreen
goto LOOP
```

I just poke a byte to the playfield

Edited by bogax, Thu Mar 14, 2013 10:06 PM.

### #32 bogaxOFFLINE

bogax

Dragonstomper

• 791 posts

Posted Thu Mar 14, 2013 8:50 PM

There are other options, but with varying degrees of distribution uniformity.

A lot of the options were posted in the "random placement" thread.

Did you try this ?

### #33 GemintronicOFFLINE

Gemintronic

Jason S. - Lead Developer & CEO

• Topic Starter
• 9,314 posts

Posted Fri Mar 15, 2013 9:39 AM

here's some quick and dirty you could try

```dim scr0 = \$93
dim scr1 = \$94
dim scr2 = \$95

scorecolor = \$AA

temp1 = 125

rem puts temp1 in the upper three score digits

scr0= 0 : scr1 = scr1 & 15
if temp1 > 99 then scr0= scr0+ 16 : temp1 = temp1 - 100
if temp1 > 99 then scr0= scr0+ 16 : temp1 = temp1 - 100
if temp1 > 49 then scr0= scr0+ 5 : temp1 = temp1 - 50
if temp1 > 29 then scr0= scr0+ 3 : temp1 = temp1 - 30
if temp1 > 19 then scr0= scr0+ 2 : temp1 = temp1 - 20
if temp1 > 9 then scr0= scr0+ 1 : temp1 = temp1 - 10
scr1 = (temp1 * 4 * 4)| scr1

temp1 = 234

rem puts temp1 in the lower three digits of score

scr1 = scr1 & 240 : scr2 = 0
if temp1 > 99 then scr1 = scr1 + 1 : temp1 = temp1 - 100
if temp1 > 99 then scr1 = scr1 + 1 : temp1 = temp1 - 100
if temp1 > 49 then scr2 = scr2 + 80 : temp1 = temp1 - 50
if temp1 > 29 then scr2 = scr2 + 48 : temp1 = temp1 - 30
if temp1 > 19 then scr2 = scr2 + 32 : temp1 = temp1 - 20
if temp1 > 9 then scr2 = scr2 + 16 : temp1 = temp1 - 10
scr2 = scr2 | temp1

LOOP
drawscreen
goto LOOP
```

I just poke a byte to the playfield

THIS WORKS!!!! Thank you bogax!!! =)

The only change I made was how the DIM'ing took place:

dim scr0 = score
dim scr1 = score+1
dim scr2 = score+2

This makes it a bit more compatible in case differing kernels use different memory locations for the score.

@R.T. you should review bogaxs new score debug code as it seems to be MUCH faster than the for .. next loop method.

### #34 RevEngOFFLINE

RevEng

Bit Player

• 5,172 posts
• Location:bottom of the stack

Posted Fri Mar 15, 2013 9:48 AM

Did you try this ?

The original random screen placement thread wasn't mine, but I'll give your code a shot next time I have the need for a random number in a non-power-of-two range!

### #35 Random TerrainONLINE

Random Terrain

Visual batari Basic User

• 28,952 posts
• Controlled Randomness
Replay Value
Nonlinear
• Location:North Carolina (USA)

Posted Fri Mar 15, 2013 11:32 AM

@R.T. you should review bogaxs new score debug code as it seems to be MUCH faster than the for .. next loop method.

That is much faster and doesn't go over 262. I stuck it in an existing example program to see it in action.

Here's the .bin file to use with an emulator or Harmony cart:

Here's the bB code:

I'll edit the bB page later today with the new code. Thanks bogax.

### #36 bogaxOFFLINE

bogax

Dragonstomper

• 791 posts

Posted Fri Mar 15, 2013 7:34 PM

I'll edit the bB page later today with the new code. Thanks bogax.

Here's essentially the same tarted up a bit
```hi_scor
rem puts temp1 in the upper three score digits

scr0 = 0 : scr1 = scr1 &amp; 15
if temp1 >= 100 then scr0 = scr0 + 16 : temp1 = temp1 - 100
if temp1 >= 100 then scr0 = scr0 + 16 : temp1 = temp1 - 100
if temp1 >= 50 then scr0 = scr0 + 5 : temp1 = temp1 - 50
if temp1 >= 30 then scr0 = scr0 + 3 : temp1 = temp1 - 30
if temp1 >= 20 then scr0 = scr0 + 2 : temp1 = temp1 - 20
if temp1 >= 10 then scr0 = scr0 + 1 : temp1 = temp1 - 10
scr1 = (temp1 * 4 * 4) | scr1
return

lo_scor
rem puts temp1 in the lower three digits of score

scr1 = scr1 &amp; 240 : scr2 = 0
if temp1 >= 100 then scr1 = scr1 + 1 : temp1 = temp1 - 100
if temp1 >= 100 then scr1 = scr1 + 1 : temp1 = temp1 - 100
if temp1 >= 50 then scr2 = scr2 + 80 : temp1 = temp1 - 50
if temp1 >= 30 then scr2 = scr2 + 48 : temp1 = temp1 - 30
if temp1 >= 20 then scr2 = scr2 + 32 : temp1 = temp1 - 20
if temp1 >= 10 then scr2 = scr2 + 16 : temp1 = temp1 - 10
scr2 = scr2 | temp1
return
```

here's something similar but shorter and probably slower.
also uses two more temp variables

```hi_scor
rem puts temp1 in the upper three score digits
scor0 = 0 : scor1 = scor1 &amp; 15
hi_100s
if temp1 >= 100 then scor0 = scor0 + 16 : temp1 = temp1 - 100 : goto hi_100s
temp2 = 80 : temp3 = 8
hi_scor_loop
if temp1 >= temp2 then scor0 = scor0 + temp3 : temp1 = temp1 - temp2
temp2 = temp2 / 2 : temp3 = temp3 / 2
if temp3 then hi_scor_loop
scor1 = (temp1 * 4 * 4) | scor1
return

lo_scor
rem puts temp1 in the lower three digits of score
scor1 = scor1 &amp; 240 : scor2 = 0
lo_100s
if temp1 >= 100 then scor1 = scor1 + 1 : temp1 = temp1 - 100 : goto lo_100s
temp2 = 80 : temp3 = 128
lo_scor_loop
if temp1 >= temp2 then scor2 = scor2 + temp3 : temp1 = temp1 - temp2
temp2 = temp2 / 2 : temp3 = temp3 / 2
if temp2 >= 10 then lo_scor_loop
scor2 = scor2 | temp1
return
```

edit:

Here's another shorter bB version that is
even shorter and doesn't use more temp variables
and I think is not substantially slower than
the previous shorter bB version.

```hi_scor
rem puts temp1 in the upper three score digits
scor0 = 0
hi_scor_100s_loop
if temp1 >= 100 then scor0 = scor0 + 16 : temp1 = temp1 - 100 : goto hi_scor_100s_loop
goto enter_hi_scor_10s_loop
hi_scor_10s_loop
scor0 = scor0 + 1 : temp1 = temp1 - 10
enter_hi_scor_10s_loop
if temp1 >= 10 then hi_scor_10s_loop
scor1 = (((temp1 * 4 * 4) ^ scor1) & \$F0) ^ scor1
return

lo_scor
rem puts temp1 in the lower three digits of score
scor1 = scor1 & 240 : scor2 = 0
lo_scor_100s_loop
if temp1 >= 100 then scor1 = scor1 + 1 : temp1 = temp1 - 100 : goto lo_scor_100s_loop
goto enter_lo_scor_10s_loop
lo_scor_10s_loop
scor2 = scor2 + 1 : temp1 = temp1 - 10
enter_lo_scor_10s_loop
if temp1 >= 10 then lo_scor_10s_loop
scor2 = (scor2 * 4 * 4) | temp1
return
```

here's some asm

```hi_scor
rem puts temp1 in the upper three score digits
asm
ldx #\$FF
lda temp1
sec
hi_100s_loop
inx
sbc #\$64
bcs hi_100s_loop
sta temp1
txa
asl
asl
asl
asl
tax
lda temp1
sec
hi_10s_loop
inx
sbc #\$0A
bcs hi_10s_loop
dex
asl
asl
asl
asl
eor scor1
and #\$F0
eor scor1
sta scor1
stx scor0
rts
end

lo_scor
rem puts temp1 in the lower three digits of score
asm
ldx #\$FF
lda temp1
sec
lo_100s_loop
inx
sbc #\$64
bcs lo_100s_loop
sta temp1
txa
eor scor1
and #\$0F
eor scor1
sta scor1
lda temp1
ldx #\$FF
sec
lo_10s_loop
inx
sbc #\$0A
bcs lo_10s_loop
sta temp1
txa
asl
asl
asl
asl
ora temp1
sta scor2
rts
end
```

edit:
yet another (asm) version
a little shorter a little faster

```rem puts temp1 into the upper 3 score digits
rem uses the table defined in lo_score
hi_scor
asm
lda temp1
ldx #\$00
hi_100s_loop
cmp #\$64
bcc skip_hi_100s
sbc #\$64
inx
bne hi_100s_loop
skip_hi_100s
ldy scor_x10_tbl,x
sec
hi_10s_loop
iny
sbc #\$0A
bcs hi_10s_loop
dey
sty scor0
tax
lda scor_x10_tbl,x
eor scor1
and #\$F0
eor scor1
sta scor1
rts
end

rem puts temp1 into the lower 3 score digits
lo_scor
asm
lda scor1
and #\$F0
sta scor1
lda temp1
lo_100s_loop
cmp #\$64
bcc skip_lo_100s
sbc #\$64
inc scor1
bne lo_100s_loop
skip_lo_100s
ldx #\$FF
SEC
lo_10s_loop
inx
sbc #\$0A
bcs lo_10s_loop
ora scor_x10_tbl,x
sta scor2
rts

scor_x10_tbl
.byte \$00, \$10, \$20, \$30, \$40, \$50, \$60, \$70, \$80, \$90
end
```

edit I hate uncommented code does this need
commenting?

Edited by bogax, Mon Mar 18, 2013 11:04 PM.

### #37 iespostaONLINE

iesposta

River Patroller

• 3,890 posts
• Retro-gaming w/my VCS
• Location:Pennsylvania

Posted Sun Mar 17, 2013 8:01 AM

Is this where Space Invaders are born?

### #38 GemintronicOFFLINE

Gemintronic

Jason S. - Lead Developer & CEO

• Topic Starter
• 9,314 posts

Posted Tue Mar 19, 2013 9:23 AM

Is this where Space Invaders are born?

That effect is similar to my suggestion in Gate Racer to redirect the sprite pointer to code for an explosion effect. The multi sprite kernel seems to do the same thing for the playfield if you don't immediately have playfield data at the start.

#### 0 user(s) are browsing this forum

0 members, 0 guests, 0 anonymous users