Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (476)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (532)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
  JavaGaming.org - Pastebin



Author: counterp (posted 2012-07-08 07:41:01, viewed 278 times)

1   2   3   4   5   6   7   8   9   10   11   12   13   14   15   16   17   18   19   20   21   22   23   24   25   26   27   28   29   30   31   32   33   34   35   36   37   38   39   40   41   42   43   44   45   46   47   48   49   50   51   52   53   54   55   56   57   58   59   60   61   62   63   64   65   66   67   68   69   70   71   72   73   74   75   76   77   78   79   80   81   82   83   84   85   86   87   88   89   90   91   92   93   94   95   96   97   98   99   100   101   102   103   104   105   106   107   108   109   110   111   112   113   114   115   116   117   118   119   120   121   122   123   124   125   126   127   128   129   130   131   132   133   134   135   136   137   138   139   140   141   142   143   144   145   146   147   148   149   150   151   152   153   154   155   156   157   158   159   160   161   162   163   164   165   166   167   168   169   170   171   172   173   174   175   176   177   178   179   180   181   182   183   184   185   186   187   188   189   190   191   192   193   194   195   196   197   198   199   200   201   202   203   204   205   206   207   208   209   210   211   212   213   214   215   216   217   218   219   220   221   222   223   224   225   226   227   228   229   230   231   232   233   234   235   236   237   238   239   240   241   242   243   244   245   246   247   248   249   250   251   252   253   254   255   256   257   258   259   260   261   262   263   264   265   266   267   268   269   270   271   272   273   274   275   276   277   278   279   280   281   282   283   284   285   286   287   288   289   290   291   292   293   294   295   296   297   298   299   300   301   302   303   304   305   306   307   308   309   310   311   312   313   314   315   316   317   318   319   320   321   322   323   324   325   326   327   328   329   330   331   332   333   334   335   336   337   338   339   340   341   342   343   344   345   346   347   348   349   350   351   352   353   354   355   356   357   358   359   360   361   362   363   364   365   366   367   368   369   370   371   372   373   374   375   376   377   378   379   380   381   382   383   384   385   386   387   388   389   390   391   392  
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferStrategy;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import javax.swing.JFrame;


public class PathFinderTest {

   public static void main(String[] args) {
      new PathFinderTest();
   }

   public static final int MAX_UNIT_SIZE_X = 2;
   public static final int MAX_UNIT_SIZE_Y = 3;

   public BufferStrategy buffer;
   public int width = 1000;
   public int height = 600;
   public int size = 50;
   public int[][] grid = new int[width / size][height / size];
   public int[][] xclearance = new int[width / size][height / size];
   public int[][] yclearance = new int[width / size][height / size];
   public boolean dragged;
   public long time = 0;
   public long totalTime = 0;
   public long trials = 0;

   public int unitSizeX = MAX_UNIT_SIZE_X;
   public int unitSizeY = MAX_UNIT_SIZE_Y;

