Skip to content

Commit

Permalink
Merge branch 'master' of github.com:andrewanderson/Web-Critters
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewanderson committed Oct 8, 2010
2 parents bf3b83f + 507767e commit ff9eb19
Show file tree
Hide file tree
Showing 20 changed files with 83 additions and 57 deletions.
6 changes: 3 additions & 3 deletions Cas/Core/Events/BirthEvent.cs
Expand Up @@ -12,8 +12,8 @@ public class BirthEvent : EventBase

public List<ISpecies> Parents { get; private set; }

public BirthEvent(Guid locationId, int generation, Type reproductionType, params ISpecies[] parents)
: base(locationId, generation)
public BirthEvent(ILocation location, int generation, Type reproductionType, params ISpecies[] parents)
: base(location, generation)
{
if (reproductionType == null) throw new ArgumentNullException("reproductionType");

Expand All @@ -24,7 +24,7 @@ public BirthEvent(Guid locationId, int generation, Type reproductionType, params
public override string ToString()
{
string parents = string.Join(",", Parents.Select(x => x.ToString()));
return string.Format("{0}: Birthed by {1} at location {2} by parent(s): {3}", this.Generation, ReproductionType.Name, LocationId, parents);
return string.Format("{0}: Birthed by {1} at {2} by parent(s): {3}", this.Generation, ReproductionType.Name, Location.ToShortString(), parents);
}


Expand Down
7 changes: 4 additions & 3 deletions Cas/Core/Events/CreationEvent.cs
@@ -1,15 +1,16 @@
using System;
using Cas.Core.Interfaces;

namespace Cas.Core.Events
{
public class CreationEvent : EventBase
{
public CreationEvent(Guid locationId, int generation)
: base(locationId, generation) { }
public CreationEvent(ILocation location, int generation)
: base(location, generation) { }

public override string ToString()
{
return string.Format("{0}: Created at location {1}", this.Generation, this.LocationId);
return string.Format("{0}: Created at {1}", this.Generation, this.Location.ToShortString());
}
}
}
9 changes: 5 additions & 4 deletions Cas/Core/Events/EventBase.cs
Expand Up @@ -5,21 +5,22 @@ namespace Cas.Core.Events
{
public abstract class EventBase : IEvent
{
public Guid LocationId { get; private set; }
public ILocation Location { get; private set; }

public int Generation { get; private set; }

protected EventBase(Guid locationId,int generation)
protected EventBase(ILocation location,int generation)
{
if (location == null) throw new ArgumentNullException("location");
if (generation < 0) throw new ArgumentOutOfRangeException("generation");

this.LocationId = locationId;
this.Location = location;
this.Generation = generation;
}

public override string ToString()
{
return string.Format("{0}: {1} at location {2}", this.Generation, this.GetType().Name, this.LocationId);
return string.Format("{0}: {1} at {2}", this.Generation, this.GetType().Name, this.Location.ToShortString());
}
}
}
5 changes: 3 additions & 2 deletions Cas/Core/Events/IEvent.cs
@@ -1,13 +1,14 @@
using System;
using Cas.Core.Interfaces;

namespace Cas.Core.Events
{
public interface IEvent
{
/// <summary>
/// The identifier of the location in which the event took place
/// The location at which the event took place
/// </summary>
Guid LocationId { get; }
ILocation Location { get; }

/// <summary>
/// The generation in which the event occurred
Expand Down
13 changes: 8 additions & 5 deletions Cas/Core/Events/MigrationEvent.cs
Expand Up @@ -2,22 +2,25 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Cas.Core.Interfaces;

namespace Cas.Core.Events
{
public class MigrationEvent : PayUpkeepEvent
{
public Guid OriginLocationId { get; set; }
public ILocation OriginLocation { get; private set; }

public MigrationEvent(Guid originLocationId, Guid destinationLocationId, int cost, int generation)
: base(destinationLocationId, cost, generation)
public MigrationEvent(ILocation originLocation, ILocation destinationLocation, int cost, int generation)
: base(destinationLocation, cost, generation)
{
this.OriginLocationId = originLocationId;
if (originLocation == null) throw new ArgumentNullException("originLocation");

this.OriginLocation = originLocation;
}

public override string ToString()
{
return string.Format("{0}: Migrated at a cost of {1} from location {2} to {3}", this.Generation, this.Cost, this.OriginLocationId, this.LocationId);
return string.Format("{0}: Migrated at a cost of {1} from {2} to {3}", this.Generation, this.Cost, this.OriginLocation.ToShortString(), this.Location.ToShortString());
}
}
}
7 changes: 4 additions & 3 deletions Cas/Core/Events/PayUpkeepEvent.cs
@@ -1,20 +1,21 @@
using System;
using Cas.Core.Interfaces;

namespace Cas.Core.Events
{
public class PayUpkeepEvent : EventBase
{
public int Cost { get; private set; }

public PayUpkeepEvent(Guid locationId, int cost, int generation)
: base(locationId, generation)
public PayUpkeepEvent(ILocation location, int cost, int generation)
: base(location, generation)
{
this.Cost = cost;
}

public override string ToString()
{
return string.Format("{0}: Paid upkeep of {1} at location {2}", this.Generation, this.Cost, this.LocationId);
return string.Format("{0}: Paid upkeep of {1} at {2}", this.Generation, this.Cost, this.Location.ToShortString());
}
}
}
8 changes: 4 additions & 4 deletions Cas/Core/Events/ReproductionEvent.cs
Expand Up @@ -13,8 +13,8 @@ public class ReproductionEvent : EventBase

public Type ReproductionType { get; private set; }

public ReproductionEvent(Guid locationId, int generation, Type reproductionType, ISpecies mate, params ISpecies[] children)
: base(locationId, generation)
public ReproductionEvent(ILocation location, int generation, Type reproductionType, ISpecies mate, params ISpecies[] children)
: base(location, generation)
{
this.Mate = mate;
this.Offspring = new List<ISpecies>(children);
Expand All @@ -27,11 +27,11 @@ public override string ToString()
if (Mate == null)
{

return string.Format("{0}: {1} at location {2} produced offspring: {3}", this.Generation, ReproductionType.Name, LocationId, offspring);
return string.Format("{0}: {1} at {2} produced offspring: {3}", this.Generation, ReproductionType.Name, Location.ToShortString(), offspring);
}
else
{
return string.Format("{0}: {1} with {2} at location {3} produced offspring: {4}", this.Generation, ReproductionType.Name, Mate, LocationId, offspring);
return string.Format("{0}: {1} with {2} at {3} produced offspring: {4}", this.Generation, ReproductionType.Name, Mate, Location.ToShortString(), offspring);
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions Cas/Core/Events/TargetOfEvent.cs
Expand Up @@ -12,8 +12,8 @@ public class TargetOfEvent : EventBase

public int Result { get; private set; }

public TargetOfEvent(Guid locationId, int generation, IIsUnique actor, int result)
: base(locationId, generation)
public TargetOfEvent(ILocation location, int generation, IIsUnique actor, int result)
: base(location, generation)
{
if (actor == null) throw new ArgumentNullException("actor");

Expand All @@ -23,7 +23,7 @@ public TargetOfEvent(Guid locationId, int generation, IIsUnique actor, int resul

public override string ToString()
{
return string.Format("{0}: Lost encounter ({3}) with ({2}) {1}", this.Generation, Actor, Actor.GetType().Name, Result);
return string.Format("{0}: Lost encounter ({3}) with ({2}) {1}", this.Generation, Actor.ToShortString(), Actor.GetType().Name, Result);
}
}
}
6 changes: 3 additions & 3 deletions Cas/Core/Events/TargetedEvent.cs
Expand Up @@ -9,8 +9,8 @@ public class TargetedEvent : EventBase

public int Result { get; private set; }

public TargetedEvent(Guid locationId, int generation, IIsUnique target, int result)
: base(locationId, generation)
public TargetedEvent(ILocation location, int generation, IIsUnique target, int result)
: base(location, generation)
{
if (target == null) throw new ArgumentNullException("target");

Expand All @@ -20,7 +20,7 @@ public TargetedEvent(Guid locationId, int generation, IIsUnique target, int resu

public override string ToString()
{
return string.Format("{0}: Won encounter ({3}) against ({2}) {1}", this.Generation, Target, Target.GetType().Name, Result);
return string.Format("{0}: Won encounter ({3}) against ({2}) {1}", this.Generation, Target.ToShortString(), Target.GetType().Name, Result);
}
}
}
5 changes: 5 additions & 0 deletions Cas/Core/Fossil.cs
Expand Up @@ -17,6 +17,11 @@ public Fossil(long id)
this.Id = id;
}

public string ToShortString()
{
return this.ToString();
}

public override string ToString()
{
return string.Format("F.{0}: extinct", this.Id);
Expand Down
5 changes: 5 additions & 0 deletions Cas/Core/Interfaces/IIsUnique.cs
Expand Up @@ -17,6 +17,11 @@ public interface IIsUnique
/// Unique identifier for the object.
/// </summary>
long Id { get; }

/// <summary>
/// A terse description of the object.
/// </summary>
string ToShortString();
}

public class IIsUnqiueEqualityComparer : IEqualityComparer<IIsUnique>
Expand Down
10 changes: 5 additions & 5 deletions Cas/Core/Interfaces/ILocation.cs
Expand Up @@ -10,11 +10,6 @@ namespace Cas.Core.Interfaces
/// </summary>
public interface ILocation : IBoundary
{
/// <summary>
/// A unique identifier for the location
/// </summary>
Guid Id { get; }

/// <summary>
/// The cost, in resources, that the location levies on its agents. Must be
/// a positive whole number.
Expand Down Expand Up @@ -45,5 +40,10 @@ public interface ILocation : IBoundary
/// Remove resources equal to the UpkeepCost from all agents in this location.
/// </summary>
void ChargeUpkeep(int generation);

/// <summary>
/// A terse representation of the Location.
/// </summary>
string ToShortString();
}
}
7 changes: 3 additions & 4 deletions Cas/Core/LocationBase.cs
Expand Up @@ -10,8 +10,6 @@ namespace Cas.Core
{
public abstract class LocationBase : ILocation
{
public Guid Id { get; private set; }

protected ISimulation Simulation { get; set; }

/// <summary>
Expand Down Expand Up @@ -44,7 +42,6 @@ protected LocationBase(ISimulation simulation)
{
if (simulation == null) throw new ArgumentNullException("simulation");

this.Id = Guid.NewGuid();
this.Simulation = simulation;
}

Expand Down Expand Up @@ -79,7 +76,7 @@ public void ChargeUpkeep(int generation)
{
foreach (var agent in Agents)
{
this.Simulation.AddEventToAgent(agent, new PayUpkeepEvent(this.Id, this.UpkeepCost, generation));
this.Simulation.AddEventToAgent(agent, new PayUpkeepEvent(this, this.UpkeepCost, generation));
if (agent.IsMultiAgent() || agent.Cells.Count == 0)
{
// TODO: How do we extract payment if the base agent has no resources?
Expand All @@ -92,5 +89,7 @@ public void ChargeUpkeep(int generation)
}
}
}

public abstract string ToShortString();
}
}
2 changes: 1 addition & 1 deletion Cas/Core/SimulationBase.cs
Expand Up @@ -420,7 +420,7 @@ private void QueueForMigration(ILocation location, List<KeyValuePair<IAgent, ILo
var destination = kvp.Value;
location.Agents.Remove(agent);
this.AddEventToAgent(agent, new MigrationEvent(location.Id, destination.Id, destination.UpkeepCost, this.CurrentGeneration));
this.AddEventToAgent(agent, new MigrationEvent(location, destination, destination.UpkeepCost, this.CurrentGeneration));
pendingMigrations.Add(kvp);
});
}
Expand Down
5 changes: 5 additions & 0 deletions Cas/Core/Species.cs
Expand Up @@ -217,6 +217,11 @@ public IEnumerable<IIsUnique> Predators
}
}

