2014-11-14 12:02:52 +01:00
|
|
|
/*
|
|
|
|
* This file is part of Applied Energistics 2.
|
|
|
|
* Copyright (c) 2013 - 2014, AlgorithmX2, All rights reserved.
|
|
|
|
*
|
|
|
|
* Applied Energistics 2 is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Applied Energistics 2 is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public License
|
|
|
|
* along with Applied Energistics 2. If not, see <http://www.gnu.org/licenses/lgpl>.
|
|
|
|
*/
|
|
|
|
|
2014-06-10 19:16:14 +02:00
|
|
|
package appeng.me;
|
|
|
|
|
2014-11-30 04:58:08 +01:00
|
|
|
|
2014-06-10 19:16:14 +02:00
|
|
|
import java.util.Collection;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Iterator;
|
2014-11-30 04:58:08 +01:00
|
|
|
import java.util.Map;
|
2014-09-30 22:59:49 +02:00
|
|
|
import java.util.Map.Entry;
|
2014-06-10 19:16:14 +02:00
|
|
|
import java.util.Set;
|
|
|
|
|
|
|
|
import appeng.api.AEApi;
|
|
|
|
import appeng.api.networking.IGrid;
|
|
|
|
import appeng.api.networking.IGridCache;
|
|
|
|
import appeng.api.networking.IGridHost;
|
|
|
|
import appeng.api.networking.IGridNode;
|
2014-11-30 04:58:08 +01:00
|
|
|
import appeng.api.networking.IGridStorage;
|
2014-06-10 19:16:14 +02:00
|
|
|
import appeng.api.networking.IMachineSet;
|
|
|
|
import appeng.api.networking.events.MENetworkEvent;
|
|
|
|
import appeng.api.networking.events.MENetworkPostCacheConstruction;
|
|
|
|
import appeng.api.util.IReadOnlyCollection;
|
|
|
|
import appeng.core.WorldSettings;
|
|
|
|
import appeng.hooks.TickHandler;
|
|
|
|
import appeng.util.ReadOnlyCollection;
|
|
|
|
|
2014-11-30 04:58:08 +01:00
|
|
|
|
2014-06-10 19:16:14 +02:00
|
|
|
public class Grid implements IGrid
|
|
|
|
{
|
2014-11-30 04:58:08 +01:00
|
|
|
private final NetworkEventBus eventBus = new NetworkEventBus();
|
|
|
|
private final Map<Class<? extends IGridHost>, MachineSet> machines = new HashMap<Class<? extends IGridHost>, MachineSet>();
|
|
|
|
private final Map<Class<? extends IGridCache>, GridCacheWrapper> caches = new HashMap<Class<? extends IGridCache>, GridCacheWrapper>();
|
|
|
|
private GridNode pivot;
|
|
|
|
private int priority; // how import is this network?
|
|
|
|
private GridStorage myStorage;
|
|
|
|
|
|
|
|
public Grid( GridNode center )
|
|
|
|
{
|
2014-06-10 19:16:14 +02:00
|
|
|
this.pivot = center;
|
|
|
|
|
2014-11-30 04:58:08 +01:00
|
|
|
Map<Class<? extends IGridCache>, IGridCache> myCaches = AEApi.instance().registries().gridCache().createCacheInstance( this );
|
2015-04-03 08:54:31 +02:00
|
|
|
for( Entry<Class<? extends IGridCache>, IGridCache> c : myCaches.entrySet() )
|
2014-06-10 19:16:14 +02:00
|
|
|
{
|
2014-11-30 04:58:08 +01:00
|
|
|
Class<? extends IGridCache> key = c.getKey();
|
|
|
|
IGridCache value = c.getValue();
|
|
|
|
Class<? extends IGridCache> valueClass = value.getClass();
|
|
|
|
|
|
|
|
this.eventBus.readClass( key, valueClass );
|
|
|
|
this.caches.put( key, new GridCacheWrapper( value ) );
|
2014-06-10 19:16:14 +02:00
|
|
|
}
|
|
|
|
|
2014-11-30 04:58:08 +01:00
|
|
|
this.postEvent( new MENetworkPostCacheConstruction() );
|
2014-06-28 07:21:37 +02:00
|
|
|
|
2015-01-01 22:13:10 +01:00
|
|
|
TickHandler.INSTANCE.addNetwork( this );
|
2014-06-10 19:16:14 +02:00
|
|
|
center.setGrid( this );
|
|
|
|
}
|
|
|
|
|
2014-11-30 04:58:08 +01:00
|
|
|
public int getPriority()
|
2014-06-10 19:16:14 +02:00
|
|
|
{
|
2014-11-30 04:58:08 +01:00
|
|
|
return this.priority;
|
2014-06-10 19:16:14 +02:00
|
|
|
}
|
|
|
|
|
2014-11-30 04:58:08 +01:00
|
|
|
public IGridStorage getMyStorage()
|
|
|
|
{
|
|
|
|
return this.myStorage;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Map<Class<? extends IGridCache>, GridCacheWrapper> getCaches()
|
|
|
|
{
|
|
|
|
return this.caches;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Iterable<Class<? extends IGridHost>> getMachineClasses()
|
2014-06-10 19:16:14 +02:00
|
|
|
{
|
2014-11-30 04:58:08 +01:00
|
|
|
return this.machines.keySet();
|
2014-06-10 19:16:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public int size()
|
|
|
|
{
|
|
|
|
int out = 0;
|
2015-04-03 08:54:31 +02:00
|
|
|
for( Collection<?> x : this.machines.values() )
|
2014-06-10 19:16:14 +02:00
|
|
|
out += x.size();
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2014-11-30 04:58:08 +01:00
|
|
|
public void remove( GridNode gridNode )
|
2014-06-10 19:16:14 +02:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
for( IGridCache c : this.caches.values() )
|
2014-11-30 04:58:08 +01:00
|
|
|
{
|
|
|
|
IGridHost machine = gridNode.getMachine();
|
|
|
|
c.removeNode( gridNode, machine );
|
|
|
|
}
|
2014-06-10 19:16:14 +02:00
|
|
|
|
2014-11-30 04:58:08 +01:00
|
|
|
Class<? extends IGridHost> machineClass = gridNode.getMachineClass();
|
|
|
|
Set<IGridNode> nodes = this.machines.get( machineClass );
|
2015-04-03 08:54:31 +02:00
|
|
|
if( nodes != null )
|
2014-06-10 19:16:14 +02:00
|
|
|
nodes.remove( gridNode );
|
|
|
|
|
|
|
|
gridNode.setGridStorage( null );
|
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
if( this.pivot == gridNode )
|
2014-06-10 19:16:14 +02:00
|
|
|
{
|
2014-11-30 04:58:08 +01:00
|
|
|
Iterator<IGridNode> n = this.getNodes().iterator();
|
2015-04-03 08:54:31 +02:00
|
|
|
if( n.hasNext() )
|
|
|
|
this.pivot = (GridNode) n.next();
|
2014-06-10 19:16:14 +02:00
|
|
|
else
|
|
|
|
{
|
2014-11-30 04:58:08 +01:00
|
|
|
this.pivot = null;
|
2015-01-01 22:13:10 +01:00
|
|
|
TickHandler.INSTANCE.removeNetwork( this );
|
2014-11-30 04:58:08 +01:00
|
|
|
this.myStorage.remove();
|
2014-06-10 19:16:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-30 04:58:08 +01:00
|
|
|
public void add( GridNode gridNode )
|
2014-06-10 19:16:14 +02:00
|
|
|
{
|
|
|
|
Class<? extends IGridHost> mClass = gridNode.getMachineClass();
|
2014-11-30 04:58:08 +01:00
|
|
|
|
|
|
|
MachineSet nodes = this.machines.get( mClass );
|
2015-04-03 08:54:31 +02:00
|
|
|
if( nodes == null )
|
2014-06-10 19:16:14 +02:00
|
|
|
{
|
2014-11-30 04:58:08 +01:00
|
|
|
nodes = new MachineSet( mClass );
|
|
|
|
this.machines.put( mClass, nodes );
|
|
|
|
this.eventBus.readClass( mClass, mClass );
|
2014-06-10 19:16:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// handle loading grid storages.
|
2015-04-03 08:54:31 +02:00
|
|
|
if( gridNode.getGridStorage() != null )
|
2014-06-10 19:16:14 +02:00
|
|
|
{
|
|
|
|
GridStorage gs = gridNode.getGridStorage();
|
2014-11-02 10:23:32 +01:00
|
|
|
IGrid grid = gs.getGrid();
|
2014-06-10 19:16:14 +02:00
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
if( grid == null )
|
2014-06-10 19:16:14 +02:00
|
|
|
{
|
2014-11-30 04:58:08 +01:00
|
|
|
this.myStorage = gs;
|
|
|
|
this.myStorage.setGrid( this );
|
2014-06-10 19:16:14 +02:00
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
for( IGridCache gc : this.caches.values() )
|
2014-11-30 04:58:08 +01:00
|
|
|
gc.onJoin( this.myStorage );
|
2014-06-10 19:16:14 +02:00
|
|
|
}
|
2015-04-03 08:54:31 +02:00
|
|
|
else if( grid != this )
|
2014-06-10 19:16:14 +02:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
if( this.myStorage == null )
|
2014-06-10 19:16:14 +02:00
|
|
|
{
|
2014-11-30 04:58:08 +01:00
|
|
|
this.myStorage = WorldSettings.getInstance().getNewGridStorage();
|
|
|
|
this.myStorage.setGrid( this );
|
2014-06-10 19:16:14 +02:00
|
|
|
}
|
|
|
|
|
2014-11-30 04:58:08 +01:00
|
|
|
IGridStorage tmp = new GridStorage();
|
2015-04-03 08:54:31 +02:00
|
|
|
if( !gs.hasDivided( this.myStorage ) )
|
2014-06-10 19:16:14 +02:00
|
|
|
{
|
2014-11-30 04:58:08 +01:00
|
|
|
gs.addDivided( this.myStorage );
|
2014-06-10 19:16:14 +02:00
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
for( IGridCache gc : ( (Grid) grid ).caches.values() )
|
2014-06-10 19:16:14 +02:00
|
|
|
gc.onSplit( tmp );
|
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
for( IGridCache gc : this.caches.values() )
|
2014-06-10 19:16:14 +02:00
|
|
|
gc.onJoin( tmp );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-04-03 08:54:31 +02:00
|
|
|
else if( this.myStorage == null )
|
2014-06-10 19:16:14 +02:00
|
|
|
{
|
2014-11-30 04:58:08 +01:00
|
|
|
this.myStorage = WorldSettings.getInstance().getNewGridStorage();
|
|
|
|
this.myStorage.setGrid( this );
|
2014-06-10 19:16:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// update grid node...
|
2014-11-30 04:58:08 +01:00
|
|
|
gridNode.setGridStorage( this.myStorage );
|
2014-06-10 19:16:14 +02:00
|
|
|
|
|
|
|
// track node.
|
|
|
|
nodes.add( gridNode );
|
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
for( IGridCache cache : this.caches.values() )
|
2014-11-30 04:58:08 +01:00
|
|
|
{
|
|
|
|
IGridHost machine = gridNode.getMachine();
|
|
|
|
cache.addNode( gridNode, machine );
|
|
|
|
}
|
2014-06-10 19:16:14 +02:00
|
|
|
|
2014-11-30 04:58:08 +01:00
|
|
|
gridNode.getGridProxy().gridChanged();
|
2014-06-10 19:16:14 +02:00
|
|
|
// postEventTo( gridNode, networkChanged );
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-11-30 04:58:08 +01:00
|
|
|
@SuppressWarnings( "unchecked" )
|
|
|
|
public <C extends IGridCache> C getCache( Class<? extends IGridCache> iface )
|
2014-06-10 19:16:14 +02:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
return (C) this.caches.get( iface ).myCache;
|
2014-11-30 04:58:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public MENetworkEvent postEvent( MENetworkEvent ev )
|
|
|
|
{
|
|
|
|
return this.eventBus.postEvent( this, ev );
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public MENetworkEvent postEventTo( IGridNode node, MENetworkEvent ev )
|
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
return this.eventBus.postEventTo( this, (GridNode) node, ev );
|
2014-06-10 19:16:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public IReadOnlyCollection<Class<? extends IGridHost>> getMachinesClasses()
|
|
|
|
{
|
2014-11-30 04:58:08 +01:00
|
|
|
Set<Class<? extends IGridHost>> machineKeys = this.machines.keySet();
|
|
|
|
|
|
|
|
return new ReadOnlyCollection<Class<? extends IGridHost>>( machineKeys );
|
2014-06-10 19:16:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-11-30 04:58:08 +01:00
|
|
|
public IMachineSet getMachines( Class<? extends IGridHost> c )
|
2014-06-10 19:16:14 +02:00
|
|
|
{
|
2014-11-30 04:58:08 +01:00
|
|
|
MachineSet s = this.machines.get( c );
|
2015-04-03 08:54:31 +02:00
|
|
|
if( s == null )
|
2014-06-10 19:16:14 +02:00
|
|
|
return new MachineSet( c );
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-11-30 04:58:08 +01:00
|
|
|
public IReadOnlyCollection<IGridNode> getNodes()
|
2014-06-10 19:16:14 +02:00
|
|
|
{
|
2014-11-30 04:58:08 +01:00
|
|
|
return new GridNodeCollection( this.machines );
|
2014-06-10 19:16:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-11-30 04:58:08 +01:00
|
|
|
public boolean isEmpty()
|
2014-06-10 19:16:14 +02:00
|
|
|
{
|
2014-11-30 04:58:08 +01:00
|
|
|
return this.pivot == null;
|
2014-06-10 19:16:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-11-30 04:58:08 +01:00
|
|
|
public IGridNode getPivot()
|
2014-06-10 19:16:14 +02:00
|
|
|
{
|
2014-11-30 04:58:08 +01:00
|
|
|
return this.pivot;
|
2014-06-10 19:16:14 +02:00
|
|
|
}
|
|
|
|
|
2014-11-30 04:58:08 +01:00
|
|
|
public void setPivot( GridNode pivot )
|
2014-06-10 19:16:14 +02:00
|
|
|
{
|
2014-11-30 04:58:08 +01:00
|
|
|
this.pivot = pivot;
|
2014-06-10 19:16:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public void update()
|
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
for( IGridCache gc : this.caches.values() )
|
2014-06-10 19:16:14 +02:00
|
|
|
{
|
|
|
|
// are there any nodes left?
|
2015-04-03 08:54:31 +02:00
|
|
|
if( this.pivot != null )
|
2014-06-10 19:16:14 +02:00
|
|
|
gc.onUpdateTick();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void saveState()
|
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
for( IGridCache c : this.caches.values() )
|
2014-06-10 19:16:14 +02:00
|
|
|
{
|
2014-11-30 04:58:08 +01:00
|
|
|
c.populateGridStorage( this.myStorage );
|
2014-06-10 19:16:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-30 04:58:08 +01:00
|
|
|
public void setImportantFlag( int i, boolean publicHasPower )
|
2014-06-10 19:16:14 +02:00
|
|
|
{
|
|
|
|
int flag = 1 << i;
|
2014-11-30 04:58:08 +01:00
|
|
|
this.priority = ( this.priority & ~flag ) | ( publicHasPower ? flag : 0 );
|
2014-06-10 19:16:14 +02:00
|
|
|
}
|
|
|
|
}
|