forked from PerpetuumOnline/PerpetuumServer
/
PBSEgg.cs
227 lines (186 loc) · 8.04 KB
/
PBSEgg.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
using System;
using System.Transactions;
using Perpetuum.Data;
using Perpetuum.EntityFramework;
using Perpetuum.ExportedTypes;
using Perpetuum.Log;
using Perpetuum.Players;
using Perpetuum.Units;
using Perpetuum.Zones.Beams;
using Perpetuum.Zones.Eggs;
using Perpetuum.Zones.PBS.DockingBases;
using Perpetuum.Zones.PBS.Turrets;
namespace Perpetuum.Zones.PBS
{
/// <summary>
/// Summonable object that players has to activate to build pbs nodes
/// </summary>
public class PBSEgg : Egg
{
private readonly IEntityServices _entityServices;
private bool _successfulSummon;
public PBSEgg(IEntityServices entityServices)
{
_entityServices = entityServices;
}
public override void Initialize()
{
DespawnTime = TimeSpan.FromHours(1);
base.Initialize();
}
protected override void OnSummonSuccess(IZone zone, Player[] summoners)
{
DoSummon(zone);
}
private void DoSummon(IZone zone)
{
Logger.Info("DoSummon starts on zone:" + zone.Id + " " + this);
if (DeployerPlayer == null)
{
Logger.Error("no deployer player " + this);
return;
}
var corporation = DeployerPlayer.Character.GetPrivateCorporation();
if (corporation == null)
{
Logger.Error("no private corporation was found. Deployer character: " + DeployerPlayer.Character);
DeployerPlayer.Character.SendErrorMessage(new Command("pbsDeployItem"), ErrorCodes.PrivateCorporationAllowedOnly);
return;
}
var centerTile = CurrentPosition.Center;
PBSDockingBase dockingBase;
using (var scope = Db.CreateTransaction())
{
try
{
var deployableItem = (Unit)_entityServices.Factory.CreateWithRandomEID(TargetPBSNodeDefault);
var zoneStorage = zone.Configuration.GetStorage();
zoneStorage.AddChild(deployableItem);
SetStartCore(deployableItem);
dockingBase = deployableItem as PBSDockingBase;
if (dockingBase != null)
PBSHelper.CreatePBSDockingBase(dockingBase);
deployableItem.Owner = corporation.Eid;
deployableItem.Orientation = FastRandom.NextInt(0, 3)*0.25;
deployableItem.CurrentPosition = CurrentPosition.Center;
if (deployableItem is PBSTurret turret)
{
// csak a turret kell, gyerekek nem
Repository.Insert(turret);
}
else
{
// itt mindent insertalunk
deployableItem.Save();
}
Logger.Info("node saved to sql " + deployableItem);
Logger.Info("pbs insert start in zoneuser entities: " + deployableItem);
zone.UnitService.AddUserUnit(deployableItem,centerTile);
Logger.Info("pbs log starting " + deployableItem);
PBSHelper.WritePBSLog(PBSLogType.deployed, deployableItem.Eid, deployableItem.Definition, deployableItem.Owner, DeployerPlayer.Character.Id, background: false, zoneId: zone.Id);
Transaction.Current.OnCompleted((completed) =>
{
if (!completed)
{
Logger.Error("DoSummon rollback " + this);
return;
}
Logger.Info("starting zone enter: " + deployableItem);
deployableItem.AddToZone(zone, centerTile);
Logger.Info("added to zone " + deployableItem);
dockingBase?.OnDockingBaseDeployed();
//draw terrain stuff
PBSHelper.OnPBSObjectDeployed(zone, deployableItem, true, true, true);
Logger.Info("terrain stuff done, sending update. " + deployableItem);
//send update
((IPBSObject) deployableItem).SendNodeUpdate();
zone.CreateBeam(BeamType.red_20sec,
builder =>
builder.WithPosition(CurrentPosition.Center)
.WithState(BeamState.Hit)
.WithDuration(15000));
Logger.Info("pbs node successfully deployed.");
_successfulSummon = true;
});
scope.Complete();
}
catch (Exception ex)
{
Logger.Exception(ex);
}
}
}
private void SetStartCore(Unit unit)
{
if (unit is IPBSAcceptsCore || unit is IPBSCorePump )
{
unit.Core = 0;
}
}
public void CheckDefinitionRelatedConditionsOrThrow(IZone zone, Position spawnPosition, long owner)
{
//the egg WILL build this pbsNode after activation
var pbsNodeEntityDefault = TargetPBSNodeDefault;
if (pbsNodeEntityDefault == EntityDefault.None)
{
Logger.Error("pbsNodeDefinition was not found:" + pbsNodeEntityDefault + " in egg:" + Definition + " " + ED.Name);
throw new PerpetuumException(ErrorCodes.ConsistencyError);
}
// Check zone level (Gamma Tier)
(pbsNodeEntityDefault.Tier.level > zone.Configuration.PBSTechLimit).ThrowIfTrue(ErrorCodes.PBSTechLevelTooHighForZone);
// For bases
if (pbsNodeEntityDefault.CategoryFlags.IsCategory(CategoryFlags.cf_pbs_docking_base))
{
// Special "Expiring" base
if (pbsNodeEntityDefault.Name == DefinitionNames.PBS_EXPIRING_DOCKING_BASE)
{
PBSHelper.ValidateExpiringPBSDockingBasePlacement(zone, spawnPosition, owner, pbsNodeEntityDefault).ThrowIfError();
}
else // Normal PBS Docking base
{
PBSHelper.ValidatePBSDockingBasePlacement(zone, spawnPosition, owner, pbsNodeEntityDefault).ThrowIfError();
}
return;
}
//kiveve amiket lehet kivulre pakolni
if (! PBSHelper.IsPlaceableOutsideOfBase(pbsNodeEntityDefault.CategoryFlags))
{
//checks for corporation's docking base in range
PBSHelper.ValidatePBSNodePlacing(zone, spawnPosition, owner, pbsNodeEntityDefault).ThrowIfError();
}
//itt meg lehet mast is megnezni egyelore nem kellett
}
public EntityDefault TargetPBSNodeDefault
{
get { return ED.Config.TargetEntityDefault; }
}
protected override void OnDead(Unit killer)
{
var zone = Zone;
PBSHelper.OnPBSEggRemoved(zone, this);
base.OnDead(killer);
}
protected override void OnRemovedFromZone(IZone zone)
{
if (!_successfulSummon)
{
PBSHelper.OnPBSEggRemoved(zone, this);
}
base.OnRemovedFromZone(zone);
}
private int _constructionRadius;
public int GetConstructionRadius()
{
return PBSHelper.LazyInitConstructionRadiusByEgg(this, ref _constructionRadius);
}
public Player DeployerPlayer { get; set; }
protected override void OnEnterZone(IZone zone, ZoneEnterType enterType)
{
if (enterType == ZoneEnterType.Deploy)
{
PBSHelper.OnPBSEggDeployed(Zone, this);
}
base.OnEnterZone(zone, enterType);
}
}
}