2009-05-25 03:15:53 +01: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-05-25 03:15:53 +01:00
|
|
|
#include <Python.h>
|
|
|
|
|
|
2020-01-20 23:17:55 +01:00
|
|
|
#include "../python_testers.h"
|
2009-11-20 20:18:53 +00:00
|
|
|
|
2020-01-20 23:17:55 +01:00
|
|
|
#include "../TestWorld.h"
|
|
|
|
|
#include "../TestPropertyManager.h"
|
2009-05-25 04:10:34 +01:00
|
|
|
|
2021-06-27 17:00:34 +02:00
|
|
|
#include "pythonbase/Python_API.h"
|
2018-10-31 21:38:35 +01:00
|
|
|
#include "rules/simulation/Entity.h"
|
2009-05-25 03:15:53 +01:00
|
|
|
|
|
|
|
|
#include <cassert>
|
2018-04-25 20:09:14 +02:00
|
|
|
#include <common/Inheritance.h>
|
2021-06-27 17:00:34 +02:00
|
|
|
#include "pythonbase/Python_Script_Utils.h"
|
2018-10-31 21:38:35 +01:00
|
|
|
#include <rules/python/CyPy_LocatedEntity.h>
|
2018-11-03 16:43:33 +01:00
|
|
|
#include <rules/simulation/python/CyPy_UsageInstance.h>
|
|
|
|
|
#include <rules/simulation/python/CyPy_Server.h>
|
2019-01-17 20:39:48 +01:00
|
|
|
#include <rules/simulation/python/CyPy_Entity.h>
|
|
|
|
|
#include <Atlas/Objects/Operation.h>
|
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>
|
2021-06-27 17:00:34 +02:00
|
|
|
#include "pythonbase/PythonMalloc.h"
|
2018-05-04 19:17:55 +02:00
|
|
|
|
2020-01-20 23:17:55 +01:00
|
|
|
#include "../stubs/common/stubMonitors.h"
|
2018-08-04 01:12:45 +02:00
|
|
|
|
2019-09-09 20:45:16 +02:00
|
|
|
Atlas::Objects::Factories factories;
|
|
|
|
|
|
2009-05-25 03:15:53 +01:00
|
|
|
int main()
|
|
|
|
|
{
|
2018-05-04 19:17:55 +02:00
|
|
|
|
2021-05-27 17:38:58 +02:00
|
|
|
setupPythonMalloc();
|
2019-01-13 22:13:23 +01:00
|
|
|
{
|
2021-01-25 23:27:17 +01:00
|
|
|
Inheritance inheritance(factories);
|
|
|
|
|
TestPropertyManager testPropertyManager;
|
|
|
|
|
init_python_api({&CyPy_Server::init,
|
|
|
|
|
&CyPy_Rules::init,
|
|
|
|
|
&CyPy_Atlas::init,
|
|
|
|
|
&CyPy_Physics::init,
|
|
|
|
|
&CyPy_Common::init});
|
|
|
|
|
|
|
|
|
|
run_python_string("from server import *");
|
|
|
|
|
run_python_string("from atlas import Operation");
|
|
|
|
|
run_python_string("from atlas import Oplist");
|
|
|
|
|
|
|
|
|
|
UsageInstance usageInstance;
|
2022-07-04 15:37:51 +02:00
|
|
|
usageInstance.actor = new Entity(100);
|
2021-01-25 23:27:17 +01:00
|
|
|
usageInstance.op = Atlas::Objects::Operation::Action();
|
|
|
|
|
|
|
|
|
|
Py::Module serverModule("server");
|
|
|
|
|
serverModule.setAttr("usage_instance", CyPy_UsageInstance::wrap(usageInstance));
|
|
|
|
|
|
|
|
|
|
//Check that reference handling is correct.
|
2019-01-13 22:13:23 +01:00
|
|
|
{
|
2022-07-04 15:37:51 +02:00
|
|
|
Ref<Entity> entity = new Entity(2);
|
2021-01-25 23:27:17 +01:00
|
|
|
assert(entity->checkRef() == 1);
|
2019-01-13 22:13:23 +01:00
|
|
|
{
|
2021-01-25 23:27:17 +01:00
|
|
|
auto wrap1 = CyPy_LocatedEntity::wrap(entity);
|
|
|
|
|
assert(CyPy_Entity::check(wrap1));
|
|
|
|
|
assert(entity->checkRef() == 2);
|
|
|
|
|
{
|
|
|
|
|
//Since the Python object is cached another call to "wrap" should _not_ increase the count,
|
|
|
|
|
// since no new Python object is creatd.
|
|
|
|
|
auto wrap2 = CyPy_LocatedEntity::wrap(entity);
|
|
|
|
|
assert(entity->checkRef() == 2);
|
|
|
|
|
assert(CyPy_Entity::check(wrap2));
|
|
|
|
|
}
|
2019-01-14 22:50:09 +01:00
|
|
|
assert(entity->checkRef() == 2);
|
2019-01-13 22:13:23 +01:00
|
|
|
}
|
2021-01-25 23:27:17 +01:00
|
|
|
assert(entity->checkRef() == 1);
|
2019-01-13 22:13:23 +01:00
|
|
|
}
|
2009-11-20 20:18:53 +00:00
|
|
|
|
2022-07-04 15:37:51 +02:00
|
|
|
Ref<Entity> e = new Entity(1);
|
2021-01-25 23:27:17 +01:00
|
|
|
|
2021-05-18 22:09:57 +02:00
|
|
|
{
|
|
|
|
|
auto prop = std::make_unique<SoftProperty>();
|
|
|
|
|
prop->set("bar");
|
|
|
|
|
e->setProperty("foo", std::move(prop));
|
|
|
|
|
}
|
2022-07-04 15:37:51 +02:00
|
|
|
Ref<Entity> wrld = new Entity(0);
|
2021-06-18 22:44:30 +02:00
|
|
|
e->m_parent = wrld.get();
|
|
|
|
|
e->m_parent->makeContainer();
|
|
|
|
|
assert(e->m_parent->m_contains != nullptr);
|
|
|
|
|
e->m_parent->m_contains->insert(e);
|
2021-01-25 23:27:17 +01:00
|
|
|
TestWorld test_world(wrld);
|
|
|
|
|
|
|
|
|
|
auto wrap_e = CyPy_LocatedEntity::wrap(e);
|
|
|
|
|
assert(CyPy_LocatedEntity::check(wrap_e));
|
|
|
|
|
auto wrap_e_again = CyPy_LocatedEntity::wrap(e);
|
|
|
|
|
assert(CyPy_LocatedEntity::check(wrap_e_again));
|
|
|
|
|
assert(wrap_e == wrap_e_again);
|
|
|
|
|
|
2022-07-04 15:37:51 +02:00
|
|
|
Ref<Entity> c = new Entity(2);
|
2021-01-25 23:27:17 +01:00
|
|
|
auto wrap_c = CyPy_LocatedEntity::wrap(c);
|
|
|
|
|
assert(CyPy_LocatedEntity::check(wrap_c));
|
|
|
|
|
|
2021-01-26 22:03:16 +01:00
|
|
|
Py::Module module("__main__");
|
2021-01-25 23:27:17 +01:00
|
|
|
module.setAttr("testentity", wrap_e);
|
|
|
|
|
|
|
|
|
|
run_python_string("import server")
|
2021-01-26 22:03:16 +01:00
|
|
|
run_python_string("assert testentity is not None");
|
|
|
|
|
run_python_string("assert testentity.props.foo == 'bar'");
|
|
|
|
|
run_python_string("assert testentity.is_destroyed == False");
|
2021-06-18 22:44:30 +02:00
|
|
|
run_python_string("assert testentity.parent.id == '0'"); //Parent should be world
|
|
|
|
|
run_python_string("assert testentity.parent.parent is None"); //The world should have no parent
|
2021-01-25 23:27:17 +01:00
|
|
|
|
|
|
|
|
expect_python_error("Thing()", PyExc_IndexError);
|
|
|
|
|
expect_python_error("Thing('s')", PyExc_TypeError);
|
|
|
|
|
run_python_string("le=Thing('1')");
|
|
|
|
|
run_python_string("le_ent = le.as_entity()");
|
|
|
|
|
run_python_string("assert(le_ent.id == '1')");
|
|
|
|
|
run_python_string("le.send_world(Operation('get'))");
|
|
|
|
|
run_python_string("le==Thing('2')");
|
|
|
|
|
run_python_string("le.type==''");
|
|
|
|
|
expect_python_error("print(le.foo_operation)", PyExc_AttributeError);
|
|
|
|
|
run_python_string("print(le.contains)");
|
|
|
|
|
run_python_string("le.type==''");
|
|
|
|
|
expect_python_error("le.type='non_exist'", PyExc_ValueError);
|
|
|
|
|
expect_python_error("le.type=1", PyExc_TypeError);
|
|
|
|
|
run_python_string("le.type='game_entity'");
|
|
|
|
|
expect_python_error("le.type='game_entity'", PyExc_RuntimeError);
|
|
|
|
|
run_python_string("le.type");
|
|
|
|
|
expect_python_error("le.map=1", PyExc_AttributeError);
|
|
|
|
|
run_python_string("le.props.map_attr={'1': 2}");
|
|
|
|
|
run_python_string("le.props.map_attr");
|
|
|
|
|
run_python_string("le.props.list_attr=[1,2]");
|
|
|
|
|
run_python_string("le.props.list_attr");
|
|
|
|
|
run_python_string("le.props.string_attr='foo'");
|
|
|
|
|
run_python_string("le.props.int_attr=1");
|
|
|
|
|
run_python_string("le.props.float_attr=2.0");
|
|
|
|
|
run_python_string("assert(le.get_prop_string('string_attr')=='foo')");
|
|
|
|
|
run_python_string("assert(le.get_prop_string('string_attr', 'bar')=='foo')");
|
|
|
|
|
run_python_string("assert(le.get_prop_string('none_attr', 'bar')=='bar')");
|
|
|
|
|
run_python_string("assert(le.get_prop_float('float_attr')==2.0)");
|
|
|
|
|
run_python_string("assert(le.get_prop_float('float_attr', 10.0)==2.0)");
|
|
|
|
|
run_python_string("assert(le.get_prop_float('none_attr', 10.0)==10.0)");
|
|
|
|
|
run_python_string("assert(le.get_prop_int('int_attr')==1)");
|
|
|
|
|
run_python_string("assert(le.get_prop_int('int_attr', 10)==1)");
|
|
|
|
|
run_python_string("assert(le.get_prop_int('none_attr', 10)==10)");
|
|
|
|
|
expect_python_error("le.props.non_atlas=set([1,2])", PyExc_TypeError);
|
|
|
|
|
run_python_string("le.props.non_atlas == None");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
run_python_string("Thing(Thing('1'))");
|
|
|
|
|
run_python_string("t=Thing('1')");
|
|
|
|
|
run_python_string("Thing(t)");
|
|
|
|
|
//run_python_string("Thing(Character('1'))");
|
|
|
|
|
run_python_string("t.as_entity()");
|
|
|
|
|
run_python_string("t.send_world(Operation('get'))");
|
|
|
|
|
expect_python_error("t.send_world('get')", PyExc_TypeError);
|
|
|
|
|
run_python_string("t.type==''");
|
|
|
|
|
expect_python_error("print(t.foo_operation)", PyExc_AttributeError);
|
|
|
|
|
run_python_string("print(t.contains)");
|
2009-11-12 23:35:39 +00:00
|
|
|
|
2021-01-26 22:03:16 +01:00
|
|
|
module.getDict().clear();
|
|
|
|
|
|
|
|
|
|
e->destroy();
|
2021-01-25 23:27:17 +01:00
|
|
|
}
|
2009-05-25 03:15:53 +01:00
|
|
|
shutdown_python_api();
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2012-09-06 13:48:40 +01:00
|
|
|
|