From 107adc304c3a917eea78ae718ac321ce31966046 Mon Sep 17 00:00:00 2001 From: "sigonasr2, Sig, Sigo" Date: Mon, 13 Jun 2022 14:07:30 +0000 Subject: [PATCH] Change how maps are loaded so they are more dynamic. When going from gameplay to editing mode events may have been consumed. Co-authored-by: r3cp3ct <45179536+r3cp3ct@users.noreply.github.com> Co-authored-by: sigonasr2 --- src/sig/RabiClone.java | 5 +++- src/sig/map/DataTile.java | 18 ++++++++++++ src/sig/map/Map.java | 59 ++++++++++++++++++++++++++++++++++++++- src/sig/map/Maps.java | 2 +- 4 files changed, 81 insertions(+), 3 deletions(-) create mode 100644 src/sig/map/DataTile.java diff --git a/src/sig/RabiClone.java b/src/sig/RabiClone.java index a920584..36415e8 100644 --- a/src/sig/RabiClone.java +++ b/src/sig/RabiClone.java @@ -51,7 +51,7 @@ public class RabiClone { public static ConfigureControls control_settings_menu; public static Player player; - public static Maps CURRENT_MAP = Maps.WORLD1; + public static Maps CURRENT_MAP; public static Controller[] CONTROLLERS = new Controller[] {}; public static long lastControllerScan = System.currentTimeMillis(); @@ -73,6 +73,8 @@ public class RabiClone { f.setUndecorated(true); f.setSize(BASE_WIDTH, BASE_HEIGHT); // 1024x576 (64x64) ChooseBestRatio(); + + Map.LoadMap(Maps.WORLD1); p = new Panel(f); @@ -118,6 +120,7 @@ public class RabiClone { if (!(level_renderer instanceof EditorRenderer)) { OBJ.clear(); ResetGame(); + Map.LoadMap(Maps.WORLD1,CURRENT_MAP.getMap()); OBJ.add(level_renderer = new EditorRenderer(p)); } } diff --git a/src/sig/map/DataTile.java b/src/sig/map/DataTile.java new file mode 100644 index 0000000..5c42a8b --- /dev/null +++ b/src/sig/map/DataTile.java @@ -0,0 +1,18 @@ +package sig.map; + +import sig.engine.String; + +public enum DataTile { + NULL, //File is populated by 0s by default. This represents nothing. + BUN1("Spawns a blue bun"), + BUN2("Spawns a green bun"), + BUN3("Spawns a yellow bun"), + BUN4("Spawns a red bun"); + + String description; + + DataTile(){} + DataTile(java.lang.String s) { + this.description=new String(s); + } +} diff --git a/src/sig/map/Map.java b/src/sig/map/Map.java index b255ca8..09be4e9 100644 --- a/src/sig/map/Map.java +++ b/src/sig/map/Map.java @@ -3,8 +3,10 @@ package sig.map; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.FileInputStream; +import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; +import java.util.Arrays; import sig.RabiClone; import sig.engine.Sprite; @@ -13,6 +15,7 @@ public class Map { //Maps contain 512x288 tiles, allowing for 16384x9216 pixels of action per map. //Since a screen normally fits 16x9 tiles, you get 32x32 (1024) screens of gameplay per world. // + //Starts with 294912 bytes for visual tiles. //After the map data, the next 1024 bytes will indicate the map view information. //After that, the next 1024 bytes will indicate the background information. //After that, the next 1024 bytes will indicate the map color information. @@ -45,7 +48,14 @@ public class Map { public static Map LoadMap(Maps map) { try { - Map newMap = new Map(); + if (RabiClone.CURRENT_MAP!=map) { + resetMapData(RabiClone.CURRENT_MAP.getMap()); + } else { + resetAndReloadEventData(RabiClone.CURRENT_MAP); + return RabiClone.CURRENT_MAP.getMap(); + } + RabiClone.CURRENT_MAP=map; + Map newMap = RabiClone.CURRENT_MAP.getMap()!=null?RabiClone.CURRENT_MAP.getMap():new Map(); DataInputStream stream = new DataInputStream(new FileInputStream(map.getFile())); int marker=0; int iterationCount=MAP_WIDTH*MAP_HEIGHT; @@ -107,6 +117,41 @@ public class Map { return null; } + private static void resetAndReloadEventData(Maps map) { + Arrays.fill(map.getMap().data,(char)0); + map.getMap().eventTileCount=0; + try { + DataInputStream stream = new DataInputStream(new FileInputStream(map.getFile())); + final long requestBytes = 294912+1024+1024+1024+1024; + long actualBytes = stream.skip(requestBytes); + if (actualBytes!=requestBytes) { + stream.close(); + throw new IOException("Could not read "+requestBytes+" bytes! Read "+actualBytes+" instead."); + } + + map.getMap().eventTileCount=stream.readInt(); + int remainingCount = map.getMap().eventTileCount; + while (remainingCount-->0) { + int dataPacket = stream.readInt(); + //First 14 bits are event info. Last 18 bits are index info. + char event = (char)(dataPacket>>>18); + int index = dataPacket&0b00000000000000111111111111111111; + map.getMap().data[index]=event; + } + stream.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + private static void resetMapData(Map newMap) { + Arrays.fill(newMap.tiles,(char)0); + Arrays.fill(newMap.views,(byte)0); + Arrays.fill(newMap.backgrounds,(byte)0); + Arrays.fill(newMap.colors,(byte)0); + Arrays.fill(newMap.types,(byte)0); + } + public static void SaveMap(Maps map) throws IOException { DataOutputStream stream = new DataOutputStream(new FileOutputStream(map.getFile())); saveCharData(stream,map.map.tiles); @@ -158,4 +203,16 @@ public class Map { tiles[y*Map.MAP_WIDTH+x]=(char)(t.ordinal()); //System.out.println("Tile "+(y*MAP_WIDTH+x)+" is now "+tiles[y*MAP_WIDTH+x]+"."); } + + public void ModifyDataTile(int x,int y,DataTile t) { + DataTile prevTile = DataTile.values()[data[y*Map.MAP_WIDTH+x]]; + if (prevTile.ordinal()==0) { + eventTileCount++; + } + if (t.ordinal()==0) { + eventTileCount--; + } + data[y*Map.MAP_WIDTH+x]=(char)(t.ordinal()); + //System.out.println("Tile "+(y*MAP_WIDTH+x)+" is now "+tiles[y*MAP_WIDTH+x]+"."); + } } diff --git a/src/sig/map/Maps.java b/src/sig/map/Maps.java index 35555be..006a5b4 100644 --- a/src/sig/map/Maps.java +++ b/src/sig/map/Maps.java @@ -13,7 +13,7 @@ public enum Maps { Maps(String filename) { file=new File(new File("..",MAPS_DIR),filename); - this.map=Map.LoadMap(this); + //this.map=Map.LoadMap(this); //We don't necessarily have to load the map right away. } public File getFile() {