public void AddPlayer(GameSession sender, IFieldObject <Player> player) { Debug.Assert(player.ObjectId > 0, "Player was added to field without initialized objectId."); player.Coord = player.Value.Coord; player.Value.MapId = MapId; // TODO: Determine new coordinates for player as well lock (Sessions) { Sessions.Add(sender); } // TODO: Send the initialization state of the field foreach (IFieldObject <Player> existingPlayer in State.Players.Values) { sender.Send(FieldPacket.AddPlayer(existingPlayer)); sender.Send(FieldObjectPacket.LoadPlayer(existingPlayer)); } foreach (IFieldObject <Item> existingItem in State.Items.Values) { sender.Send(FieldPacket.AddItem(existingItem, 123456)); } foreach (IFieldObject <Npc> existingNpc in State.Npcs.Values) { sender.Send(FieldPacket.AddNpc(existingNpc)); sender.Send(FieldObjectPacket.LoadNpc(existingNpc)); } foreach (IFieldObject <Portal> existingPortal in State.Portals.Values) { sender.Send(FieldPacket.AddPortal(existingPortal)); } foreach (IFieldObject <Mob> existingMob in State.Mobs.Values) { sender.Send(FieldPacket.AddMob(existingMob)); sender.Send(FieldObjectPacket.LoadMob(existingMob)); } if (State.InteractActors.Values.Count > 0) { sender.Send(InteractActorPacket.AddInteractActors(State.InteractActors.Values)); } if (MapEntityStorage.HasHealingSpot(MapId)) { if (HealingSpotThread == null || HealingSpotThread.IsCompleted) { HealingSpotThread = StartHealingSpot(sender, player); } sender.Send(RegionSkillPacket.Send(player, MapEntityStorage.GetHealingSpot(MapId), new SkillCast(70000018, 1, 0, 1))); } State.AddPlayer(player); // Broadcast new player to all players in map Broadcast(session => { session.Send(FieldPacket.AddPlayer(player)); session.Send(FieldObjectPacket.LoadPlayer(player)); }); }
private Task StartHealingSpot(GameSession session, IFieldObject <Player> player) { int healAmount = 30; Status status = new Status(new SkillCast(70000018, 1, 0, 1), player.ObjectId, player.ObjectId, 1, healAmount); return(Task.Run(async() => { while (!State.Players.IsEmpty) { CoordS healingCoord = MapEntityStorage.GetHealingSpot(MapId); if ((healingCoord - player.Coord.ToShort()).Length() < Block.BLOCK_SIZE * 2 && healingCoord.Z == player.Coord.ToShort().Z - 1) // 3x3x1 area { session.Send(BuffPacket.SendBuff(0, status)); session.Send(SkillDamagePacket.ApplyHeal(player, status)); session.Player.Stats.Increase(PlayerStatId.Hp, healAmount); session.Send(StatPacket.UpdateStats(player, PlayerStatId.Hp)); } await Task.Delay(1000); } })); }
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))); } } } }
public FieldManager(int mapId) { MapId = mapId; 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.Flags.HasFlag(MapPortalFlag.Visible), IsEnabled = portal.Flags.HasFlag(MapPortalFlag.Enabled), IsMinimapVisible = portal.Flags.HasFlag(MapPortalFlag.MinimapVisible), Rotation = portal.Rotation.ToFloat(), TargetMapId = portal.Target, }); 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); string mapName = MapMetadataStorage.GetMetadata(mapId).Name; Triggers = TriggerLoader.GetTriggers(mapName).Select(initializer => { TriggerContext context = new TriggerContext(this, Logger); TriggerState startState = initializer.Invoke(context); return(new TriggerScript(context, startState)); }).ToArray(); 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))); } } } }