Esempio n. 1
0
 /// <summary>
 /// Dispatches an event for a robot.
 /// </summary>
 /// <remarks>
 /// Too old events will not be dispatched and a critical event is always dispatched.
 /// </remarks>
 /// <param name="evnt">is the event to dispatch to the robot.</param>
 private void Dispatch(Event evnt)
 {
     if (robot != null && evnt != null)
     {
         try
         {
             // skip too old events
             if ((evnt.Time > Time - MAX_EVENT_STACK) || HiddenAccessN.IsCriticalEvent(evnt))
             {
                 HiddenAccessN.Dispatch(evnt, robot, robotProxy.getStatics(), robotProxy.getGraphicsImpl());
             }
         }
         catch (Exception ex)
         {
             if (ex is SecurityException)
             {
                 robotProxy.punishSecurityViolation(robotProxy.getStatics().getName() + " " + ex.Message);
             }
             else if (ex.InnerException is SecurityException)
             {
                 robotProxy.punishSecurityViolation(robotProxy.getStatics().getName() + " " + ex.InnerException + ": " + ex.Message);
             }
             else if (!(ex is EventInterruptedException || ex is AbortedException || ex is DeathException || ex is DisabledException || ex is WinException))
             {
                 robotProxy.println("SYSTEM: " + ex.GetType().Name + " occurred on " + evnt.GetType().Name);
                 robotProxy.GetOut().WriteLine(ex.StackTrace);
             }
             throw;
         }
     }
 }
Esempio n. 2
0
            public object deserialize(RbSerializerN serializer, ByteBuffer buffer)
            {
                bool        isJuniorRobot      = serializer.deserializeBoolean(buffer);
                bool        isInteractiveRobot = serializer.deserializeBoolean(buffer);
                bool        isPaintRobot       = serializer.deserializeBoolean(buffer);
                bool        isAdvancedRobot    = serializer.deserializeBoolean(buffer);
                bool        isTeamRobot        = serializer.deserializeBoolean(buffer);
                bool        isTeamLeader       = serializer.deserializeBoolean(buffer);
                bool        isDroid            = serializer.deserializeBoolean(buffer);
                string      name           = serializer.deserializeString(buffer);
                string      shortName      = serializer.deserializeString(buffer);
                string      veryShortName  = serializer.deserializeString(buffer);
                string      fullClassName  = serializer.deserializeString(buffer);
                string      shortClassName = serializer.deserializeString(buffer);
                BattleRules battleRules    = HiddenAccessN.createRules(
                    serializer.deserializeInt(buffer),
                    serializer.deserializeInt(buffer),
                    serializer.deserializeInt(buffer),
                    serializer.deserializeDouble(buffer),
                    serializer.deserializeLong(buffer)
                    );

                var    teammates = new List <string>();
                object item      = serializer.deserializeString(buffer);

                while (item != null)
                {
                    if (item is string)
                    {
                        teammates.Add((string)item);
                    }
                    item = serializer.deserializeString(buffer);
                }

                string teamName        = serializer.deserializeString(buffer);
                int    index           = serializer.deserializeInt(buffer);
                int    contestantIndex = serializer.deserializeInt(buffer);


                return(new RobotStatics(
                           isJuniorRobot,
                           isInteractiveRobot,
                           isPaintRobot,
                           isAdvancedRobot,
                           isTeamRobot,
                           isTeamLeader,
                           isDroid,
                           name,
                           shortName,
                           veryShortName,
                           fullClassName,
                           shortClassName,
                           battleRules,
                           teammates.ToArray(),
                           teamName,
                           index,
                           contestantIndex
                           ));
            }
Esempio n. 3
0
 /// <summary>
 /// Adds an event to the event queue.
 /// </summary>
 /// <param name="evnt">is the event to add to the event queue.</param>
 public void Add(Event evnt)
 {
     if (!HiddenAccessN.IsCriticalEvent(evnt))
     {
         int priority = GetEventPriority(evnt.GetType().Name);
         HiddenAccessN.SetEventPriority(evnt, priority);
     }
     AddImpl(evnt);
 }
