Example #1
0
    private void InitFromFileWorker(string filename)
    {
        try
        {
            PathfindingType = (PathfindingType)Config.sv_pathfinding;

            AllodsMap mapStructure = AllodsMap.LoadFrom(filename);
            if (mapStructure == null)
            {
                //Core.Abort("Couldn't load \"{0}\"", filename);
                GameConsole.Instance.WriteLine("Couldn't load \"{0}\"", filename);
                Unload();
                return;
            }

            Width  = (int)mapStructure.Data.Width;
            Height = (int)mapStructure.Data.Height;

            // load in map.reg
            // for base terrain costs here
            Registry reg = null;
            try
            {
                reg = new Registry("world/data/map.reg");
            }
            catch (Exception)
            {
                reg = new Registry();
            }

            int[] NodeCosts = new int[]
            {
                reg.GetInt("Terrain", "CostLand", 8),
                reg.GetInt("Terrain", "CostGrass", 8),
                reg.GetInt("Terrain", "CostFlowers", 9),
                reg.GetInt("Terrain", "CostSand", 14),
                reg.GetInt("Terrain", "CostCracked", 6),
                reg.GetInt("Terrain", "CostStones", 12),
                reg.GetInt("Terrain", "CostSavanna", 11),
                reg.GetInt("Terrain", "CostMountain", 16),
                reg.GetInt("Terrain", "CostWater", 8),
                reg.GetInt("Terrain", "CostRoad", 6)
            };

            int[] NodeTileTypesInCell = new int[]
            {
                2, 3, 2, 4, 3, 4, 2, 2, 2, 2, 4, 4, 4, 4, 0, 0,
                3, 5, 3, 3, 1, 3, 2, 4, 2, 2, 4, 2, 4, 4, 0, 0,
                2, 3, 2, 4, 3, 4, 2, 4, 2, 2, 4, 2, 4, 4, 0, 0,
                5, 5, 5, 5, 5, 5, 2, 2, 2, 2, 4, 4, 4, 4, 0, 0
            };

            int[,] NodeTileTypes = new int[16, 2]
            {
                { 2, 1 }, { 5, 1 }, { 4, 1 }, { 7, 1 }, { 6, 1 }, { 5, 6 }, { 3, 7 }, { 8, 6 },
                { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 10, 1 }, { 0, 0 }, { 0, 0 }, { 0, 0 }
            };
            // end loading terrain costs

            Nodes = new MapNode[Width, Height];
            for (int y = 0; y < Height; y++)
            {
                for (int x = 0; x < Width; x++)
                {
                    MapNode node = Nodes[x, y] = new MapNode();
                    node.Tile   = (ushort)(mapStructure.Tiles[y * Width + x] & 0x3FF);
                    node.Height = mapStructure.Heights[y * Width + x];
                    node.Flags  = 0;
                    node.Light  = 255;

                    int   costTileType     = (node.Tile & 0x3C0) >> 6;
                    int   costTileBorder   = (node.Tile & 0x3F);
                    float costTileFactor   = (NodeTileTypesInCell[costTileBorder] - 1) / 4f; // 1..5 -> 0..4 -> 0..1
                    int   costSubTileType1 = NodeTileTypes[costTileType, 1] - 1;
                    int   costSubTileType2 = NodeTileTypes[costTileType, 0] - 1;
                    int   costSubTile1     = (costSubTileType1 >= 0) ? NodeCosts[costSubTileType1] : 0;
                    int   costSubTile2     = (costSubTileType2 >= 0) ? NodeCosts[costSubTileType2] : 0;
                    node.BaseWalkCost = (byte)((costSubTile1 * (1f - costTileFactor)) + (costSubTile2 * costTileFactor));

                    // 7 = rock
                    // 8 = water
                    int   walkType   = costSubTileType1;
                    float walkFactor = 1f - costTileFactor;
                    if (costSubTileType2 == 7 || costSubTileType2 == 8 || costSubTileType1 == -1)
                    {
                        walkType   = costSubTileType2;
                        walkFactor = costTileFactor;
                    }

                    if (walkType < 0 || walkType == 8 || (walkType == 7 && walkFactor > 0.25f))
                    {
                        node.Flags |= MapNodeFlags.BlockedTerrain;
                    }
                }
            }

            // load players
            foreach (AllodsMap.AlmPlayer almplayer in mapStructure.Players)
            {
                Player player = new Player(almplayer);
                Players.Add(player);
                //Debug.Log(string.Format("player ID={2} {0} (flags {1})", player.Name, player.Flags, player.ID));
            }

            GameManager.Instance.CallDelegateOnNextFrame(() =>
            {
                Speed = 5;
                return(false);
            });

            _TopObjectID = 0;

            // load obstacles
            for (int y = 0; y < Height; y++)
            {
                for (int x = 0; x < Width; x++)
                {
                    int typeId = mapStructure.Objects[y * Width + x];
                    if (typeId <= 0)
                    {
                        continue;
                    }
                    typeId -= 1;
                    MapObstacle mob = new MapObstacle(typeId);
                    mob.X = x;
                    mob.Y = y;
                    mob.LinkToWorld();
                    AddObject(mob, false);
                }
            }

            // load structures
            if (mapStructure.Structures != null)
            {
                foreach (AllodsMap.AlmStructure almstruc in mapStructure.Structures)
                {
                    MapStructure struc;
                    struc = new MapStructure(almstruc.TypeID);
                    if (struc.Class == null)
                    {
                        Debug.LogFormat("Tried to load invalid structure {0}", almstruc.TypeID);
                        continue;
                    }

                    struc.X      = (int)almstruc.X;
                    struc.Y      = (int)almstruc.Y;
                    struc.Health = almstruc.Health;
                    struc.Tag    = almstruc.ID;
                    struc.Player = GetPlayerByID(almstruc.Player - 1);
                    if (almstruc.IsBridge)
                    {
                        struc.Width  = almstruc.Width;
                        struc.Height = almstruc.Height;
                        // also this crutch is apparently done by ROM2
                        if (struc.Width < 2)
                        {
                            struc.Width = 2;
                        }
                        if (struc.Height < 2)
                        {
                            struc.Height = 2;
                        }
                        struc.IsBridge = true;
                    }

                    // find inn/shop data if needed
                    if (struc.Class != null && struc.Class.Usable)
                    {
                        if ((struc.Class.ID >= 105 && struc.Class.ID <= 107) || // druid shop
                            (struc.Class.ID >= 34 && struc.Class.ID <= 35) ||   // plagat shop
                            (struc.Class.ID >= 93 && struc.Class.ID <= 95))     // kaarg shop
                        {
                            foreach (AllodsMap.AlmShop shop in mapStructure.Shops)
                            {
                                if (shop.ID == struc.Tag)
                                {
                                    struc.Logic = new ShopStructure(struc, shop);
                                    break;
                                }
                            }
                            if (struc.Logic == null)
                            {
                                Debug.LogFormat("Warning: Loaded multiplayer shop without data (ID={0})", struc.ID);
                            }
                        }
                        else if ((struc.Class.ID >= 99 && struc.Class.ID <= 101) ||  // kaarg inn
                                 (struc.Class.ID >= 111 && struc.Class.ID <= 113) || // druid inn
                                 (struc.Class.ID >= 67 && struc.Class.ID <= 69))     // plagat inn
                        {
                            foreach (AllodsMap.AlmInnInfo info in mapStructure.Inns)
                            {
                                if (info.ID == struc.Tag)
                                {
                                    struc.Logic = new InnStructure(struc, info);
                                    break;
                                }
                            }
                            if (struc.Logic == null)
                            {
                                Debug.LogFormat("Warning: Loaded multiplayer inn without data (ID={0})", struc.ID);
                            }
                        }
                    }

                    struc.LinkToWorld();
                    AddObject(struc, (struc.Logic != null));
                }
            }

            // load groups
            if (!NetworkManager.IsClient && mapStructure.Groups != null)
            {
                foreach (AllodsMap.AlmGroup almgroup in mapStructure.Groups)
                {
                    Group grp = FindNewGroup((int)almgroup.GroupID);
                    grp.RepopDelay = (int)almgroup.RepopTime * TICRATE;
                    grp.Flags      = 0;
                    if (almgroup.GroupFlag.HasFlag(AllodsMap.AlmGroup.AlmGroupFlags.RandomPositions))
                    {
                        grp.Flags |= GroupFlags.RandomPositions;
                    }
                    if (almgroup.GroupFlag.HasFlag(AllodsMap.AlmGroup.AlmGroupFlags.QuestKill))
                    {
                        grp.Flags |= GroupFlags.QuestKill;
                    }
                    if (almgroup.GroupFlag.HasFlag(AllodsMap.AlmGroup.AlmGroupFlags.QuestIntercept))
                    {
                        grp.Flags |= GroupFlags.QuestIntercept;
                    }
                }
            }

            // load units
            if (!NetworkManager.IsClient && mapStructure.Units != null)
            {
                System.Diagnostics.Stopwatch sw = System.Diagnostics.Stopwatch.StartNew();
                foreach (AllodsMap.AlmUnit almunit in mapStructure.Units)
                {
                    if ((almunit.Flags & 0x10) != 0)
                    {
                        MapHuman human = new MapHuman(almunit.ServerID);
                        if (human.Class == null)
                        {
                            Debug.LogFormat("Tried to load invalid unit {0}", almunit.ServerID);
                            continue;
                        }

                        human.X      = human.TargetX = human.SpawnX = human.LastSpawnX = (int)almunit.X;
                        human.Y      = human.TargetY = human.SpawnY = human.LastSpawnY = (int)almunit.Y;
                        human.Tag    = almunit.ID;
                        human.Player = GetPlayerByID(almunit.Player - 1);
                        if (almunit.HealthMax >= 0)
                        {
                            human.CoreStats.HealthMax = almunit.HealthMax;
                            human.UpdateItems();
                        }
                        if (almunit.Health >= 0)
                        {
                            human.Stats.TrySetHealth(almunit.Health);
                        }
                        human.CalculateVision();
                        human.Group = FindNewGroup(almunit.Group);

                        human.LinkToWorld();
                        AddObject(human, true);
                    }
                    else
                    {
                        MapUnit unit = new MapUnit(almunit.ServerID);
                        if (unit.Class == null)
                        {
                            Debug.LogFormat("Tried to load invalid unit {0}", almunit.ServerID);
                            continue;
                        }

                        unit.X      = unit.TargetX = unit.SpawnX = unit.LastSpawnX = (int)almunit.X;
                        unit.Y      = unit.TargetY = unit.SpawnY = unit.LastSpawnY = (int)almunit.Y;
                        unit.Tag    = almunit.ID;
                        unit.Player = GetPlayerByID(almunit.Player - 1);
                        if (almunit.HealthMax >= 0)
                        {
                            unit.CoreStats.HealthMax = almunit.HealthMax;
                            unit.UpdateItems();
                        }
                        if (almunit.Health >= 0)
                        {
                            unit.Stats.TrySetHealth(almunit.Health);
                        }
                        unit.CalculateVision();
                        unit.Group = FindNewGroup(almunit.Group);

                        unit.LinkToWorld();
                        AddObject(unit, true);
                    }
                }
            }

            // load items into units
            if (!NetworkManager.IsClient && mapStructure.Sacks != null)
            {
                foreach (AllodsMap.AlmSack almsack in mapStructure.Sacks)
                {
                    if (almsack.Items.Length <= 0)
                    {
                        continue;
                    }

                    MapUnit unit = null;
                    MapSack sack = null;
                    if (almsack.UnitID != 0)
                    {
                        unit = GetUnitByTag((int)almsack.UnitID);
                        if (unit == null)
                        {
                            Debug.LogFormat("Error: Can't resolve unit ID {0} for sack", almsack.UnitID);
                            continue;
                        }
                    }
                    else
                    {
                        sack             = PutSackAt((int)almsack.X, (int)almsack.Y, new ItemPack(), false);
                        sack.Pack.Money += almsack.Gold;
                    }

                    List <Item> items = new List <Item>();
                    foreach (AllodsMap.AlmSackItem almitem in almsack.Items)
                    {
                        List <ItemEffect>   effects       = new List <ItemEffect>();
                        AllodsMap.AlmEffect almBaseEffect = null;
                        int almBaseEffectId = (int)(almitem.EffectNumber - 1);
                        if (mapStructure.Effects != null && almBaseEffectId >= 0 && almBaseEffectId < mapStructure.Effects.Length)
                        {
                            almBaseEffect = mapStructure.Effects[almBaseEffectId];
                            foreach (AllodsMap.AlmEffectModifier mod in almBaseEffect.EffectModifiers)
                            {
                                ItemEffect effect = new ItemEffect((ItemEffect.Effects)mod.TypeOfMod, (int)mod.Value);
                                effects.Add(effect);
                            }
                        }
                        else if (almBaseEffectId >= 0)
                        {
                            Debug.LogFormat("Warning: Can't resolve effect #{0} for sack item", almBaseEffectId);
                        }

                        Item item = new Item((ushort)almitem.ItemID, effects);
                        if (unit != null)
                        {
                            if (almitem.Wielded > 0)
                            {
                                unit.PutItemToBody((MapUnit.BodySlot)item.Class.Option.Slot, item);
                            }
                            else
                            {
                                unit.ItemsPack.PutItem(unit.ItemsPack.Count, item);
                            }
                        }
                        else if (sack != null)
                        {
                            sack.Pack.PutItem(sack.Pack.Count, item);
                        }
                    }
                }
            }

            //* WB_pathfind
            if (PathfindingType == PathfindingType.Flood)
            {
                Wizard.LoadMap(this);
            }
            //* end

            // only if loaded
            MapStructure = mapStructure;
            FileName     = filename;
            FileMD5      = ResourceManager.CalcMD5(FileName);
            MapLighting  = new TerrainLighting(Width, Height);
            CalculateLighting(180);

            // postprocessing
            // if we are playing in singleplayer, then console player is Self.
            if (!NetworkManager.IsClient && !NetworkManager.IsServer)
            {
                Player Self = GetPlayerByName("Self");
                if (Self == null)
                {
                    GameConsole.Instance.WriteLine("Error: couldn't set ConsolePlayer: Self not found!");
                }
                else
                {
                    ConsolePlayer = Self;
                }
                if (ConsolePlayer != null)
                {
                    ConsolePlayer.Name  = Config.cl_nickname.Length == 0 ? "Self" : Config.cl_nickname;
                    ConsolePlayer.Flags = 0;
                    ConsolePlayer.Diplomacy[ConsolePlayer.ID] = DiplomacyFlags.Ally | DiplomacyFlags.Vision;
                    GameManager.Instance.CallDelegateOnNextFrame(() =>
                    {
                        ConsolePlayer.Avatar = CreateAvatar(ConsolePlayer);
                        // center view on avatar.
                        MapView.Instance.CenterOnObject(ConsolePlayer.Avatar);
                        return(false);
                    });
                }
            }

            if (!NetworkManager.IsClient)
            {
                // testing

                /*
                 * ItemPack testpack = new ItemPack();
                 * testpack.PutItem(0, new Item("Very Rare Crystal Ring"));
                 * PutSackAt(16, 16, testpack, false);
                 */

                /*
                 * MapProjectile proj = new MapProjectile(15);
                 * proj.SetPosition(16, 16, 0);
                 * Objects.Add(proj);
                 */
            }

            GameManager.Instance.CallDelegateOnNextFrame(() =>
            {
                MapView.Instance.OnMapLoaded();
                return(false);
            });
        }
        catch (Exception e)
        {
            Debug.LogError(e);
            GameConsole.Instance.WriteLine("Failed to load {0}: {1}: {2}", filename, e.GetType().Name, e.Message);
            MapStructure = null;
        }
        finally
        {
            IsLoading = false;
        }
    }
