1
0
mirror of https://github.com/fazo96/AIrium.git synced 2025-01-26 12:04:19 +01:00

Starting migration to new config system

This commit is contained in:
Enrico Fasoli 2015-07-06 21:43:30 +02:00
parent 61454f7979
commit d97995ddde
3 changed files with 48 additions and 27 deletions

View File

@ -7,6 +7,7 @@ import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.g2d.BitmapFont; import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer; import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import java.util.ConcurrentModificationException; import java.util.ConcurrentModificationException;
import java.util.Map;
import logic.Creature; import logic.Creature;
import logic.Element; import logic.Element;
import logic.World; import logic.World;
@ -35,7 +36,11 @@ public class Game extends ApplicationAdapter {
} }
public Game() { public Game() {
world = new World(2500, 2500); this(null);
}
public Game(Map<String, Float> options) {
world = new World(options);
} }
@Override @Override

View File

@ -14,11 +14,11 @@ import logic.neural.Brain;
*/ */
public class Creature extends Element implements Runnable { public class Creature extends Element implements Runnable {
public static final int default_radius = 20, maxHp = 100; public static int default_radius = 20, max_hp = 100;
public static final float max_speed = 3, max_beak = default_radius / 4; public static float max_speed = 3, max_beak = default_radius / 4,fov,sightRange;
private Brain brain; private Brain brain;
private float dir, hp, prevHp, speed, sightRange, fov, fitness, rotSpeed, beak; private float dir, hp, prevHp, speed, fitness, rotSpeed, beak;
private boolean eating = false, killing = false, workerDone = false; private boolean eating = false, killing = false, workerDone = false;
private Sight[] sights; private Sight[] sights;
private Thread workerThread; private Thread workerThread;
@ -26,12 +26,10 @@ public class Creature extends Element implements Runnable {
public Creature(float x, float y) { public Creature(float x, float y) {
super(x, y, default_radius); super(x, y, default_radius);
dir = (float) (Math.random() * 2 * Math.PI); dir = (float) (Math.random() * 2 * Math.PI);
hp = maxHp; hp = max_hp;
prevHp = hp; prevHp = hp;
speed = 0;//(float) Math.random() * 3; speed = 0;
rotSpeed = 0;//(float) Math.random() - 0.5f; rotSpeed = 0;
sightRange = 100;
fov = (float) Math.PI / 2.5f;
fitness = 0; fitness = 0;
brain = new Brain(9, 5, 2, 10); brain = new Brain(9, 5, 2, 10);
sights = new Sight[2]; sights = new Sight[2];
@ -129,13 +127,12 @@ public class Creature extends Element implements Runnable {
values[3 + mul] = sights[i].getElement().getSize() / default_radius; values[3 + mul] = sights[i].getElement().getSize() / default_radius;
} else { } else {
values[0 + mul] = 1f; values[0 + mul] = 1f;
values[3 + mul] = maxHp - ((Creature) sights[i].getElement()).getHp() / maxHp; values[3 + mul] = max_hp - ((Creature) sights[i].getElement()).getHp() / max_hp;
values[3 + mul] = ((Creature) sights[i].getElement()).getBeak() / max_beak; values[3 + mul] = ((Creature) sights[i].getElement()).getBeak() / max_beak;
} }
} }
} }
values[8] = eating || killing ? 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 {
@ -158,7 +155,7 @@ public class Creature extends Element implements Runnable {
@Override @Override
public void render(ShapeRenderer s) { public void render(ShapeRenderer s) {
// Body // Body
s.setColor(1 - (hp / maxHp), hp / maxHp, 0, 1); s.setColor(1 - (hp / max_hp), hp / max_hp, 0, 1);
s.circle(getX(), getY(), getSize()); s.circle(getX(), getY(), getSize());
// Vision // Vision
double relX = Math.cos(dir), relY = Math.sin(dir); double relX = Math.cos(dir), relY = Math.sin(dir);
@ -239,8 +236,8 @@ public class Creature extends Element implements Runnable {
} }
hp++; hp++;
fitness++; fitness++;
if (hp > maxHp) { if (hp > max_hp) {
hp = maxHp; hp = max_hp;
} }
} }
} }
@ -278,8 +275,8 @@ public class Creature extends Element implements Runnable {
// Attacking! // Attacking!
hp++; hp++;
fitness++; fitness++;
if (hp > maxHp) { if (hp > max_hp) {
hp = maxHp; hp = max_hp;
} }
killing = true; killing = true;
Creature c = (Creature) e; Creature c = (Creature) e;
@ -303,8 +300,8 @@ public class Creature extends Element implements Runnable {
} }
hp++; hp++;
fitness++; fitness++;
if (hp > maxHp) { if (hp > max_hp) {
hp = maxHp; hp = max_hp;
} }
} }
} }
@ -344,7 +341,7 @@ public class Creature extends Element implements Runnable {
public void reset() { public void reset() {
fitness = 0; fitness = 0;
hp = maxHp; hp = max_hp;
} }
public float getBeak() { public float getBeak() {

View File

@ -12,6 +12,8 @@ import java.util.ArrayList;
import java.util.Comparator; import java.util.Comparator;
import java.util.ConcurrentModificationException; import java.util.ConcurrentModificationException;
import java.util.Date; import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
@ -21,10 +23,11 @@ import java.util.logging.Logger;
*/ */
public class World implements Runnable { public class World implements Runnable {
private final int width, height, nPlants, creatPerGen; private int width, height, nPlants, creatPerGen;
private int generation = 1; private int generation = 1;
private boolean multithreading = false, cmdLaunchNewGen = false; private boolean multithreading, cmdLaunchNewGen = false;
private int fpsLimit = 60, fps = 0; private int fpsLimit, fps = 0;
private Map<String, Float> options;
private Creature selected; private Creature selected;
private final ArrayList<Element> elements; private final ArrayList<Element> elements;
private final ArrayList<Element> toAdd; private final ArrayList<Element> toAdd;
@ -34,14 +37,16 @@ public class World implements Runnable {
private final ArrayList<Vegetable> deadPlants; private final ArrayList<Vegetable> deadPlants;
private final ArrayList<Listener> listeners; private final ArrayList<Listener> listeners;
public World(int width, int height) { public World(Map<String, Float> options) {
this.width = width; if (options == null) {
this.height = height; this.options = new HashMap<String, Float>();
} else {
this.options = options;
}
reloadOptions();
elements = new ArrayList(); elements = new ArrayList();
creatures = new ArrayList(); creatures = new ArrayList();
toAdd = new ArrayList(); toAdd = new ArrayList();
creatPerGen = Math.min(Math.round(width * height / 20000), 50);
nPlants = Math.round(width * height / 5500);
plants = new ArrayList(); plants = new ArrayList();
deadPlants = new ArrayList(); deadPlants = new ArrayList();
graveyard = new ArrayList(); graveyard = new ArrayList();
@ -187,6 +192,20 @@ public class World implements Runnable {
} }
} }
public void reloadOptions() {
width = Math.round(options.getOrDefault("world_width", 2000f));
height = Math.round(options.getOrDefault("world_height", 2000f));
fpsLimit = Math.round(options.getOrDefault("fps_limit", 60f));
creatPerGen = Math.round(options.getOrDefault("creatures_per_generation", (float) Math.min(Math.round(width * height / 20000), 50)));
nPlants = Math.round(options.getOrDefault("number_of_plants", width * height / 5500f));
multithreading = options.getOrDefault("enable_multithreading", -1f) > 0;
Creature.default_radius = Math.round(options.getOrDefault("creature_radius", 20f));
Creature.max_hp = Math.round(options.getOrDefault("creature_max_hp", 100f));
Creature.max_speed = Math.round(options.getOrDefault("creature_max_speed", 3f));
Creature.fov = Math.round(options.getOrDefault("creature_fov", (float) Math.PI / 2.5f));
Creature.sightRange = Math.round(options.getOrDefault("creature_sight_range", 100f));
}
private Element spawn(boolean isCreature, float[][][] brainMap) { private Element spawn(boolean isCreature, float[][][] brainMap) {
int x, y, r; int x, y, r;
boolean overlaps = false; boolean overlaps = false;