
extern

  typedef int int32

  function minmax(int v,min,max) as int32
  =========================================
    if v<min
      return min
    elseif v>max
      return max
    else
      return v
    endif
  end function

  int seed=0x12345678

  function rnd(int z1, z2) as int
  ================================
  mov    eax,z2
  sub    eax,z1
  inc    eax
  imul   edx,Seed,0x8088405
  inc    edx
  mov    Seed,edx 'store new seed
  mul    edx 'multiply eax by edx
  return edx+z1
  end Function 


 'function Do_Fire(int32 me_fire_height, me_fire_width, ubound_me_fire, *me_fire) link #p3
  =========================================================================================
  function Do_Fire(byval me_fire_height as int32, byval me_fire_width as int32, byval ubound_me_fire as int32, me_fire as int32) link #p3
    int32 x, y
    int32 src
    int32 rand
    int32 target_pixel
    '
    for x = 1 to me_fire_width
      for y = 1 to me_fire_height-1
        src=(y-1) * me_fire_width + x
        'SpreadFire:
        rand = rnd(0, 3) and 3
        target_pixel = minMax(src + rand - 2 + me_FIRE_WIDTH, 1, ubound_me_fire)
        me_fire(target_pixel) = minmax(me_fire(src) - (rand and 1), 1, 37)
      Next
    Next
  end function
      

  'function render_fire(int32 ubound_me_fire, *me_fire, *me_palette, *trueColors) as int, link #p2 
   =================================================================================================
   function render_fire(byval ubound_me_fire as int32, me_fire as int32, me_palette as int32 , trueColors as int32) as int32, link #p2
    int32 fire_index, true_color_index
    int32 fire_color_index
    ' We need to convert palette colors to real colors
    for fire_index = ubound_me_fire to 1 step -1
        fire_color_index = me_fire(fire_index)
    
        inc true_color_index
        trueColors(true_color_index) = me_palette(fire_color_index)
    next
  end function


  Sub finish() link #p1
  =====================
    terminate
  End Sub

