forked from PerpetuumOnline/PerpetuumServer
-
-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathLargeHarvesterModule.cs
More file actions
103 lines (89 loc) · 4.77 KB
/
LargeHarvesterModule.cs
File metadata and controls
103 lines (89 loc) · 4.77 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
using Perpetuum.Data;
using Perpetuum.EntityFramework;
using Perpetuum.ExportedTypes;
using Perpetuum.Items;
using Perpetuum.Players;
using Perpetuum.Services.MissionEngine.MissionTargets;
using Perpetuum.Zones;
using Perpetuum.Zones.Beams;
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 sealed class LargeHarvesterModule : HarvesterModule
{
public LargeHarvesterModule(CategoryFlags ammoCategoryFlags, PlantHarvester.Factory plantHarvesterFactory)
: base(ammoCategoryFlags, plantHarvesterFactory)
{
}
public override void DoHarvesting(IZone zone)
{
Position centralTile = ParentRobot.PositionWithHeight.OffsetInDirection(ParentRobot.Direction, 3);
List<Position> mineralPositions = centralTile.GetTwentyFourNeighbours(ParentRobot.Zone.Size).ToList();
mineralPositions.Add(centralTile);
int emptyTilesCounter = 0;
// make it parallel
foreach (Position position in mineralPositions)
{
using (TransactionScope scope = Db.CreateTransaction())
{
using (new TerrainUpdateMonitor(zone))
{
PlantInfo plantInfo = zone.Terrain.Plants.GetValue(position);
if (plantInfo.type == PlantType.NotDefined ||
Zone.Configuration.PlantRules.GetPlantRule(plantInfo.type) == null ||
plantInfo.material <= 0)
{
emptyTilesCounter++;
_ = emptyTilesCounter
.ThrowIfEqual(25, ErrorCodes.NoPlantOnTile);
continue;
}
CreateBeam(position, BeamState.AlignToTerrain);
double amountModifier = _harverstingAmountModifier.GetValueByPlantType(plantInfo.type);
IPlantHarvester plantHarvester = _plantHarvesterFactory(zone, amountModifier);
IEnumerable<ItemInfo> harvestedPlants = plantHarvester.HarvestPlant(position);
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, position));
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, 6);
}
}
}