cyphesis/client/Py_CreatorClient.cpp
Al Riddoch fac57eee73 2006-01-19 Al Riddoch <alriddoch@zepler.org>
* aiclient/ClientAccount.cpp, aiclient/ClientAccount.h,
	  aiclient/ClientConnection.cpp, aiclient/ClientConnection.h,
	  aiclient/client.cpp, aiclient/client.h, client/BaseClient.cpp,
	  client/BaseClient.h, client/CharacterClient.cpp,
	  client/CharacterClient.h, client/ClientConnection.cpp,
	  client/ClientConnection.h, client/CreatorClient.cpp,
	  client/CreatorClient.h, client/ObserverClient.cpp,
	  client/ObserverClient.h, client/Py_CreatorClient.cpp,
	  client/Py_CreatorClient.h, client/client.cpp,
	  client/define_world.cpp, client/define_world.h, common/Add.h,
	  common/Affect.h, common/AtlasFileLoader.cpp,
	  common/AtlasFileLoader.h, common/Attack.h, common/BaseEntity.cpp,
	  common/BaseEntity.h, common/BaseWorld.cpp, common/BaseWorld.h,
	  common/Burn.h, common/Chop.h, common/Connect.h, common/CustomOp.cpp,
	  common/CustomOp.h, common/CustomOp_impl.h, common/Cut.h,
	  common/Database.cpp, common/Database.h, common/Delve.h, common/Dig.h,
	  common/Eat.h, common/FormattedXMLWriter.cpp,
	  common/FormattedXMLWriter.h, common/Generic.h, common/Load.h,
	  common/Monitor.h, common/Mow.h, common/Nourish.h,
	  common/OOGThing.cpp, common/OOGThing.h, common/Property.cpp,
	  common/Property.h, common/Property_impl.h, common/Save.h,
	  common/Setup.h, common/Tick.h, common/Unseen.h, common/Update.h,
	  common/Use.h, common/Wield.h, common/accountbase.cpp,
	  common/accountbase.h, common/const.cpp, common/const.h,
	  common/custom.cpp, common/debug.h, common/globals.cpp,
	  common/globals.h, common/id.cpp, common/id.h, common/inheritance.cpp,
	  common/inheritance.h, common/inheritance_impl.h, common/log.cpp,
	  common/log.h, common/newid.cpp, common/nls.h, common/op_switch.h,
	  common/operations.cpp, common/operations.h, common/random.h,
	  common/refno.h, common/serialno.cpp, common/serialno.h,
	  common/system.cpp, common/system.h, common/terrain_utils.cpp,
	  common/terrain_utils.h, common/type_utils.h, common/types.h,
	  common/utility.cpp, common/utility.h, modules/DateTime.cpp,
	  modules/DateTime.h, modules/Location.cpp, modules/Location.h,
	  modules/WorldTime.cpp, modules/WorldTime.h, physics/BBox.cpp,
	  physics/BBox.h, physics/Collision.cpp, physics/Collision.h,
	  physics/Quaternion.cpp, physics/Quaternion.h, physics/Vector3D.cpp,
	  physics/Vector3D.h, physics/VectorPol.h, rulesets/Area.cpp,
	  rulesets/Area.h, rulesets/AreaProperty.cpp, rulesets/AreaProperty.h,
	  rulesets/ArithmeticScript.cpp, rulesets/ArithmeticScript.h,
	  rulesets/BaseMind.cpp, rulesets/BaseMind.h,
	  rulesets/CalendarProperty.cpp, rulesets/CalendarProperty.h,
	  rulesets/Character.cpp, rulesets/Character.h, rulesets/Combat.cpp,
	  rulesets/Combat.h, rulesets/Container.cpp, rulesets/Container.h,
	  rulesets/Creator.cpp, rulesets/Creator.h, rulesets/Entity.cpp,
	  rulesets/Entity.h, rulesets/EntityProperties.cpp,
	  rulesets/Entity_getLocation.h, rulesets/Fell.cpp, rulesets/Fell.h,
	  rulesets/Food.cpp, rulesets/Food.h, rulesets/Line.cpp,
	  rulesets/Line.h, rulesets/LineProperty.cpp, rulesets/LineProperty.h,
	  rulesets/MemEntity.cpp, rulesets/MemEntity.h, rulesets/MemMap.cpp,
	  rulesets/MemMap.h, rulesets/MemMap_methods.h,
	  rulesets/MindFactory.cpp, rulesets/MindFactory.h, rulesets/Missile.h,
	  rulesets/Motion.cpp, rulesets/Motion.h, rulesets/Movement.cpp,
	  rulesets/Movement.h, rulesets/Pedestrian.cpp, rulesets/Pedestrian.h,
	  rulesets/Plant.cpp, rulesets/Plant.h, rulesets/Py_BBox.cpp,
	  rulesets/Py_BBox.h, rulesets/Py_EntityWrapper.h,
	  rulesets/Py_Location.cpp, rulesets/Py_Location.h,
	  rulesets/Py_Map.cpp, rulesets/Py_Map.h, rulesets/Py_Mind.cpp,
	  rulesets/Py_Mind.h, rulesets/Py_Object.cpp, rulesets/Py_Object.h,
	  rulesets/Py_Operation.cpp, rulesets/Py_Operation.h,
	  rulesets/Py_Oplist.cpp, rulesets/Py_Oplist.h, rulesets/Py_Optime.cpp,
	  rulesets/Py_Optime.h, rulesets/Py_Point3D.cpp, rulesets/Py_Point3D.h,
	  rulesets/Py_Property.cpp, rulesets/Py_Property.h,
	  rulesets/Py_Quaternion.cpp, rulesets/Py_Quaternion.h,
	  rulesets/Py_RootEntity.cpp, rulesets/Py_RootEntity.h,
	  rulesets/Py_Statistics.cpp, rulesets/Py_Statistics.h,
	  rulesets/Py_Task.cpp, rulesets/Py_Task.h, rulesets/Py_Thing.cpp,
	  rulesets/Py_Thing.h, rulesets/Py_Vector3D.cpp,
	  rulesets/Py_Vector3D.h, rulesets/Py_World.cpp, rulesets/Py_World.h,
	  rulesets/Py_WorldTime.cpp, rulesets/Py_WorldTime.h,
	  rulesets/PythonArithmeticScript.cpp,
	  rulesets/PythonArithmeticScript.h, rulesets/PythonMindScript.cpp,
	  rulesets/PythonMindScript.h, rulesets/PythonScript.cpp,
	  rulesets/PythonScript.h, rulesets/PythonThingScript.cpp,
	  rulesets/PythonThingScript.h, rulesets/PythonWrapper.cpp,
	  rulesets/PythonWrapper.h, rulesets/Python_API.cpp,
	  rulesets/Python_API.h, rulesets/Python_Script_Utils.h,
	  rulesets/Script.cpp, rulesets/Script.h, rulesets/Stackable.cpp,
	  rulesets/Stackable.h, rulesets/Statistics.cpp, rulesets/Statistics.h,
	  rulesets/StatisticsProperty.cpp, rulesets/StatisticsProperty.h,
	  rulesets/Structure.cpp, rulesets/Structure.h, rulesets/Task.cpp,
	  rulesets/Task.h, rulesets/TaskScript.cpp, rulesets/TaskScript.h,
	  rulesets/TerrainProperty.cpp, rulesets/TerrainProperty.h,
	  rulesets/Thing.cpp, rulesets/Thing.h, rulesets/ThingFactory.cpp,
	  rulesets/ThingFactory.h, rulesets/World.cpp, rulesets/World.h,
	  rulesets/attributes.h, server/Account.cpp, server/Account.h,
	  server/Admin.cpp, server/Admin.h, server/ArithmeticFactory.cpp,
	  server/ArithmeticFactory.h, server/CommClient.cpp,
	  server/CommClient.h, server/CommIdleSocket.cpp,
	  server/CommIdleSocket.h, server/CommListener.cpp,
	  server/CommListener.h, server/CommLocalClient.cpp,
	  server/CommLocalClient.h, server/CommMDNSPublisher.cpp,
	  server/CommMDNSPublisher.h, server/CommMaster.cpp,
	  server/CommMaster.h, server/CommMetaClient.cpp,
	  server/CommMetaClient.h, server/CommPSQLSocket.cpp,
	  server/CommPSQLSocket.h, server/CommPeer.cpp,
	  server/CommPeer.h, server/CommPeerListener.cpp,
	  server/CommPeerListener.h, server/CommRemoteClient.cpp,
	  server/CommRemoteClient.h, server/CommServer.cpp,
	  server/CommServer.h, server/CommSlaveClient.cpp,
	  server/CommSlaveClient.h, server/CommSlaveListener.cpp,
	  server/CommSlaveListener.h, server/CommSocket.cpp,
	  server/CommSocket.h, server/CommUnixListener.cpp,
	  server/CommUnixListener.h, server/Connection.cpp,
	  server/Connection.h, server/Connection_methods.h,
	  server/EntityFactory.cpp, server/EntityFactory.h,
	  server/ExternalMind.cpp, server/ExternalMind.h,
	  server/Idle.cpp, server/Idle.h, server/Lobby.cpp, server/Lobby.h,
	  server/Master.cpp, server/Master.h, server/Peer.cpp, server/Peer.h,
	  server/Persistance.cpp, server/Persistance.h,
	  server/PersistantThingFactory.cpp, server/PersistantThingFactory.h,
	  server/PersistantThingFactory_impl.h, server/Persistor.cpp,
	  server/Persistor.h, server/Persistor_impl.h,
	  server/Player.cpp, server/Player.h, server/Restoration.cpp,
	  server/Restoration.h, server/Restorer.cpp, server/Restorer.h,
	  server/Restorer_impl.h, server/ScriptFactory.cpp,
	  server/ScriptFactory.h, server/ServerRouting.cpp,
	  server/ServerRouting.h, server/ServerRouting_methods.h,
	  server/SlaveClientConnection.cpp, server/SlaveClientConnection.h,
	  server/TaskFactory.cpp, server/TaskFactory.h,
	  server/TrustedConnection.cpp, server/TrustedConnection.h,
	  server/WorldRouter.cpp, server/WorldRouter.h,
	  server/server.cpp, server/server.h, server/slave.cpp,
	  tests/Areatest.cpp, tests/BaseEntitytest.cpp,
	  tests/Charactertest.cpp, tests/Containertest.cpp,
	  tests/Creatortest.cpp, tests/EntityExerciser.h,
	  tests/EntityFactorytest.cpp, tests/Entitytest.cpp,
	  tests/Foodtest.cpp, tests/IGEntityExerciser.h,
	  tests/Linetest.cpp, tests/Locationtest.cpp, tests/OOGThingtest.cpp,
	  tests/Planttest.cpp, tests/Propertytest.cpp, tests/Stackabletest.cpp,
	  tests/Structuretest.cpp, tests/Tasktest.cpp, tests/TestWorld.h,
	  tests/Thingtest.cpp, tests/WorldTimetest.cpp, tests/Worldtest.cpp,
	  tests/allOperations.h, tests/collisiontest.cpp, tests/consttest.cpp,
	  tests/distancetest.cpp, tests/emergencetest.cpp,
	  tests/inheritancetest.cpp, tests/logtest.cpp, tests/randomtest.cpp,
	  tests/systemtest.cpp, tests/transformtest.cpp,
	  tests/type_utilstest.cpp, tests/utilitytest.cpp,
	  tools/AdminClient.cpp, tools/AdminClient.h, tools/cyaddrules.cpp,
	  tools/cycmd.cpp, tools/cyconfig.cpp, tools/cyconvertrules.cpp,
	  tools/cyctrl.cpp, tools/cydbload.cpp, tools/cydumprules.cpp,
	  tools/cyisoload.cpp, tools/cyloadrules.cpp, tools/cypasswd.cpp,
	  tools/cywatchdog.cpp: Replace the copyright and license statements
	  with new versions that include disclaimer of warranty, and
	  permit distribution under later versions of the GPL.
