コード例 #1
0
        public void Tick()
        {
            tick++;
            foreach (Rule r in rules)
            {
                if (r.lastTick + tick >= r.lastTick + r.rate)
                {
                    bool success = r.Run(this);
                    r.PostRun(success);
                    r.lastTick = 0;
                }
            }

            foreach (Unit u in units)
            {
                foreach (string rule in u.rules)
                {
                    UnitRule r = unitRules[rule];
                    if (r.lastTick + tick >= r.lastTick + r.rate)
                    {
                        bool success = r.Run(this, u);
                        r.PostRun(success);
                        r.lastTick = 0;
                    }
                }
            }
        }
コード例 #2
0
        public static void LoadScript(Sim sim, string filename)
        {
            List <string> simulation  = TextFileReader.LoadTextList(filename);
            bool          inRule      = false;
            int           lineNr      = 0;
            Rule          currentRule = new Rule("invalid rule - check description");

            foreach (string line in simulation)
            {
                string l = line.Trim();
                if (l[0] != '#')
                {
                    lineNr++;
                    continue;
                }

                string[] tokens = l.Split('\t');

                switch (tokens[0])
                {
                case "sim":
                    sim.name = tokens[1];
                    break;

                case "resource":
                    sim.CheckResource(tokens[1]);
                    break;

                case "globalBin":
                    sim.CheckResource(tokens[1]);
                    sim.globalBins[tokens[1]] = new Bin(tokens[1], int.Parse(tokens[2]));
                    break;

                case "rule":
                    Debug.Assert(!inRule, "LoadScript warning on line " + lineNr + ": Rule definition while in rule: '" + tokens[0] + "'");
                    inRule      = true;
                    currentRule = new Rule(tokens[1]);
                    break;

                case "unitRule":
                    Debug.Assert(!inRule, "LoadScript warning on line " + lineNr + ": Rule definition while in rule: '" + tokens[0] + "'");
                    inRule      = true;
                    currentRule = new UnitRule(tokens[1]);
                    break;

                case "mapRule":
                    Debug.Assert(!inRule, "LoadScript warning on line " + lineNr + ": Rule definition while in rule: '" + tokens[0] + "'");
                    inRule      = true;
                    currentRule = new MapRule(tokens[1]);
                    break;

                // in rule parsing
                case "rate":
                    Debug.Assert(inRule, "LoadScript warning on line " + lineNr + ": Spurious symbol '" + tokens[0] + "'");
                    currentRule.rate = int.Parse(tokens[1]);
                    break;

                case "global":
                    Debug.Assert(inRule, "LoadScript warning on line " + lineNr + ": Spurious symbol '" + tokens[0] + "'");
                    if (tokens[2] == "in")
                    {
                        currentRule.inResourceGlobal[tokens[1]] = int.Parse(tokens[3]);
                    }
                    else
                    {
                        currentRule.outResourceGlobal[tokens[1]] = int.Parse(tokens[3]);
                    }
                    break;

                case "local":
                    Debug.Assert(inRule, "LoadScript warning on line " + lineNr + ": Spurious symbol '" + tokens[0] + "'");
                    if (tokens[2] == "in")
                    {
                        (currentRule as UnitRule).inResourceLocal[tokens[1]] = int.Parse(tokens[3]);
                    }
                    else
                    {
                        (currentRule as UnitRule).outResourceLocal[tokens[1]] = int.Parse(tokens[3]);
                    }
                    break;

                case "map":
                    Debug.Assert(inRule, "LoadScript warning on line " + lineNr + ": Spurious symbol '" + tokens[0] + "'");
                    if (tokens[2] == "in")
                    {
                        (currentRule as MapRule).inResourceMap[tokens[1]] = int.Parse(tokens[3]);
                    }
                    else
                    {
                        (currentRule as MapRule).outResourceMap[tokens[1]] = int.Parse(tokens[3]);
                    }
                    break;

                case "onSucceeded":
                    Debug.Assert(inRule, "LoadScript warning on line " + lineNr + ": Spurious symbol '" + tokens[0] + "'");
                    currentRule.onSucceeded.Add(tokens[1]);
                    break;

                case "onFailed":
                    Debug.Assert(inRule, "LoadScript warning on line " + lineNr + ": Spurious symbol '" + tokens[0] + "'");
                    currentRule.onFailed.Add(tokens[1]);
                    break;

                case "behaviour":
                    Debug.Assert(inRule, "LoadScript warning on line " + lineNr + ": Spurious symbol '" + tokens[0] + "'");
                    Debug.Assert(currentRule.type == RuleTypes.UNIT_RULE, "LoadScript warning on line " + lineNr + ": Rule '" + tokens[0] + "' not unit rule but has behaviour");
                    if (tokens[1] == "destination")
                    {
                        (currentRule as UnitRule).destination = tokens[2];
                    }
                    if (tokens[1] == "prefab")
                    {
                        (currentRule as UnitRule).prefab = tokens[2];
                    }
                    break;

                case "create":                         // only units can create units for now
                    Debug.Assert(inRule, "LoadScript warning on line " + lineNr + ": Spurious symbol '" + tokens[0] + "'");
                    Debug.Assert(currentRule.type == RuleTypes.UNIT_RULE, "LoadScript warning on line " + lineNr + ": Rule '" + tokens[0] + "' not unit rule but has behaviour");
                    if (tokens[1] == "prefab")
                    {
                        (currentRule as UnitRule).createPrefab = tokens[2];
                    }
                    break;


                case "end":
                    Debug.Assert(inRule, "LoadScript warning on line " + lineNr + ": Spurious symbol '" + tokens[0] + "'");

                    if (currentRule.type == RuleTypes.RULE)
                    {
                        sim.rules.Add(currentRule);
                    }
                    else if (currentRule.type == RuleTypes.UNIT_RULE)
                    {
                        sim.unitRules[currentRule.name] = currentRule as UnitRule;
                    }
                    else if (currentRule.type == RuleTypes.MAP_RULE)
                    {
                        sim.mapRules.Add(currentRule as MapRule);
                    }
                    inRule = false;
                    break;

                default:
                    Debug.LogWarning("LoadScript warning on line " + lineNr + ": Ignoring line! No parsing rule for " + tokens[0]);
                    break;
                }

                // Resource res = new Resource(items[1], items[2], items[3], System.Single.Parse(items[4]), System.Boolean.Parse(items[5]));
//				Debug.Log("Adding resource "+res.ID+" ("+res.Symbol+")");

                lineNr++;
            }
        }