public Combat DoAttack(string targetId, string attackerId, Item weapon) { var mobs = _mobService.GetMobs().ToList(); var combat = new Combat { SourceId = attackerId, TargetId = targetId, Weapon = weapon }; var locale = new Vector2(0, 0); var sourcePlayer = _userService.ActiveUsers.Find(p => p?.Id == combat.SourceId); var sourceMob = mobs.Find(m => m.Id == combat.SourceId); var targetMob = mobs.Find(m => m.Id == combat.TargetId); combat.WeaponDamage = random.Next(weapon.Damage - (int)Math.Ceiling(weapon.Damage * .2), weapon.Damage + (int)Math.Ceiling(weapon.Damage * .2)); // If target was a mob, do combat here. Otherwise do it in calling method cause players are annoying if (targetMob != null) { targetMob.Shield -= combat.WeaponDamage; if (targetMob.Shield < 0) { targetMob.Health += targetMob.Shield; targetMob.Shield = 0; } if (targetMob.Health <= 0) { targetMob.Alive = false; targetMob.KilledDate = DateTime.UtcNow; combat.TargetId = "dead"; } combat.TargetX = targetMob.X; combat.TargetY = targetMob.Y; } locale = sourcePlayer != null ? new Vector2(sourcePlayer.X, sourcePlayer.Y) : locale; locale = sourceMob != null ? new Vector2(sourceMob.X, sourceMob.Y) : locale; combat.X = (int)locale.X; combat.Y = (int)locale.Y; combat.SourceX = locale.X; combat.SourceY = locale.Y; _pendingCombats.Add(combat); return(combat); }
public void PreparePulseBroadcast() { var mobRange = 2000; _combatService.CycleArrays(); for (var i = 1; i < Constants.MAX_PLAYERS; i++) { if (ServerTCP.Clients[i].Socket != null && ServerTCP.tempPlayer[i].inGame && ServerTCP.tempPlayer[i].receiving) { var buffer = new PacketBuffer(); buffer.AddInteger((int)SPulse); buffer.AddInteger(_userService.ActiveUsers.Count); buffer.AddBytes(BitConverter.GetBytes(DateTime.UtcNow.ToBinary())); _userService.ActiveUsers.ForEach(p => { var ndx = Array.IndexOf(Types.PlayerIds, p.Id); buffer.AddInteger(ndx); buffer.AddString(p.Id); buffer.AddFloat(p.X); buffer.AddFloat(p.Y); buffer.AddFloat(p.Rotation); buffer.AddInteger(p.Health); buffer.AddInteger(p.MaxHealth); buffer.AddInteger(p.Shield); buffer.AddInteger(p.MaxShield); buffer.AddBytes(BitConverter.GetBytes(ServerTCP.tempPlayer[ndx].inGame)); }); var player = _userService.ActiveUsers.Find(p => p.Id == Types.PlayerIds[i]); var minX = (int)player.X - mobRange; var minY = (int)player.Y - mobRange; var maxX = (int)player.X + mobRange; var maxY = (int)player.Y + mobRange; buffer.AddArray(_mobService.GetMobs(minX, maxX, minY, maxY).ToArray()); buffer.AddArray(_combatService.GetCombats((int)player.X, (int)player.Y).ToArray()); buffer.AddArray(Globals.Inventory.Where(m => m.X >= minX && m.X <= maxX && m.Y >= minY && m.Y <= maxY).ToArray()); buffer.AddArray(Globals.Loot.Where(L => L.X >= minX && L.X <= maxX && L.Y >= minY && L.Y <= maxY && L.Owner == player.Id).ToArray()); SendData(i, buffer.ToArray()); buffer.Dispose(); } } }