it is done

This commit is contained in:
Anuken
2019-12-25 01:39:38 -05:00
parent 5b21873f3c
commit 514d4817c8
488 changed files with 4572 additions and 4574 deletions

View File

@@ -0,0 +1,282 @@
package mindustry.entities;
import arc.*;
import mindustry.annotations.Annotations.*;
import arc.struct.*;
import arc.func.*;
import arc.graphics.*;
import arc.math.*;
import arc.math.geom.*;
import arc.util.*;
import mindustry.content.*;
import mindustry.entities.Effects.*;
import mindustry.entities.effect.*;
import mindustry.entities.type.*;
import mindustry.game.EventType.*;
import mindustry.game.*;
import mindustry.gen.*;
import mindustry.graphics.*;
import mindustry.world.*;
import static mindustry.Vars.*;
/** Utility class for damaging in an area. */
public class Damage{
private static Rectangle rect = new Rectangle();
private static Rectangle hitrect = new Rectangle();
private static Vector2 tr = new Vector2();
private static GridBits bits = new GridBits(30, 30);
private static IntQueue propagation = new IntQueue();
private static IntSet collidedBlocks = new IntSet();
/** Creates a dynamic explosion based on specified parameters. */
public static void dynamicExplosion(float x, float y, float flammability, float explosiveness, float power, float radius, Color color){
for(int i = 0; i < Mathf.clamp(power / 20, 0, 6); i++){
int branches = 5 + Mathf.clamp((int)(power / 30), 1, 20);
Time.run(i * 2f + Mathf.random(4f), () -> Lightning.create(Team.derelict, Pal.power, 3,
x, y, Mathf.random(360f), branches + Mathf.range(2)));
}
for(int i = 0; i < Mathf.clamp(flammability / 4, 0, 30); i++){
Time.run(i / 2f, () -> Call.createBullet(Bullets.fireball, Team.derelict, x, y, Mathf.random(360f), 1, 1));
}
int waves = Mathf.clamp((int)(explosiveness / 4), 0, 30);
for(int i = 0; i < waves; i++){
int f = i;
Time.run(i * 2f, () -> {
Damage.damage(x, y, Mathf.clamp(radius + explosiveness, 0, 50f) * ((f + 1f) / waves), explosiveness / 2f);
Effects.effect(Fx.blockExplosionSmoke, x + Mathf.range(radius), y + Mathf.range(radius));
});
}
if(explosiveness > 15f){
Effects.effect(Fx.shockwave, x, y);
}
if(explosiveness > 30f){
Effects.effect(Fx.bigShockwave, x, y);
}
float shake = Math.min(explosiveness / 4f + 3f, 9f);
Effects.shake(shake, shake, x, y);
Effects.effect(Fx.dynamicExplosion, x, y, radius / 8f);
}
public static void createIncend(float x, float y, float range, int amount){
for(int i = 0; i < amount; i++){
float cx = x + Mathf.range(range);
float cy = y + Mathf.range(range);
Tile tile = world.tileWorld(cx, cy);
if(tile != null){
Fire.create(tile);
}
}
}
public static void collideLine(Bullet hitter, Team team, Effect effect, float x, float y, float angle, float length){
collideLine(hitter, team, effect, x, y, angle, length, false);
}
/**
* Damages entities in a line.
* Only enemies of the specified team are damaged.
*/
public static void collideLine(Bullet hitter, Team team, Effect effect, float x, float y, float angle, float length, boolean large){
collidedBlocks.clear();
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)){
tile.entity.collision(hitter);
collidedBlocks.add(tile.pos());
hitter.getBulletType().hit(hitter, tile.worldx(), tile.worldy());
}
};
world.raycastEachWorld(x, y, x + tr.x, y + tr.y, (cx, cy) -> {
collider.get(cx, cy);
if(large){
for(Point2 p : Geometry.d4){
collider.get(cx + p.x, cy + p.y);
}
}
return false;
});
rect.setPosition(x, y).setSize(tr.x, tr.y);
float x2 = tr.x + x, y2 = tr.y + y;
if(rect.width < 0){
rect.x += rect.width;
rect.width *= -1;
}
if(rect.height < 0){
rect.y += rect.height;
rect.height *= -1;
}
float expand = 3f;
rect.y -= expand;
rect.x -= expand;
rect.width += expand * 2;
rect.height += expand * 2;
Cons<Unit> cons = e -> {
e.hitbox(hitrect);
Rectangle other = hitrect;
other.y -= expand;
other.x -= expand;
other.width += expand * 2;
other.height += expand * 2;
Vector2 vec = Geometry.raycastRect(x, y, x2, y2, other);
if(vec != null){
Effects.effect(effect, vec.x, vec.y);
e.collision(hitter, vec.x, vec.y);
hitter.collision(e, vec.x, vec.y);
}
};
Units.nearbyEnemies(team, rect, cons);
}
/** Damages all entities and blocks in a radius that are enemies of the team. */
public static void damageUnits(Team team, float x, float y, float size, float damage, Boolf<Unit> predicate, Cons<Unit> acceptor){
Cons<Unit> cons = entity -> {
if(!predicate.get(entity)) return;
entity.hitbox(hitrect);
if(!hitrect.overlaps(rect)){
return;
}
entity.damage(damage);
acceptor.get(entity);
};
rect.setSize(size * 2).setCenter(x, y);
if(team != null){
Units.nearbyEnemies(team, rect, cons);
}else{
Units.nearby(rect, cons);
}
}
/** Damages everything in a radius. */
public static void damage(float x, float y, float radius, float damage){
damage(null, x, y, radius, damage, false);
}
/** Damages all entities and blocks in a radius that are enemies of the team. */
public static void damage(Team team, float x, float y, float radius, float damage){
damage(team, x, y, radius, damage, false);
}
/** Damages all entities and blocks in a radius that are enemies of the team. */
public static void damage(Team team, float x, float y, float radius, float damage, boolean complete){
Cons<Unit> cons = entity -> {
if(entity.getTeam() == team || entity.dst(x, y) > radius){
return;
}
float amount = calculateDamage(x, y, entity.x, entity.y, radius, damage);
entity.damage(amount);
//TODO better velocity displacement
float dst = tr.set(entity.x - x, entity.y - y).len();
entity.velocity().add(tr.setLength((1f - dst / radius) * 2f / entity.mass()));
if(complete && damage >= 9999999f && entity == player){
Events.fire(Trigger.exclusionDeath);
}
};
rect.setSize(radius * 2).setCenter(x, y);
if(team != null){
Units.nearbyEnemies(team, rect, cons);
}else{
Units.nearby(rect, cons);
}
if(!complete){
int trad = (int)(radius / tilesize);
Tile tile = world.tileWorld(x, y);
if(tile != null){
tileDamage(team, tile.x, tile.y, trad, damage);
}
}else{
completeDamage(team, x, y, radius, damage);
}
}
public static void tileDamage(Team team, int startx, int starty, int radius, float baseDamage){
bits.clear();
propagation.clear();
int bitOffset = bits.width() / 2;
propagation.addFirst(PropCell.get((byte)0, (byte)0, (short)baseDamage));
//clamp radius to fit bits
radius = Math.min(radius, bits.width() / 2);
while(!propagation.isEmpty()){
int prop = propagation.removeLast();
int x = PropCell.x(prop);
int y = PropCell.y(prop);
int damage = PropCell.damage(prop);
//manhattan distance used for calculating falloff, results in a diamond pattern
int dst = Math.abs(x) + Math.abs(y);
int scaledDamage = (int)(damage * (1f - (float)dst / radius));
bits.set(bitOffset + x, bitOffset + y);
Tile tile = world.ltile(startx + x, starty + y);
if(scaledDamage <= 0 || tile == null) continue;
//apply damage to entity if needed
if(tile.entity != null && tile.getTeam() != team){
int health = (int)tile.entity.health;
if(tile.entity.health > 0){
tile.entity.damage(scaledDamage);
scaledDamage -= health;
if(scaledDamage <= 0) continue;
}
}
for(Point2 p : Geometry.d4){
if(!bits.get(bitOffset + x + p.x, bitOffset + y + p.y)){
propagation.addFirst(PropCell.get((byte)(x + p.x), (byte)(y + p.y), (short)scaledDamage));
}
}
}
}
private static void completeDamage(Team team, float x, float y, float radius, float damage){
int trad = (int)(radius / tilesize);
for(int dx = -trad; dx <= trad; dx++){
for(int dy = -trad; dy <= trad; dy++){
Tile tile = world.tile(Math.round(x / tilesize) + dx, Math.round(y / tilesize) + dy);
if(tile != null && tile.entity != null && (team == null || state.teams.areEnemies(team, tile.getTeam())) && Mathf.dst(dx, dy) <= trad){
tile.entity.damage(damage);
}
}
}
}
private static float calculateDamage(float x, float y, float tx, float ty, float radius, float damage){
float dist = Mathf.dst(x, y, tx, ty);
float falloff = 0.4f;
float scaled = Mathf.lerp(1f - dist / radius, 1f, falloff);
return damage * scaled;
}
@Struct
class PropCellStruct{
byte x;
byte y;
short damage;
}
}

View File

@@ -0,0 +1,168 @@
package mindustry.entities;
import arc.Core;
import arc.struct.Array;
import arc.func.Cons;
import arc.graphics.Color;
import arc.graphics.g2d.*;
import arc.math.Mathf;
import arc.math.geom.Position;
import arc.util.pooling.Pools;
import mindustry.entities.type.EffectEntity;
import mindustry.entities.traits.ScaleTrait;
public class Effects{
private static final EffectContainer container = new EffectContainer();
private static Array<Effect> effects = new Array<>();
private static ScreenshakeProvider shakeProvider;
private static float shakeFalloff = 10000f;
private static EffectProvider provider = (effect, color, x, y, rotation, data) -> {
EffectEntity entity = Pools.obtain(EffectEntity.class, EffectEntity::new);
entity.effect = effect;
entity.color = color;
entity.rotation = rotation;
entity.data = data;
entity.set(x, y);
entity.add();
};
public static void setEffectProvider(EffectProvider prov){
provider = prov;
}
public static void setScreenShakeProvider(ScreenshakeProvider provider){
shakeProvider = provider;
}
public static void renderEffect(int id, Effect render, Color color, float life, float rotation, float x, float y, Object data){
container.set(id, color, life, render.lifetime, rotation, x, y, data);
render.draw.render(container);
Draw.reset();
}
public static Effect getEffect(int id){
if(id >= effects.size || id < 0)
throw new IllegalArgumentException("The effect with ID \"" + id + "\" does not exist!");
return effects.get(id);
}
public static Array<Effect> all(){
return effects;
}
public static void effect(Effect effect, float x, float y, float rotation){
provider.createEffect(effect, Color.white, x, y, rotation, null);
}
public static void effect(Effect effect, float x, float y){
effect(effect, x, y, 0);
}
public static void effect(Effect effect, Color color, float x, float y){
provider.createEffect(effect, color, x, y, 0f, null);
}
public static void effect(Effect effect, Position loc){
provider.createEffect(effect, Color.white, loc.getX(), loc.getY(), 0f, null);
}
public static void effect(Effect effect, Color color, float x, float y, float rotation){
provider.createEffect(effect, color, x, y, rotation, null);
}
public static void effect(Effect effect, Color color, float x, float y, float rotation, Object data){
provider.createEffect(effect, color, x, y, rotation, data);
}
public static void effect(Effect effect, float x, float y, float rotation, Object data){
provider.createEffect(effect, Color.white, x, y, rotation, data);
}
/** Default value is 1000. Higher numbers mean more powerful shake (less falloff). */
public static void setShakeFalloff(float falloff){
shakeFalloff = falloff;
}
private static void shake(float intensity, float duration){
if(shakeProvider == null) throw new RuntimeException("Screenshake provider is null! Set it first.");
shakeProvider.accept(intensity, duration);
}
public static void shake(float intensity, float duration, float x, float y){
if(Core.camera == null) return;
float distance = Core.camera.position.dst(x, y);
if(distance < 1) distance = 1;
shake(Mathf.clamp(1f / (distance * distance / shakeFalloff)) * intensity, duration);
}
public static void shake(float intensity, float duration, Position loc){
shake(intensity, duration, loc.getX(), loc.getY());
}
public interface ScreenshakeProvider{
void accept(float intensity, float duration);
}
public static class Effect{
private static int lastid = 0;
public final int id;
public final EffectRenderer draw;
public final float lifetime;
/** Clip size. */
public float size;
public Effect(float life, float clipsize, EffectRenderer draw){
this.id = lastid++;
this.lifetime = life;
this.draw = draw;
this.size = clipsize;
effects.add(this);
}
public Effect(float life, EffectRenderer draw){
this(life, 28f, draw);
}
}
public static class EffectContainer implements ScaleTrait{
public float x, y, time, lifetime, rotation;
public Color color;
public int id;
public Object data;
private EffectContainer innerContainer;
public void set(int id, Color color, float life, float lifetime, float rotation, float x, float y, Object data){
this.x = x;
this.y = y;
this.color = color;
this.time = life;
this.lifetime = lifetime;
this.id = id;
this.rotation = rotation;
this.data = data;
}
public void scaled(float lifetime, Cons<EffectContainer> cons){
if(innerContainer == null) innerContainer = new EffectContainer();
if(time <= lifetime){
innerContainer.set(id, color, time, lifetime, rotation, x, y, data);
cons.get(innerContainer);
}
}
@Override
public float fin(){
return time / lifetime;
}
}
public interface EffectProvider{
void createEffect(Effect effect, Color color, float x, float y, float rotation, Object data);
}
public interface EffectRenderer{
void render(EffectContainer effect);
}
}

View File

@@ -0,0 +1,33 @@
package mindustry.entities;
import arc.struct.*;
import mindustry.entities.traits.*;
/** Simple container for managing entity groups.*/
public class Entities{
private final Array<EntityGroup<?>> groupArray = new Array<>();
public void clear(){
for(EntityGroup group : groupArray){
group.clear();
}
}
public EntityGroup<?> get(int id){
return groupArray.get(id);
}
public Array<EntityGroup<?>> all(){
return groupArray;
}
public <T extends Entity> EntityGroup<T> add(Class<T> type){
return add(type, true);
}
public <T extends Entity> EntityGroup<T> add(Class<T> type, boolean useTree){
EntityGroup<T> group = new EntityGroup<>(groupArray.size, type, useTree);
groupArray.add(group);
return group;
}
}

View File

@@ -0,0 +1,236 @@
package mindustry.entities;
import arc.struct.Array;
import arc.math.Mathf;
import arc.math.geom.*;
import mindustry.entities.traits.Entity;
import mindustry.entities.traits.SolidTrait;
import mindustry.world.Tile;
import static mindustry.Vars.tilesize;
import static mindustry.Vars.world;
public class EntityCollisions{
//range for tile collision scanning
private static final int r = 1;
//move in 1-unit chunks
private static final float seg = 1f;
//tile collisions
private Rectangle tmp = new Rectangle();
private Vector2 vector = new Vector2();
private Vector2 l1 = new Vector2();
private Rectangle r1 = new Rectangle();
private Rectangle r2 = new Rectangle();
//entity collisions
private Array<SolidTrait> arrOut = new Array<>();
public void move(SolidTrait entity, float deltax, float deltay){
boolean movedx = false;
while(Math.abs(deltax) > 0 || !movedx){
movedx = true;
moveDelta(entity, Math.min(Math.abs(deltax), seg) * Mathf.sign(deltax), 0, true);
if(Math.abs(deltax) >= seg){
deltax -= seg * Mathf.sign(deltax);
}else{
deltax = 0f;
}
}
boolean movedy = false;
while(Math.abs(deltay) > 0 || !movedy){
movedy = true;
moveDelta(entity, 0, Math.min(Math.abs(deltay), seg) * Mathf.sign(deltay), false);
if(Math.abs(deltay) >= seg){
deltay -= seg * Mathf.sign(deltay);
}else{
deltay = 0f;
}
}
}
public void moveDelta(SolidTrait entity, float deltax, float deltay, boolean x){
Rectangle rect = r1;
entity.hitboxTile(rect);
entity.hitboxTile(r2);
rect.x += deltax;
rect.y += deltay;
int tilex = Math.round((rect.x + rect.width / 2) / tilesize), tiley = Math.round((rect.y + rect.height / 2) / tilesize);
for(int dx = -r; dx <= r; dx++){
for(int dy = -r; dy <= r; dy++){
int wx = dx + tilex, wy = dy + tiley;
if(solid(wx, wy) && entity.collidesGrid(wx, wy)){
tmp.setSize(tilesize).setCenter(wx * tilesize, wy * tilesize);
if(tmp.overlaps(rect)){
Vector2 v = Geometry.overlap(rect, tmp, x);
rect.x += v.x;
rect.y += v.y;
}
}
}
}
entity.setX(entity.getX() + rect.x - r2.x);
entity.setY(entity.getY() + rect.y - r2.y);
}
public boolean overlapsTile(Rectangle rect){
rect.getCenter(vector);
int r = 1;
//assumes tiles are centered
int tilex = Math.round(vector.x / tilesize);
int tiley = Math.round(vector.y / tilesize);
for(int dx = -r; dx <= r; dx++){
for(int dy = -r; dy <= r; dy++){
int wx = dx + tilex, wy = dy + tiley;
if(solid(wx, wy)){
r2.setSize(tilesize).setCenter(wx * tilesize, wy * tilesize);
if(r2.overlaps(rect)){
return true;
}
}
}
}
return false;
}
@SuppressWarnings("unchecked")
public <T extends Entity> void updatePhysics(EntityGroup<T> group){
QuadTree tree = group.tree();
tree.clear();
for(Entity entity : group.all()){
if(entity instanceof SolidTrait){
SolidTrait s = (SolidTrait)entity;
s.lastPosition().set(s.getX(), s.getY());
tree.insert(s);
}
}
}
private static boolean solid(int x, int y){
Tile tile = world.tile(x, y);
return tile != null && tile.solid();
}
private void checkCollide(Entity entity, Entity other){
SolidTrait a = (SolidTrait)entity;
SolidTrait b = (SolidTrait)other;
a.hitbox(this.r1);
b.hitbox(this.r2);
r1.x += (a.lastPosition().x - a.getX());
r1.y += (a.lastPosition().y - a.getY());
r2.x += (b.lastPosition().x - b.getX());
r2.y += (b.lastPosition().y - b.getY());
float vax = a.getX() - a.lastPosition().x;
float vay = a.getY() - a.lastPosition().y;
float vbx = b.getX() - b.lastPosition().x;
float vby = b.getY() - b.lastPosition().y;
if(a != b && a.collides(b) && b.collides(a)){
l1.set(a.getX(), a.getY());
boolean collide = r1.overlaps(r2) || collide(r1.x, r1.y, r1.width, r1.height, vax, vay,
r2.x, r2.y, r2.width, r2.height, vbx, vby, l1);
if(collide){
a.collision(b, l1.x, l1.y);
b.collision(a, l1.x, l1.y);
}
}
}
private boolean collide(float x1, float y1, float w1, float h1, float vx1, float vy1,
float x2, float y2, float w2, float h2, float vx2, float vy2, Vector2 out){
float px = vx1, py = vy1;
vx1 -= vx2;
vy1 -= vy2;
float xInvEntry, yInvEntry;
float xInvExit, yInvExit;
if(vx1 > 0.0f){
xInvEntry = x2 - (x1 + w1);
xInvExit = (x2 + w2) - x1;
}else{
xInvEntry = (x2 + w2) - x1;
xInvExit = x2 - (x1 + w1);
}
if(vy1 > 0.0f){
yInvEntry = y2 - (y1 + h1);
yInvExit = (y2 + h2) - y1;
}else{
yInvEntry = (y2 + h2) - y1;
yInvExit = y2 - (y1 + h1);
}
float xEntry, yEntry;
float xExit, yExit;
xEntry = xInvEntry / vx1;
xExit = xInvExit / vx1;
yEntry = yInvEntry / vy1;
yExit = yInvExit / vy1;
float entryTime = Math.max(xEntry, yEntry);
float exitTime = Math.min(xExit, yExit);
if(entryTime > exitTime || xExit < 0.0f || yExit < 0.0f || xEntry > 1.0f || yEntry > 1.0f){
return false;
}else{
float dx = x1 + w1 / 2f + px * entryTime;
float dy = y1 + h1 / 2f + py * entryTime;
out.set(dx, dy);
return true;
}
}
@SuppressWarnings("unchecked")
public void collideGroups(EntityGroup<?> groupa, EntityGroup<?> groupb){
for(Entity entity : groupa.all()){
if(!(entity instanceof SolidTrait))
continue;
SolidTrait solid = (SolidTrait)entity;
solid.hitbox(r1);
r1.x += (solid.lastPosition().x - solid.getX());
r1.y += (solid.lastPosition().y - solid.getY());
solid.hitbox(r2);
r2.merge(r1);
arrOut.clear();
groupb.tree().getIntersect(arrOut, r2);
for(SolidTrait sc : arrOut){
sc.hitbox(r1);
if(r2.overlaps(r1)){
checkCollide(entity, sc);
}
}
}
}
}

View File

@@ -0,0 +1,260 @@
package mindustry.entities;
import arc.*;
import arc.struct.*;
import arc.func.*;
import arc.graphics.*;
import arc.math.geom.*;
import mindustry.entities.traits.*;
import static mindustry.Vars.collisions;
/** Represents a group of a certain type of entity.*/
@SuppressWarnings("unchecked")
public class EntityGroup<T extends Entity>{
private final boolean useTree;
private final int id;
private final Class<T> type;
private final Array<T> entityArray = new Array<>(false, 32);
private final Array<T> entitiesToRemove = new Array<>(false, 32);
private final Array<T> entitiesToAdd = new Array<>(false, 32);
private final Array<T> intersectArray = new Array<>();
private final Rectangle intersectRect = new Rectangle();
private IntMap<T> map;
private QuadTree tree;
private Cons<T> removeListener;
private Cons<T> addListener;
private final Rectangle viewport = new Rectangle();
private int count = 0;
public EntityGroup(int id, Class<T> type, boolean useTree){
this.useTree = useTree;
this.id = id;
this.type = type;
if(useTree){
tree = new QuadTree<>(new Rectangle(0, 0, 0, 0));
}
}
public void update(){
updateEvents();
if(useTree()){
collisions.updatePhysics(this);
}
for(Entity e : all()){
e.update();
}
}
public int countInBounds(){
count = 0;
draw(e -> true, e -> count++);
return count;
}
public void draw(){
draw(e -> true);
}
public void draw(Boolf<T> toDraw){
draw(toDraw, t -> ((DrawTrait)t).draw());
}
public void draw(Boolf<T> toDraw, Cons<T> cons){
Camera cam = Core.camera;
viewport.set(cam.position.x - cam.width / 2, cam.position.y - cam.height / 2, cam.width, cam.height);
for(Entity e : all()){
if(!(e instanceof DrawTrait) || !toDraw.get((T)e) || !e.isAdded()) continue;
DrawTrait draw = (DrawTrait)e;
if(viewport.overlaps(draw.getX() - draw.drawSize()/2f, draw.getY() - draw.drawSize()/2f, draw.drawSize(), draw.drawSize())){
cons.get((T)e);
}
}
}
public boolean useTree(){
return useTree;
}
public void setRemoveListener(Cons<T> removeListener){
this.removeListener = removeListener;
}
public void setAddListener(Cons<T> addListener){
this.addListener = addListener;
}
public EntityGroup<T> enableMapping(){
map = new IntMap<>();
return this;
}
public boolean mappingEnabled(){
return map != null;
}
public Class<T> getType(){
return type;
}
public int getID(){
return id;
}
public void updateEvents(){
for(T e : entitiesToAdd){
if(e == null)
continue;
entityArray.add(e);
e.added();
if(map != null){
map.put(e.getID(), e);
}
}
entitiesToAdd.clear();
for(T e : entitiesToRemove){
entityArray.removeValue(e, true);
if(map != null){
map.remove(e.getID());
}
e.removed();
}
entitiesToRemove.clear();
}
public T getByID(int id){
if(map == null) throw new RuntimeException("Mapping is not enabled for group " + id + "!");
return map.get(id);
}
public void removeByID(int id){
if(map == null) throw new RuntimeException("Mapping is not enabled for group " + id + "!");
T t = map.get(id);
if(t != null){ //remove if present in map already
remove(t);
}else{ //maybe it's being queued?
for(T check : entitiesToAdd){
if(check.getID() == id){ //if it is indeed queued, remove it
entitiesToAdd.removeValue(check, true);
if(removeListener != null){
removeListener.get(check);
}
break;
}
}
}
}
@SuppressWarnings("unchecked")
public void intersect(float x, float y, float width, float height, Cons<? super T> out){
//don't waste time for empty groups
if(isEmpty()) return;
tree().getIntersect(out, x, y, width, height);
}
@SuppressWarnings("unchecked")
public Array<T> intersect(float x, float y, float width, float height){
intersectArray.clear();
//don't waste time for empty groups
if(isEmpty()) return intersectArray;
tree().getIntersect(intersectArray, intersectRect.set(x, y, width, height));
return intersectArray;
}
public QuadTree tree(){
if(!useTree) throw new RuntimeException("This group does not support quadtrees! Enable quadtrees when creating it.");
return tree;
}
/** Resizes the internal quadtree, if it is enabled.*/
public void resize(float x, float y, float w, float h){
if(useTree){
tree = new QuadTree<>(new Rectangle(x, y, w, h));
}
}
public boolean isEmpty(){
return entityArray.size == 0;
}
public int size(){
return entityArray.size;
}
public int count(Boolf<T> pred){
int count = 0;
for(int i = 0; i < entityArray.size; i++){
if(pred.get(entityArray.get(i))) count++;
}
return count;
}
public void add(T type){
if(type == null) throw new RuntimeException("Cannot add a null entity!");
if(type.getGroup() != null) return;
type.setGroup(this);
entitiesToAdd.add(type);
if(mappingEnabled()){
map.put(type.getID(), type);
}
if(addListener != null){
addListener.get(type);
}
}
public void remove(T type){
if(type == null) throw new RuntimeException("Cannot remove a null entity!");
type.setGroup(null);
entitiesToRemove.add(type);
if(removeListener != null){
removeListener.get(type);
}
}
public void clear(){
for(T entity : entityArray){
entity.removed();
entity.setGroup(null);
}
for(T entity : entitiesToAdd)
entity.setGroup(null);
for(T entity : entitiesToRemove)
entity.setGroup(null);
entitiesToAdd.clear();
entitiesToRemove.clear();
entityArray.clear();
if(map != null)
map.clear();
}
public T find(Boolf<T> pred){
for(int i = 0; i < entityArray.size; i++){
if(pred.get(entityArray.get(i))) return entityArray.get(i);
}
return null;
}
/** Returns the logic-only array for iteration. */
public Array<T> all(){
return entityArray;
}
}

