public string Generate()
    {
        int fullWeight = 0;

        foreach (ModuleInfo mInfo in modules)
        {
            fullWeight += mInfo.weight;
        }

        Debug.Log("In Generate Function");
        int rand = StaticRandom.NextInt(fullWeight);

        Debug.Log("Outside Generate Function");

        int weightCounter = fullWeight;

        for (int j = modules.Count - 1; j >= 0; j--)
        {
            if (rand >= weightCounter - modules[j].weight)
            {
                string moduleName = modules[j].name;
                modules[j].max--;
                if (modules[j].max == 0)
                {
                    modules.RemoveAt(j);
                }
                max--;
                return(moduleName);
            }
            weightCounter -= modules[j].weight;
        }
        //This WILL crash
        return("");
    }
Exemplo n.º 2
0
        /// <summary>
        /// Rolls the dice with parameters in this instantiated object.
        /// </summary>
        /// <returns></returns>
        public int Roll()
        {
            int result = 0;

            if (DropLowest > 0)
            {
                List <int> results = new List <int>();
                for (int i = 0; i < Quantity; i++)
                {
                    results.Add(StaticRandom.NextInt(Sides));
                }
                results.Sort();
                for (int i = 0; i < DropLowest; i++)
                {
                    results.RemoveAt(results.Count);
                }
                foreach (int roll in results)
                {
                    result += roll;
                }
            }
            else
            {
                for (int i = 0; i < Quantity; i++)
                {
                    result += StaticRandom.NextInt(Sides);
                }
            }
            result += Modifier;
            return(result);
        }
    void Start()
    {
        base.Start();

        submit      = transform.Find("Submit").gameObject;
        wire        = transform.Find("Wire").gameObject;
        lightSource = transform.Find("LightSource").gameObject;

        digitSet   = transform.Find("ToggleDigits").gameObject;
        moduleBase = transform.Find("Base").gameObject;

        //Lights initially off
        lightSource.GetComponent <Light>().enabled = false;

        operationBits  = new GameObject[bitsPerNumber];
        operationState = new bool[bitsPerNumber];

        for (int i = 1; i < bitsPerNumber + 1; ++i)
        {
            //Create the prefab...
            GameObject go = Instantiate(digitPrefab);
            //Set the parent...
            go.transform.SetParent(digitSet.transform);
            //No really, SET THE DAMN PARENT
            go.transform.localPosition = new Vector3(0, 0, 0);
            //Use magic to move the bit to the right place, in English:
            //Take an arbitrary number, subtract half the bit width from it, then subtract for every bit already made
            go.transform.localPosition = new Vector3((0.64f - (0.64f / bitsPerNumber)) - (i - 1) * (1.24f / bitsPerNumber), 0, 0);
            //print (i.ToString() + " " + go.transform.position.ToString());
            //Scale it accordingly
            go.transform.localScale = new Vector3(1.24f / bitsPerNumber, 0.2f, 0.4f);
            //Give it a name
            go.name = "Digit " + i;

            //Prepare puzzle values
            operationBits[i - 1]  = go;
            operationState[i - 1] = false;
            go.GetComponent <Renderer>().material.SetColor("_Color", falseColor);
            go.GetComponent <Renderer>().material.SetColor("_EmissionColor", falseColor);
        }

        //Answer generation, both numbers, and the answer are guaranteed to fit within the alloted bits per number
        a = StaticRandom.NextInt((int)(Math.Pow(2, bitsPerNumber)) - 1);
        b = StaticRandom.NextInt((int)(Math.Pow(2, bitsPerNumber)) - 1 - a);

        lights = new bool[bitsPerNumber * 2];
        IntToBoolArray(a, lights, 0);
        IntToBoolArray(b, lights, bitsPerNumber);

        answer = a + b;

        blink = BlinkLight();
        StartCoroutine(blink);
    }
