Search in sources :

Example 1 with IRadioWaveReceiver

use of com.builtbroken.mc.api.map.radio.IRadioWaveReceiver in project Engine by VoltzEngine-Project.

the class WirelessNetwork method updateConnections.

/**
     * Called to update all connections
     */
public void updateConnections() {
    //Update list if we still have a sender
    if (hub != null && hub.getWirelessCoverageArea() != null) {
        //================================
        //==Discovery Phase===============
        //================================
        //Get all receivers in range
        Cube range = hub.getWirelessCoverageArea();
        RadioMap map = RadioRegistry.getRadioMapForWorld(hub.world());
        List<IRadioWaveReceiver> receivers = map.getReceiversInRange(range, hub instanceof IRadioWaveReceiver ? (IRadioWaveReceiver) hub : null);
        //Loop threw receivers
        if (!receivers.isEmpty()) {
            for (IRadioWaveReceiver receiver : receivers) {
                if (receiver instanceof IWirelessConnector) {
                    if (!addConnector((IWirelessConnector) receiver) && wirelessConnectors.contains(receiver)) {
                        //Update connection list as connector is not going to update every tick
                        for (IWirelessNetworkObject object : ((IWirelessConnector) receiver).getWirelessNetworkObjects()) {
                            addConnection(((IWirelessConnector) receiver), object);
                        }
                    }
                }
            }
        }
        //Ensure we have added the primary sender if it is a connector
        if (getPrimarySender() instanceof IWirelessConnector) {
            addConnector((IWirelessConnector) getPrimarySender());
        }
        //================================
        //==Clean Up Phase================
        //================================
        //Clear invalid connectors
        Iterator<IWirelessConnector> it2 = wirelessConnectors.iterator();
        while (it2.hasNext()) {
            IWirelessConnector con = it2.next();
            if (con instanceof TileEntity && ((TileEntity) con).isInvalid() || con instanceof Entity && !((Entity) con).isEntityAlive()) {
                it2.remove();
                clearConnections(con);
                con.removeWirelessNetwork(this, ConnectionRemovedReason.TILE_INVALIDATE);
            }
        }
        //Clear invalid attached devices
        Iterator<IWirelessNetworkObject> it = attachedDevices.iterator();
        while (it.hasNext()) {
            IWirelessNetworkObject obj = it.next();
            if (obj instanceof TileEntity && ((TileEntity) obj).isInvalid() || obj instanceof Entity && !((Entity) obj).isEntityAlive()) {
                it.remove();
                obj.removeWirelessNetwork(this, ConnectionRemovedReason.TILE_INVALIDATE);
                if (obj instanceof IWirelessDataPoint) {
                    dataPoints.remove(obj);
                //TODO notify listeners
                }
            }
        }
    } else {
        kill();
    }
}
Also used : TileEntity(net.minecraft.tileentity.TileEntity) IRadioWaveReceiver(com.builtbroken.mc.api.map.radio.IRadioWaveReceiver) Entity(net.minecraft.entity.Entity) TileEntity(net.minecraft.tileentity.TileEntity) Cube(com.builtbroken.mc.imp.transform.region.Cube) RadioMap(com.builtbroken.mc.lib.world.radio.RadioMap)

Example 2 with IRadioWaveReceiver

use of com.builtbroken.mc.api.map.radio.IRadioWaveReceiver in project Engine by VoltzEngine-Project.

the class RadioMap method remove.

public boolean remove(IRadioWaveReceiver receiver) {
    if (fullMapRangeReceives.contains(receiver)) {
        fullMapRangeReceives.remove(receiver);
    }
    if (receive_to_chunks.containsKey(receiver)) {
        //Clear cached chunk positions
        for (ChunkCoordIntPair pair : receive_to_chunks.get(receiver)) {
            if (chunk_to_entities.containsKey(pair)) {
                chunk_to_entities.get(pair).remove(receiver);
            }
        }
        //Clear entry in receiver map
        receive_to_chunks.remove(receiver);
        //Update sender cache
        for (IRadioWaveSender sender : sender_to_receivers_cache.keySet()) {
            List<IRadioWaveReceiver> receivers = sender_to_receivers_cache.get(sender);
            if (receivers == null) {
                receivers = new ArrayList();
            }
            if (receivers.contains(receiver)) {
                receivers.remove(receiver);
            }
            sender_to_receivers_cache.put(sender, receivers);
        }
        return true;
    }
    return false;
}
Also used : IRadioWaveReceiver(com.builtbroken.mc.api.map.radio.IRadioWaveReceiver) ChunkCoordIntPair(net.minecraft.world.ChunkCoordIntPair) IRadioWaveSender(com.builtbroken.mc.api.map.radio.IRadioWaveSender) ArrayList(java.util.ArrayList)

Example 3 with IRadioWaveReceiver

use of com.builtbroken.mc.api.map.radio.IRadioWaveReceiver in project Engine by VoltzEngine-Project.

the class RadioMap method getReceiversInRange.

/**
     * Gets a list of all receivers in the range
     *
     * @param range       - range to check inside
     * @param excludeList - tiles to ignore
     * @return list of receivers, or empty list
     */
