@@ -15,9 +15,24 @@ import { LandscapeObject } from '@runejs/filestore';
15
15
import { logger } from '@runejs/common' ;
16
16
17
17
class WoodcuttingTask extends ActorLandscapeObjectInteractionTask < Player > {
18
+ /**
19
+ * The tree being cut down.
20
+ */
18
21
private treeInfo : IHarvestable ;
22
+
23
+ /**
24
+ * The number of ticks that `execute` has been called inside this task.
25
+ */
19
26
private elapsedTicks = 0 ;
20
27
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
+ */
21
36
constructor (
22
37
player : Player ,
23
38
landscapeObject : LandscapeObject ,
@@ -43,6 +58,14 @@ class WoodcuttingTask extends ActorLandscapeObjectInteractionTask<Player> {
43
58
}
44
59
}
45
60
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
+ */
46
69
public execute ( ) : void {
47
70
super . execute ( ) ;
48
71
@@ -67,59 +90,68 @@ class WoodcuttingTask extends ActorLandscapeObjectInteractionTask<Player> {
67
90
return ;
68
91
}
69
92
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 ) {
111
95
if ( taskIteration % 1 === 0 ) {
112
96
const randomSoundIdx = Math . floor ( Math . random ( ) * soundIds . axeSwing . length ) ;
113
97
this . actor . playSound ( soundIds . axeSwing [ randomSoundIdx ] , 7 , 0 ) ;
114
98
}
99
+ return ;
115
100
}
116
101
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 ;
119
113
}
120
114
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
+
121
150
}
122
151
152
+ /**
153
+ * This method is called when the task stops.
154
+ */
123
155
public onStop ( ) : void {
124
156
super . onStop ( ) ;
125
157
0 commit comments