mirror of
https://github.com/fazo96/AIrium.git
synced 2025-01-09 09:29:53 +01:00
fixed settings loading
This commit is contained in:
parent
28029c2a66
commit
9cbbc7c9b2
@ -22,13 +22,19 @@ public class Log {
|
||||
|
||||
public static void log(int level, String msg) {
|
||||
if (level <= logLevel) {
|
||||
String s = "";
|
||||
if (logListeners == null) {
|
||||
logListeners = new ArrayList<LogListener>();
|
||||
}
|
||||
for (LogListener l : logListeners) {
|
||||
l.onLog(level, msg);
|
||||
if (level == 0) {
|
||||
s = "[ERROR] ";
|
||||
} else if (level == 2) {
|
||||
s = "[DEBUG] ";
|
||||
}
|
||||
System.out.println(msg);
|
||||
for (LogListener l : logListeners) {
|
||||
l.onLog(level, s + msg);
|
||||
}
|
||||
System.out.println(s + msg);
|
||||
}
|
||||
}
|
||||
|
||||
@ -41,6 +47,7 @@ public class Log {
|
||||
}
|
||||
|
||||
public interface LogListener {
|
||||
|
||||
public abstract void onLog(int level, String msg);
|
||||
}
|
||||
|
||||
|
@ -2,14 +2,11 @@ package com.mygdx.game;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileReader;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
/**
|
||||
* Hangles File I/O for AIrium components.
|
||||
@ -46,16 +43,19 @@ public class Serializer {
|
||||
|
||||
public static String serializeSettings(Map<String, Float> options) {
|
||||
String a = "# Settings file for use with AIrium.\n"
|
||||
+ "# More information at http://github.com/fazo96/AIrium";
|
||||
+ "# More information at http://github.com/fazo96/AIrium\n";
|
||||
for (Object o : options.entrySet().toArray()) {
|
||||
Map.Entry<String, Float> e = (Map.Entry<String, Float>) o;
|
||||
a += e.getKey() + " = " + e.getValue() + "\n";
|
||||
if (!e.getKey().equals("fps_limit")) { // dont save this one
|
||||
a += e.getKey() + " = " + e.getValue() + "\n";
|
||||
}
|
||||
}
|
||||
return a;
|
||||
}
|
||||
|
||||
public static void readSettings(String fileContent, Map<String, Float> m) {
|
||||
public static Map<String,Float> readSettings(String fileContent) {
|
||||
int line = 0;
|
||||
Map<String, Float> m = new HashMap<String, Float>();
|
||||
for (String s : fileContent.split("\n")) {
|
||||
line++;
|
||||
if (s.startsWith("#")) {
|
||||
@ -67,12 +67,13 @@ public class Serializer {
|
||||
if (ss.length != 2) {
|
||||
throw new Exception("Invalid string at line " + line);
|
||||
}
|
||||
Log.log(Log.DEBUG,"Loading setting \""+ss[0].trim()+"\" with value \""+Float.parseFloat(ss[1].trim())+"\"");
|
||||
Log.log(Log.DEBUG, "Loading setting \"" + ss[0].trim() + "\" with value \"" + Float.parseFloat(ss[1].trim()) + "\"");
|
||||
m.put(ss[0].trim(), Float.parseFloat(ss[1].trim()));
|
||||
} catch (Exception e) {
|
||||
Log.log(Log.ERROR, e.getMessage());
|
||||
}
|
||||
}
|
||||
return m;
|
||||
}
|
||||
|
||||
public static String serializeBrain(float brainMap[][][]) {
|
||||
|
@ -343,6 +343,7 @@ public class World implements Runnable {
|
||||
* codes.
|
||||
*/
|
||||
public void fire(int eventCode) {
|
||||
Log.log(Log.DEBUG, "Firing Event. Code: " + eventCode);
|
||||
for (Listener f : listeners) {
|
||||
f.on(eventCode);
|
||||
}
|
||||
@ -432,6 +433,11 @@ public class World implements Runnable {
|
||||
this.multithreading = multithreading;
|
||||
}
|
||||
|
||||
public void replaceOptions(Map<String, Float> options) {
|
||||
this.options = options;
|
||||
reloadOptions();
|
||||
}
|
||||
|
||||
public void restart() {
|
||||
cmdRestart = true;
|
||||
}
|
||||
|
@ -36,6 +36,7 @@ public class GUI extends javax.swing.JFrame implements LogListener, Listener {
|
||||
private boolean shouldUpdateGUI = false;
|
||||
private final Thread guiUpdater;
|
||||
private Map<String, Float> options;
|
||||
private boolean updatingSliders = false;
|
||||
|
||||
/**
|
||||
* Creates new form GUI
|
||||
@ -876,42 +877,40 @@ public class GUI extends javax.swing.JFrame implements LogListener, Listener {
|
||||
* Adjusts settings using values from the UI
|
||||
*/
|
||||
private void updateSettings() {
|
||||
currentFpsLimit.setText("" + fpsLimitSlider.getValue());
|
||||
if (toggleFPSLimitCheckbox.isSelected()) {
|
||||
options.put("fps_limit", 0f);
|
||||
} else {
|
||||
options.put("fps_limit", (float) fpsLimitSlider.getValue());
|
||||
if (!updatingSliders) {
|
||||
options.put("fps_limit", toggleFPSLimitCheckbox.isSelected() ? 0 : (float) fpsLimitSlider.getValue());
|
||||
options.put("enable_multithreading", multithreadingCheckbox.isSelected() ? 1f : 0f);
|
||||
options.put("number_of_creatures", (float) nCreaturesSlider.getValue());
|
||||
options.put("number_of_plants", (float) nPlantsSlider.getValue());
|
||||
options.put("corpse_decay_rate", corpseDecaySlider.getValue() / 1000f);
|
||||
options.put("enable_corpses", enableCorpsesCheckbox.isSelected() ? 1f : 0f);
|
||||
options.put("world_width", (float) worldSizeSlider.getValue());
|
||||
options.put("world_height", (float) worldSizeSlider.getValue());
|
||||
topSizeSlider.setMaximum(nCreaturesSlider.getValue());
|
||||
options.put("parents_count", (float) topSizeSlider.getValue());
|
||||
options.put("creature_sight_range", (float) sightRangeSlider.getValue());
|
||||
options.put("creature_hp_decay", (float) hpDecaySlider.getValue() / 1000);
|
||||
options.put("max_ticks", (float) maxTicksSlider.getValue());
|
||||
options.put("draw_view_cones", drawViewCones.isSelected() ? 1f : 0);
|
||||
options.put("draw_sight_lines", drawSightLines.isSelected() ? 1f : 0);
|
||||
options.put("nMutatedBrains", (float) nMutatedBrainsSlider.getValue() / 100);
|
||||
options.put("nMutatedNeurons", (float) nMutatedNeuronsSlider.getValue() / 100);
|
||||
options.put("nMutatedConnections", (float) nMutatedConnectionsSlider.getValue() / 100);
|
||||
options.put("mutationFactor", (float) mutationFactorSlider.getValue() / 100);
|
||||
}
|
||||
options.put("enable_multithreading", multithreadingCheckbox.isSelected() ? 1f : 0f);
|
||||
options.put("number_of_creatures", (float) nCreaturesSlider.getValue());
|
||||
options.put("number_of_plants", (float) nPlantsSlider.getValue());
|
||||
options.put("corpse_decay_rate", corpseDecaySlider.getValue() / 1000f);
|
||||
options.put("enable_corpses", enableCorpsesCheckbox.isSelected() ? 1f : 0f);
|
||||
currentNCreatures.setText(nCreaturesSlider.getValue() + "");
|
||||
currentNPlants.setText(nPlantsSlider.getValue() + "");
|
||||
currentCorpseDecay.setText(corpseDecaySlider.getValue() / 1000f + "");
|
||||
options.put("world_width", (float) worldSizeSlider.getValue());
|
||||
options.put("world_height", (float) worldSizeSlider.getValue());
|
||||
currentNMutatedNeurons.setText(String.format("%.2f", (float) nMutatedNeuronsSlider.getValue() / 100) + "%");
|
||||
currentSightRange.setText(sightRangeSlider.getValue() + "");
|
||||
currentNMutatedBrains.setText(String.format("%.2f", (float) nMutatedBrainsSlider.getValue() / 100) + "%");
|
||||
currentWorldSize.setText(worldSizeSlider.getValue() + "");
|
||||
topSizeSlider.setMaximum(nCreaturesSlider.getValue());
|
||||
currentTopSize.setText(topSizeSlider.getValue() + (topSizeSlider.getValue() <= 0 ? " (Auto)" : ""));
|
||||
options.put("parents_count", (float) topSizeSlider.getValue());
|
||||
options.put("creature_sight_range", (float) sightRangeSlider.getValue());
|
||||
options.put("creature_hp_decay", (float) hpDecaySlider.getValue() / 1000);
|
||||
options.put("max_ticks", (float) maxTicksSlider.getValue());
|
||||
options.put("draw_view_cones", drawViewCones.isSelected() ? 1f : 0);
|
||||
options.put("draw_sight_lines", drawSightLines.isSelected() ? 1f : 0);
|
||||
currentMaxTicks.setText(maxTicksSlider.getValue() + "");
|
||||
currentHpDecay.setText(hpDecaySlider.getValue() / 1000f + "");
|
||||
currentSightRange.setText(sightRangeSlider.getValue() + "");
|
||||
options.put("nMutatedBrains", (float) nMutatedBrainsSlider.getValue() / 100);
|
||||
currentNMutatedBrains.setText(String.format("%.2f", (float) nMutatedBrainsSlider.getValue() / 100) + "%");
|
||||
options.put("nMutatedNeurons", (float) nMutatedNeuronsSlider.getValue() / 100);
|
||||
currentNMutatedNeurons.setText(String.format("%.2f", (float) nMutatedNeuronsSlider.getValue() / 100) + "%");
|
||||
options.put("nMutatedConnections", (float) nMutatedConnectionsSlider.getValue() / 100);
|
||||
currentNMutatedConnections.setText(String.format("%.2f", (float) nMutatedConnectionsSlider.getValue() / 100) + "%");
|
||||
options.put("mutationFactor", (float) mutationFactorSlider.getValue() / 100);
|
||||
currentMutationFactor.setText(String.format("%.2f", (float) mutationFactorSlider.getValue() / 100));
|
||||
currentFpsLimit.setText("" + fpsLimitSlider.getValue());
|
||||
currentNCreatures.setText(nCreaturesSlider.getValue() + "");
|
||||
currentNMutatedConnections.setText(String.format("%.2f", (float) nMutatedConnectionsSlider.getValue() / 100) + "%");
|
||||
currentNPlants.setText(nPlantsSlider.getValue() + "");
|
||||
currentCorpseDecay.setText(corpseDecaySlider.getValue() / 1000f + "");
|
||||
if (game != null) {
|
||||
game.getWorld().reloadOptions();
|
||||
}
|
||||
@ -1077,6 +1076,7 @@ public class GUI extends javax.swing.JFrame implements LogListener, Listener {
|
||||
return;
|
||||
}
|
||||
Serializer.saveToFile(f, Serializer.serializeBrain(game.getWorld().getSelectedCreature().getBrain().getMap()));
|
||||
JOptionPane.showMessageDialog(this, "Done");
|
||||
}//GEN-LAST:event_saveBrainBtnActionPerformed
|
||||
|
||||
private void loadBrainBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_loadBrainBtnActionPerformed
|
||||
@ -1085,6 +1085,9 @@ public class GUI extends javax.swing.JFrame implements LogListener, Listener {
|
||||
}
|
||||
updateGUI();
|
||||
File f = loadDialog();
|
||||
if (f == null) {
|
||||
return;
|
||||
}
|
||||
float map[][][] = Serializer.loadBrain(Serializer.loadFromFile(f));
|
||||
Creature c = (Creature) game.getWorld().spawnCreature(map);
|
||||
game.getWorld().selectCreature(c);
|
||||
@ -1102,6 +1105,7 @@ public class GUI extends javax.swing.JFrame implements LogListener, Listener {
|
||||
}
|
||||
String settings = Serializer.serializeSettings(options);
|
||||
Serializer.saveToFile(f, settings);
|
||||
JOptionPane.showMessageDialog(this, "Done");
|
||||
}//GEN-LAST:event_saveSettingsBtnActionPerformed
|
||||
|
||||
private void loadSettingsBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_loadSettingsBtnActionPerformed
|
||||
@ -1113,32 +1117,37 @@ public class GUI extends javax.swing.JFrame implements LogListener, Listener {
|
||||
if (f == null) {
|
||||
return;
|
||||
}
|
||||
Serializer.readSettings(Serializer.loadFromFile(f), options);
|
||||
options.putAll(Serializer.readSettings(Serializer.loadFromFile(f)));
|
||||
/*if (game != null) {
|
||||
game.getWorld().replaceOptions(options);
|
||||
}*/
|
||||
updateSettingsUI();
|
||||
JOptionPane.showMessageDialog(this, "Done");
|
||||
}//GEN-LAST:event_loadSettingsBtnActionPerformed
|
||||
/**
|
||||
* Reads settings and adjusts UI sliders.
|
||||
*/
|
||||
private void updateSettingsUI() {
|
||||
fpsLimitSlider.setValue(options.get("fps_limit").intValue());
|
||||
updatingSliders = true;
|
||||
//fpsLimitSlider.setValue(Math.round(options.get("fps_limit")));
|
||||
multithreadingCheckbox.setSelected(options.get("enable_multithreading") > 0f);
|
||||
nCreaturesSlider.setValue(options.get("number_of_creatures").intValue());
|
||||
nCreaturesSlider.setValue(Math.round(options.get("number_of_creatures")));
|
||||
topSizeSlider.setMaximum(nCreaturesSlider.getValue());
|
||||
nPlantsSlider.setValue(options.get("number_of_plants").intValue());
|
||||
corpseDecaySlider.setValue((int) (options.get("corpse_decay_rate") * 1000));
|
||||
topSizeSlider.setValue(Math.round(options.get("parents_count")));
|
||||
nPlantsSlider.setValue(Math.round(options.get("number_of_plants")));
|
||||
corpseDecaySlider.setValue(Math.round(options.get("corpse_decay_rate") * 1000));
|
||||
enableCorpsesCheckbox.setSelected(options.get("enable_corpses") > 0f);
|
||||
worldSizeSlider.setValue(options.get("world_height").intValue());
|
||||
topSizeSlider.setValue(options.get("parents_count").intValue());
|
||||
sightRangeSlider.setValue(options.get("creature_sight_range").intValue());
|
||||
hpDecaySlider.setValue((int) (options.get("creature_hp_decay") * 1000));
|
||||
maxTicksSlider.setValue(options.get("max_ticks").intValue());
|
||||
worldSizeSlider.setValue(Math.round(options.get("world_height")));
|
||||
sightRangeSlider.setValue(Math.round(options.get("creature_sight_range")));
|
||||
hpDecaySlider.setValue(Math.round(options.get("creature_hp_decay") * 1000));
|
||||
maxTicksSlider.setValue(Math.round(options.get("max_ticks")));
|
||||
drawViewCones.setSelected(options.get("draw_view_cones") > 0f);
|
||||
drawSightLines.setSelected(options.get("draw_sight_lines") > 0f);
|
||||
nMutatedBrainsSlider.setValue((int) (options.get("nMutatedBrains").intValue() * 100));
|
||||
nMutatedNeuronsSlider.setValue((int) (options.get("nMutatedNeurons").intValue() * 100));
|
||||
nMutatedConnectionsSlider.setValue((int) (options.get("nMutatedConnections").intValue() * 100));
|
||||
mutationFactorSlider.setValue((int) (options.get("mutationFactor").intValue() * 100));
|
||||
updateSettings();
|
||||
nMutatedBrainsSlider.setValue(Math.round(options.get("nMutatedBrains") * 100));
|
||||
nMutatedNeuronsSlider.setValue(Math.round(options.get("nMutatedNeurons") * 100));
|
||||
nMutatedConnectionsSlider.setValue(Math.round(options.get("nMutatedConnections") * 100));
|
||||
mutationFactorSlider.setValue(Math.round(options.get("mutationFactor") * 100));
|
||||
updatingSliders = false;
|
||||
}
|
||||
|
||||
// Variables declaration - do not modify//GEN-BEGIN:variables
|
||||
|
Loading…
Reference in New Issue
Block a user