Eliminate polymorphism from the CommandInterface hierarchy

This is similar to what I did to Gravity in 9068920de3dd. The idea is that we can choose between the implementations at compile time.
This commit is contained in:
Tamás Bálint Misius 2024-01-24 18:45:55 +01:00
parent d87130bd66
commit 7a10847780
No known key found for this signature in database
GPG Key ID: 5B472A12F6ECA9F2
19 changed files with 252 additions and 209 deletions

View File

@ -90,7 +90,7 @@ GameController::GameController():
gameView->SetDebugHUD(GlobalPrefs::Ref().Get("Renderer.DebugMode", false));
CommandInterface::Create(this, gameModel);
commandInterface = CommandInterface::Create(this, gameModel);
Client::Ref().AddListener(this);
@ -146,7 +146,7 @@ GameController::~GameController()
{
delete *iter;
}
delete commandInterface;
commandInterface.reset();
delete gameModel;
if (gameView->CloseActiveWindow())
{
@ -1386,7 +1386,7 @@ void GameController::OpenOptions()
void GameController::ShowConsole()
{
if (!console)
console = new ConsoleController(NULL, commandInterface);
console = new ConsoleController(NULL, commandInterface.get());
if (console->GetView() != ui::Engine::Ref().GetWindow())
ui::Engine::Ref().ShowWindow(console->GetView());
}

View File

@ -1,4 +1,5 @@
#pragma once
#include "lua/CommandInterfacePtr.h"
#include "client/ClientListener.h"
#include "client/StartupInfo.h"
#include "gui/interface/Point.h"
@ -28,7 +29,6 @@ class LocalBrowserController;
class SearchController;
class PreviewController;
class RenderController;
class CommandInterface;
class VideoBuffer;
class Tool;
class Menu;
@ -39,6 +39,8 @@ class TagsController;
class ConsoleController;
class GameController: public ClientListener
{
CommandInterfacePtr commandInterface;
private:
bool firstTick;
int foundSignID;

View File

@ -1712,7 +1712,7 @@ void GameModel::BeforeSim()
{
if (!sim->sys_pause || sim->framerender)
{
commandInterface->HandleEvent(BeforeSimEvent{});
CommandInterface::Ref().HandleEvent(BeforeSimEvent{});
}
sim->BeforeSim();
}
@ -1720,5 +1720,5 @@ void GameModel::BeforeSim()
void GameModel::AfterSim()
{
sim->AfterSim();
commandInterface->HandleEvent(AfterSimEvent{});
CommandInterface::Ref().HandleEvent(AfterSimEvent{});
}

View File

@ -17,21 +17,12 @@
#include <cstring>
#include <deque>
CommandInterface *commandInterface = nullptr;
CommandInterface::CommandInterface(GameController *newGameController, GameModel *newGameModel)
{
assert(!commandInterface);
commandInterface = this;
this->m = newGameModel;
this->c = newGameController;
}
CommandInterface::~CommandInterface()
{
commandInterface = nullptr;
}
void CommandInterface::Log(LogType type, String message)
{
m->Log(message, type == LogError || type == LogNotice);
@ -80,7 +71,7 @@ String CommandInterface::GetLastError()
return lastError;
}
int CommandInterface::Command(String command)
int CommandInterface::PlainCommand(String command)
{
lastError = "";
std::deque<String> words;
@ -273,7 +264,7 @@ AnyType CommandInterface::eval(std::deque<String> * words)
return StringType(word);
}
String CommandInterface::FormatCommand(String command)
String CommandInterface::PlainFormatCommand(String command)
{
std::deque<String> words;
std::deque<AnyType> commandWords;

View File

@ -1,4 +1,6 @@
#pragma once
#include "CommandInterfacePtr.h"
#include "common/ExplicitSingleton.h"
#include "common/String.h"
#include "gui/game/GameControllerEvents.h"
#include "TPTSTypes.h"
@ -8,34 +10,38 @@ class GameModel;
class GameController;
class Tool;
class CommandInterface
class CommandInterface : public ExplicitSingleton<CommandInterface>
{
protected:
String lastError;
GameModel * m;
GameController * c;
CommandInterface(GameController *newGameController, GameModel *newGameModel);
int PlainCommand(String command);
String PlainFormatCommand(String command);
public:
CommandInterface(GameController *newGameController, GameModel *newGameModel);
enum LogType { LogError, LogWarning, LogNotice };
enum FormatType { FormatInt, FormatString, FormatChar, FormatFloat, FormatElement };
int GetPropertyOffset(ByteString key, FormatType & format);
void Log(LogType type, String message);
//void AttachGameModel(GameModel * m);
virtual void OnTick() { }
virtual void Init() { }
void OnTick();
void Init();
virtual bool HandleEvent(const GameControllerEvent &event) { return true; }
bool HandleEvent(const GameControllerEvent &event);
virtual int Command(String command);
virtual String FormatCommand(String command);
int Command(String command);
String FormatCommand(String command);
void SetLastError(String err)
{
lastError = err;
}
String GetLastError();
virtual ~CommandInterface();
AnyType eval(std::deque<String> * words);
int parseNumber(String str);
@ -48,7 +54,5 @@ public:
AnyType tptS_quit(std::deque<String> * words);
ValueType testType(String word);
static CommandInterface *Create(GameController *newGameController, GameModel *newGameModel);
static CommandInterfacePtr Create(GameController *newGameController, GameModel *newGameModel);
};
extern CommandInterface *commandInterface;

View File

@ -0,0 +1,9 @@
#pragma once
#include <memory>
class CommandInterface;
struct CommandInterfaceDeleter
{
void operator ()(CommandInterface *ptr) const;
};
using CommandInterfacePtr = std::unique_ptr<CommandInterface, CommandInterfaceDeleter>;

View File

@ -18,7 +18,7 @@ LuaComponent::LuaComponent(lua_State *L) : component(nullptr), owner_ref(LUA_REF
{
this->L = L; // I don't get how this doesn't cause crashes later on
ci = static_cast<LuaScriptInterface *>(commandInterface);
ci = static_cast<LuaScriptInterface *>(&CommandInterface::Ref());
}
int LuaComponent::position(lua_State *L)

View File

@ -102,7 +102,7 @@ static int luaUpdateWrapper(UPDATE_FUNC_ARGS)
{
return 0;
}
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
auto &builtinElements = GetElements();
auto *builtinUpdate = builtinElements[parts[i].type].Update;
auto &customElements = lsi->customElements;
@ -152,7 +152,7 @@ static int luaGraphicsWrapper(GRAPHICS_FUNC_ARGS)
{
return Element::defaultGraphics(GRAPHICS_FUNC_SUBCALL_ARGS);
}
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
auto &customElements = lsi->customElements;
auto *sim = lsi->sim;
if (customElements[cpart->type].graphics)
@ -215,7 +215,7 @@ static void luaCreateWrapper(ELEMENT_CREATE_FUNC_ARGS)
{
return;
}
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
auto &customElements = lsi->customElements;
if (customElements[sim->parts[i].type].create)
{
@ -242,7 +242,7 @@ static bool luaCreateAllowedWrapper(ELEMENT_CREATE_ALLOWED_FUNC_ARGS)
// instances of something that should be limited to one instance.
return 1;
}
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
auto &customElements = lsi->customElements;
bool ret = false;
if (customElements[t].createAllowed)
@ -273,7 +273,7 @@ static void luaChangeTypeWrapper(ELEMENT_CHANGETYPE_FUNC_ARGS)
{
return;
}
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
auto &customElements = lsi->customElements;
if (customElements[sim->parts[i].type].changeType)
{
@ -297,7 +297,7 @@ static bool luaCtypeDrawWrapper(CTYPEDRAW_FUNC_ARGS)
{
return false;
}
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
auto &customElements = lsi->customElements;
bool ret = false;
if (customElements[sim->parts[i].type].ctypeDraw)
@ -323,7 +323,7 @@ static bool luaCtypeDrawWrapper(CTYPEDRAW_FUNC_ARGS)
static int allocate(lua_State *L)
{
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
luaL_checktype(L, 1, LUA_TSTRING);
luaL_checktype(L, 2, LUA_TSTRING);
auto group = tpt_lua_toByteString(L, 1).ToUpper();
@ -410,7 +410,7 @@ static int allocate(lua_State *L)
static int element(lua_State *L)
{
auto &builtinElements = GetElements();
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
auto &customElements = lsi->customElements;
int id = luaL_checkinteger(L, 1);
if (!SimulationData::CRef().IsElementOrNone(id))
@ -556,7 +556,7 @@ static int element(lua_State *L)
static int property(lua_State *L)
{
auto &builtinElements = GetElements();
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
auto &customElements = lsi->customElements;
int id = luaL_checkinteger(L, 1);
if (!SimulationData::CRef().IsElementOrNone(id))
@ -750,7 +750,7 @@ static int ffree(lua_State *L)
std::unique_lock lk(sd.elementGraphicsMx);
sd.elements[id].Enabled = false;
}
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
lsi->gameModel->BuildMenus();
lua_getglobal(L, "elements");
@ -775,7 +775,7 @@ static int loadDefault(lua_State *L)
std::unique_lock lk(sd.elementGraphicsMx);
auto &elements = sd.elements;
auto &builtinElements = GetElements();
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
{
auto loadDefaultOne = [L, &elements, &builtinElements](int id) {
lua_getglobal(L, "elements");

View File

@ -4,7 +4,7 @@
static int fregister(lua_State *L)
{
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
int eventType = luaL_checkinteger(L, 1);
luaL_checktype(L, 2, LUA_TFUNCTION);
if (eventType < 0 || eventType >= int(lsi->gameControllerEventHandlers.size()))
@ -21,7 +21,7 @@ static int fregister(lua_State *L)
static int unregister(lua_State *L)
{
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
int eventType = luaL_checkinteger(L, 1);
luaL_checktype(L, 2, LUA_TFUNCTION);
if (eventType < 0 || eventType >= int(lsi->gameControllerEventHandlers.size()))

View File

@ -13,7 +13,7 @@ static int32_t int32Truncate(double n)
static std::variant<Graphics *, Renderer *> currentGraphics()
{
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
if (lsi->eventTraits & eventTraitSimGraphics)
{
return lsi->ren;
@ -71,7 +71,7 @@ static int drawPixel(lua_State *L)
else if (b > 255) b = 255;
if (a < 0 ) a = 0 ;
else if (a > 255) a = 255;
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
lsi->g->BlendPixel({ x, y }, RGBA<uint8_t>(r, g, b, a));
return 0;
}
@ -259,7 +259,7 @@ static int getHexColor(lua_State *L)
static int setClipRect(lua_State *L)
{
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
if (lsi->eventTraits & eventTraitSimGraphics)
{
return luaL_error(L, "simulation graphics do not support clip rects");

View File

@ -52,7 +52,7 @@ static int beginMessageBox(lua_State *L)
auto cb = std::make_shared<LuaSmartRef>(); // * Bind to main lua state (might be different from L).
cb->Assign(L, lua_gettop(L));
new InformationMessage(title, message, large, { [cb]() {
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
auto L = lsi->L;
cb->Push(L);
if (lua_isfunction(L, -1))
@ -76,7 +76,7 @@ static int beginThrowError(lua_State *L)
auto cb = std::make_shared<LuaSmartRef>(); // * Bind to main lua state (might be different from L).
cb->Assign(L, lua_gettop(L));
new ErrorMessage("Error", errorMessage, { [cb]() {
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
auto L = lsi->L;
cb->Push(L);
if (lua_isfunction(L, -1))
@ -103,7 +103,7 @@ static int beginInput(lua_State *L)
auto cb = std::make_shared<LuaSmartRef>(); // * Bind to main lua state (might be different from L).
cb->Assign(L, lua_gettop(L));
auto handle = [cb](std::optional<String> input) {
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
auto L = lsi->L;
cb->Push(L);
if (lua_isfunction(L, -1))
@ -142,7 +142,7 @@ static int beginConfirm(lua_State *L)
auto cb = std::make_shared<LuaSmartRef>(); // * Bind to main lua state (might be different from L).
cb->Assign(L, lua_gettop(L));
auto handle = [cb](int result) {
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
auto L = lsi->L;
cb->Push(L);
if (lua_isfunction(L, -1))
@ -168,7 +168,7 @@ static int beginConfirm(lua_State *L)
static int console(lua_State *L)
{
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
int acount = lua_gettop(L);
if (acount == 0)
{
@ -184,7 +184,7 @@ static int console(lua_State *L)
static int brushID(lua_State *L)
{
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
if (lua_gettop(L) < 1)
{
lua_pushnumber(L, lsi->gameModel->GetBrushID());
@ -201,7 +201,7 @@ static int brushID(lua_State *L)
static int brushRadius(lua_State *L)
{
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
if (lua_gettop(L) < 1)
{
auto radius = lsi->gameModel->GetBrush().GetRadius();
@ -215,7 +215,7 @@ static int brushRadius(lua_State *L)
static int mousePosition(lua_State *L)
{
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
auto pos = lsi->gameController->GetView()->GetMousePosition();
lua_pushnumber(L, pos.X);
lua_pushnumber(L, pos.Y);
@ -223,7 +223,7 @@ static int mousePosition(lua_State *L)
}
static int activeTool(lua_State *L)
{ auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
{ auto *lsi = GetLSI();
auto index = luaL_checkint(L, 1);
if (index < 0 || index >= NUM_TOOLINDICES)
{
@ -246,7 +246,7 @@ static int activeTool(lua_State *L)
static int addComponent(lua_State *L)
{
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
void *opaque = nullptr;
LuaComponent *luaComponent = nullptr;
if ((opaque = Luna<LuaButton>::tryGet(L, 1)))
@ -279,7 +279,7 @@ static int addComponent(lua_State *L)
static int removeComponent(lua_State *L)
{
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
void *opaque = nullptr;
LuaComponent *luaComponent = nullptr;
if ((opaque = Luna<LuaButton>::tryGet(L, 1)))
@ -313,7 +313,7 @@ static int removeComponent(lua_State *L)
static int grabTextInput(lua_State *L)
{
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
lsi->textInputRefcount += 1;
lsi->gameController->GetView()->DoesTextInput = lsi->textInputRefcount > 0;
return 0;
@ -321,7 +321,7 @@ static int grabTextInput(lua_State *L)
static int dropTextInput(lua_State *L)
{
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
lsi->textInputRefcount -= 1;
lsi->gameController->GetView()->DoesTextInput = lsi->textInputRefcount > 0;
return 0;
@ -356,7 +356,7 @@ static int closeWindow(lua_State *L)
static int perfectCircleBrush(lua_State *L)
{
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
if (!lua_gettop(L))
{
lua_pushboolean(L, lsi->gameModel->GetPerfectCircle());
@ -369,7 +369,7 @@ static int perfectCircleBrush(lua_State *L)
static int activeMenu(lua_State *L)
{
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
int acount = lua_gettop(L);
if (acount == 0)
{
@ -401,7 +401,7 @@ static int menuEnabled(lua_State *L)
auto &sd = SimulationData::Ref();
sd.msections[menusection].doshow = enabled;
}
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
lsi->gameModel->BuildMenus();
return 0;
}
@ -415,7 +415,7 @@ static int numMenus(lua_State *L)
luaL_checktype(L, 1, LUA_TBOOLEAN);
onlyEnabled = lua_toboolean(L, 1);
}
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
lua_pushinteger(L, lsi->gameController->GetNumMenus(onlyEnabled));
return 1;
}

View File

@ -12,7 +12,7 @@
static int getUserName(lua_State *L)
{
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
if (lsi->gameModel->GetUser().UserID)
{
tpt_lua_pushByteString(L, lsi->gameModel->GetUser().Username);
@ -24,7 +24,7 @@ static int getUserName(lua_State *L)
static int installScriptManager(lua_State *L)
{
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
if (lsi->scriptManagerDownload)
{
new ErrorMessage("Script download", "A script download is already pending");
@ -43,7 +43,7 @@ static int installScriptManager(lua_State *L)
void LuaMisc::Tick(lua_State *L)
{
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
if (lsi->scriptManagerDownload && lsi->scriptManagerDownload->CheckDone())
{
struct Status
@ -117,7 +117,7 @@ void LuaMisc::Tick(lua_State *L)
static int flog(lua_State *L)
{
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
int args = lua_gettop(L);
String text;
bool hasText = false;
@ -154,7 +154,7 @@ static int screenshot(lua_State *L)
int captureUI = luaL_optint(L, 1, 0);
int fileType = luaL_optint(L, 2, 0);
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
ByteString filename = lsi->gameController->TakeScreenshot(captureUI, fileType);
if (filename.size())
{
@ -169,7 +169,7 @@ static int record(lua_State *L)
if (!lua_isboolean(L, -1))
return luaL_typerror(L, 1, lua_typename(L, LUA_TBOOLEAN));
bool record = lua_toboolean(L, -1);
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
int recordingFolder = lsi->gameController->Record(record);
lua_pushinteger(L, recordingFolder);
return 1;
@ -182,7 +182,7 @@ static int compatChunk(lua_State *L)
}
static int debug(lua_State *L)
{
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
int acount = lua_gettop(L);
if (acount == 0)
{

View File

@ -6,7 +6,7 @@
static int renderModes(lua_State *L)
{
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
int args = lua_gettop(L);
if(args)
{
@ -40,7 +40,7 @@ static int renderModes(lua_State *L)
static int hud(lua_State *L)
{
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
int acount = lua_gettop(L);
if (acount == 0)
{
@ -54,7 +54,7 @@ static int hud(lua_State *L)
static int debugHud(lua_State *L)
{
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
int acount = lua_gettop(L);
if (acount == 0)
{
@ -68,7 +68,7 @@ static int debugHud(lua_State *L)
static int useDisplayPreset(lua_State *L)
{
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
int cmode = luaL_optint(L, 1, 3)+1;
if (cmode == 11)
cmode = 0;
@ -81,7 +81,7 @@ static int useDisplayPreset(lua_State *L)
static int fireSize(lua_State *L)
{
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
if (lua_gettop(L) < 1)
{
lua_pushnumber(L, lsi->gameModel->GetRenderer()->GetFireIntensity());
@ -94,7 +94,7 @@ static int fireSize(lua_State *L)
static int displayModes(lua_State *L)
{
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
int args = lua_gettop(L);
if(args)
{
@ -128,7 +128,7 @@ static int displayModes(lua_State *L)
static int colorMode(lua_State *L)
{
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
int args = lua_gettop(L);
if(args)
{
@ -145,7 +145,7 @@ static int colorMode(lua_State *L)
static int decorations(lua_State *L)
{
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
int acount = lua_gettop(L);
if (acount == 0)
{
@ -160,7 +160,7 @@ static int decorations(lua_State *L)
static int grid(lua_State *L)
{
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
int acount = lua_gettop(L);
if (acount == 0)
{
@ -174,7 +174,7 @@ static int grid(lua_State *L)
static int showBrush(lua_State *L)
{
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
int acount = lua_gettop(L);
if (acount == 0)
{
@ -193,7 +193,7 @@ static int depth3d(lua_State *L)
static int zoomEnabled(lua_State *L)
{
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
if (lua_gettop(L) == 0)
{
lua_pushboolean(L, lsi->ren->zoomEnabled);
@ -209,7 +209,7 @@ static int zoomEnabled(lua_State *L)
static int zoomWindow(lua_State *L)
{
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
auto *ren = lsi->ren;
if (lua_gettop(L) == 0)
{
@ -237,7 +237,7 @@ static int zoomWindow(lua_State *L)
static int zoomScope(lua_State *L)
{
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
auto *ren = lsi->ren;
if (lua_gettop(L) == 0)
{

View File

@ -27,7 +27,7 @@ static int osExit(lua_State *L)
static int mathRandom(lua_State *L)
{
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
// only thing that matters is that the rng not be sim->rng when !(eventTraits & eventTraitSimRng)
auto &rng = (lsi->eventTraits & eventTraitSimRng) ? lsi->sim->rng : interfaceRng;
int lower, upper;
@ -72,7 +72,7 @@ static int mathRandomseed(lua_State *L)
static void hook(lua_State *L, lua_Debug * ar)
{
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
if (ar->event == LUA_HOOKCOUNT && int(Platform::GetTime() - lsi->luaExecutionStart) > lsi->luaHookTimeout)
{
luaL_error(L, "Error: Script not responding");
@ -107,7 +107,7 @@ int LuaToLoggableString(lua_State *L, int n)
String LuaGetError()
{
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
LuaToLoggableString(lsi->L, -1);
String err = tpt_lua_optString(lsi->L, -1, "failed to execute");
lua_pop(lsi->L, 1);
@ -197,8 +197,10 @@ void LuaScriptInterface::InitCustomCanMove()
}
}
void LuaScriptInterface::Init()
void CommandInterface::Init()
{
auto *lsi = static_cast<LuaScriptInterface *>(this);
auto *L = lsi->L;
if (Platform::FileExists("autorun.lua"))
{
if(luaL_loadfile(L, "autorun.lua") || tpt_lua_pcall(L, 0, 0, 0, eventTraitNone))
@ -287,7 +289,7 @@ void LuaSetProperty(lua_State *L, StructProperty property, intptr_t propertyAddr
void LuaSetParticleProperty(lua_State *L, int particleID, StructProperty property, intptr_t propertyAddress, int stackPos)
{
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
auto *sim = lsi->sim;
if (property.Name == "type")
{
@ -373,10 +375,12 @@ static int pushGameControllerEvent(lua_State *L, const GameControllerEvent &even
return 0;
}
bool LuaScriptInterface::HandleEvent(const GameControllerEvent &event)
bool CommandInterface::HandleEvent(const GameControllerEvent &event)
{
auto *lsi = static_cast<LuaScriptInterface *>(this);
auto *L = lsi->L;
bool cont = true;
gameControllerEventHandlers[event.index()].Push(L);
lsi->gameControllerEventHandlers[event.index()].Push(L);
int len = lua_objlen(L, -1);
for (int i = 1; i <= len && cont; i++)
{
@ -413,35 +417,38 @@ bool LuaScriptInterface::HandleEvent(const GameControllerEvent &event)
return cont;
}
void LuaScriptInterface::OnTick()
void CommandInterface::OnTick()
{
LuaMisc::Tick(L);
auto *lsi = static_cast<LuaScriptInterface *>(this);
LuaMisc::Tick(lsi->L);
HandleEvent(TickEvent{});
}
int LuaScriptInterface::Command(String command)
int CommandInterface::Command(String command)
{
auto *lsi = static_cast<LuaScriptInterface *>(this);
auto *L = lsi->L;
lastError = "";
luacon_hasLastError = false;
lsi->luacon_hasLastError = false;
if (command[0] == '!')
{
int ret = CommandInterface::Command(command.Substr(1));
int ret = PlainCommand(command.Substr(1));
lastError = GetLastError();
return ret;
}
else
{
int level = lua_gettop(L), ret = -1;
currentCommand = true;
if (lastCode.length())
lastCode += "\n";
lastCode += command;
ByteString tmp = ("return " + lastCode).ToUtf8();
lsi->currentCommand = true;
if (lsi->lastCode.length())
lsi->lastCode += "\n";
lsi->lastCode += command;
ByteString tmp = ("return " + lsi->lastCode).ToUtf8();
luaL_loadbuffer(L, tmp.data(), tmp.size(), "@console");
if (lua_type(L, -1) != LUA_TFUNCTION)
{
lua_pop(L, 1);
ByteString lastCodeUtf8 = lastCode.ToUtf8();
ByteString lastCodeUtf8 = lsi->lastCode.ToUtf8();
luaL_loadbuffer(L, lastCodeUtf8.data(), lastCodeUtf8.size(), "@console");
}
if (lua_type(L, -1) != LUA_TFUNCTION)
@ -451,11 +458,11 @@ int LuaScriptInterface::Command(String command)
if (err.Contains("near '<eof>'")) //the idea stolen from lua-5.1.5/lua.c
lastError = "...";
else
lastCode = "";
lsi->lastCode = "";
}
else
{
lastCode = "";
lsi->lastCode = "";
ret = tpt_lua_pcall(L, 0, LUA_MULTRET, 0, eventTraitNone);
if (ret)
{
@ -489,7 +496,7 @@ int LuaScriptInterface::Command(String command)
}
}
currentCommand = false;
lsi->currentCommand = false;
return ret;
}
}
@ -670,11 +677,11 @@ static String highlight(String command)
return result.Build();
}
String LuaScriptInterface::FormatCommand(String command)
String CommandInterface::FormatCommand(String command)
{
if(command.size() && command[0] == '!')
{
return "!" + CommandInterface::FormatCommand(command.Substr(1));
return "!" + PlainFormatCommand(command.Substr(1));
}
else
return highlight(command);
@ -766,7 +773,7 @@ bool tpt_lua_equalsString(lua_State *L, int index, const char *data, size_t size
int tpt_lua_pcall(lua_State *L, int numArgs, int numResults, int errorFunc, EventTraits newEventTraits)
{
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
lsi->luaExecutionStart = Platform::GetTime();
struct AtReturn
{
@ -774,21 +781,26 @@ int tpt_lua_pcall(lua_State *L, int numArgs, int numResults, int errorFunc, Even
AtReturn(EventTraits newEventTraits)
{
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
oldEventTraits = lsi->eventTraits;
lsi->eventTraits = newEventTraits;
}
~AtReturn()
{
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
lsi->eventTraits = oldEventTraits;
}
} atReturn(newEventTraits);
return lua_pcall(L, numArgs, numResults, errorFunc);
}
CommandInterface *CommandInterface::Create(GameController *newGameController, GameModel *newGameModel)
CommandInterfacePtr CommandInterface::Create(GameController *newGameController, GameModel *newGameModel)
{
return new LuaScriptInterface(newGameController, newGameModel);
return CommandInterfacePtr(new LuaScriptInterface(newGameController, newGameModel));
}
void CommandInterfaceDeleter::operator ()(CommandInterface *ptr) const
{
delete static_cast<LuaScriptInterface *>(ptr);
}

View File

@ -3,7 +3,6 @@
#include "LuaSmartRef.h"
#include "CommandInterface.h"
#include "gui/game/GameControllerEvents.h"
#include "CommandInterface.h"
#include "simulation/StructProperty.h"
#include "simulation/ElementDefs.h"
#include <cstdint>
@ -95,13 +94,6 @@ public:
char customCanMove[PT_NUM][PT_NUM];
void InitCustomCanMove();
void OnTick() override;
bool HandleEvent(const GameControllerEvent &event) override;
void Init() override;
int Command(String command) override;
String FormatCommand(String command) override;
};
void tpt_lua_pushByteString(lua_State *L, const ByteString &str);
@ -198,3 +190,8 @@ namespace LuaSocket
void Open(lua_State *L);
void OpenTCP(lua_State *L);
}
inline LuaScriptInterface *GetLSI()
{
return static_cast<LuaScriptInterface *>(&CommandInterface::Ref());
}

View File

@ -18,7 +18,7 @@
static int ambientHeatSim(lua_State *L)
{
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
int acount = lua_gettop(L);
if (acount == 0)
{
@ -34,7 +34,7 @@ static int ambientHeatSim(lua_State *L)
static int heatSim(lua_State *L)
{
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
int acount = lua_gettop(L);
if (acount == 0)
{
@ -48,7 +48,7 @@ static int heatSim(lua_State *L)
static int newtonianGravity(lua_State *L)
{
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
int acount = lua_gettop(L);
if (acount == 0)
{
@ -66,7 +66,7 @@ static int newtonianGravity(lua_State *L)
static int paused(lua_State *L)
{
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
int acount = lua_gettop(L);
if (acount == 0)
{
@ -80,14 +80,14 @@ static int paused(lua_State *L)
static int partCount(lua_State *L)
{
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
lua_pushinteger(L, lsi->sim->NUM_PARTS);
return 1;
}
static int decoSpace(lua_State *L)
{
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
if (lua_gettop(L) < 1)
{
lua_pushnumber(L, lsi->gameModel->GetDecoSpace());
@ -171,7 +171,7 @@ static int LuaBlockMap(lua_State *L, Accessor accessor)
static int velocityX(lua_State *L)
{
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
return LuaBlockMap(L, MIN_PRESSURE, MAX_PRESSURE, [lsi](Vec2<int> p) -> float & {
return lsi->sim->vx[p.Y][p.X];
});
@ -179,7 +179,7 @@ static int velocityX(lua_State *L)
static int velocityY(lua_State *L)
{
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
return LuaBlockMap(L, MIN_PRESSURE, MAX_PRESSURE, [lsi](Vec2<int> p) -> float & {
return lsi->sim->vy[p.Y][p.X];
});
@ -187,7 +187,7 @@ static int velocityY(lua_State *L)
static int ambientHeat(lua_State *L)
{
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
return LuaBlockMap(L, MIN_TEMP, MAX_TEMP, [lsi](Vec2<int> p) -> float & {
return lsi->sim->hv[p.Y][p.X];
});
@ -195,7 +195,7 @@ static int ambientHeat(lua_State *L)
static int pressure(lua_State *L)
{
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
return LuaBlockMap(L, MIN_PRESSURE, MAX_PRESSURE, [lsi](Vec2<int> p) -> float & {
return lsi->sim->pv[p.Y][p.X];
});
@ -203,7 +203,7 @@ static int pressure(lua_State *L)
static int gravityMass(lua_State *L)
{
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
return LuaBlockMap(L, [lsi](Vec2<int> p) -> float & {
return lsi->sim->gravmap[p.Y * XCELLS + p.X];
});
@ -211,7 +211,7 @@ static int gravityMass(lua_State *L)
static int gravityField(lua_State *L)
{
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
auto pos = Vec2{ luaL_checkint(L, 1), luaL_checkint(L, 2) };
if (!CELLS.OriginRect().Contains(pos))
{
@ -224,7 +224,7 @@ static int gravityField(lua_State *L)
static int elecMap(lua_State *L)
{
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
return LuaBlockMap(L, [lsi](Vec2<int> p) -> unsigned char & {
return lsi->sim->emap[p.Y][p.X];
});
@ -232,7 +232,7 @@ static int elecMap(lua_State *L)
static int wallMap(lua_State *L)
{
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
return LuaBlockMap(L, 0, UI_WALLCOUNT - 1, [lsi](Vec2<int> p) -> unsigned char & {
return lsi->sim->bmap[p.Y][p.X];
});
@ -240,14 +240,14 @@ static int wallMap(lua_State *L)
static int fanVelocityX(lua_State *L)
{
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
return LuaBlockMap(L, [lsi](Vec2<int> p) -> float & {
return lsi->sim->fvx[p.Y][p.X];
});
}
static int fanVelocityY(lua_State *L)
{ auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
{ auto *lsi = GetLSI();
return LuaBlockMap(L, [lsi](Vec2<int> p) -> float & {
return lsi->sim->fvy[p.Y][p.X];
});
@ -255,7 +255,7 @@ static int fanVelocityY(lua_State *L)
static int partNeighbors(lua_State *L)
{
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
auto *sim = lsi->sim;
lua_newtable(L);
int id = 1;
@ -299,7 +299,7 @@ static int partNeighbors(lua_State *L)
static int partChangeType(lua_State *L)
{
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
int partIndex = lua_tointeger(L, 1);
if(partIndex < 0 || partIndex >= NPART || !lsi->sim->parts[partIndex].type)
return 0;
@ -309,7 +309,7 @@ static int partChangeType(lua_State *L)
static int partCreate(lua_State *L)
{
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
int newID = lua_tointeger(L, 1);
if (newID >= NPART || newID < -3)
{
@ -338,7 +338,7 @@ static int partCreate(lua_State *L)
static int partID(lua_State *L)
{
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
int x = lua_tointeger(L, 1);
int y = lua_tointeger(L, 2);
@ -360,7 +360,7 @@ static int partID(lua_State *L)
static int partPosition(lua_State *L)
{
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
auto *sim = lsi->sim;
int particleID = lua_tointeger(L, 1);
int argCount = lua_gettop(L);
@ -394,7 +394,7 @@ static int partPosition(lua_State *L)
static int partProperty(lua_State *L)
{
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
int argCount = lua_gettop(L);
int particleID = luaL_checkinteger(L, 1);
StructProperty property;
@ -461,7 +461,7 @@ static int partProperty(lua_State *L)
static int partKill(lua_State *L)
{
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
if(lua_gettop(L)==2)
lsi->sim->delete_part(lua_tointeger(L, 1), lua_tointeger(L, 2));
else
@ -475,7 +475,7 @@ static int partKill(lua_State *L)
static int partExists(lua_State *L)
{
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
int i = luaL_checkinteger(L, 1);
lua_pushboolean(L, i >= 0 && i < NPART && lsi->sim->parts[i].type);
return 1;
@ -483,7 +483,7 @@ static int partExists(lua_State *L)
static int createParts(lua_State *L)
{
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
int x = luaL_optint(L,1,-1);
int y = luaL_optint(L,2,-1);
int rx = luaL_optint(L,3,5);
@ -505,7 +505,7 @@ static int createParts(lua_State *L)
static int createLine(lua_State *L)
{
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
int x1 = luaL_optint(L,1,-1);
int y1 = luaL_optint(L,2,-1);
int x2 = luaL_optint(L,3,-1);
@ -528,7 +528,7 @@ static int createLine(lua_State *L)
static int createBox(lua_State *L)
{
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
int x1 = luaL_optint(L,1,-1);
int y1 = luaL_optint(L,2,-1);
int x2 = luaL_optint(L,3,-1);
@ -542,7 +542,7 @@ static int createBox(lua_State *L)
static int floodParts(lua_State *L)
{
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
int x = luaL_optint(L,1,-1);
int y = luaL_optint(L,2,-1);
int c = luaL_optint(L,3,lsi->gameModel->GetActiveTool(0)->ToolID);
@ -570,7 +570,7 @@ static int createWalls(lua_State *L)
if (c < 0 || c >= UI_WALLCOUNT)
return luaL_error(L, "Unrecognised wall id '%d'", c);
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
int ret = lsi->sim->CreateWalls(x, y, rx, ry, c, NULL);
lua_pushinteger(L, ret);
return 1;
@ -591,7 +591,7 @@ static int createWallLine(lua_State *L)
if (c < 0 || c >= UI_WALLCOUNT)
return luaL_error(L, "Unrecognised wall id '%d'", c);
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
lsi->sim->CreateWallLine(x1, y1, x2, y2, rx, ry, c, NULL);
return 0;
}
@ -609,7 +609,7 @@ static int createWallBox(lua_State *L)
if (c < 0 || c >= UI_WALLCOUNT)
return luaL_error(L, "Unrecognised wall id '%d'", c);
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
lsi->sim->CreateWallBox(x1, y1, x2, y2, c);
return 0;
}
@ -629,7 +629,7 @@ static int floodWalls(lua_State *L)
lua_pushinteger(L, 0);
return 1;
}
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
int ret = lsi->sim->FloodWalls(x, y, c, bm);
lua_pushinteger(L, ret);
return 1;
@ -653,7 +653,7 @@ static int toolBrush(lua_State *L)
else if (tool < 0 || tool > (int)sd.tools.size())
return luaL_error(L, "Invalid tool id '%d'", tool);
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
Brush *brush = lsi->gameModel->GetBrushByID(brushID);
if (!brush)
return luaL_error(L, "Invalid brush id '%d'", brushID);
@ -683,7 +683,7 @@ static int toolLine(lua_State *L)
if (tool < 0 || tool >= (int)sd.tools.size()+1)
return luaL_error(L, "Invalid tool id '%d'", tool);
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
Brush *brush = lsi->gameModel->GetBrushByID(brushID);
if (!brush)
return luaL_error(L, "Invalid brush id '%d'", brushID);
@ -722,7 +722,7 @@ static int toolBox(lua_State *L)
else if (tool < 0 || tool >= (int)sd.tools.size())
return luaL_error(L, "Invalid tool id '%d'", tool);
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
lsi->sim->ToolBox(x1, y1, x2, y2, tool, strength);
return 0;
}
@ -740,7 +740,7 @@ static int decoBrush(lua_State *L)
int tool = luaL_optint(L,9,DECO_DRAW);
int brushID = luaL_optint(L,10,BRUSH_CIRCLE);
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
Brush *brush = lsi->gameModel->GetBrushByID(brushID);
if (!brush)
return luaL_error(L, "Invalid brush id '%d'", brushID);
@ -769,7 +769,7 @@ static int decoLine(lua_State *L)
if (x1 < 0 || x2 < 0 || x1 >= XRES || x2 >= XRES || y1 < 0 || y2 < 0 || y1 >= YRES || y2 >= YRES)
return luaL_error(L, "coordinates out of range (%d,%d),(%d,%d)", x1, y1, x2, y2);
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
Brush *brush = lsi->gameModel->GetBrushByID(brushID);
if (!brush)
return luaL_error(L, "Invalid brush id '%d'", brushID);
@ -795,14 +795,14 @@ static int decoBox(lua_State *L)
if (x1 < 0 || x2 < 0 || x1 >= XRES || x2 >= XRES || y1 < 0 || y2 < 0 || y1 >= YRES || y2 >= YRES)
return luaL_error(L, "coordinates out of range (%d,%d),(%d,%d)", x1, y1, x2, y2);
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
lsi->sim->ApplyDecorationBox(x1, y1, x2, y2, r, g, b, a, tool);
return 0;
}
static int decoColor(lua_State *L)
{
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
int acount = lua_gettop(L);
RGBA<uint8_t> color(0, 0, 0, 0);
if (acount == 0)
@ -835,7 +835,7 @@ static int floodDeco(lua_State *L)
if (x < 0 || x >= XRES || y < 0 || y >= YRES)
return luaL_error(L, "coordinates out of range (%d,%d)", x, y);
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
// hilariously broken, intersects with console and all Lua graphics
auto loc = RGB<uint8_t>::Unpack(lsi->ren->GetPixel({ x, y }));
lsi->sim->ApplyDecorationFill(lsi->ren, x, y, r, g, b, a, loc.Red, loc.Green, loc.Blue);
@ -844,14 +844,14 @@ static int floodDeco(lua_State *L)
static int clearSim(lua_State *L)
{
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
lsi->gameController->ClearSim();
return 0;
}
static int clearRect(lua_State *L)
{
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
int x = luaL_checkint(L,1);
int y = luaL_checkint(L,2);
int w = luaL_checkint(L,3)-1;
@ -862,7 +862,7 @@ static int clearRect(lua_State *L)
static int resetTemp(lua_State *L)
{
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
auto *sim = lsi->sim;
auto &sd = SimulationData::CRef();
auto &elements = sd.elements;
@ -900,7 +900,7 @@ static int resetPressure(lua_State *L)
width = XCELLS-x1;
if(y1+height > YCELLS-1)
height = YCELLS-y1;
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
for (int nx = x1; nx<x1+width; nx++)
for (int ny = y1; ny<y1+height; ny++)
{
@ -916,7 +916,7 @@ static int saveStamp(lua_State *L)
int w = luaL_optint(L,3,XRES-1);
int h = luaL_optint(L,4,YRES-1);
bool includePressure = luaL_optint(L, 5, 1);
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
ByteString name = lsi->gameController->StampRegion(ui::Point(x, y), ui::Point(x+w, y+h), includePressure);
tpt_lua_pushByteString(L, name);
return 1;
@ -967,7 +967,7 @@ static int loadStamp(lua_State *L)
}
gameSave->Transform(transform, { remX, remY });
}
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
lsi->sim->Load(gameSave.get(), includePressure, { quoX, quoY });
lua_pushinteger(L, 1);
@ -1035,21 +1035,21 @@ static int loadSave(lua_State *L)
int saveID = luaL_optint(L,1,0);
int instant = luaL_optint(L,2,0);
int history = luaL_optint(L,3,0); //Exact second a previous save was saved
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
lsi->gameController->OpenSavePreview(saveID, history, instant ? savePreviewInstant : savePreviewNormal);
return 0;
}
static int reloadSave(lua_State *L)
{
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
lsi->gameController->ReloadSim();
return 0;
}
static int getSaveID(lua_State *L)
{
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
auto *tempSave = lsi->gameModel->GetSave();
if (tempSave)
{
@ -1062,7 +1062,7 @@ static int getSaveID(lua_State *L)
static int adjustCoords(lua_State *L)
{
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
int x = luaL_optint(L,1,0);
int y = luaL_optint(L,2,0);
ui::Point Coords = lsi->gameController->PointTranslate(ui::Point(x, y));
@ -1073,7 +1073,7 @@ static int adjustCoords(lua_State *L)
static int prettyPowders(lua_State *L)
{
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
int acount = lua_gettop(L);
if (acount == 0)
{
@ -1088,7 +1088,7 @@ static int prettyPowders(lua_State *L)
static int gravityGrid(lua_State *L)
{
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
int acount = lua_gettop(L);
if (acount == 0)
{
@ -1103,7 +1103,7 @@ static int gravityGrid(lua_State *L)
static int edgeMode(lua_State *L)
{
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
int acount = lua_gettop(L);
if (acount == 0)
{
@ -1117,7 +1117,7 @@ static int edgeMode(lua_State *L)
static int gravityMode(lua_State *L)
{
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
int acount = lua_gettop(L);
if (acount == 0)
{
@ -1131,7 +1131,7 @@ static int gravityMode(lua_State *L)
static int customGravity(lua_State *L)
{
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
auto *sim = lsi->sim;
int acount = lua_gettop(L);
if (acount == 0)
@ -1153,7 +1153,7 @@ static int customGravity(lua_State *L)
static int airMode(lua_State *L)
{
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
int acount = lua_gettop(L);
if (acount == 0)
{
@ -1167,7 +1167,7 @@ static int airMode(lua_State *L)
static int waterEqualization(lua_State *L)
{
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
int acount = lua_gettop(L);
if (acount == 0)
{
@ -1181,7 +1181,7 @@ static int waterEqualization(lua_State *L)
static int ambientAirTemp(lua_State *L)
{
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
int acount = lua_gettop(L);
if (acount == 0)
{
@ -1199,14 +1199,14 @@ static int elementCount(lua_State *L)
if (element < 0 || element >= PT_NUM)
return luaL_error(L, "Invalid element ID (%d)", element);
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
lua_pushnumber(L, lsi->sim->elementCount[element]);
return 1;
}
static int canMove(lua_State *L)
{
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
int movingElement = luaL_checkint(L, 1);
int destinationElement = luaL_checkint(L, 2);
if (movingElement < 0 || movingElement >= PT_NUM)
@ -1252,7 +1252,7 @@ static int brushClosure(lua_State *L)
static int brush(lua_State *L)
{
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
int argCount = lua_gettop(L);
int positionX = luaL_checkint(L, 1);
int positionY = luaL_checkint(L, 2);
@ -1290,7 +1290,7 @@ static int brush(lua_State *L)
static int partsClosure(lua_State *L)
{
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
for (int i = lua_tointeger(L, lua_upvalueindex(1)); i <= lsi->sim->parts_lastActiveIndex; ++i)
{
if (lsi->sim->parts[i].type)
@ -1306,7 +1306,7 @@ static int partsClosure(lua_State *L)
static int neighboursClosure(lua_State *L)
{
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
int cx = lua_tointeger(L, lua_upvalueindex(1));
int cy = lua_tointeger(L, lua_upvalueindex(2));
int rx = lua_tointeger(L, lua_upvalueindex(3));
@ -1387,7 +1387,7 @@ static int parts(lua_State *L)
static int pmap(lua_State *L)
{
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
int x = luaL_checkint(L, 1);
int y = luaL_checkint(L, 2);
if (x < 0 || x >= XRES || y < 0 || y >= YRES)
@ -1401,7 +1401,7 @@ static int pmap(lua_State *L)
static int photons(lua_State *L)
{
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
int x = luaL_checkint(L, 1);
int y = luaL_checkint(L, 2);
if (x < 0 || x >= XRES || y < 0 || y >= YRES)
@ -1415,7 +1415,7 @@ static int photons(lua_State *L)
static int frameRender(lua_State *L)
{
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
if (lua_gettop(L) == 0)
{
lua_pushinteger(L, lsi->sim->framerender);
@ -1430,7 +1430,7 @@ static int frameRender(lua_State *L)
static int golSpeedRatio(lua_State *L)
{
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
if (lua_gettop(L) == 0)
{
lua_pushinteger(L, lsi->sim->GSPEED);
@ -1445,7 +1445,7 @@ static int golSpeedRatio(lua_State *L)
static int takeSnapshot(lua_State *L)
{
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
lsi->gameController->HistorySnapshot();
return 0;
}
@ -1453,7 +1453,7 @@ static int takeSnapshot(lua_State *L)
static int historyRestore(lua_State *L)
{
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
bool successful = lsi->gameController->HistoryRestore();
lua_pushboolean(L, successful);
return 1;
@ -1461,7 +1461,7 @@ static int historyRestore(lua_State *L)
static int historyForward(lua_State *L)
{
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
bool successful = lsi->gameController->HistoryForward();
lua_pushboolean(L, successful);
return 1;
@ -1469,7 +1469,7 @@ static int historyForward(lua_State *L)
static int replaceModeFlags(lua_State *L)
{
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
if (lua_gettop(L) == 0)
{
lua_pushinteger(L, lsi->gameController->GetReplaceModeFlags());
@ -1536,14 +1536,14 @@ static int addCustomGol(lua_State *L)
if (!AddCustomGol(ruleString, nameString, color1, color2))
return luaL_error(L, "Duplicate name, cannot add");
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
lsi->gameModel->BuildMenus();
return 0;
}
static int removeCustomGol(lua_State *L)
{
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
ByteString nameString = tpt_lua_checkByteString(L, 1);
bool removedAny = lsi->gameModel->RemoveCustomGOLType("DEFAULT_PT_LIFECUST_" + nameString);
if (removedAny)
@ -1554,7 +1554,7 @@ static int removeCustomGol(lua_State *L)
static int lastUpdatedID(lua_State *L)
{
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
if (lsi->sim->debug_mostRecentlyUpdated != -1)
{
lua_pushinteger(L, lsi->sim->debug_mostRecentlyUpdated);
@ -1579,7 +1579,7 @@ static int updateUpTo(lua_State *L)
{
return luaL_error(L, "ID not in valid range");
}
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
lsi->sim->framerender = 1;
lsi->gameModel->UpdateUpTo(upTo + 1);
return 0;
@ -1587,7 +1587,7 @@ static int updateUpTo(lua_State *L)
static int temperatureScale(lua_State *L)
{
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
if (lua_gettop(L) == 0)
{
lua_pushinteger(L, lsi->gameModel->GetTemperatureScale());
@ -1614,7 +1614,7 @@ static int signsIndex(lua_State *L)
luaL_error(L, "Invalid sign ID (stop messing with things): %i", id);
return 0;
}
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
auto *sim = lsi->sim;
if (id >= (int)sim->signs.size())
{
@ -1662,7 +1662,7 @@ static int signsIndex(lua_State *L)
static int signsNewIndex(lua_State *L)
{
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
auto *sim = lsi->sim;
ByteString key = tpt_lua_checkByteString(L, 2);
@ -1732,7 +1732,7 @@ static int signsNewIndex(lua_State *L)
// Creates a new sign at the first open index
static int Sign_new(lua_State *L)
{
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
if (lsi->sim->signs.size() >= MAXSIGNS)
return lua_pushnil(L), 1;
@ -1756,7 +1756,7 @@ static int Sign_new(lua_State *L)
// Deletes a sign
static int Sign_delete(lua_State *L)
{
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
int signID = luaL_checkinteger(L, 1);
if (signID <= 0 || signID > (int)lsi->sim->signs.size())
return luaL_error(L, "Sign doesn't exist");
@ -1781,7 +1781,7 @@ static int resetVelocity(lua_State *L)
width = XCELLS-x1;
if(y1+height > YCELLS-1)
height = YCELLS-y1;
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
for (nx = x1; nx<x1+width; nx++)
for (ny = y1; ny<y1+height; ny++)
{
@ -1793,7 +1793,7 @@ static int resetVelocity(lua_State *L)
static int resetSpark(lua_State *L)
{
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
lsi->gameController->ResetSpark();
return 0;
}
@ -1814,7 +1814,7 @@ static int resetGravityField(lua_State *L)
width = XCELLS-x1;
if(y1+height > YCELLS-1)
height = YCELLS-y1;
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
auto *sim = lsi->sim;
for (nx = x1; nx<x1+width; nx++)
for (ny = y1; ny<y1+height; ny++)
@ -1828,7 +1828,7 @@ static int resetGravityField(lua_State *L)
static int randomSeed(lua_State *L)
{
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
if (lua_gettop(L))
{
lsi->sim->rng.state({
@ -1847,14 +1847,14 @@ static int randomSeed(lua_State *L)
static int hash(lua_State *L)
{
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
lua_pushinteger(L, lsi->sim->CreateSnapshot()->Hash());
return 1;
}
static int ensureDeterminism(lua_State *L)
{
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
if (lua_gettop(L))
{
lsi->sim->ensureDeterminism = lua_toboolean(L, 1);
@ -1866,7 +1866,7 @@ static int ensureDeterminism(lua_State *L)
void LuaSimulation::Open(lua_State *L)
{
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
auto &sd = SimulationData::CRef();
static const luaL_Reg reg[] = {
#define LFUNC(v) { #v, v }

View File

@ -3,7 +3,7 @@
void LuaSmartRef::Clear()
{
auto *lsi = static_cast<LuaScriptInterface *>(commandInterface);
auto *lsi = GetLSI();
if (lsi)
{
luaL_unref(lsi->L, LUA_REGISTRYINDEX, ref);

View File

@ -53,7 +53,7 @@ LuaWindow::LuaWindow(lua_State *L)
if (sizeY < 10)
sizeY = 10;
ci = static_cast<LuaScriptInterface *>(commandInterface);
ci = static_cast<LuaScriptInterface *>(&CommandInterface::Ref());
class DrawnWindow : public ui::Window
{

View File

@ -1,6 +1,34 @@
#include "CommandInterface.h"
CommandInterface *CommandInterface::Create(GameController * c, GameModel * m)
CommandInterfacePtr CommandInterface::Create(GameController *newGameController, GameModel *newGameModel)
{
return new CommandInterface(c, m);
return CommandInterfacePtr(new CommandInterface(newGameController, newGameModel));
}
void CommandInterfaceDeleter::operator ()(CommandInterface *ptr) const
{
delete ptr;
}
void CommandInterface::OnTick()
{
}
void CommandInterface::Init()
{
}
bool CommandInterface::HandleEvent(const GameControllerEvent &event)
{
return true;
}
int CommandInterface::Command(String command)
{
return PlainCommand(command);
}
String CommandInterface::FormatCommand(String command)
{
return PlainFormatCommand(command);
}