1
0
mirror of https://github.com/fazo96/AIrium.git synced 2025-01-10 09:34:20 +01:00

Fix some bugs

This commit is contained in:
Enrico Fasoli 2015-07-06 20:23:42 +02:00
parent 7ee5c502c4
commit 61454f7979
4 changed files with 380 additions and 332 deletions

View File

@ -42,7 +42,7 @@ public class Game extends ApplicationAdapter {
public void render() { public void render() {
// Controls // Controls
if (Gdx.input.isKeyJustPressed(Input.Keys.SPACE)) { if (Gdx.input.isKeyJustPressed(Input.Keys.SPACE)) {
world.newGen(false); world.launchNewGen();
} }
if (Gdx.input.isKeyPressed(Input.Keys.RIGHT)) { if (Gdx.input.isKeyPressed(Input.Keys.RIGHT)) {
renderer.translate(-cameraSpeed, 0, 0); renderer.translate(-cameraSpeed, 0, 0);

View File

@ -12,15 +12,16 @@ import logic.neural.Brain;
* *
* @author fazo * @author fazo
*/ */
public class Creature extends Element { public class Creature extends Element implements Runnable {
public static final int default_radius = 20, maxHp = 100; public static final int default_radius = 20, maxHp = 100;
public static final float max_speed = 3, max_beak = default_radius / 4; public static final float max_speed = 3, max_beak = default_radius / 4;
private Brain brain; private Brain brain;
private float dir, hp, prevHp, speed, sightRange, fov, fitness, rotSpeed, beak; private float dir, hp, prevHp, speed, sightRange, fov, fitness, rotSpeed, beak;
private boolean eating = false, killing = false; private boolean eating = false, killing = false, workerDone = false;
private Sight[] sights; private Sight[] sights;
private Thread workerThread;
public Creature(float x, float y) { public Creature(float x, float y) {
super(x, y, default_radius); super(x, y, default_radius);
@ -32,10 +33,25 @@ public class Creature extends Element {
sightRange = 100; sightRange = 100;
fov = (float) Math.PI / 2.5f; fov = (float) Math.PI / 2.5f;
fitness = 0; fitness = 0;
brain = new Brain(10, 5, 2, 10); brain = new Brain(9, 5, 2, 10);
sights = new Sight[2]; sights = new Sight[2];
} }
@Override
public void run() {
for (;;) {
if (workerDone) {
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
}
} else {
update();
workerDone = true;
}
}
}
@Override @Override
public void update() { public void update() {
// apply hunger // apply hunger
@ -94,7 +110,7 @@ public class Creature extends Element {
// 8: sight(c): hunger // 8: sight(c): hunger
// 7: sight(c): beak // 7: sight(c): beak
// OTHER: // OTHER:
// 9: food sensor // 8: food sensor
int viewSensors = 4; int viewSensors = 4;
for (int i = 0; i < sights.length; i++) { for (int i = 0; i < sights.length; i++) {
int mul = i * viewSensors; int mul = i * viewSensors;
@ -118,7 +134,8 @@ public class Creature extends Element {
} }
} }
} }
values[9] = eating ? 1 : 0; values[8] = eating || killing ? 1 : 0;
System.out.println(values[8]);
// compute behavior // compute behavior
float[] actions = null; float[] actions = null;
try { try {
@ -194,7 +211,6 @@ public class Creature extends Element {
// Try to see plant // Try to see plant
Element seen = null; Element seen = null;
float dist = 0, angle = 0, ndir = dir - (float) Math.PI; float dist = 0, angle = 0, ndir = dir - (float) Math.PI;
killing = false;
eating = false; eating = false;
for (Element e : Game.get().getWorld().getPlants()) { for (Element e : Game.get().getWorld().getPlants()) {
float tempDist = distanceFrom(e); float tempDist = distanceFrom(e);
@ -237,7 +253,6 @@ public class Creature extends Element {
angle = 0; angle = 0;
ndir = dir - (float) Math.PI; ndir = dir - (float) Math.PI;
killing = false; killing = false;
eating = false;
for (Element e : Game.get().getWorld().getCreatures()) { for (Element e : Game.get().getWorld().getCreatures()) {
if (e == this) { if (e == this) {
continue; continue;
@ -295,6 +310,26 @@ public class Creature extends Element {
} }
} }
/**
* Check if the Worker thread has finished its current iteration
*
* @return true if worker thread has finished its current iteration
*/
public boolean isWorkerDone() {
return workerDone;
}
/**
* Command the Worker thread to start another iteration.
*/
public void startWorker() {
workerDone = false;
if (workerThread == null) {
workerThread = new Thread(this);
workerThread.start();
}
}
public Brain getBrain() { public Brain getBrain() {
return brain; return brain;
} }

View File

@ -23,6 +23,7 @@ public class World implements Runnable {
private final int width, height, nPlants, creatPerGen; private final int width, height, nPlants, creatPerGen;
private int generation = 1; private int generation = 1;
private boolean multithreading = false, cmdLaunchNewGen = false;
private int fpsLimit = 60, fps = 0; private int fpsLimit = 60, fps = 0;
private Creature selected; private Creature selected;
private final ArrayList<Element> elements; private final ArrayList<Element> elements;
@ -108,6 +109,10 @@ public class World implements Runnable {
if (creatures.removeAll(graveyard)) { if (creatures.removeAll(graveyard)) {
fire(Listener.CREATURE_LIST_CHANGED); fire(Listener.CREATURE_LIST_CHANGED);
} }
if (cmdLaunchNewGen) {
newGen(false);
cmdLaunchNewGen = false;
}
if (creatures.isEmpty()) { if (creatures.isEmpty()) {
// All dead, next gen // All dead, next gen
newGen(false); newGen(false);
@ -120,7 +125,7 @@ public class World implements Runnable {
} }
} }
public void newGen(boolean restart) { private void newGen(boolean restart) {
elements.removeAll(creatures); elements.removeAll(creatures);
graveyard.addAll(creatures); graveyard.addAll(creatures);
creatures.clear(); creatures.clear();
@ -233,7 +238,7 @@ public class World implements Runnable {
} }
} }
public void fire(int eventCode) { private void fire(int eventCode) {
for (Listener f : listeners) { for (Listener f : listeners) {
f.on(eventCode); f.on(eventCode);
} }
@ -311,4 +316,15 @@ public class World implements Runnable {
this.selected = selected; this.selected = selected;
} }
public boolean isMultithreading() {
return multithreading;
}
public void setMultithreading(boolean multithreading) {
this.multithreading = multithreading;
}
public void launchNewGen() {
cmdLaunchNewGen = true;
}
} }

View File

@ -11,10 +11,7 @@ import com.mygdx.game.Game;
import com.mygdx.game.Listener; import com.mygdx.game.Listener;
import com.mygdx.game.Log; import com.mygdx.game.Log;
import com.mygdx.game.Log.LogListener; import com.mygdx.game.Log.LogListener;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane; import javax.swing.JOptionPane;
import logic.World;
/** /**
* *