1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
|
/* bzflag
* Copyright (c) 1993 - 2006 Tim Riker
*
* This package is free software; you can redistribute it and/or
* modify it under the terms of the license found in the file
* named COPYING that should have accompanied this file.
*
* THIS PACKAGE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*/
#include "bzfsAPI.h"
#include "PyBZFlag.h"
#include "PyPlayer.h"
namespace Python
{
static PyObject *DebugMessage (PyObject *self, PyObject *args);
static PyObject *FireWorldWeapon (PyObject *self, PyObject *args);
static PyObject *GetCurrentTime (PyObject *self, PyObject *args);
static PyObject *GetMaxWaitTime (PyObject *self, PyObject *args);
static PyObject *GetPublic (PyObject *self, PyObject *args);
static PyObject *GetPublicAddr (PyObject *self, PyObject *args);
static PyObject *GetPublicDescription (PyObject *self, PyObject *args);
static PyObject *GetStandardSpawn (PyObject *self, PyObject *args);
static PyObject *PlayClientSound (PyObject *self, PyObject *args);
static PyObject *SendTextMessage (PyObject *self, PyObject *args, PyObject *keywords);
static PyObject *SetMaxWaitTime (PyObject *self, PyObject *args);
static struct PyMethodDef methods[] =
{
// FIXME - docstrings
{"DebugMessage", (PyCFunction) DebugMessage, METH_VARARGS, NULL},
{"FireWorldWeapon", (PyCFunction) FireWorldWeapon, METH_VARARGS, NULL},
{"GetCurrentTime", (PyCFunction) GetCurrentTime, METH_NOARGS, NULL},
{"GetMaxWaitTime", (PyCFunction) GetMaxWaitTime, METH_NOARGS, NULL},
{"GetPublic", (PyCFunction) GetPublic, METH_NOARGS, NULL},
{"GetPublicAddr", (PyCFunction) GetPublicAddr, METH_NOARGS, NULL},
{"GetPublicDescription",(PyCFunction) GetPublicDescription, METH_NOARGS, NULL},
{"GetStandardSpawn", (PyCFunction) GetStandardSpawn, METH_VARARGS, NULL},
{"PlayClientSound", (PyCFunction) PlayClientSound, METH_VARARGS, NULL},
{"SendTextMessage", (PyCFunction) SendTextMessage, METH_VARARGS | METH_KEYWORDS, NULL},
{"SetMaxWaitTime", (PyCFunction) SetMaxWaitTime, METH_VARARGS, NULL},
{NULL, (PyCFunction) NULL, 0, NULL},
};
void
BZFlag::RegisterEvent (Handler *handler, bz_eEventType event)
{
handler->parent = this;
bz_registerEvent (event, handler);
PyDict_SetItem (event_listeners, PyInt_FromLong ((long) event), PyList_New (0));
}
BZFlag::BZFlag ()
{
module = Py_InitModule3 ("BZFlag", methods, NULL);
Py_INCREF (Py_None);
Py_INCREF (Py_False);
Py_INCREF (Py_True);
// Create and add submodules
event_sub = new Event ();
team_sub = new Team ();
bzdb = CreateBZDB ();
PyModule_AddObject (module, "Event", event_sub->GetSubModule ());
PyModule_AddObject (module, "Team", team_sub->GetSubModule ());
PyModule_AddObject (module, "BZDB", bzdb);
// Register event handlers
event_listeners = PyDict_New ();
RegisterEvent (&capture_handler, bz_eCaptureEvent);
RegisterEvent (&die_handler, bz_ePlayerDieEvent);
RegisterEvent (&spawn_handler, bz_ePlayerSpawnEvent);
RegisterEvent (&zone_entry_handler, bz_eZoneEntryEvent);
RegisterEvent (&zone_exit_handler, bz_eZoneExitEvent);
RegisterEvent (&join_handler, bz_ePlayerJoinEvent);
RegisterEvent (&part_handler, bz_ePlayerPartEvent);
RegisterEvent (&chat_handler, bz_eChatMessageEvent);
RegisterEvent (&unknownslash_handler, bz_eUnknownSlashCommand);
RegisterEvent (&getspawnpos_handler, bz_eGetPlayerSpawnPosEvent);
RegisterEvent (&getautoteam_handler, bz_eGetAutoTeamEvent);
RegisterEvent (&allowplayer_handler, bz_eAllowPlayer);
RegisterEvent (&tick_handler, bz_eTickEvent);
RegisterEvent (&generateworld_handler,bz_eGenerateWorldEvent);
RegisterEvent (&getplayerinfo_handler,bz_eGetPlayerInfoEvent);
PyModule_AddObject (module, "Events", event_listeners);
// Create the players list
players = PyDict_New ();
PyModule_AddObject (module, "Players", players);
}
PyObject *
BZFlag::GetListeners (int event)
{
PyObject *o = PyInt_FromLong (event);
PyObject *ret = PyDict_GetItem (event_listeners, o);
Py_DECREF (o);
return ret;
}
void
BZFlag::AddPlayer (int id)
{
PyObject *pyp = CreatePlayer (id);
PyDict_SetItem (players, PyInt_FromLong (id), pyp);
}
void
BZFlag::RemovePlayer (int id)
{
PyDict_DelItem (players, PyInt_FromLong (id));
}
static PyObject *
DebugMessage (PyObject *self, PyObject *args)
{
int level;
char *message;
if (!PyArg_ParseTuple (args, "is", &level, &message)) {
fprintf (stderr, "couldn't parse args\n");
// FIXME - throw error
return NULL;
}
bz_debugMessage (level, message);
return NULL;
}
static PyObject *
FireWorldWeapon (PyObject *self, PyObject *args)
{
printf ("FireWorldWeapon ()\n");
char *flag;
float lifetime;
int from_player;
return Py_None;
}
static PyObject *
GetCurrentTime (PyObject *self, PyObject *args)
{
printf ("GetCurrentTime ()\n");
return Py_None;
}
static PyObject *
GetMaxWaitTime (PyObject *self, PyObject *args)
{
return Py_BuildValue ("f", bz_getMaxWaitTime ());
}
static PyObject *
GetPublic (PyObject *self, PyObject *args)
{
return (bz_getPublic () ? Py_True : Py_False);
}
static PyObject *
GetPublicAddr (PyObject *self, PyObject *args)
{
return PyString_FromString (bz_getPublicAddr ().c_str ());
}
static PyObject *
GetPublicDescription (PyObject *self, PyObject *args)
{
return PyString_FromString (bz_getPublicDescription ().c_str ());
}
static PyObject *
GetStandardSpawn (PyObject *self, PyObject *args)
{
return Py_None;
}
static PyObject *
PlayClientSound (PyObject *self, PyObject *args)
{
return Py_None;
}
static PyObject *
SendTextMessage (PyObject *self, PyObject *args, PyObject *keywords)
{
int to, from;
char *message;
static char *kwlist[] = {"to", "from", "message", NULL};
if (!PyArg_ParseTupleAndKeywords (args, keywords, "iis", kwlist, &to, &from, &message)) {
fprintf (stderr, "couldn't parse args\n");
// FIXME - throw error
return NULL;
}
bool result = bz_sendTextMessage (to, from, message);
return (result ? Py_True : Py_False);
}
static PyObject *
SetMaxWaitTime (PyObject *self, PyObject *args)
{
float time;
if (!PyArg_ParseTuple (args, "f", &time)) {
fprintf (stderr, "couldn't parse args\n");
// FIXME - throw error
return NULL;
}
bz_setMaxWaitTime (time);
return Py_None;
}
};
// Local Variables: ***
// mode:C++ ***
// tab-width: 8 ***
// c-basic-offset: 2 ***
// indent-tabs-mode: t ***
// End: ***
// ex: shiftwidth=2 tabstop=8
|