Game state cleanup

This commit is contained in:
Anuken
2020-03-31 14:16:38 -04:00
parent 9081f3ff03
commit f1fd072400
20 changed files with 51 additions and 71 deletions

View File

@@ -18,6 +18,7 @@ import mindustry.game.EventType.*;
import mindustry.gen.*; import mindustry.gen.*;
import mindustry.input.*; import mindustry.input.*;
import mindustry.maps.*; import mindustry.maps.*;
import mindustry.maps.Map;
import mindustry.mod.*; import mindustry.mod.*;
import mindustry.net.Net; import mindustry.net.Net;
import mindustry.net.*; import mindustry.net.*;
@@ -142,6 +143,8 @@ public class Vars implements Loadable{
public static Fi schematicDirectory; public static Fi schematicDirectory;
/** data subdirectory used for bleeding edge build versions */ /** data subdirectory used for bleeding edge build versions */
public static Fi bebuildDirectory; public static Fi bebuildDirectory;
/** empty map, indicates no current map */
public static Map emptyMap;
/** map file extension */ /** map file extension */
public static final String mapExtension = "msav"; public static final String mapExtension = "msav";
/** save file extension */ /** save file extension */
@@ -219,6 +222,7 @@ public class Vars implements Loadable{
modDirectory = dataDirectory.child("mods/"); modDirectory = dataDirectory.child("mods/");
schematicDirectory = dataDirectory.child("schematics/"); schematicDirectory = dataDirectory.child("schematics/");
bebuildDirectory = dataDirectory.child("be_builds/"); bebuildDirectory = dataDirectory.child("be_builds/");
emptyMap = new Map(new StringMap());
if(tree == null) tree = new FileTree(); if(tree == null) tree = new FileTree();
if(mods == null) mods = new Mods(); if(mods == null) mods = new Mods();

View File

@@ -88,9 +88,9 @@ public class Control implements ApplicationListener, Loadable{
}); });
Events.on(WaveEvent.class, event -> { Events.on(WaveEvent.class, event -> {
if(world.getMap().getHightScore() < state.wave){ if(state.map.getHightScore() < state.wave){
hiscore = true; hiscore = true;
world.getMap().setHighScore(state.wave); state.map.setHighScore(state.wave);
} }
Sounds.wave.play(); Sounds.wave.play();

View File

@@ -5,9 +5,10 @@ import arc.util.ArcAnnotate.*;
import mindustry.game.EventType.*; import mindustry.game.EventType.*;
import mindustry.game.*; import mindustry.game.*;
import mindustry.gen.*; import mindustry.gen.*;
import mindustry.maps.*;
import mindustry.type.*; import mindustry.type.*;
import static mindustry.Vars.net; import static mindustry.Vars.*;
public class GameState{ public class GameState{
/** Current wave number, can be anything in non-wave modes. */ /** Current wave number, can be anything in non-wave modes. */
@@ -16,6 +17,8 @@ public class GameState{
public float wavetime; public float wavetime;
/** Whether the game is in game over state. */ /** Whether the game is in game over state. */
public boolean gameOver = false, launched = false; public boolean gameOver = false, launched = false;
/** Map that is currently being played on. */
public @NonNull Map map = emptyMap;
/** The current game rules. */ /** The current game rules. */
public Rules rules = new Rules(); public Rules rules = new Rules();
/** Statistics for this save/game. Displayed after game over. */ /** Statistics for this save/game. Displayed after game over. */

View File

@@ -120,12 +120,11 @@ public class Logic implements ApplicationListener{
} }
public void reset(){ public void reset(){
state.wave = 1; State prev = state.getState();
state.wavetime = state.rules.waveSpacing; //recreate gamestate - sets state to menu
state.gameOver = state.launched = false; state = new GameState();
state.teams = new Teams(); //fire change event, since it was technically changed
state.rules = new Rules(); Events.fire(new StateChangeEvent(prev, State.menu));
state.stats = new Stats();
Groups.all.clear(); Groups.all.clear();
Time.clear(); Time.clear();

View File

@@ -92,7 +92,6 @@ public class NetClient implements ApplicationListener{
if(quietReset) return; if(quietReset) return;
connecting = false; connecting = false;
state.set(State.menu);
logic.reset(); logic.reset();
platform.updateRPC(); platform.updateRPC();
player.name(Core.settings.getString("name")); player.name(Core.settings.getString("name"));
@@ -207,7 +206,6 @@ public class NetClient implements ApplicationListener{
@Remote(called = Loc.client, variants = Variant.one) @Remote(called = Loc.client, variants = Variant.one)
public static void onConnect(String ip, int port){ public static void onConnect(String ip, int port){
netClient.disconnectQuietly(); netClient.disconnectQuietly();
state.set(State.menu);
logic.reset(); logic.reset();
ui.join.connect(ip, port); ui.join.connect(ip, port);
@@ -233,7 +231,6 @@ public class NetClient implements ApplicationListener{
@Remote(variants = Variant.one, priority = PacketPriority.high) @Remote(variants = Variant.one, priority = PacketPriority.high)
public static void onKick(KickReason reason){ public static void onKick(KickReason reason){
netClient.disconnectQuietly(); netClient.disconnectQuietly();
state.set(State.menu);
logic.reset(); logic.reset();
if(!reason.quiet){ if(!reason.quiet){
@@ -249,7 +246,6 @@ public class NetClient implements ApplicationListener{
@Remote(variants = Variant.one, priority = PacketPriority.high) @Remote(variants = Variant.one, priority = PacketPriority.high)
public static void onKick(String reason){ public static void onKick(String reason){
netClient.disconnectQuietly(); netClient.disconnectQuietly();
state.set(State.menu);
logic.reset(); logic.reset();
ui.showText("$disconnect", reason, Align.left); ui.showText("$disconnect", reason, Align.left);
ui.loadfrag.hide(); ui.loadfrag.hide();

View File

@@ -26,7 +26,6 @@ import static mindustry.Vars.*;
public class World{ public class World{
public final Context context = new Context(); public final Context context = new Context();
private Map currentMap = new Map(new StringMap());
public @NonNull Tiles tiles = new Tiles(0, 0); public @NonNull Tiles tiles = new Tiles(0, 0);
private boolean generating, invalidMap; private boolean generating, invalidMap;
@@ -60,14 +59,6 @@ public class World{
return !wallSolid(x, y - 1) || !wallSolid(x, y + 1) || !wallSolid(x - 1, y) || !wallSolid(x + 1, y); return !wallSolid(x, y - 1) || !wallSolid(x, y + 1) || !wallSolid(x - 1, y) || !wallSolid(x + 1, y);
} }
public Map getMap(){
return currentMap;
}
public void setMap(Map map){
this.currentMap = map;
}
public int width(){ public int width(){
return tiles.width; return tiles.width;
} }
@@ -214,7 +205,7 @@ public class World{
} }
public void loadSector(Sector sector){ public void loadSector(Sector sector){
currentMap = new Map(StringMap.of("name", sector.planet.localizedName + "; Sector " + sector.id)); state.map = new Map(StringMap.of("name", sector.planet.localizedName + "; Sector " + sector.id));
state.rules.sector = sector; state.rules.sector = sector;
int size = sector.getSize(); int size = sector.getSize();
loadGenerator(size, size, tiles -> sector.planet.generator.generate(tiles, sector)); loadGenerator(size, size, tiles -> sector.planet.generator.generate(tiles, sector));
@@ -238,7 +229,7 @@ public class World{
return; return;
} }
this.currentMap = map; state.map = map;
invalidMap = false; invalidMap = false;

View File

@@ -247,11 +247,11 @@ public class MapEditorDialog extends Dialog implements Disposable{
player.reset(); player.reset();
state.rules = Gamemode.editor.apply(lastSavedRules.copy()); state.rules = Gamemode.editor.apply(lastSavedRules.copy());
state.rules.sector = null; state.rules.sector = null;
world.setMap(new Map(StringMap.of( state.map = new Map(StringMap.of(
"name", "Editor Playtesting", "name", "Editor Playtesting",
"width", editor.width(), "width", editor.width(),
"height", editor.height() "height", editor.height()
))); ));
world.endMapLoad(); world.endMapLoad();
//add entities so they update. is this really needed? //add entities so they update. is this really needed?
for(Tile tile : world.tiles){ for(Tile tile : world.tiles){

View File

@@ -62,7 +62,7 @@ public abstract class SaveVersion extends SaveFileReader{
"saved", Time.millis(), "saved", Time.millis(),
"playtime", headless ? 0 : control.saves.getTotalPlaytime(), "playtime", headless ? 0 : control.saves.getTotalPlaytime(),
"build", Version.build, "build", Version.build,
"mapname", world.getMap() == null ? "unknown" : world.getMap().name(), "mapname", state.map.name(),
"wave", state.wave, "wave", state.wave,
"wavetime", state.wavetime, "wavetime", state.wavetime,
"stats", JsonIO.write(state.stats), "stats", JsonIO.write(state.stats),
@@ -84,11 +84,12 @@ public abstract class SaveVersion extends SaveFileReader{
lastReadBuild = map.getInt("build", -1); lastReadBuild = map.getInt("build", -1);
Map worldmap = maps.byName(map.get("mapname", "\\\\\\")); Map worldmap = maps.byName(map.get("mapname", "\\\\\\"));
world.setMap(worldmap == null ? new Map(StringMap.of( Map map1 = worldmap == null ? new Map(StringMap.of(
"name", map.get("mapname", "Unknown"), "name", map.get("mapname", "Unknown"),
"width", 1, "width", 1,
"height", 1 "height", 1
)) : worldmap); )) : worldmap;
state.map = map1;
} }
public void writeMap(DataOutput stream) throws IOException{ public void writeMap(DataOutput stream) throws IOException{

View File

@@ -22,6 +22,7 @@ public class FileMapGenerator implements WorldGenerator{
@Override @Override
public void generate(Tiles tiles){ public void generate(Tiles tiles){
if(true) throw new IllegalArgumentException("no!");
tiles.fill(); tiles.fill();
SaveIO.load(map.file); SaveIO.load(map.file);
@@ -60,6 +61,6 @@ public class FileMapGenerator implements WorldGenerator{
} }
world.prepareTiles(tiles); world.prepareTiles(tiles);
world.setMap(map); state.map = map;
} }
} }

View File

@@ -58,7 +58,7 @@ public class ArcNetProvider implements NetProvider{
Core.app.post(() -> { Core.app.post(() -> {
try{ try{
net.handleClientReceived(object); net.handleClientReceived(object);
}catch(Exception e){ }catch(Throwable e){
handleException(e); handleException(e);
} }
}); });
@@ -113,9 +113,7 @@ public class ArcNetProvider implements NetProvider{
Core.app.post(() -> { Core.app.post(() -> {
try{ try{
net.handleServerReceived(k, object); net.handleServerReceived(k, object);
}catch(RuntimeException e){ }catch(Throwable e){
e.printStackTrace();
}catch(Exception e){
e.printStackTrace(); e.printStackTrace();
} }
}); });
@@ -267,7 +265,7 @@ public class ArcNetProvider implements NetProvider{
return null; return null;
} }
private void handleException(Exception e){ private void handleException(Throwable e){
if(e instanceof ArcNetException){ if(e instanceof ArcNetException){
Core.app.post(() -> net.showError(new IOException("mismatch"))); Core.app.post(() -> net.showError(new IOException("mismatch")));
}else if(e instanceof ClosedChannelException){ }else if(e instanceof ClosedChannelException){

View File

@@ -21,7 +21,7 @@ public class NetworkIO{
try(DataOutputStream stream = new DataOutputStream(os)){ try(DataOutputStream stream = new DataOutputStream(os)){
stream.writeUTF(JsonIO.write(state.rules)); stream.writeUTF(JsonIO.write(state.rules));
SaveIO.getSaveWriter().writeStringMap(stream, world.getMap().tags); SaveIO.getSaveWriter().writeStringMap(stream, state.map.tags);
stream.writeInt(state.wave); stream.writeInt(state.wave);
stream.writeFloat(state.wavetime); stream.writeFloat(state.wavetime);
@@ -41,7 +41,7 @@ public class NetworkIO{
try(DataInputStream stream = new DataInputStream(is)){ try(DataInputStream stream = new DataInputStream(is)){
Time.clear(); Time.clear();
state.rules = JsonIO.read(Rules.class, stream.readUTF()); state.rules = JsonIO.read(Rules.class, stream.readUTF());
world.setMap(new Map(SaveIO.getSaveWriter().readStringMap(stream))); state.map = new Map(SaveIO.getSaveWriter().readStringMap(stream));
state.wave = stream.readInt(); state.wave = stream.readInt();
state.wavetime = stream.readFloat(); state.wavetime = stream.readFloat();
@@ -65,7 +65,7 @@ public class NetworkIO{
public static ByteBuffer writeServerData(){ public static ByteBuffer writeServerData(){
String name = (headless ? Config.name.string() : player.name()); String name = (headless ? Config.name.string() : player.name());
String description = headless && !Config.desc.string().equals("off") ? Config.desc.string() : ""; String description = headless && !Config.desc.string().equals("off") ? Config.desc.string() : "";
String map = world.getMap() == null ? "None" : world.getMap().name(); String map = state.map.name();
ByteBuffer buffer = ByteBuffer.allocate(512); ByteBuffer buffer = ByteBuffer.allocate(512);

View File

@@ -1,12 +1,11 @@
package mindustry.ui.dialogs; package mindustry.ui.dialogs;
import arc.*; import arc.*;
import mindustry.core.GameState.*;
import mindustry.game.*;
import mindustry.game.EventType.*; import mindustry.game.EventType.*;
import mindustry.game.Stats.*; import mindustry.game.Stats.*;
import mindustry.game.*;
import mindustry.type.*; import mindustry.type.*;
import mindustry.ui.Cicon; import mindustry.ui.*;
import static mindustry.Vars.*; import static mindustry.Vars.*;
@@ -40,7 +39,6 @@ public class GameOverDialog extends FloatingDialog{
cont.add(Core.bundle.format("gameover.pvp", winner.localized())).pad(6); cont.add(Core.bundle.format("gameover.pvp", winner.localized())).pad(6);
buttons.addButton("$menu", () -> { buttons.addButton("$menu", () -> {
hide(); hide();
state.set(State.menu);
logic.reset(); logic.reset();
}).size(130f, 60f); }).size(130f, 60f);
}else{ }else{
@@ -90,14 +88,12 @@ public class GameOverDialog extends FloatingDialog{
if(state.isCampaign()){ if(state.isCampaign()){
buttons.addButton("$continue", () -> { buttons.addButton("$continue", () -> {
hide(); hide();
state.set(State.menu);
logic.reset(); logic.reset();
ui.planet.show(); ui.planet.show();
}).size(130f, 60f); }).size(130f, 60f);
}else{ }else{
buttons.addButton("$menu", () -> { buttons.addButton("$menu", () -> {
hide(); hide();
state.set(State.menu);
logic.reset(); logic.reset();
}).size(130f, 60f); }).size(130f, 60f);
} }

View File

@@ -178,7 +178,6 @@ public class LoadDialog extends FloatingDialog{
state.set(State.playing); state.set(State.playing);
}catch(SaveException e){ }catch(SaveException e){
Log.err(e); Log.err(e);
state.set(State.menu);
logic.reset(); logic.reset();
ui.showErrorMessage("$save.corrupted"); ui.showErrorMessage("$save.corrupted");
} }

View File

@@ -2,7 +2,6 @@ package mindustry.ui.dialogs;
import arc.*; import arc.*;
import arc.input.*; import arc.input.*;
import mindustry.core.GameState.*;
import mindustry.gen.*; import mindustry.gen.*;
import static mindustry.Vars.*; import static mindustry.Vars.*;
@@ -118,7 +117,6 @@ public class PausedDialog extends FloatingDialog{
} }
if(control.saves.getCurrent() == null || !control.saves.getCurrent().isAutosave() || state.rules.tutorial || wasClient){ if(control.saves.getCurrent() == null || !control.saves.getCurrent().isAutosave() || state.rules.tutorial || wasClient){
state.set(State.menu);
logic.reset(); logic.reset();
return; return;
} }
@@ -130,7 +128,6 @@ public class PausedDialog extends FloatingDialog{
e.printStackTrace(); e.printStackTrace();
ui.showException("[accent]" + Core.bundle.get("savefail"), e); ui.showException("[accent]" + Core.bundle.get("savefail"), e);
} }
state.set(State.menu);
logic.reset(); logic.reset();
}); });
} }

View File

@@ -248,10 +248,9 @@ public class DesktopLauncher extends ClientLauncher{
String uiState = ""; String uiState = "";
if(inGame){ if(inGame){
if(world.getMap() != null){ //TODO implement nice name for sector
//TODO implement nice name for sector gameMapWithWave = Strings.capitalize(state.map.name());
gameMapWithWave = Strings.capitalize(world.getMap().name());
}
if(state.rules.waves){ if(state.rules.waves){
gameMapWithWave += " | Wave " + state.wave; gameMapWithWave += " | Wave " + state.wave;
} }

View File

@@ -9,7 +9,6 @@ import com.codedisaster.steamworks.*;
import com.codedisaster.steamworks.SteamFriends.*; import com.codedisaster.steamworks.SteamFriends.*;
import com.codedisaster.steamworks.SteamMatchmaking.*; import com.codedisaster.steamworks.SteamMatchmaking.*;
import com.codedisaster.steamworks.SteamNetworking.*; import com.codedisaster.steamworks.SteamNetworking.*;
import mindustry.core.GameState.*;
import mindustry.core.*; import mindustry.core.*;
import mindustry.game.EventType.*; import mindustry.game.EventType.*;
import mindustry.game.*; import mindustry.game.*;
@@ -242,7 +241,6 @@ public class SNet implements SteamNetworkingCallback, SteamMatchmakingCallback,
logic.reset(); logic.reset();
net.reset(); net.reset();
state.set(State.menu);
currentLobby = steamIDLobby; currentLobby = steamIDLobby;
currentServer = smat.getLobbyOwner(steamIDLobby); currentServer = smat.getLobbyOwner(steamIDLobby);
@@ -346,7 +344,7 @@ public class SNet implements SteamNetworkingCallback, SteamMatchmakingCallback,
currentLobby = steamID; currentLobby = steamID;
smat.setLobbyData(steamID, "name", player.name()); smat.setLobbyData(steamID, "name", player.name());
smat.setLobbyData(steamID, "mapname", world.getMap().name()); smat.setLobbyData(steamID, "mapname", state.map.name());
smat.setLobbyData(steamID, "version", Version.build + ""); smat.setLobbyData(steamID, "version", Version.build + "");
smat.setLobbyData(steamID, "versionType", Version.type); smat.setLobbyData(steamID, "versionType", Version.type);
smat.setLobbyData(steamID, "wave", state.wave + ""); smat.setLobbyData(steamID, "wave", state.wave + "");

View File

@@ -1,3 +1,3 @@
org.gradle.daemon=true org.gradle.daemon=true
org.gradle.jvmargs=-Xms256m -Xmx1024m org.gradle.jvmargs=-Xms256m -Xmx1024m
archash=463da29cdf7fb64526c06852a10c750a2dbc48d3 archash=1b96f9149cce63e7c4b72b5798c58e8f62b1f185

View File

@@ -11,7 +11,6 @@ import arc.util.CommandHandler.*;
import arc.util.Timer.*; import arc.util.Timer.*;
import arc.util.serialization.*; import arc.util.serialization.*;
import arc.util.serialization.JsonValue.*; import arc.util.serialization.JsonValue.*;
import mindustry.*;
import mindustry.core.GameState.*; import mindustry.core.GameState.*;
import mindustry.core.*; import mindustry.core.*;
import mindustry.game.EventType.*; import mindustry.game.EventType.*;
@@ -129,13 +128,13 @@ public class ServerControl implements ApplicationListener{
Events.on(GameOverEvent.class, event -> { Events.on(GameOverEvent.class, event -> {
if(inExtraRound) return; if(inExtraRound) return;
if(state.rules.waves){ if(state.rules.waves){
info("&lcGame over! Reached wave &ly{0}&lc with &ly{1}&lc players online on map &ly{2}&lc.", state.wave, Groups.player.size(), Strings.capitalize(world.getMap().name())); info("&lcGame over! Reached wave &ly{0}&lc with &ly{1}&lc players online on map &ly{2}&lc.", state.wave, Groups.player.size(), Strings.capitalize(state.map.name()));
}else{ }else{
info("&lcGame over! Team &ly{0}&lc is victorious with &ly{1}&lc players online on map &ly{2}&lc.", event.winner.name, Groups.player.size(), Strings.capitalize(world.getMap().name())); info("&lcGame over! Team &ly{0}&lc is victorious with &ly{1}&lc players online on map &ly{2}&lc.", event.winner.name, Groups.player.size(), Strings.capitalize(state.map.name()));
} }
//set next map to be played //set next map to be played
Map map = nextMapOverride != null ? nextMapOverride : maps.getNextMap(world.getMap()); Map map = nextMapOverride != null ? nextMapOverride : maps.getNextMap(state.map);
nextMapOverride = null; nextMapOverride = null;
if(map != null){ if(map != null){
Call.onInfoMessage((state.rules.pvp Call.onInfoMessage((state.rules.pvp
@@ -220,8 +219,7 @@ public class ServerControl implements ApplicationListener{
return; return;
} }
}else{ }else{
Array<Map> maps = Vars.maps.customMaps().size == 0 ? Vars.maps.defaultMaps() : Vars.maps.customMaps(); result = maps.getShuffleMode().next(state.map);
result = maps.random();
info("Randomized next map to be {0}.", result.name()); info("Randomized next map to be {0}.", result.name());
} }
@@ -280,7 +278,7 @@ public class ServerControl implements ApplicationListener{
info("Status: &rserver closed"); info("Status: &rserver closed");
}else{ }else{
info("Status:"); info("Status:");
info(" &lyPlaying on map &fi{0}&fb &lb/&ly Wave {1}", Strings.capitalize(world.getMap().name()), state.wave); info(" &lyPlaying on map &fi{0}&fb &lb/&ly Wave {1}", Strings.capitalize(state.map.name()), state.wave);
if(state.rules.waves){ if(state.rules.waves){
info("&ly {0} enemies.", state.enemies); info("&ly {0} enemies.", state.enemies);
@@ -884,7 +882,7 @@ public class ServerControl implements ApplicationListener{
Call.onWorldDataBegin(); Call.onWorldDataBegin();
run.run(); run.run();
state.rules = world.getMap().applyRules(lastMode); state.rules = state.map.applyRules(lastMode);
logic.play(); logic.play();
for(Playerc p : players){ for(Playerc p : players){

View File

@@ -201,7 +201,7 @@ public class ApplicationTests{
@Test @Test
void load(){ void load(){
world.loadMap(testMap); world.loadMap(testMap);
Map map = world.getMap(); Map map = state.map;
SaveIO.save(saveDirectory.child("0.msav")); SaveIO.save(saveDirectory.child("0.msav"));
resetWorld(); resetWorld();

View File

@@ -171,9 +171,9 @@ def scaleImage = { File file ->
M = getRGB(x, y - 2) M = getRGB(x, y - 2)
if(B == D && B != F && D != H && (E != A || E == C || E == G || A == J || A == K)) p1 = B if(B == D && B != F && D != H && (E != A || E == C || E == G || A == J || A == K)) p1 = B
if(B == F & B != D & F != H && (E != C || E == A || E == I || C == J || C == L)) p2 = F if(B == F && B != D && F != H && (E != C || E == A || E == I || C == J || C == L)) p2 = F
if(D == H & B != D & F != H && (E != G || E == A || E == I || G == K || G == M)) p3 = D if(D == H && B != D && F != H && (E != G || E == A || E == I || G == K || G == M)) p3 = D
if(F == H & B != F & D != H && (E != I || E == C || E == G || I == L || I == M)) p4 = H if(F == H && B != F && D != H && (E != I || E == C || E == G || I == L || I == M)) p4 = H
scaled.setRGB(x * 2, y * 2 + 1, p1) scaled.setRGB(x * 2, y * 2 + 1, p1)
scaled.setRGB(x * 2 + 1, y * 2 + 1, p2) scaled.setRGB(x * 2 + 1, y * 2 + 1, p2)