Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

item on npc, and abusing sheep #111

Merged
merged 5 commits into from
Mar 2, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions data/config/npc-spawns.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,27 @@
x: 3253
y: 3274
radius: 1
- npcId: 3579
x: 3197
y: 3262
radius: 10
- npcId: 43
x: 3199
y: 3267
radius: 10
- npcId: 43
x: 3203
y: 3271
radius: 10
- npcId: 43
x: 3199
y: 3273
radius: 10
- npcId: 43
x: 3208
y: 3273
radius: 10
- npcId: 43
x: 3209
y: 3260
radius: 10
2 changes: 2 additions & 0 deletions src/game-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { setWidgetPlugins } from '@server/world/actor/player/action/widget-actio
import { setItemPlugins } from '@server/world/actor/player/action/item-action';
import { setWorldItemPlugins } from '@server/world/actor/player/action/world-item-action';
import { setItemOnObjectPlugins } from '@server/world/actor/player/action/item-on-object-action';
import { setItemOnNpcPlugins } from '@server/world/actor/player/action/item-on-npc-action';

export let serverConfig: ServerConfig;
export let gameCache377: EarlyFormatGameCache;
Expand All @@ -42,6 +43,7 @@ export async function injectPlugins(): Promise<void> {
setNpcPlugins(actionTypes[ActionType.NPC_ACTION]);
setObjectPlugins(actionTypes[ActionType.OBJECT_ACTION]);
setItemOnObjectPlugins(actionTypes[ActionType.ITEM_ON_OBJECT_ACTION]);
setItemOnNpcPlugins(actionTypes[ActionType.ITEM_ON_NPC_ACTION]);
setItemOnItemPlugins(actionTypes[ActionType.ITEM_ON_ITEM]);
setItemPlugins(actionTypes[ActionType.ITEM_ACTION]);
setWorldItemPlugins(actionTypes[ActionType.WORLD_ITEM_ACTION]);
Expand Down
2 changes: 2 additions & 0 deletions src/net/incoming-packet-directory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { pickupItemPacket } from '@server/net/incoming-packets/pickup-item-packe
import { itemInteractionPacket } from '@server/net/incoming-packets/item-interaction-packet';
import { itemOnObjectPacket } from '@server/net/incoming-packets/item-on-object-packet';
import { numberInputPacket } from '@server/net/incoming-packets/number-input-packet';
import { itemOnNpcPacket } from '@server/net/incoming-packets/item-on-npc-packet';

const ignore = [ 234, 160, 58 /* camera move */ ];

Expand Down Expand Up @@ -52,6 +53,7 @@ const packets: { [key: number]: incomingPacket } = {
63: npcInteractionPacket,
116: npcInteractionPacket,
24: itemOnObjectPacket,
208: itemOnNpcPacket,

30: objectInteractionPacket,
164: objectInteractionPacket,
Expand Down
2 changes: 1 addition & 1 deletion src/net/incoming-packet-sizes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export const incomingPacketSizes: number[] = [
-3, -3, -3, -3, -3, -3, 0, -3, 0, -3, //170
-3, -3, -3, 6, -3, -3, -3, -3, -3, -3, //180
-3, -3, -3, -3, -3, -3, -3, -3, -3, -3, //190
-3, -3, -3, -3, -3, -3, -3, -3, -3, -3, //200
-3, -3, -3, -3, -3, -3, -3, -3, 10, -3, //200
-3, -3, -3, -3, -3, -3, 0, -3, -3, -3, //210
-3, -3, -3, -3, -3, -3, -3, -3, 8, -3, //220
-3, 13, -3, -3, 4, -3, -1, -3, 4, -3, //230
Expand Down
60 changes: 60 additions & 0 deletions src/net/incoming-packets/item-on-npc-packet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { incomingPacket } from '../incoming-packet';
import { Player } from '../../world/actor/player/player';
import { RsBuffer } from '@server/net/rs-buffer';
import { widgets } from '@server/world/config/widget';
import { logger } from '@runejs/logger/dist/logger';
import { itemOnItemAction } from '@server/world/actor/player/action/item-on-item-action';
import { Position } from '@server/world/position';
import { gameCache, world } from '@server/game-server';
import { objectAction } from '@server/world/actor/player/action/object-action';
import { itemOnObjectAction } from '@server/world/actor/player/action/item-on-object-action';
import { World } from '@server/world/world';
import { npcAction } from '@server/world/actor/player/action/npc-action';
import { itemOnNpcAction } from '@server/world/actor/player/action/item-on-npc-action';

export const itemOnNpcPacket: incomingPacket = (player: Player, packetId: number, packetSize: number, packet: RsBuffer): void => {
const npcIndex = packet.readNegativeOffsetShortBE();
const itemId = packet.readNegativeOffsetShortBE();
const itemSlot = packet.readNegativeOffsetShortLE();
const itemWidgetId = packet.readShortBE();
const itemContainerId = packet.readShortBE();

let usedItem;
if (itemWidgetId === widgets.inventory.widgetId && itemContainerId === widgets.inventory.containerId) {
if (itemSlot < 0 || itemSlot > 27) {
return;
}

usedItem = player.inventory.items[itemSlot];
if (!usedItem) {
return;
}

if (usedItem.itemId !== itemId) {
return;
}
} else {
logger.warn(`Unhandled item on object case using widget ${itemWidgetId}:${itemContainerId}`);
}


if (npcIndex < 0 || npcIndex > World.MAX_NPCS - 1) {
return;
}

const npc = world.npcList[npcIndex];
if (!npc) {
return;
}

const position = npc.position;
const distance = Math.floor(position.distanceBetween(player.position));

// Too far away
if (distance > 16) {
return;
}

itemOnNpcAction(player, npc, position, usedItem, itemWidgetId, itemContainerId);

};
34 changes: 34 additions & 0 deletions src/plugins/npcs/lumbridge/shear-sheep-plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { World } from '@server/world/world';
import { ActionType, RunePlugin } from '@server/plugins/plugin';
import { itemIds } from '@server/world/config/item-ids';
import { itemOnObjectAction } from '@server/world/actor/player/action/item-on-object-action';
import { itemOnNpcAction } from '@server/world/actor/player/action/item-on-npc-action';


export const action: itemOnNpcAction = (details) => {
details.player.busy = true;
details.player.playAnimation(893);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add to animation-ids.ts

details.player.outgoingPackets.playSound(761, 5);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add to sound-ids.ts


setTimeout(() => {
if (Math.random() >= 0.66) {
details.player.outgoingPackets.chatboxMessage('The sheep manages to get away from you!');
// TODO: sheep says baa!, makes baa sound and goes backwards about 5 tiles
} else {
details.player.outgoingPackets.chatboxMessage('You get some wool.');
details.player.giveItem(1737);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add to item-ids.ts

// TODO: sheep says baa!, makes baa sound and gets replaces with skinned sheep

}
details.player.busy = false;
}, World.TICK_LENGTH);

};

export default new RunePlugin({
type: ActionType.ITEM_ON_NPC_ACTION,
npcsIds: [43],
itemIds: [1735, 5603],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add to item-ids.ts

walkTo: true,
action
});
8 changes: 5 additions & 3 deletions src/plugins/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { WidgetActionPlugin } from '@server/world/actor/player/action/widget-act
import { ItemActionPlugin } from '@server/world/actor/player/action/item-action';
import { WorldItemActionPlugin } from '@server/world/actor/player/action/world-item-action';
import { ItemOnObjectActionPlugin } from '@server/world/actor/player/action/item-on-object-action';
import { ItemOnNpcActionPlugin } from '@server/world/actor/player/action/item-on-npc-action';

export enum ActionType {
BUTTON = 'button',
Expand All @@ -17,6 +18,7 @@ export enum ActionType {
NPC_ACTION = 'npc_action',
OBJECT_ACTION = 'object_action',
ITEM_ON_OBJECT_ACTION = 'item_on_object_action',
ITEM_ON_NPC_ACTION = 'item_on_npc_action',
COMMAND = 'command'
}

Expand All @@ -27,12 +29,12 @@ export interface ActionPlugin {
export class RunePlugin {

public actions: (NpcActionPlugin | ObjectActionPlugin | ButtonActionPlugin | ItemOnItemActionPlugin | ItemOnObjectActionPlugin |
CommandActionPlugin | WidgetActionPlugin | ItemActionPlugin | WorldItemActionPlugin)[];
CommandActionPlugin | WidgetActionPlugin | ItemActionPlugin | WorldItemActionPlugin | ItemOnNpcActionPlugin)[];

public constructor(actions: NpcActionPlugin | ObjectActionPlugin | ButtonActionPlugin | ItemOnItemActionPlugin | ItemOnObjectActionPlugin |
CommandActionPlugin | WidgetActionPlugin | ItemActionPlugin | WorldItemActionPlugin |
CommandActionPlugin | WidgetActionPlugin | ItemActionPlugin | WorldItemActionPlugin | ItemOnNpcActionPlugin |
(NpcActionPlugin | ObjectActionPlugin | ButtonActionPlugin | ItemOnItemActionPlugin | ItemOnObjectActionPlugin |
CommandActionPlugin | WidgetActionPlugin | ItemActionPlugin | WorldItemActionPlugin)[]) {
CommandActionPlugin | WidgetActionPlugin | ItemActionPlugin | WorldItemActionPlugin | ItemOnNpcActionPlugin)[]) {
if (!Array.isArray(actions)) {
this.actions = [actions];
} else {
Expand Down
110 changes: 110 additions & 0 deletions src/world/actor/player/action/item-on-npc-action.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import { Player } from '@server/world/actor/player/player';
import { Position } from '@server/world/position';
import { walkToAction } from '@server/world/actor/player/action/action';
import { pluginFilter } from '@server/plugins/plugin-loader';
import { logger } from '@runejs/logger/dist/logger';
import { ActionPlugin } from '@server/plugins/plugin';
import { Item } from '@server/world/items/item';
import { Npc } from '@server/world/actor/npc/npc';

/**
* The definition for an item on npc action function.
*/
export type itemOnNpcAction = (details: ItemOnNpcActionDetails) => void;

/**
* Details about an npc being interacted with. and the item being used.
*/
export interface ItemOnNpcActionDetails {
player: Player;
npc: Npc;
position: Position;
item: Item;
itemWidgetId: number;
itemContainerId: number;
}

/**
* Defines an item on npc interaction plugin.
* A list of npc ids that apply to the plugin, the items that can be performed on,
* and whether or not the player must first walk to the npc.
*/
export interface ItemOnNpcActionPlugin extends ActionPlugin {
npcsIds: number | number[];
itemIds: number | number[];
walkTo: boolean;
action: itemOnNpcAction;
}

/**
* A directory of all item on npc interaction plugins.
*/
let itemOnNpcInteractions: ItemOnNpcActionPlugin[] = [];

/**
* Sets the list of item on npc interaction plugins.
* @param plugins The plugin list.
*/
export const setItemOnNpcPlugins = (plugins: ActionPlugin[]): void => {
itemOnNpcInteractions = plugins as ItemOnNpcActionPlugin[];
};

// @TODO priority and cancelling other (lower priority) actions
export const itemOnNpcAction = (player: Player, npc: Npc,
position: Position, item: Item, itemWidgetId: number, itemContainerId: number): void => {
if (player.busy) {
return;
}

// Find all item on npc action plugins that reference this landscape object
let interactionPlugins = itemOnNpcInteractions.filter(plugin => pluginFilter(plugin.npcsIds, npc.id));

// Find all item on npc action plugins that reference this item
if (interactionPlugins.length !== 0) {
interactionPlugins = interactionPlugins.filter(plugin => pluginFilter(plugin.itemIds, item.itemId));
}

if (interactionPlugins.length === 0) {
player.outgoingPackets.chatboxMessage(`Unhandled item on npc interaction: ${item.itemId} on ${npc.name} ` +
`(id-${npc.id}) @ ${position.x},${position.y},${position.level}`);
return;
}

player.actionsCancelled.next();

// Separate out walk-to actions from immediate actions
const walkToPlugins = interactionPlugins.filter(plugin => plugin.walkTo);
const immediatePlugins = interactionPlugins.filter(plugin => !plugin.walkTo);

// Make sure we walk to the npc before running any of the walk-to plugins
if (walkToPlugins.length !== 0) {
walkToAction(player, position)
.then(() => {
player.face(position);

walkToPlugins.forEach(plugin =>
plugin.action({
player,
npc,
position,
item,
itemWidgetId,
itemContainerId
}));
})
.catch(() => logger.warn(`Unable to complete walk-to action.`));
}

// Immediately run any non-walk-to plugins
if (immediatePlugins.length !== 0) {
immediatePlugins.forEach(plugin =>
plugin.action({
player,
npc,
position,
item,
itemWidgetId,
itemContainerId
}));
}
};