mirror of
https://github.com/worldforge/cyphesis
synced 2026-08-13 12:26:04 -04:00
rulesets/Py_Location.h, rulesets/Py_Map.h, rulesets/Py_Mind.h, rulesets/Py_Object.h, rulesets/Py_Operation.h, rulesets/Py_Oplist.h, rulesets/Py_Point3D.h, rulesets/Py_Quaternion.h, rulesets/Py_RootEntity.h, rulesets/Py_Task.h, rulesets/Py_Thing.h, rulesets/Py_Vector3D.h, rulesets/Py_World.h, rulesets/Py_WorldTime.h: Re-wrote PyFoo_Check macros to comply with recent conventions and be a little more efficient. * rulesets/Py_Statistics.cpp, rulesets/PythonArithmeticScript.cpp, rulesets/mason/world/statistics/Statistics.py: Remove lots of debug output. * rulesets/PythonArithmeticScript.h: Add an accessor to get to the python object. * rulesets/StatisticsProperty.h: Add an accessor to get to the Statistics reference handled by this class. * rulesets/Py_Property.cpp: Return the script for a statistics object rather than creating a wrapper for it.
47 lines
1.2 KiB
C++
47 lines
1.2 KiB
C++
// This file may be redistributed and modified only under the terms of
|
|
// the GNU General Public License (See COPYING for details).
|
|
// Copyright (C) 2005 Alistair Riddoch
|
|
|
|
#include <Python.h>
|
|
|
|
#include "PythonArithmeticScript.h"
|
|
|
|
#include "common/log.h"
|
|
|
|
#include <iostream>
|
|
|
|
PythonArithmeticScript::PythonArithmeticScript(PyObject * o) : m_script(o)
|
|
{
|
|
}
|
|
|
|
PythonArithmeticScript::~PythonArithmeticScript()
|
|
{
|
|
Py_DECREF(m_script);
|
|
}
|
|
|
|
int PythonArithmeticScript::attribute(const std::string & name, float & val)
|
|
{
|
|
PyObject * py_name = PyString_FromString(name.c_str());
|
|
PyObject * ret = PyObject_CallMethod(m_script, "attribute", "(O)", py_name);
|
|
Py_DECREF(py_name);
|
|
if (ret == NULL) {
|
|
if (PyErr_Occurred() == NULL) {
|
|
// std::cout << "No attribute method" << std::endl << std::flush;
|
|
} else {
|
|
log(ERROR, "Reporting python error");
|
|
PyErr_Print();
|
|
}
|
|
return -1;
|
|
}
|
|
if (PyFloat_Check(ret)) {
|
|
val = PyFloat_AsDouble(ret);
|
|
} else if (PyInt_Check(ret)) {
|
|
val = PyInt_AsLong(ret);
|
|
} else if (ret == Py_None) {
|
|
return -1;
|
|
} else {
|
|
log(ERROR, "Invalid response from script");
|
|
return -1;
|
|
}
|
|
return 0;
|
|
}
|