Skip to content

Commit

Permalink
Added maximum width parameter to roomer and lifetime decay parameter …
Browse files Browse the repository at this point in the history
…to tunneler
  • Loading branch information
1upD committed Apr 30, 2018
1 parent 6f8f928 commit 6765c1e
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 50 deletions.
39 changes: 28 additions & 11 deletions Alife/Agents/Roomer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ public class Roomer : BaseAgent
private int _dz;
public int Height;
public string Style;
public int MaxWidth;
public bool MustDeploy;

// Default constructor required by serializer
public Roomer()
Expand All @@ -41,13 +43,17 @@ public Roomer()

Random random = new Random();

this._x1 = random.Next(1, 4);
this._x2 = random.Next(-4, -1);
this._y1 = random.Next(1, 4);
this._y2 = random.Next(-4, -1);
this.MaxWidth = 8;

this._x1 = random.Next(1, this.MaxWidth);
this._x2 = random.Next(-1 * this.MaxWidth, -1);
this._y1 = random.Next(1, this.MaxWidth);
this._y2 = random.Next(-1 * this.MaxWidth, -1);

this.Style = "default";
this.Height = 2;

this.MustDeploy = true;
}

/**
Expand All @@ -56,7 +62,7 @@ public Roomer()
* <author>1upD</author>
* </summary>
*/
public Roomer(int x = 0, int y = 0, int z = 0, string style = "", int height = 2)
public Roomer(int x = 0, int y = 0, int z = 0, string style = "", int height = 2, int maxWidth = 8, bool mustDeploy = true)
{
this._completed = false;
this.X = x;
Expand All @@ -65,13 +71,19 @@ public Roomer(int x = 0, int y = 0, int z = 0, string style = "", int height = 2

Random random = new Random();

this._x1 = random.Next(1, 4);
this._x2 = random.Next(-4, -1);
this._y1 = random.Next(1, 4);
this._y2 = random.Next(-4, -1);
this.MaxWidth = maxWidth;

this._x1 = random.Next(1, this.MaxWidth);
this._x2 = random.Next(-1 * this.MaxWidth, -1);
this._y1 = random.Next(1, this.MaxWidth);
this._y2 = random.Next(-1 * this.MaxWidth, -1);

this.Style = style;
this.Height = height;

this.MustDeploy = mustDeploy;

log.Debug(string.Format("Roomer spawned at {0}, {1}, {2}.", this.X, this.Y, this.Z));
}

public void GetObjectData(SerializationInfo info, StreamingContext context)
Expand Down Expand Up @@ -115,19 +127,24 @@ public override void Step(AlifeMap map)
for (int y = this.Y + this._y2; y < this.Y + this._y1; y++)
{
locationsToMark.Add(new Tuple<int, int, int>(x, y, z));
bool isSpaceOccupied = map.GetLocation(x, y, z) != null || map.GetLocation(x, y, z) != "";
bool isSpaceOccupied = map.GetLocation(x, y, z) != null && map.GetLocation(x, y, z) != "";
spacesOccupied += isSpaceOccupied ? 1 : 0;
total++;
}
}
}

if (spacesOccupied < total / 2)
if (spacesOccupied < total / 2 && MustDeploy)
{
log.Debug(string.Format("Roomer deployed: {0} spaces occupied, {1} total.", spacesOccupied, total));
log.Debug(string.Format("Roomer deployed at x: {0} y: {1} z: {2}", this.X, this.Y, this.Z));
foreach (var tuple in locationsToMark)
{
map.MarkLocation(this.Style, tuple.Item1, tuple.Item2, tuple.Item3);
}
} else
{
log.Debug(string.Format("Roomer did not deploy. {0} spaces occupied, {1} total.", spacesOccupied, total));
}

}
Expand Down
29 changes: 21 additions & 8 deletions Alife/Agents/Tunneler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public class Tunneler : BaseAgent
public string Style;
public AlifeDirection Direction;
public bool SpawnRoomerOnDeath;
public int LifetimeDecayRate;

private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

Expand All @@ -58,6 +59,7 @@ public Tunneler()
this.MinWidthDecayRate = 0;
this.ProbSpawnRoomer = 0.0f;
this.SpawnRoomerOnDeath = true;
this.LifetimeDecayRate = 1;
}

