-
-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #678 from zyxkad/vs2-feature
make automata turtle interact vs2 ship correctly
- Loading branch information
Showing
14 changed files
with
278 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
...ava/de/srendi/advancedperipherals/common/addons/valkyrienskies/AutomataVSMountPlugin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
2 changes: 2 additions & 0 deletions
2
src/main/java/de/srendi/advancedperipherals/common/addons/valkyrienskies/Integration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
src/main/java/de/srendi/advancedperipherals/common/addons/valkyrienskies/ValkyrienSkies.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.