'---- STARFIELD ----


'-- RESET A SINGLE STAR --

SUB RESET_STAR(T)

 X(T)=MAXX/2
 Y(T)=MAXY/2
 DO WHILE INT(XX(T))=0 AND INT(YY(T))=0
  XX(T)=RND(3)*20-10+RND(10)*1/10
  YY(T)=RND(4)*20-10+RND(11)*1/10
 LOOP
 SP(T)=RND(5)*25+1

END SUB


'-- DRAW AN STARFIELD WITH "N" STARS --

SUB STARFIELD(N)

 LOCAL T,C

 FOR T=1 TO N
  X(T)=X(T)+XX(T): Y(T)=Y(T)+YY(T)
  IF X(T)<0 OR X(T)>=MAXX OR Y(T)<0 OR Y(T)>=MAXY THEN RESET_STAR(T)
  C=ABS(X(T)-MAXX/2)+ABS(Y(T)-MAXY/2)+SP(T): IF C>255 THEN C=255
  PIXEL X(T),Y(T),RGB(C,C,C)
 NEXT T

END SUB


'-- RESET A SINGLE REVERSED STAR --

SUB RESET_REVERSED_STAR(T)

 LOCAL STP, X1,Y1,X2,Y2,TT

 'STARS FROM SCREEN CENTER TO BORDER
 IF X(T)>MAXX/2-12 AND X(T)<MAXX/2+12 AND Y(T)>MAXY/2-12 AND Y(T)<MAXY/2+12 THEN
  IF RND(1)*10>5 THEN
   IF RND(5)*10>5 THEN X(T)=0 ELSE X(T)=MAXX
   Y(T)=RND(10)*MAXY
  ELSE
   IF RND(5)*10<5 THEN Y(T)=0 ELSE Y(T)=MAXY
   X(T)=RND(10)*MAXX
  ENDIF
 ENDIF

 'DDA - DIGITAL DIFFERENTIAL ANALYSER ALGORITHM
 'USED FOR PLOT FROM BORDER TO CENTER SCREEN
 X1=X(T): Y1=Y(T): X2=MAXX/2: Y2=MAXY/2
 STP = ABS(X2-X1)
 IF ABS(Y2-Y1) > STP) THEN STP = ABS(Y2-Y1)
 TT=RND(50)*7+3
 XX(T) = ((X2-X1)/STP)*TT
 YY(T) = ((Y2-Y1)/STP)*TT

 SP(T)=RND(5)*25+1

END SUB


'-- DRAW AN REVERSED STARFIELD WITH "N" STARS --

SUB REVERSED_STARFIELD(N)

 LOCAL T,C

 FOR T=1 TO N
  X(T)=X(T)+XX(T): Y(T)=Y(T)+YY(T)
  'RESET STARS WHEN OUT OF SCREEN LIMITS
  IF X(T)>MAXX/2-12 AND X(T)<MAXX/2+12 AND Y(T)>MAXY/2-12 AND Y(T)<MAXY/2+12 THEN
   RESET_REVERSED_STAR(T)
  ENDIF
  C=ABS(X(T)-MAXX/2)+ABS(Y(T)-MAXY/2)+SP(T): IF C>255 THEN C=255
  PIXEL X(T),Y(T),RGB(C,C,C)
 NEXT T

END SUB


'-- RESET "N" STARS --

SUB INIT_STARS(N)

 LOCAL T

 FOR T=1 TO N
  RESET_STAR(T)
 NEXT T
 PAGE WRITE 1

END SUB


'-- RESET "N" REVERSED STARS --

SUB INIT_REVERSED_STARS(N)

 LOCAL T

 FOR T=1 TO N
  RESET_REVERSED_STAR(T)
 NEXT T
 PAGE WRITE 1

END SUB


'-- RESET A SINGLE STAR - SIDE --

SUB RESET_STAR_SIDE(T,S)

 IF S>0 THEN X(T)=0 ELSE X(T)=MAXX
 Y(T)=RND(1)*MAXY
 DO WHILE XX(T)>=-2 AND XX(T)<=2
  XX(T)=(ABS(RND(1)*500)/100)*S
  YY(T)=0
 LOOP
 SP(T)=50+XX(T)*20

END SUB


'-- DRAW AN STARFIELD WITH "N" STARS --

SUB STARFIELD_SIDE(N,S)

 LOCAL T,C

 FOR T=1 TO N
  X(T)=X(T)+XX(T)
  IF X(T)<0 OR X(T)>=MAXX THEN RESET_STAR_SIDE(T,S)
  C=SP(T)
  IF PIXEL(X(T),Y(T)) = 0 THEN PIXEL X(T),Y(T),RGB(C,C,C)
 NEXT T

END SUB



