Skip to content

Commit a9fad1c

Browse files
author
Roberto De Ioris
committed
added FRandomStream implementation, closed #123
1 parent a953810 commit a9fad1c

File tree

5 files changed

+264
-0
lines changed

5 files changed

+264
-0
lines changed
Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
#include "UnrealEnginePythonPrivatePCH.h"
2+
3+
static PyObject *py_ue_frandomstream_frand(ue_PyFRandomStream *self, PyObject * args) {
4+
return PyFloat_FromDouble(self->rstream.FRand());
5+
}
6+
7+
static PyObject *py_ue_frandomstream_frand_range(ue_PyFRandomStream *self, PyObject * args) {
8+
float min;
9+
float max;
10+
if (!PyArg_ParseTuple(args, "ff:frand_range", &min, &max))
11+
return NULL;
12+
return PyFloat_FromDouble(self->rstream.FRandRange(min, max));
13+
}
14+
15+
static PyObject *py_ue_frandomstream_generate_new_seed(ue_PyFRandomStream *self, PyObject * args) {
16+
self->rstream.GenerateNewSeed();
17+
Py_INCREF(Py_None);
18+
return Py_None;
19+
}
20+
21+
static PyObject *py_ue_frandomstream_get_current_seed(ue_PyFRandomStream *self, PyObject * args) {
22+
return PyLong_FromLong(self->rstream.GetCurrentSeed());
23+
}
24+
25+
static PyObject *py_ue_frandomstream_get_fraction(ue_PyFRandomStream *self, PyObject * args) {
26+
return PyFloat_FromDouble(self->rstream.GetFraction());
27+
}
28+
29+
static PyObject *py_ue_frandomstream_get_initial_seed(ue_PyFRandomStream *self, PyObject * args) {
30+
return PyLong_FromLong(self->rstream.GetInitialSeed());
31+
}
32+
33+
static PyObject *py_ue_frandomstream_get_unit_vector(ue_PyFRandomStream *self, PyObject * args) {
34+
return py_ue_new_fvector(self->rstream.GetUnitVector());
35+
}
36+
37+
static PyObject *py_ue_frandomstream_get_unsigned_int(ue_PyFRandomStream *self, PyObject * args) {
38+
return PyLong_FromUnsignedLong(self->rstream.GetUnsignedInt());
39+
}
40+
41+
static PyObject *py_ue_frandomstream_initialize(ue_PyFRandomStream *self, PyObject * args) {
42+
int seed;
43+
if (!PyArg_ParseTuple(args, "i:initialize", &seed))
44+
return NULL;
45+
self->rstream.Initialize(seed);
46+
Py_INCREF(Py_None);
47+
return Py_None;
48+
}
49+
50+
static PyObject *py_ue_frandomstream_rand_helper(ue_PyFRandomStream *self, PyObject * args) {
51+
int max;
52+
if (!PyArg_ParseTuple(args, "i:rand_helper", &max))
53+
return NULL;
54+
return PyLong_FromLong(self->rstream.RandHelper(max));
55+
}
56+
57+
static PyObject *py_ue_frandomstream_rand_range(ue_PyFRandomStream *self, PyObject * args) {
58+
int min;
59+
int max;
60+
if (!PyArg_ParseTuple(args, "ii:rand_range", &min, &max))
61+
return NULL;
62+
return PyLong_FromLong(self->rstream.RandRange(min, max));
63+
}
64+
65+
static PyObject *py_ue_frandomstream_reset(ue_PyFRandomStream *self, PyObject * args) {
66+
self->rstream.Reset();
67+
Py_INCREF(Py_None);
68+
return Py_None;
69+
}
70+
71+
static PyObject *py_ue_frandomstream_vrand(ue_PyFRandomStream *self, PyObject * args) {
72+
return py_ue_new_fvector(self->rstream.VRand());
73+
}
74+
75+
static PyObject *py_ue_frandomstream_vrand_cone(ue_PyFRandomStream *self, PyObject * args) {
76+
PyObject *py_obj;
77+
float horizontal = 0;
78+
float vertical = -1;
79+
if (!PyArg_ParseTuple(args, "Of|f:vrand_cone", &py_obj, &horizontal, &vertical))
80+
return NULL;
81+
ue_PyFVector *py_vec = py_ue_is_fvector(py_obj);
82+
if (!py_vec)
83+
return PyErr_Format(PyExc_TypeError, "argument is not a FVector");
84+
if (vertical < 0)
85+
vertical = horizontal;
86+
return py_ue_new_fvector(self->rstream.VRandCone(py_vec->vec, horizontal, vertical));
87+
}
88+
89+
90+
static PyMethodDef ue_PyFRandomStream_methods[] = {
91+
92+
{ "frand", (PyCFunction)py_ue_frandomstream_frand, METH_VARARGS, "" },
93+
{ "frand_range", (PyCFunction)py_ue_frandomstream_frand_range, METH_VARARGS, "" },
94+
{ "generate_new_seed", (PyCFunction)py_ue_frandomstream_generate_new_seed, METH_VARARGS, "" },
95+
{ "get_current_seed", (PyCFunction)py_ue_frandomstream_get_current_seed, METH_VARARGS, "" },
96+
{ "get_fraction", (PyCFunction)py_ue_frandomstream_get_fraction, METH_VARARGS, "" },
97+
{ "get_initial_seed", (PyCFunction)py_ue_frandomstream_get_initial_seed, METH_VARARGS, "" },
98+
{ "get_unit_vector", (PyCFunction)py_ue_frandomstream_get_unit_vector, METH_VARARGS, "" },
99+
{ "get_unsigned_int", (PyCFunction)py_ue_frandomstream_get_unsigned_int, METH_VARARGS, "" },
100+
{ "initialize", (PyCFunction)py_ue_frandomstream_initialize, METH_VARARGS, "" },
101+
{ "rand_helper", (PyCFunction)py_ue_frandomstream_rand_helper, METH_VARARGS, "" },
102+
{ "rand_range", (PyCFunction)py_ue_frandomstream_rand_range, METH_VARARGS, "" },
103+
{ "reset", (PyCFunction)py_ue_frandomstream_reset, METH_VARARGS, "" },
104+
{ "vrand", (PyCFunction)py_ue_frandomstream_vrand, METH_VARARGS, "" },
105+
{ "vrand_cone", (PyCFunction)py_ue_frandomstream_vrand_cone, METH_VARARGS, "" },
106+
{ NULL } /* Sentinel */
107+
};
108+
109+
110+
111+
static PyObject *ue_PyFRandomStream_str(ue_PyFRandomStream *self)
112+
{
113+
return PyUnicode_FromFormat("<unreal_engine.FRandomStream {'seed': %d}>",
114+
self->rstream.GetCurrentSeed());
115+
}
116+
117+
static PyTypeObject ue_PyFRandomStreamType = {
118+
PyVarObject_HEAD_INIT(NULL, 0)
119+
"unreal_engine.FRandomStream", /* tp_name */
120+
sizeof(ue_PyFRandomStream), /* tp_basicsize */
121+
0, /* tp_itemsize */
122+
0, /* tp_dealloc */
123+
0, /* tp_print */
124+
0, /* tp_getattr */
125+
0, /* tp_setattr */
126+
0, /* tp_reserved */
127+
0, /* tp_repr */
128+
0, /* tp_as_number */
129+
0, /* tp_as_sequence */
130+
0, /* tp_as_mapping */
131+
0, /* tp_hash */
132+
0, /* tp_call */
133+
(reprfunc)ue_PyFRandomStream_str, /* tp_str */
134+
0, /* tp_getattro */
135+
0, /* tp_setattro */
136+
0, /* tp_as_buffer */
137+
Py_TPFLAGS_DEFAULT, /* tp_flags */
138+
"Unreal Engine FRandomStream", /* tp_doc */
139+
0, /* tp_traverse */
140+
0, /* tp_clear */
141+
0, /* tp_richcompare */
142+
0, /* tp_weaklistoffset */
143+
0, /* tp_iter */
144+
0, /* tp_iternext */
145+
ue_PyFRandomStream_methods, /* tp_methods */
146+
0,
147+
0,
148+
};
149+
150+
static int ue_py_frandomstream_init(ue_PyFRandomStream *self, PyObject *args, PyObject *kwargs) {
151+
self->rstream.GenerateNewSeed();
152+
return 0;
153+
}
154+
155+
void ue_python_init_frandomstream(PyObject *ue_module) {
156+
ue_PyFRandomStreamType.tp_new = PyType_GenericNew;
157+
158+
ue_PyFRandomStreamType.tp_init = (initproc)ue_py_frandomstream_init;
159+
160+
if (PyType_Ready(&ue_PyFRandomStreamType) < 0)
161+
return;
162+
163+
Py_INCREF(&ue_PyFRandomStreamType);
164+
PyModule_AddObject(ue_module, "FRandomStream", (PyObject *)&ue_PyFRandomStreamType);
165+
}
166+
167+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#pragma once
2+
3+
4+
5+
#include "UnrealEnginePython.h"
6+
7+
typedef struct {
8+
PyObject_HEAD
9+
/* Type-specific fields go here. */
10+
FRandomStream rstream;
11+
} ue_PyFRandomStream;
12+
13+
void ue_python_init_frandomstream(PyObject *);

