Skip to content

Commit

Permalink
Added a 'Corpse' class that represents a deceased agent.
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewanderson committed Oct 12, 2010
1 parent 753f615 commit 6eef9ce
Show file tree
Hide file tree
Showing 5 changed files with 145 additions and 0 deletions.
19 changes: 19 additions & 0 deletions Cas/Core/AgentBase.cs
Expand Up @@ -142,6 +142,25 @@ public bool CanReplicate(double reproductionThreshold)
&& agents.All(agent => agent.CanReplicate(reproductionThreshold));
}

public List<Resource> GeneticMaterial
{
get
{
var geneticResources = new List<Resource>();
foreach (ICell cell in this.Cells)
{
geneticResources.AddRange(cell.GeneticMaterial);
}

foreach (IAgent agent in this.Agents)
{
geneticResources.AddRange(agent.GeneticMaterial);
}

return geneticResources;
}
}

public abstract bool IsEligableForDeath { get; }

#endregion
Expand Down
14 changes: 14 additions & 0 deletions Cas/Core/CellBase.cs
Expand Up @@ -169,6 +169,20 @@ public bool CanReplicate(double reproductionThreshold)
}
}

public List<Resource> GeneticMaterial
{
get
{
var geneticResources = new List<Resource>();
for (int i = 0; i < this.ActiveTagsInModel; i++)
{
geneticResources.AddRange(this.GetTagByIndex(i).Data);
}

return geneticResources;
}
}

#endregion

public override string ToString()
Expand Down
1 change: 1 addition & 0 deletions Cas/Core/Core.csproj
Expand Up @@ -71,6 +71,7 @@
<ItemGroup>
<Compile Include="AgentBase.cs" />
<Compile Include="CellBase.cs" />
<Compile Include="Corpse.cs" />
<Compile Include="DietType.cs">
<SubType>Code</SubType>
</Compile>
Expand Down
107 changes: 107 additions & 0 deletions Cas/Core/Corpse.cs
@@ -0,0 +1,107 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Cas.Core.Extensions;
using Cas.Core.Interfaces;

namespace Cas.Core
{
/// <summary>
/// When an agent dies, a corpse object is created which contains the resources that
/// were encoded in its genetic code.
/// </summary>
public class Corpse : IResourceNode
{
internal List<Resource> Reservoir { get; private set; }

public long Id { get; private set; }

public Tag Offense { get; set; }

public Tag Defense { get; set; }

public Tag Exchange { get; set; }


public Corpse(IAgent deceased)
{
if (deceased == null) throw new ArgumentNullException("deceased");

this.Id = deceased.Species.Id;
this.Offense = Tag.New(deceased.Offense);
this.Defense = Tag.New(deceased.Defense);
this.Exchange = Tag.New(deceased.Exchange);

// Build the reservoir from the genetic code of the deceased.
this.Reservoir = deceased.GeneticMaterial;
}

public int CurrentResourceCount
{
get
{
return this.Reservoir.Count;
}
}

public List<Resource> RemoveResources(int count)
{
if (count < 0) throw new ArgumentOutOfRangeException("count");

var resources = new List<Resource>();
for (int i = 0; i < count; i++)
{
if (this.Reservoir.Count == 0) break;
resources.Add(this.Reservoir.RemoveRandom());
}
return resources;
}

public void AddResources(List<Resource> resources)
{
throw new NotSupportedException("Resources cannot be added to a corpse.");
}

public string ShowResourcePool(string delimiter)
{
return string.Join(delimiter, this.Reservoir.Select(x => x.Label.ToString()));
}

public IResourceNode Source
{
get
{
return null;
}
}

public List<Resource> RenewableResources
{
get
{
return new List<Resource>();
}
}

public void RefreshReservoir()
{
throw new NotSupportedException("Cannot refresh a corpse");
}

public string ToShortString()
{
return string.Format("C.{0}: {1} {2}", this.Id, this.Offense, this.Defense);
}

public override string ToString()
{
return string.Format("C.{0}: {1} {2}, {3} resources", this.Id, this.Offense, this.Defense, this.Reservoir.Count);
}

public IResourceNode DeepCopy()
{
throw new NotImplementedException();
}
}
}
4 changes: 4 additions & 0 deletions Cas/Core/Interfaces/IIsAlive.cs
Expand Up @@ -20,5 +20,9 @@ public interface IIsAlive
/// </summary>
bool IsEligableForDeath { get; }

/// <summary>
/// Resources corresponding to all of the tags in the living object.
/// </summary>
List<Resource> GeneticMaterial { get; }
}
}

0 comments on commit 6eef9ce

Please sign in to comment.