Improved efficiency of areEnemies() function

This commit is contained in:
Anuken
2018-06-16 11:35:32 -04:00
parent 6281826b92
commit ff542a9946
3 changed files with 36 additions and 6 deletions

View File

@@ -81,6 +81,15 @@ public class Bullet extends BulletEntity<BulletType> implements TeamTrait, SyncT
supressCollision = true; supressCollision = true;
} }
public void resetOwner(Entity entity, Team team){
this.owner = entity;
this.team = team;
}
public void scaleTime(float add){
time += add;
}
@Override @Override
public int getTypeID() { public int getTypeID() {
return typeID; return typeID;

View File

@@ -15,6 +15,8 @@ public class TeamInfo {
enemyData = new ThreadSet<>(); enemyData = new ThreadSet<>();
private ThreadSet<TeamData> allTeamData = new ThreadSet<>(); private ThreadSet<TeamData> allTeamData = new ThreadSet<>();
private ThreadSet<Team> allTeams = new ThreadSet<>(); private ThreadSet<Team> allTeams = new ThreadSet<>();
private int allyBits = 0;
private int enemyBits = 0;
/**Returns all teams on a side.*/ /**Returns all teams on a side.*/
public ObjectSet<TeamData> getTeams(boolean ally) { public ObjectSet<TeamData> getTeams(boolean ally) {
@@ -38,9 +40,11 @@ public class TeamInfo {
if(ally) { if(ally) {
allies.add(team); allies.add(team);
allyData.add(data); allyData.add(data);
allyBits |= (1 << team.ordinal());
}else { }else {
enemies.add(team); enemies.add(team);
enemyData.add(data); enemyData.add(data);
enemyBits |= (1 << team.ordinal());
} }
allTeamData.add(data); allTeamData.add(data);
@@ -88,9 +92,9 @@ public class TeamInfo {
/**Returns whether or not these two teams are enemies.*/ /**Returns whether or not these two teams are enemies.*/
public boolean areEnemies(Team team, Team other){ public boolean areEnemies(Team team, Team other){
if(team == other) return false; //fast fail to be more efficient if(team == other) return false; //fast fail to be more efficient
boolean ally = allies.contains(team); boolean ally = (allyBits & (1 << team.ordinal())) != 0;
boolean ally2 = enemies.contains(other); boolean ally2 = (enemyBits & (1 << other.ordinal())) != 0;
return (ally == ally2) || !allTeams.contains(team); //if it's not in the game, target everything. return (ally == ally2) || !ally; //if it's not in the game, target everything.
} }
public class TeamData { public class TeamData {

View File

@@ -1,21 +1,28 @@
package io.anuke.mindustry.world.blocks.defense; package io.anuke.mindustry.world.blocks.defense;
import com.badlogic.gdx.graphics.Color; import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;
import io.anuke.mindustry.entities.TileEntity; import io.anuke.mindustry.entities.TileEntity;
import io.anuke.mindustry.entities.bullet.Bullet; import io.anuke.mindustry.entities.bullet.Bullet;
import io.anuke.mindustry.entities.bullet.BulletType; import io.anuke.mindustry.entities.bullet.BulletType;
import io.anuke.mindustry.game.Team;
import io.anuke.mindustry.world.Tile; import io.anuke.mindustry.world.Tile;
import io.anuke.mindustry.world.blocks.Wall; import io.anuke.mindustry.world.blocks.Wall;
import io.anuke.ucore.core.Graphics; import io.anuke.ucore.core.Graphics;
import io.anuke.ucore.core.Timers; import io.anuke.ucore.core.Timers;
import io.anuke.ucore.graphics.Draw; import io.anuke.ucore.graphics.Draw;
import io.anuke.ucore.util.Mathf; import io.anuke.ucore.util.Mathf;
import io.anuke.ucore.util.Physics;
import static io.anuke.mindustry.Vars.tilesize; import static io.anuke.mindustry.Vars.tilesize;
public class DeflectorWall extends Wall { public class DeflectorWall extends Wall {
static final float hitTime = 10f; static final float hitTime = 10f;
protected float maxDamageDeflect = 5f;
protected Rectangle rect = new Rectangle();
public DeflectorWall(String name) { public DeflectorWall(String name) {
super(name); super(name);
update = false; update = false;
@@ -45,17 +52,27 @@ public class DeflectorWall extends Wall {
public void handleBulletHit(TileEntity entity, Bullet bullet){ public void handleBulletHit(TileEntity entity, Bullet bullet){
super.handleBulletHit(entity, bullet); super.handleBulletHit(entity, bullet);
//doesn't reflect powerful bullets
if(bullet.getDamage() > maxDamageDeflect) return;
float penX = Math.abs(entity.x - bullet.x), penY = Math.abs(entity.y - bullet.y); float penX = Math.abs(entity.x - bullet.x), penY = Math.abs(entity.y - bullet.y);
if(penX < tilesize/2f * size) { Vector2 position = Physics.raycastRect(bullet.lastPosition().x, bullet.lastPosition().y, bullet.x, bullet.y,
bullet.getVelocity().x *= -1; rect.setCenter(entity.x, entity.y).setSize(size * tilesize + bullet.hitbox.width + bullet.hitbox.height));
if(position != null){
bullet.set(position.x, position.y);
} }
if(penY < tilesize/2f * size){ if(penX > penY) {
bullet.getVelocity().x *= -1;
}else{
bullet.getVelocity().y *= -1; bullet.getVelocity().y *= -1;
} }
bullet.updateVelocity(BulletType.getByID(bullet.getTypeID()).drag); bullet.updateVelocity(BulletType.getByID(bullet.getTypeID()).drag);
bullet.resetOwner(entity, Team.none);
bullet.scaleTime(1f);
bullet.supressCollision(); bullet.supressCollision();
((DeflectorEntity)entity).hit = 1f; ((DeflectorEntity)entity).hit = 1f;