I am programming a simulation thingy that I can use as a basis for a game or something later. I am programming in C# and with Unity. Unity is up to date on version 2017.1.0f3 (or 3.3.0.2 ?).
The simulation just spawns some random people and when they get older they may search for a partner and reproduce themselves.
Now I have some weird behaviour in that "search for a partner"-method. I will show the code below in a second. The method takes a list of people as an argument and validates that list to filter out every person that is older than 15 years and don't have a partner already. Also related persons get filtered out because I don't really want incest inside my simulation .. (yet? ^^)
To clarify my problem: in the List<> called potentialPartners are somehow people already having a partner. I am sure that it is no multithreading-caused problem because those partners were not set in the same frame. Also I by myself don't do any multithreading stuff yet, there're just the threads Unity creates as default.
public void SearchForPartner(List<PawnLogic> pawnsInRange)
{
if (!this.PartnerAllowed)
return;
System.Random rnd = new System.Random();
List<PawnLogic> potentialPartners = new List<PawnLogic>();
var pP = pawnsInRange.Where(x => x.PartnerAllowed && x.Gender != this.Gender && !this.IsKindred(x) && !x.IsKindred(this));
if (pP.Count() > 0)
potentialPartners.AddRange(pP);
if (potentialPartners.Count == 0)
return;
this.Partner = potentialPartners[rnd.Next(0, potentialPartners.Count)];
if (this.Partner.IsAlive)
{
this.Partner.Partner = this;
if (this.News != null)
{
this.News.Invoke(this, new NewsEventArgs(this, this.FirstLastName + " and " + this.Partner.FirstLastName + " become a couple."));
}
if (Gender == 'M')
this.Partner.SetNewLastname(this.thisPawn.LastnameNumber);
else
this.SetNewLastname(Partner.thisPawn.LastnameNumber);
}
else
this.Partner = null;
}
I guess my problem comes from the property "PartnerAllowed" because if I remember correctly the &&-operator only validates the right hand side if the left hand side returns true.
public bool PartnerAllowed
{
get
{
return this.partner == null && this.Age >= 16;
}
}
Just to be complete here is the IsKindred-method.. I don't really think that my problem comes outside of this but maybe I am not seeing something important.
public bool IsKindred(PawnLogic check)
{
if (Generation <= check.Generation - 4)
return false;
if ((Father == null || Mother == null || check.Father == null || check.Mother == null) && Siblings.Count == 0)
return false;
if (check == Mother || check == Father)
return true;
if (Siblings.Contains(check))
return true;
for (int i = 0; i < Siblings.Count; i++)
{
if (Siblings[i].Children.Contains(check))
return true;
}
if (Father != null && Father.IsKindred(check))
return true;
if (Mother != null && Mother.IsKindred(check))
return true;
return false;
}
My Partner-property looks like this:
public PawnLogic Partner
{
get
{
if (this.partner == null && this.thisPawn.PartnerNumber != null)
{
int indexOfPartner = this.thisPawn.PartnerNumber.ToInt();
if (indexOfPartner != -1)
this.partner = new PawnLogic(indexOfPartner);
else
this.partner = null;
}
return this.partner;
}
private set
{
if (value == null)
this.thisPawn.PartnerNumber = null;
else
{
this.thisPawn.PartnerNumber = value.Number;
this.partner = value;
}
}
}
I have no idea how to fix this. Especially because it works in another program which I wrote first. But I needed to transfer the code to this new project because Unity doesn't use .Net 4.6 yet. Maybe there was a change in Linq's behaviour between the framework-versions? Do someone can help me?
It is just a private project, but I want it to work properly anyway. :P
Let me know if you need more code or explanation to understand what is happening, what is supposed to happen or how something works.
Thank you for your time and effort :)