Skip to content

Commit 169bcfa

Browse files
committed
docs: add docs around Task configuration and subtypes
docs: add docs around Task configuration docs: add docs on task subtypes docs: update progress tracker docs: update documentation
1 parent 35b02d0 commit 169bcfa

File tree

3 files changed

+152
-59
lines changed

3 files changed

+152
-59
lines changed

src/engine/task/README.md

Lines changed: 106 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,91 +1,139 @@
11
# Task system
22

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.
44

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.
66

77
## Scheduling a task
88

9-
You can schedule a task by registering it with the scheduler.
9+
You can schedule a task by registering it with a `TaskScheduler`.
1010

1111
The task in the example of below runs with an interval of `2`, i.e. it will be executed every 2 ticks.
1212

1313
```ts
14-
this.taskScheduler.addTask(new class extends Task {
14+
class MyTask extends Task {
1515
public constructor() {
1616
super({ interval: 2 });
1717
}
1818

1919
public execute(): void {
20-
sendGlobalMessage('2 ticks');
20+
console.log('2 ticks');
2121
}
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;
2391
```
2492

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.
26100

27-
# Implementing this into RuneJS
101+
- `ActorTask`
28102

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`.
30104

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`
39106

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.
41108

42-
#### Actor
109+
- `ActorLandscapeObjectInteractionTask`
43110

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.
56112

57-
#### World
113+
- `ActorWorldItemInteractionTask`
58114

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.
64116

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
66125

67126
Highest priority is to convert pieces of content which make use of the old `task` system. These are:
68127

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
80134

81135
The following areas will make interesting use of the task system and would serve as a good demonstration:
82136

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

src/engine/task/task.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { TaskBreakType, TaskConfig, TaskStackGroup, TaskStackType } from './types';
22

33
const DEFAULT_TASK_CONFIG: Required<TaskConfig> = {
4-
interval: 0,
4+
interval: 1,
55
stackType: TaskStackType.STACK,
66
stackGroup: TaskStackGroup.ACTION,
77
immediate: false,
@@ -40,12 +40,34 @@ export interface Task {
4040
* @author jameskmonger
4141
*/
4242
export abstract class Task {
43+
/**
44+
* How the task should be stacked with other tasks of the same stack group.
45+
*/
4346
public readonly stackType: TaskStackType;
47+
48+
/**
49+
* The stack group for this task.
50+
*/
4451
public readonly stackGroup: string;
52+
53+
/**
54+
* Conditions under which the task should be broken.
55+
*/
4556
public readonly breakTypes: TaskBreakType[];
4657

58+
/**
59+
* The number of ticks between each execution of the task.
60+
*/
4761
private interval: number;
62+
63+
/**
64+
* The number of ticks remaining before the task is executed.
65+
*/
4866
private ticksRemaining: number;
67+
68+
/**
69+
* Should the task be repeated indefinitely?
70+
*/
4971
private repeat: boolean;
5072

5173
private _isActive = true;

src/engine/task/types.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,33 @@ export enum TaskBreakType {
4949
* @author jameskmonger
5050
*/
5151
export type TaskConfig = Partial<Readonly<{
52+
/**
53+
* How the task should be stacked with other tasks of the same stack group.
54+
*/
5255
stackType: TaskStackType;
56+
57+
/**
58+
* The stack group for this task.
59+
*/
5360
stackGroup: string;
61+
62+
/**
63+
* Conditions under which the task should be broken.
64+
*/
5465
breakTypes: TaskBreakType[];
66+
67+
/**
68+
* The number of ticks between each execution of the task.
69+
*/
5570
interval: number;
71+
72+
/**
73+
* Should the task be executed on the first tick after it is added?
74+
*/
5675
immediate: boolean;
76+
77+
/**
78+
* Should the task be repeated indefinitely?
79+
*/
5780
repeat: boolean;
5881
}>>;

0 commit comments

Comments
 (0)