Permalink
Browse files

Added Counter IC.

  • Loading branch information...
me4502 committed Jun 28, 2017
1 parent 0b857a4 commit e8a95e4eea716e3325bb8c423fbe419572438ccc
@@ -19,6 +19,7 @@
import com.sk89q.craftbook.sponge.mechanics.ics.chips.logic.AndGate;
import com.sk89q.craftbook.sponge.mechanics.ics.chips.logic.Clock;
import com.sk89q.craftbook.sponge.mechanics.ics.chips.logic.CombinationLock;
import com.sk89q.craftbook.sponge.mechanics.ics.chips.logic.Counter;
import com.sk89q.craftbook.sponge.mechanics.ics.chips.logic.Dispatcher;
import com.sk89q.craftbook.sponge.mechanics.ics.chips.logic.EdgeTriggerDFlipFlop;
import com.sk89q.craftbook.sponge.mechanics.ics.chips.logic.FullAdder;
@@ -99,6 +100,8 @@
registerICType(new ICType<>("MC3050", "COMBO", "Combination Lock", "Outputs high if the correct combination is inputed", new CombinationLock.Factory(), "3ISO"));
registerICType(new ICType<>("MC3102", "COUNTER", "Counter", "Increments on redstone signal, outputs high when reset.", new Counter.Factory(), "3ISO"));
registerICType(new ICType<>("MC3300", "ROM SET", "ROM Set", "Sets the memory state for a file for usage in the MemorySetter/Access IC group.", new MemorySetter.Factory(), "3ISO"));
//3I3O
@@ -0,0 +1,144 @@
/*
* CraftBook Copyright (C) 2010-2017 sk89q <http://www.sk89q.com>
* CraftBook Copyright (C) 2011-2017 me4502 <http://www.me4502.com>
* CraftBook Copyright (C) Contributors
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
* License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program. If not,
* see <http://www.gnu.org/licenses/>.
*/
package com.sk89q.craftbook.sponge.mechanics.ics.chips.logic;
import com.sk89q.craftbook.core.util.RegexUtil;
import com.sk89q.craftbook.sponge.mechanics.ics.IC;
import com.sk89q.craftbook.sponge.mechanics.ics.InvalidICException;
import com.sk89q.craftbook.sponge.mechanics.ics.factory.ICFactory;
import com.sk89q.craftbook.sponge.util.SignUtil;
import org.spongepowered.api.entity.living.player.Player;
import org.spongepowered.api.text.Text;
import org.spongepowered.api.world.Location;
import org.spongepowered.api.world.World;
import java.util.List;
public class Counter extends IC {
private int resetVal;
private boolean inf;
public Counter(ICFactory<Counter> icFactory, Location<World> block) {
super(icFactory, block);
}
@Override
public void create(Player player, List<Text> lines) throws InvalidICException {
super.create(player, lines);
String[] config = RegexUtil.COLON_PATTERN.split(SignUtil.getTextRaw(lines.get(2)));
resetVal = 0;
inf = false;
try {
resetVal = Integer.parseInt(config[0]);
inf = config[1].equals("INF");
} catch (NumberFormatException e) {
resetVal = 5;
} catch (ArrayIndexOutOfBoundsException e) {
inf = false;
} catch (Exception ignored) {
}
lines.set(2, Text.of(resetVal + (inf ? ":INF" : "")));
}
@Override
public void load() {
super.load();
String[] config = RegexUtil.COLON_PATTERN.split(getLine(2));
resetVal = 0;
inf = false;
try {
resetVal = Integer.parseInt(config[0]);
inf = config[1].equals("INF");
} catch (NumberFormatException e) {
resetVal = 5;
} catch (ArrayIndexOutOfBoundsException e) {
inf = false;
} catch (Exception ignored) {
}
setLine(2, Text.of(resetVal + (inf ? ":INF" : "")));
}
@Override
public void trigger() {
int curVal;
try {
curVal = Integer.parseInt(getLine(3));
} catch (Exception e) {
curVal = 0;
}
int oldVal = curVal;
// If clock input triggered
if (getPinSet().getInput(0, this)) {
if (curVal == resetVal) { // If we've gotten to 0, reset if infinite mode
if (inf) {
curVal = 0;
}
} else {
curVal++;
}
// Set output to high if we're at 0, otherwise low
getPinSet().setOutput(0, curVal == resetVal, this);
} else if (getPinSet().getInput(1, this)) {
curVal = 0;
getPinSet().setOutput(0, false, this);
}
// Update counter value stored on sign if it's changed
if (curVal != oldVal) {
setLine(3, Text.of(String.valueOf(curVal)));
}
}
public static class Factory implements ICFactory<Counter> {
@Override
public Counter createInstance(Location<World> location) {
return new Counter(this, location);
}
@Override
public String[] getLineHelp() {
return new String[] {
"Reset Value{:INF}",
"Current Value"
};
}
@Override
public String[][] getPinHelp() {
return new String[][] {
new String[] {
"Increment Counter",
"Reset Counter",
"Nothing"
},
new String[] {
"High on Counter Complete"
}
};
}
}
}
@@ -1,3 +1,19 @@
/*
* CraftBook Copyright (C) 2010-2017 sk89q <http://www.sk89q.com>
* CraftBook Copyright (C) 2011-2017 me4502 <http://www.me4502.com>
* CraftBook Copyright (C) Contributors
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
* License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program. If not,
* see <http://www.gnu.org/licenses/>.
*/
package com.sk89q.craftbook.sponge.mechanics.ics.chips.logic;
import com.sk89q.craftbook.sponge.mechanics.ics.IC;
@@ -1,3 +1,19 @@
/*
* CraftBook Copyright (C) 2010-2017 sk89q <http://www.sk89q.com>
* CraftBook Copyright (C) 2011-2017 me4502 <http://www.me4502.com>
* CraftBook Copyright (C) Contributors
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
* License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program. If not,
* see <http://www.gnu.org/licenses/>.
*/
package com.sk89q.craftbook.sponge.mechanics.ics.chips.logic;
import com.sk89q.craftbook.sponge.mechanics.ics.IC;

0 comments on commit e8a95e4

Please sign in to comment.