Ejemplo n.º 1
0
    private void Awake()
    {
        Block = new NormalBlock(transform.localScale.x, transform.localScale.y, 0f, transform.position, null, Color.cyan);
        GameObject visualPart = Instantiate(visualBlock, transform);

        visualPart.GetComponent <SpriteRenderer>().color = new Color(0.749f, 0.2f, 0.188f, 1f);
    }
Ejemplo n.º 2
0
        private IBlock CreateBlock(String type, Vector2 startingPos, int numBlocks)
        {
            IBlock newBlock = new NormalBlock(game, BLOCK_SIDE * numBlocks, BlockSpriteFactory.Instance.CreateNormalBlockSprite(game.SpriteBatch, startingPos), startingPos);

            switch (type)
            {
            case "Normal":
                break;

            case "Water":
                newBlock = new NormalBlock(game, 0, BlockSpriteFactory.Instance.CreateWaterBlockSprite(game.SpriteBatch, startingPos), startingPos);
                break;

            case "Ladder":
                newBlock = new WalkableBlock(game, BlockSpriteFactory.Instance.CreateLadderSprite(game.SpriteBatch, startingPos));
                break;

            case "Brick":
                newBlock = new NormalBlock(game, 0, BlockSpriteFactory.Instance.CreateBrickSprite(game.SpriteBatch, startingPos), startingPos);
                break;

            case "Invisible":
                newBlock = new NormalBlock(game, 0, BlockSpriteFactory.Instance.CreateInvisibleBlockSprite(game.SpriteBatch, startingPos), startingPos);
                break;

            case "Red":
                newBlock = new NormalBlock(game, 0, BlockSpriteFactory.Instance.CreateWaterBlockSprite(game.SpriteBatch, startingPos), startingPos);
                break;

            case "Wall":
                Background.Instance.generateRoomWall(startingPos);
                newBlock = null;
                break;

            default:
                break;
            }
            return(newBlock);
        }
Ejemplo n.º 3
0
    void SpawnBlocks()
    {
        foreach (BlockSpawn spawn in _blockSpawns)
        {
            // GameObject newBlock;
            // newBlock = Instantiate(normalBlock, spawn.position, quaternion.identity);
            // blocks.Add(newBlock);
            if (spawn.isNormal)
            {
                //Set hit points
                //newBlockScript.SetHitpoints(spawn.hitPoints);
                //Set color
                GameObject newInstatiatedBlock = Instantiate(normalBlock, spawn.position, quaternion.identity);
                blocks.Add(newInstatiatedBlock);
                NormalBlock blockScript = newInstatiatedBlock.GetComponent <NormalBlock>();
                if (blockScript == null)
                {
                    Debug.Log("no script");
                }
                else
                {
                    Debug.Log("script here");
                }
                blockScript.SetSpriteColor(spawn.color);

                Debug.Log("trying");
            }
            else
            {
                int         rand         = Random.Range(0, SpecialBlocks.Count);
                GameObject  specialBlock = SpecialBlocks[rand];
                NormalBlock blockScript  = specialBlock.GetComponent <NormalBlock>();
                //blockScript.SetHitpoints(spawn.hitPoints);
                GameObject instatiatedSpecialBlock = Instantiate(specialBlock, spawn.position, quaternion.identity);
                blocks.Add(instatiatedSpecialBlock);
            }
        }
    }
Ejemplo n.º 4
0
        //Blockのテスト2
        public static void Test12()
        {
            BlockGenerator bg = new BlockGenerator();

            Block[] blks = new Block[10];
            BlockContext[] blkCons = new BlockContext[blks.Length];
            for (int i = 0; i < blks.Length; i++)
            {
                blkCons[i] = bg.CreateNextValidBlock();
                blks[i] = blkCons[i].block;

                if (i > 0)
                {
                    NormalBlock nblk = blks[i] as NormalBlock;

                    byte[] nblkBytes = nblk.ToBinary();

                    NormalBlock nblkRestore = SHAREDDATA.FromBinary<NormalBlock>(nblkBytes);

                    if (!nblk.Id.Equals(nblkRestore.Id))
                        throw new Exception("test12_1");

                    byte[] nblkBytes2 = SHAREDDATA.ToBinary<Block>(blks[i]);

                    NormalBlock nblkRestore2 = SHAREDDATA.FromBinary<Block>(nblkBytes2) as NormalBlock;

                    if (!nblk.Id.Equals(nblkRestore2.Id))
                        throw new Exception("test12_2");
                }
            }

            GenesisBlock gblk = blks[0] as GenesisBlock;

            Creahash gblkid = new Creahash(gblk.ToBinary());

            if (!gblk.Id.Equals(gblkid))
                throw new Exception("test12_3");

            NormalBlock nblk2 = blks[1] as NormalBlock;

            Creahash nblkid = new Creahash(nblk2.header.ToBinary());

            if (!nblk2.Id.Equals(nblkid))
                throw new Exception("test12_4");

            nblk2.UpdateTimestamp(DateTime.Now);

            Creahash nblkid2 = new Creahash(nblk2.header.ToBinary());

            if (!nblk2.Id.Equals(nblkid2))
                throw new Exception("test12_5");

            nblk2.UpdateNonce(new byte[10]);

            Creahash nblkid3 = new Creahash(nblk2.header.ToBinary());

            if (!nblk2.Id.Equals(nblkid3))
                throw new Exception("test12_6");

            nblk2.UpdateMerkleRootHash();

            Creahash nblkid4 = new Creahash(nblk2.header.ToBinary());

            if (!nblk2.Id.Equals(nblkid4))
                throw new Exception("test12_7");

            if (!nblk2.VerifyBlockType())
                throw new Exception("test12_8");

            FoundationalBlock fblk = new FoundationalBlock();
            fblk.LoadVersion0(nblk2.header, nblk2.coinbaseTxToMiner, nblk2.coinbaseTxToMiner, nblk2.transferTxs);

            byte[] fblkBytes = fblk.ToBinary();

            FoundationalBlock fblkRestore = SHAREDDATA.FromBinary<FoundationalBlock>(fblkBytes);

            if (!fblk.Id.Equals(fblkRestore.Id))
                throw new Exception("test12_9");

            byte[] fblkBytes2 = SHAREDDATA.ToBinary<Block>(fblk);

            FoundationalBlock fblkRestore2 = SHAREDDATA.FromBinary<Block>(fblkBytes2) as FoundationalBlock;

            if (!fblk.Id.Equals(fblkRestore2.Id))
                throw new Exception("test12_10");

            if (fblk.VerifyBlockType())
                throw new Exception("test12_11");

            if (!nblk2.VerifyMerkleRootHash())
                throw new Exception("test12_12");

            byte[] nblkBytes3 = nblk2.ToBinary();

            NormalBlock nblk3 = SHAREDDATA.FromBinary<NormalBlock>(nblkBytes3);

            nblk3.header.merkleRootHash.hash[0] ^= 255;

            if (nblk3.VerifyMerkleRootHash())
                throw new Exception("test12_13");

            byte[] nonce = new byte[10];
            while (true)
            {
                nblk2.UpdateNonce(nonce);

                if (nblk2.Id.hash[0] == 0 && nblk2.Id.hash[1] <= 127)
                {
                    if (!nblk2.VerifyId())
                        throw new Exception("test12_14");

                    break;
                }

                if (nblk2.VerifyId())
                    throw new Exception("test12_15");

                int index = nonce.Length.RandomNum();
                int value = 256.RandomNum();

                nonce[index] = (byte)value;
            }

            TransferTransaction[] transferTxs1 = new TransferTransaction[99];
            for (int i = 0; i < transferTxs1.Length; i++)
                transferTxs1[i] = (blks[2] as TransactionalBlock).transferTxs[0];

            TransferTransaction[] transferTxs2 = new TransferTransaction[100];
            for (int i = 0; i < transferTxs2.Length; i++)
                transferTxs2[i] = (blks[2] as TransactionalBlock).transferTxs[0];

            NormalBlock nblk4 = new NormalBlock();
            nblk4.LoadVersion0(nblk2.header, nblk2.coinbaseTxToMiner, transferTxs1);

            NormalBlock nblk5 = new NormalBlock();
            nblk5.LoadVersion0(nblk2.header, nblk2.coinbaseTxToMiner, transferTxs2);

            if (!nblk4.VerifyNumberOfTxs())
                throw new Exception("test12_16");

            if (nblk5.VerifyNumberOfTxs())
                throw new Exception("test12_17");

            for (int i = 1; i < blks.Length; i++)
            {
                TransactionalBlock tblk = blks[i] as TransactionalBlock;

                CurrencyUnit amount = tblk.GetActualRewardToMinerAndTxFee();
                CurrencyUnit amount2 = tblk.GetValidRewardToMinerAndTxFee(blkCons[i].prevTxOutss);
                CurrencyUnit amount3 = tblk.GetValidTxFee(blkCons[i].prevTxOutss);

                if (amount.rawAmount != (long)5400000000 + blkCons[i].feeRawAmount)
                    throw new Exception("test12_18");
                if (amount2.rawAmount != amount.rawAmount)
                    throw new Exception("test12_19");
                if (amount3.rawAmount != blkCons[i].feeRawAmount)
                    throw new Exception("test12_20");

                if (!tblk.VerifyRewardAndTxFee(blkCons[i].prevTxOutss))
                    throw new Exception("test12_21");
                if (!tblk.VerifyTransferTransaction(blkCons[i].prevTxOutss))
                    throw new Exception("test12_22");

                bool flag = false;
                TransactionOutput[][] invalidPrevTxOutss = new TransactionOutput[blkCons[i].prevTxOutss.Length][];
                for (int j = 0; j < invalidPrevTxOutss.Length; j++)
                {
                    invalidPrevTxOutss[j] = new TransactionOutput[blkCons[i].prevTxOutss[j].Length];
                    for (int k = 0; k < invalidPrevTxOutss[j].Length; k++)
                    {
                        if (j == 1 && k == 0)
                        {
                            invalidPrevTxOutss[j][k] = new TransactionOutput();
                            invalidPrevTxOutss[j][k].LoadVersion0(new Sha256Ripemd160Hash(), new CurrencyUnit(0));

                            flag = true;
                        }
                        else
                            invalidPrevTxOutss[j][k] = blkCons[i].prevTxOutss[j][k];
                    }
                }

                if (flag)
                {
                    if (tblk.VerifyRewardAndTxFee(invalidPrevTxOutss))
                        throw new Exception("test12_23");
                    if (tblk.VerifyTransferTransaction(invalidPrevTxOutss))
                        throw new Exception("test12_24");
                }
            }

            Console.WriteLine("test12_succeeded");
        }
