diff --git a/src/lua/LuaScriptInterface.cpp b/src/lua/LuaScriptInterface.cpp index 88a9b7461..720f8516b 100644 --- a/src/lua/LuaScriptInterface.cpp +++ b/src/lua/LuaScriptInterface.cpp @@ -567,6 +567,7 @@ void LuaScriptInterface::initSimulationAPI() {"decoColor", simulation_decoColor}, {"decoColour", simulation_decoColor}, {"clearSim", simulation_clearSim}, + {"clearRect", simulation_clearRect}, {"resetTemp", simulation_resetTemp}, {"resetPressure", simulation_resetPressure}, {"saveStamp", simulation_saveStamp}, @@ -1398,6 +1399,16 @@ int LuaScriptInterface::simulation_clearSim(lua_State * l) return 0; } +int LuaScriptInterface::simulation_clearRect(lua_State * l) +{ + int x = luaL_checkint(l,1); + int y = luaL_checkint(l,2); + int w = luaL_checkint(l,3)-1; + int h = luaL_checkint(l,4)-1; + luacon_sim->clear_area(x, y, w, h); + return 0; +} + int LuaScriptInterface::simulation_resetTemp(lua_State * l) { bool onlyConductors = luaL_optint(l, 1, 0); diff --git a/src/lua/LuaScriptInterface.h b/src/lua/LuaScriptInterface.h index 165447324..68a625a53 100644 --- a/src/lua/LuaScriptInterface.h +++ b/src/lua/LuaScriptInterface.h @@ -77,6 +77,7 @@ class LuaScriptInterface: public CommandInterface static int simulation_decoBox(lua_State * l); static int simulation_decoColor(lua_State * l); static int simulation_clearSim(lua_State * l); + static int simulation_clearRect(lua_State * l); static int simulation_resetTemp(lua_State * l); static int simulation_resetPressure(lua_State * l); static int simulation_saveStamp(lua_State * l); diff --git a/src/simulation/Simulation.cpp b/src/simulation/Simulation.cpp index 4501827d5..8f2dd11aa 100644 --- a/src/simulation/Simulation.cpp +++ b/src/simulation/Simulation.cpp @@ -356,27 +356,27 @@ void Simulation::Restore(const Snapshot & snap) void Simulation::clear_area(int area_x, int area_y, int area_w, int area_h) { - int i = 0; - for (i = 0; i <= parts_lastActiveIndex; i++) { + float fx = area_x-.5f, fy = area_y-.5f; + for (int i = 0; i <= parts_lastActiveIndex; i++) + { if (parts[i].type) - if (parts[i].x >= area_x && parts[i].x <= area_x + area_w && parts[i].y >= area_y && parts[i].y <= area_y + area_h) + if (parts[i].x >= fx && parts[i].x <= fx+area_w+1 && parts[i].y >= fy && parts[i].y <= fy+area_h+1) kill_part(i); } - int cx = 0; - int cy = 0; - for (cy=0; cy<=area_h; cy++) + int cx1 = area_x/CELL, cy1 = area_y/CELL, cx2 = (area_x+area_w)/CELL, cy2 = (area_y+area_h)/CELL; + for (int y = cy1; y <= cy2; y++) { - for (cx=0; cx<=area_w; cx++) + for (int x = cx1; x <= cx2; x++) { - if(bmap[(cy+area_y)/CELL][(cx+area_x)/CELL] == WL_GRAV) + if (bmap[y][x] == WL_GRAV) gravWallChanged = true; - bmap[(cy+area_y)/CELL][(cx+area_x)/CELL] = 0; - emap[(cy+area_y)/CELL][(cx+area_x)/CELL] = 0; + bmap[y][x] = 0; + emap[y][x] = 0; } } - for(int i = signs.size()-1; i >= 0; i--) + for( int i = signs.size()-1; i >= 0; i--) { - if(signs[i].text.length() && signs[i].x >= area_x && signs[i].y >= area_y && signs[i].x <= area_x+area_w && signs[i].y <= area_y+area_h) + if (signs[i].text.length() && signs[i].x >= area_x && signs[i].y >= area_y && signs[i].x <= area_x+area_w && signs[i].y <= area_y+area_h) { signs.erase(signs.begin()+i); }