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

## Recommended Posts

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.

##### Share on other sites

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

AND #5

[code]

... 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 ?

##### Share on other sites

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

##### Share on other sites

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

##### Share on other sites

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

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

##### Share on other sites

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

##### Share on other sites

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 ?

##### Share on other sites

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.

##### Share on other sites
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!

##### Share on other sites
@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:

ex_move_sprite_with_coordinates_2013y_03m_15d_1256t.bin

Here's the bB code:

ex_move_sprite_with_coordinates_2013y_03m_15d_1256t.bas

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

##### Share on other sites

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 & 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 & 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 & 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 & 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

##### Share on other sites

Is this where Space Invaders are born?

##### Share on other sites

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.

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.