Ejemplo n.º 5
0
        public BlockContext CreateNextValidBlock()
        {
            currentBIndex++;

            if (currentBIndex == 0)
            {
                Block blk = new GenesisBlock();

                blks.Add(blk);

                Dictionary<Sha256Ripemd160Hash, List<TransactionOutputContext>> unspentTxOutsDictClone2 = new Dictionary<Sha256Ripemd160Hash, List<TransactionOutputContext>>();
                Dictionary<Sha256Ripemd160Hash, List<TransactionOutputContext>> spentTxOutsDictClone2 = new Dictionary<Sha256Ripemd160Hash, List<TransactionOutputContext>>();

                for (int i = 0; i < keyPairs.Length; i++)
                {
                    unspentTxOutsDictClone2.Add(addresses[i], new List<TransactionOutputContext>());
                    foreach (var toc in unspentTxOutsDict[addresses[i]])
                        unspentTxOutsDictClone2[addresses[i]].Add(toc);
                    spentTxOutsDictClone2.Add(addresses[i], new List<TransactionOutputContext>());
                    foreach (var toc in spentTxOutsDict[addresses[i]])
                        spentTxOutsDictClone2[addresses[i]].Add(toc);
                }

                return new BlockContext(blk, new TransactionOutput[][] { }, 0, unspentTxOutsDictClone2, spentTxOutsDictClone2);
            }

            int numOfSpendTxs = maxNumOfSpendTxs.RandomNum() + 1;
            int[] numOfSpendTxOutss = new int[numOfSpendTxs];
            for (int i = 0; i < numOfSpendTxOutss.Length; i++)
                numOfSpendTxOutss[i] = maxNumOfSpendTxOuts.RandomNum() + 1;

            TransactionOutputContext[][] spendTxOutss = new TransactionOutputContext[numOfSpendTxs][];
            for (int i = 0; i < spendTxOutss.Length; i++)
            {
                if (unspentTxOuts.Count == 0)
                    break;

                spendTxOutss[i] = new TransactionOutputContext[numOfSpendTxOutss[i]];

                for (int j = 0; j < spendTxOutss[i].Length; j++)
                {
                    int index = unspentTxOuts.Count.RandomNum();

                    spendTxOutss[i][j] = unspentTxOuts[index];

                    spentTxOutsDict[unspentTxOuts[index].address].Add(unspentTxOuts[index]);
                    unspentTxOutsDict[unspentTxOuts[index].address].Remove(unspentTxOuts[index]);

                    spentTxOuts.Add(unspentTxOuts[index]);
                    unspentTxOuts.RemoveAt(index);

                    if (unspentTxOuts.Count == 0)
                        break;
                }
            }

            long fee = 0;
            List<TransferTransaction> transferTxs = new List<TransferTransaction>();
            List<TransactionOutput[]> prevTxOutsList = new List<TransactionOutput[]>();
            for (int i = 0; i < spendTxOutss.Length; i++)
            {
                if (spendTxOutss[i] == null)
                    break;

                long sumRawAmount = 0;

                List<TransactionInput> txInputsList = new List<TransactionInput>();
                for (int j = 0; j < spendTxOutss[i].Length; j++)
                {
                    if (spendTxOutss[i][j] == null)
                        break;

                    txInputsList.Add(spendTxOutss[i][j].GenerateTransactionInput());

                    sumRawAmount += spendTxOutss[i][j].amount.rawAmount;
                }

                TransactionInput[] txIns = txInputsList.ToArray();

                int num = sumRawAmount > 1000000 ? (int)Math.Ceiling(((avgIORatio - 1) * 2) * 1.RandomDouble() * txIns.Length) : 1;

                TransactionOutputContext[] txOutsCon = new TransactionOutputContext[num];
                TransactionOutput[] txOuts = new TransactionOutput[num];
                for (int j = 0; j < txOutsCon.Length; j++)
                {
                    long outAmount = 0;
                    if (sumRawAmount > 1000000)
                    {
                        long sumRawAmountDivided = sumRawAmount / 1000000;

                        int subtract = ((int)sumRawAmountDivided / 2).RandomNum() + 1;

                        outAmount = (long)subtract * 1000000;
                    }
                    else
                        outAmount = sumRawAmount;

                    sumRawAmount -= outAmount;

                    int index = numOfKeyPairs.RandomNum();

                    txOutsCon[j] = new TransactionOutputContext(currentBIndex, i + 1, j, new CurrencyUnit(outAmount), addresses[index], keyPairs[index]);
                    txOuts[j] = txOutsCon[j].GenerateTrasactionOutput();
                }

                fee += sumRawAmount;

                for (int j = 0; j < txOutsCon.Length; j++)
                {
                    unspentTxOutsDict[txOutsCon[j].address].Add(txOutsCon[j]);

                    unspentTxOuts.Add(txOutsCon[j]);
                }

                TransactionOutput[] prevTxOuts = new TransactionOutput[txIns.Length];
                Ecdsa256PrivKey[] privKeys = new Ecdsa256PrivKey[txIns.Length];
                for (int j = 0; j < prevTxOuts.Length; j++)
                {
                    prevTxOuts[j] = spendTxOutss[i][j].GenerateTrasactionOutput();
                    privKeys[j] = spendTxOutss[i][j].keyPair.privKey;
                }

                TransferTransaction tTx = new TransferTransaction();
                tTx.LoadVersion0(txIns, txOuts);
                tTx.Sign(prevTxOuts, privKeys);

                transferTxs.Add(tTx);
                prevTxOutsList.Add(prevTxOuts);
            }

            long rewardAndFee = TransactionalBlock.GetRewardToMiner(currentBIndex, 0).rawAmount + fee;

            TransactionOutputContext[] coinbaseTxOutsCon = new TransactionOutputContext[numOfCoinbaseTxOuts];
            TransactionOutput[] coinbaseTxOuts = new TransactionOutput[numOfCoinbaseTxOuts];
            for (int i = 0; i < coinbaseTxOutsCon.Length; i++)
            {
                long outAmount2 = 0;
                if (i != coinbaseTxOutsCon.Length - 1)
                {
                    long rewardAndFeeDevided = rewardAndFee / 1000000;

                    int subtract2 = ((int)rewardAndFeeDevided / 2).RandomNum() + 1;

                    outAmount2 = (long)subtract2 * 1000000;

                    rewardAndFee -= outAmount2;
                }
                else
                    outAmount2 = rewardAndFee;

                int index = numOfKeyPairs.RandomNum();

                coinbaseTxOutsCon[i] = new TransactionOutputContext(currentBIndex, 0, i, new CurrencyUnit(outAmount2), addresses[index], keyPairs[index]);
                coinbaseTxOuts[i] = coinbaseTxOutsCon[i].GenerateTrasactionOutput();
            }

            CoinbaseTransaction coinbaseTx = new CoinbaseTransaction();
            coinbaseTx.LoadVersion0(coinbaseTxOuts);

            for (int i = 0; i < coinbaseTxOutsCon.Length; i++)
            {
                unspentTxOutsDict[coinbaseTxOutsCon[i].address].Add(coinbaseTxOutsCon[i]);

                unspentTxOuts.Add(coinbaseTxOutsCon[i]);
            }

            prevTxOutsList.Insert(0, new TransactionOutput[] { });

            Difficulty<Creahash> diff = new Difficulty<Creahash>(HASHBASE.FromHash<Creahash>(new byte[] { 0, 127, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255 }));
            byte[] nonce = new byte[10] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

            BlockHeader bh = new BlockHeader();
            bh.LoadVersion0(currentBIndex, blks[blks.Count - 1].Id, DateTime.Now, diff, nonce);

            NormalBlock nblk = new NormalBlock();
            nblk.LoadVersion0(bh, coinbaseTx, transferTxs.ToArray());
            nblk.UpdateMerkleRootHash();

            blks.Add(nblk);

            Dictionary<Sha256Ripemd160Hash, List<TransactionOutputContext>> unspentTxOutsDictClone = new Dictionary<Sha256Ripemd160Hash, List<TransactionOutputContext>>();
            Dictionary<Sha256Ripemd160Hash, List<TransactionOutputContext>> spentTxOutsDictClone = new Dictionary<Sha256Ripemd160Hash, List<TransactionOutputContext>>();

            for (int i = 0; i < keyPairs.Length; i++)
            {
                unspentTxOutsDictClone.Add(addresses[i], new List<TransactionOutputContext>());
                foreach (var toc in unspentTxOutsDict[addresses[i]])
                    unspentTxOutsDictClone[addresses[i]].Add(toc);
                spentTxOutsDictClone.Add(addresses[i], new List<TransactionOutputContext>());
                foreach (var toc in spentTxOutsDict[addresses[i]])
                    spentTxOutsDictClone[addresses[i]].Add(toc);
            }

            BlockContext blkCon = new BlockContext(nblk, prevTxOutsList.ToArray(), fee, unspentTxOutsDictClone, spentTxOutsDictClone);

            return blkCon;
        }
