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"
|
|
|
|
|
|
2018-11-03 16:43:33 +01:00
|
|
|
#include "rules/python/Python_API.h"
|
2018-10-31 21:38:35 +01:00
|
|
|
#include "rules/simulation/Entity.h"
|
2018-11-03 16:43:33 +01:00
|
|
|
#include "rules/simulation/TerrainProperty.h"
|
|
|
|
|
#include "rules/simulation/python/CyPy_Server.h"
|
|
|
|
|
#include "rules/simulation/python/CyPy_Entity.h"
|
2009-11-17 00:54:03 +00:00
|
|
|
|
2018-07-24 21:18:50 +02:00
|
|
|
#include "external/pycxx/CXX/Extensions.hxx"
|
2018-11-03 16:43:33 +01:00
|
|
|
#include <rules/python/CyPy_Atlas.h>
|
|
|
|
|
#include <rules/python/CyPy_Physics.h>
|
|
|
|
|
#include <rules/python/CyPy_Common.h>
|
|
|
|
|
#include <rules/python/CyPy_Rules.h>
|
2018-07-24 21:18:50 +02:00
|
|
|
|
2018-11-03 16:43:33 +01:00
|
|
|
#include <cassert>
|
2009-11-17 00:54:03 +00:00
|
|
|
|
2018-07-24 21:18:50 +02:00
|
|
|
class TestProp : public Py::ExtensionModule<TestProp>
|
2009-11-17 00:54:03 +00:00
|
|
|
{
|
2018-07-24 21:18:50 +02:00
|
|
|
public:
|
|
|
|
|
Py::Object add_properties(const Py::Tuple& args)
|
|
|
|
|
{
|
2018-07-29 21:10:22 +02:00
|
|
|
auto ent = CyPy_Entity::value(args.front());
|
2009-11-17 00:54:03 +00:00
|
|
|
|
2018-07-24 21:18:50 +02:00
|
|
|
PropertyBase * p = ent->setProperty("terrain", new TerrainProperty);
|
2018-08-05 20:47:09 +02:00
|
|
|
p->install(ent.get(), "terrain");
|
|
|
|
|
p->apply(ent.get());
|
2018-07-24 21:18:50 +02:00
|
|
|
ent->propertyApplied("terrain", *p);
|
2009-11-17 00:54:03 +00:00
|
|
|
|
2018-07-24 21:18:50 +02:00
|
|
|
return Py::None();
|
|
|
|
|
}
|
2009-11-17 00:54:03 +00:00
|
|
|
|
2018-07-24 21:18:50 +02:00
|
|
|
TestProp() : ExtensionModule("testprop")
|
|
|
|
|
{
|
|
|
|
|
add_varargs_method("add_properties", &TestProp::add_properties, "");
|
2018-05-04 19:17:55 +02:00
|
|
|
|
2018-07-24 21:18:50 +02:00
|
|
|
initialize("testprop");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
};
|
2009-11-17 00:54:03 +00:00
|
|
|
|
|
|
|
|
int main()
|
|
|
|
|
{
|
2018-07-24 21:18:50 +02:00
|
|
|
PyImport_AppendInittab("testprop", [](){
|
|
|
|
|
auto module = new TestProp();
|
|
|
|
|
return module->module().ptr();
|
|
|
|
|
});
|
2018-11-03 16:43:33 +01:00
|
|
|
init_python_api({&CyPy_Server::init,
|
|
|
|
|
&CyPy_Rules::init,
|
|
|
|
|
&CyPy_Atlas::init,
|
|
|
|
|
&CyPy_Physics::init,
|
2018-12-16 23:20:33 +01:00
|
|
|
&CyPy_Common::init});
|
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);
|
2018-07-24 23:00:06 +02:00
|
|
|
expect_python_error("terrain.get_height()", PyExc_IndexError);
|
2009-11-20 20:18:53 +00:00
|
|
|
run_python_string("terrain.get_height(0,0)");
|
2018-07-24 23:00:06 +02:00
|
|
|
expect_python_error("terrain.get_surface()", PyExc_IndexError);
|
|
|
|
|
expect_python_error("terrain.get_surface('1')", PyExc_IndexError);
|
2009-11-20 20:18:53 +00:00
|
|
|
run_python_string("from physics import *");
|
2018-07-24 23:00:06 +02:00
|
|
|
expect_python_error("terrain.get_surface(Point3D(0,0,0))", PyExc_IndexError);
|
|
|
|
|
expect_python_error("terrain.get_normal()", PyExc_IndexError);
|
2009-11-20 20:18:53 +00:00
|
|
|
run_python_string("terrain.get_normal(0,0)");
|
2018-07-24 23:00:06 +02:00
|
|
|
run_python_string("terrain.find_mods(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.
|
2018-07-24 23:00:06 +02:00
|
|
|
run_python_string("assert terrain.get_surface(0,0) is None");
|
2014-09-29 19:24:05 +02:00
|
|
|
|
|
|
|
|
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
|
|
|
|
2018-07-24 23:00:06 +02:00
|
|
|
run_python_string("terrain.get_surface(0,0)");
|
2009-11-17 00:54:03 +00:00
|
|
|
|
|
|
|
|
shutdown_python_api();
|
|
|
|
|
return 0;
|
|
|
|
|
}
|