From ff542a9946807fa92e1aa782f67612ebdbc1153c Mon Sep 17 00:00:00 2001 From: Anuken Date: Sat, 16 Jun 2018 11:35:32 -0400 Subject: [PATCH] Improved efficiency of areEnemies() function --- .../mindustry/entities/bullet/Bullet.java | 9 ++++++++ .../src/io/anuke/mindustry/game/TeamInfo.java | 10 +++++--- .../world/blocks/defense/DeflectorWall.java | 23 ++++++++++++++++--- 3 files changed, 36 insertions(+), 6 deletions(-) diff --git a/core/src/io/anuke/mindustry/entities/bullet/Bullet.java b/core/src/io/anuke/mindustry/entities/bullet/Bullet.java index 56be16936f..69ec44dcaa 100644 --- a/core/src/io/anuke/mindustry/entities/bullet/Bullet.java +++ b/core/src/io/anuke/mindustry/entities/bullet/Bullet.java @@ -81,6 +81,15 @@ public class Bullet extends BulletEntity implements TeamTrait, SyncT supressCollision = true; } + public void resetOwner(Entity entity, Team team){ + this.owner = entity; + this.team = team; + } + + public void scaleTime(float add){ + time += add; + } + @Override public int getTypeID() { return typeID; diff --git a/core/src/io/anuke/mindustry/game/TeamInfo.java b/core/src/io/anuke/mindustry/game/TeamInfo.java index f09f75a707..241b65ab37 100644 --- a/core/src/io/anuke/mindustry/game/TeamInfo.java +++ b/core/src/io/anuke/mindustry/game/TeamInfo.java @@ -15,6 +15,8 @@ public class TeamInfo { enemyData = new ThreadSet<>(); private ThreadSet allTeamData = new ThreadSet<>(); private ThreadSet allTeams = new ThreadSet<>(); + private int allyBits = 0; + private int enemyBits = 0; /**Returns all teams on a side.*/ public ObjectSet getTeams(boolean ally) { @@ -38,9 +40,11 @@ public class TeamInfo { if(ally) { allies.add(team); allyData.add(data); + allyBits |= (1 << team.ordinal()); }else { enemies.add(team); enemyData.add(data); + enemyBits |= (1 << team.ordinal()); } allTeamData.add(data); @@ -88,9 +92,9 @@ public class TeamInfo { /**Returns whether or not these two teams are enemies.*/ public boolean areEnemies(Team team, Team other){ if(team == other) return false; //fast fail to be more efficient - boolean ally = allies.contains(team); - boolean ally2 = enemies.contains(other); - return (ally == ally2) || !allTeams.contains(team); //if it's not in the game, target everything. + boolean ally = (allyBits & (1 << team.ordinal())) != 0; + boolean ally2 = (enemyBits & (1 << other.ordinal())) != 0; + return (ally == ally2) || !ally; //if it's not in the game, target everything. } public class TeamData { diff --git a/core/src/io/anuke/mindustry/world/blocks/defense/DeflectorWall.java b/core/src/io/anuke/mindustry/world/blocks/defense/DeflectorWall.java index 1223e44354..67db17e2af 100644 --- a/core/src/io/anuke/mindustry/world/blocks/defense/DeflectorWall.java +++ b/core/src/io/anuke/mindustry/world/blocks/defense/DeflectorWall.java @@ -1,21 +1,28 @@ package io.anuke.mindustry.world.blocks.defense; 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.bullet.Bullet; import io.anuke.mindustry.entities.bullet.BulletType; +import io.anuke.mindustry.game.Team; import io.anuke.mindustry.world.Tile; import io.anuke.mindustry.world.blocks.Wall; import io.anuke.ucore.core.Graphics; import io.anuke.ucore.core.Timers; import io.anuke.ucore.graphics.Draw; import io.anuke.ucore.util.Mathf; +import io.anuke.ucore.util.Physics; import static io.anuke.mindustry.Vars.tilesize; public class DeflectorWall extends Wall { static final float hitTime = 10f; + protected float maxDamageDeflect = 5f; + protected Rectangle rect = new Rectangle(); + public DeflectorWall(String name) { super(name); update = false; @@ -45,17 +52,27 @@ public class DeflectorWall extends Wall { public void handleBulletHit(TileEntity entity, Bullet 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); - if(penX < tilesize/2f * size) { - bullet.getVelocity().x *= -1; + Vector2 position = Physics.raycastRect(bullet.lastPosition().x, bullet.lastPosition().y, bullet.x, bullet.y, + 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.updateVelocity(BulletType.getByID(bullet.getTypeID()).drag); + bullet.resetOwner(entity, Team.none); + bullet.scaleTime(1f); bullet.supressCollision(); ((DeflectorEntity)entity).hit = 1f;