Ejemplo n.º 6
0
        //BlockChainのテスト(分岐がある場合・採掘・後ろが無効な場合)
        public static void Test24()
        {
            string basepath = System.IO.Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);

            BlockchainAccessDB bcadb = new BlockchainAccessDB(basepath);
            string bcadbPath = bcadb.GetPath();

            if (File.Exists(bcadbPath))
                File.Delete(bcadbPath);

            BlockManagerDB bmdb = new BlockManagerDB(basepath);
            string bmdbPath = bmdb.GetPath();

            if (File.Exists(bmdbPath))
                File.Delete(bmdbPath);

            BlockDB bdb = new BlockDB(basepath);
            string bdbPath = bdb.GetPath(0);

            if (File.Exists(bdbPath))
                File.Delete(bdbPath);

            BlockFilePointersDB bfpdb = new BlockFilePointersDB(basepath);
            string bfpPath = bfpdb.GetPath();

            if (File.Exists(bfpPath))
                File.Delete(bfpPath);

            UtxoFileAccessDB ufadb = new UtxoFileAccessDB(basepath);
            string ufadbPath = ufadb.GetPath();

            if (File.Exists(ufadbPath))
                File.Delete(ufadbPath);

            UtxoFilePointersDB ufpdb = new UtxoFilePointersDB(basepath);
            string ufpdbPath = ufpdb.GetPath();

            if (File.Exists(ufpdbPath))
                File.Delete(ufpdbPath);

            UtxoFilePointersTempDB ufptempdb = new UtxoFilePointersTempDB(basepath);
            string ufptempdbPath = ufptempdb.GetPath();

            if (File.Exists(ufptempdbPath))
                File.Delete(ufptempdbPath);

            UtxoDB utxodb = new UtxoDB(basepath);
            string utxodbPath = utxodb.GetPath();

            if (File.Exists(utxodbPath))
                File.Delete(utxodbPath);

            BlockChain blockchain = new BlockChain(bcadb, bmdb, bdb, bfpdb, ufadb, ufpdb, ufptempdb, utxodb, 100, 300, 1000, 1000);

            BlockGenerator bg = new BlockGenerator();

            Block[] blks = new Block[10];
            BlockContext[] blkCons = new BlockContext[blks.Length];
            for (int i = 0; i < blks.Length; i++)
            {
                blkCons[i] = bg.CreateNextValidBlock();
                blks[i] = blkCons[i].block;

                Console.WriteLine("block" + i.ToString() + " created.");
            }

            Block[] blks2 = new Block[blks.Length];
            double cumulativeDiff1 = 0.0;
            byte[] nonce = null;

            Func<long, TransactionalBlock> _indexToBlock = (index) => blks2[index] as TransactionalBlock;

            for (int i = 0; i < blks.Length; i++)
            {
                if (i == 0)
                {
                    blks2[i] = blks[i];
                    cumulativeDiff1 += blks2[i].Difficulty.Diff;

                    Console.WriteLine("block" + i.ToString() + "_1 " + blks2[i].Difficulty.Diff.ToString() + " " + cumulativeDiff1.ToString());

                    continue;
                }

                TransactionalBlock tblk = blks[i] as TransactionalBlock;

                TransactionalBlock tblk2 = TransactionalBlock.GetBlockTemplate(tblk.Index, tblk.coinbaseTxToMiner, tblk.transferTxs, _indexToBlock, 0);

                nonce = new byte[10];

                while (true)
                {
                    tblk2.UpdateTimestamp(DateTime.Now);
                    tblk2.UpdateNonce(nonce);

                    if (tblk2.Id.CompareTo(tblk2.header.difficulty.Target) <= 0)
                    {
                        blks2[i] = tblk2;
                        cumulativeDiff1 += blks2[i].Difficulty.Diff;

                        Console.WriteLine("block" + i.ToString() + "_1 mined. " + blks2[i].Difficulty.Diff.ToString() + " " + cumulativeDiff1.ToString());

                        break;
                    }

                    int index = nonce.Length.RandomNum();
                    int value = 256.RandomNum();

                    nonce[index] = (byte)value;
                }
            }

            TransactionalBlock tblk2_1 = blks2[1] as TransactionalBlock;
            TransactionalBlock tblk2_2 = blks2[2] as TransactionalBlock;

            TransactionalBlock blk3_2 = TransactionalBlock.GetBlockTemplate(2, tblk2_2.coinbaseTxToMiner, tblk2_2.transferTxs, _indexToBlock, 0);

            while (true)
            {
                blk3_2.UpdateTimestamp(DateTime.Now);
                blk3_2.UpdateNonce(nonce);

                if (blk3_2.Id.CompareTo(blk3_2.header.difficulty.Target) <= 0)
                {
                    Console.WriteLine("block3_2 mined. " + blk3_2.Difficulty.Diff.ToString());

                    break;
                }

                int index = nonce.Length.RandomNum();
                int value = 256.RandomNum();

                nonce[index] = (byte)value;
            }

            BlockHeader bh3 = new BlockHeader();
            bh3.LoadVersion0(3, blk3_2.Id, DateTime.Now, blks2[1].Difficulty, new byte[10]);
            NormalBlock blk3_3 = new NormalBlock();
            blk3_3.LoadVersion0(bh3, tblk2_1.coinbaseTxToMiner, tblk2_1.transferTxs);
            blk3_3.UpdateMerkleRootHash();

            int forkLength = (int)Math.Floor(cumulativeDiff1 / blks2[1].Difficulty.Diff + 10);

            TransactionalBlock[] blks3 = new TransactionalBlock[forkLength];

            blks3[0] = blk3_3;

            for (int i = 1; i < blks3.Length; i++)
            {
                BlockHeader bh = new BlockHeader();
                bh.LoadVersion0(i + 3, blks3[i - 1].Id, DateTime.Now, blks2[1].Difficulty, new byte[10]);
                NormalBlock blk3 = new NormalBlock();
                blk3.LoadVersion0(bh, tblk2_1.coinbaseTxToMiner, tblk2_1.transferTxs);
                blk3.UpdateMerkleRootHash();

                blks3[i] = blk3;
            }

            BlockHeader bh4 = new BlockHeader();
            bh4.LoadVersion0(4, blk3_3.Id, DateTime.Now, blks2[1].Difficulty, new byte[10]);
            NormalBlock blk4 = new NormalBlock();
            blk4.LoadVersion0(bh4, tblk2_1.coinbaseTxToMiner, tblk2_1.transferTxs);
            blk4.UpdateMerkleRootHash();

            BlockHeader bh10 = new BlockHeader();
            bh10.LoadVersion0(10, blks2[9].Id, DateTime.Now, blks2[1].Difficulty, new byte[10]);
            NormalBlock blk5_10 = new NormalBlock();
            blk5_10.LoadVersion0(bh10, tblk2_1.coinbaseTxToMiner, tblk2_1.transferTxs);
            blk5_10.UpdateMerkleRootHash();

            TransactionalBlock[] blks5 = new TransactionalBlock[5];

            blks5[0] = blk5_10;

            for (int i = 1; i < blks5.Length; i++)
            {
                BlockHeader bh = new BlockHeader();
                bh.LoadVersion0(i + 10, blks5[i - 1].Id, DateTime.Now, blks2[1].Difficulty, new byte[10]);
                NormalBlock blk5 = new NormalBlock();
                blk5.LoadVersion0(bh, tblk2_1.coinbaseTxToMiner, tblk2_1.transferTxs);
                blk5.UpdateMerkleRootHash();

                blks5[i] = blk5;
            }

            BlockChain.UpdateChainReturnType type5 = blockchain.UpdateChain(blks2[0]);

            if (type5 != BlockChain.UpdateChainReturnType.updated)
                throw new Exception("test24_5");

            BlockChain.UpdateChainReturnType type = blockchain.UpdateChain(blk4);

            if (type != BlockChain.UpdateChainReturnType.pending)
                throw new Exception("test24_1");

            for (int i = 2; i < blks2.Length; i++)
            {
                BlockChain.UpdateChainReturnType type2 = blockchain.UpdateChain(blks2[i]);

                if (type2 != BlockChain.UpdateChainReturnType.pending)
                    throw new Exception("test24_2");
            }

            for (int i = 0; i < blks5.Length; i++)
            {
                BlockChain.UpdateChainReturnType type2 = blockchain.UpdateChain(blks5[i]);

                if (type2 != BlockChain.UpdateChainReturnType.pending)
                    throw new Exception("test24_3");
            }

            BlockChain.UpdateChainReturnType type3 = blockchain.UpdateChain(blk3_2);

            if (type3 != BlockChain.UpdateChainReturnType.pending)
                throw new Exception("test24_1");

            for (int i = 0; i < blks3.Length; i++)
            {
                BlockChain.UpdateChainReturnType type2 = blockchain.UpdateChain(blks3[i]);

                if (type2 != BlockChain.UpdateChainReturnType.pending)
                    throw new Exception("test24_4");
            }

            BlockChain.UpdateChainReturnType type4 = blockchain.UpdateChain(blks2[1]);

            if (type4 != BlockChain.UpdateChainReturnType.updatedAndRejected)
                throw new Exception("test24_6");
            if (blockchain.blocksCurrent.value != 10)
                throw new Exception("test19_2");

            Console.WriteLine("test24_succeeded");
        }
