Arcturus-Community/src/main/java/com/eu/habbo/habbohotel/items/interactions/wired/effects/WiredEffectMoveFurniTowards.java

364 lines
14 KiB
Java
Raw Normal View History

2018-07-06 15:30:00 +02:00
package com.eu.habbo.habbohotel.items.interactions.wired.effects;
import com.eu.habbo.Emulator;
import com.eu.habbo.habbohotel.gameclients.GameClient;
import com.eu.habbo.habbohotel.items.Item;
import com.eu.habbo.habbohotel.items.interactions.InteractionWiredEffect;
2022-11-16 16:56:06 +01:00
import com.eu.habbo.habbohotel.items.interactions.wired.WiredSettings;
2019-04-22 01:42:00 +02:00
import com.eu.habbo.habbohotel.rooms.*;
2018-07-06 15:30:00 +02:00
import com.eu.habbo.habbohotel.users.Habbo;
import com.eu.habbo.habbohotel.users.HabboItem;
import com.eu.habbo.habbohotel.wired.WiredEffectType;
import com.eu.habbo.habbohotel.wired.WiredHandler;
import com.eu.habbo.messages.ClientMessage;
import com.eu.habbo.messages.ServerMessage;
import com.eu.habbo.messages.incoming.wired.WiredSaveException;
import com.eu.habbo.messages.outgoing.rooms.items.FloorItemOnRollerComposer;
import com.eu.habbo.threading.runnables.WiredCollissionRunnable;
import gnu.trove.map.hash.THashMap;
2018-07-06 15:30:00 +02:00
import gnu.trove.set.hash.THashSet;
2019-05-26 20:14:53 +02:00
2018-07-06 15:30:00 +02:00
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
2018-07-06 15:30:00 +02:00
/**
* Wired effect: move to closest user
* Confirmed as working exactly like Habbo.com 03/05/2019 04:00
2019-05-26 20:14:53 +02:00
*
* @author Beny.
*/
2019-05-26 20:14:53 +02:00
public class WiredEffectMoveFurniTowards extends InteractionWiredEffect {
2018-07-06 15:30:00 +02:00
public static final WiredEffectType type = WiredEffectType.CHASE;
private THashSet<HabboItem> items;
private THashMap<Integer, RoomUserRotation> lastDirections;
2019-05-26 20:14:53 +02:00
public WiredEffectMoveFurniTowards(ResultSet set, Item baseItem) throws SQLException {
2018-07-06 15:30:00 +02:00
super(set, baseItem);
2019-03-18 02:22:00 +01:00
this.items = new THashSet<>();
this.lastDirections = new THashMap<>();
2018-07-06 15:30:00 +02:00
}
2019-05-26 20:14:53 +02:00
public WiredEffectMoveFurniTowards(int id, int userId, Item item, String extradata, int limitedStack, int limitedSells) {
2018-07-06 15:30:00 +02:00
super(id, userId, item, extradata, limitedStack, limitedSells);
2019-03-18 02:22:00 +01:00
this.items = new THashSet<>();
this.lastDirections = new THashMap<>();
2018-07-06 15:30:00 +02:00
}
public List<RoomUserRotation> getAvailableDirections(HabboItem item, Room room) {
List<RoomUserRotation> availableDirections = new ArrayList<>();
RoomLayout layout = room.getLayout();
RoomTile currentTile = layout.getTile(item.getX(), item.getY());
2019-05-26 20:14:53 +02:00
RoomUserRotation[] rotations = new RoomUserRotation[]{RoomUserRotation.NORTH, RoomUserRotation.EAST, RoomUserRotation.SOUTH, RoomUserRotation.WEST};
2019-05-26 20:14:53 +02:00
for (RoomUserRotation rot : rotations) {
RoomTile tile = layout.getTileInFront(currentTile, rot.getValue());
2019-05-26 20:14:53 +02:00
if (tile == null || tile.state == RoomTileState.BLOCKED || tile.state == RoomTileState.INVALID)
continue;
2019-05-26 20:14:53 +02:00
if (!layout.tileExists(tile.x, tile.y))
continue;
2019-05-26 20:14:53 +02:00
if (room.furnitureFitsAt(tile, item, item.getRotation()) == FurnitureMovementError.INVALID_MOVE)
continue;
HabboItem topItem = room.getTopItemAt(tile.x, tile.y);
2019-05-26 20:14:53 +02:00
if (topItem != null && !topItem.getBaseItem().allowStack())
continue;
2019-05-26 20:14:53 +02:00
if (tile.getAllowStack()) {
availableDirections.add(rot);
}
}
return availableDirections;
}
2018-07-06 15:30:00 +02:00
@Override
2019-05-26 20:14:53 +02:00
public boolean execute(RoomUnit roomUnit, Room room, Object[] stuff) {
2020-10-08 19:34:14 +02:00
THashSet<HabboItem> items = new THashSet<>();
2018-07-06 15:30:00 +02:00
2019-05-26 20:14:53 +02:00
for (HabboItem item : this.items) {
if (Emulator.getGameEnvironment().getRoomManager().getRoom(this.getRoomId()).getHabboItem(item.getId()) == null)
2018-07-06 15:30:00 +02:00
items.add(item);
}
2019-05-26 20:14:53 +02:00
for (HabboItem item : items) {
this.items.remove(item);
}
2019-05-26 20:14:53 +02:00
for (HabboItem item : this.items) {
2019-05-26 20:14:53 +02:00
if (item == null)
continue;
// direction the furni will move in
RoomUserRotation moveDirection = null;
RoomUserRotation lastDirection = lastDirections.get(item.getId());
2018-07-06 15:30:00 +02:00
// 1. Check if any user is within 3 tiles from the item
2020-10-08 19:34:14 +02:00
RoomUnit target = null; // closest found user
RoomLayout layout = room.getLayout();
2018-09-28 21:25:00 +02:00
boolean collided = false;
2020-05-05 12:57:18 +02:00
if (layout == null) {
break;
}
2019-05-26 20:14:53 +02:00
for (int i = 0; i < 3; i++) {
if (target != null)
break;
2019-05-26 20:14:53 +02:00
RoomUserRotation[] rotations = new RoomUserRotation[]{RoomUserRotation.NORTH, RoomUserRotation.EAST, RoomUserRotation.SOUTH, RoomUserRotation.WEST};
2019-05-26 20:14:53 +02:00
for (RoomUserRotation rot : rotations) {
RoomTile startTile = layout.getTile(item.getX(), item.getY());
2019-05-26 20:14:53 +02:00
for (int ii = 0; ii <= i; ii++) {
if (startTile == null)
2019-04-22 01:42:00 +02:00
break;
startTile = layout.getTileInFront(startTile, rot.getValue());
}
2018-09-28 21:25:00 +02:00
2019-05-26 20:14:53 +02:00
if (startTile != null && layout.tileExists(startTile.x, startTile.y)) {
Collection<RoomUnit> roomUnitsAtTile = room.getRoomUnitsAt(startTile);
2020-10-08 19:34:14 +02:00
if (roomUnitsAtTile.size() > 0) {
target = roomUnitsAtTile.iterator().next();
if (i == 0) { // i = 0 means right next to it
collided = true;
2020-10-08 19:34:14 +02:00
Emulator.getThreading().run(new WiredCollissionRunnable(target, room, new Object[]{item}));
2019-04-22 01:42:00 +02:00
}
break;
2019-04-22 01:42:00 +02:00
}
2018-09-28 21:25:00 +02:00
}
2018-07-06 15:30:00 +02:00
}
2018-09-28 21:25:00 +02:00
}
2018-07-06 15:30:00 +02:00
2019-05-26 20:14:53 +02:00
if (collided)
continue;
2018-07-06 15:30:00 +02:00
2019-05-26 20:14:53 +02:00
if (target != null) {
2020-10-08 19:34:14 +02:00
if (target.getX() == item.getX()) {
if (item.getY() < target.getY())
moveDirection = RoomUserRotation.SOUTH;
else
moveDirection = RoomUserRotation.NORTH;
2020-10-08 19:34:14 +02:00
} else if (target.getY() == item.getY()) {
if (item.getX() < target.getX())
moveDirection = RoomUserRotation.EAST;
else
moveDirection = RoomUserRotation.WEST;
2020-10-08 19:34:14 +02:00
} else if (target.getX() - item.getX() > target.getY() - item.getY()) {
if (target.getX() - item.getX() > 0)
moveDirection = RoomUserRotation.EAST;
else
moveDirection = RoomUserRotation.WEST;
2019-05-26 20:14:53 +02:00
} else {
2020-10-08 19:34:14 +02:00
if (target.getY() - item.getY() > 0)
moveDirection = RoomUserRotation.SOUTH;
else
moveDirection = RoomUserRotation.NORTH;
2018-09-28 21:25:00 +02:00
}
}
2018-07-06 15:30:00 +02:00
// 2. Get a random direction
/*
getAvailableDirections:
0 available - don't move
1 available - move in that direction
2 available - if lastdirection = null move in random possible direction
else if direction[0] = lastdirection opposite, move in direction[1]
else move in direction[0]
3+ available - move in random direction, but never the opposite
*/
List<RoomUserRotation> availableDirections = this.getAvailableDirections(item, room);
2019-05-26 20:14:53 +02:00
if (moveDirection != null && !availableDirections.contains(moveDirection))
moveDirection = null;
2019-05-26 20:14:53 +02:00
if (moveDirection == null) {
if (availableDirections.size() == 0) {
continue;
} else if (availableDirections.size() == 1) {
moveDirection = availableDirections.iterator().next();
} else if (availableDirections.size() == 2) {
if (lastDirection == null) {
moveDirection = availableDirections.get(Emulator.getRandom().nextInt(availableDirections.size()));
} else {
RoomUserRotation oppositeLast = lastDirection.getOpposite();
if (availableDirections.get(0) == oppositeLast) {
moveDirection = availableDirections.get(1);
} else {
moveDirection = availableDirections.get(0);
}
}
} else {
if (lastDirection != null) {
RoomUserRotation opposite = lastDirection.getOpposite();
availableDirections.remove(opposite);
}
moveDirection = availableDirections.get(Emulator.getRandom().nextInt(availableDirections.size()));
2018-07-06 15:30:00 +02:00
}
}
RoomTile newTile = room.getLayout().getTileInFront(room.getLayout().getTile(item.getX(), item.getY()), moveDirection.getValue());
RoomTile oldLocation = room.getLayout().getTile(item.getX(), item.getY());
double oldZ = item.getZ();
if(newTile != null) {
lastDirections.put(item.getId(), moveDirection);
if(newTile.state != RoomTileState.INVALID && newTile != oldLocation && room.furnitureFitsAt(newTile, item, item.getRotation(), true) == FurnitureMovementError.NONE) {
if (room.moveFurniTo(item, newTile, item.getRotation(), null, false) == FurnitureMovementError.NONE) {
room.sendComposer(new FloorItemOnRollerComposer(item, null, oldLocation, oldZ, newTile, item.getZ(), 0, room).compose());
}
}
}
2018-07-06 15:30:00 +02:00
}
return true;
}
@Override
2019-05-26 20:14:53 +02:00
public String getWiredData() {
return WiredHandler.getGsonBuilder().create().toJson(new JsonData(
this.getDelay(),
this.items.stream().map(HabboItem::getId).collect(Collectors.toList())
));
2018-07-06 15:30:00 +02:00
}
@Override
2019-05-26 20:14:53 +02:00
public void loadWiredData(ResultSet set, Room room) throws SQLException {
2018-09-28 21:25:00 +02:00
this.items = new THashSet<>();
String wiredData = set.getString("wired_data");
2018-07-06 15:30:00 +02:00
if (wiredData.startsWith("{")) {
JsonData data = WiredHandler.getGsonBuilder().create().fromJson(wiredData, JsonData.class);
this.setDelay(data.delay);
2018-07-06 15:30:00 +02:00
for (Integer id: data.itemIds) {
HabboItem item = room.getHabboItem(id);
if (item != null) {
this.items.add(item);
}
}
} else {
String[] wiredDataOld = wiredData.split("\t");
if (wiredDataOld.length >= 1) {
this.setDelay(Integer.parseInt(wiredDataOld[0]));
}
if (wiredDataOld.length == 2) {
if (wiredDataOld[1].contains(";")) {
for (String s : wiredDataOld[1].split(";")) {
HabboItem item = room.getHabboItem(Integer.parseInt(s));
if (item != null)
this.items.add(item);
}
2018-07-06 15:30:00 +02:00
}
}
}
}
@Override
2019-05-26 20:14:53 +02:00
public void onPickUp() {
2018-07-06 15:30:00 +02:00
this.items.clear();
this.setDelay(0);
}
@Override
2019-05-26 20:14:53 +02:00
public WiredEffectType getType() {
2018-07-06 15:30:00 +02:00
return type;
}
@Override
2019-05-26 20:14:53 +02:00
public void serializeWiredData(ServerMessage message, Room room) {
2018-09-28 21:25:00 +02:00
THashSet<HabboItem> items = new THashSet<>();
2018-07-06 15:30:00 +02:00
2019-05-26 20:14:53 +02:00
for (HabboItem item : this.items) {
if (item.getRoomId() != this.getRoomId() || Emulator.getGameEnvironment().getRoomManager().getRoom(this.getRoomId()).getHabboItem(item.getId()) == null)
2018-07-06 15:30:00 +02:00
items.add(item);
}
2019-05-26 20:14:53 +02:00
for (HabboItem item : items) {
2018-07-06 15:30:00 +02:00
this.items.remove(item);
}
message.appendBoolean(false);
message.appendInt(WiredHandler.MAXIMUM_FURNI_SELECTION);
message.appendInt(this.items.size());
2019-05-26 20:14:53 +02:00
for (HabboItem item : this.items)
2018-07-06 15:30:00 +02:00
message.appendInt(item.getId());
message.appendInt(this.getBaseItem().getSpriteId());
message.appendInt(this.getId());
message.appendString("");
message.appendInt(0);
message.appendInt(0);
message.appendInt(this.getType().code);
message.appendInt(this.getDelay());
message.appendInt(0);
}
@Override
2022-11-16 16:56:06 +01:00
public boolean saveData(WiredSettings settings, GameClient gameClient) throws WiredSaveException {
int itemsCount = settings.getFurniIds().length;
2020-09-15 19:38:31 +02:00
if(itemsCount > Emulator.getConfig().getInt("hotel.wired.furni.selection.count")) {
throw new WiredSaveException("Too many furni selected");
}
List<HabboItem> newItems = new ArrayList<>();
for (int i = 0; i < itemsCount; i++) {
2022-11-16 16:56:06 +01:00
int itemId = settings.getFurniIds()[i];
HabboItem it = Emulator.getGameEnvironment().getRoomManager().getRoom(this.getRoomId()).getHabboItem(itemId);
2018-07-06 15:30:00 +02:00
if(it == null)
throw new WiredSaveException(String.format("Item %s not found", itemId));
newItems.add(it);
2018-07-06 15:30:00 +02:00
}
2022-11-16 16:56:06 +01:00
int delay = settings.getDelay();
if(delay > Emulator.getConfig().getInt("hotel.wired.max_delay", 20))
throw new WiredSaveException("Delay too long");
this.items.clear();
this.items.addAll(newItems);
this.setDelay(delay);
2018-07-06 15:30:00 +02:00
return true;
}
2018-09-12 18:45:00 +02:00
@Override
2019-05-26 20:14:53 +02:00
protected long requiredCooldown() {
2018-09-12 18:45:00 +02:00
return 495;
}
static class JsonData {
int delay;
List<Integer> itemIds;
public JsonData(int delay, List<Integer> itemIds) {
this.delay = delay;
this.itemIds = itemIds;
}
}
}