   public PathFinderTest() {
      JFrame frame = new JFrame("Path Finder Test");
      frame.setSize(width, height);
      Canvas canvas = new Canvas();
      frame.setIgnoreRepaint(true);
      canvas.setIgnoreRepaint(true);
      canvas.addMouseMotionListener(new MouseMotionListener() {

         int lastX;
         int lastY;

         @Override
         public void mouseDragged(MouseEvent e) {
            int x = e.getX() / size;
            int y = e.getY() / size;
            if (x < 0 || x >= grid.length || y < 0 || y >= grid[x].length)
               return;
            if (e.getModifiers() == MouseEvent.BUTTON2_MASK && (lastX != x || lastY != y)) {
               grid[x][y] = grid[x][y] == 3 ? 0 : 3;
               dragged = true;
               calculate();
            }
            lastX = x;
            lastY = y;
         }

         @Override
         public void mouseMoved(MouseEvent arg0) {
            
         }
         
      });
      canvas.addMouseListener(new MouseListener() {

         Thread thread;

         @Override
         public void mouseClicked(MouseEvent e) {
         }

         @Override
         public void mouseEntered(MouseEvent arg0) {
            // TODO Auto-generated method stub
            
         }

         @Override
         public void mouseExited(MouseEvent arg0) {
            // TODO Auto-generated method stub
            
         }

         @Override
         public void mousePressed(MouseEvent e) {
         }

         @Override
         public void mouseReleased(MouseEvent e) {
            if (dragged) {
               dragged = false;
               return;
            }
            int x = e.getX() / size;
            int y = e.getY() / size;
            if (e.getButton() == MouseEvent.BUTTON1) {
               for (int i = 0; i < grid.length; i++)
                  for (int j = 0; j < grid[i].length; j++)
                     if (grid[i][j] == 1)
                        grid[i][j] = 0;
               grid[x][y] = grid[x][y] == 1 ? 0 : 1;
            } else if (e.getButton() == MouseEvent.BUTTON3) {
               for (int i = 0; i < grid.length; i++)
                  for (int j = 0; j < grid[i].length; j++)
                     if (grid[i][j] == 2)
                        grid[i][j] = 0;
               grid[x][y] = grid[x][y] == 2 ? 0 : 2;
            } else if (e.getButton() == MouseEvent.BUTTON2) {
               grid[x][y] = grid[x][y] == 3 ? 0 : 3;
            }
            if (thread != null)
               thread.interrupt();
            thread = new Thread() {
               @Override
               public void run() {
                  try {
                     calculate();
                  } catch (Exception e) {
                     e.printStackTrace();
                  }
               }
            };
            thread.start();
         }
         
      });
      canvas.setSize(frame.getSize());
      frame.add(canvas);
      frame.setResizable(false);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.pack();
      frame.setVisible(true);
      canvas.createBufferStrategy(2);
      buffer = canvas.getBufferStrategy();
      calculate();
      while (true) {
         try {
            
            do {

               do {
                  Graphics2D g = (Graphics2D) buffer.getDrawGraphics();

                  g.setColor(Color.black);
                  g.fillRect(0,0,width,height);

                  paint(g);

                  g.dispose();

               } while (buffer.contentsRestored());

               buffer.show();

            } while (buffer.contentsLost());

         } catch (Exception e) {
            e.printStackTrace();
         }
         try {
            Thread.sleep(20);
         } catch (InterruptedException e) {
            e.printStackTrace();
         }
      }
   }

   public void calculate() {
      int startX = -1;
      int startY = -1;
      int endX = -1;
      int endY = -1;

      int offset = MAX_UNIT_SIZE_Y - MAX_UNIT_SIZE_X;
      for (int i = 0; i < grid.length; i++)
         for (int j = 0; j < grid[i].length; j++) {
            if (grid[i][j] != 3) {
               if (grid[i][j] == 4)
                  grid[i][j] = 0;
               int n;
               main: for (n = 1; n < MAX_UNIT_SIZE_X && n + i < grid.length && n + j + offset < grid[n].length; n++) {
                  int yoff = n + offset;
                  if (grid[i + n][j + yoff] == 3)
                     break;
                  for (int x = i; x < i + n; x++) {
                     if (grid[x][j + yoff] == 3)
                        break main;
                  }
                  for (int y = j; y < j + yoff; y++) {
                     if (grid[i + n][y] == 3) 
                        break main;
                  }
               }
               xclearance[i][j] = n;
               yclearance[i][j] = n + offset;
            }
            if (grid[i][j] == 1) {
               startX = i;
               startY = j;
            } else if (grid[i][j] == 2) {
               endX = i;
               endY = j;
            }
         }


      if (startX != -1 && endX != -1) {
         long startTime = System.nanoTime();
         end = new Node(endX, endY, null);
         Node start = new Node(startX, startY, null);
         open.add(start);
         Node current = null;
         while (!open.isEmpty()) {
            for (Iterator<Node> it$ = open.iterator(); it$.hasNext(); ) {
               Node node = it$.next();
               if (current == null) {
                  current = node;
               } else if (node.cost < current.cost)
                     current = node;
            }
            if (current == null)
               break;
            if (current.equals(end) || ((unitSizeX > 1 || unitSizeY > 1) && current.inReach(end, unitSizeX, unitSizeY))) {
               Node parent = current;
               while (parent != start) {
                  if (unitSizeX == 1 && unitSizeY == 1)
                     grid[parent.x][parent.y] = 4;
                  else {
                     for (int x = 0; x < unitSizeX; x++)
                        for (int y = 0; y < unitSizeY; y++)
                           grid[parent.x + x][parent.y + y] = 4;
                     grid[start.x][start.y] = 1; 
                     grid[end.x][end.y] = 2; 
                  }
                  parent = parent.parent;
               }
               break;
            }
            closed.add(current);
            open.remove(current);
            Node[] nodes = getSurrounding(current);
            for (int i = 0; i < nodes.length; i++) {
               if (!nodes[i].traversible || xclearance[nodes[i].x][nodes[i].y] < unitSizeX || yclearance[nodes[i].x][nodes[i].y] < unitSizeY)
                  continue;
               boolean inClosed = closed.contains(nodes[i]);
               boolean inOpen = open.contains(nodes[i]);

               int cost = 10;
               if (nodes[i].x != current.x && nodes[i].y != current.y)
                  cost = 14;

               float traversal = current.cost + cost;

               if (!inOpen && !inClosed) {
                  nodes[i].parent = current;
                  nodes[i].cost = traversal;

                  if (inClosed) {
                     closed.remove(nodes[i]);
                  }
                  if (!inOpen) {
                     open.add(nodes[i]);
                  }
               }
            }
            current = null;
         }
         open.clear();
         closed.clear();
         long endTime = System.nanoTime();
         time = (endTime - startTime) / 1000000;
         totalTime += time;
         trials++;
      }
   }

