-
Notifications
You must be signed in to change notification settings - Fork 2
/
HopperFreightContainer.cs
124 lines (108 loc) · 3.52 KB
/
HopperFreightContainer.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
using System;
using System.Collections.Generic;
using FortressCraft.Community.Utilities;
using System.Text;
public class HopperFreightContainer : FreightSystemInterface
{
public FreightCartStation Station;
public StorageMachineInterface Machine;
public ItemBase OfferItem;
public ItemBase RequestItem;
public int RequestLimit;
public int OfferLimit;
public HopperFreightContainer(StorageMachineInterface machine, FreightCartStation station)
{
this.Machine = machine;
this.Station = station;
}
public List<FreightListing> FreightOfferings
{
get
{
List<FreightListing> offers = new List<FreightListing>(1);
if (Station.OfferAll)
Machine.IterateContents(HopperIterator, offers);
else if (OfferItem != null)
{
int count = GetItemCountFromItemBase(OfferItem);
if (count > OfferLimit)
offers.Add(new FreightListing(OfferItem, count - OfferLimit));
else
offers.Add(new FreightListing(OfferItem, 0)); // Add listing of zero to register in system monitor
}
return offers;
}
}
bool HopperIterator(ItemBase item, object offers)
{
List<FreightListing> offerslist = (List<FreightListing>)offers;
if (offerslist == null)
return false;
bool matchfound = false;
foreach (FreightListing entry in offers as List<FreightListing>)
{
if (entry.Equals(item))
{
entry.Quantity += item.GetAmount();
matchfound = true;
break;
}
}
if (!matchfound)
(offers as List<FreightListing>).Add(new FreightListing(item, item.GetAmount()));
return true;
}
public List<FreightListing> FreightRequests
{
get
{
List<FreightListing> requests = new List<FreightListing>(1);
if (RequestItem != null)
{
int count = GetItemCountFromItemBase(RequestItem);
if (count < RequestLimit)
requests.Add(new FreightListing(RequestItem, RequestLimit - count));
else
requests.Add(new FreightListing(RequestItem, 0));
}
return requests;
}
}
private int GetItemCountFromItemBase(ItemBase item)
{
if (item.mnItemID == -1)
{
ItemCubeStack cubes = item as ItemCubeStack;
return Machine.CountCubes(cubes.mCubeType, cubes.mCubeValue);
}
else
{
return Machine.CountItems(item.mnItemID);
}
}
public bool ProvideFreight(ItemBase item)
{
if (item.mType == ItemType.ItemCubeStack)
{
ItemCubeStack stack = (ItemCubeStack)item;
return Machine.TryExtractItemsOrCubes(Station, item.mnItemID, stack.mCubeType, stack.mCubeValue, item.GetAmount());
}
else
return Machine.TryExtractItems(null, item.mnItemID, item.GetAmount());
}
public bool ReceiveFreight(ItemBase item)
{
return Machine.TryInsert(Station, item);
}
public bool FreightDataRequest
{
get
{
return false;
}
}
public void FreightNetworkData(List<FreightData> data)
{
return;
}
}