Idle sector resource production

This commit is contained in:
Anuken
2020-06-28 18:55:42 -04:00
parent fceac4c1a1
commit 0d6657a1e8
5 changed files with 61 additions and 55 deletions

View File

@@ -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<Item, ExportStat> production = new ObjectMap<>();
/** Export statistics. */
public ObjectMap<Item, ExportStat> 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<Item> getCurrentItems(float turnsPassed){
ObjectIntMap<Item> 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;
}

View File

@@ -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());
}