示例#1
0
        public static List<Room> build(Player player, List<Node> nodes)
        {
            List<Room> rooms = new List<Room>();

            string startingRoomName = null;

            foreach (Node n in nodes)
            {
                if (n is Pair)
                {
                    Pair p = (Pair)n;
                    if (p.name.Equals("StartingRoom", StringComparison.OrdinalIgnoreCase))
                        startingRoomName = p.value;
                    else if (p.name.Equals("Intro", StringComparison.OrdinalIgnoreCase))
                        player.intro = p.value.Replace("\\n","\n");
                    else
                        throw new InvalidDataException();
                }
                else
                {
                    Room r = buildRoom(player, n);
                    rooms.Add(r);
                    if (r.name.Equals(startingRoomName))
                        player.room = r;
                }
            }

            foreach (Room r in rooms)
                foreach (Wall w in r.getWalls())
                    if (w.door != null && w.door.name != null)
                        w.door.room = getRoom(player, rooms, w.door.name);

            return rooms;
        }
示例#2
0
文件: World.cs 项目: ra4king/Dungeons
 public void execute(Player p, Item origin, Item target)
 {
     v.Value = newValue;
 }
示例#3
0
文件: World.cs 项目: ra4king/Dungeons
 public void execute(Player p, Item origin, Item target)
 {
     v.Value = (int)v.Value + 1;
 }
示例#4
0
文件: World.cs 项目: ra4king/Dungeons
            public void execute(Player p, Item origin, Item target)
            {
                Console.WriteLine("You " + (win ? "win!" : "lose!"));

                Console.Write("Press ENTER to exit... ");

                Console.Read();

                Environment.Exit(0);
            }
示例#5
0
文件: World.cs 项目: ra4king/Dungeons
            public void execute(Player p, Item origin, Item target)
            {
                if (!p.removeItem(origin))
                    throw new InvalidDataException("You don't have this item in your inventory.");

                target.addChild(origin);
            }
示例#6
0
文件: World.cs 项目: ra4king/Dungeons
            public void execute(Player p, Item origin, Item target)
            {
                object va;
                if (value is string)
                {
                    if (((string)value).Equals("%origin%", StringComparison.OrdinalIgnoreCase))
                    {
                        if (origin == null)
                            throw new InvalidDataException("No origin specified.");

                        va = origin.name;
                    }
                    else if (((string)value).Equals("%target%", StringComparison.OrdinalIgnoreCase))
                    {
                        if (target == null)
                            throw new InvalidDataException("No target specified.");

                        va = target.name;
                    }
                    else
                        va = value;
                }
                else
                {
                    va = value;
                }

                if (v.Value.Equals(va))
                    c(target == null ? "" : target.name);
                else if (va is string)
                    throw new InvalidDataException("Invalid target.");
            }
示例#7
0
        private static List<Item> buildItems(Player player, Wall wall, Node node)
        {
            List<Item> items = new List<Item>();

            foreach (Node n in node.children)
            {
                if (n.name.Equals("Door", StringComparison.OrdinalIgnoreCase))
                    continue;

                if (n is Pair)
                    throw new InvalidDataException("Cannot have a free standing pair under Items");
                else
                    items.Add(buildItem(player, wall, n));
            }

            return items;
        }
示例#8
0
文件: World.cs 项目: ra4king/Dungeons
            public void execute(Player p, Item origin, Item target)
            {
                if (p.inventory.Contains(origin))
                    throw new InvalidDataException("You already have this item in your inventory.");

                p.addItem(origin);
                p.room.removeItem(origin);
            }
