Exemplo n.º 1
0
 protected override void NotifyBumped(VisibleObject bumpedObject)
 {
     if (bumpedObject is Food)
     {
         return;
     }
     if (bumpedObject is Poop)
     {
         return;
     }
     base.NotifyBumped(bumpedObject);
     try
     {
         critterBrain.NotifyBumped(bumpedObject);
     }
     catch (System.Threading.ThreadAbortException)
     {
         // Do nothing
     }
     catch (Exception e)
     {
         Logger.OutputToLog("Error in NotifyBumped from file " + fileContainingBrain + ": Exception is " + e.Message + "\n" + e.StackTrace, Logger.LogLevel.Error);
         Die("Crashed");
         CritterWorldForm.AddMarqueeMessage(critterBrain.Name + " by " + critterBrain.Creator + " crashed.");
     }
 }
Exemplo n.º 2
0
        internal void Activate(CritterWrapper critterWrapper)
        {
            if (!critterWrapper.IsWaiting)
            {
                return;
            }
            Critter critter = null;

            try
            {
                Point possibleCritterPosition = GetStartingCritterPosition();
                critter                = Critter.CreateCritter(critterWrapper.File, critterWrapper.Brain, this, possibleCritterPosition);
                critter.MaximumAge     = maximumCritterAge;
                critterWrapper.Critter = critter;
                runningCritters.Add(critterWrapper);
                displayCritters.Add(critterWrapper);
            }
            catch (Exception e)
            {
                Logger.OutputToLog("Critter " + critterWrapper.CritterName + " by " + critterWrapper.CritterCreator + " in file " + critterWrapper.File + " failed to activate: " + e, Logger.LogLevel.Error);
                if (critter != null)
                {
                    critter.Die("Crashed");
                }
                CritterWorldForm.AddMarqueeMessage(critterWrapper.CritterName + " by " + critterWrapper.CritterCreator + " crashed.");
            }
        }
Exemplo n.º 3
0
 public override void Processing()
 {
     base.Processing();
     if (critterBrain == null)
     {
         return;
     }
     if (isNewborn)
     {
         try
         {
             birthday = DateTime.Now;
             critterBrain.Birth();
             isNewborn = false;
             Logger.OutputToLog("Critter " + critterBrain.Name + " by " + critterBrain.Creator + " in file " + File + " was born", Logger.LogLevel.Message);
         }
         catch (Exception e)
         {
             Logger.OutputToLog("Brain crashed from file " + File + " during birth: " + e.Message, Logger.LogLevel.Error);
             Die("Crashed");
             CritterWorldForm.AddMarqueeMessage(critterBrain.Name + " by " + critterBrain.Creator + " crashed.");
         }
         return;
     }
     try
     {
         critterBrain.Think();
     }
     catch (Exception e)
     {
         Logger.OutputToLog("Brain crashed from file " + File + " whilst thinking: " + e.Message, Logger.LogLevel.Error);
         Die("Crashed");
         CritterWorldForm.AddMarqueeMessage(critterBrain.Name + " by " + critterBrain.Creator + " crashed.");
     }
 }
Exemplo n.º 4
0
 public Competition(CritterWorldForm critterWorld)
 {
     this.critterWorld             = critterWorld;
     checkCompletionTimer          = new Timer();
     checkCompletionTimer.Interval = 1000;
     checkCompletionTimer.Tick    += new EventHandler(checkCompletionTimer_Tick);
     checkCompletionTimer.Start();
 }
Exemplo n.º 5
0
        public void LoadCritters()
        {
            StopWorld();
            // Get list of all dll files in specified folder. Iterate through them to find classes that implement ICritterFactory.
            string configDLLPath = Utility.GetConfiguration().BrainDLLPath.Trim();
            string dllPath       = (configDLLPath.Length > 0) ? configDLLPath : Path.GetDirectoryName(Application.ExecutablePath);

            Logger.OutputToLog("Loading critter brains from " + dllPath, Logger.LogLevel.Message);
            string[] dllFiles = System.IO.Directory.GetFiles(dllPath, "*.dll");
            foreach (string file in dllFiles)
            {
                try
                {
                    Assembly assembly = Assembly.LoadFrom(file);
                    foreach (Type type in assembly.GetTypes())
                    {
                        if (type.IsClass && type.GetInterface("ICritterFactory") != null)
                        {
                            // ICritterFactory is implemented. Get the brains and add them to the list.
                            try
                            {
                                ICritterFactory critterFactory = (ICritterFactory)Activator.CreateInstance(type);
                                foreach (CritterBrains.CritterBrain brain in critterFactory.GetCritterBrains())
                                {
                                    if (brain == null)
                                    {
                                        Logger.OutputToLog("Error in brain loading from " + file + ". Failed to load.  Brain is null.", Logger.LogLevel.Error);
                                        CritterWorldForm.AddMarqueeMessage(file + " crashed.");
                                    }
                                    else
                                    {
                                        waitingCritters.Add(new CritterWrapper(file, brain));
                                    }
                                }
                            }
                            catch (Exception e)
                            {
                                Logger.OutputToLog("Error in brain loading from " + file + ". Exception is " + e.Message + "\n" + e.StackTrace, Logger.LogLevel.Error);
                                CritterWorldForm.AddMarqueeMessage(file + " couldn't load.");
                            }
                        }
                        else
                        {
                            Logger.OutputToLog("Skipped type " + type.Name + " in " + file + "; it is not a class or doesn't implement ICritterFactory.", Logger.LogLevel.Warning);
                        }
                    }
                }
                catch (Exception e)
                {
                    Logger.OutputToLog("Error loading file " + file + ". Exception is " + e.Message + "\n" + e.StackTrace, Logger.LogLevel.Error);
                    CritterWorldForm.AddMarqueeMessage(file + " couldn't load.");
                }
            }
            updateTimer.Start();
        }
