Skip to content

Commit

Permalink
Merge pull request #678 from zyxkad/vs2-feature
Browse files Browse the repository at this point in the history
make automata turtle interact vs2 ship correctly
  • Loading branch information
SirEndii authored Jan 2, 2025
2 parents 0ed9a9d + 087f783 commit 5c8434e
Show file tree
Hide file tree
Showing 14 changed files with 278 additions and 99 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build-and-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name: Build and Test
on:
workflow_dispatch:
push:
pull_request_target:
pull_request:
types:
- opened
- synchronize
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ public enum SingleOperation implements IPeripheralOperation<SingleOperationConte
WARP(1000, DistancePolicy.IGNORED, CountPolicy.MULTIPLY, 1, DistancePolicy.SQRT, CountPolicy.MULTIPLY),
ACCURE_PLACE(1000, DistancePolicy.IGNORED, CountPolicy.MULTIPLY, 1, DistancePolicy.LINEAR, CountPolicy.MULTIPLY),
PREPARE_PORTAL(3_000, 600),
ACTIVE_PORTAL(60_000, 1);
ACTIVE_PORTAL(60_000, 1),
MOUNT_SHIP(1000, 1);

private final int defaultCooldown;
private final DistancePolicy distanceCooldownPolicy;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
public class EnvironmentDetectorPeripheral extends BasePeripheral<IPeripheralOwner> {

public static final String PERIPHERAL_TYPE = "environment_detector";
private static final List<Function<IPeripheralOwner, IPeripheralPlugin>> PERIPHERAL_PLUGINS = new LinkedList<>();
private static final List<Function<IPeripheralOwner, IPeripheralPlugin>> PERIPHERAL_PLUGINS = new ArrayList<>();

protected EnvironmentDetectorPeripheral(IPeripheralOwner owner) {
super(PERIPHERAL_TYPE, owner);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,37 @@
import de.srendi.advancedperipherals.common.configuration.APConfig;
import de.srendi.advancedperipherals.lib.metaphysics.IAutomataCoreTier;
import de.srendi.advancedperipherals.lib.peripherals.AutomataCorePeripheral;
import de.srendi.advancedperipherals.lib.peripherals.IPeripheralPlugin;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;

public class WeakAutomataCorePeripheral extends AutomataCorePeripheral {
public static final String TYPE = "weak_automata";
private static final List<Function<AutomataCorePeripheral, IPeripheralPlugin>> PERIPHERAL_PLUGINS = new ArrayList<>();

static {
addIntegrationPlugin(AutomataItemSuckPlugin::new);
addIntegrationPlugin(AutomataLookPlugin::new);
addIntegrationPlugin(AutomataBlockHandPlugin::new);
addIntegrationPlugin(AutomataSoulFeedingPlugin::new);
addIntegrationPlugin(AutomataChargingPlugin::new);
}

public WeakAutomataCorePeripheral(ITurtleAccess turtle, TurtleSide side) {
this(TYPE, turtle, side, AutomataCoreTier.TIER1);
}

protected WeakAutomataCorePeripheral(String type, ITurtleAccess turtle, TurtleSide side, IAutomataCoreTier tier) {
super(type, turtle, side, tier);
addPlugin(new AutomataItemSuckPlugin(this));
addPlugin(new AutomataLookPlugin(this));
addPlugin(new AutomataBlockHandPlugin(this));
addPlugin(new AutomataSoulFeedingPlugin(this));
addPlugin(new AutomataChargingPlugin(this));
for (Function<AutomataCorePeripheral, IPeripheralPlugin> plugin : PERIPHERAL_PLUGINS) {
addPlugin(plugin.apply(this));
}
}

public static void addIntegrationPlugin(Function<AutomataCorePeripheral, IPeripheralPlugin> plugin) {
PERIPHERAL_PLUGINS.add(plugin);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,20 @@
import dan200.computercraft.api.lua.LuaFunction;
import dan200.computercraft.api.lua.MethodResult;
import dan200.computercraft.core.apis.TableHelper;
import de.srendi.advancedperipherals.common.addons.APAddons;
import de.srendi.advancedperipherals.common.addons.computercraft.owner.TurtlePeripheralOwner;
import de.srendi.advancedperipherals.common.util.LuaConverter;
import de.srendi.advancedperipherals.common.util.fakeplayer.APFakePlayer;
import de.srendi.advancedperipherals.lib.peripherals.AutomataCorePeripheral;
import net.minecraft.core.BlockPos;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.phys.EntityHitResult;
import net.minecraft.world.phys.HitResult;
import net.minecraft.world.phys.Vec3;
import net.minecraftforge.registries.ForgeRegistries;
import org.valkyrienskies.core.api.ships.Ship;

import org.jetbrains.annotations.NotNull;
import java.util.Collections;
Expand All @@ -36,16 +40,29 @@ public final MethodResult lookAtBlock(@NotNull IArguments arguments) throws LuaE
automataCore.addRotationCycle();
TurtlePeripheralOwner owner = automataCore.getPeripheralOwner();
HitResult result = owner.withPlayer(APFakePlayer.wrapActionWithRot(yaw, pitch, p -> p.findHit(true, false)));
if (result.getType() == HitResult.Type.MISS)
if (result.getType() == HitResult.Type.MISS) {
return MethodResult.of(null, "No block find");
}

BlockHitResult blockHit = (BlockHitResult) result;
BlockState state = owner.getLevel().getBlockState(blockHit.getBlockPos());
BlockPos blockPos = blockHit.getBlockPos();
BlockState state = owner.getLevel().getBlockState(blockPos);
Map<String, Object> data = new HashMap<>();
ResourceLocation blockName = ForgeRegistries.BLOCKS.getKey(state.getBlock());
if (blockName != null)
data.put("name", blockName.toString());
data.put("name", blockName == null ? null : blockName.toString());
data.put("tags", LuaConverter.tagsToList(() -> state.getBlock().builtInRegistryHolder().tags()));
Vec3 pos = blockHit.getLocation();
Vec3 origin = automataCore.getWorldPos();
data.put("x", pos.x - origin.x);
data.put("y", pos.y - origin.y);
data.put("z", pos.z - origin.z);
if (APAddons.vs2Loaded) {
Ship ship = APAddons.getVS2Ship(automataCore.getLevel(), blockPos);
if (ship != null) {
data.put("shipId", ship.getId());
data.put("shipName", ship.getSlug());
}
}
return MethodResult.of(data);
}

Expand All @@ -57,11 +74,13 @@ public final MethodResult lookAtEntity(@NotNull IArguments arguments) throws Lua

automataCore.addRotationCycle();
HitResult result = automataCore.getPeripheralOwner().withPlayer(APFakePlayer.wrapActionWithRot(yaw, pitch, p -> p.findHit(false, true)));
if (result.getType() == HitResult.Type.MISS)
if (result.getType() == HitResult.Type.MISS) {
return MethodResult.of(null, "No entity find");
}

EntityHitResult entityHit = (EntityHitResult) result;
return MethodResult.of(LuaConverter.entityToLua(entityHit.getEntity(), true));
Vec3 origin = automataCore.getWorldPos();
return MethodResult.of(LuaConverter.completeEntityWithPositionToLua(entityHit.getEntity(), origin, true));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package de.srendi.advancedperipherals.common.addons.valkyrienskies;

import dan200.computercraft.api.lua.IArguments;
import dan200.computercraft.api.lua.LuaException;
import dan200.computercraft.api.lua.LuaFunction;
import dan200.computercraft.api.lua.MethodResult;
import de.srendi.advancedperipherals.common.addons.APAddons;
import de.srendi.advancedperipherals.common.addons.computercraft.operations.SingleOperationContext;
import de.srendi.advancedperipherals.common.addons.computercraft.owner.IPeripheralOwner;
import de.srendi.advancedperipherals.common.addons.computercraft.peripheral.plugins.AutomataCorePlugin;
import de.srendi.advancedperipherals.common.util.LuaConverter;
import de.srendi.advancedperipherals.lib.peripherals.AutomataCorePeripheral;
import net.minecraft.core.BlockPos;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.level.Level;
import net.minecraft.world.phys.Vec3;
import org.joml.Vector3d;
import org.valkyrienskies.core.api.ships.ServerShip;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static de.srendi.advancedperipherals.common.addons.computercraft.operations.SingleOperation.MOUNT_SHIP;

public class AutomataVSMountPlugin extends AutomataCorePlugin {

public AutomataVSMountPlugin(AutomataCorePeripheral automataCore) {
super(automataCore);
}

@LuaFunction(mainThread = true)
public final boolean isOnShip() {
IPeripheralOwner owner = this.automataCore.getPeripheralOwner();
return APAddons.isBlockOnShip(owner.getLevel(), owner.getPos());
}

@LuaFunction(mainThread = true)
public final MethodResult getCurrentShip() {
IPeripheralOwner owner = this.automataCore.getPeripheralOwner();
ServerShip ship = (ServerShip) APAddons.getVS2Ship(owner.getLevel(), owner.getPos());
if (ship == null) {
return MethodResult.of();
}
Map<String, Object> data = LuaConverter.shipToObjectOnShip(ship, this.automataCore.getCenterPos());
return MethodResult.of(data);
}

@LuaFunction(mainThread = true)
public final MethodResult canMountToShip() {
List<ServerShip> ships = this.getMountableShips();
if (ships.size() == 0) {
return MethodResult.of();
}
List<String> shipNames = ships.stream().map(s -> s.getSlug()).collect(Collectors.toList());
return MethodResult.of(shipNames);
}

@LuaFunction(mainThread = true)
public final MethodResult mountToShip(IArguments args) throws LuaException {
String name = args.optString(0).orElse(null);
List<ServerShip> ships = this.getMountableShips();
if (ships.size() == 0) {
return MethodResult.of(false, "no mountable ship detected");
}
ServerShip targetShip = null;
if (name == null) {
targetShip = ships.get(0);
} else {
for (ServerShip s : ships) {
if (s.getSlug().equals(name)) {
targetShip = s;
break;
}
}
}
if (targetShip == null) {
return MethodResult.of(false, "target ship not found");
}
IPeripheralOwner owner = this.automataCore.getPeripheralOwner();
Level level = owner.getLevel();
Vec3 pos = this.getMountDetectPosition();
Vector3d targetPos = targetShip.getWorldToShip().transformPosition(new Vector3d(pos.x, pos.y, pos.z));
BlockPos newPosition = new BlockPos(targetPos.x, targetPos.y, targetPos.z);
return this.automataCore.withOperation(MOUNT_SHIP, new SingleOperationContext(1, 1), context -> {
boolean result = owner.move(level, newPosition);
if (!result) {
return MethodResult.of(false, "cannot mount to ship");
}
return MethodResult.of(true);
}, context -> {
if (!owner.isMovementPossible(level, newPosition)) {
return MethodResult.of(false, "move forbidden");
}
return null;
});
}

protected Vec3 getMountDetectPosition() {
IPeripheralOwner owner = this.automataCore.getPeripheralOwner();
return owner.getCenterPos();
}

protected List<ServerShip> getMountableShips() {
IPeripheralOwner owner = this.automataCore.getPeripheralOwner();
return ValkyrienSkies.getNearbyShips((ServerLevel) owner.getLevel(), this.getMountDetectPosition(), 0.5);
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package de.srendi.advancedperipherals.common.addons.valkyrienskies;

import de.srendi.advancedperipherals.common.addons.computercraft.peripheral.EnvironmentDetectorPeripheral;
import de.srendi.advancedperipherals.common.addons.computercraft.peripheral.metaphysics.WeakAutomataCorePeripheral;

public class Integration implements Runnable {

@Override
public void run() {
EnvironmentDetectorPeripheral.addIntegrationPlugin(ShipScannerPlugin::new);
WeakAutomataCorePeripheral.addIntegrationPlugin(AutomataVSMountPlugin::new);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,18 @@
import dan200.computercraft.api.lua.LuaException;
import dan200.computercraft.api.lua.LuaFunction;
import dan200.computercraft.api.lua.MethodResult;
import de.srendi.advancedperipherals.common.addons.APAddons;
import de.srendi.advancedperipherals.common.addons.computercraft.operations.SphereOperationContext;
import de.srendi.advancedperipherals.common.addons.computercraft.owner.IPeripheralOwner;
import de.srendi.advancedperipherals.common.util.LuaConverter;
import de.srendi.advancedperipherals.lib.peripherals.BasePeripheralPlugin;
import de.srendi.advancedperipherals.lib.peripherals.IPeripheralOperation;
import net.minecraft.core.BlockPos;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.phys.Vec3;
import org.joml.Vector3d;
import org.valkyrienskies.core.api.ships.ServerShip;
import org.valkyrienskies.core.api.ships.Ship;
import org.valkyrienskies.mod.common.VSGameUtilsKt;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static de.srendi.advancedperipherals.common.addons.computercraft.operations.SphereOperation.SCAN_SHIPS;

Expand All @@ -38,21 +33,9 @@ public final MethodResult scanShips(int radius) throws LuaException {
return withOperation(SCAN_SHIPS, new SphereOperationContext(radius), context -> {
return context.getRadius() > SCAN_SHIPS.getMaxCostRadius() ? MethodResult.of(null, "Radius exceeds max value") : null;
}, context -> {
ServerLevel level = (ServerLevel) this.owner.getLevel();
Vec3 pos = this.owner.getCenterPos();
Ship ship = APAddons.getVS2Ship(level, new BlockPos(pos));
if (ship != null) {
Vector3d newPos = ship.getShipToWorld().transformPosition(new Vector3d(pos.x, pos.y, pos.z));
pos = new Vec3(newPos.x, newPos.y, newPos.z);
}
List<Vector3d> shipPoses = VSGameUtilsKt.transformToNearbyShipsAndWorld(level, pos.x, pos.y, pos.z, context.getRadius());
List<Map<String, Object>> shipDatas = new ArrayList<>(shipPoses.size());
for (Vector3d p : shipPoses) {
ServerShip s = VSGameUtilsKt.getShipManagingPos(level, p.x, p.y, p.z);
if (ship == null || s.getId() != ship.getId()) {
shipDatas.add(LuaConverter.shipToObject(s, pos));
}
}
List<ServerShip> ships = ValkyrienSkies.getNearbyShips((ServerLevel) this.owner.getLevel(), pos, context.getRadius());
List<Map<String, Object>> shipDatas = ships.stream().map(s -> LuaConverter.shipToObject(s, pos)).collect(Collectors.toList());
return MethodResult.of(shipDatas);
}, null);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package de.srendi.advancedperipherals.common.addons.valkyrienskies;

import de.srendi.advancedperipherals.common.addons.APAddons;
import net.minecraft.core.BlockPos;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.phys.Vec3;
import org.joml.Vector3d;
import org.valkyrienskies.core.api.ships.ServerShip;
import org.valkyrienskies.core.api.ships.Ship;
import org.valkyrienskies.mod.common.VSGameUtilsKt;

import java.util.ArrayList;
import java.util.List;

public final class ValkyrienSkies {
private ValkyrienSkies() {}

public static List<ServerShip> getNearbyShips(ServerLevel level, Vec3 pos, double radius) {
Ship ship = APAddons.getVS2Ship(level, new BlockPos(pos));
if (ship != null) {
Vector3d newPos = ship.getShipToWorld().transformPosition(new Vector3d(pos.x, pos.y, pos.z));
pos = new Vec3(newPos.x, newPos.y, newPos.z);
}
List<Vector3d> shipPoses = VSGameUtilsKt.transformToNearbyShipsAndWorld(level, pos.x, pos.y, pos.z, radius);
List<ServerShip> ships = new ArrayList<>(shipPoses.size());
for (Vector3d p : shipPoses) {
ServerShip s = VSGameUtilsKt.getShipManagingPos(level, p.x, p.y, p.z);
if (ship == null || s.getId() != ship.getId()) {
ships.add(s);
}
}
return ships;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public static EntityHitResult getEntityHitResult(Vec3 to, Vec3 from, Level level
*/
@NotNull
public static BlockHitResult getBlockHitResult(Vec3 to, Vec3 from, Level level, boolean ignoreNoOccluded) {
return getBlockHitResult(to, from, level, ignoreNoOccluded);
return getBlockHitResult(to, from, level, ignoreNoOccluded, null);
}

/**
Expand Down
Loading

0 comments on commit 5c8434e

Please sign in to comment.