mkxp-z/binding/table-binding.cpp

188 lines
4.2 KiB
C++
Raw Permalink Normal View History

2013-09-01 16:27:21 +02:00
/*
** table-binding.cpp
**
** This file is part of mkxp.
**
** Copyright (C) 2013 - 2021 Amaryllis Kulla <ancurio@mapleshrine.eu>
2013-09-01 16:27:21 +02:00
**
** mkxp 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.
**
** mkxp 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 mkxp. If not, see <http://www.gnu.org/licenses/>.
*/
#include "binding-util.h"
#include "serializable-binding.h"
#include "table.h"
#include <algorithm>
2013-09-01 16:27:21 +02:00
static int num2TableSize(VALUE v) {
int i = NUM2INT(v);
return std::max(0, i);
}
static void parseArgsTableSizes(int argc, VALUE *argv, int *x, int *y, int *z) {
*y = *z = 1;
switch (argc) {
case 3:
*z = num2TableSize(argv[2]);
/* fall through */
case 2:
*y = num2TableSize(argv[1]);
/* fall through */
case 1:
*x = num2TableSize(argv[0]);
break;
default:
rb_error_arity(argc, 1, 3);
}
}
#if RAPI_FULL > 187
2013-09-01 16:27:21 +02:00
DEF_TYPE(Table);
2019-07-30 02:13:36 -04:00
#else
DEF_ALLOCFUNC(Table);
#endif
2013-09-01 16:27:21 +02:00
RB_METHOD(tableInitialize) {
int x, y, z;
2013-09-01 16:27:21 +02:00
parseArgsTableSizes(argc, argv, &x, &y, &z);
2013-09-01 16:27:21 +02:00
Table *t = getPrivateDataNoRaise<Table>(self);
if (t) {
t->resize(x, y, z);
} else {
t = new Table(x, y, z);
2013-09-01 16:27:21 +02:00
setPrivateData(self, t);
}
2013-09-01 16:27:21 +02:00
return self;
2013-09-01 16:27:21 +02:00
}
RB_METHOD(tableResize) {
Table *t = getPrivateData<Table>(self);
2013-09-01 16:27:21 +02:00
int x, y, z;
parseArgsTableSizes(argc, argv, &x, &y, &z);
2013-09-01 16:27:21 +02:00
t->resize(x, y, z);
2013-09-01 16:27:21 +02:00
return Qnil;
2013-09-01 16:27:21 +02:00
}
#define TABLE_SIZE(d, D) \
RB_METHOD(table##D##Size) { \
RB_UNUSED_PARAM \
Table *t = getPrivateData<Table>(self); \
return INT2NUM(t->d##Size()); \
}
2013-09-01 16:27:21 +02:00
TABLE_SIZE(x, X)
TABLE_SIZE(y, Y)
TABLE_SIZE(z, Z)
RB_METHOD_GUARD(tableGetAt) {
Table *t = getPrivateData<Table>(self);
2013-09-01 16:27:21 +02:00
int x, y, z;
x = y = z = 0;
2013-09-01 16:27:21 +02:00
x = NUM2INT(argv[0]);
if (argc > 1)
y = NUM2INT(argv[1]);
if (argc > 2)
z = NUM2INT(argv[2]);
2013-09-01 16:27:21 +02:00
if (argc > 3)
throw Exception(Exception::ArgumentError, "wrong number of arguments");
2013-09-01 16:27:21 +02:00
if (x < 0 || x >= t->xSize() || y < 0 || y >= t->ySize() || z < 0 ||
z >= t->zSize()) {
return Qnil;
}
2013-09-01 16:27:21 +02:00
short result = t->get(x, y, z);
2013-09-01 16:27:21 +02:00
return INT2FIX(result); /* short always fits in a Fixnum */
2013-09-01 16:27:21 +02:00
}
RB_METHOD_GUARD_END
2013-09-01 16:27:21 +02:00
RB_METHOD_GUARD(tableSetAt) {
Table *t = getPrivateData<Table>(self);
2013-09-01 16:27:21 +02:00
int x, y, z, value;
x = y = z = 0;
2013-09-01 16:27:21 +02:00
if (argc < 2)
throw Exception(Exception::ArgumentError, "wrong number of arguments");
2013-09-01 16:27:21 +02:00
switch (argc) {
default:
case 2:
x = NUM2INT(argv[0]);
value = NUM2INT(argv[1]);
2014-01-01 12:56:45 +01:00
break;
case 3:
x = NUM2INT(argv[0]);
y = NUM2INT(argv[1]);
value = NUM2INT(argv[2]);
2014-01-01 12:56:45 +01:00
break;
case 4:
x = NUM2INT(argv[0]);
y = NUM2INT(argv[1]);
z = NUM2INT(argv[2]);
value = NUM2INT(argv[3]);
2014-01-01 12:56:45 +01:00
break;
}
2013-09-01 16:27:21 +02:00
t->set(value, x, y, z);
2013-09-01 16:27:21 +02:00
return argv[argc - 1];
2013-09-01 16:27:21 +02:00
}
RB_METHOD_GUARD_END
2013-09-01 16:27:21 +02:00
MARSH_LOAD_FUN(Table)
INITCOPY_FUN(Table)
2013-09-01 16:27:21 +02:00
RB_METHOD(tableInitializeDefault) {
Table *t = new Table(0, 0, 0);
setPrivateData(self, t);
return self;
}
CLASS_ALLOCATE_PRE_INIT(Table, tableInitializeDefault);
void tableBindingInit() {
VALUE klass = rb_define_class("Table", rb_cObject);
rb_define_alloc_func(klass, TableAllocatePreInit);
2013-09-01 16:27:21 +02:00
serializableBindingInit<Table>(klass);
2013-09-01 16:27:21 +02:00
rb_define_class_method(klass, "_load", TableLoad);
2013-09-01 16:27:21 +02:00
_rb_define_method(klass, "initialize", tableInitialize);
_rb_define_method(klass, "initialize_copy", TableInitializeCopy);
_rb_define_method(klass, "resize", tableResize);
_rb_define_method(klass, "xsize", tableXSize);
_rb_define_method(klass, "ysize", tableYSize);
_rb_define_method(klass, "zsize", tableZSize);
_rb_define_method(klass, "[]", tableGetAt);
_rb_define_method(klass, "[]=", tableSetAt);
2013-09-01 16:27:21 +02:00
}