private SimulationStep GetNextSimulationStep()
            {
                SimulationStep ss = null;

                lock (LockNextSimulationStep)
                {
                    ss = NextSimulationStep;
                    NextSimulationStep = new SimulationStep();
                }
                if (ss == null || NextSimulationStep == null)
                {
                    System.Diagnostics.Debugger.Break();
                }
                return(ss);

                //do
                //{
                //    lock (SimulationStepQueue)
                //    {
                //        if (SimulationStepQueue.Count > 0)
                //            ss = SimulationStepQueue.Dequeue();
                //        if (SimulationStepQueue.Count == 0)
                //            SimulationQueueNotEmpty.Reset();
                //    }
                //    if (ss != null)
                //        return ss;
                //    else
                //        SimulationQueueNotEmpty.WaitOne(Timeout.Infinite);
                //} while (true);
            }
        private void UseSwapTool(Vector2Int from, Vector2Int to)
        {
            boardRenderer.StopIndicatingSwap(from);

            // TODO: Indicate this error to the user
            if (toolbox.GetAvailableUses(ActiveTool) == 0)
            {
                return;
            }

            try
            {
                SimulationStep step = toolbox.UseSwapTool(ActiveTool, from, to);

                sessionMetrics.RegisterToolUse(ActiveTool);

                KickOffAnimation(step);
                ActiveTool = Tool.ToggleMarked;
                ActiveToolChanged?.Invoke(ActiveTool);

                UpdateUI();
            }
            catch (InvalidOperationException)
            {
                // TODO: Indicate error to the user
            }
        }
Exemple #3
0
        public void TestGetCurrentStep()
        {
            SimulationStep simulationStep = ariesApiHelper.GetCurrentStep();

            Assert.IsNotNull(simulationStep);
            Assert.AreEqual(7, simulationStep.simulation_result.Count);
        }
        public SimulationStep UseTool(Tool tool, Vector2Int gridPos, RotationSense rotSense = RotationSense.CW)
        {
            switch (tool)
            {
            case Tool.SwapTiles:
            case Tool.SwapLines:
                throw new ArgumentOutOfRangeException(nameof(tool));
            }

            if (availableToolUses[tool] == 0)
            {
                throw new InvalidOperationException("No tool uses available.");
            }

            SimulationStep step = null;

            switch (tool)
            {
            case Tool.ToggleMarked:
                step = simulator.TogglePrediction(gridPos); break;

            case Tool.RemoveTile:
                step = simulator.RemoveTile(gridPos); break;

            case Tool.RefillInert:
                step = simulator.RefillTile(gridPos); break;

            case Tool.Bomb:
                step = simulator.RemoveBlock(gridPos); break;

            case Tool.PlusBomb:
                step = simulator.RemovePlus(gridPos); break;

            case Tool.RemoveRow:
                step = simulator.RemoveRow(gridPos.y); break;

            case Tool.RemoveColor:
                step = simulator.RemoveColor(gridPos); break;

            case Tool.Rotate3x3:
                step = simulator.Rotate3x3Block(gridPos, rotSense); break;

            case Tool.CreateWildcard:
                step = simulator.CreateWildcard(gridPos); break;
            }

            if (availableToolUses[tool] > 0)
            {
                --availableToolUses[tool];
            }

            if (tool != Tool.ToggleMarked)
            {
                toolUsedThisTurn = true;
            }

            return(step);
        }
        private async void KickOffAnimation(SimulationStep step)
        {
            await boardRenderer.AnimateSimulationStepAsync(step);

            if (!AnyToolsAvailable())
            {
                LastToolUsed?.Invoke();
            }
        }
Exemple #6
0
        public void UseSwapTool_returns_simulation_step(Tool tool, Type stepType)
        {
            toolData.Map[tool].InitialUses = 1;

            Toolbox toolbox = CreateToolbox();

            SimulationStep step = toolbox.UseSwapTool(tool, Vector2Int.one, new Vector2Int(2, 1));

            Assert.That(step, Is.TypeOf(stepType));
        }
