User prompt
Fix Bug: 'TypeError: Graphics is not a constructor' in this line: 'self.viewRayLeft = new Graphics();' Line Number: 90
User prompt
Fix Bug: 'TypeError: self.viewRayLeft.lineStyle is not a function' in this line: 'self.viewRayLeft.lineStyle(1, 0xFF0000, 1);' Line Number: 91
User prompt
Fix Bug: 'TypeError: Graphics is not a constructor' in this line: 'self.viewRayLeft = new Graphics();' Line Number: 90
User prompt
Fix Bug: 'TypeError: Graphics is not a constructor' in this line: 'self.viewRayLeft = new Graphics();' Line Number: 90
User prompt
Fix Bug: 'TypeError: self.viewRayLeft.lineStyle is not a function' in this line: 'self.viewRayLeft.lineStyle(1, 0xFF0000, 1);' Line Number: 91
User prompt
Fix Bug: 'TypeError: Graphics is not a constructor' in this line: 'self.viewRayLeft = new Graphics();' Line Number: 90
User prompt
Fix Bug: 'TypeError: Graphics is not a constructor' in this line: 'self.viewRayLeft = new Graphics();' Line Number: 90
User prompt
Fix Bug: 'TypeError: Graphics is not a constructor' in this line: 'self.viewRayLeft = new Graphics();' Line Number: 90
User prompt
Fix Bug: 'TypeError: self.viewRayLeft.lineStyle is not a function' in this line: 'self.viewRayLeft.lineStyle(1, 0xFF0000, 1);' Line Number: 91
User prompt
Fix Bug: 'TypeError: Graphics is not a constructor' in this line: 'self.viewRayLeft = new Graphics();' Line Number: 57
User prompt
Fix Bug: 'TypeError: self.viewRayLeft.lineStyle is not a function' in this line: 'self.viewRayLeft.lineStyle(1, 0xFF0000, 1);' Line Number: 91
User prompt
Fix Bug: 'TypeError: Graphics is not a constructor' in this line: 'self.viewRayLeft = new Graphics();' Line Number: 90
User prompt
Fix Bug: 'TypeError: Graphics is not a constructor' in this line: 'self.viewRayLeft = new Graphics();' Line Number: 90
User prompt
Fix Bug: 'TypeError: self.viewRayLeft.lineStyle is not a function' in this line: 'self.viewRayLeft.lineStyle(1, 0xFF0000, 1);' Line Number: 91
User prompt
Fix Bug: 'TypeError: Graphics is not a constructor' in this line: 'self.viewRayLeft = new Graphics();' Line Number: 90
User prompt
Fix Bug: 'TypeError: Graphics is not a constructor' in this line: 'self.viewRayLeft = new Graphics();' Line Number: 90
User prompt
Fix Bug: 'TypeError: self.viewRayLeft.lineStyle is not a function' in this line: 'self.viewRayLeft.lineStyle(1, 0xFF0000, 1);' Line Number: 91
User prompt
Fix Bug: 'TypeError: self.viewRayLeft.clear is not a function' in this line: 'self.viewRayLeft.clear();' Line Number: 90
User prompt
Fix Bug: 'TypeError: Graphics is not a constructor' in this line: 'self.viewRayLeft = new Graphics();' Line Number: 57
User prompt
Make the field of view tracable. (small rays casting from the character so we can see how big the field is)
User prompt
Make it so the ai has a field of view in front of it, and if there is food in it's view, it goes for that food. Make it so the size and how wide the field of view differes from ai to ai, and can suffer mutations when passed to children.
User prompt
Fix Bug: 'TypeError: nearestFood is null' in this line: 'var dx = nearestFood.x - this.x;' Line Number: 71
User prompt
Fix Bug: 'TypeError: nearestFood is null' in this line: 'var decision = this.neuralNetwork.feedForward([this.food, nearestFood.amount, nearestFoodDistance]);' Line Number: 65
User prompt
Fix Bug: 'TypeError: this.foods is undefined' in this line: 'for (var i = 0; i < this.foods.length; i++) {' Line Number: 57
User prompt
Fix Bug: 'ReferenceError: foods is not defined' in this line: 'for (var i = 0; i < foods.length; i++) {' Line Number: 57
var NeuralNetwork = function () { var self = this; self.weights = []; self.bias = 0; self.clone = function () { var clone = new NeuralNetwork(); clone.weights = this.weights.slice(); clone.bias = this.bias; return clone; }; self.feedForward = function (inputs) { var sum = 0; for (var i = 0; i < inputs.length; i++) { sum += inputs[i] * this.weights[i]; } return sum + this.bias; }; }; var AI = Container.expand(function () { var self = Container.call(this); self.inheritTraits = function (parent) { this.food = parent.food * 0.5; }; var ais = []; self.aiGraphics = self.createAsset('ai', 'AI character', .5, .5); self.food = 10; self.eat = function (food) { if (food.amount > 0) { this.food += food.consume(1); } }; self.share = function (otherAI) { var decision = this.neuralNetwork.feedForward([this.food, otherAI.food]); if (decision > 0) { var shareAmount = this.food * 0.1; this.food -= shareAmount; otherAI.food += shareAmount; } }; self.survive = function () { this.food -= 0.01; if (this.food < 0) { this.destroy(); var index = ais.indexOf(this); if (index > -1) { ais.splice(index, 1); } } }; self.neuralNetwork = new NeuralNetwork(); self.neuralNetwork.weights = [Math.random(), Math.random()]; self.neuralNetwork.bias = Math.random(); self.direction = Math.random() * 2 * Math.PI; self.move = function () { var nearestFood = null; var nearestFoodDistance = Infinity; for (var i = 0; i < this.foods.length; i++) { var distance = Math.sqrt(Math.pow(this.x - this.foods[i].x, 2) + Math.pow(this.y - this.foods[i].y, 2)); if (distance < nearestFoodDistance) { nearestFood = this.foods[i]; nearestFoodDistance = distance; } } var decision = this.neuralNetwork.feedForward([this.food, nearestFood.amount, nearestFoodDistance]); if (decision > 0) { if (Math.random() < 0.05) { self.direction += Math.random() * 0.2 - 0.1; } } else { var dx = nearestFood.x - this.x; var dy = nearestFood.y - this.y; self.direction = Math.atan2(dy, dx); } var speed = 5; var newX = self.x + Math.cos(self.direction) * speed; var newY = self.y + Math.sin(self.direction) * speed; if (newX < 0) { newX = 0; self.direction = Math.PI - self.direction; } else if (newX > 2048) { newX = 2048; self.direction = Math.PI - self.direction; } if (newY < 0) { newY = 0; self.direction = -self.direction; } else if (newY > 2732) { newY = 2732; self.direction = -self.direction; } self.x = newX; self.y = newY; }; self.reproduce = function () { if (this.food > 10) { var child = new AI(); child.neuralNetwork = this.neuralNetwork.clone(); for (var i = 0; i < child.neuralNetwork.weights.length; i++) { if (Math.random() < 0.1) { child.neuralNetwork.weights[i] += Math.random() * 0.2 - 0.1; } } if (Math.random() < 0.1) { child.neuralNetwork.bias += Math.random() * 0.2 - 0.1; } child.inheritTraits(this); this.food -= 10; ais.push(child); child.direction = Math.random() * 2 * Math.PI; return child; } return null; }; }); var Food = Container.expand(function () { var self = Container.call(this); var foodGraphics = self.createAsset('food', 'Food', .5, .5); self.amount = 10; self.consume = function (amount) { this.amount -= amount; return amount; }; }); var Game = Container.expand(function () { var self = Container.call(this); var ais = []; var foods = []; for (var i = 0; i < 3; i++) { var ai = new AI(); ai.x = Math.random() * 2048; ai.y = Math.random() * 2732; ais.push(ai); self.addChild(ai); var food = new Food(); food.x = Math.random() * 2048; food.y = Math.random() * 2732; foods.push(food); self.addChild(food); } LK.on('tick', function () { for (var i = 0; i < ais.length; i++) { var ai = ais[i]; ai.move(); ai.survive(); for (var j = 0; j < foods.length; j++) { var food = foods[j]; if (ai.intersects(food)) { ai.eat(food); food.consume(1); if (food.amount <= 0) { food.destroy(); foods.splice(j, 1); j--; } } } for (var j = i + 1; j < ais.length; j++) { var otherAI = ais[j]; if (ai.intersects(otherAI)) { var decision = ai.neuralNetwork.feedForward([ai.food, otherAI.food]); if (decision > 0) { ai.share(otherAI); } } } if (ai.food <= 0) { var index = ais.indexOf(ai); if (index > -1) { ais.splice(index, 1); } ai.destroy(); i--; } } if (LK.ticks % 600 == 0) { for (var i = 0; i < ais.length; i++) { var ai = ais[i]; var child = ai.reproduce(); if (child) { child.x = ai.x; child.y = ai.y; self.addChild(child); ais.push(child); } } } if (LK.ticks % 60 == 0) { var food = new Food(); food.x = Math.random() * 2048; food.y = Math.random() * 2732; foods.push(food); self.addChild(food); } }); });
var NeuralNetwork = function () {
var self = this;
self.weights = [];
self.bias = 0;
self.clone = function () {
var clone = new NeuralNetwork();
clone.weights = this.weights.slice();
clone.bias = this.bias;
return clone;
};
self.feedForward = function (inputs) {
var sum = 0;
for (var i = 0; i < inputs.length; i++) {
sum += inputs[i] * this.weights[i];
}
return sum + this.bias;
};
};
var AI = Container.expand(function () {
var self = Container.call(this);
self.inheritTraits = function (parent) {
this.food = parent.food * 0.5;
};
var ais = [];
self.aiGraphics = self.createAsset('ai', 'AI character', .5, .5);
self.food = 10;
self.eat = function (food) {
if (food.amount > 0) {
this.food += food.consume(1);
}
};
self.share = function (otherAI) {
var decision = this.neuralNetwork.feedForward([this.food, otherAI.food]);
if (decision > 0) {
var shareAmount = this.food * 0.1;
this.food -= shareAmount;
otherAI.food += shareAmount;
}
};
self.survive = function () {
this.food -= 0.01;
if (this.food < 0) {
this.destroy();
var index = ais.indexOf(this);
if (index > -1) {
ais.splice(index, 1);
}
}
};
self.neuralNetwork = new NeuralNetwork();
self.neuralNetwork.weights = [Math.random(), Math.random()];
self.neuralNetwork.bias = Math.random();
self.direction = Math.random() * 2 * Math.PI;
self.move = function () {
var nearestFood = null;
var nearestFoodDistance = Infinity;
for (var i = 0; i < this.foods.length; i++) {
var distance = Math.sqrt(Math.pow(this.x - this.foods[i].x, 2) + Math.pow(this.y - this.foods[i].y, 2));
if (distance < nearestFoodDistance) {
nearestFood = this.foods[i];
nearestFoodDistance = distance;
}
}
var decision = this.neuralNetwork.feedForward([this.food, nearestFood.amount, nearestFoodDistance]);
if (decision > 0) {
if (Math.random() < 0.05) {
self.direction += Math.random() * 0.2 - 0.1;
}
} else {
var dx = nearestFood.x - this.x;
var dy = nearestFood.y - this.y;
self.direction = Math.atan2(dy, dx);
}
var speed = 5;
var newX = self.x + Math.cos(self.direction) * speed;
var newY = self.y + Math.sin(self.direction) * speed;
if (newX < 0) {
newX = 0;
self.direction = Math.PI - self.direction;
} else if (newX > 2048) {
newX = 2048;
self.direction = Math.PI - self.direction;
}
if (newY < 0) {
newY = 0;
self.direction = -self.direction;
} else if (newY > 2732) {
newY = 2732;
self.direction = -self.direction;
}
self.x = newX;
self.y = newY;
};
self.reproduce = function () {
if (this.food > 10) {
var child = new AI();
child.neuralNetwork = this.neuralNetwork.clone();
for (var i = 0; i < child.neuralNetwork.weights.length; i++) {
if (Math.random() < 0.1) {
child.neuralNetwork.weights[i] += Math.random() * 0.2 - 0.1;
}
}
if (Math.random() < 0.1) {
child.neuralNetwork.bias += Math.random() * 0.2 - 0.1;
}
child.inheritTraits(this);
this.food -= 10;
ais.push(child);
child.direction = Math.random() * 2 * Math.PI;
return child;
}
return null;
};
});
var Food = Container.expand(function () {
var self = Container.call(this);
var foodGraphics = self.createAsset('food', 'Food', .5, .5);
self.amount = 10;
self.consume = function (amount) {
this.amount -= amount;
return amount;
};
});
var Game = Container.expand(function () {
var self = Container.call(this);
var ais = [];
var foods = [];
for (var i = 0; i < 3; i++) {
var ai = new AI();
ai.x = Math.random() * 2048;
ai.y = Math.random() * 2732;
ais.push(ai);
self.addChild(ai);
var food = new Food();
food.x = Math.random() * 2048;
food.y = Math.random() * 2732;
foods.push(food);
self.addChild(food);
}
LK.on('tick', function () {
for (var i = 0; i < ais.length; i++) {
var ai = ais[i];
ai.move();
ai.survive();
for (var j = 0; j < foods.length; j++) {
var food = foods[j];
if (ai.intersects(food)) {
ai.eat(food);
food.consume(1);
if (food.amount <= 0) {
food.destroy();
foods.splice(j, 1);
j--;
}
}
}
for (var j = i + 1; j < ais.length; j++) {
var otherAI = ais[j];
if (ai.intersects(otherAI)) {
var decision = ai.neuralNetwork.feedForward([ai.food, otherAI.food]);
if (decision > 0) {
ai.share(otherAI);
}
}
}
if (ai.food <= 0) {
var index = ais.indexOf(ai);
if (index > -1) {
ais.splice(index, 1);
}
ai.destroy();
i--;
}
}
if (LK.ticks % 600 == 0) {
for (var i = 0; i < ais.length; i++) {
var ai = ais[i];
var child = ai.reproduce();
if (child) {
child.x = ai.x;
child.y = ai.y;
self.addChild(child);
ais.push(child);
}
}
}
if (LK.ticks % 60 == 0) {
var food = new Food();
food.x = Math.random() * 2048;
food.y = Math.random() * 2732;
foods.push(food);
self.addChild(food);
}
});
});