Esempio n. 4
0
        public static void InitN()
        {
            System.Threading.Thread.CurrentThread.CurrentCulture   = CultureInfo.InvariantCulture;
            System.Threading.Thread.CurrentThread.CurrentUICulture = CultureInfo.InvariantCulture;
            HiddenAccessN.init();
            LoggerN.IsSafeThread = true;
            LoggerN.setLogListener(new Logger(true));
            HiddenAccessN.randomHelper = new RandomFactory(true);

            var versionManager =
                Bridge.Cast <IVersionManagerBase>(ContainerBase.getComponent(IVersionManagerBase_._class));
            int currentVersion = versionManager.getVersionAsInt();

            RbSerializer.Init(currentVersion);
            RbSerializerN.Init(currentVersion);

            RbSerializerN.register(typeof(RobotStatus), RbSerializerN.RobotStatus_TYPE);
            RbSerializerN.register(typeof(BattleResults), RbSerializerN.BattleResults_TYPE);
            RbSerializerN.register(typeof(Bullet), RbSerializerN.Bullet_TYPE);
            RbSerializerN.register(typeof(RobotStatics), RbSerializerN.RobotStatics_TYPE);

            // events
            RbSerializerN.register(typeof(RoundEndedEvent), RbSerializerN.RoundEndedEvent_TYPE);
            RbSerializerN.register(typeof(BattleEndedEvent), RbSerializerN.BattleEndedEvent_TYPE);
            RbSerializerN.register(typeof(BulletHitBulletEvent), RbSerializerN.BulletHitBulletEvent_TYPE);
            RbSerializerN.register(typeof(BulletHitEvent), RbSerializerN.BulletHitEvent_TYPE);
            RbSerializerN.register(typeof(BulletMissedEvent), RbSerializerN.BulletMissedEvent_TYPE);
            RbSerializerN.register(typeof(DeathEvent), RbSerializerN.DeathEvent_TYPE);
            RbSerializerN.register(typeof(WinEvent), RbSerializerN.WinEvent_TYPE);
            RbSerializerN.register(typeof(HitWallEvent), RbSerializerN.HitWallEvent_TYPE);
            RbSerializerN.register(typeof(RobotDeathEvent), RbSerializerN.RobotDeathEvent_TYPE);
            RbSerializerN.register(typeof(SkippedTurnEvent), RbSerializerN.SkippedTurnEvent_TYPE);
            RbSerializerN.register(typeof(ScannedRobotEvent), RbSerializerN.ScannedRobotEvent_TYPE);
            RbSerializerN.register(typeof(HitByBulletEvent), RbSerializerN.HitByBulletEvent_TYPE);
            RbSerializerN.register(typeof(HitRobotEvent), RbSerializerN.HitRobotEvent_TYPE);
            RbSerializerN.register(typeof(KeyPressedEvent), RbSerializerN.KeyPressedEvent_TYPE);
            RbSerializerN.register(typeof(KeyReleasedEvent), RbSerializerN.KeyReleasedEvent_TYPE);
            RbSerializerN.register(typeof(KeyTypedEvent), RbSerializerN.KeyTypedEvent_TYPE);
            RbSerializerN.register(typeof(MouseClickedEvent), RbSerializerN.MouseClickedEvent_TYPE);
            RbSerializerN.register(typeof(MouseDraggedEvent), RbSerializerN.MouseDraggedEvent_TYPE);
            RbSerializerN.register(typeof(MouseEnteredEvent), RbSerializerN.MouseEnteredEvent_TYPE);
            RbSerializerN.register(typeof(MouseExitedEvent), RbSerializerN.MouseExitedEvent_TYPE);
            RbSerializerN.register(typeof(MouseMovedEvent), RbSerializerN.MouseMovedEvent_TYPE);
            RbSerializerN.register(typeof(MousePressedEvent), RbSerializerN.MousePressedEvent_TYPE);
            RbSerializerN.register(typeof(MouseReleasedEvent), RbSerializerN.MouseReleasedEvent_TYPE);
            RbSerializerN.register(typeof(MouseWheelMovedEvent), RbSerializerN.MouseWheelMovedEvent_TYPE);

            RbSerializerN.register(typeof(ExecCommands), RbSerializerN.ExecCommands_TYPE);
            RbSerializerN.register(typeof(BulletCommand), RbSerializerN.BulletCommand_TYPE);
            RbSerializerN.register(typeof(TeamMessage), RbSerializerN.TeamMessage_TYPE);
            RbSerializerN.register(typeof(DebugProperty), RbSerializerN.DebugProperty_TYPE);
            RbSerializerN.register(typeof(ExecResults), RbSerializerN.ExecResults_TYPE);
            RbSerializerN.register(typeof(BulletStatus), RbSerializerN.BulletStatus_TYPE);
        }
