Example #1
0
    public void UpdateRules()
    {
        var packer = new Packer(stateCount);

        rules = Enumerable.Range(0, parallelRules)
                .Select(i => transform.GetChild(i % transform.childCount))
                .Select(c => c.GetComponent <SpacetimeDisplay>())
                .Select(d => new Rule(packer, BigInteger.Parse(d.code), EmulateComputeShader.tablePackN))
                .ToArray();
    }
Example #2
0
    public void Emulate()
    {
        using (var measureTime = new MeasureTime()) {
            var random = useSeed ? new Random(seed) : new Random();

            var packer = new Packer(stateCount);
            var rule   = new Rule(packer, BigInteger.Parse(ruleCode), tablePackN);

            measureTime.Mark("After rule inst");
            if (spacetime == null || spacetime.Length != timeSize || spacetime[0].spaceSize != spaceSize)
            {
                spacetime = new PackedSpace[timeSize];
                for (var t = 0; t < spacetime.Length; t++)
                {
                    spacetime[t] = new PackedSpace(packer, spaceSize);
                }
            }
            var emulator = new Emulator(
                rule, timeSize, spaceSize, startFill, borderFill, random);

            measureTime.Mark("After raw spacetime creation");
            emulator.Emulate(spacetime);

            measureTime.Mark("After emulate itself");

            var material = GetComponent <Renderer>().material;

            material.SetInt("_TimeSize", timeSize);
            material.SetInt("_SpaceSize", spaceSize);

            var spaceSizePacked = spacetime[0].packedSize;
            var bufferSize      = timeSize * spaceSizePacked;
            if (buffer is null || buffer.count != bufferSize)
            {
                buffer?.Dispose();
                buffer = new ComputeBuffer(bufferSize, sizeof(UInt32));
            }
            for (var t = 0; t < spacetime.Length; t++)
            {
                buffer.SetData(spacetime[t].packed, 0, t * spaceSizePacked, spaceSizePacked);
            }
            material.SetBuffer("_SpacetimePacked", buffer);
            measureTime.Mark("After shader");
        }
    }
    static void Main(String[] args)
    {
        var t = Number.Parse(Console.ReadLine());

        for (Number i = 0; i < t; i++)
        {
            var l = Console.ReadLine().Split(' ');
            var n = Number.Parse(l[0]);
            var k = Number.Parse(l[1]);
            var b = Number.Parse(l[2]);

            var r = Solve(n, k, b);
            if (r == null)
            {
                Console.WriteLine(-1);
            }
            else
            {
                Console.WriteLine(string.Join(" ", r));
            }
        }
    }
