Made team a separate class

This commit is contained in:
Anuken
2019-12-25 19:07:04 -05:00
parent 8ac0949ddf
commit 9016c12d16
26 changed files with 86 additions and 85 deletions

View File

@@ -242,7 +242,7 @@ public class Vars implements Loadable{
unitGroups = new EntityGroup[Team.all.length];
for(Team team : Team.all){
unitGroups[team.ordinal()] = entities.add(BaseUnit.class).enableMapping();
unitGroups[(int) team.id] = entities.add(BaseUnit.class).enableMapping();
}
for(EntityGroup<?> group : entities.all()){

View File

@@ -103,7 +103,7 @@ public class BlockIndexer{
}
private ObjectSet<Tile>[] getFlagged(Team team){
return flagMap[team.ordinal()];
return flagMap[(int) team.id];
}
/** @return whether this item is present on this map.*/
@@ -115,11 +115,11 @@ public class BlockIndexer{
public ObjectSet<Tile> getDamaged(Team team){
returnArray.clear();
if(damagedTiles[team.ordinal()] == null){
damagedTiles[team.ordinal()] = new ObjectSet<>();
if(damagedTiles[(int) team.id] == null){
damagedTiles[(int) team.id] = new ObjectSet<>();
}
ObjectSet<Tile> set = damagedTiles[team.ordinal()];
ObjectSet<Tile> set = damagedTiles[(int) team.id];
for(Tile tile : set){
if((tile.entity == null || tile.entity.getTeam() != team || !tile.entity.damaged()) || tile.block() instanceof BuildBlock){
returnArray.add(tile);
@@ -135,7 +135,7 @@ public class BlockIndexer{
/** Get all allied blocks with a flag. */
public ObjectSet<Tile> getAllied(Team team, BlockFlag type){
return flagMap[team.ordinal()][type.ordinal()];
return flagMap[(int) team.id][type.ordinal()];
}
/** Get all enemy blocks with a flag. */
@@ -155,11 +155,11 @@ public class BlockIndexer{
}
public void notifyTileDamaged(TileEntity entity){
if(damagedTiles[entity.getTeam().ordinal()] == null){
damagedTiles[entity.getTeam().ordinal()] = new ObjectSet<>();
if(damagedTiles[(int) entity.getTeam().id] == null){
damagedTiles[(int) entity.getTeam().id] = new ObjectSet<>();
}
ObjectSet<Tile> set = damagedTiles[entity.getTeam().ordinal()];
ObjectSet<Tile> set = damagedTiles[(int) entity.getTeam().id];
set.add(entity.tile);
}
@@ -287,11 +287,11 @@ public class BlockIndexer{
//fast-set this quadrant to 'occupied' if the tile just placed is already of this team
if(tile.getTeam() == data.team && tile.entity != null && tile.block().targetable){
structQuadrants[data.team.ordinal()].set(quadrantX, quadrantY);
structQuadrants[(int) data.team.id].set(quadrantX, quadrantY);
continue; //no need to process futher
}
structQuadrants[data.team.ordinal()].set(quadrantX, quadrantY, false);
structQuadrants[(int) data.team.id].set(quadrantX, quadrantY, false);
outer:
for(int x = quadrantX * quadrantSize; x < world.width() && x < (quadrantX + 1) * quadrantSize; x++){
@@ -299,7 +299,7 @@ public class BlockIndexer{
Tile result = world.ltile(x, y);
//when a targetable block is found, mark this quadrant as occupied and stop searching
if(result.entity != null && result.getTeam() == data.team){
structQuadrants[data.team.ordinal()].set(quadrantX, quadrantY);
structQuadrants[(int) data.team.id].set(quadrantX, quadrantY);
break outer;
}
}
@@ -308,7 +308,7 @@ public class BlockIndexer{
}
private boolean getQuad(Team team, int quadrantX, int quadrantY){
return structQuadrants[team.ordinal()].get(quadrantX, quadrantY);
return structQuadrants[(int) team.id].get(quadrantX, quadrantY);
}
private int quadWidth(){

View File

@@ -84,8 +84,8 @@ public class Pathfinder implements Runnable{
}
public int debugValue(Team team, int x, int y){
if(pathMap[team.ordinal()][PathTarget.enemyCores.ordinal()] == null) return 0;
return pathMap[team.ordinal()][PathTarget.enemyCores.ordinal()].weights[x][y];
if(pathMap[(int) team.id][PathTarget.enemyCores.ordinal()] == null) return 0;
return pathMap[(int) team.id][PathTarget.enemyCores.ordinal()].weights[x][y];
}
/** Update a tile in the internal pathfinding grid. Causes a complete pathfinding reclaculation. */
@@ -149,12 +149,12 @@ public class Pathfinder implements Runnable{
public Tile getTargetTile(Tile tile, Team team, PathTarget target){
if(tile == null) return null;
PathData data = pathMap[team.ordinal()][target.ordinal()];
PathData data = pathMap[(int) team.id][target.ordinal()];
if(data == null){
//if this combination is not found, create it on request
if(!created.get(team.ordinal(), target.ordinal())){
created.set(team.ordinal(), target.ordinal());
if(!created.get((int) team.id, target.ordinal())){
created.set((int) team.id, target.ordinal());
//grab targets since this is run on main thread
IntArray targets = target.getTargets(team, new IntArray());
queue.post(() -> createPath(team, target, targets));
@@ -188,7 +188,7 @@ public class Pathfinder implements Runnable{
/** @return whether a tile can be passed through by this team. Pathfinding thread only.*/
private boolean passable(int x, int y, Team team){
int tile = tiles[x][y];
return PathTile.passable(tile) || (PathTile.team(tile) != team.ordinal() && PathTile.team(tile) != Team.derelict.ordinal());
return PathTile.passable(tile) || (PathTile.team(tile) != (int) team.id && PathTile.team(tile) != (int) Team.derelict.id);
}
/**
@@ -238,7 +238,7 @@ public class Pathfinder implements Runnable{
PathData path = new PathData(team, target, world.width(), world.height());
list.add(path);
pathMap[team.ordinal()][target.ordinal()] = path;
pathMap[(int) team.id][target.ordinal()] = path;
//grab targets from passed array
synchronized(path.targets){

View File

@@ -27,11 +27,11 @@ public class GameState{
private State state = State.menu;
public int enemies(){
return net.client() ? enemies : unitGroups[waveTeam.ordinal()].count(b -> !(b instanceof BaseDrone));
return net.client() ? enemies : unitGroups[(int) waveTeam.id].count(b -> !(b instanceof BaseDrone));
}
public BaseUnit boss(){
return unitGroups[waveTeam.ordinal()].find(BaseUnit::isBoss);
return unitGroups[(int) waveTeam.id].find(BaseUnit::isBoss);
}
public void set(State astate){

View File

@@ -217,7 +217,7 @@ public class Logic implements ApplicationListener{
Time.update();
if(state.rules.waves && state.rules.waveTimer && !state.gameOver){
if(!state.rules.waitForWaveToEnd || unitGroups[waveTeam.ordinal()].size() == 0){
if(!state.rules.waitForWaveToEnd || unitGroups[(int) waveTeam.id].size() == 0){
state.wavetime = Math.max(state.wavetime - Time.delta(), 0);
}
}

View File

@@ -376,17 +376,17 @@ public class Renderer implements ApplicationListener{
private void drawAllTeams(boolean flying){
for(Team team : Team.all){
EntityGroup<BaseUnit> group = unitGroups[team.ordinal()];
EntityGroup<BaseUnit> group = unitGroups[(int) team.id];
if(group.count(p -> p.isFlying() == flying) + playerGroup.count(p -> p.isFlying() == flying && p.getTeam() == team) == 0 && flying) continue;
unitGroups[team.ordinal()].draw(u -> u.isFlying() == flying && !u.isDead(), Unit::drawUnder);
unitGroups[(int) team.id].draw(u -> u.isFlying() == flying && !u.isDead(), Unit::drawUnder);
playerGroup.draw(p -> p.isFlying() == flying && p.getTeam() == team && !p.isDead(), Unit::drawUnder);
unitGroups[team.ordinal()].draw(u -> u.isFlying() == flying && !u.isDead(), Unit::drawAll);
unitGroups[(int) team.id].draw(u -> u.isFlying() == flying && !u.isDead(), Unit::drawAll);
playerGroup.draw(p -> p.isFlying() == flying && p.getTeam() == team, Unit::drawAll);
unitGroups[team.ordinal()].draw(u -> u.isFlying() == flying && !u.isDead(), Unit::drawOver);
unitGroups[(int) team.id].draw(u -> u.isFlying() == flying && !u.isDead(), Unit::drawOver);
playerGroup.draw(p -> p.isFlying() == flying && p.getTeam() == team, Unit::drawOver);
}
}

View File

@@ -74,7 +74,7 @@ public class EditorTile extends Tile{
return;
}
if(getTeamID() == team.ordinal()) return;
if(getTeamID() == (int) team.id) return;
op(OpType.team, getTeamID());
super.setTeam(team);
}

View File

@@ -141,7 +141,7 @@ public enum EditorTool{
if(tile.link().synthetic()){
Team dest = tile.getTeam();
if(dest == editor.drawTeam) return;
fill(editor, x, y, false, t -> t.getTeamID() == dest.ordinal() && t.link().synthetic(), t -> t.setTeam(editor.drawTeam));
fill(editor, x, y, false, t -> t.getTeamID() == (int) dest.id && t.link().synthetic(), t -> t.setTeam(editor.drawTeam));
}
}
}

View File

@@ -415,7 +415,7 @@ public class MapGenerateDialog extends FloatingDialog{
this.floor = floor.id;
this.block = wall.id;
this.ore = ore.id;
this.team = (byte)team.ordinal();
this.team = (byte) (int) team.id;
this.rotation = (byte)rotation;
}

View File

@@ -88,7 +88,7 @@ public class Damage{
tr.trns(angle, length);
Intc2 collider = (cx, cy) -> {
Tile tile = world.ltile(cx, cy);
if(tile != null && !collidedBlocks.contains(tile.pos()) && tile.entity != null && tile.getTeamID() != team.ordinal() && tile.entity.collide(hitter)){
if(tile != null && !collidedBlocks.contains(tile.pos()) && tile.entity != null && tile.getTeamID() != (int) team.id && tile.entity.collide(hitter)){
tile.entity.collision(hitter);
collidedBlocks.add(tile.pos());
hitter.getBulletType().hit(hitter, tile.worldx(), tile.worldy());

View File

@@ -157,7 +157,7 @@ public class Units{
/** Iterates over all units in a rectangle. */
public static void nearby(Team team, float x, float y, float width, float height, Cons<Unit> cons){
unitGroups[team.ordinal()].intersect(x, y, width, height, cons);
unitGroups[(int) team.id].intersect(x, y, width, height, cons);
playerGroup.intersect(x, y, width, height, player -> {
if(player.getTeam() == team){
cons.get(player);
@@ -167,7 +167,7 @@ public class Units{
/** Iterates over all units in a circle around this position. */
public static void nearby(Team team, float x, float y, float radius, Cons<Unit> cons){
unitGroups[team.ordinal()].intersect(x - radius, y - radius, radius*2f, radius*2f, unit -> {
unitGroups[(int) team.id].intersect(x - radius, y - radius, radius*2f, radius*2f, unit -> {
if(unit.withinDst(x, y, radius)){
cons.get(unit);
}
@@ -183,7 +183,7 @@ public class Units{
/** Iterates over all units in a rectangle. */
public static void nearby(float x, float y, float width, float height, Cons<Unit> cons){
for(Team team : Team.all){
unitGroups[team.ordinal()].intersect(x, y, width, height, cons);
unitGroups[(int) team.id].intersect(x, y, width, height, cons);
}
playerGroup.intersect(x, y, width, height, cons);
@@ -199,7 +199,7 @@ public class Units{
EnumSet<Team> targets = state.teams.enemiesOf(team);
for(Team other : targets){
unitGroups[other.ordinal()].intersect(x, y, width, height, cons);
unitGroups[(int) other.id].intersect(x, y, width, height, cons);
}
playerGroup.intersect(x, y, width, height, player -> {
@@ -217,7 +217,7 @@ public class Units{
/** Iterates over all units. */
public static void all(Cons<Unit> cons){
for(Team team : Team.all){
unitGroups[team.ordinal()].all().each(cons);
unitGroups[(int) team.id].all().each(cons);
}
playerGroup.all().each(cons);

View File

@@ -365,7 +365,7 @@ public abstract class BaseUnit extends Unit implements ShooterTrait{
@Override
public EntityGroup targetGroup(){
return unitGroups[team.ordinal()];
return unitGroups[(int) team.id];
}
@Override

View File

@@ -169,7 +169,7 @@ public abstract class Unit extends DestructibleEntity implements SaveTrait, Targ
public void writeSave(DataOutput stream, boolean net) throws IOException{
if(item.item == null) item.item = Items.copper;
stream.writeByte(team.ordinal());
stream.writeByte((int) team.id);
stream.writeBoolean(isDead());
stream.writeFloat(net ? interpolator.target.x : x);
stream.writeFloat(net ? interpolator.target.y : y);
@@ -220,7 +220,7 @@ public abstract class Unit extends DestructibleEntity implements SaveTrait, Targ
for(Team team : Team.all){
if(team != getTeam() || !(this instanceof Player)){
avoid(unitGroups[team.ordinal()].intersect(cx, cy, fsize, fsize));
avoid(unitGroups[(int) team.id].intersect(cx, cy, fsize, fsize));
}
}

View File

@@ -114,7 +114,7 @@ public class BuilderDrone extends BaseDrone implements BuilderTrait{
public BuilderDrone(){
if(reset.check()){
Events.on(BuildSelectEvent.class, event -> {
EntityGroup<BaseUnit> group = unitGroups[event.team.ordinal()];
EntityGroup<BaseUnit> group = unitGroups[(int) event.team.id];
if(!(event.tile.entity instanceof BuildEntity)) return;

View File

@@ -22,7 +22,7 @@ public enum Gamemode{
attack(rules -> {
rules.unitDrops = true;
rules.attackMode = true;
}, map -> map.teams.contains(waveTeam.ordinal())),
}, map -> map.teams.contains((int) waveTeam.id)),
pvp(rules -> {
rules.pvp = true;
rules.enemyCoreBuildRadius = 600f;

View File

@@ -1,27 +1,35 @@
package mindustry.game;
import arc.Core;
import arc.graphics.Color;
import arc.*;
import arc.graphics.*;
import arc.struct.*;
import mindustry.graphics.*;
public enum Team{
derelict(Color.valueOf("4d4e58")),
sharded(Pal.accent),
crux(Color.valueOf("e82d2d")),
green(Color.valueOf("4dd98b")),
purple(Color.valueOf("9a4bdf")),
blue(Color.royal.cpy());
public class Team{
/** All registered teams. */
public final static Array<Team> all = new Array<>();
public final static Team
derelict = new Team("derelict", Color.valueOf("4d4e58")),
sharded = new Team("sharded", Pal.accent.cpy()),
crux = new Team("crux", Color.valueOf("e82d2d")),
green = new Team("green", Color.valueOf("4dd98b")),
purple = new Team("purple", Color.valueOf("9a4bdf")),
blue = new Team("blue", Color.royal.cpy());
public final static Team[] all = values();
public final Color color;
public final int intColor;
public final String name;
public final int id;
Team(Color color){
public Team(String name, Color color){
this.name = name;
this.color = color;
intColor = Color.rgba8888(color);
this.intColor = Color.rgba8888(color);
this.id = all.size;
all.add(this);
}
public String localized(){
return Core.bundle.get("team." + name() + ".name");
return Core.bundle.get("team." + name + ".name");
}
}

View File

@@ -6,23 +6,22 @@ import mindustry.world.*;
/** Class for various team-based utilities. */
public class Teams{
private TeamData[] map = new TeamData[Team.all.length];
private TeamData[] map = new TeamData[256];
/**
* Register a team.
* @param team The team type enum.
* @param enemies The array of enemies of this team. Any team not in this array is considered neutral.
*/
public void add(Team team, Team... enemies){
map[team.ordinal()] = new TeamData(team, EnumSet.of(enemies));
public void add(Team team){
map[team.id] = new TeamData(team);
}
/** Returns team data by type. */
public TeamData get(Team team){
if(map[team.ordinal()] == null){
add(team, Array.with(Team.all).select(t -> t != team).toArray(Team.class));
if(map[team.id] == null){
add(team);
}
return map[team.ordinal()];
return map[team.id];
}
/** Returns whether a team is active, e.g. whether it has any cores remaining. */
@@ -31,14 +30,10 @@ public class Teams{
return team == Vars.waveTeam || get(team).cores.size > 0;
}
/** Returns a set of all teams that are enemies of this team. */
public EnumSet<Team> enemiesOf(Team team){
return get(team).enemies;
}
/** Returns whether {@param other} is an enemy of {@param #team}. */
public boolean areEnemies(Team team, Team other){
return enemiesOf(team).contains(other);
//todo what about derelict?
return team != other;
}
/** Allocates a new array with the active teams.
@@ -49,13 +44,11 @@ public class Teams{
public static class TeamData{
public final ObjectSet<Tile> cores = new ObjectSet<>();
public final EnumSet<Team> enemies;
public final Team team;
public Queue<BrokenBlock> brokenBlocks = new Queue<>();
public TeamData(Team team, EnumSet<Team> enemies){
public TeamData(Team team){
this.team = team;
this.enemies = enemies;
}
}

View File

@@ -91,7 +91,7 @@ public class MapIO{
public void setTeam(Team team){
super.setTeam(team);
if(block instanceof CoreBlock){
map.teams.add(team.ordinal());
map.teams.add((int) team.id);
}
}
};

View File

@@ -217,7 +217,7 @@ public abstract class SaveVersion extends SaveFileReader{
Array<TeamData> data = state.teams.getActive();
stream.writeInt(data.size);
for(TeamData team : data){
stream.writeInt(team.team.ordinal());
stream.writeInt((int) team.team.id);
stream.writeInt(team.brokenBlocks.size);
for(BrokenBlock block : team.brokenBlocks){
stream.writeShort(block.x);

View File

@@ -87,7 +87,7 @@ public class TypeIO{
@WriteClass(BaseUnit.class)
public static void writeBaseUnit(ByteBuffer buffer, BaseUnit unit){
buffer.put((byte)unit.getTeam().ordinal());
buffer.put((byte) (int) unit.getTeam().id);
buffer.putInt(unit.getID());
}
@@ -194,7 +194,7 @@ public class TypeIO{
@WriteClass(Team.class)
public static void writeTeam(ByteBuffer buffer, Team reason){
buffer.put((byte)reason.ordinal());
buffer.put((byte) (int) reason.id);
}
@ReadClass(Team.class)

View File

@@ -628,7 +628,7 @@ public class HudFragment extends Fragment{
}
if(state.rules.waveTimer){
builder.append((state.rules.waitForWaveToEnd && unitGroups[waveTeam.ordinal()].size() > 0) ? Core.bundle.get("wave.waveInProgress") : ( waitingf.get((int)(state.wavetime/60))));
builder.append((state.rules.waitForWaveToEnd && unitGroups[(int) waveTeam.id].size() > 0) ? Core.bundle.get("wave.waveInProgress") : ( waitingf.get((int)(state.wavetime/60))));
}else if(state.enemies() == 0){
builder.append(Core.bundle.get("waiting"));
}

View File

@@ -146,7 +146,7 @@ public class Tile implements Position, TargetTrait{
}
public void setTeam(Team team){
this.team = (byte)team.ordinal();
this.team = (byte) (int) team.id;
}
public byte getTeamID(){
@@ -156,7 +156,7 @@ public class Tile implements Position, TargetTrait{
public void setBlock(@NonNull Block type, Team team, int rotation){
preChanged();
this.block = type;
this.team = (byte)team.ordinal();
this.team = (byte) (int) team.id;
this.rotation = (byte)Mathf.mod(rotation, 4);
changed();
}

View File

@@ -58,7 +58,7 @@ public class CommandCenter extends Block{
ObjectSet<Tile> set = indexer.getAllied(tile.getTeam(), BlockFlag.comandCenter);
if(set.size == 1){
for(BaseUnit unit : unitGroups[tile.getTeam().ordinal()].all()){
for(BaseUnit unit : unitGroups[(int) tile.getTeam().id].all()){
unit.onCommand(UnitCommand.all[0]);
}
}
@@ -116,7 +116,7 @@ public class CommandCenter extends Block{
Team team = (player == null ? tile.getTeam() : player.getTeam());
for(BaseUnit unit : unitGroups[team.ordinal()].all()){
for(BaseUnit unit : unitGroups[(int) team.id].all()){
unit.onCommand(command);
}