2009-11-17 00:54:03 +00:00
|
|
|
// Cyphesis Online RPG Server and AI Engine
|
|
|
|
|
// Copyright (C) 2009 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
|
|
|
|
|
|
|
|
|
|
|
2011-02-15 09:30:46 +00:00
|
|
|
#ifdef NDEBUG
|
|
|
|
|
#undef NDEBUG
|
2011-02-16 09:00:40 +00:00
|
|
|
#else
|
|
|
|
|
#define CYPHESIS_DEBUG
|
2011-02-15 09:30:46 +00:00
|
|
|
#endif
|
|
|
|
|
#ifndef DEBUG
|
|
|
|
|
#define DEBUG
|
|
|
|
|
#endif
|
|
|
|
|
|
2009-11-17 00:54:03 +00:00
|
|
|
#include <Python.h>
|
|
|
|
|
|
2009-11-20 20:18:53 +00:00
|
|
|
#include "python_testers.h"
|
|
|
|
|
|
2009-11-17 00:54:03 +00:00
|
|
|
#include "rulesets/Python_API.h"
|
|
|
|
|
#include "rulesets/Py_Thing.h"
|
|
|
|
|
#include "rulesets/Py_Property.h"
|
|
|
|
|
#include "rulesets/Entity.h"
|
|
|
|
|
#include "rulesets/TerrainProperty.h"
|
|
|
|
|
|
|
|
|
|
#include <cassert>
|
|
|
|
|
|
|
|
|
|
static PyObject * add_properties(PyObject * self, PyEntity * o)
|
|
|
|
|
{
|
2011-12-13 19:39:09 +00:00
|
|
|
if (!PyEntity_Check(o)) {
|
2009-11-17 00:54:03 +00:00
|
|
|
PyErr_SetString(PyExc_TypeError, "Unknown Object type");
|
2018-01-30 16:54:49 +01:00
|
|
|
return nullptr;
|
2009-11-17 00:54:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Entity * ent = o->m_entity.e;
|
|
|
|
|
|
|
|
|
|
PropertyBase * p = ent->setProperty("terrain", new TerrainProperty);
|
2013-02-13 00:45:36 +00:00
|
|
|
p->install(ent, "terrain");
|
2009-11-17 00:54:03 +00:00
|
|
|
p->apply(ent);
|
2016-07-19 20:44:03 +02:00
|
|
|
ent->propertyApplied("terrain", *p);
|
2009-11-17 00:54:03 +00:00
|
|
|
|
|
|
|
|
Py_INCREF(Py_None);
|
|
|
|
|
return Py_None;
|
|
|
|
|
}
|
|
|
|
|
|
2011-12-27 16:23:22 +01:00
|
|
|
static PyObject * null_wrapper(PyObject * self, PyProperty * o)
|
2009-11-17 00:54:03 +00:00
|
|
|
{
|
|
|
|
|
if (PyTerrainProperty_Check(o)) {
|
2011-02-16 09:00:40 +00:00
|
|
|
#ifdef CYPHESIS_DEBUG
|
2018-01-30 16:54:49 +01:00
|
|
|
o->m_p.base = nullptr;
|
2009-11-17 00:54:03 +00:00
|
|
|
#endif // NDEBUG
|
|
|
|
|
} else {
|
|
|
|
|
PyErr_SetString(PyExc_TypeError, "Unknown Object type");
|
2018-01-30 16:54:49 +01:00
|
|
|
return nullptr;
|
2009-11-17 00:54:03 +00:00
|
|
|
}
|
|
|
|
|
Py_INCREF(Py_None);
|
|
|
|
|
return Py_None;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static PyMethodDef testprop_methods[] = {
|
|
|
|
|
{"add_properties", (PyCFunction)add_properties, METH_O},
|
2018-01-30 16:54:49 +01:00
|
|
|
{nullptr, nullptr} /* Sentinel */
|
2009-11-17 00:54:03 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static PyMethodDef sabotage_methods[] = {
|
|
|
|
|
{"null", (PyCFunction)null_wrapper, METH_O},
|
2018-01-30 16:54:49 +01:00
|
|
|
{nullptr, nullptr} /* Sentinel */
|
2009-11-17 00:54:03 +00:00
|
|
|
};
|
|
|
|
|
|
2018-05-04 19:17:55 +02:00
|
|
|
static PyObject* init_sabotage() {
|
|
|
|
|
static struct PyModuleDef def = {
|
|
|
|
|
PyModuleDef_HEAD_INIT,
|
|
|
|
|
"sabotage",
|
|
|
|
|
nullptr,
|
|
|
|
|
0,
|
|
|
|
|
sabotage_methods,
|
|
|
|
|
nullptr,
|
|
|
|
|
nullptr,
|
|
|
|
|
nullptr,
|
|
|
|
|
nullptr
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return PyModule_Create(&def);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static PyObject* init_testprop() {
|
|
|
|
|
static struct PyModuleDef def = {
|
|
|
|
|
PyModuleDef_HEAD_INIT,
|
|
|
|
|
"testprop",
|
|
|
|
|
nullptr,
|
|
|
|
|
0,
|
|
|
|
|
testprop_methods,
|
|
|
|
|
nullptr,
|
|
|
|
|
nullptr,
|
|
|
|
|
nullptr,
|
|
|
|
|
nullptr
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return PyModule_Create(&def);
|
2009-11-17 00:54:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int main()
|
|
|
|
|
{
|
2018-05-04 19:17:55 +02:00
|
|
|
PyImport_AppendInittab("sabotage", &init_sabotage);
|
|
|
|
|
PyImport_AppendInittab("testprop", &init_testprop);
|
2012-01-19 19:34:20 +00:00
|
|
|
init_python_api("bac81904-0516-4dd0-b9d8-32e879339b96");
|
2009-11-17 00:54:03 +00:00
|
|
|
|
2009-11-20 20:18:53 +00:00
|
|
|
run_python_string("from server import *");
|
|
|
|
|
run_python_string("import testprop");
|
|
|
|
|
run_python_string("t=Thing('1')");
|
2012-01-04 16:31:23 +00:00
|
|
|
expect_python_error("t.terrain", PyExc_AttributeError);
|
2009-11-20 20:18:53 +00:00
|
|
|
run_python_string("testprop.add_properties(t)");
|
2018-05-12 00:42:43 +02:00
|
|
|
run_python_string("terrain = t.props.terrain");
|
2012-01-04 16:31:23 +00:00
|
|
|
expect_python_error("terrain.foo = 1", PyExc_AttributeError);
|
|
|
|
|
expect_python_error("terrain.get_height()", PyExc_TypeError);
|
2009-11-20 20:18:53 +00:00
|
|
|
run_python_string("terrain.get_height(0,0)");
|
2012-01-04 16:31:23 +00:00
|
|
|
expect_python_error("terrain.get_surface()", PyExc_TypeError);
|
|
|
|
|
expect_python_error("terrain.get_surface('1')", PyExc_TypeError);
|
2009-11-20 20:18:53 +00:00
|
|
|
run_python_string("from physics import *");
|
2012-01-04 16:31:23 +00:00
|
|
|
expect_python_error("terrain.get_surface(Point3D(0,0,0))", PyExc_TypeError);
|
|
|
|
|
expect_python_error("terrain.get_normal()", PyExc_TypeError);
|
2009-11-20 20:18:53 +00:00
|
|
|
run_python_string("terrain.get_normal(0,0)");
|
2011-12-27 17:29:43 +01:00
|
|
|
run_python_string("terrain.find_mods(Point3D(0,0,0))");
|
2009-11-20 20:18:53 +00:00
|
|
|
|
|
|
|
|
run_python_string("points = { }");
|
|
|
|
|
run_python_string("points['-1x-1'] = [-1, -1, -16.8]");
|
|
|
|
|
run_python_string("points['0x-1'] = [0, -1, -3.8]");
|
|
|
|
|
run_python_string("points['-1x0'] = [-1, 0, -2.8]");
|
|
|
|
|
run_python_string("points['-1x1'] = [-1, 1, -1.8]");
|
|
|
|
|
run_python_string("points['1x-1'] = [1, -1, 15.8]");
|
|
|
|
|
run_python_string("points['0x0'] = [0, 0, 12.8]");
|
|
|
|
|
run_python_string("points['1x0'] = [1, 0, 23.1]");
|
|
|
|
|
run_python_string("points['0x1'] = [0, 1, 14.2]");
|
|
|
|
|
run_python_string("points['1x1'] = [1, 1, 19.7]");
|
2018-05-12 00:42:43 +02:00
|
|
|
run_python_string("t.props.terrain = {'points': points}");
|
2009-11-20 20:18:53 +00:00
|
|
|
|
2014-09-29 19:24:05 +02:00
|
|
|
//No surfaces until "surfaces" is defined.
|
|
|
|
|
expect_python_error("terrain.get_surface(Point3D(0,0,0))", PyExc_TypeError);
|
|
|
|
|
|
|
|
|
|
run_python_string("surface = {'name': 'rock', 'pattern': 'fill'}");
|
|
|
|
|
run_python_string("surfaces = [surface]");
|
2018-05-12 00:42:43 +02:00
|
|
|
run_python_string("t.props.terrain = {'points': points, 'surfaces': surfaces}");
|
2014-09-29 19:24:05 +02:00
|
|
|
|
2009-11-20 20:18:53 +00:00
|
|
|
run_python_string("terrain.get_surface(Point3D(0,0,0))");
|
2009-11-17 00:54:03 +00:00
|
|
|
|
2011-02-16 09:00:40 +00:00
|
|
|
#ifdef CYPHESIS_DEBUG
|
2009-11-20 20:18:53 +00:00
|
|
|
run_python_string("import sabotage");
|
2009-11-17 00:54:03 +00:00
|
|
|
// Hit the assert checks.
|
2009-11-20 20:18:53 +00:00
|
|
|
run_python_string("method_get_height = terrain.get_height");
|
|
|
|
|
run_python_string("method_get_surface = terrain.get_surface");
|
|
|
|
|
run_python_string("method_get_normal = terrain.get_normal");
|
|
|
|
|
run_python_string("sabotage.null(terrain)");
|
2012-01-04 16:31:23 +00:00
|
|
|
expect_python_error("method_get_height(0,0)", PyExc_AssertionError);
|
|
|
|
|
expect_python_error("method_get_surface(Point3D(0,0,0))",
|
|
|
|
|
PyExc_AssertionError);
|
|
|
|
|
expect_python_error("method_get_normal(0,0)", PyExc_AssertionError);
|
2009-11-17 00:54:03 +00:00
|
|
|
#endif // NDEBUG
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
shutdown_python_api();
|
|
|
|
|
return 0;
|
|
|
|
|
}
|