Search in sources :

Example 1 with NpcDefinition

use of org.apollo.cache.def.NpcDefinition in project apollo by apollo-rsps.

the class NpcDefinitionDecoder method run.

@Override
public void run() {
    try {
        Archive config = fs.getArchive(0, 2);
        ByteBuffer data = config.getEntry("npc.dat").getBuffer();
        ByteBuffer idx = config.getEntry("npc.idx").getBuffer();
        int count = idx.getShort(), index = 2;
        int[] indices = new int[count];
        for (int i = 0; i < count; i++) {
            indices[i] = index;
            index += idx.getShort();
        }
        NpcDefinition[] definitions = new NpcDefinition[count];
        for (int i = 0; i < count; i++) {
            data.position(indices[i]);
            definitions[i] = decode(i, data);
        }
        NpcDefinition.init(definitions);
    } catch (IOException e) {
        throw new UncheckedIOException("Error decoding NpcDefinitions.", e);
    }
}
Also used : Archive(org.apollo.cache.archive.Archive) NpcDefinition(org.apollo.cache.def.NpcDefinition) UncheckedIOException(java.io.UncheckedIOException) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer)

Example 2 with NpcDefinition

use of org.apollo.cache.def.NpcDefinition in project apollo by apollo-rsps.

the class NpcDefinitionDecoder method decode.

/**
 * Decodes a single definition.
 *
 * @param id The npc's id.
 * @param buffer The buffer.
 * @return The {@link NpcDefinition}.
 */
private NpcDefinition decode(int id, ByteBuffer buffer) {
    NpcDefinition definition = new NpcDefinition(id);
    while (true) {
        int opcode = buffer.get() & 0xFF;
        if (opcode == 0) {
            return definition;
        } else if (opcode == 1) {
            int length = buffer.get() & 0xFF;
            int[] models = new int[length];
            for (int index = 0; index < length; index++) {
                models[index] = buffer.getShort();
            }
        } else if (opcode == 2) {
            definition.setName(BufferUtil.readString(buffer));
        } else if (opcode == 3) {
            definition.setDescription(BufferUtil.readString(buffer));
        } else if (opcode == 12) {
            definition.setSize(buffer.get());
        } else if (opcode == 13) {
            definition.setStandAnimation(buffer.getShort());
        } else if (opcode == 14) {
            definition.setWalkAnimation(buffer.getShort());
        } else if (opcode == 17) {
            definition.setWalkAnimations(buffer.getShort(), buffer.getShort(), buffer.getShort(), buffer.getShort());
        } else if (opcode >= 30 && opcode < 40) {
            String action = BufferUtil.readString(buffer);
            if (action.equals("hidden")) {
                action = null;
            }
            definition.setInteraction(opcode - 30, action);
        } else if (opcode == 40) {
            int length = buffer.get() & 0xFF;
            int[] originalColours = new int[length];
            int[] replacementColours = new int[length];
            for (int index = 0; index < length; index++) {
                originalColours[index] = buffer.getShort();
                replacementColours[index] = buffer.getShort();
            }
        } else if (opcode == 60) {
            int length = buffer.get() & 0xFF;
            int[] additionalModels = new int[length];
            for (int index = 0; index < length; index++) {
                additionalModels[index] = buffer.getShort();
            }
        } else if (opcode >= 90 && opcode <= 92) {
            // Dummy
            buffer.getShort();
        } else if (opcode == 95) {
            definition.setCombatLevel(buffer.getShort());
        } else if (opcode == 97 || opcode == 98) {
            buffer.getShort();
        } else if (opcode == 100 || opcode == 101) {
            buffer.get();
        } else if (opcode == 102 || opcode == 103) {
            buffer.getShort();
        } else if (opcode == 106) {
            wrap(buffer.getShort());
            wrap(buffer.getShort());
            int count = buffer.get() & 0xFF;
            int[] morphisms = new int[count + 1];
            Arrays.setAll(morphisms, index -> wrap(buffer.getShort()));
        }
    }
}
Also used : NpcDefinition(org.apollo.cache.def.NpcDefinition)

Example 3 with NpcDefinition

use of org.apollo.cache.def.NpcDefinition in project apollo by apollo-rsps.

the class NpcActionVerificationHandler method handle.

@Override
public void handle(Player player, NpcActionMessage message) {
    int index = message.getIndex();
    MobRepository<Npc> repository = world.getNpcRepository();
    if (index < 0 || index >= repository.capacity()) {
        message.terminate();
        return;
    }
    Npc npc = repository.get(index);
    if (npc == null || !player.getPosition().isWithinDistance(npc.getPosition(), player.getViewingDistance() + 1)) {
        // +1 in case it was decremented after the player clicked the action.
        message.terminate();
        return;
    }
    NpcDefinition definition = npc.getDefinition();
    String[] actions = definition.getInteractions();
    int option = message.getOption();
    if (option < 0 || option >= actions.length) {
        message.terminate();
        return;
    }
    if ("null".equals(actions[option])) {
        message.terminate();
        return;
    }
}
Also used : Npc(org.apollo.game.model.entity.Npc) NpcDefinition(org.apollo.cache.def.NpcDefinition)

Aggregations

NpcDefinition (org.apollo.cache.def.NpcDefinition)3 IOException (java.io.IOException)1 UncheckedIOException (java.io.UncheckedIOException)1 ByteBuffer (java.nio.ByteBuffer)1 Archive (org.apollo.cache.archive.Archive)1 Npc (org.apollo.game.model.entity.Npc)1