/*****************************************************************************
 * $Id: game_pony_python_ifc.cc,v 1.13 2006/08/08 03:44:46 devel Exp $
 * @file
 * @author	Jim E. Brooks  http://www.jimbrooks.org
 * @brief	Game Pony and World interfaces from C++ to Python.
 * @remarks Python extension functions have the form:
 *          PyExt(PyObject* self, PyObject* args)
 *          I write PyExt(nil,args) because in fact self will be NULL
 *          (except for the esoteric "Python extension types").
 *          A few of my extensions need self which my Python code passes as
 *          the first arg which appears on the C++ side as the first element
 *          in the tuple passed as "args" to PyExt(self,args).
 *//*
 * LEGAL:	COPYRIGHT (C) 2005 JIM E. BROOKS
 *          THIS SOURCE CODE IS RELEASED UNDER THE TERMS
 *          OF THE GNU GENERAL PUBLIC LICENSE (GPL).
 *****************************************************************************/

#ifndef NO_PYTHON

#include <Python.h>
#define GAME_PONY_PYTHON_IFC_CC 1
#include "game_pony_common.hh"
#include "game_base_craft.hh"
#include "game_base_combat.hh"
#include "game_pony_view.hh"
#include "game_pony_craft.hh"
#include "game_pony_python_ifc.hh"

//==============================================================================
// Some PYTHON* macros are multi-line macros, enclose them in braces!
//==============================================================================

////////////////////////////////////////////////////////////////////////////////
///////////////////////  Python interface (Game)  //////////////////////////////
////////////////////////////////////////////////////////////////////////////////

/*****************************************************************************
 * @return String of the file directory containing Palomino Python modules.
 * args[0] = none
 *****************************************************************************/
static PyObject*
PyGame_GetPythonDir( PyObject* nil )
{
	return PYTHON_STRING( gGame.GetPythonDir() );
}

/*****************************************************************************
 * @return String of the file directory containing Palomino 3D models.
 * args[0] = none
 *****************************************************************************/
static PyObject*
PyGame_GetModelDir( PyObject* nil )
{
	return PYTHON_STRING( gGame.GetModelDir() );
}

/*****************************************************************************
 * @return Pointer to player's C++ Craft object.
 * args[0] = player number (1 or 2)
 *****************************************************************************/
static PyObject*
PyGame_GetPlayer( PyObject* nil, PyObject* args )
{
	int playerNum;
    if ( PyArg_ParseTuple( args, "i", &playerNum ) )
	{
		if ( playerNum == 1 )
		{
			return PYTHON_PYOBJECT_FROM_CPTR( GET_PTR(gGame.mPlayer1Craft) );
		}
		else if ( playerNum == 2 )
		{
			return PYTHON_PYOBJECT_FROM_CPTR( GET_PTR(gGame.mPlayer2Craft) );
		}
		// Error, wrong player num, fall thru.
	}
	PYTHON_RETURN_BAD_OBJECT_ERROR
}

/*****************************************************************************
 * @return Append Craft to list.
 * args[0]     = (Craft*) craft
 *****************************************************************************/
static PyObject*
PyGame_CraftListAppend( PyObject* nil, PyObject* args )
{
	CptrArg		cptrCraft;
	if ( PyArg_ParseTuple( args, PYTHON_CPTR_FORMAT, &cptrCraft ) )
	{
		Craft_FROM_CPTR_OR_ERROR( craft, cptrCraft );
		gGame.mCraftDlist.Append( craft );
		PYTHON_RETURN_NONE
	}
Error: PYTHON_RETURN_BAD_OBJECT_ERROR
}

/*****************************************************************************
 * @return Pointer to a new DynaModelTarget.
 * args[0]     = (Quadrant*) quadrant
 * args[1,2,3] = (float)     x,y,z
 * args[4]     = (char*)     Palomino model name
 *****************************************************************************/
