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

fixed bug with reset defaults

This commit is contained in:
Enrico Fasoli 2015-08-07 19:41:26 +02:00
parent 8bc6b26c84
commit 9892070f0c
4 changed files with 44 additions and 31 deletions

View File

@ -99,11 +99,12 @@ public class Game extends ApplicationAdapter {
} }
public Game() { public Game() {
this(null); this(new World(null));
} }
public Game(Map<String, Float> options) { public Game(World world){
world = new World(options); this.world = world;
world.start();
} }
@Override @Override

View File

@ -15,7 +15,7 @@ import logic.neural.Brain;
public class Creature extends Element implements Runnable { public class Creature extends Element implements Runnable {
public static int default_radius = 20, max_hp = 100; 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; public static boolean leaveCorpses = false;
private Brain brain; private Brain brain;
@ -258,8 +258,8 @@ public class Creature extends Element implements Runnable {
if (e.getSize() == 0) { if (e.getSize() == 0) {
e.setSize(0); e.setSize(0);
} }
hp++; hp += hpForEatingPlants;
fitness++; fitness+=pointsForEatingPlants;
if (hp > max_hp) { if (hp > max_hp) {
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) { if (beak > beak / 2 && tempDist < beak * 1.5f && tempAngle < fov / 2) {
// Attacking! // Attacking!
float damage = beak; float damage = beak;
hp += damage / 2; hp += damage * hpForAttacking / 2;
fitness += 2; fitness += pointsForAttacking;
if (hp > max_hp) { if (hp > max_hp) {
hp = max_hp; hp = max_hp;
} }

View File

@ -66,6 +66,9 @@ public class World implements Runnable {
return (int) (t1.getFitness() - t.getFitness()); return (int) (t1.getFitness() - t.getFitness());
} }
}; };
}
public void start(){
newGen(true); 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.fov = Math.round(options.getOrDefault("creature_fov", (float) Math.PI / 2.5f));
Creature.sightRange = Math.round(options.getOrDefault("creature_sight_range", 100f)); Creature.sightRange = Math.round(options.getOrDefault("creature_sight_range", 100f));
Creature.hpDecay = options.getOrDefault("creature_hp_decay", 0.5f); 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); nMutatedBrains = options.getOrDefault("nMutatedBrains", 0.2f);
nMutatedNeurons = options.getOrDefault("nMutatedNeurons", 0.5f); nMutatedNeurons = options.getOrDefault("nMutatedNeurons", 0.5f);
nMutatedConnections = options.getOrDefault("nMutatedConnections", 0.5f); nMutatedConnections = options.getOrDefault("nMutatedConnections", 0.5f);

View File

@ -27,6 +27,7 @@ import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener; import javax.swing.event.TableModelListener;
import javax.swing.table.DefaultTableModel; import javax.swing.table.DefaultTableModel;
import logic.Creature; import logic.Creature;
import logic.World;
/** /**
* *
@ -35,6 +36,7 @@ import logic.Creature;
public class GUI extends javax.swing.JFrame implements LogListener, Listener { public class GUI extends javax.swing.JFrame implements LogListener, Listener {
private Game game; private Game game;
private World world;
private LwjglApplication app; private LwjglApplication app;
private boolean shouldUpdateGUI = false; private boolean shouldUpdateGUI = false;
private final Thread guiUpdater; private final Thread guiUpdater;
@ -50,12 +52,15 @@ public class GUI extends javax.swing.JFrame implements LogListener, Listener {
setLocationRelativeTo(null); // Center the window setLocationRelativeTo(null); // Center the window
Log.addListener(this); Log.addListener(this);
options = new HashMap<String, Float>(); options = new HashMap<String, Float>();
world = new World(options);
updateSettings(); updateSettings();
settingsTable.getModel().addTableModelListener(new TableModelListener() { settingsTable.getModel().addTableModelListener(new TableModelListener() {
@Override @Override
public void tableChanged(TableModelEvent e) { public void tableChanged(TableModelEvent e) {
if(updatingTable) return; if (updatingTable) {
return;
}
saveTableChanges(); saveTableChanges();
updateSettingsUI(); 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) { if (JOptionPane.showConfirmDialog(this, "Are you sure? The simulation will be restarted!") != JOptionPane.YES_OPTION) {
return; return;
} }
game.getWorld().restart(); world.restart();
game.setPaused(false); game.setPaused(false);
} else { } else {
// Start new // Start new
@ -856,10 +861,10 @@ public class GUI extends javax.swing.JFrame implements LogListener, Listener {
config.resizable = true; config.resizable = true;
config.title = "AIrium Renderer"; config.title = "AIrium Renderer";
config.allowSoftwareMode = true; config.allowSoftwareMode = true;
app = new LwjglApplication(game = new Game(options), config); app = new LwjglApplication(game = new Game(world), config);
startButton.setText("Restart"); startButton.setText("Restart");
pauseButton.setEnabled(true); pauseButton.setEnabled(true);
game.getWorld().addListener(this); world.addListener(this);
setCreatureList(); setCreatureList();
} }
updateGUI(); updateGUI();
@ -893,7 +898,7 @@ public class GUI extends javax.swing.JFrame implements LogListener, Listener {
return; return;
} }
enableControlButtons(true); enableControlButtons(true);
status.setText("Generation: " + game.getWorld().getGeneration() + " FPS: " + game.getWorld().getFps()); status.setText("Generation: " + world.getGeneration() + " FPS: " + world.getFps());
if (game.isPaused()) { if (game.isPaused()) {
pauseButton.setSelected(true); pauseButton.setSelected(true);
pauseMenuButton.setText("Resume"); pauseMenuButton.setText("Resume");
@ -907,16 +912,16 @@ public class GUI extends javax.swing.JFrame implements LogListener, Listener {
} }
private void setCreatureList() { private void setCreatureList() {
String list[] = new String[game.getWorld().getCreatures().size()]; String list[] = new String[world.getCreatures().size()];
int selected = -1; int selected = -1;
for (int i = 0; i < list.length; i++) { for (int i = 0; i < list.length; i++) {
if (i >= game.getWorld().getCreatures().size()) { if (i >= world.getCreatures().size()) {
return; return;
} }
list[i] = game.getWorld().getCreatures().get(i).getBrain().getName() list[i] = world.getCreatures().get(i).getBrain().getName()
+ " - Fitness: " + " - Fitness: "
+ game.getWorld().getCreatures().get(i).getFitness(); + world.getCreatures().get(i).getFitness();
if (game.getWorld().getCreatures().get(i) == game.getWorld().getSelectedCreature()) { if (world.getCreatures().get(i) == world.getSelectedCreature()) {
selected = i; selected = i;
} }
} }
@ -929,6 +934,7 @@ public class GUI extends javax.swing.JFrame implements LogListener, Listener {
} }
private void resetDefaultSettings() { private void resetDefaultSettings() {
updatingSliders = true;
fpsLimitSlider.setValue(60); fpsLimitSlider.setValue(60);
nCreaturesSlider.setValue(25); nCreaturesSlider.setValue(25);
nPlantsSlider.setValue(700); nPlantsSlider.setValue(700);
@ -947,6 +953,7 @@ public class GUI extends javax.swing.JFrame implements LogListener, Listener {
enableCorpsesCheckbox.setSelected(false); enableCorpsesCheckbox.setSelected(false);
drawSightLines.setSelected(false); drawSightLines.setSelected(false);
drawViewCones.setSelected(false); drawViewCones.setSelected(false);
updatingSliders = false;
updateSettings(); updateSettings();
} }
@ -974,6 +981,7 @@ public class GUI extends javax.swing.JFrame implements LogListener, Listener {
options.put("nMutatedNeurons", (float) nMutatedNeuronsSlider.getValue() / 100); options.put("nMutatedNeurons", (float) nMutatedNeuronsSlider.getValue() / 100);
options.put("nMutatedConnections", (float) nMutatedConnectionsSlider.getValue() / 100); options.put("nMutatedConnections", (float) nMutatedConnectionsSlider.getValue() / 100);
options.put("mutationFactor", (float) mutationFactorSlider.getValue() / 100); options.put("mutationFactor", (float) mutationFactorSlider.getValue() / 100);
world.reloadOptions();
updateSettingsTable(); updateSettingsTable();
} }
currentNMutatedNeurons.setText(String.format("%.2f", (float) nMutatedNeuronsSlider.getValue() / 100) + "%"); 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) + "%"); currentNMutatedConnections.setText(String.format("%.2f", (float) nMutatedConnectionsSlider.getValue() / 100) + "%");
currentNPlants.setText(nPlantsSlider.getValue() + ""); currentNPlants.setText(nPlantsSlider.getValue() + "");
currentCorpseDecay.setText(corpseDecaySlider.getValue() / 1000f + ""); currentCorpseDecay.setText(corpseDecaySlider.getValue() / 1000f + "");
if (game != null) {
game.getWorld().reloadOptions();
}
} }
public void setScrollBarToTheBottom() { public void setScrollBarToTheBottom() {
@ -1151,15 +1156,15 @@ public class GUI extends javax.swing.JFrame implements LogListener, Listener {
game.setPaused(true); game.setPaused(true);
} }
updateGUI(); updateGUI();
if (game.getWorld().getSelectedCreature() == null) { if (world.getSelectedCreature() == null) {
JOptionPane.showMessageDialog(this, "Please select a creature first"); JOptionPane.showMessageDialog(this, "Please select a creature first");
return; return;
} }
File f = saveDialog(game.getWorld().getSelectedCreature().getBrain().getName() + ".brain"); File f = saveDialog(world.getSelectedCreature().getBrain().getName() + ".brain");
if (f == null) { if (f == null) {
return; return;
} }
Serializer.saveToFile(f, Serializer.serializeBrain(game.getWorld().getSelectedCreature().getBrain().getMap())); Serializer.saveToFile(f, Serializer.serializeBrain(world.getSelectedCreature().getBrain().getMap()));
JOptionPane.showMessageDialog(this, "Done"); JOptionPane.showMessageDialog(this, "Done");
}//GEN-LAST:event_saveBrainBtnActionPerformed }//GEN-LAST:event_saveBrainBtnActionPerformed
@ -1173,8 +1178,8 @@ public class GUI extends javax.swing.JFrame implements LogListener, Listener {
return; return;
} }
float map[][][] = Serializer.loadBrain(Serializer.loadFromFile(f)); float map[][][] = Serializer.loadBrain(Serializer.loadFromFile(f));
Creature c = (Creature) game.getWorld().spawnCreature(map); Creature c = (Creature) world.spawnCreature(map);
game.getWorld().selectCreature(c); world.selectCreature(c);
updateGUI(); updateGUI();
}//GEN-LAST:event_loadBrainBtnActionPerformed }//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))); options.putAll(Serializer.readSettings(Serializer.loadFromFile(f)));
updateSettingsUI(); updateSettingsUI();
if (game != null && game.getWorld() != null) { if (game != null && world != null) {
game.getWorld().reloadOptions(); world.reloadOptions();
} }
//JOptionPane.showMessageDialog(this, "Done"); //JOptionPane.showMessageDialog(this, "Done");
}//GEN-LAST:event_loadSettingsBtnActionPerformed }//GEN-LAST:event_loadSettingsBtnActionPerformed
private void clearSelectedCreatureBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_clearSelectedCreatureBtnActionPerformed private void clearSelectedCreatureBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_clearSelectedCreatureBtnActionPerformed
if (game != null && game.getWorld() != null) { if (game != null && world != null) {
game.getWorld().selectCreature(null); world.selectCreature(null);
creatureList.clearSelection(); creatureList.clearSelection();
} }
}//GEN-LAST:event_clearSelectedCreatureBtnActionPerformed }//GEN-LAST:event_clearSelectedCreatureBtnActionPerformed