Skip to content

Commit

Permalink
Clean up some Position usage to avoid unneccessary unpacks
Browse files Browse the repository at this point in the history
  • Loading branch information
Birdulon committed Aug 22, 2022
1 parent dbf2b91 commit 36b71d0
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import emu.grasscutter.net.proto.GadgetInteractReqOuterClass.GadgetInteractReq;
import emu.grasscutter.net.proto.SceneGadgetInfoOuterClass.SceneGadgetInfo;
import emu.grasscutter.server.packet.send.PacketGadgetInteractRsp;
import emu.grasscutter.utils.Position;
import emu.grasscutter.utils.Utils;

public class GadgetGatherObject extends GadgetContent {
Expand Down Expand Up @@ -69,11 +68,10 @@ public void dropItems(Player player) {
scene,
player,
GameData.getItemDataMap().get(itemId),
new Position(
getGadget().getPosition().getX() + (float)Utils.randomRange(1,5) / 5,
getGadget().getPosition().getY() + 2f,
getGadget().getPosition().getZ() + (float)Utils.randomRange(1,5) / 5
),
getGadget().getPosition().clone()
.addY(2f)
.addX(Utils.randomFloatRange(-1f, 1f))
.addZ(Utils.randomFloatRange(-1f, 1f)),
1,
true);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import emu.grasscutter.net.proto.GatherGadgetInfoOuterClass.GatherGadgetInfo;
import emu.grasscutter.net.proto.GadgetInteractReqOuterClass.GadgetInteractReq;
import emu.grasscutter.net.proto.SceneGadgetInfoOuterClass.SceneGadgetInfo;
import emu.grasscutter.utils.Position;
import emu.grasscutter.utils.Utils;

public class GadgetGatherPoint extends GadgetContent {
Expand Down Expand Up @@ -61,14 +60,13 @@ public void dropItems(Player player) {

for (int i = 0 ; i < times ; i++) {
EntityItem item = new EntityItem(
scene,
player,
scene,
player,
GameData.getItemDataMap().get(itemId),
new Position(
getGadget().getPosition().getX() + (float)Utils.randomRange(1,5) / 5,
getGadget().getPosition().getY() + 2f,
getGadget().getPosition().getZ() + (float)Utils.randomRange(1,5) / 5
),
getGadget().getPosition().clone()
.addY(2f)
.addX(Utils.randomFloatRange(-1f, 1f))
.addZ(Utils.randomFloatRange(-1f, 1f)),
1,
true);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,7 @@ public MapMark() {
public MapMark(MapMarkPoint mapMarkPoint) {
this.sceneId = mapMarkPoint.getSceneId();
this.name = mapMarkPoint.getName();
this.position = new Position(
mapMarkPoint.getPos().getX(),
mapMarkPoint.getPos().getY(),
mapMarkPoint.getPos().getZ()
);
this.position = new Position(mapMarkPoint.getPos());
this.mapMarkPointType = mapMarkPoint.getPointType();
this.monsterId = mapMarkPoint.getMonsterId();
this.mapMarkFromType = mapMarkPoint.getFromType();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -497,11 +497,7 @@ public void run() {
}
}
previousState = currentState;
previousCoordinates = new Position(
currentCoordinates.getX(),
currentCoordinates.getY(),
currentCoordinates.getZ()
);
previousCoordinates = currentCoordinates.clone();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,17 @@ public class HandlerPersonalSceneJumpReq extends PacketHandler {
@Override
public void handle(GameSession session, byte[] header, byte[] payload) throws Exception {
PersonalSceneJumpReq req = PersonalSceneJumpReq.parseFrom(payload);
var player = session.getPlayer();

// get the scene point
String code = session.getPlayer().getSceneId() + "_" + req.getPointId();
String code = player.getSceneId() + "_" + req.getPointId();
ScenePointEntry scenePointEntry = GameData.getScenePointEntries().get(code);

if (scenePointEntry != null) {
float x = scenePointEntry.getPointData().getTranPos().getX();
float y = scenePointEntry.getPointData().getTranPos().getY();
float z = scenePointEntry.getPointData().getTranPos().getZ();
Position pos = new Position(x, y, z);
Position pos = scenePointEntry.getPointData().getTranPos().clone(); // This might not need cloning
int sceneId = scenePointEntry.getPointData().getTranSceneId();

session.getPlayer().getWorld().transferPlayerToScene(session.getPlayer(), sceneId, pos);
player.getWorld().transferPlayerToScene(player, sceneId, pos);
session.send(new PacketPersonalSceneJumpRsp(sceneId, pos));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,26 @@
import emu.grasscutter.server.event.player.PlayerTeleportEvent.TeleportType;
import emu.grasscutter.server.game.GameSession;
import emu.grasscutter.server.packet.send.PacketSceneTransToPointRsp;
import emu.grasscutter.utils.Position;

@Opcodes(PacketOpcodes.SceneTransToPointReq)
public class HandlerSceneTransToPointReq extends PacketHandler {

@Override
public void handle(GameSession session, byte[] header, byte[] payload) throws Exception {
SceneTransToPointReq req = SceneTransToPointReq.parseFrom(payload);
var player = session.getPlayer();

String code = req.getSceneId() + "_" + req.getPointId();
ScenePointEntry scenePointEntry = GameData.getScenePointEntries().get(code);

if (scenePointEntry != null) {
float x = scenePointEntry.getPointData().getTranPos().getX();
float y = scenePointEntry.getPointData().getTranPos().getY();
float z = scenePointEntry.getPointData().getTranPos().getZ();

if (session.getPlayer().getWorld().transferPlayerToScene(session.getPlayer(), req.getSceneId(), TeleportType.WAYPOINT, new Position(x, y, z))) {
session.send(new PacketSceneTransToPointRsp(session.getPlayer(), req.getPointId(), req.getSceneId()));
if (player.getWorld().transferPlayerToScene(player, req.getSceneId(), TeleportType.WAYPOINT, scenePointEntry.getPointData().getTranPos().clone())) {
session.send(new PacketSceneTransToPointRsp(player, req.getPointId(), req.getSceneId()));
return;
}
} else {
session.send(new PacketSceneTransToPointRsp());
}

session.send(new PacketSceneTransToPointRsp());
}

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package emu.grasscutter.server.packet.send;

import emu.grasscutter.game.managers.mapmark.MapMark;
import emu.grasscutter.game.player.Player;
import emu.grasscutter.net.packet.BasePacket;
import emu.grasscutter.net.packet.PacketOpcodes;
import emu.grasscutter.net.proto.*;
Expand All @@ -21,13 +20,7 @@ public PacketMarkMapRsp(Map<String, MapMark> mapMarks) {
MapMarkPointOuterClass.MapMarkPoint.Builder markPoint = MapMarkPointOuterClass.MapMarkPoint.newBuilder();
markPoint.setSceneId(mapMark.getSceneId());
markPoint.setName(mapMark.getName());

VectorOuterClass.Vector.Builder positionVector = VectorOuterClass.Vector.newBuilder();
positionVector.setX(mapMark.getPosition().getX());
positionVector.setY(mapMark.getPosition().getY());
positionVector.setZ(mapMark.getPosition().getZ());
markPoint.setPos(positionVector.build());

markPoint.setPos(mapMark.getPosition().toProto());
markPoint.setPointType(mapMark.getMapMarkPointType());
markPoint.setFromType(mapMark.getMapMarkFromType());
markPoint.setMonsterId(mapMark.getMonsterId());
Expand Down

0 comments on commit 36b71d0

Please sign in to comment.