Source/UnrealEnginePython/Private/UEPyModule.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1091,6 +1091,8 @@ void unreal_engine_init_py_module() {
10911091
ue_python_init_fcolor(new_unreal_engine_module);
10921092
ue_python_init_flinearcolor(new_unreal_engine_module);
10931093

1094+
ue_python_init_frandomstream(new_unreal_engine_module);
1095+
10941096
ue_python_init_ftimerhandle(new_unreal_engine_module);
10951097

10961098
ue_python_init_fdelegatehandle(new_unreal_engine_module);

Source/UnrealEnginePython/Private/UnrealEnginePythonPrivatePCH.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include "UEPyFLinearColor.h"
3636
#include "UEPyFSocket.h"
3737
#include "UEPyUScriptStruct.h"
38+
#include "UEPyFRandomStream.h"
3839

3940
#include "UEPyCallable.h"
4041
#include "UEPyUClassesImporter.h"

tests/test_randomstream.py

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import unittest
2+
import unreal_engine as ue
3+
from unreal_engine import FVector, FRandomStream
4+
5+
class TestRandomStream(unittest.TestCase):
6+
7+
def test_frand(self):
8+
r = FRandomStream()
9+
self.assertTrue(type(r.frand()) is float)
10+
11+
def test_frand_range(self):
12+
r = FRandomStream()
13+
value = r.frand_range(17, 22)
14+
self.assertTrue(value >= 17)
15+
self.assertTrue(value <= 22)
16+
17+
def test_generate_new_seed(self):
18+
r = FRandomStream()
19+
first_seed = r.get_current_seed()
20+
r.generate_new_seed()
21+
second_seed = r.get_current_seed()
22+
self.assertTrue(first_seed != second_seed)
23+
24+
def test_get_fraction(self):
25+
r = FRandomStream()
26+
value = r.get_fraction()
27+
self.assertTrue(type(value) is float)
28+
self.assertTrue(value >= 0.0)
29+
self.assertTrue(value <= 1.0)
30+
31+
def test_get_fraction(self):
32+
r = FRandomStream()
33+
first_seed = r.get_current_seed()
34+
initial_seed = r.get_initial_seed()
35+
self.assertEqual(first_seed, initial_seed)
36+
37+
def test_get_unit_vector(self):
38+
r = FRandomStream()
39+
self.assertTrue((1.0 - r.get_unit_vector().length()) < 0.1)
40+
41+
def test_get_unsigned_int(self):
42+
r = FRandomStream()
43+
value = r.get_unsigned_int()
44+
self.assertTrue(type(value) is int)
45+
self.assertTrue(value >= 0)
46+
47+
def test_initialize(self):
48+
r = FRandomStream()
49+
r.initialize(17)
50+
self.assertEqual(r.get_initial_seed(), 17)
51+
52+
def test_rand_helper(self):
53+
r = FRandomStream()
54+
value = r.rand_helper(30)
55+
self.assertTrue(type(value) is int)
56+
self.assertTrue(value >= 0)
57+
self.assertTrue(value <= 30)
58+
59+
def test_rand_range(self):
60+
r = FRandomStream()
61+
value = r.rand_range(17, 30)
62+
self.assertTrue(type(value) is int)
63+
self.assertTrue(value >= 17)
64+
self.assertTrue(value <= 30)
65+
66+
def test_reset(self):
67+
r = FRandomStream()
68+
first_seed = r.get_current_seed()
69+
r.generate_new_seed()
70+
second_seed = r.get_current_seed()
71+
self.assertTrue(first_seed != second_seed)
72+
r.reset()
73+
self.assertEqual(r.get_current_seed(), r.get_initial_seed())
74+
75+
def test_vrand(self):
76+
r = FRandomStream()
77+
self.assertTrue((1.0 - r.vrand().length()) < 0.1)
78+
79+
def test_vrand_cone(self):
80+
r = FRandomStream()
81+
self.assertTrue((1.0 - r.vrand_cone(FVector(0, 0, 1), 30, 22).length()) < 0.1)

0 commit comments

Comments
 (0)