From 0d6657a1e805b079503bc70f8cc7d9a1c804b0d0 Mon Sep 17 00:00:00 2001 From: Anuken Date: Sun, 28 Jun 2020 18:55:42 -0400 Subject: [PATCH] Idle sector resource production --- core/src/mindustry/core/Logic.java | 22 +++++++++- core/src/mindustry/game/SectorInfo.java | 40 +++++++++++++++--- core/src/mindustry/game/Universe.java | 8 +--- .../mindustry/ui/dialogs/PlanetDialog.java | 5 ++- .../mindustry/ui/dialogs/ResourcesDialog.java | 41 ------------------- 5 files changed, 61 insertions(+), 55 deletions(-) delete mode 100644 core/src/mindustry/ui/dialogs/ResourcesDialog.java diff --git a/core/src/mindustry/core/Logic.java b/core/src/mindustry/core/Logic.java index 848749871d..de66f19fba 100644 --- a/core/src/mindustry/core/Logic.java +++ b/core/src/mindustry/core/Logic.java @@ -86,8 +86,26 @@ public class Logic implements ApplicationListener{ //when loading a 'damaged' sector, propagate the damage Events.on(WorldLoadEvent.class, e -> { - if(state.isCampaign() && state.rules.sector.hasWaves() && state.rules.sector.getTurnsPassed() > 0){ - SectorDamage.apply(state.rules.sector.getTurnsPassed()); + if(state.isCampaign() && state.rules.sector.getTurnsPassed() > 0){ + int passed = state.rules.sector.getTurnsPassed(); + Building core = state.rules.defaultTeam.core(); + + if(state.rules.sector.hasWaves()){ + SectorDamage.apply(passed); + } + + + //add resources based on turns passed + if(state.rules.sector.save != null && core != null){ + + state.rules.sector.save.meta.secinfo.production.each((item, stat) -> { + core.items.add(item, (int)(stat.mean * passed)); + + //ensure positive items + if(core.items.get(item) < 0) core.items.set(item, 0); + }); + } + state.rules.sector.setTurnsPassed(0); } diff --git a/core/src/mindustry/game/SectorInfo.java b/core/src/mindustry/game/SectorInfo.java index 7361acdad7..f484515a83 100644 --- a/core/src/mindustry/game/SectorInfo.java +++ b/core/src/mindustry/game/SectorInfo.java @@ -12,10 +12,12 @@ import mindustry.world.modules.*; import static mindustry.Vars.*; public class SectorInfo{ - /** export window size in seconds */ - private static final int exportWindow = 60; + /** average window size in samples */ + private static final int valueWindow = 60; /** refresh period of export in ticks */ private static final float refreshPeriod = 60; + /** Core input statistics. */ + public ObjectMap production = new ObjectMap<>(); /** Export statistics. */ public ObjectMap export = new ObjectMap<>(); /** Items stored in all cores. */ @@ -70,7 +72,8 @@ public class SectorInfo{ storageCapacity = entity != null ? entity.storageCapacity : 0; } - /** Update averages of various stats. */ + /** Update averages of various stats. + * Called every frame. */ public void update(){ //create last stored core items if(lastCoreItems == null){ @@ -78,10 +81,12 @@ public class SectorInfo{ updateCoreDeltas(); } + CoreEntity ent = state.rules.defaultTeam.core(); + //refresh throughput if(time.get(refreshPeriod)){ - CoreEntity ent = state.rules.defaultTeam.core(); + //refresh export export.each((item, stat) -> { //initialize stat after loading if(!stat.loaded){ @@ -98,10 +103,35 @@ public class SectorInfo{ stat.mean = stat.means.rawMean(); }); + //refresh core items + for(Item item : content.items()){ + ExportStat stat = production.get(item, ExportStat::new); + if(!stat.loaded){ + stat.means.fill(stat.mean); + stat.loaded = true; + } + + //get item delta + //TODO is preventing negative production a good idea? + int delta = Math.max((ent == null ? 0 : ent.items.get(item)) - lastCoreItems[item.id], 0); + + //store means + stat.means.add(delta); + stat.mean = stat.means.rawMean(); + } + updateCoreDeltas(); } } + /** @return the items in this sector now, taking into account production. */ + public ObjectIntMap getCurrentItems(float turnsPassed){ + ObjectIntMap map = new ObjectIntMap<>(); + map.putAll(coreItems); + production.each((item, stat) -> map.increment(item, (int)(stat.mean * turnsPassed))); + return map; + } + private void updateCoreDeltas(){ CoreEntity ent = state.rules.defaultTeam.core(); for(int i = 0; i < lastCoreItems.length; i++){ @@ -117,7 +147,7 @@ public class SectorInfo{ public static class ExportStat{ public transient float counter; - public transient WindowedMean means = new WindowedMean(exportWindow); + public transient WindowedMean means = new WindowedMean(valueWindow); public transient boolean loaded; public float mean; } diff --git a/core/src/mindustry/game/Universe.java b/core/src/mindustry/game/Universe.java index c5a1824542..94bcc78322 100644 --- a/core/src/mindustry/game/Universe.java +++ b/core/src/mindustry/game/Universe.java @@ -152,8 +152,8 @@ public class Universe{ //TODO a turn passing may break the core; detect this, send an event and mark the sector as having no base! for(Planet planet : content.planets()){ for(Sector sector : planet.sectors){ - //attacks happen even for sectors without bases - stuff still gets destroyed - if(!sector.isBeingPlayed() && sector.hasSave() && sector.hasWaves()){ + //update turns passed for all sectors + if(!sector.isBeingPlayed() && sector.hasSave()){ sector.setTurnsPassed(sector.getTurnsPassed() + 1); } } @@ -162,10 +162,6 @@ public class Universe{ //calculate passive item generation //TODO make exports only update for sector with items //TODO items should be added directly to cores! - int[] exports = getTotalExports(); - for(int i = 0; i < exports.length; i++){ - //data.addItem(content.item(i), exports[i]); - } Events.fire(new TurnEvent()); } diff --git a/core/src/mindustry/ui/dialogs/PlanetDialog.java b/core/src/mindustry/ui/dialogs/PlanetDialog.java index ce81fe63f0..735c3b5dc8 100644 --- a/core/src/mindustry/ui/dialogs/PlanetDialog.java +++ b/core/src/mindustry/ui/dialogs/PlanetDialog.java @@ -10,6 +10,7 @@ import arc.math.geom.*; import arc.scene.event.*; import arc.scene.ui.*; import arc.scene.ui.layout.*; +import arc.struct.*; import arc.util.*; import arc.util.ArcAnnotate.*; import mindustry.content.*; @@ -307,9 +308,11 @@ public class PlanetDialog extends BaseDialog implements PlanetInterfaceRenderer{ t.left(); t.table(res -> { + ObjectIntMap map = sector.save.meta.secinfo.getCurrentItems(sector.getTurnsPassed()); + int i = 0; for(Item item : content.items()){ - int amount = sector.save.meta.secinfo.coreItems.get(item); + int amount = map.get(item); if(amount > 0){ res.image(item.icon(Cicon.small)).padRight(3); res.add(ui.formatAmount(amount)).color(Color.lightGray); diff --git a/core/src/mindustry/ui/dialogs/ResourcesDialog.java b/core/src/mindustry/ui/dialogs/ResourcesDialog.java deleted file mode 100644 index e92713f692..0000000000 --- a/core/src/mindustry/ui/dialogs/ResourcesDialog.java +++ /dev/null @@ -1,41 +0,0 @@ -package mindustry.ui.dialogs; - -import mindustry.gen.*; -import mindustry.type.*; -import mindustry.ui.*; - -import static mindustry.Vars.*; - -public class ResourcesDialog extends BaseDialog{ - - public ResourcesDialog(){ - super("//TODO resources"); - shown(this::setup); - addCloseButton(); - } - - void setup(){ - cont.clear(); - - cont.table(Tex.button, t -> { - t.left(); - t.margin(10f); - int[] exports = universe.getTotalExports(); - for(Item item : content.items()){ - //TODO display total items - if(exports[item.id] > 0){ - t.image(item.icon(Cicon.small)).padRight(4); - //t.add(ui.formatAmount(data.getItem(item))).color(Color.lightGray); - //if(exports[item.id] > 0){ - t.add("+ [accent]" + ui.formatAmount(exports[item.id]) + " [lightgray]/T"); - //}else{ - // t.add(); - //} - t.row(); - } - } - }); - - - } -}