Example #2
0
    public void InitFromFile(string filename)
    {
        Unload();
        InitGeneric();

        AllodsMap mapStructure = AllodsMap.LoadFrom(filename);

        if (mapStructure == null)
        {
            //Core.Abort("Couldn't load \"{0}\"", filename);
            GameConsole.Instance.WriteLine("Couldn't load \"{0}\"", filename);
            Unload();
            return;
        }

        Width  = (int)mapStructure.Data.Width;
        Height = (int)mapStructure.Data.Height;

        Nodes = new MapNode[Width, Height];
        for (int y = 0; y < Height; y++)
        {
            for (int x = 0; x < Width; x++)
            {
                Nodes[x, y]        = new MapNode();
                Nodes[x, y].Tile   = (ushort)(mapStructure.Tiles[y * Width + x] & 0x3FF);
                Nodes[x, y].Height = mapStructure.Heights[y * Width + x];
                Nodes[x, y].Flags  = 0;
                Nodes[x, y].Light  = 255;
            }
        }

        // load players
        foreach (AllodsMap.AlmPlayer almplayer in mapStructure.Players)
        {
            Player player = new Player(almplayer);
            Players.Add(player);
            //Debug.Log(string.Format("player ID={2} {0} (flags {1})", player.Name, player.Flags, player.ID));
        }

        GameManager.Instance.CallDelegateOnNextFrame(() =>
        {
            Speed = 5;
            return(false);
        });

        _TopObjectID = 0;

        // load obstacles
        for (int y = 0; y < Height; y++)
        {
            for (int x = 0; x < Width; x++)
            {
                int typeId = mapStructure.Objects[y * Width + x];
                if (typeId <= 0)
                {
                    continue;
                }
                typeId -= 1;
                MapObstacle mob = new MapObstacle(typeId);
                mob.X = x;
                mob.Y = y;
                mob.LinkToWorld();
                Objects.Add(mob);
            }
        }

        // load structures
        if (mapStructure.Structures != null)
        {
            foreach (AllodsMap.AlmStructure almstruc in mapStructure.Structures)
            {
                MapStructure struc;
                struc        = new MapStructure(almstruc.TypeID);
                struc.X      = (int)almstruc.X;
                struc.Y      = (int)almstruc.Y;
                struc.Health = almstruc.Health;
                struc.Tag    = almstruc.ID;
                struc.Player = GetPlayerByID(almstruc.Player - 1);
                if (almstruc.IsBridge)
                {
                    struc.Width  = almstruc.Width;
                    struc.Height = almstruc.Height;
                    // also this crutch is apparently done by ROM2
                    if (struc.Width < 2)
                    {
                        struc.Width = 2;
                    }
                    if (struc.Height < 2)
                    {
                        struc.Height = 2;
                    }
                    struc.IsBridge = true;
                }

                struc.LinkToWorld();
                Objects.Add(struc);
            }
        }

        // load units
        if (!NetworkManager.IsClient && mapStructure.Units != null)
        {
            foreach (AllodsMap.AlmUnit almunit in mapStructure.Units)
            {
                if ((almunit.Flags & 0x10) != 0)
                {
                    MapHuman human = new MapHuman(almunit.ServerID);
                    human.X      = (int)almunit.X;
                    human.Y      = (int)almunit.Y;
                    human.Tag    = almunit.ID;
                    human.Player = GetPlayerByID(almunit.Player - 1);
                    if (almunit.HealthMax >= 0)
                    {
                        human.Stats.HealthMax = almunit.HealthMax;
                    }
                    if (almunit.Health >= 0)
                    {
                        human.Stats.TrySetHealth(almunit.Health);
                    }

                    human.LinkToWorld();
                    Objects.Add(human);
                }
                else
                {
                    MapUnit unit = new MapUnit(almunit.ServerID);
                    unit.X      = (int)almunit.X;
                    unit.Y      = (int)almunit.Y;
                    unit.Tag    = almunit.ID;
                    unit.Player = GetPlayerByID(almunit.Player - 1);
                    if (almunit.HealthMax >= 0)
                    {
                        unit.Stats.HealthMax = almunit.HealthMax;
                    }
                    if (almunit.Health >= 0)
                    {
                        unit.Stats.TrySetHealth(almunit.Health);
                    }

                    unit.LinkToWorld();
                    Objects.Add(unit);
                }
            }
        }

        // only if loaded
        MapStructure = mapStructure;
        FileName     = filename;
        FileMD5      = ResourceManager.CalcMD5(FileName);
        MapLighting  = new TerrainLighting(Width, Height);
        CalculateLighting(180);

        // postprocessing
        // if we are playing in singleplayer, then console player is Self.
        if (!NetworkManager.IsClient && !NetworkManager.IsServer)
        {
            Player Self = GetPlayerByName("Self");
            if (Self == null)
            {
                GameConsole.Instance.WriteLine("Error: couldn't set ConsolePlayer: Self not found!");
            }
            else
            {
                ConsolePlayer = Self;
            }
            if (ConsolePlayer != null)
            {
                ConsolePlayer.Flags = 0;
                ConsolePlayer.Diplomacy[ConsolePlayer.ID] = DiplomacyFlags.Ally | DiplomacyFlags.Vision;
                GameManager.Instance.CallDelegateOnNextFrame(() =>
                {
                    ConsolePlayer.Avatar = CreateAvatar(ConsolePlayer);
                    // center view on avatar.
                    MapView.Instance.CenterOnObject(ConsolePlayer.Avatar);
                    return(false);
                });
            }
        }

        if (!NetworkManager.IsClient)
        {
            // testing

            /*
             * ItemPack testpack = new ItemPack();
             * testpack.PutItem(0, new Item("Very Rare Crystal Ring"));
             * PutSackAt(16, 16, testpack, false);
             */

            /*
             * MapProjectile proj = new MapProjectile(15);
             * proj.SetPosition(16, 16, 0);
             * Objects.Add(proj);
             */
        }
    }
