public static IMechanicalPowerDeviceOld getNeighbourDevice(IWorldAccessor world, BlockPos pos, BlockFacing facing, bool connected) { if (facing == null) { return(null); } BlockEntity te = world.BlockAccessor.GetBlockEntity(pos.Offset(facing)); if (te is IMechanicalPowerDeviceOld) { IMechanicalPowerDeviceOld mechdevice = (IMechanicalPowerDeviceOld)te; if (!mechdevice.exists()) { return(null); } if (!connected && mechdevice.hasConnectorAt(facing.GetOpposite())) { return(mechdevice); } if (connected && mechdevice.isConnectedAt(facing.GetOpposite())) { return(mechdevice); } } return(null); }
public void trySetNetwork(long networkId, BlockFacing localFacing) { if (hasConnectorAt(localFacing)) { this.networkId = networkId; IMechanicalPowerDeviceOld device = getNeighbourDevice(localFacing, true); if (device != null) { if (getNetwork(localFacing).getDirection() != 0) { clockwise = device.isClockWiseDirection(localFacing.GetOpposite()); setDirectionFromFacing(localFacing.GetOpposite()); } } else { throw new Exception("Eh, a network coming from " + localFacing + ", but there is no device, instead " + api.World.BlockAccessor.GetBlock(pos.AddCopy(localFacing)) + "?!"); } getNetwork(localFacing).register(this); api.World.BlockAccessor.MarkBlockEntityDirty(pos); } else { directionFromFacing = null; this.networkId = 0; } }
public void register(IMechanicalPowerDeviceOld device) { if (device is IMechanicalPowerNetworkNode && !powerNodes.Contains(device)) { powerNodes.Add((IMechanicalPowerNetworkNode)device); } if (device is IMechanicalPowerNetworkRelay && !powerRelays.Contains(device)) { powerRelays.Add((IMechanicalPowerNetworkRelay)device); } }
public void unregister(IMechanicalPowerDeviceOld device) { if (device is IMechanicalPowerNetworkNode) { powerNodes.Remove((IMechanicalPowerNetworkNode)device); } if (device is IMechanicalPowerNetworkRelay) { powerRelays.Remove((IMechanicalPowerNetworkRelay)device); } rebuildNetwork(); }
// connected = true => get connected devices // connected = false => get connectible devices (= devices that could potentially connect to our own device) public Dictionary <BlockFacing, IMechanicalPowerDeviceOld> getNeighbourDevices(bool connected) { Dictionary <BlockFacing, IMechanicalPowerDeviceOld> connectibleNeighbours = new Dictionary <BlockFacing, IMechanicalPowerDeviceOld>(); foreach (BlockFacing facing in BlockFacing.ALLFACES) { IMechanicalPowerDeviceOld neib = getNeighbourDevice(facing, connected); if (neib == null) { continue; } connectibleNeighbours[facing] = neib; } return(connectibleNeighbours); }
// Default behavior: Device is connected to all neighbor devices public bool isConnectedAt(BlockFacing facing) { IMechanicalPowerDeviceOld device = getNeighbourDevice(facing, false); return(device != null && (device is IMechanicalPowerDeviceOld)); }
public void handleMechanicalRelayPlacement() { Dictionary <BlockFacing, MechanicalNetworkOld> networks = new Dictionary <BlockFacing, MechanicalNetworkOld>(); List <BlockFacing> nullnetworks = new List <BlockFacing>(); foreach (BlockFacing facing in BlockFacing.ALLFACES) { IMechanicalPowerDeviceOld neib = getNeighbourDevice(facing, true); if (neib == null) { continue; } MechanicalNetworkOld network = neib.getNetwork(facing.GetOpposite()); if (network != null && !networks.ContainsValue(network)) { networks[facing] = network; } if (network == null) { nullnetworks.Add(facing); } } //System.out.println(worldObj.isRemote + " found " + networks.size() + " networks "); if (networks.Count == 1) { BlockFacing facing = networks.Keys.ToArray()[0]; trySetNetwork(networks[facing].networkId, facing); foreach (BlockFacing nullnetworkfacing in nullnetworks) { getNeighbourDevice(nullnetworkfacing, true).propagateNetworkToNeighbours( MechNetworkManagerRegistry.ManagersByWorld[api.World].getUniquePropagationId(), networkId, nullnetworkfacing ); } } if (networks.Count > 1 && api.World is IClientWorldAccessor) { float maxSpeedDifference = 0; MechanicalNetworkOld dominantNetwork = null; foreach (MechanicalNetworkOld network in networks.Values) { if (dominantNetwork == null) { dominantNetwork = network; continue; } maxSpeedDifference = Math.Max(maxSpeedDifference, Math.Abs(network.getSpeed() - dominantNetwork.getSpeed())); if (Math.Abs(network.getSpeed()) > Math.Abs(dominantNetwork.getSpeed())) { dominantNetwork = network; } } // Here we could disallow connecting of networks if // maxSpeedDifference is larger than 1 // e.g. immediately break the placed block again because it cannot handle // the large torque difference. But implementation will be somewhat complicated foreach (MechanicalNetworkOld network in networks.Values) { if (network != dominantNetwork) { network.isDead = true; MechNetworkManagerRegistry.ManagersByWorld[api.World].discardNetwork(network); } } dominantNetwork.rebuildNetwork(); api.World.BlockAccessor.MarkBlockEntityDirty(pos); } }