/* * * * * * * * * * * * * * * * 
 * 	 ______     __  __     ______     __  __     ______     ______     __  __    
 *	/\  __ \   /\ \/\ \   /\  == \   /\ \_\ \   /\  __ \   /\  ___\   /\ \/ /    
 *	\ \ \/\ \  \ \ \_\ \  \ \  __<   \ \  __ \  \ \  __ \  \ \ \____  \ \  _"-.  
 *	 \ \_____\  \ \_____\  \ \_____\  \ \_\ \_\  \ \_\ \_\  \ \_____\  \ \_\ \_\ 
 *	  \/_____/   \/_____/   \/_____/   \/_/\/_/   \/_/\/_/   \/_____/   \/_/\/_/ 
 *
 *  - Version : 3L
 *  - By Oubliette
 * * * * * * * * * * * * * * * */

#include "main.h"

#include "oubhack_lua_l.h"
#include "oubhack_iluaobject.h"
#include "oubhack.h"

ILuaObject* objVector = NULL;
ILuaObject* objAngle  = NULL;
ILuaObject* objColor  = NULL;

ILuaLibrary::ILuaLibrary(){

	objVector = NULL;
	objAngle  = NULL;
	objColor  = NULL;

}

void ILuaLibrary::Close(){

	if ( objVector != NULL ) objVector->UnReference();
	if ( objAngle != NULL ) objAngle->UnReference();
	if ( objColor != NULL ) objColor->UnReference();

}

ILuaObject* ILuaLibrary::NewVector( ILuaInterface* lua, Vector& vec ){

	if ( objVector == NULL ) objVector = lua->GetGlobal("Vector");

	objVector->Push();
		
		lua->PushNumber( vec.x );
		lua->PushNumber( vec.y );
		lua->PushNumber( vec.z );

	lua->Call( 3, 1 );

	return lua->GetReturn( 0 );

}

ILuaObject* ILuaLibrary::NewAngle( ILuaInterface* lua, QAngle& ang ){

	if ( objAngle == NULL ) objAngle = lua->GetGlobal("Angle");

	objAngle->Push();
		
		lua->PushNumber( ang.x );
		lua->PushNumber( ang.y );
		lua->PushNumber( ang.z );

	lua->Call( 3, 1 );

	return lua->GetReturn( 0 );

}

ILuaObject* ILuaLibrary::NewColor( ILuaInterface* lua, Color& col ){

	if ( objColor == NULL ) objColor = lua->GetGlobal("Color");
	
	objColor->Push();
		
		lua->PushNumber( col.r() );
		lua->PushNumber( col.g() );
		lua->PushNumber( col.b() );
		lua->PushNumber( col.a() );

	lua->Call( 4, 1 );

	return lua->GetReturn( 0 );

}

void ILuaLibrary::PushVector( ILuaInterface* lua, Vector* vec ){

	ILuaObject* lo_Obj = this->NewVector( lua, *vec ); 
	lo_Obj->Push();

	lo_Obj->UnReference();

}

void ILuaLibrary::PushAngle( ILuaInterface* lua, QAngle* ang ){

	ILuaObject* lo_Obj = this->NewAngle( lua, *ang ); 
	lo_Obj->Push();

	lo_Obj->UnReference();

}


void ILuaLibrary::PushColor( ILuaInterface* lua, Color* col ){

	ILuaObject* lo_Obj = this->NewColor( lua, *col ); 
	lo_Obj->Push();

	lo_Obj->UnReference();

}

Vector& ILuaLibrary::GetVector( ILuaInterface* lua, int iStackPos ){

	return *reinterpret_cast< Vector* >( lua->GetUserdata( iStackPos ) );

}

QAngle& ILuaLibrary::GetAngle( ILuaInterface* lua, int iStackPos ){

	return *reinterpret_cast< QAngle* >( lua->GetUserdata( iStackPos ) );

}


Color& ILuaLibrary::GetColor( ILuaInterface* lua, int iStackPos ){

	return *reinterpret_cast< Color* >( lua->GetUserdata( iStackPos ) );

}

CBaseEntity& ILuaLibrary::GetEntity( ILuaInterface* lua, int iStackPos ){

	return *reinterpret_cast< CBaseEntity* >( lua->GetUserdata( iStackPos ) );

}

CBasePlayer& ILuaLibrary::GetPlayer( ILuaInterface* lua, int iStackPos ){

	return *reinterpret_cast< CBasePlayer* >( lua->GetUserdata( iStackPos ) );

}