diff --git a/core/src/com/mygdx/game/Game.java b/core/src/com/mygdx/game/Game.java index 681ae07..b869957 100644 --- a/core/src/com/mygdx/game/Game.java +++ b/core/src/com/mygdx/game/Game.java @@ -99,11 +99,12 @@ public class Game extends ApplicationAdapter { } public Game() { - this(null); + this(new World(null)); } - - public Game(Map options) { - world = new World(options); + + public Game(World world){ + this.world = world; + world.start(); } @Override diff --git a/core/src/logic/Creature.java b/core/src/logic/Creature.java index a5a618e..c31c74a 100644 --- a/core/src/logic/Creature.java +++ b/core/src/logic/Creature.java @@ -15,7 +15,7 @@ import logic.neural.Brain; public class Creature extends Element implements Runnable { public static int default_radius = 20, max_hp = 100; - public static float max_speed = 3, max_beak = default_radius / 4, fov, sightRange, corpseDecayRate = 0, hpDecay = 0.5f; + public static float max_speed = 3, max_beak = default_radius / 4, fov, sightRange, corpseDecayRate = 0, hpDecay = 0.5f, pointsForEatingPlants = 1f, pointsForAttacking = 2f, hpForAttacking = 1f, hpForEatingPlants = 1f; public static boolean leaveCorpses = false; private Brain brain; @@ -258,8 +258,8 @@ public class Creature extends Element implements Runnable { if (e.getSize() == 0) { e.setSize(0); } - hp++; - fitness++; + hp += hpForEatingPlants; + fitness+=pointsForEatingPlants; if (hp > max_hp) { hp = max_hp; } @@ -296,8 +296,8 @@ public class Creature extends Element implements Runnable { if (beak > beak / 2 && tempDist < beak * 1.5f && tempAngle < fov / 2) { // Attacking! float damage = beak; - hp += damage / 2; - fitness += 2; + hp += damage * hpForAttacking / 2; + fitness += pointsForAttacking; if (hp > max_hp) { hp = max_hp; } diff --git a/core/src/logic/World.java b/core/src/logic/World.java index 978af4d..db37246 100644 --- a/core/src/logic/World.java +++ b/core/src/logic/World.java @@ -66,6 +66,9 @@ public class World implements Runnable { return (int) (t1.getFitness() - t.getFitness()); } }; + } + + public void start(){ newGen(true); } @@ -263,6 +266,10 @@ public class World implements Runnable { Creature.fov = Math.round(options.getOrDefault("creature_fov", (float) Math.PI / 2.5f)); Creature.sightRange = Math.round(options.getOrDefault("creature_sight_range", 100f)); Creature.hpDecay = options.getOrDefault("creature_hp_decay", 0.5f); + Creature.hpForAttacking = options.getOrDefault("creature_hp_for_attacking", 1f); + Creature.hpForEatingPlants = options.getOrDefault("creature_hp_for_eating_plants", 1f); + Creature.pointsForAttacking = options.getOrDefault("creature_points_for_attacking", 2f); + Creature.pointsForEatingPlants = options.getOrDefault("creature_points_for_eating_plants", 1f); nMutatedBrains = options.getOrDefault("nMutatedBrains", 0.2f); nMutatedNeurons = options.getOrDefault("nMutatedNeurons", 0.5f); nMutatedConnections = options.getOrDefault("nMutatedConnections", 0.5f); diff --git a/desktop/src/gui/GUI.java b/desktop/src/gui/GUI.java index 0c8d4ae..0da102a 100644 --- a/desktop/src/gui/GUI.java +++ b/desktop/src/gui/GUI.java @@ -27,6 +27,7 @@ import javax.swing.event.TableModelEvent; import javax.swing.event.TableModelListener; import javax.swing.table.DefaultTableModel; import logic.Creature; +import logic.World; /** * @@ -35,6 +36,7 @@ import logic.Creature; public class GUI extends javax.swing.JFrame implements LogListener, Listener { private Game game; + private World world; private LwjglApplication app; private boolean shouldUpdateGUI = false; private final Thread guiUpdater; @@ -50,12 +52,15 @@ public class GUI extends javax.swing.JFrame implements LogListener, Listener { setLocationRelativeTo(null); // Center the window Log.addListener(this); options = new HashMap(); + world = new World(options); updateSettings(); settingsTable.getModel().addTableModelListener(new TableModelListener() { @Override public void tableChanged(TableModelEvent e) { - if(updatingTable) return; + if (updatingTable) { + return; + } saveTableChanges(); updateSettingsUI(); } @@ -845,7 +850,7 @@ public class GUI extends javax.swing.JFrame implements LogListener, Listener { if (JOptionPane.showConfirmDialog(this, "Are you sure? The simulation will be restarted!") != JOptionPane.YES_OPTION) { return; } - game.getWorld().restart(); + world.restart(); game.setPaused(false); } else { // Start new @@ -856,10 +861,10 @@ public class GUI extends javax.swing.JFrame implements LogListener, Listener { config.resizable = true; config.title = "AIrium Renderer"; config.allowSoftwareMode = true; - app = new LwjglApplication(game = new Game(options), config); + app = new LwjglApplication(game = new Game(world), config); startButton.setText("Restart"); pauseButton.setEnabled(true); - game.getWorld().addListener(this); + world.addListener(this); setCreatureList(); } updateGUI(); @@ -893,7 +898,7 @@ public class GUI extends javax.swing.JFrame implements LogListener, Listener { return; } enableControlButtons(true); - status.setText("Generation: " + game.getWorld().getGeneration() + " FPS: " + game.getWorld().getFps()); + status.setText("Generation: " + world.getGeneration() + " FPS: " + world.getFps()); if (game.isPaused()) { pauseButton.setSelected(true); pauseMenuButton.setText("Resume"); @@ -907,16 +912,16 @@ public class GUI extends javax.swing.JFrame implements LogListener, Listener { } private void setCreatureList() { - String list[] = new String[game.getWorld().getCreatures().size()]; + String list[] = new String[world.getCreatures().size()]; int selected = -1; for (int i = 0; i < list.length; i++) { - if (i >= game.getWorld().getCreatures().size()) { + if (i >= world.getCreatures().size()) { return; } - list[i] = game.getWorld().getCreatures().get(i).getBrain().getName() + list[i] = world.getCreatures().get(i).getBrain().getName() + " - Fitness: " - + game.getWorld().getCreatures().get(i).getFitness(); - if (game.getWorld().getCreatures().get(i) == game.getWorld().getSelectedCreature()) { + + world.getCreatures().get(i).getFitness(); + if (world.getCreatures().get(i) == world.getSelectedCreature()) { selected = i; } } @@ -929,6 +934,7 @@ public class GUI extends javax.swing.JFrame implements LogListener, Listener { } private void resetDefaultSettings() { + updatingSliders = true; fpsLimitSlider.setValue(60); nCreaturesSlider.setValue(25); nPlantsSlider.setValue(700); @@ -947,6 +953,7 @@ public class GUI extends javax.swing.JFrame implements LogListener, Listener { enableCorpsesCheckbox.setSelected(false); drawSightLines.setSelected(false); drawViewCones.setSelected(false); + updatingSliders = false; updateSettings(); } @@ -974,6 +981,7 @@ public class GUI extends javax.swing.JFrame implements LogListener, Listener { options.put("nMutatedNeurons", (float) nMutatedNeuronsSlider.getValue() / 100); options.put("nMutatedConnections", (float) nMutatedConnectionsSlider.getValue() / 100); options.put("mutationFactor", (float) mutationFactorSlider.getValue() / 100); + world.reloadOptions(); updateSettingsTable(); } currentNMutatedNeurons.setText(String.format("%.2f", (float) nMutatedNeuronsSlider.getValue() / 100) + "%"); @@ -989,9 +997,6 @@ public class GUI extends javax.swing.JFrame implements LogListener, Listener { currentNMutatedConnections.setText(String.format("%.2f", (float) nMutatedConnectionsSlider.getValue() / 100) + "%"); currentNPlants.setText(nPlantsSlider.getValue() + ""); currentCorpseDecay.setText(corpseDecaySlider.getValue() / 1000f + ""); - if (game != null) { - game.getWorld().reloadOptions(); - } } public void setScrollBarToTheBottom() { @@ -1151,15 +1156,15 @@ public class GUI extends javax.swing.JFrame implements LogListener, Listener { game.setPaused(true); } updateGUI(); - if (game.getWorld().getSelectedCreature() == null) { + if (world.getSelectedCreature() == null) { JOptionPane.showMessageDialog(this, "Please select a creature first"); return; } - File f = saveDialog(game.getWorld().getSelectedCreature().getBrain().getName() + ".brain"); + File f = saveDialog(world.getSelectedCreature().getBrain().getName() + ".brain"); if (f == null) { return; } - Serializer.saveToFile(f, Serializer.serializeBrain(game.getWorld().getSelectedCreature().getBrain().getMap())); + Serializer.saveToFile(f, Serializer.serializeBrain(world.getSelectedCreature().getBrain().getMap())); JOptionPane.showMessageDialog(this, "Done"); }//GEN-LAST:event_saveBrainBtnActionPerformed @@ -1173,8 +1178,8 @@ public class GUI extends javax.swing.JFrame implements LogListener, Listener { return; } float map[][][] = Serializer.loadBrain(Serializer.loadFromFile(f)); - Creature c = (Creature) game.getWorld().spawnCreature(map); - game.getWorld().selectCreature(c); + Creature c = (Creature) world.spawnCreature(map); + world.selectCreature(c); updateGUI(); }//GEN-LAST:event_loadBrainBtnActionPerformed @@ -1203,15 +1208,15 @@ public class GUI extends javax.swing.JFrame implements LogListener, Listener { } options.putAll(Serializer.readSettings(Serializer.loadFromFile(f))); updateSettingsUI(); - if (game != null && game.getWorld() != null) { - game.getWorld().reloadOptions(); + if (game != null && world != null) { + world.reloadOptions(); } //JOptionPane.showMessageDialog(this, "Done"); }//GEN-LAST:event_loadSettingsBtnActionPerformed private void clearSelectedCreatureBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_clearSelectedCreatureBtnActionPerformed - if (game != null && game.getWorld() != null) { - game.getWorld().selectCreature(null); + if (game != null && world != null) { + world.selectCreature(null); creatureList.clearSelection(); } }//GEN-LAST:event_clearSelectedCreatureBtnActionPerformed