/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program 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 3 of the License, or
* (at your option) any later version.
*
* This program 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 this program. If not, see .
*
*/
#include "twp/twp.h"
#include "twp/detection.h"
#include "twp/resmanager.h"
#include "twp/object.h"
#include "twp/room.h"
#include "twp/squtil.h"
#include "twp/clipper/clipper.hpp"
namespace Twp {
static ObjectType toObjectType(const Common::JSONObject &jObject) {
if (toBool(jObject, "prop"))
return otProp;
if (toBool(jObject, "spot"))
return otSpot;
if (toBool(jObject, "trigger"))
return otTrigger;
return otNone;
}
static Direction parseUseDir(const Common::String &s) {
if (s == "DIR_FRONT")
return dFront;
if (s == "DIR_BACK")
return dBack;
if (s == "DIR_LEFT")
return dLeft;
if (s == "DIR_RIGHT")
return dRight;
error("invalid use direction: %s", s.c_str());
}
static Math::Vector2d parseParallax(const Common::JSONValue &v) {
if (v.isIntegerNumber()) {
return {(float)v.asIntegerNumber(), 1};
}
if (v.isNumber()) {
return {(float)v.asNumber(), 1};
}
if (v.isString()) {
return parseVec2(v.asString());
}
error("parseParallax expected a float, int or string, not this: %s", v.stringify().c_str());
}
static Walkbox parseWalkbox(const Common::String &text) {
Common::Array points;
size_t i = 1;
size_t endPos;
do {
uint32 commaPos = text.find(',', i);
int x = (int)strtol(text.substr(i, commaPos - i).c_str(), nullptr, 10);
endPos = text.find('}', commaPos + 1);
int y = (int)strtol(text.substr(commaPos + 1, endPos - commaPos - 1).c_str(), nullptr, 10);
i = endPos + 3;
points.push_back({x, y});
} while ((text.size() - 1) != endPos);
return Walkbox(points);
}
static Scaling parseScaling(const Common::JSONArray &jScalings) {
float scale;
int y;
Scaling result;
for (auto it = jScalings.begin(); it != jScalings.end(); it++) {
const Common::String &v = (*it)->asString();
sscanf(v.c_str(), "%f@%d", &scale, &y);
result.values.push_back(ScalingValue{scale, y});
}
return result;
}
static ClipperLib::Path toPolygon(const Walkbox &walkbox) {
ClipperLib::Path path;
const Common::Array &points = walkbox.getPoints();
for (size_t i = 0; i < points.size(); i++) {
path.push_back(ClipperLib::IntPoint(points[i].x, points[i].y));
}
return path;
}
static Walkbox toWalkbox(const ClipperLib::Path &path) {
Common::Array pts;
for (size_t i = 0; i < path.size(); i++) {
const ClipperLib::IntPoint &pt = path[i];
pts.push_back(Vector2i(static_cast(pt.X), static_cast(pt.Y)));
}
return Walkbox(pts, ClipperLib::Orientation(path));
}
static Common::Array merge(const Common::Array &walkboxes) {
Common::Array result;
if (walkboxes.size() > 0) {
ClipperLib::Paths subjects, clips;
for (size_t i = 0; i < walkboxes.size(); i++) {
const Walkbox &wb = walkboxes[i];
if (wb.isVisible()) {
subjects.push_back(toPolygon(wb));
}
}
ClipperLib::Paths solutions;
ClipperLib::Clipper c;
c.AddPaths(subjects, ClipperLib::ptSubject, true);
c.Execute(ClipperLib::ClipType::ctUnion, solutions, ClipperLib::pftEvenOdd);
ClipperLib::Paths solutions2;
ClipperLib::Clipper c2;
c2.AddPaths(solutions, ClipperLib::ptSubject, true);
c2.AddPaths(clips, ClipperLib::ptClip, true);
c2.Execute(ClipperLib::ClipType::ctDifference, solutions2, ClipperLib::pftEvenOdd);
for (size_t i = 0; i < solutions2.size(); i++) {
result.push_back(toWalkbox(solutions2[i]));
}
}
return result;
}
Room::Room(const Common::String &name, HSQOBJECT &table) : _table(table) {
setId(_table, g_twp->_resManager->newRoomId());
_name = name;
_scene = Common::SharedPtr(new Scene());
_scene->addChild(&_overlayNode);
}
Room::~Room() {
}
Common::SharedPtr