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>.
|
|
|
|
*/
|
|
|
|
|
2013-12-27 23:59:59 +01:00
|
|
|
package appeng.me.cache;
|
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
|
2013-12-27 23:59:59 +01:00
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.LinkedList;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import appeng.api.networking.IGrid;
|
|
|
|
import appeng.api.networking.IGridHost;
|
|
|
|
import appeng.api.networking.IGridNode;
|
|
|
|
import appeng.api.networking.IGridStorage;
|
|
|
|
import appeng.api.networking.events.MENetworkBootingStatusChange;
|
|
|
|
import appeng.api.networking.events.MENetworkEventSubscribe;
|
|
|
|
import appeng.api.networking.spatial.ISpatialCache;
|
|
|
|
import appeng.api.util.DimensionalCoord;
|
|
|
|
import appeng.api.util.IReadOnlyCollection;
|
2014-02-09 06:08:27 +01:00
|
|
|
import appeng.core.AEConfig;
|
2013-12-27 23:59:59 +01:00
|
|
|
import appeng.me.cluster.implementations.SpatialPylonCluster;
|
|
|
|
import appeng.tile.spatial.TileSpatialIOPort;
|
|
|
|
import appeng.tile.spatial.TileSpatialPylon;
|
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
|
2014-10-04 08:08:28 +02:00
|
|
|
public class SpatialPylonCache implements ISpatialCache
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
final IGrid myGrid;
|
2013-12-27 23:59:59 +01:00
|
|
|
long powerRequired = 0;
|
2014-09-21 00:46:09 +02:00
|
|
|
double efficiency = 0.0;
|
2013-12-27 23:59:59 +01:00
|
|
|
DimensionalCoord captureMin;
|
|
|
|
DimensionalCoord captureMax;
|
|
|
|
boolean isValid = false;
|
2014-09-28 22:20:14 +02:00
|
|
|
List<TileSpatialIOPort> ioPorts = new LinkedList<TileSpatialIOPort>();
|
|
|
|
HashMap<SpatialPylonCluster, SpatialPylonCluster> clusters = new HashMap<SpatialPylonCluster, SpatialPylonCluster>();
|
2013-12-27 23:59:59 +01:00
|
|
|
boolean needsUpdate = false;
|
|
|
|
|
2015-09-25 23:10:56 +02:00
|
|
|
public SpatialPylonCache( final IGrid g )
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
this.myGrid = g;
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
@MENetworkEventSubscribe
|
2015-09-25 23:10:56 +02:00
|
|
|
public void bootingRender( final MENetworkBootingStatusChange c )
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
this.reset( this.myGrid );
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
|
2015-09-25 23:10:56 +02:00
|
|
|
public void reset( final IGrid grid )
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
|
|
|
|
2014-12-29 15:13:47 +01:00
|
|
|
this.clusters = new HashMap<SpatialPylonCluster, SpatialPylonCluster>();
|
|
|
|
this.ioPorts = new LinkedList<TileSpatialIOPort>();
|
2013-12-27 23:59:59 +01:00
|
|
|
|
2015-09-25 23:10:56 +02:00
|
|
|
for( final IGridNode gm : grid.getMachines( TileSpatialIOPort.class ) )
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
2014-12-29 15:13:47 +01:00
|
|
|
this.ioPorts.add( (TileSpatialIOPort) gm.getMachine() );
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
|
2015-09-25 23:10:56 +02:00
|
|
|
final IReadOnlyCollection<IGridNode> set = grid.getMachines( TileSpatialPylon.class );
|
|
|
|
for( final IGridNode gm : set )
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
if( gm.meetsChannelRequirements() )
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
2015-09-25 23:10:56 +02:00
|
|
|
final SpatialPylonCluster c = ( (TileSpatialPylon) gm.getMachine() ).getCluster();
|
2015-04-03 08:54:31 +02:00
|
|
|
if( c != null )
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2014-12-29 15:13:47 +01:00
|
|
|
this.clusters.put( c, c );
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-29 15:13:47 +01:00
|
|
|
this.captureMax = null;
|
|
|
|
this.captureMin = null;
|
|
|
|
this.isValid = true;
|
2013-12-27 23:59:59 +01:00
|
|
|
|
|
|
|
int pylonBlocks = 0;
|
2015-09-25 23:10:56 +02:00
|
|
|
for( final SpatialPylonCluster cl : this.clusters.values() )
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
if( this.captureMax == null )
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2014-12-29 15:13:47 +01:00
|
|
|
this.captureMax = cl.max.copy();
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
2015-04-03 08:54:31 +02:00
|
|
|
if( this.captureMin == null )
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2014-12-29 15:13:47 +01:00
|
|
|
this.captureMin = cl.min.copy();
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
2013-12-27 23:59:59 +01:00
|
|
|
|
|
|
|
pylonBlocks += cl.tileCount();
|
|
|
|
|
2014-12-29 15:13:47 +01:00
|
|
|
this.captureMin.x = Math.min( this.captureMin.x, cl.min.x );
|
|
|
|
this.captureMin.y = Math.min( this.captureMin.y, cl.min.y );
|
|
|
|
this.captureMin.z = Math.min( this.captureMin.z, cl.min.z );
|
2013-12-27 23:59:59 +01:00
|
|
|
|
2014-12-29 15:13:47 +01:00
|
|
|
this.captureMax.x = Math.max( this.captureMax.x, cl.max.x );
|
|
|
|
this.captureMax.y = Math.max( this.captureMax.y, cl.max.y );
|
|
|
|
this.captureMax.z = Math.max( this.captureMax.z, cl.max.z );
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
|
2015-09-25 22:52:41 +02:00
|
|
|
double maxPower = 0;
|
|
|
|
double minPower = 0;
|
2015-04-03 08:54:31 +02:00
|
|
|
if( this.hasRegion() )
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
2014-12-29 15:13:47 +01:00
|
|
|
this.isValid = this.captureMax.x - this.captureMin.x > 1 && this.captureMax.y - this.captureMin.y > 1 && this.captureMax.z - this.captureMin.z > 1;
|
2013-12-27 23:59:59 +01:00
|
|
|
|
2015-09-25 23:10:56 +02:00
|
|
|
for( final SpatialPylonCluster cl : this.clusters.values() )
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
switch( cl.currentAxis )
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
case X:
|
2013-12-27 23:59:59 +01:00
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
this.isValid = this.isValid && ( ( this.captureMax.y == cl.min.y || this.captureMin.y == cl.max.y ) || ( this.captureMax.z == cl.min.z || this.captureMin.z == cl.max.z ) ) && ( ( this.captureMax.y == cl.max.y || this.captureMin.y == cl.min.y ) || ( this.captureMax.z == cl.max.z || this.captureMin.z == cl.min.z ) );
|
2013-12-27 23:59:59 +01:00
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
break;
|
|
|
|
case Y:
|
2013-12-27 23:59:59 +01:00
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
this.isValid = this.isValid && ( ( this.captureMax.x == cl.min.x || this.captureMin.x == cl.max.x ) || ( this.captureMax.z == cl.min.z || this.captureMin.z == cl.max.z ) ) && ( ( this.captureMax.x == cl.max.x || this.captureMin.x == cl.min.x ) || ( this.captureMax.z == cl.max.z || this.captureMin.z == cl.min.z ) );
|
2013-12-27 23:59:59 +01:00
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
break;
|
|
|
|
case Z:
|
2013-12-27 23:59:59 +01:00
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
this.isValid = this.isValid && ( ( this.captureMax.y == cl.min.y || this.captureMin.y == cl.max.y ) || ( this.captureMax.x == cl.min.x || this.captureMin.x == cl.max.x ) ) && ( ( this.captureMax.y == cl.max.y || this.captureMin.y == cl.min.y ) || ( this.captureMax.x == cl.max.x || this.captureMin.x == cl.min.x ) );
|
2013-12-27 23:59:59 +01:00
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
break;
|
|
|
|
case UNFORMED:
|
|
|
|
this.isValid = false;
|
|
|
|
break;
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-25 23:10:56 +02:00
|
|
|
final int reqX = this.captureMax.x - this.captureMin.x;
|
|
|
|
final int reqY = this.captureMax.y - this.captureMin.y;
|
|
|
|
final int reqZ = this.captureMax.z - this.captureMin.z;
|
|
|
|
final int requirePylonBlocks = Math.max( 6, ( ( reqX * reqZ + reqX * reqY + reqY * reqZ ) * 3 ) / 8 );
|
2015-02-03 12:04:13 +01:00
|
|
|
|
2014-12-29 15:13:47 +01:00
|
|
|
this.efficiency = (double) pylonBlocks / (double) requirePylonBlocks;
|
2015-02-03 12:04:13 +01:00
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
if( this.efficiency > 1.0 )
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2014-12-29 15:13:47 +01:00
|
|
|
this.efficiency = 1.0;
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
2015-04-03 08:54:31 +02:00
|
|
|
if( this.efficiency < 0.0 )
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2014-12-29 15:13:47 +01:00
|
|
|
this.efficiency = 0.0;
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
2015-02-03 12:04:13 +01:00
|
|
|
|
2014-02-09 06:08:27 +01:00
|
|
|
minPower = (double) reqX * (double) reqY * reqZ * AEConfig.instance.spatialPowerMultiplier;
|
2014-09-28 11:47:17 +02:00
|
|
|
maxPower = Math.pow( minPower, AEConfig.instance.spatialPowerExponent );
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
|
2015-09-25 23:10:56 +02:00
|
|
|
final double affective_efficiency = Math.pow( this.efficiency, 0.25 );
|
2015-04-03 08:54:31 +02:00
|
|
|
this.powerRequired = (long) ( affective_efficiency * minPower + ( 1.0 - affective_efficiency ) * maxPower );
|
2013-12-27 23:59:59 +01:00
|
|
|
|
2015-09-25 23:10:56 +02:00
|
|
|
for( final SpatialPylonCluster cl : this.clusters.values() )
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
2015-09-25 23:10:56 +02:00
|
|
|
final boolean myWasValid = cl.isValid;
|
2014-12-29 15:13:47 +01:00
|
|
|
cl.isValid = this.isValid;
|
2015-04-03 08:54:31 +02:00
|
|
|
if( myWasValid != this.isValid )
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2013-12-27 23:59:59 +01:00
|
|
|
cl.updateStatus( false );
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-06 06:45:14 +01:00
|
|
|
@Override
|
2015-04-03 08:54:31 +02:00
|
|
|
public boolean hasRegion()
|
2014-03-06 06:45:14 +01:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
return this.captureMin != null;
|
2014-03-06 06:45:14 +01:00
|
|
|
}
|
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
@Override
|
|
|
|
public boolean isValidRegion()
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
return this.hasRegion() && this.isValid;
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-04-03 08:54:31 +02:00
|
|
|
public DimensionalCoord getMin()
|
|
|
|
{
|
|
|
|
return this.captureMin;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public DimensionalCoord getMax()
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
return this.captureMax;
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-04-03 08:54:31 +02:00
|
|
|
public long requiredPower()
|
|
|
|
{
|
|
|
|
return this.powerRequired;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public float currentEfficiency()
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
return (float) this.efficiency * 100;
|
|
|
|
}
|
2013-12-27 23:59:59 +01:00
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
@Override
|
|
|
|
public void onUpdateTick()
|
|
|
|
{
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-09-25 23:10:56 +02:00
|
|
|
public void removeNode( final IGridNode node, final IGridHost machine )
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-09-25 23:10:56 +02:00
|
|
|
public void addNode( final IGridNode node, final IGridHost machine )
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-09-25 23:10:56 +02:00
|
|
|
public void onSplit( final IGridStorage storageB )
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-09-25 23:10:56 +02:00
|
|
|
public void onJoin( final IGridStorage storageB )
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
@Override
|
2015-09-25 23:10:56 +02:00
|
|
|
public void populateGridStorage( final IGridStorage storage )
|
2015-04-03 08:54:31 +02:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|