static PyObject*
PyGame_DynaModelTargetNew( PyObject* nil, PyObject* args )
{
	CptrArg		cptrQuadrant;
	float		x, y, z;
	char*		modelName;
	if ( PyArg_ParseTuple( args, PYTHON_CPTR_FORMAT "fffs", &cptrQuadrant, &x, &y, &z, &modelName ) )
	{
		Quadrant_FROM_CPTR_OR_ERROR( quadrant, cptrQuadrant );
		Position pos( x, y, z );
        vector<string> modelNameVec; modelNameVec.push_back( modelName );
		return PYTHON_PYOBJECT_FROM_CPTR( new DynaModelTarget( quadrant, pos, modelNameVec ) );
	}
Error: PYTHON_RETURN_BAD_OBJECT_ERROR
}

/*****************************************************************************
 * @return Pointer to a new CraftF14Tomcat.
 * args[0]     = (Quadrant*) quadrant
 * args[1,2,3] = (float)     x,y,z
 *****************************************************************************/
static PyObject*
PyGame_CraftF14TomcatNew( PyObject* nil, PyObject* args )
{
	CptrArg		cptrQuadrant;
	float		x, y, z;
	if ( PyArg_ParseTuple( args, PYTHON_CPTR_FORMAT "fff", &cptrQuadrant, &x, &y, &z ) )
	{
		Quadrant_FROM_CPTR_OR_ERROR( quadrant, cptrQuadrant );
		Position pos( x, y, z );
		extern vector<string> gModelNameF14Vec;
		return PYTHON_PYOBJECT_FROM_CPTR( new CraftF14Tomcat( quadrant, pos, gModelNameF14Vec ) );
	}
Error: PYTHON_RETURN_BAD_OBJECT_ERROR
}

//------------------------------------------------------------------------------

// Table of C functions that will be imported as Python functions in a Python module.

// GameBase/GamePony class interface:
static PyMethodDef sMethodsGame[] =
{
	// Func name in Python:		// Func name in C:
    { "GetPythonDir",			(PyCFunction)PyGame_GetPythonDir,		METH_NOARGS,  "" },
    { "GetModelDir",			(PyCFunction)PyGame_GetModelDir,		METH_NOARGS,  "" },
    { "GetPlayer",				(PyCFunction)PyGame_GetPlayer,			METH_VARARGS, "" },
    { "CraftListAppend",		(PyCFunction)PyGame_CraftListAppend,	METH_VARARGS, "" },
    { "DynaModelTargetNew",		(PyCFunction)PyGame_DynaModelTargetNew,	METH_VARARGS, "" },
    { "CraftF14TomcatNew",		(PyCFunction)PyGame_CraftF14TomcatNew,	METH_VARARGS, "" },
	{ NULL,						NULL,									0,			  NULL }
};

/*****************************************************************************
 * Create a Python module.
 *****************************************************************************/
bool
PythonModuleGame( void )
{
#define PYMOD1	"cgame"		// Python module name
#define PYMOD2	"cgame2"	// this module will hold constants originating from C++

	// Let cgame combine Game Base and Game Pony.
	if ( gPython.NewModule( PYMOD1, sMethodsGame ) == NULL )
	{
		PRINT_PYTHON_ERROR( "failed to create " PYMOD1 " Python module" );
		return false;
	}

	// Here's why Python variables magically appear in the Python code...
	// Create another Python module, then add definitions to it
	// whose values originate from this C++ source code.
	PyObject* module2 = gPython.NewModule( PYMOD2 );
	if ( module2 == NULL )
	{
		PRINT_PYTHON_ERROR( "failed to create " PYMOD2 " Python module" );
		return false;
	}
	// PyModule*() return 0 if ok
	int definingErrors = \
	  PyModule_AddIntConstant( module2, "eAnimate_NONE",           eAnimate_NONE )
	| PyModule_AddIntConstant( module2, "eAnimate_AUTO_TRANSLATE", eAnimate_AUTO_TRANSLATE )
	| PyModule_AddIntConstant( module2, "eAnimate_PILOT_PURSUE",   eAnimate_PILOT_PURSUE );
	if ( definingErrors )
	{
		PRINT_PYTHON_ERROR( "failed to define objects in " PYMOD2 " Python module" );
		return false;
	}

	// Ok.
	return true;
}

#endif // NO_PYTHON
