Esempio n. 1
0
        public FieldManager(Player player)
        {
            MapId       = player.MapId;
            InstanceId  = player.InstanceId;
            BoundingBox = MapEntityStorage.GetBoundingBox(MapId);
            // Load default npcs for map from config
            foreach (MapNpc npc in MapEntityStorage.GetNpcs(MapId))
            {
                IFieldObject <Npc> fieldNpc = RequestFieldObject(new Npc(npc.Id)
                {
                    ZRotation = (short)(npc.Rotation.Z * 10)
                });

                if (fieldNpc.Value.Friendly == 2)
                {
                    fieldNpc.Coord = npc.Coord.ToFloat();
                    AddNpc(fieldNpc);
                }
                else
                {
                    // NPC is an enemy
                    IFieldObject <Mob> fieldMob = RequestFieldObject(new Mob(npc.Id)
                    {
                        ZRotation = (short)(npc.Rotation.Z * 10)
                    });

                    fieldMob.Coord = npc.Coord.ToFloat();
                    AddMob(fieldMob);
                }
            }

            // Spawn map's mobs at the mob spawners
            foreach (MapMobSpawn mobSpawn in MapEntityStorage.GetMobSpawns(MapId))
            {
                if (mobSpawn.SpawnData == null)
                {
                    Debug.WriteLine($"Missing mob spawn data: {mobSpawn}");
                    continue;
                }
                IFieldObject <MobSpawn> fieldMobSpawn = RequestFieldObject(new MobSpawn(mobSpawn));
                fieldMobSpawn.Coord = mobSpawn.Coord.ToFloat();
                State.AddMobSpawn(fieldMobSpawn);
                SpawnMobs(fieldMobSpawn);
            }

            // Load default portals for map from config
            foreach (MapPortal portal in MapEntityStorage.GetPortals(MapId))
            {
                IFieldObject <Portal> fieldPortal = RequestFieldObject(new Portal(portal.Id)
                {
                    IsVisible        = portal.IsVisible,
                    IsEnabled        = portal.Enable,
                    IsMinimapVisible = portal.MinimapVisible,
                    Rotation         = portal.Rotation.ToFloat(),
                    TargetMapId      = portal.Target,
                    TargetPortalId   = portal.TargetPortalId,
                    PortalType       = portal.PortalType
                });
                fieldPortal.Coord = portal.Coord.ToFloat();
                AddPortal(fieldPortal);
            }

            MapMetadata mapMetadata = MapMetadataStorage.GetMetadata(MapId);

            if (mapMetadata != null)
            {
                string xBlockName = mapMetadata.XBlockName;
                Triggers = TriggerLoader.GetTriggers(xBlockName).Select(initializer =>
                {
                    TriggerContext context  = new TriggerContext(this, Logger);
                    TriggerState startState = initializer.Invoke(context);
                    return(new TriggerScript(context, startState));
                }).ToArray();
            }

            foreach (MapTriggerMesh mapTriggerMesh in MapEntityStorage.GetTriggerMeshes(MapId))
            {
                if (mapTriggerMesh != null)
                {
                    TriggerMesh triggerMesh = new TriggerMesh(mapTriggerMesh.Id, mapTriggerMesh.IsVisible);
                    State.AddTriggerObject(triggerMesh);
                }
            }

            foreach (MapTriggerEffect mapTriggerEffect in MapEntityStorage.GetTriggerEffects(MapId))
            {
                if (mapTriggerEffect != null)
                {
                    TriggerEffect triggerEffect = new TriggerEffect(mapTriggerEffect.Id, mapTriggerEffect.IsVisible);
                    State.AddTriggerObject(triggerEffect);
                }
            }

            foreach (MapTriggerActor mapTriggerActor in MapEntityStorage.GetTriggerActors(MapId))
            {
                if (mapTriggerActor != null)
                {
                    TriggerActor triggerActor = new TriggerActor(mapTriggerActor.Id, mapTriggerActor.IsVisible, mapTriggerActor.InitialSequence);
                    State.AddTriggerObject(triggerActor);
                }
            }

            foreach (MapTriggerCamera mapTriggerCamera in MapEntityStorage.GetTriggerCameras(MapId))
            {
                if (mapTriggerCamera != null)
                {
                    TriggerCamera triggerCamera = new TriggerCamera(mapTriggerCamera.Id, mapTriggerCamera.IsEnabled);
                    State.AddTriggerObject(triggerCamera);
                }
            }

            foreach (MapTriggerCube mapTriggerCube in MapEntityStorage.GetTriggerCubes(MapId))
            {
                if (mapTriggerCube != null)
                {
                    TriggerCube triggerCube = new TriggerCube(mapTriggerCube.Id, mapTriggerCube.IsVisible);
                    State.AddTriggerObject(triggerCube);
                }
            }

            foreach (MapTriggerLadder mapTriggerLadder in MapEntityStorage.GetTriggerLadders(MapId))
            {
                if (mapTriggerLadder != null)
                {
                    TriggerLadder triggerLadder = new TriggerLadder(mapTriggerLadder.Id, mapTriggerLadder.IsVisible);
                    State.AddTriggerObject(triggerLadder);
                }
            }

            foreach (MapTriggerRope mapTriggerRope in MapEntityStorage.GetTriggerRopes(MapId))
            {
                if (mapTriggerRope != null)
                {
                    TriggerRope triggerRope = new TriggerRope(mapTriggerRope.Id, mapTriggerRope.IsVisible);
                    State.AddTriggerObject(triggerRope);
                }
            }

            foreach (MapTriggerSound mapTriggerSound in MapEntityStorage.GetTriggerSounds(MapId))
            {
                if (mapTriggerSound != null)
                {
                    TriggerSound triggerSound = new TriggerSound(mapTriggerSound.Id, mapTriggerSound.IsEnabled);
                    State.AddTriggerObject(triggerSound);
                }
            }

            foreach (MapTriggerSkill mapTriggerSkill in MapEntityStorage.GetTriggerSkills(MapId))
            {
                if (mapTriggerSkill != null)
                {
                    TriggerSkill triggerSkill = new TriggerSkill(mapTriggerSkill.Id, mapTriggerSkill.SkillId, mapTriggerSkill.SkillLevel, mapTriggerSkill.Count, mapTriggerSkill.Position);
                    IFieldObject <TriggerSkill> fieldTriggerSkill = RequestFieldObject(triggerSkill);
                    fieldTriggerSkill.Coord = fieldTriggerSkill.Value.Position;

                    State.AddTriggerSkills(fieldTriggerSkill);
                }
            }

            // Load breakables
            foreach (MapBreakableActorObject mapActor in MapEntityStorage.GetBreakableActors(MapId))
            {
                if (mapActor != null)
                {
                    BreakableActorObject actor = new BreakableActorObject(mapActor.EntityId, mapActor.IsEnabled, mapActor.HideDuration, mapActor.ResetDuration);
                    State.AddBreakable(actor);
                }
            }

            foreach (MapBreakableNifObject mapNif in MapEntityStorage.GetBreakableNifs(MapId))
            {
                if (mapNif != null)
                {
                    BreakableNifObject nif = new BreakableNifObject(mapNif.EntityId, mapNif.IsEnabled, mapNif.TriggerId, mapNif.HideDuration, mapNif.ResetDuration);
                    State.AddBreakable(nif);
                }
            }

            // Load interact objects
            foreach (MapInteractObject mapInteract in MapEntityStorage.GetInteractObjects(MapId))
            {
                if (mapInteract != null)
                {
                    InteractObject interactObject = new InteractObject(mapInteract.EntityId, mapInteract.InteractId, mapInteract.Type, InteractObjectState.Default);
                    State.AddInteractObject(interactObject);
                }
            }

            // Load cubes
            if (MapId == (int)Map.PrivateResidence)
            {
                Home home = GameServer.HomeManager.GetHomeById(player.VisitingHomeId);
                if (home != null)
                {
                    // Add cubes to state
                    Dictionary <long, Cube> cubes = home.FurnishingInventory;
                    foreach (Cube cube in cubes.Values.Where(x => x.PlotNumber == 1))
                    {
                        IFieldObject <Cube> ugcCube = RequestFieldObject(cube);
                        ugcCube.Coord    = cube.CoordF;
                        ugcCube.Rotation = cube.Rotation;
                        State.AddCube(ugcCube);
                    }

                    // Add portals to state
                    IEnumerable <Cube> cubePortals = cubes.Values.Where(x => x.Item.Id == 50400158);
                    foreach (Cube cubePortal in cubePortals)
                    {
                        Portal portal = new Portal(GuidGenerator.Int())
                        {
                            IsVisible        = true,
                            IsEnabled        = true,
                            IsMinimapVisible = false,
                            Rotation         = cubePortal.Rotation,
                            PortalType       = PortalTypes.Home
                        };

                        IFieldObject <Portal> fieldPortal = RequestFieldObject(portal);
                        fieldPortal.Coord = cubePortal.CoordF;
                        fieldPortal.Value.UGCPortalMethod = cubePortal.PortalSettings.Method;
                        if (!string.IsNullOrEmpty(cubePortal.PortalSettings.DestinationTarget))
                        {
                            switch (cubePortal.PortalSettings.Destination)
                            {
                            case UGCPortalDestination.PortalInHome:
                                fieldPortal.Value.TargetMapId = (int)Map.PrivateResidence;
                                break;

                            case UGCPortalDestination.SelectedMap:
                                fieldPortal.Value.TargetMapId = int.Parse(cubePortal.PortalSettings.DestinationTarget);
                                break;

                            case UGCPortalDestination.FriendHome:
                                fieldPortal.Value.TargetHomeAccountId = long.Parse(cubePortal.PortalSettings.DestinationTarget);
                                break;
                            }
                        }
                        cubePortal.PortalSettings.PortalObjectId = fieldPortal.ObjectId;
                        AddPortal(fieldPortal);
                    }
                }
            }
            else
            {
                List <Home> homes = GameServer.HomeManager.GetPlots(MapId);
                foreach (Home home in homes)
                {
                    Dictionary <long, Cube> cubes = home.FurnishingInventory;
                    foreach (Cube cube in cubes.Values.Where(x => x.PlotNumber != 1))
                    {
                        IFieldObject <Cube> ugcCube = RequestFieldObject(cube);
                        ugcCube.Coord    = cube.CoordF;
                        ugcCube.Rotation = cube.Rotation;
                        State.AddCube(ugcCube);
                    }
                }
            }

            if (MapEntityStorage.HasHealingSpot(MapId))
            {
                List <CoordS> healingSpots = MapEntityStorage.GetHealingSpot(MapId);
                if (State.HealingSpots.IsEmpty)
                {
                    foreach (CoordS coord in healingSpots)
                    {
                        int objectId = GuidGenerator.Int();
                        State.AddHealingSpot(RequestFieldObject(new HealingSpot(objectId, coord)));
                    }
                }
            }
        }
        public FieldManager(int mapId, long instanceId)
        {
            MapId       = mapId;
            InstanceId  = instanceId;
            BoundingBox = MapEntityStorage.GetBoundingBox(mapId);
            // Load default npcs for map from config
            foreach (MapNpc npc in MapEntityStorage.GetNpcs(mapId))
            {
                IFieldObject <Npc> fieldNpc = RequestFieldObject(new Npc(npc.Id)
                {
                    ZRotation = (short)(npc.Rotation.Z * 10)
                });

                if (fieldNpc.Value.Friendly == 2)
                {
                    fieldNpc.Coord = npc.Coord.ToFloat();
                    AddNpc(fieldNpc);
                }
                else
                {
                    // NPC is an enemy
                    IFieldObject <Mob> fieldMob = RequestFieldObject(new Mob(npc.Id)
                    {
                        ZRotation = (short)(npc.Rotation.Z * 10)
                    });

                    fieldMob.Coord = npc.Coord.ToFloat();
                    AddMob(fieldMob);
                }
            }

            // Spawn map's mobs at the mob spawners
            foreach (MapMobSpawn mobSpawn in MapEntityStorage.GetMobSpawns(mapId))
            {
                if (mobSpawn.SpawnData == null)
                {
                    Debug.WriteLine($"Missing mob spawn data: {mobSpawn}");
                    continue;
                }
                IFieldObject <MobSpawn> fieldMobSpawn = RequestFieldObject(new MobSpawn(mobSpawn));
                fieldMobSpawn.Coord = mobSpawn.Coord.ToFloat();
                State.AddMobSpawn(fieldMobSpawn);
                SpawnMobs(fieldMobSpawn);
            }

            // Load default portals for map from config
            foreach (MapPortal portal in MapEntityStorage.GetPortals(mapId))
            {
                IFieldObject <Portal> fieldPortal = RequestFieldObject(new Portal(portal.Id)
                {
                    IsVisible        = portal.IsVisible,
                    IsEnabled        = portal.Enable,
                    IsMinimapVisible = portal.MinimapVisible,
                    Rotation         = portal.Rotation.ToFloat(),
                    TargetMapId      = portal.Target,
                    PortalType       = portal.PortalType
                });
                fieldPortal.Coord = portal.Coord.ToFloat();
                AddPortal(fieldPortal);
            }

            // Load default InteractObjects
            List <IFieldObject <InteractObject> > actors = new List <IFieldObject <InteractObject> > {
            };

            foreach (MapInteractObject interactObject in MapEntityStorage.GetInteractObject(mapId))
            {
                // TODO: Group these fieldActors by their correct packet type.
                actors.Add(RequestFieldObject(new InteractObject(interactObject.Uuid, interactObject.Name, interactObject.Type)
                {
                }));
            }
            AddInteractObject(actors);

            MapMetadata mapMetadata = MapMetadataStorage.GetMetadata(mapId);

            if (mapMetadata != null)
            {
                string xBlockName = mapMetadata.XBlockName;
                Triggers = TriggerLoader.GetTriggers(xBlockName).Select(initializer =>
                {
                    TriggerContext context  = new TriggerContext(this, Logger);
                    TriggerState startState = initializer.Invoke(context);
                    return(new TriggerScript(context, startState));
                }).ToArray();
            }

            foreach (MapTriggerMesh mapTriggerMesh in MapEntityStorage.GetTriggerMeshes(mapId))
            {
                if (mapTriggerMesh != null)
                {
                    TriggerMesh triggerMesh = new TriggerMesh(mapTriggerMesh.Id, mapTriggerMesh.IsVisible);
                    State.AddTriggerObject(triggerMesh);
                }
            }

            foreach (MapTriggerEffect mapTriggerEffect in MapEntityStorage.GetTriggerEffects(mapId))
            {
                if (mapTriggerEffect != null)
                {
                    TriggerEffect triggerEffect = new TriggerEffect(mapTriggerEffect.Id, mapTriggerEffect.IsVisible);
                    State.AddTriggerObject(triggerEffect);
                }
            }

            foreach (MapTriggerActor mapTriggerActor in MapEntityStorage.GetTriggerActors(mapId))
            {
                if (mapTriggerActor != null)
                {
                    TriggerActor triggerActor = new TriggerActor(mapTriggerActor.Id, mapTriggerActor.IsVisible, mapTriggerActor.InitialSequence);
                    State.AddTriggerObject(triggerActor);
                }
            }

            foreach (MapTriggerCamera mapTriggerCamera in MapEntityStorage.GetTriggerCameras(mapId))
            {
                if (mapTriggerCamera != null)
                {
                    TriggerCamera triggerCamera = new TriggerCamera(mapTriggerCamera.Id, mapTriggerCamera.IsEnabled);
                    State.AddTriggerObject(triggerCamera);
                }
            }

            foreach (MapTriggerCube mapTriggerCube in MapEntityStorage.GetTriggerCubes(mapId))
            {
                if (mapTriggerCube != null)
                {
                    TriggerCube triggerCube = new TriggerCube(mapTriggerCube.Id, mapTriggerCube.IsVisible);
                    State.AddTriggerObject(triggerCube);
                }
            }

            foreach (MapTriggerLadder mapTriggerLadder in MapEntityStorage.GetTriggerLadders(mapId))
            {
                if (mapTriggerLadder != null)
                {
                    TriggerLadder triggerLadder = new TriggerLadder(mapTriggerLadder.Id, mapTriggerLadder.IsVisible);
                    State.AddTriggerObject(triggerLadder);
                }
            }

            foreach (MapTriggerRope mapTriggerRope in MapEntityStorage.GetTriggerRopes(mapId))
            {
                if (mapTriggerRope != null)
                {
                    TriggerRope triggerRope = new TriggerRope(mapTriggerRope.Id, mapTriggerRope.IsVisible);
                    State.AddTriggerObject(triggerRope);
                }
            }

            foreach (MapTriggerSound mapTriggerSound in MapEntityStorage.GetTriggerSounds(mapId))
            {
                if (mapTriggerSound != null)
                {
                    TriggerSound triggerSound = new TriggerSound(mapTriggerSound.Id, mapTriggerSound.IsEnabled);
                    State.AddTriggerObject(triggerSound);
                }
            }

            // Load breakables
            foreach (MapBreakableActorObject mapActor in MapEntityStorage.GetBreakableActors(mapId))
            {
                if (mapActor != null)
                {
                    BreakableActorObject actor = new BreakableActorObject(mapActor.EntityId, mapActor.IsEnabled, mapActor.HideDuration, mapActor.ResetDuration);
                    State.AddBreakable(actor);
                }
            }

            foreach (MapBreakableNifObject mapNif in MapEntityStorage.GetBreakableNifs(mapId))
            {
                if (mapNif != null)
                {
                    BreakableNifObject nif = new BreakableNifObject(mapNif.EntityId, mapNif.IsEnabled, mapNif.TriggerId, mapNif.HideDuration, mapNif.ResetDuration);
                    State.AddBreakable(nif);
                }
            }

            if (MapEntityStorage.HasHealingSpot(MapId))
            {
                List <CoordS> healingSpots = MapEntityStorage.GetHealingSpot(MapId);
                if (State.HealingSpots.IsEmpty)
                {
                    foreach (CoordS coord in healingSpots)
                    {
                        int objectId = GuidGenerator.Int();
                        State.AddHealingSpot(RequestFieldObject(new HealingSpot(objectId, coord)));
                    }
                }
            }
        }