View File

@@ -0,0 +1,79 @@
package mindustry.entities;
import arc.math.*;
import arc.math.geom.*;
import arc.util.*;
import mindustry.entities.traits.*;
/**
* Class for predicting shoot angles based on velocities of targets.
*/
public class Predict{
private static Vector2 vec = new Vector2();
private static Vector2 vresult = new Vector2();
/**
* Calculates of intercept of a stationary and moving target. Do not call from multiple threads!
* @param srcx X of shooter
* @param srcy Y of shooter
* @param dstx X of target
* @param dsty Y of target
* @param dstvx X velocity of target (subtract shooter X velocity if needed)
* @param dstvy Y velocity of target (subtract shooter Y velocity if needed)
* @param v speed of bullet
* @return the intercept location
*/
public static Vector2 intercept(float srcx, float srcy, float dstx, float dsty, float dstvx, float dstvy, float v){
dstvx /= Time.delta();
dstvy /= Time.delta();
float tx = dstx - srcx,
ty = dsty - srcy;
// Get quadratic equation components
float a = dstvx * dstvx + dstvy * dstvy - v * v;
float b = 2 * (dstvx * tx + dstvy * ty);
float c = tx * tx + ty * ty;
// Solve quadratic
Vector2 ts = quad(a, b, c);
// Find smallest positive solution
Vector2 sol = vresult.set(dstx, dsty);
if(ts != null){
float t0 = ts.x, t1 = ts.y;
float t = Math.min(t0, t1);
if(t < 0) t = Math.max(t0, t1);
if(t > 0){
sol.set(dstx + dstvx * t, dsty + dstvy * t);
}
}
return sol;
}
/**
* See {@link #intercept(float, float, float, float, float, float, float)}.
*/
public static Vector2 intercept(TargetTrait src, TargetTrait dst, float v){
return intercept(src.getX(), src.getY(), dst.getX(), dst.getY(), dst.getTargetVelocityX() - src.getTargetVelocityX()/2f, dst.getTargetVelocityY() - src.getTargetVelocityY()/2f, v);
}
private static Vector2 quad(float a, float b, float c){
Vector2 sol = null;
if(Math.abs(a) < 1e-6){
if(Math.abs(b) < 1e-6){
sol = Math.abs(c) < 1e-6 ? vec.set(0, 0) : null;
}else{
vec.set(-c / b, -c / b);
}
}else{
float disc = b * b - 4 * a * c;
if(disc >= 0){
disc = Mathf.sqrt(disc);
a = 2 * a;
sol = vec.set((-b - disc) / a, (-b + disc) / a);
}
}
return sol;
}
}

View File

@@ -0,0 +1,6 @@
package mindustry.entities;
public enum TargetPriority{
base,
turret
}

View File

@@ -0,0 +1,226 @@
package mindustry.entities;
import arc.struct.EnumSet;
import arc.func.Cons;
import arc.func.Boolf;
import arc.math.Mathf;
import arc.math.geom.Geometry;
import arc.math.geom.Rectangle;
import mindustry.entities.traits.TargetTrait;
import mindustry.entities.type.*;
import mindustry.game.Team;
import mindustry.world.Tile;
import static mindustry.Vars.*;
/** Utility class for unit and team interactions.*/
public class Units{
private static Rectangle hitrect = new Rectangle();
private static Unit result;
private static float cdist;
private static boolean boolResult;
/** @return whether this player can interact with a specific tile. if either of these are null, returns true.*/
public static boolean canInteract(Player player, Tile tile){
return player == null || tile == null || tile.interactable(player.getTeam());
}
/**
* Validates a target.
* @param target The target to validate
* @param team The team of the thing doing tha targeting
* @param x The X position of the thing doign the targeting
* @param y The Y position of the thing doign the targeting
* @param range The maximum distance from the target X/Y the targeter can be for it to be valid
* @return whether the target is invalid
*/
public static boolean invalidateTarget(TargetTrait target, Team team, float x, float y, float range){
return target == null || (range != Float.MAX_VALUE && !target.withinDst(x, y, range)) || target.getTeam() == team || !target.isValid();
}
/** See {@link #invalidateTarget(TargetTrait, Team, float, float, float)} */
public static boolean invalidateTarget(TargetTrait target, Team team, float x, float y){
return invalidateTarget(target, team, x, y, Float.MAX_VALUE);
}
/** See {@link #invalidateTarget(TargetTrait, Team, float, float, float)} */
public static boolean invalidateTarget(TargetTrait target, Unit targeter){
return invalidateTarget(target, targeter.getTeam(), targeter.x, targeter.y, targeter.getWeapon().bullet.range());
}
/** Returns whether there are any entities on this tile. */
public static boolean anyEntities(Tile tile){
float size = tile.block().size * tilesize;
return anyEntities(tile.drawx() - size/2f, tile.drawy() - size/2f, size, size);
}
public static boolean anyEntities(float x, float y, float width, float height){
boolResult = false;
nearby(x, y, width, height, unit -> {
if(boolResult) return;
if(!unit.isFlying()){
unit.hitbox(hitrect);
if(hitrect.overlaps(x, y, width, height)){
boolResult = true;
}
}
});
return boolResult;
}
/** Returns the neareset damaged tile. */
public static TileEntity findDamagedTile(Team team, float x, float y){
Tile tile = Geometry.findClosest(x, y, indexer.getDamaged(team));
return tile == null ? null : tile.entity;
}
/** Returns the neareset ally tile in a range. */
public static TileEntity findAllyTile(Team team, float x, float y, float range, Boolf<Tile> pred){
return indexer.findTile(team, x, y, range, pred);
}
/** Returns the neareset enemy tile in a range. */
public static TileEntity findEnemyTile(Team team, float x, float y, float range, Boolf<Tile> pred){
if(team == Team.derelict) return null;
for(Team enemy : state.teams.enemiesOf(team)){
TileEntity entity = indexer.findTile(enemy, x, y, range, pred, true);
if(entity != null){
return entity;
}
}
return null;
}
/** Returns the closest target enemy. First, units are checked, then tile entities. */
public static TargetTrait closestTarget(Team team, float x, float y, float range){
return closestTarget(team, x, y, range, Unit::isValid);
}
/** Returns the closest target enemy. First, units are checked, then tile entities. */
public static TargetTrait closestTarget(Team team, float x, float y, float range, Boolf<Unit> unitPred){
return closestTarget(team, x, y, range, unitPred, t -> true);
}
/** Returns the closest target enemy. First, units are checked, then tile entities. */
public static TargetTrait closestTarget(Team team, float x, float y, float range, Boolf<Unit> unitPred, Boolf<Tile> tilePred){
if(team == Team.derelict) return null;
Unit unit = closestEnemy(team, x, y, range, unitPred);
if(unit != null){
return unit;
}else{
return findEnemyTile(team, x, y, range, tilePred);
}
}
/** Returns the closest enemy of this team. Filter by predicate. */
public static Unit closestEnemy(Team team, float x, float y, float range, Boolf<Unit> predicate){
if(team == Team.derelict) return null;
result = null;
cdist = 0f;
nearbyEnemies(team, x - range, y - range, range*2f, range*2f, e -> {
if(e.isDead() || !predicate.get(e)) return;
float dst2 = Mathf.dst2(e.x, e.y, x, y);
if(dst2 < range*range && (result == null || dst2 < cdist)){
result = e;
cdist = dst2;
}
});
return result;
}
/** Returns the closest ally of this team. Filter by predicate. */
public static Unit closest(Team team, float x, float y, float range, Boolf<Unit> predicate){
result = null;
cdist = 0f;
nearby(team, x, y, range, e -> {
if(!predicate.get(e)) return;
float dist = Mathf.dst2(e.x, e.y, x, y);
if(result == null || dist < cdist){
result = e;
cdist = dist;
}
});
return result;
}
/** 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);
playerGroup.intersect(x, y, width, height, player -> {
if(player.getTeam() == team){
cons.get(player);
}
});
}
/** 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 -> {
if(unit.withinDst(x, y, radius)){
cons.get(unit);
}
});
playerGroup.intersect(x - radius, y - radius, radius*2f, radius*2f, unit -> {
if(unit.getTeam() == team && unit.withinDst(x, y, radius)){
cons.get(unit);
}
});
}
/** 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);
}
playerGroup.intersect(x, y, width, height, cons);
}
/** Iterates over all units in a rectangle. */
public static void nearby(Rectangle rect, Cons<Unit> cons){
nearby(rect.x, rect.y, rect.width, rect.height, cons);
}
/** Iterates over all units that are enemies of this team. */
public static void nearbyEnemies(Team team, float x, float y, float width, float height, Cons<Unit> cons){
EnumSet<Team> targets = state.teams.enemiesOf(team);
for(Team other : targets){
unitGroups[other.ordinal()].intersect(x, y, width, height, cons);
}
playerGroup.intersect(x, y, width, height, player -> {
if(targets.contains(player.getTeam())){
cons.get(player);
}
});
}
/** Iterates over all units that are enemies of this team. */
public static void nearbyEnemies(Team team, Rectangle rect, Cons<Unit> cons){
nearbyEnemies(team, rect.x, rect.y, rect.width, rect.height, cons);
}
/** Iterates over all units. */
public static void all(Cons<Unit> cons){
for(Team team : Team.all){
unitGroups[team.ordinal()].all().each(cons);
}
playerGroup.all().each(cons);
}
}

View File

@@ -0,0 +1,49 @@
package mindustry.entities.bullet;
import arc.graphics.g2d.*;
import mindustry.content.*;
import mindustry.entities.*;
import mindustry.entities.Effects.*;
import mindustry.entities.type.Bullet;
import mindustry.gen.*;
//TODO scale velocity depending on fslope()
public class ArtilleryBulletType extends BasicBulletType{
protected Effect trailEffect = Fx.artilleryTrail;
public ArtilleryBulletType(float speed, float damage, String bulletSprite){
super(speed, damage, bulletSprite);
collidesTiles = false;
collides = false;
collidesAir = false;
hitShake = 1f;
hitSound = Sounds.explosion;
}
public ArtilleryBulletType(){
this(1f, 1f, "shell");
}
@Override
public void update(mindustry.entities.type.Bullet b){
super.update(b);
if(b.timer.get(0, 3 + b.fslope() * 2f)){
Effects.effect(trailEffect, backColor, b.x, b.y, b.fslope() * 4f);
}
}
@Override
public void draw(Bullet b){
float baseScale = 0.7f;
float scale = (baseScale + b.fslope() * (1f - baseScale));
float height = bulletHeight * ((1f - bulletShrink) + bulletShrink * b.fout());
Draw.color(backColor);
Draw.rect(backRegion, b.x, b.y, bulletWidth * scale, height * scale, b.rot() - 90);
Draw.color(frontColor);
Draw.rect(frontRegion, b.x, b.y, bulletWidth * scale, height * scale, b.rot() - 90);
Draw.color();
}
}

View File

@@ -0,0 +1,46 @@
package mindustry.entities.bullet;
import arc.Core;
import arc.graphics.Color;
import arc.graphics.g2d.Draw;
import arc.graphics.g2d.TextureRegion;
import mindustry.entities.type.Bullet;
import mindustry.graphics.Pal;
/** An extended BulletType for most ammo-based bullets shot from turrets and units. */
public class BasicBulletType extends BulletType{
public Color backColor = Pal.bulletYellowBack, frontColor = Pal.bulletYellow;
public float bulletWidth = 5f, bulletHeight = 7f;
public float bulletShrink = 0.5f;
public String bulletSprite;
public TextureRegion backRegion;
public TextureRegion frontRegion;
public BasicBulletType(float speed, float damage, String bulletSprite){
super(speed, damage);
this.bulletSprite = bulletSprite;
}
/** For mods. */
public BasicBulletType(){
this(1f, 1f, "bullet");
}
@Override
public void load(){
backRegion = Core.atlas.find(bulletSprite + "-back");
frontRegion = Core.atlas.find(bulletSprite);
}
@Override
public void draw(Bullet b){
float height = bulletHeight * ((1f - bulletShrink) + bulletShrink * b.fout());
Draw.color(backColor);
Draw.rect(backRegion, b.x, b.y, bulletWidth, height, b.rot() - 90);
Draw.color(frontColor);
Draw.rect(frontRegion, b.x, b.y, bulletWidth, height, b.rot() - 90);
Draw.color();
}
}

View File

@@ -0,0 +1,24 @@
package mindustry.entities.bullet;
import mindustry.gen.*;
public class BombBulletType extends BasicBulletType{
public BombBulletType(float damage, float radius, String sprite){
super(0.7f, 0, sprite);
splashDamageRadius = radius;
splashDamage = damage;
collidesTiles = false;
collides = false;
bulletShrink = 0.7f;
lifetime = 30f;
drag = 0.05f;
keepVelocity = false;
collidesAir = false;
hitSound = Sounds.explosion;
}
public BombBulletType(){
this(1f, 1f, "shell");
}
}

View File

@@ -0,0 +1,165 @@
package mindustry.entities.bullet;
import arc.audio.*;
import arc.math.*;
import mindustry.content.*;
import mindustry.ctype.Content;
import mindustry.ctype.ContentType;
import mindustry.entities.*;
import mindustry.entities.Effects.*;
import mindustry.entities.effect.*;
import mindustry.entities.traits.*;
import mindustry.entities.type.*;
import mindustry.gen.*;
import mindustry.graphics.*;
import mindustry.type.*;
import mindustry.world.*;
public abstract class BulletType extends Content{
public float lifetime;
public float speed;
public float damage;
public float hitSize = 4;
public float drawSize = 40f;
public float drag = 0f;
public boolean pierce;
public Effect hitEffect, despawnEffect;
/** Effect created when shooting. */
public Effect shootEffect = Fx.shootSmall;
/** Extra smoke effect created when shooting. */
public Effect smokeEffect = Fx.shootSmallSmoke;
/** Sound made when hitting something or getting removed.*/
public Sound hitSound = Sounds.none;
/** Extra inaccuracy when firing. */
public float inaccuracy = 0f;
/** How many bullets get created per ammo item/liquid. */
public float ammoMultiplier = 2f;
/** Multiplied by turret reload speed to get final shoot speed. */
public float reloadMultiplier = 1f;
/** Recoil from shooter entities. */
public float recoil;
public float splashDamage = 0f;
/** Knockback in velocity. */
public float knockback;
/** Whether this bullet hits tiles. */
public boolean hitTiles = true;
/** Status effect applied on hit. */
public StatusEffect status = StatusEffects.none;
/** Intensity of applied status effect in terms of duration. */
public float statusDuration = 60 * 10f;
/** Whether this bullet type collides with tiles. */
public boolean collidesTiles = true;
/** Whether this bullet type collides with tiles that are of the same team. */
public boolean collidesTeam = false;
/** Whether this bullet type collides with air units. */
public boolean collidesAir = true;
/** Whether this bullet types collides with anything at all. */
public boolean collides = true;
/** Whether velocity is inherited from the shooter. */
public boolean keepVelocity = true;
//additional effects
public int fragBullets = 9;
public float fragVelocityMin = 0.2f, fragVelocityMax = 1f;
public BulletType fragBullet = null;
/** Use a negative value to disable splash damage. */
public float splashDamageRadius = -1f;
public int incendAmount = 0;
public float incendSpread = 8f;
public float incendChance = 1f;
public float homingPower = 0f;
public float homingRange = 50f;
public int lightining;
public int lightningLength = 5;
public float hitShake = 0f;
public BulletType(float speed, float damage){
this.speed = speed;
this.damage = damage;
lifetime = 40f;
hitEffect = Fx.hitBulletSmall;
despawnEffect = Fx.hitBulletSmall;
}
/** Returns maximum distance the bullet this bullet type has can travel. */
public float range(){
return speed * lifetime * (1f - drag);
}
public boolean collides(Bullet bullet, Tile tile){
return true;
}
public void hitTile(Bullet b, Tile tile){
hit(b);
}
public void hit(Bullet b){
hit(b, b.x, b.y);
}
public void hit(Bullet b, float x, float y){
Effects.effect(hitEffect, x, y, b.rot());
hitSound.at(b);
Effects.shake(hitShake, hitShake, b);
if(fragBullet != null){
for(int i = 0; i < fragBullets; i++){
float len = Mathf.random(1f, 7f);
float a = Mathf.random(360f);
Bullet.create(fragBullet, b, x + Angles.trnsx(a, len), y + Angles.trnsy(a, len), a, Mathf.random(fragVelocityMin, fragVelocityMax));
}
}
if(Mathf.chance(incendChance)){
Damage.createIncend(x, y, incendSpread, incendAmount);
}
if(splashDamageRadius > 0){
Damage.damage(b.getTeam(), x, y, splashDamageRadius, splashDamage * b.damageMultiplier());
}
}
public void despawned(Bullet b){
Effects.effect(despawnEffect, b.x, b.y, b.rot());
hitSound.at(b);
if(fragBullet != null || splashDamageRadius > 0){
hit(b);
}
for(int i = 0; i < lightining; i++){
Lightning.createLighting(Lightning.nextSeed(), b.getTeam(), Pal.surge, damage, b.x, b.y, Mathf.random(360f), lightningLength);
}
}
public void draw(Bullet b){
}
public void init(Bullet b){
}
public void update(Bullet b){
if(homingPower > 0.0001f){
TargetTrait target = Units.closestTarget(b.getTeam(), b.x, b.y, homingRange, e -> !e.isFlying() || collidesAir);
if(target != null){
b.velocity().setAngle(Mathf.slerpDelta(b.velocity().angle(), b.angleTo(target), 0.08f));
}
}
}
@Override
public ContentType getContentType(){
return ContentType.bullet;
}
}

View File

@@ -0,0 +1,46 @@
package mindustry.entities.bullet;
import arc.math.geom.Rectangle;
import arc.util.Time;
import mindustry.content.Fx;
import mindustry.entities.Units;
import mindustry.entities.type.Bullet;
public class FlakBulletType extends BasicBulletType{
protected static Rectangle rect = new Rectangle();
protected float explodeRange = 30f;
public FlakBulletType(float speed, float damage){
super(speed, damage, "shell");
splashDamage = 15f;
splashDamageRadius = 34f;
hitEffect = Fx.flakExplosionBig;
bulletWidth = 8f;
bulletHeight = 10f;
}
public FlakBulletType(){
this(1f, 1f);
}
@Override
public void update(Bullet b){
super.update(b);
if(b.getData() instanceof Integer) return;
if(b.timer.get(2, 6)){
Units.nearbyEnemies(b.getTeam(), rect.setSize(explodeRange * 2f).setCenter(b.x, b.y), unit -> {
if(b.getData() instanceof Float) return;
if(unit.dst(b) < explodeRange){
b.setData(0);
Time.run(5f, () -> {
if(b.getData() instanceof Integer){
b.time(b.lifetime());
}
});
}
});
}
}
}

View File

@@ -0,0 +1,54 @@
package mindustry.entities.bullet;
import arc.graphics.*;
import arc.graphics.g2d.*;
import mindustry.content.*;
import mindustry.entities.*;
import mindustry.entities.type.*;
import mindustry.graphics.*;
import mindustry.world.*;
import mindustry.world.blocks.*;
public class HealBulletType extends BulletType{
protected float healPercent = 3f;
public HealBulletType(float speed, float damage){
super(speed, damage);
shootEffect = Fx.shootHeal;
smokeEffect = Fx.hitLaser;
hitEffect = Fx.hitLaser;
despawnEffect = Fx.hitLaser;
collidesTeam = true;
}
public HealBulletType(){
this(1f, 1f);
}
@Override
public boolean collides(Bullet b, Tile tile){
return tile.getTeam() != b.getTeam() || tile.entity.healthf() < 1f;
}
@Override
public void draw(Bullet b){
Draw.color(Pal.heal);
Lines.stroke(2f);
Lines.lineAngleCenter(b.x, b.y, b.rot(), 7f);
Draw.color(Color.white);
Lines.lineAngleCenter(b.x, b.y, b.rot(), 3f);
Draw.reset();
}
@Override
public void hitTile(Bullet b, Tile tile){
super.hit(b);
tile = tile.link();
if(tile.entity != null && tile.getTeam() == b.getTeam() && !(tile.block() instanceof BuildBlock)){
Effects.effect(Fx.healBlockFull, Pal.heal, tile.drawx(), tile.drawy(), tile.block().size);
tile.entity.healBy(healPercent / 100f * tile.entity.maxHealth());
}
}
}

View File

@@ -0,0 +1,81 @@
package mindustry.entities.bullet;
import arc.graphics.*;
import arc.graphics.g2d.*;
import arc.math.geom.*;
import arc.util.ArcAnnotate.*;
import mindustry.content.*;
import mindustry.entities.*;
import mindustry.entities.effect.*;
import mindustry.entities.type.Bullet;
import mindustry.type.*;
import mindustry.world.*;
import static mindustry.Vars.*;
public class LiquidBulletType extends BulletType{
public @NonNull Liquid liquid;
public float puddleSize = 5f;
public LiquidBulletType(@Nullable Liquid liquid){
super(3.5f, 0);
if(liquid != null){
this.liquid = liquid;
this.status = liquid.effect;
}
lifetime = 74f;
statusDuration = 90f;
despawnEffect = Fx.none;
hitEffect = Fx.hitLiquid;
smokeEffect = Fx.none;
shootEffect = Fx.none;
drag = 0.009f;
knockback = 0.55f;
}
public LiquidBulletType(){
this(null);
}
@Override
public float range(){
return speed * lifetime / 2f;
}
@Override
public void update(Bullet b){
super.update(b);
if(liquid.canExtinguish()){
Tile tile = world.tileWorld(b.x, b.y);
if(tile != null && Fire.has(tile.x, tile.y)){
Fire.extinguish(tile, 100f);
b.remove();
hit(b);
}
}
}
@Override
public void draw(Bullet b){
Draw.color(liquid.color, Color.white, b.fout() / 100f);
Fill.circle(b.x, b.y, 0.5f + b.fout() * 2.5f);
}
@Override
public void hit(Bullet b, float hitx, float hity){
Effects.effect(hitEffect, liquid.color, hitx, hity);
Puddle.deposit(world.tileWorld(hitx, hity), liquid, puddleSize);
if(liquid.temperature <= 0.5f && liquid.flammability < 0.3f){
float intensity = 400f;
Fire.extinguish(world.tileWorld(hitx, hity), intensity);
for(Point2 p : Geometry.d4){
Fire.extinguish(world.tileWorld(hitx + p.x * tilesize, hity + p.y * tilesize), intensity);
}
}
}
}

View File

@@ -0,0 +1,107 @@
package mindustry.entities.bullet;
import arc.graphics.Color;
import arc.graphics.g2d.Draw;
import arc.math.Angles;
import arc.math.Mathf;
import mindustry.content.Fx;
import mindustry.entities.Effects;
import mindustry.entities.type.Bullet;
import mindustry.graphics.Pal;
import mindustry.world.blocks.distribution.MassDriver.DriverBulletData;
import static mindustry.Vars.content;
public class MassDriverBolt extends BulletType{
public MassDriverBolt(){
super(5.3f, 50);
collidesTiles = false;
lifetime = 200f;
despawnEffect = Fx.smeltsmoke;
hitEffect = Fx.hitBulletBig;
drag = 0.005f;
}
@Override
public void draw(mindustry.entities.type.Bullet b){
float w = 11f, h = 13f;
Draw.color(Pal.bulletYellowBack);
Draw.rect("shell-back", b.x, b.y, w, h, b.rot() + 90);
Draw.color(Pal.bulletYellow);
Draw.rect("shell", b.x, b.y, w, h, b.rot() + 90);
Draw.reset();
}
@Override
public void update(mindustry.entities.type.Bullet b){
//data MUST be an instance of DriverBulletData
if(!(b.getData() instanceof DriverBulletData)){
hit(b);
return;
}
float hitDst = 7f;
DriverBulletData data = (DriverBulletData)b.getData();
//if the target is dead, just keep flying until the bullet explodes
if(data.to.isDead()){
return;
}
float baseDst = data.from.dst(data.to);
float dst1 = b.dst(data.from);
float dst2 = b.dst(data.to);
boolean intersect = false;
//bullet has gone past the destination point: but did it intersect it?
if(dst1 > baseDst){
float angleTo = b.angleTo(data.to);
float baseAngle = data.to.angleTo(data.from);
//if angles are nearby, then yes, it did
if(Angles.near(angleTo, baseAngle, 2f)){
intersect = true;
//snap bullet position back; this is used for low-FPS situations
b.set(data.to.x + Angles.trnsx(baseAngle, hitDst), data.to.y + Angles.trnsy(baseAngle, hitDst));
}
}
//if on course and it's in range of the target
if(Math.abs(dst1 + dst2 - baseDst) < 4f && dst2 <= hitDst){
intersect = true;
} //else, bullet has gone off course, does not get recieved.
if(intersect){
data.to.handlePayload(b, data);
}
}
@Override
public void despawned(mindustry.entities.type.Bullet b){
super.despawned(b);
if(!(b.getData() instanceof DriverBulletData)) return;
DriverBulletData data = (DriverBulletData)b.getData();
for(int i = 0; i < data.items.length; i++){
int amountDropped = Mathf.random(0, data.items[i]);
if(amountDropped > 0){
float angle = b.rot() + Mathf.range(100f);
Effects.effect(Fx.dropItem, Color.white, b.x, b.y, angle, content.item(i));
}
}
}
@Override
public void hit(Bullet b, float hitx, float hity){
super.hit(b, hitx, hity);
despawned(b);
}
}

View File