示例#9
0
        private static Item buildItem(Player player, Wall wall, Node node)
        {
            Item item = new Item(wall, node.name);

            bool hasDescription = false;

            List<string> commands = new List<string>();
            commands.Add("use");
            commands.Add("examine");

            foreach (Node n in node.children)
            {
                if (n is Pair)
                {
                    Pair p = (Pair)n;

                    if (p.name.Equals("Description", StringComparison.OrdinalIgnoreCase))
                    {
                        item.description = p.value;
                        hasDescription = true;
                    }
                    else if (p.name.Equals("Takeable", StringComparison.OrdinalIgnoreCase))
                        item.takeable = bool.Parse(p.value);
                    else if (p.name.Equals("UseIfNotEquipped", StringComparison.OrdinalIgnoreCase))
                        item.useIfNotEquipped = bool.Parse(p.value);
                    else if (p.name.Equals("Visible", StringComparison.OrdinalIgnoreCase))
                        item.visible = bool.Parse(p.value);
                    else if (p.name.Equals("Detail", StringComparison.OrdinalIgnoreCase))
                        item.detail = p.value;
                    else if (p.name.Equals("Alias", StringComparison.OrdinalIgnoreCase))
                    {
                        string[] aliases = p.value.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);

                        foreach (string s in aliases)
                            item.addAlias(s.Trim());
                    }
                    else if (p.name.Equals("Commands", StringComparison.OrdinalIgnoreCase))
                    {
                        string[] cs = p.value.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);

                        foreach (string s in cs)
                            commands.Add(s.Trim());
                    }
                    else if (commands.Contains(p.name))
                    {
                        item.addCommand(p.name, createCommand(player, item, p.value, null));
                    }
                }
                else
                {
                    if (!n.name.Equals("Children", StringComparison.OrdinalIgnoreCase))
                        throw new InvalidDataException("Cannot have a node under an item except for Children.");

                    item.addAll(buildItems(player, wall, n));
                }
            }

            if (!hasDescription)
                throw new InvalidDataException("No description found in Item.");

            return item;
        }
示例#10
0
        private static Room getRoom(Player player, List<Room> rooms, string name)
        {
            foreach (Room r in rooms)
            {
                if (r.name == name)
                    return r;
            }

            throw new InvalidDataException(name + " isn't a valid room name.");
        }
