Add IsAirAt overload that outputs index too

This commit is contained in:
UnknownShadow200 2017-08-26 16:20:42 +10:00
parent d4cb1a9cb3
commit a03f1e616c
7 changed files with 56 additions and 57 deletions

View File

@ -66,7 +66,7 @@ namespace MCGalaxy.Blocks.Physics {
int i = block.Index; int i = block.Index;
if (lvl.BlockProps[i].IsDoor) { if (lvl.BlockProps[i].IsDoor) {
byte physForm; byte physForm;
PhysicsArgs args = GetDoorArgs(block, out physForm); PhysicsArgs args = GetDoorArgs(block, out physForm);
if (!instant) lvl.AddUpdate(index, physForm, false, args); if (!instant) lvl.AddUpdate(index, physForm, false, args);
else lvl.Blockchange(index, (ExtBlock)physForm, false, args); else lvl.Blockchange(index, (ExtBlock)physForm, false, args);
} else if (lvl.BlockProps[i].IsTDoor) { } else if (lvl.BlockProps[i].IsTDoor) {
@ -74,10 +74,10 @@ namespace MCGalaxy.Blocks.Physics {
lvl.AddUpdate(index, Block.Air, false, args); lvl.AddUpdate(index, Block.Air, false, args);
} else { } else {
ushort oDoorIndex = lvl.BlockProps[i].oDoorIndex; ushort oDoorIndex = lvl.BlockProps[i].oDoorIndex;
if (oDoorIndex == Block.Invalid) return; if (oDoorIndex == Block.Invalid) return;
ExtBlock oDoor = ExtBlock.FromIndex(oDoorIndex); ExtBlock oDoor = ExtBlock.FromIndex(oDoorIndex);
PhysicsArgs args = default(PhysicsArgs); PhysicsArgs args = default(PhysicsArgs);
args.ExtBlock = oDoor.BlockID == Block.custom_block; args.ExtBlock = oDoor.BlockID == Block.custom_block;
lvl.AddUpdate(index, oDoor.RawID, true, args); lvl.AddUpdate(index, oDoor.RawID, true, args);
} }

View File

@ -21,19 +21,20 @@ namespace MCGalaxy.Blocks.Physics {
public static class BirdPhysics { public static class BirdPhysics {
public static void Do(Level lvl, ref Check C) { public static void Do(Level lvl, ref Check C) {
Random rand = lvl.physRandom; Random rand = lvl.physRandom;
ushort x, y, z; ushort x, y, z;
lvl.IntToPos(C.b, out x, out y, out z); lvl.IntToPos(C.b, out x, out y, out z);
int index;
switch (rand.Next(1, 15)) { switch (rand.Next(1, 15)) {
case 1: case 1:
if (lvl.IsAirAt(x, (ushort)(y - 1), z)) if (lvl.IsAirAt(x, (ushort)(y - 1), z, out index))
lvl.AddUpdate(lvl.PosToInt(x, (ushort)(y - 1), z), lvl.blocks[C.b]); lvl.AddUpdate(index, lvl.blocks[C.b]);
else goto case 3; else goto case 3;
break; break;
case 2: case 2:
if (lvl.IsAirAt(x, (ushort)(y + 1), z)) if (lvl.IsAirAt(x, (ushort)(y + 1), z, out index))
lvl.AddUpdate(lvl.PosToInt(x, (ushort)(y + 1), z), lvl.blocks[C.b]); lvl.AddUpdate(index, lvl.blocks[C.b]);
else goto case 6; else goto case 6;
break; break;
case 3: case 3:

View File

@ -22,11 +22,12 @@ namespace MCGalaxy.Blocks.Physics {
public static class FirePhysics { public static class FirePhysics {
static bool ExpandSimple(Level lvl, int x, int y, int z) { static bool ExpandSimple(Level lvl, int x, int y, int z) {
int index = lvl.PosToInt((ushort)x, (ushort)y, (ushort)z); int index;
if (index < 0 || lvl.blocks[index] != Block.Air) return false; if (lvl.IsAirAt((ushort)x, (ushort)y, (ushort)z, out index)) {
lvl.AddUpdate(index, Block.Fire);
lvl.AddUpdate(index, Block.Fire); return true;
return true; }
return false;
} }
static void ExpandDiagonal(Level lvl, ushort x, ushort y, ushort z, static void ExpandDiagonal(Level lvl, ushort x, ushort y, ushort z,

View File

@ -124,15 +124,11 @@ namespace MCGalaxy.Commands.Fun {
} }
static void CheckTile(Level lvl, List<Vec3U16> glassCoords, int x, int y, int z) { static void CheckTile(Level lvl, List<Vec3U16> glassCoords, int x, int y, int z) {
Vec3U16 pos; Vec3U16 pos = new Vec3U16((ushort)x, (ushort)(y - 1), (ushort)z);
if (lvl.IsAirAt(x, y - 1, z)) { if (lvl.IsAirAt(pos.X, pos.Y, pos.Z)) glassCoords.Add(pos);
pos.X = (ushort)x; pos.Y = (ushort)(y - 1); pos.Z = (ushort)z;
glassCoords.Add(pos); pos.Y++;
} if (lvl.IsAirAt(pos.X, pos.Y, pos.Z)) glassCoords.Add(pos);
if (lvl.IsAirAt(x, y, z)) {
pos.X = (ushort)x; pos.Y = (ushort)y; pos.Z = (ushort)z;
glassCoords.Add(pos);
}
} }
protected abstract void PlacedMark(Player p, ushort x, ushort y, ushort z, ExtBlock block); protected abstract void PlacedMark(Player p, ushort x, ushort y, ushort z, ExtBlock block);

View File

@ -91,14 +91,15 @@ namespace MCGalaxy.Commands.Building {
bool DoRestart(Player p, Vec3S32[] m, object state, ExtBlock block) { bool DoRestart(Player p, Vec3S32[] m, object state, ExtBlock block) {
PhysicsArgs extraInfo = (PhysicsArgs)state; PhysicsArgs extraInfo = (PhysicsArgs)state;
List<int> buffer = new List<int>(); List<int> buffer = new List<int>();
int index;
for (int y = Math.Min(m[0].Y, m[1].Y); y <= Math.Max(m[0].Y, m[1].Y); y++) for (int y = Math.Min(m[0].Y, m[1].Y); y <= Math.Max(m[0].Y, m[1].Y); y++)
for (int z = Math.Min(m[0].Z, m[1].Z); z <= Math.Max(m[0].Z, m[1].Z); z++) for (int z = Math.Min(m[0].Z, m[1].Z); z <= Math.Max(m[0].Z, m[1].Z); z++)
for (int x = Math.Min(m[0].X, m[1].X); x <= Math.Max(m[0].X, m[1].X); x++) for (int x = Math.Min(m[0].X, m[1].X); x <= Math.Max(m[0].X, m[1].X); x++)
{ {
int index = p.level.PosToInt((ushort)x, (ushort)y, (ushort)z); if (p.level.IsAirAt((ushort)x, (ushort)y, (ushort)z, out index)) {
if (index >= 0 && p.level.blocks[index] != Block.Air)
buffer.Add(index); buffer.Add(index);
}
} }
if (extraInfo.Raw == 0) { if (extraInfo.Raw == 0) {
@ -113,8 +114,9 @@ namespace MCGalaxy.Commands.Building {
return false; return false;
} }
foreach (int index in buffer) foreach (int index1 in buffer) {
p.level.AddCheck(index, true, extraInfo); p.level.AddCheck(index1, true, extraInfo);
}
Player.Message(p, "Activated " + buffer.Count + " blocks."); Player.Message(p, "Activated " + buffer.Count + " blocks.");
return true; return true;
} }

View File

@ -218,50 +218,50 @@ namespace MCGalaxy.Games {
} }
void RemoveSquare(SquarePos pos) { void RemoveSquare(SquarePos pos) {
ushort minX = pos.X, maxX = (ushort)(pos.X + 1), y = 4, minZ = pos.Z, maxZ = (ushort)(pos.Z + 1); ushort x1 = pos.X, x2 = (ushort)(pos.X + 1), y = 4, z1 = pos.Z, z2 = (ushort)(pos.Z + 1);
Cuboid(minX, y, minZ, maxX, y, maxZ, Block.Yellow, Map); Cuboid(x1, y, z1, x2, y, z2, Block.Yellow, Map);
Thread.Sleep(Interval); Thread.Sleep(Interval);
Cuboid(minX, y, minZ, maxX, y, maxZ, Block.Orange, Map); Cuboid(x1, y, z1, x2, y, z2, Block.Orange, Map);
Thread.Sleep(Interval); Thread.Sleep(Interval);
Cuboid(minX, y, minZ, maxX, y, maxZ, Block.Red, Map); Cuboid(x1, y, z1, x2, y, z2, Block.Red, Map);
Thread.Sleep(Interval); Thread.Sleep(Interval);
Cuboid(minX, y, minZ, maxX, y, maxZ, Block.Air, Map); Cuboid(x1, y, z1, x2, y, z2, Block.Air, Map);
// Remove glass borders if neighbouring squared were previously removed. // Remove glass borders if neighbouring squared were previously removed.
bool airMaxX = false, airMinZ = false, airMaxZ = false, airMinX = false; bool airMaxX = false, airMinZ = false, airMaxZ = false, airMinX = false;
if (Map.IsAirAt(minX, y, maxZ + 2)) { if (Map.IsAirAt(x1, y, (ushort)(z2 + 2))) {
Map.Blockchange(minX, y, (ushort)(maxZ + 1), ExtBlock.Air); Map.Blockchange(x1, y, (ushort)(z2 + 1), ExtBlock.Air);
Map.Blockchange(maxX, y, (ushort)(maxZ + 1), ExtBlock.Air); Map.Blockchange(x2, y, (ushort)(z2 + 1), ExtBlock.Air);
airMaxZ = true; airMaxZ = true;
} }
if (Map.IsAirAt(minX, y, minZ - 2)) { if (Map.IsAirAt(x1, y, (ushort)(z1 - 2))) {
Map.Blockchange(minX, y, (ushort)(minZ - 1), ExtBlock.Air); Map.Blockchange(x1, y, (ushort)(z1 - 1), ExtBlock.Air);
Map.Blockchange(maxX, y, (ushort)(minZ - 1), ExtBlock.Air); Map.Blockchange(x2, y, (ushort)(z1 - 1), ExtBlock.Air);
airMinZ = true; airMinZ = true;
} }
if (Map.IsAirAt(maxX + 2, y, minZ)) { if (Map.IsAirAt((ushort)(x2 + 2), y, z1)) {
Map.Blockchange((ushort)(maxX + 1), y, minZ, ExtBlock.Air); Map.Blockchange((ushort)(x2 + 1), y, z1, ExtBlock.Air);
Map.Blockchange((ushort)(maxX + 1), y, maxZ, ExtBlock.Air); Map.Blockchange((ushort)(x2 + 1), y, z2, ExtBlock.Air);
airMaxX = true; airMaxX = true;
} }
if (Map.IsAirAt(minX - 2, y, minZ)) { if (Map.IsAirAt((ushort)(x1 - 2), y, z1)) {
Map.Blockchange((ushort)(minX - 1), y, minZ, ExtBlock.Air); Map.Blockchange((ushort)(x1 - 1), y, z1, ExtBlock.Air);
Map.Blockchange((ushort)(minX - 1), y, maxZ, ExtBlock.Air); Map.Blockchange((ushort)(x1 - 1), y, z2, ExtBlock.Air);
airMinX = true; airMinX = true;
} }
// Remove glass borders for diagonals too. // Remove glass borders for diagonals too.
if (Map.IsAirAt(minX - 2, y, minZ - 2) && airMinZ && airMinX) { if (Map.IsAirAt((ushort)(x1 - 2), y, (ushort)(z1 - 2)) && airMinX && airMinZ) {
Map.Blockchange((ushort)(minX - 1), y, (ushort)(minZ - 1), ExtBlock.Air); Map.Blockchange((ushort)(x1 - 1), y, (ushort)(z1 - 1), ExtBlock.Air);
} }
if (Map.IsAirAt(minX - 2, y, maxZ + 2) && airMaxZ && airMinX) { if (Map.IsAirAt((ushort)(x1 - 2), y, (ushort)(z2 + 2)) && airMinX && airMaxZ) {
Map.Blockchange((ushort)(minX - 1), y, (ushort)(maxZ + 1), ExtBlock.Air); Map.Blockchange((ushort)(x1 - 1), y, (ushort)(z2 + 1), ExtBlock.Air);
} }
if (Map.IsAirAt(maxX + 2, y, minZ - 2) && airMinZ && airMaxX) { if (Map.IsAirAt((ushort)(x2 + 2), y, (ushort)(z1 - 2)) && airMaxX && airMinZ) {
Map.Blockchange((ushort)(maxX + 1), y, (ushort)(minZ - 1), ExtBlock.Air); Map.Blockchange((ushort)(x2 + 1), y, (ushort)(z1 - 1), ExtBlock.Air);
} }
if (Map.IsAirAt(maxX + 2, y, maxZ + 2) && airMaxZ && airMaxX) { if (Map.IsAirAt((ushort)(x2 + 2), y, (ushort)(z2 + 2)) && airMaxX && airMaxZ) {
Map.Blockchange((ushort)(maxX + 1), y, (ushort)(maxZ + 1), ExtBlock.Air); Map.Blockchange((ushort)(x2 + 1), y, (ushort)(z2 + 1), ExtBlock.Air);
} }
} }

View File

@ -75,13 +75,12 @@ namespace MCGalaxy {
if (x >= Width || y >= Height || z >= Length || blocks == null) return false; if (x >= Width || y >= Height || z >= Length || blocks == null) return false;
return blocks[x + Width * (z + y * Length)] == Block.Air; return blocks[x + Width * (z + y * Length)] == Block.Air;
} }
/// <summary> Gets whether the block at the given coordinates is air. </summary> /// <summary> Gets whether the block at the given coordinates is air. </summary>
public bool IsAirAt(int x, int y, int z) { public bool IsAirAt(ushort x, ushort y, ushort z, out int index) {
if (x < 0 || y < 0 || z < 0 || blocks == null) return false; if (x >= Width || y >= Height || z >= Length || blocks == null) { index = -1; return false; }
if (x >= Width || y >= Height || z >= Length) return false; index = x + Width * (z + y * Length);
return blocks[index] == Block.Air;
return blocks[x + Width * (z + y * Length)] == Block.Air;
} }
public byte GetTile(int b) { public byte GetTile(int b) {