@@ -0,0 +1,42 @@
package mindustry.entities.bullet;
import arc.graphics.Color;
import arc.math.Mathf;
import arc.util.Time;
import mindustry.content.Fx;
import mindustry.entities.Effects;
import mindustry.entities.type.Bullet;
import mindustry.gen.*;
import mindustry.graphics.Pal;
public class MissileBulletType extends BasicBulletType{
protected Color trailColor = Pal.missileYellowBack;
protected float weaveScale = 0f;
protected float weaveMag = -1f;
public MissileBulletType(float speed, float damage, String bulletSprite){
super(speed, damage, bulletSprite);
backColor = Pal.missileYellowBack;
frontColor = Pal.missileYellow;
homingPower = 7f;
hitSound = Sounds.explosion;
}
public MissileBulletType(){
this(1f, 1f, "missile");
}
@Override
public void update(Bullet b){
super.update(b);
if(Mathf.chance(Time.delta() * 0.2)){
Effects.effect(Fx.missileTrail, trailColor, b.x, b.y, 2f);
}
if(weaveMag > 0){
b.velocity().rotate(Mathf.sin(Time.time() + b.id * 4422, weaveScale, weaveMag) * Time.delta());
}
}
}

View File

@@ -0,0 +1,36 @@
package mindustry.entities.effect;
import arc.graphics.g2d.Draw;
import arc.math.Mathf;
import mindustry.entities.EntityGroup;
import mindustry.entities.type.TimedEntity;
import mindustry.entities.traits.BelowLiquidTrait;
import mindustry.entities.traits.DrawTrait;
import mindustry.graphics.Pal;
import static mindustry.Vars.groundEffectGroup;
/**
* Class for creating block rubble on the ground.
*/
public abstract class Decal extends TimedEntity implements BelowLiquidTrait, DrawTrait{
@Override
public float lifetime(){
return 3600;
}
@Override
public void draw(){
Draw.color(Pal.rubble.r, Pal.rubble.g, Pal.rubble.b, 1f - Mathf.curve(fin(), 0.98f));
drawDecal();
Draw.color();
}
@Override
public EntityGroup targetGroup(){
return groundEffectGroup;
}
abstract void drawDecal();
}

View File

@@ -0,0 +1,229 @@
package mindustry.entities.effect;
import arc.*;
import mindustry.annotations.Annotations.*;
import arc.struct.*;
import arc.math.*;
import arc.math.geom.*;
import arc.util.*;
import mindustry.content.*;
import mindustry.entities.*;
import mindustry.entities.traits.*;
import mindustry.entities.type.*;
import mindustry.game.EventType.*;
import mindustry.game.*;
import mindustry.gen.*;
import mindustry.type.*;
import mindustry.world.*;
import java.io.*;
import static mindustry.Vars.*;
public class Fire extends TimedEntity implements SaveTrait, SyncTrait{
private static final IntMap<Fire> map = new IntMap<>();
private static final float baseLifetime = 1000f, spreadChance = 0.05f, fireballChance = 0.07f;
private int loadedPosition = -1;
private Tile tile;
private Block block;
private float baseFlammability = -1, puddleFlammability;
private float lifetime;
/** Deserialization use only! */
public Fire(){
}
@Remote
public static void onRemoveFire(int fid){
fireGroup.removeByID(fid);
}
/** Start a fire on the tile. If there already is a file there, refreshes its lifetime. */
public static void create(Tile tile){
if(net.client() || tile == null) return; //not clientside.
Fire fire = map.get(tile.pos());
if(fire == null){
fire = new Fire();
fire.tile = tile;
fire.lifetime = baseLifetime;
fire.set(tile.worldx(), tile.worldy());
fire.add();
map.put(tile.pos(), fire);
}else{
fire.lifetime = baseLifetime;
fire.time = 0f;
}
}
public static boolean has(int x, int y){
if(!Structs.inBounds(x, y, world.width(), world.height()) || !map.containsKey(Pos.get(x, y))){
return false;
}
Fire fire = map.get(Pos.get(x, y));
return fire.isAdded() && fire.fin() < 1f && fire.tile != null && fire.tile.x == x && fire.tile.y == y;
}
/**
* Attempts to extinguish a fire by shortening its life. If there is no fire here, does nothing.
*/
public static void extinguish(Tile tile, float intensity){
if(tile != null && map.containsKey(tile.pos())){
Fire fire = map.get(tile.pos());
fire.time += intensity * Time.delta();
if(fire.time >= fire.lifetime()){
Events.fire(Trigger.fireExtinguish);
}
}
}
@Override
public TypeID getTypeID(){
return TypeIDs.fire;
}
@Override
public byte version(){
return 0;
}
@Override
public float lifetime(){
return lifetime;
}
@Override
public void update(){
if(Mathf.chance(0.1 * Time.delta())){
Effects.effect(Fx.fire, x + Mathf.range(4f), y + Mathf.range(4f));
}
if(Mathf.chance(0.05 * Time.delta())){
Effects.effect(Fx.fireSmoke, x + Mathf.range(4f), y + Mathf.range(4f));
}
if(Mathf.chance(0.001 * Time.delta())){
Sounds.fire.at(this);
}
time = Mathf.clamp(time + Time.delta(), 0, lifetime());
map.put(tile.pos(), this);
if(net.client()){
return;
}
if(time >= lifetime() || tile == null){
remove();
return;
}
TileEntity entity = tile.link().entity;
boolean damage = entity != null;
float flammability = baseFlammability + puddleFlammability;
if(!damage && flammability <= 0){
time += Time.delta() * 8;
}
if(baseFlammability < 0 || block != tile.block()){
baseFlammability = tile.block().getFlammability(tile);
block = tile.block();
}
if(damage){
lifetime += Mathf.clamp(flammability / 8f, 0f, 0.6f) * Time.delta();
}
if(flammability > 1f && Mathf.chance(spreadChance * Time.delta() * Mathf.clamp(flammability / 5f, 0.3f, 2f))){
Point2 p = Geometry.d4[Mathf.random(3)];
Tile other = world.tile(tile.x + p.x, tile.y + p.y);
create(other);
if(Mathf.chance(fireballChance * Time.delta() * Mathf.clamp(flammability / 10f))){
Call.createBullet(Bullets.fireball, Team.derelict, x, y, Mathf.random(360f), 1, 1);
}
}
if(Mathf.chance(0.1 * Time.delta())){
Puddle p = Puddle.getPuddle(tile);
if(p != null){
puddleFlammability = p.getFlammability() / 3f;
}else{
puddleFlammability = 0;
}
if(damage){
entity.damage(0.4f);
}
Damage.damageUnits(null, tile.worldx(), tile.worldy(), tilesize, 3f,
unit -> !unit.isFlying() && !unit.isImmune(StatusEffects.burning),
unit -> unit.applyEffect(StatusEffects.burning, 60 * 5));
}
}
@Override
public void writeSave(DataOutput stream) throws IOException{
stream.writeInt(tile.pos());
stream.writeFloat(lifetime);
stream.writeFloat(time);
}
@Override
public void readSave(DataInput stream, byte version) throws IOException{
this.loadedPosition = stream.readInt();
this.lifetime = stream.readFloat();
this.time = stream.readFloat();
add();
}
@Override
public void write(DataOutput data) throws IOException{
data.writeInt(tile.pos());
data.writeFloat(lifetime);
}
@Override
public void read(DataInput data) throws IOException{
int pos = data.readInt();
this.lifetime = data.readFloat();
x = Pos.x(pos) * tilesize;
y = Pos.y(pos) * tilesize;
tile = world.tile(pos);
}
@Override
public void reset(){
loadedPosition = -1;
tile = null;
baseFlammability = -1;
puddleFlammability = 0f;
incrementID();
}
@Override
public void added(){
if(loadedPosition != -1){
map.put(loadedPosition, this);
tile = world.tile(loadedPosition);
set(tile.worldx(), tile.worldy());
}
}
@Override
public void removed(){
if(tile != null){
Call.onRemoveFire(id);
map.remove(tile.pos());
}
}
@Override
public EntityGroup targetGroup(){
return fireGroup;
}
}

View File

@@ -0,0 +1,90 @@
package mindustry.entities.effect;
import arc.math.Mathf;
import arc.util.Time;
import mindustry.Vars;
import mindustry.entities.Effects;
import mindustry.entities.Effects.Effect;
import mindustry.entities.Effects.EffectRenderer;
import mindustry.entities.type.EffectEntity;
import mindustry.world.Tile;
/**
* A ground effect contains an effect that is rendered on the ground layer as opposed to the top layer.
*/
public class GroundEffectEntity extends EffectEntity{
private boolean once;
@Override
public void update(){
GroundEffect effect = (GroundEffect)this.effect;
if(effect.isStatic){
time += Time.delta();
time = Mathf.clamp(time, 0, effect.staticLife);
if(!once && time >= lifetime()){
once = true;
time = 0f;
Tile tile = Vars.world.tileWorld(x, y);
if(tile != null && tile.floor().isLiquid){
remove();
}
}else if(once && time >= effect.staticLife){
remove();
}
}else{
super.update();
}
}
@Override
public void draw(){
GroundEffect effect = (GroundEffect)this.effect;
if(once && effect.isStatic)
Effects.renderEffect(id, effect, color, lifetime(), rotation, x, y, data);
else
Effects.renderEffect(id, effect, color, time, rotation, x, y, data);
}
@Override
public void reset(){
super.reset();
once = false;
}
/**
* An effect that is rendered on the ground layer as opposed to the top layer.
*/
public static class GroundEffect extends Effect{
/**
* How long this effect stays on the ground when static.
*/
public final float staticLife;
/**
* If true, this effect will stop and lie on the ground for a specific duration,
* after its initial lifetime is over.
*/
public final boolean isStatic;
public GroundEffect(float life, float staticLife, EffectRenderer draw){
super(life, draw);
this.staticLife = staticLife;
this.isStatic = true;
}
public GroundEffect(boolean isStatic, float life, EffectRenderer draw){
super(life, draw);
this.staticLife = 0f;
this.isStatic = isStatic;
}
public GroundEffect(float life, EffectRenderer draw){
super(life, draw);
this.staticLife = 0f;
this.isStatic = false;
}
}
}

View File

@@ -0,0 +1,119 @@
package mindustry.entities.effect;
import mindustry.annotations.Annotations.Loc;
import mindustry.annotations.Annotations.Remote;
import arc.graphics.g2d.*;
import arc.math.Interpolation;
import arc.math.Mathf;
import arc.math.geom.Position;
import arc.math.geom.Vector2;
import arc.util.Time;
import arc.util.pooling.Pools;
import mindustry.entities.*;
import mindustry.entities.type.TimedEntity;
import mindustry.entities.traits.DrawTrait;
import mindustry.entities.type.Unit;
import mindustry.graphics.Pal;
import mindustry.type.Item;
import mindustry.world.Tile;
import static mindustry.Vars.*;
public class ItemTransfer extends TimedEntity implements DrawTrait{
private Vector2 from = new Vector2();
private Vector2 current = new Vector2();
private Vector2 tovec = new Vector2();
private Item item;
private float seed;
private Position to;
private Runnable done;
public ItemTransfer(){
}
@Remote(called = Loc.server, unreliable = true)
public static void transferItemEffect(Item item, float x, float y, Unit to){
if(to == null) return;
create(item, x, y, to, () -> {
});
}
@Remote(called = Loc.server, unreliable = true)
public static void transferItemToUnit(Item item, float x, float y, Unit to){
if(to == null) return;
create(item, x, y, to, () -> to.addItem(item));
}
@Remote(called = Loc.server)
public static void transferItemTo(Item item, int amount, float x, float y, Tile tile){
if(tile == null || tile.entity == null || tile.entity.items == null) return;
for(int i = 0; i < Mathf.clamp(amount / 3, 1, 8); i++){
Time.run(i * 3, () -> create(item, x, y, tile, () -> {}));
}
tile.entity.items.add(item, amount);
}
public static void create(Item item, float fromx, float fromy, Position to, Runnable done){
ItemTransfer tr = Pools.obtain(ItemTransfer.class, ItemTransfer::new);
tr.item = item;
tr.from.set(fromx, fromy);
tr.to = to;
tr.done = done;
tr.seed = Mathf.range(1f);
tr.add();
}
@Override
public float lifetime(){
return 60;
}
@Override
public void reset(){
super.reset();
item = null;
to = null;
done = null;
from.setZero();
current.setZero();
tovec.setZero();
}
@Override
public void removed(){
if(done != null){
done.run();
}
Pools.free(this);
}
@Override
public void update(){
if(to == null){
remove();
return;
}
super.update();
current.set(from).interpolate(tovec.set(to.getX(), to.getY()), fin(), Interpolation.pow3);
current.add(tovec.set(to.getX(), to.getY()).sub(from).nor().rotate90(1).scl(seed * fslope() * 10f));
set(current.x, current.y);
}
@Override
public void draw(){
Lines.stroke(fslope() * 2f, Pal.accent);
Lines.circle(x, y, fslope() * 2f);
Draw.color(item.color);
Fill.circle(x, y, fslope() * 1.5f);
Draw.reset();
}
@Override
public EntityGroup targetGroup(){
return effectGroup;
}
}

View File

@@ -0,0 +1,160 @@
package mindustry.entities.effect;
import mindustry.annotations.Annotations.Loc;
import mindustry.annotations.Annotations.Remote;
import arc.struct.Array;
import arc.struct.IntSet;
import arc.graphics.Color;
import arc.graphics.g2d.*;
import arc.math.*;
import arc.math.geom.*;
import arc.util.pooling.Pools;
import mindustry.content.Bullets;
import mindustry.entities.EntityGroup;
import mindustry.entities.Units;
import mindustry.entities.type.Bullet;
import mindustry.entities.type.TimedEntity;
import mindustry.entities.traits.DrawTrait;
import mindustry.entities.traits.TimeTrait;
import mindustry.entities.type.Unit;
import mindustry.game.Team;
import mindustry.gen.Call;
import mindustry.graphics.Pal;
import mindustry.world.Tile;
import static mindustry.Vars.*;
public class Lightning extends TimedEntity implements DrawTrait, TimeTrait{
public static final float lifetime = 10f;
private static final RandomXS128 random = new RandomXS128();
private static final Rectangle rect = new Rectangle();
private static final Array<Unit> entities = new Array<>();
private static final IntSet hit = new IntSet();
private static final int maxChain = 8;
private static final float hitRange = 30f;
private static int lastSeed = 0;
private Array<Vector2> lines = new Array<>();
private Color color = Pal.lancerLaser;
/** For pooling use only. Do not call directly! */
public Lightning(){
}
/** Create a lighting branch at a location. Use Team.none to damage everyone. */
public static void create(Team team, Color color, float damage, float x, float y, float targetAngle, int length){
Call.createLighting(nextSeed(), team, color, damage, x, y, targetAngle, length);
}
public static int nextSeed(){
return lastSeed++;
}
/** Do not invoke! */
@Remote(called = Loc.server, unreliable = true)
public static void createLighting(int seed, Team team, Color color, float damage, float x, float y, float rotation, int length){
Lightning l = Pools.obtain(Lightning.class, Lightning::new);
Float dmg = damage;
l.x = x;
l.y = y;
l.color = color;
l.add();
random.setSeed(seed);
hit.clear();
boolean[] bhit = {false};
for(int i = 0; i < length / 2; i++){
Bullet.create(Bullets.damageLightning, l, team, x, y, 0f, 1f, 1f, dmg);
l.lines.add(new Vector2(x + Mathf.range(3f), y + Mathf.range(3f)));
if(l.lines.size > 1){
bhit[0] = false;
Position from = l.lines.get(l.lines.size - 2);
Position to = l.lines.get(l.lines.size - 1);
world.raycastEach(world.toTile(from.getX()), world.toTile(from.getY()), world.toTile(to.getX()), world.toTile(to.getY()), (wx, wy) -> {
Tile tile = world.ltile(wx, wy);
if(tile != null && tile.block().insulated){
bhit[0] = true;
//snap it instead of removing
l.lines.get(l.lines.size -1).set(wx * tilesize, wy * tilesize);
return true;
}
return false;
});
if(bhit[0]) break;
}
rect.setSize(hitRange).setCenter(x, y);
entities.clear();
if(hit.size < maxChain){
Units.nearbyEnemies(team, rect, u -> {
if(!hit.contains(u.getID())){
entities.add(u);
}
});
}
Unit furthest = Geometry.findFurthest(x, y, entities);
if(furthest != null){
hit.add(furthest.getID());
x = furthest.x;
y = furthest.y;
}else{
rotation += random.range(20f);
x += Angles.trnsx(rotation, hitRange / 2f);
y += Angles.trnsy(rotation, hitRange / 2f);
}
}
}
@Override
public float lifetime(){
return lifetime;
}
@Override
public void reset(){
super.reset();
color = Pal.lancerLaser;
lines.clear();
}
@Override
public void removed(){
super.removed();
Pools.free(this);
}
@Override
public void draw(){
Lines.stroke(3f * fout());
Draw.color(color, Color.white, fin());
Lines.beginLine();
Lines.linePoint(x, y);
for(Position p : lines){
Lines.linePoint(p.getX(), p.getY());
}
Lines.endLine();
int i = 0;
for(Position p : lines){
Fill.square(p.getX(), p.getY(), (5f - (float)i++ / lines.size * 2f) * fout(), 45);
}
Draw.reset();
}
@Override
public EntityGroup targetGroup(){
return bulletGroup;
}
}

View File

@@ -0,0 +1,322 @@
package mindustry.entities.effect;
import mindustry.annotations.Annotations.*;
import arc.struct.*;
import arc.graphics.*;
import arc.graphics.g2d.*;
import arc.math.*;
import arc.math.geom.*;
import arc.util.*;
import arc.util.pooling.Pool.*;
import arc.util.pooling.*;
import mindustry.content.*;
import mindustry.entities.*;
import mindustry.entities.traits.*;
import mindustry.entities.type.*;
import mindustry.game.*;
import mindustry.gen.*;
import mindustry.type.*;
import mindustry.world.*;
import java.io.*;
import static mindustry.Vars.*;
public class Puddle extends SolidEntity implements SaveTrait, Poolable, DrawTrait, SyncTrait{
private static final IntMap<Puddle> map = new IntMap<>();
private static final float maxLiquid = 70f;
private static final int maxGeneration = 2;
private static final Color tmp = new Color();
private static final Rectangle rect = new Rectangle();
private static final Rectangle rect2 = new Rectangle();
private static int seeds;
private int loadedPosition = -1;
private float updateTime;
private float lastRipple;
private Tile tile;
private Liquid liquid;
private float amount, targetAmount;
private float accepting;
private byte generation;
/** Deserialization use only! */
public Puddle(){
}
/** Deposists a puddle between tile and source. */
public static void deposit(Tile tile, Tile source, Liquid liquid, float amount){
deposit(tile, source, liquid, amount, 0);
}
/** Deposists a puddle at a tile. */
public static void deposit(Tile tile, Liquid liquid, float amount){
deposit(tile, tile, liquid, amount, 0);
}
/** Returns the puddle on the specified tile. May return null. */
public static Puddle getPuddle(Tile tile){
return map.get(tile.pos());
}
private static void deposit(Tile tile, Tile source, Liquid liquid, float amount, int generation){
if(tile == null) return;
if(tile.floor().isLiquid && !canStayOn(liquid, tile.floor().liquidDrop)){
reactPuddle(tile.floor().liquidDrop, liquid, amount, tile,
(tile.worldx() + source.worldx()) / 2f, (tile.worldy() + source.worldy()) / 2f);
Puddle p = map.get(tile.pos());
if(generation == 0 && p != null && p.lastRipple <= Time.time() - 40f){
Effects.effect(Fx.ripple, tile.floor().liquidDrop.color,
(tile.worldx() + source.worldx()) / 2f, (tile.worldy() + source.worldy()) / 2f);
p.lastRipple = Time.time();
}
return;
}
Puddle p = map.get(tile.pos());
if(p == null){
if(net.client()) return; //not clientside.
Puddle puddle = Pools.obtain(Puddle.class, Puddle::new);
puddle.tile = tile;
puddle.liquid = liquid;
puddle.amount = amount;
puddle.generation = (byte)generation;
puddle.set((tile.worldx() + source.worldx()) / 2f, (tile.worldy() + source.worldy()) / 2f);
puddle.add();
map.put(tile.pos(), puddle);
}else if(p.liquid == liquid){
p.accepting = Math.max(amount, p.accepting);
if(generation == 0 && p.lastRipple <= Time.time() - 40f && p.amount >= maxLiquid / 2f){
Effects.effect(Fx.ripple, p.liquid.color, (tile.worldx() + source.worldx()) / 2f, (tile.worldy() + source.worldy()) / 2f);
p.lastRipple = Time.time();
}
}else{
p.amount += reactPuddle(p.liquid, liquid, amount, p.tile, p.x, p.y);
}
}
/**
* Returns whether the first liquid can 'stay' on the second one.
* Currently, the only place where this can happen is oil on water.
*/
private static boolean canStayOn(Liquid liquid, Liquid other){
return liquid == Liquids.oil && other == Liquids.water;
}
/** Reacts two liquids together at a location. */
private static float reactPuddle(Liquid dest, Liquid liquid, float amount, Tile tile, float x, float y){
if((dest.flammability > 0.3f && liquid.temperature > 0.7f) ||
(liquid.flammability > 0.3f && dest.temperature > 0.7f)){ //flammable liquid + hot liquid
Fire.create(tile);
if(Mathf.chance(0.006 * amount)){
Call.createBullet(Bullets.fireball, Team.derelict, x, y, Mathf.random(360f), 1f, 1f);
}
}else if(dest.temperature > 0.7f && liquid.temperature < 0.55f){ //cold liquid poured onto hot puddle
if(Mathf.chance(0.5f * amount)){
Effects.effect(Fx.steam, x, y);
}
return -0.1f * amount;
}else if(liquid.temperature > 0.7f && dest.temperature < 0.55f){ //hot liquid poured onto cold puddle
if(Mathf.chance(0.8f * amount)){
Effects.effect(Fx.steam, x, y);
}
return -0.4f * amount;
}
return 0f;
}
@Remote(called = Loc.server)
public static void onPuddleRemoved(int puddleid){
puddleGroup.removeByID(puddleid);
}
public float getFlammability(){
return liquid.flammability * amount;
}
@Override
public TypeID getTypeID(){
return TypeIDs.puddle;
}
@Override
public byte version(){
return 0;
}
@Override
public void hitbox(Rectangle rectangle){
rectangle.setCenter(x, y).setSize(tilesize);
}
@Override
public void hitboxTile(Rectangle rectangle){
rectangle.setCenter(x, y).setSize(0f);
}
@Override
public void update(){
//no updating happens clientside
if(net.client()){
amount = Mathf.lerpDelta(amount, targetAmount, 0.15f);
}else{
//update code
float addSpeed = accepting > 0 ? 3f : 0f;
amount -= Time.delta() * (1f - liquid.viscosity) / (5f + addSpeed);
amount += accepting;
accepting = 0f;
if(amount >= maxLiquid / 1.5f && generation < maxGeneration){
float deposited = Math.min((amount - maxLiquid / 1.5f) / 4f, 0.3f) * Time.delta();
for(Point2 point : Geometry.d4){
Tile other = world.tile(tile.x + point.x, tile.y + point.y);
if(other != null && other.block() == Blocks.air){
deposit(other, tile, liquid, deposited, generation + 1);
amount -= deposited / 2f; //tweak to speed up/slow down puddle propagation
}
}
}
amount = Mathf.clamp(amount, 0, maxLiquid);
if(amount <= 0f){
Call.onPuddleRemoved(getID());
}
}
//effects-only code
if(amount >= maxLiquid / 2f && updateTime <= 0f){
Units.nearby(rect.setSize(Mathf.clamp(amount / (maxLiquid / 1.5f)) * 10f).setCenter(x, y), unit -> {
if(unit.isFlying()) return;
unit.hitbox(rect2);
if(!rect.overlaps(rect2)) return;
unit.applyEffect(liquid.effect, 60 * 2);
if(unit.velocity().len() > 0.1){
Effects.effect(Fx.ripple, liquid.color, unit.x, unit.y);
}
});
if(liquid.temperature > 0.7f && (tile.link().entity != null) && Mathf.chance(0.3 * Time.delta())){
Fire.create(tile);
}
updateTime = 20f;
}
updateTime -= Time.delta();
}
@Override
public void draw(){
seeds = id;
boolean onLiquid = tile.floor().isLiquid;
float f = Mathf.clamp(amount / (maxLiquid / 1.5f));
float smag = onLiquid ? 0.8f : 0f;
float sscl = 20f;
Draw.color(tmp.set(liquid.color).shiftValue(-0.05f));
Fill.circle(x + Mathf.sin(Time.time() + seeds * 532, sscl, smag), y + Mathf.sin(Time.time() + seeds * 53, sscl, smag), f * 8f);
Angles.randLenVectors(id, 3, f * 6f, (ex, ey) -> {
Fill.circle(x + ex + Mathf.sin(Time.time() + seeds * 532, sscl, smag),
y + ey + Mathf.sin(Time.time() + seeds * 53, sscl, smag), f * 5f);
seeds++;
});
Draw.color();
if(liquid.lightColor.a > 0.001f && f > 0){
Color color = liquid.lightColor;
float opacity = color.a * f;
renderer.lights.add(tile.drawx(), tile.drawy(), 30f * f, color, opacity * 0.8f);
}
}
@Override
public float drawSize(){
return 20;
}
@Override
public void writeSave(DataOutput stream) throws IOException{
stream.writeInt(tile.pos());
stream.writeFloat(x);
stream.writeFloat(y);
stream.writeByte(liquid.id);
stream.writeFloat(amount);
stream.writeByte(generation);
}
@Override
public void readSave(DataInput stream, byte version) throws IOException{
this.loadedPosition = stream.readInt();
this.x = stream.readFloat();
this.y = stream.readFloat();
this.liquid = content.liquid(stream.readByte());
this.amount = stream.readFloat();
this.generation = stream.readByte();
add();
}
@Override
public void reset(){
loadedPosition = -1;
tile = null;
liquid = null;
amount = 0;
generation = 0;
accepting = 0;
}
@Override
public void added(){
if(loadedPosition != -1){
map.put(loadedPosition, this);
tile = world.tile(loadedPosition);
}
}
@Override
public void removed(){
if(tile != null){
map.remove(tile.pos());
}
reset();
}
@Override
public void write(DataOutput data) throws IOException{
data.writeFloat(x);
data.writeFloat(y);
data.writeByte(liquid.id);
data.writeShort((short)(amount * 4));
data.writeInt(tile.pos());
}
@Override
public void read(DataInput data) throws IOException{
x = data.readFloat();
y = data.readFloat();
liquid = content.liquid(data.readByte());
targetAmount = data.readShort() / 4f;
int pos = data.readInt();
tile = world.tile(pos);
map.put(pos, this);
}
@Override
public EntityGroup targetGroup(){
return puddleGroup;
}
}

