// Cyphesis Online RPG Server and AI Engine // Copyright (C) 2011 Alistair Riddoch // // This program is free software; you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation; either version 2 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program; if not, write to the Free Software Foundation, // Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA #include "Py_Shape.h" #include "Py_BBox.h" #include "Py_Message.h" #include "Py_Point3D.h" #include "common/log.h" #include "physics/Shape.h" #include "physics/Course.h" #include #include #include #include #include #include #include template class LinearCourse : public Course { }; using Atlas::Message::Element; using Atlas::Message::ListType; using Atlas::Message::MapType; static PyObject * Shape_area(PyShape * self) { #ifndef NDEBUG if (self->shape.s == NULL) { PyErr_SetString(PyExc_AssertionError, "NULL Shape in Shape.area"); return NULL; } #endif // NDEBUG return PyFloat_FromDouble(self->shape.s->area()); } static PyObject * Shape_footprint(PyShape * self) { #ifndef NDEBUG if (self->shape.s == NULL) { PyErr_SetString(PyExc_AssertionError, "NULL Shape in Shape.footprint"); return NULL; } #endif // NDEBUG PyShape * res = newPyBox(); if (res != 0) { res->shape.p = new MathShape(self->shape.s->footprint()); } return (PyObject*)res; } static PyObject * Shape_as_data(PyShape * self) { #ifndef NDEBUG if (self->shape.s == NULL) { PyErr_SetString(PyExc_AssertionError, "NULL Shape in Shape.as_data"); return NULL; } #endif // NDEBUG MapType atlas_data; self->shape.s->toAtlas(atlas_data); return MessageElement_asPyObject(atlas_data); } static PyMethodDef Shape_methods[] = { {"area", (PyCFunction)Shape_area, METH_NOARGS}, {"footprint", (PyCFunction)Shape_footprint, METH_NOARGS}, {"as_data", (PyCFunction)Shape_as_data, METH_NOARGS}, {NULL, NULL} /* sentinel */ }; static PyObject * Area_centre(PyShape * self) { #ifndef NDEBUG if (self->shape.s == NULL) { PyErr_SetString(PyExc_AssertionError, "NULL Shape in Shape.centre"); return NULL; } #endif // NDEBUG PyPoint3D * res = newPyPoint3D(); if (res != 0) { res->coords = self->shape.b->centre(); } return (PyObject*)res; } static PyObject * Area_low_corner(PyShape * self) { #ifndef NDEBUG if (self->shape.s == NULL) { PyErr_SetString(PyExc_AssertionError, "NULL Shape in Shape.low_corner"); return NULL; } #endif // NDEBUG PyPoint3D * res = newPyPoint3D(); if (res != 0) { res->coords = self->shape.b->lowCorner(); } return (PyObject*)res; } static PyObject * Area_high_corner(PyShape * self) { #ifndef NDEBUG if (self->shape.s == NULL) { PyErr_SetString(PyExc_AssertionError, "NULL Shape in Shape.high_corner"); return NULL; } #endif // NDEBUG PyPoint3D * res = newPyPoint3D(); if (res != 0) { res->coords = self->shape.b->highCorner(); } return (PyObject*)res; } static PyMethodDef Area_methods[] = { {"centre", (PyCFunction)Area_centre, METH_NOARGS}, {"low_corner", (PyCFunction)Area_low_corner, METH_NOARGS}, {"high_corner", (PyCFunction)Area_high_corner, METH_NOARGS}, {NULL, NULL} /* sentinel */ }; static PyObject * Body_centre(PyShape * self) { #ifndef NDEBUG if (self->shape.s == NULL) { PyErr_SetString(PyExc_AssertionError, "NULL Shape in Shape.centre"); return NULL; } #endif // NDEBUG PyPoint3D * res = newPyPoint3D(); if (res != 0) { res->coords = self->shape.b->centre(); } return (PyObject*)res; } static PyObject * Body_low_corner(PyShape * self) { #ifndef NDEBUG if (self->shape.s == NULL) { PyErr_SetString(PyExc_AssertionError, "NULL Shape in Shape.low_corner"); return NULL; } #endif // NDEBUG PyPoint3D * res = newPyPoint3D(); if (res != 0) { res->coords = self->shape.b->lowCorner(); } return (PyObject*)res; } static PyObject * Body_high_corner(PyShape * self) { #ifndef NDEBUG if (self->shape.s == NULL) { PyErr_SetString(PyExc_AssertionError, "NULL Shape in Shape.high_corner"); return NULL; } #endif // NDEBUG PyPoint3D * res = newPyPoint3D(); if (res != 0) { res->coords = self->shape.b->highCorner(); } return (PyObject*)res; } static PyMethodDef Body_methods[] = { {"centre", (PyCFunction)Body_centre, METH_NOARGS}, {"low_corner", (PyCFunction)Body_low_corner, METH_NOARGS}, {"high_corner", (PyCFunction)Body_high_corner, METH_NOARGS}, {NULL, NULL} /* sentinel */ }; static PyBBox * Box_extrude(PyShape * self, PyObject * args) { #ifndef NDEBUG if (self->shape.s == NULL) { PyErr_SetString(PyExc_AssertionError, "NULL Shape in Shape.as_data"); return NULL; } #endif // NDEBUG MathShape * shape = dynamic_cast *>(self->shape.p); if (shape == 0) { PyErr_SetString(PyExc_RuntimeError, "Shape is not a 2D axisbox"); return NULL; } float low, high; if (!PyArg_ParseTuple(args, "ff", &low, &high)) { return NULL; } PyBBox * ret = newPyBBox(); if (ret != 0) { ret->box = BBox(Point3D(shape->shape().lowCorner().x(), shape->shape().lowCorner().y(), low), Point3D(shape->shape().highCorner().x(), shape->shape().highCorner().y(), high), true); } return ret; } static PyMethodDef Box_methods[] = { {"extrude", (PyCFunction)Box_extrude, METH_VARARGS}, {NULL, NULL} /* sentinel */ }; static PyMethodDef Course_methods[] = { {NULL, NULL} /* sentinel */ }; static void Shape_dealloc(PyShape *self) { if (self->shape.s != NULL) { delete self->shape.s; } self->ob_type->tp_free((PyObject*)self); } static PyObject * Shape_getattro(PyShape *self, PyObject * oname) { #ifndef NDEBUG if (self->shape.s == NULL) { PyErr_SetString(PyExc_AssertionError, "NULL Shape in Shape.getattro"); return NULL; } #endif // NDEBUG // char * name = PyString_AsString(oname); return PyObject_GenericGetAttr((PyObject *)self, oname); } static int Shape_setattro(PyShape *self, PyObject *oname, PyObject *v) { #ifndef NDEBUG if (self->shape.s == NULL) { PyErr_SetString(PyExc_AssertionError, "NULL Shape in Shape.setattr"); return -1; } #endif // NDEBUG // char * name = PyString_AsString(oname); PyErr_SetString(PyExc_AttributeError, "unknown attribute"); return -1; } static PyObject * Polygon_getattro(PyShape *self, PyObject * oname) { #ifndef NDEBUG if (self->shape.s == NULL) { PyErr_SetString(PyExc_AssertionError, "NULL Shape in Polygon.getattro"); return NULL; } #endif // NDEBUG // char * name = PyString_AsString(oname); return PyObject_GenericGetAttr((PyObject *)self, oname); } static int Polygon_setattro(PyShape *self, PyObject *oname, PyObject *v) { #ifndef NDEBUG if (self->shape.s == NULL) { PyErr_SetString(PyExc_AssertionError, "NULL Shape in Polygon.setattr"); return -1; } #endif // NDEBUG // char * name = PyString_AsString(oname); PyErr_SetString(PyExc_AttributeError, "unknown attribute"); return -1; } static PyObject * Shape_repr(PyShape *self) { std::stringstream r; r << *self->shape.s; return PyString_FromString(r.str().c_str()); } static int Shape_init(PyShape * self, PyObject * args, PyObject * kwds) { PyObject * arg = 0; if (!PyArg_ParseTuple(args, "|O", &arg)) { return -1; } if (arg == 0) { return 0; } if (PyDict_Check(arg)) { MapType data; if (PyDictObject_asElement(arg, data) != 0) { PyErr_SetString(PyExc_TypeError, "Error converting dict to atlas"); return -1; } self->shape.s = Shape::newFromAtlas(data); if (self->shape.s == 0) { PyErr_SetString(PyExc_TypeError, "Error converting atlas to shape"); return -1; } return 0; } if (PyMessage_Check(arg)) { Element * data = ((PyMessage*)arg)->m_obj; if (!data->isMap()) { PyErr_SetString(PyExc_TypeError, "Error converting dict to atlas"); return -1; } self->shape.s = Shape::newFromAtlas(data->Map()); if (self->shape.s == 0) { PyErr_SetString(PyExc_TypeError, "Error converting atlas to shape"); return -1; } return 0; } PyErr_SetString(PyExc_TypeError, "Error converting unknown to shape"); return -1; } template