Applied-Energistics-2-tiler.../src/main/java/appeng/me/Grid.java

283 lines
6.9 KiB
Java
Raw Normal View History

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-06-10 19:16:14 +02:00
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
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;
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-06-10 19:16:14 +02:00
public class Grid implements IGrid
{
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;
Map<Class<? extends IGridCache>, IGridCache> myCaches = AEApi.instance().registries().gridCache().createCacheInstance( this );
for( Entry<Class<? extends IGridCache>, IGridCache> c : myCaches.entrySet() )
2014-06-10 19:16:14 +02: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
}
this.postEvent( new MENetworkPostCacheConstruction() );
2015-01-01 22:13:10 +01:00
TickHandler.INSTANCE.addNetwork( this );
2014-06-10 19:16:14 +02:00
center.setGrid( this );
}
public int getPriority()
2014-06-10 19:16:14 +02:00
{
return this.priority;
2014-06-10 19:16:14 +02: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
{
return this.machines.keySet();
2014-06-10 19:16:14 +02:00
}
public int size()
{
int out = 0;
for( Collection<?> x : this.machines.values() )
2014-06-10 19:16:14 +02:00
out += x.size();
return out;
}
public void remove( GridNode gridNode )
2014-06-10 19:16:14 +02:00
{
for( IGridCache c : this.caches.values() )
{
IGridHost machine = gridNode.getMachine();
c.removeNode( gridNode, machine );
}
2014-06-10 19:16:14 +02:00
Class<? extends IGridHost> machineClass = gridNode.getMachineClass();
Set<IGridNode> nodes = this.machines.get( machineClass );
if( nodes != null )
2014-06-10 19:16:14 +02:00
nodes.remove( gridNode );
gridNode.setGridStorage( null );
if( this.pivot == gridNode )
2014-06-10 19:16:14 +02:00
{
Iterator<IGridNode> n = this.getNodes().iterator();
if( n.hasNext() )
this.pivot = (GridNode) n.next();
2014-06-10 19:16:14 +02:00
else
{
this.pivot = null;
2015-01-01 22:13:10 +01:00
TickHandler.INSTANCE.removeNetwork( this );
this.myStorage.remove();
2014-06-10 19:16:14 +02:00
}
}
}
public void add( GridNode gridNode )
2014-06-10 19:16:14 +02:00
{
Class<? extends IGridHost> mClass = gridNode.getMachineClass();
MachineSet nodes = this.machines.get( mClass );
if( nodes == null )
2014-06-10 19:16:14 +02: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.
if( gridNode.getGridStorage() != null )
2014-06-10 19:16:14 +02:00
{
GridStorage gs = gridNode.getGridStorage();
IGrid grid = gs.getGrid();
2014-06-10 19:16:14 +02:00
if( grid == null )
2014-06-10 19:16:14 +02:00
{
this.myStorage = gs;
this.myStorage.setGrid( this );
2014-06-10 19:16:14 +02:00
for( IGridCache gc : this.caches.values() )
gc.onJoin( this.myStorage );
2014-06-10 19:16:14 +02:00
}
else if( grid != this )
2014-06-10 19:16:14 +02:00
{
if( this.myStorage == null )
2014-06-10 19:16:14 +02:00
{
this.myStorage = WorldSettings.getInstance().getNewGridStorage();
this.myStorage.setGrid( this );
2014-06-10 19:16:14 +02:00
}
IGridStorage tmp = new GridStorage();
if( !gs.hasDivided( this.myStorage ) )
2014-06-10 19:16:14 +02:00
{
gs.addDivided( this.myStorage );
2014-06-10 19:16:14 +02:00
for( IGridCache gc : ( (Grid) grid ).caches.values() )
2014-06-10 19:16:14 +02:00
gc.onSplit( tmp );
for( IGridCache gc : this.caches.values() )
2014-06-10 19:16:14 +02:00
gc.onJoin( tmp );
}
}
}
else if( this.myStorage == null )
2014-06-10 19:16:14 +02:00
{
this.myStorage = WorldSettings.getInstance().getNewGridStorage();
this.myStorage.setGrid( this );
2014-06-10 19:16:14 +02:00
}
// update grid node...
gridNode.setGridStorage( this.myStorage );
2014-06-10 19:16:14 +02:00
// track node.
nodes.add( gridNode );
for( IGridCache cache : this.caches.values() )
{
IGridHost machine = gridNode.getMachine();
cache.addNode( gridNode, machine );
}
2014-06-10 19:16:14 +02:00
gridNode.getGridProxy().gridChanged();
2014-06-10 19:16:14 +02:00
// postEventTo( gridNode, networkChanged );
}
@Override
@SuppressWarnings( "unchecked" )
public <C extends IGridCache> C getCache( Class<? extends IGridCache> iface )
2014-06-10 19:16:14 +02:00
{
return (C) this.caches.get( iface ).myCache;
}
@Override
public MENetworkEvent postEvent( MENetworkEvent ev )
{
return this.eventBus.postEvent( this, ev );
}
@Override
public MENetworkEvent postEventTo( IGridNode node, MENetworkEvent ev )
{
return this.eventBus.postEventTo( this, (GridNode) node, ev );
2014-06-10 19:16:14 +02:00
}
@Override
public IReadOnlyCollection<Class<? extends IGridHost>> getMachinesClasses()
{
Set<Class<? extends IGridHost>> machineKeys = this.machines.keySet();
return new ReadOnlyCollection<Class<? extends IGridHost>>( machineKeys );
2014-06-10 19:16:14 +02:00
}
@Override
public IMachineSet getMachines( Class<? extends IGridHost> c )
2014-06-10 19:16:14 +02:00
{
MachineSet s = this.machines.get( c );
if( s == null )
2014-06-10 19:16:14 +02:00
return new MachineSet( c );
return s;
}
@Override
public IReadOnlyCollection<IGridNode> getNodes()
2014-06-10 19:16:14 +02:00
{
return new GridNodeCollection( this.machines );
2014-06-10 19:16:14 +02:00
}
@Override
public boolean isEmpty()
2014-06-10 19:16:14 +02:00
{
return this.pivot == null;
2014-06-10 19:16:14 +02:00
}
@Override
public IGridNode getPivot()
2014-06-10 19:16:14 +02:00
{
return this.pivot;
2014-06-10 19:16:14 +02:00
}
public void setPivot( GridNode pivot )
2014-06-10 19:16:14 +02:00
{
this.pivot = pivot;
2014-06-10 19:16:14 +02:00
}
public void update()
{
for( IGridCache gc : this.caches.values() )
2014-06-10 19:16:14 +02:00
{
// are there any nodes left?
if( this.pivot != null )
2014-06-10 19:16:14 +02:00
gc.onUpdateTick();
}
}
public void saveState()
{
for( IGridCache c : this.caches.values() )
2014-06-10 19:16:14 +02:00
{
c.populateGridStorage( this.myStorage );
2014-06-10 19:16:14 +02:00
}
}
public void setImportantFlag( int i, boolean publicHasPower )
2014-06-10 19:16:14 +02:00
{
int flag = 1 << i;
this.priority = ( this.priority & ~flag ) | ( publicHasPower ? flag : 0 );
2014-06-10 19:16:14 +02:00
}
}