Esempio n. 5
0
        /// <summary>
        /// Registers the full and simple class name of the specified event and sets the default
        /// priority of the event class.
        /// </summary>
        /// <param name="evnt">an event belonging to the event class to register the class name for etc.</param>
        private void registerEventNames(Event evnt)
        {
            if (!HiddenAccessN.IsCriticalEvent(evnt))
            {
                HiddenAccessN.SetDefaultPriority(evnt);
            }
            Type type = evnt.GetType();

            eventNames.Add(type.FullName, evnt); // full name with package name
            eventNames.Add(type.Name, evnt);     // only the class name
        }
Esempio n. 6
0
        public void clear(long clearTime)
        {
            for (int i = 0; i < Count; i++)
            {
                Event e = this[i];

                if ((e.Time <= clearTime) && !HiddenAccessN.IsCriticalEvent(e))
                {
                    RemoveAt(i--);
                }
            }
        }
Esempio n. 7
0
 private static int CheckCount()
 {
     lock (syncRoot)
     {
         if (runningCounter > 5)
         {
             string message = "Preventing " + HiddenAccessN.GetRobotName() + "from thread creation. You may only create 5 threads at same time.";
             LoggerN.logError(message);
             LoggerN.WriteLineToRobotsConsole(message);
             throw new AccessViolationException(message);
         }
         runningCounter++;
         return(runningCounter);
     }
 }
Esempio n. 8
0
 public static void ForceStopThread()
 {
     try
     {
         LoggerN.logMessage(HiddenAccessN.GetRobotName() + " is not stopping.  Forcing a stop.");
         robotThread.Priority = ThreadPriority.Lowest;
         robotThread.Abort();
         robotThread.Interrupt();
     }
     catch (Exception ex)
     {
         LoggerN.logError(ex);
         throw;
     }
 }
Esempio n. 9
0
 /// <summary>
 /// Internal method for adding an event to the event queue.
 /// </summary>
 /// <param name="evnt">is the event to add to the event queue.</param>
 private void AddImpl(Event evnt)
 {
     if (eventQueue != null)
     {
         if (eventQueue.Count > MAX_QUEUE_SIZE)
         {
             robotProxy.println(
                 "Not adding to " + robotProxy.getStatics().getName() + "'s queue, exceeded " + MAX_QUEUE_SIZE
                 + " events in queue.");
         }
         else
         {
             HiddenAccessN.SetEventTime(evnt, Time);
             eventQueue.Add(evnt);
         }
     }
 }
Esempio n. 10
0
        public void clear(bool includingSystemEvents)
        {
            if (includingSystemEvents)
            {
                Clear();
                return;
            }

            for (int i = 0; i < Count; i++)
            {
                Event e = this[i];

                if (!HiddenAccessN.IsCriticalEvent(e))
                {
                    RemoveAt(i--);
                }
            }
        }
Esempio n. 11
0
        /// <summary>
        /// Sets the event priority of events belonging to a specific class.
        /// </summary>
        /// <param name="eventClass">
        /// is a string with the full class name of the event type to set the priority for.</param>
        /// <param name="priority">is the new priority</param>
        public void SetEventPriority(string eventClass, int priority)
        {
            if (eventClass == null)
            {
                return;
            }
            Event evnt;

            if (!eventNames.TryGetValue(eventClass, out evnt))
            {
                robotProxy.println("SYSTEM: Unknown event class: " + eventClass);
                return;
            }
            if (HiddenAccessN.IsCriticalEvent(evnt))
            {
                robotProxy.println("SYSTEM: You may not change the priority of a system event.");
            }
            HiddenAccessN.SetEventPriority(evnt, priority);
        }
Esempio n. 12
0
 public int sizeOf(Event evnt)
 {
     return(sizeOf(HiddenAccessN.GetSerializationType(evnt), evnt));
 }
Esempio n. 13
0
        public void serialize(ByteBuffer buffer, Event evnt)
        {
            byte type = HiddenAccessN.GetSerializationType(evnt);

            serialize(buffer, type, evnt);
        }
