/* 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/detection.h"
#include "twp/lighting.h"
#include "twp/object.h"
#include "twp/resmanager.h"
#include "twp/room.h"
#include "twp/squtil.h"
#include "twp/squirrel/squirrel.h"
#include "twp/squirrel/sqvm.h"
#include "twp/squirrel/sqstring.h"
#include "twp/squirrel/sqstate.h"
#include "twp/squirrel/sqtable.h"
#include "twp/squirrel/sqstdaux.h"
#include "twp/squirrel/sqfuncproto.h"
#include "twp/squirrel/sqclosure.h"
#include "twp/thread.h"
namespace Twp {
SQInteger sqpush(HSQUIRRELVM v) {
return 0;
}
template<>
SQInteger sqpush(HSQUIRRELVM v, int value) {
sq_pushinteger(v, value);
return 1;
}
template<>
SQInteger sqpush(HSQUIRRELVM v, long long value) {
sq_pushinteger(v, (SQInteger)value);
return 1;
}
template<>
SQInteger sqpush(HSQUIRRELVM v, float value) {
sq_pushfloat(v, value);
return 1;
}
template<>
SQInteger sqpush(HSQUIRRELVM v, double value) {
sq_pushfloat(v, (SQFloat)value);
return 1;
}
template<>
SQInteger sqpush(HSQUIRRELVM v, bool value) {
sq_pushinteger(v, value ? 1 : 0);
return 1;
}
template<>
SQInteger sqpush(HSQUIRRELVM v, Common::String value) {
sq_pushstring(v, value.c_str(), value.size());
return 1;
}
template<>
SQInteger sqpush(HSQUIRRELVM v, const char *value) {
sq_pushstring(v, value, -1);
return 1;
}
template<>
SQInteger sqpush(HSQUIRRELVM v, HSQOBJECT value) {
sq_pushobject(v, value);
return 1;
}
template<>
SQInteger sqpush(HSQUIRRELVM v, Vector2i value) {
sq_newtable(v);
sq_pushstring(v, "x", -1);
sq_pushinteger(v, value.x);
sq_newslot(v, -3, SQFalse);
sq_pushstring(v, "y", -1);
sq_pushinteger(v, value.y);
sq_newslot(v, -3, SQFalse);
return 1;
}
template<>
SQInteger sqpush(HSQUIRRELVM v, Math::Vector2d value) {
return sqpush(v, (Vector2i)value);
}
template<>
SQInteger sqpush(HSQUIRRELVM v, Rectf value) {
sq_newtable(v);
sq_pushstring(v, "x1", -1);
sq_pushinteger(v, value.left());
sq_newslot(v, -3, SQFalse);
sq_pushstring(v, "y1", -1);
sq_pushinteger(v, value.bottom());
sq_newslot(v, -3, SQFalse);
sq_pushstring(v, "x2", -1);
sq_pushinteger(v, value.right());
sq_newslot(v, -3, SQFalse);
sq_pushstring(v, "y2", -1);
sq_pushinteger(v, value.top());
sq_newslot(v, -3, SQFalse);
return 1;
}
template<>
HSQOBJECT sqtoobj(HSQUIRRELVM v, SQInteger value) {
SQObject o;
o._type = OT_INTEGER;
o._unVal.nInteger = value;
return o;
}
template<>
HSQOBJECT sqtoobj(HSQUIRRELVM v, const SQChar *value) {
SQObject o;
o._type = OT_STRING;
o._unVal.pString = SQString::Create(_ss(v), value);
return o;
}
template<>
SQRESULT sqget(HSQUIRRELVM v, int i, SQInteger &value) {
return sq_getinteger(v, i, &value);
}
template<>
SQRESULT sqget(HSQUIRRELVM v, int i, bool &value) {
SQInteger itg;
SQRESULT result = sq_getinteger(v, i, &itg);
value = itg != 0;
return result;
}
template<>
SQRESULT sqget(HSQUIRRELVM v, int i, float &value) {
SQFloat f;
SQRESULT result = sq_getfloat(v, i, &f);
value = static_cast(f);
return result;
}
template<>
SQRESULT sqget(HSQUIRRELVM v, int i, Common::String &value) {
const SQChar *s;
SQRESULT result = sq_getstring(v, i, &s);
value = s;
return result;
}
template<>
SQRESULT sqget(HSQUIRRELVM v, int i, const SQChar *&value) {
return sq_getstring(v, i, &value);
}
template<>
SQRESULT sqget(HSQUIRRELVM v, int i, HSQOBJECT &value) {
return sq_getstackobj(v, i, &value);
}
void sqgetarray(HSQUIRRELVM v, HSQOBJECT o, Common::Array &arr) {
sq_pushobject(v, o);
sq_pushnull(v);
while (SQ_SUCCEEDED(sq_next(v, -2))) {
const SQChar *str;
sq_getstring(v, -1, &str);
arr.push_back(str);
sq_pop(v, 2);
}
sq_pop(v, 1);
}
SQRESULT sqgetarray(HSQUIRRELVM v, int i, Common::Array &arr) {
HSQOBJECT obj;
SQRESULT result = sq_getstackobj(v, i, &obj);
sqgetarray(v, obj, arr);
return result;
}
void setId(HSQOBJECT &o, int id) {
sqsetf(o, "_id", id);
}
bool sqrawexists(HSQOBJECT obj, const Common::String &name) {
HSQUIRRELVM v = g_twp->getVm();
SQInteger top = sq_gettop(v);
sqpush(v, obj);
sq_pushstring(v, name.c_str(), -1);
if (SQ_SUCCEEDED(sq_rawget(v, -2))) {
SQObjectType oType = sq_gettype(v, -1);
sq_settop(v, top);
return oType != OT_NULL;
}
sq_settop(v, top);
return false;
}
void sqsetdelegate(HSQOBJECT obj, HSQOBJECT del) {
HSQUIRRELVM v = g_twp->getVm();
sqpush(v, obj);
sqpush(v, del);
sq_setdelegate(v, -2);
sq_pop(v, 1);
}
HSQOBJECT sqrootTbl(HSQUIRRELVM v) {
HSQOBJECT result;
sq_resetobject(&result);
sq_pushroottable(v);
sq_getstackobj(v, -1, &result);
sq_pop(v, 1);
return result;
}
void sqcall(const char *name, const Common::Array &args) {
HSQUIRRELVM v = g_twp->getVm();
HSQOBJECT o = sqrootTbl(v);
SQInteger top = sq_gettop(v);
sqpushfunc(v, o, name);
sq_pushobject(v, o);
for (size_t i = 0; i < args.size(); i++) {
sq_pushobject(v, args[i]);
}
sq_call(v, 1 + args.size(), SQFalse, SQTrue);
sq_settop(v, top);
}
int getId(HSQOBJECT table) {
SQInteger result = 0;
(void)sqgetf(table, "_id", result);
return (int)result;
}
Common::SharedPtr sqroom(HSQOBJECT table) {
int id = getId(table);
return getRoom(id);
}
Common::SharedPtr getRoom(int id) {
for (size_t i = 0; i < g_twp->_rooms.size(); i++) {
Common::SharedPtr room(g_twp->_rooms[i]);
if (getId(room->_table) == id)
return room;
}
return nullptr;
}
Common::SharedPtr sqroom(HSQUIRRELVM v, int i) {
HSQOBJECT table;
if (SQ_SUCCEEDED(sqget(v, i, table))) {
return sqroom(table);
}
return nullptr;
}
Common::SharedPtr