Ejemplo n.º 7
0
        //BlockChainのテスト(分岐がない場合・無効ブロックなどを追加しようとした場合)
        public static void Test18()
        {
            string basepath = System.IO.Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);

            BlockchainAccessDB bcadb = new BlockchainAccessDB(basepath);
            string bcadbPath = bcadb.GetPath();

            if (File.Exists(bcadbPath))
                File.Delete(bcadbPath);

            BlockManagerDB bmdb = new BlockManagerDB(basepath);
            string bmdbPath = bmdb.GetPath();

            if (File.Exists(bmdbPath))
                File.Delete(bmdbPath);

            BlockDB bdb = new BlockDB(basepath);
            string bdbPath = bdb.GetPath(0);

            if (File.Exists(bdbPath))
                File.Delete(bdbPath);

            BlockFilePointersDB bfpdb = new BlockFilePointersDB(basepath);
            string bfpPath = bfpdb.GetPath();

            if (File.Exists(bfpPath))
                File.Delete(bfpPath);

            UtxoFileAccessDB ufadb = new UtxoFileAccessDB(basepath);
            string ufadbPath = ufadb.GetPath();

            if (File.Exists(ufadbPath))
                File.Delete(ufadbPath);

            UtxoFilePointersDB ufpdb = new UtxoFilePointersDB(basepath);
            string ufpdbPath = ufpdb.GetPath();

            if (File.Exists(ufpdbPath))
                File.Delete(ufpdbPath);

            UtxoFilePointersTempDB ufptempdb = new UtxoFilePointersTempDB(basepath);
            string ufptempdbPath = ufptempdb.GetPath();

            if (File.Exists(ufptempdbPath))
                File.Delete(ufptempdbPath);

            UtxoDB utxodb = new UtxoDB(basepath);
            string utxodbPath = utxodb.GetPath();

            if (File.Exists(utxodbPath))
                File.Delete(utxodbPath);

            BlockChain blockchain = new BlockChain(bcadb, bmdb, bdb, bfpdb, ufadb, ufpdb, ufptempdb, utxodb);

            BlockGenerator bg = new BlockGenerator();

            Block[] blks = new Block[10];
            BlockContext[] blkCons = new BlockContext[blks.Length];
            for (int i = 0; i < blks.Length; i++)
            {
                blkCons[i] = bg.CreateNextValidBlock();
                blks[i] = blkCons[i].block;

                Console.WriteLine("block" + i.ToString() + " created.");
            }

            Block[] blks2 = new Block[blks.Length];
            byte[] nonce = null;

            Func<long, TransactionalBlock> _indexToBlock = (index) => blks2[index] as TransactionalBlock;

            for (int i = 0; i < blks.Length; i++)
            {
                if (i == 0)
                {
                    blks2[i] = blks[i];

                    continue;
                }

                TransactionalBlock tblk = blks[i] as TransactionalBlock;

                TransactionalBlock tblk2 = TransactionalBlock.GetBlockTemplate(tblk.Index, tblk.coinbaseTxToMiner, tblk.transferTxs, _indexToBlock, 0);

                nonce = new byte[10];

                while (true)
                {
                    tblk2.UpdateTimestamp(DateTime.Now);
                    tblk2.UpdateNonce(nonce);

                    if (tblk2.Id.CompareTo(tblk2.header.difficulty.Target) <= 0)
                    {
                        blks2[i] = tblk2;

                        Console.WriteLine("block" + i.ToString() + " mined.");

                        break;
                    }

                    int index = nonce.Length.RandomNum();
                    int value = 256.RandomNum();

                    nonce[index] = (byte)value;
                }
            }

            if (blockchain.blocksCurrent.value != 0)
                throw new Exception("test18_1");
            for (int i = 0; i < blockchain.pendingBlocks.Length; i++)
            {
                if (blockchain.pendingBlocks[i] != null)
                    throw new Exception("test18_2");
                if (blockchain.rejectedBlocks[i] != null)
                    throw new Exception("test18_3");
            }

            TransactionalBlock blk1 = blks2[1] as TransactionalBlock;
            TransactionalBlock blk2 = blks2[2] as TransactionalBlock;

            Creahash hashzero = new Creahash();

            BlockHeader bh5 = new BlockHeader();
            bh5.LoadVersion0(100, hashzero, DateTime.Now, blk1.Difficulty, new byte[10]);

            BlockHeader bh6 = new BlockHeader();
            bh6.LoadVersion0(101, hashzero, DateTime.Now, blk1.Difficulty, new byte[10]);

            TransactionalBlock blk100 = new NormalBlock();
            blk100.LoadVersion0(bh5, blk1.coinbaseTxToMiner, blk1.transferTxs);
            blk100.UpdateMerkleRootHash();

            TransactionalBlock blk101 = new NormalBlock();
            blk101.LoadVersion0(bh6, blk1.coinbaseTxToMiner, blk1.transferTxs);
            blk101.UpdateMerkleRootHash();

            blockchain.pendingBlocks[101] = new Dictionary<Creahash, Block>();
            blockchain.rejectedBlocks[101] = new Dictionary<Creahash, Block>();

            BlockChain.UpdateChainReturnType type1 = blockchain.UpdateChain(blks[0]);

            if (type1 != BlockChain.UpdateChainReturnType.updated)
                throw new Exception("test18_5");

            if (blockchain.blocksCurrent.value != 1)
                throw new Exception("test18_6");
            for (int i = 0; i < blockchain.pendingBlocks.Length; i++)
            {
                if (blockchain.pendingBlocks[i] != null)
                    throw new Exception("test18_7");
                if (blockchain.rejectedBlocks[i] != null)
                    throw new Exception("test18_8");
            }

            bool flag2 = false;
            try
            {
                blockchain.UpdateChain(blk101);
            }
            catch (InvalidOperationException)
            {
                flag2 = true;
            }
            if (!flag2)
                throw new Exception("test18_9");

            BlockChain.UpdateChainReturnType type2 = blockchain.UpdateChain(blk100);

            if (type2 != BlockChain.UpdateChainReturnType.pending)
                throw new Exception("test18_10");

            if (blockchain.blocksCurrent.value != 1)
                throw new Exception("test18_11");
            for (int i = 0; i < blockchain.pendingBlocks.Length; i++)
            {
                if (i == 101)
                {
                    if (blockchain.pendingBlocks[i] == null)
                        throw new Exception("test18_12");
                    if (blockchain.pendingBlocks[i].Count != 1)
                        throw new Exception("test18_13");
                    if (!blockchain.pendingBlocks[i].Keys.Contains(blk100.Id))
                        throw new Exception("test18_14");
                }
                else
                {
                    if (blockchain.pendingBlocks[i] != null)
                        throw new Exception("test18_15");
                }

                if (blockchain.rejectedBlocks[i] != null)
                    throw new Exception("test18_16");
            }

            BlockHeader bh1 = new BlockHeader();
            bh1.LoadVersion0(1, hashzero, DateTime.Now, blk1.Difficulty, new byte[10]);

            TransactionalBlock blk1_2 = new NormalBlock();
            blk1_2.LoadVersion0(bh1, blk1.coinbaseTxToMiner, blk1.transferTxs);
            blk1_2.UpdateMerkleRootHash();

            BlockChain.UpdateChainReturnType type3 = blockchain.UpdateChain(blk1_2);

            if (type3 != BlockChain.UpdateChainReturnType.pending)
                throw new Exception("test18_17");

            if (blockchain.blocksCurrent.value != 1)
                throw new Exception("test18_18");
            for (int i = 0; i < blockchain.pendingBlocks.Length; i++)
            {
                if (i == 2)
                {
                    if (blockchain.pendingBlocks[i] == null)
                        throw new Exception("test18_19");
                    if (blockchain.pendingBlocks[i].Count != 1)
                        throw new Exception("test18_20");
                    if (!blockchain.pendingBlocks[i].Keys.Contains(blk1_2.Id))
                        throw new Exception("test18_21");
                }
                else if (i == 101)
                {
                    if (blockchain.pendingBlocks[i] == null)
                        throw new Exception("test18_22");
                    if (blockchain.pendingBlocks[i].Count != 1)
                        throw new Exception("test18_23");
                    if (!blockchain.pendingBlocks[i].Keys.Contains(blk100.Id))
                        throw new Exception("test18_24");
                }
                else
                {
                    if (blockchain.pendingBlocks[i] != null)
                        throw new Exception("test18_25");
                }

                if (blockchain.rejectedBlocks[i] != null)
                    throw new Exception("test18_26");
            }

            BlockHeader bh2 = new BlockHeader();
            bh2.LoadVersion0(1, blks[0].Id, DateTime.Now, blk1.Difficulty, new byte[10]);

            TransactionalBlock blk1_3 = new NormalBlock();
            blk1_3.LoadVersion0(bh2, blk1.coinbaseTxToMiner, blk1.transferTxs);
            blk1_3.UpdateMerkleRootHash();

            BlockChain.UpdateChainReturnType type4 = blockchain.UpdateChain(blk1_3);

            if (type4 != BlockChain.UpdateChainReturnType.rejected)
                throw new Exception("test18_27");

            if (blockchain.blocksCurrent.value != 1)
                throw new Exception("test18_28");
            for (int i = 0; i < blockchain.pendingBlocks.Length; i++)
            {
                if (i == 2)
                {
                    if (blockchain.pendingBlocks[i] == null)
                        throw new Exception("test18_29");
                    if (blockchain.pendingBlocks[i].Count != 1)
                        throw new Exception("test18_30");
                    if (!blockchain.pendingBlocks[i].Keys.Contains(blk1_2.Id))
                        throw new Exception("test18_31");

                    if (blockchain.rejectedBlocks[i] == null)
                        throw new Exception("test18_32");
                    if (blockchain.rejectedBlocks[i].Count != 1)
                        throw new Exception("test18_33");
                    if (!blockchain.rejectedBlocks[i].Keys.Contains(blk1_3.Id))
                        throw new Exception("test18_34");
                }
                else if (i == 101)
                {
                    if (blockchain.pendingBlocks[i] == null)
                        throw new Exception("test18_35");
                    if (blockchain.pendingBlocks[i].Count != 1)
                        throw new Exception("test18_36");
                    if (!blockchain.pendingBlocks[i].Keys.Contains(blk100.Id))
                        throw new Exception("test18_37");

                    if (blockchain.rejectedBlocks[i] != null)
                        throw new Exception("test18_38");
                }
                else
                {
                    if (blockchain.pendingBlocks[i] != null)
                        throw new Exception("test18_39");

                    if (blockchain.rejectedBlocks[i] != null)
                        throw new Exception("test18_40");
                }
            }

            TransactionalBlock blk1_4 = TransactionalBlock.GetBlockTemplate(1, blk1.coinbaseTxToMiner, blk2.transferTxs, _indexToBlock, 0);

            while (true)
            {
                blk1_4.UpdateTimestamp(DateTime.Now);
                blk1_4.UpdateNonce(nonce);

                if (blk1_4.Id.CompareTo(blk1_4.header.difficulty.Target) <= 0)
                {
                    Console.WriteLine("block1_4 mined.");

                    break;
                }

                int index = nonce.Length.RandomNum();
                int value = 256.RandomNum();

                nonce[index] = (byte)value;
            }

            BlockChain.UpdateChainReturnType type5 = blockchain.UpdateChain(blk1_4);

            if (type5 != BlockChain.UpdateChainReturnType.rejected)
                throw new Exception("test18_41");

            if (blockchain.blocksCurrent.value != 1)
                throw new Exception("test18_42");
            for (int i = 0; i < blockchain.pendingBlocks.Length; i++)
            {
                if (i == 2)
                {
                    if (blockchain.pendingBlocks[i] == null)
                        throw new Exception("test18_43");
                    if (blockchain.pendingBlocks[i].Count != 1)
                        throw new Exception("test18_44");
                    if (!blockchain.pendingBlocks[i].Keys.Contains(blk1_2.Id))
                        throw new Exception("test18_45");

                    if (blockchain.rejectedBlocks[i] == null)
                        throw new Exception("test18_46");
                    if (blockchain.rejectedBlocks[i].Count != 2)
                        throw new Exception("test18_47");
                    if (!blockchain.rejectedBlocks[i].Keys.Contains(blk1_3.Id))
                        throw new Exception("test18_48");
                    if (!blockchain.rejectedBlocks[i].Keys.Contains(blk1_4.Id))
                        throw new Exception("test18_49");
                }
                else if (i == 101)
                {
                    if (blockchain.pendingBlocks[i] == null)
                        throw new Exception("test18_50");
                    if (blockchain.pendingBlocks[i].Count != 1)
                        throw new Exception("test18_51");
                    if (!blockchain.pendingBlocks[i].Keys.Contains(blk100.Id))
                        throw new Exception("test18_52");

                    if (blockchain.rejectedBlocks[i] != null)
                        throw new Exception("test18_53");
                }
                else
                {
                    if (blockchain.pendingBlocks[i] != null)
                        throw new Exception("test18_54");

                    if (blockchain.rejectedBlocks[i] != null)
                        throw new Exception("test18_55");
                }
            }

            BlockChain.UpdateChainReturnType type8 = blockchain.UpdateChain(blk1_3);

            if (type8 != BlockChain.UpdateChainReturnType.invariable)
                throw new Exception("test18_56");

            if (blockchain.blocksCurrent.value != 1)
                throw new Exception("test18_57");
            for (int i = 0; i < blockchain.pendingBlocks.Length; i++)
            {
                if (i == 2)
                {
                    if (blockchain.pendingBlocks[i] == null)
                        throw new Exception("test18_58");
                    if (blockchain.pendingBlocks[i].Count != 1)
                        throw new Exception("test18_59");
                    if (!blockchain.pendingBlocks[i].Keys.Contains(blk1_2.Id))
                        throw new Exception("test18_60");

                    if (blockchain.rejectedBlocks[i] == null)
                        throw new Exception("test18_61");
                    if (blockchain.rejectedBlocks[i].Count != 2)
                        throw new Exception("test18_62");
                    if (!blockchain.rejectedBlocks[i].Keys.Contains(blk1_3.Id))
                        throw new Exception("test18_63");
                    if (!blockchain.rejectedBlocks[i].Keys.Contains(blk1_4.Id))
                        throw new Exception("test18_64");
                }
                else if (i == 101)
                {
                    if (blockchain.pendingBlocks[i] == null)
                        throw new Exception("test18_65");
                    if (blockchain.pendingBlocks[i].Count != 1)
                        throw new Exception("test18_66");
                    if (!blockchain.pendingBlocks[i].Keys.Contains(blk100.Id))
                        throw new Exception("test18_67");

                    if (blockchain.rejectedBlocks[i] != null)
                        throw new Exception("test18_68");
                }
                else
                {
                    if (blockchain.pendingBlocks[i] != null)
                        throw new Exception("test18_69");

                    if (blockchain.rejectedBlocks[i] != null)
                        throw new Exception("test18_70");
                }
            }

            BlockChain.UpdateChainReturnType type6 = blockchain.UpdateChain(blk1);

            if (type6 != BlockChain.UpdateChainReturnType.updated)
                throw new Exception("test18_71");

            BlockChain.UpdateChainReturnType type7 = blockchain.UpdateChain(blk1_3);

            if (type7 != BlockChain.UpdateChainReturnType.invariable)
                throw new Exception("test18_72");

            if (blockchain.blocksCurrent.value != 2)
                throw new Exception("test18_73");
            for (int i = 0; i < blockchain.pendingBlocks.Length; i++)
            {
                if (i == 2)
                {
                    if (blockchain.pendingBlocks[i] == null)
                        throw new Exception("test18_74");
                    if (blockchain.pendingBlocks[i].Count != 1)
                        throw new Exception("test18_75");
                    if (!blockchain.pendingBlocks[i].Keys.Contains(blk1_2.Id))
                        throw new Exception("test18_76");

                    if (blockchain.rejectedBlocks[i] == null)
                        throw new Exception("test18_77");
                    if (blockchain.rejectedBlocks[i].Count != 2)
                        throw new Exception("test18_78");
                    if (!blockchain.rejectedBlocks[i].Keys.Contains(blk1_3.Id))
                        throw new Exception("test18_79");
                    if (!blockchain.rejectedBlocks[i].Keys.Contains(blk1_4.Id))
                        throw new Exception("test18_80");
                }
                else if (i == 101)
                {
                    if (blockchain.pendingBlocks[i] == null)
                        throw new Exception("test18_81");
                    if (blockchain.pendingBlocks[i].Count != 1)
                        throw new Exception("test18_82");
                    if (!blockchain.pendingBlocks[i].Keys.Contains(blk100.Id))
                        throw new Exception("test18_83");

                    if (blockchain.rejectedBlocks[i] != null)
                        throw new Exception("test18_84");
                }
                else
                {
                    if (blockchain.pendingBlocks[i] != null)
                        throw new Exception("test18_85");

                    if (blockchain.rejectedBlocks[i] != null)
                        throw new Exception("test18_86");
                }
            }

            BlockHeader bh3 = new BlockHeader();
            bh3.LoadVersion0(2, hashzero, DateTime.Now, blk2.Difficulty, new byte[10]);

            TransactionalBlock blk2_2 = new NormalBlock();
            blk2_2.LoadVersion0(bh3, blk2.coinbaseTxToMiner, blk2.transferTxs);
            blk2_2.UpdateMerkleRootHash();

            BlockChain.UpdateChainReturnType type9 = blockchain.UpdateChain(blk2_2);

            if (type9 != BlockChain.UpdateChainReturnType.pending)
                throw new Exception("test18_87");

            BlockHeader bh4 = new BlockHeader();
            bh4.LoadVersion0(2, blk1_2.Id, DateTime.Now, blk2.Difficulty, new byte[10]);

            TransactionalBlock blk2_3 = new NormalBlock();
            blk2_3.LoadVersion0(bh4, blk2.coinbaseTxToMiner, blk2.transferTxs);
            blk2_3.UpdateMerkleRootHash();

            BlockChain.UpdateChainReturnType type10 = blockchain.UpdateChain(blk2_3);

            if (type10 != BlockChain.UpdateChainReturnType.pending)
                throw new Exception("test18_88");

            BlockHeader bh7 = new BlockHeader();
            bh7.LoadVersion0(2, blk1_3.Id, DateTime.Now, blk2.Difficulty, new byte[10]);

            TransactionalBlock blk2_4 = new NormalBlock();
            blk2_4.LoadVersion0(bh7, blk2.coinbaseTxToMiner, blk2.transferTxs);
            blk2_4.UpdateMerkleRootHash();

            BlockChain.UpdateChainReturnType type13 = blockchain.UpdateChain(blk2_4);

            if (type13 != BlockChain.UpdateChainReturnType.rejected)
                throw new Exception("test18_91");

            for (int i = 2; i < blks2.Length; i++)
            {
                BlockChain.UpdateChainReturnType type11 = blockchain.UpdateChain(blks2[i]);

                if (type11 != BlockChain.UpdateChainReturnType.updated)
                    throw new Exception("test18_89");
            }

            TransactionalBlock blk10 = TransactionalBlock.GetBlockTemplate(10, blk2.coinbaseTxToMiner, blk2.transferTxs, _indexToBlock, 0);

            while (true)
            {
                blk10.UpdateTimestamp(DateTime.Now);
                blk10.UpdateNonce(nonce);

                if (blk10.Id.CompareTo(blk10.header.difficulty.Target) <= 0)
                {
                    Console.WriteLine("block10 mined.");

                    break;
                }

                int index = nonce.Length.RandomNum();
                int value = 256.RandomNum();

                nonce[index] = (byte)value;
            }

            BlockChain.UpdateChainReturnType type12 = blockchain.UpdateChain(blk10);

            if (type12 != BlockChain.UpdateChainReturnType.rejected)
                throw new Exception("test18_90");

            Console.WriteLine("test18_succeeded");
        }
