mirror of
https://github.com/fazo96/AIrium.git
synced 2025-01-10 09:34:20 +01:00
reimplemented double vision
This commit is contained in:
parent
5798bce060
commit
b7dd099fdb
@ -20,7 +20,7 @@ public class Creature extends Element {
|
|||||||
private Brain brain;
|
private Brain brain;
|
||||||
private float dir, hp, prevHp, speed, sightRange, fov, fitness, rotSpeed, beak;
|
private float dir, hp, prevHp, speed, sightRange, fov, fitness, rotSpeed, beak;
|
||||||
private boolean eating = false, killing = false;
|
private boolean eating = false, killing = false;
|
||||||
private Sight sight;
|
private Sight[] sights;
|
||||||
|
|
||||||
public Creature(float x, float y) {
|
public Creature(float x, float y) {
|
||||||
super(x, y, default_radius);
|
super(x, y, default_radius);
|
||||||
@ -32,7 +32,7 @@ public class Creature extends Element {
|
|||||||
sightRange = 100;
|
sightRange = 100;
|
||||||
fov = (float) Math.PI / 2.5f;
|
fov = (float) Math.PI / 2.5f;
|
||||||
fitness = 0;
|
fitness = 0;
|
||||||
brain = new Brain(6, 5, 2, 10);
|
brain = new Brain(10, 5, 2, 10);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -78,37 +78,46 @@ public class Creature extends Element {
|
|||||||
dir += 2 * Math.PI;
|
dir += 2 * Math.PI;
|
||||||
}
|
}
|
||||||
// read from sensors and interact with world
|
// read from sensors and interact with world
|
||||||
sight = interactWithWorld();
|
sights = interactWithWorld();
|
||||||
// feed data to brain
|
// feed data to brain
|
||||||
float[] values = new float[brain.getNeurons()[0].length];
|
float[] values = new float[brain.getNeurons()[0].length];
|
||||||
// 0: sight: see food?
|
// VIEW: PLANTS
|
||||||
// 1: sight: see creat?
|
// 0: sight(v): see food?
|
||||||
// 2: sight: distance
|
// 1: sight(v): distance
|
||||||
// 3: sight: angle
|
// 2: sight(v): angle
|
||||||
// 4: sight: size for vegetables, hunger for creatures
|
// 3: sight(v): size
|
||||||
// 5: food sensor
|
// VIEW: CREATS
|
||||||
if (sight == null || sight.getElement() == null) {
|
// 4: sight(c): see food?
|
||||||
// See nothing
|
// 5: sight(c): distance
|
||||||
values[0] = 0;
|
// 6: sight(c): angle
|
||||||
values[1] = 0;
|
// 8: sight(c): hunger
|
||||||
values[2] = 0;
|
// 7: sight(c): beak
|
||||||
values[3] = 0;
|
// OTHER:
|
||||||
values[4] = 0;
|
// 9: food sensor
|
||||||
} else {
|
int viewSensors = 4;
|
||||||
// See something
|
for (int i = 0; i < sights.length; i++) {
|
||||||
values[2] = sight.getDistance() / sightRange;
|
int mul = i * viewSensors;
|
||||||
values[3] = sight.getAngle();
|
if (sights[i] == null || sights[i].getElement() == null) {
|
||||||
if (sight.getElement() instanceof Vegetable) {
|
// See nothing
|
||||||
values[0] = 1f;
|
values[0 + mul] = 0;
|
||||||
values[1] = 0;
|
values[1 + mul] = 0;
|
||||||
values[4] = sight.getElement().getSize() / default_radius;
|
values[2 + mul] = 0;
|
||||||
|
values[3 + mul] = 0;
|
||||||
} else {
|
} else {
|
||||||
values[0] = 0f;
|
// See something
|
||||||
values[1] = 1f;
|
values[1 + mul] = sights[i].getDistance() / sightRange;
|
||||||
values[4] = maxHp - ((Creature) sight.getElement()).getHp() / maxHp;
|
values[2 + mul] = sights[i].getAngle();
|
||||||
|
if (sights[i].getElement() instanceof Vegetable) {
|
||||||
|
values[0 + mul] = 1f;
|
||||||
|
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] = ((Creature) sights[i].getElement()).getBeak() / max_beak;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
values[5] = eating ? 1 : 0;
|
values[9] = eating ? 1 : 0;
|
||||||
// compute behavior
|
// compute behavior
|
||||||
float[] actions = null;
|
float[] actions = null;
|
||||||
try {
|
try {
|
||||||
@ -136,19 +145,25 @@ public class Creature extends Element {
|
|||||||
// Vision
|
// Vision
|
||||||
double relX = Math.cos(dir), relY = Math.sin(dir);
|
double relX = Math.cos(dir), relY = Math.sin(dir);
|
||||||
float c = 0;
|
float c = 0;
|
||||||
if (sight != null) {
|
|
||||||
c = sight.getDistance() / sightRange * 2 + sightRange;
|
|
||||||
} else {
|
|
||||||
s.setColor(1, 1, 1, 1);
|
|
||||||
}
|
|
||||||
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);
|
||||||
if (sight != null) {
|
for (int i = 0; i < sights.length; i++) {
|
||||||
s.line(eyeX + getX(), getY() + eyeY, sight.getElement().getX(), sight.getElement().getY());
|
if (sights[i] != null) {
|
||||||
if (sight.getElement() instanceof Creature) {
|
c = sights[i].getDistance() / sightRange * 2 + sightRange;
|
||||||
s.setColor(c, 0, 0, 1);
|
} else {
|
||||||
} else if (sight.getElement() instanceof Vegetable) {
|
|
||||||
s.setColor(0, c, 0, 1);
|
|
||||||
}
|
}
|
||||||
|
if (sights[i] != null) {
|
||||||
|
if (sights[i].getElement() instanceof Creature) {
|
||||||
|
s.setColor(c, 0, 0, 1);
|
||||||
|
} else if (sights[i].getElement() instanceof Vegetable) {
|
||||||
|
s.setColor(0, c, 0, 1);
|
||||||
|
}
|
||||||
|
s.line(eyeX + getX(), getY() + eyeY, sights[i].getElement().getX(), sights[i].getElement().getY());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (sights[0] == null && sights[1] == null) {
|
||||||
|
s.setColor(1, 1, 1, 1);
|
||||||
|
} else {
|
||||||
|
s.setColor(sights[1] == null ? 0 : 1, sights[0] == null ? 0 : 1, 0, 1);
|
||||||
}
|
}
|
||||||
s.circle(getX() + eyeX, getY() + eyeY, 3);
|
s.circle(getX() + eyeX, getY() + eyeY, 3);
|
||||||
//FOV
|
//FOV
|
||||||
@ -173,12 +188,56 @@ public class Creature extends Element {
|
|||||||
s.line((float) (relX * getSize() * 0.8f + getX()), (float) (relY * getSize() * 0.8f + getY()), (float) (relX * getSize() * (1.5f + beak / max_beak) + getX()), (float) (relY * getSize() * (1.5f + beak / max_beak) + getY()));
|
s.line((float) (relX * getSize() * 0.8f + getX()), (float) (relY * getSize() * 0.8f + getY()), (float) (relX * getSize() * (1.5f + beak / max_beak) + getX()), (float) (relY * getSize() * (1.5f + beak / max_beak) + getY()));
|
||||||
}
|
}
|
||||||
|
|
||||||
public Sight interactWithWorld() {
|
public Sight[] interactWithWorld() {
|
||||||
|
Sight[] newSights = new Sight[2];
|
||||||
|
// Try to see plant
|
||||||
Element seen = null;
|
Element seen = null;
|
||||||
float dist = 0, angle = 0, ndir = dir - (float) Math.PI;
|
float dist = 0, angle = 0, ndir = dir - (float) Math.PI;
|
||||||
killing = false;
|
killing = false;
|
||||||
eating = false;
|
eating = false;
|
||||||
for (Element e : Game.get().getWorld().getElements()) {
|
for (Element e : Game.get().getWorld().getPlants()) {
|
||||||
|
float tempDist = distanceFrom(e);
|
||||||
|
if (tempDist > sightRange) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
//Log.log(Log.DEBUG,"TempDist "+tempDist+" SightRange "+sightRange);
|
||||||
|
float relAngle = (float) (Math.atan2(getY() - e.getY(), getX() - e.getX()));
|
||||||
|
if (tempDist < dist || seen == null) {
|
||||||
|
// Check if Visible
|
||||||
|
if (Math.abs(relAngle - ndir) < fov) {
|
||||||
|
// Visible
|
||||||
|
seen = e;
|
||||||
|
angle = relAngle - ndir;
|
||||||
|
dist = tempDist;
|
||||||
|
}
|
||||||
|
//Log.log(Log.DEBUG,"RelAngle "+relAngle+" Dir "+ndir);
|
||||||
|
}
|
||||||
|
// Check if eatable
|
||||||
|
if (tempDist < 0) {
|
||||||
|
// Eat
|
||||||
|
eating = true;
|
||||||
|
e.setSize(e.getSize() - 0.1f);
|
||||||
|
if (e.getSize() == 0) {
|
||||||
|
e.setSize(0);
|
||||||
|
}
|
||||||
|
hp++;
|
||||||
|
fitness++;
|
||||||
|
if (hp > maxHp) {
|
||||||
|
hp = maxHp;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (seen != null) {
|
||||||
|
newSights[0] = new Sight(seen, dist, angle);
|
||||||
|
}
|
||||||
|
// Try to see creature
|
||||||
|
seen = null;
|
||||||
|
dist = 0;
|
||||||
|
angle = 0;
|
||||||
|
ndir = dir - (float) Math.PI;
|
||||||
|
killing = false;
|
||||||
|
eating = false;
|
||||||
|
for (Element e : Game.get().getWorld().getCreatures()) {
|
||||||
if (e == this) {
|
if (e == this) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@ -198,20 +257,6 @@ public class Creature extends Element {
|
|||||||
}
|
}
|
||||||
//Log.log(Log.DEBUG,"RelAngle "+relAngle+" Dir "+ndir);
|
//Log.log(Log.DEBUG,"RelAngle "+relAngle+" Dir "+ndir);
|
||||||
}
|
}
|
||||||
// Check if eatable
|
|
||||||
if (e instanceof Vegetable && tempDist < 0) {
|
|
||||||
// Eat
|
|
||||||
eating = true;
|
|
||||||
e.setSize(e.getSize() - 0.1f);
|
|
||||||
if (e.getSize() == 0) {
|
|
||||||
e.setSize(0);
|
|
||||||
}
|
|
||||||
hp++;
|
|
||||||
fitness++;
|
|
||||||
if (hp > maxHp) {
|
|
||||||
hp = maxHp;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// Check if attackable
|
// Check if attackable
|
||||||
if (e instanceof Creature && beak > 5 && tempDist < beak * 1.5f && Math.abs(relAngle - ndir) < (float) Math.PI / 10f) {
|
if (e instanceof Creature && beak > 5 && tempDist < beak * 1.5f && Math.abs(relAngle - ndir) < (float) Math.PI / 10f) {
|
||||||
// Attacking!
|
// Attacking!
|
||||||
@ -224,13 +269,11 @@ public class Creature extends Element {
|
|||||||
Creature c = (Creature) e;
|
Creature c = (Creature) e;
|
||||||
c.setHp(c.getHp() - 0.2f);
|
c.setHp(c.getHp() - 0.2f);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
if (seen != null) {
|
if (seen != null) {
|
||||||
return new Sight(seen, dist, angle);
|
newSights[1] = new Sight(seen, dist, angle);
|
||||||
} else {
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
return newSights;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void eat() {
|
public void eat() {
|
||||||
@ -268,6 +311,10 @@ public class Creature extends Element {
|
|||||||
hp = maxHp;
|
hp = maxHp;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public float getBeak() {
|
||||||
|
return beak;
|
||||||
|
}
|
||||||
|
|
||||||
public float getHp() {
|
public float getHp() {
|
||||||
return hp;
|
return hp;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user