1local wildGrowth = {1499, 11099} -- wild growth destroyable by machete
2local jungleGrass = { -- grass destroyable by machete
3 [2782] = 2781,
4 [3985] = 3984,
5 [19433] = 19431
6}
7local groundIds = {354, 355} -- pick usable ground
8local sandIds = {231, 9059} -- desert sand
9local holeId = { -- usable rope holes, for rope spots see global.lua
10 294, 369, 370, 383, 392, 408, 409, 410, 427, 428, 429, 430, 462, 469, 470, 482,
11 484, 485, 489, 924, 1369, 3135, 3136, 4835, 4837, 7933, 7938, 8170, 8249, 8250,
12 8251, 8252, 8254, 8255, 8256, 8276, 8277, 8279, 8281, 8284, 8285, 8286, 8323,
13 8567, 8585, 8595, 8596, 8972, 9606, 9625, 13190, 14461, 19519, 21536, 23713,
14 26020
15}
16local holes = {468, 481, 483, 7932, 23712} -- holes opened by shovel
17local fruits = {2673, 2674, 2675, 2676, 2677, 2678, 2679, 2680, 2681, 2682, 2684, 2685, 5097, 8839, 8840, 8841} -- fruits to make decorated cake with knife
18
19function destroyItem(player, target, toPosition)
20 if type(target) ~= "userdata" or not target:isItem() then
21 return false
22 end
23
24 if target:hasAttribute(ITEM_ATTRIBUTE_UNIQUEID) or target:hasAttribute(ITEM_ATTRIBUTE_ACTIONID) then
25 return false
26 end
27
28 if toPosition.x == CONTAINER_POSITION then
29 player:sendCancelMessage(RETURNVALUE_NOTPOSSIBLE)
30 return true
31 end
32
33 local destroyId = ItemType(target.itemid):getDestroyId()
34 if destroyId == 0 then
35 return false
36 end
37
38 if math.random(7) == 1 then
39 local item = Game.createItem(destroyId, 1, toPosition)
40 if item then
41 item:decay()
42 end
43
44 -- Move items outside the container
45 if target:isContainer() then
46 for i = target:getSize() - 1, 0, -1 do
47 local containerItem = target:getItem(i)
48 if containerItem then
49 containerItem:moveTo(toPosition)
50 end
51 end
52 end
53
54 target:remove(1)
55 end
56
57 toPosition:sendMagicEffect(CONST_ME_POFF)
58 return true
59end
60
61function onUseMachete(player, item, fromPosition, target, toPosition, isHotkey)
62 local targetId = target.itemid
63 if not targetId then
64 return true
65 end
66
67 if table.contains(wildGrowth, targetId) then
68 toPosition:sendMagicEffect(CONST_ME_POFF)
69 target:remove()
70 return true
71 end
72
73 local grass = jungleGrass[targetId]
74 if grass then
75 target:transform(grass)
76 target:decay()
77 player:addAchievementProgress("Nothing Can Stop Me", 100)
78 return true
79 end
80
81 return destroyItem(player, target, toPosition)
82end
83
84function onUsePick(player, item, fromPosition, target, toPosition, isHotkey)
85 if target.itemid == 11227 then -- shiny stone refining
86 local chance = math.random(1, 100)
87 if chance == 1 then
88 player:addItem(ITEM_CRYSTAL_COIN) -- 1% chance of getting crystal coin
89 elseif chance <= 6 then
90 player:addItem(ITEM_GOLD_COIN) -- 5% chance of getting gold coin
91 elseif chance <= 51 then
92 player:addItem(ITEM_PLATINUM_COIN) -- 45% chance of getting platinum coin
93 else
94 player:addItem(2145) -- 49% chance of getting small diamond
95 end
96 player:addAchievementProgress("Petrologist", 100)
97 target:getPosition():sendMagicEffect(CONST_ME_BLOCKHIT)
98 target:remove(1)
99 return true
100 end
101
102 local tile = Tile(toPosition)
103 if not tile then
104 return false
105 end
106
107 local ground = tile:getGround()
108 if not ground then
109 return false
110 end
111
112 if table.contains(groundIds, ground.itemid) and ground.actionid == actionIds.pickHole then
113 ground:transform(392)
114 ground:decay()
115
116 toPosition.z = toPosition.z + 1
117 tile:relocateTo(toPosition)
118 end
119
120 return true
121end
122
123function onUseRope(player, item, fromPosition, target, toPosition, isHotkey)
124 local tile = Tile(toPosition)
125 if not tile then
126 return false
127 end
128
129 if table.contains(ropeSpots, tile:getGround():getId()) or tile:getItemById(14435) then
130 if Tile(toPosition:moveUpstairs()):hasFlag(TILESTATE_PROTECTIONZONE) and player:isPzLocked() then
131 player:sendCancelMessage(RETURNVALUE_PLAYERISPZLOCKED)
132 return true
133 end
134 player:teleportTo(toPosition, false)
135 return true
136 elseif table.contains(holeId, target.itemid) then
137 toPosition.z = toPosition.z + 1
138 tile = Tile(toPosition)
139 if tile then
140 local thing = tile:getTopVisibleThing()
141 if thing:isPlayer() then
142 if Tile(toPosition:moveUpstairs()):hasFlag(TILESTATE_PROTECTIONZONE) and thing:isPzLocked() then
143 return false
144 end
145 return thing:teleportTo(toPosition, false)
146 end
147 if thing:isItem() and thing:getType():isMovable() then
148 return thing:moveTo(toPosition:moveUpstairs())
149 end
150 end
151 player:sendCancelMessage(RETURNVALUE_NOTPOSSIBLE)
152 return true
153 end
154 return false
155end
156
157function onUseShovel(player, item, fromPosition, target, toPosition, isHotkey)
158 local tile = Tile(toPosition)
159 if not tile then
160 return false
161 end
162
163 local ground = tile:getGround()
164 if not ground then
165 return false
166 end
167
168 local groundId = ground:getId()
169 if table.contains(holes, groundId) then
170 ground:transform(groundId + 1)
171 ground:decay()
172
173 toPosition.z = toPosition.z + 1
174 tile:relocateTo(toPosition)
175 player:addAchievementProgress("The Undertaker", 500)
176 elseif target.itemid == 20230 then -- swamp digging
177 if (player:getStorageValue(PlayerStorageKeys.swampDigging)) <= os.time() then
178 local chance = math.random(100)
179 if chance >= 1 and chance <= 42 then
180 player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "You dug up a dead snake.")
181 player:addItem(3077)
182 elseif chance >= 43 and chance <= 79 then
183 player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "You dug up a small diamond.")
184 player:addItem(2145)
185 elseif chance >= 80 then
186 player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "You dug up a leech.")
187 player:addItem(20138)
188 end
189 player:setStorageValue(PlayerStorageKeys.swampDigging, os.time() + 7 * 24 * 60 * 60)
190 player:getPosition():sendMagicEffect(CONST_ME_GREEN_RINGS)
191 end
192 elseif table.contains(sandIds, groundId) then
193 local randomValue = math.random(1, 100)
194 if target.actionid == actionIds.sandHole and randomValue <= 20 then
195 ground:transform(489)
196 ground:decay()
197 elseif randomValue == 1 then
198 Game.createItem(2159, 1, toPosition)
199 player:addAchievementProgress("Gold Digger", 100)
200 elseif randomValue > 95 then
201 Game.createMonster("Scarab", toPosition)
202 end
203 toPosition:sendMagicEffect(CONST_ME_POFF)
204 else
205 return false
206 end
207
208 return true
209end
210
211function onUseScythe(player, item, fromPosition, target, toPosition, isHotkey)
212 if not table.contains({2550, 10513}, item.itemid) then
213 return false
214 end
215
216 if target.itemid == 2739 then -- wheat
217 target:transform(2737)
218 target:decay()
219 Game.createItem(2694, 1, toPosition) -- bunch of wheat
220 player:addAchievementProgress("Happy Farmer", 200)
221 return true
222 end
223 if target.itemid == 5465 then -- burning sugar cane
224 target:transform(5464)
225 target:decay()
226 Game.createItem(5467, 1, toPosition) -- bunch of sugar cane
227 player:addAchievementProgress("Natural Sweetener", 50)
228 return true
229 end
230 return destroyItem(player, target, toPosition)
231end
232
233function onUseCrowbar(player, item, fromPosition, target, toPosition, isHotkey)
234 if not table.contains({2416, 10515}, item.itemid) then
235 return false
236 end
237
238 return destroyItem(player, target, toPosition)
239end
240
241function onUseKitchenKnife(player, item, fromPosition, target, toPosition, isHotkey)
242 if not table.contains({2566, 10511, 10515}, item.itemid) then
243 return false
244 end
245
246 if table.contains(fruits, target.itemid) and player:removeItem(6278, 1) then
247 target:remove(1)
248 player:addItem(6279, 1)
249 player:getPosition():sendMagicEffect(CONST_ME_MAGIC_GREEN)
250 return true
251 end
252
253 return false
254end
255