public void InitializeGeneration()
        {
#if UNITY_EDITOR
            UnityEditor.Undo.RecordObject(this.gameObject, "Intialize Generation");
            UnityEditor.Undo.RecordObject(this, "Intialize Generation");
            UnityEditor.Undo.RecordObject(patternView, "Intialize Generation");
            UnityEditor.PrefabUtility.RecordPrefabInstancePropertyModifications(this.gameObject);
            UnityEditor.PrefabUtility.RecordPrefabInstancePropertyModifications(this);
            UnityEditor.PrefabUtility.RecordPrefabInstancePropertyModifications(patternView);
#endif
            if (increaseSeed)
            {
                seed++;
            }

            patternView.pattern = new Pattern(startSize, Tile.Empty);
            ReplacementEngine re = GetComponent <ReplacementEngine>();
            re.ResetGeneration(seed);
            PatternView.UpdateView();

            ParseInstructions();
            currentInstruction = 0;
            applicationCounter = 0;
            gotoStack          = new Stack <ProgramState>();
        }
예제 #2
0
        public void GenerateStep()
        {
#if UNITY_EDITOR
            UnityEditor.Undo.RecordObject(this.gameObject, "Generation Step");
            UnityEditor.Undo.RecordObject(this, "Generation Step");
            UnityEditor.Undo.RecordObject(patternView, "Generation Step");
            UnityEditor.PrefabUtility.RecordPrefabInstancePropertyModifications(this.gameObject);
            UnityEditor.PrefabUtility.RecordPrefabInstancePropertyModifications(this);
            UnityEditor.PrefabUtility.RecordPrefabInstancePropertyModifications(patternView);
#endif

            ReplacementEngine re = GetComponent <ReplacementEngine>();
            re.ReplaceMatch();
            PatternView.UpdateView();
        }
예제 #3
0
        public void Generate()
        {
            ReplacementEngine re = GetComponent <ReplacementEngine>();

            InitializeGeneration();

            if (re != null)
            {
                for (int i = 0; i < maxGenerationSteps; i++)
                {
                    if (!re.ReplaceMatch())
                    {
                        break;
                    }
                }
            }
        }
        public void Generate()
        {
            ReplacementEngine re = GetComponent <ReplacementEngine>();

            InitializeGeneration();

// #if UNITY_EDITOR
//          UnityEditor.Undo.RecordObject(this.gameObject, "Generate Dungeon");
//          UnityEditor.Undo.RecordObject(patternView, "Generate Dungeon");
//          UnityEditor.Undo.RecordObject(this, "Generate Dungeon");
//          UnityEditor.PrefabUtility.RecordPrefabInstancePropertyModifications(this.gameObject);
// #endif

            if (re != null)
            {
                for (int i = 0; i < maxGenerationSteps; i++)
                {
                    if (GenerationStepInternal() == GenerationStepResult.EndOfFile)
                    {
                        break;
                    }
                }
            }
        }
        private GenerationStepResult GenerationStepInternal()
        {
            ReplacementEngine re = GetComponent <ReplacementEngine>();

            if (printDebugInfo)
            {
                Debug.Log("Generation step, instruction " + currentInstruction + ", applications left: " + applicationCounter);
            }

            if (currentInstruction >= instructions.Length)
            {
                return(GenerationStepResult.EndOfFile);
            }

            bool        visibleChange = false;
            Instruction instr         = instructions[currentInstruction];

            switch (instr.type)
            {
            case InstructionType.Skip:
                // skip to after next return
                while (currentInstruction < instructions.Length &&
                       instructions[currentInstruction].type != InstructionType.Return)
                {
                    currentInstruction++;
                }
                currentInstruction++;
                break;

            case InstructionType.Return:
                if (gotoStack.Count > 0)
                {
                    ProgramState ps = gotoStack.Pop();
                    currentInstruction = ps.instruction;
                    applicationCounter = ps.applicationCounter;
                }
                break;

            case InstructionType.Repeat:
                // we just set the counter, the execution is handled by the repeatable instructions
                System.Random sysRandom = GetComponent <ReplacementEngine>().systemRandom;
                int           repeats;
                if (sysRandom != null)
                {
                    repeats = instr.minRepeats + Mathf.FloorToInt((float)sysRandom.NextDouble() * (instr.maxRepeats - instr.minRepeats));
                }
                else
                {
                    repeats = Random.Range(instr.minRepeats, instr.maxRepeats);
                }
                applicationCounter = Mathf.Max(1, repeats);
                currentInstruction++;
                break;

            // repeatable instructions
            case InstructionType.Goto:
                if (UseApplication())
                {
                    gotoStack.Push(new ProgramState(currentInstruction, applicationCounter));
                    currentInstruction = instr.gotoPosition;
                }
                else
                {
                    currentInstruction++;
                }
                break;

            case InstructionType.ApplyRule:
            case InstructionType.ApplyRuleRange:
                if (UseApplication())
                {
                    // apply rule once and count down
                    if (instr.isSweep)
                    {
                        visibleChange = re.SweepReplace(instr.filter, instr.type == InstructionType.ApplyRuleRange);
                    }
                    else
                    {
                        visibleChange = re.ReplaceMatch(instr.filter, instr.type == InstructionType.ApplyRuleRange);
                    }
                }
                else
                {
                    currentInstruction++;
                }
                break;

            case InstructionType.Subdivide:
                visibleChange = true;
                re.DoSubdividision(instr.dimensions);
                currentInstruction++;
                break;

            case InstructionType.Error:
                currentInstruction++;
                break;
            }

            return(visibleChange? GenerationStepResult.VisibleChange : GenerationStepResult.NoVisibleChange);
        }