// Consumes energy and returns color for new state // If herbivores or carnivores are not still activated, don't consume energy public void BeginIteration(Color myColor) { me = LifeParametersFromColor(myColor); // not "living creatures" doesn't have metabolism... if (me.trophicLevel == SOIL) { return; } if (this.IsActivated() == false) { return; } // consumes energy switch (me.trophicLevel) { case PLANT: me.energyLevel -= ENERGY_CONSUMPTION_PLANTS; break; case HERBIVORE: me.energyLevel -= ENERGY_CONSUMPTION_HERBIVORES; break; case CARNIVORE: me.energyLevel -= ENERGY_CONSUMPTION_CARNIVORES; break; } me.energyLevel = me.energyLevel < 0 ? 0 : me.energyLevel; // update my color with new energy level me.myColor = ColorFromLifeParameters(me); }
// Changes color to reflect energy level at the beginning of simulation // Plants have ENERGY_LEVEL_AT_BIRTH // Herbivores and Carnivores have -1. They don't start losing energy until they eat something // Return White if color is not a valid creature public static Color SetInitialEnergyLevel(Color c) { try { lifeParameters myPars = LifeParametersFromColor(c); switch (myPars.trophicLevel) { case SOIL: break; case PLANT: myPars.energyLevel = ENERGY_LEVEL_AT_BIRTH; break; case HERBIVORE: myPars.energyLevel = -1; // non activated break; case CARNIVORE: myPars.energyLevel = -1; // non activated break; } return(ColorFromLifeParameters(myPars)); } catch (Exception) { return(deadCreatureColor); } }
// returns offspring color or white if it is not time for reproduction public static Color IsTimeForReproduction(Color c2) { Random r = new Random(); float p = r.Next(1000) / 1000f; float q = 0f; lifeParameters pars = new lifeParameters(); pars = LifeParametersFromColor(c2); pars.energyLevel = ENERGY_LEVEL_AT_BIRTH; // <--- TO BE INCLUDED: mutations switch (pars.trophicLevel) { case PLANT: q = PLANT_REPRODUCTION_PROBABILITY; break; case HERBIVORE: q = HERBIVORE_REPRODUCTION_PROBABILITY; break; case CARNIVORE: q = CARNIVORE_REPRODUCTION_PROBABILITY; break; } if (p < q) { return(ColorFromLifeParameters(pars)); } else { return(deadCreatureColor); } }
/// <summary> /// returns creature's color from parameters /// </summary> public static Color GetColor(int trophicLevel, int energyLevel, int attackLevel, int defenseLevel) { lifeParameters me = new lifeParameters(); me.trophicLevel = trophicLevel; me.energyLevel = energyLevel; me.attackLevel = attackLevel; me.defenseLevel = defenseLevel; me.myColor = ColorFromLifeParameters(me); return(me.myColor); }
private static Color ColorFromLifeParameters(lifeParameters me) { // for SOIL just return original color if (me.trophicLevel == SOIL) { return(me.myColor); } // if creature has died, cell becames white (to avoid problems with soil defence) <----- TO BE ADJUSTED if (me.energyLevel == 0) // -1 means not activated { return(deadCreatureColor); } // format color depending on thropic level int e; if (me.energyLevel == -1) // non activated { e = ENERGY_LEVEL_COLOR_NOT_ACTIVATED; } else { e = (int)Math.Ceiling(ENERGY_LEVEL_COLOR_MIN + me.energyLevel); } switch (me.trophicLevel) { case PLANT: return(Color.FromArgb(me.attackLevel, e, me.defenseLevel)); case HERBIVORE: return(Color.FromArgb(me.defenseLevel, me.attackLevel, e)); case CARNIVORE: return(Color.FromArgb(e, me.defenseLevel, me.attackLevel)); default: // something is wrong throw new IndexOutOfRangeException(); } }
// Assign lifeParameters values from color // assume color is of a valid type // exception if trophicLevel is not valid private static lifeParameters LifeParametersFromColor(Color col) { lifeParameters pars = new lifeParameters(); // backup pars.myColor = col; if (col.R == 255 && col.G == 255) { pars.trophicLevel = SOIL; } // black color is treated as SOIL else if (col.R == 0 && col.G == 0 && col.R == 0) { pars.trophicLevel = SOIL; } // a plant else if (col.R <= ATTACK_LEVEL_MAX && col.G >= ENERGY_LEVEL_COLOR_MIN && col.B <= DEFENSE_LEVEL_MAX) { pars.trophicLevel = PLANT; } // a herbivore else if (col.R <= DEFENSE_LEVEL_MAX && col.G <= ATTACK_LEVEL_MAX && col.B >= ENERGY_LEVEL_COLOR_NOT_ACTIVATED) { pars.trophicLevel = HERBIVORE; } // a carnivore else if (col.R >= ENERGY_LEVEL_COLOR_NOT_ACTIVATED && col.G <= DEFENSE_LEVEL_MAX && col.B <= ATTACK_LEVEL_MAX) { pars.trophicLevel = CARNIVORE; } else { // something is wrong throw new IndexOutOfRangeException(); } switch (pars.trophicLevel) { case SOIL: pars.energyLevel = 0; pars.attackLevel = 0; pars.defenseLevel = 128 - col.B / 2; break; case PLANT: pars.energyLevel = col.G - ENERGY_LEVEL_COLOR_MIN; pars.attackLevel = col.R; pars.defenseLevel = col.B; break; case HERBIVORE: pars.energyLevel = col.B - ENERGY_LEVEL_COLOR_MIN; pars.attackLevel = col.G; pars.defenseLevel = col.R; break; case CARNIVORE: pars.energyLevel = col.R - ENERGY_LEVEL_COLOR_MIN; pars.attackLevel = col.B; pars.defenseLevel = col.G; break; } return(pars); }