示例#11
0
        private static Command createCommand(Player player, Item origin, string value, Dictionary<string, Instruction> variables)
        {
            value = value.Replace("[", "").Replace("]", "").Trim();

            string[] cs = value.Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries);

            if (cs.Length == 0)
                throw new InvalidDataException("No data found for this command.");

            List<Instruction> instructions = new List<Instruction>();

            if (variables == null)
                variables = new Dictionary<string, Instruction>();

            foreach (string s in cs)
            {
                string c = s.Trim();

                if (c.StartsWith("print", StringComparison.OrdinalIgnoreCase))
                    instructions.Add(InstructionFactory.createPrint(c.Substring(c.IndexOf('-') + 1).Trim()));
                else if (c.StartsWith("remove", StringComparison.OrdinalIgnoreCase))
                    instructions.Add(InstructionFactory.createRemove());
                else if (c.StartsWith("drop", StringComparison.OrdinalIgnoreCase))
                    instructions.Add(InstructionFactory.createDrop());
                else if (c.StartsWith("take", StringComparison.OrdinalIgnoreCase))
                    instructions.Add(InstructionFactory.createTake());
                else if (c.StartsWith("GameOver", StringComparison.OrdinalIgnoreCase))
                    instructions.Add(InstructionFactory.createGameOver(bool.Parse(c.ToLower().Replace("gameover","").Trim())));
                else if (c.StartsWith("if", StringComparison.OrdinalIgnoreCase))
                {
                    int i = c.IndexOf("if");
                    int e = c.IndexOf("then");

                    string cond = c.Substring(i + 2, e - 1 - 2).Trim();

                    Instruction variable;
                    object v;

                    if (cond.Contains("is"))
                    {
                        string[] conds = cond.Split(new string[] { "is" }, StringSplitOptions.RemoveEmptyEntries);

                        if (conds.Length != 2)
                            throw new InvalidDataException("Invalid conditional statement.");

                        variable = InstructionFactory.createVariable(conds[1].Trim());

                        v = conds[0].Trim();
                    }
                    else
                    {
                        string[] conds = cond.Split(new string[] { "==" }, StringSplitOptions.RemoveEmptyEntries);

                        if (conds.Length != 2)
                            throw new InvalidDataException("Invalid conditional statement.");

                        try
                        {
                            v = int.Parse(conds[1].Trim());
                        }
                        catch
                        {
                            v = conds[1].Trim();
                        }

                        variable = variables[conds[0].Trim()];
                    }

                    try
                    {
                        instructions.Add(InstructionFactory.createConditional(variable, v, createCommand(player, origin, c.Substring(e + 4).Replace(" >", ":").Trim(), variables)));
                    }
                    catch (KeyNotFoundException ex)
                    {
                        throw new InvalidDataException("Invalid variable:" + cond[0]);
                    }
                }
                else if (c.StartsWith("var ", StringComparison.OrdinalIgnoreCase))
                {
                    string[] var = c.Substring(4).Trim().Split(new char[] { '=' }, StringSplitOptions.RemoveEmptyEntries);

                    variables.Add(var[0].Trim(), InstructionFactory.createVariable(int.Parse(var[1].Trim())));
                }
                else if (c.Contains("++"))
                {
                    c = c.Replace("++", "").Trim();

                    try
                    {
                        instructions.Add(InstructionFactory.incrementVariable(variables[c]));
                    }
                    catch (KeyNotFoundException e)
                    {
                        throw new InvalidDataException("Invalid variable:" + c);
                    }
                }
                else if (c.Contains("--"))
                {
                    c = c.Replace("--", "").Trim();

                    try
                    {
                        instructions.Add(InstructionFactory.decrementVariable(variables[c]));
                    }
                    catch (KeyNotFoundException e)
                    {
                        throw new InvalidDataException("Invalid variable:" + c);
                    }
                }
                else if (c.Contains("="))
                {
                    string[] assignments = c.Split(new char[] { '=' }, StringSplitOptions.RemoveEmptyEntries);

                    if (assignments.Length == 0)
                        throw new InvalidDataException("No data found for this instruction.");
                    if (assignments.Length == 1)
                        throw new InvalidDataException("Invalid instruction:" + c);
                    if (assignments.Length > 2)
                        throw new InvalidDataException("Too many values:" + c);

                    string[] item = assignments[0].Split(new char[] { '.' }, StringSplitOptions.RemoveEmptyEntries);

                    if (item.Length == 1)
                    {
                        try
                        {
                            instructions.Add(InstructionFactory.modifyVariable(variables[item[0].Trim()], int.Parse(assignments[1].Trim())));
                        }
                        catch (KeyNotFoundException e)
                        {
                            throw new InvalidDataException("Invalid variable:" + item[0]);
                        }
                    }
                    else if (item.Length < 3)
                        throw new InvalidDataException("Too little fields:" + assignments[0]);
                    else if (item.Length > 3)
                        throw new InvalidDataException("Too many field:" + assignments[0]);
                    else
                        instructions.Add(InstructionFactory.createAssignment(item[0].Trim() + '.' + item[1].Trim(), item[2].Trim(), bool.Parse(assignments[1].Trim())));
                }
                else
                {
                    throw new InvalidDataException("Invalid instruction:" + c);
                }
            }

            return str =>
            {
                Item target;

                if (str.IndexOf(" door", StringComparison.OrdinalIgnoreCase) >= 0)
                {
                    str = str.ToLower().Replace(" door", "");

                    Wall w = player.room.getWall(str);
                    if (w == null)
                    {
                        Console.WriteLine("'" + str + "' is not a valid direction.");
                        return;
                    }

                    if (w.door == null)
                    {
                        Console.WriteLine("There is no door here.");
                        return;
                    }

                    target = w.door;
                }
                else
                {
                    target = player.getItem(str);
                    if (target == null)
                        target = player.room.getItem(str);
                }

                try
                {
                    foreach (Instruction i in instructions)
                        i.execute(player, origin, target);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }
            };
        }
