Skip to content

Commit

Permalink
Merge pull request #474 from bernatvadell/feat/separete-jewel-drop-group
Browse files Browse the repository at this point in the history
  • Loading branch information
sven-n authored Aug 29, 2024
2 parents e1249ed + c2eb017 commit d901d0c
Show file tree
Hide file tree
Showing 11 changed files with 319 additions and 42 deletions.
80 changes: 44 additions & 36 deletions src/GameLogic/DefaultDropGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,36 +111,7 @@ public DefaultDropGenerator(GameConfiguration config, IRandomizer randomizer)
/// <inheritdoc/>
public Item? GenerateItemDrop(DropItemGroup selectedGroup)
{
var item = selectedGroup.ItemType == SpecialItemType.Ancient
? this.GenerateRandomAncient()
: this.GenerateRandomItem(selectedGroup.PossibleItems);

if (item is null)
{
return null;
}

if (selectedGroup is ItemDropItemGroup itemDropItemGroup)
{
item.Level = (byte)this._randomizer.NextInt(itemDropItemGroup.MinimumLevel, itemDropItemGroup.MaximumLevel + 1);
}
else if (selectedGroup.ItemLevel is { } itemLevel)
{
item.Level = itemLevel;
}
else
{
// no level defined, so it stays at 0.
}

item.Level = Math.Min(item.Level, item.Definition!.MaximumItemLevel);

if (selectedGroup.ItemType == SpecialItemType.Excellent)
{
this.AddRandomExcOptions(item);
}

return item;
return this.GenerateItemDrop(selectedGroup, selectedGroup.PossibleItems);
}

/// <inheritdoc/>
Expand Down Expand Up @@ -293,6 +264,40 @@ private static bool IsGroupRelevant(MonsterDefinition monsterDefinition, DropIte
return true;
}

private Item? GenerateItemDrop(DropItemGroup selectedGroup, ICollection<ItemDefinition> possibleItems)
{
var item = selectedGroup.ItemType == SpecialItemType.Ancient
? this.GenerateRandomAncient()
: this.GenerateRandomItem(possibleItems);

if (item is null)
{
return null;
}

if (selectedGroup is ItemDropItemGroup itemDropItemGroup)
{
item.Level = (byte)this._randomizer.NextInt(itemDropItemGroup.MinimumLevel, itemDropItemGroup.MaximumLevel + 1);
}
else if (selectedGroup.ItemLevel is { } itemLevel)
{
item.Level = itemLevel;
}
else
{
// no level defined, so it stays at 0.
}

item.Level = Math.Min(item.Level, item.Definition!.MaximumItemLevel);

if (selectedGroup.ItemType == SpecialItemType.Excellent)
{
this.AddRandomExcOptions(item);
}

return item;
}

private void ApplyOption(Item item, ItemOptionDefinition option)
{
for (int i = 0; i < option.MaximumOptionsPerItem; i++)
Expand All @@ -318,7 +323,7 @@ private void ApplyOption(Item item, ItemOptionDefinition option)

private Item? GenerateRandomItem(ICollection<ItemDefinition>? possibleItems)
{
if (possibleItems is null || !possibleItems.Any())
if (possibleItems is null || possibleItems.Count == 0)
{
return null;
}
Expand Down Expand Up @@ -398,9 +403,12 @@ private void AddRandomExcOptions(Item item)
private Item? GenerateItemDropOrMoney(MonsterDefinition monster, DropItemGroup selectedGroup, int gainedExperience, out uint? droppedMoney)
{
droppedMoney = null;

if (selectedGroup.PossibleItems?.Count > 0)
{
return this.GenerateItemDrop(selectedGroup);
var monsterLevel = (int)monster[Stats.Level];
var filteredPossibleItems = selectedGroup.PossibleItems.Where(it => it.DropLevel == 0 || ((it.DropLevel <= monsterLevel) && (it.DropLevel > monsterLevel - 12))).ToArray();
return this.GenerateItemDrop(selectedGroup, filteredPossibleItems);
}

switch (selectedGroup.ItemType)
Expand Down Expand Up @@ -454,9 +462,9 @@ private void AddRandomExcOptions(Item item)

return this._droppableItemsPerMonsterLevel[monsterLevel]
??= (from it in this._droppableItems
where (it.DropLevel <= monsterLevel)
&& (it.DropLevel > monsterLevel - 12)
&& (!socketItems || it.MaximumSockets > 0)
select it).ToList();
where (it.DropLevel <= monsterLevel)
&& (it.DropLevel > monsterLevel - 12)
&& (!socketItems || it.MaximumSockets > 0)
select it).ToList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,14 @@ private void AddItemDropGroups()
this.GameConfiguration.DropItemGroups.Add(excellentItemDropItemGroup);
BaseMapInitializer.RegisterDefaultDropItemGroup(excellentItemDropItemGroup);
}

var jewelsDropItemGroup = this.Context.CreateNew<DropItemGroup>();
jewelsDropItemGroup.SetGuid(4);
jewelsDropItemGroup.Chance = 0.001;
jewelsDropItemGroup.ItemType = SpecialItemType.RandomItem;
jewelsDropItemGroup.Description = "The jewels drop item group (0.1 % drop chance)";
this.GameConfiguration.DropItemGroups.Add(jewelsDropItemGroup);
BaseMapInitializer.RegisterDefaultDropItemGroup(jewelsDropItemGroup);
}

