# UTILS.library

string UTILS_text
string UTILS_dimensions

integer UTILS_dimension_count
integer UTILS_temp
integer UTILS_count
integer UTILS_checksum
integer UTILS_index
integer UTILS_total_needed
integer UTILS_current_offset
integer UTILS_array_size
integer UTILS_argument

{+print_right
set UTILS_temp = status_width
length INDEX string_arg[0]
set UTILS_temp - INDEX
set UTILS_temp - 1
cursor UTILS_temp arg[1]
write string_arg[0]
}

{+print_centred
set UTILS_temp = status_width
length INDEX string_arg[0]
set UTILS_temp - INDEX
set UTILS_temp / 2
cursor UTILS_temp arg[1]
write string_arg[0]
}

{+reverse_window
style reverse
padstring UTILS_text " " status_width
set UTILS_temp = 0
while UTILS_temp < status_height
  cursor 0 UTILS_temp
  write UTILS_text
  set UTILS_temp + 1
endwhile
style normal
}

string LCNumber zero one two three four five six seven eight nine ten
string UCNumber Zero One Two Three Four Five Six Seven Eight Nine Ten

{+number_upper
if arg[0] < 0 : arg[0] > 10
   write arg[0]
else
   write UCNumber[arg[0]]
endif
}

{+number_lower
if arg[0] < 0 : arg[0] > 10
   write arg[0]
else
   write LCNumber[arg[0]]
endif
}

{+random_number
# arg[0] = lower bound
# arg[1] = upper bound
if arg[1] > arg[0]
   set max_rand = arg[1] - arg[0]
   set UTILS_temp = arg[0] + random
   return UTILS_temp
else
   write "ERROR: +random_number must be passed a lower bound and an upper "
   write "bound for the number required.^"
   return false
endif
}

{+init_array
# string_arg[0] = name of array

setstring UTILS_text  "@" string_arg[0]
set UTILS_count = UTILS_text # Get the value of @array_name.

setstring UTILS_dimensions "@" string_arg[0] "_dimensions"
set UTILS_dimension_count = UTILS_dimensions
set UTILS_dimension_count - 1 # Move to the index of the final dimension

set UTILS_total_needed = 1

# Calculate the total space requested
while UTILS_dimension_count > -1
   # Find the integer value of each of the dimentions
   setstring UTILS_text string_arg[0] "_dimensions[" UTILS_dimension_count "]"
   set UTILS_total_needed * UTILS_text
   set UTILS_dimension_count - 1
endwhile

if UTILS_total_needed > UTILS_count
   write "ERROR: size requested (" UTILS_total_needed) 
   write " is larger than the size available (" UTILS_count ").^"
   return false
endif

# Calculate the one-dimensional offsets
set UTILS_dimension_count = UTILS_dimensions
set UTILS_dimension_count - 1
set UTILS_current_offset = 1
while UTILS_dimension_count > -1
   # Set the offset for the current dimension
   setstring UTILS_text string_arg[0] "_offsets[" UTILS_dimension_count "]"
   set UTILS_text = UTILS_current_offset

   # Find the integer value of each of the dimentions
   setstring UTILS_text string_arg[0] "_dimensions[" UTILS_dimension_count "]"
   set UTILS_temp = UTILS_text
   set UTILS_current_offset * UTILS_temp

   set UTILS_dimension_count - 1
endwhile
}


{+get_array : +set_array
# string_arg[0] = name of array
# arg[1] -->    = one or more array co-ordinate(s)
# return code   = value of array element

setstring UTILS_dimensions "@" string_arg[0] "_dimensions"
set UTILS_dimension_count = UTILS_dimensions

ifstring function_name = "+get_array"
   set UTILS_checksum = @arg - 1 # The number of dimensions supplied as arguments
else
   set UTILS_checksum = @arg - 2 # The number of dimensions supplied as arguments
endif

if UTILS_dimension_count = UTILS_checksum
   set UTILS_dimension_count - 1 # Move to the index of the final dimension
   set UTILS_index = 0 # Reset the one-dimensional index

   # Calculate the one dimensional index
   while UTILS_dimension_count > -1
      setstring UTILS_text string_arg[0] "_offsets[" UTILS_dimension_count "]"

      # Get the index of the argument
      ifstring function_name = "+get_array"
         set UTILS_argument = UTILS_dimension_count + 1
      else
         set UTILS_argument = UTILS_dimension_count + 2
      endif

      # Multiple the supplied index by the offset value
      set UTILS_temp = UTILS_text * arg[UTILS_argument]

      # Add this offset to the one-dimension index
      set UTILS_index + UTILS_temp

      set UTILS_dimension_count - 1
   endwhile 

   setstring UTILS_text string_arg[0] "[" UTILS_index "]"
   ifstring function_name = "+get_array"
      # Return the value at the calculated one-dimensional index
      return UTILS_text
   else
      set UTILS_text = arg[1]
      return true
   endif
else
   write "Number of dimensions supplied does not match array.^"
   return -1
endif
}