public List<IRadioWaveReceiver> getReceiversInRange(Cube range, List excludeList) {
    List<IRadioWaveReceiver> receivers = new ArrayList();
    receivers.addAll(fullMapRangeReceives);
    if (range != null) {
        for (IRadioWaveReceiver receiver : receive_to_chunks.keySet()) {
            if (receiver != null && (excludeList == null || !excludeList.contains(receiver))) {
                Cube receiverRange = receiver.getRadioReceiverRange();
                if (receiverRange != null && range.doesOverlap(receiverRange)) {
                    receivers.add(receiver);
                }
            }
        }
    }
    return receivers;
}
Also used : IRadioWaveReceiver(com.builtbroken.mc.api.map.radio.IRadioWaveReceiver) Cube(com.builtbroken.mc.imp.transform.region.Cube) ArrayList(java.util.ArrayList)

Example 4 with IRadioWaveReceiver

use of com.builtbroken.mc.api.map.radio.IRadioWaveReceiver in project Engine by VoltzEngine-Project.

the class RadioMap method updateCache.

/**
     * Updates the list of receivers for this sender.
     * <p>
     * Make sure if you call this you clear your
     * cache when the sender is no longer valid
     * use {@link TileEntity#invalidate()}
     * or {@link Entity#setDead()} to
     * clear the cache.
     * <p>
     * As well make sure to call this method
     * every so often. As the cache is not maintained
     * often or quickly. It is only updated
     * when receives are added or removed. So
     * if the sender's range changes this method
     * needs to be called.
     * <p>
     * Make sure to not call this method often
     * as its a high CPU task.
     *
     * @param sender - object that needs to
     *               have receivers cache. Needs
     *               to have a valid {@link IRadioWaveSender#getRadioSenderRange()}
     *               value in order to be cached.
     */
public void updateCache(IRadioWaveSender sender) {
    //Clear cache value
    sender_to_receivers_cache.remove(sender);
    Cube range = sender.getRadioSenderRange();
    if (range != null) {
        sender_to_receivers_cache.put(sender, getReceiversInRange(range, sender instanceof IRadioWaveReceiver ? (IRadioWaveReceiver) sender : (IRadioWaveReceiver) null));
    }
}
Also used : IRadioWaveReceiver(com.builtbroken.mc.api.map.radio.IRadioWaveReceiver) Cube(com.builtbroken.mc.imp.transform.region.Cube)

Example 5 with IRadioWaveReceiver

use of com.builtbroken.mc.api.map.radio.IRadioWaveReceiver in project Engine by VoltzEngine-Project.

the class RadioMap method popMessage.

/**
     * Called to send a message over the network
     *
     * @param sender - object that sent the message
     * @param hz     - frequency of the message
     * @param header - descriptive header of the message, mainly an ID system
     * @param data   - data being sent in the message
     */
public void popMessage(IRadioWaveSender sender, float hz, String header, Object[] data) {
    //Cache for senders that know they will be active
    if (sender_to_receivers_cache.containsKey(sender)) {
        for (IRadioWaveReceiver receiver : sender_to_receivers_cache.get(sender)) {
            receiver.receiveRadioWave(hz, sender, header, data);
        }
        return;
    }
    //Receivers that have full map range, used for legacy systems mainly
    for (IRadioWaveReceiver receiver : fullMapRangeReceives) {
        receiver.receiveRadioWave(hz, sender, header, data);
    }
    //Slow way to update receives with messages
    Cube range = sender.getRadioSenderRange();
    if (range != null) {
        //Use simpler method if the range of number of entries is small
        if (//20 chunks
        receive_to_chunks.size() < 200 || range.getSizeX() > 320 || range.getSizeY() > 320) {
            for (IRadioWaveReceiver receiver : receive_to_chunks.keySet()) {
                if (receiver != null && receiver != sender) {
                    Cube receiverRange = receiver.getRadioReceiverRange();
                    if (range.doesOverlap(receiverRange) || receiverRange.doesOverlap(range)) {
                        receiver.receiveRadioWave(hz, sender, header, data);
                    }
                }
            }
        } else //Complex method only used if number of receive is very high, e.g. is faster~ish than the above method
        {
            List<ChunkCoordIntPair> coords = range.getChunkCoords();
            List<IRadioWaveReceiver> receivers = new ArrayList();
            for (ChunkCoordIntPair pair : coords) {
                List<IRadioWaveReceiver> l = chunk_to_entities.get(pair);
                if (l != null && l.size() > 0) {
                    for (IRadioWaveReceiver r : l) {
                        if (r != null && r != sender && !receivers.contains(r)) {
                            receivers.add(r);
                        }
                    }
                }
            }
            for (IRadioWaveReceiver receiver : receivers) {
                receiver.receiveRadioWave(hz, sender, header, data);
            }
        }
    }
}
Also used : IRadioWaveReceiver(com.builtbroken.mc.api.map.radio.IRadioWaveReceiver) ChunkCoordIntPair(net.minecraft.world.ChunkCoordIntPair) Cube(com.builtbroken.mc.imp.transform.region.Cube) ArrayList(java.util.ArrayList)

Aggregations

IRadioWaveReceiver (com.builtbroken.mc.api.map.radio.IRadioWaveReceiver)7 Cube (com.builtbroken.mc.imp.transform.region.Cube)5 ArrayList (java.util.ArrayList)5 ChunkCoordIntPair (net.minecraft.world.ChunkCoordIntPair)3 IRadioWaveSender (com.builtbroken.mc.api.map.radio.IRadioWaveSender)2 RadioMap (com.builtbroken.mc.lib.world.radio.RadioMap)1 Entity (net.minecraft.entity.Entity)1 TileEntity (net.minecraft.tileentity.TileEntity)1