private ItemOptionDefinition CreateLuckOptionDefinition()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
// <copyright file="AddItemDropGroupForJewelsUpdate075.cs" company="MUnique">
// Licensed under the MIT License. See LICENSE file in the project root for full license information.
// </copyright>

namespace MUnique.OpenMU.Persistence.Initialization.Updates;

using System;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using MUnique.OpenMU.DataModel.Configuration;
using MUnique.OpenMU.PlugIns;

/// <summary>
/// This update creates a specific item drop group for jewels with a default chance of 5%.
/// </summary>
[PlugIn(PlugInName, PlugInDescription)]
[Guid("DCF14924-BB19-4CA2-93EC-397A89AA3EB3")]
public class AddItemDropGroupForJewelsUpdate075 : UpdatePlugInBase
{
/// <summary>
/// The plug in name.
/// </summary>
internal const string PlugInName = "Create item drop group for jewels";

/// <summary>
/// The plug in description.
/// </summary>
internal const string PlugInDescription = "This update creates a specific item drop group for jewels with a default chance of 5%.";

/// <inheritdoc />
public override string Name => PlugInName;

/// <inheritdoc />
public override string Description => PlugInDescription;

/// <inheritdoc />
public override UpdateVersion Version => UpdateVersion.AddItemDropGroupForJewels075;

/// <inheritdoc />
public override string DataInitializationKey => Version075.DataInitialization.Id;

/// <inheritdoc />
public override bool IsMandatory => false;

/// <inheritdoc />
public override DateTime CreatedAt => new(2024, 08, 26, 20, 0, 0, DateTimeKind.Utc);

/// <inheritdoc />
protected override async ValueTask ApplyAsync(IContext context, GameConfiguration gameConfiguration)
{
this.CreateDropItemGroupForJewels(context, gameConfiguration, 4, null, "The jewels drop item group (0.1 % drop chance)");

this.AddJewelToItemDrop(gameConfiguration, 4, null, "Jewel of Bless");
this.AddJewelToItemDrop(gameConfiguration, 4, null, "Jewel of Soul");
this.AddJewelToItemDrop(gameConfiguration, 4, null, "Jewel of Chaos");
}

/// <summary>
/// Add jewel to specific drop item group.
/// </summary>
/// <param name="gameConfiguration">Game configuration context to update.</param>
/// <param name="dropId">Drop to which the jewel will be associated.</param>
/// <param name="mapNumber">Optionally the map number to asociate drop group item.</param>
/// <param name="jewelName">Jewel name to add into specific drop item group.</param>
protected void AddJewelToItemDrop(GameConfiguration gameConfiguration, short dropId, short? mapNumber, string jewelName)
{
var jewelsItemDrop = GetJewelsDropItemGroup(gameConfiguration, dropId, mapNumber);

if (jewelsItemDrop == null)
{
return;
}

var item = gameConfiguration.Items.FirstOrDefault(x => x.Name == jewelName);

if (item == null)
{
return;
}

if (item.DropsFromMonsters == true)
{
item.DropsFromMonsters = false;
}

var jewelId = item.GetItemId();

if (!jewelsItemDrop.PossibleItems.Any(x => x.GetItemId() == jewelId))
{
jewelsItemDrop.PossibleItems.Add(item);
}
}

/// <summary>
/// Create drop item group for jewels.
/// </summary>
/// <param name="context">The persistence context.</param>
/// <param name="gameConfiguration">Game configuration context to update.</param>
/// <param name="dropId">New drop id.</param>
/// <param name="mapNumber">Optionally the map number to asociate drop group item.</param>
/// <param name="name">Description of drop,</param>
/// <returns>Returns new drop item group.</returns>
protected DropItemGroup CreateDropItemGroupForJewels(IContext context, GameConfiguration gameConfiguration, short dropId, short? mapNumber, string name)
{
var jewelsItemDrop = GetJewelsDropItemGroup(gameConfiguration, dropId, mapNumber);

if (jewelsItemDrop != null)
{
return jewelsItemDrop;
}

var jewelsDropItemGroup = context.CreateNew<DropItemGroup>();

if (mapNumber != null)
{
jewelsDropItemGroup.SetGuid(mapNumber.Value, dropId);
}
else
{
jewelsDropItemGroup.SetGuid(dropId);
}

jewelsDropItemGroup.Chance = 0.001;
jewelsDropItemGroup.ItemType = SpecialItemType.RandomItem;
jewelsDropItemGroup.Description = name;
gameConfiguration.DropItemGroups.Add(jewelsDropItemGroup);

if (mapNumber != null)
{
var map = gameConfiguration.Maps.First(x => x.Number == mapNumber);
map.DropItemGroups.Add(jewelsDropItemGroup);
}

return jewelsDropItemGroup;
}

private static DropItemGroup? GetJewelsDropItemGroup(GameConfiguration gameConfiguration, short dropId, short? mapNumber)
{
var id = mapNumber != null
? GuidHelper.CreateGuid<DropItemGroup>(mapNumber ?? 0, dropId)
: GuidHelper.CreateGuid<DropItemGroup>(dropId);

var jewelsItemDrop = gameConfiguration.DropItemGroups.FirstOrDefault(x => x.GetId() == id);
return jewelsItemDrop;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// <copyright file="AddItemDropGroupForJewelsUpdate095D.cs" company="MUnique">
// Licensed under the MIT License. See LICENSE file in the project root for full license information.
// </copyright>

namespace MUnique.OpenMU.Persistence.Initialization.Updates;

using System.Runtime.InteropServices;
using System.Threading.Tasks;
using MUnique.OpenMU.DataModel.Configuration;
using MUnique.OpenMU.PlugIns;

/// <summary>
/// This update creates a specific item drop group for jewels with a default chance of 5%.
/// </summary>
[PlugIn(PlugInName, PlugInDescription)]
[Guid("D21056E6-E912-416B-A076-3C2D17DA517B")]
public class AddItemDropGroupForJewelsUpdate095D : AddItemDropGroupForJewelsUpdate075
{
/// <inheritdoc />
public override UpdateVersion Version => UpdateVersion.AddItemDropGroupForJewels095d;

/// <inheritdoc />
public override string DataInitializationKey => Version095d.DataInitialization.Id;

/// <inheritdoc />
protected override async ValueTask ApplyAsync(IContext context, GameConfiguration gameConfiguration)
{
await base.ApplyAsync(context, gameConfiguration).ConfigureAwait(false);
this.AddJewelToItemDrop(gameConfiguration, 4, null, "Jewel of Life");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// <copyright file="AddItemDropGroupForJewelsUpdateSeason6.cs" company="MUnique">
// Licensed under the MIT License. See LICENSE file in the project root for full license information.
// </copyright>

namespace MUnique.OpenMU.Persistence.Initialization.Updates;

using System.Runtime.InteropServices;
using System.Threading.Tasks;
using MUnique.OpenMU.DataModel.Configuration;
using MUnique.OpenMU.PlugIns;

/// <summary>
/// This update creates a specific item drop group for jewels with a default chance of 5%.
/// </summary>
[PlugIn(PlugInName, PlugInDescription)]
[Guid("F958CC5B-C1E6-4F67-B48D-4BF75EC5CAA8")]
public class AddItemDropGroupForJewelsUpdateSeason6 : AddItemDropGroupForJewelsUpdate075
{
/// <inheritdoc />
public override UpdateVersion Version => UpdateVersion.AddItemDropGroupForJewelsSeason6;

/// <inheritdoc />
public override string DataInitializationKey => VersionSeasonSix.DataInitialization.Id;

/// <inheritdoc />
protected override async ValueTask ApplyAsync(IContext context, GameConfiguration gameConfiguration)
{
this.CreateDropItemGroupForJewels(context, gameConfiguration, 1, VersionSeasonSix.Maps.LandOfTrials.Number, "Jewel of Guardian");

await base.ApplyAsync(context, gameConfiguration).ConfigureAwait(false);
this.AddJewelToItemDrop(gameConfiguration, 4, null, "Jewel of Creation");
this.AddJewelToItemDrop(gameConfiguration, 1, VersionSeasonSix.Maps.LandOfTrials.Number, "Jewel of Guardian");
}
}
15 changes: 15 additions & 0 deletions src/Persistence/Initialization/Updates/UpdateVersion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,4 +144,19 @@ public enum UpdateVersion
/// The version of the <see cref="FixDrainLifeSkillUpdate"/>.
/// </summary>
FixDrainLifeSkill = 27,

/// <summary>
/// The version of the <see cref="AddItemDropGroupForJewelsUpdate075"/>.
/// </summary>
AddItemDropGroupForJewels075 = 28,

/// <summary>
/// The version of the <see cref="AddItemDropGroupForJewelsUpdate095D"/>.
/// </summary>
AddItemDropGroupForJewels095d = 29,

/// <summary>
/// The version of the <see cref="AddItemDropGroupForJewelsUpdateSeason6"/>.
/// </summary>
AddItemDropGroupForJewelsSeason6 = 30,
}
Loading

0 comments on commit d901d0c

Please sign in to comment.