forked from PerpetuumOnline/PerpetuumServer
-
-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathHarvesterModule.cs
More file actions
160 lines (142 loc) · 6.78 KB
/
HarvesterModule.cs
File metadata and controls
160 lines (142 loc) · 6.78 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
using Perpetuum.Data;
using Perpetuum.EntityFramework;
using Perpetuum.ExportedTypes;
using Perpetuum.Items;
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.Plants;
using Perpetuum.Zones.Terrains.Materials.Plants.Harvesters;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Transactions;
namespace Perpetuum.Modules
{
public class HarvesterModule : GathererModule
{
protected const int MAX_EP_PER_DAY = 1440;
protected readonly PlantHarvester.Factory _plantHarvesterFactory;
protected readonly HarvestingAmountModifierProperty _harverstingAmountModifier;
public HarvesterModule(CategoryFlags ammoCategoryFlags, PlantHarvester.Factory plantHarvesterFactory) : base(ammoCategoryFlags, true)
{
_plantHarvesterFactory = plantHarvesterFactory;
_harverstingAmountModifier = new HarvestingAmountModifierProperty(this);
AddProperty(_harverstingAmountModifier);
cycleTime.AddEffectModifier(AggregateField.effect_harvesting_cycle_time_modifier);
}
public override void AcceptVisitor(IEntityVisitor visitor)
{
if (!TryAcceptVisitor(this, visitor))
{
base.AcceptVisitor(visitor);
}
}
public override void UpdateProperty(AggregateField field)
{
base.UpdateProperty(field);
switch (field)
{
case AggregateField.harvesting_amount_modifier:
case AggregateField.effect_harvesting_amount_modifier:
case AggregateField.drone_amplification_harvesting_amount_modifier:
case AggregateField.drone_remote_command_translation_harvesting_amount_modifier:
case AggregateField.effect_excavator_harvesting_amount_modifier:
{
_harverstingAmountModifier.Update();
break;
}
}
}
protected override int CalculateEp(int materialType)
{
HarvesterModule[] activeGathererModules = this is RemoteControlledHarvesterModule
? ParentRobot.ActiveModules
.OfType<RemoteControlledHarvesterModule>()
.Where(m => m.State.Type != ModuleStateType.Idle)
.ToArray()
: ParentRobot.ActiveModules
.OfType<HarvesterModule>()
.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;
double rand = FastRandom.NextDouble();
return rand <= chance
? 1
: 0;
}
protected override void OnAction()
{
IZone zone = Zone;
if (zone == null)
{
return;
}
DoHarvesting(zone);
ConsumeAmmo();
}
public virtual void DoHarvesting(IZone zone)
{
TerrainLock terrainLock = GetLock().ThrowIfNotType<TerrainLock>(ErrorCodes.InvalidLockType);
CreateBeam(terrainLock.Location, BeamState.AlignToTerrain);
using (TransactionScope scope = Db.CreateTransaction())
{
using (new TerrainUpdateMonitor(zone))
{
PlantInfo plantInfo = zone.Terrain.Plants.GetValue(terrainLock.Location);
double amountModifier = _harverstingAmountModifier.GetValueByPlantType(plantInfo.type);
IPlantHarvester plantHarvester = _plantHarvesterFactory(zone, amountModifier);
if (ParentRobot is RemoteControlledCreature creature)
{
creature.ProcessIndustrialTarget(terrainLock.Location.Center, plantInfo.material);
}
IEnumerable<ItemInfo> harvestedPlants = plantHarvester.HarvestPlant(terrainLock.Location);
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 extractedMaterial in harvestedPlants)
{
Db.Query()
.CommandText("exec sp_RecordResourceGathered @gathered_on, @resource_name, @quantity")
.SetParameter("@gathered_on", DateTime.UtcNow)
.SetParameter("@resource_name", extractedMaterial.EntityDefault.Name)
.SetParameter("@quantity", extractedMaterial.Quantity)
.ExecuteNonQuery();
Item item = (Item)Factory.CreateWithRandomEID(extractedMaterial.Definition);
item.Owner = Owner;
item.Quantity = extractedMaterial.Quantity;
container.AddItem(item, true);
int extractedHarvestDefinition = extractedMaterial.Definition;
int extractedQuantity = extractedMaterial.Quantity;
player.MissionHandler.EnqueueMissionEventInfo(new HarvestPlantEventInfo(player, extractedHarvestDefinition, extractedQuantity, terrainLock.Location));
player.Zone?.HarvestLogHandler.EnqueueHarvestLog(extractedHarvestDefinition, extractedQuantity);
}
container.Save();
OnGathererMaterial(zone, player, (int)plantInfo.type);
Transaction.Current.OnCommited(() => container.SendUpdateToOwnerAsync());
scope.Complete();
}
}
GenerateHeat(EffectType.effect_excavator);
}
}
}