CoopPuzzle/scripts/Common.gd

127 lines
3.1 KiB
GDScript

extends Node
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))
#class ByteArray2D:
# var _array: PoolByteArray
# var _rows := 1
# var _cols := 1
# const XORMASK := (1<<8)-1
# func _init(rows:=1, cols:=1):
# assert(rows>0)
# assert(cols>0)
# _rows = rows
# _cols = cols
# _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)
#
# func clear_flag(row: int, col: int, value: int):
# _array.set(row + col*_rows, _array[row + col*_rows]&(value^XORMASK))
#class IntArray2D:
# var _array: PoolIntArray
# var _rows := 1
# var _cols := 1
# const XORMASK := (1<<32)-1
# func _init(rows:=1, cols:=1):
# assert(rows>0)
# assert(cols>0)
# _rows = rows
# _cols = cols
# _array = PoolIntArray()
# _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)
#
# func clear_flag(row: int, col: int, value: int):
# _array.set(row + col*_rows, _array[row + col*_rows]&(value^XORMASK))