Skip to content

Instantly share code, notes, and snippets.

@AymericG
Last active August 17, 2016 11:56
Show Gist options
  • Save AymericG/c54f3c1f5f2441d8efeb35e8b3b25a1e to your computer and use it in GitHub Desktop.
Save AymericG/c54f3c1f5f2441d8efeb35e8b3b25a1e to your computer and use it in GitHub Desktop.
Another herbivore
// 1. Set the species attributes
var species = new Species();
species.Name = "ant";
// Determine how your animal looks like
species.Skin = AnimalSkin.ant; // options: ant, beetle, inchworm, scorpion or spider
// How big can your creature grow?
// The bigger the more powerful it is, but the slower it is to reproduce.
species.MatureSize = 24; // between 24 and 48
// Sum should equal to 100.
species.PercentOfMaximumEnergyPerUnitRadius = 0;
species.PercentOfMaximumEatingSpeedPerUnitOfRadius = 0;
species.PercentOfMaximumAttackDamagePerUnitRadius = 0;
species.PercentOfMaximumDefendDamagePerUnitRadius = 0;
species.PercentOfMaximumEyeSightRadius = 0;
species.PercentOfMaximumSpeed = 100;
species.PercentOfMaximumInvisibleOdds = 0;
// For more information about what functions you can use
// Check out the SDK files: https://github.com/AymericG/terrariumjs/tree/master/app/assets/javascripts/SDK
var me = new AnimalMind(species);
// Looks for target plants, and starts moving towards the first one it finds
me.ScanForTargetPlant = function()
{
var minDistance = 9999;
var closestPlant = null;
for (var i = 0; i < this.State.SeenOrganisms.length; i++)
{
var target = this.State.SeenOrganisms[i];
if (target.IsPlant()){
var distanceToPlant = this.State.Position.DistanceWith(target.Position);
if (distanceToPlant < minDistance)
{
minDistance = distanceToPlant;
closestPlant = target;
}
}
}
if (closestPlant != null)
{
this.TargetPlant = closestPlant;
this.BeginMoving(new MovementVector(this.TargetPlant.Position, this.State.Species.MaximumSpeed()));
return true;
}
// Tell the caller we couldn't find a target
return false;
};
me.MoveToRandomPoint = function(){
// Pick random point on the map.
var y = MathUtils.RandomBetween(0, this.World.WorldHeight);
var x = MathUtils.RandomBetween(0, this.World.WorldWidth);
this.WriteTrace("Moving to random point...");
this.BeginMoving(new MovementVector(new Point(x, y), 2));
};
me.TargetPlant = null;
me.OnAttacked = function (attackedId){
this.WriteTrace("I am being attacked! I see " + this.State.SeenOrganisms.length + " potential enemie(s).");
var target = this.LookFor(attackedId);
if (target != null)
{
this.WriteTrace("Defending...");
this.BeginDefending(target);
}
};
me.OnEatCompleted = function (){
};
me.OnIdle = function() {
if (this.TargetPlant != null)
{
// See if our target plant still exists (it may have died)
// LookFor returns null if it isn't found
this.TargetPlant = this.LookFor(this.TargetPlant.Id);
}
// Our Creature will reproduce as often as possible so
// every turn it checks CanReproduce to know when it
// is capable. If so we call BeginReproduction with
// a null Dna parameter to being reproduction.
if (this.CanReproduce())
{
this.WriteTrace("Reproducing...");
this.BeginReproduction(null);
}
// Check to see if we are capable of eating
// If we are then try to eat or find food,
// else we'll just stop moving.
if (this.CanEat() && !this.IsEating())
{
// If we have a Target Plant we can try
// to either eat it, or move towards it.
// else we'll move to a random vector
// in search of a plant.
if (this.TargetPlant != null)
{
// If we are within range start eating
// and stop moving. Else we'll try
// to move towards the plant.
if (this.WithinEatingRange(this.TargetPlant))
{
this.WriteTrace("Eating...");
this.BeginEating(this.TargetPlant);
if (this.IsMoving())
this.StopMoving();
}
else
{
if (!this.IsMoving())
this.BeginMoving(new MovementVector(this.TargetPlant.Position, this.State.Species.MaximumSpeed()));
}
}
else
{
// We'll try try to find a target plant
// If we don't find one we'll move to
// a random vector
if(!this.ScanForTargetPlant())
{
if(!this.IsMoving())
this.MoveToRandomPoint();
}
}
}
else
{
// Since we are Full or we are already eating
// We should stop moving.
if(this.IsMoving())
this.StopMoving();
}
};
me.OnMoveCompleted = function (reason, blockerId){
};
me.OnReproduceCompleted = function()
{
// Count children?
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment