Atrinik Server  4.0
atrinik_region.c
Go to the documentation of this file.
1 /*************************************************************************
2  * Atrinik, a Multiplayer Online Role Playing Game *
3  * *
4  * Copyright (C) 2009-2014 Alex Tokar and Atrinik Development Team *
5  * *
6  * Fork from Crossfire (Multiplayer game for X-windows). *
7  * *
8  * This program is free software; you can redistribute it and/or modify *
9  * it under the terms of the GNU General Public License as published by *
10  * the Free Software Foundation; either version 2 of the License, or *
11  * (at your option) any later version. *
12  * *
13  * This program is distributed in the hope that it will be useful, *
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16  * GNU General Public License for more details. *
17  * *
18  * You should have received a copy of the GNU General Public License *
19  * along with this program; if not, write to the Free Software *
20  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
21  * *
22  * The author can be reached at admin@atrinik.org *
23  ************************************************************************/
24 
32 #include <plugin_python.h>
33 
37 static fields_struct fields[] = {
38  {"next", FIELDTYPE_REGION, offsetof(region_struct, next), 0, 0,
39  "Next region in a linked list.; Atrinik.Region.Region or None "
40  "(readonly)"},
41  {"parent", FIELDTYPE_REGION, offsetof(region_struct, parent), 0, 0,
42  "Region's parent.; Atrinik.Region.Region or None (readonly)"},
43  {"name", FIELDTYPE_CSTR, offsetof(region_struct, name), 0, 0,
44  "Name of the region (eg, 'world').; str (readonly)"},
45  {"longname", FIELDTYPE_CSTR, offsetof(region_struct, longname), 0, 0,
46  "Long name (eg, 'Strakewood Island').; str or None (readonly)"},
47  {"msg", FIELDTYPE_CSTR, offsetof(region_struct, msg), 0, 0,
48  "Description of the region.; str or None (readonly)"},
49  {"jailmap", FIELDTYPE_CSTR, offsetof(region_struct, jailmap), 0, 0,
50  "Path to the region's jail map.; str or None (readonly)"},
51  {"jailx", FIELDTYPE_INT16, offsetof(region_struct, jailx), 0, 0,
52  "X coordinate of the region's jail.; int (readonly)"},
53  {"jaily", FIELDTYPE_INT16, offsetof(region_struct, jaily), 0, 0,
54  "Y coordinate of the region's jail.; int (readonly)"},
55  {"map_first", FIELDTYPE_CSTR, offsetof(region_struct, map_first), 0, 0,
56  "Path to some beginning map in the region. Used for world maker "
57  "generation purposes.; str or None (readonly)"}
58 };
59 
69 static PyObject *Region_GetAttribute(Atrinik_Region *r, void *context)
70 {
71  return generic_field_getter(context, r->region);
72 }
73 
85 static PyObject *Atrinik_Region_new(PyTypeObject *type, PyObject *args,
86  PyObject *kwds)
87 {
88  Atrinik_Region *self = (Atrinik_Region *) type->tp_alloc(type, 0);
89  if (self != NULL) {
90  self->region = NULL;
91  }
92 
93  return (PyObject *) self;
94 }
95 
102 {
103  self->region = NULL;
104 #ifndef IS_PY_LEGACY
105  Py_TYPE(self)->tp_free((PyObject *) self);
106 #else
107  self->ob_type->tp_free((PyObject *) self);
108 #endif
109 }
110 
118 static PyObject *Atrinik_Region_str(Atrinik_Region *self)
119 {
120  return Py_BuildValue("s", self->region->name);
121 }
122 
123 static int Atrinik_Region_InternalCompare(Atrinik_Region *left,
124  Atrinik_Region *right)
125 {
126  return (left->region < right->region ? -1 :
127  (left->region == right->region ? 0 : 1));
128 }
129 
130 static PyObject *Atrinik_Region_RichCompare(Atrinik_Region *left,
131  Atrinik_Region *right, int op)
132 {
133  if (left == NULL || right == NULL ||
134  !PyObject_TypeCheck((PyObject *) left, &Atrinik_RegionType) ||
135  !PyObject_TypeCheck((PyObject *) right, &Atrinik_RegionType)) {
136  Py_INCREF(Py_NotImplemented);
137  return Py_NotImplemented;
138  }
139 
140  return generic_rich_compare(op,
141  Atrinik_Region_InternalCompare(left, right));
142 }
143 
147 static PyGetSetDef getseters[NUM_FIELDS + 1];
148 
150 PyTypeObject Atrinik_RegionType = {
151 #ifdef IS_PY3K
152  PyVarObject_HEAD_INIT(NULL, 0)
153 #else
154  PyObject_HEAD_INIT(NULL)
155  0,
156 #endif
157  "Atrinik.Region",
158  sizeof(Atrinik_Region),
159  0,
160  (destructor) Atrinik_Region_dealloc,
161  NULL, NULL, NULL,
162 #ifdef IS_PY3K
163  NULL,
164 #else
165  (cmpfunc) Atrinik_Region_InternalCompare,
166 #endif
167  0, 0, 0, 0, 0, 0,
168  (reprfunc) Atrinik_Region_str,
169  0, 0, 0,
170  Py_TPFLAGS_DEFAULT,
171  "Atrinik regions",
172  NULL, NULL,
173  (richcmpfunc) Atrinik_Region_RichCompare,
174  0, 0, 0,
175  NULL,
176  0,
177  getseters,
178  0, 0, 0, 0, 0, 0, 0,
180  0, 0, 0, 0, 0, 0, 0, 0
181 #ifndef IS_PY_LEGACY
182  , 0
183 #endif
184 #ifdef Py_TPFLAGS_HAVE_FINALIZE
185  , NULL
186 #endif
187 };
188 
196 int Atrinik_Region_init(PyObject *module)
197 {
198  size_t i;
199 
200  /* Field getters */
201  for (i = 0; i < NUM_FIELDS; i++) {
202  PyGetSetDef *def = &getseters[i];
203 
204  def->name = fields[i].name;
205  def->get = (getter) Region_GetAttribute;
206  def->set = NULL;
207  def->doc = fields[i].doc;
208  def->closure = &fields[i];
209  }
210 
211  getseters[i].name = NULL;
212 
213  Atrinik_RegionType.tp_new = PyType_GenericNew;
214 
215  if (PyType_Ready(&Atrinik_RegionType) < 0) {
216  return 0;
217  }
218 
219  Py_INCREF(&Atrinik_RegionType);
220  PyModule_AddObject(module, "Region", (PyObject *) &Atrinik_RegionType);
221 
222  return 1;
223 }
224 
232 PyObject *wrap_region(region_struct *what)
233 {
234  /* Return None if no region was to be wrapped. */
235  if (what == NULL) {
236  Py_INCREF(Py_None);
237  return Py_None;
238  }
239 
240  Atrinik_Region *wrapper = PyObject_NEW(Atrinik_Region, &Atrinik_RegionType);
241  if (wrapper != NULL) {
242  wrapper->region = what;
243  }
244 
245  return (PyObject *) wrapper;
246 }
static PyObject * Atrinik_Region_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
uint8_t type
One of operation types.
Definition: sound_ambient.c:45
PyObject * generic_rich_compare(int op, int result)
PyObject * wrap_region(region_struct *what)
#define IS_PY_LEGACY
Definition: plugin_python.h:55
int Atrinik_Region_init(PyObject *module)
struct sound_ambient_match * next
Next match rule in a linked list.
Definition: sound_ambient.c:39
static PyObject * Atrinik_Region_str(Atrinik_Region *self)
#define NUM_FIELDS
static PyObject * Region_GetAttribute(Atrinik_Region *r, void *context)
static PyGetSetDef getseters[NUM_FIELDS+1]
PyTypeObject Atrinik_RegionType
PyObject * generic_field_getter(fields_struct *field, void *ptr)
static fields_struct fields[]
PyObject_HEAD region_struct * region
static void Atrinik_Region_dealloc(Atrinik_Region *self)