2006-01-19 02:17:06 +00:00

374 lines
12 KiB
C++

// Cyphesis Online RPG Server and AI Engine
// Copyright (C) 2000 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_CreatorClient.h"
#include "CreatorClient.h"
#include "rulesets/Py_Operation.h"
#include "rulesets/Py_RootEntity.h"
#include "rulesets/Py_WorldTime.h"
#include "rulesets/Py_Point3D.h"
#include "rulesets/Py_Location.h"
#include "rulesets/Py_Object.h"
#include "rulesets/Py_Thing.h"
#include "rulesets/Py_Map.h"
#include "common/debug.h"
using Atlas::Message::Element;
using Atlas::Message::MapType;
static const bool debug_flag = false;
static PyObject * CreatorClient_as_entity(PyCreatorClient * self, PyObject * args)
{
#ifndef NDEBUG
if (self->m_mind == NULL) {
PyErr_SetString(PyExc_AssertionError, "NULL CreatorClient in CreatorClient.as_entity");
return NULL;
}
#endif // NDEBUG
if (!PyArg_ParseTuple(args, "")) {
return NULL;
}
PyMessageElement * ret = newPyMessageElement();
if (ret == NULL) {
return NULL;
}
ret->m_obj = new Element(MapType());
self->m_mind->addToMessage(ret->m_obj->asMap());
return (PyObject *)ret;
}
static PyObject * CreatorClient_make(PyCreatorClient * self, PyObject * args)
{
#ifndef NDEBUG
if (self->m_mind == NULL) {
PyErr_SetString(PyExc_AssertionError, "NULL CreatorClient in CreatorClient.make");
return NULL;
}
#endif // NDEBUG
PyRootEntity * entity = NULL;
if (!PyArg_ParseTuple(args, "O", &entity)) {
return NULL;
}
if (!PyRootEntity_Check(entity)) {
PyErr_SetString(PyExc_TypeError, "Can only make Atlas entity");
return NULL;
}
Entity * retval = self->m_mind->make(entity->entity);
if (retval == NULL) {
PyErr_SetString(PyExc_RuntimeError, "Entity creation failed");
return NULL;
}
PyEntity * ret = newPyEntity();
ret->m_entity = retval;
return (PyObject *)ret;
}
static PyObject * CreatorClient_set(PyCreatorClient * self, PyObject * args)
{
#ifndef NDEBUG
if (self->m_mind == NULL) {
PyErr_SetString(PyExc_AssertionError, "NULL CreatorClient in CreatorClient.set");
return NULL;
}
#endif // NDEBUG
PyRootEntity * entity = NULL;
char * id = NULL;
if (!PyArg_ParseTuple(args, "sO", &id, &entity)) {
return NULL;
}
if (!PyRootEntity_Check(entity)) {
PyErr_SetString(PyExc_TypeError, "Can only set Atlas entity");
return NULL;
}
self->m_mind->sendSet(id, entity->entity);
Py_INCREF(Py_None);
return Py_None;
}
static PyObject * CreatorClient_look(PyCreatorClient * self, PyObject * args)
{
#ifndef NDEBUG
if (self->m_mind == NULL) {
PyErr_SetString(PyExc_AssertionError, "NULL CreatorClient in CreatorClient.look");
return NULL;
}
#endif // NDEBUG
char * id = NULL;
if (!PyArg_ParseTuple(args, "s", &id)) {
return NULL;
}
Entity * retval = self->m_mind->look(id);
if (retval == NULL) {
PyErr_SetString(PyExc_RuntimeError, "Entity look failed");
return NULL;
}
PyEntity * ret = newPyEntity();
ret->m_entity = retval;
return (PyObject *)ret;
}
static PyObject * CreatorClient_look_for(PyCreatorClient * self,
PyObject * args)
{
#ifndef NDEBUG
if (self->m_mind == NULL) {
PyErr_SetString(PyExc_AssertionError, "NULL CreatorClient in CreatorClient.look_for");
return NULL;
}
#endif // NDEBUG
PyRootEntity * ent;
if (!PyArg_ParseTuple(args, "O", &ent)) {
return NULL;
}
if (!PyRootEntity_Check(ent)) {
PyErr_SetString(PyExc_TypeError, "Can only look for Atlas description");
return NULL;
}
Entity * retval = self->m_mind->lookFor(ent->entity);
if (retval == NULL) {
PyErr_SetString(PyExc_RuntimeError, "Entity look_for failed");
return NULL;
}
PyEntity * ret = newPyEntity();
ret->m_entity = retval;
return (PyObject *)ret;
}
static PyObject * CreatorClient_send(PyCreatorClient * self, PyObject * args)
{
#ifndef NDEBUG
if (self->m_mind == NULL) {
PyErr_SetString(PyExc_AssertionError, "NULL CreatorClient in CreatorClient.send");
return NULL;
}
#endif // NDEBUG
PyOperation * op;
if (!PyArg_ParseTuple(args, "O", &op)) {
return NULL;
}
if (!PyOperation_Check(op)) {
PyErr_SetString(PyExc_TypeError, "Can only send Atlas operation");
return NULL;
}
self->m_mind->send(op->operation);
Py_INCREF(Py_None);
return Py_None;
}
static PyMethodDef CreatorClient_methods[] = {
{"as_entity", (PyCFunction)CreatorClient_as_entity, METH_NOARGS},
{"make", (PyCFunction)CreatorClient_make, METH_VARARGS},
{"set", (PyCFunction)CreatorClient_set, METH_VARARGS},
{"look", (PyCFunction)CreatorClient_look, METH_VARARGS},
{"look_for", (PyCFunction)CreatorClient_look_for, METH_VARARGS},
{"send", (PyCFunction)CreatorClient_send, METH_VARARGS},
{NULL, NULL} /* sentinel */
};
static void CreatorClient_dealloc(PyCreatorClient *self)
{
//if (self->m_mind != NULL) {
//delete self->m_mind;
//}
Py_XDECREF(self->CreatorClient_attr);
PyMem_DEL(self);
}
static PyObject * CreatorClient_getattr(PyCreatorClient *self, char *name)
{
// Fairly major re-write of this to use operator[] of CreatorClient base class
#ifndef NDEBUG
if (self->m_mind == NULL) {
PyErr_SetString(PyExc_AssertionError, "NULL CreatorClient in CreatorClient.getattr");
return NULL;
}
#endif // NDEBUG
// If operation search gets to here, it goes no further
if (strstr(name, "_operation") != NULL) {
PyErr_SetString(PyExc_AttributeError, name);
return NULL;
}
if (strcmp(name, "type") == 0) {
PyObject * list = PyList_New(0);
if (list == NULL) {
return NULL;
}
PyList_Append(list, PyString_FromString(self->m_mind->getType().c_str()));
return list;
}
if (strcmp(name, "map") == 0) {
PyMap * map = newPyMap();
map->m_map = self->m_mind->getMap();
return (PyObject *)map;
}
if (strcmp(name, "location") == 0) {
PyLocation * loc = newPyLocation();
loc->location = &self->m_mind->m_location;
loc->owner = self->m_mind;
return (PyObject *)loc;
}
if (strcmp(name, "time") == 0) {
PyWorldTime * worldtime = newPyWorldTime();
worldtime->time = self->m_mind->getTime();
return (PyObject *)worldtime;
}
if (self->CreatorClient_attr != NULL) {
PyObject *v = PyDict_GetItemString(self->CreatorClient_attr, name);
if (v != NULL) {
Py_INCREF(v);
return v;
}
}
Entity * thing = self->m_mind;
Element attr;
if (!thing->get(name, attr)) {
return Py_FindMethod(CreatorClient_methods, (PyObject *)self, name);
}
PyObject * ret = MessageElement_asPyObject(attr);
if (ret == NULL) {
return Py_FindMethod(CreatorClient_methods, (PyObject *)self, name);
}
return ret;
}
static int CreatorClient_setattr(PyCreatorClient *self, char *name, PyObject *v)
{
if (self->m_mind == NULL) {
return -1;
}
if (self->CreatorClient_attr == NULL) {
self->CreatorClient_attr = PyDict_New();
if (self->CreatorClient_attr == NULL) {
return -1;
}
}
if (strcmp(name, "status") == 0) {
// This needs to be here until we can sort the difference
// between floats and ints in python.
if (PyInt_Check(v)) {
self->m_mind->setStatus((double)PyInt_AsLong(v));
} else if (PyFloat_Check(v)) {
self->m_mind->setStatus(PyFloat_AsDouble(v));
} else {
PyErr_SetString(PyExc_TypeError, "status must be numeric type");
return -1;
}
return 0;
}
if (strcmp(name, "map") == 0) {
return -1;
}
Entity * thing = self->m_mind;
//std::string attr(name);
//if (v == NULL) {
//thing->attributes.erase(attr);
//return 0;
//}
Element obj = PyObject_asMessageElement(v);
if (!obj.isNone() && !obj.isMap() && !obj.isList()) {
thing->set(name, obj);
return 0;
}
// If we get here, then the attribute is not Atlas compatable, so we
// need to store it in a python dictionary
return PyDict_SetItemString(self->CreatorClient_attr, name, v);
}
static int CreatorClient_compare(PyCreatorClient *self, PyCreatorClient *other)
{
if ((self->m_mind == NULL) || (other->m_mind == NULL)) {
return -1;
}
return (self->m_mind == other->m_mind) ? 0 : 1;
}
PyTypeObject PyCreatorClient_Type = {
PyObject_HEAD_INIT(&PyType_Type)
0, /*ob_size*/
"cppCreatorClient", /*tp_name*/
sizeof(PyCreatorClient), /*tp_basicsize*/
0, /*tp_itemsize*/
/* methods */
(destructor)CreatorClient_dealloc, /*tp_dealloc*/
0, /*tp_print*/
(getattrfunc)CreatorClient_getattr, /*tp_getattr*/
(setattrfunc)CreatorClient_setattr, /*tp_setattr*/
(cmpfunc)CreatorClient_compare, /*tp_compare*/
0, /*tp_repr*/
0, /*tp_as_number*/
0, /*tp_as_sequence*/
0, /*tp_as_mapping*/
0, /*tp_hash*/
};
PyCreatorClient * newPyCreatorClient()
{
PyCreatorClient * self;
self = PyObject_NEW(PyCreatorClient, &PyCreatorClient_Type);
if (self == NULL) {
return NULL;
}
self->CreatorClient_attr = NULL;
return self;
}
int runClientScript(CreatorClient * c, const std::string & package,
const std::string & func)
{
PyObject * package_name = PyString_FromString(package.c_str());
PyObject * mod_dict = PyImport_Import(package_name);
Py_DECREF(package_name);
if (mod_dict == NULL) {
std::cerr << "Cld not find python module " << package
<< std::endl << std::flush;
PyErr_Print();
return -1;
}
PyObject * function = PyObject_GetAttrString(mod_dict,
(char *)func.c_str());
Py_DECREF(mod_dict);
if (function == NULL) {
std::cerr << "Could not find " << func << " function" << std::endl
<< std::flush;
PyErr_Print();
return -1;
}
if (PyCallable_Check(function) == 0) {
std::cerr << "It does not seem to be a function at all" << std::endl
<< std::flush;
Py_DECREF(function);
return -1;
}
PyCreatorClient * editor = newPyCreatorClient();
editor->m_mind = c;
PyObject * pyob = PyEval_CallFunction(function, "(O)", editor);
if (pyob == NULL) {
if (PyErr_Occurred() == NULL) {
std::cerr << "Could not call function" << std::endl << std::flush;
} else {
std::cerr << "Reporting python error" << std::endl << std::flush;
PyErr_Print();
}
}
Py_DECREF(function);
return 0;
}