/**
Expand All @@ -70,7 +72,7 @@ public Tunneler(string style = "", int x = 0, int y = 0, int z = 0,
int width = 1, int height = 2,
int lifetime = 1, int maxLifetime = 1,
float probReproduce = 0.0f, float probTurn = 0.0f, float probAscend = 0.0f,
AlifeDirection direction = AlifeDirection.East, int MaxHeighDecayRate = 0, int MinHeightDecayRate = 0, int MaxWidthDecayRate = 0, int MinWidthDecayRate = 0, float ProbSpawnRoomer = 0.0f, bool spawnRoomerOnDeath = true)
AlifeDirection direction = AlifeDirection.East, int MaxHeighDecayRate = 0, int MinHeightDecayRate = 0, int MaxWidthDecayRate = 0, int MinWidthDecayRate = 0, float ProbSpawnRoomer = 0.0f, bool spawnRoomerOnDeath = true, int LifeTimeDecayRate = 1)
{
this.X = x;
this.Y = y;
Expand All @@ -86,6 +88,9 @@ public Tunneler(string style = "", int x = 0, int y = 0, int z = 0,
this.Style = style;
this.ProbSpawnRoomer = ProbSpawnRoomer;
this.SpawnRoomerOnDeath = spawnRoomerOnDeath;
this.LifetimeDecayRate = 1;

log.Debug(string.Format("Tunneler spawned at {0}, {1}, {2}.", this.X, this.Y, this.Z));
}

public new void GetObjectData(SerializationInfo info, StreamingContext context)
Expand Down Expand Up @@ -115,7 +120,7 @@ public override void Step(AlifeMap map)
{
try {
// Check if agent is still alive
if (this.Lifetime-- > 0)
if (this.Lifetime > 0)
{
int seed = this.X + this.Y + this.Z + (int)this.Direction + this.Height + this.Width + (int)System.DateTime.Now.Ticks;

Expand All @@ -142,16 +147,17 @@ public override void Step(AlifeMap map)
AlifeDirection childDirection = AlifeDirectionOperations.Add(this.Direction, polarity);
int widthDecay = random.Next(this.MinWidthDecayRate, this.MaxWidthDecayRate);
int heightDecay = random.Next(this.MinHeightDecayRate, this.MaxHeightDecayRate);
Tunneler child = new Tunneler(this.Style, this.X, this.Y, this.Z, this.Width - widthDecay, this.Height - heightDecay, this.MaxLifetime - 1, this.MaxLifetime - 1, this.ProbReproduce, this.ProbTurn, this.ProbAscend, childDirection);
Tunneler child = new Tunneler(this.Style, this.X, this.Y, this.Z, this.Width - widthDecay, this.Height - heightDecay, this.MaxLifetime - this.LifetimeDecayRate, this.MaxLifetime - this.LifetimeDecayRate, this.ProbReproduce, this.ProbTurn, this.ProbAscend, childDirection);
map.Agents.Add(child);
}
else
{
sample = random.NextDouble();
if (sample < this.ProbSpawnRoomer)
{
Roomer child = new Roomer(x: this.X, y: this.Y, z: this.Z, style: this.Style, height: Math.Max(this.Height, 2));
}
Roomer child = new Roomer(x: this.X, y: this.Y, z: this.Z, style: this.Style, height: Math.Max(this.Height, 2), maxWidth: Math.Min(this.Width * 2, 3), mustDeploy: false);
map.Agents.Add(child);
}
}

// Get new random seed
Expand Down Expand Up @@ -209,11 +215,18 @@ public override void Step(AlifeMap map)
}
else if (this.SpawnRoomerOnDeath)
{
// Add a roomer
map.Agents.Add(new Roomer(this.X, this.Y, this.Z, this.Style, Math.Max(this.Height, 2)));
log.Debug(string.Format("Tunneler died at {0}, {1}, {2}.", this.X, this.Y, this.Z));

// Add a roomer
Roomer child = new Roomer(x: this.X, y: this.Y, z: this.Z, style: this.Style, height: Math.Max(this.Height, 2), maxWidth: Math.Min(this.Width * 2, 3));
map.Agents.Add(child);


}

this.Lifetime--;
}
} catch(Exception e){
catch (Exception e){
log.Error("Error in Tunneler Step function: ", e);
}
}
Expand Down
42 changes: 11 additions & 31 deletions NailsCmd/alife.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,53 +22,33 @@
</BaseAgent> -->
<BaseAgent xsi:type="Tunneler">
<X>0</X>
<Y>1</Y>
<Y>0</Y>
<Z>0</Z>
<Width>3</Width>
<Height>2</Height>
<Lifetime>20</Lifetime>
<MaxLifetime>20</MaxLifetime>
<ProbReproduce>0.05</ProbReproduce>
<ProbTurn>0.1</ProbTurn>
<ProbAscend>0.05</ProbAscend>
<Height>3</Height>
<Lifetime>32</Lifetime>
<MaxLifetime>32</MaxLifetime>
<ProbReproduce>0.0625</ProbReproduce>
<ProbTurn>0.0625</ProbTurn>
<ProbAscend>0.0625</ProbAscend>
<Style>dev</Style>
<Direction>North</Direction>
<ProbSpawnRoomeer>0.005</ProbSpawnRoomeer>
<ProbSpawnRoomeer>0.0625</ProbSpawnRoomeer>
<MinHeightDecayRate>0</MinHeightDecayRate>
<MaxHeightDecayRate>1</MaxHeightDecayRate>
<MinWidthDecayRate>0</MinWidthDecayRate>
<MaxWidthDecayRate>2</MaxWidthDecayRate>
<MaxWidthDecayRate>1</MaxWidthDecayRate>
<SpawnRoomerOnDeath>true</SpawnRoomerOnDeath>
<LifetimeDecayRate>8</LifetimeDecayRate>
</BaseAgent>

<BaseAgent xsi:type="Roomer">
<X>0</X>
<Y>0</Y>
<Z>0</Z>
<Height>4</Height>
<Style>IndustrialTest</Style>

</BaseAgent>

<BaseAgent xsi:type="Tunneler">
<X>0</X>
<Y>-1</Y>
<Z>0</Z>
<Width>3</Width>
<Height>2</Height>
<Lifetime>20</Lifetime>
<MaxLifetime>20</MaxLifetime>
<ProbReproduce>0.05</ProbReproduce>
<ProbTurn>0.1</ProbTurn>
<ProbAscend>0.05</ProbAscend>
<Style>dev</Style>
<Direction>South</Direction>
<ProbSpawnRoomeer>0.005</ProbSpawnRoomeer>
<MinHeightDecayRate>0</MinHeightDecayRate>
<MaxHeightDecayRate>1</MaxHeightDecayRate>
<MinWidthDecayRate>0</MinWidthDecayRate>
<MaxWidthDecayRate>2</MaxWidthDecayRate>
<SpawnRoomerOnDeath>true</SpawnRoomerOnDeath>

</BaseAgent>

</ArrayOfBaseAgent>

0 comments on commit 6765c1e

Please sign in to comment.