示例#12
0
        private static void buildWalls(Player player, Room room, Node node)
        {
            foreach (Node n in node.children)
            {
                Direction d;

                try
                {
                    d = (Direction)Enum.Parse(typeof(Direction), n.name, true);
                }
                catch
                {
                    continue;
                }

                buildWall(player, room.getWall(d), n);
            }
        }
示例#13
0
        private static void buildWall(Player player, Wall wall, Node node)
        {
            foreach (Node n in node.children)
            {
                if (n.name.Equals("Door", StringComparison.OrdinalIgnoreCase))
                    wall.door = buildDoor(wall, n);
            }

            wall.items = buildItems(player, wall, node);
        }
示例#14
0
        private static Room buildRoom(Player player, Node node)
        {
            Room room = new Room(node.name);

            foreach (Node n in node.children)
            {
                if (n is Pair && n.name.Equals("Description", StringComparison.OrdinalIgnoreCase))
                    room.description = ((Pair)n).value;
                else if (n.name.Equals("Items", StringComparison.OrdinalIgnoreCase))
                    room.Items = buildItems(player, null, n);
            }

            buildWalls(player, room, node);

            return room;
        }
示例#15
0
文件: World.cs 项目: ra4king/Dungeons
            public void execute(Player p, Item origin, Item target)
            {
                string newS = "";
                if (origin != null)
                    newS = s.Replace("%origin%", origin.name).Trim();
                else if (s.Contains("%origin%"))
                    throw new InvalidDataException("No origin specified.");

                if (target != null)
                    newS = s.Replace("%target%", target.name).Trim();
                else if (s.Contains("%target%"))
                    throw new InvalidDataException("No target specified.");

                newS = newS.Replace("\\n", "\n");

                Console.WriteLine(newS);
            }
示例#16
0
文件: World.cs 项目: ra4king/Dungeons
 public void execute(Player p, Item origin, Item target)
 {
     if(!p.removeItem(origin))
         p.room.removeItem(origin);
 }
