Skip to content

Commit

Permalink
Holy mother of refactors
Browse files Browse the repository at this point in the history
  • Loading branch information
Meshulam Silk committed Feb 6, 2014
1 parent 6fd3151 commit a072e38
Show file tree
Hide file tree
Showing 74 changed files with 1,324 additions and 1,213 deletions.
Binary file modified Build/Grame.jar
Binary file not shown.
Binary file modified src/com/moomoohk/Grame/.DS_Store
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
package com.moomoohk.Grame.AI;
package com.moomoohk.Grame.Basics.AI;

import java.awt.Color;
import java.io.Serializable;
import java.util.ArrayList;

import com.moomoohk.Grame.Basics.Dir;
import com.moomoohk.Grame.Basics.Entity;
import com.moomoohk.Grame.Basics.MovementAI;
import com.moomoohk.Grame.Basics.Schematic;
import com.moomoohk.Grame.Essentials.Base;
import com.moomoohk.Grame.Essentials.Coordinates;
import com.moomoohk.Grame.Essentials.GrameManager;
import com.moomoohk.Grame.Essentials.GrameObjectLayer;
import com.moomoohk.Grame.Essentials.GrameUtils;
import com.moomoohk.Grame.Graphics.PlainGridRender;
import com.moomoohk.Grame.Graphics.RenderManager;
import com.moomoohk.Grame.Interfaces.MainGrameClass;
import com.moomoohk.Grame.Interfaces.MovementAI;
import com.moomoohk.Grame.Core.Coordinates;
import com.moomoohk.Grame.Core.GrameManager;
import com.moomoohk.Grame.Core.GrameObjectLayer;
import com.moomoohk.Grame.Core.GrameUtils;
import com.moomoohk.Grame.Core.Grid;
import com.moomoohk.Grame.Core.MainGrameClass;
import com.moomoohk.Grame.Core.Graphics.CleanGridRender;
import com.moomoohk.Grame.Core.Graphics.RenderManager;

public class AStarPathfindingMovementAI extends MovementAI implements MainGrameClass
{
Expand All @@ -37,23 +37,23 @@ public String author()
}

