/// <summary> /// Plays sound in range of source. /// </summary> /// <param name="file">e.g. "data/sound/Glasgavelen_blowaway_endure.wav"</param> public static void PlaySound(string file, MabiEntity source) { var packet = new MabiPacket(Op.PlaySound, source.Id); packet.PutString(file); WorldManager.Instance.Broadcast(packet, SendTargets.Range, source); }
/// <summary> /// Broadcasts Effect packet in range. Parameters can be added, /// but you have to watch the types. /// </summary> public static void Effect(uint effect, MabiEntity source, params object[] args) { var packet = new MabiPacket(Op.Effect, source.Id); packet.PutInt(effect); foreach (var arg in args) { if (!(arg is bool)) packet.Put(arg); else packet.PutByte((bool)arg); } WorldManager.Instance.Broadcast(packet, SendTargets.Range, source); }
private static void AddPublicEntityInfo(this MabiPacket packet, MabiEntity entity) { if (entity is MabiCreature) packet.AddCreatureInfo(entity as MabiCreature, CreaturePacketType.Public); else if (entity is MabiItem) packet.AddItemInfo(entity as MabiItem, ItemPacketType.Public); else if (entity is MabiProp) packet.AddPropInfo(entity as MabiProp); else throw new Exception("Unknown entity class '" + entity.GetType() + "'"); }
/// <summary> /// Calculates and sets new position for creature, and returns a copy /// of the current coordinates, for later use. /// </summary> /// <param name="target">Creature to knock back</param> /// <param name="attacker">Creature that attacked</param> /// <param name="distance">Knock back distance</param> /// <returns>Position of creature, before the knock back</returns> public static MabiVertex KnockBack(MabiCreature target, MabiEntity attacker, int distance = 375) { var oldPos = target.GetPosition(); var pos = WorldManager.CalculatePosOnLine(attacker, target, distance); // Check for collision, set new pos 200 points before the // intersection, to prevent glitching through. MabiVertex intersection; if (WorldManager.Instance.FindCollision(attacker.Region, oldPos, pos, out intersection)) pos = WorldManager.CalculatePosOnLine(oldPos, intersection, -200); target.SetPosition(pos.X, pos.Y); return oldPos; }
private static MabiPacket GetEntityDisappears(MabiEntity entity) { uint op = Op.EntityDisappears; if (entity is MabiItem) op = Op.ItemDisappears; var packet = new MabiPacket(op, Id.Broadcast); packet.PutLong(entity.Id); packet.PutByte(0); return packet; }
/// <summary> /// Checks distance between the entity and the coordinates, does not check region. /// </summary> public static bool InRange(MabiEntity entity, uint x, uint y, uint range = 0) { var pos = entity.GetPosition(); return InRange(pos.X, pos.Y, x, y, range); }
public static MabiPacket SpawnEffect(MabiEntity entity, SpawnEffect type, MabiVertex pos) { return new MabiPacket(Op.Effect, entity.Id) .PutInt(Effect.Spawn) .PutInt(entity.Region) .PutFloats(pos.X, pos.Y) .PutByte((byte)type); }
protected IEnumerable Circle(MabiEntity center, bool clockwise, int radius, bool wait) { var centerPos = center.GetPosition(); var myPos = this.Creature.GetPosition(); var deltaX = (double)myPos.X - (double)centerPos.X; var deltaY = (double)myPos.Y - (double)centerPos.Y; var angle = Math.Atan2(deltaY, deltaX); angle += (clockwise ? -1 : 1) * rnd.NextDouble() * (Math.PI / 6); var x = (int)(Math.Cos(angle) * radius); var y = (int)(Math.Sin(angle) * radius); var dest = new MabiVertex(centerPos.X + x, centerPos.Y + y); foreach (var a in WalkTo(dest, wait)) yield return a; }
public static List<MabiMail> FindAllSent(MabiEntity e) { return FindAllSent(e.Id); }
public static void OnEntityLeavesRegion(MabiEntity entity) { if (EntityLeavesRegion != null) EntityLeavesRegion(entity); }
/// <summary> /// Calculates a position on the line between source and target. /// e.g. distance 0 would be the position of target, 100 would be /// 100 points farther away from source. /// </summary> public static MabiVertex CalculatePosOnLine(MabiEntity source, MabiEntity target, int distance) { return CalculatePosOnLine(source.GetPosition(), target.GetPosition(), distance); }
public List<MabiCreature> GetPlayersInRange(MabiEntity entity, uint range = 0) { if (range < 1) range = WorldConf.SightRange; lock (_creatures) return _creatures.FindAll(a => a != entity && a.IsPlayer && a.Region == entity.Region && InRange(a, entity, range)); }
public List<MabiEntity> GetEntitiesInRange(MabiEntity entity, uint range = 0) { var pos = entity.GetPosition(); return this.GetEntitiesInRange(entity.Region, pos.X, pos.Y, range); }
public List<MabiCreature> GetAttackableCreaturesInRange(MabiEntity entity, uint range = 0) { if (range < 1) range = WorldConf.SightRange; lock (_creatures) return _creatures.FindAll(a => a != entity && a is MabiNPC && !(a as MabiNPC).Has(CreatureStates.GoodNpc) && !a.IsDead && a.Region == entity.Region && InRange(a, entity, range)); }
/// <summary> /// Sends packet to all clients that match the parameters. /// </summary> /// <param name="packet"></param> /// <param name="targets"></param> /// <param name="source"></param> /// <param name="range"></param> public void Broadcast(MabiPacket packet, SendTargets targets, MabiEntity source = null, uint range = 0) { if (range < 1) range = WorldConf.SightRange; var excludeSender = ((targets & SendTargets.ExcludeSender) != 0); WorldClient sourceClient = null; if (source != null && source is MabiCreature) sourceClient = (source as MabiCreature).Client as WorldClient; lock (_clients) { if ((targets & SendTargets.All) != 0) { foreach (var client in _clients) { if (!(excludeSender && client == sourceClient)) client.Send(packet); } } else if ((targets & SendTargets.Region) != 0) { var region = source.Region; foreach (var client in _clients) { if (!(excludeSender && client == sourceClient)) if (region == client.Character.Region) client.Send(packet); } } else if ((targets & SendTargets.Range) != 0) { var region = source.Region; foreach (var client in _clients) { if (!(excludeSender && client == sourceClient)) if (region == client.Character.Region && InRange(client.Character, source, range)) client.Send(packet); } } } }
/// <summary> /// Broadcasts appear to everybody in range, except for entity itself. /// </summary> /// <param name="entity"></param> public static void EntityAppearsOthers(MabiEntity entity) { WorldManager.Instance.Broadcast(GetEntityAppears(entity), SendTargets.Range | SendTargets.ExcludeSender, entity); }
public static List<MabiMail> FindAllRecieved(MabiEntity e) { return FindAllRecieved(e.Id); }
/// <summary> /// Broadcasts disappear in range of entity. /// </summary> /// <param name="entity"></param> public static void EntityDisappears(MabiEntity entity) { WorldManager.Instance.Broadcast(GetEntityDisappears(entity), SendTargets.Range, entity); }
public static int GetUnreadCount(MabiEntity e) { return GetUnreadCount(e.Id); }
/// <summary> /// Sends dispappear to client. /// </summary> /// <param name="client"></param> /// <param name="entity"></param> public static void EntityDisappears(Client client, MabiEntity entity) { client.Send(GetEntityDisappears(entity)); }
public static MabiPacket SpawnEffect(MabiEntity entity, SpawnEffect type) { return SpawnEffect(entity, type, entity.GetPosition()); }
private static MabiPacket GetEntityAppears(MabiEntity entity) { var op = Op.EntityAppears; if (entity.EntityType == EntityType.Item) op = Op.ItemAppears; else if (entity.EntityType == EntityType.Prop) op = Op.PropAppears; var packet = new MabiPacket(op, Id.Broadcast); packet.AddPublicEntityInfo(entity); //entity.AddToPacket(packet); return packet; }
/// <summary> /// Lets the creature face the target. /// </summary> /// <param name="creature"></param> /// <param name="target"></param> /// <returns></returns> public static MabiPacket TurnTo(MabiEntity creature, MabiEntity target) { var cpos = creature.GetPosition(); var tpos = target.GetPosition(); var p = new MabiPacket(Op.TurnTo, creature.Id); p.PutFloat((float)tpos.X - (float)cpos.X); p.PutFloat((float)tpos.Y - (float)cpos.Y); return p; }
// Range and position calculations // ================================================================== /// <summary> /// Checks distance between the two entities, does not check region. /// </summary> public static bool InRange(MabiEntity c1, MabiEntity c2, uint range = 0) { return InRange(c1.GetPosition(), c2.GetPosition(), range); }