Skip to content

Commit e6e44f2

Browse files
Fix generating the SpawnAssets file for the map sometimes with weights (chances) of items different from original spawn asset
1 parent 5f1adad commit e6e44f2

File tree

3 files changed

+47
-12
lines changed

3 files changed

+47
-12
lines changed

SpawnsManager/Models/SpawnItemInfo.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Xml.Serialization;
23

34
namespace RestoreMonarchy.SpawnsManager.Models
45
{
@@ -7,5 +8,8 @@ public class SpawnItemInfo
78
public ushort AssetId { get; set; }
89
public string Name { get; set; }
910
public int Weight { get; set; }
11+
12+
[XmlIgnore]
13+
public decimal Chance { get; set; }
1014
}
1115
}

SpawnsManager/SpawnsManager.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
<TargetFramework>net48</TargetFramework>
55
<LangVersion>latest</LangVersion>
66
<RootNamespace>RestoreMonarchy.SpawnsManager</RootNamespace>
7-
<Version>1.0.1</Version>
7+
<Version>1.0.2</Version>
88
</PropertyGroup>
99

1010
<ItemGroup>
11-
<PackageReference Include="RestoreMonarchy.RocketRedist" Version="3.24.4" ExcludeAssets="runtime" />
11+
<PackageReference Include="RestoreMonarchy.RocketRedist" Version="3.25.5" ExcludeAssets="runtime" />
1212
</ItemGroup>
1313
</Project>

SpawnsManager/SpawnsManagerPlugin.Assets.cs

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
using RestoreMonarchy.SpawnsManager.Helpers;
22
using RestoreMonarchy.SpawnsManager.Models;
33
using SDG.Unturned;
4+
using System;
45
using System.Collections.Generic;
6+
using System.Linq;
57

68
namespace RestoreMonarchy.SpawnsManager
79
{
@@ -55,35 +57,42 @@ private void AddAndOverrideSpawnAssets()
5557
LogDebug($"{SpawnAssetsConfiguration.SpawnAssets.Length} spawn assets have been added or overriden!");
5658
}
5759

58-
public List<SpawnItemInfo> GetSpawnItems(SpawnAsset spawnAsset, EAssetType assetType, int num = 0)
60+
public List<SpawnItemInfo> GetSpawnItems(SpawnAsset spawnAsset, EAssetType assetType, List<SpawnItemInfo> spawnItems = null, int depth = 0, decimal chance = 1)
5961
{
60-
List<SpawnItemInfo> spawnItems = new();
62+
bool isFirstDepth = depth == 0;
63+
if (spawnItems == null)
64+
{
65+
spawnItems = new List<SpawnItemInfo>();
66+
}
6167

62-
if (num++ > 32)
68+
if (depth > 32)
6369
{
6470
return spawnItems;
6571
}
6672

73+
decimal sumWeight = spawnAsset.tables.Sum(x => x.weight);
6774
foreach (SpawnTable spawnTable in spawnAsset.tables)
6875
{
6976
Asset asset = spawnTable.FindAsset(assetType);
70-
7177
if (asset == null)
7278
{
73-
return [];
79+
continue;
7480
}
7581

82+
decimal spawnChance = spawnTable.weight / sumWeight * chance;
83+
7684
if (asset is SpawnAsset spawnAsset2)
7785
{
78-
spawnItems.AddRange(GetSpawnItems(spawnAsset2, assetType, num));
86+
depth++;
87+
GetSpawnItems(spawnAsset2, assetType, spawnItems, depth, spawnChance);
7988
}
8089
else if (asset is ItemAsset itemAsset)
8190
{
8291
spawnItems.Add(new SpawnItemInfo()
8392
{
8493
AssetId = itemAsset.id,
8594
Name = itemAsset.itemName,
86-
Weight = spawnTable.weight
95+
Chance = spawnChance
8796
});
8897
}
8998
else if (asset is VehicleAsset vehicleAsset)
@@ -92,7 +101,7 @@ public List<SpawnItemInfo> GetSpawnItems(SpawnAsset spawnAsset, EAssetType asset
92101
{
93102
AssetId = vehicleAsset.id,
94103
Name = vehicleAsset.vehicleName,
95-
Weight = spawnTable.weight
104+
Chance = spawnChance
96105
});
97106
}
98107
else if (asset is VehicleRedirectorAsset vehicleRedirectorAsset)
@@ -101,7 +110,7 @@ public List<SpawnItemInfo> GetSpawnItems(SpawnAsset spawnAsset, EAssetType asset
101110
{
102111
AssetId = vehicleRedirectorAsset.id,
103112
Name = vehicleRedirectorAsset.FriendlyName,
104-
Weight = spawnTable.weight
113+
Chance = spawnChance
105114
});
106115
}
107116
else if (asset is AnimalAsset animalAsset)
@@ -110,7 +119,7 @@ public List<SpawnItemInfo> GetSpawnItems(SpawnAsset spawnAsset, EAssetType asset
110119
{
111120
AssetId = animalAsset.id,
112121
Name = animalAsset.animalName,
113-
Weight = spawnTable.weight
122+
Chance = spawnChance
114123
});
115124
}
116125
else
@@ -119,6 +128,28 @@ public List<SpawnItemInfo> GetSpawnItems(SpawnAsset spawnAsset, EAssetType asset
119128
}
120129
}
121130

131+
if (isFirstDepth)
132+
{
133+
// aggregate spawn items by AssetId
134+
spawnItems = spawnItems
135+
.GroupBy(x => x.AssetId)
136+
.Select(g => new SpawnItemInfo
137+
{
138+
AssetId = g.Key,
139+
Name = g.First().Name,
140+
Chance = g.Sum(x => x.Chance)
141+
})
142+
.ToList();
143+
144+
// calculate weights
145+
int totalWeight = 10000;
146+
foreach (SpawnItemInfo spawnItem in spawnItems)
147+
{
148+
spawnItem.Weight = Math.Max(1, (int)(spawnItem.Chance * totalWeight));
149+
}
150+
spawnItems = spawnItems.OrderByDescending(x => x.Weight).ToList();
151+
}
152+
122153
return spawnItems;
123154
}
124155
}

0 commit comments

Comments
 (0)