Skip to content

Commit 9eb45e8

Browse files
committed
Added magic attack packet handling, pipeline, actions, hooks, and finally plugins. Working full circle.
No damage or effects calculated yet. No sounds, animations, or projectiles yet.
1 parent 5cfeecc commit 9eb45e8

File tree

4 files changed

+149
-0
lines changed

4 files changed

+149
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { Player, playerOptions } from '../../world/actor/player/player';
2+
import { world } from '../../game-server';
3+
import { World } from '../../world';
4+
import { logger } from '@runejs/core';
5+
import { PacketData } from '../inbound-packets';
6+
7+
8+
const magicAttackPacket = (player: Player, packet: PacketData) => {
9+
const { buffer } = packet;
10+
const npcWorldIndex = buffer.get('short', 'u'); // unsigned short BE
11+
const widgetId = buffer.get('short', 'u', 'le'); // unsigned short LE
12+
const widgetChildId = buffer.get(); // unsigned short LE
13+
14+
const npc = world.npcList[npcWorldIndex];
15+
//console.log(`actual spell being cast is ${widgetChildId}`);
16+
player.actionPipeline.call('magic_on_npc', npc, player, widgetId, widgetChildId);
17+
};
18+
19+
export default [{
20+
opcode: 253,
21+
size: 6,
22+
handler: magicAttackPacket
23+
}];

src/game-engine/world/action/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ export type ActionType =
3636
| 'move_item'
3737
| 'spawned_item_interaction'
3838

39+
| 'magic_on_item'
40+
| 'magic_on_player'
41+
| 'magic_on_npc'
42+
3943
| 'player_init'
4044
| 'player_command'
4145
| 'player_interaction'
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import { Player } from '@engine/world/actor/player/player';
2+
import { ActionHook, getActionHooks } from '@engine/world/action/hooks';
3+
import { advancedNumberHookFilter, questHookFilter } from '@engine/world/action/hooks/hook-filters';
4+
import { ActionPipe, RunnableHooks } from '@engine/world/action/index';
5+
import { Npc } from '../actor/npc/npc';
6+
7+
8+
/**
9+
* Defines a button action hook.
10+
*/
11+
export interface Magic_on_NPCActionHook extends ActionHook<Magic_on_NPCAction, magic_on_npcActionHandler> {
12+
// The npc world id that was clicked on after choosing the spell
13+
npcworldId?: number;
14+
// The IDs of the UI widgets that the buttons are on.
15+
widgetIds?: number[];
16+
// The child ID or list of child IDs of the button(s) within the UI widget.
17+
buttonIds?: number | number[];
18+
// Whether or not this item action should cancel other running or queued actions.
19+
cancelActions?: boolean;
20+
}
21+
22+
23+
/**
24+
* The button action hook handler function to be called when the hook's conditions are met.
25+
*/
26+
export type magic_on_npcActionHandler = (buttonAction: Magic_on_NPCAction) => void | Promise<void>;
27+
28+
29+
/**
30+
* Details about a button action being performed.
31+
*/
32+
export interface Magic_on_NPCAction {
33+
// The npc world id that was clicked on after choosing the spell
34+
npc: Npc,
35+
// The player performing the action.
36+
player: Player;
37+
// The ID of the UI widget that the button is on.
38+
widgetId: number;
39+
// The child ID of the button within the UI widget.
40+
buttonId: number;
41+
}
42+
43+
44+
/**
45+
* The pipe that the game engine hands button actions off to.
46+
* @param npc
47+
* @param player
48+
* @param widgetId
49+
* @param buttonId
50+
*/
51+
const buttonActionPipe = (npc:Npc, player: Player, widgetId: number, buttonId: number): RunnableHooks<Magic_on_NPCAction> => {
52+
//console.info(`pew pew you use magic on ${npc.name}!`);
53+
54+
// Find all object action plugins that reference this location object
55+
let matchingHooks = getActionHooks<Magic_on_NPCActionHook>('magic_on_npc');
56+
57+
58+
return {
59+
hooks: matchingHooks,
60+
actionPosition: player.position,
61+
action: {
62+
npc,
63+
player,
64+
widgetId,
65+
buttonId
66+
}
67+
}
68+
69+
70+
};
71+
72+
73+
/**
74+
* Button action pipe definition.
75+
*/
76+
export default [ 'magic_on_npc', buttonActionPipe ] as ActionPipe;
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
2+
import { Player } from '@engine/world/actor/player/player';
3+
import { Position } from '@engine/world/position';
4+
import { animationIds } from '@engine/world/config/animation-ids';
5+
import { soundIds } from '@engine/world/config/sound-ids';
6+
import { gfxIds } from '@engine/world/config/gfx-ids';
7+
import { loopingEvent } from '@engine/game-server';
8+
import { TaskExecutor } from '@engine/world/action';
9+
import { widgetButtonIds } from '../skills/smithing/smelting-constants';
10+
import { magic_on_npcActionHandler, Magic_on_NPCActionHook, Magic_on_NPCAction } from '../../game-engine/world/action/magic-on-npc.action';
11+
12+
const buttonIds: number[] = [
13+
0, // Home Teleport
14+
];
15+
16+
function attack_target(player: Player, elapsedTicks: number): boolean {
17+
console.log("attacking?");
18+
return true;
19+
}
20+
21+
const spells = ["Wind Strike","Confuse", "Water Strike","unknown?", "Earth Strike"];
22+
export const activate = (task: TaskExecutor<Magic_on_NPCAction>, elapsedTicks: number = 0) => {
23+
const {
24+
npc,
25+
player,
26+
widgetId,
27+
buttonId
28+
} = task.actionData;
29+
30+
console.info(`${player.username} smites ${npc.name} with ${spells[buttonId]}`);
31+
};
32+
33+
export default {
34+
pluginId: 'rs:magic',
35+
hooks:
36+
{
37+
type: 'magic_on_npc',
38+
widgetId: 192,
39+
buttonIds: buttonIds,
40+
task: {
41+
activate,
42+
interval: 0
43+
}
44+
} as Magic_on_NPCActionHook
45+
46+
};

0 commit comments

Comments
 (0)