public string ToShortString()
{
return this.ToString();
}

public override string ToString()
{
return string.Format("S.{0}: {1}", this.id, this.exemplar.ToShortString());
Expand Down
7 changes: 6 additions & 1 deletion Cas/TestCas/GridLocation.cs
Expand Up @@ -37,9 +37,14 @@ public GridLocation(int x, int y, ISimulation simulation, List<IResourceNode> re
ResourceAllocation = resourceAllocation;
}

public override string ToShortString()
{
return string.Format("X={0}, Y={1}", X, Y);
}

public override string ToString()
{
return string.Format("X={0}, Y={1} : [{3} upkeep] : {2} agents", X, Y, this.Agents.Count, UpkeepCost);
return string.Format("{0} : [{1} upkeep] : {2} agents", this.ToShortString(), UpkeepCost, this.Agents.Count);
}
}
}
16 changes: 8 additions & 8 deletions Cas/TestCas/GridSimulation.cs
Expand Up @@ -100,11 +100,11 @@ protected override void InnerDoInteraction(ILocation location, IAgent actor, IIn
{
actor.Species.RecordConsumptionOf(ToUnique(target), result);

this.AddEventToAgent(actor, new TargetedEvent(location.Id, CurrentGeneration, ToUnique(target), result));
this.AddEventToAgent(actor, new TargetedEvent(location, CurrentGeneration, ToUnique(target), result));
if (target is IAgent)
{
var targetAgent = target as IAgent;
this.AddEventToAgent(targetAgent, new TargetOfEvent(location.Id, CurrentGeneration, ToUnique(actor), result));
this.AddEventToAgent(targetAgent, new TargetOfEvent(location, CurrentGeneration, ToUnique(actor), result));
}
}
}
Expand Down Expand Up @@ -156,9 +156,9 @@ private IAgent DoAsexualReproduction(IAgent parent, ILocation location)
var childCell = asexualReproductionInteraction.Interact(parent.Cells[0], null);
var childAgent = new GridAgent();
childAgent.Cells.Add(childCell);
this.RegisterBirth(childAgent, new BirthEvent(location.Id, CurrentGeneration, asexualReproductionInteraction.GetType(), parent.Species));
this.RegisterBirth(childAgent, new BirthEvent(location, CurrentGeneration, asexualReproductionInteraction.GetType(), parent.Species));