Exemplo n.º 6
0
 public void ReachedGoal()
 {
     if (isGoalReached)
     {
         return;
     }
     timeToGoal    = DateTime.Now.Subtract(birthday);
     isGoalReached = true;
     Die("Win");   // happy
     CritterWorldForm.AddPriorityMarqueeMessage(WhoAmI + " reached the goal!", Color.Green);
 }
Exemplo n.º 7
0
 public override void NotifyCloseToObject(VisibleObject objectCloseBy)
 {
     if (objectCloseBy is Food)
     {
         try
         {
             critterBrain.NotifyCloseToFood(objectCloseBy);
         }
         catch (System.Threading.ThreadAbortException)
         {
             // Do nothing
         }
         catch (Exception e)
         {
             Logger.OutputToLog("Error in NotifyCloseToFood from file " + fileContainingBrain + ": Exception is " + e.Message + "\n" + e.StackTrace, Logger.LogLevel.Error);
             Die("Crashed");
             CritterWorldForm.AddMarqueeMessage(critterBrain.Name + " by " + critterBrain.Creator + " crashed.");
         }
     }
     else if (objectCloseBy is Critter)
     {
         try
         {
             critterBrain.NotifyCloseToCritter((IOtherCritter)objectCloseBy);
         }
         catch (System.Threading.ThreadAbortException)
         {
             // Do nothing
         }
         catch (Exception e)
         {
             Logger.OutputToLog("Error in NotifyCloseToCritter from file " + fileContainingBrain + ": Exception is " + e.Message + "\n" + e.StackTrace, Logger.LogLevel.Error);
             Die("Crashed");
             CritterWorldForm.AddMarqueeMessage(critterBrain.Name + " by " + critterBrain.Creator + " crashed.");
         }
     }
     else if (objectCloseBy is Poop)
     {
         try
         {
             critterBrain.NotifyCloseToPoop(objectCloseBy.X, objectCloseBy.Y);
         }
         catch (System.Threading.ThreadAbortException)
         {
             // Do nothing
         }
         catch (Exception e)
         {
             Logger.OutputToLog("Error in NotifyCloseToPoop from file " + fileContainingBrain + ": Exception is " + e.Message + "\n" + e.StackTrace, Logger.LogLevel.Error);
             Die("Crashed");
             CritterWorldForm.AddMarqueeMessage(critterBrain.Name + " by " + critterBrain.Creator + " crashed.");
         }
     }
 }
Exemplo n.º 8
0
 public World(CritterWorldForm worldForm)
 {
     this.worldForm   = worldForm;
     waitingCritters  = new BindingList <CritterWrapper>();
     runningCritters  = new BindingList <CritterWrapper>();
     finishedCritters = new BindingList <CritterWrapper>();
     deadCritters     = new BindingList <CritterWrapper>();
     displayCritters  = new BindingList <CritterWrapper>();
     allObjects       = new List <VisibleObject>();
     leaderboard      = new Leaderboard(HighscoresFileName);
     CreateMap();
     newFoodNeeded        = Utility.GetConfiguration().FoodDumps;
     updateTimer          = new System.Windows.Forms.Timer();
     updateTimer.Interval = 1000;
     updateTimer.Tick    += new EventHandler(updateTimer_Tick);
 }
Exemplo n.º 9
0
 public override void NotifyBlockedByTerrain()
 {
     try
     {
         critterBrain.NotifyBlockedByTerrain();
     }
     catch (System.Threading.ThreadAbortException)
     {
         // Do nothing
     }
     catch (Exception e)
     {
         Logger.OutputToLog("Error in NotifyBlockedByTerrain from file " + fileContainingBrain + ": Exception is " + e.Message + "\n" + e.StackTrace, Logger.LogLevel.Error);
         Die("Crashed");
         CritterWorldForm.AddMarqueeMessage(critterBrain.Name + " by " + critterBrain.Creator + " crashed.");
     }
 }
