CoopPuzzle/scripts/Common.gd

69 lines
1.6 KiB
GDScript

extends Node
func num2alpha(num: int, uppercase:=false):
var c = ord('A' if uppercase else 'a')
if num >= 26:
return char(c+(num/26)-1) + char(c+(num%26))
else:
return char(c+num)
class PoolArray2D:
var _array
var _rows := 1
var _cols := 1
# const XORMASK := (1<<8)-1
func _init(rows:=1, cols:=1, init=null):
assert(rows>0)
assert(cols>0)
_rows = rows
_cols = cols
make_array()
if init != null:
for row in rows:
for col in cols:
set_cell(row, col, init)
func make_array():
_array = PoolByteArray()
_array.resize(_rows * _cols)
func set_cell(row: int, col: int, value: int):
assert(row < _rows)
assert(col < _cols)
_array.set(row + col*_rows, value)
func get_cell(row: int, col: int) -> int:
assert(row < _rows)
assert(col < _cols)
return _array[row + col*_rows]
func get_flag(row: int, col: int, value: int) -> bool:
return bool(_array[row + col*_rows] & value)
func set_flag(row: int, col: int, value: int):
_array.set(row + col*_rows, _array[row + col*_rows]|value)
class ByteArray2D extends PoolArray2D:
const XORMASK := (1<<8)-1
func _init(rows:=1, cols:=1, init=null).(rows, cols, init):
pass
func clear_flag(row: int, col: int, value: int):
_array.set(row + col*_rows, _array[row + col*_rows]&(value^XORMASK))
class IntArray2D extends PoolArray2D:
const XORMASK := (1<<32)-1
func _init(rows:=1, cols:=1, init=null).(rows, cols, init):
pass
func make_array():
_array = PoolIntArray()
_array.resize(_rows * _cols)
func clear_flag(row: int, col: int, value: int):
_array.set(row + col*_rows, _array[row + col*_rows]&(value^XORMASK))