Skip to content

Commit

Permalink
fixed and / or computation with parameter actions
Browse files Browse the repository at this point in the history
  • Loading branch information
SpaceToad committed Jun 15, 2014
1 parent 7bb2351 commit e53f267
Show file tree
Hide file tree
Showing 12 changed files with 127 additions and 92 deletions.
11 changes: 11 additions & 0 deletions api/buildcraft/api/gates/ActionParameterItemStack.java
Expand Up @@ -51,4 +51,15 @@ public void writeToNBT(NBTTagCompound compound) {
public void readFromNBT(NBTTagCompound compound) {
stack = ItemStack.loadItemStackFromNBT(compound.getCompoundTag("stack"));
}

@Override
public boolean equals(Object object) {
if (object instanceof ActionParameterItemStack) {
ActionParameterItemStack param = (ActionParameterItemStack) object;

return ItemStack.areItemStackTagsEqual(stack, param.stack);
} else {
return false;
}
}
}
2 changes: 1 addition & 1 deletion api/buildcraft/api/gates/GateExpansionController.java
Expand Up @@ -37,7 +37,7 @@ public void tick(IGate gate) {
public void startResolution() {
}

public boolean resolveAction(IAction action, int count) {
public boolean resolveAction(IAction action) {
return false;
}

Expand Down
103 changes: 60 additions & 43 deletions common/buildcraft/transport/Gate.java
Expand Up @@ -9,14 +9,11 @@
package buildcraft.transport;

import java.util.BitSet;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

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 net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
Expand Down Expand Up @@ -49,18 +46,21 @@

public final class Gate implements IGate {

public static int MAX_STATEMENTS = 8;
public static int MAX_PARAMETERS = 3;

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][3];
public ITrigger[] triggers = new ITrigger[MAX_STATEMENTS];
public ITriggerParameter[][] triggerParameters = new ITriggerParameter[8][MAX_PARAMETERS];

public IAction[] actions = new IAction[8];
public IActionParameter[][] actionParameters = new IActionParameter[8][3];
public IAction[] actions = new IAction[MAX_STATEMENTS];
public IActionParameter[][] actionParameters = new IActionParameter[8][MAX_PARAMETERS];

public ActionState[] actionsState = new ActionState[8];
public ActionState[] actionsState = new ActionState[MAX_STATEMENTS];

public BitSet broadcastSignal = new BitSet(PipeWire.VALUES.length);
public BitSet prevBroadcastSignal = new BitSet(PipeWire.VALUES.length);
Expand Down Expand Up @@ -304,58 +304,67 @@ public void resolveActions() {
// 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];

actionsState [it] = ActionState.Deactivated;
int [] actionGroups = new int [] {0, 1, 2, 3, 4, 5, 6, 7};

if (trigger != null && action != null) {
actionCount.add(action);
for (int i = 0; i < MAX_PARAMETERS; ++i) {
for (int j = i - 1; j >= 0; --j) {
if (actions[i] != null && actions[j] != null
&& actions[i].getUniqueTag().equals(actions[j].getUniqueTag())) {

boolean active = isNearbyTriggerActive(trigger, parameter);
boolean sameParams = true;

if (!activeActions.containsKey(action)) {
activeActions.put(action, active);
} else if (logic == GateLogic.AND) {
activeActions.put(action, activeActions.get(action) && active);
} else {
activeActions.put(action, activeActions.get(action) || active);
}
for (int p = 0; p < MAX_PARAMETERS; ++p) {
if ((actionParameters[i][p] != null && actionParameters[j][p] == null)
|| (actionParameters[i][p] == null && actionParameters[j][p] != null)
|| (actionParameters[i][p] != null
&& actionParameters[j][p] != null
&& !actionParameters[i][p].equals(actionParameters[j][p]))) {
sameParams = false;
}
}

if (active) {
actionsState[it] = ActionState.Partial;
if (sameParams) {
actionGroups[i] = j;
}
}
}
}

for (int it = 0; it < 8; ++it) {
IAction action = actions[it];
// Computes the actions depending on the triggers
for (int it = 0; it < MAX_STATEMENTS; ++it) {
actionsState[it] = ActionState.Deactivated;

ITrigger trigger = triggers[it];
ITriggerParameter[] parameter = triggerParameters[it];

if (trigger != null) {
boolean active = isTriggerActive(trigger, parameter);

if (activeActions.containsKey(action)) {
if (activeActions.get(action)) {
actionsState[it] = ActionState.Activated;
if (actionGroups[it] == it) {
if (active) {
actionsState[it] = ActionState.Activated;
}
} else {
if (active && actionsState[actionGroups[it]] != ActionState.Activated) {
actionsState[actionGroups[it]] = ActionState.Partial;
} else if (!active && actionsState[actionGroups[it]] == ActionState.Activated) {
actionsState[actionGroups[it]] = ActionState.Partial;
}
}
}
}

// Activate the actions
for (int it = 0; it < activeActions.size(); ++it) {
if (actionsState[it] == ActionState.Activated) {
for (int it = 0; it < MAX_STATEMENTS; ++it) {
if (actions[it] != null && actionGroups[it] == it && actionsState[it] == ActionState.Activated) {
IAction action = actions[it];
action.actionActivate(this, actionParameters[it]);

// TODO: A lot of the code below should be removed in favor
// of calls to actionActivate

// Custom gate actions take precedence over defaults.
if (resolveAction(action, actionCount.count(action))) {
if (resolveAction(action)) {
continue;
}

Expand All @@ -375,6 +384,14 @@ public void resolveActions() {
}
}

LinkedList<IAction> activeActions = new LinkedList<IAction>();

for (int it = 0; it < MAX_STATEMENTS; ++it) {
if (actionGroups[it] == it && actionsState[it] == ActionState.Activated) {
activeActions.add(actions[it]);
}
}

pipe.actionsActivated(activeActions);

if (oldRedstoneOutput != redstoneOutput) {
Expand All @@ -390,16 +407,16 @@ public void resolveActions() {
}
}

public boolean resolveAction(IAction action, int count) {
public boolean resolveAction(IAction action) {
for (GateExpansionController expansion : expansions.values()) {
if (expansion.resolveAction(action, count)) {
if (expansion.resolveAction(action)) {
return true;
}
}
return false;
}

public boolean isNearbyTriggerActive(ITrigger trigger, ITriggerParameter[] parameters) {
public boolean isTriggerActive(ITrigger trigger, ITriggerParameter[] parameters) {
if (trigger == null) {
return false;
}
Expand Down
3 changes: 2 additions & 1 deletion common/buildcraft/transport/Pipe.java
Expand Up @@ -10,6 +10,7 @@

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
Expand Down Expand Up @@ -537,7 +538,7 @@ public void resetGates() {
container.scheduleRenderUpdate();
}

protected void actionsActivated(Map<IAction, Boolean> actions) {
protected void actionsActivated(Collection<IAction> actions) {
}

public TileGenericPipe getContainer() {
Expand Down
21 changes: 9 additions & 12 deletions common/buildcraft/transport/gates/GateExpansionPulsar.java
Expand Up @@ -42,7 +42,6 @@ private class GateExpansionControllerPulsar extends GateExpansionController {
private boolean isActive;
private boolean singlePulse;
private boolean hasPulsed;
private int pulseCount;
private int tick;

public GateExpansionControllerPulsar(TileEntity pipeTile) {
Expand All @@ -63,12 +62,12 @@ public void startResolution() {
}

@Override
public boolean resolveAction(IAction action, int count) {
public boolean resolveAction(IAction action) {
if (action instanceof ActionEnergyPulsar) {
enablePulse(count);
enablePulse();
return true;
} else if (action instanceof ActionSingleEnergyPulse) {
enableSinglePulse(count);
enableSinglePulse();
return true;
}
return false;
Expand Down Expand Up @@ -101,31 +100,31 @@ public void tick(IGate gate) {

if (battery != null && (!singlePulse || !hasPulsed)) {
gate.setPulsing(true);
battery.addEnergy(Math.min(1 << (pulseCount - 1), 64) * 1.01f);
// TODO: (1 - 1) is coming from pulse count, which has been
// removed. The add energy algorithm probably needs to be
// reviewed altogether.
battery.addEnergy(Math.min(1 << (1 - 1), 64) * 1.01f);
hasPulsed = true;
} else {
gate.setPulsing(true);
}
}

private void enableSinglePulse(int count) {
private void enableSinglePulse() {
singlePulse = true;
isActive = true;
pulseCount = count;
}

private void enablePulse(int count) {
private void enablePulse() {
isActive = true;
singlePulse = false;
pulseCount = count;
}

private void disablePulse() {
if (!isActive) {
hasPulsed = false;
}
isActive = false;
pulseCount = 0;
}

@Override
Expand All @@ -138,7 +137,6 @@ public void writeToNBT(NBTTagCompound nbt) {
nbt.setBoolean("singlePulse", singlePulse);
nbt.setBoolean("isActive", isActive);
nbt.setBoolean("hasPulsed", hasPulsed);
nbt.setInteger("pulseCount", pulseCount);
nbt.setInteger("tick", tick);
}

Expand All @@ -147,7 +145,6 @@ public void readFromNBT(NBTTagCompound nbt) {
isActive = nbt.getBoolean("isActive");
singlePulse = nbt.getBoolean("singlePulse");
hasPulsed = nbt.getBoolean("hasPulsed");
pulseCount = nbt.getInteger("pulseCount");
tick = nbt.getInteger("tick");
}
}
Expand Down
10 changes: 5 additions & 5 deletions common/buildcraft/transport/pipes/PipeFluidsIron.java
Expand Up @@ -8,8 +8,8 @@
*/
package buildcraft.transport.pipes;

import java.util.Collection;
import java.util.LinkedList;
import java.util.Map;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
Expand Down Expand Up @@ -101,12 +101,12 @@ public int getIconIndex(ForgeDirection direction) {
}

@Override
protected void actionsActivated(Map<IAction, Boolean> actions) {
protected void actionsActivated(Collection<IAction> actions) {
super.actionsActivated(actions);

for (Map.Entry<IAction, Boolean> action : actions.entrySet()) {
if (action.getKey() instanceof ActionPipeDirection && action.getValue() != null && action.getValue()) {
logic.setFacing(((ActionPipeDirection) action.getKey()).direction);
for (IAction action : actions) {
if (action instanceof ActionPipeDirection) {
logic.setFacing(((ActionPipeDirection) action).direction);
break;
}
}
Expand Down
16 changes: 8 additions & 8 deletions common/buildcraft/transport/pipes/PipeItemsDaizuli.java
Expand Up @@ -9,8 +9,8 @@
package buildcraft.transport.pipes;

import java.util.Arrays;
import java.util.Collection;
import java.util.LinkedList;
import java.util.Map;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
Expand Down Expand Up @@ -152,19 +152,19 @@ public void eventHandler(PipeEventItem.AdjustSpeed event) {
}

@Override
protected void actionsActivated(Map<IAction, Boolean> actions) {
protected void actionsActivated(Collection<IAction> actions) {
super.actionsActivated(actions);

for (Map.Entry<IAction, Boolean> action : actions.entrySet()) {
if (action.getKey() instanceof ActionPipeColor && action.getValue() != null && action.getValue()) {
setColor(((ActionPipeColor) action.getKey()).color);
for (IAction action : actions) {
if (action instanceof ActionPipeColor) {
setColor(((ActionPipeColor) action).color);
break;
}
}

for (Map.Entry<IAction, Boolean> action : actions.entrySet()) {
if (action.getKey() instanceof ActionPipeDirection && action.getValue() != null && action.getValue()) {
logic.setFacing(((ActionPipeDirection) action.getKey()).direction);
for (IAction action : actions) {
if (action instanceof ActionPipeDirection) {
logic.setFacing(((ActionPipeDirection) action).direction);
break;
}
}
Expand Down
11 changes: 5 additions & 6 deletions common/buildcraft/transport/pipes/PipeItemsEmzuli.java
Expand Up @@ -9,9 +9,8 @@
package buildcraft.transport.pipes;

import java.util.BitSet;
import java.util.Collection;
import java.util.LinkedList;
import java.util.Map;
import java.util.Map.Entry;

import io.netty.buffer.ByteBuf;

Expand Down Expand Up @@ -142,14 +141,14 @@ public IInventory getFilters() {
}

@Override
protected void actionsActivated(Map<IAction, Boolean> actions) {
protected void actionsActivated(Collection<IAction> actions) {
super.actionsActivated(actions);

activeFlags.clear();

for (Entry<IAction, Boolean> action : actions.entrySet()) {
if (action.getKey() instanceof ActionExtractionPreset && action.getValue() != null && action.getValue()) {
setActivePreset(((ActionExtractionPreset) action.getKey()).color);
for (IAction action : actions) {
if (action instanceof ActionExtractionPreset) {
setActivePreset(((ActionExtractionPreset) action).color);
}
}
}
Expand Down

0 comments on commit e53f267

Please sign in to comment.