1
#include "./boardprototype.h"
4
#include <QScriptEngine>
8
#include "boardwrapper.h"
11
Q_DECLARE_METATYPE(QVector<QScriptValue>)
13
BoardPrototype::BoardPrototype(QObject *parent)
18
BoardWrapper* BoardPrototype::thisBoard() const {
19
return thisObject().data().toVariant().value<BoardWrapper*>();
23
void BoardPrototype::setItem(const QScriptValue& item, int x, int y, int z, int w) {
24
BoardWrapper *wrapper = thisBoard();
25
wrapper->setItemAt(item, x, y, z, w);
28
QScriptValue BoardPrototype::items(int x, int y, int z, int w) {
29
BoardWrapper *wrapper = thisBoard();
30
ItemBoard *board = wrapper->board();
33
// reimplement the ItemBoard::items functionality
34
// NOTE must reflect changes in the ItemBoard class
35
QVector<QScriptValue> items;
36
int min0 = x, min1 = y, min2 = z, min3 = w;
37
int max0 = x, max1 = y, max2 = z, max3 = w;
38
if(min0 < 0) { min0 = 0; max0 = board->size(0)-1; }
39
if(min1 < 0) { min1 = 0; max1 = board->size(1)-1; }
40
if(min2 < 0) { min2 = 0; max2 = board->size(2)-1; }
41
if(min3 < 0) { min3 = 0; max3 = board->size(3)-1; }
43
for(int i3 = min3; i3 <= max3; ++i3) {
44
for(int i2 = min2; i2 <= max2; ++i2) {
45
for(int i1 = min1; i1 <= max1; ++i1) {
46
for(int i0 = min0; i0 <= max0; ++i0) {
47
items.append(wrapper->itemAt(i0, i1, i2, i3));
53
return engine()->toScriptValue(items);
56
QScriptValue BoardPrototype::split(int x, int y, int z, int w) {
57
BoardWrapper *wrapper = thisBoard();
58
ItemBoard *board = wrapper->board();
61
int countX = 1, countY = 1, countZ = 1, countW = 1;
62
int sizeX = board->size(0), sizeY = board->size(1), sizeZ = board->size(2), sizeW = board->size(3);
65
if(sizeX % x) return context()->throwError(QString("blocks of size (%1,%1,%1,%1) don't fit into board "
66
"(x mismatch)").arg(x).arg(y).arg(z).arg(w));
71
if(sizeY % y) return context()->throwError(QString("blocks of size (%1,%1,%1,%1) don't fit into board "
72
"(y mismatch)").arg(x).arg(y).arg(z).arg(w));
77
if(sizeZ % z) return context()->throwError(QString("blocks of size (%1,%1,%1,%1) don't fit into board "
78
"(z mismatch)").arg(x).arg(y).arg(z).arg(w));
83
if(sizeW % w) return context()->throwError(QString("blocks of size (%1,%1,%1,%1) don't fit into board "
84
"(w mismatch)").arg(x).arg(y).arg(z).arg(w));
89
QVector<QScriptValue> blocks;
90
for(int ix = 0; ix < countX; ++ix) {
91
for(int iy = 0; iy < countY; ++iy) {
92
for(int iz = 0; iz < countZ; ++iz) {
93
for(int iw = 0; iw < countW; ++iw) {
94
QVector<QScriptValue> block;
95
block.reserve(sizeX*sizeY*sizeZ*sizeW);
96
for(int jx = 0; jx < sizeX; ++jx) {
97
for(int jy = 0; jy < sizeY; ++jy) {
98
for(int jz = 0; jz < sizeZ; ++jz) {
99
for(int jw = 0; jw < sizeW; ++jw) {
100
QScriptValue item = wrapper->itemAt(ix*sizeX+jx, iy*sizeY+jy, iz*sizeZ+jz, iw*sizeW+jw);
103
blocks << engine()->toScriptValue(block);
106
return engine()->toScriptValue(blocks);
109
#include "boardprototype.moc"