2013-09-01 14:27:21 +00:00
|
|
|
/*
|
|
|
|
** table.cpp
|
|
|
|
**
|
|
|
|
** This file is part of mkxp.
|
|
|
|
**
|
|
|
|
** Copyright (C) 2013 Jonas Kulla <Nyocurio@gmail.com>
|
|
|
|
**
|
|
|
|
** 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 "table.h"
|
|
|
|
|
|
|
|
#include <string.h>
|
2013-12-08 12:19:22 +00:00
|
|
|
#include <algorithm>
|
2013-09-01 14:27:21 +00:00
|
|
|
|
|
|
|
#include "serial-util.h"
|
|
|
|
#include "exception.h"
|
2013-10-15 17:39:29 +00:00
|
|
|
#include "util.h"
|
2013-09-01 14:27:21 +00:00
|
|
|
|
|
|
|
/* Init normally */
|
|
|
|
Table::Table(int x, int y /*= 1*/, int z /*= 1*/)
|
2014-11-20 13:45:54 +00:00
|
|
|
: xs(x), ys(y), zs(z),
|
2014-11-02 22:33:13 +00:00
|
|
|
data(x*y*z)
|
|
|
|
{}
|
2013-09-01 14:27:21 +00:00
|
|
|
|
2014-08-04 12:43:56 +00:00
|
|
|
Table::Table(const Table &other)
|
2014-11-20 13:45:54 +00:00
|
|
|
: xs(other.xs), ys(other.ys), zs(other.zs),
|
2014-11-02 22:33:13 +00:00
|
|
|
data(other.data)
|
|
|
|
{}
|
2013-09-01 14:27:21 +00:00
|
|
|
|
|
|
|
int16_t Table::get(int x, int y, int z) const
|
|
|
|
{
|
2014-11-20 13:45:54 +00:00
|
|
|
return data[xs*ys*z + xs*y + x];
|
2013-09-01 14:27:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Table::set(int16_t value, int x, int y, int z)
|
|
|
|
{
|
2014-11-20 13:45:54 +00:00
|
|
|
if (x < 0 || x >= xs
|
|
|
|
|| y < 0 || y >= ys
|
|
|
|
|| z < 0 || z >= zs)
|
2013-09-04 15:01:51 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-11-20 13:45:54 +00:00
|
|
|
data[xs*ys*z + xs*y + x] = value;
|
2013-09-01 14:27:21 +00:00
|
|
|
|
|
|
|
modified();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Table::resize(int x, int y, int z)
|
|
|
|
{
|
2014-11-20 13:45:54 +00:00
|
|
|
if (x == xs && y == ys && z == zs)
|
2013-09-01 14:27:21 +00:00
|
|
|
return;
|
|
|
|
|
2014-11-02 22:33:13 +00:00
|
|
|
std::vector<int16_t> newData(x*y*z);
|
|
|
|
|
2014-11-20 13:45:54 +00:00
|
|
|
for (int k = 0; k < std::min(z, zs); ++k)
|
|
|
|
for (int j = 0; j < std::min(y, ys); ++j)
|
|
|
|
for (int i = 0; i < std::min(x, xs); ++i)
|
2014-11-02 22:33:13 +00:00
|
|
|
newData[x*y*k + x*j + i] = at(i, j, k);
|
|
|
|
|
|
|
|
data.swap(newData);
|
2013-09-01 14:27:21 +00:00
|
|
|
|
2014-11-20 13:45:54 +00:00
|
|
|
xs = x;
|
|
|
|
ys = y;
|
|
|
|
zs = z;
|
2013-09-01 14:27:21 +00:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Table::resize(int x, int y)
|
|
|
|
{
|
2014-11-20 13:45:54 +00:00
|
|
|
resize(x, y, zs);
|
2013-09-01 14:27:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Table::resize(int x)
|
|
|
|
{
|
2014-11-20 13:45:54 +00:00
|
|
|
resize(x, ys, zs);
|
2013-09-01 14:27:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Serializable */
|
|
|
|
int Table::serialSize() const
|
|
|
|
{
|
|
|
|
/* header + data */
|
2014-11-20 13:45:54 +00:00
|
|
|
return 20 + (xs * ys * zs) * 2;
|
2013-09-01 14:27:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Table::serialize(char *buffer) const
|
|
|
|
{
|
2014-01-02 02:39:22 +00:00
|
|
|
/* Table dimensions: we don't care
|
|
|
|
* about them but RMXP needs them */
|
|
|
|
int dim = 1;
|
2014-11-20 13:45:54 +00:00
|
|
|
int size = xs * ys * zs;
|
2014-01-02 02:39:22 +00:00
|
|
|
|
2014-11-20 13:45:54 +00:00
|
|
|
if (ys > 1)
|
2014-01-02 02:39:22 +00:00
|
|
|
dim = 2;
|
|
|
|
|
2014-11-20 13:45:54 +00:00
|
|
|
if (zs > 1)
|
2014-01-02 02:39:22 +00:00
|
|
|
dim = 3;
|
|
|
|
|
2014-11-20 13:45:54 +00:00
|
|
|
writeInt32(&buffer, dim);
|
|
|
|
writeInt32(&buffer, xs);
|
|
|
|
writeInt32(&buffer, ys);
|
|
|
|
writeInt32(&buffer, zs);
|
|
|
|
writeInt32(&buffer, size);
|
2013-09-01 14:27:21 +00:00
|
|
|
|
2014-11-20 13:45:54 +00:00
|
|
|
memcpy(buffer, dataPtr(data), sizeof(int16_t)*size);
|
2013-09-01 14:27:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Table *Table::deserialize(const char *data, int len)
|
|
|
|
{
|
|
|
|
if (len < 20)
|
|
|
|
throw Exception(Exception::RGSSError, "Marshal: Table: bad file format");
|
|
|
|
|
2014-11-20 13:45:54 +00:00
|
|
|
readInt32(&data);
|
|
|
|
int x = readInt32(&data);
|
|
|
|
int y = readInt32(&data);
|
|
|
|
int z = readInt32(&data);
|
|
|
|
int size = readInt32(&data);
|
2013-09-01 14:27:21 +00:00
|
|
|
|
|
|
|
if (size != x*y*z)
|
|
|
|
throw Exception(Exception::RGSSError, "Marshal: Table: bad file format");
|
|
|
|
|
|
|
|
if (len != 20 + x*y*z*2)
|
|
|
|
throw Exception(Exception::RGSSError, "Marshal: Table: bad file format");
|
|
|
|
|
|
|
|
Table *t = new Table(x, y, z);
|
2014-11-20 13:45:54 +00:00
|
|
|
memcpy(dataPtr(t->data), data, sizeof(int16_t)*size);
|
2013-09-01 14:27:21 +00:00
|
|
|
|
|
|
|
return t;
|
|
|
|
}
|