Repository navigation
Expand file tree
/
Copy pathRandomValueModLib.cs
More file actions
140 lines (120 loc) · 4.09 KB
/
Copy pathRandomValueModLib.cs
File metadata and controls
140 lines (120 loc) · 4.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using UnityEngine;
namespace RandomValueLib
{
/// <summary>
/// 随机化配置
/// </summary>
public class RandomizationConfig
{
public float MinMultiplier { get; set; } = 0.5f;
public float MaxMultiplier { get; set; } = 2.0f;
public bool EnableBlacklist { get; set; } = true;
public List<string> BlacklistKeywords { get; set; } = new List<string>();
public bool EnableWhitelist { get; set; } = false;
public List<string> WhitelistKeywords { get; set; } = new List<string>();
public bool ExcludeHighValueItems { get; set; } = false;
public float HighValueThreshold { get; set; } = 50000f;
public bool RandomizeCashBags { get; set; } = false;
}
/// <summary>
/// 核心随机价格服务
/// </summary>
public static class RandomValueService
{
private static FieldInfo dollarValueCurrentField;
private static FieldInfo dollarValueOriginalField;
static RandomValueService()
{
var valuableType = typeof(ValuableObject);
dollarValueCurrentField = valuableType.GetField("dollarValueCurrent",
BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
dollarValueOriginalField = valuableType.GetField("dollarValueOriginal",
BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
}
/// <summary>
/// 获取物品当前价格(反射)
/// </summary>
public static float GetItemPrice(ValuableObject vo)
{
if (dollarValueCurrentField == null) return 0f;
return (float)dollarValueCurrentField.GetValue(vo);
}
/// <summary>
/// 设置物品价格(反射)
/// </summary>
public static void SetItemPrice(ValuableObject vo, float newPrice)
{
dollarValueCurrentField?.SetValue(vo, newPrice);
dollarValueOriginalField?.SetValue(vo, newPrice);
}
/// <summary>
/// 纯随机价格计算(不修改物品)
/// </summary>
public static float GetRandomPrice(int instanceID, float originalPrice, RandomizationConfig config, int? extraSeed = null)
{
float min = Math.Max(0.1f, config.MinMultiplier);
float max = Math.Max(min + 0.1f, config.MaxMultiplier);
int seed = instanceID;
if (extraSeed.HasValue)
seed ^= extraSeed.Value;
var rand = new System.Random(seed);
float multiplier = (float)(rand.NextDouble() * (max - min) + min);
float newPrice = originalPrice;
if (originalPrice > 0)
{
newPrice = originalPrice * multiplier;
newPrice = Mathf.Clamp(newPrice, 1f, 1000000f);
}
return newPrice;
}
/// <summary>
/// 检查物品是否应被随机化(黑白名单、现金袋等)
/// </summary>
public static bool ShouldRandomize(GameObject obj, RandomizationConfig config)
{
string name = obj.name.ToLower();
if (name.Contains("surplus") && !config.RandomizeCashBags)
return false;
if (config.ExcludeHighValueItems)
{
var vo = obj.GetComponent<ValuableObject>();
if (vo != null && GetItemPrice(vo) >= config.HighValueThreshold)
return false;
}
if (config.EnableBlacklist && config.BlacklistKeywords.Any(k => name.Contains(k)))
return false;
if (config.EnableWhitelist && !config.WhitelistKeywords.Any(k => name.Contains(k)))
return false;
return true;
}
/// <summary>
/// 完整应用随机价格(含规则检查和价格修改)
/// </summary>
public static float ApplyRandomPrice(ValuableObject vo, RandomizationConfig config, int? extraSeed = null)
{
if (vo == null) return 0f;
int instanceID = vo.gameObject.GetInstanceID();
float original = GetItemPrice(vo);
if (original <= 0) return original;
if (!ShouldRandomize(vo.gameObject, config))
return original;
float newPrice = GetRandomPrice(instanceID, original, config, extraSeed);
SetItemPrice(vo, newPrice);
return newPrice;
}
}
/// <summary>
/// ValuableObject 扩展方法(可选)
/// </summary>
public static class ValuableObjectExtensions
{
public static float ApplyRandomPrice(this ValuableObject vo, RandomizationConfig config, int? extraSeed = null)
{
return RandomValueService.ApplyRandomPrice(vo, config, extraSeed);
}
}
}