From 29ea81164288f5b95b6a829aa4015d71f5183b1e Mon Sep 17 00:00:00 2001 From: jacob1 Date: Thu, 5 Oct 2023 21:32:09 -0400 Subject: [PATCH] Add enums for gravity/air/edge modes, add more constants to Lua Constants for the new enums were added, along with walls (in the sim.walls table), x/y center, and a few movement constants --- src/client/GameSave.cpp | 6 ++-- src/gui/game/GameController.cpp | 22 ++++++------ src/gui/game/GameModel.cpp | 8 ++--- src/lua/LuaScriptInterface.cpp | 59 ++++++++++++++++++++++++++++++-- src/simulation/Air.cpp | 14 ++++---- src/simulation/Simulation.cpp | 30 ++++++++-------- src/simulation/SimulationData.h | 15 ++++++++ src/simulation/elements/STKM.cpp | 8 ++--- 8 files changed, 115 insertions(+), 47 deletions(-) diff --git a/src/client/GameSave.cpp b/src/client/GameSave.cpp index 5d4533061..1ef394ff8 100644 --- a/src/client/GameSave.cpp +++ b/src/client/GameSave.cpp @@ -1251,8 +1251,8 @@ void GameSave::readPSv(const std::vector &dataVec) auto partP = blockP * CELL; if (ver<46) { - gravityMode = 0; - airMode = 0; + gravityMode = GRAV_VERTICAL; + airMode = AIR_ON; } PlaneAdapter> particleIDMap(RES, 0); @@ -2295,7 +2295,7 @@ std::pair> GameSave::serialiseOPS() const bson_append_string(&b, "platform", IDENT_PLATFORM); bson_append_string(&b, "ident", IDENT); bson_append_finish_object(&b); - if (gravityMode == 3) + if (gravityMode == GRAV_CUSTOM) { bson_append_double(&b, "customGravityX", double(customGravityX)); bson_append_double(&b, "customGravityY", double(customGravityY)); diff --git a/src/gui/game/GameController.cpp b/src/gui/game/GameController.cpp index b0c9e23f4..751168ce6 100644 --- a/src/gui/game/GameController.cpp +++ b/src/gui/game/GameController.cpp @@ -773,20 +773,20 @@ void GameController::ResetSpark() void GameController::SwitchGravity() { - gameModel->GetSimulation()->gravityMode = (gameModel->GetSimulation()->gravityMode+1)%4; + gameModel->GetSimulation()->gravityMode = (gameModel->GetSimulation()->gravityMode + 1) % NUM_GRAV_MODES; switch (gameModel->GetSimulation()->gravityMode) { - case 0: + case GRAV_VERTICAL: gameModel->SetInfoTip("Gravity: Vertical"); break; - case 1: + case GRAV_OFF: gameModel->SetInfoTip("Gravity: Off"); break; - case 2: + case GRAV_RADIAL: gameModel->SetInfoTip("Gravity: Radial"); break; - case 3: + case GRAV_CUSTOM: gameModel->SetInfoTip("Gravity: Custom"); break; } @@ -794,23 +794,23 @@ void GameController::SwitchGravity() void GameController::SwitchAir() { - gameModel->GetSimulation()->air->airMode = (gameModel->GetSimulation()->air->airMode+1)%5; + gameModel->GetSimulation()->air->airMode = (gameModel->GetSimulation()->air->airMode + 1) % NUM_AIR_MODES; switch (gameModel->GetSimulation()->air->airMode) { - case 0: + case AIR_ON: gameModel->SetInfoTip("Air: On"); break; - case 1: + case AIR_PRESSURE_OFF: gameModel->SetInfoTip("Air: Pressure Off"); break; - case 2: + case AIR_VELOCITY_OFF: gameModel->SetInfoTip("Air: Velocity Off"); break; - case 3: + case AIR_OFF: gameModel->SetInfoTip("Air: Off"); break; - case 4: + case AIR_NO_UPDATE: gameModel->SetInfoTip("Air: No Update"); break; } diff --git a/src/gui/game/GameModel.cpp b/src/gui/game/GameModel.cpp index 3c0e3c0ae..81250ff6e 100644 --- a/src/gui/game/GameModel.cpp +++ b/src/gui/game/GameModel.cpp @@ -51,7 +51,7 @@ GameModel::GameModel(): activeColourPreset(0), colourSelector(false), colour(255, 0, 0, 255), - edgeMode(0), + edgeMode(EDGE_VOID), ambientAirTemp(R_TEMP + 273.15f), decoSpace(0) { @@ -91,7 +91,7 @@ GameModel::GameModel(): ren->decorations_enable = prefs.Get("Renderer.Decorations", true); //Load config into simulation - edgeMode = prefs.Get("Simulation.EdgeMode", 0); // TODO: EdgeMode enum + edgeMode = prefs.Get("Simulation.EdgeMode", (int)EDGE_VOID); sim->SetEdgeMode(edgeMode); ambientAirTemp = float(R_TEMP) + 273.15f; { @@ -1332,10 +1332,10 @@ void GameModel::FrameStep(int frames) void GameModel::ClearSimulation() { //Load defaults - sim->gravityMode = 0; + sim->gravityMode = GRAV_VERTICAL; sim->customGravityX = 0.0f; sim->customGravityY = 0.0f; - sim->air->airMode = 0; + sim->air->airMode = AIR_ON; sim->legacy_enable = false; sim->water_equal_test = false; sim->SetEdgeMode(edgeMode); diff --git a/src/lua/LuaScriptInterface.cpp b/src/lua/LuaScriptInterface.cpp index 3a14b1a30..67e14bfb4 100644 --- a/src/lua/LuaScriptInterface.cpp +++ b/src/lua/LuaScriptInterface.cpp @@ -1215,6 +1215,8 @@ void LuaScriptInterface::initSimulationAPI() SETCONST(l, NCELL); SETCONST(l, XRES); SETCONST(l, YRES); + SETCONST(l, XCNTR); + SETCONST(l, YCNTR); SETCONST(l, NPART); SETCONST(l, NT); SETCONST(l, ST); @@ -1229,6 +1231,9 @@ void LuaScriptInterface::initSimulationAPI() SETCONSTF(l, MIN_TEMP); SETCONSTF(l, MAX_PRESSURE); SETCONSTF(l, MIN_PRESSURE); + SETCONST(l, ISTP); + SETCONSTF(l, CFDS); + SETCONSTF(l, SIM_MAXVELOCITY); SETCONST(l, TOOL_HEAT); SETCONST(l, TOOL_COOL); @@ -1239,6 +1244,7 @@ void LuaScriptInterface::initSimulationAPI() SETCONST(l, TOOL_MIX); SETCONST(l, TOOL_CYCL); lua_pushinteger(l, luacon_sim->tools.size()); lua_setfield(l, -2, "TOOL_WIND"); + SETCONST(l, DECO_DRAW); SETCONST(l, DECO_CLEAR); SETCONST(l, DECO_ADD); @@ -1250,6 +1256,53 @@ void LuaScriptInterface::initSimulationAPI() SETCONST(l, PMAPBITS); SETCONST(l, PMAPMASK); + SETCONST(l, CIRCLE_BRUSH); + SETCONST(l, SQUARE_BRUSH); + SETCONST(l, TRI_BRUSH); + SETCONST(l, BRUSH_NUM); + + SETCONST(l, EDGE_VOID); + SETCONST(l, EDGE_SOLID); + SETCONST(l, EDGE_LOOP); + SETCONST(l, NUM_EDGE_MODES); + + SETCONST(l, AIR_ON); + SETCONST(l, AIR_PRESSURE_OFF); + SETCONST(l, AIR_VELOCITY_OFF); + SETCONST(l, AIR_OFF); + SETCONST(l, AIR_NO_UPDATE); + SETCONST(l, NUM_AIR_MODES); + + SETCONST(l, GRAV_VERTICAL); + SETCONST(l, GRAV_OFF); + SETCONST(l, GRAV_RADIAL); + SETCONST(l, GRAV_CUSTOM); + SETCONST(l, NUM_GRAV_MODES); + + lua_newtable(l); + SETCONST(l, WL_ERASE); + SETCONST(l, WL_WALLELEC); + SETCONST(l, WL_EWALL); + SETCONST(l, WL_DETECT); + SETCONST(l, WL_STREAM); + SETCONST(l, WL_FAN); + SETCONST(l, WL_ALLOWLIQUID); + SETCONST(l, WL_DESTROYALL); + SETCONST(l, WL_WALL); + SETCONST(l, WL_ALLOWAIR); + SETCONST(l, WL_ALLOWPOWDER); + SETCONST(l, WL_ALLOWALLELEC); + SETCONST(l, WL_EHOLE); + SETCONST(l, WL_ALLOWGAS); + SETCONST(l, WL_GRAV); + SETCONST(l, WL_ALLOWENERGY); + SETCONST(l, WL_BLOCKAIR); + SETCONST(l, WL_ERASEALL); + SETCONST(l, WL_STASIS); + SETCONST(l, WL_FLOODHELPER); + SETCONST(l, UI_WALLCOUNT); + lua_setfield(l, -2, "walls"); + //Declare FIELD_BLAH constants { int particlePropertiesCount = 0; @@ -2301,7 +2354,7 @@ int LuaScriptInterface::simulation_edgeMode(lua_State * l) lua_pushnumber(l, luacon_model->GetEdgeMode()); return 1; } - int edgeMode = luaL_optint(l, 1, 0); + int edgeMode = luaL_optint(l, 1, EDGE_VOID); luacon_model->SetEdgeMode(edgeMode); return 0; } @@ -2314,7 +2367,7 @@ int LuaScriptInterface::simulation_gravityMode(lua_State * l) lua_pushnumber(l, luacon_sim->gravityMode); return 1; } - int gravityMode = luaL_optint(l, 1, 0); + int gravityMode = luaL_optint(l, 1, GRAV_VERTICAL); luacon_sim->gravityMode = gravityMode; return 0; } @@ -2347,7 +2400,7 @@ int LuaScriptInterface::simulation_airMode(lua_State * l) lua_pushnumber(l, luacon_sim->air->airMode); return 1; } - int airMode = luaL_optint(l, 1, 0); + int airMode = luaL_optint(l, 1, AIR_ON); luacon_sim->air->airMode = airMode; return 0; } diff --git a/src/simulation/Air.cpp b/src/simulation/Air.cpp index 8a0996163..166ea561e 100644 --- a/src/simulation/Air.cpp +++ b/src/simulation/Air.cpp @@ -119,7 +119,7 @@ void Air::update_air(void) { const float advDistanceMult = 0.7f; - if (airMode != 4) //airMode 4 is no air/pressure update + if (airMode != AIR_NO_UPDATE) //airMode 4 is no air/pressure update { for (auto i=0; i= CELL && nx < XRES-CELL); bool y_ok = (ny >= CELL && ny < YRES-CELL); @@ -2076,15 +2076,15 @@ void Simulation::GetGravityField(int x, int y, float particleGrav, float newtonG switch (gravityMode) { default: - case 0: //normal, vertical gravity + case GRAV_VERTICAL: //normal, vertical gravity pGravX = 0; pGravY = particleGrav; break; - case 1: //no gravity + case GRAV_OFF: //no gravity pGravX = 0; pGravY = 0; break; - case 2: //radial gravity + case GRAV_RADIAL: //radial gravity { pGravX = 0; pGravY = 0; @@ -2098,7 +2098,7 @@ void Simulation::GetGravityField(int x, int y, float particleGrav, float newtonG } } break; - case 3: //custom gravity + case GRAV_CUSTOM: //custom gravity pGravX = particleGrav * customGravityX; pGravY = particleGrav * customGravityY; break; @@ -2885,7 +2885,7 @@ killed: fin_yf += dy; fin_x = (int)(fin_xf+0.5f); fin_y = (int)(fin_yf+0.5f); - if (edgeMode == 2) + if (edgeMode == EDGE_LOOP) { bool x_ok = (fin_xf >= CELL-.5f && fin_xf < XRES-CELL-.5f); bool y_ok = (fin_yf >= CELL-.5f && fin_yf < YRES-CELL-.5f); @@ -2901,7 +2901,7 @@ killed: // nothing found fin_xf = parts[i].x + parts[i].vx; fin_yf = parts[i].y + parts[i].vy; - if (edgeMode == 2) + if (edgeMode == EDGE_LOOP) { bool x_ok = (fin_xf >= CELL-.5f && fin_xf < XRES-CELL-.5f); bool y_ok = (fin_yf >= CELL-.5f && fin_yf < YRES-CELL-.5f); @@ -2945,7 +2945,7 @@ killed: parts[i].y += parts[i].vy; int nx = (int)((float)parts[i].x+0.5f); int ny = (int)((float)parts[i].y+0.5f); - if (edgeMode == 2) + if (edgeMode == EDGE_LOOP) { bool x_ok = (nx >= CELL && nx < XRES-CELL); bool y_ok = (ny >= CELL && ny < YRES-CELL); @@ -3218,7 +3218,7 @@ killed: goto movedone; } } - if (elements[t].Falldown>1 && !grav->IsEnabled() && gravityMode==0 && parts[i].vy>fabsf(parts[i].vx)) + if (elements[t].Falldown>1 && !grav->IsEnabled() && gravityMode==GRAV_VERTICAL && parts[i].vy>fabsf(parts[i].vx)) { s = 0; // stagnant is true if FLAG_STAGNANT was set for this particle in previous frame @@ -3942,8 +3942,8 @@ Simulation::Simulation(): gravWallChanged(false), CGOL(0), GSPEED(1), - edgeMode(0), - gravityMode(0), + edgeMode(EDGE_VOID), + gravityMode(GRAV_VERTICAL), customGravityX(0), customGravityY(0), legacy_enable(0), diff --git a/src/simulation/SimulationData.h b/src/simulation/SimulationData.h index 08138e38f..90dbed540 100644 --- a/src/simulation/SimulationData.h +++ b/src/simulation/SimulationData.h @@ -133,6 +133,21 @@ constexpr int NGT_BRAN = 23; constexpr auto REPLACE_MODE = UINT32_C(0x00000001); constexpr auto SPECIFIC_DELETE = UINT32_C(0x00000002); +enum EdgeMode +{ + EDGE_VOID, EDGE_SOLID, EDGE_LOOP, NUM_EDGE_MODES +}; + +enum AirMode +{ + AIR_ON, AIR_PRESSURE_OFF, AIR_VELOCITY_OFF, AIR_OFF, AIR_NO_UPDATE, NUM_AIR_MODES +}; + +enum GravityMode +{ + GRAV_VERTICAL, GRAV_OFF, GRAV_RADIAL, GRAV_CUSTOM, NUM_GRAV_MODES +}; + struct part_type; struct part_transition; diff --git a/src/simulation/elements/STKM.cpp b/src/simulation/elements/STKM.cpp index 4c88356e7..78455a74c 100644 --- a/src/simulation/elements/STKM.cpp +++ b/src/simulation/elements/STKM.cpp @@ -147,13 +147,13 @@ int Element_STKM_run_stickman(playerst *playerp, UPDATE_FUNC_ARGS) switch (sim->gravityMode) { default: - case 0: + case GRAV_VERTICAL: gvy = 1; break; - case 1: + case GRAV_OFF: gvy = gvx = 0.0f; break; - case 2: + case GRAV_RADIAL: { float gravd; gravd = 0.01f - hypotf((parts[i].x - XCNTR), (parts[i].y - YCNTR)); @@ -161,7 +161,7 @@ int Element_STKM_run_stickman(playerst *playerp, UPDATE_FUNC_ARGS) gvy = ((float)(parts[i].y - YCNTR) / gravd); } break; - case 3: + case GRAV_CUSTOM: gvx = sim->customGravityX; gvy = sim->customGravityY; break;