/// <summary> /// Check LoS and wait for a reply before returning /// </summary> /// <param name="player">Client used to make the LoS check</param> /// <param name="source">GameObject from witch LoS check start</param> /// <param name="target">GameObject to check LoS to</param> /// <param name="notifier">GameObject to Notify when Check is made</param> /// <param name="cached">Use a cached result</param> /// <param name="timeout">Cache Timeout, 0 = default</param> public void LosCheck(GamePlayer player, GameObject source, GameObject target, IDOLEventHandler notifier, bool cached = true, int timeout = 0) { if(player == null || source == null || target == null) throw new LosUnavailableException(); // FIXME debug if(LOSMGR_DEBUG_LEVEL >= LOSMGR_DEBUG_WARN) log.Warn("LOSMGR_W : Starting Los Check with - Player : "+player.Name+" Source : "+source.Name+" Target : "+target.Name+"."); if(timeout <= 0) timeout = GetDefaultTimeouts(source, target); // check Threshold first if(source.IsWithinRadius(target, GetDefaultThreshold(source, target), false)) { // FIXME debug if(LOSMGR_DEBUG_LEVEL >= LOSMGR_DEBUG_DEBUG) log.Warn("LOSMGR_D : Threshold hitted ("+GetDefaultThreshold(source, target)+") with - Player : "+player.Name+" Source : "+source.Name+" Target : "+target.Name+"."); notifier.Notify(GameObjectEvent.FinishedLosCheck, player, new LosCheckData(source, target, GameTimer.GetTickCount(), true)); return; } // check in cache then ! if(cached) { try { bool los = GetLosCheckFromCache(source, target, timeout); notifier.Notify(GameObjectEvent.FinishedLosCheck, player, new LosCheckData(source, target, GameTimer.GetTickCount(), los)); return; } catch (LosUnavailableException) { // we have no cache } } // Check if a LoS is pending, or this player is already LoS Checking... Tuple<GameObject, GameObject> cacheKey = new Tuple<GameObject, GameObject>(source, target); // We need to lock the Pending list during the checks lock(((ICollection)PendingChecks).SyncRoot) { IEnumerable<Tuple<GamePlayer, ushort, ushort>> pendings = (from pending in PendingChecks where (pending.Key.Item2 == source.ObjectID && pending.Key.Item3 == target.ObjectID) || (pending.Key.Item2 == target.ObjectID && pending.Key.Item3 == source.ObjectID) select pending.Key).Take(1); // We have pending data not too old, Register this event handler foreach(Tuple<GamePlayer, ushort, ushort> pend in pendings) { // FIXME debug if(LOSMGR_DEBUG_LEVEL >= LOSMGR_DEBUG_INFO) log.Warn("LOSMGR_D : Registered to an other LoS with - Player : "+player.Name+" Source : "+source.Name+" Target : "+target.Name+"."); AddRegisteredEvent(cacheKey, notifier); return; } } // Throttle if(IsPvpLosCheck(source, target) || IsAvailableForLosCheck(player, source, target)) { // Not pending, Not in Cache, let's work AddRegisteredEvent(cacheKey, notifier); EventNotifierLosCheck(player, source, target); } else { // Get best checker ! GamePlayer checker = GetBestLosChecker(source, target); if(checker != null) { // FIXME Debug if(LOSMGR_DEBUG_LEVEL >= LOSMGR_DEBUG_WARN) log.Warn("LOSMGR_D : Deferred LoSCheck to "+checker.Name+" From - Player : "+player.Name+" Source : "+source.Name+" Target : "+target.Name+"."); AddRegisteredEvent(cacheKey, notifier); EventNotifierLosCheck(checker, source, target); } else { // the Player checker is unavailable to make this LosCheck throw new LosUnavailableException(); } } }
/// <summary> /// LoscheckVincinity can be used when source player isn't available to check Los /// </summary> /// <param name="source">GameObject from witch LoS check start</param> /// <param name="target">GameObject to check LoS to</param> /// <param name="notifier">GameObject to Notify when Check is made</param> /// <param name="cached">Use a cached result</param> /// <param name="timeout">Cache Timeout, 0 = default</param> public void LosCheckVincinity(GameObject source, GameObject target, IDOLEventHandler notifier, bool cached = true, int timeout = 0) { // FIXME debug if(LOSMGR_DEBUG_LEVEL >= LOSMGR_DEBUG_WARN) log.Warn("LOSMGR_W : Starting Vincinity Los Check Between - Source : "+source.Name+" Target : "+target.Name+"."); if(timeout == 0) timeout = GetDefaultTimeouts(source, target); // check Threshold first if(source.IsWithinRadius(target, GetDefaultThreshold(source, target), false)) { //we need an arbitrary player foreach(GamePlayer player in source.GetPlayersInRadius(WorldMgr.VISIBILITY_DISTANCE)) { if(player.ObjectState == GameNPC.eObjectState.Active) { // FIXME debug if(LOSMGR_DEBUG_LEVEL >= LOSMGR_DEBUG_DEBUG) log.Warn("LOSMGR_D : Vincinity Hitted Treshold ("+GetDefaultThreshold(source, target)+") - Source : "+source.Name+" Target : "+target.Name+"."); notifier.Notify(GameObjectEvent.FinishedLosCheck, player, new LosCheckData(source, target, GameTimer.GetTickCount(), true)); return; } } } // check cache then ! if(cached) { try { bool los = GetLosCheckFromCache(source, target, timeout); //we need an arbitrary player foreach(GamePlayer player in source.GetPlayersInRadius(WorldMgr.VISIBILITY_DISTANCE)) { if(player.ObjectState == GameNPC.eObjectState.Active) { notifier.Notify(GameObjectEvent.FinishedLosCheck, player, new LosCheckData(source, target, GameTimer.GetTickCount(), los)); return; } } } catch (LosUnavailableException) { // we have no cache } } // check pending lock(((ICollection)PendingChecks).SyncRoot) { IEnumerable<Tuple<GamePlayer, ushort, ushort>> pendings = (from pending in PendingChecks where (pending.Key.Item2 == source.ObjectID && pending.Key.Item3 == target.ObjectID) || (pending.Key.Item2 == target.ObjectID && pending.Key.Item3 == source.ObjectID) select pending.Key).Take(1); foreach(Tuple<GamePlayer, ushort, ushort> pend in pendings) { // FIXME debug if(LOSMGR_DEBUG_LEVEL >= LOSMGR_DEBUG_INFO) log.Warn("LOSMGR_I : Vincinity Registered to an other LoS - Source : "+source.Name+" Target : "+target.Name+"."); AddRegisteredEvent(new Tuple<GameObject, GameObject>(source, target), notifier); return; } } // check if source is player and available for Los check if(IsAvailableForLosCheck(source, source, target)) { LosCheck((GamePlayer)source, source, target, notifier, cached, timeout); return; } // check if target is player and available for Los check if(IsAvailableForLosCheck(target, source, target)) { LosCheck((GamePlayer)target, source, target, notifier, cached, timeout); return; } // check if source has an available owner if(source is GameNPC && ((GameNPC)source).Brain != null && ((GameNPC)source).Brain is IControlledBrain) { GamePlayer owner = ((IControlledBrain)((GameNPC)source).Brain).GetPlayerOwner(); if(owner != null && IsAvailableForLosCheck(owner, source, target)) { LosCheck(owner, source, target, notifier, cached, timeout); return; } } // check if target has an available owner if(target is GameNPC && ((GameNPC)target).Brain != null && ((GameNPC)target).Brain is IControlledBrain) { GamePlayer tgtowner = ((IControlledBrain)((GameNPC)target).Brain).GetPlayerOwner(); if(tgtowner != null && IsAvailableForLosCheck(tgtowner, source, target)) { LosCheck(tgtowner, source, target, notifier, cached, timeout); return; } } GamePlayer checker = GetBestLosChecker(source, target); if(checker != null) { // FIXME debug if(LOSMGR_DEBUG_LEVEL >= LOSMGR_DEBUG_INFO) log.Warn("LOSMGR_I : Vincinity found best checker "+checker.Name+" - Source : "+source.Name+" Target : "+target.Name+"."); LosCheck(checker, source, target, notifier, cached, timeout); return; } throw new LosUnavailableException(); }
private GamePlayer GetBestLosChecker(GameObject source, GameObject target) { // Stealthed Player can't be third-party los Checked // FIXME could use player who can detect the Stealther (Realm mate) for a third party check if(source is GamePlayer && ((GamePlayer)source).IsStealthed) { // FIXME Debug if(LOSMGR_DEBUG_LEVEL >= LOSMGR_DEBUG_DEBUG) log.Warn("LOSMGR_D : Vincinity Player choosen because source is stealthed - "+source.Name); return (GamePlayer)source; } if(target is GamePlayer && ((GamePlayer)target).IsStealthed) { // FIXME Debug if(LOSMGR_DEBUG_LEVEL >= LOSMGR_DEBUG_DEBUG) log.Warn("LOSMGR_D : Vincinity Player choosen because target is stealthed - "+target.Name); return (GamePlayer)target; } // Get all players around able to check LoS IEnumerable<GamePlayer> playersQuery = (from GamePlayer player in source.GetPlayersInRadius(WorldMgr.VISIBILITY_DISTANCE) select player); List<GamePlayer> players = new List<GamePlayer>(); foreach(GamePlayer test in playersQuery) { if(target.IsWithinRadius(test, WorldMgr.VISIBILITY_DISTANCE)) players.Add(test); } // first get all players that haven't done anything lock(((ICollection)ClientChecks).SyncRoot) { IEnumerable<GamePlayer> checkers = (from checker in players where !ClientChecks.ContainsKey(checker) || !ClientStats.ContainsKey(checker) select checker).Take(1); // Pick one that isn't in cache foreach(GamePlayer choosed in checkers) { // FIXME Debug if(LOSMGR_DEBUG_LEVEL >= LOSMGR_DEBUG_DEBUG) log.Warn("LOSMGR_D : Vincinity Player choosen because inactive - "+choosed.Name); return choosed; } } lock(((ICollection)ClientStats).SyncRoot) { long currentTime = GameTimer.GetTickCount(); // We don't have player that aren't in cache try selecting available ones (lowest count && best instant checker) IEnumerable<GamePlayer> bestavails = (from best in ClientStats.Keys where players.Contains(best) && (ClientChecks.ContainsKey(best) && (currentTime-ClientChecks[best]) > LOSMGR_PLAYER_CHECK_FREQUENCY) orderby ClientStats[best].Item1, ClientStats[best].Item3 descending select best).Take(1); // Pick the best foreach(GamePlayer choosed in bestavails) { // FIXME Debug if(LOSMGR_DEBUG_LEVEL >= LOSMGR_DEBUG_DEBUG) log.Warn("LOSMGR_D : Vincinity Player choosen because available and best checker - "+choosed.Name); return choosed; } // All of them are busy pick the fastest instant checker IEnumerable<GamePlayer> betters = (from best in ClientStats.Keys where players.Contains(best) orderby ClientStats[best].Item3 descending, ClientStats[best].Item2 descending select best).Take(1); // Pick the best foreach(GamePlayer choosed in betters) { // FIXME Debug if(LOSMGR_DEBUG_LEVEL >= LOSMGR_DEBUG_DEBUG) log.Warn("LOSMGR_D : Vincinity Player choosen because best checker even if not available - "+choosed.Name); return choosed; } } // Return any default players foreach(GamePlayer choosed in players) { // FIXME Debug if(LOSMGR_DEBUG_LEVEL >= LOSMGR_DEBUG_DEBUG) log.Warn("LOSMGR_D : Vincinity Player choosen because there was no one else - "+choosed.Name); return choosed; } // FIXME Debug if(LOSMGR_DEBUG_LEVEL >= LOSMGR_DEBUG_DEBUG) { string concat = ""; foreach(GamePlayer choosed in players) concat += ", "+choosed.Name; log.Warn("LOSMGR_D : Vincinity Player could not be choosen amoung"+concat); } return null; }