Exemplo n.º 10
0
        public CompetitionDefault(CritterWorldForm critterWorld) : base(critterWorld)
        {
            Add(new Level("BackgroundBase.png", 10, 5, new Point(595, 430)));
            Add(new Level("Background01.png", 10, 5, new Point(595, 430)));
            Add(new Level("Background02.png", 10, 5, new Point(595, 430)));
            Add(new Level("Background03.png", 10, 5, new Point(595, 100)));
            Add(new Level("Background04.png", 10, 5, new Point(595, 230)));
            Add(new Level("Background05.png", 10, 5, new Point(595, 330)));

            Add(new Level("BackgroundBase.png", 10, 5, new Point(295, 430)));
            Add(new Level("Background01.png", 10, 5, new Point(395, 430)));
            Add(new Level("Background02.png", 10, 5, new Point(595, 130)));
            Add(new Level("Background03.png", 10, 5, new Point(495, 120)));
            Add(new Level("Background04.png", 10, 5, new Point(195, 230)));
            Add(new Level("Background05.png", 10, 5, new Point(595, 130)));

            Add(new Level("Background06.png", 10, 5, new Point(255, 340)));
        }
Exemplo n.º 11
0
 private void PumpEvents()
 {
     while (!isDead)
     {
         Message nextMessage = null;
         lock (messageQueue)
         {
             if (messageQueue.Count > 0)
             {
                 nextMessage = messageQueue.Dequeue();
             }
         }
         if (nextMessage != null)
         {
             try
             {
                 HandleEvent(nextMessage);
             }
             catch (Exception e)
             {
                 String who = "System";
                 if (this is Critter critter)
                 {
                     critter.Die("Crashed");
                     CritterWorldForm.AddMarqueeMessage((string)(critter.CritterBrain.Name + " by " + critter.CritterBrain.Creator + " crashed."));
                     who = ((Critter)this).File;
                 }
                 Logger.OutputToLog("HandleEvent crashed processing message " + nextMessage.GetType().ToString() + " in " + who + "'s VisibleObject.PumpEvents: " + e.Message, Logger.LogLevel.Error);
             }
         }
         // Do other stuff.
         Processing();
         // Give up context to another thread.
         Thread.Sleep(1);
     }
 }
Exemplo n.º 12
0
        public EnumAttackResult Attack(IOtherCritter otherCritter)
        {
            if (IsDead)
            {
                return(EnumAttackResult.IAmDead);
            }
            Critter other = (Critter)otherCritter;

            if (other.IsDead)
            {
                return(EnumAttackResult.DeadAlready);
            }
            if (!IsTouching(otherCritter))
            {
                return(EnumAttackResult.TooFarAway);
            }
            bool iWin = false;

            if (other.Energy == energy)
            {
                // The two are evenly matched, so the result is 50/50
                iWin = Utility.NextRandom(0, 2) == 0;
            }
            else if (other.Energy < energy)
            {
                // The other critter is weaker
                if (other.Energy + 20 < energy)
                {
                    // 90% of me winning
                    iWin = Utility.NextRandom(0, 10) > 0;
                }
                else
                {
                    // 75% of me winning
                    iWin = Utility.NextRandom(0, 100) > 24;
                }
            }
            else
            {
                if (other.Energy > energy + 20)
                {
                    // 10% of me winning
                    iWin = Utility.NextRandom(0, 10) == 0;
                }
                else
                {
                    // 25% of me winning
                    iWin = Utility.NextRandom(0, 100) < 25;
                }
            }
            int energyLoss = Utility.NextRandom(5, 11);

            if (iWin)
            {
                // We win
                Energy = Energy - energyLoss > 5 ? Energy - energyLoss : 5;
                string msg = WhoAmI + " attacked and killed " + other.WhoAmI + ".";
                Logger.OutputToLog(msg, Logger.LogLevel.Message);
                CritterWorldForm.AddMarqueeMessage(msg, Color.DarkRed);
                other.Die("Killed by " + WhoAmI);
                return(EnumAttackResult.Killed);
            }
            else
            {
                // They win
                other.Energy = other.Energy - energyLoss > 5 ? other.Energy - energyLoss : 5;
                string msg = WhoAmI + " attacked and was killed by " + other.WhoAmI + ".";
                Logger.OutputToLog(msg, Logger.LogLevel.Message);
                CritterWorldForm.AddMarqueeMessage(msg, Color.DarkRed);
                Die("Killed by " + other.WhoAmI);
                return(EnumAttackResult.IDied);
            }
        }