02-15-2012, 06:56 PM

To see if 2 game characters can see each other you need 2 functions

distance:

line:

create 2 arrays to hold the x and y coordinates of the line

The size of the arrays should be the distance of the longest line you intend to create

a line going across the screen of the speccy is about 322 in length

You have 2 characters on a playing field that has x and y coordinates

you call the line function:

one character is at 1,1

the other is at 100,95

then finally you go through the co-ordinates on the line to check if anything is in the way, like walls or other characters

distance:

Code:

`Function distance (x1 as Integer, y1 as Integer, x2 as Integer, y2 as Integer) as Integer`

Dim x as Integer

x=ABS(x1-x2)

Dim y as Integer

y=ABS(y1-y2)

Return Sqr((x^2)+(y^2))

End Function

line:

Code:

`Function line(X1 as Integer,Y1 as Integer,X2 as Integer,Y2 as Integer) as Integer`

'creates a line of individual co-ords from X1,Y1 to X2,Y2 at any angle

Dim dist as Integer

dist=distance(X1,Y1,X2,Y2)

dim bresiter as Integer

For bresiter=0 To dist

bresx(bresiter)=0

bresy(bresiter)=0

Next

Dim steep as Integer

if Abs(Y2-Y1) > Abs(X2-X1) then

steep=1

else

steep=0

end if

PRINT AT 1,0 ; PAPER 1 ; INK 0 ; "steep "+STR(steep)

If steep =1 Then

Dim Temp as Integer

Temp=X1

X1=Y1

Y1=Temp

Temp=X2

X2=Y2

Y2=Temp

End If

Dim DeltaX as Integer

DeltaX=Abs(X2-X1)

Dim DeltaY as Integer

DeltaY=Abs(Y2-Y1)

Dim IError as Integer

IError=0

Dim DeltaError as Integer

DeltaError=DeltaY

Dim x as Integer

x=X1 'Start at X1,Y1

Dim y as Integer

y=Y1

Dim XStep as Integer

Dim YStep as Integer

If X1<X2 Then

XStep=1

Else

XStep=-1

end if

If Y1<Y2 Then

YStep=1

Else

YStep=-1 'Direction

end if

Dim iter as Integer

iter=1

While x<>X2

x=x+XStep 'Move in X

IError=IError+DeltaError 'Add to counter

If (IError SHL 1)>DeltaX then 'Would it overflow?

y=y+YStep 'Move in Y

IError=IError-DeltaX 'Overflow/wrap the counter

End if

If steep then

bresx(iter)=y

bresy(iter)=x

Else

bresx(iter)=x

bresy(iter)=y

End If

iter=iter+1

If iter>dist then

'Print at 3,0;PAPER 1;INK 0;"bresenham over distance "+STR(dist)

Return dist

End If

Wend

Return dist

End Function

create 2 arrays to hold the x and y coordinates of the line

Code:

`Dim bresx(350) as Integer`

Dim bresy(350) as Integer

The size of the arrays should be the distance of the longest line you intend to create

a line going across the screen of the speccy is about 322 in length

You have 2 characters on a playing field that has x and y coordinates

you call the line function:

one character is at 1,1

the other is at 100,95

Code:

`Dim dist as Integer`

dist=line(1,1,100,95)

then finally you go through the co-ordinates on the line to check if anything is in the way, like walls or other characters

Code:

`Dim can_see:UByte`

can_see=1

for x=0 to dist

if wall_in_way(bresx(x),bresy(x)) then

can_see=0

end if

next

if can_see =1 then

'yes they can see each other

end if

if can_see =0 then

'no they can not see each other

end if