Skip to content

Commit 3f855cb

Browse files
committed
refactor: add docs to plugin, task and canCut
add comments docs in task
1 parent acfc3b1 commit 3f855cb

File tree

3 files changed

+92
-44
lines changed

3 files changed

+92
-44
lines changed

src/plugins/skills/woodcutting/chance.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
import { randomBetween } from '@engine/util';
22
import { IHarvestable } from '@engine/world/config';
33

4+
/**
5+
* Roll a random number between 0 and 255 and compare it to the percent needed to cut the tree.
6+
*
7+
* @param tree The tree to cut
8+
* @param toolLevel The level of the axe being used
9+
* @param woodcuttingLevel The player's woodcutting level
10+
*
11+
* @returns True if the tree was successfully cut, false otherwise
12+
*/
413
export const canCut = (
514
tree: IHarvestable,
615
toolLevel: number,

src/plugins/skills/woodcutting/index.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,17 @@ import {
44
import { getTreeIds } from '@engine/world/config/harvestable-object';
55
import { runWoodcuttingTask } from './woodcutting-task';
66

7-
7+
/**
8+
* Woodcutting plugin
9+
*
10+
* This uses the task system to schedule actions.
11+
*/
812
export default {
913
pluginId: 'rs:woodcutting',
1014
hooks: [
15+
/**
16+
* "Chop down" / "chop" object interaction hook.
17+
*/
1118
{
1219
type: 'object_interaction',
1320
options: [ 'chop down', 'chop' ],

src/plugins/skills/woodcutting/woodcutting-task.ts

Lines changed: 75 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,24 @@ import { LandscapeObject } from '@runejs/filestore';
1515
import { logger } from '@runejs/common';
1616

1717
class WoodcuttingTask extends ActorLandscapeObjectInteractionTask<Player> {
18+
/**
19+
* The tree being cut down.
20+
*/
1821
private treeInfo: IHarvestable;
22+
23+
/**
24+
* The number of ticks that `execute` has been called inside this task.
25+
*/
1926
private elapsedTicks = 0;
2027

28+
/**
29+
* Create a new woodcutting task.
30+
*
31+
* @param player The player that is attempting to cut down the tree.
32+
* @param landscapeObject The object that represents the tree.
33+
* @param sizeX The size of the tree in x axis.
34+
* @param sizeY The size of the tree in y axis.
35+
*/
2136
constructor(
2237
player: Player,
2338
landscapeObject: LandscapeObject,
@@ -43,6 +58,14 @@ class WoodcuttingTask extends ActorLandscapeObjectInteractionTask<Player> {
4358
}
4459
}
4560

61+
/**
62+
* Execute the main woodcutting task loop. This method is called every game tick until the task is completed.
63+
*
64+
* As this task extends {@link ActorLandscapeObjectInteractionTask}, it's important that the
65+
* `super.execute` method is called at the start of this method.
66+
*
67+
* The base `execute` performs a number of checks that allow this task to function healthily.
68+
*/
4669
public execute(): void {
4770
super.execute();
4871

@@ -67,59 +90,68 @@ class WoodcuttingTask extends ActorLandscapeObjectInteractionTask<Player> {
6790
return;
6891
}
6992

70-
if(taskIteration % 3 === 0) {
71-
72-
let toolLevel = tool.level - 1;
73-
if(tool.itemId === 1349 || tool.itemId === 1267) {
74-
toolLevel = 2;
75-
}
76-
77-
const succeeds = canCut(this.treeInfo, toolLevel, this.actor.skills.woodcutting.level);
78-
if(succeeds) {
79-
const targetName: string = findItem(this.treeInfo.itemId).name.toLowerCase();
80-
81-
if(this.actor.inventory.hasSpace()) {
82-
const itemToAdd = this.treeInfo.itemId;
83-
const roll = randomBetween(1, 256);
84-
85-
if(roll === 1) { // Bird nest chance
86-
this.actor.sendMessage(colorText(`A bird's nest falls out of the tree.`, colors.red));
87-
activeWorld.globalInstance.spawnWorldItem(rollBirdsNestType(), this.actor.position,
88-
{ owner: this.actor || null, expires: 300 });
89-
} else { // Standard log chopper
90-
this.actor.sendMessage(`You manage to chop some ${targetName}.`);
91-
this.actor.giveItem(itemToAdd);
92-
}
93-
94-
this.actor.skills.woodcutting.addExp(this.treeInfo.experience);
95-
96-
if(randomBetween(0, 100) <= this.treeInfo.break) {
97-
this.actor.playSound(soundIds.oreDepeleted);
98-
this.actor.instance.replaceGameObject(this.treeInfo.objects.get(this.landscapeObject.objectId),
99-
this.landscapeObject, randomBetween(this.treeInfo.respawnLow, this.treeInfo.respawnHigh));
100-
this.stop();
101-
return;
102-
}
103-
} else {
104-
this.actor.sendMessage(`Your inventory is too full to hold any more ${targetName}.`, true);
105-
this.actor.playSound(soundIds.inventoryFull);
106-
this.stop();
107-
return;
108-
}
109-
}
110-
} else {
93+
// play a random axe sound at the correct time
94+
if(taskIteration % 3 !== 0) {
11195
if(taskIteration % 1 === 0) {
11296
const randomSoundIdx = Math.floor(Math.random() * soundIds.axeSwing.length);
11397
this.actor.playSound(soundIds.axeSwing[randomSoundIdx], 7, 0);
11498
}
99+
return;
115100
}
116101

117-
if(taskIteration % 3 === 0) {
118-
this.actor.playAnimation(tool.animation);
102+
// Get tool level, and set it to 2 if the tool is an iron hatchet or iron pickaxe axe
103+
// TODO why is this set to 2? Was ported from the old code
104+
let toolLevel = tool.level - 1;
105+
if(tool.itemId === 1349 || tool.itemId === 1267) {
106+
toolLevel = 2;
107+
}
108+
109+
// roll for success
110+
const succeeds = canCut(this.treeInfo, toolLevel, this.actor.skills.woodcutting.level);
111+
if(!succeeds) {
112+
return;
119113
}
120114

115+
const targetName: string = findItem(this.treeInfo.itemId).name.toLowerCase();
116+
117+
// if player doesn't have space in inventory, stop the task
118+
if(!this.actor.inventory.hasSpace()) {
119+
this.actor.sendMessage(`Your inventory is too full to hold any more ${targetName}.`, true);
120+
this.actor.playSound(soundIds.inventoryFull);
121+
this.stop();
122+
return;
123+
}
124+
125+
const itemToAdd = this.treeInfo.itemId;
126+
const roll = randomBetween(1, 256);
127+
// roll for bird nest chance
128+
if(roll === 1) {
129+
this.actor.sendMessage(colorText(`A bird's nest falls out of the tree.`, colors.red));
130+
activeWorld.globalInstance.spawnWorldItem(rollBirdsNestType(), this.actor.position,
131+
{ owner: this.actor || null, expires: 300 });
132+
} else { // Standard log chopper
133+
this.actor.sendMessage(`You manage to chop some ${targetName}.`);
134+
this.actor.giveItem(itemToAdd);
135+
}
136+
137+
this.actor.skills.woodcutting.addExp(this.treeInfo.experience);
138+
139+
// check if the tree should be broken
140+
if(randomBetween(0, 100) <= this.treeInfo.break) {
141+
this.actor.playSound(soundIds.oreDepeleted);
142+
this.actor.instance.replaceGameObject(this.treeInfo.objects.get(this.landscapeObject.objectId),
143+
this.landscapeObject, randomBetween(this.treeInfo.respawnLow, this.treeInfo.respawnHigh));
144+
this.stop();
145+
return;
146+
}
147+
148+
this.actor.playAnimation(tool.animation);
149+
121150
}
122151

152+
/**
153+
* This method is called when the task stops.
154+
*/
123155
public onStop(): void {
124156
super.onStop();
125157

0 commit comments

Comments
 (0)