Skip to content

Commit

Permalink
Probably fix buildplan issues due to weird code
Browse files Browse the repository at this point in the history
  • Loading branch information
buthed010203 committed May 26, 2024
1 parent 0743468 commit 026a5ea
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -416,15 +416,17 @@ public void process(RoundEnvironment env) throws Exception{

//add all methods from components
for(ObjectMap.Entry<String, Seq<Smethod>> entry : methods){
if(entry.value.contains(m -> m.has(Replace.class))){
//check replacements
if(entry.value.count(m -> m.has(Replace.class)) > 1){
err("Type " + type + " has multiple components replacing method " + entry.key + ".");
Smethod replacement = null;
for(Smethod method : entry.value){
if(method.has(Replace.class)){
if(replacement == null) replacement = method;
else{ //multiple replacements for one method
err("Type " + type + " has multiple components replacing method " + entry.key + ".");
break;
}
}
Smethod base = entry.value.find(m -> m.has(Replace.class));
entry.value.clear();
entry.value.add(base);
}
if(replacement != null) entry.value.clear().add(replacement);

//check multi return
if(entry.value.count(m -> !m.isAny(Modifier.NATIVE, Modifier.ABSTRACT) && !m.isVoid()) > 1){
Expand Down
2 changes: 2 additions & 0 deletions core/src/mindustry/ai/BaseRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public Seq<BasePart> forResource(Content item){
}

public void load(){
Time.mark();
cores.clear();
parts.clear();
reqParts.clear();
Expand Down Expand Up @@ -111,6 +112,7 @@ public void load(){
cores.sort(b -> b.tier);
parts.sort();
reqParts.each((key, arr) -> arr.sort());
Log.debug("Bases in @ms", Time.elapsed());
}

public static class BasePart implements Comparable<BasePart>{
Expand Down
2 changes: 1 addition & 1 deletion core/src/mindustry/client/navigation/AStarNavigator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import kotlin.math.*
// and modified

object AStarNavigator : Navigator() {
private val pool = Pools.get(PositionWaypoint::class.java) { PositionWaypoint() }
private val pool = Pools.get(PositionWaypoint::class.java, ::PositionWaypoint)
private var grid: Array<Cell> = emptyArray()
private var gridSize = Point2()
private var open = BinaryHeap<Cell>(65_536, false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import kotlin.math.*
// https://arxiv.org/ftp/arxiv/papers/1506/1506.01864.pdf the stuff looks familiar

object AStarNavigatorOptimised : Navigator() {
private val pool = Pools.get(PositionWaypoint::class.java) { PositionWaypoint() }
private val pool = Pools.get(PositionWaypoint::class.java, ::PositionWaypoint)
private var grid: Array<Cell> = emptyArray()
private var gridSize = Point2()
private var open = BinaryHeap<Cell>(65_536, false)
Expand Down
19 changes: 10 additions & 9 deletions core/src/mindustry/client/navigation/Navigator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,14 @@ abstract class Navigator {
start.clamp(0f, 0f, world.unitWidth().toFloat(), world.unitHeight().toFloat())
end.clamp(0f, 0f, world.unitWidth().toFloat(), world.unitHeight().toFloat())
val additionalRadius = player.unit().hitSize / 2 + tilesize
val pool = Pools.get(Circle::class.java, ::Circle)

// Turrets and units FINISHME: Turrets should probably not use this system
if (player.unit().type.targetable(player.unit(), player.team()) && player.unit().type.hittable(player.unit())) {
for (turret in obstacles) {
if (turret.isObstacle()) {
realObstacles.add(
Pools.obtain(Circle::class.java) { Circle() }.set(
pool.obtain().set(
turret.x(),
turret.y(),
turret.range() + additionalRadius
Expand All @@ -59,7 +60,7 @@ abstract class Navigator {
if (state.hasSpawns()) {
spawner.spawns.each { spawn -> // Use .each() as this is on the client thread and the iterator isn't thread safe. Each works fine as this Seq should never be edited at runtime. This might actually be a bad assumption to make? Idk
realObstacles.add(
Pools.obtain(Circle::class.java) { Circle() }.set(
pool.obtain().set(
spawn.worldx(),
spawn.worldy(),
state.rules.dropZoneRadius + additionalRadius
Expand All @@ -69,13 +70,13 @@ abstract class Navigator {
}

// Shield projectors
for (team in state.teams.active) {
if (team === player.team()) continue
arrayOf(team.getBuildings(Blocks.shieldProjector), team.getBuildings(Blocks.largeShieldProjector)).forEach { shields ->
val radius = ((shields.firstOpt()?.block ?: return@forEach) as BaseShield).radius + additionalRadius
for (shield in shields) {
state.teams.active.each { team ->
if (team === player.team()) return@each
for (block in BaseShield.baseShields) {
val radius = block.radius + additionalRadius
team.getBuildings(block).each { shield ->
realObstacles.add(
Pools.obtain(Circle::class.java) { Circle() }.set(
pool.obtain().set(
shield.x,
shield.y,
radius
Expand Down Expand Up @@ -112,7 +113,7 @@ abstract class Navigator {
!canBoost && solidity?.solid(x, y) ?: false && // Units that cannot hover will check for solid blocks
world.tiles.getn(x, y).run { build === null || build.team !== player.team() || !block().teamPassable } // Ignore teamPassable blocks such as erekir blastDoors
}
Pools.freeAll(realObstacles)
Pools.freeAll(realObstacles, true)
realObstacles.clear()
return ret
}
Expand Down
2 changes: 1 addition & 1 deletion core/src/mindustry/client/navigation/Path.kt
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ abstract class Path {
// }
// }
val path = Navigation.navigator.navigate(v1, v2.set(destX, destY), Navigation.getEnts())
Pools.freeAll(filter)
Pools.freeAll(filter, true)
filter.clear()
if (!Vars.player.unit().isNull) v1.set(Vars.player.unit())
if (path.isNotEmpty() && (targetPos.within(destX, destY, 1F) || (Navigation.currentlyFollowing != null && Navigation.currentlyFollowing !is WaypointPath<*>))) { // Same destination
Expand Down
12 changes: 2 additions & 10 deletions core/src/mindustry/entities/units/BuildPlan.java
Original file line number Diff line number Diff line change
Expand Up @@ -233,16 +233,8 @@ public void hitbox(Rect out){
}
}

public Rect bounds(Rect rect) {
return bounds(rect, false);
}

public Rect bounds(Rect rect, boolean allowBreak){
if(breaking && !allowBreak){
return rect.set(-100f, -100f, 0f, 0f);
}else{
return block.bounds(x, y, rect);
}
public Rect bounds(Rect rect){
return block.bounds(x, y, rect);
}

@Override
Expand Down
43 changes: 19 additions & 24 deletions core/src/mindustry/input/InputHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -1241,15 +1241,13 @@ protected void drawFreezeSelection(int x1, int y1, int x2, int y2, int maxLength
Draw.color(Pal.freeze);
Lines.stroke(1f);

for(BuildPlan plan: player.unit().plans()){
if(plan.breaking) continue;
if(plan.bounds(Tmp.r2).overlaps(Tmp.r1)){
for(BuildPlan plan : player.unit().plans()){
if(!plan.breaking && plan.bounds(Tmp.r2).overlaps(Tmp.r1)){
drawFreezing(plan);
}
}
for(BuildPlan plan: selectPlans){
if(plan.breaking) continue;
if(plan.bounds(Tmp.r2).overlaps(Tmp.r1)){
for(BuildPlan plan : selectPlans){
if(!plan.breaking && plan.bounds(Tmp.r2).overlaps(Tmp.r1)){
drawFreezing(plan);
}
}
Expand All @@ -1276,15 +1274,13 @@ protected void drawBreakSelection(int x1, int y1, int x2, int y2, int maxLength)
Lines.stroke(1f);

for(var plan : player.unit().plans()){
if(plan.breaking) continue;
if(plan.bounds(Tmp.r2).overlaps(Tmp.r1)){
if(!plan.breaking && plan.bounds(Tmp.r2).overlaps(Tmp.r1)){
drawBreaking(plan);
}
}

for(var plan : selectPlans){
if(plan.breaking) continue;
if(plan.bounds(Tmp.r2).overlaps(Tmp.r1)){
if(!plan.breaking && plan.bounds(Tmp.r2).overlaps(Tmp.r1)){
drawBreaking(plan);
}
}
Expand Down Expand Up @@ -1385,10 +1381,11 @@ public void flushPlans(Seq<BuildPlan> plans, boolean freeze, boolean force, bool
if (plan.block == null) continue;

if (removeFrozen) {
plan.bounds(Tmp.r1);
Iterator<BuildPlan> it = frozenPlans.iterator();
while(it.hasNext()){
BuildPlan frz = it.next();
if(plan.bounds(Tmp.r1, true).overlaps(frz.bounds(Tmp.r2, true))){
if(Tmp.r1.overlaps(frz.bounds(Tmp.r2))){
it.remove();
}
}
Expand Down Expand Up @@ -1594,15 +1591,15 @@ protected void removeSelectionPlans(int x1, int y1, int x2, int y2, int maxLengt
Iterator<BuildPlan> it = player.unit().plans().iterator();
while(it.hasNext()){
BuildPlan plan = it.next();
if(plan.bounds(Tmp.r2, true).overlaps(Tmp.r1)){
if(plan.bounds(Tmp.r2).overlaps(Tmp.r1)){
it.remove();
}
}

it = selectPlans.iterator();
while(it.hasNext()){
BuildPlan plan = it.next();
if(plan.bounds(Tmp.r2, true).overlaps(Tmp.r1)){
if(plan.bounds(Tmp.r2).overlaps(Tmp.r1)){
it.remove();
}
}
Expand All @@ -1611,7 +1608,7 @@ protected void removeSelectionPlans(int x1, int y1, int x2, int y2, int maxLengt
it = frozenPlans.iterator();
while (it.hasNext()) {
BuildPlan plan = it.next();
if (plan.bounds(Tmp.r2, true).overlaps(Tmp.r1)) it.remove();
if (plan.bounds(Tmp.r2).overlaps(Tmp.r1)) it.remove();
}
}
}
Expand All @@ -1637,25 +1634,23 @@ protected void freezeSelection(int x1, int y1, int x2, int y2, boolean flush, in
Tmp.r1.set(result.x * tilesize, result.y * tilesize, (result.x2 - result.x) * tilesize, (result.y2 - result.y) * tilesize);

for(BuildPlan plan : player.unit().plans()){
if(plan.bounds(Tmp.r2, true).overlaps(Tmp.r1)) tmpFrozenPlans.add(plan);
if(plan.bounds(Tmp.r2).overlaps(Tmp.r1)) tmpFrozenPlans.add(plan);
}

for(BuildPlan plan : selectPlans){
if(plan.bounds(Tmp.r2, true).overlaps(Tmp.r1)) tmpFrozenPlans.add(plan);
if(plan.bounds(Tmp.r2).overlaps(Tmp.r1)) tmpFrozenPlans.add(plan);
}

Seq<BuildPlan> unfreeze = new Seq<>();
for(BuildPlan plan : frozenPlans){
if(plan.bounds(Tmp.r2, true).overlaps(Tmp.r1)) unfreeze.add(plan);
if(plan.bounds(Tmp.r2).overlaps(Tmp.r1)) unfreeze.add(plan);
}

Iterator<BuildPlan> it1, it2;
if(unfreeze.size > tmpFrozenPlans.size) flushPlans(unfreeze, false, false, true);
else{
it1 = player.unit().plans().iterator();
it2 = selectPlans.iterator();
for (BuildPlan frz : tmpFrozenPlans) {
if(checkFreezeSelectionHasNext(frz, it1)) continue;
if(unfreeze.size > tmpFrozenPlans.size) flushPlans(unfreeze, false, false, true); // Unfreeze the selection when there's more frozen blocks in the area
else{ // If there's fewer frozen blocks than unfrozen ones, we freeze the selection
Iterator<BuildPlan> it1 = player.unit().plans().iterator(), it2 = selectPlans.iterator();
for(BuildPlan frz : tmpFrozenPlans){
if(checkFreezeSelectionHasNext(frz, it1)) continue; // Player plans contains frz: remove it and continue.
if(/*!itHasNext implied*/ it2 != null){
it1 = it2;
it2 = null; // swap it2 into it1, continue iterating through without changing frz
Expand Down
6 changes: 6 additions & 0 deletions core/src/mindustry/world/blocks/defense/BaseShield.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import arc.graphics.*;
import arc.graphics.g2d.*;
import arc.math.*;
import arc.struct.*;
import arc.util.*;
import arc.util.io.*;
import mindustry.content.*;
Expand All @@ -20,6 +21,8 @@ public class BaseShield extends Block{
public float radius = 200f;
public int sides = 24;

public static Seq<BaseShield> baseShields = new Seq<>(2);

protected static BaseShieldBuild paramBuild;
//protected static Effect paramEffect;
protected static final Cons<Bullet> bulletConsumer = bullet -> {
Expand Down Expand Up @@ -60,6 +63,9 @@ public BaseShield(String name){
hasPower = true;
update = solid = true;
rebuildable = false;

baseShields.add(this);
if(baseShields.size > 2) baseShields.shrink(); // Modded shields will grow the seq, we may as well just keep it as small as we can
}

@Override
Expand Down

0 comments on commit 026a5ea

Please sign in to comment.