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. Hold one and Interact, and it's shot the way
3-- you look (world.on("item_used"), world.aims and world.shoot, world
4-- 0.19): a bow shoots an Arrow you carry, a sling a Cobblestone, and a
5-- spear flies itself, one at a time, and is back in your hand when it
6-- lands. A creature it hits hears `hit` with the shot, whose power says
7-- how hard (a Gloomling pops at 2). Arrows that land on a block are
8-- sometimes found again. WEAPONS below says how each shoots, so a remix
9-- can change them or add its own. In creative there's no need to carry
10-- arrows or stones, and a Thrifty bow or sling (the Charms creation's
11-- charm) now and then shoots without using one up. Where the world's rules let players hurt each other
12-- (world.rule("pvp"), world 0.28), shots hit other players too, as hard
13-- as their power says, less what their armor keeps off.
14
15local world = require("@potluck/world@0.28")
16local tweak = require("@potluck/tweak")
17local ui = require("@potluck/ui@0.2")
18local sound = require("@potluck/sound@0.2")
19
20local knobs = tweak {
21 power = { 1, min = 0.5, max = 3, label = "How hard shots hit", order = 1 },
22 found = { 0.5, min = 0, max = 1, label = "Share of arrows found after a miss", order = 2 },
23 hint = { true, label = "Say how to shoot", order = 3 },
24 thrifty = { 0.33, min = 0, max = 1, label = "How often a Thrifty bow or sling keeps its ammo", order = 4 },
25}
26
27-- What each shoots (nil for itself), how it looks, how fast it flies
28-- (blocks a second) and falls (0 not at all, 1 like a stone), how hard it
29-- hits, how long till the next, and its sound.
30type Weapon = { ammo: string?, look: "arrow" | "stone" | "spear", speed: number, drop: number, power: number, wait: number, sound: string }
31local WEAPONS: { [string]: Weapon } = {
32 Bow = { ammo = "Arrow", look = "arrow", speed = 38, drop = 0.45, power = 2, wait = 0.7, sound = "twang" },
33 Sling = { ammo = "Cobblestone", look = "stone", speed = 24, drop = 1, power = 1, wait = 0.5, sound = "whirr" },
34 Spear = { look = "spear", speed = 24, drop = 0.6, power = 3, wait = 0.6, sound = "whoosh" },
35}
36
37-- each is aimed wherever it points, at a block or a creature in reach too
38for name in WEAPONS do
39 world.aims(name)
40end
41
42-- one line above the hotbar for a few seconds, for each player
43local shown: { [number]: number } = {}
44local function tell(player, text: string)
45 ui.show(player, "ranged", text, { at = "bottom" })
46 local mine = (shown[player.id] or 0) + 1
47 shown[player.id] = mine
48 world.after(3, function()
49 if shown[player.id] == mine then
50 shown[player.id] = nil
51 ui.hide(player, "ranged")
52 end
53 end)
54end
55
56-- when each player can shoot again, and whose spears are in the air
57local ready: { [number]: number } = {}
58local spears: { [number]: boolean } = {}
59
60-- whether a player is still here
61local function here(id: number): boolean
62 for _, p in world.players() do
63 if p.id == id then
64 return true
65 end
66 end
67 return false
68end
69
70world.on("item_used", function(player, item: string?, aim: vector)
71 local w = item and WEAPONS[item]
72 if not w then
73 return
74 end
75 local pid = player.id
76 if world.time() < (ready[pid] or 0) or (item == "Spear" and spears[pid]) then
77 return
78 end
79 -- in survival it takes what it shoots; in creative there's plenty
80 local survival = world.pockets(player) ~= nil
81 -- a Thrifty bow or sling (the Charms creation's charm) sometimes keeps it
82 local thrifty = survival and w.ammo ~= nil and world.has(player, w.ammo) > 0
83 and table.find(world.charms(player, item :: string), "Thrifty") ~= nil and world.random() < knobs.thrifty
84 if w.ammo and survival and not thrifty and not world.take(player, w.ammo, 1) then
85 -- "You've no arrows to shoot."
86 local what = string.lower(w.ammo) .. (if w.ammo == "Arrow" then "s" else "")
87 tell(player, "You've no " .. what .. " to shoot.")
88 return
89 end
90 ready[pid] = world.time() + w.wait
91 local ammo = w.ammo
92 -- a Keen bow, sling or spear hits harder (world.might)
93 local opts = { aim = aim, look = w.look, speed = w.speed, drop = w.drop, power = w.power * knobs.power * world.might(player) }
94 local id = world.shoot(player, opts, function(at: vector, block: string?, creature: number?, target)
95 if creature then
96 sound.play("thud", { at = at, volume = 0.7 })
97 elseif target and world.rule("pvp") then
98 world.hurt(target, opts.power)
99 sound.play("thud", { at = at, volume = 0.7 })
100 end
101 if item == "Spear" then
102 spears[pid] = nil
103 elseif ammo == "Arrow" and block and survival and here(pid) and world.random() < knobs.found then
104 world.give(pid, "Arrow", 1)
105 end
106 end)
107 if id and item == "Spear" then
108 spears[pid] = true
109 end
110 sound.play(w.sound, { at = player.position })
111end)
112
113-- a word to each player the first time they hold one
114local told: { [number]: boolean } = {}
115world.every(0.5, function()
116 if not knobs.hint then
117 return
118 end
119 for _, p in world.players() do
120 local held = world.held(p)
121 if held and WEAPONS[held] and not told[p.id] then
122 told[p.id] = true
123 ui.show(p, "ranged_says", "Interact to shoot, the way you look.", { at = "left" })
124 world.after(6, function()
125 ui.hide(p, "ranged_says")
126 end)
127 end
128 end
129end)
130
131world.on("player_left", function(p)
132 ready[p.id] = nil
133 spears[p.id] = nil
134 told[p.id] = nil
135 shown[p.id] = nil
136end)