this.AddEventToAgent(parent, new ReproductionEvent(location.Id, CurrentGeneration, asexualReproductionInteraction.GetType(), null, childAgent.Species));
this.AddEventToAgent(parent, new ReproductionEvent(location, CurrentGeneration, asexualReproductionInteraction.GetType(), null, childAgent.Species));

return childAgent;
}
Expand Down Expand Up @@ -187,15 +187,15 @@ private IList<IAgent> DoSexualReproduction(IAgent parent1, IAgent parent2, IInte

var child1 = new GridAgent();
child1.Cells.Add(childCells[0]);
this.RegisterBirth(child1, new BirthEvent(location.Id, CurrentGeneration, interaction.GetType(), parent1.Species, parent2.Species));
this.RegisterBirth(child1, new BirthEvent(location, CurrentGeneration, interaction.GetType(), parent1.Species, parent2.Species));

var child2 = new GridAgent();
child2.Cells.Add(childCells[1]);
this.RegisterBirth(child2, new BirthEvent(location.Id, CurrentGeneration, interaction.GetType(), parent1.Species, parent2.Species));
this.RegisterBirth(child2, new BirthEvent(location, CurrentGeneration, interaction.GetType(), parent1.Species, parent2.Species));

// Events
this.AddEventToAgent(parent1, new ReproductionEvent(location.Id, CurrentGeneration, interaction.GetType(), parent2.Species, child1.Species, child2.Species));
this.AddEventToAgent(parent2, new ReproductionEvent(location.Id, CurrentGeneration, interaction.GetType(), parent1.Species, child1.Species, child2.Species));
this.AddEventToAgent(parent1, new ReproductionEvent(location, CurrentGeneration, interaction.GetType(), parent2.Species, child1.Species, child2.Species));
this.AddEventToAgent(parent2, new ReproductionEvent(location, CurrentGeneration, interaction.GetType(), parent1.Species, child1.Species, child2.Species));

return new[] {child1, child2};
}
Expand Down
2 changes: 1 addition & 1 deletion Cas/WebCritters/SpeciesDetails.cs
Expand Up @@ -12,7 +12,7 @@ namespace WebCritters
{
public partial class SpeciesDetails : Form
{
private WebCrittersForm ParentForm { get; set; }
private new WebCrittersForm ParentForm { get; set; }
private ISpecies CurrentSpecies { get; set; }
private ISimulation Simulation { get; set; }

Expand Down

0 comments on commit ff9eb19

Please sign in to comment.