World changes
This commit is contained in:
@@ -0,0 +1,331 @@
|
||||
using DataServer.Data;
|
||||
using DataServer.Data.Entities;
|
||||
using GameServer.Dungeon;
|
||||
using GameServer.Shared;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Server.Networking;
|
||||
using Server.Networking.Messages;
|
||||
|
||||
namespace GameServer.World;
|
||||
|
||||
public sealed partial class WorldHandler
|
||||
{
|
||||
private sealed class WorldAttackRequest
|
||||
{
|
||||
public string TargetType { get; set; } = "mob"; // "mob" or "player"
|
||||
public int? TargetMobId { get; set; }
|
||||
public ulong? TargetPlayerId { get; set; }
|
||||
}
|
||||
|
||||
private sealed class WorldMobLootRequest
|
||||
{
|
||||
public int MobId { get; set; }
|
||||
}
|
||||
|
||||
private async Task HandleWorldAttackAsync(IConnection conn, WorldSessionState s, ReadOnlyMemory<byte> payload, CancellationToken ct)
|
||||
{
|
||||
var req = WorldInputParser.Parse<WorldAttackRequest>(payload);
|
||||
if (req == null) return;
|
||||
|
||||
var zoneKey = WorldChunkCache.ZoneKey(s.RegionId, s.ZoneX, s.ZoneY, s.ZoneZ);
|
||||
if (!_activeZones.TryGetValue(zoneKey, out var zoneSim)) return;
|
||||
|
||||
if (!zoneSim.Players.TryGetValue(s.CharacterId, out var attacker))
|
||||
return;
|
||||
|
||||
if (!attacker.IsAlive || attacker.IsStunned)
|
||||
{
|
||||
await _broadcast.SendAsync(conn, OpCodes.WORLD_COMBAT_STATE, new
|
||||
{
|
||||
events = new[] { new { type = "AttackFailed", error = attacker.IsAlive ? "stunned" : "dead" } }
|
||||
}, ct);
|
||||
return;
|
||||
}
|
||||
|
||||
if (req.TargetType == "mob" && req.TargetMobId.HasValue)
|
||||
{
|
||||
await HandleAttackMobAsync(conn, s, zoneSim, attacker, req.TargetMobId.Value, ct);
|
||||
}
|
||||
else if (req.TargetType == "player" && req.TargetPlayerId.HasValue)
|
||||
{
|
||||
await HandleAttackPlayerAsync(conn, s, zoneSim, attacker, req.TargetPlayerId.Value, ct);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task HandleAttackMobAsync(
|
||||
IConnection conn, WorldSessionState s,
|
||||
WorldZoneSimulation zoneSim, WorldPlayerCombatState attacker,
|
||||
int targetMobId, CancellationToken ct)
|
||||
{
|
||||
if (!zoneSim.Mobs.TryGetValue(targetMobId, out var mob) || !mob.IsAlive)
|
||||
{
|
||||
await _broadcast.SendAsync(conn, OpCodes.WORLD_COMBAT_STATE, new
|
||||
{
|
||||
events = new[] { new { type = "AttackFailed", error = "target_not_found" } }
|
||||
}, ct);
|
||||
return;
|
||||
}
|
||||
|
||||
// Check range
|
||||
float distance = MathF.Sqrt(MathF.Pow(mob.X - attacker.X, 2) + MathF.Pow(mob.Y - attacker.Y, 2));
|
||||
if (distance > 80f) // ~2.5 tiles melee range
|
||||
{
|
||||
await _broadcast.SendAsync(conn, OpCodes.WORLD_COMBAT_STATE, new
|
||||
{
|
||||
events = new[] { new { type = "AttackFailed", error = "out_of_range" } }
|
||||
}, ct);
|
||||
return;
|
||||
}
|
||||
|
||||
// Calculate damage
|
||||
int baseDamage = Math.Max(1, attacker.Strength * 2 + 5); // Base melee damage formula
|
||||
var result = CombatCalculator.CalculatePlayerToMobDamage(
|
||||
attacker, mob, attacker.WeaponDamageTypeKey, baseDamage);
|
||||
|
||||
mob.Health -= result.FinalDamage;
|
||||
attacker.IsInCombat = true;
|
||||
attacker.LastCombatTick = zoneSim.CurrentTick;
|
||||
|
||||
// Alert mob to attacker
|
||||
if (mob.TargetId == null)
|
||||
{
|
||||
mob.TargetId = attacker.CharacterId;
|
||||
mob.State = MobState.Chasing;
|
||||
}
|
||||
|
||||
var events = new List<object>
|
||||
{
|
||||
new
|
||||
{
|
||||
type = "PlayerAttack",
|
||||
sourcePlayerId = attacker.CharacterId,
|
||||
targetMobId = mob.Id,
|
||||
damage = result.FinalDamage,
|
||||
damageTypeKey = attacker.WeaponDamageTypeKey,
|
||||
x = mob.X,
|
||||
y = mob.Y,
|
||||
wasResisted = result.WasResisted,
|
||||
wasWeakness = result.WasWeakness
|
||||
}
|
||||
};
|
||||
|
||||
if (mob.Health <= 0)
|
||||
{
|
||||
mob.Health = 0;
|
||||
mob.IsAlive = false;
|
||||
mob.State = MobState.Dead;
|
||||
mob.CurrentAnimation = mob.GetDirectionalAnimation("death");
|
||||
zoneSim.MobSpawnTracker?.OnMobDied(mob.Id);
|
||||
|
||||
events.Add(new
|
||||
{
|
||||
type = "MobDeath",
|
||||
targetMobId = mob.Id,
|
||||
sourcePlayerId = attacker.CharacterId,
|
||||
x = mob.X,
|
||||
y = mob.Y,
|
||||
xpReward = mob.Stats.XpReward,
|
||||
lootAvailable = true
|
||||
});
|
||||
|
||||
// Award XP
|
||||
_ = AwardXpAsync(s.CharacterId, mob.Stats.XpReward);
|
||||
}
|
||||
|
||||
// Broadcast attack event to all players in zone
|
||||
foreach (var player in zoneSim.Players.Values)
|
||||
{
|
||||
await _broadcast.SendAsync(player.Session.Connection, OpCodes.WORLD_COMBAT_STATE, new { events }, ct);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task HandleAttackPlayerAsync(
|
||||
IConnection conn, WorldSessionState s,
|
||||
WorldZoneSimulation zoneSim, WorldPlayerCombatState attacker,
|
||||
ulong targetPlayerId, CancellationToken ct)
|
||||
{
|
||||
// Check PvP enabled
|
||||
if (!zoneSim.IsPvpEnabled)
|
||||
{
|
||||
await _broadcast.SendAsync(conn, OpCodes.WORLD_COMBAT_STATE, new
|
||||
{
|
||||
events = new[] { new { type = "AttackFailed", error = "pvp_disabled_in_zone" } }
|
||||
}, ct);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!zoneSim.Players.TryGetValue(targetPlayerId, out var defender) || !defender.IsAlive)
|
||||
{
|
||||
await _broadcast.SendAsync(conn, OpCodes.WORLD_COMBAT_STATE, new
|
||||
{
|
||||
events = new[] { new { type = "AttackFailed", error = "target_not_found" } }
|
||||
}, ct);
|
||||
return;
|
||||
}
|
||||
|
||||
if (defender.IsInvulnerable)
|
||||
{
|
||||
await _broadcast.SendAsync(conn, OpCodes.WORLD_COMBAT_STATE, new
|
||||
{
|
||||
events = new[] { new { type = "AttackFailed", error = "target_invulnerable" } }
|
||||
}, ct);
|
||||
return;
|
||||
}
|
||||
|
||||
// Check range
|
||||
float distance = MathF.Sqrt(MathF.Pow(defender.X - attacker.X, 2) + MathF.Pow(defender.Y - attacker.Y, 2));
|
||||
if (distance > 80f)
|
||||
{
|
||||
await _broadcast.SendAsync(conn, OpCodes.WORLD_COMBAT_STATE, new
|
||||
{
|
||||
events = new[] { new { type = "AttackFailed", error = "out_of_range" } }
|
||||
}, ct);
|
||||
return;
|
||||
}
|
||||
|
||||
// PvP damage (includes 30% PvP reduction from CombatCalculator)
|
||||
int baseDamage = Math.Max(1, attacker.Strength * 2 + 5);
|
||||
var result = CombatCalculator.CalculatePlayerToPlayerDamage(
|
||||
attacker, defender, attacker.WeaponDamageTypeKey, baseDamage);
|
||||
|
||||
defender.Health -= result.FinalDamage;
|
||||
attacker.IsInCombat = true;
|
||||
attacker.LastCombatTick = zoneSim.CurrentTick;
|
||||
defender.IsInCombat = true;
|
||||
defender.LastCombatTick = zoneSim.CurrentTick;
|
||||
|
||||
var events = new List<object>
|
||||
{
|
||||
new
|
||||
{
|
||||
type = "PvpDamage",
|
||||
sourcePlayerId = attacker.CharacterId,
|
||||
targetPlayerId = defender.CharacterId,
|
||||
damage = result.FinalDamage,
|
||||
damageTypeKey = attacker.WeaponDamageTypeKey,
|
||||
x = defender.X,
|
||||
y = defender.Y
|
||||
}
|
||||
};
|
||||
|
||||
if (defender.Health <= 0)
|
||||
{
|
||||
defender.Health = 0;
|
||||
defender.IsAlive = false;
|
||||
|
||||
events.Add(new
|
||||
{
|
||||
type = "PlayerDeath",
|
||||
targetPlayerId = defender.CharacterId,
|
||||
sourcePlayerId = attacker.CharacterId,
|
||||
x = defender.X,
|
||||
y = defender.Y,
|
||||
isPvpKill = true
|
||||
});
|
||||
}
|
||||
|
||||
// Broadcast to all players in zone
|
||||
foreach (var player in zoneSim.Players.Values)
|
||||
{
|
||||
await _broadcast.SendAsync(player.Session.Connection, OpCodes.WORLD_COMBAT_STATE, new { events }, ct);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task HandleWorldMobLootAsync(IConnection conn, WorldSessionState s, ReadOnlyMemory<byte> payload, CancellationToken ct)
|
||||
{
|
||||
var req = WorldInputParser.Parse<WorldMobLootRequest>(payload);
|
||||
if (req == null) return;
|
||||
|
||||
var zoneKey = WorldChunkCache.ZoneKey(s.RegionId, s.ZoneX, s.ZoneY, s.ZoneZ);
|
||||
if (!_activeZones.TryGetValue(zoneKey, out var zoneSim)) return;
|
||||
|
||||
if (!zoneSim.Mobs.TryGetValue(req.MobId, out var mob))
|
||||
{
|
||||
await _broadcast.SendAsync(conn, OpCodes.WORLD_MOB_LOOT_RESULT, new
|
||||
{
|
||||
success = false,
|
||||
error = "mob_not_found"
|
||||
}, ct);
|
||||
return;
|
||||
}
|
||||
|
||||
if (mob.IsAlive)
|
||||
{
|
||||
await _broadcast.SendAsync(conn, OpCodes.WORLD_MOB_LOOT_RESULT, new
|
||||
{
|
||||
success = false,
|
||||
error = "mob_still_alive"
|
||||
}, ct);
|
||||
return;
|
||||
}
|
||||
|
||||
// Check distance
|
||||
float distance = MathF.Sqrt(MathF.Pow(mob.X - s.X, 2) + MathF.Pow(mob.Y - s.Y, 2));
|
||||
if (distance > 128f)
|
||||
{
|
||||
await _broadcast.SendAsync(conn, OpCodes.WORLD_MOB_LOOT_RESULT, new
|
||||
{
|
||||
success = false,
|
||||
error = "too_far"
|
||||
}, ct);
|
||||
return;
|
||||
}
|
||||
|
||||
// Generate loot from mob's definition loot table
|
||||
using var scope = _scopeFactory.CreateScope();
|
||||
var db = scope.ServiceProvider.GetRequiredService<GameDbContext>();
|
||||
|
||||
if (!_mobDefinitions.TryGetValue(mob.DefinitionId, out var mobDef))
|
||||
{
|
||||
await _broadcast.SendAsync(conn, OpCodes.WORLD_MOB_LOOT_RESULT, new
|
||||
{
|
||||
success = true,
|
||||
mobId = mob.Id,
|
||||
items = Array.Empty<object>(),
|
||||
gold = 0
|
||||
}, ct);
|
||||
return;
|
||||
}
|
||||
|
||||
// Award gold based on mob tier
|
||||
int goldDrop = Math.Max(1, mob.Stats.XpReward / 2);
|
||||
var character = await db.DungeonCharacters.FirstOrDefaultAsync(c => c.Id == s.CharacterId, ct);
|
||||
if (character != null)
|
||||
{
|
||||
character.Gold += goldDrop;
|
||||
await db.SaveChangesAsync(ct);
|
||||
}
|
||||
|
||||
// Mark mob as looted (it will be cleaned up eventually)
|
||||
mob.LootEmptiedAtTick = zoneSim.CurrentTick;
|
||||
|
||||
await _broadcast.SendAsync(conn, OpCodes.WORLD_MOB_LOOT_RESULT, new
|
||||
{
|
||||
success = true,
|
||||
mobId = mob.Id,
|
||||
gold = goldDrop,
|
||||
items = Array.Empty<object>() // TODO: Loot table integration in future
|
||||
}, ct);
|
||||
}
|
||||
|
||||
private async Task AwardXpAsync(ulong characterId, int xpAmount)
|
||||
{
|
||||
if (xpAmount <= 0) return;
|
||||
|
||||
try
|
||||
{
|
||||
using var scope = _scopeFactory.CreateScope();
|
||||
var db = scope.ServiceProvider.GetRequiredService<GameDbContext>();
|
||||
var character = await db.DungeonCharacters.FirstOrDefaultAsync(c => c.Id == characterId);
|
||||
if (character == null) return;
|
||||
|
||||
character.Experience += xpAmount;
|
||||
await db.SaveChangesAsync();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_log.LogWarning(ex, "Failed to award XP to character {CharacterId}", characterId);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user