-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAISystem.cs
More file actions
349 lines (305 loc) · 10.8 KB
/
AISystem.cs
File metadata and controls
349 lines (305 loc) · 10.8 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
using System.Text;
using Newtonsoft.Json;
using Terraria;
namespace MonsterSpeed;
// 新增:AI模式配置类
public class AIModes
{
[JsonProperty("步进说明", Order = -3)]
public string Text2 = "0递增(从最小值加到最大值,回到最小值), 1递减(从最大值减到最小值,回到最大值), 2往复(从最小值到最大值,再从最大值回到最小值), 3随机(在最小值与最大值之间随机)";
[JsonProperty("启用模式", Order = -2)]
public bool Enabled { get; set; } = false;
[JsonProperty("固定AI", Order = -1)]
public Dictionary<int, float> FixedAI { get; set; } = new Dictionary<int, float>();
[JsonProperty("步进AI", Order = 0)]
public Dictionary<int, AISetting> StepAI { get; set; } = new Dictionary<int, AISetting>();
[JsonProperty("固定LocalAI", Order = 0)]
public Dictionary<int, float> FixedLocalAI { get; set; } = new Dictionary<int, float>();
[JsonProperty("步进LocalAI", Order = 2)]
public Dictionary<int, AISetting> StepLocalAI { get; set; } = new Dictionary<int, AISetting>();
[JsonProperty("原版AI", Order = 2)]
public List<BossAI> BossAI { get; set; } = new List<BossAI>();
}
// 步进AI的配置
public class AISetting
{
[JsonProperty("模式")]
public int Type { get; set; } = 2;
[JsonProperty("步长")]
public float Step { get; set; } = 1.0f;
[JsonProperty("最小")]
public float MinValue { get; set; } = -10.0f;
[JsonProperty("最大")]
public float MaxValue { get; set; } = 10.0f;
[JsonProperty("循环")]
public bool Loop { get; set; } = true;
}
// 新增:AI模式状态类
public class AIState
{
public Dictionary<int, int> Directions { get; set; } = new Dictionary<int, int>();
public Dictionary<int, float> Values { get; set; } = new Dictionary<int, float>();
public Dictionary<int, int> LocalDirections { get; set; } = new Dictionary<int, int>();
public Dictionary<int, float> LocalValues { get; set; } = new Dictionary<int, float>();
}
// 原版 BOSS AI
public class BossAI
{
[JsonProperty("保持头顶", Order = -1)]
public bool AlwaysTop { get; set; } = false;
[JsonProperty("白光AI", Order = 0)]
public bool HallowBoss { get; set; } = false;
[JsonProperty("猪鲨AI", Order = 1)]
public bool DukeFishron { get; set; } = false;
[JsonProperty("鹿角怪AI", Order = 2)]
public bool Deerclops { get; set; } = false;
[JsonProperty("鹦鹉螺AI", Order = 3)]
public bool BloodNautilus { get; set; } = false;
}
internal class AISystem
{
#region 应用步进AI模式控制
public static void AIPairs(NPC npc, AIModes aiMode, string npcName, ref bool handled)
{
if (!aiMode.Enabled) return;
// 从NpcState获取AI状态
var state = StateApi.GetState(npc);
if (state?.AIState == null) return;
bool flag = false;
// 处理固定AI
if (aiMode.FixedAI != null && aiMode.FixedAI.Count > 0)
{
FixedAI(npc, aiMode.FixedAI, state.AIState);
flag = true;
}
// 处理固定localAI
if (aiMode.FixedLocalAI != null && aiMode.FixedLocalAI.Count > 0)
{
FixedLocalAI(npc, aiMode.FixedLocalAI, state.AIState);
flag = true;
}
// 处理步进AI
if (aiMode.StepAI != null && aiMode.StepAI.Count > 0)
{
StepAI(npc, aiMode.StepAI, state.AIState, npcName, false);
flag = true;
}
// 处理步进localAI
if (aiMode.StepLocalAI != null && aiMode.StepLocalAI.Count > 0)
{
StepAI(npc, aiMode.StepLocalAI, state.AIState, npcName, true);
flag = true;
}
handled = !flag;
}
#endregion
#region 固定AI
private static void FixedAI(NPC npc, Dictionary<int, float> fixedAI, AIState state)
{
foreach (var kvp in fixedAI)
{
int key = kvp.Key;
float val = kvp.Value;
if (key >= 0 && key < npc.ai.Length)
{
npc.ai[key] = val;
state.Values[key] = val;
}
}
}
#endregion
#region 固定localAI
private static void FixedLocalAI(NPC npc, Dictionary<int, float> fixedLocalAI, AIState state)
{
foreach (var kvp in fixedLocalAI)
{
int key = kvp.Key;
float val = kvp.Value;
if (key >= 0 && key < npc.localAI.Length)
{
npc.localAI[key] = val;
state.LocalValues[key] = val;
}
}
}
#endregion
#region 步进AI
private static Random random = new Random(); // 静态Random实例
private static void StepAI(NPC npc, Dictionary<int, AISetting> stepAI, AIState state, string npcName, bool isLocalAI)
{
foreach (var kvp in stepAI)
{
int Index = kvp.Key; // 键就是AI索引
AISetting setting = kvp.Value;
// 检查索引范围
var target = isLocalAI ? npc.localAI : npc.ai;
if (Index < 0 || Index >= target.Length) continue;
// 初始化当前值
var val = isLocalAI ? state.LocalValues : state.Values;
var Dict = isLocalAI ? state.LocalDirections : state.Directions;
if (!val.ContainsKey(Index))
{
val[Index] = target[Index];
}
// 初始化方向(用于往复模式)
if (!Dict.ContainsKey(Index) && setting.Type == 2)
{
Dict[Index] = 1;
}
// 修复:使用正确的字典获取当前值
float Value = val[Index];
float newVal = Value;
// 根据模式类型计算新值
switch (setting.Type)
{
case 0: // 递增模式
newVal = Value + setting.Step;
if (newVal > setting.MaxValue)
{
newVal = setting.Loop ? setting.MinValue : setting.MaxValue;
}
break;
case 1: // 递减模式
newVal = Value - setting.Step;
if (newVal < setting.MinValue)
{
newVal = setting.Loop ? setting.MaxValue : setting.MinValue;
}
break;
case 2: // 往复模式
newVal = Value + (setting.Step * Dict[Index]);
if (newVal >= setting.MaxValue || newVal <= setting.MinValue)
{
// 使用 Math.Clamp 确保值在范围内
newVal = Math.Clamp(newVal, setting.MinValue, setting.MaxValue);
// 反转方向
Dict[Index] *= -1;
}
break;
case 3: // 随机模式 - 修复:简化逻辑
newVal = (float)(random.NextDouble() * (setting.MaxValue - setting.MinValue) + setting.MinValue);
break;
}
// 对于非随机模式,限制(随机模式已经限制在范围内,所以不需要额外限制)
if (setting.Type != 3)
{
newVal = Math.Max(setting.MinValue, Math.Min(setting.MaxValue, newVal));
}
// 更新AI值
target[Index] = newVal;
val[Index] = newVal;
}
}
#endregion
#region 输出正在赋值的AI信息
private static string GetModeName(int type)
{
return type switch
{
0 => "递增",
1 => "递减",
2 => "往复",
3 => "随机",
_ => "未知"
};
}
public static string GetAiInfo(AIState state, AIModes aiMode, string npcName)
{
if (state == null) return string.Empty;
var info = new StringBuilder();
// 显示固定AI
if (aiMode.FixedAI != null && aiMode.FixedAI.Count > 0)
{
info.Append("\n [固定] ");
foreach (var kvp in aiMode.FixedAI)
{
info.Append($" [ai{kvp.Key}] {kvp.Value:F1} ");
}
}
// 显示固定localAI
if (aiMode.FixedLocalAI != null && aiMode.FixedLocalAI.Count > 0)
{
info.Append("\n [固定] ");
foreach (var kvp in aiMode.FixedLocalAI)
{
info.Append($" [Lai{kvp.Key}] {kvp.Value:F1} ");
}
}
// 显示步进AI
if (aiMode.StepAI != null && aiMode.StepAI.Count > 0)
{
info.Append("\n [步进] ");
foreach (var kvp in aiMode.StepAI)
{
var aiIndex = kvp.Key;
var setting = kvp.Value;
if (state.Values.ContainsKey(aiIndex))
{
string modeName = GetModeName(setting.Type);
info.Append($" [ai{aiIndex}] {state.Values[aiIndex]:F1}({modeName}) ");
}
}
}
// 显示步进localAI
if (aiMode.StepLocalAI != null && aiMode.StepLocalAI.Count > 0)
{
info.Append("\n [步进] ");
foreach (var kvp in aiMode.StepLocalAI)
{
var aiIndex = kvp.Key;
var setting = kvp.Value;
if (state.LocalValues.ContainsKey(aiIndex))
{
string modeName = GetModeName(setting.Type);
info.Append($" [Lai{aiIndex}] {state.LocalValues[aiIndex]:F1}({modeName}) ");
}
}
}
// 对结果应用渐变色效果
string result = info.ToString().Trim();
if (!string.IsNullOrEmpty(result))
{
return Tool.TextGradient(result);
}
return result;
}
#endregion
#region 泰拉瑞亚 Boss AI
public static void TR_AI(BossAI bossAI, NPC npc, ref bool handled)
{
Player plr = Main.player[npc.target];
var flag = false;
//始终保持保持玩家头顶
if (bossAI.AlwaysTop && !plr.dead && plr.active)
{
npc.AI_120_HallowBoss_DashTo(plr.position);
flag = true;
}
//猪鲨AI
if (bossAI.DukeFishron)
{
npc.AI_069_DukeFishron();
flag = true;
}
//鹦鹉螺AI
if (bossAI.BloodNautilus)
{
npc.AI_117_BloodNautilus();
flag = true;
}
//白光AI
if (bossAI.HallowBoss)
{
npc.AI_120_HallowBoss();
flag = true;
}
//鹿角怪AI
if (bossAI.Deerclops)
{
npc.AI_123_Deerclops();
flag = true;
}
handled = !flag;
}
#endregion
}