Exemplo n.º 4
0
    public void GenerateCodes()
    {
        string codeLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        string vowels      = "AEIOU";
        string codeNumbers = "0123456789";

        id         = StaticRandom.NextInt(256);
        idAsBinary = "";
        int tmpId = id;

        for (int power = 128; power > 0; power /= 2)
        {
            if (tmpId >= power)
            {
                idAsBinary += "1";
                tmpId      -= power;
            }
            else
            {
                idAsBinary += "0";
            }
        }

        GameObject idPlate = transform.Find("ID Plate").gameObject;

        idPlate.transform.localPosition = new Vector3(modules[0, 0].transform.position.x - (1.1f + edgeConstant), modules[0, 0].transform.position.y, modules[0, 0].transform.position.z);


        idTag = idPlate.transform.Find("ID").gameObject;
        idTag.GetComponent <TMP_Text>().text = "ID: " + idAsBinary.ToString().Substring(0, 4) + " " + idAsBinary.ToString().Substring(4, 4);
        //idTag.transform.localPosition = new Vector3(modules[0, 0].transform.position.x - 1.01f, modules[0, 0].transform.position.y, modules[0, 0].transform.position.z);

        //Serial code generation
        serialContainsVowel = false;
        serialCode          = "";
        for (int i = 0; i < serialLength; ++i)
        {
            if (StaticRandom.Next() < 0.5f)
            {
                serialCode += codeLetters.Substring(StaticRandom.NextInt(26), 1);
                if (vowels.Contains(serialCode.Substring(i, 1)))
                {
                    serialContainsVowel = true;
                }
            }
            else
            {
                serialCode += codeNumbers.Substring(StaticRandom.NextInt(10), 1);
            }
        }
        serialTag = idPlate.transform.Find("Serial Code").gameObject;
        serialTag.GetComponent <TMP_Text>().text = serialCode;
        //serialTag.transform.localPosition = new Vector3(modules[0, 0].transform.position.x - 1.01f, modules[0, 0].transform.position.y - 0.5f, modules[0, 0].transform.position.z);
    }
Exemplo n.º 5
0
    static PlayerData()
    {
        string chars = "1234567890";

        for (int i = 0; i < 16; i++)
        {
            int rand = StaticRandom.NextInt(chars.Length);
            playerID += chars[rand];
        }
        Debug.Log("PLAYER ID");
        Debug.Log(playerID);
    }
    void Start()
    {
        base.Start();

        startSymbol   = StaticRandom.NextInt(10);
        currentSymbol = startSymbol;

        //symbolText = transform.Find("Text").gameObject;
        //symbolText.GetComponent<TMP_Text>().text = symbol[currentSymbol];

        mainIcon = transform.Find("Main Icon").gameObject;
        mainIcon.GetComponent <MeshRenderer>().material.SetTexture("_MainTex", symbolTextures[currentSymbol]);

        functionButtons = new GameObject[3];

        functionButtons[0] = transform.Find("Top Button").gameObject;
        functionButtons[1] = transform.Find("Left Button").gameObject;
        functionButtons[2] = transform.Find("Right Button").gameObject;
    }
