math.randomseed( os.time() )
math.random();  math.random();  math.random();	--first few numbers aren't really random :O

char = string.char;
byte = string.byte;



function ArgD ( inTab, inKey, inDef )
	if inTab[ inKey ] == nil then
		return inDef
	else
		return inTab[ inKey ]
	end
end


function deepcopy(orig)
    local orig_type = type(orig)
    local copy
    if orig_type == 'table' then
        copy = {}
        for orig_key, orig_value in next, orig, nil do
            copy[deepcopy(orig_key)] = deepcopy(orig_value)
        end
        setmetatable(copy, deepcopy(getmetatable(orig)))
    else -- number, string, boolean, etc
        copy = orig
    end
    return copy
end

--our current method to remove items from a table, as the original loops aren't very nice to do this in.
--simply declare dels, and when you want to remove an item, use the insert function commented below after the loop.
function DoDels ( tabl, indices )
	local n = #indices
	for i = n, 1, -1 do
		table.remove( tabl, indices[ i ] )
	end
	while #indices > 0 do
		table.remove( indices, 1 )
	end
end
--dels = {}	
--insert( dels, i )
--DoDels( tab, dels )

function choice( inAry )
	n = #inAry
	ri = RandInt( 1, n )
	return inAry[ ri ]
end

function RandSeed( inSeed )
	math.randomseed( inSeed )
end

function RandInt( low, high )  --includes high, so RandInt( 0, 2 ) returns 0, 1, 2.
	retval = low + math.floor( math.random() * ( high - low + .999 ) )
	return retval
end

function Rand( low, high )
	retval = low + ( math.random() * ( high - low ) )
	return retval
end


function int( a )
	return math.floor( a )
end

function chari( a )
	return string.byte( a )
end
function schari( a )
	local v = string.byte( a )
	local rv = 255
	
	if ( v - string.byte( 'a' ) >= 0 ) then
		rv = v - string.byte( 'a' )
	end
	if ( v >= 48 and v <= 48 + 9 ) then
		rv = v - 48 + 26
	end
	--print( string.byte( '0' ) )
	return rv
end


require( "lib/pickle" )

function WriteSaveData( inGame, inFile, inData )
	pick = pickle( inData )
	WriteSaveString( inGame, inFile, pick )
end


function ReadSaveData( inGame, inFile )
	txt = ReadSaveString( inGame, inFile )
	data = unpickle( txt )
	return data
	
end


bx_KeyFwd = 0
bx_KeyBack = 1
bx_KeyLeft = 2
bx_KeyRight = 3
bx_KeyJump = 4
bx_KeyDuck = 5
bx_KeyFire0 = 6
bx_KeyFire1 = 7
bx_KeyFire2 = 8
bx_KeyFire3 = 9
bx_KeyFire4 = 10
bx_KeyScrollUp = 11
bx_KeyScrollDown = 12
bx_KeyBind0 = 13
bx_KeyBind1 = 14
bx_KeyBind2 = 15
bx_KeyBind3 = 16
bx_KeyBind4 = 17
bx_KeyBind5 = 18
bx_KeyBind6 = 19
bx_KeyTab = bx_KeyBind6
bx_KeyBind7 = 20
bx_KeyBind8 = 21
bx_KeyBind9 = 22
bx_KeyBind10 = 23
bx_KeyBind11 = 24
bx_KeyBind12 = 25
bx_KeyBind13 = 26
bx_KeyBind14 = 27
bx_KeyBind15 = 28
bx_KeyBind16 = 29
bx_KeyBind17 = 30
bx_KeyBind18 = 31
bx_KeyBind19 = 32
bx_KeyBind20 = 33
bx_KeyBind21 = 34

bx_KeyPrintScreen = 35


bx_SPACEMAPPER2D = 1


--bind default keys ( the ,1 at the end of some lines means this is a secondary keybind, won't overwrite prev binds )
BindKey( bx_KeyFwd, 73 ) --up
BindKey( bx_KeyFwd, schari( 'w' ), 1 ) --w
BindKey( bx_KeyBack, 74 ) --down
BindKey( bx_KeyBack, schari( 's' ), 1 ) --s
BindKey( bx_KeyLeft, 71 ) --left
BindKey( bx_KeyLeft, schari( 'a' ), 1 ) --a
BindKey( bx_KeyRight, 72 ) --right
BindKey( bx_KeyRight, schari( 'd' ), 1 ) --d

BindKey( bx_KeyJump, 57 ) --space
BindKey( bx_KeyDuck, schari( 'c' ) )

--mouse keys, L, R, Mid, alt1, alt2, scrollup, scrolldown
BindKey( bx_KeyFire0, 101 )
BindKey( bx_KeyFire1, 102 )
BindKey( bx_KeyFire2, 103 )
BindKey( bx_KeyFire3, 104 )
BindKey( bx_KeyFire4, 105 )
BindKey( bx_KeyScrollUp, 106 )
BindKey( bx_KeyScrollDown, 107 )

--standard keys for 'weapon select'

BindKey( bx_KeyBind0, schari( '1' ) )
BindKey( bx_KeyBind1, schari( '2' ) )
BindKey( bx_KeyBind2, schari( '3' ) )
BindKey( bx_KeyBind3, schari( '4' ) )
BindKey( bx_KeyBind4, schari( '5' ) )
BindKey( bx_KeyBind5, schari( '6' ) )

BindKey( bx_KeyBind6, 60 )	--tab

--general use keys
BindKey( bx_KeyBind7, schari( 'q' ) )
BindKey( bx_KeyBind8, schari( 'e' ) )
BindKey( bx_KeyBind9, schari( 'r' ) )
BindKey( bx_KeyBind10, schari( 'f' ) )
BindKey( bx_KeyBind11, schari( 'z' ) )
BindKey( bx_KeyBind12, schari( 'x' ) )
BindKey( bx_KeyBind13, schari( 'v' ) )
BindKey( bx_KeyBind14, schari( '7' ) )
BindKey( bx_KeyBind15, schari( 't' ) )
BindKey( bx_KeyBind16, schari( 'g' ) )
BindKey( bx_KeyBind17, schari( 'b' ) )
BindKey( bx_KeyBind18, schari( 'n' ) )
BindKey( bx_KeyBind19, schari( 'm' ) )

BindKey( bx_KeyBind20, 38 ) --lshift
BindKey( bx_KeyBind21, 37 ) --lcontrol

BindKey( bx_KeyPrintScreen, schari( 'p' ) )

--set this to your desired folder, brozies.
SetScreenshotDir( "C:\\Users\\m2c\\Desktop" )
--VSync only works in fullscreen (at least on my machine)
SetVSync( 1 )


