2013-04-22 09:37:53 +02:00
|
|
|
package com.massivecraft.factions.entity;
|
2013-04-11 08:49:14 +02:00
|
|
|
|
2013-04-11 09:25:26 +02:00
|
|
|
import java.util.ArrayList;
|
2014-10-07 17:08:40 +02:00
|
|
|
import java.util.Collection;
|
2013-04-24 11:16:37 +02:00
|
|
|
import java.util.HashSet;
|
2014-10-13 11:42:40 +02:00
|
|
|
import java.util.LinkedHashMap;
|
2014-10-07 17:08:40 +02:00
|
|
|
import java.util.LinkedHashSet;
|
2014-10-13 11:42:40 +02:00
|
|
|
import java.util.Map;
|
2014-10-22 07:50:30 +02:00
|
|
|
import java.util.Map.Entry;
|
2013-04-24 11:16:37 +02:00
|
|
|
import java.util.Set;
|
2013-04-11 09:50:48 +02:00
|
|
|
|
2014-09-17 13:17:33 +02:00
|
|
|
import com.massivecraft.factions.Const;
|
2013-04-22 09:37:53 +02:00
|
|
|
import com.massivecraft.factions.Factions;
|
|
|
|
import com.massivecraft.factions.RelationParticipator;
|
|
|
|
import com.massivecraft.factions.TerritoryAccess;
|
2014-10-22 07:50:30 +02:00
|
|
|
import com.massivecraft.massivecore.collections.MassiveMap;
|
2014-06-04 14:02:23 +02:00
|
|
|
import com.massivecraft.massivecore.ps.PS;
|
|
|
|
import com.massivecraft.massivecore.store.Coll;
|
|
|
|
import com.massivecraft.massivecore.store.MStore;
|
|
|
|
import com.massivecraft.massivecore.util.MUtil;
|
2013-04-11 08:49:14 +02:00
|
|
|
|
2013-04-12 15:10:11 +02:00
|
|
|
public class BoardColl extends Coll<Board> implements BoardInterface
|
2013-04-11 08:49:14 +02:00
|
|
|
{
|
|
|
|
// -------------------------------------------- //
|
2014-09-17 13:17:33 +02:00
|
|
|
// INSTANCE & CONSTRUCT
|
2013-04-11 08:49:14 +02:00
|
|
|
// -------------------------------------------- //
|
|
|
|
|
2014-09-17 13:17:33 +02:00
|
|
|
private static BoardColl i = new BoardColl();
|
|
|
|
public static BoardColl get() { return i; }
|
|
|
|
private BoardColl()
|
2013-04-11 08:49:14 +02:00
|
|
|
{
|
2014-09-17 13:17:33 +02:00
|
|
|
super(Const.COLLECTION_BOARD, Board.class, MStore.getDb(), Factions.get(), false, true, true);
|
2013-04-11 08:49:14 +02:00
|
|
|
}
|
|
|
|
|
2013-04-11 09:25:26 +02:00
|
|
|
// -------------------------------------------- //
|
|
|
|
// OVERRIDE: COLL
|
|
|
|
// -------------------------------------------- //
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String fixId(Object oid)
|
|
|
|
{
|
|
|
|
if (oid == null) return null;
|
|
|
|
if (oid instanceof String) return (String)oid;
|
|
|
|
if (oid instanceof Board) return this.getId(oid);
|
|
|
|
|
|
|
|
return MUtil.extract(String.class, "worldName", oid);
|
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------------- //
|
|
|
|
// OVERRIDE: BOARD
|
|
|
|
// -------------------------------------------- //
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public TerritoryAccess getTerritoryAccessAt(PS ps)
|
|
|
|
{
|
|
|
|
if (ps == null) return null;
|
|
|
|
Board board = this.get(ps.getWorld());
|
|
|
|
if (board == null) return null;
|
|
|
|
return board.getTerritoryAccessAt(ps);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Faction getFactionAt(PS ps)
|
|
|
|
{
|
|
|
|
if (ps == null) return null;
|
|
|
|
Board board = this.get(ps.getWorld());
|
|
|
|
if (board == null) return null;
|
|
|
|
return board.getFactionAt(ps);
|
|
|
|
}
|
|
|
|
|
|
|
|
// SET
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setTerritoryAccessAt(PS ps, TerritoryAccess territoryAccess)
|
|
|
|
{
|
|
|
|
if (ps == null) return;
|
|
|
|
Board board = this.get(ps.getWorld());
|
|
|
|
if (board == null) return;
|
|
|
|
board.setTerritoryAccessAt(ps, territoryAccess);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setFactionAt(PS ps, Faction faction)
|
|
|
|
{
|
|
|
|
if (ps == null) return;
|
|
|
|
Board board = this.get(ps.getWorld());
|
|
|
|
if (board == null) return;
|
|
|
|
board.setFactionAt(ps, faction);
|
|
|
|
}
|
|
|
|
|
|
|
|
// REMOVE
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void removeAt(PS ps)
|
|
|
|
{
|
|
|
|
if (ps == null) return;
|
|
|
|
Board board = this.get(ps.getWorld());
|
|
|
|
if (board == null) return;
|
|
|
|
board.removeAt(ps);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void removeAll(Faction faction)
|
|
|
|
{
|
|
|
|
for (Board board : this.getAll())
|
|
|
|
{
|
|
|
|
board.removeAll(faction);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void clean()
|
|
|
|
{
|
|
|
|
for (Board board : this.getAll())
|
|
|
|
{
|
|
|
|
board.clean();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-24 11:16:37 +02:00
|
|
|
// CHUNKS
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Set<PS> getChunks(Faction faction)
|
|
|
|
{
|
|
|
|
Set<PS> ret = new HashSet<PS>();
|
|
|
|
for (Board board : this.getAll())
|
|
|
|
{
|
|
|
|
ret.addAll(board.getChunks(faction));
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-10-22 07:50:30 +02:00
|
|
|
@Override
|
|
|
|
public Set<PS> getChunks(String factionId)
|
|
|
|
{
|
|
|
|
Set<PS> ret = new HashSet<PS>();
|
|
|
|
for (Board board : this.getAll())
|
|
|
|
{
|
|
|
|
ret.addAll(board.getChunks(factionId));
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Map<Faction, Set<PS>> getFactionToChunks()
|
|
|
|
{
|
|
|
|
Map<Faction, Set<PS>> ret = null;
|
|
|
|
for (Board board : this.getAll())
|
|
|
|
{
|
|
|
|
// Use the first board directly
|
|
|
|
Map<Faction, Set<PS>> factionToChunks = board.getFactionToChunks();
|
|
|
|
if (ret == null)
|
|
|
|
{
|
|
|
|
ret = factionToChunks;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Merge the following boards
|
|
|
|
for (Entry<Faction, Set<PS>> entry : factionToChunks.entrySet())
|
|
|
|
{
|
|
|
|
Faction faction = entry.getKey();
|
|
|
|
Set<PS> chunks = ret.get(faction);
|
|
|
|
if (chunks == null)
|
|
|
|
{
|
|
|
|
ret.put(faction, entry.getValue());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
chunks.addAll(entry.getValue());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-07 08:38:26 +01:00
|
|
|
if (ret == null) ret = new MassiveMap<Faction, Set<PS>>();
|
2014-10-22 07:50:30 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-04-11 09:25:26 +02:00
|
|
|
// COUNT
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getCount(Faction faction)
|
2014-10-18 18:30:13 +02:00
|
|
|
{
|
|
|
|
return this.getCount(faction.getId());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getCount(String factionId)
|
2013-04-11 09:25:26 +02:00
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
for (Board board : this.getAll())
|
|
|
|
{
|
2014-10-18 18:30:13 +02:00
|
|
|
ret += board.getCount(factionId);
|
2013-04-11 09:25:26 +02:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-10-22 07:50:30 +02:00
|
|
|
@Override
|
|
|
|
public Map<Faction, Integer> getFactionToCount()
|
|
|
|
{
|
|
|
|
Map<Faction, Integer> ret = null;
|
|
|
|
for (Board board : this.getAll())
|
|
|
|
{
|
|
|
|
// Use the first board directly
|
|
|
|
Map<Faction, Integer> factionToCount = board.getFactionToCount();
|
|
|
|
if (ret == null)
|
|
|
|
{
|
|
|
|
ret = factionToCount;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Merge the following boards
|
|
|
|
for (Entry<Faction, Integer> entry : factionToCount.entrySet())
|
|
|
|
{
|
|
|
|
Faction faction = entry.getKey();
|
|
|
|
Integer count = ret.get(faction);
|
|
|
|
if (count == null)
|
|
|
|
{
|
|
|
|
ret.put(faction, entry.getValue());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ret.put(faction, count + entry.getValue());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-07 08:38:26 +01:00
|
|
|
if (ret == null) ret = new MassiveMap<Faction, Integer>();
|
2014-10-22 07:50:30 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-04-11 09:25:26 +02:00
|
|
|
// NEARBY DETECTION
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean isBorderPs(PS ps)
|
|
|
|
{
|
|
|
|
if (ps == null) return false;
|
|
|
|
Board board = this.get(ps.getWorld());
|
|
|
|
if (board == null) return false;
|
|
|
|
return board.isBorderPs(ps);
|
|
|
|
}
|
|
|
|
|
2014-10-13 11:42:40 +02:00
|
|
|
@Override
|
|
|
|
public boolean isAnyBorderPs(Set<PS> pss)
|
|
|
|
{
|
|
|
|
for (PS ps : pss)
|
|
|
|
{
|
|
|
|
if (this.isBorderPs(ps)) return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-04-11 09:25:26 +02:00
|
|
|
@Override
|
|
|
|
public boolean isConnectedPs(PS ps, Faction faction)
|
|
|
|
{
|
|
|
|
if (ps == null) return false;
|
|
|
|
Board board = this.get(ps.getWorld());
|
|
|
|
if (board == null) return false;
|
|
|
|
return board.isConnectedPs(ps, faction);
|
|
|
|
}
|
|
|
|
|
2014-10-13 11:42:40 +02:00
|
|
|
@Override
|
|
|
|
public boolean isAnyConnectedPs(Set<PS> pss, Faction faction)
|
|
|
|
{
|
|
|
|
for (PS ps : pss)
|
|
|
|
{
|
|
|
|
if (this.isConnectedPs(ps, faction)) return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-04-11 09:25:26 +02:00
|
|
|
// MAP GENERATION
|
|
|
|
|
|
|
|
@Override
|
2014-10-14 09:09:37 +02:00
|
|
|
public ArrayList<String> getMap(RelationParticipator observer, PS centerPs, double inDegrees, int width, int height)
|
2013-04-11 09:25:26 +02:00
|
|
|
{
|
|
|
|
if (centerPs == null) return null;
|
|
|
|
Board board = this.get(centerPs.getWorld());
|
|
|
|
if (board == null) return null;
|
2014-10-14 09:09:37 +02:00
|
|
|
return board.getMap(observer, centerPs, inDegrees, width, height);
|
2013-04-11 09:25:26 +02:00
|
|
|
}
|
|
|
|
|
2014-10-07 17:08:40 +02:00
|
|
|
// -------------------------------------------- //
|
|
|
|
// UTIL
|
|
|
|
// -------------------------------------------- //
|
2014-09-17 13:17:33 +02:00
|
|
|
|
2014-10-07 17:08:40 +02:00
|
|
|
// Distance -1 returns 0 chunks always.
|
|
|
|
// Distance 0 returns 1 chunk only (the one supplied).
|
|
|
|
// Distance 1 returns 3x3 = 9 chunks.
|
2014-10-13 11:42:40 +02:00
|
|
|
public static Set<PS> getNearbyChunks(PS chunk, int distance)
|
2014-09-17 13:17:33 +02:00
|
|
|
{
|
2014-10-07 17:08:40 +02:00
|
|
|
// Fix Args
|
|
|
|
if (chunk == null) throw new NullPointerException("chunk");
|
|
|
|
chunk = chunk.getChunk(true);
|
|
|
|
|
|
|
|
// Create Ret
|
|
|
|
Set<PS> ret = new LinkedHashSet<PS>();
|
2014-09-17 13:17:33 +02:00
|
|
|
|
2014-10-07 17:08:40 +02:00
|
|
|
if (distance < 0) return ret;
|
2014-09-17 13:17:33 +02:00
|
|
|
|
2014-10-07 17:08:40 +02:00
|
|
|
// Main
|
|
|
|
int xmin = chunk.getChunkX() - distance;
|
|
|
|
int xmax = chunk.getChunkX() + distance;
|
2014-09-17 13:17:33 +02:00
|
|
|
|
2014-10-07 17:08:40 +02:00
|
|
|
int zmin = chunk.getChunkZ() - distance;
|
|
|
|
int zmax = chunk.getChunkZ() + distance;
|
|
|
|
|
|
|
|
for (int x = xmin; x <= xmax; x++)
|
2014-09-17 13:17:33 +02:00
|
|
|
{
|
2014-10-07 17:08:40 +02:00
|
|
|
for (int z = zmin; z <= zmax; z++)
|
2014-09-17 13:17:33 +02:00
|
|
|
{
|
2014-10-07 17:08:40 +02:00
|
|
|
ret.add(chunk.withChunkX(x).withChunkZ(z));
|
2014-09-17 13:17:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-07 17:08:40 +02:00
|
|
|
// Return Ret
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-10-13 11:42:40 +02:00
|
|
|
public static Set<PS> getNearbyChunks(Collection<PS> chunks, int distance)
|
|
|
|
{
|
|
|
|
// Fix Args
|
|
|
|
if (chunks == null) throw new NullPointerException("chunks");
|
|
|
|
|
|
|
|
// Create Ret
|
|
|
|
Set<PS> ret = new LinkedHashSet<PS>();
|
|
|
|
|
|
|
|
if (distance < 0) return ret;
|
|
|
|
|
|
|
|
// Main
|
|
|
|
for (PS chunk : chunks)
|
|
|
|
{
|
|
|
|
ret.addAll(getNearbyChunks(chunk, distance));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return Ret
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-10-07 17:08:40 +02:00
|
|
|
public static Set<Faction> getDistinctFactions(Collection<PS> chunks)
|
|
|
|
{
|
|
|
|
// Fix Args
|
|
|
|
if (chunks == null) throw new NullPointerException("chunks");
|
|
|
|
|
|
|
|
// Create Ret
|
|
|
|
Set<Faction> ret = new LinkedHashSet<Faction>();
|
|
|
|
|
|
|
|
// Main
|
|
|
|
for (PS chunk : chunks)
|
|
|
|
{
|
|
|
|
Faction faction = BoardColl.get().getFactionAt(chunk);
|
|
|
|
if (faction == null) continue;
|
|
|
|
ret.add(faction);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return Ret
|
|
|
|
return ret;
|
2014-09-17 13:17:33 +02:00
|
|
|
}
|
|
|
|
|
2014-10-13 11:42:40 +02:00
|
|
|
public static Map<PS, Faction> getChunkFaction(Collection<PS> chunks)
|
|
|
|
{
|
|
|
|
Map<PS, Faction> ret = new LinkedHashMap<PS, Faction>();
|
|
|
|
|
|
|
|
for (PS chunk : chunks)
|
|
|
|
{
|
|
|
|
chunk = chunk.getChunk(true);
|
|
|
|
Faction faction = get().getFactionAt(chunk);
|
|
|
|
if (faction == null) faction = FactionColl.get().getNone();
|
|
|
|
ret.put(chunk, faction);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-04-11 08:49:14 +02:00
|
|
|
}
|