too many things to list

This commit is contained in:
Anuken
2020-10-15 13:44:20 -04:00
parent fb0179da95
commit 86c2fe8805
52 changed files with 665 additions and 117 deletions

View File

@@ -3,11 +3,18 @@ package mindustry.maps;
import arc.math.*;
import arc.math.geom.*;
import arc.struct.*;
import arc.util.*;
import mindustry.ai.*;
import mindustry.content.*;
import mindustry.entities.*;
import mindustry.entities.abilities.*;
import mindustry.game.*;
import mindustry.gen.*;
import mindustry.logic.*;
import mindustry.world.*;
import mindustry.world.blocks.defense.*;
import mindustry.world.blocks.defense.turrets.*;
import mindustry.world.blocks.defense.turrets.Turret.*;
import mindustry.world.blocks.storage.*;
import static mindustry.Vars.*;
@@ -15,6 +22,264 @@ import static mindustry.Vars.*;
public class SectorDamage{
//direct damage is for testing only
private static final boolean direct = false, rubble = true;
private static final int maxWavesSimulated = 50;
/** @return calculated capture progress of the enemy */
public static float getDamage(SectorInfo info, float waveSpace, int wave, int wavesPassed){
float health = info.sumHealth;
//this approach is O(n), it simulates every wave passing.
//other approaches can assume all the waves come as one, but that's not as fair.
if(wavesPassed > 0){
int waveBegin = wave;
int waveEnd = wave + wavesPassed;
//do not simulate every single wave if there's too many
if(wavesPassed > maxWavesSimulated){
waveBegin = waveEnd - maxWavesSimulated;
}
for(int i = waveBegin; i <= waveEnd; i++){
float efficiency = health / info.sumHealth;
float dps = info.sumDps * efficiency;
float rps = info.sumRps * efficiency;
float enemyDps = info.waveDpsBase + info.waveDpsSlope * (i);
float enemyHealth = info.waveHealthBase + info.waveHealthSlope * (i);
//happens due to certain regressions
if(enemyHealth < 0 || enemyDps < 0) continue;
//calculate time to destroy both sides
float timeDestroyEnemy = dps <= 0.0001f ? Float.POSITIVE_INFINITY : enemyHealth / dps; //if dps == 0, this is infinity
float timeDestroyBase = health / (enemyDps - rps); //if regen > enemyDps this is negative
//sector is lost, enemy took too long.
if(timeDestroyEnemy > timeDestroyBase){
health = 0f;
break;
}
//otherwise, the enemy shoots for timeDestroyEnemy seconds, so calculate damage taken
float damageTaken = timeDestroyEnemy * (enemyDps - rps);
//damage the base.
health -= damageTaken;
//regen health after wave.
health = Math.min(health + rps / 60f * waveSpace, info.sumHealth);
}
}
return 1f - Mathf.clamp(health / info.sumHealth);
}
/** Applies wave damage based on sector parameters. */
public static void applyCalculatedDamage(int wavesPassed){
//calculate base damage fraction
float damage = getDamage(state.secinfo, state.rules.waveSpacing, state.wave, wavesPassed);
//scaled damage has a power component to make it seem a little more realistic (as systems fail, enemy capturing gets easier and easier)
float scaled = Mathf.pow(damage, 1.5f);
//apply damage to units
float unitDamage = damage * state.secinfo.sumHealth;
Tile spawn = spawner.getFirstSpawn();
//damage only units near the spawn point
if(spawn != null){
Seq<Unit> allies = new Seq<>();
for(Unit ally : Groups.unit){
if(ally.team == state.rules.defaultTeam && ally.within(spawn, state.rules.dropZoneRadius * 2.5f)){
allies.add(ally);
}
}
allies.sort(u -> u.dst2(spawn));
//damage units one by one, not uniformly
for(var u : allies){
if(u.health < unitDamage){
u.remove();
unitDamage -= u.health;
}else{
u.health -= unitDamage;
break;
}
}
}
//finally apply scaled damage
apply(scaled);
}
/** Calculates damage simulation parameters before a game is saved. */
public static void writeParameters(SectorInfo info){
Building core = state.rules.defaultTeam.core();
Seq<Tile> spawns = new Seq<>();
spawner.eachGroundSpawn((x, y) -> spawns.add(world.tile(x, y)));
if(core == null || spawns.isEmpty()) return;
Tile start = spawns.first();
Time.mark();
var field = pathfinder.getField(state.rules.waveTeam, Pathfinder.costGround, Pathfinder.fieldCore);
Seq<Tile> path = new Seq<>();
boolean found = false;
if(field != null && field.weights != null){
int[][] weights = field.weights;
int count = 0;
Tile current = start;
while(count < world.width() * world.height()){
int minCost = Integer.MAX_VALUE;
int cx = current.x, cy = current.y;
for(Point2 p : Geometry.d4){
int nx = cx + p.x, ny = cy + p.y;
Tile other = world.tile(nx, ny);
if(other != null && weights[nx][ny] < minCost && weights[nx][ny] != -1){
minCost = weights[nx][ny];
current = other;
}
}
path.add(current);
if(current.build == core){
found = true;
break;
}
count ++;
}
}
if(!found){
path = Astar.pathfind(start, core.tile, SectorDamage::cost, t -> !(t.block().isStatic() && t.solid()));
}
//create sparse tile array for fast range query
int sparseSkip = 6;
//TODO if this is slow, use a quadtree
Seq<Tile> sparse = new Seq<>(path.size / sparseSkip + 1);
for(int i = 0; i < path.size; i++){
if(i % sparseSkip == 0){
sparse.add(path.get(i));
}
}
//regen is in health per second
//dps is per second
float sumHealth = 0f, sumRps = 0f, sumDps = 0f;
float totalPathBuild = 0;
//first, calculate the total health of blocks in the path
for(Tile t : path){
int radius = 2;
//radius is square.
for(int dx = -radius; dx <= radius; dx++){
for(int dy = -radius; dy <= radius; dy++){
int wx = dx + t.x, wy = dy + t.y;
if(wx >= 0 && wy >= 0 && wx < world.width() && wy < world.height()){
Tile tile = world.rawTile(wx, wy);
if(tile.build != null && tile.team() == state.rules.defaultTeam){
//health is divided by block size, because multiblocks are counted multiple times.
sumHealth += tile.build.health / tile.block().size;
totalPathBuild += 1f / tile.block().size;
}
}
}
}
}
float avgHealth = totalPathBuild <= 1 ? sumHealth : sumHealth / totalPathBuild;
//block dps + regen + extra health/shields
for(Building build : Groups.build){
float e = build.efficiency();
if(e > 0.08f){
if(build.team == state.rules.defaultTeam && build instanceof Ranged ranged && sparse.contains(t -> t.within(build, ranged.range()))){
if(build.block instanceof Turret t && build instanceof TurretBuild b && b.hasAmmo()){
sumDps += t.shots / t.reloadTime * 60f * b.peekAmmo().estimateDPS() * e;
}
if(build.block instanceof MendProjector m){
sumRps += m.healPercent / m.reload * avgHealth * 60f / 100f * e;
}
if(build.block instanceof ForceProjector f){
sumHealth += f.breakage * e;
sumRps += 1f * e;
}
}
}
}
float curEnemyHealth = 0f, curEnemyDps = 0f;
//unit regen + health + dps
for(Unit unit : Groups.unit){
//skip player
if(unit.isPlayer()) continue;
if(unit.team == state.rules.defaultTeam){
sumHealth += unit.health + unit.shield;
sumDps += unit.type().dpsEstimate;
if(unit.abilities.find(a -> a instanceof HealFieldAbility) instanceof HealFieldAbility h){
sumRps += h.amount / h.reload * 60f;
}
}else{
curEnemyDps += unit.type().dpsEstimate;
curEnemyHealth += unit.health;
}
}
//calculate DPS and health for the next few waves and store in list
var reg = new LinearRegression();
Seq<Vec2> waveDps = new Seq<>(), waveHealth = new Seq<>();
for(int wave = state.wave, i = 0; i < 3; wave += (1 + i++)){
float sumWaveDps = 0f, sumWaveHealth = 0f;
//first wave has to take into account current dps
if(wave == state.wave){
sumWaveDps += curEnemyDps;
sumWaveHealth += curEnemyHealth;
}
for(SpawnGroup group : state.rules.spawns){
int spawned = group.getSpawned(wave);
if(spawned <= 0) continue;
sumWaveHealth += spawned * (group.getShield(wave) + group.type.health);
sumWaveDps += spawned * group.type.dpsEstimate;
}
waveDps.add(new Vec2(wave, sumWaveDps));
waveHealth.add(new Vec2(wave, sumWaveHealth));
}
//calculate linear regression of the wave data and store it
reg.calculate(waveHealth);
info.waveHealthBase = reg.intercept;
info.waveHealthSlope = reg.slope;
reg.calculate(waveDps);
info.waveDpsBase = reg.intercept;
info.waveDpsSlope = reg.slope;
info.sumHealth = sumHealth;
info.sumDps = sumDps;
info.sumRps = sumRps;
//finally, find an equation to put it all together and produce a 0-1 number
//due to the way most defenses are structured, this number will likely need a ^4 power or so
}
public static void apply(float fraction){
Tiles tiles = world.tiles;
@@ -35,22 +300,62 @@ public class SectorDamage{
if(core != null && !frontier.isEmpty()){
for(Tile spawner : frontier){
//find path from spawn to core
//TODO this is broken
Seq<Tile> path = Astar.pathfind(spawner, core.tile, SectorDamage::cost, t -> !(t.block().isStatic() && t.solid()));
int amount = (int)(path.size * fraction);
for(int i = 0; i < amount; i++){
Tile t = path.get(i);
Geometry.circle(t.x, t.y, tiles.width, tiles.height, 5, (cx, cy) -> {
Tile other = tiles.getn(cx, cy);
//just remove all the buildings in the way - as long as they're not cores!
if(other.build != null && other.team() == state.rules.defaultTeam && !(other.block() instanceof CoreBlock)){
if(rubble && !other.floor().solid && !other.floor().isLiquid && Mathf.chance(0.4)){
Effect.rubble(other.build.x, other.build.y, other.block().size);
}
Seq<Building> removal = new Seq<>();
other.remove();
int radius = 3;
//only penetrate a certain % by health, not by distance
float totalHealth = path.sumf(t -> {
float s = 0;
for(int dx = -radius; dx <= radius; dx++){
for(int dy = -radius; dy <= radius; dy++){
int wx = dx + t.x, wy = dy + t.y;
if(wx >= 0 && wy >= 0 && wx < world.width() && wy < world.height() && Mathf.within(dx, dy, radius)){
Tile other = world.rawTile(wx, wy);
s += other.team() == state.rules.defaultTeam ? other.build.health / other.block().size : 0f;
}
}
});
}
return s;
});
float targetHealth = totalHealth * fraction;
float healthCount = 0;
out:
for(int i = 0; i < path.size && healthCount < targetHealth; i++){
Tile t = path.get(i);
for(int dx = -radius; dx <= radius; dx++){
for(int dy = -radius; dy <= radius; dy++){
int wx = dx + t.x, wy = dy + t.y;
if(wx >= 0 && wy >= 0 && wx < world.width() && wy < world.height() && Mathf.within(dx, dy, radius)){
Tile other = world.rawTile(wx, wy);
//just remove all the buildings in the way - as long as they're not cores
if(other.build != null && other.team() == state.rules.defaultTeam && !(other.block() instanceof CoreBlock)){
if(rubble && !other.floor().solid && !other.floor().isLiquid && Mathf.chance(0.4)){
Effect.rubble(other.build.x, other.build.y, other.block().size);
}
//since the whole block is removed, count the whole health
healthCount += other.build.health;
removal.add(other.build);
if(healthCount >= targetHealth){
break out;
}
}
}
}
}
}
for(Building r : removal){
if(r.tile.build == r){
r.tile.remove();
}
}
}
}