yuanshen/Yuanshen.Gameserver/Game/WorldSpawner.cs
wisedev 62205c8062 Chests, ore and artifact drops
Gadgets are streamed alongside monsters now, so chests and ore nodes show up in
the world and can be interacted with. The two flows differ: for a chest the
client sends the finish request itself and the loot is granted on that one, for
ore it sends nothing more so the server has to close everything on the first
request.

Artifacts used to be silently dropped on the floor because the item ids that
actually roll were not in the reliquary table at all. Added the missing rows and
gave them real main stat and substat rolls instead of the fixed four.

Dropped items survive a relogin. The inventory is still rebuilt from the tables
on every login, so the trick is to remember where the default set ends and only
persist what was found in the world. Gacha and shop weapons were disappearing
the same way and are fixed by the same change.
2026-09-04 10:11:21 +03:00

130 lines
4 KiB
C#

using Yuanshen.Protocol;
namespace Yuanshen.Gameserver.Game;
internal sealed class WorldSpawner
{
private const float SpawnRadiusSq = 120f * 120f;
private const float DespawnRadiusSq = 150f * 150f;
private const float StreamStepSq = 20f * 20f;
private const int ConcurrentCap = 25;
private const int WildlifeCap = 6;
private const int GadgetCap = 40;
private readonly Player _player;
private readonly SceneManager _scene;
private readonly MonsterSpawnTable _table;
private readonly GadgetSpawnTable _gadgets;
private Vector? _lastStream;
public WorldSpawner(Player player, SceneManager scene, MonsterSpawnTable table, GadgetSpawnTable gadgets)
{
_player = player;
_scene = scene;
_table = table;
_gadgets = gadgets;
}
public async ValueTask EnterSceneAsync(Vector pos)
{
_player.Monsters.Clear();
_player.Gadgets.Clear();
_lastStream = null;
await StreamAsync(pos, force: true);
}
public async ValueTask StreamAsync(Vector pos)
{
await StreamAsync(pos, force: false);
}
public async ValueTask RespawnAsync(Vector pos)
{
_player.KilledNodeIds.Clear();
await EnterSceneAsync(pos);
}
private async ValueTask StreamAsync(Vector pos, bool force)
{
if (!force && _lastStream is { } last && HorizontalSq(pos, last) < StreamStepSq)
return;
_lastStream = pos;
HashSet<uint> active = new();
foreach (GameMonster monster in _player.Monsters)
if (monster.NodeId != 0)
active.Add(monster.NodeId);
foreach (GameMonster monster in _player.Monsters
.Where(monster => monster.NodeId != 0 && HorizontalSq(pos, monster.Pos) > DespawnRadiusSq)
.ToList())
{
active.Remove(monster.NodeId);
await _scene.DespawnMonsterAsync(monster);
}
int wildlife = _player.Monsters.Count(monster => monster.IsWildlife);
foreach (SpawnNode node in _table.Nodes.OrderBy(node => node.IsWildlife))
{
if (active.Count >= ConcurrentCap)
break;
if (active.Contains(node.NodeId) || _player.KilledNodeIds.Contains(node.NodeId))
continue;
if (HorizontalSq(pos, node.Pos) > SpawnRadiusSq)
continue;
if (node.IsWildlife && wildlife >= WildlifeCap)
continue;
if (node.IsWildlife)
wildlife++;
GameMonster monster = _player.SpawnMonster(node.MonsterId, node.Pos, node.Level, node.Hp, node.Attack, node.Defense, node.NodeId, node.ConfigId, node.GroupId);
active.Add(node.NodeId);
await _scene.AppearMonsterAsync(monster);
}
await StreamGadgetsAsync(pos);
}
private async ValueTask StreamGadgetsAsync(Vector pos)
{
HashSet<uint> live = new();
foreach (GameGadget gadget in _player.Gadgets)
live.Add(gadget.NodeId);
foreach (GameGadget gadget in _player.Gadgets
.Where(gadget => HorizontalSq(pos, gadget.Node.Pos) > DespawnRadiusSq)
.ToList())
{
live.Remove(gadget.NodeId);
await _scene.DespawnGadgetAsync(gadget, VisionType.VisionRemove);
}
foreach (GadgetNode node in _gadgets.Nodes)
{
if (live.Count >= GadgetCap)
break;
if (live.Contains(node.NodeId))
continue;
if (node.IsChest && _player.OpenedGadgetIds.Contains(node.NodeId))
continue;
if (HorizontalSq(pos, node.Pos) > SpawnRadiusSq)
continue;
GameGadget gadget = _player.SpawnGadget(node);
live.Add(node.NodeId);
await _scene.AppearGadgetAsync(gadget);
}
}
private static float HorizontalSq(Vector a, Vector b)
{
float dx = a.X - b.X;
float dz = a.Z - b.Z;
return dx * dx + dz * dz;
}
}