From 3714775621c4e46180fc40a3e6a5e47f6c7859cb Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Fri, 16 Sep 2016 13:28:19 +1000 Subject: [PATCH] Style: Move activate functions out of DoorPhysics to allow them to be used by other block physics. --- Levels/Physics/ActivateablePhysics.cs | 83 +++++++++++++++++++++++++ Levels/Physics/DoorPhysics.cs | 89 ++++----------------------- MCGalaxy_.csproj | 1 + 3 files changed, 96 insertions(+), 77 deletions(-) create mode 100644 Levels/Physics/ActivateablePhysics.cs diff --git a/Levels/Physics/ActivateablePhysics.cs b/Levels/Physics/ActivateablePhysics.cs new file mode 100644 index 000000000..8d21939dd --- /dev/null +++ b/Levels/Physics/ActivateablePhysics.cs @@ -0,0 +1,83 @@ +/* + Copyright 2010 MCSharp team (Modified for use with MCZall/MCLawl/MCGalaxy) + + Dual-licensed under the Educational Community License, Version 2.0 and + the GNU General Public License, Version 3 (the "Licenses"); you may + not use this file except in compliance with the Licenses. You may + obtain a copy of the Licenses at + + http://www.opensource.org/licenses/ecl2.php + http://www.gnu.org/licenses/gpl-3.0.html + + Unless required by applicable law or agreed to in writing, + software distributed under the Licenses are distributed on an "AS IS" + BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + or implied. See the Licenses for the specific language governing + permissions and limitations under the Licenses. + */ +using System; + +namespace MCGalaxy.BlockPhysics { + public static class ActivateablePhysics { + + /// Activates fireworks, rockets, and TNT in 1 block radius around (x, y, z) + public static void DoNeighbours(Level lvl, int index, ushort x, ushort y, ushort z) { + for (int yy = -1; yy <= 1; yy++) + for (int zz = -1; zz <= 1; zz++) + for (int xx = -1; xx <= 1; xx++) + { + byte b = lvl.GetTile(lvl.IntOffset(index, xx, yy, zz)); + if (b == Block.rocketstart) { + int b1 = lvl.IntOffset(index, xx * 3, yy * 3, zz * 3); + int b2 = lvl.IntOffset(index, xx * 2, yy * 2, zz * 2); + bool unblocked = lvl.GetTile(b1) == Block.air && lvl.GetTile(b2) == Block.air && + !lvl.listUpdateExists.Get(x + xx * 3, y + yy * 3, z + zz * 3) && + !lvl.listUpdateExists.Get(x + xx * 2, y + yy * 2, z + zz * 2); + + if (unblocked) { + lvl.AddUpdate(b1, Block.rockethead); + lvl.AddUpdate(b2, Block.lava_fire); + } + } else if (b == Block.firework) { + int b1 = lvl.IntOffset(index, xx, yy + 1, zz); + int b2 = lvl.IntOffset(index, xx, yy + 2, zz); + bool unblocked = lvl.GetTile(b1) == Block.air && lvl.GetTile(b2) == Block.air && + !lvl.listUpdateExists.Get(x + xx, y + yy + 1, z + zz) && + !lvl.listUpdateExists.Get(x + xx, y + yy + 2, z + zz); + + if (unblocked) { + lvl.AddUpdate(b2, Block.firework); + PhysicsArgs args = default(PhysicsArgs); + args.Type1 = PhysicsArgs.Dissipate; args.Value1 = 100; + lvl.AddUpdate(b1, Block.lavastill, false, args); + } + } else if (b == Block.tnt) { + lvl.MakeExplosion((ushort)(x + xx), (ushort)(y + yy), (ushort)(z + zz), 0); + } + } + } + + /// Activates doors, tdoors and toggles odoors at (x, y, z) + public static void DoDoors(Level lvl, ushort x, ushort y, ushort z, bool instant) { + int index = lvl.PosToInt(x, y, z); + if (index < 0) return; + byte b = lvl.blocks[index]; + + byte airDoor = Block.DoorAirs(b); + if (airDoor != 0) { + if (!instant) lvl.AddUpdate(index, airDoor); + else lvl.Blockchange(x, y, z, airDoor); + } else if (Block.Props[b].IsTDoor) { + PhysicsArgs args = default(PhysicsArgs); + args.Type1 = PhysicsArgs.Wait; args.Value1 = 16; + args.Type2 = PhysicsArgs.Revert; args.Value2 = b; + args.TDoor = true; + lvl.AddUpdate(index, Block.air, false, args); + } else { + byte oDoor = Block.odoor(b); + if (oDoor != Block.Zero) + lvl.AddUpdate(index, oDoor, true); + } + } + } +} \ No newline at end of file diff --git a/Levels/Physics/DoorPhysics.cs b/Levels/Physics/DoorPhysics.cs index 201cc553f..d304ccdfd 100644 --- a/Levels/Physics/DoorPhysics.cs +++ b/Levels/Physics/DoorPhysics.cs @@ -41,99 +41,34 @@ namespace MCGalaxy.BlockPhysics { lvl.AddUpdate(index, block, true); } - //Change any door blocks nearby into door_air - public static void AnyDoor(Level lvl, ref Check C, int timer, bool instaUpdate = false) { - ushort x, y, z; + //Change any door blocks nearby into door_air + public static void AnyDoor(Level lvl, ref Check C, int timer, bool instant = false) { + ushort x, y, z; lvl.IntToPos(C.b, out x, out y, out z); if (C.data.Data != 0) { CheckDoorRevert(lvl, ref C, timer); return; } - PhysDoor(lvl, (ushort)(x + 1), y, z, instaUpdate); - PhysDoor(lvl, (ushort)(x - 1), y, z, instaUpdate); - PhysDoor(lvl, x, y, (ushort)(z + 1), instaUpdate); - PhysDoor(lvl, x, y, (ushort)(z - 1), instaUpdate); - PhysDoor(lvl, x, (ushort)(y - 1), z, instaUpdate); - PhysDoor(lvl, x, (ushort)(y + 1), z, instaUpdate); + ActivateablePhysics.DoDoors(lvl, (ushort)(x + 1), y, z, instant); + ActivateablePhysics.DoDoors(lvl, (ushort)(x - 1), y, z, instant); + ActivateablePhysics.DoDoors(lvl, x, y, (ushort)(z + 1), instant); + ActivateablePhysics.DoDoors(lvl, x, y, (ushort)(z - 1), instant); + ActivateablePhysics.DoDoors(lvl, x, (ushort)(y - 1), z, instant); + ActivateablePhysics.DoDoors(lvl, x, (ushort)(y + 1), z, instant); - if (lvl.blocks[C.b] != Block.door_green_air) { - CheckDoorRevert(lvl, ref C, timer); return; + if (lvl.blocks[C.b] == Block.door_green_air && lvl.physics != 5) { + ActivateablePhysics.DoNeighbours(lvl, C.b, x, y, z); } - - if (lvl.physics != 5) - ActivateNeighbours(lvl, ref C, x, y, z); CheckDoorRevert(lvl, ref C, timer); } - static void ActivateNeighbours(Level lvl, ref Check C, ushort x, ushort y, ushort z) { - for (int yy = -1; yy <= 1; yy++) - for (int zz = -1; zz <= 1; zz++) - for (int xx = -1; xx <= 1; xx++) - { - byte b = lvl.GetTile(lvl.IntOffset(C.b, xx, yy, zz)); - if (b == Block.rocketstart) { - int b1 = lvl.IntOffset(C.b, xx * 3, yy * 3, zz * 3); - int b2 = lvl.IntOffset(C.b, xx * 2, yy * 2, zz * 2); - bool unblocked = lvl.GetTile(b1) == Block.air && lvl.GetTile(b2) == Block.air && - !lvl.listUpdateExists.Get(x + xx * 3, y + yy * 3, z + zz * 3) && - !lvl.listUpdateExists.Get(x + xx * 2, y + yy * 2, z + zz * 2); - - if (unblocked) { - lvl.AddUpdate(b1, Block.rockethead); - lvl.AddUpdate(b2, Block.lava_fire); - } - } else if (b == Block.firework) { - int b1 = lvl.IntOffset(C.b, xx, yy + 1, zz); - int b2 = lvl.IntOffset(C.b, xx, yy + 2, zz); - bool unblocked = lvl.GetTile(b1) == Block.air && lvl.GetTile(b2) == Block.air && - !lvl.listUpdateExists.Get(x + xx, y + yy + 1, z + zz) && - !lvl.listUpdateExists.Get(x + xx, y + yy + 2, z + zz); - - if (unblocked) { - lvl.AddUpdate(b2, Block.firework); - PhysicsArgs args = default(PhysicsArgs); - args.Type1 = PhysicsArgs.Dissipate; args.Value1 = 100; - lvl.AddUpdate(b1, Block.lavastill, false, args); - } - } else if (b == Block.tnt) { - lvl.MakeExplosion((ushort)(x + xx), (ushort)(y + yy), (ushort)(z + zz), 0); - } - } - } - static void CheckDoorRevert(Level lvl, ref Check C, int timer) { if (C.data.Data < timer) { C.data.Data++; } else { - lvl.AddUpdate(C.b, Block.Props[lvl.blocks[C.b]].DoorId); + lvl.AddUpdate(C.b, Block.Props[lvl.blocks[C.b]].DoorId); C.data.Data = 255; } } - - static void PhysDoor(Level lvl, ushort x, ushort y, ushort z, bool instaUpdate) { - int index = lvl.PosToInt(x, y, z); - if (index < 0) return; - byte rawBlock = lvl.blocks[index]; - - byte airDoor = Block.DoorAirs(rawBlock); - if (airDoor != 0) { - if (!instaUpdate) - lvl.AddUpdate(index, airDoor); - else - lvl.Blockchange(x, y, z, airDoor); - return; - } - - if (Block.Props[rawBlock].IsTDoor) { - PhysicsArgs args = default(PhysicsArgs); - args.Type1 = PhysicsArgs.Wait; args.Value1 = 16; - args.Type2 = PhysicsArgs.Revert; args.Value2 = rawBlock; - args.TDoor = true; - lvl.AddUpdate(index, Block.air, false, args); - } - byte oDoor = Block.odoor(rawBlock); - if (oDoor != Block.Zero) - lvl.AddUpdate(index, oDoor, true); - } } } \ No newline at end of file diff --git a/MCGalaxy_.csproj b/MCGalaxy_.csproj index 0c388c9c1..56ab6c178 100644 --- a/MCGalaxy_.csproj +++ b/MCGalaxy_.csproj @@ -519,6 +519,7 @@ +