Progress on sector state

This commit is contained in:
Anuken
2020-05-30 21:04:41 -04:00
parent 8a4824e72d
commit 0842c3f0a0
20 changed files with 314 additions and 211 deletions

View File

@@ -14,6 +14,7 @@ import mindustry.graphics.*;
import mindustry.type.*;
import mindustry.world.*;
import mindustry.world.blocks.*;
import mindustry.world.blocks.campaign.LaunchPad;
import mindustry.world.blocks.defense.*;
import mindustry.world.blocks.defense.turrets.*;
import mindustry.world.blocks.distribution.*;
@@ -1315,7 +1316,7 @@ public class Blocks implements ContentList{
speed = 7f;
}};
launchPad = new LaunchPad("launch-pad"){{
launchPad = new mindustry.world.blocks.campaign.LaunchPad("launch-pad"){{
requirements(Category.effect, BuildVisibility.campaignOnly, ItemStack.with(Items.copper, 350, Items.silicon, 140, Items.lead, 200, Items.titanium, 150));
size = 3;
itemCapacity = 100;

View File

@@ -23,6 +23,8 @@ public class GameState{
public Rules rules = new Rules();
/** Statistics for this save/game. Displayed after game over. */
public Stats stats = new Stats();
/** Sector information. Only valid in the campaign. */
public SectorInfo secinfo = new SectorInfo();
/** Team data. Gets reset every new game. */
public Teams teams = new Teams();
/** Number of enemies in the game; only used clientside in servers. */

View File

@@ -90,7 +90,7 @@ public class Logic implements ApplicationListener{
}
});
Events.on(LaunchItemEvent.class, e -> state.stats.handleItemExport(e.stack));
Events.on(LaunchItemEvent.class, e -> state.secinfo.handleItemExport(e.stack));
//when loading a 'damaged' sector, propagate the damage
Events.on(WorldLoadEvent.class, e -> {
@@ -100,13 +100,14 @@ public class Logic implements ApplicationListener{
}
});
//TODO this should be in the same place as launch handling code
//TODO dying takes up a turn (?)
/*
Events.on(GameOverEvent.class, e -> {
//simulate a turn on a normal non-launch gameover
if(state.isCampaign() && !state.launched){
universe.runTurn();
}
});
});*/
//disable new waves after the boss spawns
Events.on(WaveEvent.class, e -> {
@@ -269,11 +270,9 @@ public class Logic implements ApplicationListener{
//save over the data w/o the cores
sector.save.save();
//TODO mark sector as not containing any cores
//run a turn, since launching takes up a turn
universe.runTurn();
//TODO needs extra damage to prevent player from landing immediately afterwards
sector.setTurnsPassed(sector.getTurnsPassed() + 3);
Events.fire(new LaunchEvent());
@@ -300,7 +299,7 @@ public class Logic implements ApplicationListener{
}
if(!state.isPaused()){
state.stats.update();
state.secinfo.update();
if(state.isCampaign()){
universe.update();

View File

@@ -188,6 +188,12 @@ abstract class TileComp implements Posc, Teamc, Healthc, Tilec, Timerc, QuadTree
}
}
/** Called clientside when the client taps a block to config.
* @return whether the configuration UI should be shown. */
public boolean configTapped(){
return true;
}
public void applyBoost(float intensity, float duration){
timeScale = Math.max(timeScale, intensity);
timeScaleDuration = Math.max(timeScaleDuration, duration);

View File

@@ -242,10 +242,6 @@ public class Saves{
return isSector();
}
public ObjectFloatMap<Item> getProductionRates(){
return meta.exportRates;
}
public String getPlayTime(){
return Strings.formatMillis(current == this ? totalPlaytime : meta.timePlayed);
}

View File

@@ -0,0 +1,124 @@
package mindustry.game;
import arc.math.*;
import arc.struct.*;
import arc.util.*;
import mindustry.content.*;
import mindustry.type.*;
import mindustry.world.*;
import mindustry.world.blocks.storage.CoreBlock.*;
import mindustry.world.modules.*;
import static mindustry.Vars.*;
public class SectorInfo{
/** export window size in seconds */
private static final int exportWindow = 60;
/** refresh period of export in ticks */
private static final float refreshPeriod = 60;
/** Export statistics. */
public ObjectMap<Item, ExportStat> export = new ObjectMap<>();
/** Items stored in all cores. */
public ObjectIntMap<Item> coreItems = new ObjectIntMap<>();
/** The best available core type. */
public Block bestCoreType = Blocks.air;
/** Max storage capacity. */
public int storageCapacity = 0;
/** Whether a core is available here. */
public boolean hasCore = true;
/** Counter refresh state. */
private transient Interval time = new Interval();
/** Core item storage to prevent spoofing. */
private transient int[] lastCoreItems;
/** Updates export statistics. */
public void handleItemExport(ItemStack stack){
handleItemExport(stack.item, stack.amount);
}
/** Updates export statistics. */
public void handleItemExport(Item item, int amount){
export.get(item, ExportStat::new).counter += amount;
}
/** Subtracts from export statistics. */
public void handleItemImport(Item item, int amount){
export.get(item, ExportStat::new).counter -= amount;
}
public float getExport(Item item){
return export.get(item, ExportStat::new).mean;
}
/** Prepare data for writing to a save. */
public void prepare(){
//update core items
coreItems.clear();
CoreEntity entity = state.rules.defaultTeam.core();
if(entity != null){
ItemModule items = entity.items;
for(int i = 0; i < items.length(); i++){
coreItems.put(content.item(i), items.get(i));
}
}
hasCore = entity != null;
bestCoreType = !hasCore ? Blocks.air : state.rules.defaultTeam.cores().max(e -> e.block.size).block;
storageCapacity = entity != null ? entity.storageCapacity : 0;
}
/** Update averages of various stats. */
public void update(){
//create last stored core items
if(lastCoreItems == null){
lastCoreItems = new int[content.items().size];
updateCoreDeltas();
}
//refresh throughput
if(time.get(refreshPeriod)){
CoreEntity ent = state.rules.defaultTeam.core();
export.each((item, stat) -> {
//initialize stat after loading
if(!stat.loaded){
stat.means.fill(stat.mean);
stat.loaded = true;
}
//how the resources changed - only interested in negative deltas, since that's what happens during spoofing
int coreDelta = Math.min(ent == null ? 0 : ent.items.get(item) - lastCoreItems[item.id], 0);
//add counter, subtract how many items were taken from the core during this time
stat.means.add(Math.max(stat.counter + coreDelta, 0));
stat.counter = 0;
stat.mean = stat.means.rawMean();
});
updateCoreDeltas();
}
}
private void updateCoreDeltas(){
CoreEntity ent = state.rules.defaultTeam.core();
for(int i = 0; i < lastCoreItems.length; i++){
lastCoreItems[i] = ent == null ? 0 : ent.items.get(i);
}
}
public ObjectFloatMap<Item> exportRates(){
ObjectFloatMap<Item> map = new ObjectFloatMap<>();
export.each((item, value) -> map.put(item, value.mean));
return map;
}
public static class ExportStat{
public transient float counter;
public transient WindowedMean means = new WindowedMean(exportWindow);
public transient boolean loaded;
public float mean;
}
}

View File

@@ -2,21 +2,11 @@ package mindustry.game;
import arc.math.*;
import arc.struct.*;
import arc.util.*;
import mindustry.type.*;
import mindustry.world.blocks.storage.CoreBlock.*;
import mindustry.world.modules.*;
import static mindustry.Vars.*;
//TODO more stats:
//- units constructed
public class Stats{
/** export window size in seconds */
private static final int exportWindow = 60;
/** refresh period of export in ticks */
private static final float refreshPeriod = 60;
/** Total items delivered to global resoure counter. Campaign only. */
public ObjectIntMap<Item> itemsDelivered = new ObjectIntMap<>();
/** Enemy (red team) units destroyed. */
@@ -31,89 +21,6 @@ public class Stats{
public int buildingsDeconstructed;
/** Friendly buildings destroyed. */
public int buildingsDestroyed;
/** Export statistics. */
public ObjectMap<Item, ExportStat> export = new ObjectMap<>();
/** Items stored in all cores. Used for the campaign. */
public ObjectIntMap<Item> coreItems = new ObjectIntMap<>();
/** Counter refresh state. */
private transient Interval time = new Interval();
/** Core item storage to prevent spoofing. */
private transient int[] lastCoreItems;
/** Updates export statistics. */
public void handleItemExport(ItemStack stack){
handleItemExport(stack.item, stack.amount);
}
/** Updates export statistics. */
public void handleItemExport(Item item, int amount){
export.get(item, ExportStat::new).counter += amount;
}
/** Subtracts from export statistics. */
public void handleItemImport(Item item, int amount){
export.get(item, ExportStat::new).counter -= amount;
}
public float getExport(Item item){
return export.get(item, ExportStat::new).mean;
}
public void update(){
//update core items
CoreEntity entity = state.rules.defaultTeam.core();
if(entity != null){
ItemModule items = entity.items;
for(int i = 0; i < items.length(); i++){
coreItems.put(content.item(i), items.get(i));
}
}else{
coreItems.clear();
}
//create last stored core items
if(lastCoreItems == null){
lastCoreItems = new int[content.items().size];
updateCoreDeltas();
}
//refresh throughput
if(time.get(refreshPeriod)){
CoreEntity ent = state.rules.defaultTeam.core();
export.each((item, stat) -> {
//initialize stat after loading
if(!stat.loaded){
stat.means.fill(stat.mean);
stat.loaded = true;
}
//how the resources changed - only interested in negative deltas, since that's what happens during spoofing
int coreDelta = Math.min(ent == null ? 0 : ent.items.get(item) - lastCoreItems[item.id], 0);
//add counter, subtract how many items were taken from the core during this time
stat.means.add(Math.max(stat.counter + coreDelta, 0));
stat.counter = 0;
stat.mean = stat.means.rawMean();
});
updateCoreDeltas();
}
}
private void updateCoreDeltas(){
CoreEntity ent = state.rules.defaultTeam.core();
for(int i = 0; i < lastCoreItems.length; i++){
lastCoreItems[i] = ent == null ? 0 : ent.items.get(i);
}
}
public ObjectFloatMap<Item> exportRates(){
ObjectFloatMap<Item> map = new ObjectFloatMap<>();
export.each((item, value) -> map.put(item, value.mean));
return map;
}
public RankResult calculateRank(Sector zone, boolean launched){
float score = 0;
@@ -161,15 +68,7 @@ public class Stats{
}
}
public enum Rank{
F, D, C, B, A, S, SS
}
public static class ExportStat{
public transient float counter;
public transient WindowedMean means = new WindowedMean(exportWindow);
public transient boolean loaded;
public float mean;
}
}

View File

@@ -3,7 +3,6 @@ package mindustry.io;
import arc.struct.*;
import mindustry.game.*;
import mindustry.maps.*;
import mindustry.type.*;
import static mindustry.Vars.maps;
@@ -15,13 +14,12 @@ public class SaveMeta{
public Map map;
public int wave;
public Rules rules;
public SectorInfo secinfo;
public StringMap tags;
public String[] mods;
/** These are in items/second. */
public ObjectFloatMap<Item> exportRates;
public boolean hasProduction;
public SaveMeta(int version, long timestamp, long timePlayed, int build, String map, int wave, Rules rules, ObjectFloatMap<Item> exportRates, StringMap tags){
public SaveMeta(int version, long timestamp, long timePlayed, int build, String map, int wave, Rules rules, SectorInfo secinfo, StringMap tags){
this.version = version;
this.build = build;
this.timestamp = timestamp;
@@ -31,8 +29,8 @@ public class SaveMeta{
this.rules = rules;
this.tags = tags;
this.mods = JsonIO.read(String[].class, tags.get("mods", "[]"));
this.exportRates = exportRates;
this.secinfo = secinfo;
exportRates.each(e -> hasProduction |= e.value > 0.001f);
secinfo.exportRates().each(e -> hasProduction |= e.value > 0.001f);
}
}

View File

@@ -39,7 +39,7 @@ public abstract class SaveVersion extends SaveFileReader{
map.get("mapname"),
map.getInt("wave"),
JsonIO.read(Rules.class, map.get("rules", "{}")),
JsonIO.read(Stats.class, map.get("stats", "{}")).exportRates(),
JsonIO.read(SectorInfo.class, map.get("secinfo", "{}")),
map
);
}
@@ -70,6 +70,11 @@ public abstract class SaveVersion extends SaveFileReader{
}
public void writeMeta(DataOutput stream, StringMap tags) throws IOException{
//prepare campaign data for writing
if(state.isCampaign()){
state.secinfo.prepare();
}
writeStringMap(stream, StringMap.of(
"saved", Time.millis(),
"playtime", headless ? 0 : control.saves.getTotalPlaytime(),
@@ -78,6 +83,7 @@ public abstract class SaveVersion extends SaveFileReader{
"wave", state.wave,
"wavetime", state.wavetime,
"stats", JsonIO.write(state.stats),
"secinfo", state.isCampaign() ? JsonIO.write(state.secinfo) : "{}",
"rules", JsonIO.write(state.rules),
"mods", JsonIO.write(mods.getModStrings().toArray(String.class)),
"width", world.width(),
@@ -94,6 +100,7 @@ public abstract class SaveVersion extends SaveFileReader{
state.wave = map.getInt("wave");
state.wavetime = map.getFloat("wavetime", state.rules.waveSpacing);
state.stats = JsonIO.read(Stats.class, map.get("stats", "{}"));
state.secinfo = JsonIO.read(SectorInfo.class, map.get("secinfo", "{}"));
state.rules = JsonIO.read(Rules.class, map.get("rules", "{}"));
if(state.rules.spawns.isEmpty()) state.rules.spawns = defaultWaves.get();
lastReadBuild = map.getInt("build", -1);

View File

@@ -17,6 +17,7 @@ import java.net.*;
import java.util.regex.*;
public class Scripts implements Disposable{
private final static Object[] emptyObjects = {};
private final Array<String> blacklist = Array.with("net", "files", "reflect", "javax", "rhino", "file", "channels", "jdk",
"runtime", "util.os", "rmi", "security", "org.", "sun.", "beans", "sql", "http", "exec", "compiler", "process", "system",
".awt", "socket", "classloader", "oracle", "invoke", "arc.events", "java.util.function", "java.util.stream");

View File

@@ -9,6 +9,7 @@ import arc.input.*;
import arc.math.*;
import arc.math.geom.*;
import arc.scene.event.*;
import arc.scene.ui.*;
import arc.scene.ui.TextButton.*;
import arc.scene.ui.layout.*;
import arc.struct.*;
@@ -152,6 +153,17 @@ public class PlanetDialog extends BaseDialog{
shown(this::setup);
}
/** show with no limitations, just as a map. */
@Override
public Dialog show(){
//TODO
return super.show();
}
public void show(Sector selected, int range){
//TODO
}
void makeBloom(){
if(bloom != null){
bloom.dispose();
@@ -413,7 +425,7 @@ public class PlanetDialog extends BaseDialog{
stable.table(t -> {
t.left();
selected.save.meta.exportRates.each(entry -> {
selected.save.meta.secinfo.exportRates().each(entry -> {
int total = (int)(entry.value * turnDuration / 60f);
if(total > 1){
t.image(entry.key.icon(Cicon.small)).padRight(3);
@@ -421,7 +433,24 @@ public class PlanetDialog extends BaseDialog{
t.row();
}
});
});
}).row();
}
//stored resources
if(selected.hasBase() && selected.save.meta.secinfo.coreItems.size > 0){
stable.add("Stored Resources:").row();
stable.table(t -> {
t.left();
for(Item item : content.items()){
int amount = selected.save.meta.secinfo.coreItems.get(item);
if(amount > 0){
t.image(item.icon(Cicon.small)).padRight(3);
t.add(ui.formatAmount(amount)).color(Color.lightGray);
t.row();
}
}
}).row();
}
//display how many turns this sector has been attacked

View File

@@ -43,29 +43,31 @@ public class BlockConfigFragment extends Fragment{
}
public void showConfig(Tilec tile){
configTile = tile;
if(tile.configTapped()){
configTile = tile;
table.visible(true);
table.clear();
tile.buildConfiguration(table);
table.pack();
table.setTransform(true);
table.actions(Actions.scaleTo(0f, 1f), Actions.visible(true),
Actions.scaleTo(1f, 1f, 0.07f, Interp.pow3Out));
table.visible(true);
table.clear();
tile.buildConfiguration(table);
table.pack();
table.setTransform(true);
table.actions(Actions.scaleTo(0f, 1f), Actions.visible(true),
Actions.scaleTo(1f, 1f, 0.07f, Interp.pow3Out));
table.update(() -> {
if(configTile != null && configTile.shouldHideConfigure(player)){
hideConfig();
return;
}
table.update(() -> {
if(configTile != null && configTile.shouldHideConfigure(player)){
hideConfig();
return;
}
table.setOrigin(Align.center);
if(configTile == null || configTile.block() == Blocks.air || !configTile.isValid()){
hideConfig();
}else{
configTile.updateTableAlign(table);
}
});
table.setOrigin(Align.center);
if(configTile == null || configTile.block() == Blocks.air || !configTile.isValid()){
hideConfig();
}else{
configTile.updateTableAlign(table);
}
});
}
}
public boolean hasConfigMouse(){

View File

@@ -326,9 +326,9 @@ public class HudFragment extends Fragment{
c.clearChildren();
for(Item item : content.items()){
if(state.stats.getExport(item) >= 1){
if(state.secinfo.getExport(item) >= 1){
c.image(item.icon(Cicon.small));
c.label(() -> (int)state.stats.getExport(item) + " /s").color(Color.lightGray);
c.label(() -> (int)state.secinfo.getExport(item) + " /s").color(Color.lightGray);
c.row();
}
}
@@ -337,7 +337,7 @@ public class HudFragment extends Fragment{
c.update(() -> {
boolean wrong = false;
for(Item item : content.items()){
boolean has = state.stats.getExport(item) >= 1;
boolean has = state.secinfo.getExport(item) >= 1;
if(used.get(item.id) != has){
used.set(item.id, has);
wrong = true;

View File

@@ -0,0 +1,33 @@
package mindustry.world.blocks.campaign;
import mindustry.*;
import mindustry.gen.*;
import mindustry.world.*;
public class CoreLauncher extends Block{
public CoreLauncher(String name){
super(name);
hasItems = true;
configurable = true;
}
public class CoreLauncherEntity extends TileEntity{
@Override
public void updateTile(){
super.updateTile();
}
@Override
public boolean configTapped(){
//TODO show w/ sector
Vars.ui.planet.show();
return false;
}
}
}

View File

@@ -1,4 +1,4 @@
package mindustry.world.blocks.storage;
package mindustry.world.blocks.campaign;
import arc.*;
import arc.graphics.*;

View File

@@ -1,4 +1,4 @@
package mindustry.world.blocks.production;
package mindustry.world.blocks.campaign;
import arc.scene.ui.layout.*;
import arc.util.ArcAnnotate.*;