View File

@@ -0,0 +1,41 @@
package mindustry.entities.effect;
import arc.Core;
import arc.graphics.g2d.Draw;
import arc.graphics.g2d.TextureRegion;
import arc.math.Mathf;
import static mindustry.Vars.headless;
public class RubbleDecal extends Decal{
private TextureRegion region;
/** Creates a rubble effect at a position. Provide a block size to use. */
public static void create(float x, float y, int size){
if(headless) return;
RubbleDecal decal = new RubbleDecal();
decal.region = Core.atlas.find("rubble-" + size + "-" + Mathf.randomSeed(decal.id, 0, 1));
if(!Core.atlas.isFound(decal.region)){
return;
}
decal.set(x, y);
decal.add();
}
@Override
public float lifetime(){
return 8200f;
}
@Override
public void drawDecal(){
if(!Core.atlas.isFound(region)){
remove();
return;
}
Draw.rect(region, x, y, Mathf.randomSeed(id, 0, 4) * 90);
}
}

View File

@@ -0,0 +1,49 @@
package mindustry.entities.effect;
import arc.Core;
import arc.graphics.g2d.Draw;
import arc.graphics.g2d.TextureRegion;
import arc.math.Angles;
import arc.math.Mathf;
import mindustry.world.Tile;
import static mindustry.Vars.headless;
import static mindustry.Vars.world;
public class ScorchDecal extends Decal{
private static final int scorches = 5;
private static final TextureRegion[] regions = new TextureRegion[scorches];
public static void create(float x, float y){
if(headless) return;
if(regions[0] == null || regions[0].getTexture().isDisposed()){
for(int i = 0; i < regions.length; i++){
regions[i] = Core.atlas.find("scorch" + (i + 1));
}
}
Tile tile = world.tileWorld(x, y);
if(tile == null || tile.floor().liquidDrop != null) return;
ScorchDecal decal = new ScorchDecal();
decal.set(x, y);
decal.add();
}
@Override
public void drawDecal(){
for(int i = 0; i < 3; i++){
TextureRegion region = regions[Mathf.randomSeed(id - i, 0, scorches - 1)];
float rotation = Mathf.randomSeed(id + i, 0, 360);
float space = 1.5f + Mathf.randomSeed(id + i + 1, 0, 20) / 10f;
Draw.rect(region,
x + Angles.trnsx(rotation, space),
y + Angles.trnsy(rotation, space) + region.getHeight() / 2f * Draw.scl,
region.getWidth() * Draw.scl,
region.getHeight() * Draw.scl,
region.getWidth() / 2f * Draw.scl, 0, rotation - 90);
}
}
}

View File

@@ -0,0 +1,13 @@
package mindustry.entities.traits;
public interface AbsorbTrait extends Entity, TeamTrait, DamageTrait{
void absorb();
default boolean canBeAbsorbed(){
return true;
}
default float getShieldDamage(){
return damage();
}
}

View File

@@ -0,0 +1,7 @@
package mindustry.entities.traits;
/**
* A flag interface for marking an effect as appearing below liquids.
*/
public interface BelowLiquidTrait{
}

View File

@@ -0,0 +1,22 @@
package mindustry.entities.traits;
/** A class for gracefully merging mining and building traits.*/
public interface BuilderMinerTrait extends MinerTrait, BuilderTrait{
default void updateMechanics(){
updateBuilding();
//mine only when not building
if(buildRequest() == null){
updateMining();
}
}
default void drawMechanics(){
if(isBuilding()){
drawBuilding();
}else{
drawMining();
}
}
}

View File

@@ -0,0 +1,394 @@
package mindustry.entities.traits;
import arc.*;
import arc.struct.Queue;
import arc.graphics.g2d.*;
import arc.math.*;
import arc.math.geom.*;
import arc.util.ArcAnnotate.*;
import arc.util.*;
import mindustry.*;
import mindustry.content.*;
import mindustry.entities.type.*;
import mindustry.game.EventType.*;
import mindustry.gen.*;
import mindustry.graphics.*;
import mindustry.world.*;
import mindustry.world.blocks.*;
import mindustry.world.blocks.BuildBlock.*;
import java.io.*;
import java.util.*;
import static mindustry.Vars.*;
import static mindustry.entities.traits.BuilderTrait.BuildDataStatic.*;
/** Interface for units that build things.*/
public interface BuilderTrait extends Entity, TeamTrait{
//these are not instance variables!
float placeDistance = 220f;
float mineDistance = 70f;
/** Updates building mechanism for this unit.*/
default void updateBuilding(){
float finalPlaceDst = state.rules.infiniteResources ? Float.MAX_VALUE : placeDistance;
Unit unit = (Unit)this;
Iterator<BuildRequest> it = buildQueue().iterator();
while(it.hasNext()){
BuildRequest req = it.next();
Tile tile = world.tile(req.x, req.y);
if(tile == null || (req.breaking && tile.block() == Blocks.air) || (!req.breaking && (tile.rotation() == req.rotation || !req.block.rotate) && tile.block() == req.block)){
it.remove();
}
}
TileEntity core = unit.getClosestCore();
//nothing to build.
if(buildRequest() == null) return;
//find the next build request
if(buildQueue().size > 1){
int total = 0;
BuildRequest req;
while((dst((req = buildRequest()).tile()) > finalPlaceDst || shouldSkip(req, core)) && total < buildQueue().size){
buildQueue().removeFirst();
buildQueue().addLast(req);
total++;
}
}
BuildRequest current = buildRequest();
if(dst(current.tile()) > finalPlaceDst) return;
Tile tile = world.tile(current.x, current.y);
if(!(tile.block() instanceof BuildBlock)){
if(!current.initialized && canCreateBlocks() && !current.breaking && Build.validPlace(getTeam(), current.x, current.y, current.block, current.rotation)){
Call.beginPlace(getTeam(), current.x, current.y, current.block, current.rotation);
}else if(!current.initialized && canCreateBlocks() && current.breaking && Build.validBreak(getTeam(), current.x, current.y)){
Call.beginBreak(getTeam(), current.x, current.y);
}else{
buildQueue().removeFirst();
return;
}
}
if(tile.entity instanceof BuildEntity && !current.initialized){
Core.app.post(() -> Events.fire(new BuildSelectEvent(tile, unit.getTeam(), this, current.breaking)));
current.initialized = true;
}
//if there is no core to build with or no build entity, stop building!
if((core == null && !state.rules.infiniteResources) || !(tile.entity instanceof BuildEntity)){
return;
}
//otherwise, update it.
BuildEntity entity = tile.ent();
if(entity == null){
return;
}
if(unit.dst(tile) <= finalPlaceDst){
unit.rotation = Mathf.slerpDelta(unit.rotation, unit.angleTo(entity), 0.4f);
}
if(current.breaking){
entity.deconstruct(unit, core, 1f / entity.buildCost * Time.delta() * getBuildPower(tile) * state.rules.buildSpeedMultiplier);
}else{
if(entity.construct(unit, core, 1f / entity.buildCost * Time.delta() * getBuildPower(tile) * state.rules.buildSpeedMultiplier, current.hasConfig)){
if(current.hasConfig){
Call.onTileConfig(null, tile, current.config);
}
}
}
current.stuck = Mathf.equal(current.progress, entity.progress);
current.progress = entity.progress;
}
/** @return whether this request should be skipped, in favor of the next one. */
default boolean shouldSkip(BuildRequest request, @Nullable TileEntity core){
//requests that you have at least *started* are considered
if(state.rules.infiniteResources || request.breaking || !request.initialized || core == null) return false;
return request.stuck && !core.items.has(request.block.requirements);
}
/** Returns the queue for storing build requests. */
Queue<BuildRequest> buildQueue();
/** Build power, can be any float. 1 = builds recipes in normal time, 0 = doesn't build at all. */
float getBuildPower(Tile tile);
/** Whether this type of builder can begin creating new blocks. */
default boolean canCreateBlocks(){
return true;
}
default void writeBuilding(DataOutput output) throws IOException{
BuildRequest request = buildRequest();
if(request != null && (request.block != null || request.breaking)){
output.writeByte(request.breaking ? 1 : 0);
output.writeInt(Pos.get(request.x, request.y));
output.writeFloat(request.progress);
if(!request.breaking){
output.writeShort(request.block.id);
output.writeByte(request.rotation);
}
}else{
output.writeByte(-1);
}
}
default void readBuilding(DataInput input) throws IOException{
readBuilding(input, true);
}
default void readBuilding(DataInput input, boolean applyChanges) throws IOException{
if(applyChanges) buildQueue().clear();
byte type = input.readByte();
if(type != -1){
int position = input.readInt();
float progress = input.readFloat();
BuildRequest request;
if(type == 1){ //remove
request = new BuildRequest(Pos.x(position), Pos.y(position));
}else{ //place
short block = input.readShort();
byte rotation = input.readByte();
request = new BuildRequest(Pos.x(position), Pos.y(position), rotation, content.block(block));
}
request.progress = progress;
if(applyChanges){
buildQueue().addLast(request);
}else if(isBuilding()){
BuildRequest last = buildRequest();
last.progress = progress;
if(last.tile() != null && last.tile().entity instanceof BuildEntity){
((BuildEntity)last.tile().entity).progress = progress;
}
}
}
}
/** Return whether this builder's place queue contains items. */
default boolean isBuilding(){
return buildQueue().size != 0;
}
/** Clears the placement queue. */
default void clearBuilding(){
buildQueue().clear();
}
/** Add another build requests to the tail of the queue, if it doesn't exist there yet. */
default void addBuildRequest(BuildRequest place){
addBuildRequest(place, true);
}
/** Add another build requests to the queue, if it doesn't exist there yet. */
default void addBuildRequest(BuildRequest place, boolean tail){
BuildRequest replace = null;
for(BuildRequest request : buildQueue()){
if(request.x == place.x && request.y == place.y){
replace = request;
break;
}
}
if(replace != null){
buildQueue().remove(replace);
}
Tile tile = world.tile(place.x, place.y);
if(tile != null && tile.entity instanceof BuildEntity){
place.progress = tile.<BuildEntity>ent().progress;
}
if(tail){
buildQueue().addLast(place);
}else{
buildQueue().addFirst(place);
}
}
/**
* Return the build requests currently active, or the one at the top of the queue.
* May return null.
*/
default @Nullable
BuildRequest buildRequest(){
return buildQueue().size == 0 ? null : buildQueue().first();
}
//due to iOS weirdness, this is apparently required
class BuildDataStatic{
static Vector2[] tmptr = new Vector2[]{new Vector2(), new Vector2(), new Vector2(), new Vector2()};
}
/** Draw placement effects for an entity. */
default void drawBuilding(){
if(!isBuilding()) return;
Unit unit = (Unit)this;
BuildRequest request = buildRequest();
Tile tile = world.tile(request.x, request.y);
if(dst(tile) > placeDistance && !state.isEditor()){
return;
}
Lines.stroke(1f, Pal.accent);
float focusLen = 3.8f + Mathf.absin(Time.time(), 1.1f, 0.6f);
float px = unit.x + Angles.trnsx(unit.rotation, focusLen);
float py = unit.y + Angles.trnsy(unit.rotation, focusLen);
float sz = Vars.tilesize * tile.block().size / 2f;
float ang = unit.angleTo(tile);
tmptr[0].set(tile.drawx() - sz, tile.drawy() - sz);
tmptr[1].set(tile.drawx() + sz, tile.drawy() - sz);
tmptr[2].set(tile.drawx() - sz, tile.drawy() + sz);
tmptr[3].set(tile.drawx() + sz, tile.drawy() + sz);
Arrays.sort(tmptr, (a, b) -> -Float.compare(Angles.angleDist(Angles.angle(unit.x, unit.y, a.x, a.y), ang),
Angles.angleDist(Angles.angle(unit.x, unit.y, b.x, b.y), ang)));
float x1 = tmptr[0].x, y1 = tmptr[0].y,
x3 = tmptr[1].x, y3 = tmptr[1].y;
Draw.alpha(1f);
Lines.line(px, py, x1, y1);
Lines.line(px, py, x3, y3);
Fill.circle(px, py, 1.6f + Mathf.absin(Time.time(), 0.8f, 1.5f));
Draw.color();
}
/** Class for storing build requests. Can be either a place or remove request. */
class BuildRequest{
/** Position and rotation of this request. */
public int x, y, rotation;
/** Block being placed. If null, this is a breaking request.*/
public @Nullable Block block;
/** Whether this is a break request.*/
public boolean breaking;
/** Whether this request comes with a config int. If yes, any blocks placed with this request will not call playerPlaced.*/
public boolean hasConfig;
/** Config int. Not used unless hasConfig is true.*/
public int config;
/** Original position, only used in schematics.*/
public int originalX, originalY, originalWidth, originalHeight;
/** Last progress.*/
public float progress;
/** Whether construction has started for this request, and other special variables.*/
public boolean initialized, worldContext = true, stuck;
/** Visual scale. Used only for rendering.*/
public float animScale = 0f;
/** This creates a build request. */
public BuildRequest(int x, int y, int rotation, Block block){
this.x = x;
this.y = y;
this.rotation = rotation;
this.block = block;
this.breaking = false;
}
/** This creates a remove request. */
public BuildRequest(int x, int y){
this.x = x;
this.y = y;
this.rotation = -1;
this.block = world.tile(x, y).block();
this.breaking = true;
}
public BuildRequest(){
}
public BuildRequest copy(){
BuildRequest copy = new BuildRequest();
copy.x = x;
copy.y = y;
copy.rotation = rotation;
copy.block = block;
copy.breaking = breaking;
copy.hasConfig = hasConfig;
copy.config = config;
copy.originalX = originalX;
copy.originalY = originalY;
copy.progress = progress;
copy.initialized = initialized;
copy.animScale = animScale;
return copy;
}
public BuildRequest original(int x, int y, int originalWidth, int originalHeight){
originalX = x;
originalY = y;
this.originalWidth = originalWidth;
this.originalHeight = originalHeight;
return this;
}
public Rectangle bounds(Rectangle rect){
if(breaking){
return rect.set(-100f, -100f, 0f, 0f);
}else{
return block.bounds(x, y, rect);
}
}
public BuildRequest set(int x, int y, int rotation, Block block){
this.x = x;
this.y = y;
this.rotation = rotation;
this.block = block;
this.breaking = false;
return this;
}
public float drawx(){
return x*tilesize + block.offset();
}
public float drawy(){
return y*tilesize + block.offset();
}
public BuildRequest configure(int config){
this.config = config;
this.hasConfig = true;
return this;
}
public @Nullable Tile tile(){
return world.tile(x, y);
}
@Override
public String toString(){
return "BuildRequest{" +
"x=" + x +
", y=" + y +
", rotation=" + rotation +
", recipe=" + block +
", breaking=" + breaking +
", progress=" + progress +
", initialized=" + initialized +
'}';
}
}
}

View File

@@ -0,0 +1,9 @@
package mindustry.entities.traits;
public interface DamageTrait{
float damage();
default void killed(Entity other){
}
}

View File

@@ -0,0 +1,10 @@
package mindustry.entities.traits;
public interface DrawTrait extends Entity{
default float drawSize(){
return 20f;
}
void draw();
}

View File

@@ -0,0 +1,42 @@
package mindustry.entities.traits;
import mindustry.entities.EntityGroup;
public interface Entity extends MoveTrait{
int getID();
void resetID(int id);
default void update(){}
default void removed(){}
default void added(){}
EntityGroup targetGroup();
@SuppressWarnings("unchecked")
default void add(){
if(targetGroup() != null){
targetGroup().add(this);
}
}
@SuppressWarnings("unchecked")
default void remove(){
if(getGroup() != null){
getGroup().remove(this);
}
setGroup(null);
}
EntityGroup getGroup();
void setGroup(EntityGroup group);
default boolean isAdded(){
return getGroup() != null;
}
}

View File

@@ -0,0 +1,52 @@
package mindustry.entities.traits;
import arc.math.Mathf;
public interface HealthTrait{
void health(float health);
float health();
float maxHealth();
boolean isDead();
void setDead(boolean dead);
default void onHit(SolidTrait entity){
}
default void onDeath(){
}
default boolean damaged(){
return health() < maxHealth() - 0.0001f;
}
default void damage(float amount){
health(health() - amount);
if(health() <= 0 && !isDead()){
onDeath();
setDead(true);
}
}
default void clampHealth(){
health(Mathf.clamp(health(), 0, maxHealth()));
}
default float healthf(){
return health() / maxHealth();
}
default void healBy(float amount){
health(health() + amount);
clampHealth();
}
default void heal(){
health(maxHealth());
setDead(false);
}
}

View File

@@ -0,0 +1,5 @@
package mindustry.entities.traits;
public interface KillerTrait{
void killed(Entity other);
}

View File

@@ -0,0 +1,102 @@
package mindustry.entities.traits;
import arc.Core;
import arc.graphics.Color;
import arc.graphics.g2d.*;
import arc.math.*;
import arc.util.Time;
import mindustry.content.*;
import mindustry.entities.Effects;
import mindustry.entities.type.*;
import mindustry.gen.Call;
import mindustry.graphics.*;
import mindustry.type.Item;
import mindustry.world.Tile;
import static mindustry.Vars.*;
public interface MinerTrait extends Entity{
/** Returns the range at which this miner can mine blocks.*/
default float getMiningRange(){
return 70f;
}
default boolean isMining(){
return getMineTile() != null;
}
/** Returns the tile this builder is currently mining. */
Tile getMineTile();
/** Sets the tile this builder is currently mining. */
void setMineTile(Tile tile);
/** Returns the mining speed of this miner. 1 = standard, 0.5 = half speed, 2 = double speed, etc. */
float getMinePower();
/** Returns whether or not this builder can mine a specific item type. */
boolean canMine(Item item);
default void updateMining(){
Unit unit = (Unit)this;
Tile tile = getMineTile();
TileEntity core = unit.getClosestCore();
if(tile == null || core == null || tile.block() != Blocks.air || dst(tile.worldx(), tile.worldy()) > getMiningRange()
|| tile.drop() == null || !unit.acceptsItem(tile.drop()) || !canMine(tile.drop())){
setMineTile(null);
}else{
Item item = tile.drop();
unit.rotation = Mathf.slerpDelta(unit.rotation, unit.angleTo(tile.worldx(), tile.worldy()), 0.4f);
if(Mathf.chance(Time.delta() * (0.06 - item.hardness * 0.01) * getMinePower())){
if(unit.dst(core) < mineTransferRange && core.tile.block().acceptStack(item, 1, core.tile, unit) == 1){
Call.transferItemTo(item, 1,
tile.worldx() + Mathf.range(tilesize / 2f),
tile.worldy() + Mathf.range(tilesize / 2f), core.tile);
}else if(unit.acceptsItem(item)){
Call.transferItemToUnit(item,
tile.worldx() + Mathf.range(tilesize / 2f),
tile.worldy() + Mathf.range(tilesize / 2f),
unit);
}
}
if(Mathf.chance(0.06 * Time.delta())){
Effects.effect(Fx.pulverizeSmall,
tile.worldx() + Mathf.range(tilesize / 2f),
tile.worldy() + Mathf.range(tilesize / 2f), 0f, item.color);
}
}
}
default void drawMining(){
Unit unit = (Unit)this;
Tile tile = getMineTile();
if(tile == null) return;
float focusLen = 4f + Mathf.absin(Time.time(), 1.1f, 0.5f);
float swingScl = 12f, swingMag = tilesize / 8f;
float flashScl = 0.3f;
float px = unit.x + Angles.trnsx(unit.rotation, focusLen);
float py = unit.y + Angles.trnsy(unit.rotation, focusLen);
float ex = tile.worldx() + Mathf.sin(Time.time() + 48, swingScl, swingMag);
float ey = tile.worldy() + Mathf.sin(Time.time() + 48, swingScl + 2f, swingMag);
Draw.color(Color.lightGray, Color.white, 1f - flashScl + Mathf.absin(Time.time(), 0.5f, flashScl));
Drawf.laser(Core.atlas.find("minelaser"), Core.atlas.find("minelaser-end"), px, py, ex, ey, 0.75f);
if(unit instanceof Player && ((Player)unit).isLocal){
Lines.stroke(1f, Pal.accent);
Lines.poly(tile.worldx(), tile.worldy(), 4, tilesize / 2f * Mathf.sqrt2, Time.time());
}
Draw.color();
}
}

View File

@@ -0,0 +1,20 @@
package mindustry.entities.traits;
import arc.math.geom.Position;
public interface MoveTrait extends Position{
void setX(float x);
void setY(float y);
default void moveBy(float x, float y){
setX(getX() + x);
setY(getY() + y);
}
default void set(float x, float y){
setX(x);
setY(y);
}
}

View File

@@ -0,0 +1,8 @@
package mindustry.entities.traits;
/**
* Marks an entity as serializable.
*/
public interface SaveTrait extends Entity, TypeTrait, Saveable{
byte version();
}

View File

@@ -0,0 +1,8 @@
package mindustry.entities.traits;
import java.io.*;
public interface Saveable{
void writeSave(DataOutput stream) throws IOException;
void readSave(DataInput stream, byte version) throws IOException;
}

View File

@@ -0,0 +1,43 @@
package mindustry.entities.traits;
import arc.math.Interpolation;
public interface ScaleTrait{
/** 0 to 1. */
float fin();
/** 1 to 0 */
default float fout(){
return 1f - fin();
}
/** 1 to 0 */
default float fout(Interpolation i){
return i.apply(fout());
}
/** 1 to 0, ending at the specified margin */
default float fout(float margin){
float f = fin();
if(f >= 1f - margin){
return 1f - (f - (1f - margin)) / margin;
}else{
return 1f;
}
}
/** 0 to 1 **/
default float fin(Interpolation i){
return i.apply(fin());
}
/** 0 to 1 */
default float finpow(){
return Interpolation.pow3Out.apply(fin());
}
/** 0 to 1 to 0 */
default float fslope(){
return (0.5f - Math.abs(fin() - 0.5f)) * 2f;
}
}

View File

@@ -0,0 +1,13 @@
package mindustry.entities.traits;
import arc.util.Interval;
import mindustry.type.Weapon;
public interface ShooterTrait extends VelocityTrait, TeamTrait{
Interval getTimer();
int getShootTimer(boolean left);
Weapon getWeapon();
}

View File

@@ -0,0 +1,38 @@
package mindustry.entities.traits;
import arc.math.geom.*;
import arc.math.geom.QuadTree.QuadTreeObject;
import mindustry.Vars;
public interface SolidTrait extends QuadTreeObject, MoveTrait, VelocityTrait, Entity, Position{
void hitbox(Rectangle rectangle);
void hitboxTile(Rectangle rectangle);
Vector2 lastPosition();
default boolean collidesGrid(int x, int y){
return true;
}
default float getDeltaX(){
return getX() - lastPosition().x;
}
default float getDeltaY(){
return getY() - lastPosition().y;
}
default boolean collides(SolidTrait other){
return true;
}
default void collision(SolidTrait other, float x, float y){
}
default void move(float x, float y){
Vars.collisions.move(this, x, y);
}
}

View File

@@ -0,0 +1,18 @@
package mindustry.entities.traits;
import arc.math.geom.Position;
import mindustry.entities.type.*;
import mindustry.world.Tile;
public interface SpawnerTrait extends TargetTrait, Position{
Tile getTile();
void updateSpawning(Player unit);
boolean hasUnit(Unit unit);
@Override
default boolean isValid(){
return getTile().entity instanceof SpawnerTrait;
}
}

View File

@@ -0,0 +1,48 @@
package mindustry.entities.traits;
import mindustry.net.Interpolator;
import java.io.*;
public interface SyncTrait extends Entity, TypeTrait{
/** Sets the position of this entity and updated the interpolator. */
default void setNet(float x, float y){
set(x, y);
if(getInterpolator() != null){
getInterpolator().target.set(x, y);
getInterpolator().last.set(x, y);
getInterpolator().pos.set(0, 0);
getInterpolator().updateSpacing = 16;
getInterpolator().lastUpdated = 0;
}
}
/** Interpolate entity position only. Override if you need to interpolate rotations or other values. */
default void interpolate(){
if(getInterpolator() == null){
throw new RuntimeException("This entity must have an interpolator to interpolate()!");
}
getInterpolator().update();
setX(getInterpolator().pos.x);
setY(getInterpolator().pos.y);
}
/** Return the interpolator used for smoothing the position. Optional. */
default Interpolator getInterpolator(){
return null;
}
/** Whether syncing is enabled for this entity; true by default. */
default boolean isSyncing(){
return true;
}
//Read and write sync data, usually position
void write(DataOutput data) throws IOException;
void read(DataInput data) throws IOException;
}

View File

@@ -0,0 +1,35 @@
package mindustry.entities.traits;
import arc.math.geom.Position;
import mindustry.game.Team;
/**
* Base interface for targetable entities.
*/
public interface TargetTrait extends Position, VelocityTrait{
boolean isDead();
Team getTeam();
default float getTargetVelocityX(){
if(this instanceof SolidTrait){
return ((SolidTrait)this).getDeltaX();
}
return velocity().x;
}
default float getTargetVelocityY(){
if(this instanceof SolidTrait){
return ((SolidTrait)this).getDeltaY();
}
return velocity().y;
}
/**
* Whether this entity is a valid target.
*/
default boolean isValid(){
return !isDead();
}
}

View File

@@ -0,0 +1,7 @@
package mindustry.entities.traits;
import mindustry.game.Team;
public interface TeamTrait extends Entity{
Team getTeam();
}

View File

@@ -0,0 +1,23 @@
package mindustry.entities.traits;
import arc.math.Mathf;
import arc.util.Time;
public interface TimeTrait extends ScaleTrait, Entity{
float lifetime();
void time(float time);
float time();
default void updateTime(){
time(Mathf.clamp(time() + Time.delta(), 0, lifetime()));
if(time() >= lifetime()){
remove();
}
}
//fin() is not implemented due to compiler issues with iOS/RoboVM
}

View File