Ejemplo n.º 8
0
    public static Block[] StageLoad(string stagename)
    {
        string       path         = Application.dataPath + "/Resources/StageFiles/" + stagename + ".csv";
        FileInfo     fileInfo     = new FileInfo(path);
        FileStream   fileStream   = fileInfo.OpenRead();
        StreamReader streamReader = new StreamReader(fileStream);
        string       data         = streamReader.ReadToEnd();

#if UNITY_EDITOR
        Debug.Log(data);
#endif
        // 改行で分割(各ブロックごとに
        string[] enter = { "\n" };
        string[] arr   = data.Split(enter, StringSplitOptions.None);
        // 1行目のLength情報を取得
        string[] comma = { "," };
        string[] temp  = arr[0].Split(comma, StringSplitOptions.None);
        Block[]  stage = new Block[int.Parse(temp[1])];
        // 最後の改行を除くので-1
        for (int i = 1; i < arr.Length - 1; i++)
        {
            Block block = null;
            // それぞれ型名で書き出しているので型名で検索かければOK
            if (arr[i].IndexOf(nameof(GoalBlock)) >= 0)
            {
                block = GoalBlock.Instantiate();
            }
            else if (arr[i].IndexOf(nameof(NormalBlock)) >= 0)
            {
                block = NormalBlock.Instantiate();
            }
            else if (arr[i].IndexOf(nameof(CannotBlock)) >= 0)
            {
                block = CannotBlock.Instantiate();
            }
            else if (arr[i].IndexOf(nameof(AcidBlock)) >= 0)
            {
                block = AcidBlock.Instantiate();
            }
            else if (arr[i].IndexOf(nameof(FixedBlock)) >= 0)
            {
                block = FixedBlock.Instantiate();
            }
            // 矢印は方向プロパティも読み込む
            else if (arr[i].IndexOf(nameof(ArrowBlock)) >= 0)
            {
                string[] direction = arr[i].Split(comma, StringSplitOptions.None);
                block = ArrowBlock.Instantiate((ArrowBlock.Direction) int.Parse(direction[1]));
            }
            // 中間ブロックはレベルプロパティも読み込む
            else if (arr[i].IndexOf(nameof(FriendBlock)) >= 0)
            {
                string[] level = arr[i].Split(comma, StringSplitOptions.None);
                block = FriendBlock.Instantiate(int.Parse(level[1]));
            }
            // どれにも一致しなければファイル破損として扱う
            else
            {
                Debug.Log(stagename + " is corrupted!");
                Debug.Log(arr[i]);
                return(null);
            }
            stage[i - 1] = block;
        }
        return(stage);
    }
