Merge branch 'refactor-move-rotate-wired' into 'dev'

Small refactoring of Wired Effect: Move Rotate

See merge request morningstar/Arcturus-Community!96
This commit is contained in:
Harmonic 2020-02-05 12:39:16 -05:00
commit aab96e59dd

View File

@ -39,46 +39,23 @@ public class WiredEffectMoveRotateFurni extends InteractionWiredEffect {
@Override @Override
public boolean execute(RoomUnit roomUnit, Room room, Object[] stuff) { public boolean execute(RoomUnit roomUnit, Room room, Object[] stuff) {
THashSet<HabboItem> items = new THashSet<>(this.items.size()); // remove items that are no longer in the room
this.items.removeIf( item -> Emulator.getGameEnvironment().getRoomManager().getRoom(this.getRoomId()).getHabboItem(item.getId()) == null);
THashSet<RoomTile> tilesToUpdate = new THashSet<>(Math.min(this.items.size(), 10)); THashSet<RoomTile> tilesToUpdate = new THashSet<>(Math.min(this.items.size(), 10));
for (HabboItem item : this.items) {
if (Emulator.getGameEnvironment().getRoomManager().getRoom(this.getRoomId()).getHabboItem(item.getId()) == null)
items.add(item);
}
for (HabboItem item : items) {
this.items.remove(item);
}
for (HabboItem item : this.items) { for (HabboItem item : this.items) {
//Handle rotation //Handle rotation
int rotationToAdd = 0;
if (this.rotation > 0) { if (this.rotation > 0) {
tilesToUpdate.addAll(room.getLayout().getTilesAt(room.getLayout().getTile(item.getX(), item.getY()), item.getBaseItem().getWidth(), item.getBaseItem().getLength(), item.getRotation())); tilesToUpdate.addAll(room.getLayout().getTilesAt(room.getLayout().getTile(item.getX(), item.getY()), item.getBaseItem().getWidth(), item.getBaseItem().getLength(), item.getRotation()));
if (this.rotation == 1) { int newRotation = this.getNewRotation(item);
rotationToAdd = 2;
} else if (this.rotation == 2) {
rotationToAdd = 6;
}
//Random rotation
else if (this.rotation == 3) {
if (Emulator.getRandom().nextInt(2) == 1) {
rotationToAdd = 2;
} else {
rotationToAdd = 6;
}
}
}
int newRotation = ((item.getRotation() + rotationToAdd) % 8) % (item.getBaseItem().getWidth() > 1 || item.getBaseItem().getLength() > 1 ? 4 : 8);
//Verify if rotation result in a valid position //Verify if rotation result in a valid position
FurnitureMovementError rotateError = room.furnitureFitsAt(room.getLayout().getTile(item.getX(), item.getY()), item, newRotation); FurnitureMovementError rotateError = room.furnitureFitsAt(room.getLayout().getTile(item.getX(), item.getY()), item, newRotation);
if (rotationToAdd > 0 && (rotateError.equals(FurnitureMovementError.TILE_HAS_HABBOS) || rotateError.equals(FurnitureMovementError.TILE_HAS_PETS) || if (item.getRotation() != newRotation && (rotateError.equals(FurnitureMovementError.TILE_HAS_HABBOS) || rotateError.equals(FurnitureMovementError.TILE_HAS_PETS) ||
rotateError.equals(FurnitureMovementError.TILE_HAS_BOTS) || rotateError.equals(FurnitureMovementError.NONE))) rotateError.equals(FurnitureMovementError.TILE_HAS_BOTS) || rotateError.equals(FurnitureMovementError.NONE)))
{ {
item.setRotation(newRotation); item.setRotation(newRotation);
if(this.direction == 0) { if(this.direction == 0) {
tilesToUpdate.addAll(room.getLayout().getTilesAt(room.getLayout().getTile(item.getX(), item.getY()), item.getBaseItem().getWidth(), item.getBaseItem().getLength(), item.getRotation())); tilesToUpdate.addAll(room.getLayout().getTilesAt(room.getLayout().getTile(item.getX(), item.getY()), item.getBaseItem().getWidth(), item.getBaseItem().getLength(), item.getRotation()));
room.sendComposer(new FloorItemUpdateComposer(item).compose()); room.sendComposer(new FloorItemUpdateComposer(item).compose());
@ -88,53 +65,12 @@ public class WiredEffectMoveRotateFurni extends InteractionWiredEffect {
} }
} }
} }
}
//handle movement
if (this.direction > 0) { if (this.direction > 0) {
int nextDirectionOffset = 0; RoomUserRotation moveDirection = this.getMovementDirection();
RoomUserRotation startMoveDirection = RoomUserRotation.NORTH;
RoomUserRotation moveDirection = null;
if (this.direction == 1) {
startMoveDirection = RoomUserRotation.values()[Emulator.getRandom().nextInt(RoomUserRotation.values().length / 2) * 2];
nextDirectionOffset = 2;
} else if (this.direction == 2) {
if (Emulator.getRandom().nextInt(2) == 1) {
startMoveDirection = RoomUserRotation.EAST;
nextDirectionOffset = 4;
} else {
startMoveDirection = RoomUserRotation.WEST;
nextDirectionOffset = 4;
}
} else if (this.direction == 3) {
if (Emulator.getRandom().nextInt(2) == 1) {
startMoveDirection = RoomUserRotation.NORTH;
nextDirectionOffset = 4;
} else {
startMoveDirection = RoomUserRotation.SOUTH;
nextDirectionOffset = 4;
}
} else if (this.direction == 4) {
startMoveDirection = RoomUserRotation.SOUTH;
nextDirectionOffset = 8;
} else if (this.direction == 5) {
startMoveDirection = RoomUserRotation.EAST;
nextDirectionOffset = 8;
} else if (this.direction == 6) {
startMoveDirection = RoomUserRotation.NORTH;
nextDirectionOffset = 8;
} else if (this.direction == 7) {
startMoveDirection = RoomUserRotation.WEST;
nextDirectionOffset = 8;
}
boolean validMove = false; boolean validMove = false;
int count = 0;
int maxCount = 8 / nextDirectionOffset;
while (moveDirection != startMoveDirection && !validMove && count < maxCount) {
count++;
if (moveDirection == null) {
moveDirection = startMoveDirection;
}
RoomLayout layout = room.getLayout(); RoomLayout layout = room.getLayout();
if (layout == null) return false; if (layout == null) return false;
@ -165,8 +101,7 @@ public class WiredEffectMoveRotateFurni extends InteractionWiredEffect {
for (short y = (short) rectangle.y; y < rectangle.y + rectangle.getHeight(); y++) { for (short y = (short) rectangle.y; y < rectangle.y + rectangle.getHeight(); y++) {
RoomTile tile = layout.getTile(x, y); RoomTile tile = layout.getTile(x, y);
if (tile == null || tile.state == RoomTileState.INVALID || !tile.getAllowStack()) {
if (tile == null || tile.state == RoomTileState.INVALID || tile.state == RoomTileState.BLOCKED || !tile.getAllowStack()) {
validMove = false; validMove = false;
break; break;
} }
@ -177,7 +112,7 @@ public class WiredEffectMoveRotateFurni extends InteractionWiredEffect {
break; break;
} }
java.util.List tileItems = new ArrayList<Pair<RoomTile, THashSet<HabboItem>>>(rectangle.width * rectangle.height); ArrayList<Pair<RoomTile, THashSet<HabboItem>>> tileItems = new ArrayList<>(rectangle.width * rectangle.height);
tileItems.add(Pair.create(tile, itemsAtNewTile)); tileItems.add(Pair.create(tile, itemsAtNewTile));
if (!item.canStackAt(room, tileItems)) { if (!item.canStackAt(room, tileItems)) {
validMove = false; validMove = false;
@ -200,13 +135,21 @@ public class WiredEffectMoveRotateFurni extends InteractionWiredEffect {
} }
if (validMove) { if (validMove) {
if(this.rotation > 0) {
item.setX(newTile.x);
item.setY(newTile.y);
item.setZ(offset);
room.sendComposer(new FloorItemUpdateComposer(item).compose());
for (RoomTile t : tilesToUpdate) {
room.updateHabbosAt(t.x, t.y);
room.updateBotsAt(t.x, t.y);
}
}
else {
room.sendComposer(new FloorItemOnRollerComposer(item, null, newTile, offset, room).compose()); room.sendComposer(new FloorItemOnRollerComposer(item, null, newTile, offset, room).compose());
} }
} }
} }
if (!validMove) {
moveDirection = RoomUserRotation.fromValue(moveDirection.getValue() + nextDirectionOffset);
}
} }
} }
} }
@ -252,14 +195,14 @@ public class WiredEffectMoveRotateFurni extends InteractionWiredEffect {
if (data.length == 4) { if (data.length == 4) {
try { try {
this.direction = Integer.valueOf(data[0]); this.direction = Integer.parseInt(data[0]);
this.rotation = Integer.valueOf(data[1]); this.rotation = Integer.parseInt(data[1]);
this.setDelay(Integer.valueOf(data[2])); this.setDelay(Integer.parseInt(data[2]));
} catch (Exception e) { } catch (Exception e) {
} }
for (String s : data[3].split("\r")) { for (String s : data[3].split("\r")) {
HabboItem item = room.getHabboItem(Integer.valueOf(s)); HabboItem item = room.getHabboItem(Integer.parseInt(s));
if (item != null) if (item != null)
this.items.add(item); this.items.add(item);
@ -340,4 +283,60 @@ public class WiredEffectMoveRotateFurni extends InteractionWiredEffect {
protected long requiredCooldown() { protected long requiredCooldown() {
return 100; return 100;
} }
/**
* Returns a new rotation for an item based on the wired options
* @param item
* @return new rotation
*/
private int getNewRotation(HabboItem item) {
int rotationToAdd = 0;
if (this.rotation == 1) {
rotationToAdd = 2;
} else if (this.rotation == 2) {
rotationToAdd = 6;
}
//Random rotation
else if (this.rotation == 3) {
if (Emulator.getRandom().nextInt(2) == 1) {
rotationToAdd = 2;
} else {
rotationToAdd = 6;
}
}
return ((item.getRotation() + rotationToAdd) % 8) % (item.getBaseItem().getWidth() > 1 || item.getBaseItem().getLength() > 1 ? 4 : 8);
}
/**
* Returns the direction of movement based on the wired settings
* @return direction
*/
private RoomUserRotation getMovementDirection() {
RoomUserRotation movemementDirection = RoomUserRotation.NORTH;
if (this.direction == 1) {
movemementDirection = RoomUserRotation.values()[Emulator.getRandom().nextInt(RoomUserRotation.values().length / 2) * 2];
} else if (this.direction == 2) {
if (Emulator.getRandom().nextInt(2) == 1) {
movemementDirection = RoomUserRotation.EAST;
} else {
movemementDirection = RoomUserRotation.WEST;
}
} else if (this.direction == 3) {
if (Emulator.getRandom().nextInt(2) == 1) {
movemementDirection = RoomUserRotation.NORTH;
} else {
movemementDirection = RoomUserRotation.SOUTH;
}
} else if (this.direction == 4) {
movemementDirection = RoomUserRotation.SOUTH;
} else if (this.direction == 5) {
movemementDirection = RoomUserRotation.EAST;
} else if (this.direction == 6) {
movemementDirection = RoomUserRotation.NORTH;
} else if (this.direction == 7) {
movemementDirection = RoomUserRotation.WEST;
}
return movemementDirection;
}
} }