Exemple #7
0
        public void TestSimulationStep()
        {
            Assert.IsTrue(isServerExists);
            SimulationStep simulationStep = this.ariesApi.GetSimulationStep();

            Assert.IsNotNull(simulationStep.simulation_result, "Simulation result is not initialized");
            Assert.AreEqual(7, simulationStep.simulation_result.Count);
            Assert.IsNotNull(simulationStep.agents_states, "Agent states is not initialized");
            Assert.AreEqual(2, simulationStep.agents_states.Count);
        }
            private SimulationStep GetNextSimulationStep()
            {
                SimulationStep ss = null;

                lock (LockNextSimulationStep)
                {
                    ss = NextSimulationStep;
                    NextSimulationStep = new SimulationStep();
                }
                return(ss);
            }
Exemple #9
0
        private async Task PlayBackReactionAsync()
        {
            while (true)
            {
                SimulationStep step = simulator.SimulateNextStep();

                sessionMetrics.RegisterSimulationStep(step);

                scoreKeeper.ScoreStep(step, simulator.DifficultyLevel);
                // We might need to tie this into the board renderer
                // to sync the update with the match animation.
                scoreRenderer.UpdateScore();

                if (step is MatchStep matchStep)
                {
                    await boardManipulator.RewardMatches(matchStep);

                    if (gameEnded)
                    {
                        break;
                    }
                }
                else if (step is CleanUpStep cleanUpStep)
                {
                    if (cleanUpStep.InertTiles.Count(tile => tile.Marked) > 0)
                    {
                        await ShowIncorrectPredictionsTutorialAsync(cleanUpStep.InertTiles);

                        if (gameEnded)
                        {
                            break;
                        }
                    }

                    if (cleanUpStep.CrackedTiles.Count > 0)
                    {
                        await ShowCrackedVialsTutorialAsync(cleanUpStep.CrackedTiles);

                        if (gameEnded)
                        {
                            break;
                        }
                    }
                }

                await boardRenderer.AnimateSimulationStepAsync(step);

                if (step.FinalStep || gameEnded)
                {
                    break;
                }
            }
        }
        public int ScoreStep(SimulationStep step, int level)
        {
            int stepScore = 0;

            switch (step)
            {
            case MatchStep matchStep: stepScore = ScoreMatchStep(matchStep, level); break;

            case CleanUpStep cleanUpStep: stepScore = ScoreCleanUpStep(cleanUpStep); break;
            }

            Score += stepScore;

            return(stepScore);
        }
        private void ExecuteQueuedFunctionCalls(SimulationStep stepInfo)
        {
            // no locks required since this is run on an instance that has already been dequeued from the main list

            //Program.DebugOutput("Executing queued object function calls (" + queuedObjectFunctionCalls.Count + ")");
            foreach (var fn in stepInfo.QueuedObjectFunctionCalls)
            {
                fn.Invoke();
            }

            //Program.DebugOutput("Executing queued function calls (" + queuedFunctionCalls.Count + ")");
            foreach (var fn in stepInfo.QueuedFunctionCalls)
            {
                fn.Invoke();
            }
        }
        private void ExecuteQueuedFunctionCalls(SimulationStep stepInfo)
        {       // no locks required since this method is run on an instance that has already been dequeued from the main list
            //foreach (var fn in stepInfo.QueuedObjectFunctionCalls)
            //    fn.Invoke();

            foreach (var fn in stepInfo.QueuedFunctionCalls)
            {
                fn.Invoke();
            }

            // NOTE: Having just one list here might cause strange effects should the physics thread lag behind
            foreach (var o in All)
            {
                ((ThreadObjectProxy)o).CleanAllEvents();
            }
        }
        public SimulationStep UseSwapTool(Tool tool, Vector2Int from, Vector2Int to)
        {
            switch (tool)
            {
            case Tool.ToggleMarked:
            case Tool.RemoveTile:
            case Tool.RefillInert:
            case Tool.Bomb:
            case Tool.PlusBomb:
            case Tool.RemoveRow:
            case Tool.RemoveColor:
            case Tool.Rotate3x3:
            case Tool.CreateWildcard:
                throw new ArgumentOutOfRangeException(nameof(tool));
            }

            if (availableToolUses[tool] == 0)
            {
                throw new InvalidOperationException("No tool uses available.");
            }

            SimulationStep step = null;

            switch (tool)
            {
            case Tool.SwapTiles:
                step = simulator.SwapTiles(from, to); break;

            case Tool.SwapLines:
                step = from.x == to.x
                    ? simulator.SwapRows(from.y, to.y)
                    : simulator.SwapColumns(from.x, to.x);
                break;
            }

            if (availableToolUses[tool] > 0)
            {
                --availableToolUses[tool];
            }

            toolUsedThisTurn = true;

            return(step);
        }