@@ -0,0 +1,45 @@
package mindustry.entities.traits;
import mindustry.type.TypeID;
public interface TypeTrait{
TypeID getTypeID();
/*
int[] lastRegisteredID = {0};
Array<Supplier<? extends TypeTrait>> registeredTypes = new Array<>();
ObjectIntMap<Class<? extends TypeTrait>> typeToID = new ObjectIntMap<>();
/**
* Register and return a type ID. The supplier should return a fresh instace of that type.
static <T extends TypeTrait> void registerType(Class<T> type, Supplier<T> supplier){
if(typeToID.get(type, -1) != -1){
return; //already registered
}
registeredTypes.add(supplier);
int result = lastRegisteredID[0];
typeToID.put(type, result);
lastRegisteredID[0]++;
}
/**Gets a syncable type by ID.
static Supplier<? extends TypeTrait> getTypeByID(int id){
if(id == -1){
throw new IllegalArgumentException("Attempt to retrieve invalid entity type ID! Did you forget to set it in ContentLoader.registerTypes()?");
}
return registeredTypes.get(id);
}
/**
* Returns the type ID of this entity used for intstantiation. Should be < BYTE_MAX.
* Do not override!
default int getTypeID(){
int id = typeToID.get(getClass(), -1);
if(id == -1)
throw new RuntimeException("Class of type '" + getClass() + "' is not registered! Did you forget to register it in ContentLoader#registerTypes()?");
return id;
}*/
}

View File

@@ -0,0 +1,36 @@
package mindustry.entities.traits;
import arc.math.geom.Vector2;
import arc.util.Time;
public interface VelocityTrait extends MoveTrait{
Vector2 velocity();
default void applyImpulse(float x, float y){
velocity().x += x / mass();
velocity().y += y / mass();
}
default float maxVelocity(){
return Float.MAX_VALUE;
}
default float mass(){
return 1f;
}
default float drag(){
return 0f;
}
default void updateVelocity(){
velocity().scl(1f - drag() * Time.delta());
if(this instanceof SolidTrait){
((SolidTrait)this).move(velocity().x * Time.delta(), velocity().y * Time.delta());
}else{
moveBy(velocity().x * Time.delta(), velocity().y * Time.delta());
}
}
}

View File

@@ -0,0 +1,75 @@
package mindustry.entities.type;
import mindustry.*;
import mindustry.entities.EntityGroup;
import mindustry.entities.traits.Entity;
public abstract class BaseEntity implements Entity{
private static int lastid;
/** Do not modify. Used for network operations and mapping. */
public int id;
public float x, y;
protected transient EntityGroup group;
public BaseEntity(){
id = lastid++;
}
public int tileX(){
return Vars.world.toTile(x);
}
public int tileY(){
return Vars.world.toTile(y);
}
@Override
public int getID(){
return id;
}
@Override
public void resetID(int id){
this.id = id;
}
@Override
public EntityGroup getGroup(){
return group;
}
@Override
public void setGroup(EntityGroup group){
this.group = group;
}
@Override
public float getX(){
return x;
}
@Override
public void setX(float x){
this.x = x;
}
@Override
public float getY(){
return y;
}
@Override
public void setY(float y){
this.y = y;
}
@Override
public String toString(){
return getClass() + " " + id;
}
/** Increments this entity's ID. Used for pooled entities.*/
public void incrementID(){
id = lastid++;
}
}

View File

@@ -0,0 +1,419 @@
package mindustry.entities.type;
import arc.*;
import mindustry.annotations.Annotations.*;
import arc.graphics.g2d.*;
import arc.math.*;
import arc.math.geom.*;
import arc.util.*;
import arc.util.ArcAnnotate.*;
import mindustry.*;
import mindustry.content.*;
import mindustry.ctype.ContentType;
import mindustry.entities.*;
import mindustry.entities.traits.*;
import mindustry.entities.units.*;
import mindustry.game.EventType.*;
import mindustry.game.*;
import mindustry.gen.*;
import mindustry.type.*;
import mindustry.type.TypeID;
import mindustry.ui.Cicon;
import mindustry.world.*;
import mindustry.world.blocks.*;
import mindustry.world.blocks.defense.DeflectorWall.*;
import mindustry.world.blocks.units.CommandCenter.*;
import mindustry.world.blocks.units.UnitFactory.*;
import mindustry.world.meta.*;
import java.io.*;
import static mindustry.Vars.*;
/** Base class for AI units. */
public abstract class BaseUnit extends Unit implements ShooterTrait{
protected static int timerIndex = 0;
protected static final int timerTarget = timerIndex++;
protected static final int timerTarget2 = timerIndex++;
protected static final int timerShootLeft = timerIndex++;
protected static final int timerShootRight = timerIndex++;
protected boolean loaded;
protected UnitType type;
protected Interval timer = new Interval(5);
protected StateMachine state = new StateMachine();
protected TargetTrait target;
protected int spawner = noSpawner;
/** internal constructor used for deserialization, DO NOT USE */
public BaseUnit(){
}
@Remote(called = Loc.server)
public static void onUnitDeath(BaseUnit unit){
if(unit == null) return;
if(net.server() || !net.active()){
UnitDrops.dropItems(unit);
}
unit.onSuperDeath();
unit.type.deathSound.at(unit);
//visual only.
if(net.client()){
Tile tile = world.tile(unit.spawner);
if(tile != null){
tile.block().unitRemoved(tile, unit);
}
unit.spawner = noSpawner;
}
//must run afterwards so the unit's group is not null when sending the removal packet
Core.app.post(unit::remove);
}
@Override
public float drag(){
return type.drag;
}
@Override
public TypeID getTypeID(){
return type.typeID;
}
@Override
public void onHit(SolidTrait entity){
if(entity instanceof Bullet && ((Bullet)entity).getOwner() instanceof DeflectorEntity && player != null && getTeam() != player.getTeam()){
Core.app.post(() -> {
if(isDead()){
Events.fire(Trigger.phaseDeflectHit);
}
});
}
}
public @Nullable
Tile getSpawner(){
return world.tile(spawner);
}
public boolean isCommanded(){
return indexer.getAllied(team, BlockFlag.comandCenter).size != 0 && indexer.getAllied(team, BlockFlag.comandCenter).first().entity instanceof CommandCenterEntity;
}
public @Nullable UnitCommand getCommand(){
if(isCommanded()){
return indexer.getAllied(team, BlockFlag.comandCenter).first().<CommandCenterEntity>ent().command;
}
return null;
}
/**Called when a command is recieved from the command center.*/
public void onCommand(UnitCommand command){
}
/** Initialize the type and team of this unit. Only call once! */
public void init(UnitType type, Team team){
if(this.type != null) throw new RuntimeException("This unit is already initialized!");
this.type = type;
this.team = team;
}
public UnitType getType(){
return type;
}
public void setSpawner(Tile tile){
this.spawner = tile.pos();
}
public void rotate(float angle){
rotation = Mathf.slerpDelta(rotation, angle, type.rotatespeed);
}
public boolean targetHasFlag(BlockFlag flag){
return (target instanceof TileEntity && ((TileEntity)target).tile.block().flags.contains(flag)) ||
(target instanceof Tile && ((Tile)target).block().flags.contains(flag));
}
public void setState(UnitState state){
this.state.set(state);
}
public boolean retarget(){
return timer.get(timerTarget, 20);
}
/** Only runs when the unit has a target. */
public void behavior(){
}
public void updateTargeting(){
if(target == null || (target instanceof Unit && (target.isDead() || target.getTeam() == team))
|| (target instanceof TileEntity && ((TileEntity)target).tile.entity == null)){
target = null;
}
}
public void targetClosestAllyFlag(BlockFlag flag){
Tile target = Geometry.findClosest(x, y, indexer.getAllied(team, flag));
if(target != null) this.target = target.entity;
}
public void targetClosestEnemyFlag(BlockFlag flag){
Tile target = Geometry.findClosest(x, y, indexer.getEnemy(team, flag));
if(target != null) this.target = target.entity;
}
public void targetClosest(){
TargetTrait newTarget = Units.closestTarget(team, x, y, Math.max(getWeapon().bullet.range(), type.range), u -> type.targetAir || !u.isFlying());
if(newTarget != null){
target = newTarget;
}
}
public Tile getClosest(BlockFlag flag){
return Geometry.findClosest(x, y, indexer.getAllied(team, flag));
}
public Tile getClosestSpawner(){
return Geometry.findClosest(x, y, Vars.spawner.getGroundSpawns());
}
public TileEntity getClosestEnemyCore(){
for(Team enemy : Vars.state.teams.enemiesOf(team)){
Tile tile = Geometry.findClosest(x, y, Vars.state.teams.get(enemy).cores);
if(tile != null){
return tile.entity;
}
}
return null;
}
public UnitState getStartState(){
return null;
}
public boolean isBoss(){
return hasEffect(StatusEffects.boss);
}
@Override
public float getDamageMultipler(){
return status.getDamageMultiplier() * Vars.state.rules.unitDamageMultiplier;
}
@Override
public boolean isImmune(StatusEffect effect){
return type.immunities.contains(effect);
}
@Override
public boolean isValid(){
return super.isValid() && isAdded();
}
@Override
public Interval getTimer(){
return timer;
}
@Override
public int getShootTimer(boolean left){
return left ? timerShootLeft : timerShootRight;
}
@Override
public Weapon getWeapon(){
return type.weapon;
}
@Override
public TextureRegion getIconRegion(){
return type.icon(Cicon.full);
}
@Override
public int getItemCapacity(){
return type.itemCapacity;
}
@Override
public void interpolate(){
super.interpolate();
if(interpolator.values.length > 0){
rotation = interpolator.values[0];
}
}
@Override
public float maxHealth(){
return type.health * Vars.state.rules.unitHealthMultiplier;
}
@Override
public float mass(){
return type.mass;
}
@Override
public boolean isFlying(){
return type.flying;
}
@Override
public void update(){
if(isDead()){
//dead enemies should get immediately removed
remove();
return;
}
hitTime -= Time.delta();
if(net.client()){
interpolate();
status.update(this);
return;
}
if(!isFlying() && (world.tileWorld(x, y) != null && !(world.tileWorld(x, y).block() instanceof BuildBlock) && world.tileWorld(x, y).solid())){
kill();
}
avoidOthers();
if(spawner != noSpawner && (world.tile(spawner) == null || !(world.tile(spawner).entity instanceof UnitFactoryEntity))){
kill();
}
updateTargeting();
state.update();
updateVelocityStatus();
if(target != null) behavior();
if(!isFlying()){
clampPosition();
}
}
@Override
public void draw(){
}
@Override
public float maxVelocity(){
return type.maxVelocity;
}
@Override
public void removed(){
super.removed();
Tile tile = world.tile(spawner);
if(tile != null && !net.client()){
tile.block().unitRemoved(tile, this);
}
spawner = noSpawner;
}
@Override
public float drawSize(){
return type.hitsize * 10;
}
@Override
public void onDeath(){
Call.onUnitDeath(this);
}
@Override
public void added(){
state.set(getStartState());
if(!loaded){
health(maxHealth());
}
if(isCommanded()){
onCommand(getCommand());
}
}
@Override
public void hitbox(Rectangle rectangle){
rectangle.setSize(type.hitsize).setCenter(x, y);
}
@Override
public void hitboxTile(Rectangle rectangle){
rectangle.setSize(type.hitsizeTile).setCenter(x, y);
}
@Override
public EntityGroup targetGroup(){
return unitGroups[team.ordinal()];
}
@Override
public byte version(){
return 0;
}
@Override
public void writeSave(DataOutput stream) throws IOException{
super.writeSave(stream);
stream.writeByte(type.id);
stream.writeInt(spawner);
}
@Override
public void readSave(DataInput stream, byte version) throws IOException{
super.readSave(stream, version);
loaded = true;
byte type = stream.readByte();
this.spawner = stream.readInt();
this.type = content.getByID(ContentType.unit, type);
add();
}
@Override
public void write(DataOutput data) throws IOException{
super.writeSave(data);
data.writeByte(type.id);
data.writeInt(spawner);
}
@Override
public void read(DataInput data) throws IOException{
float lastx = x, lasty = y, lastrot = rotation;
super.readSave(data, version());
this.type = content.getByID(ContentType.unit, data.readByte());
this.spawner = data.readInt();
interpolator.read(lastx, lasty, x, y, rotation);
rotation = lastrot;
x = lastx;
y = lasty;
}
public void onSuperDeath(){
super.onDeath();
}
}

View File

@@ -0,0 +1,323 @@
package mindustry.entities.type;
import mindustry.annotations.Annotations.*;
import arc.math.*;
import arc.math.geom.*;
import arc.util.*;
import arc.util.pooling.Pool.*;
import arc.util.pooling.*;
import mindustry.entities.*;
import mindustry.entities.bullet.*;
import mindustry.entities.effect.*;
import mindustry.entities.traits.*;
import mindustry.game.*;
import mindustry.graphics.*;
import mindustry.world.*;
import static mindustry.Vars.*;
public class Bullet extends SolidEntity implements DamageTrait, ScaleTrait, Poolable, DrawTrait, VelocityTrait, TimeTrait, TeamTrait, AbsorbTrait{
public Interval timer = new Interval(3);
private float lifeScl;
private Team team;
private Object data;
private boolean supressCollision, supressOnce, initialized, deflected;
protected BulletType type;
protected Entity owner;
protected float time;
/** Internal use only! */
public Bullet(){
}
public static Bullet create(BulletType type, TeamTrait owner, float x, float y, float angle){
return create(type, owner, owner.getTeam(), x, y, angle);
}
public static Bullet create(BulletType type, Entity owner, Team team, float x, float y, float angle){
return create(type, owner, team, x, y, angle, 1f);
}
public static Bullet create(BulletType type, Entity owner, Team team, float x, float y, float angle, float velocityScl){
return create(type, owner, team, x, y, angle, velocityScl, 1f, null);
}
public static Bullet create(BulletType type, Entity owner, Team team, float x, float y, float angle, float velocityScl, float lifetimeScl){
return create(type, owner, team, x, y, angle, velocityScl, lifetimeScl, null);
}
public static Bullet create(BulletType type, Entity owner, Team team, float x, float y, float angle, float velocityScl, float lifetimeScl, Object data){
Bullet bullet = Pools.obtain(Bullet.class, Bullet::new);
bullet.type = type;
bullet.owner = owner;
bullet.data = data;
bullet.velocity.set(0, type.speed).setAngle(angle).scl(velocityScl);
if(type.keepVelocity){
bullet.velocity.add(owner instanceof VelocityTrait ? ((VelocityTrait)owner).velocity() : Vector2.ZERO);
}
bullet.team = team;
bullet.type = type;
bullet.lifeScl = lifetimeScl;
bullet.set(x - bullet.velocity.x * Time.delta(), y - bullet.velocity.y * Time.delta());
bullet.add();
return bullet;
}
public static Bullet create(BulletType type, Bullet parent, float x, float y, float angle){
return create(type, parent.owner, parent.team, x, y, angle);
}
public static Bullet create(BulletType type, Bullet parent, float x, float y, float angle, float velocityScl){
return create(type, parent.owner, parent.team, x, y, angle, velocityScl);
}
@Remote(called = Loc.server, unreliable = true)
public static void createBullet(BulletType type, Team team, float x, float y, float angle, float velocityScl, float lifetimeScl){
create(type, null, team, x, y, angle, velocityScl, lifetimeScl, null);
}
public Entity getOwner(){
return owner;
}
public boolean collidesTiles(){
return type.collidesTiles;
}
public void deflect(){
supressCollision = true;
supressOnce = true;
deflected = true;
}
public boolean isDeflected(){
return deflected;
}
public BulletType getBulletType(){
return type;
}
public void resetOwner(Entity entity, Team team){
this.owner = entity;
this.team = team;
}
public void scaleTime(float add){
time += add;
}
public Object getData(){
return data;
}
public void setData(Object data){
this.data = data;
}
public float damageMultiplier(){
if(owner instanceof Unit){
return ((Unit)owner).getDamageMultipler();
}
return 1f;
}
@Override
public void killed(Entity other){
if(owner instanceof KillerTrait){
((KillerTrait)owner).killed(other);
}
}
@Override
public void absorb(){
supressCollision = true;
remove();
}
@Override
public float drawSize(){
return type.drawSize;
}
@Override
public float damage(){
if(owner instanceof Lightning && data instanceof Float){
return (Float)data;
}
return type.damage * damageMultiplier();
}
@Override
public Team getTeam(){
return team;
}
@Override
public float getShieldDamage(){
return Math.max(damage(), type.splashDamage);
}
@Override
public boolean collides(SolidTrait other){
return type.collides && (other != owner && !(other instanceof DamageTrait)) && !supressCollision && !(other instanceof Unit && ((Unit)other).isFlying() && !type.collidesAir);
}
@Override
public void collision(SolidTrait other, float x, float y){
if(!type.pierce) remove();
type.hit(this, x, y);
if(other instanceof Unit){
Unit unit = (Unit)other;
unit.velocity().add(Tmp.v3.set(other.getX(), other.getY()).sub(x, y).setLength(type.knockback / unit.mass()));
unit.applyEffect(type.status, type.statusDuration);
}
}
@Override
public void update(){
type.update(this);
x += velocity.x * Time.delta();
y += velocity.y * Time.delta();
velocity.scl(Mathf.clamp(1f - type.drag * Time.delta()));
time += Time.delta() * 1f / (lifeScl);
time = Mathf.clamp(time, 0, type.lifetime);
if(time >= type.lifetime){
if(!supressCollision) type.despawned(this);
remove();
}
if(type.hitTiles && collidesTiles() && !supressCollision && initialized){
world.raycastEach(world.toTile(lastPosition().x), world.toTile(lastPosition().y), world.toTile(x), world.toTile(y), (x, y) -> {
Tile tile = world.ltile(x, y);
if(tile == null) return false;
if(tile.entity != null && tile.entity.collide(this) && type.collides(this, tile) && !tile.entity.isDead() && (type.collidesTeam || tile.getTeam() != team)){
if(tile.getTeam() != team){
tile.entity.collision(this);
}
if(!supressCollision){
type.hitTile(this, tile);
remove();
}
return true;
}
return false;
});
}
if(supressOnce){
supressCollision = false;
supressOnce = false;
}
initialized = true;
}
@Override
public void reset(){
type = null;
owner = null;
velocity.setZero();
time = 0f;
timer.clear();
lifeScl = 1f;
team = null;
data = null;
supressCollision = false;
supressOnce = false;
deflected = false;
initialized = false;
}
@Override
public void hitbox(Rectangle rectangle){
rectangle.setSize(type.hitSize).setCenter(x, y);
}
@Override
public void hitboxTile(Rectangle rectangle){
rectangle.setSize(type.hitSize).setCenter(x, y);
}
@Override
public float lifetime(){
return type.lifetime;
}
@Override
public void time(float time){
this.time = time;
}
@Override
public float time(){
return time;
}
@Override
public void removed(){
Pools.free(this);
}
@Override
public EntityGroup targetGroup(){
return bulletGroup;
}
@Override
public void added(){
type.init(this);
}
@Override
public void draw(){
type.draw(this);
renderer.lights.add(x, y, 16f, Pal.powerLight, 0.3f);
}
@Override
public float fin(){
return time / type.lifetime;
}
@Override
public Vector2 velocity(){
return velocity;
}
public void velocity(float speed, float angle){
velocity.set(0, speed).setAngle(angle);
}
public void limit(float f){
velocity.limit(f);
}
/** Sets the bullet's rotation in degrees. */
public void rot(float angle){
velocity.setAngle(angle);
}
/** @return the bullet's rotation. */
public float rot(){
float angle = Mathf.atan2(velocity.x, velocity.y) * Mathf.radiansToDegrees;
if(angle < 0) angle += 360;
return angle;
}
}

View File

@@ -0,0 +1,47 @@
package mindustry.entities.type;
import mindustry.entities.traits.*;
public abstract class DestructibleEntity extends SolidEntity implements HealthTrait{
public transient boolean dead;
public float health;
@Override
public boolean collides(SolidTrait other){
return other instanceof DamageTrait;
}
@Override
public void collision(SolidTrait other, float x, float y){
if(other instanceof DamageTrait){
boolean wasDead = isDead();
onHit(other);
damage(((DamageTrait)other).damage());
if(!wasDead && isDead()){
((DamageTrait)other).killed(this);
}
}
}
@Override
public void health(float health){
this.health = health;
}
@Override
public float health(){
return health;
}
@Override
public boolean isDead(){
return dead;
}
@Override
public void setDead(boolean dead){
this.dead = dead;
}
}

View File

@@ -0,0 +1,81 @@
package mindustry.entities.type;
import arc.graphics.Color;
import arc.util.pooling.Pool.Poolable;
import arc.util.pooling.Pools;
import mindustry.entities.Effects;
import mindustry.entities.Effects.Effect;
import mindustry.entities.EntityGroup;
import mindustry.entities.traits.DrawTrait;
import mindustry.entities.traits.Entity;
import static mindustry.Vars.effectGroup;
public class EffectEntity extends TimedEntity implements Poolable, DrawTrait{
public Effect effect;
public Color color = new Color(Color.white);
public Object data;
public float rotation = 0f;
public Entity parent;
public float poffsetx, poffsety;
/** For pooling use only! */
public EffectEntity(){
}
public void setParent(Entity parent){
this.parent = parent;
this.poffsetx = x - parent.getX();
this.poffsety = y - parent.getY();
}
@Override
public EntityGroup targetGroup(){
//this should never actually be called
return effectGroup;
}
@Override
public float lifetime(){
return effect.lifetime;
}
@Override
public float drawSize(){
return effect.size;
}
@Override
public void update(){
if(effect == null){
remove();
return;
}
super.update();
if(parent != null){
x = parent.getX() + poffsetx;
y = parent.getY() + poffsety;
}
}
@Override
public void reset(){
effect = null;
color.set(Color.white);
rotation = time = poffsetx = poffsety = 0f;
parent = null;
data = null;
}
@Override
public void draw(){
Effects.renderEffect(id, effect, color, time, rotation, x, y, data);
}
@Override
public void removed(){
Pools.free(this);
}
}

View File