示例#17
0
文件: World.cs 项目: ra4king/Dungeons
        public World(string file)
        {
            player = new Player();

            List<Node> nodes = WorldParser.parseFile(file);
            worldName = nodes[0].name;
            rooms = WorldBuilder.build(player, nodes[0].children);

            Command look = s =>
            {
                if (s.StartsWith("at ", StringComparison.OrdinalIgnoreCase))
                    player.commands["examine"](s.Substring(3).Trim());
                else if (DirectionUtils.isDirection(s))
                    Console.WriteLine(player.room.getWall(s).look());
                else
                    Console.WriteLine(player.room.look());
            };

            Command move = s =>
            {
                Direction d;
                try
                {
                    d = DirectionUtils.parse(s);
                }
                catch
                {
                    Console.WriteLine("I don't think that is a valid direction :P");
                    return;
                }

                Wall wall = player.room.getWall(d);
                if (wall == null || wall.door == null || !wall.door.visible)
                    Console.WriteLine("You have nowhere to go.");
                else if (wall.door.locked)
                    Console.WriteLine("This door is locked. Find a key.");
                else
                {
                    player.room = wall.door.room;
                    look("");
                }
            };

            Command take = s =>
            {
                if (s == "")
                {
                    Console.WriteLine("Take what?");
                    return;
                }

                Item i = player.room.getItem(s);
                if (i == null)
                {
                    if (player.getItem(s) != null)
                        Console.WriteLine("This item is already equipped.");
                    else
                        Console.WriteLine("You can't see such an item.");
                    return;
                }

                if (!i.visible)
                {
                    Console.WriteLine("You can't see such an item.");
                    return;
                }

                if (!i.takeable)
                {
                    Console.WriteLine("Sorry you can't take this.");
                    return;
                }

                player.addItem(i);
                player.room.removeItem(i);

                Console.WriteLine(char.ToUpper(i.name[0]) + i.name.Substring(1) + " taken.");
            };

            Command examine = s =>
            {
                if (s == "")
                {
                    Console.WriteLine("Examine what?");
                    return;
                }

                Item i = player.getItem(s);

                if (i == null)
                {
                    i = player.room.getItem(s);

                    if (i == null || !i.visible)
                    {
                        Console.WriteLine("You can't see such an item.");
                        return;
                    }
                }

                if (!i.visible)
                {
                    Console.WriteLine("You can't see such an item.");
                    return;
                }

                Command c = i.getCommand("examine");
                if (c != null)
                    c("");

                Console.WriteLine(i.examine());
            };

            Command inventory = s =>
            {
                if (player.inventory.Count == 0)
                {
                    Console.WriteLine("Your inventory is empty.");
                    return;
                }

                Console.WriteLine("Your inventory:");

                foreach (Item i in player.inventory)
                    Console.WriteLine("  - " + i.name);
            };

            Command use = s =>
            {
                string[] ps = s.Split(new string[] { " on " }, StringSplitOptions.RemoveEmptyEntries);

                if (ps.Length == 0)
                {
                    Console.WriteLine("Use what?");
                    return;
                }

                Item i = player.getItem(ps[0].Trim());
                if (i == null)
                {
                    i = player.room.getItem(ps[0].Trim());

                    if (i == null || !i.visible)
                    {
                        Console.WriteLine("You can't see such an item.");
                        return;
                    }

                    if (!i.useIfNotEquipped)
                    {
                        Console.WriteLine("This item is not equipped.");
                        return;
                    }
                }

                if (!i.visible)
                {
                    Console.WriteLine("You can't see such an item.");
                    return;
                }

                Command c = i.getCommand("use");

                if (c == null)
                {
                    Console.WriteLine("You can't use this item.");
                    return;
                }

                string p = "";
                for (int a = 1; a < ps.Length; a++)
                    p += ps[a].Trim();

                c(p);
            };

            Command help = s =>
            {
                foreach (string c in player.commands.Keys)
                {
                    if (c == "?" || c == "help")
                        continue;

                    Console.WriteLine(c);
                }

                foreach (Item i in player.inventory)
                {
                    foreach (string c in i.commands.Keys)
                    {
                        if (!player.commands.ContainsKey(c))
                            Console.WriteLine(c);
                    }
                }
            };

            player.commands.Add("look", look);
            player.commands.Add("move", move);
            player.commands.Add("go", move);
            player.commands.Add("take", take);
            player.commands.Add("examine", examine);
            player.commands.Add("inventory", inventory);
            player.commands.Add("use", use);
            player.commands.Add("help", help);
            player.commands.Add("?", help);
        }
示例#18
0
文件: World.cs 项目: ra4king/Dungeons
 public void execute(Player p, Item origin, Item target)
 {
 }
示例#19
0
文件: World.cs 项目: ra4king/Dungeons
            public void execute(Player p, Item origin, Item target)
            {
                Item i;
                if (room.Equals("%origin%", StringComparison.OrdinalIgnoreCase))
                {
                    if (origin == null)
                        throw new InvalidDataException("No origin specified.");

                    i = origin;
                }
                else if (room.Equals("%target%", StringComparison.OrdinalIgnoreCase))
                {
                    if (target == null)
                        throw new InvalidDataException("No target specified.");

                    i = target;
                }
                else
                {
                    i = p.getItem(item);
                    if (i == null)
                        i = findItem(p.room);

                    if (i == null)
                        return;
                }

                if (field.Equals("visible",StringComparison.OrdinalIgnoreCase))
                    i.visible = value;
                else if (field.Equals("takeable",StringComparison.OrdinalIgnoreCase))
                    i.takeable = value;

                if (field.Equals("locked", StringComparison.OrdinalIgnoreCase))
                {
                    if (i is Door)
                    {
                        ((Door)i).locked = value;

                        string r = room;
                        r = r.ToLower().Replace("%target%", p.room.name);

                        Console.WriteLine(char.ToUpper(r[0]) + r.Substring(1) + "'s " + ((Door)i).parent.name + " door is " + (value ? "locked." : "unlocked."));
                    }
                    else
                        throw new InvalidDataException("Invalid target specified.");
                }
            }