Ejemplo n.º 9
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="field"></param>
        /// <param name="withWall"></param>
        /// <param name="leftPadding"></param>
        public static void Draw(Field field, bool withWall, int leftPadding)
        {
            var defaultForegroundColor = Console.ForegroundColor;

            Console.ForegroundColor = ConsoleColor.Red;
            for (int y = 0; y < field.Height; ++y)
            {
                for (int p = 0; p < leftPadding; ++p)
                {
                    System.Console.Write(PaddingSpace.ToString());
                }

                if (withWall)
                {
                    Console.ForegroundColor = ConsoleColor.Gray;
                    System.Console.Write(Wall);
                    Console.ForegroundColor = ConsoleColor.Red;
                }

                for (int x = 0; x < field.Width; ++x)
                {
                    System.Console.Write("{0}", field.FieldBlocks[y, x] == 0 ? EmptyBlock.ToString() : NormalBlock.ToString());
                }

                if (withWall)
                {
                    Console.ForegroundColor = ConsoleColor.Gray;
                    System.Console.Write(Wall);
                }

                System.Console.WriteLine();
            }

            for (int p = 0; p < leftPadding; ++p)
            {
                System.Console.Write(PaddingSpace.ToString());
            }

            if (withWall)
            {
                Console.ForegroundColor = ConsoleColor.Gray;

                for (int x = 0; x < field.Width + 2; ++x)
                {
                    System.Console.Write(Wall);
                }
                System.Console.WriteLine();
            }

            Console.ForegroundColor = defaultForegroundColor;
        }
