Atrinik Server 2.5
plugins/plugin_python/atrinik_region.c
Go to the documentation of this file.
00001 /************************************************************************
00002 *            Atrinik, a Multiplayer Online Role Playing Game            *
00003 *                                                                       *
00004 *    Copyright (C) 2009-2011 Alex Tokar and Atrinik Development Team    *
00005 *                                                                       *
00006 * Fork from Daimonin (Massive Multiplayer Online Role Playing Game)     *
00007 * and Crossfire (Multiplayer game for X-windows).                       *
00008 *                                                                       *
00009 * This program is free software; you can redistribute it and/or modify  *
00010 * it under the terms of the GNU General Public License as published by  *
00011 * the Free Software Foundation; either version 2 of the License, or     *
00012 * (at your option) any later version.                                   *
00013 *                                                                       *
00014 * This program is distributed in the hope that it will be useful,       *
00015 * but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00017 * GNU General Public License for more details.                          *
00018 *                                                                       *
00019 * You should have received a copy of the GNU General Public License     *
00020 * along with this program; if not, write to the Free Software           *
00021 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.             *
00022 *                                                                       *
00023 * The author can be reached at admin@atrinik.org                        *
00024 ************************************************************************/
00025 
00030 #include <plugin_python.h>
00031 
00034 /* @cparser
00035  * @page plugin_python_region_fields Python region fields
00036  * <h2>Python region fields</h2>
00037  * List of the region fields and their meaning. */
00038 static fields_struct fields[] =
00039 {
00040     {"next", FIELDTYPE_REGION, offsetof(region, next), 0, 0},
00041     {"parent", FIELDTYPE_REGION, offsetof(region, parent), 0, 0},
00042     {"name", FIELDTYPE_CSTR, offsetof(region, name), 0, 0},
00043     {"longname", FIELDTYPE_CSTR, offsetof(region, longname), 0, 0},
00044     {"msg", FIELDTYPE_CSTR, offsetof(region, msg), 0, 0},
00045     {"jailmap", FIELDTYPE_CSTR, offsetof(region, jailmap), 0, 0},
00046     {"jailx", FIELDTYPE_SINT16, offsetof(region, jailx), 0, 0},
00047     {"jaily", FIELDTYPE_SINT16, offsetof(region, jaily), 0, 0}
00048 };
00049 /* @endcparser */
00050 
00056 static PyObject *Region_GetAttribute(Atrinik_Region *r, void *context)
00057 {
00058     return generic_field_getter((fields_struct *) context, r->region);
00059 }
00060 
00067 static PyObject *Atrinik_Region_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
00068 {
00069     Atrinik_Region *self;
00070 
00071     (void) args;
00072     (void) kwds;
00073 
00074     self = (Atrinik_Region *) type->tp_alloc(type, 0);
00075 
00076     if (self)
00077     {
00078         self->region = NULL;
00079     }
00080 
00081     return (PyObject *) self;
00082 }
00083 
00087 static void Atrinik_Region_dealloc(Atrinik_Region *self)
00088 {
00089     self->region = NULL;
00090 #ifndef IS_PY_LEGACY
00091     Py_TYPE(self)->tp_free((PyObject *) self);
00092 #else
00093     self->ob_type->tp_free((PyObject *) self);
00094 #endif
00095 }
00096 
00101 static PyObject *Atrinik_Region_str(Atrinik_Region *self)
00102 {
00103     return Py_BuildValue("s", self->region->name);
00104 }
00105 
00106 static int Atrinik_Region_InternalCompare(Atrinik_Region *left, Atrinik_Region *right)
00107 {
00108     return (left->region < right->region ? -1 : (left->region == right->region ? 0 : 1));
00109 }
00110 
00111 static PyObject *Atrinik_Region_RichCompare(Atrinik_Region *left, Atrinik_Region *right, int op)
00112 {
00113     if (!left || !right || !PyObject_TypeCheck((PyObject *) left, &Atrinik_RegionType) || !PyObject_TypeCheck((PyObject *) right, &Atrinik_RegionType))
00114     {
00115         Py_INCREF(Py_NotImplemented);
00116         return Py_NotImplemented;
00117     }
00118 
00119     return generic_rich_compare(op, Atrinik_Region_InternalCompare(left, right));
00120 }
00121 
00124 static PyGetSetDef getseters[NUM_FIELDS + 1];
00125 
00127 PyTypeObject Atrinik_RegionType =
00128 {
00129 #ifdef IS_PY3K
00130     PyVarObject_HEAD_INIT(NULL, 0)
00131 #else
00132     PyObject_HEAD_INIT(NULL)
00133     0,
00134 #endif
00135     "Atrinik.Region",
00136     sizeof(Atrinik_Region),
00137     0,
00138     (destructor) Atrinik_Region_dealloc,
00139     NULL, NULL, NULL,
00140 #ifdef IS_PY3K
00141     NULL,
00142 #else
00143     (cmpfunc) Atrinik_Region_InternalCompare,
00144 #endif
00145     0, 0, 0, 0, 0, 0,
00146     (reprfunc) Atrinik_Region_str,
00147     0, 0, 0,
00148     Py_TPFLAGS_DEFAULT,
00149     "Atrinik regions",
00150     NULL, NULL,
00151     (richcmpfunc) Atrinik_Region_RichCompare,
00152     0, 0, 0,
00153     NULL,
00154     0,
00155     getseters,
00156     0, 0, 0, 0, 0, 0, 0,
00157     Atrinik_Region_new,
00158     0, 0, 0, 0, 0, 0, 0, 0
00159 #ifndef IS_PY_LEGACY
00160     , 0
00161 #endif
00162 };
00163 
00168 int Atrinik_Region_init(PyObject *module)
00169 {
00170     size_t i;
00171 
00172     /* Field getters */
00173     for (i = 0; i < NUM_FIELDS; i++)
00174     {
00175         PyGetSetDef *def = &getseters[i];
00176 
00177         def->name = fields[i].name;
00178         def->get = (getter) Region_GetAttribute;
00179         def->set = NULL;
00180         def->doc = NULL;
00181         def->closure = (void *) &fields[i];
00182     }
00183 
00184     getseters[i].name = NULL;
00185 
00186     Atrinik_RegionType.tp_new = PyType_GenericNew;
00187 
00188     if (PyType_Ready(&Atrinik_RegionType) < 0)
00189     {
00190         return 0;
00191     }
00192 
00193     Py_INCREF(&Atrinik_RegionType);
00194     PyModule_AddObject(module, "Region", (PyObject *) &Atrinik_RegionType);
00195 
00196     return 1;
00197 }
00198 
00203 PyObject *wrap_region(region *what)
00204 {
00205     Atrinik_Region *wrapper;
00206 
00207     /* Return None if no region was to be wrapped. */
00208     if (!what)
00209     {
00210         Py_INCREF(Py_None);
00211         return Py_None;
00212     }
00213 
00214     wrapper = PyObject_NEW(Atrinik_Region, &Atrinik_RegionType);
00215 
00216     if (wrapper)
00217     {
00218         wrapper->region = what;
00219     }
00220 
00221     return (PyObject *) wrapper;
00222 }