Exemplo n.º 7
0
    public void GenerateBomb()
    {
        strikes = 0;

        int      fullWeight = 0;
        BombData bombData   = null;

        //This try catch block is only necessary while in production
        //So the game can still run from the level generation scene
        try {
            bombData = GameObject.Find("BombData").GetComponent <BombData>();
        } catch {
            UnityEngine.Object o = Resources.Load("BombData");
            print(o);
            GameObject oo = GameObject.Instantiate(o) as GameObject;
            bombData = oo.GetComponent <BombData>();
            //I shouldnt need this but screw me I guess
            bombData.SetData("Assets/Generators/default.json");
            bombData.Start();
        }
        BombInfo generator = bombData.meta;

        //print(JsonUtility.ToJson(generator));
        width      = generator.width;
        height     = generator.height;
        numModules = generator.numModules;

        //Merge modules and pools in generate
        generator.MergePools();
        print(JsonUtility.ToJson(generator));

        //Count number of modules generated pre-randomizer
        int generatedModules = 0;

        //Generate a list of modules to make
        List <GameObject> genModules = new List <GameObject>();

        //Always include timer module!
        genModules.Add(timerModule);


        //Add weights to get a maximum weight value for random generation
        for (int i = 0; i < generator.pools.Count; i++)
        {
            fullWeight += generator.pools[i].weight;
        }

        //Ensure minimum module generation is done
        foreach (PoolInfo pInfo in generator.pools)
        {
            List <string> poolSpecificModules = pInfo.GetMinModules();
            foreach (string moduleName in poolSpecificModules)
            {
                genModules.Add(bombData.allModules[moduleName]);
                generatedModules++;
            }
            for (int i = 0; i < pInfo.min; i++)
            {
                genModules.Add(bombData.allModules[pInfo.Generate()]);
                generatedModules++;
            }
        }

        //Number of modules = width * height, front and back, minus 1 for timer module
        for (int i = generatedModules; i < width * height * 2 - 1; ++i)
        {
            if (i < numModules && generator.pools.Count > 0)
            {
                int rand = StaticRandom.NextInt(fullWeight);
                //Insert some fancy heuristic some other day
                int weightCounter = fullWeight;

                //We can guarantee a certain module order from the fullWeight generation
                for (int j = generator.pools.Count - 1; j >= 0; j--)
                {
                    if (rand >= weightCounter - generator.pools[j].weight)
                    {
                        string s = generator.pools[j].Generate();
                        //print(s);
                        genModules.Add(bombData.allModules[s]);
                        //Dirty maximum check
                        //Note how this ignores maximum if <= 0 on startup
                        //This IS intended
                        if (!generator.pools[j].Validate() || generator.pools[j].max == 0)
                        {
                            print("REMOVED A POOL");
                            generator.pools.RemoveAt(j);
                            //Regenrate the fullWeight value
                            fullWeight = 0;
                            for (int k = 0; k < generator.pools.Count; k++)
                            {
                                fullWeight += generator.pools[k].weight;
                            }
                        }
                        break;
                    }
                    else
                    {
                        weightCounter -= generator.pools[j].weight;
                    }
                }
            }
            else
            {
                //When number of modules is exhausted, add blanks until the list is full
                genModules.Add(blankModule);
            }
        }

        //Shuffle the list
        //WARNING: This needs to be changed if interdependent modules exist
        //Used for shuffling modules
        Debug.Log("Shuffling");
        GameObject tmp;

        for (int i = 0; i < width * height * 2; ++i)
        {
            int j = StaticRandom.NextInt(i, width * height * 2);
            tmp           = genModules[i];
            genModules[i] = genModules[j];
            genModules[j] = tmp;
        }


        //Now we actually fill the bomb with modules
        modules = new Module[width, height * 2];
        for (int x = 0; x < width; ++x)
        {
            for (int z = 0; z < height; ++z)
            {
                GameObject model = Instantiate(moduleHolder, new Vector3(edgeConstant + x * moduleHolderWidth - ((moduleHolderWidth / 2) * width - 1), 0.5f, edgeConstant + z * moduleHolderWidth - ((moduleHolderWidth / 2) * height - 1)), Quaternion.Euler(-90, 0, 0));
                GameObject go    = Instantiate(genModules[x + z * width], new Vector3(edgeConstant + x * moduleHolderWidth - ((moduleHolderWidth / 2) * width - 1), 0.5f, edgeConstant + z * moduleHolderWidth - ((moduleHolderWidth / 2) * height - 1)), Quaternion.identity);
                if (go.GetComponent <Module>() != null)
                {
                    go.GetComponent <Module>().bombSource = this;
                }
                if (go.GetComponent <BombTimerScript>())
                {
                    loadedTimerModule = go;
                }
                modules[x, z] = go.GetComponent <Module>();

                model.transform.parent = this.transform;
                go.transform.parent    = this.transform;
            }
        }

        for (int x = 0; x < width; ++x)
        {
            for (int z = height; z < height * 2; ++z)
            {
                GameObject model = Instantiate(moduleHolder, new Vector3(edgeConstant + x * moduleHolderWidth - ((moduleHolderWidth / 2) * width - 1), -0.5f, edgeConstant + (z % height) * moduleHolderWidth - ((moduleHolderWidth / 2) * height - 1)), Quaternion.Euler(90, 0, 180));
                GameObject go    = Instantiate(genModules[(width * height) + x + (z % height) * width], new Vector3(edgeConstant + x * moduleHolderWidth - ((moduleHolderWidth / 2) * width - 1), -0.5f, edgeConstant + (z % height) * moduleHolderWidth - ((moduleHolderWidth / 2) * height - 1)), Quaternion.Euler(0, 0, 180));
                if (go.GetComponent <Module>() != null)
                {
                    go.GetComponent <Module>().bombSource = this;
                }
                if (go.GetComponent <BombTimerScript>())
                {
                    loadedTimerModule = go;
                }
                modules[x, z] = go.GetComponent <Module>();

                model.transform.parent = this.transform;
                go.transform.parent    = this.transform;
            }
        }

        loadedTimerModule.GetComponent <BombTimerScript>().secondsLeft = generator.time;

        //bombData.Consume();
    }
