/*     */ package org.world;
/*     */ 
/*     */ import java.awt.Point;
/*     */ import java.awt.geom.Point2D;
/*     */ import java.io.BufferedReader;
/*     */ import java.io.File;
/*     */ import java.io.FileReader;
/*     */ import java.io.IOException;
/*     */ import java.io.InputStreamReader;
/*     */ import java.util.Arrays;
/*     */ import java.util.LinkedList;
/*     */ import java.util.List;
/*     */ import javax.swing.JOptionPane;
/*     */ import org.bot.Direction;
/*     */ import org.bot.Gytebot;
/*     */ import org.objects.Coin;
/*     */ import org.objects.Wall;
/*     */ import org.objects.WorldObject;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class World
/*     */ {
/*     */   public static final int MAP_SIZE = 32;
/*     */   private List<String> rawMapData;
/*     */   private int rawDataWidth;
/*     */   private int coinCount;
/*     */   private String author;
/*     */   private String version;
/*     */   private String name;
/*     */   private String date;
/*     */   private String mapPath;
/*     */   private volatile WorldObject[][] objects;
/*     */   private volatile Gytebot gytebot;
/*     */   private boolean updateCoinBuffer;
/*     */   private int[] coinBufferData;
/*     */   private boolean updateWallBufferData;
/*     */   private float[] wallBufferData;
/*     */   
/*     */   public World() {
/*  71 */     this.rawMapData = new LinkedList<>();
/*     */     
/*  73 */     this.coinBufferData = new int[1024];
/*  74 */     this.wallBufferData = new float[1024];
/*     */ 
/*     */     
/*  77 */     setWorld("/data/standardlevel.map");
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   public final void setWorld(String fileName) {
/*  88 */     BufferedReader reader = null;
/*     */ 
/*     */ 
/*     */     
/*     */     try {
/*  93 */       reader = new BufferedReader(new InputStreamReader(getClass().getResourceAsStream(fileName)));
/*  94 */     } catch (Exception e1) {
/*     */       
/*     */       try {
/*  97 */         reader = new BufferedReader(new FileReader(new File(fileName)));
/*  98 */       } catch (Exception e2) {
/*     */         
/* 100 */         JOptionPane.showMessageDialog(null, "Unable to open Map: " + fileName, "Error - opening map", 0);
/*     */       } 
/*     */     } 
/*     */     
/* 104 */     if (reader != null) {
/*     */       
/*     */       try {
/*     */ 
/*     */ 
/*     */         
/* 110 */         this.gytebot = null;
/* 111 */         this.rawDataWidth = 0;
/* 112 */         this.rawMapData.clear();
/*     */ 
/*     */         
/*     */         try {
/* 116 */           reader.lines().forEachOrdered(line -> parseLine(line));
/*     */ 
/*     */           
/* 119 */           if (this.rawDataWidth > 0 && this.rawMapData.size() > 0) {
/*     */ 
/*     */ 
/*     */             
/* 123 */             this.objects = new WorldObject[this.rawDataWidth][this.rawMapData.size()];
/*     */             
/* 125 */             createWorldData();
/*     */             
/* 127 */             updateTransferBuffers();
/*     */             
/* 129 */             this.mapPath = fileName;
/*     */           
/*     */           }
/*     */           else {
/*     */             
/* 134 */             setWorld(this.mapPath);
/*     */             
/* 136 */             JOptionPane.showMessageDialog(null, "Unable to open Map: " + fileName, "Error - opening map", 0);
/*     */           } 
/* 138 */         } catch (Error e) {
/*     */           
/* 140 */           setWorld(this.mapPath);
/*     */           
/* 142 */           JOptionPane.showMessageDialog(null, "Unable to open Map: " + fileName, "Error - opening map", 0);
/*     */         } finally {
/*     */           
/* 145 */           reader.close();
/*     */         } 
/* 147 */       } catch (IOException e) {
/* 148 */         e.printStackTrace();
/*     */       } 
/*     */     }
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   public final void setDataString(String data) {
/* 161 */     this.gytebot = null;
/* 162 */     this.rawDataWidth = 0;
/* 163 */     this.rawMapData.clear();
/*     */ 
/*     */     
/*     */     try {
/* 167 */       Arrays.<String>asList(data.split("(?=\\n)")).forEach(line -> parseLine(line));
/*     */ 
/*     */       
/* 170 */       if (this.rawDataWidth > 0 && this.rawMapData.size() > 0) {
/*     */ 
/*     */         
/* 173 */         this.objects = new WorldObject[this.rawDataWidth][this.rawMapData.size()];
/*     */         
/* 175 */         createWorldData();
/*     */         
/* 177 */         updateTransferBuffers();
/*     */       
/*     */       }
/*     */       else {
/*     */ 
/*     */         
/* 183 */         setWorld(this.mapPath);
/* 184 */         JOptionPane.showMessageDialog(null, "The map was not properly loaded", "Error - converting map", 0);
/*     */       } 
/* 186 */     } catch (Error e) {
/*     */       
/* 188 */       setWorld(this.mapPath);
/* 189 */       JOptionPane.showMessageDialog(null, "Unable to convert Map: " + e.getMessage(), "Error - converting map", 0);
/*     */     } 
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   private void parseLine(String line) throws Error {
/* 201 */     String trimed = line.trim();
/*     */ 
/*     */     
/* 204 */     if (trimed.length() != 0) {
/*     */ 
/*     */       
/* 207 */       String[] splitted = trimed.split("[/](?=/)|(?=[@])");
/*     */       
/* 209 */       String rawMapLine = new String(); byte b;
/*     */       int i;
/*     */       String[] arrayOfString1;
/* 212 */       for (i = (arrayOfString1 = splitted).length, b = 0; b < i; ) { String content = arrayOfString1[b];
/*     */         
/* 214 */         if (content.startsWith("/")) {
/*     */           break;
/*     */         }
/*     */ 
/*     */         
/* 219 */         if (content.startsWith("@")) {
/*     */ 
/*     */           
/* 222 */           String[] tag = content.split("\\s+");
/*     */ 
/*     */           
/* 225 */           if (tag.length > 2)
/*     */           {
/*     */             
/* 228 */             throw new AssertionError("To many arguments provided for tag: " + content);
/*     */           }
/*     */ 
/*     */           
/*     */           String str;
/*     */           
/* 234 */           switch ((str = tag[0]).hashCode()) { case -440667701: if (str.equals("@author")) {
/* 235 */                 this.author = tag[1]; break;
/*     */               } 
/*     */             case 62181358:
/* 238 */               if (str.equals("@date")) { this.date = tag[1]; break; } case 62479051: if (str.equals("@name")) { this.name = tag[1]; break; } case 222319768: if (str.equals("@version")) { this.version = tag[1]; break; } 
/* 239 */             default: throw new AssertionError("Unknown tag found: " + tag[0]); }
/*     */ 
/*     */ 
/*     */         
/*     */         } else {
/* 244 */           rawMapLine = String.valueOf(rawMapLine) + content;
/*     */         }  b++; }
/*     */       
/* 247 */       if (rawMapLine.length() > 0) {
/* 248 */         this.rawMapData.add(rawMapLine);
/*     */       }
/*     */       
/* 251 */       this.rawDataWidth = Math.max(this.rawDataWidth, rawMapLine.length());
/*     */ 
/*     */       
/* 254 */       if (this.rawDataWidth > 32)
/*     */       {
/* 256 */         throw new AssertionError("Map size exceeds valid map bounds of 32x32! It is to wide");
/*     */       }
/* 258 */       if (this.rawMapData.size() > 32)
/*     */       {
/* 260 */         throw new AssertionError("Map size exceeds valid map bounds of 32x32! It is to large");
/*     */       }
/*     */     } 
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   private void createWorldData() {
/* 272 */     this.coinCount = 0;
/*     */ 
/*     */     
/* 275 */     for (int y = 0; y < this.rawMapData.size(); y++) {
/*     */       
/* 277 */       for (int x = 0; x < ((String)this.rawMapData.get(y)).length(); x++) {
/*     */         
/* 279 */         char current = ((String)this.rawMapData.get(y)).charAt(x);
/*     */         
/* 281 */         if (current == '#') {
/*     */ 
/*     */           
/* 284 */           this.objects[x][y] = (WorldObject)new Wall(new Point2D.Float(x, y));
/*     */         }
/* 286 */         else if (current == '€' || current == '$') {
/*     */ 
/*     */           
/* 289 */           this.objects[x][y] = (WorldObject)new Coin(new Point2D.Float(x, y));
/*     */           
/* 291 */           this.coinCount++;
/*     */         }
/* 293 */         else if (current == '^' || current == '>' || current == 'v' || current == 'V' || current == '<') {
/*     */ 
/*     */           
/* 296 */           if (this.gytebot != null) {
/*     */             
/* 298 */             System.err.println("Conflicting world rule: There is already a gytebot!");
/* 299 */             JOptionPane.showMessageDialog(null, "Conflicting world rule: There is already a gytebot", "Error - converting map", 0);
/*     */ 
/*     */ 
/*     */ 
/*     */           
/*     */           }
/* 305 */           else if (x < ((String)this.rawMapData.get(0)).length() && y < this.rawMapData.size()) {
/*     */             
/* 307 */             this.gytebot = new Gytebot(new Point2D.Float(x, y), Direction.directionFromCodePoint(current));
/*     */           } else {
/*     */             
/* 310 */             this.gytebot = null;
/* 311 */             JOptionPane.showMessageDialog(null, "Gytebot is placed outside Map", "Error - converting map", 0);
/*     */           }
/*     */         
/*     */         }
/* 315 */         else if (current != ' ') {
/*     */ 
/*     */           
/* 318 */           System.err.println("Unkown map entity: " + current);
/*     */         } 
/*     */       } 
/*     */     } 
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   private void updateTransferBuffers() {
/* 332 */     Arrays.fill(this.coinBufferData, 0);
/* 333 */     Arrays.fill(this.wallBufferData, 0.0F);
/*     */ 
/*     */     
/* 336 */     for (int y = 0; y < (this.objects[0]).length; y++) {
/* 337 */       for (int x = 0; x < this.objects.length; x++) {
/*     */         
/* 339 */         if (this.objects[x][y] instanceof Coin) {
/*     */ 
/*     */           
/* 342 */           this.coinBufferData[x + y * this.objects.length] = 1;
/*     */         }
/* 344 */         else if (this.objects[x][y] instanceof Wall) {
/*     */ 
/*     */           
/* 347 */           this.wallBufferData[x + y * 32] = 1.0F;
/*     */         } 
/*     */       } 
/*     */     } 
/*     */     
/* 352 */     this.updateCoinBuffer = true;
/* 353 */     this.updateWallBufferData = true;
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   public final void setWall(int x, int y) {
/* 364 */     setObject(x, y, (WorldObject)new Wall(new Point2D.Float(x, y)));
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   public final void setCoin(int x, int y) {
/* 375 */     setObject(x, y, (WorldObject)new Coin(new Point2D.Float(x, y)));
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   public final void removeObject(int x, int y) {
/* 385 */     if (this.objects[x][y] != null) {
/*     */ 
/*     */       
/* 388 */       this.objects[x][y] = null;
/*     */       
/* 390 */       updateTransferBuffers();
/*     */     
/*     */     }
/*     */     else {
/*     */       
/* 395 */       JOptionPane.showMessageDialog(null, "Index to remove object from is already empty", "Exception", 0);
/*     */     } 
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   private void setObject(int x, int y, WorldObject object) {
/* 410 */     if (x >= 0 && x < this.objects.length && y >= 0 && y < (this.objects[0]).length) {
/*     */ 
/*     */       
/* 413 */       this.objects[x][y] = object;
/*     */       
/* 415 */       updateTransferBuffers();
/*     */     }
/*     */     else {
/*     */       
/* 419 */       JOptionPane.showMessageDialog(null, "The requested object placement occured outside of valid bounds!", "Exception", 0);
/*     */     } 
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   public boolean isLocationObstructed(Point point) {
/* 432 */     if (point.x < 0 || point.y < 0 || point.x >= getWidth() || point.y >= getHeight())
/*     */     {
/* 434 */       return true;
/*     */     }
/* 436 */     if (this.objects[point.x][point.y] instanceof Wall)
/*     */     {
/* 438 */       return true;
/*     */     }
/*     */ 
/*     */ 
/*     */     
/* 443 */     return false;
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   public boolean isCoinPlacedHere(Point2D.Float location) {
/* 455 */     if (location.x < 0.0F || location.y < 0.0F || location.x >= getWidth() || location.y >= getHeight())
/*     */     {
/* 457 */       return false;
/*     */     }
/* 459 */     if (this.objects[Math.round(location.x)][Math.round(location.y)] instanceof Coin)
/*     */     {
/* 461 */       return true;
/*     */     }
/*     */ 
/*     */ 
/*     */     
/* 466 */     return false;
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   public final String toString() {
/* 476 */     return this.rawMapData.stream().reduce("", (A, B) -> String.valueOf(A) + B + System.lineSeparator());
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   public boolean updateWallBufferData() {
/* 486 */     if (this.updateWallBufferData) {
/*     */       
/* 488 */       this.updateWallBufferData = false;
/* 489 */       return true;
/*     */     } 
/*     */ 
/*     */     
/* 493 */     return false;
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   public boolean updateCoinTextureData() {
/* 504 */     if (this.updateCoinBuffer) {
/*     */       
/* 506 */       this.updateCoinBuffer = false;
/* 507 */       return true;
/*     */     } 
/*     */     
/* 510 */     return false;
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   public final int getCoinCount() {
/* 518 */     return this.coinCount;
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   public final String getAuthor() {
/* 525 */     return this.author;
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   public final String getVersion() {
/* 532 */     return this.version;
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   public final String getName() {
/* 539 */     return this.name;
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   public final String getDate() {
/* 546 */     return this.date;
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   public final synchronized Gytebot getGytebot() {
/* 553 */     return this.gytebot;
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   public final int[] getCoinBufferData() {
/* 560 */     return this.coinBufferData;
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   public final int getWidth() {
/* 567 */     return this.objects.length;
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   public final int getHeight() {
/* 574 */     return (this.objects[0]).length;
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   public int getDrawGytebotAsInt() {
/* 581 */     return (this.gytebot == null) ? 0 : 1;
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   public int[] getCoinTextureData() {
/* 588 */     return this.coinBufferData;
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   public float[] getWallBufferData() {
/* 595 */     return this.wallBufferData;
/*     */   }
/*     */ }


/* Location:              /home/teridax/Projects/Gytebot/GYTEBOT.jar!/org/world/World.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.1.3
 */