Exemple #14
0
        public void RegisterSimulationStep(SimulationStep step)
        {
            switch (step)
            {
            case MatchStep matchStep:
                if (matchStep.ChainLength > MaxChainLength)
                {
                    MaxChainLength = matchStep.ChainLength;
                }

                if (matchStep.MatchedTiles.Count > MaxMatchSize)
                {
                    MaxMatchSize = matchStep.MatchedTiles.Count;
                }
                break;

            case CleanUpStep cleanUpStep:
                MistakeCount += cleanUpStep.InertTiles.Count(tile => tile.Marked);
                break;
            }
        }
        public async Task AnimateSimulationStepAsync(SimulationStep step)
        {
            switch (step)
            {
            case MatchStep matchStep: await AnimateMatchStepAsync(matchStep); break;

            case CleanUpStep cleanUpStep: await AnimateCleanUpStepAsync(cleanUpStep); break;

            case RemovalStep removalStep: await AnimateRemovalStepAsync(removalStep); break;

            case PermutationStep permutationStep: await AnimatePermutationStepAsync(permutationStep); break;

            case RotationStep rotationStep: await AnimateRotationStepAsync(rotationStep); break;

            case PredictionStep predictionStep: await AnimatePredictionStepAsync(predictionStep); break;

            case RefillStep refillStep: await AnimateRefillStepAsync(refillStep); break;

            case WildcardStep wildcardStep: await AnimateWildcardStepAsync(wildcardStep); break;
            }
        }