Ejemplo n.º 10
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="block"></param>
        /// <param name="leftPadding"></param>
        public static void Draw(Defs.Blocks[,] block, int leftPadding)
        {
            var defaultForegroundColor = Console.ForegroundColor;

            Console.ForegroundColor = ConsoleColor.Red;
            for (int y = 0; y < block.GetLength(0); ++y)
            {
                for (int p = 0; p < leftPadding; ++p)
                {
                    System.Console.Write(PaddingSpace.ToString());
                }

                for (int x = 0, max = block.GetLength(1); x < max; ++x)
                {
                    System.Console.Write("{0}", block[y, x] == 0 ? EmptyBlock.ToString() : NormalBlock.ToString());
                }
                System.Console.WriteLine();
            }
            Console.ForegroundColor = defaultForegroundColor;
        }
Ejemplo n.º 11
0
        private void Generate(int width, int height, int seed)
        {
            int centerHoleDiameter = 10;

            generatorDrawer = bot.Room.BlockDrawerPool.CreateBlockDrawer(1);
            Random random = new Random();

            Graphics.Tools.Noise.Primitive.SimplexPerlin noise = new Graphics.Tools.Noise.Primitive.SimplexPerlin(seed, NoiseQuality.Best);
            BlockMap blockMap = new BlockMap(bot, width, height);

            for (int x = 1; x < width - 1; x++)
            {
                for (int y = 1; y < height - 1; y++)
                {
                    double distanceFromCenter    = Math.Sqrt(Math.Pow(x - width / 2, 2) + Math.Pow(y - height / 2, 2)) / ((width > height) ? width : height) * 2;
                    double distanceFromCenterPow = Math.Pow(distanceFromCenter, 1.5);

                    //if (noise.GetValue(x * 0.015625F, y * 0.015625F, 0) > 1 - 0.25F * distanceFromCenterPow)                 // slimy mud
                    //    blockMap.setBlock(x, y, new NormalBlock(21, 0));

                    //else if (noise.GetValue(x * 0.03125F, y * 0.03125F, 32) > 1 - 0.75 * distanceFromCenter)      // slimy mud
                    //    blockMap.setBlock(x, y, new NormalBlock(21, 0));

                    //else if (noise.GetValue(x * 0.015625F, y * 0.015625F, 48) > 1 - 0.5 * distanceFromCenter) // Water
                    //    blockMap.setBlock(x, y, new NormalBlock(197, 0));

                    //else if (noise.GetValue(x * 0.03125F, y * 0.03125F, 64) > 1 - 0.75 * distanceFromCenter) //wet stones
                    //    blockMap.setBlock(x, y, new NormalBlock(197, 0));

                    if (noise.GetValue(x * 0.0078125F, y * 0.0078125F, 96) > 1 - 0.75 * distanceFromCenterPow)
                    {
                        blockMap.setBlock(x, y, new NormalBlock((int)Blocks.Stone, 0));
                    }

                    else if (noise.GetValue(x * 0.015625F, y * 0.015625F, 128) > 1 - 0.75 * distanceFromCenter)
                    {
                        blockMap.setBlock(x, y, new NormalBlock((int)Blocks.Stone, 0));
                    }

                    else if (distanceFromCenter + 0.33 * noise.GetValue(x * 0.015625F, y * 0.015625F, 48) > 1)
                    {
                        blockMap.setBlock(x, y, new NormalBlock((int)1022, 0));
                    }

                    else if (distanceFromCenter + 0.33 * noise.GetValue(x * 0.015625F, y * 0.015625F, 48) > 0.5)
                    {
                        blockMap.setBlock(x, y, new NormalBlock((int)Skylight.BlockIds.Blocks.Sand.GRAY, 0));
                    }

                    else// if (noise.GetValue(x * 0.015625F, y * 0.015625F, 160) > 0)
                    {
                        blockMap.setBlock(x, y, new NormalBlock(Skylight.BlockIds.Blocks.Sand.BROWN, 0));
                    }
                }
            }

            Queue <BlockWithPos> blockQueue = new Queue <BlockWithPos>();

            for (int i = 0; i < 64; i++)
            {
                blockQueue.Enqueue(new BlockWithPos(random.Next(1, width - 1), random.Next(1, height - 1), new NormalBlock((int)Blocks.Stone, 0)));
            }
            for (int i = 0; i < 64; i++)
            {
                blockQueue.Enqueue(new BlockWithPos(random.Next(1, width - 1), random.Next(1, height - 1), new NormalBlock((int)Blocks.Copper, 0)));
            }
            for (int i = 0; i < 32; i++)
            {
                blockQueue.Enqueue(new BlockWithPos(random.Next(1, width - 1), random.Next(1, height - 1), new NormalBlock((int)Blocks.Iron, 0)));
            }
            for (int i = 0; i < 16; i++)
            {
                blockQueue.Enqueue(new BlockWithPos(random.Next(1, width - 1), random.Next(1, height - 1), new NormalBlock((int)Blocks.Gold, 0)));
            }
            for (int i = 0; i < 8; i++)
            {
                blockQueue.Enqueue(new BlockWithPos(random.Next(1, width - 1), random.Next(1, height - 1), new NormalBlock((int)Blocks.Emerald, 0)));
            }

            int amount = 1536;//2048 later

            while (blockQueue.Count > 0 && amount > 0)
            {
                BlockWithPos block = blockQueue.Dequeue();

                blockMap.setBlock(block.X, block.Y, block.Block);

                if (random.Next(8) == 0)
                {
                    BlockWithPos block2 = null;

                    switch (random.Next(4))
                    {
                    case 0: block2 = new BlockWithPos(block.X + 1, block.Y, block.Block); break;

                    case 1: block2 = new BlockWithPos(block.X, block.Y + 1, block.Block); break;

                    case 2: block2 = new BlockWithPos(block.X - 1, block.Y, block.Block); break;

                    case 3: block2 = new BlockWithPos(block.X, block.Y - 1, block.Block); break;
                    }

                    Console.WriteLine("s");

                    if (block2 != blockMap.getBlock(0, block2.X, block2.Y) && block2.X > 1 && block2.Y > 1 && block2.X < width - 1 && block2.Y < height - 1)
                    {
                        blockQueue.Enqueue(block2);
                        blockMap.setBlock(block2.X, block2.Y, block2.Block);
                        amount--;
                    }
                }

                blockQueue.Enqueue(block);
            }

            //Make hole in center for the shop
            for (int x = width / 2 - (centerHoleDiameter / 2 + 1); x < width / 2 + centerHoleDiameter / 2; x++)
            {
                for (int y = height / 2 - (centerHoleDiameter / 2 + 1); y < height / 2 + centerHoleDiameter / 2; y++)
                {
                    blockMap.setBlock(x, y, new NormalBlock(414, 0));
                }
            }

            blockMap.setBlock(width / 2 - 1, height / 2 - 1, new NormalBlock(255, 0));
            Shop.SetLocation(width / 2 - 1, height / 2 - 2);
            blockMap.setBlock(width / 2 - 1, height / 2 - 2, new NormalBlock(Skylight.BlockIds.Blocks.Pirate.CHEST, 0));


            for (int x = 1; x < width - 1; x++)
            {
                for (int y = 1; y < height - 1; y++)
                {
                    if (blockMap.getBlock(0, x, y) != null)
                    {
                        IBlock block = blockMap.getBlock(0, x, y);
                        generatorDrawer.PlaceBlock(x, y, block);
                        IBlock background = null;
                        switch (block.Id)
                        {
                        case 197:
                            background = new NormalBlock(574, 1);
                            break;

                        case 21:
                            background = new NormalBlock(630, 1);
                            break;

                        default:
                            background = new NormalBlock(584, 1);
                            break;
                        }
                        //generatorDrawer.PlaceBlock(x, y, background);
                        resetBlockHardness(x, y, blockMap.getBlock(0, x, y).Id);
                    }
                }
            }
            generatorDrawer.Start();
        }