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-29 15:19:36 +00:00
|
|
|
#include <server/WorldTime.h>
|
2000-11-21 19:24:29 +00:00
|
|
|
#include <common/const.h>
|
2000-11-14 15:52:10 +00:00
|
|
|
|
2000-11-29 18:02:01 +00:00
|
|
|
static void Function_dealloc(FunctionObject * self)
|
|
|
|
|
{
|
|
|
|
|
PyMem_DEL(self);
|
|
|
|
|
}
|
|
|
|
|
|
2000-12-01 00:13:39 +00:00
|
|
|
static PyObject * log_debug(PyObject * self, PyObject * args, PyObject * kwds)
|
2000-11-29 18:02:01 +00:00
|
|
|
{
|
|
|
|
|
printf("LOG.DEBUG\n");
|
|
|
|
|
Py_INCREF(Py_None);
|
|
|
|
|
return Py_None;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
PyTypeObject log_debug_type = {
|
|
|
|
|
PyObject_HEAD_INIT(&PyType_Type)
|
|
|
|
|
0,
|
|
|
|
|
"Function",
|
|
|
|
|
sizeof(FunctionObject),
|
|
|
|
|
0,
|
|
|
|
|
/* methods */
|
|
|
|
|
(destructor)Function_dealloc,
|
|
|
|
|
0, /* tp_print */
|
|
|
|
|
0, /* tp_getattr */
|
|
|
|
|
0, /* tp_setattr */
|
|
|
|
|
0, /* tp_compare */
|
|
|
|
|
0, /* tp_repr */
|
|
|
|
|
0, /* tp_as_number */
|
|
|
|
|
0, /* tp_as_sequence */
|
|
|
|
|
0, /* tp_as_mapping */
|
|
|
|
|
0, /* tp_hash */
|
|
|
|
|
log_debug, /* tp_call */
|
|
|
|
|
};
|
2000-11-19 00:02:29 +00:00
|
|
|
|
2000-12-01 00:13:39 +00:00
|
|
|
static PyObject * dictlist_add_value(PyObject * self, PyObject * args, PyObject * kwds)
|
|
|
|
|
{
|
|
|
|
|
PyObject * dict, * item;
|
|
|
|
|
char * key;
|
|
|
|
|
if (!PyArg_ParseTuple(args, "OsO", &dict, &key, &item)) {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
if (!PyDict_Check(dict)) {
|
|
|
|
|
PyErr_SetString(PyExc_TypeError, "Dict is not a dictionary");
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
PyObject * list = PyDict_GetItemString(dict, key);
|
|
|
|
|
if (list != NULL) {
|
|
|
|
|
if (!PyList_Check(list)) {
|
|
|
|
|
PyErr_SetString(PyExc_TypeError, "Dict does not contain a list");
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
int i, size = PyList_Size(list);
|
|
|
|
|
for(i = 0; i < size; i++) {
|
|
|
|
|
if (PyList_GetItem(list, i) == item) {
|
|
|
|
|
goto present;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
PyList_Append(list, item);
|
|
|
|
|
} else {
|
|
|
|
|
list = PyList_New(1);
|
2000-12-03 14:23:26 +00:00
|
|
|
Py_INCREF(item);
|
2000-12-01 00:13:39 +00:00
|
|
|
PyList_SetItem(list, 1, item);
|
|
|
|
|
PyDict_SetItemString(dict, key, list);
|
2000-12-03 14:23:26 +00:00
|
|
|
Py_DECREF(list);
|
2000-12-01 00:13:39 +00:00
|
|
|
}
|
|
|
|
|
present:
|
|
|
|
|
Py_INCREF(Py_None);
|
|
|
|
|
return Py_None;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
PyTypeObject dictlist_add_value_type = {
|
|
|
|
|
PyObject_HEAD_INIT(&PyType_Type)
|
|
|
|
|
0,
|
|
|
|
|
"Function",
|
|
|
|
|
sizeof(FunctionObject),
|
|
|
|
|
0,
|
|
|
|
|
/* methods */
|
|
|
|
|
(destructor)Function_dealloc,
|
|
|
|
|
0, /* tp_print */
|
|
|
|
|
0, /* tp_getattr */
|
|
|
|
|
0, /* tp_setattr */
|
|
|
|
|
0, /* tp_compare */
|
|
|
|
|
0, /* tp_repr */
|
|
|
|
|
0, /* tp_as_number */
|
|
|
|
|
0, /* tp_as_sequence */
|
|
|
|
|
0, /* tp_as_mapping */
|
|
|
|
|
0, /* tp_hash */
|
|
|
|
|
dictlist_add_value, /* tp_call */
|
|
|
|
|
};
|
|
|
|
|
|
2000-12-03 14:23:26 +00:00
|
|
|
void Create_PyThing(Thing * thing, const string & package, const string & _type)
|
2000-11-19 00:02:29 +00:00
|
|
|
{
|
2000-12-03 14:23:26 +00:00
|
|
|
string type = _type;
|
|
|
|
|
type[0] = toupper(type[0]);
|
2000-11-19 00:02:29 +00:00
|
|
|
PyObject * mod_dict;
|
|
|
|
|
if ((mod_dict = PyImport_ImportModule((char *)package.c_str()))==NULL) {
|
2000-12-03 14:23:26 +00:00
|
|
|
cerr << "Cld no find python module " << package << endl << flush;
|
2000-11-19 00:02:29 +00:00
|
|
|
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) {
|
2000-12-03 14:23:26 +00:00
|
|
|
cerr << "Cld no find class in module " << package << endl << flush;
|
2000-11-19 00:02:29 +00:00
|
|
|
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) {
|
2000-12-03 14:23:26 +00:00
|
|
|
cerr << "Could not get python obj" << endl << flush;
|
2000-11-19 00:02:29 +00:00
|
|
|
} 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;
|
2000-12-01 00:13:39 +00:00
|
|
|
Vector3D val;
|
2000-11-20 20:28:08 +00:00
|
|
|
// We need to deal with actual args here
|
2000-12-01 00:13:39 +00:00
|
|
|
PyObject * clist;
|
|
|
|
|
double x,y,z;
|
|
|
|
|
if ( (PyArg_ParseTuple(args, "O", &clist)) &&
|
|
|
|
|
(PyList_Check(clist)) &&
|
|
|
|
|
(PyList_Size(clist) == 3) ) {
|
|
|
|
|
PyObject * X = PyList_GetItem(clist, 0);
|
|
|
|
|
PyObject * Y = PyList_GetItem(clist, 1);
|
|
|
|
|
PyObject * Z = PyList_GetItem(clist, 2);
|
|
|
|
|
if (PyFloat_Check(X) && PyFloat_Check(Y) && PyFloat_Check(Z)) {
|
|
|
|
|
val = Vector3D(PyFloat_AsDouble(X),
|
|
|
|
|
PyFloat_AsDouble(Y),
|
|
|
|
|
PyFloat_AsDouble(Z));
|
|
|
|
|
} else if (PyInt_Check(X) && PyInt_Check(Y) && PyInt_Check(Z)) {
|
|
|
|
|
val = Vector3D(double(PyInt_AsLong(X)),
|
|
|
|
|
double(PyInt_AsLong(Y)),
|
|
|
|
|
double(PyInt_AsLong(Z)));
|
|
|
|
|
} else {
|
|
|
|
|
PyErr_SetString(PyExc_TypeError, "Vector3D must take list of floats, or ints");
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
} else if (PyArg_ParseTuple(args, "ddd", &x, &y, &z)) {
|
|
|
|
|
val = Vector3D(x,y,z);
|
|
|
|
|
} else if (!PyArg_ParseTuple(args, "")) {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2000-11-20 20:28:08 +00:00
|
|
|
o = newVector3DObject(args);
|
|
|
|
|
if ( o == NULL ) {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2000-12-01 00:13:39 +00:00
|
|
|
o->coords = val;
|
2000-11-20 20:28:08 +00:00
|
|
|
return (PyObject *)o;
|
|
|
|
|
}
|
|
|
|
|
|
2000-11-29 15:19:36 +00:00
|
|
|
static PyObject * worldtime_new(PyObject * self, PyObject * args)
|
|
|
|
|
{
|
|
|
|
|
WorldTimeObject *o;
|
|
|
|
|
|
|
|
|
|
int seconds;
|
|
|
|
|
if (!PyArg_ParseTuple(args, "i", &seconds)) {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
o = newWorldTimeObject(args);
|
|
|
|
|
if ( o == NULL ) {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
o->time = new WorldTime(seconds);
|
|
|
|
|
return (PyObject *)o;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static PyObject * oplist_new(PyObject * self, PyObject * args)
|
|
|
|
|
{
|
|
|
|
|
OplistObject *o;
|
|
|
|
|
|
2000-12-01 00:13:39 +00:00
|
|
|
PyObject * op1 = NULL, * op2 = NULL;
|
|
|
|
|
if (!PyArg_ParseTuple(args, "|OO", &op1, &op2)) {
|
2000-11-29 15:19:36 +00:00
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
o = newOplistObject(args);
|
|
|
|
|
if ( o == NULL ) {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
o->ops = new oplist();
|
2000-12-01 00:13:39 +00:00
|
|
|
if (op1 != NULL) {
|
2000-12-03 14:23:26 +00:00
|
|
|
if ((PyTypeObject *)PyObject_Type(op1) == &RootOperation_Type) {
|
|
|
|
|
o->ops->push_back( ((RootOperationObject*)op1)->operation );
|
|
|
|
|
} else if (op1 != Py_None) {
|
2000-12-01 00:13:39 +00:00
|
|
|
PyErr_SetString(PyExc_TypeError, "Argument must be an op");
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (op2 != NULL) {
|
2000-12-03 14:23:26 +00:00
|
|
|
if ((PyTypeObject *)PyObject_Type(op2) == &RootOperation_Type) {
|
|
|
|
|
o->ops->push_back( ((RootOperationObject*)op2)->operation );
|
|
|
|
|
} else if (op1 != Py_None) {
|
2000-12-01 00:13:39 +00:00
|
|
|
PyErr_SetString(PyExc_TypeError, "Argument must be an op");
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
}
|
2000-11-29 15:19:36 +00:00
|
|
|
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-12-01 00:13:39 +00:00
|
|
|
static PyObject * entity_new(PyObject * self, PyObject * args, PyObject * kwds)
|
|
|
|
|
{
|
|
|
|
|
AtlasObject *o;
|
|
|
|
|
char * id = NULL;
|
|
|
|
|
|
|
|
|
|
if (!PyArg_ParseTuple(args, "|s", id)) {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
Object::MapType _omap;
|
|
|
|
|
Object obj(_omap);
|
|
|
|
|
Object::MapType & omap = obj.AsMap();
|
|
|
|
|
if (id != NULL) {
|
|
|
|
|
omap["id"] = string(id);
|
|
|
|
|
}
|
|
|
|
|
PyObject * keys = PyMapping_Keys(kwds);
|
|
|
|
|
PyObject * vals = PyMapping_Values(kwds);
|
|
|
|
|
if ((keys == NULL) || (vals == NULL)) {
|
|
|
|
|
PyErr_SetString(PyExc_TypeError, "Error in keywords");
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
int i, size=PyMapping_Length(keys);
|
|
|
|
|
for(i = 0; i < size; i++) {
|
|
|
|
|
char * key = PyString_AsString(PyList_GetItem(keys, i));
|
|
|
|
|
PyObject * val = PyList_GetItem(vals, i);
|
|
|
|
|
if ((strcmp(key, "location") == 0) &&
|
|
|
|
|
((PyTypeObject *)PyObject_Type(val) == &Location_Type)) {
|
|
|
|
|
LocationObject * loc = (LocationObject*)val;
|
|
|
|
|
loc->location->addObject(&obj);
|
|
|
|
|
} else {
|
|
|
|
|
Object val_obj = PyObject_asObject(val);
|
|
|
|
|
if (val_obj.GetType() == Object::TYPE_NONE) {
|
2000-12-03 14:23:26 +00:00
|
|
|
fprintf(stderr, "Could not handle %s value in Entity()", key);
|
2000-12-01 00:13:39 +00:00
|
|
|
PyErr_SetString(PyExc_TypeError, "Argument type error to Entity()");
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
omap[key] = val_obj;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
o = newAtlasObject(args);
|
|
|
|
|
if ( o == NULL ) {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
o->m_obj = new Object(obj);
|
|
|
|
|
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;
|
2000-11-26 19:58:36 +00:00
|
|
|
if (strcmp(type, "tick") == 0) {
|
2000-11-21 19:24:29 +00:00
|
|
|
*op->operation = Tick::Instantiate();
|
2000-11-26 19:58:36 +00:00
|
|
|
} else if (strcmp(type, "create") == 0) {
|
2000-11-21 19:24:29 +00:00
|
|
|
*op->operation = Create::Instantiate();
|
2000-11-26 19:58:36 +00:00
|
|
|
} else if (strcmp(type, "setup") == 0) {
|
|
|
|
|
*op->operation = Setup::Instantiate();
|
|
|
|
|
} else if (strcmp(type, "look") == 0) {
|
|
|
|
|
*op->operation = Look::Instantiate();
|
|
|
|
|
} else if (strcmp(type, "move") == 0) {
|
|
|
|
|
*op->operation = Move::Instantiate();
|
|
|
|
|
} else if (strcmp(type, "talk") == 0) {
|
|
|
|
|
*op->operation = Talk::Instantiate();
|
2000-11-21 19:24:29 +00:00
|
|
|
} else {
|
2000-12-03 14:23:26 +00:00
|
|
|
fprintf(stderr, "ERROR: PYTHON CREATING AN UNHANDLED %s OPERATION\n", type);
|
2000-11-21 19:24:29 +00:00
|
|
|
*op->operation = RootOperation::Instantiate();
|
2000-12-01 00:13:39 +00:00
|
|
|
// FIXME, just to test
|
|
|
|
|
Py_INCREF(Py_None);
|
|
|
|
|
return Py_None;
|
2000-11-21 19:24:29 +00:00
|
|
|
}
|
|
|
|
|
if (PyMapping_HasKeyString(kwds, "to")) {
|
|
|
|
|
to = PyMapping_GetItemString(kwds, "to");
|
|
|
|
|
printf("Operation creation sets to\n");
|
2000-12-03 14:23:26 +00:00
|
|
|
// FIXME I think I need to actually do something with said value now
|
2000-11-21 19:24:29 +00:00
|
|
|
}
|
|
|
|
|
if (PyMapping_HasKeyString(kwds, "from_")) {
|
|
|
|
|
from = PyMapping_GetItemString(kwds, "from_");
|
|
|
|
|
printf("Operation creation sets from\n");
|
2000-12-03 14:23:26 +00:00
|
|
|
// FIXME I think I need to actually do something with said value now
|
2000-11-21 19:24:29 +00:00
|
|
|
}
|
|
|
|
|
return (PyObject *)op;
|
2000-11-14 15:52:10 +00:00
|
|
|
}
|
|
|
|
|
|
2000-11-30 12:51:59 +00:00
|
|
|
static PyObject * set_kw(PyObject * meth_self, PyObject * args)
|
2000-11-24 01:41:06 +00:00
|
|
|
{
|
|
|
|
|
// Takes self, kw, name, default=None
|
2000-11-30 12:51:59 +00:00
|
|
|
PyObject * self;
|
|
|
|
|
PyObject * kw;
|
|
|
|
|
char * name;
|
|
|
|
|
PyObject * def = NULL;
|
|
|
|
|
|
|
|
|
|
if (!PyArg_ParseTuple(args, "OOs|O", &self, &kw, &name, &def)) {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
PyObject * attr = PyObject_GetAttrString(self, "attributes");
|
|
|
|
|
if (attr == NULL) {
|
|
|
|
|
PyErr_SetString(PyExc_TypeError, "SET_KW: No attributes list");
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
int i = PyList_Size(attr);
|
|
|
|
|
char * entry;
|
|
|
|
|
PyObject * item;
|
|
|
|
|
for(i= 0; i < PyList_Size(attr); i++) {
|
|
|
|
|
item = PyList_GetItem(attr, i);
|
|
|
|
|
if (!PyString_Check(item)) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
entry = PyString_AsString(item);
|
|
|
|
|
if (strcmp(entry, name) == 0) {
|
|
|
|
|
goto list_contains_it;
|
|
|
|
|
}
|
|
|
|
|
// Should I free entry at this point?
|
|
|
|
|
}
|
2000-12-03 14:23:26 +00:00
|
|
|
{
|
|
|
|
|
PyObject * namestr = PyString_FromString(name);
|
|
|
|
|
PyList_Append(attr, namestr);
|
|
|
|
|
Py_DECREF(namestr);
|
|
|
|
|
}
|
2000-11-30 12:51:59 +00:00
|
|
|
list_contains_it:
|
|
|
|
|
if (!PyDict_Check(kw)) {
|
|
|
|
|
PyErr_SetString(PyExc_TypeError, "SET_KW: kw not a dict");
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
PyObject * value = NULL;
|
|
|
|
|
if ((value = PyDict_GetItemString(kw, name)) == NULL) {
|
|
|
|
|
PyObject * copy = PyDict_GetItemString(kw, "copy");
|
|
|
|
|
if ((copy != NULL) && (PyObject_HasAttrString(copy, name))) {
|
|
|
|
|
value = PyObject_GetAttrString(copy, name);
|
|
|
|
|
} else {
|
|
|
|
|
value = def;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (value == NULL) {
|
|
|
|
|
Py_INCREF(Py_None);
|
2000-12-03 14:23:26 +00:00
|
|
|
value = Py_None;
|
2000-11-30 12:51:59 +00:00
|
|
|
}
|
|
|
|
|
PyObject_SetAttrString(self, name, value);
|
|
|
|
|
|
2000-11-24 01:41:06 +00:00
|
|
|
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},
|
2000-12-01 00:13:39 +00:00
|
|
|
{"Entity", (PyCFunction)entity_new, METH_VARARGS|METH_KEYWORDS},
|
2000-11-29 15:19:36 +00:00
|
|
|
{"Message", oplist_new, METH_VARARGS},
|
2000-11-21 19:24:29 +00:00
|
|
|
{"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[] = {
|
2000-11-29 15:19:36 +00:00
|
|
|
{"WorldTime", worldtime_new, METH_VARARGS},
|
2000-11-20 20:28:08 +00:00
|
|
|
{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);
|
2000-12-03 14:23:26 +00:00
|
|
|
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
|
2000-12-03 14:23:26 +00:00
|
|
|
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-12-03 14:23:26 +00:00
|
|
|
PyRun_SimpleString("from hooks import ruleset_import_hooks\n");
|
|
|
|
|
PyRun_SimpleString("ruleset_import_hooks.install(['acorn','basic'])\n");
|
2000-11-24 01:41:06 +00:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (Py_InitModule("Vector3D", Vector3D_methods) == NULL) {
|
|
|
|
|
printf("Failed to Create Vector3D thing\n");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
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-12-01 00:13:39 +00:00
|
|
|
PyObject * debug = (PyObject *)PyObject_NEW(FunctionObject, &log_debug_type);
|
2000-11-29 18:02:01 +00:00
|
|
|
PyObject_SetAttrString(log, "debug", debug);
|
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",
|
2000-11-27 18:28:20 +00:00
|
|
|
PyFloat_FromDouble(consts::basic_tick));
|
2000-11-21 19:24:29 +00:00
|
|
|
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");
|
2000-12-01 00:13:39 +00:00
|
|
|
PyObject * add_value = (PyObject *)PyObject_NEW(FunctionObject, &dictlist_add_value_type);
|
|
|
|
|
PyObject_SetAttrString(dictlist, "add_value", add_value);
|
2000-11-20 20:28:08 +00:00
|
|
|
PyDict_SetItemString(dict, "dictlist", dictlist);
|
2000-11-14 15:52:10 +00:00
|
|
|
}
|