Getting a random array position in batari basic (atari 2600)

70 views Asked by At

I need to random a new specific position for the player each N cycles of game loop. So my idea was use a ROM array (data) and a rand function using (&) operator for fast processing following the random terrain documentation : https://www.randomterrain.com/atari-2600-memories-batari-basic-commands.html#rand

However it doesn't work. It is like the response is the same array position always and it returns strange positions randomly haha.. It is more random than I want lol.

I'm using Standard Kernel, with these options below and basic logic for this example :

  ;***************************************************************
  ;
  ;  Initializes the batari Basic compiler directives.
  set kernel_options pfcolors player1colors no_blank_lines
  set tv ntsc
  set romsize 32k
  set optimization speed
  set smartbranching on
  set optimization noinlinedata
  set optimization inlinerand

  ;***************************************************************
  ;
  ;  Debug. The rem below is removed when I want to check to see
  ;  if I'm going over cycle count during tests.
  ;
  ; set debug


  ;*************************************************************** 
  ;
  ;  Variable aliases go here (DIMs).

  ;```````````````````````````````````````````````````````````````
  ;  _Animation_Frame.
  ;
  dim _Player_Animation_Frame = a
  dim _PlayerPosition = b
   
  dim p1posx=player0x.d
  dim p1posy=player0y.e

__GameStart
  COLUPF = $0E
  _Player_Animation_Frame = 0
  drawscreen



  ;***************************************************************
  ;
  ;  Road Section Positions X for Spawn Enemies
  ;
   data _Road_Pos
   49,77,105
end

__MAIN_LOOP_STAGE1
  COLUPF = $0E
  
  ; Change position each 15 cycles 

  if _Player_Animation_Frame = 0 then temp1 = (rand&3) 
  if _Player_Animation_Frame = 15 then temp1 = (rand&3)
  if _Player_Animation_Frame = 30 then temp1 = (rand&3)

  ; GETTING RANDOM ARRAY POSITION


  if _Player_Animation_Frame < 10 then gosub __Player_Sprite : COLUP0 = $84 : p1posx = _Road_Pos[temp1] : p1posy = 50
  if _Player_Animation_Frame > 9  && _Player_Animation_Frame < 20 then gosub __Player_Sprite : COLUP0 = $34 : p1posx = _Road_Pos[temp1] : p1posy = 50
  if _Player_Animation_Frame > 19 then gosub __Player_Sprite : COLUP0 = $0E : p1posx = _Road_Pos[temp1] : p1posy = 50

  _Player_Animation_Frame = _Player_Animation_Frame + 1
  if _Player_Animation_Frame = 30 then _Player_Animation_Frame = 0 

  drawscreen
  goto __MAIN_LOOP_STAGE1


__Player_Sprite
 player0:
 %01111110
 %01111110
 %11111111
 %11111111
 %11111111
 %01111110
 %01111110
end
 return thisbank
1

There are 1 answers

0
Eduardo Fabricio On

It seems happen because i use the optimization kernel option

set optimization noinlinedata

I'm no sure, but i solve the issue using the following strategy, creating a subroutine for random position and put the value of array position in a variable, so after that i set the player position using the variable instead get array position inline.

The following code works fine :)

  ;***************************************************************
  ;
  ;  Initializes the batari Basic compiler directives.
  set kernel_options pfcolors player1colors no_blank_lines
  set tv ntsc
  set romsize 32k
  set optimization speed
  set smartbranching on
  set optimization noinlinedata
  set optimization inlinerand

  ;***************************************************************
  ;
  ;  Debug. The rem below is removed when I want to check to see
  ;  if I'm going over cycle count during tests.
  ;
  ; set debug


  ;*************************************************************** 
  ;
  ;  Variable aliases go here (DIMs).

  ;```````````````````````````````````````````````````````````````
  ;  _Animation_Frame.
  ;
  dim _Player_Animation_Frame = a
  dim _PlayerPosition = b
   
  dim p1posx=player0x.d
  dim p1posy=player0y.e

__GameStart
  COLUPF = $0E
  _Player_Animation_Frame = 0
  drawscreen

__MAIN_LOOP_STAGE1
  COLUPF = $0E
  
  ; Change position each 15 cycles 

  if _Player_Animation_Frame = 0 then gosub __RandomRoadPos
  if _Player_Animation_Frame = 15 then gosub __RandomRoadPos
  if _Player_Animation_Frame = 30 then gosub __RandomRoadPos

  ; GETTING RANDOM ARRAY POSITION


  if _Player_Animation_Frame < 10 then gosub __Player_Sprite : COLUP0 = $84 : p1posx = _PlayerPosition : p1posy = 50
  if _Player_Animation_Frame > 9  && _Player_Animation_Frame < 20 then gosub __Player_Sprite : COLUP0 = $34 : p1posx = _PlayerPosition : p1posy = 50
  if _Player_Animation_Frame > 19 then gosub __Player_Sprite : COLUP0 = $0E : p1posx = _PlayerPosition : p1posy = 50

  _Player_Animation_Frame = _Player_Animation_Frame + 1
  if _Player_Animation_Frame = 30 then _Player_Animation_Frame = 0 

  drawscreen
  goto __MAIN_LOOP_STAGE1


__Player_Sprite
 player0:
 %01111110
 %01111110
 %11111111
 %11111111
 %11111111
 %01111110
 %01111110
end
 return thisbank



__RandomRoadPos
  temp1 = (rand&3)
  if temp1 > 2 then temp1 = 2 ; this fix value 3 return from (rand&3) that not exists in array :)
  _PlayerPosition = _Road_Pos[temp1] 
 return thisbank

  ;***************************************************************
  ;
  ;  Road Section Positions X for Spawn Enemies
  ;
   data _Road_Pos
   49,77,105
end