-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDroneHijacker.cs
303 lines (259 loc) · 10.9 KB
/
DroneHijacker.cs
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
using FMOD;
using HarmonyLib;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TwitchLib.Client.Events;
using static CobaltChatCore.CommandManager;
using static CobaltChatCore.Configuration;
namespace CobaltChatCore
{
public class ChatterDrone : IComparable<ChatterDrone>
{
public string owner;
public WeakReference droneRef;
public bool nameStaysUp = false;
public Color color;
public int moves = Configuration.Instance.ChatterDroneMoves;
public int shots = Configuration.Instance.ChatterDroneShots;
public ChatterDrone(string selectedChatter, WeakReference weakReference) : this(selectedChatter, weakReference, new Color())
{
}
public ChatterDrone(string selectedChatter, WeakReference weakReference, Color color)
{
this.owner = selectedChatter;
this.droneRef = weakReference;
this.color = color;
}
public int CompareTo(ChatterDrone other)
{
if (droneRef.Target == null)
return -1;
if (other.droneRef.Target == null)
return 1;
StuffBase a = (StuffBase)droneRef.Target;
StuffBase b = (StuffBase)other.droneRef.Target;
return a.x > b.x ? 1 : -1;//we will never have 2 objects in the same place
}
}
class SingleDroneShot : CardAction
{
StuffBase drone;
public SingleDroneShot(StuffBase drone)
{
this.drone = drone;
}
public override void Begin(G g, State s, Combat c)
{
this.timer = 0;
c.Queue(drone.GetActions(s, c));
}
}
class SingleDroneMove : ADroneMove
{
StuffBase drone;
public SingleDroneMove(StuffBase drone, DroneHijacker.ChatterDroneAction direction)
{
this.drone = drone;
dir = direction == DroneHijacker.ChatterDroneAction.LEFT ? -1 : 1;
timer = 0.2;
isRandom = false;
}
public override void Begin(G g, State s, Combat c)
{
var newX = drone.x + dir;
//check for collission
foreach (StuffBase stuff in c.stuff.Values)
{
if (stuff.x == newX && stuff != drone)
{
c.DestroyDroneAt(s, newX, true);
c.DestroyDroneAt(s, drone.x, true);
return;
}
}
//move
c.stuff.Remove(drone.x);
drone.x = newX;
c.stuff.Add(drone.x, drone);
Audio.Play(new GUID?(FSPRO.Event.Move));
//check for walls
if (c.leftWall.HasValue && newX < c.leftWall.Value)
c.DestroyDroneAt(s, newX, true);
if (c.rightWall.HasValue && newX >= c.rightWall.Value)
c.DestroyDroneAt(s, newX, true);
}
}
public class DroneHijacker
{
public static List<ChatterDrone> hijackedDrones = new List<ChatterDrone>();
public static List<string> Owners { get => hijackedDrones.Select(h => h.owner).ToList(); }
ConcurrentQueue<Action<Combat>> combatActions = new ConcurrentQueue<Action<Combat>>();
public enum ChatterDroneAction {LEFT, RIGHT, SHOOT }
ILogger Logger;
public void Setup(ILogger logger)
{
this.Logger = logger;
//make sure we don't select the current enemy
CobaltChatCoreManifest.EventHub.ConnectToEvent<string>(CobaltChatCoreManifest.SelectEnemyChatterEvent, (s) => { hijackedDrones.Add(new ChatterDrone(s, new WeakReference(null))); });
CobaltChatCoreManifest.EventHub.ConnectToEvent<ASpawn>(CobaltChatCoreManifest.ASpawnBeginPre, (asp) => PrefixReplaceDroneWithChatter(asp));
CobaltChatCoreManifest.EventHub.ConnectToEvent<ASpawn>(CobaltChatCoreManifest.ASpawnBeginPost, (asp) => PostfixSortDronesAndFixNamePositions(asp));
CobaltChatCoreManifest.EventHub.ConnectToEvent<string>(CobaltChatCoreManifest.EnterRouteEvent, (s) => OnStateEnterClearDrones());
//CobaltChatCoreManifest.EventHub.ConnectToEvent<State>(CobaltChatCoreManifest.StartCombatEvent, (s) => OnStateEnterClearDrones());
CobaltChatCoreManifest.EventHub.ConnectToEvent<Combat>(CobaltChatCoreManifest.UpdateCombatEvent, (c) => OnCombatUpdate(c));
CobaltChatCoreManifest.EventHub.ConnectToEvent<string>(CobaltChatCoreManifest.ChatterEjectedEvent, (s) => OnChatterEjected(s));
if (Configuration.Instance.ChatterDroneMoves > 0)
commands.Add(Configuration.Instance.ChatterDroneMoveLeftCommand, new TwitchCommand(TwitchCommand.AccessLevel.EVERYONE, (e) =>
{
foreach(ChatterDrone chd in hijackedDrones)
{
if (chd.owner == e.ChatMessage.DisplayName)
{
combatActions.Enqueue((c) => { DoDroneAction(chd, ChatterDroneAction.LEFT, c); });
return;
}
}
}));
if (Configuration.Instance.ChatterDroneMoves > 0)
commands.Add(Configuration.Instance.ChatterDroneMoveRightCommand, new TwitchCommand(TwitchCommand.AccessLevel.EVERYONE, (e) =>
{
foreach (ChatterDrone chd in hijackedDrones)
{
if (chd.owner == e.ChatMessage.DisplayName)
{
combatActions.Enqueue((c) => { DoDroneAction(chd, ChatterDroneAction.RIGHT, c); });
return;
}
}
}));
if (Configuration.Instance.ChatterDroneShots > 0)
commands.Add(Configuration.Instance.ChatterDroneShootCommand, new TwitchCommand(TwitchCommand.AccessLevel.EVERYONE, (e) =>
{
foreach (ChatterDrone chd in hijackedDrones)
{
if (chd.owner == e.ChatMessage.DisplayName)
{
combatActions.Enqueue((c) => { DoDroneAction(chd, ChatterDroneAction.SHOOT, c); });
return;
}
}
}));
}
void OnChatterEjected(string s)
{
foreach (ChatterDrone chd in hijackedDrones)
{
if (chd.owner == s && chd.droneRef.Target != null)
{
TwitchChat.SendMessageToChat(Configuration.Instance.ChatterDroneEjectText.Replace("{User}", s));
Logger.LogInformation($"Ejecting ChatterDrone {s}");
chd.droneRef.Target = null;
break;
}
}
}
void DoDroneAction(ChatterDrone chd, ChatterDroneAction action, Combat c)
{
StuffBase drone = (StuffBase)chd.droneRef.Target;
//drones are not immediatelly removed from WeakReference - check like this instead
if (drone == null || !c.stuff.Keys.Contains(drone.x) || c.stuff[drone.x] != drone)
{
//don't do anything, drone dead
return;
}
switch (action)
{
case ChatterDroneAction.LEFT:
if (chd.moves > 0)
{
c.Queue(new SingleDroneMove(drone, ChatterDroneAction.LEFT));
chd.moves--;
}
break;
case ChatterDroneAction.RIGHT:
if (chd.moves > 0)
{
c.Queue(new SingleDroneMove(drone, ChatterDroneAction.RIGHT));
chd.moves--;
}
break;
case ChatterDroneAction.SHOOT:
if (chd.shots > 0)
{
c.Queue(new SingleDroneShot(drone));
chd.shots--;
}
break;
}
}
void OnCombatUpdate(Combat combat)
{
if (combatActions.TryDequeue(out Action<Combat> action))
{
action.Invoke(combat);
}
}
void OnStateEnterClearDrones()
{
Logger.LogInformation("Cleaned drone list");
hijackedDrones.Clear();
//currentState = null;
}
void PostfixSortDronesAndFixNamePositions(ASpawn spawn)
{
hijackedDrones.Sort();
bool nameSorter = true;
foreach (ChatterDrone cd in hijackedDrones)
{
cd.nameStaysUp = nameSorter;
nameSorter = !nameSorter;
}
}
void PrefixReplaceDroneWithChatter(ASpawn spawn)
{
if (!Configuration.Instance.AllowChattersAsDrones)
return;
if (!spawn.fromPlayer && !Configuration.Instance.AllowChatterDroneEnemies)
return;
if (spawn.thing is not AttackDrone && spawn.thing is not ShieldDrone && spawn.thing is not EnergyDrone)
{
Logger.LogInformation("Not allowed drone type");
return;
}
var selectedChatter = SelectChatter();
if (selectedChatter != null)
{
if (!CommandManager.ChatterColors.TryGetValue(selectedChatter, out Color color))
color = new Color(1,1,1);
ChatterDrone chdrone = new ChatterDrone(selectedChatter, new WeakReference(spawn.thing), color);
Logger.LogInformation($"Drone hijacked by {selectedChatter}");
hijackedDrones.Add(chdrone);
spawn.isaacNamesIt = false;
if (spawn.fromPlayer)
spawn.thing.droneNameAccordingToIsaac = selectedChatter;
}
else
{
Logger.LogInformation($"Normal drone selected");
}
}
string SelectChatter()
{
Random random = new Random();
double randomNumber = random.NextDouble();
List<string> notAllowed = Owners;
List<string> list = CommandManager.ChattersAvailable.ToImmutableDictionary().Keys.Where(k => !notAllowed.Contains(k)).ToList();
if (list.Count > 0 && Configuration.Instance.ChatterDroneChance > randomNumber)
{
return list.ElementAt(random.Next(0, list.Count()));
}
else
return null;
}
}
}