|
1 | 1 | # Task system
|
2 | 2 |
|
3 |
| -This is a tick-based task system which allows for extensions of the `Task` class to be executed. |
| 3 | +The task system allows you to write content which will be executed on the tick cycles of the server. |
4 | 4 |
|
5 |
| -Tasks can be executed after a delay, or immediately. They can also be set to repeat indefinitely or continue. |
| 5 | +You can configure a task to execute every `n` ticks (minimum of `1` for every tick), and you can also choose a number of other behaviours, such as whether the task should execute immediately or after a delay, as well as set to repeat indefinitely. |
6 | 6 |
|
7 | 7 | ## Scheduling a task
|
8 | 8 |
|
9 |
| -You can schedule a task by registering it with the scheduler. |
| 9 | +You can schedule a task by registering it with a `TaskScheduler`. |
10 | 10 |
|
11 | 11 | The task in the example of below runs with an interval of `2`, i.e. it will be executed every 2 ticks.
|
12 | 12 |
|
13 | 13 | ```ts
|
14 |
| -this.taskScheduler.addTask(new class extends Task { |
| 14 | +class MyTask extends Task { |
15 | 15 | public constructor() {
|
16 | 16 | super({ interval: 2 });
|
17 | 17 | }
|
18 | 18 |
|
19 | 19 | public execute(): void {
|
20 |
| - sendGlobalMessage('2 ticks'); |
| 20 | + console.log('2 ticks'); |
21 | 21 | }
|
22 |
| -}); |
| 22 | +} |
| 23 | + |
| 24 | +const scheduler = new TaskScheduler(); |
| 25 | + |
| 26 | +scheduler.addTask(new MyTask()); |
| 27 | + |
| 28 | +scheduler.tick(); |
| 29 | +scheduler.tick(); // '2 ticks' |
| 30 | +``` |
| 31 | + |
| 32 | +Every two times that `scheduler.tick()` is called, it will run the `execute` function of your task. |
| 33 | + |
| 34 | +## Task configuration |
| 35 | + |
| 36 | +You can pass a `TaskConfig` object to the `Task` constructor in order to configure various aspects of your task. |
| 37 | + |
| 38 | +### Timing |
| 39 | + |
| 40 | +The most simple configuration option for a `Task` is the `interval` option. Your task will be executed every `interval` amount of ticks. |
| 41 | + |
| 42 | +```ts |
| 43 | +/** |
| 44 | +* The number of ticks between each execution of the task. |
| 45 | +*/ |
| 46 | +interval: number; |
| 47 | +``` |
| 48 | + |
| 49 | +For example, with an interval of `1`, your task will run every tick. The default value is `1`. |
| 50 | + |
| 51 | +### Immediate execution |
| 52 | + |
| 53 | +You can configure your task to execute immediately with the `immediate` option. |
| 54 | + |
| 55 | +```ts |
| 56 | +/** |
| 57 | +* Should the task be executed on the first tick after it is added? |
| 58 | +*/ |
| 59 | +immediate: boolean; |
| 60 | +``` |
| 61 | + |
| 62 | +For example, if `immediate` is `true` and `interval` is `5`, your task will run on the 1st and 6th ticks (and so on). |
| 63 | + |
| 64 | +### Repeating |
| 65 | + |
| 66 | +You can use the `repeat` option to tell your task to run forever. |
| 67 | + |
| 68 | +```ts |
| 69 | +/** |
| 70 | +* Should the task be repeated indefinitely? |
| 71 | +*/ |
| 72 | +repeat: boolean; |
| 73 | +``` |
| 74 | + |
| 75 | +You can use `this.stop()` inside the task to stop it from repeating further. |
| 76 | + |
| 77 | +### Stacking |
| 78 | + |
| 79 | +The `stackType` and `stackGroup` properties allow you to control how your task interacts with other, similar tasks. |
| 80 | + |
| 81 | +```ts |
| 82 | +/** |
| 83 | +* How the task should be stacked with other tasks of the same stack group. |
| 84 | +*/ |
| 85 | +stackType: TaskStackType; |
| 86 | + |
| 87 | +/** |
| 88 | +* The stack group for this task. |
| 89 | +*/ |
| 90 | +stackGroup: string; |
23 | 91 | ```
|
24 | 92 |
|
25 |
| -Every two times that `taskScheduler.tick()` is called, it will run the `execute` function of your task. |
| 93 | +When `stackType` is set to `TaskStackType.NEVER`, other tasks with the same `stackGroup` will be stopped when your task is enqueued. A `stackType` of `TaskStackType.STACK` will allow your task to run with others of the same group. |
| 94 | + |
| 95 | +The default type is `TaskStackType.STACK` and the group is `TaskStackGroup.ACTION` (`'action'`) |
| 96 | + |
| 97 | +## Task Subtypes |
| 98 | + |
| 99 | +Rather than extending `Task`, there are a number of subclasses you can extend which will give you some syntactic sugar around common functionality. |
26 | 100 |
|
27 |
| -# Implementing this into RuneJS |
| 101 | +- `ActorTask` |
28 | 102 |
|
29 |
| -## Task System |
| 103 | + This is the base task to be performed by an `Actor`. It will automatically listen to the actor's walking queue, and stop the task if it has a `breakType` of `ON_MOVE`. |
30 | 104 |
|
31 |
| -- Tick-based task scheduler :heavy_check_mark: |
32 |
| -- Delay task :heavy_check_mark: |
33 |
| -- Repeating task :heavy_check_mark: |
34 |
| -- Schedule tasks on world :heavy_check_mark: |
35 |
| -- Schedule tasks on players/npcs :heavy_check_mark: |
36 |
| -- Task stacking :heavy_check_mark: |
37 |
| -- Task breaking on walking :heavy_check_mark: |
38 |
| -- Task delay until arriving :heavy_check_mark: |
| 105 | +- `ActorWalkToTask` |
39 | 106 |
|
40 |
| -### Subtasks |
| 107 | + This task will make an actor walk to a `Position` or `LandscapeObject` and will expose the `atDestination` property for your extended task to query. You can then begin executing your task logic. |
41 | 108 |
|
42 |
| -#### Actor |
| 109 | +- `ActorLandscapeObjectInteractionTask` |
43 | 110 |
|
44 |
| -- Actor task :heavy_check_mark: |
45 |
| - - Handle break on move :heavy_check_mark: |
46 |
| -- Actor to actor interaction task :think: :x: |
47 |
| - - Handle walkto :x: |
48 |
| - - Keep track of interaction distance and stop if exceeded :x: |
49 |
| -- Actor to world item interaction task :heavy_check_mark: |
50 |
| - - Handle walkto :heavy_check_mark: |
51 |
| - - Keep track of interaction distance and stop if exceeded :heavy_check_mark: |
52 |
| -- Actor to object interaction task :think: :x: |
53 |
| - - (maybe need some generic Actor to entity interaction task) |
54 |
| - - Handle walkto :x: |
55 |
| - - Keep track of interaction distance and stop if exceeded :x: |
| 111 | + This task extends `ActorWalkToTask` and will make an actor walk to a given `LandscapeObject`, before exposing the `landscapeObject` property for your task to use. |
56 | 112 |
|
57 |
| -#### World |
| 113 | +- `ActorWorldItemInteractionTask` |
58 | 114 |
|
59 |
| -- World task :yellow_square: |
60 |
| -- Spawn game object task :x: |
61 |
| -- Remove game object task :x: |
62 |
| -- Spawn world item task :x: |
63 |
| -- Remove world item task :x: |
| 115 | + This task extends `ActorWalkToTask` and will make an actor walk to a given `WorldItem`, before exposing the `worldItem` property for your task to use. |
64 | 116 |
|
65 |
| -### Content |
| 117 | +# Future improvements |
| 118 | + |
| 119 | +- Stalling executions for certain tasks when interface is open |
| 120 | + - should we create a `PlayerTask` to contain this behaviour? The `breakType` behaviour could be moved to this base, rather than `ActorTask` |
| 121 | + |
| 122 | +- Consider refactoring this system to use functional programming patterns. Composition should be favoured over inheritance generally, and there are some examples of future tasks which may be easier if we could compose tasks from building blocks. Consider the implementation of some task which requires both a `LandscapeObject` and a `WorldItem` - we currently would need to create some custom task which borrowed behaviour from the `ActorLandscapeObjectInteractionTask` and `ActorWorldItemInteractionTask`. TypeScript mixins could be useful here. |
| 123 | + |
| 124 | +# Content requiring conversion to task system |
66 | 125 |
|
67 | 126 | Highest priority is to convert pieces of content which make use of the old `task` system. These are:
|
68 | 127 |
|
69 |
| -- Magic attack :x: |
70 |
| -- Magic teleports :x: |
71 |
| -- Prayer :yellow_square: |
72 |
| - - ensure that one-tick pray flicking works. Speak to @jameskmonger if you want to implement this and need guidance |
73 |
| -- Combat :x: |
74 |
| - - this one is quite broken and may not be so easy to port across |
75 |
| -- Forging (smithing) :x: |
76 |
| -- Woodcutting :yellow_square: |
77 |
| - - Time to cut :heavy_check_mark: |
78 |
| - - Replace tree with treestump :yellow_square: |
79 |
| - - Replace treestump with tree :yellow_square: |
| 128 | +- Magic attack |
| 129 | +- Magic teleports |
| 130 | +- Prayer |
| 131 | +- Combat |
| 132 | +- Forging (smithing) |
| 133 | +- Woodcutting |
80 | 134 |
|
81 | 135 | The following areas will make interesting use of the task system and would serve as a good demonstration:
|
82 | 136 |
|
83 |
| -- Health regen :x: |
84 |
| -- NPC movement :x: |
85 |
| -- Firemaking :yellow_square: |
86 |
| - - logs in inventory :heavy_check_mark: |
87 |
| - - logs on ground :heavy_check_mark: |
88 |
| - - all log types :yellow_square: |
89 |
| - - spawn ashes :x: |
90 |
| - |
91 |
| -Also any content @gruckion is working on will be using the new Task system to aid with development of the API |
| 137 | +- Health regen |
| 138 | +- NPC movement |
| 139 | +- Firemaking |
0 commit comments