Sign in
mod

Bows, slings and spears

by Potluck · version 1 · Checked for all ages

A bow shoots arrows, a sling throws cobblestone and a spear flies far and hard. Hold one and Interact looking where you'd like it to go. They hit creatures from afar (a Gloomling pops at an arrow), and arrows that miss can sometimes be found again; a thrown spear comes back to you. Make them at a workbench.

1--!strict
2-- Bows, slings and spears' own tests: `potluck-server --test=ranged`.
3
4local test = require("@potluck/test@0.9")
5local world = require("@potluck/world@0.22")
6
7local AHEAD = vector.create(1, 0.2, 0)
8
9test.case("a bow shoots an arrow you carry, and waits a moment for the next", function()
10 local p = test.player("Ada", { survival = true, carrying = { Bow = 1, Arrow = 3 } })
11 test.hold(p, "Bow")
12 test.use_item(p, AHEAD)
13 test.use_item(p, AHEAD)
14 test.same(test.heard("twang"), 1, "one shot, the second too soon")
15 test.same(world.has(p, "Arrow"), 2, "the arrow it shot")
16 test.wait(1)
17 test.use_item(p, AHEAD)
18 test.same(test.heard("twang"), 2, "and the next")
19end)
20
21test.case("with nothing to shoot, it says so", function()
22 local p = test.player("Ada", { survival = true, carrying = { Bow = 1, Sling = 1 } })
23 test.hold(p, "Bow")
24 test.use_item(p, AHEAD)
25 test.same(test.heard("twang"), 0, "no arrows, no shot")
26 test.check(string.find(test.shown(p, "ranged") or "", "You've no arrows to shoot.", 1, true) ~= nil, test.shown(p, "ranged") or "")
27 test.hold(p, "Sling")
28 test.use_item(p, AHEAD)
29 test.same(test.heard("whirr"), 0, "no stones either")
30end)
31
32test.case("a sling throws cobblestone", function()
33 local p = test.player("Ada", { survival = true, carrying = { Sling = 1, Cobblestone = 2 } })
34 test.hold(p, "Sling")
35 test.use_item(p, AHEAD)
36 test.same(test.heard("whirr"), 1)
37 test.same(world.has(p, "Cobblestone"), 1, "the stone it threw")
38end)
39
40test.case("a spear flies one at a time and stays yours", function()
41 local p = test.player("Ada", { survival = true, carrying = { Spear = 1 } })
42 test.hold(p, "Spear")
43 test.use_item(p, vector.create(0, 1, 0))
44 test.wait(0.8)
45 test.use_item(p, AHEAD)
46 test.same(test.heard("whoosh"), 1, "not while the first is in the air")
47 test.same(world.has(p, "Spear"), 1, "still yours")
48 test.wait(6)
49 test.use_item(p, AHEAD)
50 test.same(test.heard("whoosh"), 2, "once it's down")
51end)
52
53test.case("in creative a bow needs no arrows", function()
54 local p = test.player("Bo")
55 test.hold(p, "Bow")
56 test.use_item(p, AHEAD)
57 test.same(test.heard("twang"), 1)
58end)
59
60-- two players up in the air, well apart, the first with a bow
61local function apart(): (any, any)
62 local p = test.player("Ada", { survival = true, at = vector.create(200.5, 70, 200.5), carrying = { Bow = 1, Arrow = 3 } })
63 local q = test.player("Bo", { survival = true, at = vector.create(206.5, 70, 200.5) })
64 test.hold(p, "Bow")
65 return p, q
66end
67
68test.case("an arrow passes another player by, unless the rules let players hurt each other", function()
69 local p, q = apart()
70 test.use_item(p, vector.create(1, 0, 0))
71 test.wait(1)
72 test.same(test.health(q), 20, "no harm done")
73 test.rule("pvp", true)
74 test.use_item(p, vector.create(1, 0, 0))
75 test.wait(1)
76 test.same(test.health(q), 18, "an arrow's power")
77 test.same(test.health(p), 20, "and never its archer")
78end)
79
80test.case("a Thrifty sling now and then keeps its stone", function()
81 local p = test.player("Ada", { survival = true, carrying = { Sling = 1, Cobblestone = 30 } })
82 world.charm(p, "Sling", { "Thrifty" })
83 test.hold(p, "Sling")
84 for _ = 1, 30 do
85 test.use_item(p, AHEAD)
86 test.wait(0.55)
87 end
88 test.same(test.heard("whirr"), 30, "thirty throws")
89 local left = world.has(p, "Cobblestone")
90 test.check(left > 0 and left < 30, "some stones kept: " .. left)
91end)
92