Example #3
0
    private void InitFromFileWorker(string filename)
    {
        try
        {
            AllodsMap mapStructure = AllodsMap.LoadFrom(filename);
            if (mapStructure == null)
            {
                //Core.Abort("Couldn't load \"{0}\"", filename);
                GameConsole.Instance.WriteLine("Couldn't load \"{0}\"", filename);
                Unload();
                return;
            }

            Width  = (int)mapStructure.Data.Width;
            Height = (int)mapStructure.Data.Height;

            Nodes = new MapNode[Width, Height];
            for (int y = 0; y < Height; y++)
            {
                for (int x = 0; x < Width; x++)
                {
                    Nodes[x, y]        = new MapNode();
                    Nodes[x, y].Tile   = (ushort)(mapStructure.Tiles[y * Width + x] & 0x3FF);
                    Nodes[x, y].Height = mapStructure.Heights[y * Width + x];
                    Nodes[x, y].Flags  = 0;
                    Nodes[x, y].Light  = 255;
                }
            }

            // load players
            foreach (AllodsMap.AlmPlayer almplayer in mapStructure.Players)
            {
                Player player = new Player(almplayer);
                Players.Add(player);
                //Debug.Log(string.Format("player ID={2} {0} (flags {1})", player.Name, player.Flags, player.ID));
            }

            GameManager.Instance.CallDelegateOnNextFrame(() =>
            {
                Speed = 5;
                return(false);
            });

            _TopObjectID = 0;

            // load obstacles
            for (int y = 0; y < Height; y++)
            {
                for (int x = 0; x < Width; x++)
                {
                    int typeId = mapStructure.Objects[y * Width + x];
                    if (typeId <= 0)
                    {
                        continue;
                    }
                    typeId -= 1;
                    MapObstacle mob = new MapObstacle(typeId);
                    mob.X = x;
                    mob.Y = y;
                    mob.LinkToWorld();
                    Objects.Add(mob);
                }
            }

            // load structures
            if (mapStructure.Structures != null)
            {
                foreach (AllodsMap.AlmStructure almstruc in mapStructure.Structures)
                {
                    MapStructure struc;
                    struc        = new MapStructure(almstruc.TypeID);
                    struc.X      = (int)almstruc.X;
                    struc.Y      = (int)almstruc.Y;
                    struc.Health = almstruc.Health;
                    struc.Tag    = almstruc.ID;
                    struc.Player = GetPlayerByID(almstruc.Player - 1);
                    if (almstruc.IsBridge)
                    {
                        struc.Width  = almstruc.Width;
                        struc.Height = almstruc.Height;
                        // also this crutch is apparently done by ROM2
                        if (struc.Width < 2)
                        {
                            struc.Width = 2;
                        }
                        if (struc.Height < 2)
                        {
                            struc.Height = 2;
                        }
                        struc.IsBridge = true;
                    }

                    struc.LinkToWorld();
                    Objects.Add(struc);
                }
            }

            // load groups
            if (!NetworkManager.IsClient && mapStructure.Groups != null)
            {
                foreach (AllodsMap.AlmGroup almgroup in mapStructure.Groups)
                {
                    Group grp = FindNewGroup((int)almgroup.GroupID);
                    grp.RepopDelay = (int)almgroup.RepopTime * TICRATE;
                    grp.Flags      = 0;
                    if (almgroup.GroupFlag.HasFlag(AllodsMap.AlmGroup.AlmGroupFlags.RandomPositions))
                    {
                        grp.Flags |= GroupFlags.RandomPositions;
                    }
                    if (almgroup.GroupFlag.HasFlag(AllodsMap.AlmGroup.AlmGroupFlags.QuestKill))
                    {
                        grp.Flags |= GroupFlags.QuestKill;
                    }
                    if (almgroup.GroupFlag.HasFlag(AllodsMap.AlmGroup.AlmGroupFlags.QuestIntercept))
                    {
                        grp.Flags |= GroupFlags.QuestIntercept;
                    }
                }
            }

            // load units
            if (!NetworkManager.IsClient && mapStructure.Units != null)
            {
                int c = 0;
                System.Diagnostics.Stopwatch sw = System.Diagnostics.Stopwatch.StartNew();
                foreach (AllodsMap.AlmUnit almunit in mapStructure.Units)
                {
                    if ((almunit.Flags & 0x10) != 0)
                    {
                        MapHuman human = new MapHuman(almunit.ServerID);
                        human.X      = human.TargetX = human.SpawnX = human.LastSpawnX = (int)almunit.X;
                        human.Y      = human.TargetY = human.SpawnY = human.LastSpawnY = (int)almunit.Y;
                        human.Tag    = almunit.ID;
                        human.Player = GetPlayerByID(almunit.Player - 1);
                        if (almunit.HealthMax >= 0)
                        {
                            human.CoreStats.HealthMax = almunit.HealthMax;
                            human.UpdateItems();
                        }
                        if (almunit.Health >= 0)
                        {
                            human.Stats.TrySetHealth(almunit.Health);
                        }
                        human.CalculateVision();
                        human.Group = FindNewGroup(almunit.Group);

                        human.LinkToWorld();
                        Objects.Add(human);
                    }
                    else
                    {
                        MapUnit unit = new MapUnit(almunit.ServerID);
                        unit.X      = unit.TargetX = unit.SpawnX = unit.LastSpawnX = (int)almunit.X;
                        unit.Y      = unit.TargetY = unit.SpawnY = unit.LastSpawnY = (int)almunit.Y;
                        unit.Tag    = almunit.ID;
                        unit.Player = GetPlayerByID(almunit.Player - 1);
                        if (almunit.HealthMax >= 0)
                        {
                            unit.CoreStats.HealthMax = almunit.HealthMax;
                            unit.UpdateItems();
                        }
                        if (almunit.Health >= 0)
                        {
                            unit.Stats.TrySetHealth(almunit.Health);
                        }
                        unit.CalculateVision();
                        unit.Group = FindNewGroup(almunit.Group);

                        unit.LinkToWorld();
                        Objects.Add(unit);
                    }
                }
            }

            // only if loaded
            MapStructure = mapStructure;
            FileName     = filename;
            FileMD5      = ResourceManager.CalcMD5(FileName);
            MapLighting  = new TerrainLighting(Width, Height);
            CalculateLighting(180);

            // postprocessing
            // if we are playing in singleplayer, then console player is Self.
            if (!NetworkManager.IsClient && !NetworkManager.IsServer)
            {
                Player Self = GetPlayerByName("Self");
                if (Self == null)
                {
                    GameConsole.Instance.WriteLine("Error: couldn't set ConsolePlayer: Self not found!");
                }
                else
                {
                    ConsolePlayer = Self;
                }
                if (ConsolePlayer != null)
                {
                    ConsolePlayer.Name  = Config.cl_nickname.Length == 0 ? "Self" : Config.cl_nickname;
                    ConsolePlayer.Flags = 0;
                    ConsolePlayer.Diplomacy[ConsolePlayer.ID] = DiplomacyFlags.Ally | DiplomacyFlags.Vision;
                    GameManager.Instance.CallDelegateOnNextFrame(() =>
                    {
                        ConsolePlayer.Avatar = CreateAvatar(ConsolePlayer);
                        // center view on avatar.
                        MapView.Instance.CenterOnObject(ConsolePlayer.Avatar);
                        return(false);
                    });
                }
            }

            if (!NetworkManager.IsClient)
            {
                // testing

                /*
                 * ItemPack testpack = new ItemPack();
                 * testpack.PutItem(0, new Item("Very Rare Crystal Ring"));
                 * PutSackAt(16, 16, testpack, false);
                 */

                /*
                 * MapProjectile proj = new MapProjectile(15);
                 * proj.SetPosition(16, 16, 0);
                 * Objects.Add(proj);
                 */
            }

            /* WarBeginner */
            Wizard.LoadMap(this);

            GameManager.Instance.CallDelegateOnNextFrame(() =>
            {
                MapView.Instance.OnMapLoaded();
                return(false);
            });
        }
        catch (Exception e)
        {
            Debug.LogError(e);
            GameConsole.Instance.WriteLine("Failed to load {0}: {1}: {2}", filename, e.GetType().Name, e.Message);
            MapStructure = null;
        }
        finally
        {
            IsLoading = false;
        }
    }