@@ -0,0 +1,953 @@
package mindustry.entities.type;
import arc.*;
import mindustry.annotations.Annotations.*;
import arc.struct.*;
import arc.graphics.*;
import arc.graphics.g2d.*;
import arc.math.*;
import arc.math.geom.*;
import arc.scene.ui.*;
import arc.scene.ui.layout.*;
import arc.util.*;
import arc.util.ArcAnnotate.*;
import arc.util.pooling.*;
import mindustry.*;
import mindustry.content.*;
import mindustry.core.*;
import mindustry.ctype.ContentType;
import mindustry.entities.*;
import mindustry.entities.traits.*;
import mindustry.game.*;
import mindustry.gen.*;
import mindustry.input.*;
import mindustry.io.*;
import mindustry.net.Administration.*;
import mindustry.net.*;
import mindustry.type.*;
import mindustry.ui.*;
import mindustry.world.*;
import mindustry.world.blocks.*;
import java.io.*;
import static mindustry.Vars.*;
public class Player extends Unit implements BuilderMinerTrait, ShooterTrait{
public static final int timerSync = 2;
public static final int timerAbility = 3;
public static final int timerTransfer = 4;
private static final int timerShootLeft = 0;
private static final int timerShootRight = 1;
private static final float liftoffBoost = 0.2f;
private static final Rectangle rect = new Rectangle();
//region instance variables
public float baseRotation;
public float pointerX, pointerY;
public String name = "noname";
public @Nullable
String uuid, usid;
public boolean isAdmin, isTransferring, isShooting, isBoosting, isMobile, isTyping, isBuilding = true;
public boolean buildWasAutoPaused = false;
public float boostHeat, shootHeat, destructTime;
public boolean achievedFlight;
public Color color = new Color();
public Mech mech = Mechs.starter;
public SpawnerTrait spawner, lastSpawner;
public int respawns;
public @Nullable NetConnection con;
public boolean isLocal = false;
public Interval timer = new Interval(6);
public TargetTrait target;
public TargetTrait moveTarget;
public @Nullable String lastText;
public float textFadeTime;
private float walktime, itemtime;
private Queue<BuildRequest> placeQueue = new Queue<>();
private Tile mining;
private Vector2 movement = new Vector2();
private boolean moved;
//endregion
//region unit and event overrides, utility methods
@Remote(targets = Loc.server, called = Loc.server)
public static void onPlayerDeath(Player player){
if(player == null) return;
player.dead = true;
player.placeQueue.clear();
player.onDeath();
}
@Override
public float getDamageMultipler(){
return status.getDamageMultiplier() * state.rules.playerDamageMultiplier;
}
@Override
public void hitbox(Rectangle rectangle){
rectangle.setSize(mech.hitsize).setCenter(x, y);
}
@Override
public void hitboxTile(Rectangle rectangle){
rectangle.setSize(mech.hitsize * 2f / 3f).setCenter(x, y);
}
@Override
public void onRespawn(Tile tile){
velocity.setZero();
boostHeat = 1f;
achievedFlight = true;
rotation = 90f;
baseRotation = 90f;
dead = false;
spawner = null;
respawns --;
Sounds.respawn.at(tile);
setNet(tile.drawx(), tile.drawy());
clearItem();
heal();
}
@Override
public TypeID getTypeID(){
return TypeIDs.player;
}
@Override
public void move(float x, float y){
if(!mech.flying){
collisions.move(this, x, y);
}else{
moveBy(x, y);
}
}
@Override
public float drag(){
return mech.drag;
}
@Override
public Interval getTimer(){
return timer;
}
@Override
public int getShootTimer(boolean left){
return left ? timerShootLeft : timerShootRight;
}
@Override
public Weapon getWeapon(){
return mech.weapon;
}
@Override
public float getMinePower(){
return mech.mineSpeed;
}
@Override
public TextureRegion getIconRegion(){
return mech.icon(Cicon.full);
}
@Override
public int getItemCapacity(){
return mech.itemCapacity;
}
@Override
public void interpolate(){
super.interpolate();
if(interpolator.values.length > 1){
baseRotation = interpolator.values[1];
}
if(interpolator.target.dst(interpolator.last) > 1f){
walktime += Time.delta();
}
}
@Override
public float getBuildPower(Tile tile){
return mech.buildPower;
}
@Override
public float maxHealth(){
return mech.health * state.rules.playerHealthMultiplier;
}
@Override
public Tile getMineTile(){
return mining;
}
@Override
public void setMineTile(Tile tile){
this.mining = tile;
}
@Override
public boolean canMine(Item item){
return item.hardness <= mech.drillPower;
}
@Override
public float calculateDamage(float amount){
return amount * Mathf.clamp(1f - (status.getArmorMultiplier() + mech.getExtraArmor(this)) / 100f);
}
@Override
public void added(){
baseRotation = 90f;
}
@Override
public float mass(){
return mech.mass;
}
@Override
public boolean isFlying(){
return mech.flying || boostHeat > liftoffBoost;
}
@Override
public void damage(float amount){
hitTime = hitDuration;
if(!net.client()){
health -= calculateDamage(amount);
}
if(health <= 0 && !dead){
Call.onPlayerDeath(this);
}
}
@Override
public void set(float x, float y){
this.x = x;
this.y = y;
}
@Override
public float maxVelocity(){
return mech.maxSpeed;
}
@Override
public Queue<BuildRequest> buildQueue(){
return placeQueue;
}
@Override
public String toString(){
return "Player{" + name + ", mech=" + mech.name + ", id=" + id + ", local=" + isLocal + ", " + x + ", " + y + "}";
}
@Override
public EntityGroup targetGroup(){
return playerGroup;
}
public void setTeam(Team team){
this.team = team;
}
//endregion
//region draw methods
@Override
public float drawSize(){
return isLocal ? Float.MAX_VALUE : 40 + placeDistance;
}
@Override
public void drawShadow(float offsetX, float offsetY){
float scl = mech.flying ? 1f : boostHeat / 2f;
Draw.rect(getIconRegion(), x + offsetX * scl, y + offsetY * scl, rotation - 90);
}
@Override
public void draw(){
if(dead) return;
if(!movement.isZero() && moved && !state.isPaused()){
walktime += movement.len() * getFloorOn().speedMultiplier * 2f;
baseRotation = Mathf.slerpDelta(baseRotation, movement.angle(), 0.13f);
}
float ft = Mathf.sin(walktime, 6f, 2f) * (1f - boostHeat);
Floor floor = getFloorOn();
Draw.color();
Draw.mixcol(Color.white, hitTime / hitDuration);
if(!mech.flying){
if(floor.isLiquid){
Draw.color(Color.white, floor.color, 0.5f);
}
float boostTrnsY = -boostHeat * 3f;
float boostTrnsX = boostHeat * 3f;
float boostAng = boostHeat * 40f;
for(int i : Mathf.signs){
Draw.rect(mech.legRegion,
x + Angles.trnsx(baseRotation, ft * i + boostTrnsY, -boostTrnsX * i),
y + Angles.trnsy(baseRotation, ft * i + boostTrnsY, -boostTrnsX * i),
mech.legRegion.getWidth() * i * Draw.scl,
(mech.legRegion.getHeight() - Mathf.clamp(ft * i, 0, 2)) * Draw.scl,
baseRotation - 90 + boostAng * i);
}
Draw.rect(mech.baseRegion, x, y, baseRotation - 90);
}
if(floor.isLiquid){
Draw.color(Color.white, floor.color, drownTime);
}else{
Draw.color(Color.white);
}
Draw.rect(mech.region, x, y, rotation - 90);
mech.draw(this);
for(int i : Mathf.signs){
float tra = rotation - 90, trY = -mech.weapon.getRecoil(this, i > 0) + mech.weaponOffsetY;
float w = i > 0 ? -mech.weapon.region.getWidth() : mech.weapon.region.getWidth();
Draw.rect(mech.weapon.region,
x + Angles.trnsx(tra, (mech.weaponOffsetX + mech.spreadX(this)) * i, trY),
y + Angles.trnsy(tra, (mech.weaponOffsetX + mech.spreadX(this)) * i, trY),
w * Draw.scl,
mech.weapon.region.getHeight() * Draw.scl,
rotation - 90);
}
Draw.reset();
}
@Override
public void drawStats(){
Draw.color(Color.black, team.color, healthf() + Mathf.absin(Time.time(), healthf() * 5f, 1f - healthf()));
Draw.rect(getPowerCellRegion(), x + Angles.trnsx(rotation, mech.cellTrnsY, 0f), y + Angles.trnsy(rotation, mech.cellTrnsY, 0f), rotation - 90);
Draw.reset();
drawBackItems(itemtime, isLocal);
drawLight();
}
@Override
public void drawOver(){
if(dead) return;
if(isBuilding() && isBuilding){
if(!state.isPaused()){
drawBuilding();
}
}else{
drawMining();
}
}
@Override
public void drawUnder(){
if(dead) return;
float size = mech.engineSize * (mech.flying ? 1f : boostHeat);
Draw.color(mech.engineColor);
Fill.circle(x + Angles.trnsx(rotation + 180, mech.engineOffset), y + Angles.trnsy(rotation + 180, mech.engineOffset),
size + Mathf.absin(Time.time(), 2f, size / 4f));
Draw.color(Color.white);
Fill.circle(x + Angles.trnsx(rotation + 180, mech.engineOffset - 1f), y + Angles.trnsy(rotation + 180, mech.engineOffset - 1f),
(size + Mathf.absin(Time.time(), 2f, size / 4f)) / 2f);
Draw.color();
}
public void drawName(){
BitmapFont font = Fonts.def;
GlyphLayout layout = Pools.obtain(GlyphLayout.class, GlyphLayout::new);
final float nameHeight = 11;
final float textHeight = 15;
boolean ints = font.usesIntegerPositions();
font.setUseIntegerPositions(false);
font.getData().setScale(0.25f / Scl.scl(1f));
layout.setText(font, name);
if(!isLocal){
Draw.color(0f, 0f, 0f, 0.3f);
Fill.rect(x, y + nameHeight - layout.height / 2, layout.width + 2, layout.height + 3);
Draw.color();
font.setColor(color);
font.draw(name, x, y + nameHeight, 0, Align.center, false);
if(isAdmin){
float s = 3f;
Draw.color(color.r * 0.5f, color.g * 0.5f, color.b * 0.5f, 1f);
Draw.rect(Core.atlas.find("icon-admin-badge"), x + layout.width / 2f + 2 + 1, y + nameHeight - 1.5f, s, s);
Draw.color(color);
Draw.rect(Core.atlas.find("icon-admin-badge"), x + layout.width / 2f + 2 + 1, y + nameHeight - 1f, s, s);
}
}
if(Core.settings.getBool("playerchat") && ((textFadeTime > 0 && lastText != null) || isTyping)){
String text = textFadeTime <= 0 || lastText == null ? "[LIGHT_GRAY]" + Strings.animated(Time.time(), 4, 15f, ".") : lastText;
float width = 100f;
float visualFadeTime = 1f - Mathf.curve(1f - textFadeTime, 0.9f);
font.setColor(1f, 1f, 1f, textFadeTime <= 0 || lastText == null ? 1f : visualFadeTime);
layout.setText(font, text, Color.white, width, Align.bottom, true);
Draw.color(0f, 0f, 0f, 0.3f * (textFadeTime <= 0 || lastText == null ? 1f : visualFadeTime));
Fill.rect(x, y + textHeight + layout.height - layout.height/2f, layout.width + 2, layout.height + 3);
font.draw(text, x - width/2f, y + textHeight + layout.height, width, Align.center, true);
}
Draw.reset();
Pools.free(layout);
font.getData().setScale(1f);
font.setColor(Color.white);
font.setUseIntegerPositions(ints);
}
/** Draw all current build requests. Does not draw the beam effect, only the positions. */
public void drawBuildRequests(){
if(!isLocal) return;
for(BuildRequest request : buildQueue()){
if(request.progress > 0.01f || (buildRequest() == request && request.initialized && (dst(request.x * tilesize, request.y * tilesize) <= placeDistance || state.isEditor()))) continue;
request.animScale = 1f;
if(request.breaking){
control.input.drawBreaking(request);
}else{
request.block.drawRequest(request, control.input.allRequests(),
Build.validPlace(getTeam(), request.x, request.y, request.block, request.rotation) || control.input.requestMatches(request));
}
}
Draw.reset();
}
//endregion
//region update methods
@Override
public void updateMechanics(){
if(isBuilding){
updateBuilding();
}
//mine only when not building
if(buildRequest() == null || !isBuilding){
updateMining();
}
}
@Override
public void update(){
hitTime -= Time.delta();
textFadeTime -= Time.delta() / (60 * 5);
itemtime = Mathf.lerpDelta(itemtime, Mathf.num(item.amount > 0), 0.1f);
if(Float.isNaN(x) || Float.isNaN(y)){
velocity.set(0f, 0f);
x = 0;
y = 0;
setDead(true);
}
if(netServer.isWaitingForPlayers()){
setDead(true);
}
if(!isDead() && isOutOfBounds()){
destructTime += Time.delta();
if(destructTime >= boundsCountdown){
kill();
}
}else{
destructTime = 0f;
}
if(!isDead() && isFlying()){
loops.play(Sounds.thruster, this, Mathf.clamp(velocity.len() * 2f) * 0.3f);
}
BuildRequest request = buildRequest();
if(isBuilding() && isBuilding && request.tile() != null && (request.tile().withinDst(x, y, placeDistance) || state.isEditor())){
loops.play(Sounds.build, request.tile(), 0.75f);
}
if(isDead()){
isBoosting = false;
boostHeat = 0f;
if(respawns > 0 || !state.rules.limitedRespawns){
updateRespawning();
}
return;
}else{
spawner = null;
}
if(isLocal || net.server()){
avoidOthers();
}
Tile tile = world.tileWorld(x, y);
boostHeat = Mathf.lerpDelta(boostHeat, (tile != null && tile.solid()) || (isBoosting && ((!movement.isZero() && moved) || !isLocal)) ? 1f : 0f, 0.08f);
shootHeat = Mathf.lerpDelta(shootHeat, isShooting() ? 1f : 0f, 0.06f);
mech.updateAlt(this); //updated regardless
if(boostHeat > liftoffBoost + 0.1f){
achievedFlight = true;
}
if(boostHeat <= liftoffBoost + 0.05f && achievedFlight && !mech.flying){
if(tile != null){
if(mech.shake > 1f){
Effects.shake(mech.shake, mech.shake, this);
}
Effects.effect(Fx.unitLand, tile.floor().color, x, y, tile.floor().isLiquid ? 1f : 0.5f);
}
mech.onLand(this);
achievedFlight = false;
}
if(!isLocal){
interpolate();
updateMechanics(); //building happens even with non-locals
status.update(this); //status effect updating also happens with non locals for effect purposes
updateVelocityStatus(); //velocity too, for visual purposes
if(net.server()){
updateShooting(); //server simulates player shooting
}
return;
}else if(world.isZone()){
//unlock mech when used
data.unlockContent(mech);
}
if(control.input instanceof MobileInput){
updateTouch();
}else{
updateKeyboard();
}
isTyping = ui.chatfrag.shown();
updateMechanics();
if(!mech.flying){
clampPosition();
}
}
protected void updateKeyboard(){
Tile tile = world.tileWorld(x, y);
isBoosting = Core.input.keyDown(Binding.dash) && !mech.flying;
//if player is in solid block
if(tile != null && tile.solid()){
isBoosting = true;
}
float speed = isBoosting && !mech.flying ? mech.boostSpeed : mech.speed;
if(mech.flying){
//prevent strafing backwards, have a penalty for doing so
float penalty = 0.2f; //when going 180 degrees backwards, reduce speed to 0.2x
speed *= Mathf.lerp(1f, penalty, Angles.angleDist(rotation, velocity.angle()) / 180f);
}
movement.setZero();
float xa = Core.input.axis(Binding.move_x);
float ya = Core.input.axis(Binding.move_y);
if(!(Core.scene.getKeyboardFocus() instanceof TextField)){
movement.y += ya * speed;
movement.x += xa * speed;
}
if(Core.input.keyDown(Binding.mouse_move)){
movement.x += Mathf.clamp((Core.input.mouseX() - Core.graphics.getWidth() / 2) * 0.005f, -1, 1) * speed;
movement.y += Mathf.clamp((Core.input.mouseY() - Core.graphics.getHeight() / 2) * 0.005f, -1, 1) * speed;
}
Vector2 vec = Core.input.mouseWorld(control.input.getMouseX(), control.input.getMouseY());
pointerX = vec.x;
pointerY = vec.y;
updateShooting();
movement.limit(speed).scl(Time.delta());
if(!Core.scene.hasKeyboard()){
velocity.add(movement.x, movement.y);
}else{
isShooting = false;
}
float prex = x, prey = y;
updateVelocityStatus();
moved = dst(prex, prey) > 0.001f;
if(!Core.scene.hasKeyboard()){
float baseLerp = mech.getRotationAlpha(this);
if(!isShooting() || !mech.turnCursor){
if(!movement.isZero()){
rotation = Mathf.slerpDelta(rotation, mech.flying ? velocity.angle() : movement.angle(), 0.13f * baseLerp);
}
}else{
float angle = control.input.mouseAngle(x, y);
this.rotation = Mathf.slerpDelta(this.rotation, angle, 0.1f * baseLerp);
}
}
}
protected void updateShooting(){
if(!state.isEditor() && isShooting() && mech.canShoot(this)){
if(!mech.turnCursor){
//shoot forward ignoring cursor
mech.weapon.update(this, x + Angles.trnsx(rotation, 1f), y + Angles.trnsy(rotation, 1f));
}else{
mech.weapon.update(this, pointerX, pointerY);
}
}
}
protected void updateTouch(){
if(Units.invalidateTarget(target, this) &&
!(target instanceof TileEntity && ((TileEntity)target).damaged() && target.isValid() && target.getTeam() == team && mech.canHeal && dst(target) < getWeapon().bullet.range() && !(((TileEntity)target).block instanceof BuildBlock))){
target = null;
}
if(state.isEditor()){
target = null;
}
float targetX = Core.camera.position.x, targetY = Core.camera.position.y;
float attractDst = 15f;
float speed = isBoosting && !mech.flying ? mech.boostSpeed : mech.speed;
if(moveTarget != null && !moveTarget.isDead()){
targetX = moveTarget.getX();
targetY = moveTarget.getY();
boolean tapping = moveTarget instanceof TileEntity && moveTarget.getTeam() == team;
attractDst = 0f;
if(tapping){
velocity.setAngle(angleTo(moveTarget));
}
if(dst(moveTarget) <= 2f * Time.delta()){
if(tapping && !isDead()){
Tile tile = ((TileEntity)moveTarget).tile;
tile.block().tapped(tile, this);
}
moveTarget = null;
}
}else{
moveTarget = null;
}
movement.set((targetX - x) / Time.delta(), (targetY - y) / Time.delta()).limit(speed);
movement.setAngle(Mathf.slerp(movement.angle(), velocity.angle(), 0.05f));
if(dst(targetX, targetY) < attractDst){
movement.setZero();
}
float expansion = 3f;
hitbox(rect);
rect.x -= expansion;
rect.y -= expansion;
rect.width += expansion * 2f;
rect.height += expansion * 2f;
isBoosting = collisions.overlapsTile(rect) || dst(targetX, targetY) > 85f;
velocity.add(movement.scl(Time.delta()));
if(velocity.len() <= 0.2f && mech.flying){
rotation += Mathf.sin(Time.time() + id * 99, 10f, 1f);
}else if(target == null){
rotation = Mathf.slerpDelta(rotation, velocity.angle(), velocity.len() / 10f);
}
float lx = x, ly = y;
updateVelocityStatus();
moved = dst(lx, ly) > 0.001f;
if(mech.flying){
//hovering effect
x += Mathf.sin(Time.time() + id * 999, 25f, 0.08f);
y += Mathf.cos(Time.time() + id * 999, 25f, 0.08f);
}
//update shooting if not building, not mining and there's ammo left
if(!isBuilding() && getMineTile() == null){
//autofire
if(target == null){
isShooting = false;
if(Core.settings.getBool("autotarget")){
target = Units.closestTarget(team, x, y, getWeapon().bullet.range(), u -> u.getTeam() != Team.derelict, u -> u.getTeam() != Team.derelict);
if(mech.canHeal && target == null){
target = Geometry.findClosest(x, y, indexer.getDamaged(Team.sharded));
if(target != null && dst(target) > getWeapon().bullet.range()){
target = null;
}else if(target != null){
target = ((Tile)target).entity;
}
}
if(target != null){
setMineTile(null);
}
}
}else if(target.isValid() || (target instanceof TileEntity && ((TileEntity)target).damaged() && target.getTeam() == team &&
mech.canHeal && dst(target) < getWeapon().bullet.range())){
//rotate toward and shoot the target
if(mech.turnCursor){
rotation = Mathf.slerpDelta(rotation, angleTo(target), 0.2f);
}
Vector2 intercept = Predict.intercept(this, target, getWeapon().bullet.speed);
pointerX = intercept.x;
pointerY = intercept.y;
updateShooting();
isShooting = true;
}
}
}
//endregion
//region utility methods
public void sendMessage(String text){
if(isLocal){
if(Vars.ui != null){
Log.info("add " + text);
Vars.ui.chatfrag.addMessage(text, null);
}
}else{
Call.sendMessage(con, text, null, null);
}
}
public void sendMessage(String text, Player from){
sendMessage(text, from, NetClient.colorizeName(from.id, from.name));
}
public void sendMessage(String text, Player from, String fromName){
if(isLocal){
if(Vars.ui != null){
Vars.ui.chatfrag.addMessage(text, fromName);
}
}else{
Call.sendMessage(con, text, fromName, from);
}
}
public PlayerInfo getInfo(){
if(uuid == null){
throw new IllegalArgumentException("Local players cannot be traced and do not have info.");
}else{
return netServer.admins.getInfo(uuid);
}
}
/** Resets all values of the player. */
public void reset(){
resetNoAdd();
add();
}
public void resetNoAdd(){
status.clear();
team = Team.sharded;
item.amount = 0;
placeQueue.clear();
dead = true;
lastText = null;
isBuilding = true;
textFadeTime = 0f;
target = null;
moveTarget = null;
isShooting = isBoosting = isTransferring = isTyping = false;
spawner = lastSpawner = null;
health = maxHealth();
mining = null;
boostHeat = drownTime = hitTime = 0f;
mech = Mechs.starter;
placeQueue.clear();
respawns = state.rules.respawns;
}
public boolean isShooting(){
return isShooting && (boostHeat < 0.1f || mech.flying) && mining == null;
}
public void updateRespawning(){
if(state.isEditor()){
//instant respawn at center of map.
set(world.width() * tilesize/2f, world.height() * tilesize/2f);
setDead(false);
}else if(spawner != null && spawner.isValid()){
spawner.updateSpawning(this);
}else if(!netServer.isWaitingForPlayers()){
if(!net.client()){
if(lastSpawner != null && lastSpawner.isValid()){
this.spawner = lastSpawner;
}else if(getClosestCore() != null){
this.spawner = (SpawnerTrait)getClosestCore();
}
}
}else if(getClosestCore() != null){
set(getClosestCore().getX(), getClosestCore().getY());
}
}
public void beginRespawning(SpawnerTrait spawner){
this.spawner = spawner;
this.lastSpawner = spawner;
this.dead = true;
setNet(spawner.getX(), spawner.getY());
spawner.updateSpawning(this);
}
//endregion
//region read and write methods
@Override
public byte version(){
return 0;
}
@Override
public void writeSave(DataOutput stream) throws IOException{
stream.writeBoolean(isLocal);
if(isLocal){
stream.writeByte(mech.id);
stream.writeInt(lastSpawner == null ? noSpawner : lastSpawner.getTile().pos());
super.writeSave(stream, false);
}
}
@Override
public void readSave(DataInput stream, byte version) throws IOException{
boolean local = stream.readBoolean();
if(local){
byte mechid = stream.readByte();
int spawner = stream.readInt();
Tile stile = world.tile(spawner);
Player player = headless ? this : Vars.player;
player.readSaveSuper(stream, version);
player.mech = content.getByID(ContentType.mech, mechid);
player.dead = false;
if(stile != null && stile.entity instanceof SpawnerTrait){
player.lastSpawner = (SpawnerTrait)stile.entity;
}
}
}
private void readSaveSuper(DataInput stream, byte version) throws IOException{
super.readSave(stream, version);
add();
}
@Override
public void write(DataOutput buffer) throws IOException{
super.writeSave(buffer, !isLocal);
TypeIO.writeStringData(buffer, name);
buffer.writeByte(Pack.byteValue(isAdmin) | (Pack.byteValue(dead) << 1) | (Pack.byteValue(isBoosting) << 2) | (Pack.byteValue(isTyping) << 3)| (Pack.byteValue(isBuilding) << 4));
buffer.writeInt(Color.rgba8888(color));
buffer.writeByte(mech.id);
buffer.writeInt(mining == null ? noSpawner : mining.pos());
buffer.writeInt(spawner == null || !spawner.hasUnit(this) ? noSpawner : spawner.getTile().pos());
buffer.writeShort((short)(baseRotation * 2));
writeBuilding(buffer);
}
@Override
public void read(DataInput buffer) throws IOException{
float lastx = x, lasty = y, lastrot = rotation, lastvx = velocity.x, lastvy = velocity.y;
super.readSave(buffer, version());
name = TypeIO.readStringData(buffer);
byte bools = buffer.readByte();
isAdmin = (bools & 1) != 0;
dead = (bools & 2) != 0;
boolean boosting = (bools & 4) != 0;
isTyping = (bools & 8) != 0;
boolean building = (bools & 16) != 0;
color.set(buffer.readInt());
mech = content.getByID(ContentType.mech, buffer.readByte());
int mine = buffer.readInt();
int spawner = buffer.readInt();
float baseRotation = buffer.readShort() / 2f;
readBuilding(buffer, !isLocal);
interpolator.read(lastx, lasty, x, y, rotation, baseRotation);
rotation = lastrot;
x = lastx;
y = lasty;
if(isLocal){
velocity.x = lastvx;
velocity.y = lastvy;
}else{
mining = world.tile(mine);
isBuilding = building;
isBoosting = boosting;
}
Tile tile = world.tile(spawner);
if(tile != null && tile.entity instanceof SpawnerTrait){
this.spawner = (SpawnerTrait)tile.entity;
}else{
this.spawner = null;
}
}
//endregion
}

View File

@@ -0,0 +1,19 @@
package mindustry.entities.type;
import arc.math.geom.Vector2;
import mindustry.entities.traits.SolidTrait;
public abstract class SolidEntity extends BaseEntity implements SolidTrait{
protected transient Vector2 velocity = new Vector2(0f, 0.0001f);
private transient Vector2 lastPosition = new Vector2();
@Override
public Vector2 lastPosition(){
return lastPosition;
}
@Override
public Vector2 velocity(){
return velocity;
}
}

View File