Esempio n. 14
0
            public object deserialize(RbSerializerN serializer, ByteBuffer buffer)
            {
                string      robocodeVersion    = serializer.deserializeString(buffer);
                bool        isJuniorRobot      = serializer.deserializeBoolean(buffer);
                bool        isInteractiveRobot = serializer.deserializeBoolean(buffer);
                bool        isPaintRobot       = serializer.deserializeBoolean(buffer);
                bool        isAdvancedRobot    = serializer.deserializeBoolean(buffer);
                bool        isTeamRobot        = serializer.deserializeBoolean(buffer);
                bool        isTeamLeader       = serializer.deserializeBoolean(buffer);
                bool        isDroid            = serializer.deserializeBoolean(buffer);
                bool        isSentryRobot      = serializer.deserializeBoolean(buffer);
                string      name           = serializer.deserializeString(buffer);
                string      shortName      = serializer.deserializeString(buffer);
                string      veryShortName  = serializer.deserializeString(buffer);
                string      fullClassName  = serializer.deserializeString(buffer);
                string      shortClassName = serializer.deserializeString(buffer);
                BattleRules battleRules    = HiddenAccessN.createRules(
                    serializer.deserializeInt(buffer),     // BattlefieldWidth
                    serializer.deserializeInt(buffer),     // BattlefieldHeight
                    serializer.deserializeInt(buffer),     // NumRounds
                    serializer.deserializeDouble(buffer),  // GunCoolingRate
                    serializer.deserializeLong(buffer),    // InactivityTime
                    serializer.deserializeBoolean(buffer), // HideEnemyNames
                    serializer.deserializeInt(buffer)      // SentryBorderSize
                    );

                var    teammates = new List <string>();
                object item      = serializer.deserializeString(buffer);

                while (item != null)
                {
                    if (item is string)
                    {
                        teammates.Add((string)item);
                    }
                    item = serializer.deserializeString(buffer);
                }

                string teamName        = serializer.deserializeString(buffer);
                int    index           = serializer.deserializeInt(buffer);
                int    contestantIndex = serializer.deserializeInt(buffer);

                return(new RobotStatics(
                           robocodeVersion,
                           isJuniorRobot,
                           isInteractiveRobot,
                           isPaintRobot,
                           isAdvancedRobot,
                           isTeamRobot,
                           isTeamLeader,
                           isDroid,
                           isSentryRobot,
                           name,
                           shortName,
                           veryShortName,
                           fullClassName,
                           shortClassName,
                           battleRules,
                           teammates.ToArray(),
                           teamName,
                           index,
                           contestantIndex
                           ));
            }
Esempio n. 15
0
        protected override sealed void executeImpl()
        {
            if (execResults == null)
            {
                // this is to slow down undead robot after cleanup, from fast exception-loop
                Thread.Sleep(1000);
            }

            // Entering tick
            if (testingCondition)
            {
                throw new RobotException(
                          "You cannot take action inside Condition.Test().  You should handle OnCustomEvent instead.");
            }

            setSetCallCount(0);
            setGetCallCount(0);

            // This stops autoscan from scanning...
            if (waitCondition != null && waitCondition.Test())
            {
                waitCondition = null;
                commands.setScan(true);
            }

            commands.setOutputText(GetOutTextAndReset());
            commands.setGraphicsCalls(graphicsProxy.readoutQueuedCalls());

            // call server
            SerializeCommands();
            peer.executeImplSerial();
            DeserializeResults();

            if (execResults == null)
            {
                throw new InvalidOperationException();
            }

            updateStatus(execResults.getCommands(), execResults.getStatus());

            graphicsProxy.setPaintingEnabled(execResults.isPaintEnabled());
            firedEnergy = 0;
            firedHeat   = 0;

            // add new events
            eventManager.Add(new StatusEvent(execResults.getStatus()));
            if (statics.IsPaintRobot() && execResults.isPaintEnabled())
            {
                // Add paint event, if robot is a paint robot and its painting is enabled
                eventManager.Add(new PaintEvent());
            }

            // add other events
            if (execResults.getEvents() != null)
            {
                foreach (Event evnt in execResults.getEvents())
                {
                    eventManager.Add(evnt);
                    HiddenAccessN.UpdateBullets(evnt, bullets);
                }
            }

            if (execResults.getBulletUpdates() != null)
            {
                foreach (BulletStatus bulletStatus in execResults.getBulletUpdates())
                {
                    Bullet bullet;
                    if (bullets.TryGetValue(bulletStatus.bulletId, out bullet))
                    {
                        HiddenAccessN.Update(bullet, bulletStatus.x, bulletStatus.y, bulletStatus.victimName, bulletStatus.isActive);
                        if (!bulletStatus.isActive)
                        {
                            bullets.Remove(bulletStatus.bulletId);
                        }
                    }
                }
            }

            // add new team messages
            loadTeamMessages(execResults.getTeamMessages());

            eventManager.ProcessEvents();
        }