2000-11-14 15:52:10 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
|
|
#include <Python.h>
|
|
|
|
|
|
|
|
|
|
#include "Python_API.h"
|
2000-11-17 00:18:32 +00:00
|
|
|
#include "Thing.h"
|
2000-11-21 19:24:29 +00:00
|
|
|
|
2000-11-20 20:28:08 +00:00
|
|
|
#include <modules/Location.h>
|
2000-11-21 19:24:29 +00:00
|
|
|
#include <common/const.h>
|
2000-11-14 15:52:10 +00:00
|
|
|
|
2000-11-19 00:02:29 +00:00
|
|
|
|
|
|
|
|
void Create_PyThing(Thing * thing, const string & package, const string & type)
|
|
|
|
|
{
|
|
|
|
|
PyObject * mod_dict;
|
|
|
|
|
if ((mod_dict = PyImport_ImportModule((char *)package.c_str()))==NULL) {
|
|
|
|
|
cout << "Cld no find python module " << package << endl << flush;
|
|
|
|
|
PyErr_Print();
|
|
|
|
|
return;
|
|
|
|
|
} else {
|
|
|
|
|
cout << "Got python module " << package << endl << flush;
|
|
|
|
|
}
|
|
|
|
|
PyObject * my_class = PyObject_GetAttrString(mod_dict, (char *)type.c_str());
|
|
|
|
|
if (my_class == NULL) {
|
|
|
|
|
cout << "Cld no find class in module " << package << endl << flush;
|
|
|
|
|
PyErr_Print();
|
|
|
|
|
return;
|
|
|
|
|
} else {
|
|
|
|
|
cout << "Got python class " << type << " in " << package << endl << flush;
|
|
|
|
|
}
|
|
|
|
|
if (PyCallable_Check(my_class) == 0) {
|
|
|
|
|
cout << "It does not seem to be a class at all" << endl << flush;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
ThingObject * pyThing = newThingObject(NULL);
|
|
|
|
|
pyThing->m_thing = thing;
|
|
|
|
|
if (thing->set_object(PyEval_CallFunction(my_class,"(O)", (PyObject *)pyThing)) == -1) {
|
|
|
|
|
if (PyErr_Occurred() == NULL) {
|
|
|
|
|
cout << "Could not get python obj" << endl << flush;
|
|
|
|
|
} else {
|
|
|
|
|
cout << "Reporting python error for " << type << endl << flush;
|
|
|
|
|
PyErr_Print();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2000-11-20 20:28:08 +00:00
|
|
|
static PyObject * location_new(PyObject * self, PyObject * args)
|
|
|
|
|
{
|
|
|
|
|
LocationObject *o;
|
|
|
|
|
// We need to deal with actual args here
|
|
|
|
|
if (!PyArg_ParseTuple(args, "")) {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
o = newLocationObject(args);
|
|
|
|
|
if ( o == NULL ) {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
o->location = new Location;
|
|
|
|
|
return (PyObject *)o;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static PyObject * vector3d_new(PyObject * self, PyObject * args)
|
|
|
|
|
{
|
|
|
|
|
Vector3DObject *o;
|
|
|
|
|
// We need to deal with actual args here
|
|
|
|
|
if (!PyArg_ParseTuple(args, "")) {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
o = newVector3DObject(args);
|
|
|
|
|
if ( o == NULL ) {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
return (PyObject *)o;
|
|
|
|
|
}
|
|
|
|
|
|
2000-11-17 00:18:32 +00:00
|
|
|
static PyObject * object_new(PyObject * self, PyObject * args)
|
2000-11-16 16:35:03 +00:00
|
|
|
{
|
2000-11-17 00:18:32 +00:00
|
|
|
AtlasObject *o;
|
2000-11-16 16:35:03 +00:00
|
|
|
|
|
|
|
|
if (!PyArg_ParseTuple(args, "")) {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2000-11-17 00:18:32 +00:00
|
|
|
o = newAtlasObject(args);
|
2000-11-16 16:35:03 +00:00
|
|
|
if ( o == NULL ) {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2000-11-17 00:18:32 +00:00
|
|
|
o->m_obj = new Object;
|
2000-11-16 16:35:03 +00:00
|
|
|
return (PyObject *)o;
|
|
|
|
|
}
|
|
|
|
|
|
2000-11-17 00:18:32 +00:00
|
|
|
static PyObject * cppthing_new(PyObject * self, PyObject * args)
|
2000-11-16 16:35:03 +00:00
|
|
|
{
|
2000-11-17 00:18:32 +00:00
|
|
|
ThingObject *o;
|
2000-11-16 16:35:03 +00:00
|
|
|
|
|
|
|
|
if (!PyArg_ParseTuple(args, "")) {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2000-11-17 00:18:32 +00:00
|
|
|
o = newThingObject(args);
|
2000-11-16 16:35:03 +00:00
|
|
|
if ( o == NULL ) {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2000-11-17 00:18:32 +00:00
|
|
|
//o->m_thing = new Thing;
|
2000-11-16 16:35:03 +00:00
|
|
|
return (PyObject *)o;
|
|
|
|
|
}
|
|
|
|
|
|
2000-11-21 19:24:29 +00:00
|
|
|
static PyObject * operation_new(PyObject * self, PyObject * args, PyObject * kwds)
|
2000-11-14 15:52:10 +00:00
|
|
|
{
|
2000-11-21 19:24:29 +00:00
|
|
|
printf("New Operation\n");
|
|
|
|
|
RootOperationObject * op;
|
|
|
|
|
|
|
|
|
|
char * type;
|
|
|
|
|
PyObject * to = NULL;
|
|
|
|
|
PyObject * from = NULL;
|
|
|
|
|
PyObject * arg1 = NULL;
|
|
|
|
|
PyObject * arg2 = NULL;
|
|
|
|
|
PyObject * arg3 = NULL;
|
|
|
|
|
|
|
|
|
|
printf("New Operation: parsing args\n");
|
|
|
|
|
if (!PyArg_ParseTuple(args, "s|OOO", &type, &arg1, &arg2, &arg3)) {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
printf("New Operation: creating operation\n");
|
|
|
|
|
op = newAtlasRootOperation(args);
|
|
|
|
|
if (op == NULL) {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
op->operation = new RootOperation;
|
|
|
|
|
if (strcpy(type, "tick") == 0) {
|
|
|
|
|
*op->operation = Tick::Instantiate();
|
|
|
|
|
} else if (strcpy(type, "create") == 0) {
|
|
|
|
|
*op->operation = Create::Instantiate();
|
|
|
|
|
} else {
|
|
|
|
|
*op->operation = RootOperation::Instantiate();
|
|
|
|
|
}
|
|
|
|
|
if (PyMapping_HasKeyString(kwds, "to")) {
|
|
|
|
|
to = PyMapping_GetItemString(kwds, "to");
|
|
|
|
|
printf("Operation creation sets to\n");
|
|
|
|
|
}
|
|
|
|
|
if (PyMapping_HasKeyString(kwds, "from_")) {
|
|
|
|
|
from = PyMapping_GetItemString(kwds, "from_");
|
|
|
|
|
printf("Operation creation sets from\n");
|
|
|
|
|
}
|
|
|
|
|
return (PyObject *)op;
|
2000-11-14 15:52:10 +00:00
|
|
|
}
|
|
|
|
|
|
2000-11-24 01:41:06 +00:00
|
|
|
static PyObject * set_kw(PyObject * self, PyObject * args)
|
|
|
|
|
{
|
|
|
|
|
// Takes self, kw, name, default=None
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2000-11-19 00:02:29 +00:00
|
|
|
static PyMethodDef atlas_methods[] = {
|
2000-11-21 19:24:29 +00:00
|
|
|
/* {"system", spam_system, METH_VARARGS}, */
|
|
|
|
|
{"Operation", (PyCFunction)operation_new, METH_VARARGS|METH_KEYWORDS},
|
|
|
|
|
{"Location", location_new, METH_VARARGS},
|
|
|
|
|
{"Object", object_new, METH_VARARGS},
|
|
|
|
|
{"cppThing", cppthing_new, METH_VARARGS},
|
|
|
|
|
{NULL, NULL} /* Sentinel */
|
2000-11-14 15:52:10 +00:00
|
|
|
};
|
|
|
|
|
|
2000-11-20 20:28:08 +00:00
|
|
|
static PyMethodDef Vector3D_methods[] = {
|
|
|
|
|
{"Vector3D", vector3d_new, METH_VARARGS},
|
|
|
|
|
{NULL, NULL} /* Sentinel */
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static PyMethodDef server_methods[] = {
|
|
|
|
|
//{"null", null_new, METH_VARARGS},
|
|
|
|
|
{NULL, NULL} /* Sentinel */
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static PyMethodDef common_methods[] = {
|
|
|
|
|
//{"null", null_new, METH_VARARGS},
|
|
|
|
|
{NULL, NULL} /* Sentinel */
|
|
|
|
|
};
|
|
|
|
|
|
2000-11-24 01:41:06 +00:00
|
|
|
static PyMethodDef misc_methods[] = {
|
|
|
|
|
{"set_kw", set_kw, METH_VARARGS},
|
|
|
|
|
{NULL, NULL} /* Sentinel */
|
|
|
|
|
};
|
|
|
|
|
|
2000-11-14 15:52:10 +00:00
|
|
|
void init_python_api()
|
|
|
|
|
{
|
|
|
|
|
char * cwd;
|
|
|
|
|
|
|
|
|
|
if ((cwd = getcwd(NULL, 0)) != NULL) {
|
2000-11-24 01:41:06 +00:00
|
|
|
size_t len = strlen(cwd) * 2 + 60;
|
2000-11-17 00:18:32 +00:00
|
|
|
char * pypath = (char *)malloc(len);
|
|
|
|
|
strcpy(pypath, cwd);
|
|
|
|
|
strcat(pypath, "/rulesets/basic");
|
2000-11-24 01:41:06 +00:00
|
|
|
// This should eventually pull in a ruleset name from
|
|
|
|
|
// the commandline args.
|
|
|
|
|
// basic ruleset should always be left on the end
|
|
|
|
|
// strcat(pypath, cwd);
|
|
|
|
|
// strcat(pypath, "/rulesets/acorn");
|
2000-11-17 00:18:32 +00:00
|
|
|
setenv("PYTHONPATH", pypath, 1);
|
2000-11-14 15:52:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Py_Initialize();
|
|
|
|
|
|
2000-11-24 01:41:06 +00:00
|
|
|
PyRun_SimpleString("from hooks import ruleset_import_hooks\n");
|
|
|
|
|
PyRun_SimpleString("ruleset_import_hooks.install(['basic','acorn'])\n");
|
|
|
|
|
|
2000-11-19 00:02:29 +00:00
|
|
|
if (Py_InitModule("atlas", atlas_methods) == NULL) {
|
2000-11-20 20:28:08 +00:00
|
|
|
printf("Failed to Create atlas thing\n");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
printf("Created atlas thing\n");
|
|
|
|
|
|
|
|
|
|
if (Py_InitModule("Vector3D", Vector3D_methods) == NULL) {
|
|
|
|
|
printf("Failed to Create Vector3D thing\n");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
printf("Created Vector3D thing\n");
|
|
|
|
|
|
2000-11-24 01:41:06 +00:00
|
|
|
PyObject * misc;
|
|
|
|
|
if ((misc = Py_InitModule("misc", misc_methods)) == NULL) {
|
|
|
|
|
printf("Failed to Create misc thing\n");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2000-11-20 20:28:08 +00:00
|
|
|
PyObject * common;
|
|
|
|
|
PyObject * dict;
|
|
|
|
|
if ((common = Py_InitModule("common", common_methods)) == NULL) {
|
|
|
|
|
printf("Failed to Create common thing\n");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
printf("Created common thing\n");
|
|
|
|
|
PyObject * _const = PyModule_New("const");
|
|
|
|
|
PyObject * log = PyModule_New("log");
|
|
|
|
|
dict = PyModule_GetDict(common);
|
|
|
|
|
PyDict_SetItemString(dict, "const", _const);
|
|
|
|
|
PyDict_SetItemString(dict, "log", log);
|
2000-11-24 01:41:06 +00:00
|
|
|
//PyDict_SetItemString(dict, "misc", misc);
|
2000-11-20 20:28:08 +00:00
|
|
|
PyObject_SetAttrString(_const, "server_python", PyInt_FromLong(0));
|
2000-11-21 19:24:29 +00:00
|
|
|
PyObject_SetAttrString(_const, "debug_level",
|
|
|
|
|
PyInt_FromLong(consts::debug_level));
|
|
|
|
|
PyObject_SetAttrString(_const, "debug_thinking",
|
|
|
|
|
PyInt_FromLong(consts::debug_thinking));
|
|
|
|
|
|
|
|
|
|
PyObject_SetAttrString(_const, "time_multiplier",
|
|
|
|
|
PyFloat_FromDouble(consts::time_multiplier));
|
|
|
|
|
PyObject_SetAttrString(_const, "base_velocity_coefficient",
|
|
|
|
|
PyFloat_FromDouble(consts::base_velocity_coefficient));
|
|
|
|
|
PyObject_SetAttrString(_const, "base_velocity",
|
|
|
|
|
PyFloat_FromDouble(consts::base_velocity));
|
|
|
|
|
|
|
|
|
|
PyObject_SetAttrString(_const, "basic_tick",
|
|
|
|
|
PyInt_FromLong(consts::basic_tick));
|
|
|
|
|
PyObject_SetAttrString(_const, "day_in_seconds",
|
|
|
|
|
PyInt_FromLong(consts::day_in_seconds));
|
|
|
|
|
|
|
|
|
|
PyObject_SetAttrString(_const, "sight_range",
|
|
|
|
|
PyFloat_FromDouble(consts::sight_range));
|
|
|
|
|
PyObject_SetAttrString(_const, "hearing_range",
|
|
|
|
|
PyFloat_FromDouble(consts::hearing_range));
|
|
|
|
|
PyObject_SetAttrString(_const, "collision_range",
|
|
|
|
|
PyFloat_FromDouble(consts::collision_range));
|
|
|
|
|
PyObject_SetAttrString(_const, "enable_ranges",
|
|
|
|
|
PyInt_FromLong(consts::enable_ranges));
|
2000-11-20 20:28:08 +00:00
|
|
|
|
|
|
|
|
PyObject * server;
|
|
|
|
|
if ((server = Py_InitModule("server", server_methods)) == NULL) {
|
|
|
|
|
printf("Failed to Create server thing\n");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
dict = PyModule_GetDict(server);
|
|
|
|
|
PyObject * dictlist = PyModule_New("dictlist");
|
|
|
|
|
PyDict_SetItemString(dict, "dictlist", dictlist);
|
|
|
|
|
if (PyExc_IOError != NULL) {
|
|
|
|
|
printf("Got PyExc_IOError\n");
|
2000-11-14 15:52:10 +00:00
|
|
|
}
|
|
|
|
|
}
|