@@ -0,0 +1,347 @@
package mindustry.entities.type;
import mindustry.annotations.Annotations.*;
import arc.Events;
import arc.struct.Array;
import arc.struct.ObjectSet;
import arc.math.geom.Point2;
import arc.math.geom.Vector2;
import arc.util.*;
import arc.util.ArcAnnotate.*;
import mindustry.entities.EntityGroup;
import mindustry.entities.traits.HealthTrait;
import mindustry.entities.traits.TargetTrait;
import mindustry.game.*;
import mindustry.game.EventType.BlockDestroyEvent;
import mindustry.gen.*;
import mindustry.world.*;
import mindustry.world.consumers.*;
import mindustry.world.modules.*;
import java.io.*;
import static mindustry.Vars.*;
public class TileEntity extends BaseEntity implements TargetTrait, HealthTrait{
public static final float timeToSleep = 60f * 4; //4 seconds to fall asleep
private static final ObjectSet<Tile> tmpTiles = new ObjectSet<>();
/** This value is only used for debugging. */
public static int sleepingEntities = 0;
public Tile tile;
public Block block;
public Interval timer;
public float health;
public float timeScale = 1f, timeScaleDuration;
public PowerModule power;
public ItemModule items;
public LiquidModule liquids;
public @Nullable ConsumeModule cons;
/** List of (cached) tiles with entities in proximity, used for outputting to */
private Array<Tile> proximity = new Array<>(8);
private boolean dead = false;
private boolean sleeping;
private float sleepTime;
private @Nullable SoundLoop sound;
@Remote(called = Loc.server, unreliable = true)
public static void onTileDamage(Tile tile, float health){
if(tile.entity != null){
tile.entity.health = health;
if(tile.entity.damaged()){
indexer.notifyTileDamaged(tile.entity);
}
}
}
@Remote(called = Loc.server)
public static void onTileDestroyed(Tile tile){
if(tile.entity == null) return;
tile.entity.onDeath();
}
/** Sets this tile entity data to this tile, and adds it if necessary. */
public TileEntity init(Tile tile, boolean shouldAdd){
this.tile = tile;
x = tile.drawx();
y = tile.drawy();
block = tile.block();
if(block.activeSound != Sounds.none){
sound = new SoundLoop(block.activeSound, block.activeSoundVolume);
}
health = block.health;
timer = new Interval(block.timers);
if(shouldAdd){
add();
}
return this;
}
/** Scaled delta. */
public float delta(){
return Time.delta() * timeScale;
}
/** Base efficiency. If this entity has non-buffered power, returns the power %, otherwise returns 1. */
public float efficiency(){
return power != null && (block.consumes.has(ConsumeType.power) && !block.consumes.getPower().buffered) ? power.status : 1f;
}
/** Call when nothing is happening to the entity. This increments the internal sleep timer. */
public void sleep(){
sleepTime += Time.delta();
if(!sleeping && sleepTime >= timeToSleep){
remove();
sleeping = true;
sleepingEntities++;
}
}
/** Call when this entity is updating. This wakes it up. */
public void noSleep(){
sleepTime = 0f;
if(sleeping){
add();
sleeping = false;
sleepingEntities--;
}
}
public boolean isSleeping(){
return sleeping;
}
public boolean isDead(){
return dead || tile.entity != this;
}
@CallSuper
public void write(DataOutput stream) throws IOException{
stream.writeShort((short)health);
stream.writeByte(Pack.byteByte(tile.getTeamID(), tile.rotation())); //team + rotation
if(items != null) items.write(stream);
if(power != null) power.write(stream);
if(liquids != null) liquids.write(stream);
if(cons != null) cons.write(stream);
}
@CallSuper
public void read(DataInput stream, byte revision) throws IOException{
health = stream.readUnsignedShort();
byte tr = stream.readByte();
byte team = Pack.leftByte(tr);
byte rotation = Pack.rightByte(tr);
tile.setTeam(Team.all[team]);
tile.rotation(rotation);
if(items != null) items.read(stream);
if(power != null) power.read(stream);
if(liquids != null) liquids.read(stream);
if(cons != null) cons.read(stream);
}
/** Returns the version of this TileEntity IO code.*/
public byte version(){
return 0;
}
public boolean collide(Bullet other){
return true;
}
public void collision(Bullet other){
block.handleBulletHit(this, other);
}
public void kill(){
Call.onTileDestroyed(tile);
}
public void damage(float damage){
if(dead) return;
float preHealth = health;
Call.onTileDamage(tile, health - block.handleDamage(tile, damage));
if(health <= 0){
Call.onTileDestroyed(tile);
}
if(preHealth >= maxHealth() - 0.00001f && health < maxHealth() && world != null){ //when just damaged
indexer.notifyTileDamaged(this);
}
}
public Tile getTile(){
return tile;
}
public void removeFromProximity(){
block.onProximityRemoved(tile);
Point2[] nearby = Edges.getEdges(block.size);
for(Point2 point : nearby){
Tile other = world.ltile(tile.x + point.x, tile.y + point.y);
//remove this tile from all nearby tile's proximities
if(other != null){
other.block().onProximityUpdate(other);
if(other.entity != null){
other.entity.proximity.removeValue(tile, true);
}
}
}
}
public void updateProximity(){
tmpTiles.clear();
proximity.clear();
Point2[] nearby = Edges.getEdges(block.size);
for(Point2 point : nearby){
Tile other = world.ltile(tile.x + point.x, tile.y + point.y);
if(other == null) continue;
if(other.entity == null || !(other.interactable(tile.getTeam()))) continue;
//add this tile to proximity of nearby tiles
if(!other.entity.proximity.contains(tile, true)){
other.entity.proximity.add(tile);
}
tmpTiles.add(other);
}
//using a set to prevent duplicates
for(Tile tile : tmpTiles){
proximity.add(tile);
}
block.onProximityAdded(tile);
block.onProximityUpdate(tile);
for(Tile other : tmpTiles){
other.block().onProximityUpdate(other);
}
}
public Array<Tile> proximity(){
return proximity;
}
/** Tile configuration. Defaults to 0. Used for block rebuilding. */
public int config(){
return 0;
}
@Override
public void removed(){
if(sound != null){
sound.stop();
}
}
@Override
public void health(float health){
this.health = health;
}
@Override
public float health(){
return health;
}
@Override
public float maxHealth(){
return block.health;
}
@Override
public void setDead(boolean dead){
this.dead = dead;
}
@Override
public void onDeath(){
if(!dead){
dead = true;
Events.fire(new BlockDestroyEvent(tile));
block.breakSound.at(tile);
block.onDestroyed(tile);
world.removeBlock(tile);
remove();
}
}
@Override
public Team getTeam(){
return tile.getTeam();
}
@Override
public Vector2 velocity(){
return Vector2.ZERO;
}
@Override
public void update(){
timeScaleDuration -= Time.delta();
if(timeScaleDuration <= 0f || !block.canOverdrive){
timeScale = 1f;
}
if(health <= 0){
onDeath();
return; //no need to update anymore
}
if(sound != null){
sound.update(x, y, block.shouldActiveSound(tile));
}
if(block.idleSound != Sounds.none && block.shouldIdleSound(tile)){
loops.play(block.idleSound, this, block.idleSoundVolume);
}
block.update(tile);
if(liquids != null){
liquids.update();
}
if(cons != null){
cons.update();
}
if(power != null){
power.graph.update();
}
}
@Override
public boolean isValid(){
return !isDead() && tile.entity == this;
}
@Override
public EntityGroup targetGroup(){
return tileGroup;
}
@Override
public String toString(){
return "TileEntity{" +
"tile=" + tile +
", health=" + health +
'}';
}
}

View File

@@ -0,0 +1,34 @@
package mindustry.entities.type;
import arc.util.pooling.Pool.Poolable;
import mindustry.entities.traits.ScaleTrait;
import mindustry.entities.traits.TimeTrait;
public abstract class TimedEntity extends BaseEntity implements ScaleTrait, TimeTrait, Poolable{
public float time;
@Override
public void time(float time){
this.time = time;
}
@Override
public float time(){
return time;
}
@Override
public void update(){
updateTime();
}
@Override
public void reset(){
time = 0f;
}
@Override
public float fin(){
return time() / lifetime();
}
}

View File

@@ -0,0 +1,473 @@
package mindustry.entities.type;
import arc.*;
import arc.struct.*;
import arc.graphics.*;
import arc.graphics.g2d.*;
import arc.math.*;
import arc.math.geom.*;
import arc.scene.ui.layout.*;
import arc.util.*;
import arc.util.ArcAnnotate.*;
import mindustry.content.*;
import mindustry.entities.*;
import mindustry.entities.effect.*;
import mindustry.entities.traits.*;
import mindustry.entities.units.*;
import mindustry.game.EventType.*;
import mindustry.game.*;
import mindustry.game.Teams.*;
import mindustry.gen.*;
import mindustry.graphics.*;
import mindustry.net.*;
import mindustry.type.*;
import mindustry.ui.*;
import mindustry.ui.Cicon;
import mindustry.world.*;
import mindustry.world.blocks.*;
import java.io.*;
import static mindustry.Vars.*;
public abstract class Unit extends DestructibleEntity implements SaveTrait, TargetTrait, SyncTrait, DrawTrait, TeamTrait{
/** Total duration of hit flash effect */
public static final float hitDuration = 9f;
/** Percision divisor of velocity, used when writing. For example a value of '2' would mean the percision is 1/2 = 0.5-size chunks. */
public static final float velocityPercision = 8f;
/** Maximum absolute value of a velocity vector component. */
public static final float maxAbsVelocity = 127f / velocityPercision;
public static final int noSpawner = Pos.get(-1, 1);
private static final Vector2 moveVector = new Vector2();
public float rotation;
protected final Interpolator interpolator = new Interpolator();
protected final Statuses status = new Statuses();
protected final ItemStack item = new ItemStack(content.item(0), 0);
protected Team team = Team.sharded;
protected float drownTime, hitTime;
@Override
public boolean collidesGrid(int x, int y){
return !isFlying();
}
@Override
public Team getTeam(){
return team;
}
@Override
public void interpolate(){
interpolator.update();
x = interpolator.pos.x;
y = interpolator.pos.y;
if(interpolator.values.length > 0){
rotation = interpolator.values[0];
}
}
@Override
public Interpolator getInterpolator(){
return interpolator;
}
@Override
public void damage(float amount){
if(!net.client()){
super.damage(calculateDamage(amount));
}
hitTime = hitDuration;
}
@Override
public boolean collides(SolidTrait other){
if(isDead()) return false;
if(other instanceof DamageTrait){
return other instanceof TeamTrait && state.teams.areEnemies((((TeamTrait)other).getTeam()), team);
}else{
return other instanceof Unit && ((Unit)other).isFlying() == isFlying();
}
}
@Override
public void onDeath(){
float explosiveness = 2f + item.item.explosiveness * item.amount;
float flammability = item.item.flammability * item.amount;
Damage.dynamicExplosion(x, y, flammability, explosiveness, 0f, getSize() / 2f, Pal.darkFlame);
ScorchDecal.create(x, y);
Effects.effect(Fx.explosion, this);
Effects.shake(2f, 2f, this);
Sounds.bang.at(this);
item.amount = 0;
drownTime = 0f;
status.clear();
Events.fire(new UnitDestroyEvent(this));
if(explosiveness > 7f && this == player){
Events.fire(Trigger.suicideBomb);
}
}
@Override
public Vector2 velocity(){
return velocity;
}
@Override
public void move(float x, float y){
if(!isFlying()){
super.move(x, y);
}else{
moveBy(x, y);
}
}
@Override
public boolean isValid(){
return !isDead() && isAdded();
}
@Override
public void writeSave(DataOutput stream) throws IOException{
writeSave(stream, false);
}
@Override
public void readSave(DataInput stream, byte version) throws IOException{
byte team = stream.readByte();
boolean dead = stream.readBoolean();
float x = stream.readFloat();
float y = stream.readFloat();
byte xv = stream.readByte();
byte yv = stream.readByte();
float rotation = stream.readShort() / 2f;
int health = stream.readShort();
byte itemID = stream.readByte();
short itemAmount = stream.readShort();
this.status.readSave(stream, version);
this.item.amount = itemAmount;
this.item.item = content.item(itemID);
this.dead = dead;
this.team = Team.all[team];
this.health = health;
this.x = x;
this.y = y;
this.velocity.set(xv / velocityPercision, yv / velocityPercision);
this.rotation = rotation;
}
public void writeSave(DataOutput stream, boolean net) throws IOException{
if(item.item == null) item.item = Items.copper;
stream.writeByte(team.ordinal());
stream.writeBoolean(isDead());
stream.writeFloat(net ? interpolator.target.x : x);
stream.writeFloat(net ? interpolator.target.y : y);
stream.writeByte((byte)(Mathf.clamp(velocity.x, -maxAbsVelocity, maxAbsVelocity) * velocityPercision));
stream.writeByte((byte)(Mathf.clamp(velocity.y, -maxAbsVelocity, maxAbsVelocity) * velocityPercision));
stream.writeShort((short)(rotation * 2));
stream.writeShort((short)health);
stream.writeByte(item.item.id);
stream.writeShort((short)item.amount);
status.writeSave(stream);
}
protected void clampPosition(){
x = Mathf.clamp(x, 0, world.width() * tilesize - tilesize);
y = Mathf.clamp(y, 0, world.height() * tilesize - tilesize);
}
public void kill(){
health = -1;
damage(1);
}
public boolean isImmune(StatusEffect effect){
return false;
}
public boolean isOutOfBounds(){
return x < -worldBounds || y < -worldBounds || x > world.width() * tilesize + worldBounds || y > world.height() * tilesize + worldBounds;
}
public float calculateDamage(float amount){
return amount * Mathf.clamp(1f - status.getArmorMultiplier() / 100f);
}
public float getDamageMultipler(){
return status.getDamageMultiplier();
}
public boolean hasEffect(StatusEffect effect){
return status.hasEffect(effect);
}
public void avoidOthers(){
float radScl = 1.5f;
float fsize = getSize() / radScl;
moveVector.setZero();
float cx = x - fsize/2f, cy = y - fsize/2f;
for(Team team : Team.all){
if(team != getTeam() || !(this instanceof Player)){
avoid(unitGroups[team.ordinal()].intersect(cx, cy, fsize, fsize));
}
}
if(!(this instanceof Player)){
avoid(playerGroup.intersect(cx, cy, fsize, fsize));
}
velocity.add(moveVector.x / mass() * Time.delta(), moveVector.y / mass() * Time.delta());
}
private void avoid(Array<? extends Unit> arr){
float radScl = 1.5f;
for(Unit en : arr){
if(en.isFlying() != isFlying() || (en instanceof Player && en.getTeam() != getTeam())) continue;
float dst = dst(en);
float scl = Mathf.clamp(1f - dst / (getSize()/(radScl*2f) + en.getSize()/(radScl*2f)));
moveVector.add(Tmp.v1.set((x - en.x) * scl, (y - en.y) * scl).limit(0.4f));
}
}
public @Nullable TileEntity getClosestCore(){
TeamData data = state.teams.get(team);
Tile tile = Geometry.findClosest(x, y, data.cores);
if(tile == null){
return null;
}else{
return tile.entity;
}
}
public Floor getFloorOn(){
Tile tile = world.tileWorld(x, y);
return tile == null ? (Floor)Blocks.air : tile.floor();
}
public void onRespawn(Tile tile){
}
/** Updates velocity and status effects. */
public void updateVelocityStatus(){
Floor floor = getFloorOn();
Tile tile = world.tileWorld(x, y);
status.update(this);
velocity.limit(maxVelocity()).scl(1f + (status.getSpeedMultiplier() - 1f) * Time.delta());
if(x < -finalWorldBounds || y < -finalWorldBounds || x >= world.width() * tilesize + finalWorldBounds || y >= world.height() * tilesize + finalWorldBounds){
kill();
}
//apply knockback based on spawns
if(getTeam() != waveTeam){
float relativeSize = state.rules.dropZoneRadius + getSize()/2f + 1f;
for(Tile spawn : spawner.getGroundSpawns()){
if(withinDst(spawn.worldx(), spawn.worldy(), relativeSize)){
velocity.add(Tmp.v1.set(this).sub(spawn.worldx(), spawn.worldy()).setLength(0.1f + 1f - dst(spawn) / relativeSize).scl(0.45f * Time.delta()));
}
}
}
//repel player out of bounds
final float warpDst = 180f;
if(x < 0) velocity.x += (-x/warpDst);
if(y < 0) velocity.y += (-y/warpDst);
if(x > world.unitWidth()) velocity.x -= (x - world.unitWidth())/warpDst;
if(y > world.unitHeight()) velocity.y -= (y - world.unitHeight())/warpDst;
if(isFlying()){
drownTime = 0f;
move(velocity.x * Time.delta(), velocity.y * Time.delta());
}else{
boolean onLiquid = floor.isLiquid;
if(tile != null){
tile.block().unitOn(tile, this);
if(tile.block() != Blocks.air){
onLiquid = false;
}
}
if(onLiquid && velocity.len() > 0.4f && Mathf.chance((velocity.len() * floor.speedMultiplier) * 0.06f * Time.delta())){
Effects.effect(floor.walkEffect, floor.color, x, y);
}
if(onLiquid){
status.handleApply(this, floor.status, floor.statusDuration);
if(floor.damageTaken > 0f){
damagePeriodic(floor.damageTaken);
}
}
if(onLiquid && floor.drownTime > 0){
drownTime += Time.delta() * 1f / floor.drownTime;
if(Mathf.chance(Time.delta() * 0.05f)){
Effects.effect(floor.drownUpdateEffect, floor.color, x, y);
}
}else{
drownTime = Mathf.lerpDelta(drownTime, 0f, 0.03f);
}
drownTime = Mathf.clamp(drownTime);
if(drownTime >= 0.999f && !net.client()){
damage(health + 1);
if(this == player){
Events.fire(Trigger.drown);
}
}
float px = x, py = y;
move(velocity.x * floor.speedMultiplier * Time.delta(), velocity.y * floor.speedMultiplier * Time.delta());
if(Math.abs(px - x) <= 0.0001f) velocity.x = 0f;
if(Math.abs(py - y) <= 0.0001f) velocity.y = 0f;
}
velocity.scl(Mathf.clamp(1f - drag() * (isFlying() ? 1f : floor.dragMultiplier) * Time.delta()));
}
public boolean acceptsItem(Item item){
return this.item.amount <= 0 || (this.item.item == item && this.item.amount <= getItemCapacity());
}
public void addItem(Item item){
addItem(item, 1);
}
public void addItem(Item item, int amount){
this.item.amount = this.item.item == item ? this.item.amount + amount : amount;
this.item.item = item;
}
public void clearItem(){
item.amount = 0;
}
public ItemStack item(){
return item;
}
public int maxAccepted(Item item){
return this.item.item != item && this.item.amount > 0 ? 0 : getItemCapacity() - this.item.amount;
}
public void applyEffect(StatusEffect effect, float duration){
if(dead || net.client()) return; //effects are synced and thus not applied through clients
status.handleApply(this, effect, duration);
}
public void damagePeriodic(float amount){
damage(amount * Time.delta(), hitTime <= -20 + hitDuration);
}
public void damage(float amount, boolean withEffect){
float pre = hitTime;
damage(amount);
if(!withEffect){
hitTime = pre;
}
}
public void drawUnder(){
}
public void drawOver(){
}
public void drawStats(){
Draw.color(Color.black, team.color, healthf() + Mathf.absin(Time.time(), Math.max(healthf() * 5f, 1f), 1f - healthf()));
Draw.rect(getPowerCellRegion(), x, y, rotation - 90);
Draw.color();
drawBackItems(item.amount > 0 ? 1f : 0f, false);
drawLight();
}
public void drawLight(){
renderer.lights.add(x, y, 50f, Pal.powerLight, 0.6f);
}
public void drawBackItems(float itemtime, boolean number){
//draw back items
if(itemtime > 0.01f && item.item != null){
float backTrns = 5f;
float size = (itemSize + Mathf.absin(Time.time(), 5f, 1f)) * itemtime;
Draw.mixcol(Pal.accent, Mathf.absin(Time.time(), 5f, 0.5f));
Draw.rect(item.item.icon(Cicon.medium),
x + Angles.trnsx(rotation + 180f, backTrns),
y + Angles.trnsy(rotation + 180f, backTrns),
size, size, rotation);
Draw.mixcol();
Lines.stroke(1f, Pal.accent);
Lines.circle(
x + Angles.trnsx(rotation + 180f, backTrns),
y + Angles.trnsy(rotation + 180f, backTrns),
(3f + Mathf.absin(Time.time(), 5f, 1f)) * itemtime);
if(number){
Fonts.outline.draw(item.amount + "",
x + Angles.trnsx(rotation + 180f, backTrns),
y + Angles.trnsy(rotation + 180f, backTrns) - 3,
Pal.accent, 0.25f * itemtime / Scl.scl(1f), false, Align.center
);
}
}
Draw.reset();
}
public TextureRegion getPowerCellRegion(){
return Core.atlas.find("power-cell");
}
public void drawAll(){
if(!isDead()){
draw();
drawStats();
}
}
public void drawShadow(float offsetX, float offsetY){
Draw.rect(getIconRegion(), x + offsetX, y + offsetY, rotation - 90);
}
public float getSize(){
hitbox(Tmp.r1);
return Math.max(Tmp.r1.width, Tmp.r1.height) * 2f;
}
public abstract TextureRegion getIconRegion();
public abstract Weapon getWeapon();
public abstract int getItemCapacity();
public abstract float mass();
public abstract boolean isFlying();
}

View File

@@ -0,0 +1,63 @@
package mindustry.entities.type.base;
import arc.math.Mathf;
import arc.math.geom.Geometry;
import mindustry.entities.units.*;
import mindustry.world.Tile;
import mindustry.world.meta.BlockFlag;
import static mindustry.Vars.*;
public abstract class BaseDrone extends FlyingUnit{
public final UnitState retreat = new UnitState(){
public void entered(){
target = null;
}
public void update(){
if(health >= maxHealth()){
state.set(getStartState());
}else if(!targetHasFlag(BlockFlag.repair)){
if(retarget()){
Tile repairPoint = Geometry.findClosest(x, y, indexer.getAllied(team, BlockFlag.repair));
if(repairPoint != null){
target = repairPoint;
}else{
setState(getStartState());
}
}
}else{
circle(40f);
}
}
};
@Override
public void onCommand(UnitCommand command){
//do nothing, normal commands are not applicable here
}
@Override
protected void updateRotation(){
if(target != null && shouldRotate() && target.dst(this) < type.range){
rotation = Mathf.slerpDelta(rotation, angleTo(target), 0.3f);
}else{
rotation = Mathf.slerpDelta(rotation, velocity.angle(), 0.3f);
}
}
@Override
public void behavior(){
if(health <= maxHealth() * type.retreatPercent && !state.is(retreat) && Geometry.findClosest(x, y, indexer.getAllied(team, BlockFlag.repair)) != null){
setState(retreat);
}
}
public boolean shouldRotate(){
return state.is(getStartState());
}
@Override
public abstract UnitState getStartState();
}

View File

@@ -0,0 +1,242 @@
package mindustry.entities.type.base;
import arc.*;
import arc.struct.*;
import arc.math.*;
import arc.util.*;
import mindustry.*;
import mindustry.entities.*;
import mindustry.entities.traits.*;
import mindustry.entities.type.*;
import mindustry.entities.units.*;
import mindustry.game.EventType.*;
import mindustry.game.Teams.*;
import mindustry.world.*;
import mindustry.world.blocks.*;
import mindustry.world.blocks.BuildBlock.*;
import java.io.*;
import static mindustry.Vars.*;
public class BuilderDrone extends BaseDrone implements BuilderTrait{
private static final StaticReset reset = new StaticReset();
private static final IntIntMap totals = new IntIntMap();
protected Queue<BuildRequest> placeQueue = new Queue<>();
protected BuildRequest lastFound;
protected boolean isBreaking;
protected Player playerTarget;
public final UnitState
build = new UnitState(){
public void entered(){
if(!(target instanceof BuildEntity)){
target = null;
}
}
public void update(){
BuildEntity entity = (BuildEntity)target;
TileEntity core = getClosestCore();
if(isBuilding() && entity == null && canRebuild()){
target = world.tile(buildRequest().x, buildRequest().y);
circle(placeDistance * 0.7f);
target = null;
BuildRequest request = buildRequest();
if(world.tile(request.x, request.y).entity instanceof BuildEntity){
target = world.tile(request.x, request.y).entity;
}
}else if(entity != null && core != null && (entity.progress < 1f || entity.progress > 0f) && entity.tile.block() instanceof BuildBlock){ //building is valid
if(!isBuilding() && dst(target) < placeDistance * 0.9f){ //within distance, begin placing
if(isBreaking){
buildQueue().addLast(new BuildRequest(entity.tile.x, entity.tile.y));
}else{
buildQueue().addLast(new BuildRequest(entity.tile.x, entity.tile.y, entity.tile.rotation(), entity.cblock));
if(lastFound != null && lastFound.hasConfig){
buildQueue().last().configure(lastFound.config);
}
}
}
circle(placeDistance * 0.7f);
velocity.scl(0.74f);
}else{ //else, building isn't valid, follow a player
target = null;
if(playerTarget == null || playerTarget.getTeam() != team || !playerTarget.isValid()){
playerTarget = null;
if(retarget()){
float minDst = Float.POSITIVE_INFINITY;
int minDrones = Integer.MAX_VALUE;
//find player with min amount of drones
for(Player player : playerGroup.all()){
if(player.getTeam() == team){
int drones = getDrones(player);
float dst = dst2(player);
if(playerTarget == null || drones < minDrones || (drones == minDrones && dst < minDst)){
minDrones = drones;
minDst = dst;
playerTarget = player;
}
}
}
}
if(getSpawner() != null){
target = getSpawner();
circle(40f);
target = null;
}
}else{
incDrones(playerTarget);
TargetTrait prev = target;
target = playerTarget;
float dst = 90f + (id % 10)*3;
float tdst = dst(target);
float scale = (Mathf.lerp(1f, 0.2f, 1f - Mathf.clamp((tdst - dst) / dst)));
circle(dst);
velocity.scl(scale);
target = prev;
}
}
}
};
public BuilderDrone(){
if(reset.check()){
Events.on(BuildSelectEvent.class, event -> {
EntityGroup<BaseUnit> group = unitGroups[event.team.ordinal()];
if(!(event.tile.entity instanceof BuildEntity)) return;
for(BaseUnit unit : group.all()){
if(unit instanceof BuilderDrone){
BuilderDrone drone = (BuilderDrone)unit;
if(drone.isBuilding()){
//stop building if opposite building begins.
BuildRequest req = drone.buildRequest();
if(req.breaking != event.breaking && req.x == event.tile.x && req.y == event.tile.y){
drone.clearBuilding();
drone.target = null;
}
}
}
}
});
}
}
int getDrones(Player player){
return Pack.leftShort(totals.get(player.id, 0));
}
void incDrones(Player player){
int num = totals.get(player.id, 0);
int amount = Pack.leftShort(num), frame = Pack.rightShort(num);
short curFrame = (short)(Core.graphics.getFrameId() % Short.MAX_VALUE);
if(frame != curFrame){
totals.put(player.id, Pack.shortInt((short)1, curFrame));
}else{
totals.put(player.id, Pack.shortInt((short)(amount + 1), curFrame));
}
}
boolean canRebuild(){
return true;
}
@Override
public float getBuildPower(Tile tile){
return type.buildPower;
}
@Override
public Queue<BuildRequest> buildQueue(){
return placeQueue;
}
@Override
public void update(){
super.update();
if(!isBuilding() && timer.get(timerTarget2, 15)){
for(Player player : playerGroup.all()){
if(player.getTeam() == team && player.buildRequest() != null){
BuildRequest req = player.buildRequest();
Tile tile = world.tile(req.x, req.y);
if(tile != null && tile.entity instanceof BuildEntity){
BuildEntity b = tile.ent();
float dist = Math.min(b.dst(x, y) - placeDistance, 0);
if(dist / type.maxVelocity < b.buildCost * 0.9f){
lastFound = req;
target = b;
this.isBreaking = req.breaking;
setState(build);
break;
}
}
}
}
if(timer.get(timerTarget, 80) && Units.closestEnemy(getTeam(), x, y, 100f, u -> !(u instanceof BaseDrone)) == null && !isBuilding()){
TeamData data = Vars.state.teams.get(team);
if(!data.brokenBlocks.isEmpty()){
BrokenBlock block = data.brokenBlocks.removeLast();
if(Build.validPlace(getTeam(), block.x, block.y, content.block(block.block), block.rotation)){
placeQueue.addFirst(new BuildRequest(block.x, block.y, block.rotation, content.block(block.block)).configure(block.config));
setState(build);
}
}
}
}
updateBuilding();
}
@Override
public boolean shouldRotate(){
return isBuilding();
}
@Override
public UnitState getStartState(){
return build;
}
@Override
public void drawOver(){
drawBuilding();
}
@Override
public float drawSize(){
return isBuilding() ? placeDistance * 2f : 30f;
}
@Override
public boolean canCreateBlocks(){
return true;
}
@Override
public void write(DataOutput data) throws IOException{
super.write(data);
writeBuilding(data);
}
@Override
public void read(DataInput data) throws IOException{
super.read(data);
readBuilding(data);
}
}

View File