Exemple #16
0
    private void Plan()
    {
        for (int i = 0; i < goals.Count; i++)
        {
            if (goals[i].isValid(this))
            {
                goal = goals[i];
                break;
            }
        }

        if (worldState.CompareState(goal.goalStates) == 0)
        {
            return;
        }

        currentPlan.Clear();
        Stack <SimulationStep> sim = new Stack <SimulationStep>();

        GOAP_Action[] simPlan = new GOAP_Action[maxPlanDepth];

        int minDepth = int.MaxValue;

        sim.Push(new SimulationStep(new GOAP_StatesList(worldState), null, 0));

        List <GOAP_States> targetStates = new List <GOAP_States>(goal.goalStates.states);

        while (sim.Count != 0)
        {
            currentSimData = sim.Pop();
            simPlan[currentSimData.depth] = currentSimData.action;

            if (currentSimData.depth > minDepth)
            {
                continue;
            }

            if (currentSimData.worldState.CompareState(goal.goalStates) == 0 || currentSimData.depth >= maxPlanDepth)
            {
                if (currentSimData.depth < minDepth)
                {
                    currentPlan.Clear();
                    for (int i = 0; i <= currentSimData.depth; i++)
                    {
                        if (simPlan[i] != null)
                        {
                            if (!currentPlan.Contains(simPlan[i]))
                            {
                                currentPlan.Enqueue(simPlan[i]);
                            }
                        }
                    }
                    minDepth = currentSimData.depth;
                }
            }
            else
            {
                for (int i = 0; i < actions.Count; i++)
                {
                    if (actions[i].isValid(this))
                    {
                        GOAP_StatesList newSimState = new GOAP_StatesList(currentSimData.worldState);
                        newSimState.AddStates(actions[i].resultStates);
                        sim.Push(new SimulationStep(newSimState, actions[i], (currentSimData.depth + 1)));
                    }
                }
            }
        }
    }
 public void TestAgent()
 {
     SimulationStep simulationStep = this.ariesApi.GetSimulationStep();
     List <Agent>   agents         = ariesApi.GetAgents(simulationStep._id);
 }
 public PhysicsWorker()
 {
     NextSimulationStep = new SimulationStep();
     SimulationRunning  = new ManualResetEvent(true);
     SimulationKilled   = new ManualResetEvent(false);
 }
        private void ExecuteQueuedFunctionCalls(SimulationStep stepInfo)
        {
            // no locks required since this is run on an instance that has already been dequeued from the main list

            //Program.DebugOutput("Executing queued object function calls (" + queuedObjectFunctionCalls.Count + ")");
            foreach (var fn in stepInfo.QueuedObjectFunctionCalls)
                fn.Invoke();

            //Program.DebugOutput("Executing queued function calls (" + queuedFunctionCalls.Count + ")");
            foreach (var fn in stepInfo.QueuedFunctionCalls)
                fn.Invoke();
        }
 public PhysicsWorker()
 {
     NextSimulationStep = new SimulationStep();
 }
            private SimulationStep GetNextSimulationStep()
            {
                SimulationStep ss = null;
                lock (LockNextSimulationStep)
                {
                    ss = NextSimulationStep;
                    NextSimulationStep = new SimulationStep();
                }
                if (ss == null || NextSimulationStep == null)
                    System.Diagnostics.Debugger.Break();
                return ss;

                //do
                //{
                //    lock (SimulationStepQueue)
                //    {
                //        if (SimulationStepQueue.Count > 0)
                //            ss = SimulationStepQueue.Dequeue();
                //        if (SimulationStepQueue.Count == 0)
                //            SimulationQueueNotEmpty.Reset();
                //    }
                //    if (ss != null)
                //        return ss;
                //    else
                //        SimulationQueueNotEmpty.WaitOne(Timeout.Infinite);
                //} while (true);
            }
 public PhysicsWorker()
 {
     NextSimulationStep = new SimulationStep();
 }
        public void TestSimulationStep()
        {
            SimulationStep simulationStep = this.ariesApi.GetSimulationStep();

            Assert.AreEqual(Math.Round(simulationStep.simulation_result["B0"].real, 3),
                            Math.Round(10.736046501091344, 3));
            Assert.AreEqual(Math.Round(simulationStep.simulation_result["B0"].imag, 3),
                            Math.Round(-0.03351327202899204, 3));

            Assert.AreEqual(Math.Round(simulationStep.simulation_result["B1"].real, 3),
                            Math.Round(4.298619745304598, 3));
            Assert.AreEqual(Math.Round(simulationStep.simulation_result["B1"].imag, 3),
                            Math.Round(-0.012355199796137507, 3));

            Assert.AreEqual(Math.Round(simulationStep.simulation_result["B2"].real, 3),
                            Math.Round(6.437426755786747, 3));
            Assert.AreEqual(Math.Round(simulationStep.simulation_result["B2"].imag, 3),
                            Math.Round(-0.02115807223285453, 3));

            Assert.AreEqual(Math.Round(simulationStep.simulation_result["AGENT0"].real, 3),
                            Math.Round(4.298619745304598, 3));
            Assert.AreEqual(Math.Round(simulationStep.simulation_result["AGENT0"].imag, 3),
                            Math.Round(-0.012355199796137507, 3));

            Assert.AreEqual(Math.Round(simulationStep.simulation_result["AGENT1"].real, 3),
                            Math.Round(6.437426755786747, 3));
            Assert.AreEqual(Math.Round(simulationStep.simulation_result["AGENT1"].imag, 3),
                            Math.Round(-0.02115807223285453, 3));

            Assert.AreEqual(Math.Round(simulationStep.simulation_result["power_from_main"].real, 3),
                            Math.Round(2469.2906952510093, 3));
            Assert.AreEqual(Math.Round(simulationStep.simulation_result["power_from_main"].imag, 3),
                            Math.Round(7.70805256666817, 3));

            Assert.AreEqual(Math.Round(simulationStep.simulation_result["distribution_loss"].real, 3),
                            Math.Round(30.306661228036212, 3));
            Assert.AreEqual(Math.Round(simulationStep.simulation_result["distribution_loss"].imag, 3),
                            Math.Round(0.0, 3));

            Assert.IsNotNull(simulationStep.agents_states, "Agent states is not initialized");

            Assert.AreEqual(Math.Round(simulationStep.agents_states["AGENT0"].impedance.resistance, 3),
                            Math.Round(52.9, 3));
            Assert.AreEqual(Math.Round(simulationStep.agents_states["AGENT0"].impedance.reactance, 3),
                            Math.Round(12.1231233, 3));

            Assert.AreEqual(Math.Round(simulationStep.agents_states["AGENT0"].inject_power.active_power, 3),
                            Math.Round(433.1234, 3));
            Assert.AreEqual(Math.Round(simulationStep.agents_states["AGENT0"].inject_power.reactive_power, 3),
                            Math.Round(1234.12344, 3));

            Assert.AreEqual(Math.Round(simulationStep.agents_states["AGENT0"].demand_power.active_power, 3),
                            Math.Round(344.234, 3));
            Assert.AreEqual(Math.Round(simulationStep.agents_states["AGENT0"].demand_power.reactive_power, 3),
                            Math.Round(2345.6236, 3));

            Assert.AreEqual(Math.Round(simulationStep.agents_states["AGENT0"].battery_power.active_power, 3),
                            Math.Round(-236.65, 3));
            Assert.AreEqual(Math.Round(simulationStep.agents_states["AGENT0"].battery_power.reactive_power, 3),
                            Math.Round(3565.656256, 3));


            Assert.AreEqual(Math.Round(simulationStep.agents_states["AGENT1"].impedance.resistance, 3),
                            Math.Round(152.9, 3));
            Assert.AreEqual(Math.Round(simulationStep.agents_states["AGENT1"].impedance.reactance, 3),
                            Math.Round(112.1231233, 3));

            Assert.AreEqual(Math.Round(simulationStep.agents_states["AGENT1"].inject_power.active_power, 3),
                            Math.Round(1433.1234, 3));
            Assert.AreEqual(Math.Round(simulationStep.agents_states["AGENT1"].inject_power.reactive_power, 3),
                            Math.Round(11234.12344, 3));

            Assert.AreEqual(Math.Round(simulationStep.agents_states["AGENT1"].demand_power.active_power, 3),
                            Math.Round(1344.234, 3));
            Assert.AreEqual(Math.Round(simulationStep.agents_states["AGENT1"].demand_power.reactive_power, 3),
                            Math.Round(12345.6236, 3));

            Assert.AreEqual(Math.Round(simulationStep.agents_states["AGENT1"].battery_power.active_power, 3),
                            Math.Round(-1236.65, 3));
            Assert.AreEqual(Math.Round(simulationStep.agents_states["AGENT1"].battery_power.reactive_power, 3),
                            Math.Round(13565.656256, 3));
        }
Exemple #24
0
 public void TestAgent()
 {
     Assert.IsTrue(isServerExists);
     SimulationStep simulationStep = this.ariesApi.GetSimulationStep();
     List <Agent>   agents         = ariesApi.GetAgents(simulationStep._id);
 }