From 47f898ca5ada23dab99ccbbd7c9ad3e899b77af9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tam=C3=A1s=20B=C3=A1lint=20Misius?= Date: Sat, 30 Mar 2019 19:46:58 +0100 Subject: [PATCH] Tweak replace mode and specific delete mode (fixes #631) --- src/gui/game/GameController.cpp | 16 ++++++ src/gui/game/GameView.cpp | 6 ++- src/lua/LuaScriptInterface.cpp | 14 ++++- src/simulation/Simulation.cpp | 90 +++++++++++++++++++++------------ 4 files changed, 91 insertions(+), 35 deletions(-) diff --git a/src/gui/game/GameController.cpp b/src/gui/game/GameController.cpp index c9173455a..56c559bd5 100644 --- a/src/gui/game/GameController.cpp +++ b/src/gui/game/GameController.cpp @@ -1206,6 +1206,22 @@ int GameController::GetReplaceModeFlags() void GameController::SetReplaceModeFlags(int flags) { + int old_flags = gameModel->GetSimulation()->replaceModeFlags; + if (!(old_flags & REPLACE_MODE) && (flags & REPLACE_MODE)) + { + // if replace mode has just been enabled, disable specific delete + flags &= ~SPECIFIC_DELETE; + } + if (!(old_flags & SPECIFIC_DELETE) && (flags & SPECIFIC_DELETE)) + { + // if specific delete has just been enabled, disable replace mode + flags &= ~REPLACE_MODE; + } + if ((flags & SPECIFIC_DELETE) && (flags & REPLACE_MODE)) + { + // if both have just been enabled, arbitrarily disable one of them + flags &= ~SPECIFIC_DELETE; + } gameModel->GetSimulation()->replaceModeFlags = flags; } diff --git a/src/gui/game/GameView.cpp b/src/gui/game/GameView.cpp index 85a0723c1..dd2000ff9 100644 --- a/src/gui/game/GameView.cpp +++ b/src/gui/game/GameView.cpp @@ -559,8 +559,12 @@ public: else { if (v->CtrlBehaviour() && v->AltBehaviour() && !v->ShiftBehaviour()) - if (tool->GetIdentifier().BeginsWith("DEFAULT_PT_")) + { + if (tool->GetIdentifier().Contains("_PT_")) + { sender->SetSelectionState(3); + } + } if (sender->GetSelectionState() >= 0 && sender->GetSelectionState() <= 3) v->c->SetActiveTool(sender->GetSelectionState(), tool); diff --git a/src/lua/LuaScriptInterface.cpp b/src/lua/LuaScriptInterface.cpp index f32605a61..953062d8c 100644 --- a/src/lua/LuaScriptInterface.cpp +++ b/src/lua/LuaScriptInterface.cpp @@ -2581,8 +2581,18 @@ int LuaScriptInterface::elements_allocate(lua_State * l) group = ByteString(lua_tostring(l, 1)).ToUpper(); id = ByteString(lua_tostring(l, 2)).ToUpper(); - if(group == "DEFAULT") - return luaL_error(l, "You cannot create elements in the 'default' group."); + if (id.Contains("_")) + { + return luaL_error(l, "The element name may not contain '_'."); + } + if (group.Contains("_")) + { + return luaL_error(l, "The group name may not contain '_'."); + } + if (group == "DEFAULT") + { + return luaL_error(l, "You cannot create elements in the 'DEFAULT' group."); + } identifier = group + "_PT_" + id; diff --git a/src/simulation/Simulation.cpp b/src/simulation/Simulation.cpp index e4e40316d..5c2e962b7 100644 --- a/src/simulation/Simulation.cpp +++ b/src/simulation/Simulation.cpp @@ -1601,38 +1601,6 @@ int Simulation::CreateParts(int x, int y, int rx, int ry, int c, int flags) return !created; } -int Simulation::CreatePartFlags(int x, int y, int c, int flags) -{ - //delete - if (c == 0 && !(flags&REPLACE_MODE)) - delete_part(x, y); - //specific delete - else if ((flags&SPECIFIC_DELETE) && !(flags&REPLACE_MODE)) - { - if (!replaceModeSelected || TYP(pmap[y][x]) == replaceModeSelected || TYP(photons[y][x]) == replaceModeSelected) - delete_part(x, y); - } - //replace mode - else if (flags&REPLACE_MODE) - { - if (x<0 || y<0 || x>=XRES || y>=YRES) - return 0; - if (replaceModeSelected && TYP(pmap[y][x]) != replaceModeSelected && TYP(photons[y][x]) != replaceModeSelected) - return 0; - if ((pmap[y][x])) - { - delete_part(x, y); - if (c!=0) - create_part(-2, x, y, TYP(c), ID(c)); - } - } - //normal draw - else - if (create_part(-2, x, y, TYP(c), ID(c)) == -1) - return 1; - return 0; -} - void Simulation::CreateLine(int x1, int y1, int x2, int y2, int c, Brush * cBrush, int flags) { int x, y, dx, dy, sy, rx = cBrush->GetRadius().X, ry = cBrush->GetRadius().Y; @@ -1686,6 +1654,64 @@ void Simulation::CreateLine(int x1, int y1, int x2, int y2, int c, Brush * cBrus } } +int Simulation::CreatePartFlags(int x, int y, int c, int flags) +{ + if (x < 0 || y < 0 || x >= XRES || y >= YRES) + { + return 0; + } + + if (flags & REPLACE_MODE) + { + // if replace whatever and there's something to replace + // or replace X and there's a non-energy particle on top with type X + // or replace X and there's an energy particle on top with type X + if ((!replaceModeSelected && (photons[y][x] || pmap[y][x])) || + (!photons[y][x] && pmap[y][x] && TYP(pmap[y][x]) == replaceModeSelected) || + (photons[y][x] && TYP(photons[y][x]) == replaceModeSelected)) + { + if (c) + { + part_change_type(photons[y][x] ? ID(photons[y][x]) : ID(pmap[y][x]), x, y, TYP(c)); + } + else + { + delete_part(x, y); + } + } + return 0; + } + else if (!c) + { + delete_part(x, y); + return 0; + } + else if (flags & SPECIFIC_DELETE) + { + // if delete whatever and there's something to delete + // or delete X and there's a non-energy particle on top with type X + // or delete X and there's an energy particle on top with type X + if ((!replaceModeSelected && (photons[y][x] || pmap[y][x])) || + (!photons[y][x] && pmap[y][x] && TYP(pmap[y][x]) == replaceModeSelected) || + (photons[y][x] && TYP(photons[y][x]) == replaceModeSelected)) + { + delete_part(x, y); + } + return 0; + } + else + { + if (create_part(-2, x, y, TYP(c), ID(c)) == -1) + { + return 1; + } + return 0; + } + + // I'm sure at least one compiler exists that would complain if this wasn't here + return 0; +} + //Now simply creates a 0 pixel radius line without all the complicated flags / other checks void Simulation::CreateLine(int x1, int y1, int x2, int y2, int c) {