@@ -0,0 +1,256 @@
package mindustry.entities.type.base;
import arc.graphics.*;
import arc.graphics.g2d.*;
import arc.math.*;
import arc.math.geom.*;
import arc.util.*;
import mindustry.*;
import mindustry.entities.*;
import mindustry.entities.bullet.*;
import mindustry.entities.type.*;
import mindustry.entities.units.*;
import mindustry.graphics.*;
import mindustry.world.*;
import mindustry.world.meta.*;
import static mindustry.Vars.*;
public class FlyingUnit extends BaseUnit{
protected float[] weaponAngles = {0,0};
protected final UnitState
attack = new UnitState(){
public void entered(){
target = null;
}
public void update(){
if(Units.invalidateTarget(target, team, x, y)){
target = null;
}
if(retarget()){
targetClosest();
if(target == null) targetClosestEnemyFlag(BlockFlag.producer);
if(target == null) targetClosestEnemyFlag(BlockFlag.turret);
if(target == null && isCommanded() && getCommand() != UnitCommand.attack){
onCommand(getCommand());
}
}
if(getClosestSpawner() == null && getSpawner() != null && target == null){
target = getSpawner();
circle(80f + Mathf.randomSeed(id) * 120);
}else if(target != null){
attack(type.attackLength);
if((Angles.near(angleTo(target), rotation, type.shootCone) || getWeapon().ignoreRotation) //bombers and such don't care about rotation
&& dst(target) < getWeapon().bullet.range()){
BulletType ammo = getWeapon().bullet;
if(type.rotateWeapon){
for(boolean left : Mathf.booleans){
int wi = Mathf.num(left);
float wx = x + Angles.trnsx(rotation - 90, getWeapon().width * Mathf.sign(left));
float wy = y + Angles.trnsy(rotation - 90, getWeapon().width * Mathf.sign(left));
weaponAngles[wi] = Mathf.slerpDelta(weaponAngles[wi], Angles.angle(wx, wy, target.getX(), target.getY()), 0.1f);
Tmp.v2.trns(weaponAngles[wi], getWeapon().length);
getWeapon().update(FlyingUnit.this, wx + Tmp.v2.x, wy + Tmp.v2.y, weaponAngles[wi], left);
}
}else{
Vector2 to = Predict.intercept(FlyingUnit.this, target, ammo.speed);
getWeapon().update(FlyingUnit.this, to.x, to.y);
}
}
}else{
target = getClosestSpawner();
moveTo(Vars.state.rules.dropZoneRadius + 120f);
}
}
},
rally = new UnitState(){
public void update(){
if(retarget()){
targetClosestAllyFlag(BlockFlag.rally);
targetClosest();
if(target != null && !Units.invalidateTarget(target, team, x, y)){
setState(attack);
return;
}
if(target == null) target = getSpawner();
}
if(target != null){
circle(65f + Mathf.randomSeed(id) * 100);
}
}
},
retreat = new UnitState(){
public void entered(){
target = null;
}
public void update(){
if(retarget()){
target = getSpawner();
Tile repair = Geometry.findClosest(x, y, indexer.getAllied(team, BlockFlag.repair));
if(repair != null && damaged()) FlyingUnit.this.target = repair.entity;
if(target == null) target = getClosestCore();
}
circle(targetHasFlag(BlockFlag.repair) ? 20f : 60f + Mathf.randomSeed(id) * 50, 0.65f * type.speed);
}
};;
@Override
public void onCommand(UnitCommand command){
state.set(command == UnitCommand.retreat ? retreat :
command == UnitCommand.attack ? attack :
command == UnitCommand.rally ? rally :
null);
}
@Override
public void move(float x, float y){
moveBy(x, y);
}
@Override
public void update(){
super.update();
if(!net.client()){
updateRotation();
}
wobble();
}
@Override
public void drawUnder(){
drawEngine();
}
@Override
public void draw(){
Draw.mixcol(Color.white, hitTime / hitDuration);
Draw.rect(type.region, x, y, rotation - 90);
drawWeapons();
Draw.mixcol();
}
public void drawWeapons(){
for(int i : Mathf.signs){
float tra = rotation - 90, trY = -type.weapon.getRecoil(this, i > 0) + type.weaponOffsetY;
float w = -i * type.weapon.region.getWidth() * Draw.scl;
Draw.rect(type.weapon.region,
x + Angles.trnsx(tra, getWeapon().width * i, trY),
y + Angles.trnsy(tra, getWeapon().width * i, trY), w, type.weapon.region.getHeight() * Draw.scl, rotation - 90);
}
}
public void drawEngine(){
Draw.color(Pal.engine);
Fill.circle(x + Angles.trnsx(rotation + 180, type.engineOffset), y + Angles.trnsy(rotation + 180, type.engineOffset),
type.engineSize + Mathf.absin(Time.time(), 2f, type.engineSize / 4f));
Draw.color(Color.white);
Fill.circle(x + Angles.trnsx(rotation + 180, type.engineOffset - 1f), y + Angles.trnsy(rotation + 180, type.engineOffset - 1f),
(type.engineSize + Mathf.absin(Time.time(), 2f, type.engineSize / 4f)) / 2f);
Draw.color();
}
@Override
public void behavior(){
if(Units.invalidateTarget(target, this)){
for(boolean left : Mathf.booleans){
int wi = Mathf.num(left);
weaponAngles[wi] = Mathf.slerpDelta(weaponAngles[wi], rotation, 0.1f);
}
}
}
@Override
public UnitState getStartState(){
return attack;
}
protected void wobble(){
if(net.client()) return;
x += Mathf.sin(Time.time() + id * 999, 25f, 0.05f) * Time.delta();
y += Mathf.cos(Time.time() + id * 999, 25f, 0.05f) * Time.delta();
if(velocity.len() <= 0.05f){
//rotation += Mathf.sin(Time.time() + id * 99, 10f, 2f * type.speed)*Time.delta();
}
}
protected void updateRotation(){
rotation = velocity.angle();
}
protected void circle(float circleLength){
circle(circleLength, type.speed);
}
protected void circle(float circleLength, float speed){
if(target == null) return;
Tmp.v1.set(target.getX() - x, target.getY() - y);
if(Tmp.v1.len() < circleLength){
Tmp.v1.rotate((circleLength - Tmp.v1.len()) / circleLength * 180f);
}
Tmp.v1.setLength(speed * Time.delta());
velocity.add(Tmp.v1);
}
protected void moveTo(float circleLength){
if(target == null) return;
Tmp.v1.set(target.getX() - x, target.getY() - y);
float length = circleLength <= 0.001f ? 1f : Mathf.clamp((dst(target) - circleLength) / 100f, -1f, 1f);
Tmp.v1.setLength(type.speed * Time.delta() * length);
if(length < -0.5f){
Tmp.v1.rotate(180f);
}else if(length < 0){
Tmp.v1.setZero();
}
velocity.add(Tmp.v1);
}
protected void attack(float circleLength){
Tmp.v1.set(target.getX() - x, target.getY() - y);
float ang = angleTo(target);
float diff = Angles.angleDist(ang, rotation);
if(diff > 100f && Tmp.v1.len() < circleLength){
Tmp.v1.setAngle(velocity.angle());
}else{
Tmp.v1.setAngle(Mathf.slerpDelta(velocity.angle(), Tmp.v1.angle(), 0.44f));
}
Tmp.v1.setLength(type.speed * Time.delta());
velocity.add(Tmp.v1);
}
}

View File

@@ -0,0 +1,266 @@
package mindustry.entities.type.base;
import arc.graphics.*;
import arc.graphics.g2d.*;
import arc.math.*;
import arc.math.geom.*;
import arc.util.*;
import mindustry.*;
import mindustry.ai.Pathfinder.*;
import mindustry.entities.*;
import mindustry.entities.bullet.*;
import mindustry.entities.type.*;
import mindustry.entities.units.*;
import mindustry.game.*;
import mindustry.type.*;
import mindustry.world.*;
import mindustry.world.blocks.*;
import mindustry.world.meta.*;
import static mindustry.Vars.*;
public class GroundUnit extends BaseUnit{
protected static Vector2 vec = new Vector2();
protected float walkTime;
protected float stuckTime;
protected float baseRotation;
public final UnitState
attack = new UnitState(){
public void entered(){
target = null;
}
public void update(){
TileEntity core = getClosestEnemyCore();
if(core == null){
Tile closestSpawn = getClosestSpawner();
if(closestSpawn == null || !withinDst(closestSpawn, Vars.state.rules.dropZoneRadius + 85f)){
moveToCore(PathTarget.enemyCores);
}
}else{
float dst = dst(core);
if(dst < getWeapon().bullet.range() / 1.1f){
target = core;
}
if(dst > getWeapon().bullet.range() * 0.5f){
moveToCore(PathTarget.enemyCores);
}
}
}
},
rally = new UnitState(){
public void update(){
Tile target = getClosest(BlockFlag.rally);
if(target != null && dst(target) > 80f){
moveToCore(PathTarget.rallyPoints);
}
}
},
retreat = new UnitState(){
public void entered(){
target = null;
}
public void update(){
moveAwayFromCore();
}
};
@Override
public void onCommand(UnitCommand command){
state.set(command == UnitCommand.retreat ? retreat :
command == UnitCommand.attack ? attack :
command == UnitCommand.rally ? rally :
null);
}
@Override
public void interpolate(){
super.interpolate();
if(interpolator.values.length > 1){
baseRotation = interpolator.values[1];
}
}
@Override
public void move(float x, float y){
float dst = Mathf.dst(x, y);
if(dst > 0.01f){
baseRotation = Mathf.slerp(baseRotation, Mathf.angle(x, y), type.baseRotateSpeed * (dst / type.speed));
}
super.move(x, y);
}
@Override
public UnitState getStartState(){
return attack;
}
@Override
public void update(){
super.update();
stuckTime = !vec.set(x, y).sub(lastPosition()).isZero(0.0001f) ? 0f : stuckTime + Time.delta();
if(!velocity.isZero()){
baseRotation = Mathf.slerpDelta(baseRotation, velocity.angle(), 0.05f);
}
if(stuckTime < 1f){
walkTime += Time.delta();
}
}
@Override
public Weapon getWeapon(){
return type.weapon;
}
@Override
public void draw(){
Draw.mixcol(Color.white, hitTime / hitDuration);
float ft = Mathf.sin(walkTime * type.speed * 5f, 6f, 2f + type.hitsize / 15f);
Floor floor = getFloorOn();
if(floor.isLiquid){
Draw.color(Color.white, floor.color, 0.5f);
}
for(int i : Mathf.signs){
Draw.rect(type.legRegion,
x + Angles.trnsx(baseRotation, ft * i),
y + Angles.trnsy(baseRotation, ft * i),
type.legRegion.getWidth() * i * Draw.scl, type.legRegion.getHeight() * Draw.scl - Mathf.clamp(ft * i, 0, 2), baseRotation - 90);
}
if(floor.isLiquid){
Draw.color(Color.white, floor.color, drownTime * 0.4f);
}else{
Draw.color(Color.white);
}
Draw.rect(type.baseRegion, x, y, baseRotation - 90);
Draw.rect(type.region, x, y, rotation - 90);
for(int i : Mathf.signs){
float tra = rotation - 90, trY = -type.weapon.getRecoil(this, i > 0) + type.weaponOffsetY;
float w = -i * type.weapon.region.getWidth() * Draw.scl;
Draw.rect(type.weapon.region,
x + Angles.trnsx(tra, getWeapon().width * i, trY),
y + Angles.trnsy(tra, getWeapon().width * i, trY), w, type.weapon.region.getHeight() * Draw.scl, rotation - 90);
}
Draw.mixcol();
}
@Override
public void behavior(){
if(!Units.invalidateTarget(target, this)){
if(dst(target) < getWeapon().bullet.range()){
rotate(angleTo(target));
if(Angles.near(angleTo(target), rotation, 13f)){
BulletType ammo = getWeapon().bullet;
Vector2 to = Predict.intercept(GroundUnit.this, target, ammo.speed);
getWeapon().update(GroundUnit.this, to.x, to.y);
}
}
}
}
@Override
public void updateTargeting(){
super.updateTargeting();
if(Units.invalidateTarget(target, team, x, y, Float.MAX_VALUE)){
target = null;
}
if(retarget()){
targetClosest();
}
}
protected void patrol(){
vec.trns(baseRotation, type.speed * Time.delta());
velocity.add(vec.x, vec.y);
vec.trns(baseRotation, type.hitsizeTile * 5);
Tile tile = world.tileWorld(x + vec.x, y + vec.y);
if((tile == null || tile.solid() || tile.floor().drownTime > 0 || tile.floor().isLiquid) || stuckTime > 10f){
baseRotation += Mathf.sign(id % 2 - 0.5f) * Time.delta() * 3f;
}
rotation = Mathf.slerpDelta(rotation, velocity.angle(), type.rotatespeed);
}
protected void circle(float circleLength){
if(target == null) return;
vec.set(target.getX() - x, target.getY() - y);
if(vec.len() < circleLength){
vec.rotate((circleLength - vec.len()) / circleLength * 180f);
}
vec.setLength(type.speed * Time.delta());
velocity.add(vec);
}
protected void moveToCore(PathTarget path){
Tile tile = world.tileWorld(x, y);
if(tile == null) return;
Tile targetTile = pathfinder.getTargetTile(tile, team, path);
if(tile == targetTile) return;
velocity.add(vec.trns(angleTo(targetTile), type.speed * Time.delta()));
if(Units.invalidateTarget(target, this)){
rotation = Mathf.slerpDelta(rotation, baseRotation, type.rotatespeed);
}
}
protected void moveAwayFromCore(){
Team enemy = null;
for(Team team : Vars.state.teams.enemiesOf(team)){
if(Vars.state.teams.isActive(team)){
enemy = team;
break;
}
}
if(enemy == null){
for(Team team : Vars.state.teams.enemiesOf(team)){
enemy = team;
break;
}
}
if(enemy == null) return;
Tile tile = world.tileWorld(x, y);
if(tile == null) return;
Tile targetTile = pathfinder.getTargetTile(tile, enemy, PathTarget.enemyCores);
TileEntity core = getClosestCore();
if(tile == targetTile || core == null || dst(core) < 120f) return;
velocity.add(vec.trns(angleTo(targetTile), type.speed * Time.delta()));
rotation = Mathf.slerpDelta(rotation, baseRotation, type.rotatespeed);
}
}

View File

@@ -0,0 +1,34 @@
package mindustry.entities.type.base;
import arc.graphics.g2d.Draw;
import arc.math.Angles;
import arc.math.Mathf;
import mindustry.entities.Units;
public class HoverUnit extends FlyingUnit{
@Override
public void drawWeapons(){
for(int i : Mathf.signs){
float tra = rotation - 90, trY = -getWeapon().getRecoil(this, i > 0) + type.weaponOffsetY;
float w = i > 0 ? -12 : 12;
float wx = x + Angles.trnsx(tra, getWeapon().width * i, trY), wy = y + Angles.trnsy(tra, getWeapon().width * i, trY);
int wi = (i + 1) / 2;
Draw.rect(getWeapon().region, wx, wy, w, 12, weaponAngles[wi] - 90);
}
}
@Override
protected void attack(float circleLength){
moveTo(circleLength);
}
@Override
protected void updateRotation(){
if(!Units.invalidateTarget(target, this)){
rotation = Mathf.slerpDelta(rotation, angleTo(target), type.rotatespeed);
}else{
rotation = Mathf.slerpDelta(rotation, velocity.angle(), type.baseRotateSpeed);
}
}
}

View File

@@ -0,0 +1,179 @@
package mindustry.entities.type.base;
import arc.math.Mathf;
import arc.util.Structs;
import mindustry.content.Blocks;
import mindustry.entities.traits.MinerTrait;
import mindustry.entities.type.TileEntity;
import mindustry.entities.units.UnitState;
import mindustry.gen.Call;
import mindustry.type.Item;
import mindustry.type.ItemType;
import mindustry.world.Pos;
import mindustry.world.Tile;
import java.io.*;
import static mindustry.Vars.*;
/** A drone that only mines.*/
public class MinerDrone extends BaseDrone implements MinerTrait{
protected Item targetItem;
protected Tile mineTile;
public final UnitState
mine = new UnitState(){
public void entered(){
target = null;
}
public void update(){
TileEntity entity = getClosestCore();
if(entity == null) return;
findItem();
//core full of the target item, do nothing
if(targetItem != null && entity.block.acceptStack(targetItem, 1, entity.tile, MinerDrone.this) == 0){
MinerDrone.this.clearItem();
return;
}
//if inventory is full, drop it off.
if(item.amount >= getItemCapacity() || (targetItem != null && !acceptsItem(targetItem))){
setState(drop);
}else{
if(retarget() && targetItem != null){
target = indexer.findClosestOre(x, y, targetItem);
}
if(target instanceof Tile){
moveTo(type.range / 1.5f);
if(dst(target) < type.range && mineTile != target){
setMineTile((Tile)target);
}
if(((Tile)target).block() != Blocks.air){
setState(drop);
}
}else{
//nothing to mine anymore, core full: circle spawnpoint
if(getSpawner() != null){
target = getSpawner();
circle(40f);
}
}
}
}
public void exited(){
setMineTile(null);
}
},
drop = new UnitState(){
public void entered(){
target = null;
}
public void update(){
if(item.amount == 0 || item.item.type != ItemType.material){
clearItem();
setState(mine);
return;
}
target = getClosestCore();
if(target == null) return;
TileEntity tile = (TileEntity)target;
if(dst(target) < type.range){
if(tile.tile.block().acceptStack(item.item, item.amount, tile.tile, MinerDrone.this) > 0){
Call.transferItemTo(item.item, item.amount, x, y, tile.tile);
}
clearItem();
setState(mine);
}
circle(type.range / 1.8f);
}
};
@Override
public UnitState getStartState(){
return mine;
}
@Override
public void update(){
super.update();
updateMining();
}
@Override
protected void updateRotation(){
if(mineTile != null && shouldRotate() && mineTile.dst(this) < type.range){
rotation = Mathf.slerpDelta(rotation, angleTo(mineTile), 0.3f);
}else{
rotation = Mathf.slerpDelta(rotation, velocity.angle(), 0.3f);
}
}
@Override
public boolean shouldRotate(){
return isMining();
}
@Override
public void drawOver(){
drawMining();
}
@Override
public boolean canMine(Item item){
return type.toMine.contains(item);
}
@Override
public float getMinePower(){
return type.minePower;
}
@Override
public Tile getMineTile(){
return mineTile;
}
@Override
public void setMineTile(Tile tile){
mineTile = tile;
}
@Override
public void write(DataOutput data) throws IOException{
super.write(data);
data.writeInt(mineTile == null || !state.is(mine) ? Pos.invalid : mineTile.pos());
}
@Override
public void read(DataInput data) throws IOException{
super.read(data);
mineTile = world.tile(data.readInt());
}
protected void findItem(){
TileEntity entity = getClosestCore();
if(entity == null){
return;
}
targetItem = Structs.findMin(type.toMine, indexer::hasOre, (a, b) -> -Integer.compare(entity.items.get(a), entity.items.get(b)));
}
}

View File

@@ -0,0 +1,73 @@
package mindustry.entities.type.base;
import mindustry.entities.Units;
import mindustry.entities.type.TileEntity;
import mindustry.entities.units.UnitState;
import mindustry.world.Pos;
import mindustry.world.Tile;
import mindustry.world.blocks.*;
import java.io.*;
import static mindustry.Vars.world;
public class RepairDrone extends BaseDrone{
public final UnitState repair = new UnitState(){
public void entered(){
target = null;
}
public void update(){
if(retarget()){
target = Units.findDamagedTile(team, x, y);
}
if(target instanceof TileEntity && ((TileEntity)target).block instanceof BuildBlock){
target = null;
}
if(target != null){
if(target.dst(RepairDrone.this) > type.range){
circle(type.range * 0.9f);
}else{
getWeapon().update(RepairDrone.this, target.getX(), target.getY());
}
}else{
//circle spawner if there's nothing to repair
if(getSpawner() != null){
target = getSpawner();
circle(type.range * 1.5f, type.speed/2f);
target = null;
}
}
}
};
@Override
public boolean shouldRotate(){
return target != null;
}
@Override
public UnitState getStartState(){
return repair;
}
@Override
public void write(DataOutput data) throws IOException{
super.write(data);
data.writeInt(state.is(repair) && target instanceof TileEntity ? ((TileEntity)target).tile.pos() : Pos.invalid);
}
@Override
public void read(DataInput data) throws IOException{
super.read(data);
Tile repairing = world.tile(data.readInt());
if(repairing != null){
target = repairing.entity;
}
}
}

View File

@@ -0,0 +1,24 @@
package mindustry.entities.units;
public class StateMachine{
private UnitState state;
public void update(){
if(state != null) state.update();
}
public void set(UnitState next){
if(next == state) return;
if(state != null) state.exited();
this.state = next;
if(next != null) next.entered();
}
public UnitState current(){
return state;
}
public boolean is(UnitState state){
return this.state == state;
}
}

View File

@@ -0,0 +1,160 @@
package mindustry.entities.units;
import arc.struct.Bits;
import arc.struct.*;
import arc.graphics.*;
import arc.util.*;
import arc.util.pooling.*;
import mindustry.content.*;
import mindustry.ctype.ContentType;
import mindustry.entities.traits.*;
import mindustry.entities.type.*;
import mindustry.type.*;
import java.io.*;
import static mindustry.Vars.content;
/** Class for controlling status effects on an entity. */
public class Statuses implements Saveable{
private static final StatusEntry globalResult = new StatusEntry();
private static final Array<StatusEntry> removals = new Array<>();
private Array<StatusEntry> statuses = new Array<>();
private Bits applied = new Bits(content.getBy(ContentType.status).size);
private float speedMultiplier;
private float damageMultiplier;
private float armorMultiplier;
public void handleApply(Unit unit, StatusEffect effect, float duration){
if(effect == StatusEffects.none || effect == null || unit.isImmune(effect)) return; //don't apply empty or immune effects
if(statuses.size > 0){
//check for opposite effects
for(StatusEntry entry : statuses){
//extend effect
if(entry.effect == effect){
entry.time = Math.max(entry.time, duration);
return;
}else if(entry.effect.reactsWith(effect)){ //find opposite
globalResult.effect = entry.effect;
entry.effect.getTransition(unit, effect, entry.time, duration, globalResult);
entry.time = globalResult.time;
if(globalResult.effect != entry.effect){
entry.effect = globalResult.effect;
}
//stop looking when one is found
return;
}
}
}
//otherwise, no opposites found, add direct effect
StatusEntry entry = Pools.obtain(StatusEntry.class, StatusEntry::new);
entry.set(effect, duration);
statuses.add(entry);
}
public Color getStatusColor(){
if(statuses.size == 0){
return Tmp.c1.set(Color.white);
}
float r = 0f, g = 0f, b = 0f;
for(StatusEntry entry : statuses){
r += entry.effect.color.r;
g += entry.effect.color.g;
b += entry.effect.color.b;
}
return Tmp.c1.set(r / statuses.size, g / statuses.size, b / statuses.size, 1f);
}
public void clear(){
statuses.clear();
}
public void update(Unit unit){
applied.clear();
speedMultiplier = damageMultiplier = armorMultiplier = 1f;
if(statuses.size == 0) return;
removals.clear();
for(StatusEntry entry : statuses){
entry.time = Math.max(entry.time - Time.delta(), 0);
applied.set(entry.effect.id);
if(entry.time <= 0){
Pools.free(entry);
removals.add(entry);
}else{
speedMultiplier *= entry.effect.speedMultiplier;
armorMultiplier *= entry.effect.armorMultiplier;
damageMultiplier *= entry.effect.damageMultiplier;
entry.effect.update(unit, entry.time);
}
}
if(removals.size > 0){
statuses.removeAll(removals, true);
}
}
public float getSpeedMultiplier(){
return speedMultiplier;
}
public float getDamageMultiplier(){
return damageMultiplier;
}
public float getArmorMultiplier(){
return armorMultiplier;
}
public boolean hasEffect(StatusEffect effect){
return applied.get(effect.id);
}
@Override
public void writeSave(DataOutput stream) throws IOException{
stream.writeByte(statuses.size);
for(StatusEntry entry : statuses){
stream.writeByte(entry.effect.id);
stream.writeFloat(entry.time);
}
}
@Override
public void readSave(DataInput stream, byte version) throws IOException{
for(StatusEntry effect : statuses){
Pools.free(effect);
}
statuses.clear();
byte amount = stream.readByte();
for(int i = 0; i < amount; i++){
byte id = stream.readByte();
float time = stream.readFloat();
StatusEntry entry = Pools.obtain(StatusEntry.class, StatusEntry::new);
entry.set(content.getByID(ContentType.status, id), time);
statuses.add(entry);
}
}
public static class StatusEntry{
public StatusEffect effect;
public float time;
public StatusEntry set(StatusEffect effect, float time){
this.effect = effect;
this.time = time;
return this;
}
}
}

View File

@@ -0,0 +1,18 @@
package mindustry.entities.units;
import arc.*;
public enum UnitCommand{
attack, retreat, rally;
private final String localized;
public static final UnitCommand[] all = values();
UnitCommand(){
localized = Core.bundle.get("command." + name());
}
public String localized(){
return localized;
}
}

View File

@@ -0,0 +1,47 @@
package mindustry.entities.units;
import arc.math.Mathf;
import mindustry.Vars;
import mindustry.content.Items;
import mindustry.entities.type.BaseUnit;
import mindustry.entities.type.TileEntity;
import mindustry.gen.Call;
import mindustry.type.Item;
public class UnitDrops{
private static Item[] dropTable;
public static void dropItems(BaseUnit unit){
//items only dropped in waves for enemy team
if(unit.getTeam() != Vars.waveTeam || !Vars.state.rules.unitDrops){
return;
}
TileEntity core = unit.getClosestEnemyCore();
if(core == null || core.dst(unit) > Vars.mineTransferRange){
return;
}
if(dropTable == null){
dropTable = new Item[]{Items.titanium, Items.silicon, Items.lead, Items.copper};
}
for(int i = 0; i < 3; i++){
for(Item item : dropTable){
//only drop unlocked items
if(!Vars.headless && !Vars.data.isUnlocked(item)){
continue;
}
if(Mathf.chance(0.03)){
int amount = Mathf.random(20, 40);
amount = core.tile.block().acceptStack(item, amount, core.tile, null);
if(amount > 0){
Call.transferItemTo(item, amount, unit.x + Mathf.range(2f), unit.y + Mathf.range(2f), core.tile);
}
}
}
}
}
}

View File

@@ -0,0 +1,12 @@
package mindustry.entities.units;
public interface UnitState{
default void entered(){
}
default void exited(){
}
default void update(){
}
}