Example #4
0
        public static bool Invoke(Message message)
        {
            var split = message.Content.Split(' ');
            var root  = split[0];

            foreach (var info in AllCommands)
            {
                if (info.ValidateStrings?.Any(s => s == root) == true ||
                    info.Regex?.IsMatch(root) == true ||
                    info.MethodInfo.Name == root)
                {
                    if (info.RequireAdmin && !Config.Instance.GetPlayerConfig(message.Sender).Admin)
                    {
                        throw new DoudizhuCommandParseException("你不是管理");
                    }

                    var reqParameters = info.MethodInfo.GetParameters().SkipInject().ToArray();
                    var parameters    = split.Skip(1).ToArray();
                    if (parameters.Length > reqParameters.Length)
                    {
                        throw new DoudizhuCommandParseException("参数数量过多");
                    }
                    if (parameters.Length < reqParameters.Length - reqParameters.Count(p => p.IsOptional))
                    {
                        throw new DoudizhuCommandParseException("参数不全");
                    }

                    // input check
                    for (var index = 0; index < parameters.Length; index++)
                    {
                        var param    = parameters[index];
                        var reqparam = reqParameters[index];
                        if (string.IsNullOrWhiteSpace(param))
                        {
                            throw new DoudizhuCommandParseException($"参数{index}为空, 需求为{GetParamHelp(reqparam)}");
                        }
                        if (reqparam.ParameterType == typeof(Number) && !Number.TryParse(param, out _))
                        {
                            throw new DoudizhuCommandParseException($"参数{index}不是数字, 需求为{GetParamHelp(reqparam)}");
                        }
                    }
                    var game          = new Lazy <Game>(() => GamesManager.Games[message.Group]);
                    var currentPlayer = new Lazy <Player>(() => game.Value.CurrentPlayer);
                    var senderPlayer  = new Lazy <Player>(() => game.Value.GetPlayer(message.Sender));
                    // attrib check


                    var stateOnly = info.MethodInfo.GetCustomAttribute <StateOnlyAttribute>();
                    if (stateOnly != null)
                    {
                        if (!GamesManager.Games.ContainsKey(message.Group))
                        {
                            throw new DoudizhuCommandParseException("该群没有游戏.");
                        }

                        if (stateOnly.GameState != game.Value.State)
                        {
                            throw new DoudizhuCommandParseException($"该命令需求游戏在以下状态{stateOnly.GameState}.");                                          //TODO
                        }
                    }

                    var playerOnly = info.MethodInfo.GetCustomAttribute <PlayerOnlyAttribute>();
                    if (playerOnly != null)
                    {
                        if (!GamesManager.Games.ContainsKey(message.Group))
                        {
                            throw new DoudizhuCommandParseException("该群没有游戏.");
                        }

                        switch (playerOnly.PlayerState)
                        {
                        case PlayerState.InGame:
                            if (senderPlayer.Value == null)
                            {
                                throw new DoudizhuCommandParseException("该命令需求你在游戏中.");
                            }
                            break;

                        case PlayerState.CurrentPlayer:
                            if (senderPlayer.Value == null || currentPlayer.Value != senderPlayer.Value)
                            {
                                throw new DoudizhuCommandParseException("该命令需求你是当前玩家.");
                            }
                            break;

                        case PlayerState.ChooseLandlordPlayer:
                            if (senderPlayer.Value != game.Value.GetPlayerByIndex(((ChooseLandlordData)game.Value.StateData).LandlordIndex))
                            {
                                throw new DoudizhuCommandParseException("该命令需求你是当前玩家.");
                            }
                            break;

                        default:
                            throw new ArgumentOutOfRangeException();
                        }
                    }


                    // build params
                    var iReqParams = info.MethodInfo.GetParameters();
                    Debug.Assert(iReqParams.Count(obj => obj.IsAttributeDefined <InjectAttribute>()) == 0 ||
                                 iReqParams.Length == 0 ||
                                 (iReqParams.First().IsAttributeDefined <InjectAttribute>() &&
                                  iReqParams.Select((param, index) => new { param, index })
                                  .Where(obj => obj.param.IsAttributeDefined <InjectAttribute>())
                                  .Select(obj => obj.index)
                                  .ToArray().IsSequential(i => i)));


                    var list      = new List <object>();
                    var callbacks = new HashSet <Action>();

                    foreach (var pinfo in iReqParams.Where(p => p.IsAttributeDefined <InjectAttribute>()))
                    {
                        if (pinfo.ParameterType == typeof(Config))
                        {
                            list.Add(Config.Instance);
                            callbacks.Add(Config.Save);
                            continue;
                        }
                        if (pinfo.ParameterType == typeof(Game))
                        {
                            if (!GamesManager.Games.ContainsKey(message.Group))
                            {
                                throw new DoudizhuCommandParseException("该群没有游戏, 请先使用[创建游戏].");
                            }
                            list.Add(game.Value);
                            continue;
                        }

                        if (pinfo.ParameterType == typeof(PlayerConfig))
                        {
                            list.Add(Config.Instance.GetPlayerConfig(message.Sender));
                            callbacks.Add(Config.Save);
                            continue;
                        }

                        if (pinfo.ParameterType.IsSubclassOf(typeof(StateData)))
                        {
                            list.Add(game.Value.StateData);
                            continue;
                        }

                        if (pinfo.ParameterType == typeof(TargetSender))
                        {
                            list.Add(message.Group.GetGroupSender());
                            continue;
                        }

                        if (pinfo.ParameterType == typeof(Player))
                        {
                            list.Add(senderPlayer.Value);
                            continue;
                        }
                        var attrib = pinfo.GetCustomAttribute <InjectAttribute>();
                        switch (attrib.Injectwhat)
                        {
                        case Injects.PlayerID:
                            list.Add(message.Sender);
                            continue;

                        case Injects.GameID:
                            list.Add(message.Group);
                            continue;
                        }

                        Debug.Assert(1 + 1 != 2);
                    }


                    for (var index = 0; index < reqParameters.Length; index++)
                    {
                        var param    = parameters.SafeGet(index);
                        var reqparam = reqParameters[index];
                        if (reqparam.IsOptional && param == null)
                        {
                            list.Add(Type.Missing);
                            continue;
                        }
                        if (reqparam.ParameterType == typeof(Number))
                        {
                            list.Add(Number.Parse(param));
                            continue;
                        }
                        if (reqparam.ParameterType == typeof(string))
                        {
                            list.Add(param);
                            continue;
                        }

                        throw new NotSupportedException("Fork 您");
                    }

                    var result = info.MethodInfo.Invoke(Activator.CreateInstance(info.MethodInfo.DeclaringType), list.ToArray());
                    if (result == null)
                    {
                        return(true);
                    }

                    switch (result)
                    {
                    case string s:
                        message.Group.GetGroupSender().Send(s);
                        break;

                    default:
                        throw new NotSupportedException("Fork 您");
                    }
                    callbacks.ForEach(c => c());
                    // TODO
                    return(true);
                }
            }

            return(false);
        }
Example #5
0
 public virtual string GetFullCode(string code)
 => GetFullCode(BigInteger.Parse(code)).ToString();
Example #6
0
 public virtual int[] GetFullTable(string code)
 => GetFullTable(BigInteger.Parse(code));