@Override
public Coordinates getNext(Coordinates pos, Coordinates targetPos, Base b, Entity ent1, Entity ent2)
public Coordinates getNext(Coordinates pos, Coordinates targetPos, Grid g, Entity ent1, Entity ent2)
{
if (ent1.getPos(b.ID).distance(ent2.getPos(b.ID)) == 1)
if (ent1.getPos(g.ID).distance(ent2.getPos(g.ID)) == 1)
return pos;
if (cachedTargetPos != null)
if (cachedTargetPos.equals(targetPos))
{
Node last = getLast(cachedNodePath.get(cachedNodePath.size() - 1));
if (cachedNodePath.remove(last)) //TODO: Fix this list
if (cachedNodePath.remove(last))
return last.getPos();
}
else
{
if (showVisualization)
b.setFloorColor(Color.white);
g.setFloorColor(Color.white);
if (showCosts)
RenderManager.clearText(b.ID);
RenderManager.clearText(g.ID);
cachedTargetPos = targetPos;
}
else
Expand All @@ -70,11 +70,11 @@ public Coordinates getNext(Coordinates pos, Coordinates targetPos, Base b, Entit
if (!current.getPos().equals(pos))
closed.add(current);
open.remove(current);
if (current.getPos().distance(ent2.getPos(b.ID)) == 1)
if (current.getPos().distance(ent2.getPos(g.ID)) == 1)
break;
for (Coordinates sur : current.getPos().getAllSurrounding(b))
for (Coordinates sur : current.getPos().getAllSurrounding(g))
{
if (!b.isInBase(sur) || b.isOccupied(sur) || contains(closed, sur))
if (!g.isInGrid(sur) || g.isOccupied(sur) || contains(closed, sur))
{
open.remove(getNodeFromList(open, sur));
continue;
Expand All @@ -90,18 +90,17 @@ public Coordinates getNext(Coordinates pos, Coordinates targetPos, Base b, Entit
}
current = findLowestFCost(open);
}
while (current.getPos() != targetPos); //FIXME: NullPointerException when can't reach target
while (current.getPos() != targetPos);

Node node = getLast(closed.get(closed.size() - 1));
// visualizationColor = new Color(new Random().nextFloat(), new Random().nextFloat(), new Random().nextFloat());
if (showCosts || showVisualization)
for (int i = 0; i < closed.size(); i++)
{
if (showVisualization)
b.setFloorColor(closed.get(i).getPos(), new Color((255 - ((255 - visualizationColor.getRed()) / closed.size() * i)), (255 - ((255 - visualizationColor.getGreen()) / closed.size() * i)), (255 - ((255 - visualizationColor.getBlue()) / closed.size() * i))));
g.setFloorColor(closed.get(i).getPos(), new Color((255 - ((255 - visualizationColor.getRed()) / closed.size() * i)), (255 - ((255 - visualizationColor.getGreen()) / closed.size() * i)), (255 - ((255 - visualizationColor.getBlue()) / closed.size() * i))));
if (showCosts)
RenderManager.setText(b.ID, new Coordinates(closed.get(i).getPos().getY(), closed.get(i).getPos().getX()), /*"F" + (int)(calcG(current, closed.get(i).getPos()) + calcH(closed.get(i).getPos(), targetPos))*/
"G" + (int) calcG(current, closed.get(i).getPos()) + "H" + (int) calcH(closed.get(i).getPos(), targetPos), costColor);
RenderManager.setText(g.ID, new Coordinates(closed.get(i).getPos().getY(), closed.get(i).getPos().getX()), /*"F" + (int)(calcG(current, closed.get(i).getPos()) + calcH(closed.get(i).getPos(), targetPos))*/
"G" + (int) calcG(current, closed.get(i).getPos()) + "H" + (int) calcH(closed.get(i).getPos(), targetPos), costColor);
}

cachedNodePath = closed;
Expand Down Expand Up @@ -166,9 +165,26 @@ private Node findLowestFCost(ArrayList<Node> open)
}

@Override
public boolean isValid(Coordinates pos, Coordinates targetPos, Base b, Entity ent1, Entity ent2)
public boolean isValid(Coordinates pos, Coordinates targetPos, Grid g, Entity ent1, Entity ent2)
{
return !pos.isSurrounded(b);
// boolean[][] checked = new boolean[b.getRows()][b.getColumns()];
// for (int i = 0; i < checked.length; i++)
// for (int j = 0; j < checked[0].length; j++)
// checked[i][j] = false;
// return floodCheck(checked, pos, targetPos, b);
return true;
}

public boolean floodCheck(boolean[][] checked, Coordinates pos, Coordinates targetPos, Grid g)
{
g.setFloorColor(pos, GrameUtils.randomColor());
if (pos.equals(targetPos))
return true;
checked[pos.getY()][pos.getX()] = true;
for (Coordinates sur : pos.getSurrounding(g))
if (!checked[sur.getY()][sur.getX()])
return floodCheck(checked, sur, targetPos, g);
return true;
}

@Override
Expand Down Expand Up @@ -247,25 +263,25 @@ public static void main(String[] args)
@Override
public void newGame()
{
Base b = new Base(20, 20);
b.setWraparound(true);
Grid g = new Grid(20, 20);
g.setWraparound(true);
Entity player = new Entity("Player", Color.gray);
Entity monster = new Entity("Monster", Color.red);
b.addGrameObjectLayer(new GrameObjectLayer(b.getColumns(), b.getRows()), 1);
player.makePlayer(1, true, b.ID);
g.addGrameObjectLayer(new GrameObjectLayer(g.getColumns(), g.getRows()), 1);
player.makePlayer(1, true, g.ID);
player.setSpeed(1);
monster.setTarget(player.ID);
AStarPathfindingMovementAI aStar = new AStarPathfindingMovementAI();
monster.addAI(aStar, b.ID);
monster.addAI(aStar, g.ID);
monster.setSpeed(5);
b.addGrameObject(player, new Coordinates(10, 10));
b.addGrameObject(monster, new Coordinates(18, 10), 1);
g.addGrameObject(player, new Coordinates(10, 10));
g.addGrameObject(monster, new Coordinates(18, 10), 1);
for (int i = 1; i <= 10; i++)
new Schematic().load(b, GrameUtils.randomCoordinates(b));
new Schematic().load(g, GrameUtils.randomCoordinates(g));
// Schematic s = new Schematic(1);
// System.out.println(s.toString());
// s.load(b, new Coordinates(10, 10));
RenderManager.render(b.ID, new PlainGridRender());
RenderManager.render(g.ID, new CleanGridRender());
RenderManager.setVisible(true);
aStar.showVisualization = true;
// for (int i = 0; i < b.getColumns(); i++)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package com.moomoohk.Grame.AI;
package com.moomoohk.Grame.Basics.AI;

import com.moomoohk.Grame.Basics.Dir;
import com.moomoohk.Grame.Basics.Entity;
import com.moomoohk.Grame.Essentials.Base;
import com.moomoohk.Grame.Essentials.Coordinates;
import com.moomoohk.Grame.Essentials.GrameManager;
import com.moomoohk.Grame.Interfaces.GrameObject;
import com.moomoohk.Grame.Interfaces.MovementAI;
import com.moomoohk.Grame.Basics.MovementAI;
import com.moomoohk.Grame.Core.Coordinates;
import com.moomoohk.Grame.Core.GrameManager;
import com.moomoohk.Grame.Core.GrameObject;
import com.moomoohk.Grame.Core.Grid;

/**
* AI that lets the user control {@link GrameObject}s using the keyboard.
Expand All @@ -32,27 +32,27 @@ public PlayerMovementAI(int player)
this.player = player;
}

public Coordinates getNext(Coordinates pos, Coordinates target, Base b, Entity ent1, Entity ent2)
public Coordinates getNext(Coordinates pos, Coordinates target, Grid g, Entity ent1, Entity ent2)
{
Dir d = player == 1 ? GrameManager.dir1 : player == 2 ? GrameManager.dir2 : null;
if (d == null)
return pos;
int layer = -1;
for (int i = 0; i < b.getGrameObjectLayers().size(); i++)
if (b.getGrameObjectLayers().get(i).contains(ent1.ID))
for (int i = 0; i < g.getGrameObjectLayers().size(); i++)
if (g.getGrameObjectLayers().get(i).contains(ent1.ID))
layer = i;
if (!b.isInBase(MovementAI.slide(b, pos, d, layer)))
if (b.getWraparound())
return MovementAI.wraparound(b, MovementAI.slide(b, pos, d, layer), d);
if (!g.isInGrid(MovementAI.slide(g, pos, d, layer)))
if (g.getWraparound())
return MovementAI.wraparound(g, MovementAI.slide(g, pos, d, layer), d);
else
return pos;
else
return MovementAI.slide(b, pos, d, layer);
return MovementAI.slide(g, pos, d, layer);
}

public boolean isValid(Coordinates pos, Coordinates target, Base b, Entity ent1, Entity ent2)
public boolean isValid(Coordinates pos, Coordinates target, Grid g, Entity ent1, Entity ent2)
{
return ent1.isPlayer(b.ID);
return ent1.isPlayer(g.ID);
}

public boolean isOverride()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package com.moomoohk.Grame.AI;
package com.moomoohk.Grame.Basics.AI;

import java.util.Random;

import com.moomoohk.Grame.Basics.Dir;
import com.moomoohk.Grame.Basics.Entity;
import com.moomoohk.Grame.Essentials.Base;
import com.moomoohk.Grame.Essentials.Coordinates;
import com.moomoohk.Grame.Interfaces.MovementAI;
import com.moomoohk.Grame.Basics.MovementAI;
import com.moomoohk.Grame.Core.Coordinates;
import com.moomoohk.Grame.Core.Grid;

/**
* AI that is supposed to simulate a player.
Expand All @@ -30,7 +30,7 @@ public PlayerSimAI()
{
super();
this.step = 0;
this.tries=0;
this.tries = 0;
}

@Override
Expand All @@ -40,37 +40,37 @@ public String author()
}

@Override
public Coordinates getNext(Coordinates pos, Coordinates targetPos, Base b, Entity ent1, Entity ent2)
public Coordinates getNext(Coordinates pos, Coordinates targetPos, Grid g, Entity ent1, Entity ent2)
{
if (this.step == 0)
this.direction = generateDir();
Coordinates next = pos.addDir(this.direction);
if (b.getWraparound())
next = MovementAI.wraparound(b, pos, this.direction);
if (g.getWraparound())
next = MovementAI.wraparound(g, pos, this.direction);
else
if (!b.isInBase(next))
if (!g.isInGrid(next))
{
this.step = 0;
return pos;
}
if(b.isOccupied(next))
if (g.isOccupied(next))
{
this.step=0;
this.step = 0;
this.tries++;
if(this.tries==10)
if (this.tries == 10)
{
this.tries=0;
this.tries = 0;
return pos;
}
return getNext(pos, targetPos, b, ent1, ent2);
return getNext(pos, targetPos, g, ent1, ent2);
}
this.tries=0;
this.tries = 0;
this.step--;
return next;
}

@Override
public boolean isValid(Coordinates pos, Coordinates targetPos, Base b, Entity ent1, Entity ent2)
public boolean isValid(Coordinates pos, Coordinates targetPos, Grid g, Entity ent1, Entity ent2)
{
return true;
}
Expand All @@ -86,7 +86,7 @@ private Dir generateDir()
this.step = new Random().nextInt(30) + 10;
return Dir.getAllDirs()[new Random().nextInt(Dir.getAllDirs().length)];
}

public String toString()
{
return "Player sim";
Expand Down
Loading

0 comments on commit a072e38

Please sign in to comment.