mirror of
https://github.com/simon987/Much-Assembly-Required.git
synced 2025-12-13 14:49:03 +00:00
wip
This commit is contained in:
@@ -24,17 +24,25 @@ public abstract class BluePrint implements InventoryHolder, JSONSerializable, Mo
|
||||
*/
|
||||
protected Class<? extends GameObject> targetObject;
|
||||
|
||||
static final int DATA_LENGTH = 1024;
|
||||
|
||||
/**
|
||||
* Set to true when all the requirements are met
|
||||
*/
|
||||
private boolean completed;
|
||||
|
||||
public BluePrint() {
|
||||
BluePrint() {
|
||||
requiredItems = new HashMap<>();
|
||||
}
|
||||
|
||||
public BluePrint(Document document) {
|
||||
requiredItems = (Map<Integer, Integer>) document.get("required_items");
|
||||
Map<String, Integer> bsonCompatibleRequiredItems = (Map<String, Integer>) document.get("required_items");
|
||||
requiredItems = new HashMap<>(bsonCompatibleRequiredItems.size());
|
||||
|
||||
for (String key : bsonCompatibleRequiredItems.keySet()) {
|
||||
requiredItems.put(Integer.valueOf(key), bsonCompatibleRequiredItems.get(key));
|
||||
}
|
||||
|
||||
completed = document.getBoolean("completed");
|
||||
try {
|
||||
targetObject = Class.forName(document.getString("target")).asSubclass(GameObject.class);
|
||||
@@ -57,7 +65,7 @@ public abstract class BluePrint implements InventoryHolder, JSONSerializable, Mo
|
||||
@Override
|
||||
public boolean placeItem(Item item) {
|
||||
|
||||
if (requiredItems.containsKey(item.getId()) && requiredItems.get(item.getId()) > 0) {
|
||||
if (canPlaceItem(item.getId())) {
|
||||
requiredItems.put(item.getId(), requiredItems.get(item.getId()) - 1);
|
||||
checkCompleted();
|
||||
return true;
|
||||
@@ -65,6 +73,11 @@ public abstract class BluePrint implements InventoryHolder, JSONSerializable, Mo
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canPlaceItem(int itemId) {
|
||||
return requiredItems.containsKey(itemId) && requiredItems.get(itemId) > 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void takeItem(int itemId) {
|
||||
|
||||
@@ -92,7 +105,7 @@ public abstract class BluePrint implements InventoryHolder, JSONSerializable, Mo
|
||||
public JSONObject jsonSerialise() {
|
||||
JSONObject json = new JSONObject();
|
||||
|
||||
json.put("target", targetObject);
|
||||
json.put("target", targetObject.getName());
|
||||
json.put("required_items", requiredItems);
|
||||
|
||||
return json;
|
||||
@@ -103,8 +116,15 @@ public abstract class BluePrint implements InventoryHolder, JSONSerializable, Mo
|
||||
Document document = new Document();
|
||||
|
||||
document.put("completed", completed);
|
||||
document.put("target", targetObject);
|
||||
document.put("required_items", requiredItems);
|
||||
document.put("target", targetObject.getName());
|
||||
document.put("type", this.getClass().getName());
|
||||
|
||||
Map<String, Integer> bsonCompatibleRequiredItems = new HashMap<>();
|
||||
for (Integer key : requiredItems.keySet()) {
|
||||
bsonCompatibleRequiredItems.put(String.valueOf(key), requiredItems.get(key));
|
||||
}
|
||||
|
||||
document.put("required_items", bsonCompatibleRequiredItems);
|
||||
|
||||
return document;
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@ import java.util.Map;
|
||||
|
||||
public class BluePrintRegistry {
|
||||
|
||||
public static final BluePrintRegistry INSTANCE = new BluePrintRegistry();
|
||||
static final BluePrintRegistry INSTANCE = new BluePrintRegistry();
|
||||
|
||||
private Map<String, Class<? extends BluePrint>> blueprints;
|
||||
private Map<String, String> digitizedBlueprints;
|
||||
@@ -19,13 +19,13 @@ public class BluePrintRegistry {
|
||||
digitizedBlueprints = new HashMap<>();
|
||||
}
|
||||
|
||||
public void registerBluePrint(Class<? extends BluePrint> clazz) {
|
||||
void registerBluePrint(Class<? extends BluePrint> clazz) {
|
||||
blueprints.put(clazz.getCanonicalName(), clazz);
|
||||
String bpData = new String(BluePrintUtil.bluePrintData(clazz));
|
||||
digitizedBlueprints.put(bpData, clazz.getCanonicalName());
|
||||
}
|
||||
|
||||
public BluePrint deserializeBlueprint(Document document) {
|
||||
BluePrint deserializeBlueprint(Document document) {
|
||||
|
||||
String type = document.getString("type");
|
||||
|
||||
@@ -48,7 +48,7 @@ public class BluePrintRegistry {
|
||||
}
|
||||
}
|
||||
|
||||
public BluePrint deserializeBluePrint(char[] chars) {
|
||||
BluePrint deserializeBluePrint(char[] chars) {
|
||||
|
||||
String bpData = new String(chars);
|
||||
|
||||
|
||||
@@ -52,7 +52,7 @@ class BluePrintUtil {
|
||||
|
||||
char[] result = new char[ARBITRARY_STRING.length * 32];
|
||||
|
||||
for (int i = ARBITRARY_STRING.length - 1; i > 0; --i) {
|
||||
for (int i = ARBITRARY_STRING.length - 1; i >= 0; --i) {
|
||||
char[] hashedBlueprint = hashMessage(ARBITRARY_STRING[i] + blueprint.getName());
|
||||
if (hashedBlueprint != null) {
|
||||
System.arraycopy(hashedBlueprint, 0, result,
|
||||
|
||||
@@ -0,0 +1,88 @@
|
||||
package net.simon987.constructionplugin;
|
||||
|
||||
import net.simon987.server.assembly.HardwareModule;
|
||||
import net.simon987.server.assembly.Register;
|
||||
import net.simon987.server.assembly.Status;
|
||||
import net.simon987.server.game.objects.ControllableUnit;
|
||||
import net.simon987.server.game.objects.GameObject;
|
||||
import org.bson.Document;
|
||||
import org.bson.types.ObjectId;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
public class ConstructionArmHardware extends HardwareModule {
|
||||
|
||||
public static final char HWID = 0x0010;
|
||||
|
||||
public static final int DEFAULT_ADDRESS = 0x0010;
|
||||
|
||||
private static final int ERR_TILE_BLOCKED = 1;
|
||||
private static final int ERR_NOT_ENOUGH_ENERGY = 2;
|
||||
private static final int ERR_MEM_READ = 3;
|
||||
private static final int ERR_INVALID_BLUEPRINT = 4;
|
||||
|
||||
private static final int PLACE_CONSTRUCTION_SITE = 1;
|
||||
private static final int PLACE_CONSTRUCTION_SITE_COST = 10;
|
||||
|
||||
|
||||
public ConstructionArmHardware(ControllableUnit unit) {
|
||||
super(null, unit);
|
||||
}
|
||||
|
||||
public ConstructionArmHardware(Document document, ControllableUnit unit) {
|
||||
super(document, unit);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleInterrupt(Status status) {
|
||||
|
||||
int a = getCpu().getRegisterSet().getRegister("A").getValue();
|
||||
int x = getCpu().getRegisterSet().getRegister("X").getValue();
|
||||
Register regB = getCpu().getRegisterSet().getRegister("B");
|
||||
|
||||
if (a == PLACE_CONSTRUCTION_SITE) {
|
||||
System.out.println("DEBUG PLACE CONTRUCTION SITE");
|
||||
|
||||
char[] bluePrintData = getCpu().getMemory().read(x, BluePrint.DATA_LENGTH);
|
||||
|
||||
if (bluePrintData == null) {
|
||||
regB.setValue(ERR_MEM_READ);
|
||||
return;
|
||||
}
|
||||
|
||||
BluePrint bluePrint = BluePrintRegistry.INSTANCE.deserializeBluePrint(bluePrintData);
|
||||
if (bluePrint == null) {
|
||||
regB.setValue(ERR_INVALID_BLUEPRINT);
|
||||
return;
|
||||
}
|
||||
|
||||
Point frontTile = unit.getFrontTile();
|
||||
|
||||
if (unit.getWorld().isTileBlocked(frontTile.x, frontTile.y)) {
|
||||
regB.setValue(ERR_TILE_BLOCKED);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!unit.spendEnergy(PLACE_CONSTRUCTION_SITE_COST)) {
|
||||
regB.setValue(ERR_NOT_ENOUGH_ENERGY);
|
||||
return;
|
||||
}
|
||||
|
||||
GameObject constructionSite = new ConstructionSite(bluePrint);
|
||||
constructionSite.setX(frontTile.x);
|
||||
constructionSite.setY(frontTile.y);
|
||||
constructionSite.setObjectId(new ObjectId());
|
||||
|
||||
unit.getWorld().addObject(constructionSite);
|
||||
regB.setValue(0);
|
||||
|
||||
System.out.println("OK");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public char getId() {
|
||||
return HWID;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package net.simon987.constructionplugin;
|
||||
|
||||
import net.simon987.constructionplugin.event.CpuInitialisationListener;
|
||||
import net.simon987.server.GameServer;
|
||||
import net.simon987.server.game.objects.GameRegistry;
|
||||
import net.simon987.server.logging.LogManager;
|
||||
@@ -17,8 +18,12 @@ public class ConstructionPlugin extends ServerPlugin {
|
||||
gameRegistry.registerGameObject(Obstacle.class);
|
||||
gameRegistry.registerGameObject(ConstructionSite.class);
|
||||
|
||||
gameRegistry.registerHardware(ConstructionArmHardware.class);
|
||||
|
||||
BluePrintRegistry.INSTANCE.registerBluePrint(ObstacleBlueprint.class);
|
||||
|
||||
listeners.add(new CpuInitialisationListener());
|
||||
|
||||
LogManager.LOGGER.info("(Construction Plugin) Initialized construction plugin");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -50,6 +50,11 @@ public class ConstructionSite extends Structure implements Updatable, InventoryH
|
||||
return bluePrint.placeItem(item);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canPlaceItem(int itemId) {
|
||||
return bluePrint.canPlaceItem(itemId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void takeItem(int itemId) {
|
||||
//NOOP
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
package net.simon987.constructionplugin.event;
|
||||
|
||||
import net.simon987.constructionplugin.ConstructionArmHardware;
|
||||
import net.simon987.server.assembly.CPU;
|
||||
import net.simon987.server.event.CpuInitialisationEvent;
|
||||
import net.simon987.server.event.GameEvent;
|
||||
import net.simon987.server.event.GameEventListener;
|
||||
import net.simon987.server.game.objects.ControllableUnit;
|
||||
|
||||
public class CpuInitialisationListener implements GameEventListener {
|
||||
@Override
|
||||
public Class getListenedEventType() {
|
||||
return CpuInitialisationEvent.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handle(GameEvent event) {
|
||||
|
||||
CPU cpu = (CPU) event.getSource();
|
||||
ControllableUnit unit = ((CpuInitialisationEvent) event).getUnit();
|
||||
|
||||
ConstructionArmHardware constructionArmHardware = new ConstructionArmHardware(unit);
|
||||
constructionArmHardware.setCpu(cpu);
|
||||
|
||||
unit.attachHardware(constructionArmHardware, ConstructionArmHardware.DEFAULT_ADDRESS);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user