forked from PerpetuumOnline/PerpetuumServer
-
-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathDrillerModule.cs
More file actions
256 lines (226 loc) · 9.85 KB
/
DrillerModule.cs
File metadata and controls
256 lines (226 loc) · 9.85 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
using Perpetuum.Data;
using Perpetuum.EntityFramework;
using Perpetuum.ExportedTypes;
using Perpetuum.Items;
using Perpetuum.Log;
using Perpetuum.Modules.ModuleProperties;
using Perpetuum.Players;
using Perpetuum.Services.MissionEngine.MissionTargets;
using Perpetuum.Zones;
using Perpetuum.Zones.Beams;
using Perpetuum.Zones.Locking.Locks;
using Perpetuum.Zones.RemoteControl;
using Perpetuum.Zones.Terrains;
using Perpetuum.Zones.Terrains.Materials;
using Perpetuum.Zones.Terrains.Materials.Minerals;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Transactions;
namespace Perpetuum.Modules
{
public class DrillerModule : GathererModule
{
private const int MAX_EP_PER_DAY = 1440;
private readonly ISet<int> LIQUIDS = new HashSet<int>(new int[]
{
(int)MaterialType.Crude,
(int)MaterialType.Liquizit,
(int)MaterialType.Epriton
});
protected readonly MaterialHelper MaterialHelper;
protected readonly ItemProperty MiningAmountModifier;
protected readonly RareMaterialHandler RareMaterialHandler;
public DrillerModule(CategoryFlags ammoCategoryFlags, RareMaterialHandler rareMaterialHandler, MaterialHelper materialHelper)
: base(ammoCategoryFlags, true)
{
RareMaterialHandler = rareMaterialHandler;
MaterialHelper = materialHelper;
MiningAmountModifier = new MiningAmountModifierProperty(this);
AddProperty(MiningAmountModifier);
}
public override void AcceptVisitor(IEntityVisitor visitor)
{
if (!TryAcceptVisitor(this, visitor))
{
base.AcceptVisitor(visitor);
}
}
public override void UpdateProperty(AggregateField field)
{
switch (field)
{
case AggregateField.mining_amount_modifier:
case AggregateField.effect_mining_amount_modifier:
case AggregateField.drone_amplification_mining_amount_modifier:
case AggregateField.effect_excavator_mining_amount_modifier:
{
MiningAmountModifier.Update();
return;
}
}
base.UpdateProperty(field);
}
public List<ItemInfo> Extract(MineralLayer layer, Point location, uint amount)
{
if (!layer.HasMineral(location))
{
return new List<ItemInfo>();
}
MineralExtractor extractor = new MineralExtractor(location, amount, MaterialHelper);
layer.AcceptVisitor(extractor);
return new List<ItemInfo>(extractor.Items);
}
protected override void OnAction()
{
IZone zone = Zone;
if (zone != null)
{
DoExtractMinerals(zone);
}
ConsumeAmmo();
}
protected override int CalculateEp(int materialType)
{
DrillerModule[] activeGathererModules = this is RemoteControlledDrillerModule
? ParentRobot.ActiveModules.OfType<RemoteControlledDrillerModule>().Where(m => m.State.Type != ModuleStateType.Idle).ToArray()
: ParentRobot.ActiveModules.OfType<DrillerModule>().Where(m => m.State.Type != ModuleStateType.Idle).ToArray();
if (activeGathererModules.Length == 0)
{
return 0;
}
TimeSpan avgCycleTime = activeGathererModules.Select(m => m.CycleTime).Average();
TimeSpan t = TimeSpan.FromDays(1).Divide(avgCycleTime);
double chance = (double)MAX_EP_PER_DAY / t.Ticks;
chance /= activeGathererModules.Length;
if (LIQUIDS.Contains(materialType))
{
chance /= 2.0;
}
double rand = FastRandom.NextDouble();
return rand <= chance
? 1
: 0;
}
public virtual void DoExtractMinerals(IZone zone)
{
TerrainLock terrainLock = GetLock().ThrowIfNotType<TerrainLock>(ErrorCodes.InvalidLockType);
MaterialType materialType;
if (ParentRobot is RemoteControlledCreature)
{
materialType = zone.Terrain.GetMaterialTypeAtPosition(terrainLock.Location);
}
else
{
if (!(GetAmmo() is MiningAmmo ammo))
{
return;
}
materialType = ammo.MaterialType;
}
MaterialInfo materialInfo = MaterialHelper.GetMaterialInfo(materialType);
CheckEnablerEffect(materialInfo, terrainLock);
MineralLayer mineralLayer = zone.Terrain
.GetMineralLayerOrThrow(
materialInfo.Type,
(PerpetuumException ex) =>
(ParentRobot as RemoteControlledCreature)
.ProcessIndustrialTarget(terrainLock.Location.Center, 0));
double materialAmount = materialInfo.Amount * MiningAmountModifier.Value;
List<ItemInfo> extractedMaterials = Extract(mineralLayer, terrainLock.Location, (uint)materialAmount);
_ = extractedMaterials.Count
.ThrowIfEqual(
0,
ErrorCodes.NoMineralOnTile,
(PerpetuumException ex) =>
{
RemoteControlledCreature creature = ParentRobot as RemoteControlledCreature;
creature?.ProcessIndustrialTarget(terrainLock.Location.Center, 0);
});
extractedMaterials
.AddRange(RareMaterialHandler.GenerateRareMaterials(materialInfo.EntityDefault.Definition));
CreateBeam(terrainLock.Location, BeamState.AlignToTerrain);
List<(string resourceName, int quantity)> resourceStats = new List<(string resourceName, int quantity)>();
using (TransactionScope scope = Db.CreateTransaction())
{
Debug.Assert(ParentRobot != null, "ParentRobot != null");
Robots.RobotInventory container = ParentRobot.GetContainer();
Debug.Assert(container != null, "container != null");
container.EnlistTransaction();
Player player = ParentRobot is RemoteControlledCreature remoteControlledCreature &&
remoteControlledCreature.CommandRobot is Player ownerPlayer
? ownerPlayer
: ParentRobot as Player;
Debug.Assert(player != null, "player != null");
foreach (ItemInfo material in extractedMaterials)
{
Item item = (Item)Factory.CreateWithRandomEID(material.Definition);
item.Owner = Owner;
item.Quantity = material.Quantity;
container.AddItem(item, true);
int drilledMineralDefinition = material.Definition;
int drilledQuantity = material.Quantity;
player.MissionHandler
.EnqueueMissionEventInfo(
new DrillMineralEventInfo(
player,
drilledMineralDefinition,
drilledQuantity,
terrainLock.Location));
player.Zone?.MiningLogHandler.EnqueueMiningLog(drilledMineralDefinition, drilledQuantity);
resourceStats.Add((material.EntityDefault.Name, material.Quantity));
}
//save container
container.Save();
OnGathererMaterial(zone, player, (int)materialInfo.Type);
Transaction.Current.OnCommited(() => container.SendUpdateToOwnerAsync());
scope.Complete();
}
foreach (var (resourceName, quantity) in resourceStats)
{
try
{
Db.Query()
.CommandText("exec sp_RecordResourceGathered @gathered_on, @resource_name, @quantity")
.SetParameter("@gathered_on", DateTime.UtcNow)
.SetParameter("@resource_name", resourceName)
.SetParameter("@quantity", quantity)
.ExecuteNonQuery();
}
catch (Exception ex)
{
Logger.Error(ex.Message);
}
}
GenerateHeat(EffectType.effect_excavator);
}
protected void CheckEnablerEffect(MaterialInfo materialInfo, Position position)
{
if (!Zone.Configuration.Terraformable)
{
return;
}
if (!materialInfo.EnablerExtensionRequired)
{
return;
}
bool containsEnablerEffect =
ParentRobot.EffectHandler.ContainsEffect(EffectCategory.effcat_pbs_mining_tower_effect) ||
(ParentRobot is RemoteControlledCreature rcu &&
rcu.CommandRobot is Player player &&
player.EffectHandler.ContainsEffect(EffectCategory.effcat_pbs_mining_tower_effect));
containsEnablerEffect
.ThrowIfFalse(
ErrorCodes.MiningEnablerEffectRequired,
(PerpetuumException ex) =>
(ParentRobot as RemoteControlledCreature)
.ProcessIndustrialTarget(position.Center, 0));
}
protected void CheckEnablerEffect(MaterialInfo materialInfo, TerrainLock terrainLock)
{
CheckEnablerEffect(materialInfo, terrainLock.Location);
}
}
}