forked from PerpetuumOnline/PerpetuumServer
-
-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathRobot.cs
More file actions
438 lines (353 loc) · 13 KB
/
Robot.cs
File metadata and controls
438 lines (353 loc) · 13 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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
using Perpetuum.Accounting.Characters;
using Perpetuum.Builders;
using Perpetuum.Containers;
using Perpetuum.Containers.SystemContainers;
using Perpetuum.EntityFramework;
using Perpetuum.ExportedTypes;
using Perpetuum.Items;
using Perpetuum.Items.Templates;
using Perpetuum.Modules;
using Perpetuum.Players;
using Perpetuum.Services.ExtensionService;
using Perpetuum.Services.Insurance;
using Perpetuum.Timers;
using Perpetuum.Units;
using Perpetuum.Zones;
using Perpetuum.Zones.DamageProcessors;
using Perpetuum.Zones.Effects;
using Perpetuum.Zones.Locking;
using Perpetuum.Zones.Locking.Locks;
using System;
using System.Collections.Generic;
using System.Linq;
namespace Perpetuum.Robots
{
public abstract partial class Robot : Unit, IUsableItem
{
private Lazy<IEnumerable<Module>> modules;
private Lazy<IEnumerable<ActiveModule>> activeModules;
private Lazy<IEnumerable<Item>> components;
private Lazy<IEnumerable<RobotComponent>> robotComponents;
private readonly TimeSpan overheatCooldownPeriod = TimeSpan.FromMilliseconds(1650);
private readonly IntervalTimer overheatCooldownTimer;
private const double HeatDissipation = 1.75;
protected Robot()
{
InitLockHander();
InitProperties();
OverheatHandler = new OverheatHandler(this);
overheatCooldownTimer = new IntervalTimer(overheatCooldownPeriod);
}
public OverheatHandler OverheatHandler { get; private set; }
public RobotHelper RobotHelper { protected get; set; }
public InsuranceHelper InsuranceHelper { protected get; set; }
public RobotTemplate Template { get; set; }
public bool IsSelected => RobotHelper.IsSelected(this);
public bool IsBlessed => EffectHandler.ContainsEffect(EffectType.effect_gods_bless);
public override double Health => IsRepackaged
? base.Health
: ArmorMax > 0.0 && Armor > 0.0
? Armor.Ratio(ArmorMax) * 100
: base.Health;
public override double Mass => RobotComponents.Sum(c => c.Mass);
public bool HasModule => Modules.Any();
public bool IsItemsInContainer
{
get
{
if (IsRepackaged)
{
return false;
}
RobotInventory robotInventory = GetContainer();
return robotInventory != null && robotInventory.HasChildren;
}
}
public override bool IsStackable => base.IsStackable && IsRepackaged;
public IEnumerable<Extension> ExtensionBonusEnablerExtensions => ExtensionBonuses.Select(cb => new Extension(cb.extensionId, 1));
protected IEnumerable<ExtensionBonus> ExtensionBonuses => RobotComponents.SelectMany(component => component.ExtensionBonuses);
public IEnumerable<Module> Modules => modules.Value;
public IEnumerable<ActiveModule> ActiveModules => activeModules.Value;
public IEnumerable<Item> Components => components.Value;
public IEnumerable<RobotComponent> RobotComponents => robotComponents.Value;
public override double Volume
{
get
{
if (IsRepackaged)
{
return base.Volume;
}
double volume = RobotComponents.Sum(c => c.Volume);
volume *= Quantity;
return volume;
}
}
public bool IsTrashed => Trashcan.IsItemTrashed(this);
private void ResetTimer()
{
overheatCooldownTimer.Interval = overheatCooldownPeriod;
}
public void IncreaseHeatLevelByValue(EffectType effectType, double value)
{
if (EffectHandler.ContainsEffect(effectType))
{
OverheatHandler.Increase(value);
}
}
public override void Initialize()
{
InitComponents();
base.Initialize();
}
public override void AcceptVisitor(IEntityVisitor visitor)
{
if (!TryAcceptVisitor(this, visitor))
{
base.AcceptVisitor(visitor);
}
}
public void VisitModules(IEntityVisitor visitor)
{
foreach (Module module in Modules)
{
module.AcceptVisitor(visitor);
}
}
public void VisitRobotComponents(IEntityVisitor visitor)
{
foreach (RobotComponent component in RobotComponents)
{
component.AcceptVisitor(visitor);
}
}
public void VisitRobotInventory(IEntityVisitor visitor)
{
RobotInventory container = GetContainer();
container?.AcceptVisitor(visitor);
}
public override IDictionary<string, object> GetDebugInfo()
{
IDictionary<string, object> info = base.GetDebugInfo();
info.Add("locksCount", lockHandler.Count);
return info;
}
public void FullArmorRepair()
{
DynamicProperties.Update(k.armor, 1.0);
Armor = ArmorMax;
}
public void FullCoreRecharge()
{
double currentCore = DynamicProperties.GetOrAdd<double>(k.currentCore);
double coreMaxValue = CoreMax;
if (currentCore >= coreMaxValue)
{
return;
}
DynamicProperties.Update(k.currentCore, coreMaxValue);
Core = CoreMax;
}
[CanBeNull]
public RobotInventory GetContainer()
{
return Children.OfType<RobotInventory>().FirstOrDefault();
}
public void StopAllModules()
{
StopAllModules(new Type[0]);
}
public void StopAllModules(Type[] except)
{
ActiveModule[] activeModules = ActiveModules
.Where(x => !except.Contains(x.GetType()))
.ToArray();
foreach (ActiveModule module in activeModules)
{
module.State.SwitchTo(ModuleStateType.Idle);
}
}
public override void OnDeleteFromDb()
{
InsuranceHelper.DeleteAndInform(this);
base.OnDeleteFromDb();
}
public void EmptyRobot(Character character, Container targetContainer, bool withContainer = true)
{
foreach (Module module in Modules)
{
module.Unequip(targetContainer);
}
if (!withContainer)
{
return;
}
RobotInventory container = GetContainer();
container?.RelocateItems(character, character, container.GetItems(), targetContainer);
}
public override Dictionary<string, object> ToDictionary()
{
Dictionary<string, object> dictionary = base.ToDictionary();
foreach (RobotComponent component in RobotComponents)
{
dictionary.Add(component.ComponentName, component.ToDictionary());
}
RobotInventory container = GetContainer();
if (container != null)
{
dictionary.Add(k.container, container.ToDictionary());
}
dictionary.Add(k.decay, Decay);
dictionary.Add(k.tint, Tint);
return dictionary;
}
[CanBeNull]
public Module FindModuleByCategoryFlag(CategoryFlags cf)
{
return Modules.FirstOrDefault(m => m.IsCategory(cf));
}
[CanBeNull]
public Module GetModule(long moduleEid)
{
return Modules.FirstOrDefault(m => m.Eid == moduleEid);
}
[CanBeNull]
public RobotComponent GetRobotComponent(string componentName)
{
return GetRobotComponent(componentName.ToEnum<RobotComponentType>());
}
[NotNull]
public RobotComponent GetRobotComponentOrThrow(RobotComponentType componentType)
{
return GetRobotComponent(componentType).ThrowIfNull(ErrorCodes.RequiredComponentNotFound);
}
[CanBeNull]
public RobotComponent GetRobotComponent(RobotComponentType componentType)
{
return RobotComponents.FirstOrDefault(c => c.Type == componentType);
}
public void CheckEnergySystemAndThrowIfFailed(Module module, bool isRemoving = false)
{
if (!CheckPowerGridForModule(module, isRemoving))
{
throw PerpetuumException.Create(ErrorCodes.OutOfPowergrid);
}
if (!CheckCpuForModule(module, isRemoving))
{
throw PerpetuumException.Create(ErrorCodes.OutOfCpu);
}
}
public void CheckEnergySystemAndThrowIfFailed()
{
if (PowerGrid < 0)
{
throw PerpetuumException.Create(ErrorCodes.OutOfPowergrid).SetData("powerGrid", Math.Abs(PowerGridMax - PowerGrid)).SetData("powerGridMax", PowerGridMax);
}
if (Cpu < 0)
{
throw PerpetuumException.Create(ErrorCodes.OutOfCpu).SetData("cpu", Math.Abs(CpuMax - Cpu)).SetData("cpuMax", CpuMax);
}
}
public void CreateComponents()
{
foreach (Item component in Template.BuildComponents())
{
AddChild(component);
}
}
[CanBeNull]
public T GetRobotComponent<T>() where T : RobotComponent
{
return RobotComponents.OfType<T>().FirstOrDefault();
}
protected virtual void OnLockError(Lock @lock, ErrorCodes error)
{
}
protected virtual void OnLockStateChanged(Lock @lock)
{
States.LockSomething = lockHandler.Count > 0;
if (@lock is UnitLock unitLock)
{
UpdateTypes |= UnitUpdateTypes.Lock;
UpdateVisibilityOf(unitLock.Target);
}
AnonymousBuilder<Packet> builder = new AnonymousBuilder<Packet>(() => LockPacketBuilder.BuildPacket(@lock));
OnBroadcastPacket(builder.ToProxy());
}
protected override void OnUpdate(TimeSpan time)
{
base.OnUpdate(time);
lockHandler.Update(time);
foreach (RobotComponent robotComponent in RobotComponents)
{
robotComponent.Update(time);
}
if (overheatCooldownTimer.Passed)
{
OverheatHandler.Decrease(HeatDissipation);
ResetTimer();
}
overheatCooldownTimer.Update(time);
}
protected internal override double ComputeHeight()
{
return RobotComponents.Sum(c => c.ComputeHeight());
}
protected override void OnDamageTaken(Unit source, DamageTakenEventArgs e)
{
base.OnDamageTaken(source, e);
double decayChance = this.decayChance.Value;
double random = FastRandom.NextDouble();
if (decayChance >= random)
{
int d = Decay;
if (d > 0)
{
d--;
Decay = d;
}
}
}
protected override bool IsDetected(Unit target)
{
return lockHandler.IsLocked(target) || base.IsDetected(target);
}
protected override void OnBeforeRemovedFromZone(IZone zone)
{
Module remoteController = Modules?.FirstOrDefault(x => x is RemoteControllerModule);
if (remoteController != null)
{
(remoteController as RemoteControllerModule).CloseAllChannels();
}
base.OnBeforeRemovedFromZone(zone);
}
//TODO: review if it's still has to be Lazy
private void InitComponents()
{
//Peanuts Plague
components = new Lazy<IEnumerable<Item>>(() => Children.OfType<Item>().ToArray());
_ = components.Value; // force evaluation
robotComponents = new Lazy<IEnumerable<RobotComponent>>(() => Components.OfType<RobotComponent>().ToArray());
_ = robotComponents.Value; // force evaluation
modules = new Lazy<IEnumerable<Module>>(() => RobotComponents
.SelectMany(c =>
{
c.Initialize();
return c.Modules;
})
.ToArray());
_ = modules.Value; // force evaluation
activeModules = new Lazy<IEnumerable<ActiveModule>>(() => Modules.OfType<ActiveModule>().ToArray());
_ = activeModules.Value; // force evaluation
}
protected override void OnEnterZone(IZone zone, ZoneEnterType enterType)
{
base.OnEnterZone(zone, enterType);
CamouflageUpdate();
}
public void UseItem(Player player)
{
throw new NotImplementedException();
}
}
}