2011-02-06 13:36:11 +01:00
|
|
|
package com.bukkit.mcteam.factions.entities;
|
|
|
|
|
|
|
|
import java.util.*;
|
|
|
|
import java.util.Map.Entry;
|
|
|
|
|
|
|
|
import org.bukkit.ChatColor;
|
2011-02-13 11:18:08 +01:00
|
|
|
import org.bukkit.World;
|
2011-02-06 13:36:11 +01:00
|
|
|
|
|
|
|
import com.bukkit.mcteam.factions.util.TextUtil;
|
|
|
|
import com.bukkit.mcteam.util.AsciiCompass;
|
|
|
|
|
|
|
|
//import com.bukkit.mcteam.factions.util.*;
|
|
|
|
|
|
|
|
public class Board {
|
2011-02-13 11:18:08 +01:00
|
|
|
public transient Long id;
|
|
|
|
protected Map<Coord, Integer> coordFactionIds;
|
2011-02-06 13:36:11 +01:00
|
|
|
|
2011-02-13 11:18:08 +01:00
|
|
|
public Board() {
|
2011-02-06 13:36:11 +01:00
|
|
|
coordFactionIds = new HashMap<Coord, Integer>();
|
|
|
|
}
|
|
|
|
|
2011-02-13 11:18:08 +01:00
|
|
|
public Faction getFactionAt(Coord coord) {
|
2011-02-06 13:36:11 +01:00
|
|
|
return Faction.get(getFactionIdAt(coord));
|
|
|
|
}
|
2011-02-13 11:18:08 +01:00
|
|
|
public int getFactionIdAt(Coord coord) {
|
2011-02-06 13:36:11 +01:00
|
|
|
Integer factionId = coordFactionIds.get(coord);
|
|
|
|
if (factionId == null) {
|
|
|
|
return 0; // No faction
|
|
|
|
}
|
|
|
|
return factionId;
|
|
|
|
}
|
|
|
|
|
2011-02-13 11:18:08 +01:00
|
|
|
public void unclaim(Coord coord) {
|
2011-02-06 13:36:11 +01:00
|
|
|
coordFactionIds.remove(coord);
|
|
|
|
save();
|
|
|
|
}
|
|
|
|
|
2011-02-13 11:18:08 +01:00
|
|
|
public void claim(Coord coord, Faction faction) {
|
2011-02-06 13:36:11 +01:00
|
|
|
coordFactionIds.put(coord, faction.id);
|
|
|
|
save();
|
|
|
|
}
|
|
|
|
|
2011-02-12 18:05:05 +01:00
|
|
|
|
|
|
|
// Is this coord NOT completely surrounded by coords claimed by the same faction?
|
|
|
|
// Simpler: Is there any nearby coord with a faction other than the faction here?
|
2011-02-13 11:18:08 +01:00
|
|
|
public boolean isBorderCoord(Coord coord) {
|
|
|
|
Faction faction = getFactionAt(coord);
|
2011-02-06 13:36:11 +01:00
|
|
|
Coord a = coord.getRelative(1, 0);
|
|
|
|
Coord b = coord.getRelative(-1, 0);
|
|
|
|
Coord c = coord.getRelative(0, 1);
|
|
|
|
Coord d = coord.getRelative(0, -1);
|
2011-02-13 11:18:08 +01:00
|
|
|
return faction != this.getFactionAt(a) || faction != this.getFactionAt(b) || faction != this.getFactionAt(c) || faction != this.getFactionAt(d);
|
2011-02-06 13:36:11 +01:00
|
|
|
}
|
|
|
|
|
2011-02-13 11:18:08 +01:00
|
|
|
//----------------------------------------------//
|
|
|
|
// Purge faction
|
|
|
|
//----------------------------------------------//
|
|
|
|
|
|
|
|
public void purgeFaction(int factionId) {
|
2011-02-06 13:36:11 +01:00
|
|
|
Iterator<Entry<Coord, Integer>> iter = coordFactionIds.entrySet().iterator();
|
|
|
|
while (iter.hasNext()) {
|
|
|
|
Entry<Coord, Integer> entry = iter.next();
|
|
|
|
if (entry.getValue().equals(factionId)) {
|
|
|
|
iter.remove();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-02-13 11:18:08 +01:00
|
|
|
public void purgeFaction(Faction faction) {
|
|
|
|
purgeFaction(faction.id);
|
|
|
|
}
|
2011-02-06 13:36:11 +01:00
|
|
|
|
2011-02-13 11:18:08 +01:00
|
|
|
public static void purgeFactionFromAllBoards(int factionId) {
|
|
|
|
for (Board board : getAll()) {
|
|
|
|
board.purgeFaction(factionId);
|
|
|
|
}
|
2011-02-06 13:36:11 +01:00
|
|
|
}
|
2011-02-13 11:18:08 +01:00
|
|
|
public static void purgeFactionFromAllBoards(Faction faction) {
|
|
|
|
purgeFactionFromAllBoards(faction.id);
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------//
|
|
|
|
// Coord count
|
|
|
|
//----------------------------------------------//
|
|
|
|
|
|
|
|
public int getFactionCoordCount(int factionId) {
|
2011-02-06 13:36:11 +01:00
|
|
|
int ret = 0;
|
|
|
|
for (int thatFactionId : coordFactionIds.values()) {
|
|
|
|
if(thatFactionId == factionId) {
|
|
|
|
ret += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
2011-02-13 11:18:08 +01:00
|
|
|
public int getFactionCoordCount(Faction faction) {
|
|
|
|
return getFactionCoordCount(faction.id);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static int getFactionCoordCountAllBoards(int factionId) {
|
|
|
|
int ret = 0;
|
|
|
|
for (Board board : getAll()) {
|
|
|
|
ret += board.getFactionCoordCount(factionId);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
public static int getFactionCoordCountAllBoards(Faction faction) {
|
|
|
|
return getFactionCoordCountAllBoards(faction.id);
|
|
|
|
}
|
|
|
|
|
2011-02-06 13:36:11 +01:00
|
|
|
|
|
|
|
//----------------------------------------------//
|
|
|
|
// Map generation
|
|
|
|
//----------------------------------------------//
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The map is relative to a coord and a faction
|
|
|
|
* north is in the direction of decreasing x
|
|
|
|
* east is in the direction of decreasing z
|
|
|
|
*/
|
2011-02-13 11:18:08 +01:00
|
|
|
public ArrayList<String> getMap(Faction faction, Coord coord, double inDegrees) {
|
2011-02-06 13:36:11 +01:00
|
|
|
ArrayList<String> ret = new ArrayList<String>();
|
2011-02-13 11:18:08 +01:00
|
|
|
ret.add(TextUtil.titleize("("+coord+") "+this.getFactionAt(coord).getTag(faction)));
|
2011-02-06 13:36:11 +01:00
|
|
|
|
|
|
|
int halfWidth = Conf.mapWidth / 2;
|
|
|
|
int halfHeight = Conf.mapHeight / 2;
|
|
|
|
Coord topLeft = coord.getRelative(-halfHeight, halfWidth);
|
|
|
|
int width = halfWidth * 2 + 1;
|
|
|
|
int height = halfHeight * 2 + 1;
|
|
|
|
|
|
|
|
// For each row
|
|
|
|
for (int dx = 0; dx < height; dx++) {
|
|
|
|
// Draw and add that row
|
|
|
|
String row = "";
|
|
|
|
for (int dz = 0; dz > -width; dz--) {
|
|
|
|
if(dz == -(halfWidth) && dx == halfHeight) {
|
|
|
|
row += ChatColor.AQUA+"+";
|
|
|
|
} else {
|
|
|
|
Coord coordHere = topLeft.getRelative(dx, dz);
|
2011-02-13 11:18:08 +01:00
|
|
|
Faction factionHere = this.getFactionAt(coordHere);
|
2011-02-06 13:36:11 +01:00
|
|
|
if (factionHere.id == 0) {
|
|
|
|
row += ChatColor.GRAY+"-";
|
|
|
|
} else {
|
|
|
|
row += factionHere.getRelation(faction).getColor()+"+";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ret.add(row);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the compass
|
|
|
|
ArrayList<String> asciiCompass = AsciiCompass.getAsciiCompass(inDegrees, ChatColor.RED, Conf.colorChrome);
|
2011-02-07 21:42:14 +01:00
|
|
|
|
2011-02-06 13:36:11 +01:00
|
|
|
// Add the compass
|
|
|
|
ret.set(1, asciiCompass.get(0)+ret.get(1).substring(3*3));
|
|
|
|
ret.set(2, asciiCompass.get(1)+ret.get(2).substring(3*3));
|
|
|
|
ret.set(3, asciiCompass.get(2)+ret.get(3).substring(3*3));
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//----------------------------------------------//
|
|
|
|
// Persistance
|
|
|
|
//----------------------------------------------//
|
|
|
|
|
2011-02-13 11:18:08 +01:00
|
|
|
public boolean save() {
|
|
|
|
return EM.boardSave(this.id);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static Board get(World world) {
|
|
|
|
return EM.boardGet(world);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static Collection<Board> getAll() {
|
|
|
|
return EM.boardGetAll();
|
2011-02-06 13:36:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|