From d97995ddded3b4820fd110eda61374b2fec933a2 Mon Sep 17 00:00:00 2001 From: Enrico Fasoli Date: Mon, 6 Jul 2015 21:43:30 +0200 Subject: [PATCH] Starting migration to new config system --- core/src/com/mygdx/game/Game.java | 7 ++++++- core/src/logic/Creature.java | 33 +++++++++++++---------------- core/src/logic/World.java | 35 ++++++++++++++++++++++++------- 3 files changed, 48 insertions(+), 27 deletions(-) diff --git a/core/src/com/mygdx/game/Game.java b/core/src/com/mygdx/game/Game.java index 5466d76..e4702f3 100644 --- a/core/src/com/mygdx/game/Game.java +++ b/core/src/com/mygdx/game/Game.java @@ -7,6 +7,7 @@ import com.badlogic.gdx.graphics.GL20; import com.badlogic.gdx.graphics.g2d.BitmapFont; import com.badlogic.gdx.graphics.glutils.ShapeRenderer; import java.util.ConcurrentModificationException; +import java.util.Map; import logic.Creature; import logic.Element; import logic.World; @@ -35,7 +36,11 @@ public class Game extends ApplicationAdapter { } public Game() { - world = new World(2500, 2500); + this(null); + } + + public Game(Map options) { + world = new World(options); } @Override diff --git a/core/src/logic/Creature.java b/core/src/logic/Creature.java index ec38460..5505a99 100644 --- a/core/src/logic/Creature.java +++ b/core/src/logic/Creature.java @@ -14,11 +14,11 @@ import logic.neural.Brain; */ public class Creature extends Element implements Runnable { - public static final int default_radius = 20, maxHp = 100; - public static final float max_speed = 3, max_beak = default_radius / 4; + public static int default_radius = 20, max_hp = 100; + public static float max_speed = 3, max_beak = default_radius / 4,fov,sightRange; 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 Sight[] sights; private Thread workerThread; @@ -26,12 +26,10 @@ public class Creature extends Element implements Runnable { public Creature(float x, float y) { super(x, y, default_radius); dir = (float) (Math.random() * 2 * Math.PI); - hp = maxHp; + hp = max_hp; prevHp = hp; - speed = 0;//(float) Math.random() * 3; - rotSpeed = 0;//(float) Math.random() - 0.5f; - sightRange = 100; - fov = (float) Math.PI / 2.5f; + speed = 0; + rotSpeed = 0; fitness = 0; brain = new Brain(9, 5, 2, 10); sights = new Sight[2]; @@ -129,13 +127,12 @@ public class Creature extends Element implements Runnable { values[3 + mul] = sights[i].getElement().getSize() / default_radius; } else { 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[8] = eating || killing ? 1 : 0; - System.out.println(values[8]); // compute behavior float[] actions = null; try { @@ -158,7 +155,7 @@ public class Creature extends Element implements Runnable { @Override public void render(ShapeRenderer s) { // 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()); // Vision double relX = Math.cos(dir), relY = Math.sin(dir); @@ -239,8 +236,8 @@ public class Creature extends Element implements Runnable { } hp++; fitness++; - if (hp > maxHp) { - hp = maxHp; + if (hp > max_hp) { + hp = max_hp; } } } @@ -278,8 +275,8 @@ public class Creature extends Element implements Runnable { // Attacking! hp++; fitness++; - if (hp > maxHp) { - hp = maxHp; + if (hp > max_hp) { + hp = max_hp; } killing = true; Creature c = (Creature) e; @@ -303,8 +300,8 @@ public class Creature extends Element implements Runnable { } hp++; fitness++; - if (hp > maxHp) { - hp = maxHp; + if (hp > max_hp) { + hp = max_hp; } } } @@ -344,7 +341,7 @@ public class Creature extends Element implements Runnable { public void reset() { fitness = 0; - hp = maxHp; + hp = max_hp; } public float getBeak() { diff --git a/core/src/logic/World.java b/core/src/logic/World.java index a5f2e85..949268f 100644 --- a/core/src/logic/World.java +++ b/core/src/logic/World.java @@ -12,6 +12,8 @@ import java.util.ArrayList; import java.util.Comparator; import java.util.ConcurrentModificationException; import java.util.Date; +import java.util.HashMap; +import java.util.Map; import java.util.logging.Level; import java.util.logging.Logger; @@ -21,10 +23,11 @@ import java.util.logging.Logger; */ public class World implements Runnable { - private final int width, height, nPlants, creatPerGen; + private int width, height, nPlants, creatPerGen; private int generation = 1; - private boolean multithreading = false, cmdLaunchNewGen = false; - private int fpsLimit = 60, fps = 0; + private boolean multithreading, cmdLaunchNewGen = false; + private int fpsLimit, fps = 0; + private Map options; private Creature selected; private final ArrayList elements; private final ArrayList toAdd; @@ -34,14 +37,16 @@ public class World implements Runnable { private final ArrayList deadPlants; private final ArrayList listeners; - public World(int width, int height) { - this.width = width; - this.height = height; + public World(Map options) { + if (options == null) { + this.options = new HashMap(); + } else { + this.options = options; + } + reloadOptions(); elements = new ArrayList(); creatures = new ArrayList(); toAdd = new ArrayList(); - creatPerGen = Math.min(Math.round(width * height / 20000), 50); - nPlants = Math.round(width * height / 5500); plants = new ArrayList(); deadPlants = 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) { int x, y, r; boolean overlaps = false;