Exemplo n.º 8
0
    void GenerateTrial()
    {
        bool a, b;

        //int operation;

        //Puzzle and answer are generated
        //The first trial will never have the not combined operators (NOR, NAND, XNOR)
        if (trialsLeft == 3)
        {
            operation = StaticRandom.NextInt(4);
        }
        else
        {
            operation = StaticRandom.NextInt(7);
        }
        //Operations:
        //NOT, AND, OR, XOR, NAND, NOR, XNOR

        a = StaticRandom.Next() < 0.5;
        b = StaticRandom.Next() < 0.5;

        //Choose operation
        if (operation == 0)
        {
            solution = !a;
            operationPrompt.GetComponent <TMP_Text>().text = "!";
        }
        else if (operation == 1)
        {
            solution = a && b;
            operationPrompt.GetComponent <TMP_Text>().text = "&&";
        }
        else if (operation == 2)
        {
            solution = a || b;
            operationPrompt.GetComponent <TMP_Text>().text = "||";
        }
        else if (operation == 3)
        {
            solution = a ^ b;
            operationPrompt.GetComponent <TMP_Text>().text = "^^";
        }
        else if (operation == 4)
        {
            solution = !(a && b);
            operationPrompt.GetComponent <TMP_Text>().text = "!&";
        }
        else if (operation == 5)
        {
            solution = !(a || b);
            operationPrompt.GetComponent <TMP_Text>().text = "!|";
        }
        else if (operation == 6)
        {
            solution = (a == b);
            operationPrompt.GetComponent <TMP_Text>().text = "!^";
        }

        //Light up components according to puzzle
        SetObjectColor(leftLight, "_EmissionColor", a ? trueColor : falseColor);
        SetObjectColor(rightLight, "_EmissionColor", b ? trueColor : falseColor);

        SetObjectColor(leftLight, "_Color", a ? trueColor : falseColor);
        SetObjectColor(rightLight, "_Color", b ? trueColor : falseColor);

        leftLightSource.GetComponent <Light>().color  = a ? trueColor : falseColor;
        rightLightSource.GetComponent <Light>().color = b ? trueColor : falseColor;

        if (operation == 0)
        {
            SetObjectColor(rightLight, "_Color", inertColor);
            SetObjectColor(rightLight, "_EmissionColor", inertColor);
            rightLightSource.GetComponent <Light>().color = inertColor;
        }
    }