Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(game): implement spawn probability #271

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/CorePluginAPI/Game/World/SpawnGroup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@ public class SpawnMember
{
public uint Id { get; init; }
}

public class SpawnGroupCollectionMember
{
public uint Id { get; init; }
public byte Amount { get; init; }

/// <summary>
/// 0 to 1 probability of spawning
/// </summary>
public float Probability { get; set; }
}

public class SpawnGroup
Expand Down
15 changes: 7 additions & 8 deletions src/Libraries/Game.Server/Extensions/ParserExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@ public static SpawnGroup ToSpawnGroup(this ParserService.DataFileGroup group)
{
Id = group.GetField<uint>("Vnum"),
Leader = group.GetField<uint>("Leader"),
Members = group.Data.Select(data => new SpawnMember
{
Id = uint.Parse(data.LastOrDefault()!)
}).ToList(),
Members = group.Data.Select(data => new SpawnMember {Id = uint.Parse(data.LastOrDefault()!)}).ToList(),
Name = group.Name
};
}
Expand All @@ -25,11 +22,13 @@ public static SpawnGroupCollection ToSpawnGroupCollection(this ParserService.Dat
{
Id = group.GetField<uint>("Vnum"),
Name = group.Name,
Groups = group.Data.Select(data => new SpawnGroupCollectionMember
Groups = group.Data.Select(data =>
{
Id = uint.Parse(data[1]),
Amount = byte.TryParse(data.ElementAtOrDefault(2), out var amount) ? amount : (byte) 1
var p = byte.TryParse(data.ElementAtOrDefault(2), out var probability) && probability > 1
? (float)(probability / 100m)
: 1;
return new SpawnGroupCollectionMember {Id = uint.Parse(data[1]), Probability = p};
}).ToList()
};
}
}
}
14 changes: 10 additions & 4 deletions src/Libraries/Game.Server/World/Map.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
private readonly IItemManager _itemManager;
private readonly IServerBase _server;

public Map(IMonsterManager monsterManager, IAnimationManager animationManager, ICacheManager cacheManager,

Check warning on line 56 in src/Libraries/Game.Server/World/Map.cs

View workflow job for this annotation

GitHub Actions / Test deployment

Non-nullable field '_options' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the field as nullable.
IWorld world, ILogger logger, ISpawnPointProvider spawnPointProvider,
IDropProvider dropProvider, IItemManager itemManager, IServerBase server, string name, Coordinates position,
uint width, uint height, TownCoordinates? townCoordinates)
Expand All @@ -80,9 +80,7 @@
Shinsoo = Position + townCoordinates.Shinsoo * SPAWN_POSITION_MULTIPLIER,
Common = Position + townCoordinates.Common * SPAWN_POSITION_MULTIPLIER
}
: null;
_quadTree = new QuadTree((int)position.X, (int)position.Y, (int)(width * MapUnit), (int)(height * MapUnit),
20);
: null; _quadTree = new QuadTree((int)position.X, (int)position.Y, (int)(width * MapUnit), (int)(height * MapUnit), 20);
_entityGauge = GameServer.Meter.CreateObservableGauge($"Map:{name}:EntityCount", () => Entities.Count);
}

Expand Down Expand Up @@ -237,7 +235,15 @@
var group = _world.GetGroup(collectionGroup.Id);
if (group != null)
{
for (var i = 0; i < collectionGroup.Amount; i++)
if (collectionGroup.Probability < 1)
{
var rand = Random.Shared.NextSingle();
if (rand > collectionGroup.Probability)
{
SpawnGroup(groupInstance, spawnPoint, group);
}
}
else
{
SpawnGroup(groupInstance, spawnPoint, group);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Tests/Core.Tests/MapTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,13 @@ public MapTests(ITestOutputHelper testOutputHelper)
{
Id = 101,
Name = "TestGroupCollection",
Groups = {new SpawnGroupCollectionMember {Id = 101, Amount = 1}}
Groups = {new SpawnGroupCollectionMember {Id = 101, Probability = 1}}
},
new SpawnGroupCollection
{
Id = 101,
Name = "TestGroupCollection",
Groups = {new SpawnGroupCollectionMember {Id = 101, Amount = 1}}
Groups = {new SpawnGroupCollectionMember {Id = 101, Probability = 1}}
}
});
return mock;
Expand Down
Loading
Loading