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

Fixed a few bugs

This commit is contained in:
Enrico Fasoli 2015-07-07 14:38:00 +02:00
parent fcd1e540e6
commit b447353891
4 changed files with 109 additions and 20 deletions

View File

@ -164,18 +164,20 @@ public class Creature extends Element implements Runnable {
double relX = Math.cos(dir), relY = Math.sin(dir); double relX = Math.cos(dir), relY = Math.sin(dir);
float c = 0; float c = 0;
float eyeX = (float) (relX * getSize() * 0.6f), eyeY = (float) (relY * getSize() * 0.6f); float eyeX = (float) (relX * getSize() * 0.6f), eyeY = (float) (relY * getSize() * 0.6f);
for (Sight sight : sights) { if (Game.get().getWorld().getOptions().getOrDefault("draw_sight_lines", 0f) > 0) {
if (sight != null) { for (Sight sight : sights) {
c = sight.getDistance() / sightRange * 2 + sightRange; if (sight != null) {
} else { c = sight.getDistance() / sightRange * 2 + sightRange;
} } else {
if (sight != null) { }
if (sight.getElement() instanceof Creature) { if (sight != null) {
s.setColor(c, 0, 0, 1); if (sight.getElement() instanceof Creature) {
} else if (sight.getElement() instanceof Vegetable) { s.setColor(c, 0, 0, 1);
s.setColor(0, c, 0, 1); } else if (sight.getElement() instanceof Vegetable) {
s.setColor(0, c, 0, 1);
}
s.line(eyeX + getX(), getY() + eyeY, sight.getElement().getX(), sight.getElement().getY());
} }
s.line(eyeX + getX(), getY() + eyeY, sight.getElement().getX(), sight.getElement().getY());
} }
} }
if (sights[0] == null && sights[1] == null) { if (sights[0] == null && sights[1] == null) {
@ -187,8 +189,10 @@ public class Creature extends Element implements Runnable {
//FOV //FOV
float degrees = fov * 360f / (float) Math.PI; float degrees = fov * 360f / (float) Math.PI;
float orient = dir * 180f / (float) Math.PI - degrees / 2; float orient = dir * 180f / (float) Math.PI - degrees / 2;
s.setColor(0.3f, 0.3f, 0.3f, 1); if (Game.get().getWorld().getOptions().getOrDefault("draw_view_cones", 0f) > 0) {
s.arc((float) eyeX + getX(), (float) eyeY + getY(), sightRange, orient, degrees); s.setColor(0.3f, 0.3f, 0.3f, 1);
s.arc((float) eyeX + getX(), (float) eyeY + getY(), sightRange, orient, degrees);
}
if (hp < prevHp) { if (hp < prevHp) {
// Damage mark // Damage mark
s.set(ShapeRenderer.ShapeType.Filled); s.set(ShapeRenderer.ShapeType.Filled);
@ -274,14 +278,16 @@ public class Creature extends Element implements Runnable {
// Check if attackable // Check if attackable
if (beak > beak / 2 && tempDist < beak * 1.5f && tempAngle < fov / 2) { if (beak > beak / 2 && tempDist < beak * 1.5f && tempAngle < fov / 2) {
// Attacking! // Attacking!
hp++; float damage = beak;
fitness++; hp += damage / 2;
fitness += 2;
if (hp > max_hp) { if (hp > max_hp) {
hp = max_hp; hp = max_hp;
} }
killing = true; killing = true;
Creature c = (Creature) e; Creature c = (Creature) e;
c.setHp(c.getHp() - 0.2f); c.heal(-damage);
//c.praise(-1);
} }
} }
//Log.log(Log.DEBUG,"RelAngle "+relAngle+" Dir "+ndir); //Log.log(Log.DEBUG,"RelAngle "+relAngle+" Dir "+ndir);
@ -311,6 +317,14 @@ public class Creature extends Element implements Runnable {
} }
} }
private void heal(float amount) {
hp += amount;
}
private void praise(float amount) {
fitness += amount;
}
/** /**
* Check if the Worker thread has finished its current iteration * Check if the Worker thread has finished its current iteration
* *

View File

@ -376,6 +376,10 @@ public class World implements Runnable {
return fps; return fps;
} }
public Map<String, Float> getOptions() {
return options;
}
public Creature getSelectedCreature() { public Creature getSelectedCreature() {
return selected; return selected;
} }

View File

@ -269,9 +269,15 @@
<Layout> <Layout>
<DimensionLayout dim="0"> <DimensionLayout dim="0">
<Group type="103" groupAlignment="0" attributes="0"> <Group type="103" groupAlignment="0" attributes="0">
<Group type="102" attributes="0"> <Group type="102" alignment="0" attributes="0">
<EmptySpace max="-2" attributes="0"/> <EmptySpace max="-2" attributes="0"/>
<Group type="103" groupAlignment="0" attributes="0"> <Group type="103" groupAlignment="0" attributes="0">
<Group type="102" attributes="0">
<Component id="drawViewCones" min="-2" max="-2" attributes="0"/>
<EmptySpace max="-2" attributes="0"/>
<Component id="drawSightLines" min="-2" max="-2" attributes="0"/>
<EmptySpace min="0" pref="0" max="32767" attributes="0"/>
</Group>
<Group type="102" alignment="0" attributes="0"> <Group type="102" alignment="0" attributes="0">
<Component id="jLabel3" min="-2" max="-2" attributes="0"/> <Component id="jLabel3" min="-2" max="-2" attributes="0"/>
<EmptySpace max="-2" attributes="0"/> <EmptySpace max="-2" attributes="0"/>
@ -429,7 +435,12 @@
<Component id="maxTicksSlider" min="-2" max="-2" attributes="0"/> <Component id="maxTicksSlider" min="-2" max="-2" attributes="0"/>
<Component id="currentMaxTicks" min="-2" pref="26" max="-2" attributes="0"/> <Component id="currentMaxTicks" min="-2" pref="26" max="-2" attributes="0"/>
</Group> </Group>
<EmptySpace pref="44" max="32767" attributes="0"/> <EmptySpace max="-2" attributes="0"/>
<Group type="103" groupAlignment="3" attributes="0">
<Component id="drawViewCones" alignment="3" min="-2" max="-2" attributes="0"/>
<Component id="drawSightLines" alignment="3" min="-2" max="-2" attributes="0"/>
</Group>
<EmptySpace pref="19" max="32767" attributes="0"/>
<Group type="103" groupAlignment="3" attributes="0"> <Group type="103" groupAlignment="3" attributes="0">
<Component id="pauseButton" alignment="3" min="-2" max="-2" attributes="0"/> <Component id="pauseButton" alignment="3" min="-2" max="-2" attributes="0"/>
<Component id="jButton1" alignment="3" min="-2" max="-2" attributes="0"/> <Component id="jButton1" alignment="3" min="-2" max="-2" attributes="0"/>
@ -600,6 +611,7 @@
<Properties> <Properties>
<Property name="maximum" type="int" value="1000"/> <Property name="maximum" type="int" value="1000"/>
<Property name="minimum" type="int" value="20"/> <Property name="minimum" type="int" value="20"/>
<Property name="value" type="int" value="100"/>
</Properties> </Properties>
<Events> <Events>
<EventHandler event="stateChanged" listener="javax.swing.event.ChangeListener" parameters="javax.swing.event.ChangeEvent" handler="sightRangeSliderStateChanged"/> <EventHandler event="stateChanged" listener="javax.swing.event.ChangeListener" parameters="javax.swing.event.ChangeEvent" handler="sightRangeSliderStateChanged"/>
@ -656,6 +668,22 @@
<EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="jButton1ActionPerformed"/> <EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="jButton1ActionPerformed"/>
</Events> </Events>
</Component> </Component>
<Component class="javax.swing.JCheckBox" name="drawViewCones">
<Properties>
<Property name="text" type="java.lang.String" value="Draw view cones"/>
</Properties>
<Events>
<EventHandler event="stateChanged" listener="javax.swing.event.ChangeListener" parameters="javax.swing.event.ChangeEvent" handler="drawViewConesStateChanged"/>
</Events>
</Component>
<Component class="javax.swing.JCheckBox" name="drawSightLines">
<Properties>
<Property name="text" type="java.lang.String" value="Draw sight lines"/>
</Properties>
<Events>
<EventHandler event="stateChanged" listener="javax.swing.event.ChangeListener" parameters="javax.swing.event.ChangeEvent" handler="drawSightLinesStateChanged"/>
</Events>
</Component>
</SubComponents> </SubComponents>
</Container> </Container>
</SubComponents> </SubComponents>

View File

@ -115,6 +115,8 @@ public class GUI extends javax.swing.JFrame implements LogListener, Listener {
currentHpDecay = new javax.swing.JLabel(); currentHpDecay = new javax.swing.JLabel();
currentMaxTicks = new javax.swing.JLabel(); currentMaxTicks = new javax.swing.JLabel();
jButton1 = new javax.swing.JButton(); jButton1 = new javax.swing.JButton();
drawViewCones = new javax.swing.JCheckBox();
drawSightLines = new javax.swing.JCheckBox();
status = new javax.swing.JLabel(); status = new javax.swing.JLabel();
menuBar = new javax.swing.JMenuBar(); menuBar = new javax.swing.JMenuBar();
jMenu1 = new javax.swing.JMenu(); jMenu1 = new javax.swing.JMenu();
@ -298,6 +300,7 @@ public class GUI extends javax.swing.JFrame implements LogListener, Listener {
sightRangeSlider.setMaximum(1000); sightRangeSlider.setMaximum(1000);
sightRangeSlider.setMinimum(20); sightRangeSlider.setMinimum(20);
sightRangeSlider.setValue(100);
sightRangeSlider.addChangeListener(new javax.swing.event.ChangeListener() { sightRangeSlider.addChangeListener(new javax.swing.event.ChangeListener() {
public void stateChanged(javax.swing.event.ChangeEvent evt) { public void stateChanged(javax.swing.event.ChangeEvent evt) {
sightRangeSliderStateChanged(evt); sightRangeSliderStateChanged(evt);
@ -337,6 +340,20 @@ public class GUI extends javax.swing.JFrame implements LogListener, Listener {
} }
}); });
drawViewCones.setText("Draw view cones");
drawViewCones.addChangeListener(new javax.swing.event.ChangeListener() {
public void stateChanged(javax.swing.event.ChangeEvent evt) {
drawViewConesStateChanged(evt);
}
});
drawSightLines.setText("Draw sight lines");
drawSightLines.addChangeListener(new javax.swing.event.ChangeListener() {
public void stateChanged(javax.swing.event.ChangeEvent evt) {
drawSightLinesStateChanged(evt);
}
});
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout); jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup( jPanel1Layout.setHorizontalGroup(
@ -344,6 +361,11 @@ public class GUI extends javax.swing.JFrame implements LogListener, Listener {
.addGroup(jPanel1Layout.createSequentialGroup() .addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap() .addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(drawViewCones)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(drawSightLines)
.addGap(0, 0, Short.MAX_VALUE))
.addGroup(jPanel1Layout.createSequentialGroup() .addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jLabel3) .addComponent(jLabel3)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
@ -470,7 +492,11 @@ public class GUI extends javax.swing.JFrame implements LogListener, Listener {
.addComponent(jLabel10, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel10, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(maxTicksSlider, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(maxTicksSlider, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(currentMaxTicks, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)) .addComponent(currentMaxTicks, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 44, Short.MAX_VALUE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(drawViewCones)
.addComponent(drawSightLines))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 19, Short.MAX_VALUE)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(pauseButton) .addComponent(pauseButton)
.addComponent(jButton1)) .addComponent(jButton1))
@ -648,9 +674,14 @@ public class GUI extends javax.swing.JFrame implements LogListener, Listener {
worldSizeSlider.setValue(2000); worldSizeSlider.setValue(2000);
corpseDecaySlider.setValue(0); corpseDecaySlider.setValue(0);
topSizeSlider.setValue(0); topSizeSlider.setValue(0);
sightRangeSlider.setValue(50); sightRangeSlider.setValue(100);
hpDecaySlider.setValue(500); hpDecaySlider.setValue(500);
maxTicksSlider.setValue(0); maxTicksSlider.setValue(0);
toggleFPSLimitCheckbox.setSelected(false);
multithreadingCheckbox.setSelected(true);
enableCorpsesCheckbox.setSelected(false);
drawSightLines.setSelected(false);
drawViewCones.setSelected(false);
updateSettings(); updateSettings();
} }
@ -678,6 +709,8 @@ public class GUI extends javax.swing.JFrame implements LogListener, Listener {
options.put("creature_sight_range", (float) sightRangeSlider.getValue()); options.put("creature_sight_range", (float) sightRangeSlider.getValue());
options.put("creature_hp_decay", (float) hpDecaySlider.getValue() / 1000); options.put("creature_hp_decay", (float) hpDecaySlider.getValue() / 1000);
options.put("max_ticks", (float) maxTicksSlider.getValue()); 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() + ""); currentMaxTicks.setText(maxTicksSlider.getValue() + "");
currentHpDecay.setText(hpDecaySlider.getValue() / 1000f + ""); currentHpDecay.setText(hpDecaySlider.getValue() / 1000f + "");
currentSightRange.setText(sightRangeSlider.getValue() + ""); currentSightRange.setText(sightRangeSlider.getValue() + "");
@ -790,6 +823,14 @@ public class GUI extends javax.swing.JFrame implements LogListener, Listener {
resetDefaultSettings(); resetDefaultSettings();
}//GEN-LAST:event_jButton1ActionPerformed }//GEN-LAST:event_jButton1ActionPerformed
private void drawViewConesStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_drawViewConesStateChanged
updateSettings();
}//GEN-LAST:event_drawViewConesStateChanged
private void drawSightLinesStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_drawSightLinesStateChanged
updateSettings();
}//GEN-LAST:event_drawSightLinesStateChanged
// Variables declaration - do not modify//GEN-BEGIN:variables // Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JPanel container; private javax.swing.JPanel container;
private javax.swing.JSlider corpseDecaySlider; private javax.swing.JSlider corpseDecaySlider;
@ -803,6 +844,8 @@ public class GUI extends javax.swing.JFrame implements LogListener, Listener {
private javax.swing.JLabel currentSightRange; private javax.swing.JLabel currentSightRange;
private javax.swing.JLabel currentTopSize; private javax.swing.JLabel currentTopSize;
private javax.swing.JLabel currentWorldSize; private javax.swing.JLabel currentWorldSize;
private javax.swing.JCheckBox drawSightLines;
private javax.swing.JCheckBox drawViewCones;
private javax.swing.JCheckBox enableCorpsesCheckbox; private javax.swing.JCheckBox enableCorpsesCheckbox;
private javax.swing.JMenuItem exitButton; private javax.swing.JMenuItem exitButton;
private javax.swing.JSlider fpsLimitSlider; private javax.swing.JSlider fpsLimitSlider;