Skip to content

Commit 8ab3728

Browse files
committed
May I present to you: Smithing
1 parent ca0e768 commit 8ab3728

File tree

1 file changed

+67
-38
lines changed

1 file changed

+67
-38
lines changed

src/plugins/skills/smithing/forging.plugin.ts

Lines changed: 67 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,17 @@ import {
1111
import { Player } from '@engine/world/actor/player/player';
1212
import { findItem } from '@engine/config';
1313
import { TaskExecutor } from '@engine/world/action';
14+
import { Position } from '@engine/world/position';
15+
16+
/**
17+
* The amount of items the player wants to forge.
18+
*/
19+
let wantedAmount = 0;
20+
21+
/**
22+
* The amount of items already forged.
23+
*/
24+
let forgedAmount = 0;
1425

1526
const mapWidgetItemsToFlatArray = (input) => {
1627
const result = [];
@@ -32,20 +43,25 @@ const mapToFlatArray = (input) => {
3243
return results;
3344
};
3445

46+
/**
47+
* Lookup a smithable from just an item id.
48+
* @param itemId
49+
*/
3550
const findSmithableByItemId = (itemId) : Smithable => {
3651
return mapToFlatArray(smithables).find((smithable) => {
3752
return smithable.item.itemId === itemId;
3853
});
3954
};
4055

56+
/**
57+
* Check if the player is able to perform the action.
58+
* @param task
59+
*/
4160
const canActivate = (task: TaskExecutor<ItemInteractionAction>): boolean => {
42-
const { actor, player, actionData, session } = task.getDetails();
43-
61+
const { actor, player, actionData } = task.getDetails();
4462
const itemId = actionData.itemId;
45-
4663
const smithable = findSmithableByItemId(itemId);
4764

48-
4965
// In case the smithable doesn't exist.
5066
if (!smithable) {
5167
return false;
@@ -58,27 +74,26 @@ const canActivate = (task: TaskExecutor<ItemInteractionAction>): boolean => {
5874
return false;
5975
}
6076

61-
const amountInInventory = player.inventory.findAll(smithable.ingredient.itemId).length;
62-
// @todo: Forging: Make a check for sufficient bars in the inventory.
63-
64-
if (!hasIngredients(player, smithable)) {
65-
player.interfaceState.closeAllSlots();
77+
// Check if the player has sufficient materials.
78+
if (!hasMaterials(player, smithable)) {
6679
const bar = findItem(smithable.ingredient.itemId);
6780
player.sendMessage(`You don't have enough ${bar.name}s.`, true);
6881
return false;
6982
}
7083

84+
player.interfaceState.closeAllSlots();
85+
7186
return true;
7287
};
7388

74-
let wantedAmount = 0;
75-
let forgedAmount = 0;
76-
89+
/**
90+
* The actual forging loop.
91+
* @param task
92+
* @param taskIteration
93+
*/
7794
const activate = (task: TaskExecutor<ItemInteractionAction>, taskIteration: number): boolean => {
7895
const { player, actionData } = task.getDetails();
79-
8096
const itemId = actionData.itemId;
81-
8297
const smithable = findSmithableByItemId(itemId);
8398

8499
// How many? Quick and dirty.
@@ -88,44 +103,58 @@ const activate = (task: TaskExecutor<ItemInteractionAction>, taskIteration: numb
88103
case 'make-10' : wantedAmount = 10; break;
89104
}
90105

91-
player.interfaceState.closeAllSlots();
92-
player.playAnimation(898);
106+
for(let m=0; m<wantedAmount; m++) {
107+
player.playAnimation(898);
108+
if(taskIteration % 4 === 0) {
109+
if (!hasMaterials(player, smithable) || wantedAmount === forgedAmount) {
110+
return false;
111+
}
93112

94-
if(taskIteration % 4 === 0 && taskIteration != 0) {
95-
if (!hasIngredients(player, smithable) || wantedAmount === forgedAmount) {
96-
return false;
97-
}
113+
// Remove ingredients
114+
for (let i=0; i<smithable.ingredient.amount; i++) {
115+
player.inventory.removeFirst(smithable.ingredient.itemId);
116+
}
117+
118+
// Add item to inventory
119+
player.inventory.add({
120+
itemId: smithable.item.itemId, amount: smithable.item.amount
121+
});
98122

99-
// Remove ingredients
100-
for (let i=0; i<smithable.ingredient.amount; i++) {
101-
player.inventory.removeFirst(smithable.ingredient.itemId);
102123
player.outgoingPackets.sendUpdateAllWidgetItems(widgets.inventory, player.inventory);
103-
}
124+
player.skills.addExp(Skill.SMITHING, smithable.experience);
104125

105-
// Add item to inventory
106-
for (let i=0; i<smithable.item.amount; i++) {
107-
player.inventory.add(smithable.item.itemId);
126+
forgedAmount++;
127+
return true;
108128
}
109-
110-
// Give the experience
111-
player.skills.addExp(Skill.SMITHING, smithable.experience);
112-
113-
forgedAmount++;
114129
}
115-
};
116130

117-
const onComplete = (task: TaskExecutor<ItemInteractionAction>): void => {
118-
task.actor.stopAnimation();
131+
// Reset the properties, and strap in for the next batch.
132+
if (forgedAmount === wantedAmount) {
133+
forgedAmount = 0;
134+
wantedAmount = 0;
135+
return false;
136+
}
119137
};
120138

121-
const hasIngredients = (player: Player, smithable: Smithable) => {
139+
/**
140+
* Checks if the player has enough materials
141+
* @param player
142+
* @param smithable
143+
*/
144+
const hasMaterials = (player: Player, smithable: Smithable) => {
122145
return smithable.ingredient.amount <= player.inventory.findAll(smithable.ingredient.itemId).length;
123146
};
124147

148+
/**
149+
* Opens the forging interface, and loads the items.
150+
* @param details
151+
*/
125152
const openForgingInterface: itemOnObjectActionHandler = (details) => {
126-
const { player, item } = details;
153+
const { player, item, object } = details;
127154
const amountInInventory = player.inventory.findAll(item).length;
128155

156+
player.face(new Position(object.x, object.y));
157+
129158
// The player does not have a hammer.
130159
if (!player.inventory.has(itemIds.hammer)) {
131160
player.sendMessage(`You need a hammer to work the metal with.`, true);
@@ -163,6 +192,7 @@ export default {
163192
itemIds: [...bars.keys()],
164193
objectIds: anvilIds,
165194
walkTo: true,
195+
166196
cancelOtherActions: true,
167197
handler: openForgingInterface
168198
} as ItemOnObjectActionHook,
@@ -174,7 +204,6 @@ export default {
174204
task: {
175205
canActivate,
176206
activate,
177-
onComplete,
178207
interval: 1
179208
}
180209
} as ItemInteractionActionHook

0 commit comments

Comments
 (0)