/*****************************************************************************
 * $LastChangedDate: 2011-03-17 19:59:52 -0400 (Thu, 17 Mar 2011) $
 * @file
 * @author  Jim E. Brooks  http://www.palomino3d.org
 * @brief   Interface of C functions for Lua scripts.
 * @remarks @remarks module.txt documents how to export new C++ classes to Lua.
 *
 * ----------------------------
 * -- THIS IS AUTO-GENERATED --
 * ----------------------------
 *
 *//*
 * LEGAL:   COPYRIGHT (C) 2008 JIM E. BROOKS
 *          THIS SOURCE CODE IS RELEASED UNDER THE TERMS
 *          OF THE GNU GENERAL PUBLIC LICENSE VERSION 2 (GPL 2).
 *****************************************************************************/

#include "base/module.hh"
using namespace base;
#include "object/module.hh"
#include "object/object.hh"
using namespace object;
#include "world/module.hh"
using namespace world;
#include "lua_bind/module.hh"
#include "lua_bind/bind.hh"

namespace lua {


namespace bind {

#define BIND_CLASS_NAME      "World"
#define BIND_METATABLE_NAME  LUABIND_METATABLE_STRING(BIND_CLASS_NAME)

//==============================================================================
// C functions called by Lua that access C++ objects.
//==============================================================================

/*****************************************************************************
 * bool World:AltitudeInMeters(worldVertex)
 *****************************************************************************/
INTERN int 
World_AltitudeInMeters( lua_State* L ) 
{ 
   try { 

        const WorldVertex pos = POP_VECTOR3<WorldVertex>( L );
        const SphereVertex sv = world::conv::WorldVertex2SphereVertex( pos );
        lua_pushnumber( L, sv.mAlt.FP() );
        return 1;  // stack items to return
    
   } CATCH_LUA_EXCEPTION 
   return 0; 
} 

/*****************************************************************************
 * bool World:IfLowAltitude(pos)
 *****************************************************************************/
INTERN int 
World_IfLowAltitude( lua_State* L ) 
{ 
   try { 

        const WorldVertex pos = POP_VECTOR3<WorldVertex>( L );
        lua_pushboolean( L, GET_WORLD().IfLowAltitude( pos ) );
        return 1;  // stack items to return
    
   } CATCH_LUA_EXCEPTION 
   return 0; 
} 

/*****************************************************************************
 * fp World:Make() -- load terrain model
 *****************************************************************************/
INTERN int 
World_Make( lua_State* L ) 
{ 
   try { 

        GET_WORLD().Make();
    
   } CATCH_LUA_EXCEPTION 
   return 0; 
} 

/*****************************************************************************
 * fp World:Meters2Sim( Meter )
 *****************************************************************************/
INTERN int 
World_Meters2Sim( lua_State* L ) 
{ 
   try { 

        const Meter meter( POP_FP( L ) );
        const fp sim = world::conv::Meters2Sim( meter );
        lua_pushnumber( L, sim );
        return 1;  // stack items to return
    
   } CATCH_LUA_EXCEPTION 
   return 0; 
} 

/*****************************************************************************
 * World:RotateTangential( Object, lat,lon,alt|Vec3 )
 * Rotate an Object to be level at a point on spherical world.
 *****************************************************************************/
INTERN int 
World_RotateTangential( lua_State* L ) 
{ 
   try { 

        const SphereVertex sv = POP_SPHERE_VERTEX( L );
        shptr<Object> object = POP_USERDATA_AS_SHPTR<Object>( L );
        CHECK_TYPESIG(object,TYPESIG_OBJECT);
        GET_WORLD().RotateTangential( *object, sv );
    
   } CATCH_LUA_EXCEPTION 
   return 0; 
} 

/*****************************************************************************
 * Meter World:Sim2Meters( simDistance )
 *****************************************************************************/
INTERN int 
World_Sim2Meters( lua_State* L ) 
{ 
   try { 

        const Meter meters = world::conv::Sim2Meters( POP_FP( L ) );
        lua_pushnumber( L, meters.FP() );
        return 1;  // stack items to return
    
   } CATCH_LUA_EXCEPTION 
   return 0; 
} 

/*****************************************************************************
 * Vec3 World:SphereVertex2WorldVertex( lat,lon,alt|Vec3 )
 *****************************************************************************/
INTERN int 
World_SphereVertex2WorldVertex( lua_State* L ) 
{ 
   try { 

        const SphereVertex sv = POP_SPHERE_VERTEX( L );
        const WorldVertex wv = world::conv::SphereVertex2WorldVertex( sv );
        PUSH_VECTOR3<WorldVertex>( L, wv );
        return 1;  // stack items to return
    
   } CATCH_LUA_EXCEPTION 
   return 0; 
} 

/*****************************************************************************
 * {lat,lon,alt} World:WorldVertex2SphereVertex( Vec3 )
 *****************************************************************************/
INTERN int 
World_WorldVertex2SphereVertex( lua_State* L ) 
{ 
   try { 

        const WorldVertex  wv = POP_VECTOR3<WorldVertex>( L );
        const SphereVertex sv = world::conv::WorldVertex2SphereVertex( wv );
        PUSH_SPHERE_VERTEX( L, sv );
        return 1;  // stack items to return
    
   } CATCH_LUA_EXCEPTION 
   return 0; 
} 

//==============================================================================
// Register binding with Lua. 
//==============================================================================

#if ! DOXYGEN 

INTERN const struct luaL_reg sMethodTable[] =
{
  // New(),__gc() omitted as Lua cannot instantiate this C++ class.
  { "AltitudeInMeters", World_AltitudeInMeters },
  { "IfLowAltitude", World_IfLowAltitude },
  { "Make", World_Make },
  { "Meters2Sim", World_Meters2Sim },
  { "RotateTangential", World_RotateTangential },
  { "Sim2Meters", World_Sim2Meters },
  { "SphereVertex2WorldVertex", World_SphereVertex2WorldVertex },
  { "WorldVertex2SphereVertex", World_WorldVertex2SphereVertex },
  { NULL, NULL }
};

void
RegisterWorld( lua_State* L )
{
    REGISTER_BINDING( L,
                      BIND_METATABLE_NAME,
                      BIND_CLASS_NAME,
                      sMethodTable );
}

#endif // ! DOXYGEN 

} // namespace bind 

} // namespace lua 
