Skip to content

Commit

Permalink
LS: Fix layer flooding not working properly after first round, and no…
Browse files Browse the repository at this point in the history
…t showing a message in-game when flooding starts

Also in GUI, don't save round/flood time if it's the same as LS defaults
  • Loading branch information
UnknownShadow200 committed Jan 29, 2023
1 parent 3a0f407 commit 9a42403
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 26 deletions.
9 changes: 6 additions & 3 deletions GUI/PropertyWindow/PropertyWindow.Games.cs
Expand Up @@ -194,6 +194,8 @@ public partial class PropertyWindow : Form

void SaveLSMapSettings() {
if (lsCurCfg == null) return;
LSConfig cfg = LSGame.Instance.Config;

lsCurCfg.WaterChance = (int)ls_numWater.Value;
lsCurCfg.FastChance = (int)ls_numFast.Value;
lsCurCfg.DestroyChance = (int)ls_numDestroy.Value;
Expand All @@ -203,9 +205,10 @@ public partial class PropertyWindow : Form
lsCurCfg.LayerCount = (int)ls_numCount.Value;
lsCurCfg.LayerHeight = (int)ls_numHeight.Value;

lsCurCfg._RoundTime = ls_numRound.Value;
lsCurCfg._FloodTime = ls_numFlood.Value;
lsCurCfg._LayerInterval = ls_numLayerTime.Value;
// TODO function for this
if (ls_numRound.Value != cfg.DefaultRoundTime) lsCurCfg._RoundTime = ls_numRound.Value;
if (ls_numFlood.Value != cfg.DefaultFloodTime) lsCurCfg._FloodTime = ls_numFlood.Value;
if (ls_numLayerTime.Value != cfg.DefaultLayerInterval) lsCurCfg._LayerInterval = ls_numLayerTime.Value;

lsCurCfg.Save(lsCurMap);
lsHelper.UpdateMapConfig(lsCurMap);
Expand Down
2 changes: 2 additions & 0 deletions MCGalaxy/Config/NumberAttributes.cs
Expand Up @@ -171,6 +171,8 @@ public ConfigOptTimespanAttribute(string name, string section, bool mins)
: base(name, section, -1, mins) { }

public override object Parse(string raw) {
if (string.IsNullOrEmpty(raw)) return null;

double value = ParseReal(raw, -1, -1, int.MaxValue);
if (value < 0) return null;

Expand Down
9 changes: 6 additions & 3 deletions MCGalaxy/Database/IDatabaseBackend.cs
Expand Up @@ -151,19 +151,22 @@ record => values.Add(record.GetText(0)),

#region Raw SQL functions

/// <summary> Executes an SQL command that does not return any results. </summary>
public void Execute(string sql, object[] parameters, bool createDB) {
/// <summary> Executes an SQL command and returns the number of affected rows. </summary>
public int Execute(string sql, object[] parameters, bool createDB) {
int rows = 0;

using (ISqlConnection conn = CreateConnection()) {
conn.Open();
if (!createDB && MultipleSchema)
conn.ChangeDatabase(Server.Config.MySQLDatabaseName);

using (ISqlCommand cmd = conn.CreateCommand(sql)) {
FillParams(cmd, parameters);
cmd.ExecuteNonQuery();
rows = cmd.ExecuteNonQuery();
}
conn.Close();
}
return rows;
}

/// <summary> Excecutes an SQL query, invoking a callback on the returned rows one by one. </summary>
Expand Down
21 changes: 14 additions & 7 deletions MCGalaxy/Drawing/CopyState.cs
Expand Up @@ -137,7 +137,8 @@ public sealed class CopyState
w.Write(data.Length);
w.Write(data);

for (int i = 0; i < extBlocks.Length; i++) {
for (int i = 0; i < extBlocks.Length; i++)
{
if (extBlocks[i] == null) {
w.Write((byte)0); continue;
}
Expand Down Expand Up @@ -198,7 +199,8 @@ public sealed class CopyState
allExtBlocks = r.ReadBytes(dataLen).Decompress((Volume + 7) / 8);
UnpackPackedExtBlocks(allExtBlocks);
} else {
for (int i = 0; i < extBlocks.Length; i++) {
for (int i = 0; i < extBlocks.Length; i++)
{
if (r.ReadByte() == 0) continue;
dataLen = r.ReadUInt16();
extBlocks[i] = r.ReadBytes(dataLen).Decompress(chunkSize);
Expand All @@ -208,14 +210,16 @@ public sealed class CopyState
}

void UnpackExtBlocks(byte[] allExtBlocks) {
for (int i = 0; i < blocks.Length; i++) {
for (int i = 0; i < blocks.Length; i++)
{
if (blocks[i] != Block.custom_block) continue;
Set((BlockID)(Block.Extended | allExtBlocks[i]), i);
}
}

void UnpackPackedExtBlocks(byte[] allExtBlocks) {
for (int i = 0; i < blocks.Length; i++) {
for (int i = 0; i < blocks.Length; i++)
{
bool isExt = (allExtBlocks[i >> 3] & (1 << (i & 0x7))) != 0;
if (isExt) { Set((BlockID)(Block.Extended | blocks[i]), i); }
}
Expand All @@ -225,11 +229,12 @@ public sealed class CopyState
public void LoadFromOld(Stream stream, Stream underlying) {
byte[] raw = new byte[underlying.Length];
underlying.Read(raw, 0, (int)underlying.Length);
raw = raw.Decompress();
raw = raw.Decompress(16);
if (raw.Length == 0) return;

CalculateBounds(raw);
for (int i = 0; i < raw.Length; i += 7) {
for (int i = 0; i < raw.Length; i += 7)
{
ushort x = BitConverter.ToUInt16(raw, i + 0);
ushort y = BitConverter.ToUInt16(raw, i + 2);
ushort z = BitConverter.ToUInt16(raw, i + 4);
Expand All @@ -244,7 +249,9 @@ public sealed class CopyState
void CalculateBounds(byte[] raw) {
int minX = int.MaxValue, minY = int.MaxValue, minZ = int.MaxValue;
int maxX = int.MinValue, maxY = int.MinValue, maxZ = int.MinValue;
for (int i = 0; i < raw.Length; i += 7) {

for (int i = 0; i < raw.Length; i += 7)
{
ushort x = BitConverter.ToUInt16(raw, i + 0);
ushort y = BitConverter.ToUInt16(raw, i + 2);
ushort z = BitConverter.ToUInt16(raw, i + 4);
Expand Down
4 changes: 4 additions & 0 deletions MCGalaxy/Modules/Games/LavaSurvival/LSGame.Plugin.cs
Expand Up @@ -91,5 +91,9 @@ public sealed partial class LSGame : RoundsGame
p.Message("Sponges Left: &4" + data.SpongesLeft);
}
}

/*bool NearLavaSpawn(ushort x, ushort y, ushort z) {
cfg.FloodPos
}*/
}
}
26 changes: 18 additions & 8 deletions MCGalaxy/Modules/Games/LavaSurvival/LSGame.Round.cs
Expand Up @@ -19,6 +19,7 @@
using System.Collections.Generic;
using System.Threading;
using MCGalaxy.Games;
using MCGalaxy.Maths;

namespace MCGalaxy.Modules.Games.LS
{
Expand All @@ -30,6 +31,7 @@ public sealed partial class LSGame : RoundsGame
if (!Running) return;
roundSecs = 0;
layerSecs = 0;
curLayer = 1;

Player[] players = PlayerInfo.Online.Items;
foreach (Player p in players)
Expand All @@ -45,30 +47,38 @@ public sealed partial class LSGame : RoundsGame
RoundInProgress = true;
UpdateBlockHandlers();
Map.SetPhysics(destroyMode ? 2 : 1);
Vec3U16 pos;

while (RoundInProgress && roundSecs < roundTotalSecs) {
if (!Running) return;
if (!flooded) AnnounceFloodTime();

if (roundSecs >= floodDelaySecs) {
if (layerMode && (layerSecs % layerIntervalSecs) == 0 && curLayer <= cfg.LayerCount) {
ushort y = (ushort)(cfg.LayerPos.Y + ((cfg.LayerHeight * curLayer) - 1));
Map.Blockchange(cfg.LayerPos.X, y, cfg.LayerPos.Z, floodBlock, true);
if (!layerMode && roundSecs == floodDelaySecs) {
FloodFrom(cfg.FloodPos);
} else if (layerMode && (layerSecs % layerIntervalSecs) == 0 && curLayer <= cfg.LayerCount) {
pos = cfg.LayerPos;
pos.Y = (ushort)(pos.Y + ((cfg.LayerHeight * curLayer) - 1));
FloodFrom(pos);
curLayer++;
} else if (!layerMode && roundSecs == floodDelaySecs) {
Map.Message("&4Look out, here comes the flood!");
Logger.Log(LogType.GameActivity, "[Lava Survival] Starting map flood.");
Map.Blockchange(cfg.FloodPos.X, cfg.FloodPos.Y, cfg.FloodPos.Z, floodBlock, true);
}

layerSecs++;
flooded = true;
}

roundSecs++;
Thread.Sleep(1000);
}
}

void FloodFrom(Vec3U16 pos) {
Map.Blockchange(pos.X, pos.Y, pos.Z, floodBlock, true);
if (flooded) return;

Map.Message("&4Look out, here comes the flood!");
Logger.Log(LogType.GameActivity, "[Lava Survival] Starting map flood.");
flooded = true;
}

public override void EndRound() {
if (!RoundInProgress) return;
Expand Down
7 changes: 4 additions & 3 deletions MCGalaxy/util/Extensions/Extensions.cs
Expand Up @@ -117,14 +117,15 @@ public static class Extensions
}
}

public static byte[] Decompress(this byte[] gzip, int capacity = 16) {
public static byte[] Decompress(this byte[] gzip, int capacity) {
using (GZipStream src = new GZipStream(new MemoryStream(gzip), CompressionMode.Decompress)) {
const int size = 4096;
byte[] buffer = new byte[size];
byte[] buffer = new byte[size];

using (MemoryStream dst = new MemoryStream(capacity)) {
int count = 0;
while ((count = src.Read(buffer, 0, size)) > 0) {
while ((count = src.Read(buffer, 0, size)) > 0)
{
dst.Write(buffer, 0, count);
}
return dst.ToArray();
Expand Down
4 changes: 2 additions & 2 deletions MCGalaxy/util/Extensions/TimeExts.cs
Expand Up @@ -62,12 +62,12 @@ public static class TimeExts
}

amount = GetTicks(num, GetUnit(value, i));
total = checked(total + amount);
total = checked(total + amount);
num = 0;
}

amount = GetTicks(num, defaultUnit);
total = checked(total + amount);
total = checked(total + amount);
return TimeSpan.FromTicks(total);
}

Expand Down

1 comment on commit 9a42403

@UnknownShadow200
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • after first round and the next round continues on same level (issue doesn't happen when next round is on a different level)

Please sign in to comment.