forked from PerpetuumOnline/PerpetuumServer
/
Egg.cs
262 lines (214 loc) · 7.26 KB
/
Egg.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
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using Perpetuum.EntityFramework;
using Perpetuum.ExportedTypes;
using Perpetuum.Log;
using Perpetuum.Players;
using Perpetuum.Players.ExtensionMethods;
using Perpetuum.StateMachines;
using Perpetuum.Timers;
using Perpetuum.Units;
using Perpetuum.Zones.Beams;
namespace Perpetuum.Zones.Eggs
{
/// <summary>
/// Base class for a summonable object
/// </summary>
public abstract class Egg : Unit,IUsableItem
{
private readonly StackFSM _fsm;
private Summoner[] _summoners = new Summoner[0];
private UnitDespawnHelper _despawnHelper;
protected Egg()
{
_fsm = new StackFSM();
_fsm.Push(new WaitForSummonersState(this));
}
public override void AcceptVisitor(IEntityVisitor visitor)
{
if (!TryAcceptVisitor(this, visitor))
base.AcceptVisitor(visitor);
}
protected TimeSpan DespawnTime
{
set => _despawnHelper = UnitDespawnHelper.Create(this,value);
}
private Player[] SummonerPlayers
{
get { return _summoners.Select(s => s.SummonerPlayer).ToArray(); }
}
private IEnumerable<Summoner> Summoners => _summoners;
protected abstract void OnSummonSuccess([NotNull] IZone zone, [NotNull] Player[] summoners);
private void OnSummonSuccess()
{
try
{
var zone = Zone;
if (zone != null)
OnSummonSuccess(zone, SummonerPlayers);
}
finally
{
RemoveFromZone();
}
}
private void AddSummoner(Summoner summoner)
{
ImmutableInterlocked.Update(ref _summoners, current =>
{
if (current.Any(s => s.SummonerPlayer == summoner.SummonerPlayer))
return current;
var updated = current.ToList();
updated.Add(summoner);
return updated.ToArray();
});
}
private void RemoveSummoner(Summoner summoner)
{
ImmutableInterlocked.Update(ref _summoners, current => current.Where(s => s.SummonerPlayer != summoner.SummonerPlayer).ToArray());
}
private void ClearSummoners()
{
_summoners = new Summoner[0];
}
private Player OwnerPlayer
{
get { return Zone.GetPlayer(Owner); }
}
private TimeSpan SummonTime
{
get
{
if (ED.Config.activationTime != null)
return TimeSpan.FromMilliseconds((int) ED.Config.activationTime);
Logger.Error("activationTime is not defined for " + Definition + " " + ED.Name);
return TimeSpan.FromSeconds(30);
}
}
protected override void OnUpdate(TimeSpan time)
{
base.OnUpdate(time);
_despawnHelper?.Update(time,this);
_fsm.Update(time);
}
public void UseItem(Player player)
{
var summoner = new Summoner(this,player);
summoner.Validate().ThrowIfError();
AddSummoner(summoner);
}
private void StartProgressBar()
{
SummonerPlayers.SendStartProgressBar(this,SummonTime);
}
private void EndProgressBar()
{
SummonerPlayers.SendEndProgressBar(this,false);
}
private class WaitForSummonersState : IState
{
private readonly Egg _egg;
private readonly IntervalTimer _timer = new IntervalTimer(TimeSpan.FromSeconds(1));
public WaitForSummonersState(Egg egg)
{
_egg = egg;
}
public void Enter()
{
_egg.ClearSummoners();
}
public void Exit()
{
}
public void Update(TimeSpan time)
{
_timer.Update(time);
if (!_timer.Passed)
return;
_timer.Reset();
foreach (var summoner in _egg.Summoners)
{
if (summoner.Validate() != ErrorCodes.NoError)
{
_egg.RemoveSummoner(summoner);
continue;
}
summoner.SendBeamToPlayer(_timer.Interval);
_egg._fsm.Push(new BeginSummonState(_egg));
return;
}
}
}
private class BeginSummonState : IState
{
private readonly Egg _egg;
private readonly IntervalTimer _timer = new IntervalTimer(TimeSpan.FromSeconds(1));
private TimeTracker _timerSummon;
public BeginSummonState(Egg egg)
{
_egg = egg;
}
public void Enter()
{
_timerSummon = new TimeTracker(_egg.SummonTime);
_egg.StartProgressBar();
}
public void Exit()
{
_egg.EndProgressBar();
}
public void Update(TimeSpan time)
{
_timer.Update(time).IsPassed(CheckSummoners);
_timerSummon.Update(time);
if (!_timerSummon.Expired)
return;
_egg._fsm.Clear();
_egg.OnSummonSuccess();
}
private void CheckSummoners()
{
foreach (var summoner in _egg.Summoners)
{
if (summoner.Validate() != ErrorCodes.NoError)
{
_egg._fsm.Pop();
return;
}
summoner.SendBeamToPlayer(_timer.Interval);
}
}
}
private class Summoner
{
private readonly Egg _egg;
private Position _summonerPosition;
public Summoner(Egg egg, Player summoner)
{
_egg = egg;
SummonerPlayer = summoner;
_summonerPosition = summoner.CurrentPosition;
}
public Player SummonerPlayer { get; private set; }
public void SendBeamToPlayer(TimeSpan duration)
{
_egg.Zone.CreateBeam(BeamType.loot_bolt, b => b.WithSource(SummonerPlayer)
.WithTarget(_egg)
.WithState(BeamState.Hit)
.WithDuration(duration));
}
public ErrorCodes Validate()
{
if (SummonerPlayer.States.Dead || !SummonerPlayer.InZone)
return ErrorCodes.PlayerNotFound;
if (!_egg.IsInRangeOf3D(SummonerPlayer, 10))
return ErrorCodes.ItemOutOfRange;
if (!_summonerPosition.Equals(SummonerPlayer.CurrentPosition))
return ErrorCodes.WTFErrorMedicalAttentionSuggested;
return ErrorCodes.NoError;
}
}
}
}