-
Notifications
You must be signed in to change notification settings - Fork 497
/
Gate.java
297 lines (243 loc) · 8.22 KB
/
Gate.java
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
package buildcraft.transport;
import buildcraft.BuildCraftTransport;
import buildcraft.api.gates.ActionManager;
import buildcraft.api.gates.IAction;
import buildcraft.api.gates.IActionReceptor;
import buildcraft.api.gates.ITrigger;
import buildcraft.api.gates.ITriggerParameter;
import buildcraft.api.gates.TriggerParameter;
import buildcraft.core.network.PacketPayload;
import buildcraft.core.network.PacketPayloadArrays;
import buildcraft.core.triggers.ActionRedstoneOutput;
import buildcraft.transport.triggers.ActionSignalOutput;
import com.google.common.collect.HashMultiset;
import com.google.common.collect.Multiset;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.common.ForgeDirection;
public abstract class Gate {
public static enum GateKind {
None, Single, AND_2, OR_2, AND_3, OR_3, AND_4, OR_4;
public static GateKind getKindFromDamage(ItemStack itemstack) {
switch (itemstack.getItemDamage()) {
case 0:
return Single;
case 1:
return AND_2;
case 2:
return OR_2;
case 3:
return AND_3;
case 4:
return OR_3;
case 5:
return AND_4;
case 6:
return OR_4;
default:
return None;
}
}
}
public static enum GateConditional {
None, AND, OR
}
protected Pipe pipe;
public GateKind kind;
public ITrigger[] triggers = new ITrigger[8];
public ITriggerParameter[] triggerParameters = new ITriggerParameter[8];
public IAction[] actions = new IAction[8];
public boolean broadcastSignal[] = new boolean[4];
public boolean broadcastRedstone = false;
// / CONSTRUCTOR
public Gate(Pipe pipe) {
this.pipe = pipe;
}
public Gate(Pipe pipe, ItemStack stack) {
this.pipe = pipe;
kind = GateKind.getKindFromDamage(stack);
}
public void setTrigger(int position, ITrigger trigger) {
triggers[position] = trigger;
}
public ITrigger getTrigger(int position) {
return triggers[position];
}
public void setAction(int position, IAction action) {
actions[position] = action;
}
public IAction getAction(int position) {
return actions[position];
}
public void setTriggerParameter(int position, ITriggerParameter p) {
triggerParameters[position] = p;
}
public ITriggerParameter getTriggerParameter(int position) {
return triggerParameters[position];
}
// / SAVING & LOADING
public void writeToNBT(NBTTagCompound data) {
data.setInteger("Kind", kind.ordinal());
for (int i = 0; i < 8; ++i) {
if (triggers[i] != null)
data.setString("trigger[" + i + "]", triggers[i].getUniqueTag());
if (actions[i] != null)
data.setString("action[" + i + "]", actions[i].getUniqueTag());
if (triggerParameters[i] != null) {
NBTTagCompound cpt = new NBTTagCompound();
triggerParameters[i].writeToNBT(cpt);
data.setTag("triggerParameters[" + i + "]", cpt);
}
}
for (int i = 0; i < 4; ++i) {
data.setBoolean("wireState[" + i + "]", broadcastSignal[i]);
}
data.setBoolean("redstoneState", broadcastRedstone);
}
public void readFromNBT(NBTTagCompound data) {
kind = Gate.GateKind.values()[data.getInteger("Kind")];
for (int i = 0; i < 8; ++i) {
if (data.hasKey("trigger[" + i + "]"))
triggers[i] = ActionManager.triggers.get(data.getString("trigger[" + i + "]"));
if (data.hasKey("action[" + i + "]"))
actions[i] = ActionManager.actions.get(data.getString("action[" + i + "]"));
if (data.hasKey("triggerParameters[" + i + "]")) {
triggerParameters[i] = new TriggerParameter();
triggerParameters[i].readFromNBT(data.getCompoundTag("triggerParameters[" + i + "]"));
}
}
for (int i = 0; i < 4; ++i) {
broadcastSignal[i] = data.getBoolean("wireState[" + i + "]");
}
broadcastRedstone = data.getBoolean("redstoneState");
}
// / SMP
public PacketPayload toPayload() {
PacketPayloadArrays payload = new PacketPayloadArrays(1, 0, 0);
payload.intPayload[0] = kind.ordinal();
return payload;
}
// GUI
public abstract void openGui(EntityPlayer player);
// / UPDATING
public abstract void update();
public abstract void dropGate();
public void resetGate() {
if (broadcastRedstone) {
broadcastRedstone = false;
pipe.updateNeighbors(true);
}
}
// / INFORMATION
public abstract String getName();
public abstract GateConditional getConditional();
public boolean isGateActive() {
for (boolean b : broadcastSignal) {
if (b)
return true;
}
return broadcastRedstone;
}
public boolean isEmittingRedstone() {
return broadcastRedstone;
}
public abstract void startResolution();
public void resolveActions() {
boolean oldBroadcastRedstone = broadcastRedstone;
boolean[] oldBroadcastSignal = broadcastSignal;
broadcastRedstone = false;
broadcastSignal = new boolean[]{false, false, false, false};
// Tell the gate to prepare for resolving actions. (Disable pulser)
startResolution();
Map<IAction, Boolean> activeActions = new HashMap<IAction, Boolean>();
Multiset<IAction> actionCount = HashMultiset.create();
// Computes the actions depending on the triggers
for (int it = 0; it < 8; ++it) {
ITrigger trigger = triggers[it];
IAction action = actions[it];
ITriggerParameter parameter = triggerParameters[it];
if (trigger != null && action != null) {
actionCount.add(action);
if (!activeActions.containsKey(action)) {
activeActions.put(action, isNearbyTriggerActive(trigger, parameter));
} else if (getConditional() == GateConditional.AND) {
activeActions.put(action, activeActions.get(action) && isNearbyTriggerActive(trigger, parameter));
} else {
activeActions.put(action, activeActions.get(action) || isNearbyTriggerActive(trigger, parameter));
}
}
}
// Activate the actions
for (Map.Entry<IAction, Boolean> entry : activeActions.entrySet()) {
if (entry.getValue()) {
IAction action = entry.getKey();
// Custom gate actions take precedence over defaults.
if (resolveAction(action, actionCount.count(action))) {
continue;
}
if (action instanceof ActionRedstoneOutput) {
broadcastRedstone = true;
} else if (action instanceof ActionSignalOutput) {
broadcastSignal[((ActionSignalOutput) action).color.ordinal()] = true;
} else {
for (ForgeDirection side : ForgeDirection.VALID_DIRECTIONS) {
TileEntity tile = pipe.container.getTile(side);
if (tile instanceof IActionReceptor) {
IActionReceptor recept = (IActionReceptor) tile;
recept.actionActivated(action);
}
}
}
}
}
pipe.actionsActivated(activeActions);
if (oldBroadcastRedstone != broadcastRedstone) {
pipe.container.scheduleRenderUpdate();
pipe.updateNeighbors(true);
}
for (int i = 0; i < oldBroadcastSignal.length; ++i) {
if (oldBroadcastSignal[i] != broadcastSignal[i]) {
// worldObj.markBlockNeedsUpdate(container.xCoord, container.yCoord, zCoord);
pipe.container.scheduleRenderUpdate();
pipe.updateSignalState();
break;
}
}
}
public abstract boolean resolveAction(IAction action, int count);
public boolean isNearbyTriggerActive(ITrigger trigger, ITriggerParameter parameter) {
if (trigger instanceof ITriggerPipe)
return ((ITriggerPipe) trigger).isTriggerActive(pipe, parameter);
else if (trigger != null) {
for (ForgeDirection o : ForgeDirection.VALID_DIRECTIONS) {
TileEntity tile = pipe.container.getTile(o);
if (tile != null && !(tile instanceof TileGenericPipe)) {
if (trigger.isTriggerActive(o.getOpposite(), tile, parameter))
return true;
}
}
}
return false;
}
// / TRIGGERS
public abstract void addTrigger(LinkedList<ITrigger> list);
// / ACTIONS
public abstract void addActions(LinkedList<IAction> list);
// / TEXTURES
public abstract int getTextureIconIndex(boolean isSignalActive);
public abstract ResourceLocation getGuiFile();
public static boolean isGateItem(ItemStack stack) {
return stack.itemID == BuildCraftTransport.pipeGate.itemID || stack.itemID == BuildCraftTransport.pipeGateAutarchic.itemID;
}
public static Gate makeGateFromNBT(NBTTagCompound data, Pipe pipe) {
Gate gate = new GateVanilla(pipe);
gate.readFromNBT(data);
return gate;
}
}