-
Notifications
You must be signed in to change notification settings - Fork 497
/
Gate.java
361 lines (306 loc) · 10.8 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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
package buildcraft.transport;
import buildcraft.api.transport.PipeWire;
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.GuiIds;
import buildcraft.core.proxy.CoreProxy;
import buildcraft.core.triggers.ActionRedstoneOutput;
import buildcraft.transport.gates.GateDefinition.GateLogic;
import buildcraft.transport.gates.GateDefinition.GateMaterial;
import buildcraft.api.gates.GateExpansionController;
import buildcraft.api.gates.IGateExpansion;
import buildcraft.api.gates.ITileTrigger;
import buildcraft.transport.gates.ItemGate;
import buildcraft.transport.triggers.ActionRedstoneFaderOutput;
import buildcraft.transport.triggers.ActionSignalOutput;
import com.google.common.collect.BiMap;
import com.google.common.collect.HashBiMap;
import com.google.common.collect.HashMultiset;
import com.google.common.collect.Multiset;
import java.util.BitSet;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.tileentity.TileEntity;
import net.minecraftforge.common.util.ForgeDirection;
public final class Gate {
public final Pipe pipe;
public final GateMaterial material;
public final GateLogic logic;
public final BiMap<IGateExpansion, GateExpansionController> expansions = HashBiMap.create();
public ITrigger[] triggers = new ITrigger[8];
public ITriggerParameter[] triggerParameters = new ITriggerParameter[8];
public IAction[] actions = new IAction[8];
public BitSet broadcastSignal = new BitSet(PipeWire.VALUES.length);
public BitSet prevBroadcastSignal = new BitSet(PipeWire.VALUES.length);
public int redstoneOutput = 0;
/**
* this is the internal pulsing state of the gate. Intended to be managed
* by the server side only, the client is supposed to be referring to the
* state of the renderer, and update moveStage accordingly.
*/
public boolean isPulsing = false;
private float pulseStage = 0;
// / CONSTRUCTOR
public Gate(Pipe pipe, GateMaterial material, GateLogic logic) {
this.pipe = pipe;
this.material = material;
this.logic = logic;
}
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];
}
public void addGateExpansion(IGateExpansion expansion) {
if (!expansions.containsKey(expansion))
expansions.put(expansion, expansion.makeController(pipe.container));
}
// / SAVING & LOADING
public void writeToNBT(NBTTagCompound data) {
data.setString("material", material.name());
data.setString("logic", logic.name());
NBTTagList exList = new NBTTagList();
for (GateExpansionController con : expansions.values()) {
NBTTagCompound conNBT = new NBTTagCompound();
conNBT.setString("type", con.getType().getUniqueIdentifier());
NBTTagCompound conData = new NBTTagCompound();
con.writeToNBT(conData);
conNBT.setTag("data", conData);
exList.appendTag(conNBT);
}
data.setTag("expansions", exList);
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 (PipeWire wire : PipeWire.VALUES) {
data.setBoolean("wireState[" + wire.ordinal() + "]", broadcastSignal.get(wire.ordinal()));
}
data.setByte("redstoneOutput", (byte) redstoneOutput);
}
public void readFromNBT(NBTTagCompound data) {
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 (PipeWire wire : PipeWire.VALUES) {
broadcastSignal.set(wire.ordinal(), data.getBoolean("wireState[" + wire.ordinal() + "]"));
}
redstoneOutput = data.getByte("redstoneOutput");
}
// GUI
public void openGui(EntityPlayer player) {
if (!player.worldObj.isRemote) {
player.openGui(BuildCraftTransport.instance, GuiIds.GATES, pipe.container.getWorldObj(), pipe.container.xCoord, pipe.container.yCoord, pipe.container.zCoord);
}
}
/**
* This code is aimed at being active on the client only, and moves
* the internal position of the gate. There's no need to do that
* or to synchronize that with the server as this is only for animation.
*/
public void updatePulse () {
if (pipe.container.renderState.isGatePulsing () || pulseStage > 0.11F) {
// if it is moving, or is still in a moved state, then complete
// the current movement
pulseStage = (pulseStage + 0.01F) % 1F;
} else {
pulseStage = 0;
}
}
// / UPDATING
public void tick() {
for (GateExpansionController expansion : expansions.values()) {
expansion.tick();
}
}
public ItemStack getGateItem() {
return ItemGate.makeGateItem(this);
}
public void dropGate() {
pipe.dropItem(getGateItem());
}
public void resetGate() {
if (redstoneOutput != 0) {
redstoneOutput = 0;
pipe.updateNeighbors(true);
}
}
public boolean isGateActive() {
for (GateExpansionController expansion : expansions.values()) {
if (expansion.isActive())
return true;
}
return redstoneOutput > 0 || !broadcastSignal.isEmpty();
}
public boolean isGatePulsing() {
return isPulsing;
}
public int getRedstoneOutput() {
return redstoneOutput;
}
public void startResolution() {
for (GateExpansionController expansion : expansions.values()) {
expansion.startResolution();
}
}
public void resolveActions() {
int oldRedstoneOutput = redstoneOutput;
redstoneOutput = 0;
BitSet temp = prevBroadcastSignal;
temp.clear();
prevBroadcastSignal = broadcastSignal;
broadcastSignal = temp;
// 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 (logic == GateLogic.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) {
redstoneOutput = 15;
} else if (action instanceof ActionRedstoneFaderOutput) {
redstoneOutput = ((ActionRedstoneFaderOutput) action).level;
} else if (action instanceof ActionSignalOutput) {
broadcastSignal.set(((ActionSignalOutput) action).color.ordinal());
} 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 (oldRedstoneOutput != redstoneOutput) {
if (redstoneOutput == 0 ^ oldRedstoneOutput == 0)
pipe.container.scheduleRenderUpdate();
pipe.updateNeighbors(true);
}
if (!prevBroadcastSignal.equals(broadcastSignal)) {
pipe.container.scheduleRenderUpdate();
pipe.updateSignalState();
}
}
public boolean resolveAction(IAction action, int count) {
for (GateExpansionController expansion : expansions.values()) {
if (expansion.resolveAction(action, count))
return true;
}
return false;
}
public boolean isNearbyTriggerActive(ITrigger trigger, ITriggerParameter parameter) {
if (trigger == null)
return false;
if (trigger instanceof IPipeTrigger)
return ((IPipeTrigger) trigger).isTriggerActive(pipe, parameter);
if (trigger instanceof ITileTrigger) {
for (ForgeDirection o : ForgeDirection.VALID_DIRECTIONS) {
TileEntity tile = pipe.container.getTile(o);
if (tile != null && !(tile instanceof TileGenericPipe)) {
if (((ITileTrigger) trigger).isTriggerActive(o.getOpposite(), tile, parameter))
return true;
}
}
return false;
}
for (GateExpansionController expansion : expansions.values()) {
if (expansion.isTriggerActive(trigger, parameter))
return true;
}
return false;
}
// / TRIGGERS
public void addTrigger(List<ITrigger> list) {
for (PipeWire wire : PipeWire.VALUES) {
if (pipe.wireSet[wire.ordinal()] && material.ordinal() >= wire.ordinal()) {
list.add(BuildCraftTransport.triggerPipeWireActive[wire.ordinal()]);
list.add(BuildCraftTransport.triggerPipeWireInactive[wire.ordinal()]);
}
}
for (GateExpansionController expansion : expansions.values()) {
expansion.addTriggers(list);
}
}
// / ACTIONS
public void addActions(List<IAction> list) {
for (PipeWire wire : PipeWire.VALUES) {
if (pipe.wireSet[wire.ordinal()] && material.ordinal() >= wire.ordinal()) {
list.add(BuildCraftTransport.actionPipeWire[wire.ordinal()]);
}
}
for (GateExpansionController expansion : expansions.values()) {
expansion.addActions(list);
}
}
public void setPulsing (boolean pulsing) {
if (pulsing != isPulsing) {
isPulsing = pulsing;
pipe.container.scheduleRenderUpdate();
}
}
public float getPulseStage () {
return pulseStage;
}
}