Skip to content

Commit

Permalink
make rockets able to launch to a precise location via the rocket laun…
Browse files Browse the repository at this point in the history
…ch platform UI's (now repurposed) button

fixes #50
  • Loading branch information
screret committed Sep 2, 2024
1 parent ca2b15e commit a07e8d3
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 90 deletions.
3 changes: 2 additions & 1 deletion src/generated/resources/assets/gcyr/lang/en_ud.json
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@
"menu.gcyr.rocket.thrust": "s/ɯ %s :ʇsnɹɥ⟘",
"menu.gcyr.rocket.unbuild": "ᗡꞀI∩ᗺN∩",
"menu.gcyr.rocket_tier": "ʇǝʞɔoᴚ %d ɹǝı⟘",
"menu.gcyr.save_destination_station": "pɹɐɔʎǝʞ ᗡI uoıʇɐʇS ǝɔɐdS ǝʌɐS",
"menu.gcyr.save_destination_position": "dıɥƆ ᗡI uoıʇısod ɯɹoɟʇɐןd ɥɔunɐꞀ ǝʌɐS",
"menu.gcyr.solar_system": "ɯǝʇsʎS ɹɐןoS",
"menu.gcyr.space_station": "uoıʇɐʇS ǝɔɐdS",
"menu.gcyr.temperature": "ǝɹnʇɐɹǝdɯǝ⟘",
Expand All @@ -170,6 +170,7 @@
"message.gcyr.rocket_not_good_enough": "˙ʇǝuɐןd pǝʇɔǝןǝs ǝɥʇ oʇ ןǝʌɐɹʇ oʇ sʇuǝuodɯoɔ %s ɹǝıʇ ɥbnouǝ ǝʌɐɥ ʇ,usǝop ʇǝʞɔoɹ ǝɥ⟘",
"metaitem.gcyr.satellite_package.has_satellite": "uoıʇɐʇs ǝɔɐds ɹo ǝʇıןןǝʇɐs sɐH",
"metaitem.planet_id_circuit.id": " :ʇǝuɐןd pǝʇɔǝןǝs ʎןʇuǝɹɹnƆ",
"metaitem.planet_id_circuit.pos": ")%s'%s'%s( :uoıʇısoԀ ʇǝbɹɐ⟘",
"metaitem.planet_id_circuit.station": ")%s :ᗡI( uoıʇɐʇs ǝɔɐds ʇıqɹo-uı o⟘",
"tagprefix.mars": "ǝɹO %s sɹɐW",
"tagprefix.mercury": "ǝɹO %s ʎɹnɔɹǝW",
Expand Down
3 changes: 2 additions & 1 deletion src/generated/resources/assets/gcyr/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@
"menu.gcyr.rocket.thrust": "Thrust: %s m/s",
"menu.gcyr.rocket.unbuild": "UNBUILD",
"menu.gcyr.rocket_tier": "Tier %d Rocket",
"menu.gcyr.save_destination_station": "Save Space Station ID to keycard",
"menu.gcyr.save_destination_position": "Save Launch platform position to ID Chip",
"menu.gcyr.solar_system": "Solar System",
"menu.gcyr.space_station": "Space Station",
"menu.gcyr.temperature": "Temperature",
Expand All @@ -170,6 +170,7 @@
"message.gcyr.rocket_not_good_enough": "The rocket doesn't have enough tier %s components to travel to the selected planet.",
"metaitem.gcyr.satellite_package.has_satellite": "Has satellite or space station",
"metaitem.planet_id_circuit.id": "Currently selected planet: ",
"metaitem.planet_id_circuit.pos": "Target Position: (%s,%s,%s)",
"metaitem.planet_id_circuit.station": "To in-orbit space station (ID: %s)",
"tagprefix.mars": "Mars %s Ore",
"tagprefix.mercury": "Mercury %s Ore",
Expand Down
52 changes: 20 additions & 32 deletions src/main/java/argent_matter/gcyr/common/entity/RocketEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,14 @@
import it.unimi.dsi.fastutil.objects.Object2BooleanOpenHashMap;
import it.unimi.dsi.fastutil.objects.Object2IntMap;
import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap;
import lombok.Getter;
import net.minecraft.ChatFormatting;
import net.minecraft.MethodsReturnNonnullByDefault;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.core.GlobalPos;
import net.minecraft.core.particles.ParticleTypes;
import net.minecraft.core.registries.Registries;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.ListTag;
import net.minecraft.nbt.NbtUtils;
import net.minecraft.nbt.Tag;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.network.chat.Component;
Expand Down Expand Up @@ -125,8 +122,6 @@ public class RocketEntity extends Entity implements HasCustomInventoryScreen, IU
private final ItemStackTransfer configSlot, satelliteSlot;
private boolean destinationIsSpaceStation;
private final Object2IntMap<IRocketPart> partCounts = new Object2IntOpenHashMap<>();
@Nullable
private GlobalPos lastPos;
private boolean returnToStart;
private SatelliteType<?> satelliteToLaunch;
private int motorTiersTotal, fuelTankTiersTotal;
Expand Down Expand Up @@ -339,12 +334,18 @@ public void spawnParticles() {
for (BlockPos pos : this.thrusterPositions) {
if (this.getStartTimer() >= 200) {
for (ServerPlayer p : serverLevel.getServer().getPlayerList().getPlayers()) {
serverLevel.sendParticles(p, ParticleTypes.FLAME, true, this.getX() - vec.x + pos.getX() + 0.5, this.getY() - vec.y - 2.2 + pos.getY() + 0.5, this.getZ() - vec.z + pos.getZ() + 0.5, 20, 0.1, 0.1, 0.1, 0.001);
serverLevel.sendParticles(p, ParticleTypes.LARGE_SMOKE, true, this.getX() - vec.x + pos.getX() + 0.5, this.getY() - vec.y - 3.2 + pos.getY() + 0.5, this.getZ() - vec.z + pos.getZ() + 0.5, 10, 0.1, 0.1, 0.1, 0.04);
serverLevel.sendParticles(p, ParticleTypes.FLAME, true,
this.getX() - vec.x + pos.getX() + 0.5, this.getY() - vec.y - 2.2 + pos.getY() + 0.5, this.getZ() - vec.z + pos.getZ() + 0.5,
20, 0.1, 0.1, 0.1, 0.001);
serverLevel.sendParticles(p, ParticleTypes.LARGE_SMOKE, true,
this.getX() - vec.x + pos.getX() + 0.5, this.getY() - vec.y - 3.2 + pos.getY() + 0.5, this.getZ() - vec.z + pos.getZ() + 0.5,
10, 0.1, 0.1, 0.1, 0.04);
}
} else {
for (ServerPlayer p : serverLevel.getServer().getPlayerList().getPlayers()) {
serverLevel.sendParticles(p, ParticleTypes.CAMPFIRE_COSY_SMOKE, true, this.getX() - vec.x + pos.getX() + 0.5, this.getY() - vec.y - 0.1 + pos.getY() + 0.5, this.getZ() - vec.z + pos.getZ() + 0.5, 6, 0.1, 0.1, 0.1, 0.023);
serverLevel.sendParticles(p, ParticleTypes.CAMPFIRE_COSY_SMOKE, true,
this.getX() - vec.x + pos.getX() + 0.5, this.getY() - vec.y - 0.1 + pos.getY() + 0.5, this.getZ() - vec.z + pos.getZ() + 0.5,
6, 0.1, 0.1, 0.1, 0.023);
}
}
}
Expand Down Expand Up @@ -469,7 +470,6 @@ public void fall() {
if (delta.y > -2.0) {
delta = delta.add(0, -LivingEntity.DEFAULT_BASE_GRAVITY, 0);
}

this.setDeltaMovement(delta);

// braking
Expand Down Expand Up @@ -565,6 +565,7 @@ private boolean consumeFuel() {
return false;
}

@SuppressWarnings("DataFlowIssue")
private void goToDestination() {
if (getY() < 600) return;
this.speed = 0.0;
Expand Down Expand Up @@ -593,6 +594,8 @@ private void goToDestination() {
}
}
ResourceKey<Level> destinationDim = this.destinationIsSpaceStation ? getDestination().orbitWorld() : getDestination().level();

// Go to a random valid planet if rocket doesn't have enough fuel to get to actual destination somehow.
if (this.fuelTank.drain(computeRequiredFuelAmountForDestination(this.getDestination()) / 3, true).isEmpty()) {
List<Planet> validPlanets = new ArrayList<>();
for (Planet planet : PlanetData.planets().values()) {
Expand All @@ -604,10 +607,14 @@ private void goToDestination() {
destinationDim = destPlanet.level();
}

final ServerLevel destinationLevel = this.getServer().getLevel(destinationDim);
final ServerLevel destinationLevel;
BlockPos destinationPos = null;
if (lastPos != null && lastPos.dimension().equals(destinationDim)) {
destinationPos = lastPos.pos();
GlobalPos destinationData = PlanetIdChipBehaviour.getSavedPosition(configStack);
if (destinationData != null) {
destinationLevel = this.getServer().getLevel(destinationData.dimension());
destinationPos = destinationData.pos();
} else {
destinationLevel = this.getServer().getLevel(destinationDim);
}

List<Entity> passengers = this.getPassengers();
Expand Down Expand Up @@ -671,8 +678,8 @@ private void goToDestination() {
});
}

BlockPos stationPos = stations.getStationWorldPos(stationId);
if (destinationPos == null) {
BlockPos stationPos = stations.getStationWorldPos(stationId);
destinationPos = new BlockPos(stationPos.getX(), (int) pos.y, stationPos.getZ());
}

Expand All @@ -691,11 +698,6 @@ private void goToDestination() {
Vec3 delta = this.getDeltaMovement();
newRocket.setDeltaMovement(delta.x, -0.5, delta.z);
if (newRocket instanceof RocketEntity rocketEntity) {
if (this.destinationIsSpaceStation) {
rocketEntity.lastPos = GlobalPos.of(destinationDim, this.getOnPos());
} else {
rocketEntity.lastPos = null;
}
rocketEntity.setDestination(null);
rocketEntity.destinationIsSpaceStation = false;
rocketEntity.entityData.set(ROCKET_STARTED, false);
Expand Down Expand Up @@ -980,13 +982,6 @@ protected void readAdditionalSaveData(CompoundTag compound) {
if (compound.contains("recipeDuration")) {
this.setRecipeDuration(compound.getInt("recipeDuration"));
}

if (compound.contains("lastPos")) {
CompoundTag tag = compound.getCompound("lastPos");
BlockPos pos = NbtUtils.readBlockPos(tag.getCompound("pos"));
ResourceKey<Level> level = ResourceKey.create(Registries.DIMENSION, new ResourceLocation(tag.getString("level")));
this.lastPos = GlobalPos.of(level, pos);
}
}

@Override
Expand All @@ -1013,13 +1008,6 @@ protected void addAdditionalSaveData(CompoundTag compound) {
compound.putInt("weight", this.getWeight());
if (this.getDestination() != null) compound.putString("destination", PlanetData.getPlanetId(getDestination()).toString());
if (this.selectedFuelRecipe != null) compound.putString("selectedFuelRecipe", selectedFuelRecipe.id.toString());

if (this.lastPos != null) {
CompoundTag tag = new CompoundTag();
tag.put("pos", NbtUtils.writeBlockPos(this.lastPos.pos()));
tag.putString("level", this.lastPos.dimension().location().toString());
compound.put("lastPos", tag);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
import argent_matter.gcyr.data.loader.PlanetData;
import com.gregtechceu.gtceu.api.item.component.IAddInformation;
import com.gregtechceu.gtceu.api.item.component.IInteractionItem;
import net.minecraft.core.BlockPos;
import net.minecraft.core.GlobalPos;
import net.minecraft.core.registries.Registries;
import net.minecraft.nbt.NbtUtils;
import net.minecraft.nbt.Tag;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceKey;
Expand All @@ -27,6 +30,7 @@
public class PlanetIdChipBehaviour implements IInteractionItem, IAddInformation {
public static final String CURRENT_STATION_KEY = "gcyr:current_station";
public static final String CURRENT_PLANET_KEY = "gcyr:current_planet";
public static final String CURRENT_POS_KEY = "gcyr:current_position";

@Override
public InteractionResultHolder<ItemStack> use(Item item, Level level, Player player, InteractionHand usedHand) {
Expand Down Expand Up @@ -54,6 +58,21 @@ public static Planet getPlanetFromStack(ItemStack stack) {
return PlanetData.getPlanetFromLevelOrOrbit(ResourceKey.create(Registries.DIMENSION, new ResourceLocation(stack.getOrCreateTag().getString(CURRENT_PLANET_KEY)))).orElse(null);
}

public static void setSavedPosition(ItemStack stack, ResourceKey<Level> level, BlockPos pos) {
if (!GCYRItems.ID_CHIP.isIn(stack)) return;
stack.getOrCreateTag().putString(CURRENT_PLANET_KEY, level.location().toString());
stack.getTag().put(CURRENT_POS_KEY, NbtUtils.writeBlockPos(pos));
}

@Nullable
public static GlobalPos getSavedPosition(ItemStack stack) {
if (!GCYRItems.ID_CHIP.isIn(stack)) return null;
if (!stack.hasTag() || !stack.getOrCreateTag().contains(CURRENT_POS_KEY)) return null;
ResourceLocation currentLevel = new ResourceLocation(stack.getTag().getString(CURRENT_PLANET_KEY));
return GlobalPos.of(ResourceKey.create(Registries.DIMENSION, currentLevel),
NbtUtils.readBlockPos(stack.getTag().getCompound(CURRENT_POS_KEY)));
}

@Override
public void appendHoverText(ItemStack stack, @Nullable Level level, List<Component> tooltipComponents, TooltipFlag isAdvanced) {
Planet currentTarget = getPlanetFromStack(stack);
Expand All @@ -64,6 +83,11 @@ public void appendHoverText(ItemStack stack, @Nullable Level level, List<Compone
if (currentStationId != null) {
tooltipComponents.add(Component.translatable("metaitem.planet_id_circuit.station", currentStationId));
}

BlockPos currentTargetPos = !stack.hasTag() || !stack.getTag().contains(CURRENT_POS_KEY) ? null :
NbtUtils.readBlockPos(stack.getTag().getCompound(CURRENT_POS_KEY));
if (currentTargetPos != null) {
tooltipComponents.add(Component.translatable("metaitem.planet_id_circuit.pos",
currentTargetPos.getX(), currentTargetPos.getY(), currentTargetPos.getZ()));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package argent_matter.gcyr.common.machine.multiblock;

import argent_matter.gcyr.api.space.planet.Planet;
import argent_matter.gcyr.common.data.GCYREntities;
import argent_matter.gcyr.common.data.GCYRItems;
import argent_matter.gcyr.common.entity.RocketEntity;
import argent_matter.gcyr.common.item.KeyCardBehaviour;
import argent_matter.gcyr.common.item.PlanetIdChipBehaviour;
import com.gregtechceu.gtceu.api.gui.GuiTextures;
import com.gregtechceu.gtceu.api.machine.IMachineBlockEntity;
import com.gregtechceu.gtceu.api.machine.MetaMachine;
import com.gregtechceu.gtceu.api.machine.feature.multiblock.IDisplayUIMachine;
import com.lowdragmc.lowdraglib.gui.modular.ModularUI;
import com.lowdragmc.lowdraglib.gui.util.ClickData;
Expand All @@ -28,13 +28,15 @@
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.HoverEvent;
import net.minecraft.network.chat.MutableComponent;
import net.minecraft.resources.ResourceKey;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.AABB;
import org.jetbrains.annotations.NotNull;

import javax.annotation.Nullable;
import javax.annotation.ParametersAreNonnullByDefault;
Expand All @@ -50,26 +52,24 @@ public class RocketScannerMachine extends PlatformMultiblockMachine implements I
@Getter
private boolean rocketBuilt;
@Getter
@Persisted
private final ItemStackTransfer configSaveSlot, configLoadSlot;
private final ItemStackTransfer posSaveSlot;

public RocketScannerMachine(IMachineBlockEntity holder) {
super(holder);
this.configSaveSlot = new ItemStackTransfer(1);
this.configSaveSlot.setFilter(GCYRItems.ID_CHIP::isIn);

this.configLoadSlot = new ItemStackTransfer(1);
this.configLoadSlot.setFilter(GCYRItems.KEYCARD::isIn);
this.posSaveSlot = new ItemStackTransfer(1);
this.posSaveSlot.setFilter(GCYRItems.ID_CHIP::isIn);
}

@Override
public ModularUI createUI(Player entityPlayer) {
ModularUI modularUI = super.createUI(entityPlayer);
modularUI.widget(new SlotWidget(configSaveSlot, 0, 149, 83));
modularUI.widget(new SlotWidget(configLoadSlot, 0, 149, 105));
modularUI.widget(new ButtonWidget(129, 83, 18, 18, this::onSaveButtonClick)
modularUI.registerCloseListener(() -> {
Block.popResource(getLevel(), getPos(), posSaveSlot.getStackInSlot(0));
});
modularUI.widget(new SlotWidget(posSaveSlot, 0, 149, 105));
modularUI.widget(new ButtonWidget(129, 105, 18, 18, this::onSaveButtonClick)
.setButtonTexture(GuiTextures.BUTTON)
.setHoverTooltips(Component.translatable("menu.gcyr.save_destination_station")));
.setHoverTooltips(Component.translatable("menu.gcyr.save_destination_position")));
return modularUI;
}

Expand Down Expand Up @@ -101,14 +101,12 @@ public void handleDisplayClick(String componentData, ClickData clickData) {
private void onSaveButtonClick(ClickData data) {
if (data.isRemote) return;

ItemStack saveStack = this.configSaveSlot.getStackInSlot(0);
ItemStack saveStack = this.posSaveSlot.getStackInSlot(0);
if (GCYRItems.ID_CHIP.isIn(saveStack)) {
Planet planet = PlanetIdChipBehaviour.getPlanetFromStack(saveStack);
if (planet == null) return;

ItemStack keyCardStack = GCYRItems.KEYCARD.asStack(1);
KeyCardBehaviour.setSavedStation(keyCardStack, PlanetIdChipBehaviour.getSpaceStationId(saveStack), planet);
this.configLoadSlot.setStackInSlot(0, keyCardStack);
Direction back = this.getFrontFacing().getOpposite();
BlockPos landPos = getPos().relative(back, bDist / 2);
ResourceKey<Level> level = this.getLevel().dimension();
PlanetIdChipBehaviour.setSavedPosition(saveStack, level, landPos);
}
}

Expand All @@ -117,28 +115,16 @@ public void setRocketBuilt(boolean rocketBuilt) {
this.rocketBuilt = rocketBuilt && isFormed;
if (getLevel().isClientSide || !this.isFormed) return;

boolean isZAxis = this.getFrontFacing().getAxis() == Direction.Axis.Z;
Direction back = this.getFrontFacing().getOpposite();
Direction left = this.getFrontFacing().getCounterClockWise();
Direction right = left.getOpposite();
BlockPos current = getPos().relative(back, 1);
int startX = current.get(back.getAxis());
int endX = current.relative(back, bDist - 1).get(back.getAxis());
int startZ = current.relative(left, lDist).get(left.getAxis());
int endZ = current.relative(right, rDist).get(right.getAxis());
int startX = current.getX();
int endX = current.relative(back, bDist - 1).getX();
int startZ = current.relative(left, lDist).getZ();
int endZ = current.relative(left.getOpposite(), rDist).getZ();
int startY = current.getY();
int endY = current.offset(0, hDist, 0).getY();

if (isZAxis) {
// swap x & z coords if we're on the Z axis
int temp = startX;
startX = startZ;
startZ = temp;
temp = endX;
endX = endZ;
endZ = temp;
}

AABB bounds = new AABB(startX, startY, startZ, endX, endY, endZ);
startX = (int) bounds.minX;
endX = (int) bounds.maxX;
Expand Down
Loading

0 comments on commit a07e8d3

Please sign in to comment.