   public Node[] getSurrounding(Node n) {
      return new Node[] {
            new Node(n.x + 1, n.y, n),
            new Node(n.x - 1, n.y, n),
            new Node(n.x, n.y - 1, n),
            new Node(n.x, n.y + 1, n),

            new Node(n.x + 1, n.y + 1, n),
            new Node(n.x + 1, n.y - 1, n),
            new Node(n.x - 1, n.y + 1, n),
            new Node(n.x - 1, n.y - 1, n)
      };
   }

   public void paint(Graphics2D g) {
      for (int x = 0; x < grid.length; x++) {
         for (int y = 0; y < grid[x].length; y++) {
            Color color = null;
            switch (grid[x][y]) {
            case 0:
               color = Color.white;
               break;
            case 1:
               color = Color.green;
               break;
            case 2:
               color = Color.red;
               break;
            case 3:
               color = Color.blue;
               break;
            case 4:
               color = Color.black;
               break;
            case 5:
               color = Color.pink;
               break;
            }
            g.setColor(color);
            g.fillRect(x * size, y * size, size, size);
            g.setColor(Color.cyan);
            g.drawRect(x * size, y * size, size, size);
            if (color == Color.black)
               g.setColor(Color.white);
            else g.setColor(Color.black);
            g.drawString(xclearance[x][y] + ", " + yclearance[x][y], x * size, y * size + 11);
         }
      }
      /*g.setColor(Color.white);
      g.fillRect(3, 3, 150, 24);
      g.setColor(Color.red);
      g.drawString("Last calcuation time: " + time, 5, 12);
      g.drawString("Average time: " + (totalTime / (float) trials), 5, 24);
      g.drawString("Last calcuation time: " + time, 6, 12);
      g.drawString("Average time: " + (totalTime / (float) trials), 6, 24);*/
   }

   public Set<Node> open = new HashSet<Node>();
   public Set<Node> closed = new HashSet<Node>();
   public Node end;

   class Node {

      public Node(int x, int y, Node parent) {
         this.x = x;
         this.y = y;
         this.parent = parent;

         if (x < 0 || x >= grid.length || y < 0 || y >= grid[x].length)
            traversible = false;
         else
            traversible = grid[x][y] != 3;

      }

      public boolean traversible = true;

      public final int x;
      public final int y;

      public Node parent;
      public float cost;

      @Override
      public boolean equals(Object other_) {
         if (this == other_)
            return true;
         if (other_ == null)
            return false;
         if (other_ instanceof Node) {
            Node other = (Node) other_;
            return other.x == x && other.y == y;
         }
         return false;
      }

      @Override
      public int hashCode() {
         return x * size + y;
      }

      public boolean inReach(Node other, int sizeX, int sizeY) {
         return (x - other.x == 0 &&  y - other.y == 0) ||  (x + --sizeX - other.x == 0 &&  y + --sizeY - other.y == 0)
               || (x + - other.x == 0 &&  y + sizeY - other.y == 0) || (x + sizeX - other.x == 0 &&  y - other.y == 0);
      }

   }

}





Dump your java code here :



Special syntax:
  • To highlight a line (yellow background), prefix it with '@@'
  • To indicate that a line should be removed (red background), prefix it with '-'
  • To indicate that a line should be added (green background), prefix it with '+'
  • To post multiple snippets, seperate them by '~~~~'
  EOF
 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

pw (17 views)
2014-07-24 01:59:36

Riven (17 views)
2014-07-23 21:16:32

Riven (14 views)
2014-07-23 21:07:15

Riven (17 views)
2014-07-23 20:56:16

ctomni231 (45 views)
2014-07-18 06:55:21

Zero Volt (40 views)
2014-07-17 23:47:54

danieldean (32 views)
2014-07-17 23:41:23

MustardPeter (36 views)
2014-07-16 23:30:00

Cero (51 views)
2014-07-16 00:42:17

Riven (50 views)
2014-07-14 18:02:53
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!